timothyw553 commented on code in PR #19275:
URL: https://github.com/apache/iceberg/pull/19275#discussion_r4771177959
##########
core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java:
##########
@@ -143,6 +143,20 @@ private PartitionField recycleOrCreatePartitionField(
sourceTransform.first(), assignFieldId(), name,
sourceTransform.second());
}
+ private boolean canRecycle(PartitionField field) {
+ for (PartitionField current : spec.fields()) {
+ if (deletes.contains(current.fieldId())) {
+ continue;
+ }
+
+ if (current.fieldId() == field.fieldId() ||
current.name().equals(field.name())) {
Review Comment:
to my understanding, `addField` already renames a conflicting void field, so
rejecting when `current` is a void field skips required id reuse
(`removeField("p_1000").addField("p")` assigns 1002 instead of 1000) and breaks
restoring `bucket(p, 16)` in v2/v3 with `Cannot add duplicate partition field
name: null`. should we exempt void fields from the name check here and add
regression tests for both cases?
```diff
- if (current.fieldId() == field.fieldId() ||
current.name().equals(field.name())) {
+ if (current.fieldId() == field.fieldId()
+ || (current.name().equals(field.name()) &&
!isVoidTransform(current))) {
return false;
}
```
##########
core/src/test/java/org/apache/iceberg/TestUpdatePartitionSpec.java:
##########
@@ -656,6 +657,55 @@ public void
testRemoveAndUpdateWithDifferentTransformation() {
}
}
+ @Test
+ public void testAddDroppedPartitionFieldWithConflictingId() {
+ assumeThat(formatVersion).isEqualTo(1);
+
+ Schema schema =
+ new Schema(
+ Types.NestedField.required(1, "id", Types.IntegerType.get()),
+ Types.NestedField.optional(2, "p", Types.IntegerType.get()));
+ TestTables.clearTables();
+ TestTables.TestTable table =
+ create(schema, PartitionSpec.builderFor(schema).identity("p").build());
+
+ table.updateSpec().removeField("p").commit();
+ table.updateProperties().set(TableProperties.FORMAT_VERSION, "2").commit();
+ table.updateSpec().addField("p").commit();
+
+ PartitionSpec spec = table.spec();
+ assertThat(spec.fields()).hasSize(2);
+
assertThat(spec.fields().stream().map(PartitionField::fieldId).distinct().count()).isEqualTo(2);
Review Comment:
here the historical field conflicts on both id and name, so removing the id
check in `canRecycle` still passes. more generally, the new tests still pass if
we stop skipping `deletes`, drop the `nameToAddedField` check, or never recycle
at all. could we add focused tests (maybe in `TestTableUpdatePartitionSpec`)
isolating an id-only conflict, a name clash with a pending add, a delete
freeing a name, and valid id reuse, checking the actual field id and name?
##########
core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java:
##########
@@ -143,6 +143,20 @@ private PartitionField recycleOrCreatePartitionField(
sourceTransform.first(), assignFieldId(), name,
sourceTransform.second());
}
+ private boolean canRecycle(PartitionField field) {
+ for (PartitionField current : spec.fields()) {
+ if (deletes.contains(current.fieldId())) {
+ continue;
+ }
+
+ if (current.fieldId() == field.fieldId() ||
current.name().equals(field.name())) {
+ return false;
+ }
+ }
+
+ return !nameToAddedField.containsKey(field.name());
Review Comment:
this checks `nameToAddedField` for name clashes with pending additions, but
what if two fields in the same update try to recycle the same historical field
id (e.g. from v1 history)? both can end up with id 1000 and break
`partitionType().field(1000)`. could we also check `adds` here, or track this
in a follow-up if it feels out of scope?
```diff
+ for (PartitionField added : adds) {
+ if (added.fieldId() == field.fieldId()) {
+ return false;
+ }
+ }
+
return !nameToAddedField.containsKey(field.name());
```
--
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]