Repository navigation
perf(jsonb): read an index union in one pass over the array - #11753
Merged
Xuanwo merged 8 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.
`select_array_values_by_indices` rescanned the array for every index in `$[...]`, decoding each element and testing it against a `HashSet` the size of the range, with no early exit: a 500-member union over 10,000 elements cost 5,000,000 steps. Each index now resolves to a clamped range, and the array is read once up to the highest position named, decoding only the elements selected. The order and multiplicity of the indices are kept. Resolving both bounds in `i64` also clamps a negative slice end, which used to wrap to `usize::MAX` and select nothing.
…-one-pass # Conflicts: # rust/lance-jsonb/src/jsonpath/path.rs # rust/lance-jsonb/tests/it/functions.rs
Member
|
[AI] GitHub reports conflicts after #11749 landed. Both PRs touch Could you integrate current |
…on-one-pass # Conflicts: # rust/lance-jsonb/src/jsonpath/path.rs # rust/lance-jsonb/tests/it/functions.rs
Xuanwo
approved these changes
Dec 2, 2026
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 #11752.
select_array_values_by_indicesscanned 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 aHashSetper member, becomesto_rangeand returns the clamped range.ArrayIterator::skip_tomoves 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_passchecks 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