Skip to content

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

[#742] feat(all): move nullable attribute from type to column (#765)

mchades4b01d3c5db2c
diff --git a/api/src/main/java/com/datastrato/gravitino/rel/Column.java b/api/src/main/java/com/datastrato/gravitino/rel/Column.java
index d40d5c6774..bbc1342987 100644
--- a/api/src/main/java/com/datastrato/gravitino/rel/Column.java
+++ b/api/src/main/java/com/datastrato/gravitino/rel/Column.java
@@ -27,5 +27,8 @@ public interface Column {
/** Returns the comment of this column, null if not specified. */
String comment();
+ /** Returns true if this column may produce null values. Default is true. */
+ boolean nullable();
+
// TODO. Support column default value. @Jerry
}
diff --git a/api/src/test/java/com/datastrato/gravitino/rel/TestTransforms.java b/api/src/test/java/com/datastrato/gravitino/rel/TestTransforms.java
index c5cb00f590..7110da9992 100644
--- a/api/src/test/java/com/datastrato/gravitino/rel/TestTransforms.java
+++ b/api/src/test/java/com/datastrato/gravitino/rel/TestTransforms.java
@@ -45,6 +45,11 @@ public class TestTransforms {
public String comment() {
return null;
}
+
+ @Override
+ public boolean nullable() {
+ return true;
+ }
};
String[] fieldName = new String[] {column.name()};
@@ -87,6 +92,11 @@ public class TestTransforms {
public String comment() {
return null;
}
+
+ @Override
+ public boolean nullable() {
+ return true;
+ }
};
// partition by foo(col_1, 'bar')
NamedReference.FieldReference arg1 = field(column.name());
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
index 0120828512..b62ece4fcb 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
@@ -40,7 +40,6 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
-import io.substrait.type.Type;
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
@@ -476,10 +475,6 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
|| !partitionFields.contains(fieldToAdd),
"Cannot alter partition column: " + fieldToAdd);
- if (c instanceof TableChange.UpdateColumnType) {
- validateColumnType(fieldToAdd, ((TableChange.UpdateColumnType) c).getNewDataType());
- }
-
if (c instanceof TableChange.UpdateColumnPosition
&& afterPartitionColumn(
partitionFields, ((TableChange.UpdateColumnPosition) c).getPosition())) {
@@ -490,8 +485,6 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
if (c instanceof TableChange.AddColumn) {
TableChange.AddColumn addColumn = (TableChange.AddColumn) c;
- validateColumnType(fieldToAdd, addColumn.getDataType());
-
if ((addColumn.getPosition() == null && !partitionFields.isEmpty())
|| (afterPartitionColumn(partitionFields, addColumn.getPosition()))) {
throw new IllegalArgumentException("Cannot add column after partition column");
@@ -561,7 +554,7 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
validatePartitionForCreate(columns, partitioning);
validateDistributionAndSort(distribution, sortOrders);
- Arrays.stream(columns).forEach(c -> validateColumnType(c.name(), c.dataType()));
+ Arrays.stream(columns).forEach(this::validateColumnType);
TableType tableType = (TableType) tablePropertiesMetadata.getOrDefault(properties, TABLE_TYPE);
Preconditions.checkArgument(
@@ -713,14 +706,14 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
}
}
- private void validateColumnType(String fieldName, Type type) {
+ private void validateColumnType(Column column) {
// The NOT NULL constraint for column is supported since Hive3.0, see
// https://issues.apache.org/jira/browse/HIVE-16575
- if (!type.nullable()) {
+ if (!column.nullable()) {
throw new IllegalArgumentException(
"The NOT NULL constraint for column is only supported since Hive 3.0, "
+ "but the current Gravitino Hive catalog only supports Hive 2.x. Illegal column: "
- + fieldName);
+ + column.name());
}
}
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveColumn.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveColumn.java
index c76d2c0472..92cbb68714 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveColumn.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveColumn.java
@@ -28,6 +28,7 @@ public class HiveColumn extends BaseColumn {
hiveColumn.name = name;
hiveColumn.comment = comment;
hiveColumn.dataType = dataType;
+ hiveColumn.nullable = nullable;
return hiveColumn;
}
}
diff --git a/catalogs/catalog-hive/src/test/java/com/datastrato/gravitino/catalog/hive/TestHiveTable.java b/catalogs/catalog-hive/src/test/java/com/datastrato/gravitino/catalog/hive/TestHiveTable.java
index 052f417560..401af0eef6 100644
--- a/catalogs/catalog-hive/src/test/java/com/datastrato/gravitino/catalog/hive/TestHiveTable.java
+++ b/catalogs/catalog-hive/src/test/java/com/datastrato/gravitino/catalog/hive/TestHiveTable.java
@@ -203,6 +203,7 @@ public class TestHiveTable extends MiniHiveMetastoreService {
.withName("col_3")
.withType(TypeCreator.REQUIRED.I8)
.withComment(HIVE_COMMENT)
+ .withNullable(false)
.build();
exception =
@@ -449,38 +450,23 @@ public class TestHiveTable extends MiniHiveMetastoreService {
TableChange.updateColumnPosition(new String[] {"col_1"}, null)));
Assertions.assertTrue(exception.getMessage().contains("Column position cannot be null"));
- exception =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () ->
- hiveCatalog
- .asTableCatalog()
- .alterTable(
- tableIdentifier,
- TableChange.addColumn(new String[] {"col_3"}, TypeCreator.REQUIRED.I8)));
- Assertions.assertTrue(
- exception
- .getMessage()
- .contains(
- "The NOT NULL constraint for column is only supported since Hive 3.0, "
- + "but the current Gravitino Hive catalog only supports Hive 2.x"));
-
- exception =
- Assertions.assertThrows(
- IllegalArgumentException.class,
- () ->
- hiveCatalog
- .asTableCatalog()
- .alterTable(
- tableIdentifier,
- TableChange.updateColumnType(
- new String[] {"col_1"}, TypeCreator.REQUIRED.I8)));
- Assertions.assertTrue(
- exception
- .getMessage()
- .contains(
- "The NOT NULL constraint for column is only supported since Hive 3.0, "
- + "but the current Gravitino Hive catalog only supports Hive 2.x"));
+ // TODO(minghuang): Uncomment below test after we support nullable in TableChange.addColumn
+ // exception =
+ // Assertions.assertThrows(
+ // IllegalArgumentException.class,
+ // () ->
+ // hiveCatalog
+ // .asTableCatalog()
+ // .alterTable(
+ // tableIdentifier,
+ // TableChange.addColumn(new String[] {"col_3"},
+ // TypeCreator.REQUIRED.I8)));
+ // Assertions.assertTrue(
+ // exception
+ // .getMessage()
+ // .contains(
+ // "The NOT NULL constraint for column is only supported since Hive 3.0, "
+ // + "but the current Gravitino Hive catalog only supports Hive 2.x"));
exception =
Assertions.assertThrows(
diff --git a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcColumn.java b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcColumn.java
index 455b5bb60a..89d84e9fba 100644
--- a/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcColumn.java
+++ b/catalogs/catalog-jdbc-common/src/main/java/com/datastrato/gravitino/catalog/jdbc/JdbcColumn.java
@@ -8,17 +8,10 @@ import com.datastrato.gravitino.catalog.rel.BaseColumn;
/** Represents a column in the Jdbc column. */
public class JdbcColumn extends BaseColumn {
-
- private boolean optional;
-
private String defaultValue;
private JdbcColumn() {}
- public boolean isOptional() {
- return optional;
- }
-
public String getDefaultValue() {
return defaultValue;
}
@@ -26,19 +19,11 @@ public class JdbcColumn extends BaseColumn {
/** A builder class for constructing JdbcColumn instances. */
public static class Builder extends BaseColumnBuilder<Builder, JdbcColumn> {
- /** Can the corresponding value of this field be null. */
- private boolean optional;
-
/**
* The default value for this field. This value will be used if the corresponding value is null.
*/
private String defaultValue;
- public Builder withOptional(boolean optional) {
- this.optional = optional;
- return this;
- }
-
public Builder withDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
return this;
@@ -55,7 +40,7 @@ public class JdbcColumn extends BaseColumn {
jdbcColumn.name = name;
jdbcColumn.comment = comment;
jdbcColumn.dataType = dataType;
- jdbcColumn.optional = optional;
+ jdbcColumn.nullable = nullable;
jdbcColumn.defaultValue = defaultValue;
return jdbcColumn;
}
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
index 51621341ed..05af5d3de3 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
@@ -492,7 +492,7 @@ public class IcebergCatalogOperations implements CatalogOperations, SupportsSche
.withName(column.name())
.withType(column.dataType())
.withComment(column.comment())
- .withOptional(true)
+ .withNullable(column.nullable())
.build())
.toArray(IcebergColumn[]::new);
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergColumn.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergColumn.java
index 8ec001be14..53109c7bd9 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergColumn.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergColumn.java
@@ -11,37 +11,23 @@ public class IcebergColumn extends BaseColumn {
private int id;
- private boolean optional;
-
private IcebergColumn() {}
public int getId() {
return id;
}
- public boolean isOptional() {
- return optional;
- }
-
/** A builder class for constructing IcebergColumn instances. */
public static class Builder extends BaseColumnBuilder<Builder, IcebergColumn> {
/** The ID of this field. */
private int id;
- /** Can the corresponding value of this field be null. */
- private boolean optional;
-
public Builder withId(int id) {
this.id = id;
return this;
}
- public Builder withOptional(boolean optional) {
- this.optional = optional;
- return this;
- }
-
/**
* Internal method to build a IcebergColumn instance using the provided values.
*
@@ -54,7 +40,7 @@ public class IcebergColumn extends BaseColumn {
icebergColumn.name = name;
icebergColumn.comment = comment;
icebergColumn.dataType = dataType;
- icebergColumn.optional = optional;
+ icebergColumn.nullable = nullable;
return icebergColumn;
}
}
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/ConvertUtil.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/ConvertUtil.java
index 08080d53fd..60124b9711 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/ConvertUtil.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/ConvertUtil.java
@@ -27,11 +27,12 @@ public class ConvertUtil {
/**
* Convert the Gravitino type to the Iceberg type.
*
- * @param gravitinoType
+ * @param nullable Whether the field is nullable.
+ * @param gravitinoType Gravitino type.
* @return Iceberg type.
*/
- public static Type toIcebergType(io.substrait.type.Type gravitinoType) {
- return ToIcebergTypeVisitor.visit(gravitinoType, new ToIcebergType());
+ public static Type toIcebergType(boolean nullable, io.substrait.type.Type gravitinoType) {
+ return ToIcebergTypeVisitor.visit(gravitinoType, new ToIcebergType(nullable));
}
/**
@@ -54,7 +55,7 @@ public class ConvertUtil {
return new IcebergColumn.Builder()
.withId(nestedField.fieldId())
.withName(nestedField.name())
- .withOptional(nestedField.isOptional())
+ .withNullable(nestedField.isOptional())
.withComment(nestedField.doc())
.withType(ConvertUtil.formIcebergType(nestedField.type()))
.build();
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergType.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergType.java
index 330d208592..0a03943f26 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergType.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/ToIcebergType.java
@@ -21,9 +21,11 @@ import org.apache.iceberg.types.Types;
public class ToIcebergType extends ToIcebergTypeVisitor<Type> {
private final IcebergTable root;
private int nextId = 0;
+ private boolean nullable;
- public ToIcebergType() {
+ public ToIcebergType(boolean nullable) {
this.root = null;
+ this.nullable = nullable;
}
public ToIcebergType(IcebergTable root) {
@@ -51,7 +53,7 @@ public class ToIcebergType extends ToIcebergTypeVisitor<Type> {
// for new conversions, use ordinals for ids in the root struct
int id = isRoot ? i : getNextId();
- if (field.isOptional()) {
+ if (field.nullable()) {
newFields.add(Types.NestedField.optional(id, field.name(), type, field.comment()));
} else {
newFields.add(Types.NestedField.required(id, field.name(), type, field.comment()));
@@ -67,7 +69,7 @@ public class ToIcebergType extends ToIcebergTypeVisitor<Type> {
@Override
public Type array(io.substrait.type.Type.ListType array, Type elementType) {
- if (array.nullable()) {
+ if (nullable) {
return Types.ListType.ofOptional(getNextId(), elementType);
} else {
return Types.ListType.ofRequired(getNextId(), elementType);
@@ -76,7 +78,7 @@ public class ToIcebergType extends ToIcebergTypeVisitor<Type> {
@Override
public Type map(io.substrait.type.Type.Map map, Type keyType, Type valueType) {
- if (map.nullable()) {
+ if (nullable) {
return Types.MapType.ofOptional(getNextId(), getNextId(), keyType, valueType);
} else {
return Types.MapType.ofRequired(getNextId(), getNextId(), keyType, valueType);
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/ops/IcebergTableOpsHelper.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/ops/IcebergTableOpsHelper.java
index 339a0f2257..160562be49 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/ops/IcebergTableOpsHelper.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/ops/IcebergTableOpsHelper.java
@@ -131,15 +131,21 @@ public class IcebergTableOpsHelper {
}
private void doUpdateColumnType(
- UpdateSchema icebergUpdateSchema, UpdateColumnType updateColumnType) {
+ UpdateSchema icebergUpdateSchema,
+ UpdateColumnType updateColumnType,
+ Schema icebergTableSchema) {
+ String fieldName = DOT.join(updateColumnType.fieldNames());
+ Preconditions.checkArgument(
+ icebergTableSchema.findField(fieldName) != null,
+ "Cannot update missing field: %s",
+ fieldName);
+
+ boolean nullable = icebergTableSchema.findField(fieldName).isOptional();
org.apache.iceberg.types.Type type =
- ConvertUtil.toIcebergType(updateColumnType.getNewDataType());
+ ConvertUtil.toIcebergType(nullable, updateColumnType.getNewDataType());
Preconditions.checkArgument(
- type.isPrimitiveType(),
- "Cannot update %s, not a primitive type: %s",
- DOT.join(updateColumnType.fieldNames()),
- type);
- icebergUpdateSchema.updateColumn(DOT.join(updateColumnType.fieldNames()), (PrimitiveType) type);
+ type.isPrimitiveType(), "Cannot update %s, not a primitive type: %s", fieldName, type);
+ icebergUpdateSchema.updateColumn(fieldName, (PrimitiveType) type);
}
private ColumnPosition getAddColumnPosition(StructType parent, ColumnPosition columnPosition) {
@@ -180,7 +186,8 @@ public class IcebergTableOpsHelper {
icebergUpdateSchema.addColumn(
getParentName(addColumn.fieldNames()),
getLeafName(addColumn.fieldNames()),
- ConvertUtil.toIcebergType(addColumn.getDataType()),
+ // TODO(minghuang): add nullable support in AddColumn
+ ConvertUtil.toIcebergType(true, addColumn.getDataType()),
addColumn.getComment());
ColumnPosition position = getAddColumnPosition(parentStruct, addColumn.getPosition());
@@ -218,7 +225,7 @@ public class IcebergTableOpsHelper {
} else if (change instanceof RenameColumn) {
doRenameColumn(icebergUpdateSchema, (RenameColumn) change);
} else if (change instanceof UpdateColumnType) {
- doUpdateColumnType(icebergUpdateSchema, (UpdateColumnType) change);
+ doUpdateColumnType(icebergUpdateSchema, (UpdateColumnType) change, icebergTableSchema);
} else if (change instanceof UpdateColumnComment) {
doUpdateColumnComment(icebergUpdateSchema, (UpdateColumnComment) change);
} else {
diff --git a/catalogs/catalog-lakehouse-iceberg/src/test/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/TestIcebergTable.java b/catalogs/catalog-lakehouse-iceberg/src/test/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/TestIcebergTable.java
index 15ce565fa0..ba6cbdfe57 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/test/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/TestIcebergTable.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/test/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/TestIcebergTable.java
@@ -130,12 +130,14 @@ public class TestIcebergTable {
.withName("col_1")
.withType(TypeCreator.NULLABLE.I8)
.withComment(ICEBERG_COMMENT)
+ .withNullable(true)
.build();
IcebergColumn col2 =
new IcebergColumn.Builder()
.withName("col_2")
.withType(TypeCreator.NULLABLE.DATE)
.withComment(ICEBERG_COMMENT)
+ .withNullable(false)
.build();
Column[] columns = new Column[] {col1, col2};
@@ -160,6 +162,8 @@ public class TestIcebergTable {
Assertions.assertEquals("val1", loadedTable.properties().get("key1"));
Assertions.assertEquals("val2", loadedTable.properties().get("key2"));
+ Assertions.assertTrue(loadedTable.columns()[0].nullable());
+ Assertions.assertFalse(loadedTable.columns()[1].nullable());
Assertions.assertTrue(icebergCatalog.asTableCatalog().tableExists(tableIdentifier));
NameIdentifier[] tableIdents =
diff --git a/catalogs/catalog-lakehouse-iceberg/src/test/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/TestConvertUtil.java b/catalogs/catalog-lakehouse-iceberg/src/test/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/TestConvertUtil.java
index 031de6dcd3..35ade5abd7 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/test/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/TestConvertUtil.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/test/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/converter/TestConvertUtil.java
@@ -84,56 +84,57 @@ public class TestConvertUtil extends TestBaseConvert {
@Test
public void testToPrimitiveType() {
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.BOOLEAN) instanceof Types.BooleanType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.BOOLEAN) instanceof Types.BooleanType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.STRING) instanceof Types.StringType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.STRING) instanceof Types.StringType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.I8) instanceof Types.IntegerType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.I8) instanceof Types.IntegerType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.I16) instanceof Types.IntegerType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.I16) instanceof Types.IntegerType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.I32) instanceof Types.IntegerType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.I32) instanceof Types.IntegerType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.I64) instanceof Types.LongType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.I64) instanceof Types.LongType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.FP32) instanceof Types.FloatType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.FP32) instanceof Types.FloatType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.FP64) instanceof Types.DoubleType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.FP64) instanceof Types.DoubleType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.DATE) instanceof Types.DateType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.DATE) instanceof Types.DateType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.TIME) instanceof Types.TimeType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.TIME) instanceof Types.TimeType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.BINARY) instanceof Types.BinaryType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.BINARY) instanceof Types.BinaryType);
Assertions.assertTrue(
- ConvertUtil.toIcebergType(TypeCreator.NULLABLE.UUID) instanceof Types.UUIDType);
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.UUID) instanceof Types.UUIDType);
- Type timestampTZ = ConvertUtil.toIcebergType(TypeCreator.NULLABLE.TIMESTAMP_TZ);
+ Type timestampTZ = ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.TIMESTAMP_TZ);
Assertions.assertTrue(timestampTZ instanceof Types.TimestampType);
Assertions.assertTrue(((Types.TimestampType) timestampTZ).shouldAdjustToUTC());
- Type timestamp = ConvertUtil.toIcebergType(TypeCreator.NULLABLE.TIMESTAMP);
+ Type timestamp = ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.TIMESTAMP);
Assertions.assertTrue(timestamp instanceof Types.TimestampType);
Assertions.assertFalse(((Types.TimestampType) timestamp).shouldAdjustToUTC());
- Type decimalType = ConvertUtil.toIcebergType(TypeCreator.NULLABLE.decimal(9, 2));
+ Type decimalType = ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.decimal(9, 2));
Assertions.assertTrue(decimalType instanceof Types.DecimalType);
Assertions.assertEquals(((Types.DecimalType) decimalType).precision(), 9);
Assertions.assertEquals(((Types.DecimalType) decimalType).scale(), 2);
- Type fixedCharType = ConvertUtil.toIcebergType(TypeCreator.NULLABLE.fixedChar(9));
+ Type fixedCharType = ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.fixedChar(9));
Assertions.assertTrue(fixedCharType instanceof Types.FixedType);
Assertions.assertEquals(((Types.FixedType) fixedCharType).length(), 9);
io.substrait.type.Type mapType =
TypeCreator.NULLABLE.map(TypeCreator.NULLABLE.STRING, TypeCreator.NULLABLE.I8);
- Type convertedMapType = ConvertUtil.toIcebergType(mapType);
+ Type convertedMapType = ConvertUtil.toIcebergType(true, mapType);
Assertions.assertTrue(convertedMapType instanceof Types.MapType);
Assertions.assertTrue(((Types.MapType) convertedMapType).keyType() instanceof Types.StringType);
Assertions.assertTrue(
((Types.MapType) convertedMapType).valueType() instanceof Types.IntegerType);
- Type listType = ConvertUtil.toIcebergType(TypeCreator.NULLABLE.list(TypeCreator.NULLABLE.FP64));
+ Type listType =
+ ConvertUtil.toIcebergType(true, TypeCreator.NULLABLE.list(TypeCreator.NULLABLE.FP64));
Assertions.assertTrue(listType instanceof Types.ListType);
Assertions.assertTrue(listType.asListType().elementType() instanceof Types.DoubleType);
}
@@ -202,7 +203,7 @@ public class TestConvertUtil extends TestBaseConvert {
Types.NestedField.optional(1, colName, Types.IntegerType.get(), doc);
IcebergColumn icebergColumn = ConvertUtil.fromNestedField(colField);
Assertions.assertEquals(icebergColumn.name(), colName);
- Assertions.assertTrue(icebergColumn.isOptional());
+ Assertions.assertTrue(icebergColumn.nullable());
Assertions.assertEquals(icebergColumn.comment(), doc);
Assertions.assertTrue(icebergColumn.dataType() instanceof io.substrait.type.Type.I32);
@@ -211,7 +212,7 @@ public class TestConvertUtil extends TestBaseConvert {
colField = Types.NestedField.required(1, colName, Types.StringType.get(), doc);
icebergColumn = ConvertUtil.fromNestedField(colField);
Assertions.assertEquals(icebergColumn.name(), colName);
- Assertions.assertFalse(icebergColumn.isOptional());
+ Assertions.assertFalse(icebergColumn.nullable());
Assertions.assertEquals(icebergColumn.comment(), doc);
Assertions.assertTrue(icebergColumn.dataType() instanceof io.substrait.type.Type.Str);
@@ -220,7 +221,7 @@ public class TestConvertUtil extends TestBaseConvert {
colField = Types.NestedField.required(1, colName, Types.DateType.get(), doc);
icebergColumn = ConvertUtil.fromNestedField(colField);
Assertions.assertEquals(icebergColumn.name(), colName);
- Assertions.assertFalse(icebergColumn.isOptional());
+ Assertions.assertFalse(icebergColumn.nullable());
Assertions.assertEquals(icebergColumn.comment(), doc);
Assertions.assertTrue(icebergColumn.dataType() instanceof io.substrait.type.Type.Date);
@@ -238,7 +239,7 @@ public class TestConvertUtil extends TestBaseConvert {
doc);
icebergColumn = ConvertUtil.fromNestedField(colField);
Assertions.assertEquals(icebergColumn.name(), colName);
- Assertions.assertFalse(icebergColumn.isOptional());
+ Assertions.assertFalse(icebergColumn.nullable());
Assertions.assertEquals(icebergColumn.comment(), doc);
Assertions.assertTrue(icebergColumn.dataType() instanceof io.substrait.type.Type.Map);
}
diff --git a/common/src/main/java/com/datastrato/gravitino/dto/rel/ColumnDTO.java b/common/src/main/java/com/datastrato/gravitino/dto/rel/ColumnDTO.java
index e294a2db2c..779a31fda7 100644
--- a/common/src/main/java/com/datastrato/gravitino/dto/rel/ColumnDTO.java
+++ b/common/src/main/java/com/datastrato/gravitino/dto/rel/ColumnDTO.java
@@ -30,6 +30,9 @@ public class ColumnDTO implements Column {
@JsonProperty("comment")
private String comment;
+ @JsonProperty("nullable")
+ private boolean nullable;
+
private ColumnDTO() {}
/**
@@ -40,9 +43,22 @@ public class ColumnDTO implements Column {
* @param comment The comment associated with the column.
*/
private ColumnDTO(String name, Type dataType, String comment) {
+ this(name, dataType, comment, true);
+ }
+
+ /**
+ * Constructs a Column DTO.
+ *
+ * @param name The name of the column.
+ * @param dataType The data type of the column.
+ * @param comment The comment associated with the column.
+ * @param nullable Whether the column value can be null.
+ */
+ private ColumnDTO(String name, Type dataType, String comment, boolean nullable) {
this.name = name;
this.dataType = dataType;
this.comment = comment;
+ this.nullable = nullable;
}
@Override
@@ -60,6 +76,11 @@ public class ColumnDTO implements Column {
return comment;
}
+ @Override
+ public boolean nullable() {
+ return nullable;
+ }
+
/**
* Creates a new Builder to build a Column DTO.
*
@@ -79,6 +100,7 @@ public class ColumnDTO implements Column {
protected String name;
protected Type dataType;
protected String comment;
+ protected boolean nullable = true;
public Builder() {}
@@ -115,6 +137,17 @@ public class ColumnDTO implements Column {
return (S) this;
}
+ /**
+ * Sets whether the column value can be null.
+ *
+ * @param nullable Whether the column value can be null.
+ * @return The Builder instance.
+ */
+ public S withNullable(boolean nullable) {
+ this.nullable = nullable;
+ return (S) this;
+ }
+
/**
* Builds a Column DTO based on the provided builder parameters.
*
@@ -124,7 +157,7 @@ public class ColumnDTO implements Column {
public ColumnDTO build() {
Preconditions.checkNotNull(name, "Column name cannot be null");
Preconditions.checkNotNull(dataType, "Column data type cannot be null");
- return new ColumnDTO(name, dataType, comment);
+ return new ColumnDTO(name, dataType, comment, nullable);
}
}
}
diff --git a/common/src/test/java/com/datastrato/gravitino/json/TestDTOJsonSerDe.java b/common/src/test/java/com/datastrato/gravitino/json/TestDTOJsonSerDe.java
index 41d20eb389..b2d93d5c35 100644
--- a/common/src/test/java/com/datastrato/gravitino/json/TestDTOJsonSerDe.java
+++ b/common/src/test/java/com/datastrato/gravitino/json/TestDTOJsonSerDe.java
@@ -42,7 +42,7 @@ public class TestDTOJsonSerDe {
private final String metalakeJson = "{\"name\":%s,\"comment\":%s,\"properties\":%s,\"audit\":%s}";
- private final String columnJson = "{\"name\":%s,\"type\":%s,\"comment\":%s}";
+ private final String columnJson = "{\"name\":%s,\"type\":%s,\"comment\":%s,\"nullable\":%s}";
private final String tableJson =
"{\"name\":%s,\"comment\":%s,\"columns\":[%s],\"properties\":%s,\"audit\":%s,\"distribution\":%s,\"sortOrders\":%s,\"partitioning\":%s}";
@@ -182,7 +182,8 @@ public class TestDTOJsonSerDe {
columnJson,
withQuotes(name),
withQuotes(type.accept(new StringTypeVisitor())),
- withQuotes(comment));
+ withQuotes(comment),
+ column.nullable());
Assertions.assertEquals(expectedJson, serJson);
ColumnDTO deserColumn = JsonUtils.objectMapper().readValue(serJson, ColumnDTO.class);
Assertions.assertEquals(column, deserColumn);
@@ -202,7 +203,12 @@ public class TestDTOJsonSerDe {
Map<String, String> properties = ImmutableMap.of("k1", "v1", "k2", "v2");
ColumnDTO column =
- ColumnDTO.builder().withName(name).withDataType(type).withComment(comment).build();
+ ColumnDTO.builder()
+ .withName(name)
+ .withDataType(type)
+ .withComment(comment)
+ .withNullable(false)
+ .build();
TableDTO table =
TableDTO.builder()
.withName(tableName)
@@ -222,7 +228,8 @@ public class TestDTOJsonSerDe {
columnJson,
withQuotes(name),
withQuotes(type.accept(new StringTypeVisitor())),
- withQuotes(comment)),
+ withQuotes(comment),
+ column.nullable()),
JsonUtils.objectMapper().writeValueAsString(properties),
String.format(auditJson, withQuotes(creator), withQuotes(now.toString()), null, null),
null,
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/rel/BaseColumn.java b/core/src/main/java/com/datastrato/gravitino/catalog/rel/BaseColumn.java
index f9711c1582..800e65c8c2 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/rel/BaseColumn.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/rel/BaseColumn.java
@@ -22,6 +22,8 @@ public abstract class BaseColumn implements Column {
protected Type dataType;
+ protected boolean nullable;
+
/**
* Returns the name of the column.
*
@@ -53,6 +55,12 @@ public abstract class BaseColumn implements Column {
return comment;
}
+ /** Returns true if the column value can be null. */
+ @Override
+ public boolean nullable() {
+ return nullable;
+ }
+
/**
* Builder interface for creating instances of {@link BaseColumn}.
*
@@ -66,6 +74,8 @@ public abstract class BaseColumn implements Column {
SELF withType(Type dataType);
+ SELF withNullable(boolean nullable);
+
T build();
}
@@ -81,6 +91,7 @@ public abstract class BaseColumn implements Column {
protected String name;
protected String comment;
protected Type dataType;
+ protected boolean nullable = true;
/**
* Sets the name of the column.
@@ -118,6 +129,18 @@ public abstract class BaseColumn implements Column {
return self();
}
+ /**
+ * Sets nullable of the column value.
+ *
+ * @param nullable Nullable of the column.
+ * @return The builder instance.
+ */
+ @Override
+ public SELF withNullable(boolean nullable) {
+ this.nullable = nullable;
+ return self();
+ }
+
/**
* Builds the instance of the column with the provided attributes.
*
diff --git a/core/src/test/java/com/datastrato/gravitino/TestColumn.java b/core/src/test/java/com/datastrato/gravitino/TestColumn.java
index 2e4c4a7c26..bfd8dd8083 100644
--- a/core/src/test/java/com/datastrato/gravitino/TestColumn.java
+++ b/core/src/test/java/com/datastrato/gravitino/TestColumn.java
@@ -23,6 +23,7 @@ public class TestColumn extends BaseColumn {
column.name = name;
column.comment = comment;
column.dataType = dataType;
+ column.nullable = nullable;
return column;
}
diff --git a/core/src/test/java/com/datastrato/gravitino/catalog/rel/TestBaseColumn.java b/core/src/test/java/com/datastrato/gravitino/catalog/rel/TestBaseColumn.java
index 7020df7217..e4bc7a651a 100644
--- a/core/src/test/java/com/datastrato/gravitino/catalog/rel/TestBaseColumn.java
+++ b/core/src/test/java/com/datastrato/gravitino/catalog/rel/TestBaseColumn.java
@@ -21,6 +21,7 @@ final class BaseColumnExtension extends BaseColumn {
column.name = name;
column.comment = comment;
column.dataType = dataType;
+ column.nullable = nullable;
return column;
}
}
diff --git a/docs/gravitino-manage-hive.md b/docs/gravitino-manage-hive.md
index 711ff6a40e..9815761688 100644
--- a/docs/gravitino-manage-hive.md
+++ b/docs/gravitino-manage-hive.md
@@ -101,22 +101,26 @@ Example JSON:
{
"name": "id",
"type": "i32?",
- "comment": "id column comment"
+ "comment": "id column comment",
+ "nullable": true
},
{
"name": "name",
"type": "string?",
- "comment": "name column comment"
+ "comment": "name column comment",
+ "nullable": true
},
{
"name": "age",
"type": "i32?",
- "comment": "age column comment"
+ "comment": "age column comment",
+ "nullable": true
},
{
"name": "dt",
"type": "date?",
- "comment": "dt column comment"
+ "comment": "dt column comment",
+ "nullable": true
}
],
"partitions": [
diff --git a/docs/gravitino-use-iceberg.md b/docs/gravitino-use-iceberg.md
index cd51c1e7d3..8d59566dac 100644
--- a/docs/gravitino-use-iceberg.md
+++ b/docs/gravitino-use-iceberg.md
@@ -81,22 +81,26 @@ Example JSON:
{
"name": "id",
"type": "i32",
- "comment": "id column comment"
+ "comment": "id column comment",
+ "nullable": false
},
{
"name": "name",
"type": "string",
- "comment": "name column comment"
+ "comment": "name column comment",
+ "nullable": false
},
{
"name": "age",
"type": "i32",
- "comment": "age column comment"
+ "comment": "age column comment",
+ "nullable": false
},
{
"name": "dt",
"type": "date",
- "comment": "dt column comment"
+ "comment": "dt column comment",
+ "nullable": false
}
],
"partitions": [