This is an automated email from the ASF dual-hosted git repository.
jerryshao 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 e7a7a00611 [#4733] fix(spark-connector): Load Paimon format tables and
test the Hive backend in the Spark connector (#14928)
e7a7a00611 is described below
commit e7a7a0061170e7052ba8749816e66380be6f1ca8
Author: COLDMANYU <[email protected]>
AuthorDate: Wed Nov 18 18:03:11 2026 +0800
[#4733] fix(spark-connector): Load Paimon format tables and test the Hive
backend in the Spark connector (#14928)
### What changes were proposed in this pull request?
- `GravitinoPaimonCatalog#createSparkTable` returns a table loaded by
Paimon's `SparkCatalog` as it is when it is not a `SparkTable`, instead
of casting it and wrapping it in `SparkPaimonTable`.
- New `SparkPaimonCatalogHiveBackendIT` (and
`SparkPaimonCatalogHiveBackendIT35`) runs the common Spark connector
tests on a Paimon catalog with the Hive backend, against the Hive
metastore of the test Hive container. It adds one test that creates a
partitioned CSV format table with a Paimon `SparkCatalog` on the same
metastore, then reads it through Gravitino, with and without a partition
filter, and drops it.
- New unit test `TestGravitinoPaimonCatalogTableWrapping`.
### Why are the changes needed?
Paimon's `SparkCatalog` returns a `SparkTable` only for Paimon file
store tables. A format table (a CSV, JSON, ORC, Parquet or text table
registered in the metastore, which the Hive backend supports) comes back
as a different Spark table: a Spark file table such as
`PartitionedCSVTable` in Paimon 1.2, or a `PaimonFormatTable` in newer
Paimon versions. `GravitinoPaimonCatalog` cast every table to
`SparkTable`, so loading a format table failed:
```
java.lang.ClassCastException: class
org.apache.spark.sql.execution.PartitionedCSVTable cannot be cast to class
org.apache.paimon.spark.SparkTable
at
org.apache.gravitino.spark.connector.paimon.GravitinoPaimonCatalog.createSparkTable(GravitinoPaimonCatalog.java:67)
at
org.apache.gravitino.spark.connector.catalog.BaseCatalog.loadTable(BaseCatalog.java:338)
```
Such a table already has its schema and partitions resolved by Paimon
and reads and writes through its own code path, so the connector returns
it unchanged.
The Hive backend had no Spark connector IT. #5858 started one but was
closed without being merged; this PR continues it.
Fixes #4733
### Does this PR introduce _any_ user-facing change?
Format tables in a Paimon catalog can be read and written through the
Gravitino Spark connector. They are returned as Paimon's own Spark
table, so Gravitino-specific table handling does not apply to them.
Paimon file store tables are unchanged.
### How was this patch tested?
- Unit test: `TestGravitinoPaimonCatalogTableWrapping` checks that a
`SparkTable` is still wrapped and that another table is returned as it
is.
- Without the fix, the unit test and `testReadExternalCsvFormatTable`
fail with the `ClassCastException` above.
- `SparkPaimonCatalogHiveBackendIT35`: 37 tests, 32 passed, 5 skipped
(features the Paimon catalog does not support; the filesystem backend
skips the same 5).
- `SparkPaimonCatalogFilesystemBackendIT35` still passes.
- Two common schema tests check the schema comment, which the Paimon
catalog does not store yet (#5719). The Hive backend IT overrides them
without the comment check, with a note to restore it once #5719 is
fixed.
---
.../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());
+ }
+}