Skip to content

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

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

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

Conversation

@rahulsmahadev

@rahulsmahadev rahulsmahadev commented Nov 24, 2026 •

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 restores the removed field, with its id, position and name, when it is added back with the same source and transform and either no name or its own name. This matches Java's rewriteDeleteAndAddField. Restoring a removed time transform alongside another time transform added in the same update works in either order, 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), test_update_spec_rename_field_added_back_in_same_update (renaming it) and test_update_spec_restore_time_transform_with_added_time_transform (both add orders, v1 and v2) fail on main and pass with this change. test_update_spec_add_field_conflicts_with_added_field (a name or a transform already added in the update) passes on main and with this change; Java rejects these too. 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 alongside another time transform added in the same update now works in either order (Java accepts; main raises). A restored field can be removed or renamed again in the same update, as in Java; main raised. 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 <[email protected]>
@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!

rahulsmahadev and others added 2 commits November 24, 2026 03:20
Co-authored-by: Isaac <[email protected]>
The re-add under another name and the swap of two removed names asserted
main's results, which differ from Java (Java restores and renames the removed
field). Remove that test and a redundant conflict case, test renaming a restored
field, name the time transform test for both orders, and describe the restore
condition in the docstring.

Co-authored-by: Isaac <[email protected]>
@rahulsmahadev
rahulsmahadev marked this pull request as draft December 1, 2026 02:16

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