Skip to content

Undelete a partition field re-added in the same update - #4476

Open
rahulsmahadev wants to merge 2 commits into
apache:mainfrom
rahulsmahadev:rahul/readd-partition-field-undelete
Open

rahulsmahadev wants to merge 2 commits into
apache:mainfrom
rahulsmahadev:rahul/readd-partition-field-undelete

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: removing a partition field and adding it back with the same source, transform and name in one update should leave the spec unchanged. PyIceberg adds a new field instead:

# format-version 1, partition by identity(region) (field id 1000)
tbl.update_spec().remove_field("region").add_identity("region").commit()
# spec: [1000 "region_1000" void, 1001 "region" identity], default spec id 1
# expected: [1000 "region" identity], default spec id 0

On format-version 2, when the field is not the last one, the re-added field moves to the end and a new spec is written.

Fix: add_field keeps the removed field, with its id and position, when it is added back with the same name, source and transform. This matches Java's rewriteDeleteAndAddField. A time transform restored after another time transform was added in the same update is accepted too, as in Java; main raises Cannot add time partition field.

Java callers: Spark's REPLACE PARTITION FIELD converts SQL transforms to Iceberg terms and removes/adds in one update; Flink's TableUpdater does this for PartitionSpecEvolution's diff of the current and requested specs. This PR covers Python's update_spec().

Re-adds under another name and unnamed re-adds after a new field takes the removed name retain main's behaviour. Java restores and renames the removed field in these cases; that remains a follow-up.

Builds on #4110 (partition field ID reuse on v3 tables) and #4307 (a new field can take a removed field's name).

Are these changes tested?

Yes. test_update_spec_remove_and_add_same_field_in_one_update (v1 and v2, first and last field), test_update_spec_remove_field_added_back_in_same_update (removing the field again) and test_update_spec_restore_time_transform_after_added_time_transform (both add orders, v1 and v2) fail on main and pass with this change. test_update_spec_add_field_with_new_name_after_remove (a re-add under another name, and a swap of two removed names) and test_update_spec_add_field_conflicts_with_added_field (a name or a transform already added in the update) pass on main and with this change, so they pin main's behaviour. test_update_spec_remove_and_add_renamed_field_in_one_update (a field renamed in an earlier update, restored under its new name, then a later add of that name) fails on main and passes with this change. test_update_spec_remove_and_add_same_field_reuses_id gains a v1 case, which fails on main.

Are there any user-facing changes?

Yes. Removing and re-adding the same source, transform and name in one update preserves the spec (no void field on v1, no new spec id). Restoring a removed time transform after another time transform was added in the same update now works (Java accepts; main raises). Re-adds under a different name are unchanged from main. An unnamed re-add keeps the current partition name, as Java does. Main generated a default name on v1 or could reuse a historical name on v2/v3. A later add can no longer take the restored name.

This pull request and its description were written by Isaac.

Removing a partition field and adding it back with the same name, source
and transform in one update created a new field (a void field and a new id
on v1, a reordered spec on v2). add_field now keeps the removed field, with
its id and position, as Java's rewriteDeleteAndAddField does. Re-adds under
another name are unchanged.

Co-authored-by: Isaac <no-reply@databricks.com>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@kevinjqliu, you merged #4110 (reuse partition field IDs on v3 tables), which this builds on. Could you take a look when you have a chance? Thanks!

Co-authored-by: Isaac <no-reply@databricks.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant