Repository navigation
fix: keep uncovered fragments when a limit is pushed into an index-answered scan - #10859
Merged
Xuanwo merged 4 commits intoNov 10, 2026
Conversation
Xuanwo
approved these changes
Nov 10, 2026
Xuanwo
left a comment
Member
There was a problem hiding this comment.
Verified the merged head. The plan guard preserves earlier uncovered fragments without disabling covered-prefix limit pushdown, and the regular fallback applies the after-filter range once in caller order. Targeted regressions, adjacent pagination and index-result coverage, formatting, full clippy, and the complete hosted CI matrix pass. The accepted trade-off is the conservative fallback read when an earlier uncovered fragment is present.
Contributor
Author
|
Thank you @Xuanwo |
Xuanwo
added a commit
that referenced
this pull request
Nov 19, 2026
A filtered scan with `limit`/`offset` returns the wrong rows when an
`OR` mixes a ZoneMap or BloomFilter predicate with a negated BTree,
Bitmap or LabelList predicate. Found while differential-testing
scalar-index scans against full scans.
```python
import lance
import pyarrow as pa
n = 200
tbl = pa.table({
"id": pa.array(range(n), pa.int32()),
"x": pa.array([(i * 7) % 11 for i in range(n)], pa.int32()),
"y": pa.array([i % 3 for i in range(n)], pa.int32()),
})
ds = lance.write_dataset(tbl, "/tmp/repro.lance", max_rows_per_file=60)
ds.create_scalar_index("x", "ZONEMAP")
ds.create_scalar_index("y", "BTREE")
ids = lambda **kw: ds.to_table(columns=["id"], filter="x = 5 OR y != 1", **kw).column("id").to_pylist()
print(ids()[3:8], ids(limit=5, offset=3))
```
Before (pylance 14.0.2 and 15.0.0b21): `[5, 6, 7, 8, 9] [5, 6, 8, 9,
11]`. The window skips row 7, which matches only through the zone map.
Paging through the 139 matches 10 at a time never returns 6 of them and
returns 6 twice. After: on the same data, every page that
`Dataset::scan` returns, at 7 or 10 rows per page, equals the matching
slice of the full result, and the pages hold all 139 matches once.
The index result is `AtLeast`: rows with `y != 1` are guaranteed
matches, and every other row is a candidate that needs a recheck.
`FilteredReadStream::apply_index_to_fragment` mirrored only the
guaranteed ranges into the pushed-down plan. `plan_scan` switched to
that plan, which skips the recheck, as soon as the guaranteed rows
filled `offset + limit`. A matching candidate earlier in scan order was
never read, so a later guaranteed row took its place. Now the guaranteed
ranges serve the window only up to the first candidate in each fragment.
Otherwise the fragment gets no mirror, and the regular plan, which
rechecks candidates in scan order, is kept. #10859 added this fallback
for fragments the index does not cover; this change applies it to rows
outside the lower bound. The push-down is still used when guaranteed
rows come before every candidate in the window. Exact and AtMost
results, such as `x = 5` on any index, plan as before. Datasets with
stable row ids are not affected end to end, because the scanner does not
push limits into them.
Testing: with `cargo test --profile ci -p lance --lib --
io::exec::filtered_read::tests::test_at_least
dataset::tests::dataset_scanner_scalar_index::test_not_or_zone_map`, 11
of the 53 selected tests fail before the change and all pass after.
`test_at_least_index_preserves_candidates` adds lower bounds that
guarantee only later matches. The new
`test_at_least_limit_pushdown_needs_guaranteed_prefix` checks that the
plan keeps the push-down when guaranteed rows come first and drops it
when candidates come first. `test_not_or_zone_map` adds `limit = 1`. The
full `lance` lib suite passes, and `cargo fmt` and `cargo clippy -p
lance --tests` are clean. An index-vs-full-scan differential over
288,000 limit/offset windows went from 9,654 wrong windows to 0. It
covered ZoneMap, BloomFilter, BTree, Bitmap and LabelList indices,
deletions, unindexed appends, one or five fragments, and stable row ids
on and off. The cost is the fallback read: on 200,000 rows in 20
fragments, `x = 5 OR y != 1` with `LIMIT 10` now reads 11,808 rows,
where it used to read 10 and return the wrong ones.
---------
Co-authored-by: Xuanwo <[email protected]>
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 #10852.
FilteredReadStream::plan_scannow records when a queued fragment has no entry in the pushed-down plan, which is the case for a fragment the index does not cover, and then keeps the regular plan instead of swapping. The regular plan appliesscan_range_after_filterto the stream in fragment order, so rows come back in order. The pushed-down plan is still used when the covered fragments fill the limit before any uncovered fragment, which is the layout of an index built before later appends.How was this patch tested
test_scan_limit_offset_with_uncovered_first_fragmentrewrites one value of fragment 0 in place, checks that the index no longer covers fragment 0, and pages through an index-answered filter two rows at a time. Every row must come back once, in fragment order. Without this change fragment 1's rows come back twice.test_limit_pushdown_keeps_preceding_uncovered_fragmentsbuildsFilteredReadExecdirectly with the uncovered fragment first, with and without stable row ids and with a non-zero offset.