World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#7807] improvement(mysql-catalog): Add index column not null limitation (#7808)
qbhanaed4b8bcec31
diff --git a/catalogs/catalog-jdbc-mysql/src/main/java/org/apache/gravitino/catalog/mysql/operation/MysqlTableOperations.java b/catalogs/catalog-jdbc-mysql/src/main/java/org/apache/gravitino/catalog/mysql/operation/MysqlTableOperations.javaindex 8a5d356847..4df98eb96f 100644--- a/catalogs/catalog-jdbc-mysql/src/main/java/org/apache/gravitino/catalog/mysql/operation/MysqlTableOperations.java+++ b/catalogs/catalog-jdbc-mysql/src/main/java/org/apache/gravitino/catalog/mysql/operation/MysqlTableOperations.java@@ -652,7 +652,7 @@ public class MysqlTableOperations extends JdbcTableOperations {Arrays.stream(columns).collect(Collectors.toMap(JdbcColumn::name, c -> c));for (Index index : indexes) {if (index.type() == Index.IndexType.UNIQUE_KEY) {- // the column in the unique index must be not null+ // the auto increment column in the unique index must be not nullfor (String[] colNames : index.fieldNames()) {JdbcColumn column = columnMap.get(colNames[0]);Preconditions.checkArgument(@@ -661,12 +661,27 @@ public class MysqlTableOperations extends JdbcTableOperations {colNames[0],index.name());Preconditions.checkArgument(- !column.nullable(),- "Column %s in the unique index %s must be a not null column",+ !(column.autoIncrement() && column.nullable()),+ "Auto increment column %s in the unique index %s must be a not null column",colNames[0],index.name());}}++ if (index.type() == Index.IndexType.PRIMARY_KEY) {+ // the column in the primary key must be not null+ for (String[] colNames : index.fieldNames()) {+ JdbcColumn column = columnMap.get(colNames[0]);+ Preconditions.checkArgument(+ column != null,+ "Column %s in the primary key does not exist in the table",+ colNames[0]);+ Preconditions.checkArgument(+ !column.nullable(),+ "Column %s in the primary key must be a not null column",+ colNames[0]);+ }+ }}}diff --git a/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/integration/test/CatalogMysqlIT.java b/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/integration/test/CatalogMysqlIT.javaindex 139c608038..981619ce5d 100644--- a/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/integration/test/CatalogMysqlIT.java+++ b/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/integration/test/CatalogMysqlIT.java@@ -1098,26 +1098,111 @@ public class CatalogMysqlIT extends BaseIT {Assertions.assertNotNull(table.index()[0].name());Assertions.assertNotNull(table.index()[1].name());- Column notNullCol = Column.of("col_6", Types.LongType.get(), "id", true, false, null);- Exception exception =+ NameIdentifier nullableTableIdent = NameIdentifier.of(schemaName, "test_nullable");+ Column nullWithAutoIncrementCol =+ Column.of("col_6", Types.LongType.get(), "id", true, true, null);++ Exception uniqueKeyNotExistException =+ assertThrows(+ IllegalArgumentException.class,+ () ->+ tableCatalog.createTable(+ nullableTableIdent,+ new Column[] {nullWithAutoIncrementCol},+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0],+ new Index[] {+ Indexes.of(+ Index.IndexType.UNIQUE_KEY,+ "u_key",+ new String[][] {{"col_7"}, {"col_6"}}),+ }));+ Assertions.assertTrue(+ uniqueKeyNotExistException+ .getMessage()+ .contains("Column col_7 in the unique index u_key does not exist in the table"));++ Exception uniqueKeyNotNullException =+ assertThrows(+ IllegalArgumentException.class,+ () ->+ tableCatalog.createTable(+ nullableTableIdent,+ new Column[] {nullWithAutoIncrementCol},+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0],+ new Index[] {+ Indexes.of(Index.IndexType.UNIQUE_KEY, "u_key", new String[][] {{"col_6"}}),+ }));+ Assertions.assertTrue(+ uniqueKeyNotNullException+ .getMessage()+ .contains(+ "Auto increment column col_6 in the unique index u_key must be a not null column"));++ Column nullWithoutAutoIncrementCol =+ Column.of("col_7", Types.LongType.get(), "id", true, false, null);+ tableCatalog.createTable(+ nullableTableIdent,+ new Column[] {nullWithoutAutoIncrementCol},+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0],+ new Index[] {+ Indexes.of(Index.IndexType.UNIQUE_KEY, "u_key", new String[][] {{"col_7"}}),+ });+ table = tableCatalog.loadTable(nullableTableIdent);++ Assertions.assertEquals(1, table.index().length);+ Assertions.assertNotNull(table.index()[0].name());++ Exception primaryKeyNotExistexception =+ assertThrows(+ IllegalArgumentException.class,+ () ->+ tableCatalog.createTable(+ nullableTableIdent,+ new Column[] {nullWithoutAutoIncrementCol},+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0],+ new Index[] {+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_8"}}),+ }));+ Assertions.assertTrue(+ primaryKeyNotExistexception+ .getMessage()+ .contains("Column col_8 in the primary key does not exist in the table"));++ Exception primaryKeyNotNullException =assertThrows(IllegalArgumentException.class,() ->tableCatalog.createTable(- tableIdent,- new Column[] {notNullCol},+ nullableTableIdent,+ new Column[] {nullWithoutAutoIncrementCol},table_comment,properties,Transforms.EMPTY_TRANSFORM,Distributions.NONE,new SortOrder[0],new Index[] {- Indexes.of(Index.IndexType.UNIQUE_KEY, null, new String[][] {{"col_6"}}),+ Indexes.createMysqlPrimaryKey(new String[][] {{"col_7"}}),}));Assertions.assertTrue(- exception+ primaryKeyNotNullException.getMessage()- .contains("Column col_6 in the unique index null must be a not null column"));+ .contains("Column col_7 in the primary key must be a not null column"));}@Testdiff --git a/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysqlTableOperations.java b/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysqlTableOperations.javaindex b0ea5fb990..1146be6b72 100644--- a/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysqlTableOperations.java+++ b/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysqlTableOperations.java@@ -64,7 +64,7 @@ public class TestMysqlTableOperations extends TestMysql {.withName("col_1").withType(VARCHAR).withComment("test_comment")- .withNullable(false)+ .withNullable(true).build());columns.add(JdbcColumn.builder()@@ -573,7 +573,7 @@ public class TestMysqlTableOperations extends TestMysql {JdbcColumn.builder().withName("col_4").withType(Types.DateType.get())- .withNullable(false)+ .withNullable(true).withComment("date").withDefaultValue(Column.DEFAULT_VALUE_NOT_SET).build());