Skip to content

feat(mem_wal): move a live writer onto a schema change instead of reopening it - #9712

Merged
jackye1995 merged 33 commits into
lance-format:mainfrom
xuanyu-z:mem-wal-evolve-schema
Oct 8, 2026
Merged

jackye1995 merged 33 commits into
lance-format:mainfrom
xuanyu-z:mem-wal-evolve-schema

Conversation

@xuanyu-z

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

Copy link
Copy Markdown
Contributor

A schema change on a MemWAL-backed table — a column added, dropped, renamed at
any depth, or relaxed to nullable — lands on the base table only. A live
ShardWriter keeps the schema it was opened with, so it rejects every write
shaped to the new one, and the only way forward is to reopen it: re-read the
WAL tail since the last flush, re-flush what fills during replay, and rebuild
every in-memory index. The shard takes no writes while that happens.

ShardWriter::evolve_schema moves the writer instead. It seals the active
memtable under the schema it was written with and starts the next one under the
new schema, so the sealed one flushes as a generation of its own schema with the
index configs it was built with. An empty memtable is replaced rather than
sealed. Nothing is replayed and no index is rebuilt.
ShardWriter::evolve_to(&Dataset) derives both halves from a dataset.

The writer side of this change is small. Most of the diff is the read side: the memtable sealed at a schema change stays in memory until it flushes, still holding its rows under the old column names, so every read path now has to resolve such a memtable.

Reading across the change

A sealed generation already resolves to the table's schema by field id. An
in-memory memtable did not — pairing one with a schema it was not created from
was documented as outside the contract. Now a memtable whose schema no longer
matches the table resolves the same way, on every read path:

arm a memtable created before the change
scan projected and filtered under its stored names; a predicate it cannot answer runs above the resolution, still after the dedup
point lookup resolved through the plan path; the probe fast paths are skipped while any memtable predates the table's schema
vector search searched on the column's stored name; with a deferred predicate it ranks every row exactly before the filter
full-text search queried and looked up under stored names, the index contract included

A memtable that matches the table — every memtable in the steady state — keeps
exactly today's path, behind a positional check.

Writing safely across it

A put is validated and conformed under the writer lock, against the schema of
the memtable it lands in, so a write racing a schema change cannot be checked
against one schema and inserted under another. The logical, storage and blob
schemas, the primary key and the index configs move together as one
WriterSchema.

Recovery

A WAL entry now records the generation its rows belong to, beside the writer
epoch it already carried. Replay reads the boundary instead of measuring it,
which is what lets a generation end at a schema change rather than at a row
count. A WAL-only writer holds no memtable and records none; replay still sizes
those entries itself. Where a recorded generation will not fit the memtable
replaying it, replay refuses before publishing anything, so the shard is
untouched and reopens under the configuration it was written with.

This is a format addition — an older reader ignores the key and falls back to
measuring — and docs/src/format/table/mem_wal.md documents it.

Two bugs surfaced on the way and are fixed here: replay indexed only its final
memtable, so any generation it sealed mid-replay was published with no
primary-key sidecar and its rows could not be looked up by key; and the writer
did not own the entry's reserved metadata keys, so a caller's schema metadata
could name a generation the entry did not have.

Testing

mem_wal: 809 passing, fmt and clippy -D warnings clean. New coverage for
each read arm across a rename, an add and a drop; for a nested path, which only
resolves because the field-id map pairs full paths rather than top-level names;
for the recorded generation, including a boundary nothing else can measure; and
for the capacity refusal, asserting the manifest is unchanged by it.

@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 the enhancement New feature or request label Oct 5, 2026
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 5, 2026
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 5, 2026
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 5, 2026
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 5, 2026
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 5, 2026
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 5, 2026
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 5, 2026
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 5, 2026
@xuanyu-z
xuanyu-z force-pushed the mem-wal-evolve-schema branch from 505ff2f to 90933a7 Compare October 5, 2026 19:30
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Oct 5, 2026
hamersaw and others added 3 commits October 5, 2026 15:47
A schema change on the base table (a column added, dropped, renamed, or
relaxed to nullable) leaves a live ShardWriter holding the schema it opened
with. Every write shaped to the new schema is rejected, and the only way on
was to reopen the writer: replay the WAL tail and rebuild every in-memory
index.

ShardWriter::evolve_schema seals the active memtable under the schema it was
written with and starts the next one under the new schema. The sealed
memtable flushes as a generation of its own schema, with the index configs it
was built with, which the flush trigger now carries instead of the writer
holding one set. ShardWriter::evolve_to derives the schema and maintained
index set from a Dataset the way mem_wal_writer does.

Reads resolve each in-memory memtable by field id the way they already
resolve sealed generations: scan, point lookup, vector search and full-text
search each route a memtable whose schema no longer matches the table through
GenerationRead, and keep today's path for one that does. The point-lookup
probe fast paths are skipped while any memtable predates the table's schema.

Writes are now validated and conformed under the writer lock, against the
schema of the memtable they land in, so a write racing a schema change cannot
land under the wrong one. MemTableScanner::use_index(false) now also forces
brute force for vector search, which a memtable with a deferred predicate
needs to rank every row.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
A schema change and an index-set change are the same operation on a live
writer: build the next `WriterSchema`, then seal so the following memtable
carries it. `evolve_schema` and `replace_index_configs` now share that half and
build their replacement under the `state` write lock, against the schema the
writer actually holds, so neither can flatten the half it does not own.

`evolve_to` read `maintained_indexes` straight off the index details. A table
maintaining every index carries no names there -- they come from the table --
so the first schema change installed an empty set and the writer stopped
maintaining every index it had until something else refreshed it. All three
paths that hand a set to a writer now derive it through one helper, which is
what let this one drift.

Folds `rebind_active_indexes` into the shared seal path, drops the second
index-set comparison in `WriterSchema::is_equivalent`, and returns
`Option<SealFence>` from `evolve_schema` so a caller can wait on what it
sealed.

`evolve_schema` now checks the fence, which `force_seal_active` and
`replace_index_configs` already did.
`rust/AGENTS.md` puts imports at the top of the file, not in function bodies.

`WriterSchema`'s doc named only `evolve_schema` as replacing the writer's
current one, which is now half the answer, and `evolve_to` did not say what it
returns when the writer already holds the schema and index set.
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Oct 6, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 6, 2026
…column

Refusing every primary-key rename was too broad: an unsharded MemWAL has one
shard that owns every row, so no name decides anything there and the rename is
as ordinary as any other. Three end-to-end tests exist to prove that, two of
them on unsharded tables.

The refusal now reads the spec and fires only for a column the MemWAL actually
shards by, which is the column whose name decides where a row goes.
@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 6, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 6, 2026
`stored_name` and its full-text twin scanned their map by value on every
lookup, once per column per read. The maps are built once per generation, so
they are inverted there instead and the lookups are a hash hit. The two sites
that ask whether a stored column is the table's at all take a set of the
stored names, which is what they were using the map's keys for.
@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 6, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 6, 2026
Restricting only the column a sharded table hashes was not enough. The key is
how a writer identifies a row at all: a writer that has not yet seen a rename
aligns a batch through the names it still holds, so an upsert naming the new
key lands its values under the wrong field ids. On a sharded table it also
hashes the wrong column and places the row in the wrong shard.

Nothing downstream separates the two schemas — swapping two names leaves every
name and type known to both — and the refresh sweep does not heal what is
already stored. Refusing the rename removes the case, beside the retype and
non-null tightenings already refused here. Every other column renames freely.
@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 6, 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 with a non-blocking risk.

Primary-key renames require dropping the attached MemWAL first.

Consumers must stay in one mode or checkpoint, unset, and reinstall before switching, as clarified by the author. The API does not enforce that transition requirement; switching the same shard with an uncheckpointed tail can leave acknowledged writes unrecoverable.

Please mark this PR with the breaking-change label.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 6, 2026

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

A few nits and questions, but overall looks great! Thanks.

Comment thread rust/lance/src/dataset/mem_wal/write.rs Resolved
Every comment this change added now says why in one or two plain
sentences: restatements of the code and step-by-step test narration are
gone, and the invariants stay, shorter. The doc on `GenerationRead::names`
described the map in the direction it had before this change; it now
matches the code.
`memtable_cannot_take` was inserted between `fill_reached_flush_threshold`
and its doc comment and lint allowance, so both applied to the wrong
function. `flush_memtable`'s doc also continued after its `#[instrument]`
attribute.
`write.rs` is past 13,000 lines, mostly tests. The tests this change adds,
for moving a writer onto a new schema and for replaying the generation
boundaries that leaves behind, now live in `write/tests/evolve_schema.rs`.
Two helpers they share with the rest of the writer tests become
`pub(super)`.
@lance-gatekeeper lance-gatekeeper Bot removed the K-risk Latest Gatekeeper recommendation includes a non-blocking risk. label Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-format On-disk format: protos and format spec docs enhancement New feature or request format-change A change to the format spec, which requires a vote. Remove if minor (e.g. fixing typo).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants