This is an automated email from the ASF dual-hosted git repository.
jerryshao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/branch-2.0 by this push:
new 1cb467a2e2 [Cherry-pick to branch-2.0] [#4733] fix(spark-connector):
Load Paimon format tables and test the Hive backend in the Spark connector
(#14928) (#15076)
1cb467a2e2 is described below
commit 1cb467a2e291a342319fbfeaa3c99c92bed634bf
Author: Jerry Shao <[email protected]>
AuthorDate: Wed Nov 18 19:15:34 2026 +0800
[Cherry-pick to branch-2.0] [#4733] fix(spark-connector): Load Paimon
format tables and test the Hive backend in the Spark connector (#14928) (#15076)
**Cherry-pick Information:**
- Original commit: e7a7a0061170e7052ba8749816e66380be6f1ca8
- Target branch: `branch-2.0`
- Status: ✅ Clean cherry-pick (no conflicts)
- Fork CI: runs in `jerryshao/gravitino`; re-run it there or push to
this PR branch
Co-authored-by: COLDMANYU <[email protected]>
---
.../connector/paimon/GravitinoPaimonCatalog.java | 9 ++
.../paimon/SparkPaimonCatalogHiveBackendIT.java | 138 +++++++++++++++++++++
.../paimon/SparkPaimonCatalogHiveBackendIT35.java | 36 ++++++
.../TestGravitinoPaimonCatalogTableWrapping.java | 94 ++++++++++++++
4 files changed, 277 insertions(+)
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 048023d71e..73bd0e094c 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
@@ -186,6 +186,15 @@ public class GravitinoPaimonCatalog extends BaseCatalog
PropertiesConverter propertiesConverter,
SparkTransformConverter sparkTransformConverter,
SparkTypeConverter sparkTypeConverter) {
+ // Paimon's SparkCatalog returns a SparkTable only for Paimon file store
tables. Other table
+ // types, such as format tables backed by a metastore (CSV, JSON, ORC,
Parquet or text files
+ // registered in Hive), come back as a different Spark table (a FileTable
or, in newer Paimon
+ // versions, a PaimonFormatTable). Their schema and partitions are already
resolved by Paimon
+ // and they read and write through their own code path, so return them as
they are instead of
+ // failing with a ClassCastException.
+ if (!(sparkTable instanceof SparkTable)) {
+ return sparkTable;
+ }
return new SparkPaimonTable(
identifier,
gravitinoTable,
diff --git
a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogHiveBackendIT.java
b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogHiveBackendIT.java
new file mode 100644
index 0000000000..50a6bfa029
--- /dev/null
+++
b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogHiveBackendIT.java
@@ -0,0 +1,138 @@
+/*
+ * 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.integration.test.paimon;
+
+import com.google.common.collect.Maps;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.spark.connector.paimon.PaimonPropertiesConstants;
+import org.apache.hadoop.fs.Path;
+import org.apache.spark.sql.SparkSession;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+/** This class uses Apache Paimon HiveCatalog for the backend catalog. */
+@Tag("gravitino-docker-test")
+public abstract class SparkPaimonCatalogHiveBackendIT extends
SparkPaimonCatalogIT {
+
+ private static final String DIRECT_CATALOG = "paimon_direct";
+
+ @Override
+ protected Map<String, String> getCatalogConfigs() {
+ Map<String, String> catalogProperties = Maps.newHashMap();
+ catalogProperties.put(
+ PaimonPropertiesConstants.GRAVITINO_PAIMON_CATALOG_BACKEND,
+ PaimonPropertiesConstants.PAIMON_CATALOG_BACKEND_HIVE);
+
catalogProperties.put(PaimonPropertiesConstants.GRAVITINO_PAIMON_CATALOG_URI,
hiveMetastoreUri);
+
catalogProperties.put(PaimonPropertiesConstants.GRAVITINO_PAIMON_CATALOG_WAREHOUSE,
warehouse);
+ return catalogProperties;
+ }
+
+ @Override
+ protected Map<String, String> getExtraSparkConfigs() {
+ // A Paimon SparkCatalog that talks to the same Hive metastore directly.
It creates the format
+ // tables that the Gravitino catalog is then expected to read, since
creating a format table
+ // through Gravitino is not supported.
+ Map<String, String> configs =
Maps.newHashMap(super.getExtraSparkConfigs());
+ configs.put("spark.sql.catalog." + DIRECT_CATALOG,
"org.apache.paimon.spark.SparkCatalog");
+ configs.put("spark.sql.catalog." + DIRECT_CATALOG + ".metastore", "hive");
+ configs.put("spark.sql.catalog." + DIRECT_CATALOG + ".uri",
hiveMetastoreUri);
+ configs.put("spark.sql.catalog." + DIRECT_CATALOG + ".warehouse",
warehouse);
+ return configs;
+ }
+
+ @Test
+ @Override
+ protected void testCreateAndLoadSchema() {
+ // Same as the common test, except that the schema comment is not checked:
Gravitino's Paimon
+ // catalog does not store the comment of a new schema yet (#5719).
+ String testDatabaseName = "t_create1";
+ dropDatabaseIfExists(testDatabaseName);
+ sql("CREATE DATABASE " + testDatabaseName + " WITH DBPROPERTIES
(ID=001);");
+ Map<String, String> databaseMeta = getDatabaseMetadata(testDatabaseName);
+ Assertions.assertTrue(databaseMeta.containsKey("Location"));
+ Assertions.assertTrue(databaseMeta.get("Properties").contains("(ID,001)"));
+
+ testDatabaseName = "t_create2";
+ dropDatabaseIfExists(testDatabaseName);
+ String testDatabaseLocation = "/tmp/" + testDatabaseName;
+ sql(
+ String.format(
+ "CREATE DATABASE %s COMMENT 'comment' LOCATION '%s' WITH
DBPROPERTIES (ID=002);",
+ testDatabaseName, testDatabaseLocation));
+ databaseMeta = getDatabaseMetadata(testDatabaseName);
+
Assertions.assertTrue(databaseMeta.get("Location").contains(testDatabaseLocation));
+ Assertions.assertTrue(databaseMeta.get("Properties").contains("(ID,002)"));
+ }
+
+ @Test
+ @Override
+ protected void testAlterSchema() {
+ String testDatabaseName = "t_alter";
+ dropDatabaseIfExists(testDatabaseName);
+ sql("CREATE DATABASE " + testDatabaseName + " WITH DBPROPERTIES
(ID=001);");
+ Map<String, String> databaseMeta = getDatabaseMetadata(testDatabaseName);
+ Assertions.assertTrue(databaseMeta.get("Properties").contains("(ID,001)"));
+
+ // Gravitino's Paimon catalog doesn't support altering a schema, whatever
the backend.
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () ->
+ sql(
+ String.format(
+ "ALTER DATABASE %s SET DBPROPERTIES ('ID'='002')",
testDatabaseName)));
+ }
+
+ @Test
+ void testReadExternalCsvFormatTable() throws Exception {
+ String tableName = "test_csv_format_table";
+ String database = getDefaultDatabase();
+ String location = warehouse + "/" + database + ".db/" + tableName +
"_data";
+ SparkSession spark = getSparkSession();
+ spark.sql(String.format("DROP TABLE IF EXISTS %s.%s.%s", DIRECT_CATALOG,
database, tableName));
+ hdfs.delete(new Path(location), true);
+
+ spark.sql(
+ String.format(
+ "CREATE TABLE %s.%s.%s (id INT, name STRING, dt STRING) USING csv "
+ + "PARTITIONED BY (dt) LOCATION '%s'",
+ DIRECT_CATALOG, database, tableName, location));
+ spark.sql(
+ String.format(
+ "INSERT INTO %s.%s.%s VALUES (1, 'a', '20260101'), (2, 'b',
'20260101'),"
+ + " (3, 'c', '20260102')",
+ DIRECT_CATALOG, database, tableName));
+
+ // Read the format table through the Gravitino catalog.
+ List<String> rows = getQueryData(String.format("SELECT * FROM %s ORDER BY
id", tableName));
+ Assertions.assertEquals(Arrays.asList("1,a,20260101", "2,b,20260101",
"3,c,20260102"), rows);
+ List<String> filtered =
+ getQueryData(
+ String.format("SELECT id FROM %s WHERE dt = '20260102' ORDER BY
id", tableName));
+ Assertions.assertEquals(Arrays.asList("3"), filtered);
+
+ // Dropping an external format table through Gravitino keeps its files.
+ sql(String.format("DROP TABLE %s", tableName));
+ Assertions.assertFalse(tableExists(tableName));
+ Assertions.assertTrue(hdfs.exists(new Path(location)));
+ hdfs.delete(new Path(location), true);
+ }
+}
diff --git
a/spark-connector/v3.5/spark/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogHiveBackendIT35.java
b/spark-connector/v3.5/spark/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogHiveBackendIT35.java
new file mode 100644
index 0000000000..a49cd0592e
--- /dev/null
+++
b/spark-connector/v3.5/spark/src/test/java/org/apache/gravitino/spark/connector/integration/test/paimon/SparkPaimonCatalogHiveBackendIT35.java
@@ -0,0 +1,36 @@
+/*
+ * 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.integration.test.paimon;
+
+import
org.apache.gravitino.spark.connector.paimon.GravitinoPaimonCatalogSpark35;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class SparkPaimonCatalogHiveBackendIT35 extends
SparkPaimonCatalogHiveBackendIT {
+
+ @Test
+ void testCatalogClassName() {
+ String catalogClass =
+ getSparkSession()
+ .sessionState()
+ .conf()
+ .getConfString("spark.sql.catalog." + getCatalogName());
+ Assertions.assertEquals(GravitinoPaimonCatalogSpark35.class.getName(),
catalogClass);
+ }
+}
diff --git
a/spark-connector/v3.5/spark/src/test/java/org/apache/gravitino/spark/connector/paimon/TestGravitinoPaimonCatalogTableWrapping.java
b/spark-connector/v3.5/spark/src/test/java/org/apache/gravitino/spark/connector/paimon/TestGravitinoPaimonCatalogTableWrapping.java
new file mode 100644
index 0000000000..07ce83e3e3
--- /dev/null
+++
b/spark-connector/v3.5/spark/src/test/java/org/apache/gravitino/spark/connector/paimon/TestGravitinoPaimonCatalogTableWrapping.java
@@ -0,0 +1,94 @@
+/*
+ * 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 org.apache.gravitino.client.GravitinoClient;
+import org.apache.gravitino.spark.connector.SparkTransformConverter;
+import org.apache.gravitino.spark.connector.SparkTypeConverter;
+import org.apache.gravitino.spark.connector.catalog.GravitinoCatalogManager;
+import org.apache.paimon.spark.SparkTable;
+import org.apache.spark.SparkConf;
+import org.apache.spark.sql.connector.catalog.Identifier;
+import org.apache.spark.sql.connector.catalog.Table;
+import org.apache.spark.sql.connector.catalog.TableCatalog;
+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 how {@link GravitinoPaimonCatalog} wraps the table Paimon's
SparkCatalog loads. Paimon
+ * returns a {@link SparkTable} for its file store tables and another Spark
table for format tables
+ * (a FileTable, or a PaimonFormatTable in Paimon 1.4.1 and later), so the
wrapping must not assume
+ * a {@link SparkTable}.
+ */
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+public class TestGravitinoPaimonCatalogTableWrapping {
+
+ private static final Identifier IDENT = Identifier.of(new String[] {"db"},
"t");
+
+ private GravitinoPaimonCatalog catalog;
+
+ @BeforeAll
+ void setUp() {
+ GravitinoCatalogManager.create(
+ new SparkConf(false), "user", identity -> mock(GravitinoClient.class));
+ catalog = new GravitinoPaimonCatalogSpark35();
+ }
+
+ @AfterAll
+ void tearDown() {
+ GravitinoCatalogManager.get().close();
+ }
+
+ @Test
+ void testNonPaimonFileStoreTableIsReturnedAsIs() {
+ // Stands in for the table Paimon returns for a format table: any Spark
table that is not a
+ // Paimon SparkTable.
+ Table formatTable = mock(Table.class);
+
+ Table result = wrap(formatTable);
+
+ Assertions.assertSame(formatTable, result);
+ }
+
+ @Test
+ void testPaimonFileStoreTableIsWrapped() {
+ SparkTable paimonTable = mock(SparkTable.class);
+
+ Table result = wrap(paimonTable);
+
+ Assertions.assertInstanceOf(SparkPaimonTable.class, result);
+ }
+
+ private Table wrap(Table sparkTable) {
+ return catalog.createSparkTable(
+ IDENT,
+ mock(org.apache.gravitino.rel.Table.class),
+ sparkTable,
+ mock(TableCatalog.class),
+ PaimonPropertiesConverter.getInstance(),
+ new SparkTransformConverter(true),
+ new SparkTypeConverter());
+ }
+}