Skip to content

fix: keep uncovered fragments when a limit is pushed into an index-answered scan - #10859

Merged
Xuanwo merged 4 commits into
lance-format:mainfrom
LuciferYang:fix/limit-pushdown-keep-uncovered-fragments
Nov 10, 2026
Merged

Xuanwo merged 4 commits into
lance-format:mainfrom
LuciferYang:fix/limit-pushdown-keep-uncovered-fragments

Conversation

@LuciferYang

Copy link
Copy Markdown
Contributor

Closes #10852.

FilteredReadStream::plan_scan now 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 applies scan_range_after_filter to 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_fragment rewrites 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_fragments builds FilteredReadExec directly with the uncovered fragment first, with and without stable row ids and with a non-zero offset.

@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 Nov 9, 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.

No blocking issues found. The guard retains limit pushdown for covered prefixes, and all 41 targeted pagination and index-result tests passed locally, including the rewrite and reordered-fragment regressions.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 9, 2026

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@Xuanwo
Xuanwo merged commit ab92d0f into lance-format:main Nov 10, 2026
41 checks passed
@LuciferYang

Copy link
Copy Markdown
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]>
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: a limit on an index-answered filter skips rows of a fragment the index does not cover

2 participants