Skip to content

fix(jsonb): clamp both ends of a JSONPath array slice - #11749

Merged
Xuanwo merged 2 commits into
lance-format:mainfrom
LuciferYang:fix/jsonb-slice-index-clamp
Dec 2, 2026
Merged

Xuanwo merged 2 commits into
lance-format:mainfrom
LuciferYang:fix/jsonb-slice-index-clamp

Conversation

@LuciferYang

Copy link
Copy Markdown
Contributor

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_indices resolved the bounds in i32, clamped only the upper one, and cast a negative end to usize, so the loop that collects the selected indices ran until memory ran out.

The bounds are now resolved in i64 and clamped at both ends. An empty range selects nothing, and a last offset near either end of the i32 range 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_range covers a slice over an empty array, a negative end, last offsets at the i32 limit, 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

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.

@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.

@github-actions github-actions Bot added the bug Something isn't working 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 fix addresses the runaway allocation while preserving inclusive slice selection. Local regression, boundary, and JSONB compatibility tests passed.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Dec 2, 2026
@Xuanwo
Xuanwo merged commit 66ae6b5 into lance-format:main Dec 2, 2026
42 checks passed
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
@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

bug Something isn't working K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: json_extract with an array slice over an empty array or negative end loops forever

2 participants