ulysses-you opened a new pull request, #60330: URL: https://github.com/apache/spark/pull/60330
### What changes were proposed in this pull request? Backport of #60271 to `branch-4.2`. `UnionExec.prepareOutputPartitioning` maps each child's partitioning onto the union's output so that the children's layouts can be compared. A member of a child's partitioning that references an attribute the child does not output is now dropped, and a child with no member left reports `UnknownPartitioning`, which leaves the whole union unknown. This branch maps the other children onto the first child's attributes, so the filter is applied to every child before that mapping. Fixes [SPARK-61211](https://issues.apache.org/jira/browse/SPARK-61211). ### Why are the changes needed? An unmapped attribute could equal another child's real output attribute, so the union advertised a layout its rows do not have. `EnsureRequirements` then omitted a necessary exchange and returned wrong results without an error. With default configs: ```scala val kv = spark.range(1000).as[Long].groupByKey(x => (x % 10, x % 7)) val c = kv.keys.toDF().repartition($"_1").cache() c.count() c.select("_1").union(c.select("_2")).groupBy("_1").count().count() // 10 expected, 38 before this PR (AQE on and off) ``` A column-pruned `InMemoryTableScanExec` reports the cached plan's partitioning over a column it does not output; that fix (SPARK-61326) is not on this branch. `MapGroupsExec` and `SerializeFromObjectExec` do the same with the grouping key. The repeated-`ExprId` case is a separate issue (SPARK-61356) and is not part of this backport. ### Does this PR introduce _any_ user-facing change? Yes, a correctness fix for queries that have returned wrong results since `spark.sql.unionOutputPartitioning` was added in 4.1.0. A union over such children now gets the exchange it needs, and members that still describe the child pass through. ### How was this patch tested? Four new tests in `DataFrameSetOperationsSuite`: a plan-level case, a grouped Dataset's keys with `mapGroups`, column-pruned cached scans, and a column-pruned cached shuffled join that keeps its exchange-free plan, with AQE on and off where it applies. All four fail before the fix. Related suites pass on this branch (777 tests): `DataFrameSetOperationsSuite`, `UnionCodegenSuite`, `KeyGroupedPartitioningSuite`, `EnsureRequirementsSuite`, `PlannerSuite`, `DatasetSuite`, `CachedTableSuite` and `InMemoryColumnarQuerySuite`, plus scalastyle. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Claude Opus 5.5) 🤖 Generated with [Claude Code](https://claude.com/claude-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]
