Skip to content

Commit 06c8f5a

Browse files
authored
fix: array_slice panics (#10547)
* fix: array_slice panics * add test * add test * update test * fix comment * fix clippy
1 parent cfa7154 commit 06c8f5a

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

datafusion/functions-array/src/extract.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,19 +418,16 @@ where
418418

419419
if let (Some(from), Some(to)) = (from_index, to_index) {
420420
let stride = stride.map(|s| s.value(row_index));
421-
// array_slice with stride in duckdb, return empty array if stride is not supported and from > to.
422-
if stride.is_none() && from > to {
423-
// return empty array
424-
offsets.push(offsets[row_index]);
425-
continue;
426-
}
421+
// Default stride is 1 if not provided
427422
let stride = stride.unwrap_or(1);
428423
if stride.is_zero() {
429424
return exec_err!(
430425
"array_slice got invalid stride: {:?}, it cannot be 0",
431426
stride
432427
);
433-
} else if from <= to && stride.is_negative() {
428+
} else if (from <= to && stride.is_negative())
429+
|| (from > to && stride.is_positive())
430+
{
434431
// return empty array
435432
offsets.push(offsets[row_index]);
436433
continue;

datafusion/sqllogictest/test_files/array.slt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,18 @@ select array_slice(arrow_cast(make_array(1, 2, 3, 4, 5), 'LargeList(Int64)'), co
19631963
[1, 2, 3, 4, 5] [43, 44, 45, 46] [41, 42, 43, 44, 45]
19641964
[5] [, 54, 55, 56, 57, 58, 59, 60] [55]
19651965

1966+
# Test issue: https://github.com/apache/datafusion/issues/10425
1967+
# `from` may be larger than `to` and `stride` is positive
1968+
query ????
1969+
select array_slice(a, -1, 2, 1), array_slice(a, -1, 2),
1970+
array_slice(a, 3, 2, 1), array_slice(a, 3, 2)
1971+
from (values ([1.0, 2.0, 3.0, 3.0]), ([4.0, 5.0, 3.0]), ([6.0])) t(a);
1972+
----
1973+
[] [] [] []
1974+
[] [] [] []
1975+
[6.0] [6.0] [] []
1976+
1977+
19661978
# make_array with nulls
19671979
query ???????
19681980
select make_array(make_array('a','b'), null),

0 commit comments

Comments
 (0)