Skip to content

Commit

Permalink
fix: array_slice panics (#10547)
Browse files Browse the repository at this point in the history
* fix: array_slice panics

* add test

* add test

* update test

* fix comment

* fix clippy
  • Loading branch information
jonahgao authored May 17, 2024
1 parent cfa7154 commit 06c8f5a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 4 additions & 7 deletions datafusion/functions-array/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,19 +418,16 @@ where

if let (Some(from), Some(to)) = (from_index, to_index) {
let stride = stride.map(|s| s.value(row_index));
// array_slice with stride in duckdb, return empty array if stride is not supported and from > to.
if stride.is_none() && from > to {
// return empty array
offsets.push(offsets[row_index]);
continue;
}
// Default stride is 1 if not provided
let stride = stride.unwrap_or(1);
if stride.is_zero() {
return exec_err!(
"array_slice got invalid stride: {:?}, it cannot be 0",
stride
);
} else if from <= to && stride.is_negative() {
} else if (from <= to && stride.is_negative())
|| (from > to && stride.is_positive())
{
// return empty array
offsets.push(offsets[row_index]);
continue;
Expand Down
12 changes: 12 additions & 0 deletions datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,18 @@ select array_slice(arrow_cast(make_array(1, 2, 3, 4, 5), 'LargeList(Int64)'), co
[1, 2, 3, 4, 5] [43, 44, 45, 46] [41, 42, 43, 44, 45]
[5] [, 54, 55, 56, 57, 58, 59, 60] [55]

# Test issue: https://github.com/apache/datafusion/issues/10425
# `from` may be larger than `to` and `stride` is positive
query ????
select array_slice(a, -1, 2, 1), array_slice(a, -1, 2),
array_slice(a, 3, 2, 1), array_slice(a, 3, 2)
from (values ([1.0, 2.0, 3.0, 3.0]), ([4.0, 5.0, 3.0]), ([6.0])) t(a);
----
[] [] [] []
[] [] [] []
[6.0] [6.0] [] []


# make_array with nulls
query ???????
select make_array(make_array('a','b'), null),
Expand Down

0 comments on commit 06c8f5a

Please sign in to comment.