peter-toth commented on code in PR #59599:
URL: https://github.com/apache/spark/pull/59599#discussion_r4339248445


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/KeyGroupedPartitionedScan.scala:
##########
@@ -18,13 +18,51 @@
 package org.apache.spark.sql.execution
 
 import org.apache.spark.sql.catalyst.InternalRow
-import org.apache.spark.sql.catalyst.expressions.RowOrdering
+import org.apache.spark.sql.catalyst.expressions.{Expression, RowOrdering}
 import org.apache.spark.sql.catalyst.plans.physical.{KeyGroupedPartitioning, 
KeyGroupedShuffleSpec}
 import org.apache.spark.sql.catalyst.util.InternalRowComparableWrapper
 import org.apache.spark.sql.execution.joins.StoragePartitionJoinParams
 
 /** Base trait for a data source scan capable of producing a key-grouped 
output. */
 trait KeyGroupedPartitionedScan[T] {
+  /**
+   * Return join-key positions for `outputPartitioning`. 
[[getInputPartitionGrouping]] always
+   * uses `spjParams.joinKeyPositions`. Skip output projection for a 
leading-prefix subset of
+   * partition keys when common-partition metadata remains at full width; 
projecting output
+   * misaligns nested siblings in a
+   * [[org.apache.spark.sql.catalyst.plans.physical.PartitioningCollection]].
+   */
+  private def joinKeyPositionsForOutput(
+      spjParams: StoragePartitionJoinParams,
+      baseExpressions: Seq[Expression]): Option[Seq[Int]] = {
+    spjParams.joinKeyPositions match {
+      case Some(pos) if pos.length < baseExpressions.length &&
+          pos == (0 until pos.length).toSeq &&
+          spjParams.commonPartitionValues.exists(
+            _.exists(_._1.numFields == baseExpressions.length)) =>
+        None
+      case other => spjParams.joinKeyPositions
+    }
+  }
+
+  private def projectCommonPartitionValuesIfNeeded(
+      commonPartValues: Seq[(InternalRow, Int)],
+      baseExpressions: Seq[Expression],
+      joinKeyPositions: Option[Seq[Int]]): Seq[(InternalRow, Int)] = {
+    joinKeyPositions match {
+      case Some(projectionPositions) =>
+        // Project only when common values match full partition-key width.
+        commonPartValues.map { case (partValue, numSplits) =>
+          if (partValue.numFields == baseExpressions.length) {

Review Comment:
   **Finding 4.** This projects full-width common values onto the composed 
positions without `distinct`, unlike the `None` branch at L91-97. So 
`numPartitions` counts the duplicates, while `getInputPartitionGrouping` builds 
its RDD from the de-duplicated `uniquePartitionValues`.
   
   With `a` and `b` as `(store_id INT, dept_id INT, data STRING) PARTITIONED BY 
(store_id, dept_id)`, both holding `(1, 2)` and `(2, 2)`, and `c` partitioned 
by `dept_id` holding depts 1 to 3, this query fails with 
`IllegalArgumentException: Can't zip RDDs with unequal numbers of partitions: 
List(1, 2)`, AQE on and off, under 
`allowJoinKeysSubsetOfPartitionKeys.enabled=true` and 
`requireAllClusterKeysForCoPartition=false`:
   
   ```sql
   SELECT * FROM a JOIN b ON a.store_id = b.store_id AND a.dept_id = b.dept_id
   JOIN c ON a.dept_id = c.dept_id
   ```
   
   It is correct on `branch-4.1`. The partially clustered branch has the same 
gap: `commonPartValuesMap` at L166-170 is built with `.toMap`, so duplicate 
projected keys keep only the last split count. Deduplicating after the 
projection, and summing the split counts of the merged keys, would fix both.
   
   
   ---
   _Generated by [Claude Code](https://claude.ai/code)_



-- 
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