pan3793 commented on code in PR #56961:
URL: https://github.com/apache/spark/pull/56961#discussion_r3531245122


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -938,6 +945,24 @@ case class UnionExec(children: Seq[SparkPlan]) extends 
SparkPlan with CodegenSup
         // Take the output attributes of this union and map the partitioner to 
them.
         val attributeMap = children.head.output.zip(output).toMap
         partitioner match {
+          case headKp: KeyedPartitioning =>
+            // A `UnionExec` concatenates its children's partitions in order 
(one child's
+            // partitions after another's), so the merged `KeyedPartitioning` 
carries the
+            // concatenation of the children's partition keys, one key per 
physical output
+            // partition. Children usually hold different key sets, so the 
merged keys often
+            // contain duplicates and `isGrouped` is false; a downstream 
`GroupPartitionsExec`
+            // regroups partitions that share a key. The children's 
expressions have already
+            // been remapped to the first child's attributes by 
`prepareOutputPartitioning`;
+            // here they are remapped to the union's output attributes.
+            val mergedKeys = partitionings.flatMap {
+              case k: KeyedPartitioning => k.partitionKeys
+              case _ => return super.outputPartitioning
+            }
+            val mergedExpressions = headKp.expressions.map(_.transform {
+              case a: Attribute if attributeMap.contains(a) => attributeMap(a)
+            })
+            val isGrouped = mergedKeys.distinct.size == mergedKeys.size
+            KeyedPartitioning(mergedExpressions, mergedKeys, isGrouped)

Review Comment:
   @peter-toth, thank you for pointing out this correctness issue, applied this 
fix.



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