World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#13978] fix(catalog-jdbc): Keep column default value when updating column type (#13979)
Yuhui997a61ef0bef
diff --git a/catalogs-contrib/catalog-jdbc-oceanbase/src/main/java/org/apache/gravitino/catalog/oceanbase/operation/OceanBaseTableOperations.java b/catalogs-contrib/catalog-jdbc-oceanbase/src/main/java/org/apache/gravitino/catalog/oceanbase/operation/OceanBaseTableOperations.javaindex 411688a85a..b51544bfee 100644--- a/catalogs-contrib/catalog-jdbc-oceanbase/src/main/java/org/apache/gravitino/catalog/oceanbase/operation/OceanBaseTableOperations.java+++ b/catalogs-contrib/catalog-jdbc-oceanbase/src/main/java/org/apache/gravitino/catalog/oceanbase/operation/OceanBaseTableOperations.java@@ -19,7 +19,6 @@package org.apache.gravitino.catalog.oceanbase.operation;import static org.apache.gravitino.catalog.jdbc.utils.JdbcConnectorUtils.escapeSqlLiteral;-import static org.apache.gravitino.rel.Column.DEFAULT_VALUE_NOT_SET;import com.google.common.annotations.VisibleForTesting;import com.google.common.base.Preconditions;@@ -627,7 +626,7 @@ public class OceanBaseTableOperations extends JdbcTableOperations {.withName(col).withType(updateColumnType.getNewDataType()).withComment(column.comment())- .withDefaultValue(DEFAULT_VALUE_NOT_SET)+ .withDefaultValue(column.defaultValue()).withNullable(column.nullable()).withAutoIncrement(column.autoIncrement()).build();diff --git a/catalogs-contrib/catalog-jdbc-oceanbase/src/test/java/org/apache/gravitino/catalog/oceanbase/operation/TestOceanBaseTableOperations.java b/catalogs-contrib/catalog-jdbc-oceanbase/src/test/java/org/apache/gravitino/catalog/oceanbase/operation/TestOceanBaseTableOperations.javaindex a0d4b2f9c9..bc011f9d8e 100644--- a/catalogs-contrib/catalog-jdbc-oceanbase/src/test/java/org/apache/gravitino/catalog/oceanbase/operation/TestOceanBaseTableOperations.java+++ b/catalogs-contrib/catalog-jdbc-oceanbase/src/test/java/org/apache/gravitino/catalog/oceanbase/operation/TestOceanBaseTableOperations.java@@ -231,6 +231,39 @@ public class TestOceanBaseTableOperations extends TestOceanBase {TABLE_OPERATIONS.drop(TEST_DB_NAME, newName), "table should be non-existent");}+ @Test+ public void testAlterTableUpdateColumnTypeKeepsDefaultValue() {+ String tableName = RandomStringUtils.randomAlphabetic(16).toLowerCase() + "_type_default_table";+ JdbcColumn col_1 =+ JdbcColumn.builder().withName("col_1").withType(INT).withNullable(false).build();+ JdbcColumn col_2 =+ JdbcColumn.builder()+ .withName("col_2")+ .withType(Types.VarCharType.of(16))+ .withDefaultValue(Literals.of("new", Types.VarCharType.of(16)))+ .withNullable(false)+ .build();+ TABLE_OPERATIONS.create(+ TEST_DB_NAME.toString(),+ tableName,+ new JdbcColumn[] {col_1, col_2},+ "test_comment",+ new HashMap<>(),+ null,+ Distributions.NONE,+ new Index[0]);++ TABLE_OPERATIONS.alterTable(+ TEST_DB_NAME.toString(),+ tableName,+ TableChange.updateColumnType(new String[] {col_2.name()}, Types.VarCharType.of(32)));++ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);+ Column updated = load.columns()[1];+ Assertions.assertEquals(Types.VarCharType.of(32), updated.dataType());+ Assertions.assertEquals(Literals.of("new", Types.VarCharType.of(32)), updated.defaultValue());+ }+@Testpublic void testAlterTable() {String tableName = RandomStringUtils.randomAlphabetic(16).toLowerCase() + "_al_table";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 bb96ca6327..f54990874c 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@@ -22,7 +22,6 @@ import static org.apache.gravitino.catalog.jdbc.converter.JdbcColumnDefaultValueimport static org.apache.gravitino.catalog.jdbc.utils.JdbcConnectorUtils.escapeSqlLiteral;import static org.apache.gravitino.catalog.mysql.MysqlTablePropertiesMetadata.MYSQL_AUTO_INCREMENT_OFFSET_KEY;import static org.apache.gravitino.catalog.mysql.MysqlTablePropertiesMetadata.MYSQL_ENGINE_KEY;-import static org.apache.gravitino.rel.Column.DEFAULT_VALUE_NOT_SET;import com.google.common.annotations.VisibleForTesting;import com.google.common.base.Preconditions;@@ -794,7 +793,7 @@ public class MysqlTableOperations extends JdbcTableOperations {.withName(col).withType(updateColumnType.getNewDataType()).withComment(column.comment())- .withDefaultValue(DEFAULT_VALUE_NOT_SET)+ .withDefaultValue(column.defaultValue()).withNullable(column.nullable()).withAutoIncrement(column.autoIncrement()).build();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.javaindex d3416e7243..bfb9c3d449 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@@ -197,6 +197,39 @@ public class TestMysqlTableOperations extends TestMysql {TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), newName), "table should be non-existent");}+ @Test+ public void testAlterTableUpdateColumnTypeKeepsDefaultValue() {+ String tableName = RandomStringUtils.randomAlphabetic(16) + "_type_default_table";+ JdbcColumn col_1 =+ JdbcColumn.builder().withName("col_1").withType(INT).withNullable(false).build();+ JdbcColumn col_2 =+ JdbcColumn.builder()+ .withName("col_2")+ .withType(Types.VarCharType.of(16))+ .withDefaultValue(Literals.of("new", Types.VarCharType.of(16)))+ .withNullable(false)+ .build();+ TABLE_OPERATIONS.create(+ TEST_DB_NAME.toString(),+ tableName,+ new JdbcColumn[] {col_1, col_2},+ "test_comment",+ new HashMap<>(),+ null,+ Distributions.NONE,+ new Index[0]);++ TABLE_OPERATIONS.alterTable(+ TEST_DB_NAME.toString(),+ tableName,+ TableChange.updateColumnType(new String[] {col_2.name()}, Types.VarCharType.of(32)));++ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);+ Column updated = load.columns()[1];+ Assertions.assertEquals(Types.VarCharType.of(32), updated.dataType());+ Assertions.assertEquals(Literals.of("new", Types.VarCharType.of(32)), updated.defaultValue());+ }+@Testpublic void testAlterTable() {String tableName = RandomStringUtils.randomAlphabetic(16) + "_al_table";diff --git a/catalogs/catalog-jdbc-starrocks/src/main/java/org/apache/gravitino/catalog/starrocks/operations/StarRocksTableOperations.java b/catalogs/catalog-jdbc-starrocks/src/main/java/org/apache/gravitino/catalog/starrocks/operations/StarRocksTableOperations.javaindex c1a9e06bf2..4f5d009b3c 100644--- a/catalogs/catalog-jdbc-starrocks/src/main/java/org/apache/gravitino/catalog/starrocks/operations/StarRocksTableOperations.java+++ b/catalogs/catalog-jdbc-starrocks/src/main/java/org/apache/gravitino/catalog/starrocks/operations/StarRocksTableOperations.java@@ -19,7 +19,6 @@package org.apache.gravitino.catalog.starrocks.operations;import static org.apache.gravitino.catalog.jdbc.utils.JdbcConnectorUtils.escapeSqlLiteral;-import static org.apache.gravitino.rel.Column.DEFAULT_VALUE_NOT_SET;import com.google.common.base.Preconditions;import com.google.common.collect.ImmutableList;@@ -607,7 +606,7 @@ public class StarRocksTableOperations extends JdbcTableOperations {.withName(col).withType(updateColumnType.getNewDataType()).withComment(column.comment())- .withDefaultValue(DEFAULT_VALUE_NOT_SET)+ .withDefaultValue(column.defaultValue()).withNullable(column.nullable()).withAutoIncrement(column.autoIncrement()).build();diff --git a/catalogs/catalog-jdbc-starrocks/src/test/java/org/apache/gravitino/catalog/starrocks/operation/TestStarRocksTableOperations.java b/catalogs/catalog-jdbc-starrocks/src/test/java/org/apache/gravitino/catalog/starrocks/operation/TestStarRocksTableOperations.javaindex 5f531e7d55..de1824a309 100644--- a/catalogs/catalog-jdbc-starrocks/src/test/java/org/apache/gravitino/catalog/starrocks/operation/TestStarRocksTableOperations.java+++ b/catalogs/catalog-jdbc-starrocks/src/test/java/org/apache/gravitino/catalog/starrocks/operation/TestStarRocksTableOperations.java@@ -40,6 +40,7 @@ import org.apache.gravitino.catalog.starrocks.converter.StarRocksTypeConverter;import org.apache.gravitino.catalog.starrocks.operations.StarRocksTablePartitionOperations;import org.apache.gravitino.exceptions.GravitinoRuntimeException;import org.apache.gravitino.integration.test.util.GravitinoITUtils;+import org.apache.gravitino.rel.Column;import org.apache.gravitino.rel.TableChange;import org.apache.gravitino.rel.expressions.Expression;import org.apache.gravitino.rel.expressions.NamedReference;@@ -245,6 +246,46 @@ public class TestStarRocksTableOperations extends TestStarRocks {}}+ @Test+ public void testAlterTableUpdateColumnTypeKeepsDefaultValue() {+ String tableName = GravitinoITUtils.genRandomName("starrocks_type_default_test_table");+ JdbcColumn col_1 = JdbcColumn.builder().withName("col_1").withType(INT).build();+ // The wide col_2 keeps col_3 out of the default key columns, so col_3 is a value column.+ JdbcColumn col_2 = JdbcColumn.builder().withName("col_2").withType(VARCHAR_255).build();+ JdbcColumn col_3 =+ JdbcColumn.builder()+ .withName("col_3")+ .withType(Types.VarCharType.of(16))+ .withDefaultValue(Literals.of("new", Types.VarCharType.of(16)))+ .build();+ TABLE_OPERATIONS.create(+ databaseName,+ tableName,+ new JdbcColumn[] {col_1, col_2, col_3},+ "test_comment",+ createProperties(),+ null,+ Distributions.hash(DEFAULT_BUCKET_SIZE, NamedReference.field("col_1")),+ new Index[0]);++ TABLE_OPERATIONS.alterTable(+ databaseName,+ tableName,+ TableChange.updateColumnType(new String[] {col_3.name()}, Types.VarCharType.of(32)));++ // StarRocks applies schema changes asynchronously.+ Awaitility.await()+ .atMost(MAX_WAIT_IN_SECONDS, TimeUnit.SECONDS)+ .pollInterval(WAIT_INTERVAL_IN_SECONDS, TimeUnit.SECONDS)+ .untilAsserted(+ () -> {+ Column updated = TABLE_OPERATIONS.load(databaseName, tableName).columns()[2];+ Assertions.assertEquals(Types.VarCharType.of(32), updated.dataType());+ Assertions.assertEquals(+ Literals.of("new", Types.VarCharType.of(32)), updated.defaultValue());+ });+ }+@Testpublic void testAlterTable() {String tableName = GravitinoITUtils.genRandomName("starrocks_alter_test_table");