Repository navigation
feat: add ScalarIndex::lookup for batched exact-match key lookups - #9875
Conversation
|
Benchmarks:
|
| 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%).
|
this seems already ready for review? |
71476a1 to
7b08cf5
Compare
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
✅ Gate recommendation: approve.
Both previous findings are fixed in e83b71ae4. Lookup preserves segment ownership and bounds output expansion while retaining shared page reads.
|
@BubbleCal It's ready for review now. I switched it to stream out results and fixed some bugs. |
|
Re-run after the streaming change and the gatekeeper fixes (e83b71a). Setup: On both trees I locally edited
|
| 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
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]>
e83b71a to
0b6dd73
Compare
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]>
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 indexedmerge_insertplan in #6444: the next PR joins merge sources against the index with it, without reading target data._rowaddrwhenresults_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_rowidotherwise. Rows are not filtered by deletions, and fragment-reuse remapping is already applied, the same assearch.keys, is never materialized at once.NotSupported.search(factored out intoevaluate_pages_stream, whichevaluate_pagescollects). 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'smake_comparator, the same scheme as DataFusion's generic IN-list filter.LogicalScalarIndexmerges its segments' streams. Under stable row ids it restricts each row-id segment's matches to the live rows of its own fragments, assearchdoes.JsonIndexforwardslookupandresults_are_row_addressesto its target index. The second forward also fixessearchon 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_searchchecks that lookup agrees withsearch(Equals)for0.0,-0.0and NaN.test_bitmap_lookupcovers the bitmap index, including empty and all-null keys.test_lookup_bounds_batches,test_bitmap_lookup_bounds_batchesandtest_bitmap_lookup_bounds_repeated_keyscheck the batch bound, for pages or values matching many rows and keys repeated more than a batch holds.test_lookup_applies_remappingandtest_bitmap_lookup_applies_remappingcheck 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_segmentschecks, 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_idscovers the address-domain case, where the stale posting comes back only as the deleted old row.test_json_zonemap_index_with_stable_row_idscovers thesearchfix; it returns 1 of 3 rows without it.test_json_lookup_forwards_to_target(both target domains) andtest_lookup_is_not_supported(zone map) cover forwarding and the default.btree_lookup_{1000,8192}andbitmap_lookup_{100,1000}benchmarks. Results and a main-vs-branch run for thesearchrefactor 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:
perffor BTreesearch, 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 afixforsearchon 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