World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#4733] fix(spark-connector): Load Paimon format tables and test the Hive backend in the Spark connector (#14928)
COLDMANYUe7a7a0061170
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.javaindex 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 BaseCatalogPropertiesConverter 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.javanew file mode 100644index 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.javanew file mode 100644index 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.javanew file mode 100644index 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());+ }+}