Skip to content

fix(mem_wal): dedup repeated row positions when training the flushed PK sidecar - #9595

Merged
Xuanwo merged 1 commit into
lance-format:mainfrom
beinan:fix/pk-sidecar-dedup-positions
Sep 29, 2026
Merged

Xuanwo merged 1 commit into
lance-format:mainfrom
beinan:fix/pk-sidecar-dedup-positions

Conversation

@beinan

@beinan beinan commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Problem

The memtable primary-key index (BTreeMemIndex) is a skiplist multiset: inserting the same (value, position) twice stores it twice. A row is indexed twice when a WAL replay (replay_wal → insert_batches_parallel) re-indexes buffered batches the WAL flusher (flush_from_batch_store) already indexed.

The flushed generation's PK sidecar (create_pk_index / write_pk_sidecar) is trained straight from pk_training_batches(), so the on-disk BTree page inherits the repeated row id. When contains_keys later loads that page, FlatIndex::try_new builds a RowAddrTreeMap via RoaringBitmap::from_sorted_iter, which requires strictly increasing input, and every probe of that generation fails with:

RowAddrTreeMap::from_sorted_iter called with non-sorted input

Because the block list is built for every LSM scan, one bad generation makes every read of the table fail. Observed in production (lance 9.0.0): a 2-row generation whose sidecar page was [(item:a, 0), (item:a, 0), (item:b, 1)] broke list on a 54k-row table for weeks, deterministically, for any limit large enough to enable the PkBlockFilterExec.

This is the same failure signature as #7230 (base-table BTree merge). The fixes proposed there were rejected in favour of fixing the training input; this PR does exactly that for the MemWAL sidecar path.

Fix

In BTreeMemIndex::to_training_batches, skip a position equal to the previous one within a value group. Positions within a group are already sorted, so this is a one-comparison dedup at the only place the multiset is turned into training data.

The write path is left as a multiset on purpose: insert_and_check_neighbors relies on neighbor inspection for the pk_has_overrides signal, and deduping there would change its cost profile.

Verification

  • test_btree_index_to_training_batches_dedups_repeated_positions: indexes a batch twice into both the fixed-int and bytes backends; the training batches carry each position once. Fails without the fix.
  • sidecar_from_doubly_indexed_memtable_is_probeable (block_list): trains a real on-disk sidecar from a doubly-indexed IndexStore, opens it with open_pk_index, and probes it. On the 9.0.0 read path this is the exact production failure; on main the probe path no longer builds a RowAddrTreeMap eagerly (perf(index): accelerate RQ scale search #8924), so the test passes either way there, but it pins the invariant for the search/into_row_addr_set paths that still do.
  • cargo test -p lance --lib mem_wal: 781 passed. cargo fmt --check, cargo clippy --lib --tests -D warnings clean.

🤖 Generated with Claude Code

…PK sidecar

The memtable primary-key index is a skiplist multiset: inserting the same
(value, position) twice stores it twice, and a row is indexed more than
once when a WAL replay re-indexes batches the flusher already indexed.
The flushed PK sidecar is trained straight from that index, so its BTree
page carried a repeated row id. Loading the page builds a RoaringBitmap
with from_sorted_iter, which rejects the repeat, so every probe of that
generation failed with "RowAddrTreeMap::from_sorted_iter called with
non-sorted input" and any LSM scan touching it returned an error. In
production one 2-row generation with page rows [(a,0),(a,0),(b,1)] broke
reads of a 54k-row table for weeks.

Skip a position equal to the previous one within a value group when
building training batches; positions within a group are already sorted.

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working label Sep 28, 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 with a non-blocking risk.

Deduplicating at training export enforces one (value, position) per new sidecar entry while preserving the memtable index's override detection. The focused training and sidecar regressions pass.

The reported 9.0.0 scan failure is not reproduced by current main's read path: #9462 made BTree search tolerate repeated row IDs, and I did not find a current replay path that double-indexes a batch. This patch does not rewrite sidecars already written by older versions; affected deployments still need a reader upgrade or index rebuild.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Sep 28, 2026
@Xuanwo
Xuanwo merged commit b94b20c into lance-format:main Sep 29, 2026
38 of 39 checks passed
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. K-risk Latest Gatekeeper recommendation includes a non-blocking risk.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants