You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ch...@apache.org on 2026/04/22 06:19:03 UTC

(kyuubi) branch master updated: [KYUUBI #7335] DynamicPartitionDataSingleWriter needs sort before write

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 100446cf6e [KYUUBI #7335] DynamicPartitionDataSingleWriter needs sort before write
100446cf6e is described below

commit 100446cf6edc4326a759cb34226d9c03e4de8ae6
Author: lifumao <li...@tencent.com>
AuthorDate: Wed Apr 22 14:18:53 2026 +0800

    [KYUUBI #7335] DynamicPartitionDataSingleWriter needs sort before write
    
    ### Why are the changes needed?
    
    Fix https://github.com/apache/kyuubi/issues/7335. For DynamicPartitionDataSingleWriter, the records to be written are required to be sorted on partition and/or bucket column(s) before writing. See [FileFormatDataWriter.scala](https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileFormatDataWriter.scala#L353-L363)
    
    ### How was this patch tested?
    
    UT
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    NO
    
    Closes #7414 from maomaodev/kyuubi-7335.
    
    Closes #7335
    
    b720bd8fd [lifumao] DynamicPartitionDataSingleWriter needs sort before write
    
    Authored-by: lifumao <li...@tencent.com>
    Signed-off-by: Cheng Pan <ch...@apache.org>
---
 .../spark/connector/hive/write/HiveWrite.scala     | 15 ++++++-
 .../spark/connector/hive/HiveQuerySuite.scala      | 49 ++++++++++++++++++++++
 2 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/extensions/spark/kyuubi-spark-connector-hive/src/main/scala/org/apache/kyuubi/spark/connector/hive/write/HiveWrite.scala b/extensions/spark/kyuubi-spark-connector-hive/src/main/scala/org/apache/kyuubi/spark/connector/hive/write/HiveWrite.scala
index df18f1f8be..cb66221095 100644
--- a/extensions/spark/kyuubi-spark-connector-hive/src/main/scala/org/apache/kyuubi/spark/connector/hive/write/HiveWrite.scala
+++ b/extensions/spark/kyuubi-spark-connector-hive/src/main/scala/org/apache/kyuubi/spark/connector/hive/write/HiveWrite.scala
@@ -32,7 +32,9 @@ import org.apache.spark.sql.SparkSession
 import org.apache.spark.sql.catalyst.InternalRow
 import org.apache.spark.sql.catalyst.catalog.CatalogTable
 import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec
-import org.apache.spark.sql.connector.write.{BatchWrite, LogicalWriteInfo, Write}
+import org.apache.spark.sql.connector.distributions.{Distribution, Distributions}
+import org.apache.spark.sql.connector.expressions.{Expressions, SortDirection, SortOrder}
+import org.apache.spark.sql.connector.write.{BatchWrite, LogicalWriteInfo, RequiresDistributionAndOrdering}
 import org.apache.spark.sql.execution.datasources.{BasicWriteJobStatsTracker, WriteJobDescription}
 import org.apache.spark.sql.execution.datasources.v2.FileBatchWrite
 import org.apache.spark.sql.execution.metric.SQLMetric
@@ -50,7 +52,8 @@ case class HiveWrite(
     info: LogicalWriteInfo,
     hiveTableCatalog: HiveTableCatalog,
     forceOverwrite: Boolean,
-    dynamicPartition: Map[String, Option[String]]) extends Write with Logging {
+    dynamicPartition: Map[String, Option[String]])
+  extends RequiresDistributionAndOrdering with Logging {
 
   private val options = info.options()
 
@@ -73,6 +76,14 @@ case class HiveWrite(
 
   override def description(): String = "Kyuubi-Hive-Connector"
 
+  override def requiredDistribution(): Distribution = Distributions.unspecified()
+
+  override def requiredOrdering(): Array[SortOrder] = {
+    partColumns.map { col =>
+      Expressions.sort(Expressions.column(col.name), SortDirection.ASCENDING)
+    }.toArray
+  }
+
   override def toBatch: BatchWrite = {
     val tmpLocation = HiveWriteHelper.getExternalTmpPath(externalCatalog, hadoopConf, tableLocation)
 
diff --git a/extensions/spark/kyuubi-spark-connector-hive/src/test/scala/org/apache/kyuubi/spark/connector/hive/HiveQuerySuite.scala b/extensions/spark/kyuubi-spark-connector-hive/src/test/scala/org/apache/kyuubi/spark/connector/hive/HiveQuerySuite.scala
index 1e0b1ea756..5cfde6bd55 100644
--- a/extensions/spark/kyuubi-spark-connector-hive/src/test/scala/org/apache/kyuubi/spark/connector/hive/HiveQuerySuite.scala
+++ b/extensions/spark/kyuubi-spark-connector-hive/src/test/scala/org/apache/kyuubi/spark/connector/hive/HiveQuerySuite.scala
@@ -117,6 +117,55 @@ class HiveQuerySuite extends KyuubiHiveTest {
     }
   }
 
+  test("[KYUUBI #7335] DynamicPartitionDataSingleWriter needs sort before write") {
+    withSparkSession(Map(
+      "hive.exec.dynamic.partition.mode" -> "nonstrict",
+      "spark.sql.shuffle.partitions" -> "1")) { spark =>
+      val table = "hive.default.test_part_table"
+      val tempTable = "hive.default.test_part_table_tmp"
+      withTable(table, tempTable) {
+        spark.sql(
+          s"""
+             | CREATE TABLE $table (
+             |   word STRING,
+             |   num BIGINT
+             | ) PARTITIONED BY (dt STRING)
+             | STORED AS ORC
+             |""".stripMargin)
+        spark.sql(
+          s"""
+             | CREATE TABLE $tempTable (
+             |   word STRING,
+             |   num BIGINT,
+             |   dt STRING
+             | ) STORED AS ORC
+             |""".stripMargin)
+        spark.sql(
+          s"""
+             | INSERT INTO $tempTable VALUES
+             | ('1', 1, '1111'),
+             | ('2', 2, '2222'),
+             | ('3', 4, '1111')
+             |""".stripMargin)
+
+        spark.sql(
+          s"""
+             | INSERT OVERWRITE TABLE $table PARTITION (dt)
+             | SELECT word, num, dt
+             | FROM $tempTable
+             | ORDER BY word
+             |""".stripMargin).collect()
+
+        checkAnswer(
+          spark.sql(s"SELECT * FROM $table"),
+          Seq(
+            Row("1", 1L, "1111"),
+            Row("2", 2L, "2222"),
+            Row("3", 4L, "1111")))
+      }
+    }
+  }
+
   test("[KYUUBI #4525] Partitioning predicates should take effect to filter data") {
     withSparkSession(Map("hive.exec.dynamic.partition.mode" -> "nonstrict")) { spark =>
       val table = "hive.default.employee"