This is an automated email from the ASF dual-hosted git repository.

yuqi4733 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new aed4b8bcec [#7807] improvement(mysql-catalog): Add index column not 
null limitation (#7808)
aed4b8bcec is described below

commit aed4b8bcec31eb45c79feb24d50feedf38faccdd
Author: qbhan <[email protected]>
AuthorDate: Tue Jul 29 11:23:57 2025 +0800

    [#7807] improvement(mysql-catalog): Add index column not null limitation 
(#7808)
    
    ### What changes were proposed in this pull request?
    - The `primary key` must be `not null`
    -  The auto increment `unique key` must be `not null`
    
    ### Why are the changes needed?
    Fix: #7807
    
    ### Does this PR introduce _any_ user-facing change?
    no
    
    ### How was this patch tested?
    local tests
---
 .../mysql/operation/MysqlTableOperations.java      | 21 ++++-
 .../mysql/integration/test/CatalogMysqlIT.java     | 99 ++++++++++++++++++++--
 .../mysql/operation/TestMysqlTableOperations.java  |  4 +-
 3 files changed, 112 insertions(+), 12 deletions(-)

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.java
index 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 null
         for (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.java
index 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"));
   }
 
   @Test
diff --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.java
index 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());

Reply via email to