Skip to content

fix(index): restrict each scalar index segment to its own coverage - #11045

Merged
Xuanwo merged 1 commit into
lance-format:mainfrom
wombatu-kun:issue/10987-segment-coverage-in-place-rewrite
Nov 20, 2026
Merged

Xuanwo merged 1 commit into
lance-format:mainfrom
wombatu-kun:issue/10987-segment-coverage-in-place-rewrite

Conversation

@wombatu-kun

Copy link
Copy Markdown
Contributor

Closes #10987

LogicalScalarIndex now restricts every segment of a multi-segment scalar index to the fragments it covers before it combines their answers. Address-domain segments are restricted by fragment, and so are row-id segments on a table without stable row ids, where a row id is the row's address; row-id segments under stable row ids keep the live-row restriction. Coverage comes from resolve_query_segment_coverages, so a segment translated through a tagged fragment reuse history answers for the destinations the reader derives. Lookups read the row column that lookup_schema names (_rowaddr or _rowid), and the fragment restriction walks each segment's hits rather than its coverage, because MapIndexExec searches once per input batch.

With more than one segment, LogicalScalarIndex::try_new now needs segment metadata from the dataset's own listing for every index, not only for row-id segments under stable row ids. FTS and MinHash search open their segments outside LogicalScalarIndex and are unchanged here, so on tables without stable row ids they keep the same gap (#6283 for FTS).

@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 pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added critical-fix Bugs that cause crashes, security vulnerabilities, or incorrect data. bug Something isn't working labels Nov 13, 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.

The per-segment restriction addresses the stale scalar answers in #10987, including rewritten NULL values and translated fragment coverage.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 13, 2026
@Xuanwo
Xuanwo merged commit 326900a into lance-format:main Nov 20, 2026
41 checks passed
Xuanwo added a commit that referenced this pull request Dec 1, 2026
…alarIndexExpr (#11176)

Closes #11155

## Summary

### Root Cause
1. **`ScalarIndexExpr::optimize`** left contiguous `Or` trees unbalanced
and did not deduplicate repeated disjuncts or exact duplicate conjuncts.
2. **`ScalarIndexExpr::evaluate_with_options`** evaluated `And` / `Or`
via unbounded recursive `try_join!`, re-loading and re-searching
identical repeated leaves (e.g., `grp = 'hot_grp'` / `'cold_grp'` across
256 `OR` branches), keeping hundreds of full-dataset `RowAddrTreeMap`
masks live concurrently, and evaluating wide predicates unconditionally
alongside narrow/empty conjuncts.
3. **`ScopedScalarIndexLoader`** did not bound physical-address leaf
hits to `fragment_scope` when a shared index segment matched
out-of-scope fragments, and **`fragments_covered_by_index_query`**
walked duplicate index references repeatedly.

### Changes
- **Borrowed-RHS 3VL algebra (`lance-select`)**:
- Added `BitAnd<&Self>` and `BitOr<&Self>` on `NullableRowAddrMask`
(`BitAndAssign<&Self>` / `BitOrAssign<&Self>` on `NullableRowAddrSet`),
mutating `self` in place without cloning `rhs` while preserving SQL
Kleene three-valued logic.
- Added `BitAnd<&Self>` and `BitOr<&Self>` on `NullableIndexExprResult`,
retaining `#11012` `MaskDomain` invariants and `#11552` single-mask fast
paths when `self.exact && rhs.exact`.
- **Expression optimization (`lance-index`)**:
- Updated `ScalarIndexExpr::optimize` with iterative contiguous `Or`
flattening, first-seen-order disjunct deduplication, and balanced
`rebuild_or_tree` (`O(log2 N)` depth).
- Made `collect_and_leaves` and `needs_recheck` iterative and
deduplicated exact duplicate conjuncts in `optimize_and_leaves`.
- **Evaluation engine (`rust/lance-index/src/scalar/eval.rs`)**:
- Pre-evaluation iterative duplicate-only leaf CSE pass allocating
`CseSlot` (`OnceCell` for native and reconciled `RowAddress` results)
only for leaves appearing `>= 2` times under `(column, index_name,
index_type, needs_recheck, track_nulls, query)`; unique leaves (`count
== 1`) bypass caching so bitmaps drop immediately after folding.
- Expression-wide unique leaf search budget
(`MAX_LEAF_SEARCH_CONCURRENCY = 32`) enforced via a per-evaluation
semaphore acquired strictly when a unique leaf search starts
(`eval_search`): parent `Not` / `And` / `Or` nodes awaiting children and
duplicate CSE callers awaiting an in-flight `OnceCell` consume zero
slots.
- Selective-first `And` ordering (`ConjunctPriority`),
`track_nulls`-aware short-circuiting, and 3-phase `And` evaluation: (1)
zero-I/O drain of ready CSE leaves, (2) lone initial selective probe,
and (3) bounded tail overlap (`MAX_AND_CONCURRENCY = 8`,
`MAX_NESTED_AND_CONCURRENCY = 4` under `Or`), plus `MAX_OR_CONCURRENCY =
8` incremental `Or` folding.
- Preserved native `MaskDomain::RowAddress` / `MaskDomain::RowId` on
leaves (`#11012`), reconciling mixed domains lazily via
`ScalarIndexLoader::row_id_result_to_row_addrs` with per-slot
memoization and cooperative polling (`drive_with_in_flight`).
- **Fragment-scoped index loading & coverage (`lance`)**:
- Retained `#11045` per-segment coverage in `open_scalar_index_segments`
/ `LogicalScalarIndex` and wrapped subset-scoped physical-address
indices in `FragmentScopedScalarIndex` so `selected` and `nulls` are
restricted via `retain_fragments` while preserving native domains and
`Exact` / `AtMost` / `AtLeast` bounds.
- Deduplicated `(column, index_name)` pairs iteratively in
`ScalarIndexExec::fragments_covered_by_index_query`.

## Benchmark (`lance#11155` repeated-leaf comparison vs rebased `main`
`1b778e120`)

Fixture: 2,000,000 rows across 40 fragments (50,000 rows/frag) with
`item_id` BTree, `grp` Bitmap, and `ts` BTree; 256-branch composite `OR`
`(grp = 'hot_grp'|'cold_grp' AND item_id = ... AND ts >= ... AND ts <=
...)`. Measured against rebased parent `1b778e120` (includes `#11042`
bitmap read coalescing, `#11045`, `#11012`, and `#11552`), `--profile
release-with-debug`, median of 5 fresh child processes (all modes
returned exact `[("item_0_0", 0)]`):

| Mode | Scope | Variant | Median (ms) | RSS delta (MB) | `parts_loaded`
| `index_comparisons` |
| --- | --- | --- | ---: | ---: | ---: | ---: |
| `enable_stable_row_ids=false` | `fragments=[frag0]` | `main`
(`1b778e120`) | 415.77 | +328.28 | 214 | 3,138,944 |
| `enable_stable_row_ids=false` | `fragments=[frag0]` | **This PR** |
**117.71** (-71.7%) | **+83.85** (-74.5%) | **213** | **1,098,881** |
| `enable_stable_row_ids=false` | Full dataset (40 frags) | `main`
(`1b778e120`) | 403.38 | +330.80 | 214 | 3,138,944 |
| `enable_stable_row_ids=false` | Full dataset (40 frags) | **This PR**
| **184.32** (-54.3%) | **+89.83** (-72.8%) | 214 | 3,138,690 |
| `enable_stable_row_ids=true` | `fragments=[frag0]` | `main`
(`1b778e120`) | 386.27 | +324.24 | 214 | 3,138,944 |
| `enable_stable_row_ids=true` | `fragments=[frag0]` | **This PR** |
**114.09** (-70.5%) | **+81.53** (-74.9%) | **213** | **1,098,881** |
| `enable_stable_row_ids=true` | Full dataset (40 frags) | `main`
(`1b778e120`) | 412.16 | +303.69 | 214 | 3,138,944 |
| `enable_stable_row_ids=true` | Full dataset (40 frags) | **This PR** |
**175.48** (-57.4%) | **+89.30** (-70.6%) | 214 | 3,138,690 |

---------

Co-authored-by: Xuanwo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working critical-fix Bugs that cause crashes, security vulnerabilities, or incorrect data. K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: after an in-place column rewrite, a scalar index returns old entries once another segment indexes the fragment

2 participants