World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#1758] feat(autoIncrement) : ColumnChange support auto_increment. (#2381)
Clearvivedca15cdca988
diff --git a/api/src/main/java/com/datastrato/gravitino/rel/TableChange.java b/api/src/main/java/com/datastrato/gravitino/rel/TableChange.javaindex be0fda3e27..81a712f54a 100644--- a/api/src/main/java/com/datastrato/gravitino/rel/TableChange.java+++ b/api/src/main/java/com/datastrato/gravitino/rel/TableChange.java@@ -88,7 +88,7 @@ public interface TableChange {* @return A TableChange for the addition.*/static TableChange addColumn(String[] fieldName, Type dataType) {- return new AddColumn(fieldName, dataType, null, null, true);+ return new AddColumn(fieldName, dataType, null, null, true, false);}/**@@ -104,7 +104,7 @@ public interface TableChange {* @return A TableChange for the addition.*/static TableChange addColumn(String[] fieldName, Type dataType, String comment) {- return new AddColumn(fieldName, dataType, comment, null, true);+ return new AddColumn(fieldName, dataType, comment, null, true, false);}/**@@ -120,7 +120,7 @@ public interface TableChange {* @return A TableChange for the addition.*/static TableChange addColumn(String[] fieldName, Type dataType, ColumnPosition position) {- return new AddColumn(fieldName, dataType, null, position, true);+ return new AddColumn(fieldName, dataType, null, position, true, false);}/**@@ -138,7 +138,7 @@ public interface TableChange {*/static TableChange addColumn(String[] fieldName, Type dataType, String comment, ColumnPosition position) {- return new AddColumn(fieldName, dataType, comment, position, true);+ return new AddColumn(fieldName, dataType, comment, position, true, false);}/**@@ -154,7 +154,7 @@ public interface TableChange {* @return A TableChange for the addition.*/static TableChange addColumn(String[] fieldName, Type dataType, boolean nullable) {- return new AddColumn(fieldName, dataType, null, null, nullable);+ return new AddColumn(fieldName, dataType, null, null, nullable, false);}/**@@ -168,7 +168,7 @@ public interface TableChange {*/static TableChange addColumn(String[] fieldName, Type dataType, String comment, boolean nullable) {- return new AddColumn(fieldName, dataType, comment, null, nullable);+ return new AddColumn(fieldName, dataType, comment, null, nullable, false);}/**@@ -191,7 +191,32 @@ public interface TableChange {String comment,ColumnPosition position,boolean nullable) {- return new AddColumn(fieldName, dataType, comment, position, nullable);+ return new AddColumn(fieldName, dataType, comment, position, nullable, false);+ }++ /**+ * Create a TableChange for adding a column.+ *+ * <p>If the field already exists, the change will result in an {@link IllegalArgumentException}.+ * If the new field is nested and its parent does not exist or is not a struct, the change will+ * result in an {@link IllegalArgumentException}.+ *+ * @param fieldName Field name of the new column.+ * @param dataType The new column's data type.+ * @param comment The new field's comment string.+ * @param position The new column's position.+ * @param nullable The new column's nullable.+ * @param autoIncrement The new column's autoIncrement.+ * @return A TableChange for the addition.+ */+ static TableChange addColumn(+ String[] fieldName,+ Type dataType,+ String comment,+ ColumnPosition position,+ boolean nullable,+ boolean autoIncrement) {+ return new AddColumn(fieldName, dataType, comment, position, nullable, autoIncrement);}/**@@ -309,6 +334,17 @@ public interface TableChange {return new DeleteIndex(name, ifExists);}+ /**+ * Create a TableChange for updating the autoIncrement of a field.+ *+ * @param fieldName The field name of the column to update.+ * @param autoIncrement Whether the column is auto-incremented.+ * @return A TableChange for the update.+ */+ static TableChange updateColumnAutoIncrement(String[] fieldName, boolean autoIncrement) {+ return new UpdateColumnAutoIncrement(fieldName, autoIncrement);+ }+/** A TableChange to rename a table. */final class RenameTable implements TableChange {private final String newName;@@ -793,17 +829,21 @@ public interface TableChange {private final ColumnPosition position;private final boolean nullable;+ private final boolean autoIncrement;+private AddColumn(String[] fieldName,Type dataType,String comment,ColumnPosition position,- boolean nullable) {+ boolean nullable,+ boolean autoIncrement) {this.fieldName = fieldName;this.dataType = dataType;this.comment = comment;this.position = position == null ? ColumnPosition.defaultPos() : position;this.nullable = nullable;+ this.autoIncrement = autoIncrement;}/**@@ -851,6 +891,15 @@ public interface TableChange {return nullable;}+ /**+ * Checks if the new column is autoIncrement.+ *+ * @return true if the column is autoIncrement; false otherwise.+ */+ public boolean isAutoIncrement() {+ return autoIncrement;+ }+/*** Compares this AddColumn instance with another object for equality. The comparison is based on* the field name, data type, comment, position, and nullability.@@ -864,6 +913,7 @@ public interface TableChange {if (o == null || getClass() != o.getClass()) return false;AddColumn addColumn = (AddColumn) o;return nullable == addColumn.nullable+ && autoIncrement == addColumn.autoIncrement&& Arrays.equals(fieldName, addColumn.fieldName)&& Objects.equals(dataType, addColumn.dataType)&& Objects.equals(comment, addColumn.comment)@@ -872,13 +922,13 @@ public interface TableChange {/*** Generates a hash code for this AddColumn instance. This hash code is based on the field name,- * data type, comment, position, and nullability.+ * data type, comment, position, nullability, and autoIncrement.** @return A hash code value for this column addition operation.*/@Overridepublic int hashCode() {- int result = Objects.hash(dataType, comment, position, nullable);+ int result = Objects.hash(dataType, comment, position, nullable, autoIncrement);result = 31 * result + Arrays.hashCode(fieldName);return result;}@@ -1285,4 +1335,59 @@ public interface TableChange {return result;}}++ /**+ * A TableChange to update the autoIncrement of a field. True is to add autoIncrement, false is to+ * delete autoIncrement.+ */+ final class UpdateColumnAutoIncrement implements ColumnChange {+ private final String[] fieldName;++ private final boolean autoIncrement;++ /**+ * Creates a new UpdateColumnAutoIncrement instance.+ *+ * @param fieldName The name of the field to be updated.+ * @param autoIncrement The new autoIncrement flag of the field.+ */+ public UpdateColumnAutoIncrement(String[] fieldName, boolean autoIncrement) {+ this.fieldName = fieldName;+ this.autoIncrement = autoIncrement;+ }++ /**+ * Retrieves the field name of the column whose autoIncrement is being updated.+ *+ * @return An array of strings representing the field name.+ */+ @Override+ public String[] fieldName() {+ return fieldName;+ }++ /**+ * The autoIncrement flag of the column.+ *+ * @return true if the column is autoIncrement; false otherwise.+ */+ public boolean isAutoIncrement() {+ return autoIncrement;+ }++ @Override+ public boolean equals(Object o) {+ if (this == o) return true;+ if (o == null || getClass() != o.getClass()) return false;+ UpdateColumnAutoIncrement that = (UpdateColumnAutoIncrement) o;+ return autoIncrement == that.autoIncrement && Arrays.equals(fieldName, that.fieldName);+ }++ @Override+ public int hashCode() {+ int result = Objects.hash(autoIncrement);+ result = 31 * result + Arrays.hashCode(fieldName);+ return result;+ }+ }}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.javaindex df193a5c64..efe7609165 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@@ -810,6 +810,9 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas} else if (change instanceof TableChange.UpdateColumnType) {doUpdateColumnType(cols, (TableChange.UpdateColumnType) change);+ } else if (change instanceof TableChange.UpdateColumnAutoIncrement) {+ throw new IllegalArgumentException(+ "Hive does not support altering column auto increment");} else {throw new IllegalArgumentException("Unsupported column change type: " + change.getClass().getSimpleName());@@ -931,6 +934,9 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemasprivate void doAddColumn(List<FieldSchema> cols, TableChange.AddColumn change) {int targetPosition;+ if (change.isAutoIncrement()) {+ throw new IllegalArgumentException("Hive catalog does not support auto-increment column");+ }if (change.getPosition() instanceof TableChange.Default) {// add to the end by defaulttargetPosition = cols.size();diff --git a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.javaindex 674b3ac963..e103828f29 100644--- a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java+++ b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlTableOperations.java@@ -20,6 +20,7 @@ import com.datastrato.gravitino.rel.TableChange;import com.datastrato.gravitino.rel.expressions.transforms.Transform;import com.datastrato.gravitino.rel.indexes.Index;import com.datastrato.gravitino.rel.indexes.Indexes;+import com.datastrato.gravitino.rel.types.Types;import com.google.common.annotations.VisibleForTesting;import com.google.common.base.Preconditions;import java.sql.Connection;@@ -328,6 +329,11 @@ public class MysqlTableOperations extends JdbcTableOperations {} else if (change instanceof TableChange.DeleteIndex) {lazyLoadTable = getOrCreateTable(databaseName, tableName, lazyLoadTable);alterSql.add(deleteIndexDefinition(lazyLoadTable, (TableChange.DeleteIndex) change));+ } else if (change instanceof TableChange.UpdateColumnAutoIncrement) {+ lazyLoadTable = getOrCreateTable(databaseName, tableName, lazyLoadTable);+ alterSql.add(+ updateColumnAutoIncrementDefinition(+ lazyLoadTable, (TableChange.UpdateColumnAutoIncrement) change));} else {throw new IllegalArgumentException("Unsupported table change type: " + change.getClass().getName());@@ -364,6 +370,34 @@ public class MysqlTableOperations extends JdbcTableOperations {return result;}+ private String updateColumnAutoIncrementDefinition(+ JdbcTable table, TableChange.UpdateColumnAutoIncrement change) {+ if (change.fieldName().length > 1) {+ throw new UnsupportedOperationException("Nested column names are not supported");+ }+ String col = change.fieldName()[0];+ JdbcColumn column = getJdbcColumnFromTable(table, col);+ if (change.isAutoIncrement()) {+ Preconditions.checkArgument(+ Types.allowAutoIncrement(column.dataType()),+ "Auto increment is not allowed, type: " + column.dataType());+ }+ JdbcColumn updateColumn =+ new JdbcColumn.Builder()+ .withName(col)+ .withDefaultValue(column.defaultValue())+ .withNullable(column.nullable())+ .withType(column.dataType())+ .withComment(column.comment())+ .withAutoIncrement(change.isAutoIncrement())+ .build();+ return MODIFY_COLUMN+ + BACK_QUOTE+ + col+ + BACK_QUOTE+ + appendColumnDefinition(updateColumn, new StringBuilder());+ }+@VisibleForTestingstatic String deleteIndexDefinition(JdbcTable lazyLoadTable, TableChange.DeleteIndex deleteIndex) {@@ -478,6 +512,13 @@ public class MysqlTableOperations extends JdbcTableOperations {.append(dataType).append(SPACE);+ if (addColumn.isAutoIncrement()) {+ Preconditions.checkArgument(+ Types.allowAutoIncrement(addColumn.getDataType()),+ "Auto increment is not allowed, type: " + addColumn.getDataType());+ columnDefinition.append(MYSQL_AUTO_INCREMENT).append(SPACE);+ }+if (!addColumn.isNullable()) {columnDefinition.append("NOT NULL ");}diff --git a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.javaindex 11905628c9..1845cdfc1c 100644--- a/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java+++ b/catalogs/catalog-jdbc-postgresql/src/main/java/com/datastrato/gravitino/catalog/postgresql/operation/PostgreSqlTableOperations.java@@ -277,6 +277,10 @@ public class PostgreSqlTableOperations extends JdbcTableOperations {alterSql.add(addIndexDefinition(tableName, (TableChange.AddIndex) change));} else if (change instanceof TableChange.DeleteIndex) {alterSql.add(deleteIndexDefinition(tableName, (TableChange.DeleteIndex) change));+ } else if (change instanceof TableChange.UpdateColumnAutoIncrement) {+ alterSql.add(+ updateColumnAutoIncrementDefinition(+ (TableChange.UpdateColumnAutoIncrement) change, tableName));} else {throw new IllegalArgumentException("Unsupported table change type: " + change.getClass().getName());@@ -294,6 +298,21 @@ public class PostgreSqlTableOperations extends JdbcTableOperations {return result;}+ @VisibleForTesting+ static String updateColumnAutoIncrementDefinition(+ TableChange.UpdateColumnAutoIncrement change, String tableName) {+ if (change.fieldName().length > 1) {+ throw new UnsupportedOperationException(POSTGRESQL_NOT_SUPPORT_NESTED_COLUMN_MSG);+ }+ String fieldName = change.fieldName()[0];+ String action =+ change.isAutoIncrement() ? "ADD GENERATED BY DEFAULT AS IDENTITY" : "DROP IDENTITY";++ return String.format(+ "ALTER TABLE %s %s %s %s;",+ PG_QUOTE + tableName + PG_QUOTE, ALTER_COLUMN, PG_QUOTE + fieldName + PG_QUOTE, action);+ }+@VisibleForTestingstatic String deleteIndexDefinition(String tableName, TableChange.DeleteIndex deleteIndex) {StringBuilder sqlBuilder = new StringBuilder();@@ -508,6 +527,17 @@ public class PostgreSqlTableOperations extends JdbcTableOperations {.append(typeConverter.fromGravitinoType(addColumn.getDataType())).append(SPACE);+ if (addColumn.isAutoIncrement()) {+ if (!Types.allowAutoIncrement(addColumn.getDataType())) {+ throw new IllegalArgumentException(+ "Unsupported auto-increment , column: "+ + Arrays.toString(addColumn.getFieldName())+ + ", type: "+ + addColumn.getDataType());+ }+ columnDefinition.append("GENERATED BY DEFAULT AS IDENTITY ");+ }+// Add NOT NULL if the column is marked as suchif (!addColumn.isNullable()) {columnDefinition.append("NOT NULL ");diff --git a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlTableOperations.java b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlTableOperations.javaindex 0db95e3dfd..a624d502fa 100644--- a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlTableOperations.java+++ b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/operation/TestPostgreSqlTableOperations.java@@ -97,4 +97,24 @@ public class TestPostgreSqlTableOperations {+ "DROP INDEX IF EXISTS \"test_2_pk\";",result);}++ @Test+ void testUpdateColumnAutoIncrementDefinition() {+ TableChange.UpdateColumnAutoIncrement updateColumnAutoIncrement =+ new TableChange.UpdateColumnAutoIncrement(new String[] {"col_1"}, true);+ String sql =+ PostgreSqlTableOperations.updateColumnAutoIncrementDefinition(+ updateColumnAutoIncrement, "table_test");+ Assertions.assertEquals(+ "ALTER TABLE \"table_test\" ALTER COLUMN \"col_1\" ADD GENERATED BY DEFAULT AS IDENTITY;",+ sql);++ updateColumnAutoIncrement =+ new TableChange.UpdateColumnAutoIncrement(new String[] {"col_2"}, false);+ sql =+ PostgreSqlTableOperations.updateColumnAutoIncrementDefinition(+ updateColumnAutoIncrement, "table_test");+ Assertions.assertEquals(+ "ALTER TABLE \"table_test\" ALTER COLUMN \"col_2\" DROP IDENTITY;", sql);+ }}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.javaindex 9e678f67a2..985a1a25ab 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@@ -191,6 +191,10 @@ public class IcebergTableOpsHelper {parentStruct = icebergTableSchema.asStruct();}+ if (addColumn.isAutoIncrement()) {+ throw new IllegalArgumentException("Iceberg doesn't support auto increment column");+ }+if (addColumn.isNullable()) {icebergUpdateSchema.addColumn(getParentName(addColumn.fieldName()),@@ -248,6 +252,8 @@ public class IcebergTableOpsHelper {} else if (change instanceof TableChange.UpdateColumnNullability) {doUpdateColumnNullability(icebergUpdateSchema, (TableChange.UpdateColumnNullability) change);+ } else if (change instanceof TableChange.UpdateColumnAutoIncrement) {+ throw new IllegalArgumentException("Iceberg doesn't support auto increment column");} else {throw new NotSupportedException("Iceberg doesn't support " + change.getClass().getSimpleName() + " for now");diff --git a/clients/client-java/src/main/java/com/datastrato/gravitino/client/DTOConverters.java b/clients/client-java/src/main/java/com/datastrato/gravitino/client/DTOConverters.javaindex 4ce7f98200..30df665782 100644--- a/clients/client-java/src/main/java/com/datastrato/gravitino/client/DTOConverters.java+++ b/clients/client-java/src/main/java/com/datastrato/gravitino/client/DTOConverters.java@@ -190,7 +190,8 @@ class DTOConverters {addColumn.getDataType(),addColumn.getComment(),addColumn.getPosition(),- addColumn.isNullable());+ addColumn.isNullable(),+ addColumn.isAutoIncrement());} else if (change instanceof TableChange.RenameColumn) {TableChange.RenameColumn renameColumn = (TableChange.RenameColumn) change;@@ -216,6 +217,9 @@ class DTOConverters {} else if (change instanceof TableChange.UpdateColumnNullability) {return new TableUpdateRequest.UpdateTableColumnNullabilityRequest(change.fieldName(), ((TableChange.UpdateColumnNullability) change).nullable());+ } else if (change instanceof TableChange.UpdateColumnAutoIncrement) {+ return new TableUpdateRequest.UpdateColumnAutoIncrementRequest(+ change.fieldName(), ((TableChange.UpdateColumnAutoIncrement) change).isAutoIncrement());} else {throw new IllegalArgumentException("Unknown column change type: " + change.getClass().getSimpleName());diff --git a/clients/client-java/src/test/java/com/datastrato/gravitino/client/TestRelationalCatalog.java b/clients/client-java/src/test/java/com/datastrato/gravitino/client/TestRelationalCatalog.javaindex 85a22c2643..8f64b678ce 100644--- a/clients/client-java/src/test/java/com/datastrato/gravitino/client/TestRelationalCatalog.java+++ b/clients/client-java/src/test/java/com/datastrato/gravitino/client/TestRelationalCatalog.java@@ -807,6 +807,7 @@ public class TestRelationalCatalog extends TestBase {Types.StringType.get(),"comment2",TableChange.ColumnPosition.after("col1"),+ false,false);testAlterTable(tableId, req, expectedTable);diff --git a/common/src/main/java/com/datastrato/gravitino/dto/requests/TableUpdateRequest.java b/common/src/main/java/com/datastrato/gravitino/dto/requests/TableUpdateRequest.javaindex fdec6e496e..b0d6887a2d 100644--- a/common/src/main/java/com/datastrato/gravitino/dto/requests/TableUpdateRequest.java+++ b/common/src/main/java/com/datastrato/gravitino/dto/requests/TableUpdateRequest.java@@ -60,7 +60,10 @@ import org.apache.commons.lang3.StringUtils;@JsonSubTypes.Type(value = TableUpdateRequest.AddTableIndexRequest.class, name = "addTableIndex"),@JsonSubTypes.Type(value = TableUpdateRequest.DeleteTableIndexRequest.class,- name = "deleteTableIndex")+ name = "deleteTableIndex"),+ @JsonSubTypes.Type(+ value = TableUpdateRequest.UpdateColumnAutoIncrementRequest.class,+ name = "updateColumnAutoIncrement")})public interface TableUpdateRequest extends RESTRequest {@@ -286,9 +289,13 @@ public interface TableUpdateRequest extends RESTRequest {@JsonProperty(value = "nullable", defaultValue = "true")private final boolean nullable;+ @Getter+ @JsonProperty(value = "autoIncrement", defaultValue = "false")+ private final boolean autoIncrement;+/** Default constructor for Jackson deserialization. */public AddTableColumnRequest() {- this(null, null, null, null, true);+ this(null, null, null, null, true, false);}/**@@ -299,18 +306,21 @@ public interface TableUpdateRequest extends RESTRequest {* @param comment the comment of the field to add* @param position the position of the field to add, null for default position* @param nullable whether the field to add is nullable+ * @param autoIncrement whether the field to add is auto increment*/public AddTableColumnRequest(String[] fieldName,Type dataType,String comment,TableChange.ColumnPosition position,- boolean nullable) {+ boolean nullable,+ boolean autoIncrement) {this.fieldName = fieldName;this.dataType = dataType;this.comment = comment;this.position = position == null ? TableChange.ColumnPosition.defaultPos() : position;this.nullable = nullable;+ this.autoIncrement = autoIncrement;}/**@@ -323,7 +333,7 @@ public interface TableUpdateRequest extends RESTRequest {*/public AddTableColumnRequest(String[] fieldName, Type dataType, String comment, TableChange.ColumnPosition position) {- this(fieldName, dataType, comment, position, true);+ this(fieldName, dataType, comment, position, true, false);}/**@@ -356,7 +366,7 @@ public interface TableUpdateRequest extends RESTRequest {/** @return An instance of TableChange. */@Overridepublic TableChange tableChange() {- return TableChange.addColumn(fieldName, dataType, comment, position, nullable);+ return TableChange.addColumn(fieldName, dataType, comment, position, nullable, autoIncrement);}}@@ -765,4 +775,54 @@ public interface TableUpdateRequest extends RESTRequest {return TableChange.deleteIndex(name, ifExists);}}++ /** Represents a request to update a column autoIncrement from a table. */+ @EqualsAndHashCode+ @ToString+ class UpdateColumnAutoIncrementRequest implements TableUpdateRequest {++ @Getter+ @JsonProperty("fieldName")+ private final String[] fieldName;++ @Getter+ @JsonProperty("autoIncrement")+ private final boolean autoIncrement;++ /**+ * Constructor for UpdateColumnAutoIncrementRequest.+ *+ * @param fieldName the field name to update.+ * @param autoIncrement Whether the column is auto-incremented.+ */+ public UpdateColumnAutoIncrementRequest(String[] fieldName, boolean autoIncrement) {+ this.fieldName = fieldName;+ this.autoIncrement = autoIncrement;+ }++ /** Default constructor for Jackson deserialization. */+ public UpdateColumnAutoIncrementRequest() {+ this(null, false);+ }++ /**+ * Validates the request.+ *+ * @throws IllegalArgumentException If the request is invalid, this exception is thrown.+ */+ @Override+ public void validate() throws IllegalArgumentException {+ Preconditions.checkArgument(+ fieldName != null+ && fieldName.length > 0+ && Arrays.stream(fieldName).allMatch(StringUtils::isNotBlank),+ "\"fieldName\" field is required and cannot be empty");+ }++ /** @return An instance of TableChange. */+ @Override+ public TableChange tableChange() {+ return TableChange.updateColumnAutoIncrement(fieldName, autoIncrement);+ }+ }}diff --git a/common/src/test/java/com/datastrato/gravitino/dto/requests/TestTableUpdatesRequest.java b/common/src/test/java/com/datastrato/gravitino/dto/requests/TestTableUpdatesRequest.javaindex 9ff8584533..82c517350d 100644--- a/common/src/test/java/com/datastrato/gravitino/dto/requests/TestTableUpdatesRequest.java+++ b/common/src/test/java/com/datastrato/gravitino/dto/requests/TestTableUpdatesRequest.java@@ -122,6 +122,7 @@ public class TestTableUpdatesRequest {Types.StringType.get(),"comment",TableChange.ColumnPosition.after("afterColumn"),+ false,false);String jsonString = JsonUtils.objectMapper().writeValueAsString(addTableColumnRequest);String expected =@@ -135,7 +136,8 @@ public class TestTableUpdatesRequest {+ " \"position\": {\n"+ " \"after\": \"afterColumn\"\n"+ " },\n"- + " \"nullable\": false\n"+ + " \"nullable\": false,\n"+ + " \"autoIncrement\": false\n"+ "}";Assertions.assertEquals(JsonUtils.objectMapper().readTree(expected), JsonUtils.objectMapper().readTree(jsonString));@@ -157,7 +159,8 @@ public class TestTableUpdatesRequest {+ " \"type\": \"string\",\n"+ " \"comment\": \"test default nullability\",\n"+ " \"position\": \"first\",\n"- + " \"nullable\": true\n"+ + " \"nullable\": true,\n"+ + " \"autoIncrement\": false\n"+ "}";Assertions.assertEquals(JsonUtils.objectMapper().readTree(expected), JsonUtils.objectMapper().readTree(jsonString));@@ -183,7 +186,8 @@ public class TestTableUpdatesRequest {+ " \"type\": \"string\",\n"+ " \"comment\": \"test default position\",\n"+ " \"position\": \"default\",\n"- + " \"nullable\": true\n"+ + " \"nullable\": true,\n"+ + " \"autoIncrement\": false\n"+ "}";Assertions.assertEquals(JsonUtils.objectMapper().readTree(expected), JsonUtils.objectMapper().readTree(jsonString));diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.javaindex 7160b967b1..d09e4d84be 100644--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java@@ -1382,4 +1382,91 @@ public class CatalogMysqlIT extends AbstractIT {assertionsTableInfo(tableName, table_comment, Arrays.asList(newColumns), createProperties(), indexes, table);}++ @Test+ void testAddColumnAutoIncrement() {+ Column col1 = Column.of("col_1", Types.LongType.get(), "uid", false, false, null);+ Column col2 = Column.of("col_2", Types.ByteType.get(), "yes", false, false, null);+ Column col3 = Column.of("col_3", Types.DateType.get(), "comment", false, false, null);+ Column col4 = Column.of("col_4", Types.VarCharType.of(255), "code", false, false, null);+ Column col5 = Column.of("col_5", Types.VarCharType.of(255), "config", false, false, null);+ String tableName = "auto_increment_table";+ Column[] newColumns = new Column[] {col1, col2, col3, col4, col5};++ NameIdentifier tableIdentifier =+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);+ Map<String, String> properties = createProperties();+ TableCatalog tableCatalog = catalog.asTableCatalog();+ tableCatalog.createTable(+ tableIdentifier,+ newColumns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0],+ Indexes.EMPTY_INDEXES);++ // Test add auto increment ,but not insert index. will failed.+ RuntimeException runtimeException =+ assertThrows(+ RuntimeException.class,+ () ->+ tableCatalog.alterTable(+ tableIdentifier,+ TableChange.addColumn(+ new String[] {"col_6"},+ Types.LongType.get(),+ "id",+ TableChange.ColumnPosition.defaultPos(),+ false,+ true)));+ Assertions.assertTrue(+ StringUtils.contains(+ runtimeException.getMessage(),+ "Incorrect table definition; there can be only one auto column and it must be defined as a key"));++ // Test add auto increment success.+ tableCatalog.alterTable(+ tableIdentifier,+ TableChange.addColumn(+ new String[] {"col_6"},+ Types.LongType.get(),+ "id",+ TableChange.ColumnPosition.defaultPos(),+ false,+ true),+ TableChange.addIndex(+ Index.IndexType.PRIMARY_KEY,+ Indexes.DEFAULT_MYSQL_PRIMARY_KEY_NAME,+ new String[][] {{"col_6"}}));++ Table table = tableCatalog.loadTable(tableIdentifier);++ Column col6 = Column.of("col_6", Types.LongType.get(), "id", false, true, null);+ Index[] indices = new Index[] {Indexes.createMysqlPrimaryKey(new String[][] {{"col_6"}})};+ newColumns = new Column[] {col1, col2, col3, col4, col5, col6};+ assertionsTableInfo(+ tableName, table_comment, Arrays.asList(newColumns), properties, indices, table);++ // Test the auto-increment property of modified fields+ tableCatalog.alterTable(+ tableIdentifier, TableChange.updateColumnAutoIncrement(new String[] {"col_6"}, false));+ table = tableCatalog.loadTable(tableIdentifier);+ col6 = Column.of("col_6", Types.LongType.get(), "id", false, false, null);+ indices = new Index[] {Indexes.createMysqlPrimaryKey(new String[][] {{"col_6"}})};+ newColumns = new Column[] {col1, col2, col3, col4, col5, col6};+ assertionsTableInfo(+ tableName, table_comment, Arrays.asList(newColumns), properties, indices, table);++ // Add the auto-increment attribute to the field+ tableCatalog.alterTable(+ tableIdentifier, TableChange.updateColumnAutoIncrement(new String[] {"col_6"}, true));+ table = tableCatalog.loadTable(tableIdentifier);+ col6 = Column.of("col_6", Types.LongType.get(), "id", false, true, null);+ indices = new Index[] {Indexes.createMysqlPrimaryKey(new String[][] {{"col_6"}})};+ newColumns = new Column[] {col1, col2, col3, col4, col5, col6};+ assertionsTableInfo(+ tableName, table_comment, Arrays.asList(newColumns), properties, indices, table);+ }}diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.javaindex 16677a1324..862ba38743 100644--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/postgresql/CatalogPostgreSqlIT.java@@ -1280,4 +1280,77 @@ public class CatalogPostgreSqlIT extends AbstractIT {assertionsTableInfo(tableName, table_comment, Arrays.asList(newColumns), createProperties(), indexes, table);}++ @Test+ void testAddColumnAutoIncrement() {+ Column col1 = Column.of("col_1", Types.LongType.get(), "uid", false, false, null);+ Column col2 = Column.of("col_2", Types.DateType.get(), "comment", false, false, null);+ Column col3 = Column.of("col_3", Types.VarCharType.of(255), "code", false, false, null);+ Column col4 = Column.of("col_4", Types.VarCharType.of(255), "config", false, false, null);+ Column[] newColumns = new Column[] {col1, col2, col3, col4};+ String tableName = "auto_increment_table";++ NameIdentifier tableIdentifier =+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName);+ Map<String, String> properties = createProperties();+ TableCatalog tableCatalog = catalog.asTableCatalog();+ tableCatalog.createTable(+ tableIdentifier,+ newColumns,+ table_comment,+ properties,+ Transforms.EMPTY_TRANSFORM,+ Distributions.NONE,+ new SortOrder[0],+ Indexes.EMPTY_INDEXES);+ tableCatalog.alterTable(+ tableIdentifier,+ TableChange.addColumn(+ new String[] {"col_5"},+ Types.LongType.get(),+ "id",+ TableChange.ColumnPosition.defaultPos(),+ false,+ true));++ Table table = tableCatalog.loadTable(tableIdentifier);++ Column col5 = Column.of("col_5", Types.LongType.get(), "id", false, true, null);+ newColumns = new Column[] {col1, col2, col3, col4, col5};+ assertionsTableInfo(+ tableName,+ table_comment,+ Arrays.asList(newColumns),+ properties,+ Indexes.EMPTY_INDEXES,+ table);++ // Test drop auto increment column+ tableCatalog.alterTable(+ tableIdentifier, TableChange.updateColumnAutoIncrement(new String[] {"col_5"}, false));+ table = tableCatalog.loadTable(tableIdentifier);+ col5 = Column.of("col_5", Types.LongType.get(), "id", false, false, null);+ newColumns = new Column[] {col1, col2, col3, col4, col5};+ assertionsTableInfo(+ tableName,+ table_comment,+ Arrays.asList(newColumns),+ properties,+ Indexes.EMPTY_INDEXES,+ table);++ // Test add auto increment column+ tableCatalog.alterTable(+ tableIdentifier, TableChange.updateColumnAutoIncrement(new String[] {"col_5"}, true));+ table = tableCatalog.loadTable(tableIdentifier);+ col5 = Column.of("col_5", Types.LongType.get(), "id", false, true, null);+ newColumns = new Column[] {col1, col2, col3, col4, col5};+ assertionsTableInfo(+ tableName,+ table_comment,+ Arrays.asList(newColumns),+ properties,+ Indexes.EMPTY_INDEXES,+ table);+ }}diff --git a/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestTableOperations.java b/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestTableOperations.javaindex c2dd309d99..931ff10256 100644--- a/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestTableOperations.java+++ b/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestTableOperations.java@@ -549,6 +549,7 @@ public class TestTableOperations extends JerseyTest {Types.StringType.get(),"mock comment",TableChange.ColumnPosition.first(),+ false,false);Column[] columns =new Column[] {@@ -569,7 +570,8 @@ public class TestTableOperations extends JerseyTest {Types.StringType.get(),"mock comment",TableChange.ColumnPosition.after("col2"),- true);+ true,+ false);Column[] columns =new Column[] {mockColumn("col1", Types.StringType.get()),@@ -665,6 +667,19 @@ public class TestTableOperations extends JerseyTest {testAlterTableRequest(req, table);}+ @Test+ public void testUpdateColumnAutoIncrement() {+ TableUpdateRequest.UpdateColumnAutoIncrementRequest req =+ new TableUpdateRequest.UpdateColumnAutoIncrementRequest(new String[] {"test"}, false);+ Column[] columns =+ new Column[] {+ mockColumn("col2", Types.ByteType.get()), mockColumn("col1", Types.StringType.get())+ };+ Table table =+ mockTable("table1", columns, "mock comment", ImmutableMap.of("k1", "v1"), new Transform[0]);+ testAlterTableRequest(req, table);+ }+@Testpublic void testDropTable() {when(dispatcher.dropTable(any())).thenReturn(true);