Skip to content

Commit 58cd40c

Browse files
committed
bug: Fix NULL handling in array_slice
This commit fixes the array_slice function so that if any arguments are NULL, the result is NULL. Previously, array_slice would return an internal error if any of the arguments were NULL. This behavior matches the behavior of DuckDB for array_slice. Fixes #10548
1 parent 633eef6 commit 58cd40c

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

datafusion/functions-nested/src/extract.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use arrow::array::MutableArrayData;
2727
use arrow::array::OffsetSizeTrait;
2828
use arrow::buffer::OffsetBuffer;
2929
use arrow::datatypes::DataType;
30-
use arrow_schema::DataType::{FixedSizeList, LargeList, List};
30+
use arrow_schema::DataType::{FixedSizeList, LargeList, List, Null};
3131
use arrow_schema::Field;
3232
use datafusion_common::cast::as_int64_array;
3333
use datafusion_common::cast::as_large_list_array;
@@ -419,16 +419,17 @@ fn array_slice_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
419419
None
420420
};
421421

422-
let from_array = as_int64_array(&args[1])?;
423-
let to_array = as_int64_array(&args[2])?;
424-
422+
let null_input = args.iter().find(|arg| arg.data_type().is_null());
425423
let array_data_type = args[0].data_type();
426-
match array_data_type {
427-
List(_) => {
424+
match (array_data_type, null_input) {
425+
(List(_) | LargeList(_) | Null, Some(null)) => Ok(null.clone()),
426+
(List(_), None) => {
428427
let array = as_list_array(&args[0])?;
428+
let from_array = as_int64_array(&args[1])?;
429+
let to_array = as_int64_array(&args[2])?;
429430
general_array_slice::<i32>(array, from_array, to_array, stride)
430431
}
431-
LargeList(_) => {
432+
(LargeList(_), None) => {
432433
let array = as_large_list_array(&args[0])?;
433434
let from_array = as_int64_array(&args[1])?;
434435
let to_array = as_int64_array(&args[2])?;

datafusion/sqllogictest/test_files/array.slt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,6 +2036,12 @@ select array_slice(a, -1, 2, 1), array_slice(a, -1, 2),
20362036
query error DataFusion error: Error during planning: array_slice does not support zero arguments
20372037
select array_slice();
20382038

2039+
# Testing with NULLs should result in a NULL
2040+
query B
2041+
select array_slice(NULL, NULL, NULL) IS NULL;
2042+
----
2043+
true
2044+
20392045
## array_any_value (aliases: list_any_value)
20402046

20412047
# Testing with empty arguments should result in an error

0 commit comments

Comments
 (0)