Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion datafusion/spark/src/function/array/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use arrow::array::{Array, ArrayRef, Int64Builder};
use arrow::datatypes::{DataType, Field, FieldRef};
use datafusion_common::cast::{as_int64_array, as_list_array};
use datafusion_common::utils::ListCoercion;
use datafusion_common::{Result, exec_err, internal_err, utils::take_function_args};
use datafusion_common::{
DataFusionError, Result, exec_err, internal_err, utils::take_function_args,
};
use datafusion_expr::{
ArrayFunctionArgument, ArrayFunctionSignature, ColumnarValue, ReturnFieldArgs,
ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility,
Expand Down Expand Up @@ -94,6 +96,10 @@ impl ScalarUDFImpl for SparkSlice {
&self,
mut func_args: ScalarFunctionArgs,
) -> Result<ColumnarValue> {
if func_args.args[0].data_type() == DataType::Null {
return Ok::<ColumnarValue, DataFusionError>(func_args.args[0].clone());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need of the turbofish here ?

Suggested change
return Ok::<ColumnarValue, DataFusionError>(func_args.args[0].clone());
return Ok(func_args.args[0].clone());

This will also make the new import of DataFusionError not needed

};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
};
}


let array_len = func_args
.args
.iter()
Expand Down Expand Up @@ -170,3 +176,40 @@ fn calculate_start_end(args: &[ArrayRef]) -> Result<(ArrayRef, ArrayRef)> {

Ok((Arc::new(adjusted_start.finish()), Arc::new(end.finish())))
}

#[cfg(test)]
mod tests {
use super::*;
use arrow::array::NullArray;
use arrow::datatypes::DataType::List;
use arrow::datatypes::Field;
use datafusion_common::ScalarValue;

#[test]
fn test_spark_slice_function_when_input_array_is_null() {
let input_args = vec![
ColumnarValue::Array(Arc::new(NullArray::new(1))),
ColumnarValue::Scalar(ScalarValue::Int64(Some(1))),
ColumnarValue::Scalar(ScalarValue::Int64(Some(3))),
];

let args = ScalarFunctionArgs {
args: input_args.to_owned(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
args: input_args.to_owned(),
args: input_args,

arg_fields: vec![Arc::new(Field::new(
"item",
List(FieldRef::new(Field::new("", DataType::Int64, true))),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
List(FieldRef::new(Field::new("", DataType::Int64, true))),
List(FieldRef::new(Field::new("f", DataType::Int64, true))),

It looks weird to use an empty name for a field.

false,
))],
number_rows: 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
number_rows: 0,
number_rows: 1,

to match with the length of NullArray::new(1)

return_field: Arc::new(Field::new(
"item",
List(FieldRef::new(Field::new_list_field(DataType::Int64, true))),
false,
)),
config_options: Arc::new(Default::default()),
};
let slice = SparkSlice::new();
let result = slice.invoke_with_args(args).unwrap();
assert!(result.to_array(1).unwrap() == Arc::new(NullArray::new(1)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert!(result.to_array(1).unwrap() == Arc::new(NullArray::new(1)));
assert_eq!(result.to_array(1).unwrap(), Arc::new(NullArray::new(1)));

}
}
18 changes: 18 additions & 0 deletions datafusion/sqllogictest/test_files/spark/array/slice.slt
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,21 @@ query ?
SELECT slice([1, 2, 3, 4], CAST('2' AS INT), 4);
----
[2, 3, 4]

query ?
SELECT slice(column1, column2, column3)
FROM VALUES
(NULL, 1, 2),
(NULL, 1, -2),
(NULL, -1, 2),
(NULL, 0, 2);
----
NULL
NULL
NULL
NULL

query ?
SELECT slice(slice(NULL, 1, 2), 1, 2)
----
NULL