dongjoon-hyun commented on code in PR #60290:
URL: https://github.com/apache/spark/pull/60290#discussion_r4527266724


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -1177,6 +1177,16 @@ case class UnionExec(children: Seq[SparkPlan]) extends 
SparkPlan with CodegenSup
    * reports unknown.
    */
   private def rawPartitioning: Partitioning = {
+    // An attribute is read from the first position that carries its `ExprId` 
(see
+    // `AttributeSeq.indexOf`), so the positions of an output that repeats one 
cannot be told
+    // apart by attribute, and they hold different values when a union sits 
below. A child's
+    // partitioning then has no reliable counterpart among this union's output 
attributes, whether
+    // the repeat is in that child's output or, through the first child, in 
this union's own.
+    // The analyzer normally leaves no such child 
(`DeduplicateUnionChildOutput`).
+    if (children.exists(c => c.output.map(_.exprId).distinct.size < 
c.output.size)) {

Review Comment:
   Thank you for the review. The guard is intentionally conservative. A 
repeated `ExprId` reaches `UnionExec` only when both internal analyzer 
configurations are disabled, and for that mode this PR prefers a check that is 
easy to verify over a per-column translation. The cost is one more shuffle in 
those sessions for a union whose child repeats an `ExprId`, and plans under the 
default configurations do not change. The same guard also applies to 
`branch-4.3`, `branch-4.2` and `branch-4.1`, where the translation code is 
different.
   
   For the recomputation, the check is a `@transient lazy val` now.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSetOperationsSuite.scala:
##########
@@ -1743,6 +1743,52 @@ class DataFrameSetOperationsSuite extends 
SharedSparkSession with AdaptiveSparkP
     }
   }
 
+  test("SPARK-61356: union outputPartitioning when a child output repeats an 
ExprId") {
+    // `SELECT p, p, w` leaves two output positions with one ExprId. The 
analyzer normally replaces
+    // a repeated output column with a fresh ExprId 
(`DeduplicateUnionChildOutput`); these confs
+    // keep it so the physical union sees it.
+    withSQLConf(
+        SQLConf.UNION_IS_RESOLVED_WHEN_DUPLICATES_PER_CHILD_RESOLVED.key -> 
"false",
+        SQLConf.ENFORCE_TYPE_COERCION_BEFORE_UNION_DEDUPLICATION.key -> 
"false",
+        SQLConf.ANALYZER_DUAL_RUN_LEGACY_AND_SINGLE_PASS_RESOLVER.key -> 
"false",
+        SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+        SQLConf.SHUFFLE_PARTITIONS.key -> "5") {
+      def repeated: DataFrame = spark.range(10).selectExpr("id p", "id 
w").select($"p", $"p", $"w")
+
+      // A later child repeats the ExprId. `r` is a union, so for `p2`'s rows 
its second position
+      // holds `r` while its first holds `q`, and its shuffle hashes the first 
one. Reading that
+      // partitioning as one on `c`, the second position, would let the 
aggregate skip its exchange
+      // and split a group over partitions.
+      val p1 = repeated
+      val p2 = spark.range(10, 110).selectExpr("id q", "id % 10 r", "id w2")
+      val r = p1.union(p2).repartition(p1.col("p"))
+      val x = spark.range(10).selectExpr("id x", "id c", "id 
u").repartition($"c")
+      // All three columns are referenced, so `ColumnPruning` adds no 
`Project` below the union.
+      val laterChild = x.union(r).groupBy($"c").agg(sum($"x"), sum($"u"))
+
+      // The first child repeats the ExprId, so this union's own output does. 
Its second child is
+      // hashed by `r`, the second position, while `p` is read from the first 
one.
+      val c0 = repeated
+      val c1 = spark.range(100).selectExpr("id % 10 q", "id r", "id 
w2").repartition($"r")
+      val firstChild = 
c0.repartition(c0.col("p")).union(c1).groupBy(c0.col("p")).agg(sum($"w"))
+
+      Seq(
+        laterChild -> (0 until 10).map(k => Row(k, 12 * k + 550, 12 * k + 
550)),
+        firstChild -> (0 until 10).map(k => Row(k, 11 * k + 450))
+      ).foreach { case (df, expected) =>
+        val unionExec = df.queryExecution.executedPlan.collect { case u: 
UnionExec => u }.head
+        // The analyzer can drop this shape, so assert it survived rather than 
letting the test
+        // go vacuous: hash-partitioned children, one of them repeating an 
ExprId.
+        
assert(unionExec.children.forall(_.outputPartitioning.isInstanceOf[HashPartitioning])
 &&

Review Comment:
   Thank you. I confirmed it. Without this PR, that shape returns split groups, 
`[1,1]` three times and `[2,1]` twice instead of `[1,3]` and `[2,2]`. I added 
it to the test as the third case. Note that the keyed merge (SPARK-57881) is in 
4.3.0 already, so this case affects a released version.
   



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