World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#14490] fix(spark): report unsupported PostgreSQL write types before execution (#14491)
Qi Yu89a71fe4811f
diff --git a/docs/spark-connector/spark-connector.md b/docs/spark-connector/spark-connector.mdindex 7e5fbe8cd6..4d1d0f1669 100644--- a/docs/spark-connector/spark-connector.md+++ b/docs/spark-connector/spark-connector.md@@ -144,3 +144,11 @@ Gravitino spark connector support the following datatype mapping between Spark aFor Gravitino `UUID` type, Spark connector represents it as `StringType` because Spark has no native UUID type.This behavior is consistent with Spark built-in PostgreSQL JDBC mapping (`uuid` -> `StringType`).:::++### PostgreSQL write limitations++Spark JDBC reads PostgreSQL UUID and External columns as strings. Writes to tables containing+these columns are rejected before JDBC execution, including writes of NULL values and+INSERT OVERWRITE. The error identifies the unsupported column. Spark binds these columns+as VARCHAR, which does not preserve the PostgreSQL target type. Use a native PostgreSQL+writer for these tables. Other PostgreSQL column types retain their existing write behavior.diff --git a/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/jdbc/SparkJdbcTable.java b/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/jdbc/SparkJdbcTable.javaindex 3de807c368..940cd43cdf 100644--- a/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/jdbc/SparkJdbcTable.java+++ b/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/jdbc/SparkJdbcTable.java@@ -19,20 +19,26 @@package org.apache.gravitino.spark.connector.jdbc;+import java.util.Locale;import java.util.Map;+import org.apache.gravitino.rel.Column;import org.apache.gravitino.rel.Table;+import org.apache.gravitino.rel.types.Types;import org.apache.gravitino.spark.connector.PropertiesConverter;import org.apache.gravitino.spark.connector.SparkTransformConverter;import org.apache.gravitino.spark.connector.SparkTypeConverter;import org.apache.gravitino.spark.connector.utils.GravitinoTableInfoHelper;import org.apache.spark.sql.connector.catalog.Identifier;+import org.apache.spark.sql.connector.write.LogicalWriteInfo;+import org.apache.spark.sql.connector.write.WriteBuilder;import org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTable;import org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTableCatalog;import org.apache.spark.sql.types.StructType;public class SparkJdbcTable extends JDBCTable {- private GravitinoTableInfoHelper gravitinoTableInfoHelper;+ private final GravitinoTableInfoHelper gravitinoTableInfoHelper;+ private final Table gravitinoTable;public SparkJdbcTable(Identifier identifier,@@ -43,6 +49,7 @@ public class SparkJdbcTable extends JDBCTable {SparkTransformConverter sparkTransformConverter,SparkTypeConverter sparkTypeConverter) {super(identifier, jdbcTable.schema(), jdbcTable.jdbcOptions());+ this.gravitinoTable = gravitinoTable;this.gravitinoTableInfoHelper =new GravitinoTableInfoHelper(false,@@ -53,6 +60,30 @@ public class SparkJdbcTable extends JDBCTable {sparkTypeConverter);}+ /**+ * Validates PostgreSQL write types before Spark creates a JDBC writer.+ *+ * @param info the write information supplied by Spark+ * @return the JDBC write builder for supported column types+ */+ @Override+ public WriteBuilder newWriteBuilder(LogicalWriteInfo info) {+ if (jdbcOptions().url().toLowerCase(Locale.ROOT).startsWith("jdbc:postgresql:")) {+ for (Column column : gravitinoTable.columns()) {+ if (column.dataType() instanceof Types.UUIDType+ || column.dataType() instanceof Types.ExternalType) {+ throw new UnsupportedOperationException(+ "Spark JDBC writes to PostgreSQL column '"+ + column.name()+ + "' of type "+ + column.dataType().simpleString()+ + " are not supported because Spark binds this type as VARCHAR; use a native PostgreSQL writer");+ }+ }+ }+ return super.newWriteBuilder(info);+ }+@Overridepublic String name() {return gravitinoTableInfoHelper.name();diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/jdbc/TestSparkJdbcTableWriteValidation.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/jdbc/TestSparkJdbcTableWriteValidation.javanew file mode 100644index 0000000000..83eb356876--- /dev/null+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/jdbc/TestSparkJdbcTableWriteValidation.java@@ -0,0 +1,98 @@+/*+ * 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.jdbc;++import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++import java.util.Collections;+import org.apache.gravitino.rel.Column;+import org.apache.gravitino.rel.Table;+import org.apache.gravitino.rel.types.Type;+import org.apache.gravitino.rel.types.Types;+import org.apache.gravitino.spark.connector.SparkTransformConverter;+import org.apache.spark.sql.connector.catalog.Identifier;+import org.apache.spark.sql.connector.write.LogicalWriteInfo;+import org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions;+import org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTable;+import org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTableCatalog;+import org.apache.spark.sql.types.DataTypes;+import org.apache.spark.sql.types.StructType;+import org.apache.spark.sql.util.CaseInsensitiveStringMap;+import org.junit.jupiter.api.Assertions;+import org.junit.jupiter.api.Test;+import scala.collection.immutable.HashMap;++/** PostgreSQL write validation before Spark creates or executes a JDBC write. */+public class TestSparkJdbcTableWriteValidation {++ @Test+ void testUnsupportedPostgreSqlColumnsFailBeforeWrite() {+ for (Type type :+ new Type[] {+ Types.UUIDType.get(),+ Types.ExternalType.of("jsonb"),+ Types.ExternalType.of("numeric"),+ Types.ExternalType.of("inet"),+ Types.ExternalType.of("interval")+ }) {+ SparkJdbcTable table = table(type, "jdbc:postgresql://localhost/test");+ // Reading remains supported; nullable columns are also rejected on write before any row is+ // bound.+ Assertions.assertEquals(DataTypes.StringType, table.schema().fields()[0].dataType());+ UnsupportedOperationException error =+ Assertions.assertThrows(+ UnsupportedOperationException.class, () -> table.newWriteBuilder(null));+ Assertions.assertTrue(error.getMessage().contains("special_value"));+ Assertions.assertTrue(error.getMessage().contains(type.simpleString()));+ }+ }++ @Test+ void testOrdinaryPostgreSqlTypesStillCreateWriteBuilder() {+ SparkJdbcTable table = table(Types.IntegerType.get(), "jdbc:postgresql://localhost/test");+ LogicalWriteInfo info = mock(LogicalWriteInfo.class);+ when(info.options()).thenReturn(new CaseInsensitiveStringMap(Collections.emptyMap()));+ Assertions.assertNotNull(table.newWriteBuilder(info));+ }++ private SparkJdbcTable table(Type type, String url) {+ Table metadata = mock(Table.class);+ when(metadata.columns())+ .thenReturn(new Column[] {Column.of("special_value", type, null, true, false, null)});+ JDBCOptions options =+ new JDBCOptions(+ new HashMap<String, String>()+ .updated("url", url)+ .updated("dbtable", "t")+ .updated("driver", "org.postgresql.Driver"));+ JDBCTable physical = mock(JDBCTable.class);+ when(physical.schema()).thenReturn(new StructType());+ when(physical.jdbcOptions()).thenReturn(options);+ return new SparkJdbcTable(+ Identifier.of(new String[] {"public"}, "t"),+ metadata,+ physical,+ mock(JDBCTableCatalog.class),+ JdbcPropertiesConverter.getInstance(),+ new SparkTransformConverter(false),+ new SparkJdbcTypeConverter());+ }+}