Skip to content

fix: preserve overwrite schema changes - #5161

Open
LuciferYang wants to merge 6 commits into
lance-format:mainfrom
LuciferYang:fix/write-lance-schema-evolution
Open

LuciferYang wants to merge 6 commits into
lance-format:mainfrom
LuciferYang:fix/write-lance-schema-evolution

Conversation

@LuciferYang

@LuciferYang LuciferYang commented Jun 4, 2026 •

Copy link
Copy Markdown

write_lance(ds, uri, mode="overwrite") failed whenever the new data had a different schema from the existing dataset, for example after ds.with_column("id_2", ...) as in #95:

OSError: Append with different schema: fields did not match, missing=[], unexpected=[id_2]

The cause is that lance-ray called pylance's write_fragments without mode, so every fragment was validated as an append against the existing schema, even when the commit that followed was an Overwrite. stream=True failed the same way.

What changed

  • write_fragment and LanceFragmentWriter take a mode and pass it to write_fragments. They default to "append", so direct callers keep the old validation. LanceDatasink passes its own mode, so create and overwrite write fragments under the incoming schema and an overwrite can add, drop or retype columns.
  • The streaming path writes its first committed batch with the requested mode and later batches with "append", since those append to the dataset the first batch just created or overwrote.
  • In create/overwrite, every write task builds field ids from its own block, but all fragments get committed under one schema. If blocks differ in column order or type (for example a union of two sources), main either fails while writing or commits a version that can't be read back (int32 and int64 blocks give Need at least 16 bytes in buffers[0] on read). on_write_complete now merges the block schemas before committing: types must be equal (so list<item> and list<element> match), a column that is all null in some blocks takes the type from the other blocks, and a column is nullable if any block allows nulls. Any other difference raises before committing and suggests passing schema=.
  • With an explicit schema, pd_to_arrow reorders an Arrow block to the schema's column order before casting, so differently ordered blocks line up by name. Blocks whose column names differ from the schema still fail as before.

LanceFragmentWriter and LanceFragmentCommitter select the mode separately. The writer docstring says to pass the same mode to both when overwriting with a new schema.

Tests

New tests in tests/test_basic_read_write.py cover overwrite with an added, dropped or retyped column, both non-streaming and with stream=True (including resume_rows), across multiple fragments, and through a directory namespace. They also cover:

  • append keeps the existing schema
  • append with an incompatible type or an extra column fails
  • blocks with different column order or types raise in create/overwrite, and the version is unchanged
  • an all-null block before or after a typed one, mixed nullability, and lists with different child field names all write correctly
  • an explicit schema fixes the column-order case

tests/test_fragment.py adds the writer + committer overwrite case. The overwrite and column-order tests fail on main.

Closes #95

🤖 Generated with Claude Opus

@github-actions github-actions Bot added the bug Something isn't working label Jun 4, 2026
Distributed create/overwrite writes assign Lance field ids positionally, so
blocks whose columns arrive in a different order (e.g. a union of differently
ordered sources) were committed under a single schema and read back transposed.
Add a guard in LanceDatasink.on_write_complete that raises on inconsistent
column order, and align Arrow blocks by name in pd_to_arrow so an explicit
schema writes correctly.

Also harden the schema-evolution mode plumbing: document per-mode schema
behaviour (append validates/drops; create and overwrite evolve the schema),
fix the LanceDatasink default-mode docstring, document the write_fragment mode
parameter and the writer/committer mode-pairing requirement, and add regression
tests for multi-fragment overwrite, schema drop/type-change, streaming overwrite
resume, streaming-append rejection, and the column-order guard.
@LuciferYang

Copy link
Copy Markdown
Author

cc @jackye1995 FYI

…ema-evolution

# Conflicts:
#	lance_ray/fragment.py
#	lance_ray/io.py
#	tests/test_basic_read_write.py
#	tests/test_fragment.py
…ema-evolution

# Conflicts:
#	lance_ray/fragment.py
@LuciferYang

Copy link
Copy Markdown
Author

@yanghua @ddupg when you have a moment, could you approve the pending PR Checks run and comment @claude review here? The same checks pass on my fork: https://github.com/LuciferYang/lance-ray/actions/runs/39268371524

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

write_lance failed after add a new column to existed dataset.

1 participant