Registry Mirror
Skip to content

Conversation

@jackye1995

Copy link
Copy Markdown
Contributor

Problem

merge_existing_index_segments for FM-Index always rebuilds the segment from source text — correct for multi-source merges (there is no cheap way to combine two BWT structures), but wasted work when merging a single segment whose fragment coverage is unchanged.

This makes distributed FM-Index builds pay for everything twice: workers build per-fragment-subset segments via execute_uncommitted, and when intermediates fold 1:1 into final segments, the merge step discards each one and rebuilds identical content over the same fragments.

Fix

After computing the merged coverage (union of source bitmaps ∩ live fragments), if there is exactly one source segment and its coverage is still fully live, return it as-is. The caller already re-stamps dataset_version/fields, and the segment (from execute_uncommitted) carries its index_details and files, so commit-side validation is unaffected.

Multi-source merges, empty coverage, and single segments with retired fragments (concurrent compaction) keep the existing rebuild behavior — the new test_fmindex_merge_single_segment_rebuilds_when_coverage_shrinks test guards that boundary.

…egment

FM-Index merge_segments always rebuilds from source text, even for a
single source segment whose fragment coverage is unchanged. Distributed
builds that fold intermediates 1:1 into final segments therefore build
every segment twice.

Return the source segment as-is when there is exactly one and its
coverage is still fully live. Multi-source merges and segments with
retired fragments keep the rebuild behavior.
@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.31507% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
rust/lance/src/index/scalar_logical.rs 99.30% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@jackye1995
jackye1995 merged commit 7134acf into lance-format:main Jul 2, 2026
29 of 30 checks passed
@jackye1995
jackye1995 deleted the fm-merge-passthrough branch July 3, 2026 03:43
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants