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. |
|
Thanks for catching this. Java rejects it because it checks that no snapshot has a sequence number above |
A snapshot without a parent may carry an old sequence number, so a stale replace committed after writes to another branch made last-sequence-number go backwards. Java rejects this when building the metadata (indexAndValidateSnapshots); do the same when adding the snapshot.
|
Thanks for the fix! I retested fc49119. The original scenario now passes, but two issues remain on SQLite:
Could we check against the table’s last-sequence-number directly and handle cleanup for definitive commit failures? |
- Reject a snapshot whose sequence number is below last-sequence-number with CommitFailedException, so expired snapshots are covered and the replace cleans up its files. - Assert every branch the replace writes to, not only main, so a concurrent commit to it fails instead of being dropped. - Write dynamic_partition_overwrite data with the transaction metadata, so it uses the new spec, schema and location. - Reject replacing any partitioned v1 table, also when upgrading: Java keeps the current v1 partition fields, which is not supported yet, and the v2 path could assign duplicate partition field IDs.
|
Thanks for the retest. Both points were right: the check now compares against last-sequence-number and raises CommitFailedException, so expired snapshots are covered and the written files get cleaned up. I also did a full pass on the PR to avoid more back and forth, and fixed a few more things: Tests added for each. A dropped table during the commit still reports "commit state unknown"; that also affects plain transactions, so I'll do it in a follow-up as it is an existing behaviour. |
|
Thanks for the fixes! Two follow-up observations on 7a0a006:
Ref-conflict rejection, associated cleanup, and dynamic overwrite checks passed. REST/Java was not retested. |
- Only reject replacing a v1 table whose current spec is partitioned. An unpartitioned v1 table has no fields to keep, so it can become partitioned when upgraded to v2. Like Java, v1 tables only reuse the partition field IDs of their current spec. - Raise CommitFailedException when a snapshot with a parent has a stale sequence number, like Java's RetryableValidationException. Plain transactions now retry instead of failing, and a replace writing to a branch cleans up its files.
|
Thanks again for testing @korsakowii, fixed in 29400f2.
While checking I found one more: the existing sequence check for snapshots with a parent raised |
Closes #4485
Rationale for this change
Engines like Polars need to run
CREATE OR REPLACE TABLE ... AS SELECT(e.g. for dbt) in a single commit, so readers never see an empty or half-written table.This adds two methods to
Catalog:replace_table_transaction: replaces an existing table, raisesNoSuchTableErrorif it doesn't existcreate_or_replace_table_transaction: creates the table, or replaces it if it existsThe replacement follows 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), identical schemas/specs/sort orders are reused, and properties are merged.Differences from Java
forReplaceTableasserts none). Without it, a concurrent commit to that branch was dropped on v1, or rejected without cleaning up the written files on v2.CommitFailedExceptionand the written files are cleaned up.Related fixes
AddSnapshotUpdatenow rejects a snapshot whose sequence number is belowlast-sequence-number, withCommitFailedException. A snapshot without a parent built on stale metadata could make it go backwards. Java only checks retained snapshots; the spec says it must only increase. This touches shared metadata code, happy to split it out.write.metadata.pathalso applies to manifests.dynamic_partition_overwritewrites with the transaction metadata instead of the committed one (pre-existing bug, visible when replacing the spec).Follow-up: a table dropped during the commit reports
CommitStateUnknownExceptioninstead of failing cleanly. Plain transactions behave the same, so I'll fix it separately.Are these changes tested?
Yes, in
tests/catalog/test_catalog_behaviors.py(memory and SQL catalogs) andtests/test_schema.py: create or replace, ID reuse, concurrent schema/sort order/branch commits, cleanup on failure, sequence numbers with snapshot expiration, new location,dynamic_partition_overwrite, format version checks. REST and Spark integration suites weren't run locally.Are there any user-facing changes?
Yes: two new
Catalogmethods, documented in a new "Create or replace a table" section inmkdocs/docs/api.md.