diff --git a/xtable-core/src/main/java/org/apache/xtable/avro/AvroSchemaConverter.java b/xtable-core/src/main/java/org/apache/xtable/avro/AvroSchemaConverter.java index 67db092e2..5162102bd 100644 --- a/xtable-core/src/main/java/org/apache/xtable/avro/AvroSchemaConverter.java +++ b/xtable-core/src/main/java/org/apache/xtable/avro/AvroSchemaConverter.java @@ -160,15 +160,13 @@ private InternalSchema toInternalSchema( metadata.put( InternalSchema.MetadataKey.TIMESTAMP_PRECISION, InternalSchema.MetadataValue.MICROS); } else if (logicalType instanceof LogicalTypes.LocalTimestampMillis) { - // TODO: https://github.com/apache/incubator-xtable/issues/672 - // Hudi 0.x writes INT64 in parquet, TimestampNTZType support added in 1.x - newDataType = InternalType.LONG; + // Hudi 1.x writes these as Parquet TIMESTAMP(isAdjustedToUTC=false), so readers that + // follow the file's annotation, like Iceberg's, need a timestamp type, not a long. + newDataType = InternalType.TIMESTAMP_NTZ; metadata.put( InternalSchema.MetadataKey.TIMESTAMP_PRECISION, InternalSchema.MetadataValue.MILLIS); } else if (logicalType instanceof LogicalTypes.LocalTimestampMicros) { - // TODO: https://github.com/apache/incubator-xtable/issues/672 - // Hudi 0.x writes INT64 in parquet, TimestampNTZType support added in 1.x - newDataType = InternalType.LONG; + newDataType = InternalType.TIMESTAMP_NTZ; metadata.put( InternalSchema.MetadataKey.TIMESTAMP_PRECISION, InternalSchema.MetadataValue.MICROS); } else { diff --git a/xtable-core/src/test/java/org/apache/xtable/ITConversionController.java b/xtable-core/src/test/java/org/apache/xtable/ITConversionController.java index 5462da73d..3a224fdd5 100644 --- a/xtable-core/src/test/java/org/apache/xtable/ITConversionController.java +++ b/xtable-core/src/test/java/org/apache/xtable/ITConversionController.java @@ -1511,22 +1511,15 @@ private void compareDatasetWithUUID(List dataset1Rows, List data } private static String[] getSelectColumnsArr(List columnsToSelect, String format) { - boolean isHudi = format.equals(HUDI); - boolean isIceberg = format.equals(ICEBERG); + boolean localTimestampsAsTimestamps = + format.equals(HUDI) || format.equals(ICEBERG) || format.equals(DELTA); return columnsToSelect.stream() .map( colName -> { - if (colName.startsWith("timestamp_local_millis")) { - if (isHudi) { - return String.format( - "unix_millis(CAST(%s AS TIMESTAMP)) AS %s", colName, colName); - } else if (isIceberg) { - // iceberg is showing up as micros, so we need to divide by 1000 to get millis - return String.format("%s div 1000 AS %s", colName, colName); - } else { - return colName; - } - } else if (isHudi && colName.startsWith("timestamp_local_micros")) { + if (localTimestampsAsTimestamps && colName.startsWith("timestamp_local_millis")) { + return String.format("unix_millis(CAST(%s AS TIMESTAMP)) AS %s", colName, colName); + } else if (localTimestampsAsTimestamps + && colName.startsWith("timestamp_local_micros")) { return String.format("unix_micros(CAST(%s AS TIMESTAMP)) AS %s", colName, colName); } else { return colName; diff --git a/xtable-core/src/test/java/org/apache/xtable/avro/TestAvroSchemaConverter.java b/xtable-core/src/test/java/org/apache/xtable/avro/TestAvroSchemaConverter.java index 4e2991989..0b6823a1f 100644 --- a/xtable-core/src/test/java/org/apache/xtable/avro/TestAvroSchemaConverter.java +++ b/xtable-core/src/test/java/org/apache/xtable/avro/TestAvroSchemaConverter.java @@ -576,7 +576,7 @@ public void testAvroLogicalTypes() { .schema( InternalSchema.builder() .name("long") - .dataType(InternalType.LONG) + .dataType(InternalType.TIMESTAMP_NTZ) .isNullable(false) .metadata(millisMetadata) .build()) @@ -586,7 +586,7 @@ public void testAvroLogicalTypes() { .schema( InternalSchema.builder() .name("long") - .dataType(InternalType.LONG) + .dataType(InternalType.TIMESTAMP_NTZ) .isNullable(false) .metadata(microsMetadata) .build()) diff --git a/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiSchemaExtractor.java b/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiSchemaExtractor.java index 9140a431e..e814e5688 100644 --- a/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiSchemaExtractor.java +++ b/xtable-core/src/test/java/org/apache/xtable/hudi/TestHudiSchemaExtractor.java @@ -23,12 +23,18 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import org.apache.avro.LogicalTypes; import org.apache.avro.Schema; import org.apache.avro.SchemaBuilder; import org.junit.jupiter.api.Test; +import org.apache.iceberg.types.Types; + import org.apache.xtable.avro.AvroSchemaConverter; +import org.apache.xtable.iceberg.IcebergSchemaExtractor; +import org.apache.xtable.model.schema.InternalField; import org.apache.xtable.model.schema.InternalSchema; +import org.apache.xtable.model.schema.InternalType; public class TestHudiSchemaExtractor { @@ -42,4 +48,40 @@ public void testHoodieTableSchema() { assertEquals(mockOutput, canonicalSchema); verify(mockConverter).toInternalSchema(schema); } + + @Test + public void testLocalTimestampsBecomeIcebergTimestampsWithoutZone() { + Schema micros = + LogicalTypes.localTimestampMicros().addToSchema(Schema.create(Schema.Type.LONG)); + Schema millis = + LogicalTypes.localTimestampMillis().addToSchema(Schema.create(Schema.Type.LONG)); + Schema hudiSchema = + SchemaBuilder.record("ntz") + .fields() + .name("ntz_micros") + .type() + .optional() + .type(micros) + .name("ntz_millis") + .type(millis) + .noDefault() + .endRecord(); + + InternalSchema internalSchema = new HudiSchemaExtractor().schema(hudiSchema); + InternalField microsField = internalSchema.getFields().get(0); + InternalField millisField = internalSchema.getFields().get(1); + assertEquals(InternalType.TIMESTAMP_NTZ, microsField.getSchema().getDataType()); + assertEquals( + InternalSchema.MetadataValue.MICROS, + microsField.getSchema().getMetadata().get(InternalSchema.MetadataKey.TIMESTAMP_PRECISION)); + assertEquals(InternalType.TIMESTAMP_NTZ, millisField.getSchema().getDataType()); + assertEquals( + InternalSchema.MetadataValue.MILLIS, + millisField.getSchema().getMetadata().get(InternalSchema.MetadataKey.TIMESTAMP_PRECISION)); + + org.apache.iceberg.Schema icebergSchema = + IcebergSchemaExtractor.getInstance().toIceberg(internalSchema); + assertEquals(Types.TimestampType.withoutZone(), icebergSchema.findType("ntz_micros")); + assertEquals(Types.TimestampType.withoutZone(), icebergSchema.findType("ntz_millis")); + } } diff --git a/xtable-hudi-support/xtable-iceberg-pluggable-tf/src/test/java/org/apache/xtable/ITIcebergTableFormat.java b/xtable-hudi-support/xtable-iceberg-pluggable-tf/src/test/java/org/apache/xtable/ITIcebergTableFormat.java index e8131e165..1388b2eae 100644 --- a/xtable-hudi-support/xtable-iceberg-pluggable-tf/src/test/java/org/apache/xtable/ITIcebergTableFormat.java +++ b/xtable-hudi-support/xtable-iceberg-pluggable-tf/src/test/java/org/apache/xtable/ITIcebergTableFormat.java @@ -608,17 +608,9 @@ private static String[] getSelectColumnsArr(List columnsToSelect, String return columnsToSelect.stream() .map( colName -> { - if (colName.startsWith("timestamp_local_millis")) { - if (isHudi) { - return String.format( - "unix_millis(CAST(%s AS TIMESTAMP)) AS %s", colName, colName); - } else if (isIceberg) { - // iceberg is showing up as micros, so we need to divide by 1000 to get millis - return String.format("%s div 1000 AS %s", colName, colName); - } else { - return colName; - } - } else if (isHudi && colName.startsWith("timestamp_local_micros")) { + if ((isHudi || isIceberg) && colName.startsWith("timestamp_local_millis")) { + return String.format("unix_millis(CAST(%s AS TIMESTAMP)) AS %s", colName, colName); + } else if ((isHudi || isIceberg) && colName.startsWith("timestamp_local_micros")) { return String.format("unix_micros(CAST(%s AS TIMESTAMP)) AS %s", colName, colName); } else { return colName;