Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#11962] feat(iceberg): support V3 nanosecond timestamp types (#11938)

Nevin Zheng7ba1da716619
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/FromIcebergType.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/FromIcebergType.java
index 12bc00ed36..1cf732e0e7 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/FromIcebergType.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/FromIcebergType.java
@@ -33,6 +33,7 @@ import org.apache.iceberg.types.Types;
public class FromIcebergType extends TypeUtil.SchemaVisitor<Type> {
public static final int PRECISION_MICROSECOND = 6;
+ public static final int PRECISION_NANOSECOND = 9;
public FromIcebergType() {}
@@ -107,6 +108,15 @@ public class FromIcebergType extends TypeUtil.SchemaVisitor<Type> {
return org.apache.gravitino.rel.types.Types.TimestampType.withoutTimeZone(
PRECISION_MICROSECOND);
}
+ case TIMESTAMP_NANO:
+ Types.TimestampNanoType tsNano = (Types.TimestampNanoType) primitive;
+ if (tsNano.shouldAdjustToUTC()) {
+ return org.apache.gravitino.rel.types.Types.TimestampType.withTimeZone(
+ PRECISION_NANOSECOND);
+ } else {
+ return org.apache.gravitino.rel.types.Types.TimestampType.withoutTimeZone(
+ PRECISION_NANOSECOND);
+ }
case STRING:
return org.apache.gravitino.rel.types.Types.StringType.get();
case UUID:
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergType.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergType.java
index 2b0ec5d4d0..7f671631eb 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergType.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergType.java
@@ -125,32 +125,44 @@ public class ToIcebergType extends ToIcebergTypeVisitor<Type> {
(org.apache.gravitino.rel.types.Types.TimeType) primitive;
// Iceberg only supports microsecond precision (6) for time type up to version 1.9.x
// See: https://iceberg.apache.org/docs/1.9.1/schemas/
- if (!timeType.hasPrecisionSet() || timeType.precision() == 6) {
+ if (!timeType.hasPrecisionSet()
+ || timeType.precision() == FromIcebergType.PRECISION_MICROSECOND) {
return Types.TimeType.get();
} else {
throw new IllegalArgumentException(
- "Iceberg only supports microsecond precision (6) for time type, but got precision: "
- + timeType.precision());
+ String.format(
+ "Cannot convert time(%d) to an Iceberg type. Expected precision %d (microsecond).",
+ timeType.precision(), FromIcebergType.PRECISION_MICROSECOND));
}
} else if (primitive instanceof org.apache.gravitino.rel.types.Types.TimestampType) {
org.apache.gravitino.rel.types.Types.TimestampType timestampType =
(org.apache.gravitino.rel.types.Types.TimestampType) primitive;
- // Iceberg only supports microsecond precision (6) for timestamp/timestamptz types up to
- // version 1.9.x
- // See: https://iceberg.apache.org/docs/1.9.1/schemas/
- if (!timestampType.hasPrecisionSet() || timestampType.precision() == 6) {
+ // Iceberg supports microsecond precision (6) for timestamp/timestamptz and, since format
+ // version 3, nanosecond precision (9) for timestamp_ns/timestamptz_ns. No other precision
+ // is representable. See: https://iceberg.apache.org/spec/#primitive-types
+ if (!timestampType.hasPrecisionSet()
+ || timestampType.precision() == FromIcebergType.PRECISION_MICROSECOND) {
if (timestampType.hasTimeZone()) {
return Types.TimestampType.withZone();
} else {
return Types.TimestampType.withoutZone();
}
+ } else if (timestampType.precision() == FromIcebergType.PRECISION_NANOSECOND) {
+ if (timestampType.hasTimeZone()) {
+ return Types.TimestampNanoType.withZone();
+ } else {
+ return Types.TimestampNanoType.withoutZone();
+ }
} else {
String timestampTypeName = timestampType.hasTimeZone() ? "timestamptz" : "timestamp";
throw new IllegalArgumentException(
- "Iceberg only supports microsecond precision (6) for "
- + timestampTypeName
- + " type, but got precision: "
- + timestampType.precision());
+ String.format(
+ "Cannot convert %s(%d) to an Iceberg type."
+ + " Expected precision %d (microsecond) or %d (nanosecond).",
+ timestampTypeName,
+ timestampType.precision(),
+ FromIcebergType.PRECISION_MICROSECOND,
+ FromIcebergType.PRECISION_NANOSECOND));
}
} else if (primitive instanceof org.apache.gravitino.rel.types.Types.DecimalType) {
return Types.DecimalType.of(
diff --git a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/TestConvertUtil.java b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/TestConvertUtil.java
index 99beaa52ce..06e6b56886 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/TestConvertUtil.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/TestConvertUtil.java
@@ -45,6 +45,10 @@ import org.junit.jupiter.api.Test;
/** Test class for {@link ConvertUtil}. */
public class TestConvertUtil extends TestBaseConvert {
+ private static final int NANO_PRECISION = 9;
+ private static final int MICRO_PRECISION = 6;
+ private static final int UNSUPPORTED_PRECISION = 3;
+
@Test
public void testVariantType() {
Assertions.assertTrue(
@@ -511,6 +515,57 @@ public class TestConvertUtil extends TestBaseConvert {
.fields()[1].nullable());
}
+ @Test
+ public void testNanosecondTimestamp() {
+ // Iceberg -> Gravitino
+ org.apache.gravitino.rel.types.Type nanoWithoutZone =
+ CONVERTER.toGravitino(Types.TimestampNanoType.withoutZone());
+ Assertions.assertTrue(
+ nanoWithoutZone instanceof org.apache.gravitino.rel.types.Types.TimestampType);
+ Assertions.assertFalse(
+ ((org.apache.gravitino.rel.types.Types.TimestampType) nanoWithoutZone).hasTimeZone());
+ Assertions.assertEquals(
+ NANO_PRECISION,
+ ((org.apache.gravitino.rel.types.Types.TimestampType) nanoWithoutZone).precision());
+
+ org.apache.gravitino.rel.types.Type nanoWithZone =
+ CONVERTER.toGravitino(Types.TimestampNanoType.withZone());
+ Assertions.assertTrue(
+ nanoWithZone instanceof org.apache.gravitino.rel.types.Types.TimestampType);
+ Assertions.assertTrue(
+ ((org.apache.gravitino.rel.types.Types.TimestampType) nanoWithZone).hasTimeZone());
+ Assertions.assertEquals(
+ NANO_PRECISION,
+ ((org.apache.gravitino.rel.types.Types.TimestampType) nanoWithZone).precision());
+
+ // Gravitino -> Iceberg
+ Type icebergNanoWithoutZone =
+ CONVERTER.fromGravitino(
+ org.apache.gravitino.rel.types.Types.TimestampType.withoutTimeZone(NANO_PRECISION));
+ Assertions.assertTrue(icebergNanoWithoutZone instanceof Types.TimestampNanoType);
+ Assertions.assertFalse(((Types.TimestampNanoType) icebergNanoWithoutZone).shouldAdjustToUTC());
+
+ Type icebergNanoWithZone =
+ CONVERTER.fromGravitino(
+ org.apache.gravitino.rel.types.Types.TimestampType.withTimeZone(NANO_PRECISION));
+ Assertions.assertTrue(icebergNanoWithZone instanceof Types.TimestampNanoType);
+ Assertions.assertTrue(((Types.TimestampNanoType) icebergNanoWithZone).shouldAdjustToUTC());
+
+ // Regression: precision 6 still maps to the microsecond timestamp, not the nanosecond one.
+ Type icebergMicro =
+ CONVERTER.fromGravitino(
+ org.apache.gravitino.rel.types.Types.TimestampType.withTimeZone(MICRO_PRECISION));
+ Assertions.assertTrue(icebergMicro instanceof Types.TimestampType);
+
+ // Any precision other than 6 or 9 is not representable in Iceberg and must be rejected.
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ CONVERTER.fromGravitino(
+ org.apache.gravitino.rel.types.Types.TimestampType.withoutTimeZone(
+ UNSUPPORTED_PRECISION)));
+ }
+
@Test
public void testFromNestedField() {
String colName = RandomStringUtils.secure().nextAlphabetic(10);
diff --git a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/integration/test/CatalogIcebergBaseIT.java b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/integration/test/CatalogIcebergBaseIT.java
index 53badb768e..d733ed701d 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/integration/test/CatalogIcebergBaseIT.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/integration/test/CatalogIcebergBaseIT.java
@@ -120,6 +120,7 @@ public abstract class CatalogIcebergBaseIT extends BaseIT {
private static final String VIEW_COMMENT = "view comment";
private static final String SPARK_DIALECT = "spark";
private static final String TRINO_DIALECT = "trino";
+ private static final int NANO_PRECISION = 9;
private static final String provider = "lakehouse-iceberg";
private static final String SELECT_ALL_TEMPLATE = "SELECT * FROM iceberg.%s";
private static String INSERT_BATCH_WITHOUT_PARTITION_TEMPLATE =
@@ -646,9 +647,8 @@ public abstract class CatalogIcebergBaseIT extends BaseIT {
// like Spark 4 would, directly through the Iceberg catalog, then load through the native
// metadata interface.
//
- // variant loads as VariantType; geometry and geography load as their native types. The
- // remaining V3 net-new types are not modeled in Gravitino's unified type system yet and load
- // as ExternalType; native support for them is pending and tracked in apache/gravitino#11929.
+ // Variant, geometry, and geography load as their native Gravitino types. Nanosecond timestamps
+ // are asserted below; unknown remains an ExternalType until it gains unified-model support.
assertV3LoadsAsVariant("v3_variant");
assertV3LoadsAsGeometry("v3_geometry", org.apache.iceberg.types.Types.GeometryType.crs84());
assertV3LoadsAsGeometry(
@@ -659,15 +659,19 @@ public abstract class CatalogIcebergBaseIT extends BaseIT {
org.apache.iceberg.types.Types.GeographyType.of(
"EPSG:4326", org.apache.iceberg.types.EdgeAlgorithm.KARNEY));
- // TODO(apache/gravitino#11929): expect native types once these gain unified-model support.
- assertV3LoadsAsExternal(
+ // Native V3 support (apache/gravitino#11936): a raw-Iceberg (IRC-side) nanosecond column loads
+ // back through the native metadata API as timestamp(9), not ExternalType. Write-path mirror:
+ // testNanosecondTimestampTypeConversion.
+ assertV3LoadsAsTimestamp(
"v3_timestamp_ns",
org.apache.iceberg.types.Types.TimestampNanoType.withoutZone(),
- "TIMESTAMP_NANO");
- assertV3LoadsAsExternal(
+ Types.TimestampType.withoutTimeZone(NANO_PRECISION));
+ assertV3LoadsAsTimestamp(
"v3_timestamptz_ns",
org.apache.iceberg.types.Types.TimestampNanoType.withZone(),
- "TIMESTAMP_NANO");
+ Types.TimestampType.withTimeZone(NANO_PRECISION));
+
+ // TODO(apache/gravitino#11929): expect native types once these gain unified-model support.
assertV3LoadsAsExternal(
"v3_unknown", org.apache.iceberg.types.Types.UnknownType.get(), "UNKNOWN");
}
@@ -939,6 +943,73 @@ public abstract class CatalogIcebergBaseIT extends BaseIT {
expectedCatalogString, ((Types.ExternalType) loaded.dataType()).catalogString());
}
+ private void assertV3LoadsAsTimestamp(
+ String tableName,
+ org.apache.iceberg.types.Type icebergType,
+ org.apache.gravitino.rel.types.Type expectedGravitinoType) {
+ NameIdentifier ident = createV3Table(tableName, icebergType);
+ Column loaded = catalog.asTableCatalog().loadTable(ident).columns()[0];
+ Assertions.assertEquals(expectedGravitinoType, loaded.dataType());
+ }
+
+ @Test
+ void testNanosecondTimestampTypeConversion() {
+ // Nanosecond columns need an Iceberg backend that can persist them. The REST path can; the
+ // Hive backend cannot yet -- Iceberg's HiveSchemaUtil has no nanosecond arm
+ // (apache/iceberg#11937) -- so this runs on REST only.
+ Assumptions.assumeTrue(
+ "rest".equalsIgnoreCase(TYPE),
+ "Nanosecond timestamps require an Iceberg backend that can persist them");
+
+ Column col1 =
+ Column.of(
+ "iceberg_column_1", Types.TimestampType.withTimeZone(NANO_PRECISION), "col_1_comment");
+ Column col2 =
+ Column.of(
+ "iceberg_column_2",
+ Types.TimestampType.withoutTimeZone(NANO_PRECISION),
+ "col_2_comment");
+
+ Column[] columns = new Column[] {col1, col2};
+
+ String timestampTableName = "timestamp_ns_table";
+
+ NameIdentifier tableIdentifier = NameIdentifier.of(schemaName, timestampTableName);
+
+ Map<String, String> properties = createProperties();
+ properties.put(IcebergTablePropertiesMetadata.FORMAT_VERSION, "3");
+ TableCatalog tableCatalog = catalog.asTableCatalog();
+ Table createdTable =
+ tableCatalog.createTable(tableIdentifier, columns, table_comment, properties);
+ Assertions.assertEquals(
+ Types.TimestampType.withTimeZone(NANO_PRECISION), createdTable.columns()[0].dataType());
+ Assertions.assertEquals(
+ Types.TimestampType.withoutTimeZone(NANO_PRECISION), createdTable.columns()[1].dataType());
+
+ Table loadTable = tableCatalog.loadTable(tableIdentifier);
+ Assertions.assertEquals("iceberg_column_1", loadTable.columns()[0].name());
+ Assertions.assertEquals(
+ Types.TimestampType.withTimeZone(NANO_PRECISION), loadTable.columns()[0].dataType());
+ Assertions.assertEquals("col_1_comment", loadTable.columns()[0].comment());
+
+ Assertions.assertEquals("iceberg_column_2", loadTable.columns()[1].name());
+ Assertions.assertEquals(
+ Types.TimestampType.withoutTimeZone(NANO_PRECISION), loadTable.columns()[1].dataType());
+ Assertions.assertEquals("col_2_comment", loadTable.columns()[1].comment());
+
+ // Confirm the write reached Iceberg as the nanosecond types, not just echoed by Gravitino.
+ org.apache.iceberg.Table table =
+ icebergCatalog.loadTable(
+ IcebergCatalogWrapperHelper.buildIcebergTableIdentifier(tableIdentifier));
+ org.apache.iceberg.Schema icebergSchema = table.schema();
+ Assertions.assertEquals(
+ org.apache.iceberg.types.Types.TimestampNanoType.withZone(),
+ icebergSchema.columns().get(0).type());
+ Assertions.assertEquals(
+ org.apache.iceberg.types.Types.TimestampNanoType.withoutZone(),
+ icebergSchema.columns().get(1).type());
+ }
+
@Test
void testListAndDropIcebergTable() {
Column[] columns = createColumns();
@@ -1783,20 +1854,16 @@ public abstract class CatalogIcebergBaseIT extends BaseIT {
Column.of("time_col_9", Types.TimeType.of(9))
};
- // Test unsupported timestamptz precision (with timezone)
Column[] unsupportedTimestamptzColumns = {
Column.of("timestamptz_col_0", Types.TimestampType.withTimeZone(0)),
Column.of("timestamptz_col_1", Types.TimestampType.withTimeZone(1)),
- Column.of("timestamptz_col_3", Types.TimestampType.withTimeZone(3)),
- Column.of("timestamptz_col_9", Types.TimestampType.withTimeZone(9))
+ Column.of("timestamptz_col_3", Types.TimestampType.withTimeZone(3))
};
- // Test unsupported timestamp precision (without timezone)
Column[] unsupportedTimestampColumns = {
Column.of("timestamp_col_0", Types.TimestampType.withoutTimeZone(0)),
Column.of("timestamp_col_1", Types.TimestampType.withoutTimeZone(1)),
- Column.of("timestamp_col_3", Types.TimestampType.withoutTimeZone(3)),
- Column.of("timestamp_col_9", Types.TimestampType.withoutTimeZone(9))
+ Column.of("timestamp_col_3", Types.TimestampType.withoutTimeZone(3))
};
TableCatalog tableCatalog = catalog.asTableCatalog();
@@ -1816,10 +1883,7 @@ public abstract class CatalogIcebergBaseIT extends BaseIT {
Transforms.EMPTY_TRANSFORM,
Distributions.NONE,
new SortOrder[0]));
- Assertions.assertTrue(
- exception
- .getMessage()
- .contains("Iceberg only supports microsecond precision (6) for time type"));
+ Assertions.assertTrue(exception.getMessage().contains("Cannot convert time("));
}
// Test timestamptz precision validation (with timezone)
@@ -1837,10 +1901,7 @@ public abstract class CatalogIcebergBaseIT extends BaseIT {
Transforms.EMPTY_TRANSFORM,
Distributions.NONE,
new SortOrder[0]));
- Assertions.assertTrue(
- exception
- .getMessage()
- .contains("Iceberg only supports microsecond precision (6) for timestamptz type"));
+ Assertions.assertTrue(exception.getMessage().contains("Cannot convert timestamptz("));
}
// Test timestamp precision validation (without timezone)
@@ -1858,10 +1919,7 @@ public abstract class CatalogIcebergBaseIT extends BaseIT {
Transforms.EMPTY_TRANSFORM,
Distributions.NONE,
new SortOrder[0]));
- Assertions.assertTrue(
- exception
- .getMessage()
- .contains("Iceberg only supports microsecond precision (6) for timestamp type"));
+ Assertions.assertTrue(exception.getMessage().contains("Cannot convert timestamp("));
}
}
diff --git a/clients/client-python/tests/unittests/json_serdes/test_type_serdes.py b/clients/client-python/tests/unittests/json_serdes/test_type_serdes.py
index 4be159e04f..898c5eada8 100644
--- a/clients/client-python/tests/unittests/json_serdes/test_type_serdes.py
+++ b/clients/client-python/tests/unittests/json_serdes/test_type_serdes.py
@@ -63,8 +63,10 @@ class TestTypeSerdes(unittest.TestCase):
"time(3)": Types.TimeType.of(3),
"timestamp(6)": Types.TimestampType.without_time_zone(6),
"timestamp(0)": Types.TimestampType.without_time_zone(0),
+ "timestamp(9)": Types.TimestampType.without_time_zone(9),
"timestamp_tz(6)": Types.TimestampType.with_time_zone(6),
"timestamp_tz(3)": Types.TimestampType.with_time_zone(3),
+ "timestamp_tz(9)": Types.TimestampType.with_time_zone(9),
"geometry(srid:3857)": Types.GeometryType.of("srid:3857"),
"geography(EPSG:4326,karney)": Types.GeographyType.of(
"EPSG:4326", "karney"
diff --git a/docs/lakehouse-iceberg-catalog.md b/docs/lakehouse-iceberg-catalog.md
index b71fb65784..3be17a3104 100644
--- a/docs/lakehouse-iceberg-catalog.md
+++ b/docs/lakehouse-iceberg-catalog.md
@@ -428,8 +428,10 @@ If you doesn't specify distribution expressions, the table distribution will be
| `String` | `String` |
| `Date` | `Date` |
| `Time(6)` | `Time` |
-| `Timestamp(6)` | `TimestampType withZone` |
-| `Timestamp_tz(6)` | `TimestampType withoutZone` |
+| `Timestamp(6)` | `TimestampType withoutZone` |
+| `Timestamp_tz(6)` | `TimestampType withZone` |
+| `Timestamp(9)` | `TimestampNanoType withoutZone` |
+| `Timestamp_tz(9)` | `TimestampNanoType withZone` |
| `Decimal` | `Decimal` |
| `Fixed` | `Fixed` |
| `Binary` | `Binary` |