rahulsmahadev opened a new pull request, #4604:
URL: https://github.com/apache/iceberg-python/pull/4604
# 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:
```python
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]