You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "ulysses-you (via GitHub)" <gi...@apache.org> on 2026/07/24 05:12:08 UTC
[PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
ulysses-you opened a new pull request, #57491:
URL: https://github.com/apache/spark/pull/57491
### What changes were proposed in this pull request?
`UnionExec.outputPartitioning` (SPARK-52921) passes a child partitioning through the union when every child is compatibly partitioned, letting a downstream operator (e.g. an aggregate) skip a shuffle. This PR extends that pass-through to children whose `outputPartitioning` is a `PartitioningCollection`.
- `outputPartitioning` now treats each child's partitioning as a set of candidate partitionings (a `PartitioningCollection` flattens to its members; a single partitioning is a one-element set) and passes through the **intersection** across all children: empty -> `UnknownPartitioning`; one member -> that partitioning; many -> a `PartitioningCollection`. Only index-co-locatable partitionings (`HashPartitioningLike` / `SinglePartition`) participate.
- The existing `KeyedPartitioning` concatenation path is kept as a separate case that fires only when every child is a single `KeyedPartitioning`. It is a distinct physical strategy (`sparkContext.union`, `numPartitions = sum`) and cannot be folded into the co-located intersection (`SQLPartitioningAwareUnionRDD`, `numPartitions = N`), because a `PartitioningCollection` requires uniform `numPartitions` across its members.
- `comparePartitioning` is documented as a leaf-only equivalence predicate; collections are flattened before it is called.
### Why are the changes needed?
`comparePartitioning` had no case for `PartitioningCollection`, so whenever a child reported one, the whole union collapsed to `UnknownPartitioning` and an extra shuffle was inserted. This is hit by a common query shape - a `UNION ALL` where one branch is an inner shuffled-hash join (whose `outputPartitioning` is `PartitioningCollection(Hash(k1), Hash(k2))`) and the other is a left join (single `HashPartitioning`), feeding a `GROUP BY`:
```sql
select c1, c2, c3, count(*) from (
select t1.c1, t1.c2, t1.c3 from t1 join t2 on t1.c1 = t2.c1
union all
select t3.c1, t3.c2, t3.c3 from t3 left join t4 on t3.c1 = t4.c1
) group by c1, c2, c3
```
The union failed to pass through and a redundant `ENSURE_REQUIREMENTS` shuffle appeared before the aggregate.
### Does this PR introduce _any_ user-facing change?
No change in query results. Under the default-on `spark.sql.unionOutputPartitioning`, affected plans drop a redundant shuffle.
### How was this patch tested?
Added tests to `DataFrameSetOperationsSuite` covering: the mixed inner+left join union (intersection to a single `HashPartitioning`, shuffle eliminated); all children reporting collections (pass-through as a `PartitioningCollection`); empty intersection (fall back to `UnknownPartitioning`); and the collection pass-through under AQE. Existing `DataFrameSetOperationsSuite` and `KeyGroupedPartitioningSuite` (97 tests) regressions pass.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on PR #57491:
URL: https://github.com/apache/spark/pull/57491#issuecomment-5098896225
thank you all !
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on PR #57491:
URL: https://github.com/apache/spark/pull/57491#issuecomment-5086422651
thank you @dongjoon-hyun, addressed comments
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #57491:
URL: https://github.com/apache/spark/pull/57491#discussion_r3646773557
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSetOperationsSuite.scala:
##########
@@ -1659,6 +1659,187 @@ class DataFrameSetOperationsSuite extends SharedSparkSession with AdaptiveSparkP
}
}
+ test("SPARK-58317: union partitioning - PartitioningCollection child intersects to single") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2", "t3", "t4") {
+ Seq((1, 2, 4), (1, 3, 5), (2, 2, 3)).toDF("c1", "c2", "c3").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+ Seq((1, 2, 4), (2, 4, 5), (3, 6, 7)).toDF("c1", "c2", "c3").createOrReplaceTempView("t3")
+ Seq((1, 9), (3, 9)).toDF("c1", "y").createOrReplaceTempView("t4")
+
+ // The first branch is an inner shuffled-hash join and selects both join keys (t1.c1 and
+ // t2.c1), so its output partitioning is a PartitioningCollection(Hash(c1), Hash(c1#..))
+ // that a downstream ProjectExec cannot narrow to a single member. The second branch is a
+ // left join, whose output partitioning is a single HashPartitioning(c1). The union should
+ // intersect the two to a single HashPartitioning(c1) and let the group-by skip a shuffle.
+ def unionDF: DataFrame = sql(
+ """SELECT c1, c2, c3, count(*) FROM (
+ | SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t1.c2, t1.c3, t2.c1 AS k
+ | FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ | UNION ALL
+ | SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t3.c2, t3.c3, t3.c1 AS k
+ | FROM t3 LEFT JOIN t4 ON t3.c1 = t4.c1
+ |) GROUP BY c1, c2, c3
Review Comment:
Since `k` is unused above the union, `ColumnPruning` drops it from both branches and each `ProjectExec` narrows the join's collection to a single `HashPartitioning(c1)`, so no `PartitioningCollection` reaches the union — this test passes on master without this PR. Referencing `k` in the group-by keeps the first branch's collection alive; aliasing `t4.c1` instead of `t3.c1` keeps the second branch a single `Hash(c1)`, so the intersection is single as intended.
```suggestion
| SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t3.c2, t3.c3, t4.c1 AS k
| FROM t3 LEFT JOIN t4 ON t3.c1 = t4.c1
|) GROUP BY c1, c2, c3, k
```
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on PR #57491:
URL: https://github.com/apache/spark/pull/57491#issuecomment-5066435638
cc @viirya @dongjoon-hyun @peter-toth thank you
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #57491:
URL: https://github.com/apache/spark/pull/57491#discussion_r3646779023
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSetOperationsSuite.scala:
##########
@@ -1659,6 +1659,187 @@ class DataFrameSetOperationsSuite extends SharedSparkSession with AdaptiveSparkP
}
}
+ test("SPARK-58317: union partitioning - PartitioningCollection child intersects to single") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2", "t3", "t4") {
+ Seq((1, 2, 4), (1, 3, 5), (2, 2, 3)).toDF("c1", "c2", "c3").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+ Seq((1, 2, 4), (2, 4, 5), (3, 6, 7)).toDF("c1", "c2", "c3").createOrReplaceTempView("t3")
+ Seq((1, 9), (3, 9)).toDF("c1", "y").createOrReplaceTempView("t4")
+
+ // The first branch is an inner shuffled-hash join and selects both join keys (t1.c1 and
+ // t2.c1), so its output partitioning is a PartitioningCollection(Hash(c1), Hash(c1#..))
+ // that a downstream ProjectExec cannot narrow to a single member. The second branch is a
+ // left join, whose output partitioning is a single HashPartitioning(c1). The union should
+ // intersect the two to a single HashPartitioning(c1) and let the group-by skip a shuffle.
+ def unionDF: DataFrame = sql(
+ """SELECT c1, c2, c3, count(*) FROM (
+ | SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t1.c2, t1.c3, t2.c1 AS k
+ | FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ | UNION ALL
+ | SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t3.c2, t3.c3, t3.c1 AS k
+ | FROM t3 LEFT JOIN t4 ON t3.c1 = t4.c1
+ |) GROUP BY c1, c2, c3
+ |""".stripMargin)
+
+ val correctResult = withSQLConf(SQLConf.UNION_OUTPUT_PARTITIONING.key -> "false") {
+ unionDF.collect()
+ }
+
+ val shuffleNums = Seq(true, false).map { enabled =>
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+ SQLConf.UNION_OUTPUT_PARTITIONING.key -> enabled.toString) {
+ val union = unionDF
+ val unionExec = union.queryExecution.executedPlan.collect { case u: UnionExec => u }
+ assert(unionExec.size == 1)
+
+ val partitioning = unionExec.head.outputPartitioning
+ if (enabled) {
+ assert(partitioning.isInstanceOf[HashPartitioning],
+ s"expected a HashPartitioning pass-through but got $partitioning")
+ } else {
+ assert(partitioning.isInstanceOf[UnknownPartitioning])
+ }
+
+ checkAnswer(union, correctResult)
+ union.queryExecution.executedPlan.collect {
+ case s: ShuffleExchangeExec => s
+ }.size
+ }
+ }
+ // Enabling the pass-through removes the shuffle before the aggregate.
+ assert(shuffleNums.head + 1 == shuffleNums.last)
+ }
+ }
+ }
+
+ test("SPARK-58317: union partitioning - all PartitioningCollection children pass through") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2", "t3", "t4") {
+ Seq((1, 2), (2, 3)).toDF("c1", "c2").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+ Seq((1, 2), (3, 4)).toDF("c1", "c2").createOrReplaceTempView("t3")
+ Seq((1, 9), (3, 9)).toDF("c1", "y").createOrReplaceTempView("t4")
+
+ // Both branches are inner shuffled-hash joins on a single key and select both sides' join
+ // key (t1.c1 and t2.c1 AS k), so a downstream ProjectExec cannot narrow either child's
+ // PartitioningCollection(Hash(c1), Hash(k)) to a single member. The union should intersect
+ // to a PartitioningCollection carrying both members.
+ def unionDF: DataFrame = sql(
+ """SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t2.c1 AS k
+ |FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ |UNION ALL
+ |SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t4.c1 AS k
+ |FROM t3 JOIN t4 ON t3.c1 = t4.c1
+ |""".stripMargin)
+
+ val correctResult = withSQLConf(SQLConf.UNION_OUTPUT_PARTITIONING.key -> "false") {
+ unionDF.collect()
+ }
+
+ Seq(true, false).foreach { enabled =>
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+ SQLConf.UNION_OUTPUT_PARTITIONING.key -> enabled.toString) {
+ val union = unionDF
+ val unionExec = union.queryExecution.executedPlan.collect { case u: UnionExec => u }
+ assert(unionExec.size == 1)
+
+ val partitioning = unionExec.head.outputPartitioning
+ if (enabled) {
+ assert(partitioning.isInstanceOf[PartitioningCollection],
+ s"expected a PartitioningCollection pass-through but got $partitioning")
+ val members = partitioning.asInstanceOf[PartitioningCollection].partitionings
+ assert(members.forall(_.isInstanceOf[HashPartitioning]))
+ assert(members.size == 2)
+ } else {
+ assert(partitioning.isInstanceOf[UnknownPartitioning])
+ }
+
+ checkAnswer(union, correctResult)
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-58317: union partitioning - empty intersection falls back") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2") {
+ Seq((1, 2, 4), (2, 3, 5)).toDF("c1", "c2", "c3").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+
+ // First branch reports PartitioningCollection(Hash(c1), Hash(c1#..)); the second branch
+ // is repartitioned on a disjoint column, so the intersection is empty.
+ def unionDF: DataFrame = sql(
+ """SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t1.c2, t1.c3, t2.c1 AS k
+ |FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ |UNION ALL
+ |SELECT c1, c2, c3, c2 AS k FROM t1 DISTRIBUTE BY c2
+ |""".stripMargin)
+
+ val correctResult = withSQLConf(SQLConf.UNION_OUTPUT_PARTITIONING.key -> "false") {
+ unionDF.collect()
+ }
+
+ Seq(true, false).foreach { enabled =>
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+ SQLConf.UNION_OUTPUT_PARTITIONING.key -> enabled.toString) {
+ val union = unionDF
+ val unionExec = union.queryExecution.executedPlan.collect { case u: UnionExec => u }
+ assert(unionExec.size == 1)
+ assert(unionExec.head.outputPartitioning.isInstanceOf[UnknownPartitioning])
+ checkAnswer(union, correctResult)
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-58317: union partitioning - PartitioningCollection pass-through under AQE") {
+ // AQE is enabled by default in production; the collection pass-through must produce correct
+ // results there too, where the union output flows through the coalesce-compatibility path.
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2", "t3", "t4") {
+ Seq((1, 2), (2, 3), (1, 4)).toDF("c1", "c2").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+ Seq((1, 2), (3, 4)).toDF("c1", "c2").createOrReplaceTempView("t3")
+ Seq((1, 9), (3, 9)).toDF("c1", "y").createOrReplaceTempView("t4")
+
+ def unionDF: DataFrame = sql(
+ """SELECT c1, count(*) FROM (
+ | SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t2.c1 AS k
+ | FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ | UNION ALL
+ | SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t4.c1 AS k
+ | FROM t3 JOIN t4 ON t3.c1 = t4.c1
+ |) GROUP BY c1
Review Comment:
Previously, `k` is pruned, so no `PartitioningCollection` reaches the union. Referencing `k` in the group-by keeps the collection alive under AQE.
```suggestion
|) GROUP BY c1, k
```
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #57491:
URL: https://github.com/apache/spark/pull/57491#discussion_r3646756356
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSetOperationsSuite.scala:
##########
@@ -1659,6 +1659,187 @@ class DataFrameSetOperationsSuite extends SharedSparkSession with AdaptiveSparkP
}
}
+ test("SPARK-58317: union partitioning - PartitioningCollection child intersects to single") {
Review Comment:
This test case seems to pass without this PR, @ulysses-you . Could you double-check this test case?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #57491:
URL: https://github.com/apache/spark/pull/57491#discussion_r3646779023
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSetOperationsSuite.scala:
##########
@@ -1659,6 +1659,187 @@ class DataFrameSetOperationsSuite extends SharedSparkSession with AdaptiveSparkP
}
}
+ test("SPARK-58317: union partitioning - PartitioningCollection child intersects to single") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2", "t3", "t4") {
+ Seq((1, 2, 4), (1, 3, 5), (2, 2, 3)).toDF("c1", "c2", "c3").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+ Seq((1, 2, 4), (2, 4, 5), (3, 6, 7)).toDF("c1", "c2", "c3").createOrReplaceTempView("t3")
+ Seq((1, 9), (3, 9)).toDF("c1", "y").createOrReplaceTempView("t4")
+
+ // The first branch is an inner shuffled-hash join and selects both join keys (t1.c1 and
+ // t2.c1), so its output partitioning is a PartitioningCollection(Hash(c1), Hash(c1#..))
+ // that a downstream ProjectExec cannot narrow to a single member. The second branch is a
+ // left join, whose output partitioning is a single HashPartitioning(c1). The union should
+ // intersect the two to a single HashPartitioning(c1) and let the group-by skip a shuffle.
+ def unionDF: DataFrame = sql(
+ """SELECT c1, c2, c3, count(*) FROM (
+ | SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t1.c2, t1.c3, t2.c1 AS k
+ | FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ | UNION ALL
+ | SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t3.c2, t3.c3, t3.c1 AS k
+ | FROM t3 LEFT JOIN t4 ON t3.c1 = t4.c1
+ |) GROUP BY c1, c2, c3
+ |""".stripMargin)
+
+ val correctResult = withSQLConf(SQLConf.UNION_OUTPUT_PARTITIONING.key -> "false") {
+ unionDF.collect()
+ }
+
+ val shuffleNums = Seq(true, false).map { enabled =>
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+ SQLConf.UNION_OUTPUT_PARTITIONING.key -> enabled.toString) {
+ val union = unionDF
+ val unionExec = union.queryExecution.executedPlan.collect { case u: UnionExec => u }
+ assert(unionExec.size == 1)
+
+ val partitioning = unionExec.head.outputPartitioning
+ if (enabled) {
+ assert(partitioning.isInstanceOf[HashPartitioning],
+ s"expected a HashPartitioning pass-through but got $partitioning")
+ } else {
+ assert(partitioning.isInstanceOf[UnknownPartitioning])
+ }
+
+ checkAnswer(union, correctResult)
+ union.queryExecution.executedPlan.collect {
+ case s: ShuffleExchangeExec => s
+ }.size
+ }
+ }
+ // Enabling the pass-through removes the shuffle before the aggregate.
+ assert(shuffleNums.head + 1 == shuffleNums.last)
+ }
+ }
+ }
+
+ test("SPARK-58317: union partitioning - all PartitioningCollection children pass through") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2", "t3", "t4") {
+ Seq((1, 2), (2, 3)).toDF("c1", "c2").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+ Seq((1, 2), (3, 4)).toDF("c1", "c2").createOrReplaceTempView("t3")
+ Seq((1, 9), (3, 9)).toDF("c1", "y").createOrReplaceTempView("t4")
+
+ // Both branches are inner shuffled-hash joins on a single key and select both sides' join
+ // key (t1.c1 and t2.c1 AS k), so a downstream ProjectExec cannot narrow either child's
+ // PartitioningCollection(Hash(c1), Hash(k)) to a single member. The union should intersect
+ // to a PartitioningCollection carrying both members.
+ def unionDF: DataFrame = sql(
+ """SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t2.c1 AS k
+ |FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ |UNION ALL
+ |SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t4.c1 AS k
+ |FROM t3 JOIN t4 ON t3.c1 = t4.c1
+ |""".stripMargin)
+
+ val correctResult = withSQLConf(SQLConf.UNION_OUTPUT_PARTITIONING.key -> "false") {
+ unionDF.collect()
+ }
+
+ Seq(true, false).foreach { enabled =>
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+ SQLConf.UNION_OUTPUT_PARTITIONING.key -> enabled.toString) {
+ val union = unionDF
+ val unionExec = union.queryExecution.executedPlan.collect { case u: UnionExec => u }
+ assert(unionExec.size == 1)
+
+ val partitioning = unionExec.head.outputPartitioning
+ if (enabled) {
+ assert(partitioning.isInstanceOf[PartitioningCollection],
+ s"expected a PartitioningCollection pass-through but got $partitioning")
+ val members = partitioning.asInstanceOf[PartitioningCollection].partitionings
+ assert(members.forall(_.isInstanceOf[HashPartitioning]))
+ assert(members.size == 2)
+ } else {
+ assert(partitioning.isInstanceOf[UnknownPartitioning])
+ }
+
+ checkAnswer(union, correctResult)
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-58317: union partitioning - empty intersection falls back") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2") {
+ Seq((1, 2, 4), (2, 3, 5)).toDF("c1", "c2", "c3").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+
+ // First branch reports PartitioningCollection(Hash(c1), Hash(c1#..)); the second branch
+ // is repartitioned on a disjoint column, so the intersection is empty.
+ def unionDF: DataFrame = sql(
+ """SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t1.c2, t1.c3, t2.c1 AS k
+ |FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ |UNION ALL
+ |SELECT c1, c2, c3, c2 AS k FROM t1 DISTRIBUTE BY c2
+ |""".stripMargin)
+
+ val correctResult = withSQLConf(SQLConf.UNION_OUTPUT_PARTITIONING.key -> "false") {
+ unionDF.collect()
+ }
+
+ Seq(true, false).foreach { enabled =>
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+ SQLConf.UNION_OUTPUT_PARTITIONING.key -> enabled.toString) {
+ val union = unionDF
+ val unionExec = union.queryExecution.executedPlan.collect { case u: UnionExec => u }
+ assert(unionExec.size == 1)
+ assert(unionExec.head.outputPartitioning.isInstanceOf[UnknownPartitioning])
+ checkAnswer(union, correctResult)
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-58317: union partitioning - PartitioningCollection pass-through under AQE") {
+ // AQE is enabled by default in production; the collection pass-through must produce correct
+ // results there too, where the union output flows through the coalesce-compatibility path.
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2", "t3", "t4") {
+ Seq((1, 2), (2, 3), (1, 4)).toDF("c1", "c2").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+ Seq((1, 2), (3, 4)).toDF("c1", "c2").createOrReplaceTempView("t3")
+ Seq((1, 9), (3, 9)).toDF("c1", "y").createOrReplaceTempView("t4")
+
+ def unionDF: DataFrame = sql(
+ """SELECT c1, count(*) FROM (
+ | SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t2.c1 AS k
+ | FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ | UNION ALL
+ | SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t4.c1 AS k
+ | FROM t3 JOIN t4 ON t3.c1 = t4.c1
+ |) GROUP BY c1
Review Comment:
```suggestion
|) GROUP BY c1, k
```
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #57491:
URL: https://github.com/apache/spark/pull/57491#discussion_r3646794488
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -937,47 +940,76 @@ case class UnionExec(children: Seq[SparkPlan]) extends SparkPlan with CodegenSup
}
override def outputPartitioning: Partitioning = {
- if (conf.getConf(SQLConf.UNION_OUTPUT_PARTITIONING)) {
- val partitionings = prepareOutputPartitioning()
- if (partitionings.forall(comparePartitioning(_, partitionings.head))) {
- val partitioner = partitionings.head
-
- // 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
- val isNarrowed = partitionings.exists {
- case k: KeyedPartitioning => k.isNarrowed
- case _ => false
- }
- KeyedPartitioning(mergedExpressions, mergedKeys, isGrouped, isNarrowed)
- case e: Expression =>
- e.transform {
- case a: Attribute if attributeMap.contains(a) => attributeMap(a)
- }.asInstanceOf[Partitioning]
- case _ => partitioner
- }
+ if (!conf.getConf(SQLConf.UNION_OUTPUT_PARTITIONING)) {
+ return super.outputPartitioning
+ }
+
+ // Children's partitionings with attributes remapped to the first child's attributes.
+ val partitionings = prepareOutputPartitioning()
+ // Map from the first child's attributes to this union's own output attributes.
+ val attributeMap = children.head.output.zip(output).toMap
+ def toUnionOutput(p: Partitioning): Partitioning = p match {
+ case e: Expression =>
+ e.transform {
+ case a: Attribute if attributeMap.contains(a) => attributeMap(a)
+ }.asInstanceOf[Partitioning]
+ case _ => p
+ }
+
+ // Case A: every child is a single `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. This concatenation (numPartitions = sum) is a
+ // distinct physical strategy from the co-located pass-through below (numPartitions = N), so
+ // it is kept as a separate case and never folded into a `PartitioningCollection`.
+ if (partitionings.forall(_.isInstanceOf[KeyedPartitioning])) {
+ val kps = partitionings.map(_.asInstanceOf[KeyedPartitioning])
+ val headKp = kps.head
+ // The `KeyedPartitioning`s must agree on the partition expressions to merge.
+ val compatible = kps.forall(comparePartitioning(_, headKp))
+ if (compatible) {
+ val mergedKeys = kps.flatMap(_.partitionKeys)
+ val mergedExpressions = headKp.expressions.map(_.transform {
+ case a: Attribute if attributeMap.contains(a) => attributeMap(a)
+ })
+ val isGrouped = mergedKeys.distinct.size == mergedKeys.size
+ val isNarrowed = kps.exists(_.isNarrowed)
+ return KeyedPartitioning(mergedExpressions, mergedKeys, isGrouped, isNarrowed)
} else {
- super.outputPartitioning
+ return super.outputPartitioning
}
- } else {
- super.outputPartitioning
+ }
+
+ // Case B: treat each child's partitioning as a set of candidate partitionings (a
+ // `PartitioningCollection` flattens to its members; a single partitioning is a one-element
+ // set) and pass through the intersection across all children. Only index-co-locatable
+ // partitionings participate; `KeyedPartitioning` is excluded here because its concatenation
+ // semantics (Case A) are incompatible with the co-located union RDD.
+ def flattenPartitioning(p: Partitioning): Seq[Partitioning] = p match {
Review Comment:
Please try to use the existing one.
https://github.com/apache/spark/blob/b65ef3e18fbe2fd499e1c48edd212cae79d377ba/sql/core/src/main/scala/org/apache/spark/sql/execution/AliasAwareOutputExpression.scala#L152-L160
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on PR #57491:
URL: https://github.com/apache/spark/pull/57491#issuecomment-5098892132
**Merge Summary:**
- merged into master https://github.com/apache/spark/commit/8ed480d6bd23c075c5e2ca34cb69d0aeb9692b8f
- merged into branch-4.x https://github.com/apache/spark/commit/19822befd2f89b8b979fa3a034ab4441f1b9e943
*Posted by `merge_spark_pr.py`*
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on code in PR #57491:
URL: https://github.com/apache/spark/pull/57491#discussion_r3648282059
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSetOperationsSuite.scala:
##########
@@ -1659,6 +1659,187 @@ class DataFrameSetOperationsSuite extends SharedSparkSession with AdaptiveSparkP
}
}
+ test("SPARK-58317: union partitioning - PartitioningCollection child intersects to single") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2", "t3", "t4") {
+ Seq((1, 2, 4), (1, 3, 5), (2, 2, 3)).toDF("c1", "c2", "c3").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+ Seq((1, 2, 4), (2, 4, 5), (3, 6, 7)).toDF("c1", "c2", "c3").createOrReplaceTempView("t3")
+ Seq((1, 9), (3, 9)).toDF("c1", "y").createOrReplaceTempView("t4")
+
+ // The first branch is an inner shuffled-hash join and selects both join keys (t1.c1 and
+ // t2.c1), so its output partitioning is a PartitioningCollection(Hash(c1), Hash(c1#..))
+ // that a downstream ProjectExec cannot narrow to a single member. The second branch is a
+ // left join, whose output partitioning is a single HashPartitioning(c1). The union should
+ // intersect the two to a single HashPartitioning(c1) and let the group-by skip a shuffle.
+ def unionDF: DataFrame = sql(
+ """SELECT c1, c2, c3, count(*) FROM (
+ | SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t1.c2, t1.c3, t2.c1 AS k
+ | FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ | UNION ALL
+ | SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t3.c2, t3.c3, t3.c1 AS k
+ | FROM t3 LEFT JOIN t4 ON t3.c1 = t4.c1
+ |) GROUP BY c1, c2, c3
+ |""".stripMargin)
+
+ val correctResult = withSQLConf(SQLConf.UNION_OUTPUT_PARTITIONING.key -> "false") {
+ unionDF.collect()
+ }
+
+ val shuffleNums = Seq(true, false).map { enabled =>
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+ SQLConf.UNION_OUTPUT_PARTITIONING.key -> enabled.toString) {
+ val union = unionDF
+ val unionExec = union.queryExecution.executedPlan.collect { case u: UnionExec => u }
+ assert(unionExec.size == 1)
+
+ val partitioning = unionExec.head.outputPartitioning
+ if (enabled) {
+ assert(partitioning.isInstanceOf[HashPartitioning],
+ s"expected a HashPartitioning pass-through but got $partitioning")
+ } else {
+ assert(partitioning.isInstanceOf[UnknownPartitioning])
+ }
+
+ checkAnswer(union, correctResult)
+ union.queryExecution.executedPlan.collect {
+ case s: ShuffleExchangeExec => s
+ }.size
+ }
+ }
+ // Enabling the pass-through removes the shuffle before the aggregate.
+ assert(shuffleNums.head + 1 == shuffleNums.last)
+ }
+ }
+ }
+
+ test("SPARK-58317: union partitioning - all PartitioningCollection children pass through") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2", "t3", "t4") {
+ Seq((1, 2), (2, 3)).toDF("c1", "c2").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+ Seq((1, 2), (3, 4)).toDF("c1", "c2").createOrReplaceTempView("t3")
+ Seq((1, 9), (3, 9)).toDF("c1", "y").createOrReplaceTempView("t4")
+
+ // Both branches are inner shuffled-hash joins on a single key and select both sides' join
+ // key (t1.c1 and t2.c1 AS k), so a downstream ProjectExec cannot narrow either child's
+ // PartitioningCollection(Hash(c1), Hash(k)) to a single member. The union should intersect
+ // to a PartitioningCollection carrying both members.
+ def unionDF: DataFrame = sql(
+ """SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t2.c1 AS k
+ |FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ |UNION ALL
+ |SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t4.c1 AS k
+ |FROM t3 JOIN t4 ON t3.c1 = t4.c1
+ |""".stripMargin)
+
+ val correctResult = withSQLConf(SQLConf.UNION_OUTPUT_PARTITIONING.key -> "false") {
+ unionDF.collect()
+ }
+
+ Seq(true, false).foreach { enabled =>
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+ SQLConf.UNION_OUTPUT_PARTITIONING.key -> enabled.toString) {
+ val union = unionDF
+ val unionExec = union.queryExecution.executedPlan.collect { case u: UnionExec => u }
+ assert(unionExec.size == 1)
+
+ val partitioning = unionExec.head.outputPartitioning
+ if (enabled) {
+ assert(partitioning.isInstanceOf[PartitioningCollection],
+ s"expected a PartitioningCollection pass-through but got $partitioning")
+ val members = partitioning.asInstanceOf[PartitioningCollection].partitionings
+ assert(members.forall(_.isInstanceOf[HashPartitioning]))
+ assert(members.size == 2)
+ } else {
+ assert(partitioning.isInstanceOf[UnknownPartitioning])
+ }
+
+ checkAnswer(union, correctResult)
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-58317: union partitioning - empty intersection falls back") {
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2") {
+ Seq((1, 2, 4), (2, 3, 5)).toDF("c1", "c2", "c3").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+
+ // First branch reports PartitioningCollection(Hash(c1), Hash(c1#..)); the second branch
+ // is repartitioned on a disjoint column, so the intersection is empty.
+ def unionDF: DataFrame = sql(
+ """SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t1.c2, t1.c3, t2.c1 AS k
+ |FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ |UNION ALL
+ |SELECT c1, c2, c3, c2 AS k FROM t1 DISTRIBUTE BY c2
+ |""".stripMargin)
+
+ val correctResult = withSQLConf(SQLConf.UNION_OUTPUT_PARTITIONING.key -> "false") {
+ unionDF.collect()
+ }
+
+ Seq(true, false).foreach { enabled =>
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+ SQLConf.UNION_OUTPUT_PARTITIONING.key -> enabled.toString) {
+ val union = unionDF
+ val unionExec = union.queryExecution.executedPlan.collect { case u: UnionExec => u }
+ assert(unionExec.size == 1)
+ assert(unionExec.head.outputPartitioning.isInstanceOf[UnknownPartitioning])
+ checkAnswer(union, correctResult)
+ }
+ }
+ }
+ }
+ }
+
+ test("SPARK-58317: union partitioning - PartitioningCollection pass-through under AQE") {
+ // AQE is enabled by default in production; the collection pass-through must produce correct
+ // results there too, where the union output flows through the coalesce-compatibility path.
+ withSQLConf(
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
+ withTempView("t1", "t2", "t3", "t4") {
+ Seq((1, 2), (2, 3), (1, 4)).toDF("c1", "c2").createOrReplaceTempView("t1")
+ Seq((1, 9), (2, 9)).toDF("c1", "x").createOrReplaceTempView("t2")
+ Seq((1, 2), (3, 4)).toDF("c1", "c2").createOrReplaceTempView("t3")
+ Seq((1, 9), (3, 9)).toDF("c1", "y").createOrReplaceTempView("t4")
+
+ def unionDF: DataFrame = sql(
+ """SELECT c1, count(*) FROM (
+ | SELECT /*+ SHUFFLE_HASH(t2) */ t1.c1, t2.c1 AS k
+ | FROM t1 JOIN t2 ON t1.c1 = t2.c1
+ | UNION ALL
+ | SELECT /*+ SHUFFLE_HASH(t4) */ t3.c1, t4.c1 AS k
+ | FROM t3 JOIN t4 ON t3.c1 = t4.c1
+ |) GROUP BY c1
Review Comment:
Applied in b79f1b2 -- `SELECT c1, k, ...` / `GROUP BY c1, k`, same reasoning as above so the `PartitioningCollection` survives `ColumnPruning` under AQE.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -937,47 +940,76 @@ case class UnionExec(children: Seq[SparkPlan]) extends SparkPlan with CodegenSup
}
override def outputPartitioning: Partitioning = {
- if (conf.getConf(SQLConf.UNION_OUTPUT_PARTITIONING)) {
- val partitionings = prepareOutputPartitioning()
- if (partitionings.forall(comparePartitioning(_, partitionings.head))) {
- val partitioner = partitionings.head
-
- // 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
- val isNarrowed = partitionings.exists {
- case k: KeyedPartitioning => k.isNarrowed
- case _ => false
- }
- KeyedPartitioning(mergedExpressions, mergedKeys, isGrouped, isNarrowed)
- case e: Expression =>
- e.transform {
- case a: Attribute if attributeMap.contains(a) => attributeMap(a)
- }.asInstanceOf[Partitioning]
- case _ => partitioner
- }
+ if (!conf.getConf(SQLConf.UNION_OUTPUT_PARTITIONING)) {
+ return super.outputPartitioning
+ }
+
+ // Children's partitionings with attributes remapped to the first child's attributes.
+ val partitionings = prepareOutputPartitioning()
+ // Map from the first child's attributes to this union's own output attributes.
+ val attributeMap = children.head.output.zip(output).toMap
+ def toUnionOutput(p: Partitioning): Partitioning = p match {
+ case e: Expression =>
+ e.transform {
+ case a: Attribute if attributeMap.contains(a) => attributeMap(a)
+ }.asInstanceOf[Partitioning]
+ case _ => p
+ }
+
+ // Case A: every child is a single `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. This concatenation (numPartitions = sum) is a
+ // distinct physical strategy from the co-located pass-through below (numPartitions = N), so
+ // it is kept as a separate case and never folded into a `PartitioningCollection`.
+ if (partitionings.forall(_.isInstanceOf[KeyedPartitioning])) {
+ val kps = partitionings.map(_.asInstanceOf[KeyedPartitioning])
+ val headKp = kps.head
+ // The `KeyedPartitioning`s must agree on the partition expressions to merge.
+ val compatible = kps.forall(comparePartitioning(_, headKp))
+ if (compatible) {
+ val mergedKeys = kps.flatMap(_.partitionKeys)
+ val mergedExpressions = headKp.expressions.map(_.transform {
+ case a: Attribute if attributeMap.contains(a) => attributeMap(a)
+ })
+ val isGrouped = mergedKeys.distinct.size == mergedKeys.size
+ val isNarrowed = kps.exists(_.isNarrowed)
+ return KeyedPartitioning(mergedExpressions, mergedKeys, isGrouped, isNarrowed)
} else {
- super.outputPartitioning
+ return super.outputPartitioning
}
- } else {
- super.outputPartitioning
+ }
+
+ // Case B: treat each child's partitioning as a set of candidate partitionings (a
+ // `PartitioningCollection` flattens to its members; a single partitioning is a one-element
+ // set) and pass through the intersection across all children. Only index-co-locatable
+ // partitionings participate; `KeyedPartitioning` is excluded here because its concatenation
+ // semantics (Case A) are incompatible with the co-located union RDD.
+ def flattenPartitioning(p: Partitioning): Seq[Partitioning] = p match {
Review Comment:
Done in b79f1b2. Rather than depend on the `sql/core` helper from catalyst, I lifted a shared `PartitioningCollection.flatten` into the catalyst companion object and reused it from both `UnionExec` and `PartitioningPreservingUnaryExecNode`, removing the duplication.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you commented on code in PR #57491:
URL: https://github.com/apache/spark/pull/57491#discussion_r3648281070
##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSetOperationsSuite.scala:
##########
@@ -1659,6 +1659,187 @@ class DataFrameSetOperationsSuite extends SharedSparkSession with AdaptiveSparkP
}
}
+ test("SPARK-58317: union partitioning - PartitioningCollection child intersects to single") {
Review Comment:
Good catch, thank you. You are right -- `k` was pruned, so no `PartitioningCollection` reached the union and the test passed on master. Fixed in b79f1b2: `GROUP BY c1, c2, c3, k` keeps the first branch's collection alive, and `t4.c1 AS k` (the left join's build side) keeps the second branch a single `Hash(c1)`. I verified the revised test now fails on pre-PR code with `UnknownPartitioning(0) was not instance of HashPartitioning` and passes with the 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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children [spark]
Posted by "ulysses-you (via GitHub)" <gi...@apache.org>.
ulysses-you closed pull request #57491: [SPARK-58317][SQL] Union output partitioning should support PartitioningCollection children
URL: https://github.com/apache/spark/pull/57491
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org