timothyw553 commented on code in PR #19288:
URL: https://github.com/apache/iceberg/pull/19288#discussion_r4794423206


##########
core/src/main/java/org/apache/iceberg/SchemaUpdate.java:
##########
@@ -381,16 +378,48 @@ public UpdateSchema unionByNameWith(Schema newSchema) {
 
   @Override
   public UpdateSchema setIdentifierFields(Collection<String> names) {
-    this.identifierFieldNames = Sets.newHashSet(names);
+    this.identifierFieldIds = Sets.newHashSet();
+    this.identifierFieldNames = Sets.newHashSet();
+    resolveIdentifierFieldNames(names);
     return this;
   }
 
   @Override
   public UpdateSchema caseSensitive(boolean caseSensitivity) {
     this.caseSensitive = caseSensitivity;
+    if (!identifierFieldNames.isEmpty()) {
+      Set<String> unresolved = identifierFieldNames;
+      this.identifierFieldNames = Sets.newHashSet();
+      resolveIdentifierFieldNames(unresolved);
+    }
     return this;
   }
 
+  private void resolveIdentifierFieldNames(Collection<String> names) {
+    if (names.isEmpty()) {
+      return;
+    }
+
+    Types.StructType currentStruct =
+        updates.isEmpty() && deletes.isEmpty() && parentToAddedIds.isEmpty()
+            ? schema.asStruct()
+            : TypeUtil.visit(schema, new ApplyChanges(deletes, updates, 
parentToAddedIds, moves))

Review Comment:
   why are we applying the whole staged schema inside `setIdentifierFields`? if 
a caller does `renameColumn("a", 
"b").setIdentifierFields("c").renameColumn("b", "a")` or `renameColumn("a", 
"b").setIdentifierFields("a").renameColumn("a", "c")`, this fails because the 
intermediate struct has duplicate names or doesn't match original names like 
other mutators do. could we resolve existing columns via the base-plus-added 
lookup instead and defer unmatched names to `apply()`, plus add tests with 
`setIdentifierFields` between colliding renames?



##########
core/src/main/java/org/apache/iceberg/SchemaUpdate.java:
##########
@@ -522,30 +558,20 @@ private static Schema applyChanges(
       Map<Integer, Types.NestedField> updates,
       Multimap<Integer, Integer> parentToAddedIds,
       Multimap<Integer, Move> moves,
+      Set<Integer> identifierFieldIds,
       Set<String> identifierFieldNames,
       boolean caseSensitive) {
     // validate existing identifier fields are not deleted
     Map<Integer, Integer> idToParent = 
TypeUtil.indexParents(schema.asStruct());
 
+    for (Integer id : identifierFieldIds) {
+      validateIdentifierFieldNotDeleted(schema.findField(id), schema, deletes, 
idToParent);
+    }
+
     for (String name : identifierFieldNames) {
       Types.NestedField field =
           caseSensitive ? schema.findField(name) : 
schema.caseInsensitiveFindField(name);
-      if (field != null) {
-        Preconditions.checkArgument(
-            !deletes.contains(field.fieldId()),
-            "Cannot delete identifier field %s. To force deletion, "
-                + "also call setIdentifierFields to update identifier fields.",
-            field);
-        Integer parentId = idToParent.get(field.fieldId());
-        while (parentId != null) {
-          Preconditions.checkArgument(
-              !deletes.contains(parentId),
-              "Cannot delete field %s as it will delete nested identifier 
field %s",
-              schema.findField(parentId),
-              field);
-          parentId = idToParent.get(parentId);
-        }
-      }
+      validateIdentifierFieldNotDeleted(field, schema, deletes, idToParent);

Review Comment:
   to my understanding `identifierFieldNames` holds pending names for the final 
schema, so checking them against the original `schema` here makes deleting `b`, 
calling `setIdentifierFields("b")`, and then adding required `b` throw while 
adding `b` before `setIdentifierFields("b")` succeeds. should we drop this loop 
since selected ids are already checked above and pending names get resolved 
against the final schema below?
   
   ```diff
   -    for (String name : identifierFieldNames) {
   -      Types.NestedField field =
   -          caseSensitive ? schema.findField(name) : 
schema.caseInsensitiveFindField(name);
   -      validateIdentifierFieldNotDeleted(field, schema, deletes, idToParent);
   -    }
   ```



##########
core/src/main/java/org/apache/iceberg/SchemaUpdate.java:
##########
@@ -557,13 +583,14 @@ private static Schema applyChanges(
     // validate identifier requirements based on the latest schema
     Map<String, Integer> nameToId =
         caseSensitive ? TypeUtil.indexByName(struct) : 
TypeUtil.indexByLowerCaseName(struct);
-    Set<Integer> freshIdentifierFieldIds = Sets.newHashSet();
+    Set<Integer> freshIdentifierFieldIds = Sets.newHashSet(identifierFieldIds);
     for (String name : identifierFieldNames) {
+      String lookupName = caseSensitive ? name : name.toLowerCase(Locale.ROOT);

Review Comment:
   none of the tests seem to hit this pending-name lowercasing path since the 
current case-insensitive test resolves an existing field early. could we add a 
test that sets `"NEW_FIELD"` and `caseSensitive(false)` before adding required 
column `"new_field"` (and the other call orders)?



-- 
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]

Reply via email to