Skip to content

fix(index): commit the live coverage an optimize merge writes on a tagged fragment-reuse history - #11426

Merged
LuQQiu merged 1 commit into
mainfrom
lu/optimize-merge-live-coverage
Dec 1, 2026
Merged

LuQQiu merged 1 commit into
mainfrom
lu/optimize-merge-live-coverage

Conversation

@LuQQiu

@LuQQiu LuQQiu commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

After compaction moves rows into new fragments, an index can keep its old row addresses and translate them when read. This PR fixes how optimize_indices records coverage when merging scalar or inverted index segments on tables with a tagged fragment-reuse history.

The merge already reads translated entries, so its output file contains current row addresses. But its manifest bitmap—the list of fragments the segment covers—still recorded the historical fragment IDs from the input segments.

That mismatch makes the merged index depend on old compaction history even though its entries have already been translated. Simply replacing the bitmap with current fragment IDs is not enough: several segments may each supply part of the same current fragment, so merging only some of them must not claim the whole fragment.

Example

Suppose segment S indexes fragment 0 and segment T indexes fragment 1. A rewrite redistributes their rows into fragments 10 and 11, so both S and T are needed to serve each destination. New data is then appended as fragment 12.

With this change:

Optimize selection Result
Default optimize selects T only Skip the merge. Keep S and T unchanged and create a separate segment for fragment 12.
merge(2) selects S and T Merge both with the new data. The resulting file and bitmap cover {10, 11, 12}.

T alone contains only part of the rows in fragments 10 and 11. S and T together contain all of them, so their merged segment can safely claim those fragments directly.

Change

Before merging, use the snapshot's query plan to check the selected segments' current coverage:

  • Every selected segment must be accounted for. Segments that no longer contribute coverage can still be removed by the merge.
  • No segment left out of the merge may contribute to a fragment the merged segment would claim.
  • The selected coverage must not overlap the fragments being newly indexed.

If these checks fail, or coverage cannot be verified, keep the existing segments and index only the unindexed fragments, as append mode does. If there are no unindexed fragments, do nothing. The check accepts or declines the whole selection; it does not expand or shrink the merge window.

An accepted merge records the selected segments' current coverage plus the newly indexed fragments. This makes its metadata agree with its file and lets cleanup remove old mapping history when no remaining index needs it.

Compatibility and limits

  • Existing segments written with historical bitmaps remain readable; this PR does not migrate them or change the reader rules from fix(index): translate only the rows a segment still owns through a tagged fragment-reuse history #11399.
  • Default optimize may leave more segments than before. Combining them requires a merge window that includes every contributor to the affected fragments.
  • Tables without tagged history, including v0 histories, keep their previous behavior.
  • Staged segments, vector merges, the explicit segment-merge API, and inverted-index corpus statistics are outside this change.

Tests

Regression tests cover accepted and declined merges for BTree and inverted indexes, shared destinations, independently covered destinations, superseded segments, and tables without tagged history. They check manifest bitmaps and compare indexed results with scans after reopening.

Additional checks verify that cleanup removes history after a complete merge but retains it after a declined merge, and that overlay updates remain visible across both paths and cleanup. The overlay cases use manifest fixtures because normal overlay commits on tagged tables are currently blocked.

@LuQQiu

LuQQiu commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions github-actions Bot added the bug Something isn't working label Nov 24, 2026

@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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment on lines +1006 to +1008
Some(MergeClaim::Declined) => {
if unindexed.is_empty() {
return Ok(None);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 (optional) Distributed callers on a steady-state tagged table now get an optimize plan whose tasks do nothing, on every planning cycle. The new Declined early return (Ok(None) when unindexed is empty) is not mirrored by unsplit_index_needs_rewrite (optimize.rs:875). That predicate still plans a task whenever two or more segments are selected. Fix: when nothing is unindexed on a tagged history, have the planner run tagged_merge_claim on the same selection and plan nothing on Declined. Same pattern at 2 sites (append.rs:1007, append.rs:2277).

Why this was flagged

Trigger: a table with a tagged FRI history and no unindexed fragments, where the OptimizeOptions::merge(n) window (n>=2) selects T and U but leaves out S, which shares a destination with T (or any segment of the index is Unverifiable). plan_optimize_indices sends tagged tables to plan_unsplit_index (optimize.rs:782). That calls unsplit_index_needs_rewrite (optimize.rs:875-887), which returns true because select_segments_to_merge(...).len() >= 2. The task then runs merge_scalar_indices, or the Inverted branch. There tagged_merge_claim returns MergeClaim::Declined and the code returns Ok(None) (append.rs:1007-1008 and 2277-2278). On the base branch this selection was merged, and the next plan was empty. After this change the selection stays declined until a wider window is used, so every plan has a task that produces nothing. frag_reuse_reader_tests.rs:4493-4496 shows this is a contract the repo tests for: "A distributed driver gets nothing to run either".

Verification: In append.rs at lines 1005-1010 and the matching Inverted branch, Some(MergeClaim::Declined) returns Ok(None) when unindexed is empty. unsplit_index_needs_rewrite returns Ok(select_segments_to_merge(dataset, segments, options).len() >= 2) (optimize.rs:887) and never calls tagged_merge_claim. This is a regression from the base, where the same merge(2) selection merged.

@LuQQiu
LuQQiu force-pushed the lu/optimize-merge-live-coverage branch from 215af05 to 0394c03 Compare November 24, 2026 06:14
@LuQQiu

LuQQiu commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@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.

Code review found no issues

No high-confidence issues detected in this change.

Still open from earlier reviews (1):

  • Unresolved: 1 minor or pre-existing.

@LuQQiu

LuQQiu commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@LuQQiu
LuQQiu force-pushed the lu/optimize-merge-live-coverage branch from 0394c03 to 7cdf8da Compare November 24, 2026 07:31

@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.

Code review found no issues

No high-confidence issues detected in this change.

Still open from earlier reviews (1):

  • Unresolved: 1 minor or pre-existing.

@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 repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 30, 2026
@LuQQiu
LuQQiu force-pushed the lu/fri-source-row-admission branch from 78c8558 to 84b9f30 Compare December 1, 2026 00:40
@LuQQiu
LuQQiu force-pushed the lu/optimize-merge-live-coverage branch from 7cdf8da to 8bb6b52 Compare December 1, 2026 00:40
@LuQQiu

LuQQiu commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Dec 1, 2026

@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.

Still open from earlier reviews (1):

  • Unresolved: 1 minor or pre-existing.

Comment thread rust/lance/src/index/append.rs Resolved
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Dec 1, 2026
@LuQQiu
LuQQiu force-pushed the lu/fri-source-row-admission branch from 84b9f30 to d97a192 Compare December 1, 2026 01:52
@LuQQiu
LuQQiu force-pushed the lu/optimize-merge-live-coverage branch from 8bb6b52 to 1a8a4e4 Compare December 1, 2026 01:52
@LuQQiu

LuQQiu commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Dec 1, 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 ZoneMap/BloomFilter cleanup regression is fixed: carried zones retain the provenance needed to translate them. All 73 focused local cases passed, including both affected index types after cleanup and reopening.

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

@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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Still open from earlier reviews (1):

  • Unresolved: 1 minor or pre-existing.

Comment on lines +1024 to +1030
// The stable-row-id fold-in above must never meet a tagged history.
if live_claim.is_some() && dataset.manifest.uses_stable_row_ids() {
return Err(Error::internal(
"merge_scalar_indices: a tagged fragment reuse history on a stable-row-id table"
.to_string(),
));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 (optional) Users of stable-row-id tables with a tagged fragment-reuse history get an internal error from every scalar-index optimize_indices. On the base branch these merges succeeded. The guard at append.rs:1025 fires whenever live_claim.is_some() and the manifest uses stable row ids, whether or not the fold-in added any segment. The commit layer explicitly allows a tagged history beside stable row ids. Fix: raise the error only when the fold-in actually pushed a row-id-domain segment, or drop the guard. If kept, use a NotSupported error naming the index and table state rather than Error::internal.

Why this was flagged

rust/lance-table-runtime/src/frag_reuse/stable_row_ids.rs:57-59 accepts an index_version 1 (tagged) history on a stable-row-id table. On such a table, tagged_merge_claim returns Some(..) for every scalar optimize. append.rs:1025 then returns Error::internal("merge_scalar_indices: a tagged fragment reuse history on a stable-row-id table"). It does this even for a plain append-mode delta or a BTree merge where the fold-in at append.rs:988-999 added nothing. On the base branch the same call merged or indexed the new fragments. Now the index can never be optimized and new data stays unindexed. The commit check also requires every index on such a table to store row addresses (frag_reuse_with_stable_row_ids.rs:132-135), so the row-id-domain fold-in this guard protects cannot occur there. The compaction-side refusal at stable_row_ids.rs:90-95 covers only defer_index_remap recording, not transitions committed through the transaction API or by other writers.

Verification: Trigger: a stable-row-id table that carries a tagged (index_version 1) FRI history. tagged_merge_claim returns Some(..) whenever the FRI entry has index_version != 0 (frag_reuse.rs:603-609). The new guard at append.rs:1025 does not check whether the fold-in at append.rs:988-999 pushed any segment, so it fails every scalar optimize on that table. The base had no such guard.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Dec 1, 2026
Base automatically changed from lu/fri-source-row-admission to main December 1, 2026 18:15
…gged fragment-reuse history

On a table with a tagged fragment-reuse history, `optimize_indices` merged
scalar and inverted segments into a file holding live row addresses but
committed the union of the sources' stored (historical) fragment bitmaps for
it. The manifest then disagreed with the file, and a merge could take over
only part of a destination that an unselected sibling still helped cover.

The merged segment now commits exactly the live fragments its file is written
for: the derived coverage of the selected segments plus the newly indexed
fragments. A selection is merged only as a whole, and only when

- every selected segment is registered in the snapshot's translation plan
  (one the reader derives nothing for contributes no coverage and is replaced,
  as before),
- no unselected segment of the same index derives coverage of a fragment the
  selection covers, and
- the selection covers none of the fragments being newly indexed.

Otherwise the old segments are left untouched and only the unindexed
fragments are indexed, as in append mode (or nothing is done when there are
none). Tables without a tagged history are unaffected.

Segments merged by earlier releases keep their provenance bitmaps and remain
readable; this change does not rewrite them.

Co-Authored-By: Claude <[email protected]>
@LuQQiu

LuQQiu commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@LuQQiu
LuQQiu force-pushed the lu/optimize-merge-live-coverage branch from 1a8a4e4 to 4d3181f Compare December 1, 2026 18:16
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Dec 1, 2026

@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.

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

Still open from earlier reviews (2):

  • Unresolved: 2 minor or pre-existing.

@LuQQiu
LuQQiu merged commit a2d0656 into main Dec 1, 2026
43 of 45 checks passed
@LuQQiu
LuQQiu deleted the lu/optimize-merge-live-coverage branch December 1, 2026 20:29
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