Repository navigation
fix(index): let a remapping scalar merge advance its version stamp - #77
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Some scalar paths could discard required remapping, while inverted merges retain stale version stamps.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Updates scalar-index merge metadata to use the manifest version and adds BTree fragment-reuse regression coverage.
Changes:
- Stamps generic merged scalar segments with the current manifest version.
- Tests remap cleanup and query correctness after a BTree merge.
File summaries
| File | Review |
|---|---|
rust/lance/src/index/append.rs |
Requires fixes for unsafe ZoneMap, BloomFilter, and RTree stamping, plus stale inverted-index merge stamps. |
rust/lance/src/dataset/optimize.rs |
Adds BTree merge, cleanup, and query regression coverage. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
c5147ad to
0becb79
Compare
`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 treats data overlays committed at or below it as incorporated. Since lance-format#7244 `merge_scalar_indices` stamps a merged segment with the oldest source segment's version. That keeps overlay masking correct, because the entries a merge carries over never incorporate overlays, but it also means a merge can never retire a fragment-reuse version: any source that predates the version 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 index types that translate their entries through the fragment-reuse index while merging (`can_remap`: BTree, Bitmap, LabelList, NGram, and JSON when its target does), stamp the merged segment with the manifest version it was built from. Per source segment, remove from the merged coverage every carried-over fragment with an overlay on an indexed field committed after that segment was built; freshly scanned fragments already reflect their overlays and are left alone. Uncovered fragments fall back to a scan and are re-indexed by the next optimize, the same treatment `remap_column_index` applies. The overlay test moves into `dataset::overlay::fragment_has_newer_indexed_overlay`, shared by both call sites. Types that do not remap keep the oldest source's stamp. The existing NGram overlay test now asserts the merged stamp equals the manifest version the merge read, that the overlaid fragment leaves the coverage while the untouched one stays, and that a second optimize restores the coverage past the overlay; its query assertions are unchanged. The new BTree test merges appended rows into a segment that predates a deferred-remap compaction, asserts the stamp, asserts the trim then drains the reuse index, and checks the plan answers from the index for every fragment with the correct count. Co-Authored-By: Claude Fable 5.1 <[email protected]>
0becb79 to
7f2af3f
Compare
There was a problem hiding this comment.
Review of the approach, with one blocking question, one design concern, and a few cleanups. The context and pseudocode below are what the inline comments refer back to; the interaction between stamps, coverage and the merge is easy to lose track of, so it is spelled out once here.
Context: the problem
IndexMetadata.dataset_version is one field read by two consumers that want opposite things:
- Overlay masking (read path) treats
overlay.committed_version <= index.dataset_versionas "already incorporated, trust the index". It wants the stamp low enough to be honest about values. - FRI cleanup (
is_index_remap_caught_up) treatsindex.dataset_version < reuse.dataset_versionas "not caught up, keep the reuse version". It wants the stamp high enough to reflect that addresses are current.
A merge copies index entries between segments without re-reading the column, so its entries are exactly as value-stale as their source. But it opens each source through open_scalar_index, which installs the FRI as a RowIdRemapper, so every copied entry comes out with current addresses. A merged segment is therefore as stale as its oldest source on values and fully current on addresses, with one u64 to say both.
Lance 10 (lance-format#7244) stamps min(source stamps). That is the only honest single value for overlays, and it is a lie for the FRI: a merge can only lower or hold the stamp, so a merged segment can never pass the < check, and the FRI never drains under a maintenance loop that relies on consolidation to advance the watermark.
Context: what this branch does
In pseudocode, with a tiny scenario:
# inputs
sources = [A, B] # the old segments being merged
manifest_version = 9 # the data version the merge is reading
coverage = A.bitmap ∪ B.bitmap # = {10, 20}
segment A stamp 1 covers fragment 10
segment B stamp 5 covers fragment 20
fragment 10 has an overlay committed at version 3, on the indexed field
fragment 20 has no overlays
# step 0 — the merge itself
created_index = merge(A's entries, B's entries, freshly-read unindexed fragments)
# step 1 — the stamp
new_segment.dataset_version = manifest_version # 9, not min(A, B) = 1
# step 2 — the prune (the loop at append.rs:613-628)
for each source S in sources: # A, then B
for each fragment F that S covers: # A → {10}, B → {20}
for each overlay O on F:
if O.committed_version > S.dataset_version: # S was built before O existed
if O touches a field this index covers:
coverage.remove(F) # stop claiming F
new_segment.fragment_bitmap = coverage # {20}
Walking it: A (stamp 1) covers fragment 10, which has an overlay at 3; 3 > 1, so fragment 10 is removed. B (stamp 5) covers fragment 20, no overlays; kept. Result: stamp 9, coverage {20}.
Why step 2 is needed: with a stamp of 9, the reader's gate reads 3 <= 9 as "already incorporated" and would trust A's pre-overlay entries for fragment 10. Dropping fragment 10 from coverage avoids making that claim; the reader scans fragment 10 instead.
What happens to fragment 10 afterwards: it is indistinguishable from freshly appended, never-indexed data. The scanner reads it directly. The next optimize_indices indexes it as a delta segment (current values, overlay applied). The optimize after that folds the delta into the base. So the fragment is out of the index for one maintenance cycle and takes two more passes to be consolidated again.
This is the same advance-and-prune shape the codebase already uses in remap_index and prune_overlay_stale_fields_from_indices, so it is idiomatic here. It trades row-level overlay masking for fragment-level fallback on the merge path, in exchange for the FRI actually draining. That is the right trade. On datasets that never write overlays (no public write API produces one; every constructor in the tree is in a mod tests block), step 2 is a no-op and the change reduces to restoring the pre-lance-format#7244 stamp.
Findings
Blocking question — inline at overlay.rs:78. The write-side predicate matches fields by exact id; the reader gate (overlay_exclusion_offsets) matches through schema ancestry in both directions. Because this PR advances the stamp, an overlay the reader would have masked but this predicate does not catch stops being masked. That is a regression relative to base for that class, which base handled by keeping the stamp low.
Design concern — inline at append.rs:624. Step 2 runs after step 0, so the merge reads, merges and writes fragment 10's entries and then makes them unreachable: wasted work on a step that is already a bottleneck for us, and an oversized index whose contents do not match its coverage. Separately, an optimize_indices call that returns a smaller index ({10, 20} → {20}) is a surprising contract. The inline comment proposes computing the prune set before the merge, excluding those fragments from the per-source copy, and re-reading them fresh in the same merge so coverage is preserved. The plumbing for both halves already exists in merge_scalar_indices.
Rebase. release-10.0.0 has moved to 1985a2a29 (#62, Bitmap segment merge). has_segment_merge_primitive is now BTree | Bitmap | NGram, so multi-segment Bitmap consolidation takes the merge path and inherits the min stamp too, where it previously rebuilt and got a fresh stamp. The fix covers it (Bitmap::can_remap() is true), but there is no Bitmap test, and the downstream regression test uses a Bitmap index.
Cleanups — inline. Orphaned doc comment at overlay.rs:64; the prune loop is sources × all manifest fragments rather than sources × covered fragments at append.rs:616.
For the upstream PR. This reverses the stamp assertion in test_ngram_optimize_preserves_overlay_staleness, which lance-format#7244 pinned deliberately. Keeping the query assertions intact is the right argument to lead with; expect the stamp reversal to be the crux of that discussion.
This review was drafted with the help of an LLM (Claude) and checked against release-10.0.0 @ 1985a2a29 and this PR's head.
| .data_file | ||
| .fields | ||
| .iter() | ||
| .any(|field_id| indexed_fields.contains(field_id)) |
There was a problem hiding this comment.
Predicate is narrower than the reader gate it claims to mirror.
This matches by exact field id. overlay_exclusion_offsets (just below) matches through field_ancestry_by_id in both directions: an overlay field that is a descendant of the indexed field, or an ancestor of it, counts.
On base that gap is harmless, because the merge keeps the min stamp and the reader masks regardless. This PR advances the stamp, so a fragment carrying an overlay the reader would mask but this predicate does not catch stays in coverage with a stamp past the overlay. The reader's gate then reads committed_version <= dataset_version as "incorporated" and serves the pre-overlay entries. That is a regression relative to base for that class.
The same exact-match form already exists in remap_index and prune_overlay_stale_fields_from_indices, so this is consistent with precedent rather than new, but it adds a third reachable instance and the first on a path that previously stayed safe.
Two ways to resolve:
- Can an index's
fieldsever hold a non-leaf id (list, struct, JSON targets)? If not, the reader's ancestry handling is dead code and the narrow form is fine everywhere; worth a comment saying so. - If it can, this predicate should use the same ancestry logic as the reader, ideally factored so the two cannot drift, plus a nested-field test.
(Tombstoned fields are fine as written: TOMBSTONE_FIELD_ID is -2 and never matches a real id.)
| /// only non-indexed fields contributes nothing. An overlay whose | ||
| /// `committed_version <= index_version` is already incorporated by the index and | ||
| /// is ignored. | ||
| /// Whether `fragment` carries an overlay on one of `indexed_fields` that was committed after |
There was a problem hiding this comment.
The new function landed between overlay_exclusion_offsets's doc comment (lines 53-63) and its signature. As written, overlay_exclusion_offsets now has no docs and this function has two stacked doc blocks, the first of which describes the other function. Rustdoc will attach both to fragment_has_newer_indexed_overlay.
| else { | ||
| continue; | ||
| }; | ||
| for fragment in dataset.manifest.fragments.iter() { |
There was a problem hiding this comment.
This is sources × every fragment in the manifest, not sources × covered fragments. On a dataset with ~100k fragments and several segments that is millions of bitmap contains calls per merge, for a condition that is false on any dataset without overlays.
Either iterate carried_over directly and look fragments up, or test fragment.overlays.is_empty() before the contains so the common case short-circuits on the cheaper check.
| segment.dataset_version, | ||
| ) | ||
| { | ||
| frag_bitmap.remove(fragment.id as u32); |
There was a problem hiding this comment.
Two related issues with the ordering here. The pseudocode and the A / B / fragment-10 scenario this refers to are in the review summary; step numbers below are from there.
Issue A: the merge does work that this loop immediately discards
Step 0 (the merge) runs before step 2 (this loop). By the time frag_bitmap.remove(fragment.id) fires for fragment 10, the merge has already:
- opened source A and read every one of its entries for fragment 10 (through the FRI remapper),
- merged them with B's entries and the fresh data, and
- written them into the new index file.
The bitmap edit then makes those entries unreachable. They are dead weight in the file until the next merge's per-source filter (build_per_segment_filters, which reads the coverage bitmap) drops them. So the cost of reading, merging and writing fragment 10's entries is paid in full and the result is thrown away, and the output is an oversized index whose contents do not match its coverage.
This matters to us concretely: index optimization is a significant bottleneck in our maintenance loop today, and this ordering adds merge work proportional to the pruned fragments' entries on every consolidation where an overlay is present.
Everything step 2 needs is metadata (source bitmaps, source stamps, fragment overlay lists), none of it requires the merged file, so step 2 can run before step 0.
Issue B: an optimize step shrinks the index's coverage
Before this merge the index covered {10, 20}. After optimize_indices returns, it covers {20}. An optimization step has reduced what the index answers for, and it takes two more optimize passes to get back to where it started.
That is a surprising contract for optimize_indices. Users expect it to catch the index up to the data (coverage goes up or stays the same), not to hand back a smaller index. remap_index and prune_overlay_stale_fields_from_indices do shrink coverage, but they run inside compaction/remap, not inside the step whose stated purpose is to make the index better.
Proposal: precompute the prune, exclude those fragments from the copy, re-read them in the same merge
# before the merge
stale = {}
for each source S in sources:
for each fragment F that S covers:
if any overlay O on F has O.committed_version > S.dataset_version
and O touches a field this index covers:
stale.add(F) # → {10}
# feed the merge two adjusted inputs
per_source_coverage[S] = S.bitmap − stale # A → {}, B → {20}
# (build_per_segment_filters uses these,
# so A's fragment-10 entries are never read)
unindexed = unindexed ∪ stale # fragment 10 is re-read from the data,
# overlays applied, via load_unindexed_training_data
# the merge, once
created_index = merge(filtered source entries, freshly-read unindexed incl. fragment 10)
new_segment.dataset_version = manifest_version # 9 — honest for every fragment now
new_segment.fragment_bitmap = A.bitmap ∪ B.bitmap ∪ unindexed # {10, 20}: coverage preserved
Properties:
- No wasted copy. Fragment 10's stale entries are never read from A; the per-source filter excludes them.
- No bloat. The output file contains exactly the entries its coverage admits to.
- Coverage preserved. The index still covers
{10, 20}after optimize. Fragment 10's entries are fresh (current values, overlay applied), so the manifest-version stamp is honest for it too. - Same total work, done once. Re-reading fragment 10 is exactly what the next optimize pass would have done anyway; this pulls it into the current pass and skips the intermediate bloated file and the two follow-up passes.
- A fully-stale source can be skipped outright. If every fragment A covers is in
stale, A drops out ofselected_old_indicesand is never opened.
The plumbing exists already: merge_scalar_indices takes unindexed: &[Fragment] and reads it fresh through load_unindexed_training_data → dataset.scan(), which applies overlays. build_per_segment_filters builds one filter per source from that source's effective coverage, so subtracting stale from those bitmaps before the filters are built is enough to exclude the copies. Both would need to happen inside merge_scalar_indices (or the inputs adjusted before the call).
One trap: frag_bitmap also feeds the rebuild branch's scan set (rebuild_scalar_segment(…, frag_bitmap.iter().collect())). In that branch every fragment is re-read from the data, so overlays are incorporated and pruning would wrongly drop a fragment the rebuild indexes correctly. The precompute must stay gated on can_merge_segments, exactly as this post-hoc loop already is.
Scope
On a dataset with no overlays, stale is always empty and none of this changes anything. The cost in Issue A and the surprise in Issue B only appear when overlays exist. But those are also the only conditions under which the current loop does anything at all, so if the loop is worth having, it is worth having in the cheaper, coverage-preserving form.
Brings in #62 (Bitmap segment merge), which routes multi-segment Bitmap consolidation through the merge path this fix covers. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
fragment_has_newer_indexed_overlay was inserted between the doc comment of overlay_exclusion_offsets and its signature, so rustdoc attached both blocks to the new function and left overlay_exclusion_offsets undocumented. Move the new function above the doc block. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The prune loop iterated every fragment in the manifest once per source segment, for a condition that is false on any fragment without overlays. Collect the overlaid fragments once and iterate those, so the common no-overlay case does no per-segment work. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Status: accepted as a fork change; not upstreamable in this formPushed three commits on top of
We are taking this as-is under What blocks proposing this upstreamTwo findings from the review are correctness/design issues that only manifest when overlays exist, which is exactly the population upstream cares about. They are deferred to a follow-up PR against this branch, after which the label should flip to 1. Write-side field predicate is narrower than the reader's — inline. 2. The prune runs after the merge, and an optimize step shrinks coverage — inline. The merge reads, merges and writes the pruned fragments' entries and only then makes them unreachable, so the output is oversized and its contents disagree with its coverage; and 3. Discussion point, not a code change. This reverses the stamp assertion in Out of scope for both this PR and the follow-up, unchanged on Drafted with the help of an LLM (Claude). |
Bump Lance version to 10.0. Two API adaptations: - `Operation::Update` renamed `merged_generations` to `compacted_sstables`. - `data_storage_format.version` is now a typed `ConcreteFileVersion` instead of a `String`. Separate Lance fixes had to be made as part of rerun-io/lance#77 Source-Ref: 795ed996496f3f023780c051c89cefe35127633a
`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. Tests, for BTree, Bitmap, NGram 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 deletion hole in the overlaid fragment; a fragment whose overlay predates its source, or touches an unindexed field, is carried over without reading its data files; every source stale with nothing appended; 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. The ZoneMap control keeps the oldest source's stamp. 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. The Bitmap merge tests told merge from rebuild by the stamp; they now check no indexed data file was read. 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]>
`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. Tests, for BTree, Bitmap, NGram 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 deletion hole in the overlaid fragment; a fragment whose overlay predates its source, or touches an unindexed field, is carried over without reading its data files; every source stale with nothing appended; 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. The ZoneMap control keeps the oldest source's stamp. 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. The Bitmap merge tests told merge from rebuild by the stamp; they now check no indexed data file was read. 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]>
`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. Tests, for BTree, Bitmap, NGram 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. The ZoneMap control keeps the oldest source's stamp. 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. The Bitmap merge tests told merge from rebuild by the stamp; they now check no indexed data file was read. 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]>
`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. Tests, for BTree, Bitmap, NGram 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. The ZoneMap control keeps the oldest source's stamp. 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. The Bitmap merge tests told merge from rebuild by the stamp; they now check no indexed data file was read. 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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
…9941) ### 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 * #7244, which introduced the minimum stamp. * rerun-io#77, which addressed the same problem by discarding fragments with overlays from index merges. Inspiration for this PR; the approach here differs. ### 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](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5.1 <[email protected]>
Related
release-10.0.0.mainstill has it; no upstream issue tracks the trim consequence.What
IndexMetadata.dataset_versionis a segment's watermark for two readers:is_index_remap_caught_uptrims fragment-reuse versions at or below it, and overlay masking stops re-checking data overlays committed at or below it.Since lance-format#7244,
merge_scalar_indicesstamps a merged segment with the oldest source segment's version. That keeps overlay masking correct, because a merge never incorporates overlays, but it also means a merge can never retire a fragment-reuse version: any source that predates the version pins it for the whole dataset, and with deferred index remap the reuse index only drains through a full rebuild.For index types that translate their entries through the fragment-reuse index while merging (
can_remap(): BTree, Bitmap, LabelList, NGram, and JSON when its target does), the merged segment is now stamped with the manifest version it was built from. Per source segment, every carried-over fragment with an overlay on an indexed field committed after that segment was built is removed from the merged coverage; freshly scanned fragments already reflect their overlays and are left alone. Uncovered fragments fall back to a scan and are re-indexed by the next optimize, the same treatmentremap_column_index(optimize/remapping.rs) already applies after a remap; the overlay predicate now lives indataset::overlay::fragment_has_newer_indexed_overlayand is shared by both call sites. Types that do not remap (ZoneMap, BloomFilter, RTree) keep the oldest source's stamp, unchanged fromrelease-10.0.0.Not covered:
merge_existing_index_segmentsinindex.rskeeps its own min-source stamp for non-RTree types (and stamps the manifest version for RTree, the one place that now contradicts thecan_remaprule); the vector and inverted merge paths also keep the min stamp. Candidate follow-ups once upstream weighs in. The field match is id-equality, like the other prune sites; the reader gate is ancestry-aware, a pre-existing difference noted for a follow-up.Provenance: fork-only change, written here; upstreamable as a fix to lance-format#7244. Not a port or backport.
Testing
test_ngram_optimize_preserves_overlay_staleness(upstream's overlay test for this merge path) now asserts that the merged stamp equals the manifest version the merge read, that the overlaid fragment left the coverage while the untouched fragment stayed, and that a secondoptimize_indicesrestores the coverage with a stamp past the overlay. Its query assertions are unchanged, repeated after the second pass, and pass. With the first version of this PR itscontains(text, 'mango')assertion returned[6]instead of[1, 6].test_segment_merge_is_stamped_with_the_version_it_was_built_from: merges appended rows into a BTree segment that predates a deferred-remap compaction, asserts the stamp equals the manifest version the merge read, asserts the trim then drains the reuse index, and on a fresh open checks the plan answers fromScalarIndexQuerywith no fragment falling back to a scan and the count matches an oracle computed with the index disabled. Fails onrelease-10.0.0at the stamp assertion (left: 1, right: 6).dataset::tests(full, includes the overlay module),dataset::optimize::tests,index::append,dataset::index::frag_reuse,index::frag_reuse: 440 passed, 0 failed, 1 ignored, at--test-threads=3; clippy clean on the touched files. At default parallelism a few tests fail at random on this branch and on the untouched base alike, always DataFusion'sFairSpillPoolrefusing anExternalSorterMergereservation: session contexts are cached by config, so every test shares one 150 MiB pool (DEFAULT_LANCE_MEM_POOL_SIZE_PER_PARTITION, upstream code identical in v9 and v10) and three concurrent 40 MB sort reservations exhaust it. All pass single-threaded.redap_lance_utillib tests go from 103 passed / 1 failed to 104 passed, the failing one being its fragment-reuse trim test.Compatibility
Metadata-only: the value written into an existing
IndexMetadatafield changes and a merged segment's coverage bitmap may shrink. No file-format or manifest schema change. Segments already stamped with the older value stay valid and keep pinning their reuse versions until their next merge or rebuild. Rolling back is safe: a pruned coverage is strictly more conservative for readers.Agent
🤖 This PR was opened by a coding agent (Claude Fable 5.1).