malon64 opened a new pull request, #4496:
URL: https://github.com/apache/iceberg-python/pull/4496
Closes #4485
# Rationale for this change
Engines built on PyIceberg (e.g. Polars with its Iceberg SQL support, used
from dbt) need `CREATE OR REPLACE TABLE ... AS SELECT` that swaps the
definition and data in a single commit, so a failed transformation never leaves
readers with an empty or half-written table. Java has this through
`createOrReplaceTransaction()`; PyIceberg had no equivalent.
This adds three methods to `Catalog`:
```python
with catalog.create_or_replace_table_transaction("db.orders",
schema=df.schema) as txn:
txn.append(df)
```
- `replace_table_transaction`: replaces an existing table, raises
`NoSuchTableError` otherwise
- `create_or_replace_table_transaction`: replaces the table if it exists,
otherwise creates it
- `create_or_replace_table`: the same without staging data
The new `ReplaceTableTransaction` follows Java's
`TableMetadata.buildReplacement`:
- Keeps the table UUID and snapshot history and removes `main`, so the new
snapshot starts a fresh lineage
- Reuses field IDs by name (`TypeUtil.assignFreshIds` with a base schema),
including nested fields and PyArrow schemas
- Reuses partition field IDs by `(source_id, transform)` across all specs,
highest ID wins (`reassignPartitionIds`)
- Reuses an identical existing schema, spec or sort order rather than adding
a duplicate
- Merges properties into the existing ones; `format-version` can upgrade the
table
- On commit, asserts only the table UUID and the last assigned field and
partition IDs, like `UpdateRequirements.forReplaceTable`; current IDs and refs
are overwritten. The commit is not retried, since the replacement was built
against the loaded metadata
The methods are concrete on the base `Catalog` and only use `load_table` and
`commit_table`, so they work for every catalog, REST included (this matches how
Java's REST client builds the replacement client side).
This is a smaller take on the approach in the earlier
apache/iceberg-python#3220, which was closed for inactivity.
## Notes for reviewers
- **Deviation from Java:** partitioned v1 tables are rejected with a clear
error instead of carrying forward the current partition fields as void
transforms. Upgrading through `format-version=2` in the same replace avoids it.
Happy to add the v1 carry-forward here or in a follow-up.
- **Deviation from Java:** the last-assigned field and partition IDs are
always asserted, not only when a schema or spec is added. This is stricter and
can only cause extra conflicts, never wrong metadata.
- When the table does not exist, the create path reuses
`create_table_transaction`, so it inherits that method's existing lack of
hierarchical namespace support in the SQL and memory catalogs.
## Are these changes tested?
Yes:
- `tests/catalog/test_catalog_behaviors.py`: create then replace with data
(UUID, field ID reuse, merged properties, kept history, only new data visible),
partition spec and schema reuse, `NoSuchTableError` for a missing table, and a
commit failure when the table was dropped and recreated.
- `tests/test_schema.py`: field ID reuse by name for nested fields and `-1`
IDs.
`make lint` passes. The catalog, table and schema suites pass locally,
except 4 Hive Kerberos tests that need the `hive-kerberos` extra, which was not
installed. REST and Spark integration tests were not run locally.
## Are there any user-facing changes?
Yes: three new public `Catalog` methods and a "Create or replace a table"
section in `mkdocs/docs/api.md`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]