Skip to content

fix(stable_row_id): segmented scalar index returns stale rows under stable row ids - #9785

Merged
Xuanwo merged 9 commits into
lance-format:mainfrom
zhangyue19921010:fix/stable-row-id-segment-masking
Oct 23, 2026
Merged

Xuanwo merged 9 commits into
lance-format:mainfrom
zhangyue19921010:fix/stable-row-id-segment-masking

Conversation

@zhangyue19921010

Copy link
Copy Markdown
Collaborator

Problem

With enable_stable_row_ids = true, a scalar index with more than one segment returns rows that no longer match the filter.

  1. Create a BTree index on id.
  2. UPDATE ... SET id = 1000 WHERE id = 0. The row keeps its row id R but moves to a new fragment; its old slot is marked deleted.
  3. optimize_indices(&OptimizeOptions::append()). The old segment A is kept; the new fragment gets segment B.
  4. WHERE id = 0 through the index returns 1 row, a full scan returns 0.

Root cause

  1. A segment's postings are only valid for rows still stored in the fragments it covers. Segment A still holds id = 0 -> R.
  2. Query-time masking uses the union of all segment coverages, and LogicalScalarIndex unions the per-segment results before that masking runs, so the segment boundary is lost.
  3. With address-based row ids this is exact: an updated row gets a new address and the stale one is deleted. With stable row ids R is live in a fragment segment B covers, so the union lets segment A's stale posting through.

Fix

  1. LogicalScalarIndex::try_new takes the dataset and each segment with its IndexMetadata, and under stable row ids keeps each segment's fragment_bitmap.
  2. At search time a segment that returned hits has its result restricted to the live row ids currently stored in its own coverage before combining. The allow-list is the existing DatasetPreFilter::do_create_deletion_mask_row_id, cached per version and coverage, fetched only for segments with hits.
  3. Address-domain segments (ZoneMap, BloomFilter, FMIndex) are not masked: their stale hits are deleted addresses that the row-id translation already drops.
  4. NullableRowAddrSet::into_parts is added so the restriction runs in place.

…w ids

A segment's postings are only valid for rows still stored in the
fragments it covers, but query-time masking used the union of all
segment coverages. Under stable row ids an updated row keeps its id while
moving to a fragment another segment covers, so the old segment's posting
for the old value passed the union mask: after
`optimize_indices(OptimizeOptions::append())`, `id = 0` through the index
returned the updated row while a full scan returned nothing.

`LogicalScalarIndex` now keeps each segment's fragment coverage and, for
segments that return hits, restricts their row-id results to the live
row ids currently stored in that coverage (the cached stable-row-id
allow-list keyed by version and coverage) before combining them.
Address-domain segments (zone map, bloom filter, FM-index) are unchanged:
their stale hits are deleted addresses that the row-id translation
already drops. `NullableRowAddrSet::into_parts` lets the restriction run
in place.

@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 Oct 8, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 8, 2026
@zhangyue19921010

zhangyue19921010 commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator Author

Hi @wjones127 @BubbleCal Would u mind to take look ? This is a bug of incorrect data querying in the stable row id scenario. Thanks!

Move the helper's imports to the top of the test module, build the input
with record_batch!(), and run the BTree and ZoneMap cases as one rstest.
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 10, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 10, 2026
@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 13, 2026
@wjones127 wjones127 added the critical-fix Bugs that cause crashes, security vulnerabilities, or incorrect data. label Oct 19, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 23, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Oct 23, 2026
Xuanwo and others added 2 commits October 23, 2026 16:58
Since lance-format#9481 a newly built BTree returns row addresses, so the btree case of
stale_posting_after_update_with_stable_row_ids no longer exercises the
row-id mask and passes without it. Add a Bitmap case, which still returns
row ids and fails without the mask, and assert each case's expected result
domain so a future domain change cannot silently hollow out the test.
@github-actions github-actions Bot added A-python Python bindings A-java Java bindings + JNI A-deps Dependency updates labels Oct 23, 2026
@Xuanwo
Xuanwo force-pushed the fix/stable-row-id-segment-masking branch from c1ccb12 to d8b0f6e Compare October 23, 2026 09:21
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Oct 23, 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 constructor integration is fixed. Restricting each segment's hits before union addresses the stale-posting cause, and the Bitmap regression now exercises the row-ID path. No blocking findings remain.

Please mark this PR with the breaking-change label.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 23, 2026
@Xuanwo
Xuanwo merged commit 012e98a into lance-format:main Oct 23, 2026
39 checks passed
Xuanwo added a commit that referenced this pull request Oct 23, 2026
…ow ids (#9872)

## Problem

On a table with stable row ids, an update keeps a row's id but moves the
row to a new fragment. After an append-only `optimize_indices` gives
that fragment its own FTS segment, the old segment's postings for the
row become visible again:

- full-text search returns the row for text it no longer contains, and
can return it twice;
- merging the segments (`optimize_indices` or
`merge_existing_index_segments`) carries the stale posting into the
merged segment, where it can no longer be filtered out.

All segments share one prefilter built over the union of their fragment
coverage. The moved row's id is live in a fragment the new segment
covers, so the old segment's posting passes the filter. Merging filters
old rows against the same union.

This is the full-text part of #9817; the BTREE part is fixed in #9785.

## Fix

- **Search:** when an index has more than one segment on a stable-row-id
table, each segment only reports the live rows stored in the fragments
it covers. Every FTS search path applies this per segment: match,
phrase/boolean, cross-column, and combined_fields, including its BM25F
document length. Tables without stable row ids and single-segment
indexes are unchanged.
- **Merge:** each source segment is filtered by its own coverage instead
of the union, in both merge paths. When the filter drops nothing, the
posting rebuild is skipped.
- **Test:** one regression test based on the reproduction in #9817
covers every search path and both merge paths.

---------

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

A-deps Dependency updates A-java Java bindings + JNI A-python Python bindings bug Something isn't working critical-fix Bugs that cause crashes, security vulnerabilities, or incorrect data. K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants