Skip to content

fix(index): fold data overlays into a remapping scalar segment merge - #9941

Merged
westonpace merged 7 commits into
lance-format:mainfrom
rerun-io:upstream/merge-stamp-current-version
Oct 22, 2026
Merged

westonpace merged 7 commits into
lance-format:mainfrom
rerun-io:upstream/merge-stamp-current-version

Conversation

@amunra

@amunra amunra commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Overview

This change improves index segment merging and resolves (for a subset of index types) the tension between:

  • Clearing FRI entries - Which require indices rebuilt against the latest data.
  • Overlay Lookups - Which invalidate index segment data.

The Problem

Each index segment tracks an IndexMetadata.dataset_version, also known as "the stamp".

An FRI entry can only be removed from a dataset's manifest once every index segment's stamp is newer than the version it records.

Overlays, however, get applied based on the dataset manifest version, and to ensure correctness #7244 changed the merge logic so that the stamped version is the minimum of the various segments being merged. This is because the earlier logic of merging and stamping latest would effectively discard the overlays, since a merge copies the source segments' entries rather than re-reading them.

This introduced the "FRIs don't clear" bug!

This PR's solution

The solution is, during index merge, to squash down the overlays by re-reading the overlaid rows from the fragments and stamp the merged segment with the manifest version the merge read.

This is only possible for index types that remap their entries through the FRI when merging (can_remap). The other index types carry their entries over untranslated, so their merges are still stamped with the minimum version of the segments being merged and still retain the "FRIs don't clear" bug.

index type can_remap stamp after a merge FRI rows overlaid after a source was built
BTree, Bitmap, LabelList, NGram yes manifest version drains below the stamp on the next trim re-read; merged file holds current values
JSON that of its target index as the target as the target as the target
ZoneMap, BloomFilter, RTree no oldest source, as before stays pinned until a rebuild or remap_column_index carried over as built; masked at read time

Testing

Tests pin four properties of a merge over fragments with overlays, for BTree, Bitmap, NGram and LabelList, with and without stable row ids:

  • The merged segment is honest. It is stamped with the manifest version and covers every fragment, and its file holds the overlaid values (NULL overrides included) and none of the replaced ones. Queries match an index-disabled oracle and the planner answers from the index alone, so no masking is left to do.
  • It is a merge, not a rebuild. A merge reads none of the base data files of the fragments its sources already cover; a merge over a large fragment with one overlaid row reads a fraction of that fragment's file. Existing merge tests that told merge from rebuild by the stamp, which no longer distinguishes them, now use this read-based check.
  • It stays in scope. A stale fragment under a segment not selected for the merge is left alone, and a second optimize is a no-op.
  • The FRI drains. A merge over a segment that predates a deferred-remap compaction lets the next trim drain the reuse index, and the tagged-upgrade tests that pinned the legacy version through a merge now assert it is released.

ZoneMap serves as the control: its merge keeps the oldest source's stamp.

Related

Compatibility

Metadata-only: a merged segment's stamp changes and its file holds the overlaid values. No format change. Existing segments stay valid. Rolling back is safe.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the bug Something isn't working label Oct 13, 2026
@amunra
amunra force-pushed the upstream/merge-stamp-current-version branch from 2279ae0 to 41314cd Compare October 13, 2026 13:57
@amunra
amunra marked this pull request as ready for review October 13, 2026 14:45

@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 pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 13, 2026
@amunra
amunra force-pushed the upstream/merge-stamp-current-version branch from 41314cd to 830518b Compare October 15, 2026 17:07
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 15, 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 merge preserves current overlay values while advancing the stamp for FRI cleanup. The rebase onto #9481 correctly separates BTree's address-domain filtering from _rowid scan selection. Targeted tests passed for both row-ID modes, NULL overrides, sparse reads, tagged upgrades, and legacy BTree rebuilds.

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

@westonpace westonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall I think this is moving in the right direction. Claude has one concern about overlap between this feature and tagged FRIs.

I think we could potentially skip the folding for now when tagged FRIs are used and just file a follow-up issue if you'd like. I would guess there are still some other kinks to work out in the entire overlay + tagged FRI combination.

Otherwise you could add the test it describes and then figure out the fix for it. Let me know how you want to proceed.


The FRI provenance bitmap (tagged tables)

With defer_index_remap, compaction moves rows to new fragments but leaves index files unchanged. The fragment reuse index (FRI) records the moves, and readers translate old addresses to new ones when they read.

The two FRI formats treat a segment's fragment_bitmap differently:

  • v0 (non-tagged): the stored bitmap is coverage. When the FRI is applied at load time (remap_fragment_bitmap), source fragment ids are swapped for destination ids.
  • Tagged: the stored bitmap is provenance. It lists the fragments the segment was originally built over, which may now be retired sources. Live coverage (the destinations the entries now resolve to) is derived separately from the tagged history. dataset.load_indices() and tagged_segment_coverage return that derived coverage. load_all_indices returns the raw stored bitmap.

Why that matters for finding 2

  1. optimize_indices gets its segments from load_all_indices (index.rs, around line 3242), so segment.fragment_bitmap is the provenance bitmap.
  2. The merge does compute derived live coverage (effective_old_frags, append.rs:793) and copies old entries for those destination fragments.
  3. Stale detection, however, tests covers_fragment against the stored provenance bitmap. A destination fragment isn't in it.

So this sequence goes wrong:

  1. A segment is built over fragments {0, 1}.
  2. A deferred compaction moves those rows into fragment 5. The stored bitmap stays {0, 1}, and the derived coverage becomes {5}.
  3. An overlay changes the indexed column on fragment 5.
  4. optimize merges. Detection doesn't consider fragment 5 covered, so it finds no stale rows. The old entries for fragment 5 are copied unchanged, and the merged segment is stamped current.

Masking then never fires, and filter queries return rows by their replaced values.

Before the PR the same rows were masked: the stamp stayed old, and query-time masking uses the derived coverage. The fix is to run detection against the coverage from tagged_coverage when it's present, which the merge already computes.

I confirmed this by tracing the code, not with a test. The tagged-upgrade tests in the PR don't appear to put an overlay on a compaction destination. A test that does would settle it.

amunra and others added 7 commits October 22, 2026 18:53
The Bitmap merge tests pinned the merge path by the stamp: a merge kept
the oldest source's `dataset_version`, a rebuild took the manifest's. A
merge is about to be stamped with the manifest too, so check the IO log
instead: a rebuild reads the already-indexed data files, a merge never
does.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…masking tests

Two tests build their own one-segment-per-fragment index and pick the
builtin params for an index type inline. Hoist both into helpers.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`scan_training_data` takes a `TrainingRows` selection: every row of a
list of fragments, as before, or exactly a set of rows keyed like `_rowid`,
which becomes an allow-list prefilter on the same sorted scan. The
unindexed-data loader of a scalar merge passes the selection through; with
its fixed arguments that loader is a plain call of the scan, so it calls it
directly. Every caller still selects by fragment, so nothing changes yet.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`IndexMetadata.dataset_version` is a segment's watermark for two readers.
`cleanup_frag_reuse_index` retires fragment-reuse versions at or below it,
and overlay masking trusts the index for data overlays committed at or
below it and re-reads the rows of newer ones.

Since lance-format#7244 `merge_scalar_indices` stamps a merged segment with the oldest
source's version. That keeps masking correct, because the entries a merge
carries over never incorporate an overlay, but it also means a merge can
never retire a reuse version: any source that predates it pins it for the
whole dataset, and with deferred index remap the reuse index only drains
through a rebuild or an explicit `remap_column_index`.

For the types that translate their entries through the reuse index while
merging (`can_remap`: BTree, Bitmap, LabelList, NGram, JSON when its target
does), stamp the merged segment with the manifest version the merge read
and make that stamp true: per source segment, find the rows whose indexed
field an overlay replaced after the source was built, using the same
per-row, field-aware, version-gated check the scanner masks by; read those
rows, with their overlays resolved, in the one sorted scan that already
indexes the newly appended fragments; and drop their carried-over entries
through each source's old-data filter, which becomes an allow-list of the
kept fragments' rows minus the stale ones. The merged segment covers every
live fragment with current values, nothing is read twice, and the sort is
over the appended and overlaid rows only. Types that do not remap keep the
oldest source's stamp and carry everything over. A seed is written with
its data file, before any overlay, so the seed path is skipped when a
merge must read overlaid rows.

The tests whose expectations this flips move with it: the NGram overlay
test now asserts the rescan, with and without stable row ids; the ZoneMap
control keeps the oldest source's stamp while BTree and Bitmap take the
manifest's; the tagged-upgrade tests that pinned the legacy version
through a merge now assert it is released.

Ported in part from #77.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
For BTree, Bitmap and LabelList, with and without stable row ids: full
coverage and the manifest stamp after one optimize; the merged file holds
the overlaid values, including a NULL override, and none of the replaced
ones; a merge over a large fragment with one overlaid row reads a fraction
of its data file; a stale fragment under a segment the merge did not
select is left alone; the scanner matches an index-disabled oracle and
plans no scan; a second optimize changes nothing. A BTree merge over a
segment that predates a deferred-remap compaction asserts the trim then
drains the reuse index and the plan answers from the index.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
lance-format#9481 moved BTree to the row-address domain, so the domain of a segment's
old-data filter is now a property of the index type, not of the dataset:
`build_old_data_filter` keeps row ids only when the dataset uses stable row
ids *and* the index does not store addresses. `overlay_rescan` still read
that domain off the manifest alone and translated its stale rows to row ids
whenever stable row ids were on, which for a BTree now subtracts row ids
from a set of addresses -- the two do not intersect, so every stale overlay
posting the merge is meant to drop is carried over instead.

Pass the domain in and apply upstream's condition. The scan selection keeps
translating unconditionally: it feeds `Scanner::with_row_id_prefilter`,
which is the `_rowid` domain whatever the index stores.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The existing merge coverage overlays fragment 0, whose addresses are
`0 << 32 | offset` -- the same integers as its row ids in a freshly written
dataset. A stale set built in one domain and used in the other is therefore
indistinguishable there, and the case passes whichever domain it is keyed
to. Fragment 1 tells them apart: address `1 << 32 | 1` against row id 7.

Same BTree/Bitmap x stable-row-ids matrix as its neighbour. The Bitmap
cases are the control: a stale set keyed to the index rather than narrowed
to it fails those instead of the BTree one.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@amunra
amunra force-pushed the upstream/merge-stamp-current-version branch from 830518b to 014741f Compare October 22, 2026 16:59
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 22, 2026
@westonpace

Copy link
Copy Markdown
Member

Based on external discussion let's go ahead and merge as-is and leave Claude's concern for a follow-up. I'll wait for CI to pass.

Copy link
Copy Markdown
Member

Re: the FRI provenance bitmap concern — filed as a follow-up: #10245


Generated by Claude Code

westonpace pushed a commit that referenced this pull request Oct 23, 2026
…ist merges

Two gaps surfaced only after merging main's overlay-rescan commit
(fix(index): fold data overlays into a remapping scalar segment merge,
#9941), which this branch predates:

- The LabelList merge arm in merge_scalar_indices built its
  old_data_filter but never passed it through block_stale_rows like
  every sibling arm does, so a fragment overlaid after the segment was
  built kept its stale postings alongside the freshly rescanned ones.
  Reproduced by the newly-merged
  test_label_list_update_rescans_overlay_stale_fragment.

- The address_domain flag passed to overlay_rescan (which determines
  how stale_rows is keyed) was hardcoded to `index_type == BTree`. That
  was correct before this PR, when BTree was the only address-domain
  scalar index. Now that Bitmap and LabelList can also be address-domain
  under stable row ids, the flag must cover all three: the
  legacy_domain_mismatch guard above already guarantees any Bitmap or
  LabelList segment still reachable here is address-domain (or the
  dataset lacks stable row ids and the domains coincide), matching the
  reasoning already used for the LabelList arm's own old_data_filter.
  Reproduced by
  test_remapping_merge_rescans_overlay_stale_rows_off_fragment_zero
  ::case_2_bitmap::stable_row_ids_2_true.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01HFbf8RxqsVZ3yyhLwQRSWP
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