Skip to content

fix(index): carry merged segment coverage through compaction only while indexed data matches - #9524

Merged
jackye1995 merged 11 commits into
lance-format:mainfrom
xuanyu-z:rtree-coverage-remap
Oct 9, 2026
Merged

jackye1995 merged 11 commits into
lance-format:mainfrom
xuanyu-z:rtree-coverage-remap

Conversation

@xuanyu-z

@xuanyu-z xuanyu-z commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Extends the merged-segment coverage remap from #9421 to RTree, and only lets coverage follow rows through a compaction where the indexed data is unchanged.

Problem

merge_existing_index_segments excluded RTree from the remap, so its staleness pass dropped every fragment a deferred compaction had retired. RTree segments staged before a compaction then merged into an index covering nothing, and their rows fell back to a flat scan.

For every scalar family, the remap also carried coverage forward when something else had changed the indexed column along the way, for example an overlay that a compaction then folded in. The merge also stamped the index as current, so an overlay on a covered fragment that no compaction touched stopped masking the index's old value. In both cases the index answered stale values.

Example: BTree segments on age are staged, an overlay updates one row's age, and a compaction folds that overlay into a new fragment. The merge claims the new fragment, and on main a query for the new age returns nothing.

Fix

  • RTree now goes through the remap. Its staleness pass exempts only the fragments the remap added.
  • Coverage moves to a compaction's output only if the segments cover the whole rewrite group.
  • Each covered fragment is followed through the compactions that rewrote it. Its indexed data must match the version its index entries describe:
    • where each indexed field is stored (base, path, file version and column), with a packed struct holding an indexed child counting as that child's storage;
    • its overlays on the indexed fields, judged by the query-time rule;
    • its deleted rows.
      Rewriting another column, even in the same file, does not count.
  • Only fragments shown fresh keep their coverage. A fragment that fails the check is left out, together with everything a compaction later produced from it, and its rows are scanned. If a manifest needed for the check was cleaned up, the affected fragments are left out the same way. That includes a segment's own build version, which costs only that segment's fragments; RTree's second pass also drops such a segment rather than failing the merge.

Tagged tables keep their own replay path.

Tests

The merge tests are in dataset/tests/merge_coverage.rs.

Coverage is dropped for:

  • partial group coverage
  • overlays before, after or between compactions
  • data replacement
  • in-place updates, including a packed struct whose child is indexed
  • a cleaned-up commit manifest, or a segment's cleaned-up build version (the other group stays covered)
  • a restore that removes an overlay or revives a row

Coverage is kept for:

  • whole-group remaps (the merged index holds every row it claims)
  • other groups in the same compaction, beside a stale one
  • a fragment no compaction touched, beside an overlaid one
  • a rewrite of another column in the same file
  • a group retired without a reuse mapping
  • RTree on a tagged history

A seeded workload stages segments in two batches, with only non-compacting writes in between so the second batch lands at a later version. It runs appends, deletes, in-place updates, overlays, folding and plain deferred compactions, added columns and config commits before, between and after the merge. Point and range lookups are then compared against a scan. main returns stale values on this workload. Each check in the staleness logic was disabled in turn, and each change made tests fail.

cargo test -p lance --lib --features geo passes. clippy is clean with and without geo.

Follow-ups

These are left out to keep the change small:

  • One pass for RTree. Its staleness pass and the remap's check overlap and could become one.
  • Fewer history reads per merge. Each merge lists every version and opens some manifests twice.
  • Overlapping staged segments. A segment staged on a fragment's compaction output alongside one staged on the fragment itself indexes those rows twice. Scalar lookups stay correct, but a full-text index counts the documents twice in its BM25 statistics. The merge should refuse a source together with its descendant.
  • Record the proof in the reuse index (Fragment reuse index does not record what a segment merge must verify #9543), so a cleanup no longer costs coverage.

@github-actions github-actions Bot added the bug Something isn't working label Sep 23, 2026
@xuanyu-z
xuanyu-z force-pushed the rtree-coverage-remap branch from b45e954 to 94a4867 Compare September 23, 2026 17:39
@xuanyu-z
xuanyu-z force-pushed the rtree-coverage-remap branch 3 times, most recently from 003a8b9 to 52e2bd0 Compare September 23, 2026 18:48
@xuanyu-z
xuanyu-z marked this pull request as ready for review September 23, 2026 18:49
@xuanyu-z
xuanyu-z force-pushed the rtree-coverage-remap branch from 52e2bd0 to c19e168 Compare September 23, 2026 18:52
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Sep 23, 2026
@xuanyu-z
xuanyu-z force-pushed the rtree-coverage-remap branch from c19e168 to 9b01820 Compare September 23, 2026 19:17
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Sep 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 Sep 23, 2026
@xuanyu-z
xuanyu-z force-pushed the rtree-coverage-remap branch from 9b01820 to 4112e46 Compare September 23, 2026 19:39
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Sep 23, 2026
@xuanyu-z
xuanyu-z marked this pull request as draft September 23, 2026 19:50
@xuanyu-z
xuanyu-z force-pushed the rtree-coverage-remap branch from 4112e46 to 8fd4384 Compare September 23, 2026 20:19
@xuanyu-z xuanyu-z changed the title fix(index): resolve RTree segment coverage through the reuse index fix(index): resolve scalar segment coverage through the reuse index Sep 23, 2026
@xuanyu-z
xuanyu-z force-pushed the rtree-coverage-remap branch from 8fd4384 to 6518fff Compare September 23, 2026 20:54
@xuanyu-z
xuanyu-z marked this pull request as ready for review September 24, 2026 01:39
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-changes Latest Gatekeeper recommendation requests changes. and removed K-changes Latest Gatekeeper recommendation requests changes. labels Sep 24, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-changes Latest Gatekeeper recommendation requests changes. and removed K-changes Latest Gatekeeper recommendation requests changes. labels Sep 24, 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 Sep 24, 2026
@xuanyu-z
xuanyu-z force-pushed the rtree-coverage-remap branch from f8cea38 to a4bd34b Compare September 24, 2026 03:45
@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 8, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. and removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 8, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 9, 2026
@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 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 with a non-blocking risk.

The merge now preserves coverage when an unrelated column in the same file is rewritten, and leaves fragments whose indexed data or history cannot be verified on the scan path.

Cleanup can still remove manifests needed to retain coverage, leaving affected rows on the scan path until the index is rebuilt. Retaining the required manifests until merging avoids that cost; #9543 tracks recording the necessary evidence in the reuse index.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 2026
xuanyu-z and others added 11 commits October 9, 2026 00:22
… compaction's own commit

Three ways the coverage proof could reach the wrong answer:

- Every followed fragment was held to the oldest segment's version. A newer
  segment's fragment that version does not have, or one rewritten before the
  newer segment was built, was refused coverage it had. Each fragment is now
  held to the segment that covers it. Where a compaction committed after a
  segment was built, conflict resolution moved the segment's coverage onto the
  compaction's output, which the segment's version does not have; the
  compaction's inputs are followed from that version instead.
- A compaction's commit was found by searching the retained versions for the
  earliest one holding its output. A restore brings a retired fragment back,
  so that search could take the restore for the compaction and compare against
  the wrong state. The reuse index records each compaction at the version
  immediately before its commit, so the commit is the next version; it is
  accepted only when its own record is the rewrite that produced the
  fragments. That also removes the search's per-fragment cost.
- Every failure to read a historical version was treated as cleanup, so a
  transient storage error could cost coverage the merged index would never get
  back. A version now counts as gone only when the dataset no longer lists it;
  any other read failure is an error.

Each version read is opened once and indexes its fragments by id.
- The BTree folded-overlay test committed a data replacement after the
  compaction, duplicating the replacement test. It now stages, overlays a
  source, compacts, merges, commits and queries.
- The cleanup test deleted every old version, so the merge gave up before it
  reached the compaction's commit. It now deletes only that commit's
  manifest, with a control showing the same merge keeps coverage before.
- Segments built at different versions keep their coverage.
- The four RTree overlay-timing tests are one test with four named cases,
  over a shared fixture.
The proof compared a fragment's state going forward only: files rewritten and
overlays added since the segments were built. A restore moves it the other way
without leaving anything newer behind. Restoring a snapshot from before an
overlay takes the overlay's values away while the data files still match, and
restoring one from before a delete brings back a row the segments never
indexed; either way the merged index claimed a fragment it no longer describes.

The overlays over the indexed fields must now be the same set on both sides,
and every row deleted then must still be deleted now. The deletion vectors are
read only when the two deletion files differ.
An RTree merge on a table with a tagged reuse history takes its coverage from
the tagged plan. The merged segment claims the destinations a stable
partition's sources cover completely, and holds every row of them.

Also states the coverage guard's comparison by reference to the one function
that makes it, and keeps test imports at the top of their modules.
Use `overlay_affects_index` and `field_affects_index` so an overlay on a
parent or child of an indexed field counts, as at query time. The
staleness.rs change is identical to the one in lance-format#9808.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A fragment whose indexed data changed since the segments were built cost
the merge every compacted fragment's coverage, clean ones included. The
check now returns the stale current fragments: a compaction whose covered
input changed, or whose manifests were cleaned up, makes the fragments its
group produced stale, and that carries through later compactions. Only
those are left out of the merged coverage; the rest still follow their rows.

The merge-coverage tests move to their own module with shared helpers, and
gain a seeded workload (two staging batches, writes, overlays and deferred
compactions before, between and after the merge) checked against a scan,
plus an explicit case for an overlaid fragment beside a compacted group.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Compare where each indexed field is stored (base, path, file version and
  its column) rather than the whole file's field list, so rewriting another
  column in the same file keeps the coverage; a packed struct holding an
  indexed child counts as that child's storage.
- A segment whose build version was cleaned up leaves only its own
  fragments unverified instead of abandoning the check, so the merge is
  still stamped current and its commit goes through. RTree's second pass
  likewise drops such a segment's coverage instead of failing.
- Keep only fragments shown fresh, rather than removing those shown stale.
- Tests: a cleaned-up build version beside a clean group, an unrelated
  column rewrite, a packed child, the RTree cleaned-up case, two staging
  batches that really land at different versions, and stronger assertions.

Co-Authored-By: Claude Opus 5.5 <[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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants