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


##########
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:
   if a parent field gets a fresh id here (for example `list<struct<x>>` -> 
`struct<x>`), wouldn't its children still match `baseSchema` by name and keep 
their old ids? to my understanding that leaves `points` with a new id while `x` 
keeps the old id, so a filtered parquet read like `points.x IS NULL` uses the 
reused child id and returns wrong results (0 rows instead of 3). once a parent 
field gets a fresh id, should we force fresh ids for all its descendants 
(struct fields, list elements, map keys/values) and add regression tests for 
list->struct, map->struct, and struct->map with filtered reads?



##########
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:
   here any `list` -> `list` or `map` -> `map` reuses the container id without 
checking its child types. for something like `list<int>` -> `list<date>` or 
`map<string, int>` -> `map<string, date>`, the container keeps the old id while 
the child gets a new one, causing reads to return stale values or fail. should 
we recursively check `canReuseId` on list element and map key/value types here?
   
   ```diff
   -    return baseType.typeId() == type.typeId()
   -        && (type.isStructType() || type.isListType() || type.isMapType());
   +    if (baseType.typeId() != type.typeId()) {
   +      return false;
   +    }
   +
   +    if (type.isListType()) {
   +      return canReuseId(
   +          baseType.asListType().elementType(), 
type.asListType().elementType());
   +    }
   +
   +    if (type.isMapType()) {
   +      return canReuseId(baseType.asMapType().keyType(), 
type.asMapType().keyType())
   +          && canReuseId(
   +              baseType.asMapType().valueType(), 
type.asMapType().valueType());
   +    }
   +
   +    return type.isStructType();
   ```



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