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 8cc6b01535 [#14929] fix(spark-connector): Let Paimon procedures that 
need the Paimon catalog run through Gravitino (#14930)
8cc6b01535 is described below

commit 8cc6b015352fd2b64b74d53ae4fc2c55ee6bc206
Author: COLDMANYU <[email protected]>
AuthorDate: Tue Nov 17 16:17:15 2026 +0800

    [#14929] fix(spark-connector): Let Paimon procedures that need the Paimon 
catalog run through Gravitino (#14930)
    
    ### What changes were proposed in this pull request?
    
    `GravitinoPaimonCatalog` implements Paimon's `WithPaimonCatalog`:
    
    - `paimonCatalog()` returns the Paimon catalog of the wrapped
    `SparkCatalog`.
    - `paimonCatalogName()` returns the name the wrapped catalog was
    initialized with. Newer Paimon versions declare this method in
    `WithPaimonCatalog`; it has no `@Override`, so the class compiles
    against both Paimon 1.2 and newer versions.
    
    ### Why are the changes needed?
    
    Paimon procedures that work on the catalog rather than on one loaded
    table (`remove_orphan_files`, `repair`, `compact_database`, ...) cast
    the catalog they are called on to `WithPaimonCatalog`, and failed with a
    `ClassCastException` through Gravitino.
    
    Fixes #14929
    
    ### Does this PR introduce _any_ user-facing change?
    
    These procedures can be called through a Gravitino Paimon catalog.
    
    ### How was this patch tested?
    
    - Unit test `TestGravitinoPaimonCatalogWithPaimonCatalog` checks the
    delegation.
    - New IT `testPaimonRemoveOrphanFilesProcedure` in
    `SparkPaimonCatalogIT` calls `remove_orphan_files` with `dry_run`.
    Without the change it fails with the `ClassCastException` in the issue.
    - `SparkPaimonCatalogFilesystemBackendIT35`: 37 tests, 32 passed, 5
    skipped.
---
 .../connector/paimon/GravitinoPaimonCatalog.java   | 23 ++++++-
 .../test/paimon/SparkPaimonCatalogIT.java          | 25 ++++++++
 ...estGravitinoPaimonCatalogWithPaimonCatalog.java | 73 ++++++++++++++++++++++
 3 files changed, 120 insertions(+), 1 deletion(-)

diff --git 
a/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/GravitinoPaimonCatalog.java
 
b/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/GravitinoPaimonCatalog.java
index 53f8860787..048023d71e 100644
--- 
a/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/GravitinoPaimonCatalog.java
+++ 
b/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/GravitinoPaimonCatalog.java
@@ -52,6 +52,7 @@ import org.apache.paimon.spark.SparkProcedures;
 import org.apache.paimon.spark.SparkTable;
 import org.apache.paimon.spark.analysis.NoSuchProcedureException;
 import org.apache.paimon.spark.catalog.ProcedureCatalog;
+import org.apache.paimon.spark.catalog.WithPaimonCatalog;
 import org.apache.paimon.spark.procedure.Procedure;
 import org.apache.paimon.spark.procedure.ProcedureBuilder;
 import org.apache.spark.sql.connector.catalog.Identifier;
@@ -67,7 +68,8 @@ import org.apache.spark.sql.types.StructField;
 import org.apache.spark.sql.types.StructType;
 import org.apache.spark.sql.util.CaseInsensitiveStringMap;
 
-public class GravitinoPaimonCatalog extends BaseCatalog implements 
ProcedureCatalog {
+public class GravitinoPaimonCatalog extends BaseCatalog
+    implements ProcedureCatalog, WithPaimonCatalog {
 
   private static final String PRIMARY_KEY_INDEX_NAME = 
"PAIMON_PRIMARY_KEY_INDEX";
 
@@ -211,6 +213,25 @@ public class GravitinoPaimonCatalog extends BaseCatalog 
implements ProcedureCata
         .purgeTable(NameIdentifier.of(getDatabase(ident), ident.name()));
   }
 
+  /**
+   * Exposes the underlying Paimon catalog. Paimon procedures that work on the 
catalog rather than
+   * on a single loaded table, such as {@code remove_orphan_files}, {@code 
repair} or {@code
+   * compact_database}, cast the catalog they are called on to {@link 
WithPaimonCatalog}.
+   */
+  @Override
+  public Catalog paimonCatalog() {
+    return ((WithPaimonCatalog) sparkCatalog).paimonCatalog();
+  }
+
+  /**
+   * The name of the underlying Paimon catalog. Newer Paimon versions declare 
this method in {@link
+   * WithPaimonCatalog} and use it when building Paimon identifiers; it is the 
catalog name the
+   * underlying Paimon catalog was initialized with.
+   */
+  public String paimonCatalogName() {
+    return sparkCatalog.name();
+  }
+
   /**
    * Procedures will validate the equality of the catalog registered to Spark 
catalogManager and the
    * catalog passed to {@code ProcedureBuilder} which invokes {@code 
loadProcedure()}. To meet the
diff --git 
a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogIT.java
 
b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogIT.java
index ebb941d64e..6a661c7646 100644
--- 
a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogIT.java
+++ 
b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogIT.java
@@ -219,6 +219,31 @@ public abstract class SparkPaimonCatalogIT extends 
SparkCommonIT {
     Assertions.assertFalse(result.isEmpty());
   }
 
+  @Test
+  void testPaimonRemoveOrphanFilesProcedure() {
+    // remove_orphan_files works on the Paimon catalog instead of a loaded 
table, so it needs the
+    // Gravitino catalog to expose the underlying Paimon catalog.
+    String tableName = "test_paimon_remove_orphan_files";
+    dropTableIfExists(tableName);
+    sql(
+        String.format(
+            "CREATE TABLE %s (id INT COMMENT 'id comment', name STRING COMMENT 
'', address STRING COMMENT '') USING paimon",
+            tableName));
+    sql(String.format("INSERT INTO %s VALUES(1, 'a', 'beijing')", tableName));
+
+    String fullTableName = String.format("%s.%s", getDefaultDatabase(), 
tableName);
+    List<Row> result =
+        getSparkSession()
+            .sql(
+                String.format(
+                    "CALL %s.sys.remove_orphan_files(table => '%s', dry_run => 
true)",
+                    getCatalogName(), fullTableName))
+            .collectAsList();
+    Assertions.assertEquals(1, result.size());
+    Assertions.assertEquals(0L, result.get(0).getLong(0));
+    Assertions.assertEquals(1, getTableData(tableName).size());
+  }
+
   @Test
   void testPaimonPartitionManagement() {
     testPaimonListAndDropPartition();
diff --git 
a/spark-connector/v3.5/spark/src/test/java/org/apache/gravitino/spark/connector/paimon/TestGravitinoPaimonCatalogWithPaimonCatalog.java
 
b/spark-connector/v3.5/spark/src/test/java/org/apache/gravitino/spark/connector/paimon/TestGravitinoPaimonCatalogWithPaimonCatalog.java
new file mode 100644
index 0000000000..d8eabfa1c7
--- /dev/null
+++ 
b/spark-connector/v3.5/spark/src/test/java/org/apache/gravitino/spark/connector/paimon/TestGravitinoPaimonCatalogWithPaimonCatalog.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.spark.connector.paimon;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.lang.reflect.Field;
+import org.apache.gravitino.client.GravitinoClient;
+import org.apache.gravitino.spark.connector.catalog.BaseCatalog;
+import org.apache.gravitino.spark.connector.catalog.GravitinoCatalogManager;
+import org.apache.paimon.catalog.Catalog;
+import org.apache.paimon.spark.SparkCatalog;
+import org.apache.paimon.spark.catalog.WithPaimonCatalog;
+import org.apache.spark.SparkConf;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+
+/**
+ * Tests that {@link GravitinoPaimonCatalog} exposes the Paimon catalog it 
wraps. Paimon procedures
+ * such as {@code remove_orphan_files} cast the catalog they run in to {@link 
WithPaimonCatalog}.
+ */
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+public class TestGravitinoPaimonCatalogWithPaimonCatalog {
+
+  @BeforeAll
+  void setUp() {
+    GravitinoCatalogManager.create(
+        new SparkConf(false), "user", identity -> mock(GravitinoClient.class));
+  }
+
+  @AfterAll
+  void tearDown() {
+    GravitinoCatalogManager.get().close();
+  }
+
+  @Test
+  void testDelegatesToTheUnderlyingPaimonCatalog() throws Exception {
+    Catalog paimonCatalog = mock(Catalog.class);
+    SparkCatalog sparkCatalog = mock(SparkCatalog.class);
+    when(sparkCatalog.paimonCatalog()).thenReturn(paimonCatalog);
+    when(sparkCatalog.name()).thenReturn("paimon_backend");
+
+    GravitinoPaimonCatalog catalog = new GravitinoPaimonCatalogSpark35();
+    Field field = BaseCatalog.class.getDeclaredField("sparkCatalog");
+    field.setAccessible(true);
+    field.set(catalog, sparkCatalog);
+
+    Assertions.assertInstanceOf(WithPaimonCatalog.class, catalog);
+    Assertions.assertSame(paimonCatalog, ((WithPaimonCatalog) 
catalog).paimonCatalog());
+    Assertions.assertEquals("paimon_backend", catalog.paimonCatalogName());
+  }
+}

Reply via email to