Skip to content

Merge Bitmap index segments instead of rescanning the dataset - #55

Closed
zehiko wants to merge 2 commits into
release-8.0.0from
zehiko/v8-bitmap-segment-merge
Closed

zehiko wants to merge 2 commits into
release-8.0.0from
zehiko/v8-bitmap-segment-merge

Conversation

@zehiko

@zehiko zehiko commented Aug 28, 2026

Copy link
Copy Markdown
Member

Problem

merge_scalar_indices listed only BTree in has_segment_merge_primitive, so a Bitmap index with more than one selected segment fell through to a full rescan + retrain over every covered fragment. Bitmap trains on TrainingOrdering::Values, so that rescan runs a blocking spilling SortExec against FairSpillPool(LANCE_MEM_POOL_SIZE). On a production boolean column that meant sorting ~955M rows to produce a ~1MB index: 12–17s failures on an 8 GiB pool, and 38+ minutes wedged on a zero-byte spill file on a 32 GiB one.

Fix

Wire Bitmap into the N:1 segment-merge path as a k-way sorted merge-join over each segment's index_map, streaming one posting at a time through the load_bitmap accessor (which applies fragment-reuse remapping), with each segment's OldIndexDataFilter applied before the union. Peak memory is one posting per segment plus the union being written. The old whole-index HashMap union (merge_bitmap_indices) is deleted.

Benchmark

Rerun dataplatform consolidation harness, 500M-row Segment-Manifest-shaped dataset, 26 index segments, FRI at 26 versions/500M rows, prod env incl. LANCE_FRAG_REUSE_REMAP_MODE=compact, cold cache, c7i.8xlarge local XFS:

  • bitmap consolidation: 2,285s → 163s (14×)
  • process peak RSS: 9.6 GiB → 2.1 GiB
  • end state identical (26→1 segments, FRI drained)

A commit-isolation ladder showed this commit alone accounts for the entire bitmap gain (baseline ≈ baseline+PR36+append-fix; adding this commit gives the full win; RSS 990 MB → 93 MB at 50M rows).

Tests

Shipped with the fix: test_bitmap_merge_k_segments, test_bitmap_merge_k_segments_without_new_data, test_optimize_bitmap_multi_segment_merge_consolidates, test_optimize_bitmap_merge_remaps_deferred_compaction, test_optimize_bitmap_drops_stale_rows_across_segments_after_update.

Added in the follow-up commit: test_bitmap_merge_k_segments_with_new_nulls (nulls on the NEW data side of the k-way merge), test_bitmap_merge_k_segments_boolean_keys (Boolean key column, the production shape), test_optimize_bitmap_wide_consolidation (26 delta segments consolidated by one optimize_indices call).

cargo test -p lance-index --lib scalar:: (330 passed), cargo test -p lance --lib index:: (572 passed), cargo clippy -p lance-index -p lance --all-targets clean.

Upstream status

Not upstream: lancedb/lance main at 0a4e20e2a5cf09641109f466ea4846e62045f085 (2026-08-28) still dispatches only IndexType::BTree | IndexType::NGram in has_segment_merge_primitive, so multi-segment Bitmap consolidation still rebuilds via full rescan there (rerun-io release-9.0.0/release-10.0.0 likewise lack it).

Suggested follow-up (not in this PR)

Replace the pre-existing .unwrap() on posting deserialize (bitmap.rs ~465) with an error — consolidation now reads every posting of every source segment through it, and panic=abort deployments would die on one corrupt posting.

🤖 Generated with Claude Code

zehiko and others added 2 commits August 28, 2026 10:18
`merge_scalar_indices` listed only `IndexType::BTree` in
`has_segment_merge_primitive`, so a Bitmap index with more than one selected
segment fell through to `rebuild_scalar_segment` -- a full rescan and retrain
over every covered fragment. Bitmap declares
`TrainingCriteria::new(TrainingOrdering::Values)`, so that rescan is wrapped
in a blocking spilling `SortExec` against `FairSpillPool(LANCE_MEM_POOL_SIZE)`.
On a production boolean column that meant sorting ~955M rows through a 32 GiB
DataFusion pool to produce a ~1 MB index: 12-17s failures on an 8 GiB pool,
or 38+ minutes wedged on a zero-byte spill file on a 32 GiB one.

Wire Bitmap into the same N:1 segment-merge path BTree uses, as a k-way
sorted merge-join over the source segments' postings. Each segment
contributes its keys from the in-memory `index_map` and its bitmaps one key
at a time through `load_bitmap`. Both choices are load-bearing:

  - `index_map` is a `BTreeMap`, so key order does not depend on the on-disk
    row order. Bitmap lookup files are not reliably key-sorted -- `remap` and
    the `merge_bitmap_indices` HashMap union wrote them from a `HashMap`, and
    even `streaming_build_and_write` appends an old-only null row last -- so a
    merge that read the files sequentially and assumed sorted input would
    silently drop postings.
  - `load_bitmap` is the accessor that applies fragment-reuse remapping.
    Reading the lookup file's raw bytes would bypass it and emit row
    addresses pointing at fragments compaction has already retired, which
    under `defer_index_remap: true` is silent data loss.

Peak memory is one posting per segment for the current key plus the union
being emitted -- the bound `merge_shards` gives -- rather than the whole
merged index in heap the DataFusion pool cannot see, which is what the
deleted `merge_bitmap_indices` HashMap union held.

Per-segment `OldIndexDataFilter`s are applied to each source posting, so
compaction-retired fragments and stable-row-id rewrites are dropped exactly
as on the BTree path, and a filter that keeps nothing skips the segment's
reads entirely.

`Dataset::merge_existing_index_segments` now routes bitmap groups through the
same primitive, which also gives it the per-segment filtering it lacked.

Co-Authored-By: Claude Opus 5 <[email protected]>
…nsolidation

Close three coverage gaps in the Bitmap segment-merge path:

- a k-way merge whose NEW data stream contains nulls, exercising the
  finish_run null branch that folds every old segment's null_map in
  exactly once (with a per-segment filter applied to one null posting);
- Boolean key columns, the motivating production column type (all
  previous tests used Utf8 keys);
- a genuinely wide consolidation: 26 delta segments merged into one
  segment by a single optimize_indices call (previous e2e merged 3).

Co-Authored-By: Claude Fable 5 <[email protected]>
@zehiko

zehiko commented Aug 28, 2026

Copy link
Copy Markdown
Member Author

@claude do a full code review, focus on correctness, edge cases, silent corruptions and data loss.

@zehiko

zehiko commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

tested, seems to be a significant improvement, prepped a PR against v10

@zehiko zehiko closed this Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant