Skip to content

perf(jsonb): read an index union in one pass over the array - #11753

Merged
Xuanwo merged 8 commits into
lance-format:mainfrom
LuciferYang:fix/jsonb-index-union-one-pass
Dec 2, 2026
Merged

Xuanwo merged 8 commits into
lance-format:mainfrom
LuciferYang:fix/jsonb-index-union-one-pass

Conversation

@LuciferYang

Copy link
Copy Markdown
Contributor

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

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

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Dec 2, 2026
@Xuanwo

Xuanwo commented Dec 2, 2026

Copy link
Copy Markdown
Member

[AI] GitHub reports conflicts after #11749 landed. Both PRs touch rust/lance-jsonb/src/jsonpath/path.rs and rust/lance-jsonb/tests/it/functions.rs.

Could you integrate current main and keep #11749’s clamped inclusive slice bounds and empty-range regression cases alongside the index-union changes? That will let CI exercise both changes together.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Dec 2, 2026
…on-one-pass

# Conflicts:
#	rust/lance-jsonb/src/jsonpath/path.rs
#	rust/lance-jsonb/tests/it/functions.rs
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Dec 2, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Gate recommendation: approve.

The merged revision preserves union order, duplicates, and clamped inclusive slices while avoiding repeated array scans. The full JSONB suite and optimized union regression passed.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Dec 2, 2026
@Xuanwo
Xuanwo merged commit 0c1e8d1 into lance-format:main Dec 2, 2026
42 checks passed
@LuciferYang

Copy link
Copy Markdown
Contributor Author

Thanks @Xuanwo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

K-approved Latest Gatekeeper recommendation permits acceptance. performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: JSONPath index union rescans the array for every member

2 participants