Skip to content

Commit 8bea9da

Browse files
committed
feat: fix slice function on OOB ranges
1 parent c8dddb8 commit 8bea9da

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

  • datafusion

datafusion/spark/src/function/array/slice.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ fn calculate_start_end(args: &[ArrayRef]) -> Result<(ArrayRef, ArrayRef)> {
172172
start
173173
};
174174

175+
// Spark returns an empty array when the adjusted start lands before
176+
// position 1 (e.g. slice([1], -2, 2)). array_slice would otherwise
177+
// treat 0 the same as 1 and return the first element.
178+
if adjusted_start_value < 1 {
179+
adjusted_start.append_value(1);
180+
end.append_value(0);
181+
continue;
182+
}
183+
175184
adjusted_start.append_value(adjusted_start_value);
176185
end.append_value(adjusted_start_value + (length - 1));
177186
}

datafusion/sqllogictest/test_files/spark/array/slice.slt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,18 @@ query ?
137137
SELECT slice(slice(make_array(NULL), 1, 2), 1, 2)
138138
----
139139
[NULL]
140+
141+
query ?
142+
SELECT slice(make_array(1), -2, 2)
143+
----
144+
[]
145+
146+
query ?
147+
SELECT slice(make_array(1, 2, 3, 4), -5, 2)
148+
----
149+
[]
150+
151+
query ?
152+
SELECT slice(make_array(1), 3, 4)
153+
----
154+
[]

0 commit comments

Comments
 (0)