This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new bb9765185a [spark] Reject DataFrame writes to partitioned engine 
format tables (#10944)
bb9765185a is described below

commit bb9765185a37bb2c0dcb9e88ad37ac270281f464
Author: YangJie <[email protected]>
AuthorDate: Mon Nov 2 09:40:31 2026 -0500

    [spark] Reject DataFrame writes to partitioned engine format tables (#10944)
---
 .../shim/PaimonReplaceTableAsSelectStrategy.scala  | 23 ++++++++-
 .../shim/PaimonReplaceTableAsSelectStrategy.scala  | 32 ++++++++++++-
 .../shim/PaimonReplaceTableAsSelectStrategy.scala  | 10 ++++
 .../java/org/apache/paimon/spark/SparkCatalog.java |  2 +-
 .../spark/sql/execution/SparkFormatTable.scala     |  9 ++++
 .../shim/PaimonReplaceTableAsSelectStrategy.scala  | 12 +++++
 .../paimon/spark/sql/FormatTableTestBase.scala     | 55 ++++++++++++++++++++++
 7 files changed, 138 insertions(+), 5 deletions(-)

diff --git 
a/paimon-spark/paimon-spark-3.2/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
 
b/paimon-spark/paimon-spark-3.2/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
index d637c006da..617f4a4252 100644
--- 
a/paimon-spark/paimon-spark-3.2/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
+++ 
b/paimon-spark/paimon-spark-3.2/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
@@ -18,13 +18,32 @@
 
 package org.apache.spark.sql.execution.shim
 
+import org.apache.paimon.spark.SparkCatalog
+
 import org.apache.spark.sql.{SparkSession, Strategy}
-import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, 
ReplaceTableAsSelect}
+import org.apache.spark.sql.connector.catalog.{CatalogV2Util, TableCatalog}
 import org.apache.spark.sql.execution.SparkPlan
 
+import scala.collection.JavaConverters._
+
 case class PaimonReplaceTableAsSelectStrategy(spark: SparkSession) extends 
Strategy {
 
-  override def apply(plan: LogicalPlan): Seq[SparkPlan] = Nil
+  // Spark plans the replace itself, which drops the old table before it 
writes, so reject what
+  // it cannot write before that. The check uses the properties Spark creates 
the table with.
+  override def apply(plan: LogicalPlan): Seq[SparkPlan] = {
+    plan match {
+      case ReplaceTableAsSelect(catalog: SparkCatalog, ident, parts, _, props, 
_, _) =>
+        val properties = CatalogV2Util.withDefaultOwnership(props)
+        catalog.checkPartitionedFormatTableCtas(
+          ident,
+          properties.get(TableCatalog.PROP_PROVIDER).orNull,
+          parts.nonEmpty,
+          properties.asJava)
+      case _ =>
+    }
+    Nil
+  }
 }
 
 case class PaimonReplaceTableStrategy(spark: SparkSession) extends Strategy {
diff --git 
a/paimon-spark/paimon-spark-3.3/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
 
b/paimon-spark/paimon-spark-3.3/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
index d637c006da..26d5dc58f1 100644
--- 
a/paimon-spark/paimon-spark-3.3/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
+++ 
b/paimon-spark/paimon-spark-3.3/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
@@ -18,13 +18,41 @@
 
 package org.apache.spark.sql.execution.shim
 
+import org.apache.paimon.spark.SparkCatalog
+
 import org.apache.spark.sql.{SparkSession, Strategy}
-import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.analysis.ResolvedDBObjectName
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, 
ReplaceTableAsSelect, TableSpec}
+import org.apache.spark.sql.connector.catalog.CatalogV2Util
 import org.apache.spark.sql.execution.SparkPlan
 
+import scala.collection.JavaConverters._
+
 case class PaimonReplaceTableAsSelectStrategy(spark: SparkSession) extends 
Strategy {
 
-  override def apply(plan: LogicalPlan): Seq[SparkPlan] = Nil
+  import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
+
+  // Spark plans the replace itself, which drops the old table before it 
writes, so reject what
+  // it cannot write before that. The check uses the properties Spark creates 
the table with.
+  override def apply(plan: LogicalPlan): Seq[SparkPlan] = {
+    plan match {
+      case ReplaceTableAsSelect(
+            ResolvedDBObjectName(catalog: SparkCatalog, ident),
+            parts,
+            _,
+            tableSpec: TableSpec,
+            _,
+            _) =>
+        val properties = CatalogV2Util.convertTableProperties(tableSpec)
+        catalog.checkPartitionedFormatTableCtas(
+          ident.asIdentifier,
+          tableSpec.provider.orNull,
+          parts.nonEmpty,
+          properties.asJava)
+      case _ =>
+    }
+    Nil
+  }
 }
 
 case class PaimonReplaceTableStrategy(spark: SparkSession) extends Strategy {
diff --git 
a/paimon-spark/paimon-spark-3.4/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
 
b/paimon-spark/paimon-spark-3.4/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
index 9da1c75f69..3f19a1ddbf 100644
--- 
a/paimon-spark/paimon-spark-3.4/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
+++ 
b/paimon-spark/paimon-spark-3.4/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
@@ -19,6 +19,7 @@
 package org.apache.spark.sql.execution.shim
 
 import org.apache.paimon.Snapshot
+import org.apache.paimon.spark.SparkCatalog
 import org.apache.paimon.spark.catalog.SparkBaseCatalog
 import org.apache.paimon.spark.write.PaimonWriteOptions
 
@@ -64,6 +65,15 @@ case class PaimonReplaceTableAsSelectStrategy(spark: 
SparkSession)
       val (tableOptions, writeOptions) =
         splitTableAndWriteOptions(options)
       val qualifiedSpec = qualifyTableSpec(tableSpec, tableOptions)
+      catalog match {
+        case sparkCatalog: SparkCatalog =>
+          sparkCatalog.checkPartitionedFormatTableCtas(
+            ident,
+            qualifiedSpec.provider.orNull,
+            parts.nonEmpty,
+            qualifiedSpec.properties.asJava)
+        case _ =>
+      }
       val operation =
         if (orCreate) Snapshot.Operation.CREATE_OR_REPLACE_TABLE_AS_SELECT
         else Snapshot.Operation.REPLACE_TABLE_AS_SELECT
diff --git 
a/paimon-spark/paimon-spark-common/src/main/java/org/apache/paimon/spark/SparkCatalog.java
 
b/paimon-spark/paimon-spark-common/src/main/java/org/apache/paimon/spark/SparkCatalog.java
index 644afd7614..93103939c2 100644
--- 
a/paimon-spark/paimon-spark-common/src/main/java/org/apache/paimon/spark/SparkCatalog.java
+++ 
b/paimon-spark/paimon-spark-common/src/main/java/org/apache/paimon/spark/SparkCatalog.java
@@ -474,7 +474,7 @@ public class SparkCatalog extends SparkBaseCatalog
                         catalog.options(),
                         properties)) {
             throw new UnsupportedOperationException(
-                    "Using CTAS with a partitioned engine format table is not 
supported. "
+                    "Using CTAS or RTAS with a partitioned engine format table 
is not supported. "
                             + "Set 'format-table.implementation' to 
'paimon'.");
         }
     }
diff --git 
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/execution/SparkFormatTable.scala
 
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/execution/SparkFormatTable.scala
index ee72953be3..5f1d944e80 100644
--- 
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/execution/SparkFormatTable.scala
+++ 
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/execution/SparkFormatTable.scala
@@ -144,6 +144,15 @@ trait PartitionedFormatTable extends 
SupportsPartitionManagement {
   }
 
   protected def wrapWriteBuilderWithOverwrite(original: WriteBuilder): 
WriteBuilder = {
+    if (partitionSchema().nonEmpty) {
+      // Spark's V2 file write does not write partition directories, so the 
rows would land in the
+      // table root, where the partitioned file index never reads them. SQL 
INSERT, except
+      // INSERT ... REPLACE WHERE, is rewritten to Spark's V1 write by 
FallBackFileSourceV2 and
+      // never gets here.
+      throw new UnsupportedOperationException(
+        "Only SQL INSERT INTO and INSERT OVERWRITE can write a partitioned 
engine format table. " +
+          "Set 'format-table.implementation' to 'paimon' to write it in other 
ways.")
+    }
     new WriteBuilder with SupportsOverwriteV2 {
       override def build(): Write = original.build()
       override def canOverwrite(predicates: Array[Predicate]): Boolean = true
diff --git 
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
 
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
index 8392750768..17a25d1076 100644
--- 
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
+++ 
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/execution/shim/PaimonReplaceTableAsSelectStrategy.scala
@@ -19,6 +19,7 @@
 package org.apache.spark.sql.execution.shim
 
 import org.apache.paimon.Snapshot
+import org.apache.paimon.spark.SparkCatalog
 import org.apache.paimon.spark.catalog.SparkBaseCatalog
 import org.apache.paimon.spark.write.PaimonWriteOptions
 
@@ -30,6 +31,8 @@ import 
org.apache.spark.sql.execution.{PaimonTableAsSelectHelper, SparkPlan, Spa
 import org.apache.spark.sql.execution.PaimonTableAsSelectHelper._
 import org.apache.spark.sql.paimon.shims.SparkShimLoader
 
+import scala.collection.JavaConverters._
+
 case class PaimonReplaceTableAsSelectStrategy(spark: SparkSession)
   extends SparkStrategy
   with PaimonTableAsSelectHelper {
@@ -55,6 +58,15 @@ case class PaimonReplaceTableAsSelectStrategy(spark: 
SparkSession)
 
       val (tableOptions, writeOptions) = splitTableAndWriteOptions(options)
       val qualifiedSpec = qualifyTableSpec(tableSpec, tableOptions)
+      catalog match {
+        case sparkCatalog: SparkCatalog =>
+          sparkCatalog.checkPartitionedFormatTableCtas(
+            ident,
+            qualifiedSpec.provider.orNull,
+            parts.nonEmpty,
+            qualifiedSpec.properties.asJava)
+        case _ =>
+      }
       val operation =
         if (orCreate) Snapshot.Operation.CREATE_OR_REPLACE_TABLE_AS_SELECT
         else Snapshot.Operation.REPLACE_TABLE_AS_SELECT
diff --git 
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/FormatTableTestBase.scala
 
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/FormatTableTestBase.scala
index bbcd1cc899..cb95176cf8 100644
--- 
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/FormatTableTestBase.scala
+++ 
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/FormatTableTestBase.scala
@@ -40,6 +40,7 @@ import org.apache.spark.sql.execution.SparkPlan
 import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
 import 
org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelationCommand
 import org.apache.spark.sql.execution.datasources.v2.{BatchScanExec, 
DataSourceV2Relation, FileTable}
+import org.apache.spark.sql.functions.col
 import org.apache.spark.sql.types.{LongType, StringType, StructField, 
StructType}
 import org.apache.spark.sql.util.CaseInsensitiveStringMap
 
@@ -513,6 +514,60 @@ abstract class FormatTableTestBase extends 
PaimonHiveTestBase with AdaptiveSpark
     }
   }
 
+  test("Format table: DataFrame writes to a partitioned engine table are 
rejected") {
+    withSparkSQLConf("spark.paimon.format-table.implementation" -> "engine") {
+      withTable("t") {
+        sql("CREATE TABLE t (a INT, b STRING, pt STRING) USING csv PARTITIONED 
BY (pt)")
+        sql("INSERT INTO t VALUES (1, 'old1', 'p1')")
+
+        // Spark's V2 file write does not write partition directories, so 
these rows would land
+        // in the table root, where the partitioned file index never reads 
them.
+        val source = Seq((5, "x5", "p1")).toDF("a", "b", "pt")
+        val writes: Seq[() => Unit] = Seq(
+          () => source.writeTo("t").append(),
+          () => source.writeTo("t").overwrite(col("pt") === "p1"),
+          () => source.write.insertInto("t"),
+          () => source.write.mode("overwrite").insertInto("t")
+        )
+        writes.foreach {
+          write =>
+            val e = intercept[UnsupportedOperationException](write())
+            assert(e.getMessage.contains("partitioned engine format table"))
+        }
+        checkAnswer(sql("SELECT * FROM t"), Row(1, "old1", "p1"))
+
+        // SQL INSERT is rewritten to Spark's V1 write and is unaffected.
+        sql("INSERT INTO t VALUES (5, 'x5', 'p1')")
+        checkAnswer(sql("SELECT * FROM t"), Seq(Row(1, "old1", "p1"), Row(5, 
"x5", "p1")))
+
+        // RTAS writes the same way, so it is rejected before the old table is 
dropped.
+        val replaces: Seq[() => Unit] = Seq(
+          () =>
+            sql(
+              "CREATE OR REPLACE TABLE t USING csv PARTITIONED BY (pt) " +
+                "AS SELECT 6 AS a, 'x6' AS b, 'p1' AS pt"),
+          () => 
source.write.format("csv").partitionBy("pt").mode("overwrite").saveAsTable("t")
+        )
+        replaces.foreach {
+          replace =>
+            val e = intercept[UnsupportedOperationException](replace())
+            assert(e.getMessage.contains("partitioned engine format table"))
+        }
+        checkAnswer(sql("SELECT * FROM t"), Seq(Row(1, "old1", "p1"), Row(5, 
"x5", "p1")))
+      }
+      // Spark 3.2 and 3.3 have no SupportsOverwriteV2, so these V2 writes 
fail there
+      if (gteqSpark3_4) {
+        withTable("t") {
+          sql("CREATE TABLE t (a INT, b STRING) USING csv")
+          Seq((5, "x5")).toDF("a", "b").writeTo("t").append()
+          checkAnswer(sql("SELECT * FROM t"), Row(5, "x5"))
+          sql("CREATE OR REPLACE TABLE t USING csv AS SELECT 6 AS a, 'x6' AS 
b")
+          checkAnswer(sql("SELECT * FROM t"), Row(6, "x6"))
+        }
+      }
+    }
+  }
+
   test("Format table: create or replace as select supports table type change") 
{
     assume(gteqSpark3_4)
     withTable("t") {

Reply via email to