World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#462] fix(hive): validate non-null column type in Hive catalog (#507)
mchades664ebef85921
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java b/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.javaindex f64d1dbbad..8472cffbdc 100644--- a/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java@@ -35,6 +35,7 @@ import com.datastrato.graviton.rel.transforms.Transforms;import com.google.common.annotations.VisibleForTesting;import com.google.common.base.Preconditions;import com.google.common.collect.Maps;+import io.substrait.type.Type;import java.io.IOException;import java.time.Instant;import java.util.Arrays;@@ -446,6 +447,8 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas"Hive partition only supports identity transform");validateDistributionAndSort(distribution, sortOrders);+ Arrays.stream(columns).forEach(c -> validateColumnType(c.name(), c.dataType()));+TableType tableType = (TableType) tablePropertiesMetadata.getOrDefault(properties, TABLE_TYPE);Preconditions.checkArgument(SUPPORT_TABLE_TYPES.contains(tableType.name()),@@ -537,7 +540,9 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemasList<FieldSchema> cols = sd.getCols();if (change instanceof TableChange.AddColumn) {- doAddColumn(cols, (TableChange.AddColumn) change);+ TableChange.AddColumn addColumn = (TableChange.AddColumn) change;+ validateColumnType(String.join(".", addColumn.fieldNames()), addColumn.getDataType());+ doAddColumn(cols, addColumn);} else if (change instanceof TableChange.DeleteColumn) {doDeleteColumn(cols, (TableChange.DeleteColumn) change);@@ -552,7 +557,10 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemasdoUpdateColumnPosition(cols, (TableChange.UpdateColumnPosition) change);} else if (change instanceof TableChange.UpdateColumnType) {- doUpdateColumnType(cols, (TableChange.UpdateColumnType) change);+ TableChange.UpdateColumnType updateColumnType = (TableChange.UpdateColumnType) change;+ validateColumnType(+ String.join(".", updateColumnType.fieldNames()), updateColumnType.getNewDataType());+ doUpdateColumnType(cols, updateColumnType);} else {throw new IllegalArgumentException(@@ -597,6 +605,17 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas}}+ private void validateColumnType(String fieldName, Type type) {+ // The NOT NULL constraint for column is supported since Hive3.0, see+ // https://issues.apache.org/jira/browse/HIVE-16575+ if (!type.nullable()) {+ throw new IllegalArgumentException(+ "The NOT NULL constraint for column is only supported since Hive 3.0, "+ + "but the current Graviton Hive catalog only supports Hive 2.x. Illegal column: "+ + fieldName);+ }+ }+private int columnPosition(List<FieldSchema> columns, TableChange.ColumnPosition position) {Preconditions.checkArgument(position != null, "Column position cannot be null");if (position instanceof TableChange.After) {@@ -650,7 +669,7 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemasparameters.remove(change.getProperty());}- void doAddColumn(List<FieldSchema> cols, TableChange.AddColumn change) {+ private void doAddColumn(List<FieldSchema> cols, TableChange.AddColumn change) {// add to the end by defaultint targetPosition =change.getPosition() == null ? cols.size() : columnPosition(cols, change.getPosition());diff --git a/catalogs/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/TestHiveTable.java b/catalogs/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/TestHiveTable.javaindex 3b819c5aaf..402e0e1c5b 100644--- a/catalogs/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/TestHiveTable.java+++ b/catalogs/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/TestHiveTable.java@@ -106,7 +106,7 @@ public class TestHiveTable extends MiniHiveMetastoreService {}@Test- public void testCreateHiveTable() throws IOException {+ public void testCreateHiveTable() {String hiveTableName = "test_hive_table";NameIdentifier tableIdentifier =NameIdentifier.of(META_LAKE_NAME, hiveCatalog.name(), hiveSchema.name(), hiveTableName);@@ -189,6 +189,34 @@ public class TestHiveTable extends MiniHiveMetastoreService {distribution,sortOrders));Assertions.assertTrue(exception.getMessage().contains("Table already exists"));++ HiveColumn illegalColumn =+ new HiveColumn.Builder()+ .withName("col_3")+ .withType(TypeCreator.REQUIRED.I8)+ .withComment(HIVE_COMMENT)+ .build();++ exception =+ Assertions.assertThrows(+ IllegalArgumentException.class,+ () ->+ hiveCatalog+ .asTableCatalog()+ .createTable(+ tableIdentifier,+ new Column[] {illegalColumn},+ HIVE_COMMENT,+ properties,+ new Transform[0],+ distribution,+ sortOrders));+ Assertions.assertTrue(+ exception+ .getMessage()+ .contains(+ "The NOT NULL constraint for column is only supported since Hive 3.0, "+ + "but the current Graviton Hive catalog only supports Hive 2.x"));}@Test@@ -412,6 +440,39 @@ 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_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 Graviton 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 Graviton Hive catalog only supports Hive 2.x"));+// test alterhiveCatalog.asTableCatalog()