Skip to content

fix: reject field metadata updates that change how stored data decodes - #11331

Merged
Xuanwo merged 5 commits into
lance-format:mainfrom
maharanay22:fix-field-metadata-decoding
Dec 1, 2026
Merged

Xuanwo merged 5 commits into
lance-format:mainfrom
maharanay22:fix-field-metadata-decoding

Conversation

@maharanay22

@maharanay22 maharanay22 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

update_field_metadata (and the deprecated replace_field_metadata) can change the metadata keys that tell readers how a column's stored data is laid out: the packed keys (packed, lance-encoding:packed), the blob key (lance-encoding:blob) and the Blob v2 extension name (ARROW:extension:name). Data files keep the layout they were written with, so after such an update the new version reads the column wrong:

  • Silent wrong results. On 2.1+, marking an existing struct or list column packed commits, and every row of the column then reads back as NULL.
  • Unreadable column. Marking an existing large-binary column as a blob commits, and every scan then fails ("there were more fields in the schema than provided column indices"). On 2.0 the same happens when a struct or list gains or loses the packed key.
  • Misleading error. On 2.1+, a replace that drops the packed key or the Blob v2 extension name fails with an internal-looking column_index error.
import lance, pyarrow as pa

tbl = pa.table({"l": pa.array([[1, 2], [3]], pa.list_(pa.int32()))})
ds = lance.write_dataset(tbl, "/tmp/t", data_storage_version="2.1")
ds.update_field_metadata({"l": {"packed": "true"}})
print(lance.dataset("/tmp/t").to_table().to_pylist())
# [{'l': None}, {'l': None}]

Fix. check_field_decoding_unchanged in transaction/validate.rs compares the schema before and after a field metadata update. For every existing field, it refuses the update with an InvalidInput error if it changes, adds or removes the raw value of packed, lance-encoding:packed or lance-encoding:blob, or of ARROW:extension:name when either side is Blob v2. This is the same immutability rule the unenforced primary-key keys already use. The values are compared as stored, not parsed, because readers and writers parse the packed keys differently. A few harmless edits are refused as a result ("true" to "TRUE", switching to the other spelling of the packed key); it's easier to loosen that later than to tighten it.

The check runs on both apply paths: UpdateConfig in build_manifest, and a composite operation that contains a ConfigUpdate with field metadata, over the schema it builds. Fields created in the same operation take their layout from their first write. The error is raised after check_blob_nesting, so a Blob v2 nested in a Map keeps its more specific error.

Why every existing field, not only fields with data. An append, or a field re-encode, staged against the old schema can still commit a file in the old layout after the update lands, and overlays keep the old layout. Refusing the change on every existing field closes that race without changing the conflict resolver.

Behavior changes.

  • Promoting an existing column to Blob v2 by setting its metadata is no longer possible.
  • A replace must restate the packed, blob and Blob v2 keys with the same values, so .replace(field, []) now errors for packed and blob columns. The Rust, Python and Java docs now say so.

Follow-up: #11696 (packed key parsing differs between readers and writers).

Testing

  • validate.rs::test_changed_decoding_key: 16 cases (same value, unrelated key, mark/drop/false/case change/alias of the packed keys, struct, list and leaf, blob mark/drop/value, Blob v2 mark/drop, a non-blob extension).
  • config_update.rs: the action path refuses a decoding change with or without stored data, and even when the field is fully re-encoded in the same operation. A re-encode that keeps the keys commits. A field created in the same operation can take any encoding. Changing a packed struct's children is not a decoding change.
  • metadata.rs (each × 2.0 / 2.1 / 2.2):
    • test_update_field_metadata_rejects_decoding_change: refused, nothing committed, data unchanged.
    • test_update_field_metadata_rejects_decoding_change_without_data.
    • test_update_field_metadata_keeps_decoding_when_allowed.
    • test_update_field_metadata_rejects_dropping_blob_v2.
    • test_packed_struct_edits_keep_appends_readable: edits that keep the packed key, under either spelling, followed by an append, read back from a reopened dataset; the alias swap and packed: "false" are refused.
  • test_dataset.py::test_field_metadata_rejects_decoding_change × 2.0 / 2.1 / 2.2.

cargo test -p lance-table --lib and cargo test -p lance --lib dataset::metadata pass. cargo fmt and cargo clippy -p lance-table -p lance --tests are clean apart from two existing Windows-only warnings in files this PR doesn't touch.

Readers take a field's layout from its metadata: the packed key
(`packed` / `lance-encoding:packed`, honored on nested fields), the blob
key (`lance-encoding:blob`) and the Blob v2 extension name. Data files
keep the layout they were written with, but `UpdateConfig` and the
Transaction V2 ConfigUpdate action applied field metadata without looking
at those keys. Marking an existing struct or list packed made it read back
as all nulls on 2.1+, marking an existing binary column a blob left it
unreadable on every storage version, and a replace that dropped one of
these keys either broke the column (2.0) or failed with a misleading
column-index error (2.1+).

Compare the schema before and after the field metadata updates, and
refuse an update that flips any of these properties for an existing
field. Whether a field can be packed follows its type, and packed counts
as changed when either reader's view changes: the Lance schema accepts
any truthy value, the Arrow-level struct decoder only "true". The check
covers fields with no data yet too, because a concurrent append that read
the old schema can still commit a file in the old layout, and its
conflict check treats an UpdateConfig as compatible. Fields minted in the
same composite operation may still be given any encoding.

@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 bug Something isn't working A-python Python bindings labels Nov 20, 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 Nov 20, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 28, 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 Nov 28, 2026

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted technical review of e10ffb8b

Two blockers remain from source inspection.

  1. packed_markers and the 2.0 struct writer apply different rules for alias precedence and key presence. A metadata edit that passes the commit check can still change how a later append lays out the struct. Please validate against both the existing reader and writer interpretations, and cover the two spellings with ordinary metadata edits followed by append and reopen. Check the returned struct values as well as the commit result.
  2. Every V2 composite commit calls check_field_decoding_unchanged, including append-only transactions. It walks the new schema and performs a linear before.field_by_id lookup for each field. Please skip this work when the actions cannot change field metadata, and use one ID map or linear traversal when they can. Compare the final state and keep the commit-time protection for fields that were empty when the edit began.

Please also clarify the boundary for a complete same-ID re-encoding. The V2 TombstoneFieldData and AddDataFile contract already supports rebinding a field to rewritten files. A guard for metadata-only edits should preserve that operation. The Rust and Python replacement docs should tell callers to retain annotations needed to decode or interpret existing values, including extension-type metadata.

… re-encodes

- Compare the 2.0 struct writer's reading of the packed keys as well:
  `packed` decides whenever present, and `lance-encoding:packed` packs a
  struct by being present at all. Edits that only it reads differently are
  now refused.
- Run the decoding check on the V2 path only when a ConfigUpdate changes
  field metadata, with one id map instead of a lookup per field.
- Allow a decoding change of a field the same operation re-encoded in
  full with TombstoneFieldData and AddDataFile.
- Document what a field metadata replace must restate.
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 29, 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 Nov 29, 2026
A same-id re-encode keeps the field's overlays in the old layout, and a
writer staged against the old schema can still commit an old-layout file
after it, so rewriting the base files does not make a marker change safe.
Drop the exemption. A re-encode that keeps the markers still commits,
together with other field metadata edits.
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Nov 29, 2026
@maharanay22

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in 4ae1208 and b284d67:

Alias precedence and key presence. The check now also compares the 2.0 struct writer's reading. That writer lets packed decide whenever it is present, and packs a struct that has lance-encoding:packed at all, even "false". Two edits used to pass the commit check and change how later appends lay out the struct: adding lance-encoding:packed: false to an unpacked struct, and adding packed: false beside lance-encoding:packed: true. Both are now rejected. test_packed_struct_edits_keep_appends_readable covers 2.0, 2.1 and 2.2 with both spellings. It makes ordinary edits, appends, reopens, and checks the returned struct values.
Commit cost. The V2 path runs the check only when a ConfigUpdate carries field metadata, so append-only commits skip it. It uses one ID map instead of field_by_id per field. It still compares against the current manifest at commit time, so a field that was empty when the edit began stays protected.
Same-ID re-encode. The guard doesn't affect TombstoneFieldData + AddDataFile re-encodes that keep the decoding markers. Changing the markers stays refused even in a full re-encode, because overlays survive it and a writer staged against the old schema can still commit afterwards. The check_field_decoding_unchanged docs state this boundary. The Rust replace and Python update_field_metadata(replace=True) docs now say to restate the packed and blob markers and extension-type metadata.
One choice to flag: the 2.0 writer rule applies on every table version, so lance-encoding:packed: false is also refused on 2.1+ tables, where it's harmless. I can limit that rule to 2.0 tables if you'd prefer.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 29, 2026
@Xuanwo

Xuanwo commented Nov 29, 2026

Copy link
Copy Markdown
Member

Please keep the guard across all table defaults. I verified on b284d67f that a table containing only 2.1 or only 2.2 fragments accepts an explicit 2.0 append, keeps its original default, and reads all values correctly after reopening. The 2.0 writer treats lance-encoding:packed as significant even when its value is false, so scoping the guard to tables with a 2.0 default would miss that append.

AI-assisted review.

@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 29, 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 mixed-version append clarification is confirmed: tables with a 2.1 or 2.2 default accept explicit 2.0 appends while retaining their default. The 2.0 writer check is therefore required across all table defaults.

Marker-changing re-encodes remain rejected, preventing the earlier overlay and stale-writer failures. Same-ID re-encodes that preserve the markers remain supported. No blocking findings.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 29, 2026
@maharanay22

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in 4ae1208 and b284d67:

Alias precedence and key presence. The check now also compares the 2.0 struct writer's reading. That writer lets packed decide whenever it is present, and packs a struct that has lance-encoding:packed at all, even "false". Two edits used to pass the commit check and change how later appends lay out the struct: adding lance-encoding:packed: false to an unpacked struct, and adding packed: false beside lance-encoding:packed: true. Both are now rejected. test_packed_struct_edits_keep_appends_readable covers 2.0, 2.1 and 2.2 with both spellings. It makes ordinary edits, appends, reopens, and checks the returned struct values.
Commit cost. The V2 path runs the check only when a ConfigUpdate carries field metadata, so append-only commits skip it. It uses one ID map instead of field_by_id per field. It still compares against the current manifest at commit time, so a field that was empty when the edit began stays protected.
Same-ID re-encode. The guard doesn't affect TombstoneFieldData + AddDataFile re-encodes that keep the decoding markers. Changing the markers stays refused even in a full re-encode, because overlays survive it and a writer staged against the old schema can still commit afterwards. The check_field_decoding_unchanged docs state this boundary. The Rust replace and Python update_field_metadata(replace=True) docs now say to restate the packed and blob markers and extension-type metadata.
One choice to flag: the 2.0 writer rule applies on every table version, so lance-encoding:packed: false is also refused on 2.1+ tables, where it's harmless. I can limit that rule to 2.0 tables if you'd prefer.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 29, 2026

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

Thanks for this, and for the thorough reproductions. Refusing decoding-marker changes on every existing field closes the stale-writer race without touching the conflict resolver, and the tests cover the cases well.

Nothing blocks from me. Non-blocking:

  • I'd rather not add a fourth parser for the packed keys; raw-key immutability would avoid it (inline).
  • suggestion: The write-time bugs under "Related, not fixed here" look like real data bugs: packed: "1" structs can't be read back on 2.1+, and on 2.0 neither can lance-encoding:packed: "false" ones. Could you file an issue? I think the real fix is one canonical struct for a field's encoding configuration, with a single parse method that every reader and writer uses instead of checking metadata keys directly. That would fix these bugs, and if we later want to allow harmless edits, this check could compare parsed configs instead of raw keys. Fine as a follow-up.
  • The "Clear field metadata" rustdoc example now fails for packed and blob columns (inline).
  • nitpick: The description is out of date after the last round. "Still allowed: setting or removing packed: "false"" no longer holds for lance-encoding:packed: "false", or for packed: "false" next to lance-encoding:packed: "true", and the Testing section misses test_packed_struct_edits_keep_appends_readable. Since it becomes the commit message, could you refresh and trim it?

/// Whether the field can be packed comes from its type rather than its
/// children, so a struct that gains its first child or loses its last one
/// keeps its markers. Leaves ignore them.
fn packed_markers(field: &Field) -> (bool, bool, bool) {

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.

suggestion: I'm a bit wary of adding a fourth parser for these keys. Today is_packed_struct in lance-core, is_packed_struct in lance-arrow, and the 2.0 writer each parse them differently, and packed_markers has to stay in step with all three. Could we avoid parsing here entirely and just treat the marker keys as immutable on existing fields? That would mean refusing any change to the raw value (including adding or removing it) of packed, lance-encoding:packed and lance-encoding:blob, and of ARROW:extension:name when either side is Blob v2. That's the rule we already use for the primary-key keys:

// The unenforced primary and clustering keys are reserved schema
// properties: each is immutable once set, and its reserved keys cannot
// be written with an invalid value. Capture what they were before the
// updates land so a violation can be rejected below. This runs on every
// apply, including a conflict rebase, so it also catches the
// concurrent-writer race.

We'd lose a few harmless edits (alias swap, true -> TRUE), but it's easier to loosen later than to tighten.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. packed_markers is gone, and the check no longer parses anything. On existing fields it now refuses any change, addition or removal of the raw value of packed, lance-encoding:packed and lance-encoding:blob, and of ARROW:extension:name when either side is Blob v2. That means the alias swap and true → TRUE are now refused. The test cases were updated to match.

Comment thread rust/lance/src/dataset/metadata.rs Outdated
/// extension-type metadata (`ARROW:extension:name`,
/// `ARROW:extension:metadata`). A replace that changes how stored data
/// decodes is refused. Other extension-type metadata is not checked, and
/// dropping it makes readers return the values as their storage type.

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.

suggestion: The "Clear field metadata" example on update_field_metadata now errors for packed and blob columns:

/// // Clear field metadata
/// dataset.update_field_metadata()
/// .replace("path.to_field", [] as [UpdateMapEntry; 0])?
/// .replace(12, [] as [UpdateMapEntry; 0])?
/// .await?;

Maybe add a line there pointing to this restate rule, or drop the example? The Java updateFieldMetadata/replace docs could use the same note as Python.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a note to the example saying a clear is refused for packed and blob fields and pointing to the restate rule on UpdateFieldMetadataBuilder::replace. Java has no updateFieldMetadata; field metadata goes through UpdateConfig.Builder.fieldMetadataUpdates, so I added the same note to its Javadoc.

Comment thread rust/lance/src/dataset/metadata.rs Outdated
/// extension-type metadata (`ARROW:extension:name`,
/// `ARROW:extension:metadata`). A replace that changes how stored data
/// decodes is refused. Other extension-type metadata is not checked, and
/// dropping it makes readers return the values as their storage type.

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.

thought: Another option for the clear/replace case would be for replace to carry the reserved encoding keys over when the new map leaves them out, and only error when the caller explicitly sets a different value. The "clear field metadata" example would keep working, and nobody's explicit write gets silently dropped. Just an idea, not needed for this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be nicer for callers. I've left it out here to keep this PR to the check itself, and noted it as an option in the follow-up issue.

@maharanay22

maharanay22 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I pushed the raw-key rule in 7d30517 along with the doc notes on the clear example and the Java builder, and refreshed the description. I filed #11696 for the packed parsing bugs, with your single-parser idea as the suggested fix. It also notes the replace carry-over option.

… them

Refuse any change, addition or removal of the raw value of packed, lance-encoding:packed and lance-encoding:blob on existing fields, and of ARROW:extension:name when either side is Blob v2, rather than comparing parsed packed readings. Document the restate rule on the clear example and the Java field metadata builder.
@github-actions github-actions Bot added the A-java Java bindings + JNI label Dec 1, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Dec 1, 2026
@Xuanwo
Xuanwo merged commit 6bbd636 into lance-format:main Dec 1, 2026
45 of 46 checks passed
wjones127 added a commit that referenced this pull request Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-java Java bindings + JNI A-python Python bindings bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants