Skip to content

fix(index): preserve segment coverage across compaction - #9271

Closed
lance-gatefixer[bot] wants to merge 5 commits into
mainfrom
gatekeeper/fix-9270-1
Closed

lance-gatefixer[bot] wants to merge 5 commits into
mainfrom
gatekeeper/fix-9270-1

Conversation

@lance-gatefixer

@lance-gatefixer lance-gatefixer Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary\n\n- remap aggregate caller-supplied scalar segment coverage through deferred-compaction fragment reuse metadata\n- rebuild FM sources after coverage remapping so their row addresses match the current fragment space\n- reject merges when required reuse history is missing, partially retained, or source coverage only partially spans a rewrite group\n- extend the eight-family regression across two rewrite groups with missing-history, mixed-history, partial-group, exact-coverage, and live-address checks\n\n## Root cause\n\nMost scalar merge helpers loaded row addresses through the fragment-reuse remapper, but several helpers first intersected each staged segment old fragment bitmap with the current manifest. Compaction had retired every old fragment, so that filter became empty. Coverage also cannot be remapped per segment because staged segments may jointly, rather than individually, cover a compaction rewrite group.\n\nFM loading is the exception: it does not apply the fragment-reuse row-address remapper. After group coverage was moved to current fragments, its single-segment passthrough could therefore advertise current coverage while retaining retired physical row addresses. Uncommitted segments also do not pin reuse history, so a delayed merge must fail explicitly if the required mapping has already been trimmed. A retained newer mapping can make the group appear applicable even when an older required mapping was trimmed, so the remapped union must also be checked for any remaining retired fragments. Likewise, partial rewrite-group coverage cannot be represented safely and must not be published as current.\n\n## Validation\n\n-

running 1 test
test index::create::tests::test_merge_uncommitted_segments_across_deferred_compaction::case_2_btree ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 3870 filtered out; finished in 0.10s\n-
running 8 tests
test index::create::tests::test_merge_uncommitted_segments_across_deferred_compaction::case_6_fm ... ok
test index::create::tests::test_merge_uncommitted_segments_across_deferred_compaction::case_3_zonemap ... ok
test index::create::tests::test_merge_uncommitted_segments_across_deferred_compaction::case_1_bitmap ... ok
test index::create::tests::test_merge_uncommitted_segments_across_deferred_compaction::case_8_label_list ... ok
test index::create::tests::test_merge_uncommitted_segments_across_deferred_compaction::case_4_bloomfilter ... ok
test index::create::tests::test_merge_uncommitted_segments_across_deferred_compaction::case_2_btree ... ok
test index::create::tests::test_merge_uncommitted_segments_across_deferred_compaction::case_7_inverted ... ok
test index::create::tests::test_merge_uncommitted_segments_across_deferred_compaction::case_5_ngram ... ok

test result: ok. 8 passed; 0 failed; 0 ignored; 0 measured; 3863 filtered out; finished in 0.23s\n-
running 11 tests
test index::tests::test_merge_existing_index_segments_rejects_mismatched_covering_fields ... ok
test index::scalar_logical::tests::test_merge_existing_index_segments_rejects_mismatched_zonemap_params ... ok
test index::tests::test_merge_existing_index_segments_preserves_covering_scalar::case_2_explicit ... ok
test index::scalar_logical::tests::test_merge_existing_index_segments_supports_zonemap_segments ... ok
test index::tests::test_merge_existing_index_segments_preserves_covering_scalar::case_1_struct_update ... ok
test index::create::tests::test_label_list_merge_existing_index_segments_drops_retired_fragments ... ok
test index::tests::test_merge_existing_index_segments_preserves_covering_rtree ... ok
test index::tests::test_merge_existing_index_segments_preserves_covering_vector ... ok
test index::create::tests::test_btree_merge_existing_index_segments ... ok
test index::create::tests::test_label_list_merge_existing_index_segments ... ok
test index::create::tests::test_merge_existing_index_segments_supports_fts_segments ... ok

test result: ok. 11 passed; 0 failed; 0 ignored; 0 measured; 3860 filtered out; finished in 0.11s\n-
running 1 test
test index::scalar_logical::tests::test_ngram_segment_merge_rebuilds_after_deferred_compaction ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 3870 filtered out; finished in 0.28s\n- \n- \n\nFixes #9270\n\n

@github-actions github-actions Bot added the bug Something isn't working label Sep 16, 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 16, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Sep 16, 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 Sep 16, 2026
@xuanyu-z

Copy link
Copy Markdown
Contributor
  1. The fix is silently inert if the fragment-reuse index was trimmed first. remap_merged_segment_coverage returns early when open_frag_reuse_index yields nothing or fragment_reuse_affects_segments is false — falling straight back to the broken intersect-with-current path. And nothing holds the FRI open for staged segments: the trim gate iterates indices loaded from the manifest, and uncommitted segments aren't in it. Worse, is_index_remap_caught_up treats a bitmap disjoint from the rewrite chain as caught up, so the committed indices happily clear the version for trimming while the staged segments still need it. In a distributed build the build→merge window can be long (retries, coordinator restart), so this is reachable. Severity: recoverable — the merged index covers nothing and an optimize_indices would rebuild it — but it's the original symptom returning, just as silently.

  2. A partial rewrite group still yields empty coverage, now stamped as current. If the union straddles (segments jointly cover only part of a group), the remap drops it and merged_coverage comes back empty. Every segment gets an empty bitmap, and because has_remapped_source_coverage is true, the index is stamped with self.manifest.version. An empty bitmap is disjoint from every chain, so it always reads as "caught up" and never blocks a future trim. FM handles empty explicitly ("produce an empty index"); the generic path does not distinguish "legitimately empty" from "we just lost the group". At minimum the union straddle deserves the same tracing::warn! the per-group straddle already emits.

  3. Test gaps. The regression covers all eight families × compacted/not, asserting both row counts and live addresses — good. Not covered: the straddle/partial-group case, the FRI-absent path, segments spanning two rewrite groups, and the vector/RTree exemptions (untested in the issue too).

@lance-gatefixer

Copy link
Copy Markdown
Contributor Author

Addressed in f92ad04: scalar segment merges now reject retired coverage when no applicable reuse mapping survives and reject partial rewrite-group coverage before it can be stamped current. The regression now spans two rewrite groups, asserts exact live coverage and row addresses, and exercises both failure modes. Vector and RTree remain intentionally outside this scalar remap path because their dispatch behavior is unchanged.

@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Sep 18, 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 18, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Sep 18, 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 revised coverage reconstruction now fails closed when retained fragment-reuse history cannot account for every retired source fragment. The two-compaction regression covers the previously incomplete merge path while retaining the group-level remap and FM rebuild needed to keep advertised coverage aligned with returned row addresses.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Sep 18, 2026
@xuanyu-z

Copy link
Copy Markdown
Contributor

I believe this PR lacks some of the fixes, therefore I'm starting #9421 and fixing there

jackye1995 pushed a commit that referenced this pull request Sep 23, 2026
…ndex (#9421)

Supersedes #9271; merging this closes #9270.

## Merging segments across a compaction

`merge_existing_index_segments` combines staged segments into one. When
a
compaction has retired the fragments those segments were built over,
their
coverage has to be resolved against the current manifest before the
merge can
mean anything.

Resolving each segment on its own cannot do that. A segment covering one
fragment of a three-fragment rewrite group is a straddle, and the
remapper drops
straddles rather than claim a fragment holding rows the segment never
indexed —
so every segment of the group resolves to nothing and the merge produces
an
empty index. This affected five of eight scalar families.

The merge owns the whole group, so it remaps the *union* of its
segments, which
is complete where each segment alone is not. FM segments are rebuilt
rather than
passed through, because a passthrough returns coverage in the new
fragment space
with row addresses still in the old one.

## Coverage may only move with the row addresses

`open_scalar_index` takes its remapper from the dataset's reuse index.
Where that
mapping is missing, the addresses stay in the old fragment space and the
coverage
must not advance to the new one. An index claiming coverage it cannot
serve is
worse than one covering less: a short claim falls back to a flat scan, a
false
claim suppresses that fallback and returns nothing.

So coverage shrinks where no mapping applies, and the three ways that
loses rows
are reported:

- **No applicable mapping over retired fragments.** They are intersected
away and
their rows leave the index. Partly retired counts, since the surviving
half
  would otherwise make the coverage look healthy.
- **A rewrite chain only partly mapped.** Fragments compacted more than
once need
  every link; a trim that dropped an earlier one leaves the remap on an
  intermediate fragment the dataset no longer has.
- **A straddling union.** The segments together cover only part of a
rewrite
group. The remapper reports the group it heals, but not that here it
costs the
  entire merged index rather than the straddling rows.

None is an error. A shrunk coverage is correct when the rows were
deleted, and at
that point nothing distinguishes that from a mapping that was trimmed. A
merge
whose coverage shrank rebuilds rather than fails, which
`test_fmindex_merge_single_segment_rebuilds_when_coverage_shrinks`
(#7569) holds.

## Index families the remap does not cover

Vector cannot remap here: its merge hands the segments to the
distributed file
merger with an object store and a directory, reaching no dataset and so
no reuse
index. Vector indexes are remapped through `optimize_vector_indices_v2`,
which
passes the reuse index to the builder.

RTree does load through the remapper, so it belongs under this rule, but
the
`all_rtree` branch writes the same coverage field from its own staleness
pruning
— which drops fragments whose indexed files changed, a different
question from
which fragments a rewrite replaced. Bringing RTree under the remap means
settling
how the two compose, and no test yet drives an RTree index through a
deferred
compaction. Both families shrink coverage rather than over-claim, so the
uncovered case costs a flat scan rather than a wrong answer.

An RTree merge without the `geo` feature is refused before the coverage
work
rather than after it.

---------

Co-authored-by: Gatefixer <312823363+lance-gatefixer[bot]@users.noreply.github.com>
@xuanyu-z

Copy link
Copy Markdown
Contributor

9421 was merged, which covers everything here, this should be closed

@Xuanwo

Xuanwo commented Oct 26, 2026

Copy link
Copy Markdown
Member

Closing as superseded by #9421, with follow-up fixes in #9524 and #9946. The regression coverage from this PR is covered by the tests on main; no unique regression tests remain to migrate.

@Xuanwo Xuanwo closed this Oct 26, 2026
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.

2 participants