Skip to content

Use the table's format version in union_by_name - #4604

Merged
Fokko merged 3 commits into
apache:mainfrom
rahulsmahadev:rahul/union-by-name-table-format-version
Dec 4, 2026
Merged

Fokko merged 3 commits into
apache:mainfrom
rahulsmahadev:rahul/union-by-name-table-format-version

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: UpdateSchema.union_by_name() converts a PyArrow schema with format_version=2 by default, so on a v3 table it rejects nanosecond timestamps and null columns:

table = catalog.load_table("db.events")  # format-version 3

table.update_schema().union_by_name(pa.schema([("event_ts", pa.timestamp("ns"))])).commit()
# UnsupportedPyArrowTypeException: Column 'event_ts' has an unsupported type: timestamp[ns]

table.update_schema().union_by_name(pa.schema([("note", pa.null())])).commit()
# ValueError: Null type (pa.null()) is not supported in Iceberg format version 2. Field: note. ...

The 2 came in #2300 as a stand-in for TableProperties.DEFAULT_FORMAT_VERSION (circular import, see the removed TODO), which is the version for new tables.

Fix: format_version now defaults to None, meaning the version of the table being updated, which the commit check already uses and append, overwrite, upsert, add_files and scans pass to the same conversion. An explicit format_version still wins, and v1 and v2 keep their rules (the v1 null error now names version 1). BigQueryMetastoreCatalog.create_table() also still converts with the default; it cannot create v3 tables until v3 metadata writes land (#1551), so that is left for a separate PR.

Java: Spark's mergeSchema write converts its schema without a format version (NullType becomes unknown), calls unionByNameWith, and TableMetadata.Builder checks the result against the table's format version; this PR makes the PyArrow conversion use it too. Unlike Java, PyIceberg also checks the version while converting, and a null column merged into an existing concrete primitive column keeps its type, as the v3 spec allows promoting unknown to any type; Java's union rejects it, as isPromotionAllowed has no unknown case.

Are these changes tested?

Yes, added test_union_with_pa_schema_uses_table_format_version in tests/test_schema.py (v3 gets timestamp_ns, timestamptz_ns and unknown; v2, and v3 with format_version=2, still raise) and a union_by_name null case to test_update_schema_rejects_v3_only_types. On main the new test fails with UnsupportedPyArrowTypeException: Column 'ts' has an unsupported type: timestamp[ns], and the v1 cases fail because the error names version 2; all pass with this change.

Are there any user-facing changes?

Yes, on a v3 table union_by_name() now converts PyArrow null to unknown and timestamp[ns] to timestamp_ns or timestamptz_ns (unless downcast-ns-timestamp-to-us-on-write is set) without format_version=3, as appends already do.

Please add the changelog label.

This pull request and its description were written by Isaac.

rahulsmahadev and others added 3 commits December 2, 2026 17:55
union_by_name converted a PyArrow schema with format_version=2 unless the caller passed another
version, so on a v3 table timestamp[ns] and null columns were rejected with an error naming v2.
format_version now defaults to None, which means the format version of the table being updated,
the same version that the schema update checks the new schema against at commit.

Co-authored-by: Isaac <no-reply@databricks.com>
A mutant that always used the table's version still passed, so also check that format_version=2 on
a v3 table rejects timestamp[ns]. List the PyArrow conversion errors in the docstring's Raises.

Co-authored-by: Isaac <no-reply@databricks.com>
UnsupportedPyArrowTypeException is raised for any unsupported PyArrow
type, not only for types that depend on the format version.

Co-authored-by: Isaac <no-reply@databricks.com>

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

Clean fix, and nice that an explicit format_version still wins. Thanks @rahulsmahadev 🚀

@Fokko
Fokko added this pull request to the merge queue Dec 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Dec 3, 2026
@Fokko
Fokko added this pull request to the merge queue Dec 4, 2026
Merged via the queue into apache:main with commit 114a967 Dec 4, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants