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
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.
|
Anyone to approve this PR ? It is done now @oresttokovenko @korsakowii |
|
Following up on my earlier validation, I tested the new snapshot guard on Concurrent appends to On SQLite, two appends to an The Java REST fixture rejected the same commit with HTTP 400. Since sequence numbers are table-wide and monotonically increasing, should we also guard against sequence advancement on other branches? Happy to share a reproducer. |
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.