Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1511,22 +1511,15 @@ private void compareDatasetWithUUID(List<String> dataset1Rows, List<String> data
}

private static String[] getSelectColumnsArr(List<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ public void testAvroLogicalTypes() {
.schema(
InternalSchema.builder()
.name("long")
.dataType(InternalType.LONG)
.dataType(InternalType.TIMESTAMP_NTZ)
.isNullable(false)
.metadata(millisMetadata)
.build())
Expand All @@ -586,7 +586,7 @@ public void testAvroLogicalTypes() {
.schema(
InternalSchema.builder()
.name("long")
.dataType(InternalType.LONG)
.dataType(InternalType.TIMESTAMP_NTZ)
.isNullable(false)
.metadata(microsMetadata)
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -608,17 +608,9 @@ private static String[] getSelectColumnsArr(List<String> 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;
Expand Down