dongjoon-hyun opened a new pull request, #60290:
URL: https://github.com/apache/spark/pull/60290

   ### What changes were proposed in this pull request?
   
   This PR fixes a correctness issue in `UnionExec.outputPartitioning`. 
`UnionExec` now reports no partitioning when the output of one of its children 
repeats an `ExprId`, instead of passing its children's partitioning through.
   
   ### Why are the changes needed?
   
   This is a correctness issue. Since SPARK-52921 (4.1.0), `UnionExec` passes 
its children's partitioning through by replacing each child's attributes with 
the union's output attributes at the same position. That replacement is keyed 
by `ExprId`, while an attribute is read from the first position that carries 
its `ExprId` (`AttributeSeq.indexOf`). When a child output repeats an `ExprId`, 
the two disagree, and the union reports a hash partitioning on a column the 
rows were not hashed by. A parent then skips its exchange and returns wrong 
results.
   
   There are two cases.
   
   1. A later child repeats an `ExprId`, e.g. `[p, p, w]` hashed by `p`. Its 
shuffle hashes the first position, but the map keeps the last one, so the union 
reports the partitioning on its second output column.
   2. The first child repeats an `ExprId`, so the union's own output does. A 
later child hashed by its second column is reported as hashed by `p`, which a 
parent reads from the first position.
   
   The positions that share an `ExprId` hold different values when a union sits 
below, so the mapping cannot be repaired by choosing one of them.
   
   The analyzer normally replaces a repeated output column of a union child 
with a fresh `ExprId` (`DeduplicateUnionChildOutput`), so this requires 
`spark.sql.analyzer.unionIsResolvedWhenDuplicatesPerChildResolved=false` and 
`spark.sql.enforceTypeCoercionBeforeUnionDeduplication.enabled=false`.
   
   ```scala
   
spark.conf.set("spark.sql.analyzer.unionIsResolvedWhenDuplicatesPerChildResolved",
 "false")
   
spark.conf.set("spark.sql.enforceTypeCoercionBeforeUnionDeduplication.enabled", 
"false")
   spark.conf.set("spark.sql.adaptive.enabled", "false")
   spark.conf.set("spark.sql.shuffle.partitions", "5")
   
   val c0 = spark.range(100).selectExpr("id p", "id w").select($"p", $"p", $"w")
   val c1 = spark.range(1000).selectExpr("id % 10 q", "id r", "id 
w2").repartition($"r")
   val df = 
c0.repartition(c0.col("p")).union(c1).groupBy(c0.col("p")).agg(sum($"w"))
   
   // Issue: It should return 100 rows, but returns 140 rows with duplicate 
groups.
   df.count()
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A query over a union whose child output repeats an `ExprId` returns 
correct results. This only happens when the two analyzer configurations above 
are disabled. The released versions since 4.1.0 are affected.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case in 
`DataFrameSetOperationsSuite`. It covers both cases and fails without this fix.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Fable 5.2
   


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