rahulsmahadev opened a new pull request, #4476:
URL: https://github.com/apache/iceberg-python/pull/4476
# 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:
```python
# 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]