Repository navigation
Conversation
Add replace_table_transaction, create_or_replace_table_transaction and create_or_replace_table to Catalog, so engines like Polars can run CREATE OR REPLACE TABLE AS SELECT (e.g. for dbt) in a single commit. Mirrors Java's TableMetadata.buildReplacement: the table keeps its UUID and history, main is removed, field ids are reused by name, partition field ids by (source, transform), and properties are merged. The commit asserts only the table UUID and last assigned ids, like UpdateRequirements.forReplaceTable. Closes apache#4485
malon64
force-pushed
the
feat/create-or-replace-table
branch
from
November 25, 2026 23:06
7beaacf to
f1d881c
Compare
|
I have some additional validation results for
All six checks passed across both catalogs. |
Drop create_or_replace_table, the transaction already covers it. Clean up written files and refuse reuse when a replace commit fails, like Transaction.commit_transaction. Build the snapshot producer's location provider from the transaction metadata, so a new location or write.metadata.path applies to manifests too. Validate the target format version and properties up front.
The replaced main has no parent, so a snapshot built from a stale base is accepted with an old sequence number and a concurrent commit makes last-sequence-number go backwards. Fail the replace instead. Also run the new replace tests with a single identifier fixture.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 SELECTthat 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 throughcreateOrReplaceTransaction(); PyIceberg had no equivalent.This adds three methods to
Catalog:replace_table_transaction: replaces an existing table, raisesNoSuchTableErrorotherwisecreate_or_replace_table_transaction: replaces the table if it exists, otherwise creates itcreate_or_replace_table: the same without staging dataThe new
ReplaceTableTransactionfollows Java'sTableMetadata.buildReplacement:main, so the new snapshot starts a fresh lineageTypeUtil.assignFreshIdswith a base schema), including nested fields and PyArrow schemas(source_id, transform)across all specs, highest ID wins (reassignPartitionIds)format-versioncan upgrade the tableUpdateRequirements.forReplaceTable; current IDs and refs are overwritten. The commit is not retried, since the replacement was built against the loaded metadataThe methods are concrete on the base
Catalogand only useload_tableandcommit_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 #3220, which was closed for inactivity.
Notes for reviewers
format-version=2in the same replace avoids it. Happy to add the v1 carry-forward here or in a follow-up.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,NoSuchTableErrorfor a missing table, and a commit failure when the table was dropped and recreated, or when a concurrent schema or sort-order change happened.tests/test_schema.py: field ID reuse by name for nested fields and-1IDs.make lintpasses. The catalog, table and schema suites pass locally, except 4 Hive Kerberos tests that need thehive-kerberosextra, which was not installed. REST and Spark integration tests were not run locally.Are there any user-facing changes?
Yes: three new public
Catalogmethods and a "Create or replace a table" section inmkdocs/docs/api.md.