Skip to content

Commit a6e152d

Browse files
committed
feat: fix slice function on OOB ranges (#22404)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #22400 . ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> (cherry picked from commit abb943d)
1 parent c8dddb8 commit a6e152d

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)