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 ff03b413cc [spark] Fix V1 insert with dynamic partition pruning
(#11122)
ff03b413cc is described below
commit ff03b413ccf858e3bb9b3c7ae2bdf781a6e22960
Author: Wenchao Wu <[email protected]>
AuthorDate: Fri Nov 13 18:54:58 2026 +0800
[spark] Fix V1 insert with dynamic partition pruning (#11122)
---
.../sql/DynamicPartitionPruningWriteTest.scala | 21 ++++
.../sql/DynamicPartitionPruningWriteTest.scala | 21 ++++
.../apache/paimon/spark/write/PaimonWrite.scala | 7 +-
.../scala/org/apache/spark/sql/PaimonUtils.scala | 3 +
.../sql/DynamicPartitionPruningWriteTestBase.scala | 110 +++++++++++++++++++++
5 files changed, 160 insertions(+), 2 deletions(-)
diff --git
a/paimon-spark/paimon-spark-3.2/src/test/scala/org/apache/paimon/spark/sql/DynamicPartitionPruningWriteTest.scala
b/paimon-spark/paimon-spark-3.2/src/test/scala/org/apache/paimon/spark/sql/DynamicPartitionPruningWriteTest.scala
new file mode 100644
index 0000000000..9a0977ba98
--- /dev/null
+++
b/paimon-spark/paimon-spark-3.2/src/test/scala/org/apache/paimon/spark/sql/DynamicPartitionPruningWriteTest.scala
@@ -0,0 +1,21 @@
+/*
+ * 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.paimon.spark.sql
+
+class DynamicPartitionPruningWriteTest extends
DynamicPartitionPruningWriteTestBase {}
diff --git
a/paimon-spark/paimon-spark-3.5/src/test/scala/org/apache/paimon/spark/sql/DynamicPartitionPruningWriteTest.scala
b/paimon-spark/paimon-spark-3.5/src/test/scala/org/apache/paimon/spark/sql/DynamicPartitionPruningWriteTest.scala
new file mode 100644
index 0000000000..9a0977ba98
--- /dev/null
+++
b/paimon-spark/paimon-spark-3.5/src/test/scala/org/apache/paimon/spark/sql/DynamicPartitionPruningWriteTest.scala
@@ -0,0 +1,21 @@
+/*
+ * 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.paimon.spark.sql
+
+class DynamicPartitionPruningWriteTest extends
DynamicPartitionPruningWriteTestBase {}
diff --git
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/write/PaimonWrite.scala
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/write/PaimonWrite.scala
index 3ed93b0dd3..1aa3bc6432 100644
---
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/write/PaimonWrite.scala
+++
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/write/PaimonWrite.scala
@@ -23,7 +23,7 @@ import org.apache.paimon.spark.SaveMode
import org.apache.paimon.spark.commands.WriteIntoPaimonTable
import org.apache.paimon.table.FileStoreTable
-import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.{DataFrame, PaimonUtils}
import org.apache.spark.sql.connector.write.V1Write
import org.apache.spark.sql.sources.InsertableRelation
@@ -33,7 +33,10 @@ class PaimonWrite(val table: FileStoreTable, saveMode:
SaveMode, options: Option
override def toInsertableRelation: InsertableRelation = {
(data: DataFrame, overwrite: Boolean) =>
{
- WriteIntoPaimonTable(table, saveMode, data,
options).run(data.sparkSession)
+ // Spark may pass an optimized plan containing DPP subqueries which
cannot be reanalyzed
+ // by writer-side Dataset transformations.
+ val writeData = PaimonUtils.createNewDataFrame(data)
+ WriteIntoPaimonTable(table, saveMode, writeData,
options).run(data.sparkSession)
}
}
diff --git
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/PaimonUtils.scala
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/PaimonUtils.scala
index 18dbdfd498..45fd057bc1 100644
---
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/PaimonUtils.scala
+++
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/spark/sql/PaimonUtils.scala
@@ -45,6 +45,9 @@ import org.apache.spark.util.{Utils => SparkUtils}
object PaimonUtils {
/**
+ * Create a new [[DataFrame]] from the planned input, so writer-side
transformations do not
+ * reanalyze optimized logical plans containing dynamic pruning subqueries.
+ *
* In the streaming write case, An "Queries with streaming sources must be
executed with
* writeStream.start()" error will occur if we transform [[DataFrame]] first
and then use it.
*
diff --git
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/DynamicPartitionPruningWriteTestBase.scala
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/DynamicPartitionPruningWriteTestBase.scala
new file mode 100644
index 0000000000..0636d3cb9d
--- /dev/null
+++
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/DynamicPartitionPruningWriteTestBase.scala
@@ -0,0 +1,110 @@
+/*
+ * 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.paimon.spark.sql
+
+import org.apache.paimon.spark.PaimonSparkTestBase
+import org.apache.paimon.table.BucketMode
+
+import org.apache.spark.SparkConf
+import org.apache.spark.sql.Row
+import org.apache.spark.sql.catalyst.expressions.DynamicPruningSubquery
+
+import java.io.File
+
+abstract class DynamicPartitionPruningWriteTestBase extends
PaimonSparkTestBase {
+
+ import testImplicits._
+
+ override protected def sparkConf: SparkConf = {
+ super.sparkConf.set("spark.sql.catalog.paimon.cache-enabled", "false")
+ }
+
+ for {
+ bucketCount <- Seq(-1, 2)
+ adaptive <- Seq(false, true)
+ pruningEnabled <- Seq(false, true)
+ } {
+ test(s"V1 insert and update with bucket=$bucketCount, AQE=$adaptive,
DPP=$pruningEnabled") {
+ withSparkSQLConf(
+ "spark.paimon.write.use-v2-write" -> "false",
+ "spark.sql.optimizer.dynamicPartitionPruning.enabled" ->
pruningEnabled.toString,
+ "spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly" ->
"false",
+ "spark.sql.optimizer.dynamicPartitionPruning.useStats" -> "false",
+ "spark.sql.optimizer.dynamicPartitionPruning.fallbackFilterRatio" ->
"1.0",
+ "spark.sql.adaptive.enabled" -> adaptive.toString,
+ "spark.sql.shuffle.partitions" -> "2"
+ ) {
+ withTempDir {
+ dir =>
+ withTempView("dpp_source", "dpp_left", "dpp_right") {
+ withTable("dpp_target") {
+ sql(s"""
+ |CREATE TABLE dpp_target (id INT, pt STRING, left_value
INT, right_value INT)
+ |PARTITIONED BY (pt)
+ |TBLPROPERTIES ('primary-key' = 'id,pt', 'bucket' =
'$bucketCount',
+ | 'dynamic-bucket.assigner-parallelism' = '2')
+ |""".stripMargin)
+ val bucketMode =
+ if (bucketCount == -1) BucketMode.HASH_DYNAMIC else
BucketMode.HASH_FIXED
+ assert(loadTable("dpp_target").bucketMode() == bucketMode)
+
+ val rows = Seq((1, "p1", 10), (2, "p2", 20), (3, "p3", 30))
+ val extraRows = (4 until 104).map(i => (i, "p4", i))
+ Seq("dpp_source", "dpp_left", "dpp_right").foreach {
+ name =>
+ val input = name match {
+ case "dpp_source" => rows
+ case "dpp_left" => rows ++ extraRows
+ case _ => rows.filterNot(_._1 == 2) ++ extraRows
+ }
+ val path = new File(dir, name).getCanonicalPath
+ input.toDF("id", "pt",
"metric").write.partitionBy("pt").parquet(path)
+ spark.read.parquet(path).createOrReplaceTempView(name)
+ }
+
+ Seq(0, 100).foreach {
+ increment =>
+ val query = s"""
+ |SELECT a.id, a.pt, b.metric + $increment
AS left_value,
+ | c.metric AS right_value
+ |FROM dpp_source a
+ |LEFT JOIN dpp_left b ON a.id = b.id AND
a.pt = b.pt
+ |LEFT JOIN dpp_right c ON a.id = c.id AND
a.pt = c.pt
+ |WHERE a.id < 3
+ |""".stripMargin
+ val plan = sql(query).queryExecution.optimizedPlan
+ val pruning = plan.flatMap(_.expressions).flatMap {
+ expression =>
+ expression.collect { case subquery:
DynamicPruningSubquery => subquery }
+ }
+ assert(
+ pruning.nonEmpty == pruningEnabled,
+ s"Unexpected DPP in input plan:\n$plan")
+ sql(s"INSERT INTO dpp_target $query")
+ checkAnswer(
+ sql("SELECT * FROM dpp_target"),
+ Seq(Row(1, "p1", 10 + increment, 10), Row(2, "p2", 20 +
increment, null)))
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}