Skip to content

feat: add ScalarIndex::lookup for batched exact-match key lookups - #9875

Merged
wjones127 merged 2 commits into
mainfrom
will/scalar-index-lookup
Oct 26, 2026
Merged

wjones127 merged 2 commits into
mainfrom
will/scalar-index-lookup

Conversation

@wjones127

@wjones127 wjones127 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds ScalarIndex::lookup, which takes a batch of keys and streams (key_index, row) record batches, one row per table row whose indexed value equals a key. This is PR 1 of the indexed merge_insert plan in #6444: the next PR joins merge sources against the index with it, without reading target data.

let mut matches = index.lookup(keys, &metrics)?;
while let Some(batch) = matches.try_next().await? {
    // column 0 `key_index`: position in `keys`
    // column 1 `_rowaddr` or `_rowid`: the matching row, named after the index's result domain
}
  • Exact equality only. A null key never matches, not even a null value. Keys must have the index's value type.
  • The row column is _rowaddr when results_are_row_addresses() (btree since feat(index): change btree scalar index to row-address domain #9481, bitmap since feat(index): change bitmap and label_list scalar indexes to row-address domain #9814) and _rowid otherwise. Rows are not filtered by deletions, and fragment-reuse remapping is already applied, the same as search.
  • Output streams in batches of at most 8192 matches, so a key matching many rows, or repeated many times in keys, is never materialized at once.
  • The default implementation returns NotSupported.
  • BTree: candidate pages come from the existing min/max page search, and pages load through the same cached, chunked-prefetch path as search (factored out into evaluate_pages_stream, which evaluate_pages collects). Each page is hashed once and probed against a hashbrown table of the keys, grouped by value. A page records only its matched rows and their key group; they are expanded to one output row per matching key as the stream is consumed, so a cold prefetch chunk holds at most one entry per page row. Equality is decided by arrow's make_comparator, the same scheme as DataFusion's generic IN-list filter.
  • Bitmap: one bitmap load per distinct key, expanded as the stream is consumed.
  • LogicalScalarIndex merges its segments' streams. Under stable row ids it restricts each row-id segment's matches to the live rows of its own fragments, as search does. JsonIndex forwards lookup and results_are_row_addresses to its target index. The second forward also fixes search on a JSON-wrapped zone map, bloom filter or FM index under stable row ids: its row addresses were read as row ids, so matches past fragment 0 were lost.

Evidence

  • test_lookup_matches_brute_force (Int64/Utf8, cold/warm cache, both row domains) checks BTree lookups against a brute-force scan, with repeated, null and absent keys and value runs that span pages.
  • test_lookup_float_equality_matches_search checks that lookup agrees with search(Equals) for 0.0, -0.0 and NaN.
  • test_bitmap_lookup covers the bitmap index, including empty and all-null keys.
  • test_lookup_bounds_batches, test_bitmap_lookup_bounds_batches and test_bitmap_lookup_bounds_repeated_keys check the batch bound, for pages or values matching many rows and keys repeated more than a batch holds.
  • test_lookup_applies_remapping and test_bitmap_lookup_applies_remapping check that fragment-reuse remapping is applied.
  • test_segmented_lookup (BTree, Bitmap) covers lookups across segments and a key-type error. test_lookup_drops_rows_moved_out_of_legacy_row_id_segments checks, on a legacy row-ID btree fixture, that lookup drops a row an update moved out of a segment's fragments, the way search does. stale_posting_after_update_with_stable_row_ids covers the address-domain case, where the stale posting comes back only as the deleted old row.
  • test_json_zonemap_index_with_stable_row_ids covers the search fix; it returns 1 of 3 rows without it.
  • test_json_lookup_forwards_to_target (both target domains) and test_lookup_is_not_supported (zone map) cover forwarding and the default.
  • New btree_lookup_{1000,8192} and bitmap_lookup_{100,1000} benchmarks. Results and a main-vs-branch run for the search refactor are in a comment below. A re-run after the streaming change is in a later comment.

Merge Risk

Door: Two-way. This adds a trait method with a default implementation and changes no on-disk format.
Blast Radius: perf for BTree search, whose page loading was refactored to share code with lookup. Back-to-back runs show no change beyond run-to-run noise: cached cases are within −8% to +5%, and the unchanged bitmap control varies as much. Also a fix for search on JSON-wrapped address-domain indices (zone map, bloom filter, FM) under stable row ids, which now return rows past fragment 0.

Part of #6444.

🤖 Generated with Claude Code

@wjones127

Copy link
Copy Markdown
Contributor Author

Benchmarks: lance-index benches, 1M-row indices, release-with-debug, m8g.4xlarge (Neoverse-V2, 16 vCPU). Medians below. "cached" means a warm index cache; "no_cache" re-reads pages from local disk on every call.

lookup

BTree. Sorted keys are a contiguous run of values (few pages); random keys are uniform over the whole range (pages across the whole index). Every key matches exactly one row.

keys type sorted, no_cache sorted, cached random, no_cache random, cached
1000 int64 360 µs 160 µs 14.7 ms 5.6 ms
1000 utf8 606 µs 237 µs 36.4 ms 8.9 ms
8192 int64 1.46 ms 1.16 ms 24.1 ms 10.1 ms
8192 utf8 2.32 ms 1.67 ms 47.2 ms 15.5 ms

Random keys cost far more than sorted ones because they touch far more pages, and each candidate page is scanned in full: pages are stored in row-id order, not value order. Whether value-ordered pages would help is left as a TODO.

Bitmap (unique values, so one bitmap per key):

keys type no_cache cached
100 int64 2.73 ms 169 µs
100 utf8 2.86 ms 186 µs
1000 int64 30.7 ms 2.66 ms
1000 utf8 32.0 ms 3.33 ms

search after the page-loading refactor

Main (2f4dcf5) and this branch, run back to back, for btree_equality and btree_in_{10,20,30} (32 cases):

  • Warm cache: −3% to +6%, with 10 of 16 cases within ±2%.
  • Cold cache: +0.4% to +7%.

As a control, bitmap search code is unchanged by this PR, and the same run measured it at +3% to +9% on cold cache. So I read the BTree cold-cache numbers as run-to-run drift rather than a regression. An earlier run against a baseline recorded before a machine reboot showed +35% on btree_in_{20,30}/int_unique/cached. It did not reproduce back to back (−3% / +1%).

@BubbleCal

Copy link
Copy Markdown
Contributor

this seems already ready for review?
i think we can get this in @wjones127

Comment thread rust/lance-index-core/src/scalar.rs Resolved
@wjones127
wjones127 force-pushed the will/scalar-index-lookup branch from 71476a1 to 7b08cf5 Compare October 23, 2026 19:38
@wjones127
wjones127 marked this pull request as ready for review October 23, 2026 19:49

@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 repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-changes Latest Gatekeeper recommendation requests changes. and removed K-changes Latest Gatekeeper recommendation requests changes. labels Oct 23, 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.

Both previous findings are fixed in e83b71ae4. Lookup preserves segment ownership and bounds output expansion while retaining shared page reads.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 23, 2026
@wjones127
wjones127 requested a review from BubbleCal October 23, 2026 20:52
@wjones127

Copy link
Copy Markdown
Contributor Author

@BubbleCal It's ready for review now. I switched it to stream out results and fixed some bugs.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 23, 2026
@wjones127

wjones127 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Re-run after the streaming change and the gatekeeper fixes (e83b71a). Setup: lance-index benches, 1M-row indices, release-with-debug, same machine as before. Main is 0d136bf, run right after the branch. Times are medians.

On both trees I locally edited benches/common.rs for the run so its btree training data uses _rowaddr instead of _rowid. Main's btree bench currently panics at setup since #9481. The edit was not committed.

lookup

BTree. Sorted keys are a contiguous run; random keys are spread over the whole index. Each key matches one row.

keys type sorted, no_cache sorted, cached random, no_cache random, cached
1000 int64 179 µs 94 µs 9.41 ms 3.66 ms
1000 utf8 335 µs 147 µs 25.9 ms 5.66 ms
8192 int64 755 µs 610 µs 11.8 ms 5.57 ms
8192 utf8 1.42 ms 932 µs 29.7 ms 8.63 ms

Bitmap, with unique values:

keys type no_cache cached
100 int64 2.60 ms 125 µs
100 utf8 2.58 ms 151 µs
1000 int64 22.3 ms 1.54 ms
1000 utf8 36.2 ms 1.88 ms

Every case is at or below the earlier, collect-based numbers above. For example, 1000 random int64 keys cached went from 5.6 to 3.66 ms. Part of that is likely from the rebase onto newer main.

search after the page-loading refactor (main → branch)

These are btree_equality and btree_in_{10,20,30}, 32 cases in total.

  • Cached: −8% to +5%, and 10 of 16 cases are within ±3% and all but one within ±5%.
  • Cold: −36% to +51%, with changes in both directions and no pattern by type or key count.

As a control, bitmap search code is unchanged by this PR. It moved −2% to +10% cached and −59% to +10% cold in the same run, so the cold numbers here are mostly local-disk noise.

🤖 Generated with Claude Code

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 23, 2026
Comment thread rust/lance-index/src/scalar/json.rs Resolved
lookup streams (key_index, _rowid | _rowaddr) record batches for the
rows whose indexed value equals each key, in batches of at most 8192
matches. Implemented for BTree, Bitmap, LogicalScalarIndex and
JsonIndex; other indices return NotSupported.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wjones127
wjones127 force-pushed the will/scalar-index-lookup branch from e83b71a to 0b6dd73 Compare October 26, 2026 20:46
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 26, 2026
Also document that only the multi-segment wrapper drops deleted lookup
matches, and assert the stale posting comes back as the deleted copy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wjones127
wjones127 merged commit 7f9ec61 into main Oct 26, 2026
41 checks passed
@wjones127
wjones127 deleted the will/scalar-index-lookup branch October 26, 2026 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-index Vector index, linalg, tokenizer enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants