szehon-ho commented on code in PR #17957:
URL: https://github.com/apache/iceberg/pull/17957#discussion_r4237378072
##########
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;
+ if (!add.isNullable() || add.defaultValue() != null) {
+ return false;
+ }
+
+ Type type = tryConvert(add.dataType());
+ return 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
+ && newType.isPrimitiveType()
+ && TypeUtil.isPromotionAllowed(field.type(), newType.asPrimitiveType())
+ && SparkSchemaUtil.convert(newType).equals(update.newDataType());
+ }
+
+ private boolean
supportsNullabilityUpdate(TableChange.UpdateColumnNullability update) {
+ Types.NestedField field = schema.findField(String.join(".",
update.fieldNames()));
+ return update.nullable() && field != null &&
!containsIdentifierField(field);
+ }
+
+ private boolean supportsDeleteColumn(TableChange.DeleteColumn delete) {
+ Types.NestedField field = schema.findField(String.join(".",
delete.fieldNames()));
+ if (field == null) {
+ return false;
+ }
+
+ // Iceberg rejects dropping an identifier field or a field whose subtree
contains one
+ return !containsIdentifierField(field);
Review Comment:
Nit: Could we return false when deleting a list element or map value itself,
and add unit coverage? For example, `items.element` and `m.value` currently
return true, but `SchemaUpdate.apply()` rejects both. Spark 4.2 only generates
additions and type updates today, so this is a hook-contract edge case.
--
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]