szehon-ho commented on code in PR #17957:
URL: https://github.com/apache/iceberg/pull/17957#discussion_r4170580134


##########
spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkTable.java:
##########
@@ -158,6 +173,118 @@ public Set<TableCapability> capabilities() {
     return capabilities;
   }
 
+  @Override
+  public boolean supportsColumnChange(TableChange.ColumnChange change) {
+    if (isMapKeyChange(change)) {
+      return false;
+    }
+
+    if (change instanceof TableChange.AddColumn) {
+      TableChange.AddColumn add = (TableChange.AddColumn) change;
+      Type type = tryConvert(add.dataType());
+      return add.isNullable()
+          && add.defaultValue() == null
+          && type != null
+          && isSupportedAtFormatVersion(type);
+    } else if (change instanceof TableChange.UpdateColumnType) {
+      return supportsTypeUpdate((TableChange.UpdateColumnType) change);
+    } else if (change instanceof TableChange.UpdateColumnNullability) {
+      return supportsNullabilityUpdate((TableChange.UpdateColumnNullability) 
change);
+    } else if (change instanceof TableChange.DeleteColumn) {
+      return supportsDeleteColumn((TableChange.DeleteColumn) change);
+    } else {
+      return change instanceof TableChange.RenameColumn
+          || change instanceof TableChange.UpdateColumnComment
+          || change instanceof TableChange.UpdateColumnPosition;
+    }
+  }
+
+  private Set<Integer> mapKeyFieldIds() {
+    if (mapKeyFieldIds == null) {
+      this.mapKeyFieldIds = mapKeyFieldIds(schema);
+    }
+
+    return mapKeyFieldIds;
+  }
+
+  private static Set<Integer> mapKeyFieldIds(Schema schema) {
+    Set<Integer> keyFieldIds = Sets.newHashSet();
+    for (Types.NestedField field : 
TypeUtil.indexById(schema.asStruct()).values()) {
+      if (field.type().isMapType()) {
+        Types.MapType map = field.type().asMapType();
+        
keyFieldIds.addAll(TypeUtil.indexById(Types.StructType.of(map.fields().get(0))).keySet());
+      }
+    }
+
+    return keyFieldIds;
+  }
+
+  private boolean isMapKeyChange(TableChange.ColumnChange change) {
+    String[] fieldNames = change.fieldNames();
+    int pathLength =
+        change instanceof TableChange.AddColumn ? fieldNames.length - 1 : 
fieldNames.length;
+    if (pathLength == 0) {
+      return false;
+    }
+
+    Types.NestedField field =
+        schema.findField(String.join(".", Arrays.copyOf(fieldNames, 
pathLength)));
+    return field != null && mapKeyFieldIds().contains(field.fieldId());
+  }
+
+  private boolean supportsTypeUpdate(TableChange.UpdateColumnType update) {
+    Types.NestedField field = schema.findField(String.join(".", 
update.fieldNames()));
+    if (field == null) {
+      return false;
+    }
+
+    Type newType = tryConvert(update.newDataType());
+    return newType != null
+        && isSupportedAtFormatVersion(newType)

Review Comment:
   As an optional cleanup, consider keeping `isSupportedAtFormatVersion` for 
additions and removing it from type updates. With the current promotion rules 
(INT to LONG, FLOAT to DOUBLE, decimal precision widening, or unchanged types), 
an update cannot introduce a type requiring a newer format version when the 
existing schema is valid. The primitive-type, promotion, and Spark round-trip 
checks are sufficient here.
   
   ```suggestion:-1+3
       return newType != null
           && newType.isPrimitiveType()
           && TypeUtil.isPromotionAllowed(field.type(), 
newType.asPrimitiveType())
           && SparkSchemaUtil.convert(newType).equals(update.newDataType());
   ```
   



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