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
9 changes: 9 additions & 0 deletions datafusion/spark/src/function/array/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ fn calculate_start_end(args: &[ArrayRef]) -> Result<(ArrayRef, ArrayRef)> {
start
};

// Spark returns an empty array when the adjusted start lands before
// position 1 (e.g. slice([1], -2, 2)). array_slice would otherwise
// treat 0 the same as 1 and return the first element.
if adjusted_start_value < 1 {
adjusted_start.append_value(1);
end.append_value(0);
continue;
}

adjusted_start.append_value(adjusted_start_value);
end.append_value(adjusted_start_value + (length - 1));
}
Expand Down
15 changes: 15 additions & 0 deletions datafusion/sqllogictest/test_files/spark/array/slice.slt
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,18 @@ query ?
SELECT slice(slice(make_array(NULL), 1, 2), 1, 2)
----
[NULL]

query ?
SELECT slice(make_array(1), -2, 2)
----
[]

query ?
SELECT slice(make_array(1, 2, 3, 4), -5, 2)
----
[]

query ?
SELECT slice(make_array(1), 3, 4)
----
[]
Loading