Skip to content

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

[#15257] fix(iceberg): Return 400 for unrecognized column types (#15258)

roryqiec348efae149
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergTypeVisitor.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergTypeVisitor.java
index 28a6849fc9..325ca21857 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergTypeVisitor.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergTypeVisitor.java
@@ -38,27 +38,10 @@ public class ToIcebergTypeVisitor<T> {
* @param visitor Visitor of Iceberg type
* @param <T> Iceberg type
* @return Iceberg type
+ * @throws IllegalArgumentException If the type contains an unrecognized type.
*/
public static <T> T visit(Type type, ToIcebergTypeVisitor<T> visitor) {
- if (type instanceof Types.MapType) {
- Types.MapType map = (Types.MapType) type;
- return visitor.map(map, visit(map.keyType(), visitor), visit(map.valueType(), visitor));
- } else if (type instanceof Types.ListType) {
- Types.ListType list = (Types.ListType) type;
- return visitor.array(list, visit(list.elementType(), visitor));
- } else if (type instanceof Types.StructType) {
- Types.StructType.Field[] fields = ((Types.StructType) type).fields();
- List<T> fieldResults = Lists.newArrayListWithExpectedSize(fields.length);
- for (Types.StructType.Field field : fields) {
- fieldResults.add(visitor.field(field, visit(field.type(), visitor)));
- }
- return visitor.struct((Types.StructType) type, fieldResults);
- } else if (type instanceof Types.NullType) {
- // NullType implements Type directly rather than PrimitiveType, so it needs its own dispatch.
- return visitor.nullType((Types.NullType) type);
- } else {
- return visitor.atomic((Type.PrimitiveType) type);
- }
+ return visit(type, visitor, "");
}
public T struct(IcebergTable struct, List<T> fieldResults) {
@@ -88,4 +71,38 @@ public class ToIcebergTypeVisitor<T> {
public T nullType(Types.NullType nullType) {
throw new UnsupportedOperationException();
}
+
+ private static <T> T visit(Type type, ToIcebergTypeVisitor<T> visitor, String columnPath) {
+ if (type instanceof Types.MapType) {
+ Types.MapType map = (Types.MapType) type;
+ return visitor.map(
+ map,
+ visit(map.keyType(), visitor, columnPath),
+ visit(map.valueType(), visitor, columnPath));
+ } else if (type instanceof Types.ListType) {
+ Types.ListType list = (Types.ListType) type;
+ return visitor.array(list, visit(list.elementType(), visitor, columnPath));
+ } else if (type instanceof Types.StructType) {
+ Types.StructType.Field[] fields = ((Types.StructType) type).fields();
+ List<T> fieldResults = Lists.newArrayListWithExpectedSize(fields.length);
+ for (Types.StructType.Field field : fields) {
+ String fieldPath = columnPath.isEmpty() ? field.name() : columnPath + "." + field.name();
+ fieldResults.add(visitor.field(field, visit(field.type(), visitor, fieldPath)));
+ }
+ return visitor.struct((Types.StructType) type, fieldResults);
+ } else if (type instanceof Types.NullType) {
+ // NullType implements Type directly rather than PrimitiveType, so it needs its own dispatch.
+ return visitor.nullType((Types.NullType) type);
+ } else if (type instanceof Types.UnparsedType) {
+ String column = columnPath.isEmpty() ? "" : " for column '" + columnPath + "'";
+ throw new IllegalArgumentException(
+ "Unsupported type '"
+ + ((Types.UnparsedType) type).unparsedType()
+ + "'"
+ + column
+ + " in an Iceberg table");
+ } else {
+ return visitor.atomic((Type.PrimitiveType) type);
+ }
+ }
}
diff --git a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergTableValidation.java b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergTableValidation.java
index 509bd8e97d..0d6bd5123a 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergTableValidation.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergTableValidation.java
@@ -89,6 +89,80 @@ class TestIcebergTableValidation {
assertFalse(operations.tableExists(table("duplicates")));
}
+ @ParameterizedTest
+ @MethodSource("unparsedTypes")
+ void testUnparsedTypeOnCreateIsBadRequest(Type type) {
+ IllegalArgumentException error =
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> create("unparsed_type", new Column[] {Column.of("id", type)}));
+ assertTrue(error.getMessage().contains("bigintz"));
+ assertTrue(error.getMessage().contains("column 'id"));
+ assertEquals(400, tableError(OperationType.CREATE, error));
+ assertFalse(operations.tableExists(table("unparsed_type")));
+ }
+
+ @Test
+ void testUnparsedNestedTypeReportsFullColumnPath() {
+ Type type =
+ Types.StructType.of(
+ Types.StructType.Field.nullableField(
+ "items",
+ Types.ListType.nullable(
+ Types.MapType.valueNullable(
+ Types.StringType.get(),
+ Types.StructType.of(
+ Types.StructType.Field.nullableField(
+ "value", Types.UnparsedType.of("bigintz")))))));
+ IllegalArgumentException error =
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> create("unparsed_nested", new Column[] {Column.of("payload", type)}));
+ assertEquals(
+ "Unsupported type 'bigintz' for column 'payload.items.value' in an Iceberg table",
+ error.getMessage());
+ assertEquals(400, tableError(OperationType.CREATE, error));
+ assertFalse(operations.tableExists(table("unparsed_nested")));
+ }
+
+ @ParameterizedTest
+ @MethodSource("unparsedTypes")
+ void testUnparsedTypeOnAddLeavesSchemaUnchanged(Type type) {
+ createOrdinaryTable("unparsed_add");
+ for (boolean nullable : new boolean[] {true, false}) {
+ IllegalArgumentException error =
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ operations.alterTable(
+ table("unparsed_add"),
+ TableChange.addColumn(new String[] {"valid"}, Types.StringType.get()),
+ TableChange.addColumn(new String[] {"bad"}, type, nullable)));
+ assertTrue(error.getMessage().contains("bigintz"));
+ assertEquals(400, tableError(OperationType.ALTER, error));
+ assertOrdinaryColumns("unparsed_add");
+ }
+ }
+
+ @Test
+ void testUnparsedTypeOnUpdateLeavesSchemaUnchanged() {
+ createOrdinaryTable("unparsed_update");
+ IllegalArgumentException error =
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ operations.alterTable(
+ table("unparsed_update"),
+ TableChange.updateColumnType(
+ new String[] {"id"}, Types.UnparsedType.of("bigintz"))));
+ assertTrue(error.getMessage().contains("bigintz"));
+ assertEquals(400, tableError(OperationType.ALTER, error));
+ assertOrdinaryColumns("unparsed_update");
+ assertEquals(
+ Types.LongType.get(),
+ operations.loadTable(table("unparsed_update")).columns()[0].dataType());
+ }
+
@Test
void testRenameColumnConflictLeavesSchemaUnchanged() {
createOrdinaryTable("rename_column");
@@ -196,6 +270,18 @@ class TestIcebergTableValidation {
assertEquals(type, operations.loadTable(table("version_three")).columns()[0].dataType());
}
+ private static Stream<Type> unparsedTypes() {
+ Type unparsed = Types.UnparsedType.of("bigintz");
+ return Stream.of(
+ unparsed,
+ Types.ListType.nullable(unparsed),
+ Types.MapType.valueNullable(unparsed, Types.StringType.get()),
+ Types.MapType.valueNullable(Types.StringType.get(), unparsed),
+ Types.StructType.of(Types.StructType.Field.nullableField("nested", unparsed)),
+ Types.ListType.nullable(
+ Types.StructType.of(Types.StructType.Field.nullableField("nested", unparsed))));
+ }
+
private static Stream<Type> versionThreeTypes() {
return Stream.of(
Types.NullType.get(),