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


##########
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 7.** Only values at the full partition-key width are projected. 
Values that an inner subset join already projected to an intermediate width 
stay as they are when a later push narrows the positions further. For `A` 
partitioned by `(a, b, c)` joined to `B` on `(b, c)` and then on `c` alone, the 
scan keeps `(b, c)` rows under a `[c]` key, so the comparisons read `b` as `c`.
   
   I traced this by reading and did not run it. Recording the layout the values 
were projected to, or always keeping them at full width and projecting on read, 
would avoid guessing it from `numFields`.
   
   
   ---
   _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