Skip to content

fix: apply fragment bitmap allow-list to index search results - #6563

Merged
westonpace merged 4 commits into
lance-format:mainfrom
westonpace:fix-fragment-bitmap-allowlist-index-search
Apr 19, 2026
Merged

westonpace merged 4 commits into
lance-format:mainfrom
westonpace:fix-fragment-bitmap-allowlist-index-search

Conversation

@westonpace

Copy link
Copy Markdown
Member

Summary

  • Restrict index search results to only fragments in the index's fragment_bitmap, filtering out stale entries from fragments whose data changed but whose index was not rewritten
  • Apply the allow-list in DatasetPreFilter (vector/FTS queries) and MapIndexExec (scalar index queries used by merge_insert)
  • Add regression tests for stale index entries after DataReplacement and partial-schema merge_insert (BTree, FTS, and vector indices)

Test plan

  • test_data_replacement_populates_invalidated_bitmap — DataReplacement removes fragment from bitmap
  • test_fts_stale_entries_after_data_replacement — stale FTS entries blocked after DataReplacement
  • test_vector_index_after_data_replacement — stale vector index entries blocked after DataReplacement
  • test_partial_merge_insert_stale_index_ambiguous — repeated partial merge_insert doesn't cause ambiguous errors
  • test_partial_merge_insert_stale_index_fragment_not_exist — stale btree entries don't cause fragment-not-found errors
  • test_partial_merge_insert_stale_index_batch_size_mismatch — stale btree entries don't cause batch size mismatch
  • test_partial_merge_insert_stale_vector_index_duplicates — no duplicate rows from stale vector index
  • test_partial_merge_insert_stale_fts_index_duplicates — no duplicate rows from stale FTS index
  • test_compaction_after_invalidated_fragment — compaction succeeds after fragment invalidation
  • All 17 existing prefilter tests pass

🤖 Generated with Claude Code

Restrict index search results to only fragments listed in the index's
fragment_bitmap. This prevents stale index entries from leaking through
when a fragment's data changes (e.g. via DataReplacement or partial
merge_insert) but the index is not rewritten.

The allow-list is applied in two places:
- DatasetPreFilter (for vector and FTS index queries)
- MapIndexExec (for scalar index queries used by merge_insert)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working label Apr 17, 2026
westonpace and others added 2 commits April 17, 2026 10:33
Instead of applying the allow-list separately in DatasetPreFilter and
MapIndexExec, move it into create_deletion_mask so all callers get the
protection automatically. When the dataset has fragments outside the
index's fragment bitmap, the mask now restricts results to only the
indexed fragments.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Split create_deletion_mask into two variants:
- create_deletion_mask: original behavior, no fragment restriction
- create_restricted_deletion_mask: also applies allow-list

The allow-list should only be applied when the fragments parameter
represents a real index fragment bitmap, not a conservative fallback
range (which happens when fragment_bitmap is None).

Fixes remap_ivf_pq_index test failure caused by the fallback range
0..max_fragment_id being empty for single-fragment datasets.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@codecov

codecov Bot commented Apr 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.63548% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
rust/lance/src/dataset/write/merge_insert.rs 98.71% 2 Missing and 4 partials ⚠️
rust/lance/src/index/prefilter.rs 97.72% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@westonpace
westonpace marked this pull request as ready for review April 17, 2026 22:22

@wjones127 wjones127 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.

This looks good! Thanks for putting this up.

@westonpace
westonpace merged commit 462faf7 into lance-format:main Apr 19, 2026
28 of 29 checks passed
westonpace pushed a commit that referenced this pull request Apr 20, 2026
## Summary

Fix `unresolved link to create_deletion_mask` rustdoc error introduced
in #6563.

`[create_deletion_mask]` → `[Self::create_deletion_mask]` in the doc
comment for `create_restricted_deletion_mask`.

## Test plan

- [x] `RUSTDOCFLAGS="-D warnings" cargo doc -p lance --no-deps` passes
jackye1995 pushed a commit that referenced this pull request May 2, 2026
#6664)

The fragment-bitmap allow-list filter dominates the cost of merge_insert
and vector-search prefilter on tables that have received writes since
their index was built. The filter ANDs an AllowList of full fragments
with the deletion BlockList. RowAddrMask::bitand expands AllowList &
BlockList into AllowList - BlockList, and the per-fragment (Full -
Partial) branch in RowAddrTreeMap::sub_assign materializes
RoaringBitmap::full() for every fragment with deletions.

Build the equivalent BlockList directly (deletions union rows in
fragments outside the index bitmap) using only Full markers and a
HashMap clone.

Existing #6563 stale-index tests cover correctness.

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
westonpace pushed a commit that referenced this pull request May 4, 2026
#6664)

The fragment-bitmap allow-list filter dominates the cost of merge_insert
and vector-search prefilter on tables that have received writes since
their index was built. The filter ANDs an AllowList of full fragments
with the deletion BlockList. RowAddrMask::bitand expands AllowList &
BlockList into AllowList - BlockList, and the per-fragment (Full -
Partial) branch in RowAddrTreeMap::sub_assign materializes
RoaringBitmap::full() for every fragment with deletions.

Build the equivalent BlockList directly (deletions union rows in
fragments outside the index bitmap) using only Full markers and a
HashMap clone.

Existing #6563 stale-index tests cover correctness.

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Xuanwo pushed a commit that referenced this pull request May 28, 2026
…6965)

## Summary

Closes #6877.

`do_create_deletion_mask_row_id` previously iterated every fragment in
the dataset, so the resulting allow-list included stable row ids whose
**current** physical home was outside the index's `fragment_bitmap`. On
stable-row-id datasets this bypassed the allow-list added in #6563:
`MapIndexExec` requests `create_restricted_deletion_mask`, but the
stable-row-id branch at `prefilter.rs:270` ignored the restriction.

### Effect on merge_insert
After a `merge_insert UpdateAll` rewrites a row into a new fragment, a
second merge_insert against the same key saw that row twice — once via
the BTREE (which holds the row's stable row id and resolves to its new
fragment through `TakeExec`) and once via the unindexed-fragments scan
that also covered the new fragment. Both branches emitted the same
`_rowid`, tripping the source-dedup HashSet:

```
Invalid user input: Ambiguous merge inserts are prohibited:
multiple source rows match the same target row on (id = "A").
```

### Effect on FTS
A `full_text_search` after a `merge_insert` returned each moved row
twice, because the inverted index's hits went through the same prefilter
and the post-filter scan picked up the new fragment. The new regression
test `test_issue_6877_fts_no_duplicates_stable_row_ids` empirically
fails with "30 unique out of 40 total" before this fix.

## Fix

- `prefilter.rs`: thread `restrict_to: Option<RoaringBitmap>` through
`do_create_deletion_mask_row_id`. When set, only iterate fragments in
the bitmap. The resulting allow-list now means "stable row ids whose
current home is inside the restriction" — semantically equivalent to the
non-stable-row-id branch.
- `prefilter.rs:270`: pass `Some(fragments)` when
`restrict_to_fragments=true`, `None` otherwise.
- `caches.rs`: add `restrict_hash: Option<u64>` to `RowAddrMaskKey` so
two consumers asking for different fragment subsets don't poison each
other's cached mask.

## Mechanism, validated

Instrumentation on the unfixed code shows the BTREE returning stable row
id `4` for the matched row, the deletion-mask allow-list containing `4`
(because the new unindexed fragment was iterated globally), `mask & 4 =
{4}` passing the filter, both branches emitting `_rowid=4`, dedup
HashSet tripping. After the fix, `mask & 4 = {}` correctly excludes the
BTREE side and only the unindexed-fragment scan delivers the row.

## Test plan

- [x] **Reproduces pre-fix**: stashed the prefilter + cache changes,
re-ran the two new regression tests — both FAIL with the documented
errors.
- [x] **Fixed post-fix**: restored the stash, both tests pass.
- [x] `cargo test -p lance --lib prefilter` — 18/18 pass (17 existing +
1 new).
- [x] `cargo test -p lance --lib merge_insert` — 145/145 pass (143
existing + 2 new).
- [x] `cargo fmt --all`
- [x] `cargo clippy --all --tests --benches -- -D warnings`

New regression tests:
- `test_restricted_deletion_mask_stable_row_id_honors_bitmap` — pins the
prefilter contract: full / restricted-subset / empty-bitmap cases.
- `test_issue_6877_repeated_merge_insert_stable_row_ids` — mirrors the
issue's exact repro: two sequential merge_insert UpdateAll against the
same key on a stable-row-id dataset with a BTREE scalar index.
- `test_issue_6877_fts_no_duplicates_stable_row_ids` — companion FTS
regression: an inverted index + merge_insert sequence that previously
returned duplicate hits.

## Notes

- The hash on `RowAddrMaskKey` uses `DefaultHasher` over the bitmap's
sorted u32s. The cache is process-local in-memory, so cross-version
stability isn't required.
Xuanwo pushed a commit to lancedb/lancedb that referenced this pull request Aug 6, 2026
## Summary

- add a Python regression test for two partial-schema merge inserts
against the same BTree-indexed rows
- verify repeated updates retain one copy of every row and the final
update values

## Root cause

Lance 4.0, used by LanceDB 0.30.2, removed a rewritten fragment from the
index bitmap while stale BTree entries for that fragment remained
searchable. The next merge found each target through both the stale
index and the unindexed-fragment scan, producing the ambiguous-match
error. Lance fixed the root cause in lance-format/lance#6563 by applying
the fragment-bitmap allow-list to index results, and the Lance release
pinned by current LanceDB includes that fix. This test preserves the
corrected behavior through the Python API.

## Validation

- `cd python && uv run --extra tests pytest python/tests/test_table.py
-k merge_insert -q` (9 passed)
- `cd python && uv run --extra tests --extra dev ruff format --check
python/tests/test_table.py`
- `cd python && uv run --extra tests --extra dev ruff check
python/tests/test_table.py`

Repository-wide Ruff also reports 20 pre-existing violations in
untouched CI and plugin scripts.

Fixes #3280

<!-- lance-gatekeeper-fix:v1 agent=ee6b9565f9780712026076930566f116
generation=1 -->

Co-authored-by: Gatefixer <313497061+lancedb-gatefixer[bot]@users.noreply.github.com>
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