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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/EnsureRequirements.scala:
##########
@@ -192,8 +202,11 @@ case class EnsureRequirements(
       // from one member, otherwise the sides end up grouped on different 
keys, or on a key set the
       // child does not even have. Pick that member once, preferring the 
finest when several
       // qualify. Only the branch that shuffles a child reads these, hence 
`lazy`.
+      // A child with a member built from a reduced scan only looks compatible 
on the transform
+      // the scan still reports, so it is shuffled onto the best spec instead.
       lazy val matchedIndexes = bestSpecOpt.toSeq.flatMap { best =>
-        childrenIndexes.filter(i => best.isCompatibleWith(specs(i)))
+        childrenIndexes.filter(i => best.isCompatibleWith(specs(i)) &&

Review Comment:
   **Finding 1.** When `nestedShufflesFollowPush` refuses the push 
(`:702-719`), `checkKeyGroupCompatible` returns `None` and the children go 
through `matchedIndexes` here. With `allowCompatibleTransforms` on:
   - `KeyGroupedShuffleSpec.isCompatibleWith` accepts `bucket(12, id)` against 
`bucket(8, id)`;
   - it then compares their raw `partitionValues` 
(`sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/physical/partitioning.scala:877`).
   
   Those values come from two different transforms. When they happen to 
coincide, both children count as matched and neither is shuffled. 
`areKeysCompatible` already notes the same domain problem for unknown-keyed 
sides and requires `isSameFunction` there (`partitioning.scala:908`).
   
   I ran this with broadcast off, and `shuffle.enabled` and 
`allowCompatibleTransforms` on. `pa12` is `bucket(12, id)`, `pb8` is `bucket(8, 
id)` and `pd` is unpartitioned, all holding BIGINT ids 0-7 and 12:
   
   ```sql
   SELECT /*+ MERGE(pa12, pd, pb8) */ pa12.id, pd.id, pb8.id
   FROM testcat.ns.pa12 JOIN testcat.ns.pd ON pa12.id = pd.id
   JOIN testcat.ns.pb8 ON pa12.id = pb8.id
   ```
   
   | | AQE on | AQE off |
   |---|---|---|
   | branch-4.0 | `PartitioningCollection requires all of its partitionings 
have the same numPartitions` | `Can't zip RDDs with unequal numbers of 
partitions: List(4, 8)` |
   | this PR | ids 0-7, 12 missing | ids 0-7, 12 missing |
   | branch-4.1 before #60058 (`44871c8fda5^`) | same as branch-4.0 | same as 
branch-4.0 |
   | branch-4.1 now (`eadba0e0592`) | ids 0-7, 12 missing | ids 0-7, 12 missing 
|
   
   The sequence:
   1. `pd` is shuffled onto `pa12`'s layout.
   2. The outer join would need `12 -> 4` and `8 -> 4`, which the nested 
shuffle cannot follow, so the push is refused.
   3. `pa12` and `pb8` both report the values `[0..7]`, so they match. Yet id 
12 is in partition 0 of `pa12` and in partition 4 of `pb8`.
   
   Keeping a child out of `matchedIndexes` when its matching member uses other 
functions than the best spec makes that child shuffle instead:
   
   ```scala
         def sameFunctions(a: ShuffleSpec, b: ShuffleSpec): Boolean = (a, b) 
match {
           case (x: KeyGroupedShuffleSpec, y: KeyGroupedShuffleSpec) =>
             x.isCompatibleWith(y) &&
               
x.partitioning.expressions.zip(y.partitioning.expressions).forall {
                 case (l: TransformExpression, r: TransformExpression) => 
l.isSameFunction(r)
                 case _ => true
               }
           case (ShuffleSpecCollection(xs), _) => xs.exists(sameFunctions(_, b))
           case (_, ShuffleSpecCollection(ys)) => ys.exists(sameFunctions(a, _))
           case _ => a.isCompatibleWith(b)
         }
         lazy val matchedIndexes = bestSpecOpt.toSeq.flatMap { best =>
           childrenIndexes.filter(i => sameFunctions(best, specs(i)) &&
             !hasMemberBuiltFromReducedScan(children(i), specs(i)))
         }
   ```
   
   With it, the query returns all 9 rows with 2 shuffles in both modes, and 
`KeyGroupedPartitioningSuite` and `EnsureRequirementsSuite` pass. Could you 
open a SPARK-60460 follow-up on branch-4.1 with this fix and a test along the 
lines of the query above? Once we have reviewed it there, this backport can 
carry it.
   



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