World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#14931] feat(spark-connector): Create Paimon format tables through the Hive backend (#14932)
COLDMANYUea42f44e3608
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 73bd0e094c..f1c9402e70 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@@ -32,6 +32,8 @@ import org.apache.gravitino.NameIdentifier;import org.apache.gravitino.catalog.lakehouse.paimon.PaimonConstants;import org.apache.gravitino.catalog.lakehouse.paimon.PaimonPropertiesUtils;import org.apache.gravitino.credential.CredentialPropertyUtils;+import org.apache.gravitino.exceptions.NoSuchSchemaException;+import org.apache.gravitino.exceptions.TableAlreadyExistsException;import org.apache.gravitino.rel.Column;import org.apache.gravitino.rel.expressions.Expression;import org.apache.gravitino.rel.expressions.NamedReference;@@ -71,20 +73,37 @@ import org.apache.spark.sql.util.CaseInsensitiveStringMap;public class GravitinoPaimonCatalog extends BaseCatalogimplements ProcedureCatalog, WithPaimonCatalog {+ // Whether the Paimon catalog uses the Hive backend, the only Paimon backend that can create+ // format tables.+ private boolean hiveBackend;+private static final String PRIMARY_KEY_INDEX_NAME = "PAIMON_PRIMARY_KEY_INDEX";/*** Creates Paimon table metadata with structured primary keys and bucket distribution.*+ * <p>With the Hive backend, a CREATE TABLE with a file format provider (USING csv, parquet, ...)+ * creates a Paimon format table, as it does in Paimon's own SparkCatalog. Other backends cannot+ * create format tables and keep creating Paimon tables.+ ** @param ident the Spark table identifier* @param schema the Spark table schema* @param transforms the requested partition and bucket transforms- * @param properties the requested Paimon table properties+ * @param sparkProperties the requested Paimon table properties* @return the created Gravitino table*/@Overrideprotected org.apache.gravitino.rel.Table createGravitinoTable(- Identifier ident, StructType schema, Transform[] transforms, Map<String, String> properties) {+ Identifier ident,+ StructType schema,+ Transform[] transforms,+ Map<String, String> sparkProperties)+ throws NoSuchSchemaException, TableAlreadyExistsException {+ Map<String, String> properties = sparkProperties;+ if (hiveBackend && sparkProperties != null) {+ properties =+ PaimonPropertiesConverter.getInstance().toHiveBackendTableProperties(sparkProperties);+ }List<Transform> partitions = new ArrayList<>();BucketTransform bucketTransform = null;if (transforms != null) {@@ -167,6 +186,9 @@ public class GravitinoPaimonCatalog extends BaseCatalog@Overrideprotected TableCatalog createAndInitSparkCatalog(String name, CaseInsensitiveStringMap options, Map<String, String> properties) {+ this.hiveBackend =+ PaimonPropertiesConstants.PAIMON_CATALOG_BACKEND_HIVE.equalsIgnoreCase(+ properties.get(PaimonPropertiesConstants.GRAVITINO_PAIMON_CATALOG_BACKEND));String catalogBackendName = PaimonPropertiesUtils.getCatalogBackendName(properties);TableCatalog paimonCatalog = new SparkCatalog();Map<String, String> all =diff --git a/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/PaimonPropertiesConstants.java b/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/PaimonPropertiesConstants.javaindex 915308ae8d..9356279a9d 100644--- a/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/PaimonPropertiesConstants.java+++ b/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/PaimonPropertiesConstants.java@@ -48,4 +48,16 @@ public class PaimonPropertiesConstants {static final String GRAVITINO_PAIMON_CATALOG_BACKEND_FILESYSTEM = "filesystem";public static final String PAIMON_TABLE_LOCATION = "path";++ /** The Spark provider (USING clause) of a Paimon table. */+ static final String PAIMON_PROVIDER = "paimon";++ /** Paimon table option that selects the table type. */+ static final String PAIMON_TABLE_TYPE = "type";++ /** Value of {@link #PAIMON_TABLE_TYPE} for a format table. */+ static final String PAIMON_TABLE_TYPE_FORMAT_TABLE = "format-table";++ /** Paimon table option that holds the file format of the table. */+ static final String PAIMON_FILE_FORMAT = "file.format";}diff --git a/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/PaimonPropertiesConverter.java b/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/PaimonPropertiesConverter.javaindex f713ca89dd..7573213c4f 100644--- a/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/PaimonPropertiesConverter.java+++ b/spark-connector/spark-common/src/main/spark35/org/apache/gravitino/spark/connector/paimon/PaimonPropertiesConverter.java@@ -21,11 +21,14 @@ package org.apache.gravitino.spark.connector.paimon;import com.google.common.base.Preconditions;import java.util.HashMap;+import java.util.Locale;import java.util.Map;import org.apache.commons.lang3.StringUtils;import org.apache.gravitino.catalog.lakehouse.paimon.PaimonConstants;import org.apache.gravitino.catalog.lakehouse.paimon.PaimonPropertiesUtils;import org.apache.gravitino.spark.connector.PropertiesConverter;+import org.apache.paimon.spark.SparkSource;+import org.apache.spark.sql.connector.catalog.TableCatalog;public class PaimonPropertiesConverter implements PropertiesConverter {@@ -64,4 +67,37 @@ public class PaimonPropertiesConverter implements PropertiesConverter {public Map<String, String> toSparkTableProperties(Map<String, String> properties) {return new HashMap<>(properties);}++ /**+ * Converts the Spark table properties of a table created in a Paimon catalog with the Hive+ * backend, the only Paimon backend that can create format tables. It follows Paimon's own+ * SparkCatalog: a provider from the USING clause that Paimon supports as a format table format+ * ({@code SparkSource.FORMAT_NAMES}, which depends on the Paimon version) makes a format table of+ * that format; any other provider, "paimon" or no provider keeps a Paimon table; and LOCATION+ * becomes the Paimon table path. Other backends keep the properties as they are.+ *+ * @param properties the Spark table properties, already converted by {@link+ * #toGravitinoTableProperties(Map)}.+ * @return the table properties to pass to the Hive backend.+ */+ Map<String, String> toHiveBackendTableProperties(Map<String, String> properties) {+ Map<String, String> tableProperties = new HashMap<>(properties);+ String provider = tableProperties.remove(TableCatalog.PROP_PROVIDER);+ if (StringUtils.isNotBlank(provider)+ && !PaimonPropertiesConstants.PAIMON_PROVIDER.equalsIgnoreCase(provider)+ && SparkSource.FORMAT_NAMES().contains(provider.toLowerCase(Locale.ROOT))) {+ tableProperties.put(+ PaimonPropertiesConstants.PAIMON_TABLE_TYPE,+ PaimonPropertiesConstants.PAIMON_TABLE_TYPE_FORMAT_TABLE);+ tableProperties.put(+ PaimonPropertiesConstants.PAIMON_FILE_FORMAT, provider.toLowerCase(Locale.ROOT));+ }+ String location = tableProperties.remove(TableCatalog.PROP_LOCATION);+ if (StringUtils.isNotBlank(location)) {+ tableProperties.put(PaimonPropertiesConstants.PAIMON_TABLE_LOCATION, location);+ }+ // Spark marks a table with a LOCATION as external; Paimon decides that from the path itself.+ tableProperties.remove(TableCatalog.PROP_EXTERNAL);+ return tableProperties;+ }}diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkCommonIT.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkCommonIT.javaindex 535ea93895..b278e033bf 100644--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkCommonIT.java+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/SparkCommonIT.java@@ -689,7 +689,7 @@ public abstract class SparkCommonIT extends SparkEnvIT {@Test@EnabledIf("supportsPartition")- void testCreateDatasourceFormatPartitionTable() {+ protected void testCreateDatasourceFormatPartitionTable() {String tableName = "datasource_partition_table";dropTableIfExists(tableName);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.javaindex 613ee856c4..243ab57889 100644--- 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@@ -22,6 +22,7 @@ import com.google.common.collect.Maps;import java.util.Arrays;import java.util.List;import java.util.Map;+import java.util.stream.Collectors;import org.apache.gravitino.spark.connector.paimon.PaimonPropertiesConstants;import org.apache.hadoop.fs.Path;import org.apache.spark.sql.SparkSession;@@ -48,9 +49,8 @@ public abstract class SparkPaimonCatalogHiveBackendIT extends SparkPaimonCatalog@Overrideprotected 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.+ // A Paimon SparkCatalog that talks to the same Hive metastore directly. The tests use it to+ // create tables outside Gravitino and to check what Gravitino created.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");@@ -135,4 +135,104 @@ public abstract class SparkPaimonCatalogHiveBackendIT extends SparkPaimonCatalogAssertions.assertTrue(hdfs.exists(new Path(location)));hdfs.delete(new Path(location), true);}++ @Test+ void testCreateFormatTable() throws Exception {+ String tableName = "test_create_csv_format_table";+ String database = getDefaultDatabase();+ String location = warehouse + "/" + database + ".db/" + tableName + "_data";+ dropTableIfExists(tableName);+ hdfs.delete(new Path(location), true);++ // Create a CSV format table through the Gravitino catalog.+ sql(+ String.format(+ "CREATE TABLE %s (id INT, name STRING, dt STRING) USING csv PARTITIONED BY (dt) "+ + "LOCATION '%s'",+ tableName, location));+ sql(+ String.format(+ "INSERT INTO %s VALUES (1, 'a', '20260101'), (2, 'b', '20260101'), (3, 'c', '20260102')",+ tableName));++ // The Paimon catalog sees a CSV format table at the given location.+ Map<String, String> paimonOptions = getPaimonOptions(tableName);+ Assertions.assertEquals("format-table", paimonOptions.get("type"));+ Assertions.assertEquals("csv", paimonOptions.get("file.format"));+ Assertions.assertTrue(hdfs.exists(new Path(location, "dt=20260102")));++ // Read it back through Gravitino and directly through Paimon.+ List<String> expected = Arrays.asList("1,a,20260101", "2,b,20260101", "3,c,20260102");+ Assertions.assertEquals(+ expected, getQueryData(String.format("SELECT * FROM %s ORDER BY id", tableName)));+ Assertions.assertEquals(+ expected,+ getQueryData(+ String.format(+ "SELECT * FROM %s.%s.%s ORDER BY id", DIRECT_CATALOG, database, tableName)));++ sql(String.format("DROP TABLE %s", tableName));+ Assertions.assertFalse(tableExists(tableName));+ hdfs.delete(new Path(location), true);+ }++ @Test+ void testCreateTableUsingPaimonIsNotAFormatTable() {+ String tableName = "test_create_using_paimon";+ dropTableIfExists(tableName);+ sql(String.format("CREATE TABLE %s (id INT, name STRING) USING paimon", tableName));+ Map<String, String> paimonOptions = getPaimonOptions(tableName);+ Assertions.assertNotEquals("format-table", paimonOptions.get("type"));+ Assertions.assertNull(paimonOptions.get("file.format"));+ sql(String.format("DROP TABLE %s", tableName));+ }++ @Test+ void testCreateTableUsingUnsupportedFormatIsNotAFormatTable() {+ // Paimon 1.2 has no text format table, so its SparkCatalog creates a Paimon table for USING+ // text.+ String tableName = "test_create_using_text";+ dropTableIfExists(tableName);+ sql(String.format("CREATE TABLE %s (value STRING) USING text", tableName));+ Map<String, String> paimonOptions = getPaimonOptions(tableName);+ Assertions.assertNotEquals("format-table", paimonOptions.get("type"));+ Assertions.assertNull(paimonOptions.get("file.format"));+ sql(String.format("INSERT INTO %s VALUES ('a')", tableName));+ Assertions.assertEquals(+ Arrays.asList("a"), getQueryData(String.format("SELECT value FROM %s", tableName)));+ sql(String.format("DROP TABLE %s", tableName));+ }++ @Test+ @Override+ protected void testCreateDatasourceFormatPartitionTable() {+ // With the Hive backend, USING PARQUET creates a Paimon format table, as in Paimon's own+ // SparkCatalog. Spark reports such a table without its database, so the common checks on the+ // Spark table do not apply; check the Paimon table and the data instead.+ String tableName = "datasource_partition_table";+ dropTableIfExists(tableName);+ sql(getCreateSimpleTableString(tableName) + " USING PARQUET PARTITIONED BY (name, age)");+ Map<String, String> paimonOptions = getPaimonOptions(tableName);+ Assertions.assertEquals("format-table", paimonOptions.get("type"));+ Assertions.assertEquals("parquet", paimonOptions.get("file.format"));++ sql(String.format("INSERT INTO %s VALUES (1, 'a', 10), (2, 'b', 20)", tableName));+ Assertions.assertEquals(+ Arrays.asList("1,a,10", "2,b,20"),+ getQueryData(String.format("SELECT id, name, age FROM %s ORDER BY id", tableName)));+ Assertions.assertEquals(+ Arrays.asList("2"),+ getQueryData(String.format("SELECT id FROM %s WHERE age = 20", tableName)));+ sql(String.format("DROP TABLE %s", tableName));+ }++ private Map<String, String> getPaimonOptions(String tableName) {+ return getSparkSession()+ .sql(+ String.format(+ "SHOW TBLPROPERTIES %s.%s.%s", DIRECT_CATALOG, getDefaultDatabase(), tableName))+ .collectAsList()+ .stream()+ .collect(Collectors.toMap(r -> r.getString(0), r -> r.getString(1)));+ }}diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/paimon/TestPaimonPropertiesConverter.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/paimon/TestPaimonPropertiesConverter.javaindex a3a0e91284..4fb4eb8637 100644--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/paimon/TestPaimonPropertiesConverter.java+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/paimon/TestPaimonPropertiesConverter.java@@ -21,6 +21,7 @@ package org.apache.gravitino.spark.connector.paimon;import com.google.common.collect.ImmutableMap;import java.util.Map;+import org.apache.spark.sql.connector.catalog.TableCatalog;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.Test;@@ -103,4 +104,75 @@ public class TestPaimonPropertiesConverter {"filesystem-warehouse"),properties);}++ @Test+ void testTablePropertiesKeepProvider() {+ // Without the Hive backend, the provider is passed on unchanged, as before.+ Assertions.assertEquals(+ ImmutableMap.of(TableCatalog.PROP_PROVIDER, "parquet", "k1", "v1"),+ paimonPropertiesConverter.toGravitinoTableProperties(+ ImmutableMap.of(TableCatalog.PROP_PROVIDER, "parquet", "k1", "v1")));+ }++ @Test+ void testHiveBackendPropertiesForPaimonTable() {+ // USING paimon, or no USING clause, keeps a Paimon table.+ Assertions.assertEquals(+ ImmutableMap.of("k1", "v1"),+ paimonPropertiesConverter.toHiveBackendTableProperties(+ ImmutableMap.of(TableCatalog.PROP_PROVIDER, "paimon", "k1", "v1")));+ Assertions.assertEquals(+ ImmutableMap.of("k1", "v1"),+ paimonPropertiesConverter.toHiveBackendTableProperties(+ ImmutableMap.of(TableCatalog.PROP_PROVIDER, "PAIMON", "k1", "v1")));+ Assertions.assertEquals(+ ImmutableMap.of("k1", "v1"),+ paimonPropertiesConverter.toHiveBackendTableProperties(ImmutableMap.of("k1", "v1")));+ }++ @Test+ void testHiveBackendPropertiesForFormatTable() {+ // A file format provider becomes a Paimon format table of that format.+ Assertions.assertEquals(+ ImmutableMap.of(+ PaimonPropertiesConstants.PAIMON_TABLE_TYPE,+ PaimonPropertiesConstants.PAIMON_TABLE_TYPE_FORMAT_TABLE,+ PaimonPropertiesConstants.PAIMON_FILE_FORMAT,+ "csv",+ "csv.field-delimiter",+ "\\t"),+ paimonPropertiesConverter.toHiveBackendTableProperties(+ ImmutableMap.of(TableCatalog.PROP_PROVIDER, "CSV", "csv.field-delimiter", "\\t")));+ }++ @Test+ void testHiveBackendPropertiesForUnsupportedFormat() {+ // A provider that Paimon does not support as a format table format keeps a Paimon table, as in+ // Paimon's own SparkCatalog. Paimon 1.2 has no text format table.+ Assertions.assertEquals(+ ImmutableMap.of("k1", "v1"),+ paimonPropertiesConverter.toHiveBackendTableProperties(+ ImmutableMap.of(TableCatalog.PROP_PROVIDER, "text", "k1", "v1")));+ }++ @Test+ void testHiveBackendPropertiesWithLocation() {+ // LOCATION is passed to Paimon as the table path; Spark's external flag is dropped.+ Assertions.assertEquals(+ ImmutableMap.of(+ PaimonPropertiesConstants.PAIMON_TABLE_TYPE,+ PaimonPropertiesConstants.PAIMON_TABLE_TYPE_FORMAT_TABLE,+ PaimonPropertiesConstants.PAIMON_FILE_FORMAT,+ "parquet",+ PaimonPropertiesConstants.PAIMON_TABLE_LOCATION,+ "hdfs://ns/data/t1"),+ paimonPropertiesConverter.toHiveBackendTableProperties(+ ImmutableMap.of(+ TableCatalog.PROP_PROVIDER,+ "parquet",+ TableCatalog.PROP_LOCATION,+ "hdfs://ns/data/t1",+ TableCatalog.PROP_EXTERNAL,+ "true")));+ }}