Repository navigation
fix(jsonb): clamp both ends of a JSONPath array slice - #11749
Merged
Xuanwo merged 2 commits intoDec 2, 2026
Merged
Conversation
ArrayIndex::to_indices resolved slice bounds in i32 and clamped only the upper end of the range. A negative resolved end, or any slice over an empty array, was cast to usize and wrapped to usize::MAX, so the loop that collects the selected indices inserted values until the process ran out of memory. `$.a[0 to -1]` and `$.a[0 to last-5]` on a three-element array, or `$.a[0 to 5]` on `[]`, never returned. Resolve the bounds in i64 and clamp both ends; an empty range selects nothing. This is what databendlabs/jsonb does today. No persisted bytes change. ## How was this patch tested `test_select_by_path_slice_out_of_range` runs the four cases on a helper thread with a timeout; on the previous code the selection does not return and the test fails after 5 s.
Xuanwo
approved these changes
Dec 2, 2026
Xuanwo
pushed a commit
that referenced
this pull request
Dec 2, 2026
Closes #11752. `select_array_values_by_indices` scanned the whole array once per union member, so a union of k members on an n-element array read k × n elements. It now collects every position the union names, sorts them, and reads the array once up to the highest one, decoding only the named elements. The output keeps the written order and multiplicity: `$[5, 0]` selects 5 then 0, and `$[1, 1]` selects the second element twice. `ArrayIndex::to_indices`, which built a `HashSet` per member, becomes `to_range` and returns the clamped range. `ArrayIterator::skip_to` moves past elements without decoding them, but still checks their bounds, so a truncated value fails the same way a read would. This branch includes #11749, because both change `to_indices`. Merge #11749 first. ## How was this patch tested `test_select_by_index_union_is_one_pass` checks written order, multiplicity, overlapping ranges, an out-of-bounds index and empty ranges. It also times a 500-member spread union against `$[*]` on a 10,000-element array: the union has to finish in less than half the time of the full pass. The previous code scanned the array 500 times and fails that check. Measured with pylance 15.0.0 (release build) on 100 rows of 500-element arrays: a 500-member union took 212 ms and a single index took 2 ms. 🤖 Generated with Claude Opus
Contributor
Author
|
Thanks @Xuanwo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #11748.
A JSONPath slice whose end resolved before index 0 never returned: any slice over an empty array, or
$.a[0 to -1]on a non-empty one.ArrayIndex::to_indicesresolved the bounds ini32, clamped only the upper one, and cast a negative end tousize, so the loop that collects the selected indices ran until memory ran out.The bounds are now resolved in
i64and clamped at both ends. An empty range selects nothing, and alastoffset near either end of thei32range no longer overflows. Slices that fall inside the array select the same elements as before. databendlabs/jsonb, which this crate is adapted from, resolves slices the same way.How was this patch tested
test_select_by_path_slice_out_of_rangecovers a slice over an empty array, a negative end,lastoffsets at thei32limit, and an in-range slice as a control. The selection runs on a helper thread with a 5 s timeout; on the previous code it times out.🤖 Generated with Claude Opus