ammarchalifah commented on code in PR #59410:
URL: https://github.com/apache/spark/pull/59410#discussion_r4268406023


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/KeyGroupedPartitioningSuite.scala:
##########
@@ -2908,6 +2908,72 @@ class KeyGroupedPartitioningSuite extends 
DistributionAndOrderingSuiteBase {
     }
   }
 
+  test("SPARK-60203: SPJ with join-key subset projects originalPartitionValues 
when AQE " +
+      "is enabled") {
+    val table1 = "table1"
+    val table2 = "table2"
+    val cols = Array(
+      Column.create("id", StringType),
+      Column.create("date", StringType),
+      Column.create("data", StringType))
+    // The join key is the second partition key of `table1` (`bucket(32, 
id)`), after the
+    // leading identity key `date`. After SPJ pushdown, callers such as 
ValidateRequirements
+    // invoke `createShuffleSpec` on the scan's projected 
`outputPartitioning`, which
+    // re-projects `originalPartitionValues` using the already-trimmed 
expressions.
+    createTable(table1, cols,
+      Array(identity("date"), bucket(32, "id"), bucket(32, "data")))
+    sql(s"INSERT INTO testcat.ns.$table1 VALUES " +
+        "('a', '2026-05-01', 'aa'), " +
+        "('b', '2026-05-01', 'bb'), " +
+        "('c', '2026-05-02', 'aa')")
+
+    createTable(table2, cols, Array(bucket(32, "id")))
+    sql(s"INSERT INTO testcat.ns.$table2 VALUES " +
+        "('a', '2026-05-01', 'xx'), " +
+        "('b', '2026-05-01', 'yy'), " +
+        "('d', '2026-05-01', 'zz')")
+
+    Seq(true, false).foreach { partiallyClustered =>
+      withSQLConf(
+        SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+        SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+        SQLConf.SKEW_JOIN_ENABLED.key -> "true",
+        SQLConf.REQUIRE_ALL_CLUSTER_KEYS_FOR_CO_PARTITION.key -> "false",
+        SQLConf.V2_BUCKETING_ALLOW_JOIN_KEYS_SUBSET_OF_PARTITION_KEYS.key -> 
"true",
+        SQLConf.V2_BUCKETING_PARTIALLY_CLUSTERED_DISTRIBUTION_ENABLED.key ->
+            partiallyClustered.toString) {
+        val df = sql(
+          s"""
+             |${selectWithMergeJoinHint("t1", "t2")}
+             |t1.id, t1.date, t1.data AS t1data, t2.data AS t2data
+             |FROM testcat.ns.$table1 t1 JOIN testcat.ns.$table2 t2
+             |ON t1.id = t2.id
+             |ORDER BY t1.id, t1.date, t1data
+             |""".stripMargin)
+        val executedPlan = df.queryExecution.executedPlan
+        val scans = collectScans(executedPlan)
+        assert(
+          scans.exists(_.spjParams.joinKeyPositions.contains(Seq(1))),
+          "SPJ should project the scan partitioning down to the bucket join 
key")
+        val shuffles = collectShuffles(executedPlan)
+        assert(shuffles.isEmpty, "SPJ should be triggered")
+        val smjs = collect(executedPlan) { case j: SortMergeJoinExec => j }
+        assert(smjs.size == 1, "expected a sort-merge join")
+        val smj = smjs.head
+        val projectedScan = 
scans.find(_.spjParams.joinKeyPositions.contains(Seq(1))).get
+        val kgp = 
projectedScan.outputPartitioning.asInstanceOf[KeyGroupedPartitioning]
+        assert(kgp.originalPartitionValues.nonEmpty)
+        assert(kgp.originalPartitionValues.forall(_.numFields == 
kgp.expressions.length),
+          "originalPartitionValues should be projected to the join keys")
+        // Without the fix, this throws when a string leading partition value 
is read as int.

Review Comment:
   Committed the change according to your recommendation



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to