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 869bdc92b6 [#10365] fix(hologres): Add GUC parameter for DROP COLUMN 
operation (#10366)
869bdc92b6 is described below

commit 869bdc92b6a3179e08eb5a4513835b9a9e13cefe
Author: Ye Ding <[email protected]>
AuthorDate: Thu Mar 12 15:37:41 2026 +0800

    [#10365] fix(hologres): Add GUC parameter for DROP COLUMN operation (#10366)
    
    ## What changes were proposed in this pull request?
    
    This PR fixes the Hologres DROP COLUMN operation by adding the required
    session-level GUC parameter before executing the ALTER TABLE DROP COLUMN
    statement.
    
    Changes:
    1. Added constant `HOLOGRES_ENABLE_DROP_COLUMN_GUC` for the GUC
    parameter
    2. Modified `deleteColumnFieldDefinition()` method to prepend the GUC
    setting to the DROP COLUMN SQL
    3. Added unit tests for DROP COLUMN functionality:
       - `testAlterTableDeleteColumn` - basic DROP COLUMN test
       - `testAlterTableDeleteColumnIfExists` - IF EXISTS option test
       - `testAlterTableDeleteColumnNotExistsThrows` - error handling test
    - `testAlterTableDeleteColumnRejectsNestedField` - nested field
    rejection test
    
    The generated SQL now looks like:
    ```sql
    SET hg_experimental_enable_drop_column = on;
    ALTER TABLE "table_name" DROP COLUMN "column_name";
    ```
    
    ## Why are the changes needed?
    
    Hologres requires the `hg_experimental_enable_drop_column` GUC parameter
    to be enabled at the session level before executing DROP COLUMN
    operations. Without this setting, the operation fails.
    
    Reference:
    https://help.aliyun.com/zh/hologres/developer-reference/alter-table
    
    Fix: https://github.com/apache/gravitino/issues/10365
    
    ## Does this PR introduce _any_ user-facing change?
    
    No. The change is internal to the Hologres catalog implementation. Users
    can now successfully drop columns from Hologres tables without any API
    changes.
    
    ## How was this patch tested?
    
    - Added 4 new unit tests in `TestHologresTableOperations.java`
    - All existing tests pass
    - Tests verify:
      - Correct SQL generation with GUC parameter
      - IF EXISTS option behavior
      - Error handling for non-existent columns
      - Rejection of nested column names
    
    ---------
    
    Co-authored-by: Claude Opus 4.6 <[email protected]>
---
 .../operation/HologresTableOperations.java         | 13 +++-
 .../operation/TestHologresTableOperations.java     | 86 ++++++++++++++++++++++
 2 files changed, 97 insertions(+), 2 deletions(-)

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.java
index 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 
JdbcTableOperations
   private 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 
JdbcTableOperations
         throw 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-table
     return 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.java
index 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

Reply via email to