Skip to content

fix(index): check staged segments against a restore at commit - #9816

Draft
xuanyu-z wants to merge 11 commits into
lance-format:mainfrom
xuanyu-z:fix/staged-segment-restore
Draft

xuanyu-z wants to merge 11 commits into
lance-format:mainfrom
xuanyu-z:fix/staged-segment-restore

Conversation

@xuanyu-z

@xuanyu-z xuanyu-z commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Depends on #9524. It reuses that PR's fragment comparison and test module. Until #9524 merges, this diff includes its commits; only the last commit is new.

Committing staged index segments (commit_existing_index_segments) prunes the fragments whose data files changed since the segments were built. It does not compare overlays or deletions, and a restore between build and commit can change both:

  • An overlay comes back. A restore brings back an overlay whose commit version predates the segments. Query-time masking treats such an overlay as already indexed, so lookups return the segments' stale value.
  • A deleted row comes back. A restore revives a row deleted before the build. The segments never indexed it, so lookups miss it.

Example: the segments are staged after a restore removed an overlay setting val = 999. A second restore brings the overlay back, then the segments commit. val = 0 still finds the row, and val = 999 does not.

Fix. At commit, a covered fragment is also stale if its overlays on indexed fields from before the build version differ from the build version's, or if a row deleted then is live again. That fragment is left out of the coverage and its rows are scanned. Overlays newer than the build are still masked at query time, so they cost no coverage.

Tests:

  • a restore that brings back an overlay;
  • a restore that revives a row;
  • an overlay added after the build, which must keep its coverage.

Each part of the check was disabled in turn and its test failed. cargo test -p lance --lib --features geo passes.

xuanyu-z and others added 9 commits October 7, 2026 16:11
… 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]>
@github-actions github-actions Bot added the bug Something isn't working label Oct 9, 2026
xuanyu-z and others added 2 commits October 9, 2026 01:01
- 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]>
Committing staged segments compared each covered fragment's data files with
the segment's build version, but not its overlays or deletions. A restore
between build and commit can bring back an overlay whose commit version
predates the segments, so query-time masking trusts their stale entry, or
revive a row they never indexed. Either now leaves the fragment out of the
committed coverage, and its rows are scanned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@xuanyu-z
xuanyu-z force-pushed the fix/staged-segment-restore branch from 2e959d1 to e0e8a69 Compare October 9, 2026 05:12
Xuanwo pushed a commit that referenced this pull request Nov 10, 2026
An index built after a deletion omits those rows. If Restore revives them before the index commits, claiming their fragment causes false-negative queries. Cleanup can hide the restore from regular commit conflict checks, and distributed commits on untagged tables previously checked files and overlays but not deletions.

Reuse the existing row-revival comparison in both coverage guards and withdraw only affected fragments from the new index's coverage, so their rows are scanned. Unchanged deletion files and additional deletions retain coverage. System-index metadata is excluded from the commit guard.

The regression suite covers both commit paths, cleanup submissions through original and refreshed dataset handles, restores with and without a remaining deletion file, and additional-deletion controls. Indexed queries are checked against index-disabled scans and fragment coverage.

Validation on Rust 1.98.1:

- `cargo test --profile ci -p lance --lib dataset::tests::merge_coverage` - 55 passed.
- `cargo test --profile ci -p lance --lib index::provenance::tests` - 7 passed.
- `cargo test --profile ci -p lance --lib io::commit::conflict_resolver::tests` - 204 passed.
- `cargo fmt --all -- --check` passed.
- Workflow-matching all-workspace-feature Clippy for all targets and the Arrow ordering fixture passed.

Related draft: #9816 also addresses staged-segment restore validation. This repair covers row revival in both distributed commits and commits whose intervening restore history was cleaned up.

Fixes #10913

This branch has not been deployed

No deployments
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.

1 participant