Anurag-S1ngh commented on code in PR #19276:
URL: https://github.com/apache/iceberg/pull/19276#discussion_r4811323663


##########
api/src/main/java/org/apache/iceberg/types/AssignFreshIds.java:
##########
@@ -54,14 +54,33 @@ private int idFor(int currentId, Type type) {
       Types.NestedField baseField = baseSchema.findField(fullName);
       // a file derives its nested field IDs from its own ID, so reusing a 
base ID is only safe when
       // the base field was already a file and had reserved that block
-      if (baseField != null && (!type.isFileType() || 
baseField.type().isFileType())) {
+      if (baseField != null

Review Comment:
   Moved the decision to a stack in the visitor: once a field gets a fresh id, 
every descendant is forced onto a fresh id too. Added 
`testAssignFreshIdsForcesDescendantsWhenParentChanges` for a list to struct 
change.



##########
api/src/main/java/org/apache/iceberg/types/AssignFreshIds.java:
##########
@@ -54,14 +54,33 @@ private int idFor(int currentId, Type type) {
       Types.NestedField baseField = baseSchema.findField(fullName);
       // a file derives its nested field IDs from its own ID, so reusing a 
base ID is only safe when
       // the base field was already a file and had reserved that block
-      if (baseField != null && (!type.isFileType() || 
baseField.type().isFileType())) {
+      if (baseField != null
+          && (!type.isFileType() || baseField.type().isFileType())
+          && canReuseId(baseField.type(), type)) {
         return baseField.fieldId();
       }
     }
 
     return nextId.get(type);
   }
 
+  private static boolean canReuseId(Type baseType, Type type) {
+    if (baseType.equals(type)) {
+      return true;
+    }
+
+    if (type.isFileType()) {
+      return baseType.isFileType();
+    }
+
+    if (baseType.isPrimitiveType() && type.isPrimitiveType()) {
+      return TypeUtil.isPromotionAllowed(baseType, type.asPrimitiveType());
+    }
+
+    return baseType.typeId() == type.typeId()

Review Comment:
   `canReuseId` now recurses through list element and map key/value types, so a 
list or map id is only reused when its children are compatible. Added list and 
map tests for changed element and value types.



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