World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#10365] fix(hologres): Add GUC parameter for DROP COLUMN operation (#10366)
Ye Ding869bdc92b6a3
diff --git a/catalogs-contrib/catalog-jdbc-hologres/src/main/java/org/apache/gravitino/catalog/hologres/operation/HologresTableOperations.java b/catalogs-contrib/catalog-jdbc-hologres/src/main/java/org/apache/gravitino/catalog/hologres/operation/HologresTableOperations.javaindex 8b244880e0..c78037d291 100644--- a/catalogs-contrib/catalog-jdbc-hologres/src/main/java/org/apache/gravitino/catalog/hologres/operation/HologresTableOperations.java+++ b/catalogs-contrib/catalog-jdbc-hologres/src/main/java/org/apache/gravitino/catalog/hologres/operation/HologresTableOperations.java@@ -78,6 +78,10 @@ public class HologresTableOperations extends JdbcTableOperationsprivate static final String HOLOGRES_NOT_SUPPORT_NESTED_COLUMN_MSG ="Hologres does not support nested column names.";+ /** GUC parameter to enable DROP COLUMN functionality in Hologres. */+ private static final String HOLOGRES_ENABLE_DROP_COLUMN_GUC =+ "SET hg_experimental_enable_drop_column = on;";+/** Properties that are handled separately or read-only, excluded from the WITH clause. */private static final Set<String> EXCLUDED_TABLE_PROPERTIES =ImmutableSet.of("distribution_key", "is_logical_partitioned_table", "primary_key");@@ -531,9 +535,14 @@ public class HologresTableOperations extends JdbcTableOperationsthrow new IllegalArgumentException("Delete column does not exist: " + col);}}+ // Hologres requires enabling the GUC parameter before dropping a column.+ // Reference: https://help.aliyun.com/zh/hologres/developer-reference/alter-tablereturn String.format(- "%s%s DROP COLUMN %s;",- ALTER_TABLE, quoteIdentifier(table.name()), quoteIdentifier(deleteColumn.fieldName()[0]));+ "%s %s%s DROP COLUMN %s;",+ HOLOGRES_ENABLE_DROP_COLUMN_GUC,+ ALTER_TABLE,+ quoteIdentifier(table.name()),+ quoteIdentifier(deleteColumn.fieldName()[0]));}private String renameColumnFieldDefinition(diff --git a/catalogs-contrib/catalog-jdbc-hologres/src/test/java/org/apache/gravitino/catalog/hologres/operation/TestHologresTableOperations.java b/catalogs-contrib/catalog-jdbc-hologres/src/test/java/org/apache/gravitino/catalog/hologres/operation/TestHologresTableOperations.javaindex f39cb4d61e..99420710ee 100644--- a/catalogs-contrib/catalog-jdbc-hologres/src/test/java/org/apache/gravitino/catalog/hologres/operation/TestHologresTableOperations.java+++ b/catalogs-contrib/catalog-jdbc-hologres/src/test/java/org/apache/gravitino/catalog/hologres/operation/TestHologresTableOperations.java@@ -50,12 +50,27 @@ public class TestHologresTableOperations {// ==================== Helper inner class for SQL generation tests ====================private static class TestableHologresTableOperations extends HologresTableOperations {+ private JdbcTable mockTable;+public TestableHologresTableOperations() {super.exceptionMapper = new HologresExceptionConverter();super.typeConverter = new HologresTypeConverter();super.columnDefaultValueConverter = new HologresColumnDefaultValueConverter();}+ public void setMockTable(JdbcTable table) {+ this.mockTable = table;+ }++ @Override+ protected JdbcTable getOrCreateTable(+ String databaseName, String tableName, JdbcTable lazyLoadCreateTable) {+ if (mockTable != null) {+ return mockTable;+ }+ return lazyLoadCreateTable;+ }+public String createTableSql(String tableName,JdbcColumn[] columns,@@ -988,6 +1003,77 @@ public class TestHologresTableOperations {TableChange.updateColumnAutoIncrement(new String[] {"col1"}, true)));}+ // ==================== deleteColumn (DROP COLUMN) tests ====================++ @Test+ void testAlterTableDeleteColumn() {+ JdbcColumn col1 =+ JdbcColumn.builder()+ .withName("id")+ .withType(Types.IntegerType.get())+ .withNullable(false)+ .build();+ JdbcColumn col2 =+ JdbcColumn.builder()+ .withName("name")+ .withType(Types.StringType.get())+ .withNullable(true)+ .build();+ ops.setMockTable(ops.buildFakeTable("test_table", col1, col2));+ String sql =+ ops.alterTableSql(+ "public", "test_table", TableChange.deleteColumn(new String[] {"name"}, false));+ // Hologres requires GUC to enable DROP COLUMN+ assertTrue(sql.contains("SET hg_experimental_enable_drop_column = on;"));+ assertTrue(sql.contains("ALTER TABLE \"test_table\" DROP COLUMN \"name\""));+ }++ @Test+ void testAlterTableDeleteColumnIfExists() {+ JdbcColumn col1 =+ JdbcColumn.builder()+ .withName("id")+ .withType(Types.IntegerType.get())+ .withNullable(false)+ .build();+ ops.setMockTable(ops.buildFakeTable("test_table", col1));+ // When column doesn't exist and IF EXISTS is true, should return empty string+ String sql =+ ops.alterTableSql(+ "public", "test_table", TableChange.deleteColumn(new String[] {"non_existent"}, true));+ assertEquals("", sql);+ }++ @Test+ void testAlterTableDeleteColumnNotExistsThrows() {+ JdbcColumn col1 =+ JdbcColumn.builder()+ .withName("id")+ .withType(Types.IntegerType.get())+ .withNullable(false)+ .build();+ ops.setMockTable(ops.buildFakeTable("test_table", col1));+ // When column doesn't exist and IF EXISTS is false, should throw+ assertThrows(+ IllegalArgumentException.class,+ () ->+ ops.alterTableSql(+ "public",+ "test_table",+ TableChange.deleteColumn(new String[] {"non_existent"}, false)));+ }++ @Test+ void testAlterTableDeleteColumnRejectsNestedField() {+ assertThrows(+ UnsupportedOperationException.class,+ () ->+ ops.alterTableSql(+ "public",+ "test_table",+ TableChange.deleteColumn(new String[] {"schema", "col"}, false)));+ }+// ==================== quoteIdentifier tests ====================@Test