This is an automated email from the ASF dual-hosted git repository.
diqiu50 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 997a61ef0b [#13978] fix(catalog-jdbc): Keep column default value when
updating column type (#13979)
997a61ef0b is described below
commit 997a61ef0bef83418ea368bbfba3390c490cf1d9
Author: Yuhui <[email protected]>
AuthorDate: Wed Oct 21 14:34:08 2026 +0800
[#13978] fix(catalog-jdbc): Keep column default value when updating column
type (#13979)
### What changes were proposed in this pull request?
Keep the existing default value when rebuilding the column in
`updateColumnTypeFieldDefinition` for MySQL, OceanBase and StarRocks,
consistent with Doris. Add a test for each.
### Why are the changes needed?
`MODIFY COLUMN` replaces the whole column definition, so rebuilding it
with `DEFAULT_VALUE_NOT_SET` silently drops the default value.
Fix: #13978
### Does this PR introduce _any_ user-facing change?
Changing a column type now keeps its default value. If the old default
is not valid for the new type, the database now rejects the change
instead of silently dropping the default.
### How was this patch tested?
Added `testAlterTableUpdateColumnTypeKeepsDefaultValue` to the MySQL,
OceanBase and StarRocks table operations tests (Docker). All three pass
locally. The MySQL test fails without the fix.
---
.../operation/OceanBaseTableOperations.java | 3 +-
.../operation/TestOceanBaseTableOperations.java | 33 +++++++++++++++++
.../mysql/operation/MysqlTableOperations.java | 3 +-
.../mysql/operation/TestMysqlTableOperations.java | 33 +++++++++++++++++
.../operations/StarRocksTableOperations.java | 3 +-
.../operation/TestStarRocksTableOperations.java | 41 ++++++++++++++++++++++
6 files changed, 110 insertions(+), 6 deletions(-)
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.java
index 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.java
index 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());
+ }
+
@Test
public 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.java
index 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.JdbcColumnDefaultValue
import 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.java
index 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());
+ }
+
@Test
public 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.java
index 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.java
index 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());
+ });
+ }
+
@Test
public void testAlterTable() {
String tableName =
GravitinoITUtils.genRandomName("starrocks_alter_test_table");