Skip to content

feat(mem_wal): answer memtable filters with Lance's scalar expression pass - #9804

Merged
jackye1995 merged 9 commits into
lance-format:mainfrom
xuanyu-z:memwal-filter-expression-pass
Oct 10, 2026
Merged

jackye1995 merged 9 commits into
lance-format:mainfrom
xuanyu-z:memwal-filter-expression-pass

Conversation

@xuanyu-z

@xuanyu-z xuanyu-z commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Why

The memtable has its own predicate vocabulary and a hand-written match over Expr that recognises =, <, >= and IN on one column. Any other filter reads every memtable row, so <=, >, IS NULL, LIKE 'prefix%' and AND across two indexed columns never use an index.

How a filter reaches an index

flowchart TB
    subgraph before["Before: a match written for the memtable"]
        f1["filter Expr"] --> m1{"one column, compared by<br/>equals, less than, at least,<br/>IN, or an OR of equals?"}
        m1 -- yes --> b1["that column's B-tree<br/>(BTreeIndexExec)"]
        m1 -- no --> s1["read every memtable row<br/>and apply the filter"]
    end
    subgraph after["After: the pass the base table's scan uses"]
        f2["filter Expr"] --> p2["apply_scalar_indices<br/>with each index's ScalarQueryParser<br/>(MemIndexPlugin::query_parser)"]
        p2 --> t2["tree of index searches<br/>AND / OR across indexes<br/>+ leftover expression"]
        t2 --> e2["ScalarMemIndexExec: evaluate the tree,<br/>clamp to visible rows"]
        e2 --> r2{"answered exactly?"}
        r2 -- yes --> o2["rows"]
        r2 -- no --> c2["re-check the whole filter<br/>on the narrowed rows"] --> o2
        p2 -- "nothing claimed, or NOT" --> s2["read every memtable row<br/>and apply the filter"]
        e2 -- "an index over its match budget declines" --> s2
    end
Loading

A memtable index then claims exactly the expressions its on-disk counterpart claims, and a single-column plugin kind is reachable from a filter by giving its parser, with no change to the memtable's planner.

What changes

Filters go through apply_scalar_indices, the pass the base table's scan uses, with a provider built from the memtable's indexes. A plugin supplies the query parser its on-disk counterpart uses (MemIndexPlugin::query_parser), so both claim the same expressions. The result is a tree of per-index queries and a leftover expression. The tree goes through ScalarIndexExpr::optimize, as on the base table, so bounds on one column split by another conjunct are searched as one range. ScalarMemIndexExec evaluates the tree and applies the whole filter to the rows it narrowed to unless the indexes answered all of it exactly. ScalarPredicate and BTreeIndexExec are removed. A list column compared with a list literal is cast because of nested field ids; the split looks through a cast that changes no value.

A broad answer is not listed. Only a batch's candidate rows are read, and the filter is re-checked on those rows alone. The scan offers an index a match budget (SearchContext::match_budget: a sixteenth of the visible rows, never under 4,096); an index that would list more may decline, and every visible row is read with the filter. The built-in B-tree honours it. A failed search is an error, not a read of every row. On 100,000 rows with B-trees on an integer and a string column (main reads every row for the shapes marked *):

Filter Read every row main This PR
id = 54321 134 µs 60 µs 58 µs
id >= 10000 AND id < 11000 (1%) 135 µs 139 µs* 73 µs
id >= 10000 AND id < 20000 (10%) 137 µs 140 µs* 122 µs
id >= 0 AND id < 50000 (50%) 146 µs 152 µs* 137 µs
id >= 0 (every row) 129 µs 4,892 µs 131 µs
name LIKE 'k05%' (10%) 265 µs 269 µs* 209 µs

The memtable's own key index answers filters too. With no maintained index on a single-column key, the B-tree the memtable keeps for its key is in the catalog, under a name no base-table index can carry, so id = 4, id IN (...) and key ranges still use an index, as before.

B-tree ranges honour each bound. An inclusive upper bound adds its key's rows and an exclusive lower bound removes them, the latter by a binary search over that key's sorted rows; a range whose bounds cross matches nothing.

Breaking changes

  • ScalarPredicate and BTreeIndexExec are removed; ScalarMemIndexExec answers a filter from any filter index.
  • SearchContext gains a public match_budget field; SearchContext::new leaves it unset.
  • MemIndexSpec gains a public index_details field (the base-table index's details message), so building one with a struct literal must set it.

Known limitations

  • NOT is not evaluated from indexes: complementing a result needs each index's null rows, which indexes do not report yet. A filter with a NOT-shaped part (<>, NOT IN, IS NOT NULL) reads every row.

Testing

  • Compared with `main`: one seeded workload (puts with overwrites and nulls, deletes, seals, flushes, crash-and-reopen and close-and-reopen; single and composite keys) ran against both builds, reading after every step through filtered and projected scans with limits and offsets, vector and full-text search, key lookups, and filters on the memtable alone. Over 200 seeds every result matches except `x IN (1, NULL)`, where `main` returned null rows (fixed by feat(mem_wal): make memtable index kinds pluggable #9803). Routing changed as intended: `>`, `<=`, `IS NULL` and `AND` across indexed columns now use an index where `main` read every row.
  • A key filter with no maintained index on the key column uses the memtable's own key index, and that index answers it exactly.
  • A differential test runs 65 filters over integers, floats, strings, booleans and dates holding nulls, NaN, both zeros, the infinities, the integer extremes, and empty and non-ASCII text, with indexes on all or some of the columns, and once with an index that declines every filter inside AND and OR, and requires the index route to return exactly the rows a full read returns; with every column indexed, a filter on each type is checked to take the index route.
  • Planning: every shape the on-disk B-tree parser claims reaches the memtable index; an index over several columns answers no filter; positions an index offers past the visible rows are cut; AND/OR across two indexes compose; an unindexed conjunct comes back as the leftover; NOT is scanned; a list comparison is claimed through the cast.
  • A filter index that declines leaves the rows to the filter, which returns the filter's rows; on the index route the filter is applied before projection, so a leftover on a dropped column still holds, and row addresses are kept; one whose search fails makes the scan fail.
  • Each B-tree backend declines a search past its budget and answers one within it, IS NULL included; split bounds on one column are merged so neither declines alone; a search with no filter to fall back on is never declined; exclusive and inverted ranges on each backend, over a key held by thousands of rows; a declined answer reads exactly the filter's rows and row ids; candidates that fill a batch and candidates that are a few of its rows are both re-checked and keep their own row ids.
  • cargo nextest run -p lance passes (5,337 tests); cargo clippy -p lance --tests --benches -- -D warnings, cargo doc -p lance --no-deps with -D warnings and cargo fmt --all -- --check are clean.

@github-actions github-actions Bot added the enhancement New feature or request label Oct 8, 2026
@xuanyu-z
xuanyu-z force-pushed the memwal-filter-expression-pass branch from 5e171fd to 563b528 Compare October 8, 2026 21:10
@xuanyu-z
xuanyu-z force-pushed the memwal-filter-expression-pass branch 10 times, most recently from d0fff70 to df1a6e2 Compare October 9, 2026 19:21
@xuanyu-z
xuanyu-z marked this pull request as ready for review October 9, 2026 19:22

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

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Oct 9, 2026
@xuanyu-z
xuanyu-z force-pushed the memwal-filter-expression-pass branch from df1a6e2 to 807829e Compare October 9, 2026 20:02
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Oct 9, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 2026
@xuanyu-z
xuanyu-z force-pushed the memwal-filter-expression-pass branch from 807829e to 9bda73f Compare October 9, 2026 20:17
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 9, 2026
@xuanyu-z
xuanyu-z force-pushed the memwal-filter-expression-pass branch from 9bda73f to 8915c06 Compare October 9, 2026 22:34
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 2026
jackye1995 pushed a commit that referenced this pull request Oct 9, 2026
…table index plugins (#9805)

Independent of #9804: based on `main`, and either can merge first.

## Why

A memtable index plugin can take no settings from the writer beyond
HNSW's own, and every scalar index it hands to the flush is built with
the index type's default parameters. A plugin whose in-memory structure
or flushed index has tunable settings has no way to receive them.

## What changes

- **Writer settings reach any plugin.**
`ShardWriterConfig::index_overrides` (`with_index_override`) carries
each index's settings, by index name, as the type its plugin reads
through `ResolveContext::overrides`. The HNSW settings (`hnsw_params`)
feed the same channel; naming one index in both is an error. Each writer
passes its own: unlike `hnsw_params`, these settings are not recorded as
writer defaults.
- **A plugin that reads settings checks their type.** Settings of a type
the plugin does not read stop the writer from opening, instead of
falling back to defaults. Settings for an index whose plugin reads none
are ignored, as before, and logged.
- **A scalar flush uses the plugin's parameters.**
`MemIndexPlugin::flush_params(spec)` gives the parameters for
`BuildFromGeneration` and `TrainingData` builds; by default these are
the index type's default parameters, so the built-in kinds build as
before. The writer calls it when it opens, so a plugin rejecting its
parameters stops the writer from opening rather than failing a flush.
- **The plugin contract is written down** on `resolve` and
`flush_params`: read settings through `ResolveContext::overrides`, keep
them in the params `resolve` returns so they reach both the in-memory
index and the flush, and so a changed setting is a different index under
`MemIndexSpec::same_index`.

No behaviour of the built-in kinds changes.

## Breaking changes

- `ShardWriterConfig` gains a public `index_overrides` field.
- `ResolveContext::overrides` returns `Result<Option<&P>>`; its settings
are no longer a public field, and the context is built only by Lance.

## Testing

- Non-default HNSW settings given through `index_overrides` are the ones
the index is built with.
- Settings of the wrong type stop the writer from opening; settings for
a kind that reads none are ignored and the index is maintained; settings
named in both channels stop the writer from opening.
- A B-tree flushed with a non-default zone size records that zone size
on disk, both when the index hands over its rows and when the flush
reads the generation.
- A plugin rejecting its flush parameters fails validation.
- `cargo nextest run -p lance` passes (5,308 tests); `cargo clippy -p
lance --all-targets -- -D warnings`, `cargo doc -p lance` with `-D
warnings` and `cargo fmt --all -- --check` are clean.
@xuanyu-z
xuanyu-z force-pushed the memwal-filter-expression-pass branch from 8c4f5b8 to 8aca5d9 Compare October 9, 2026 23:08
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 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 index_details rename is consistent across spec construction, query parsing, and index identity checks. Indexed filtering still matches full scans after the rebase, including visibility, fallback, projection, and Blob rechecks. No remaining blocker found.

Please mark this PR with the breaking-change label.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 2026
@jackye1995
jackye1995 merged commit 526dfec into lance-format:main Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants