jerryshao commented on code in PR #14659:
URL: https://github.com/apache/gravitino/pull/14659#discussion_r4512933432


##########
catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/ConvertUtil.java:
##########
@@ -76,9 +93,113 @@ public static IcebergColumn 
fromNestedField(Types.NestedField nestedField) {
         .withNullable(nestedField.isOptional())
         .withComment(nestedField.doc())
         
.withType(IcebergDataTypeConverter.CONVERTER.toGravitino(nestedField.type()))
+        .withDefaultValue(
+            fromIcebergLiteral(
+                nestedField.writeDefaultLiteral(),
+                nestedField.type(),
+                Column.DEFAULT_VALUE_NOT_SET))
+        .withInitialDefaultValue(
+            fromIcebergLiteral(
+                nestedField.initialDefaultLiteral(),
+                nestedField.type(),
+                Column.INITIAL_DEFAULT_VALUE_NOT_SET))
         .build();
   }
 
+  private static Expression fromIcebergLiteral(
+      Literal<?> icebergDefault, Type type, Expression unset) {
+    if (icebergDefault == null) {
+      return unset;
+    }
+    return toGravitinoLiteral(icebergDefault, type, unset);
+  }
+
+  private static Expression toGravitinoLiteral(
+      Literal<?> icebergDefault, Type type, Expression unset) {
+    Object value = icebergDefault.value();
+    if (value == null) {
+      return Literals.NULL;
+    }
+
+    switch (type.typeId()) {
+      case BOOLEAN:
+        return Literals.booleanLiteral((Boolean) value);
+      case INTEGER:
+        return Literals.integerLiteral((Integer) value);
+      case LONG:
+        return Literals.longLiteral((Long) value);
+      case FLOAT:
+        return Literals.floatLiteral((Float) value);
+      case DOUBLE:
+        return Literals.doubleLiteral((Double) value);
+      case STRING:
+        return Literals.stringLiteral(value.toString());
+      case DATE:
+        return Literals.dateLiteral(DateTimeUtil.dateFromDays((Integer) 
value));
+      case TIME:
+        return Literals.timeLiteral(DateTimeUtil.timeFromMicros((Long) value));
+      case TIMESTAMP:
+        {
+          Types.TimestampType timestampType = (Types.TimestampType) type;
+          if (timestampType.shouldAdjustToUTC()) {
+            // Iceberg timestamptz is a UTC instant (micros). Expose it as UTC 
LocalDateTime while
+            // keeping TimestampType.withTimeZone so the type still says "with 
time zone".
+            return Literals.of(
+                DateTimeUtil.timestamptzFromMicros((Long) 
value).toLocalDateTime(),
+                
org.apache.gravitino.rel.types.Types.TimestampType.withTimeZone(
+                    FromIcebergType.PRECISION_MICROSECOND));
+          }
+          return Literals.of(
+              DateTimeUtil.timestampFromMicros((Long) value),
+              
org.apache.gravitino.rel.types.Types.TimestampType.withoutTimeZone(
+                  FromIcebergType.PRECISION_MICROSECOND));
+        }
+      case TIMESTAMP_NANO:
+        {
+          Types.TimestampNanoType timestampNanoType = 
(Types.TimestampNanoType) type;
+          if (timestampNanoType.shouldAdjustToUTC()) {
+            // Same UTC-instant → LocalDateTime convention as TIMESTAMP with 
zone above.
+            return Literals.of(
+                DateTimeUtil.timestamptzFromNanos((Long) 
value).toLocalDateTime(),
+                
org.apache.gravitino.rel.types.Types.TimestampType.withTimeZone(
+                    FromIcebergType.PRECISION_NANOSECOND));
+          }
+          return Literals.of(
+              DateTimeUtil.timestampFromNanos((Long) value),
+              
org.apache.gravitino.rel.types.Types.TimestampType.withoutTimeZone(
+                  FromIcebergType.PRECISION_NANOSECOND));
+        }
+      case DECIMAL:
+        {
+          Types.DecimalType decimalType = (Types.DecimalType) type;
+          return Literals.decimalLiteral(
+              Decimal.of((BigDecimal) value, decimalType.precision(), 
decimalType.scale()));
+        }
+      case UUID:
+        return Literals.of(
+            ((UUID) value).toString(), 
org.apache.gravitino.rel.types.Types.UUIDType.get());
+      case FIXED:
+        return Literals.of(
+            toByteArray(value),
+            
org.apache.gravitino.rel.types.Types.FixedType.of(((Types.FixedType) 
type).length()));
+      case BINARY:
+        return Literals.of(
+            toByteArray(value), 
org.apache.gravitino.rel.types.Types.BinaryType.get());
+      default:
+        // Nested types cannot carry non-null Iceberg defaults; other V3 types 
are left unset
+        // rather than failing table load.
+        LOG.debug("Dropping Iceberg column default of unsupported type {} as 
unset", type.typeId());

Review Comment:
   Unsupported default types are silently dropped at debug level, so a user 
sees no default and no hint why. Log at warn, or once per table, so the loss is 
visible.



##########
common/src/main/java/org/apache/gravitino/dto/util/DTOConverters.java:
##########
@@ -1142,11 +1154,13 @@ public static Expression[] 
fromFunctionArgs(FunctionArg[] args) {
   public static Expression fromFunctionArg(FunctionArg arg) {
     switch (arg.argType()) {
       case LITERAL:
-        if (((LiteralDTO) arg).value() == null
-            || ((LiteralDTO) arg).dataType().equals(Types.NullType.get())) {
+        LiteralDTO literalDTO = (LiteralDTO) arg;
+        if (literalDTO.value() == null || 
literalDTO.dataType().equals(Types.NullType.get())) {
           return Literals.NULL;
         }
-        return Literals.of(((LiteralDTO) arg).value(), ((LiteralDTO) 
arg).dataType());
+        Type dataType = literalDTO.dataType();
+        Object value = literalValueFromDtoString(literalDTO.value(), dataType);

Review Comment:
   fromFunctionArg now decodes any 0x-prefixed value for a BinaryType literal 
into byte[], and throws on odd-length or non-hex input. Could this break 
existing JDBC defaults that were stored as plain strings starting with 0x? 
Please confirm with a test, or fall back to the raw string when decoding fails.



##########
catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/ConvertUtil.java:
##########
@@ -76,9 +93,113 @@ public static IcebergColumn 
fromNestedField(Types.NestedField nestedField) {
         .withNullable(nestedField.isOptional())
         .withComment(nestedField.doc())
         
.withType(IcebergDataTypeConverter.CONVERTER.toGravitino(nestedField.type()))
+        .withDefaultValue(
+            fromIcebergLiteral(
+                nestedField.writeDefaultLiteral(),
+                nestedField.type(),
+                Column.DEFAULT_VALUE_NOT_SET))
+        .withInitialDefaultValue(
+            fromIcebergLiteral(
+                nestedField.initialDefaultLiteral(),
+                nestedField.type(),
+                Column.INITIAL_DEFAULT_VALUE_NOT_SET))
         .build();
   }
 
+  private static Expression fromIcebergLiteral(
+      Literal<?> icebergDefault, Type type, Expression unset) {
+    if (icebergDefault == null) {
+      return unset;
+    }
+    return toGravitinoLiteral(icebergDefault, type, unset);
+  }
+
+  private static Expression toGravitinoLiteral(
+      Literal<?> icebergDefault, Type type, Expression unset) {
+    Object value = icebergDefault.value();
+    if (value == null) {
+      return Literals.NULL;

Review Comment:
   Iceberg literals never hold a null value, so this Literals.NULL branch looks 
unreachable, and it conflicts with the commit that treats a null initial 
default as unset. Remove it, or return the unset value here.



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

Reply via email to