ulysses-you commented on code in PR #59630:
URL: https://github.com/apache/spark/pull/59630#discussion_r4524771728


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/PushDownShuffleExchangeThroughUnion.scala:
##########
@@ -0,0 +1,614 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.adaptive
+
+import scala.annotation.tailrec
+import scala.collection.mutable
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap, 
Expression, SessionWindow, SortOrder}
+import org.apache.spark.sql.catalyst.expressions.aggregate.{Partial => 
PartialAggregate}
+import org.apache.spark.sql.catalyst.optimizer.BuildLeft
+import org.apache.spark.sql.catalyst.plans.physical.{ClusteredDistribution, 
HashPartitioning, HashShuffleSpec, Partitioning, PartitioningCollection}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreeNodeTag
+import org.apache.spark.sql.execution.{
+  FilterExec, ProjectExec, SortExec, SparkPlan, UnaryExecNode, UnionExec}
+import org.apache.spark.sql.execution.aggregate.BaseAggregateExec
+import org.apache.spark.sql.execution.exchange.{ENSURE_REQUIREMENTS, 
EnsureRequirements, Exchange, PartitioningOrigin, REPARTITION_BY_COL, 
REPARTITION_BY_NUM, ShuffleExchangeExec, ShuffleExchangeLike, 
ValidateRequirements}
+import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, 
BroadcastNestedLoopJoinExec, CartesianProductExec, ShuffledJoin}
+import org.apache.spark.sql.execution.window.{Partial => 
PartialWindowGroupLimit, WindowGroupLimitExec}
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * Push down a shuffle [[EnsureRequirements]] put above a [[UnionExec]] below 
the union. Eligible
+ * children already reporting the target retain their layouts; other children 
are retargeted or
+ * receive a new shuffle. Local sorts above the shuffle and operators between 
it and the union
+ * are rebuilt above the union:
+ *
+ * {{{
+ *   SortMergeJoin                             SortMergeJoin
+ *    :- Sort                                   :- Sort
+ *    :  +- Exchange                 =>         :  +- Union
+ *    :     +- Union                            :     :- HashAggregate
+ *    :        :- HashAggregate                 :     :  +- Exchange
+ *    :        :  +- Exchange                   :     :     +- Scan
+ *    :        :     +- Scan                    :     +- Exchange
+ *    :        +- Project                       :        +- Project
+ *    :           +- Scan                       :           +- Scan
+ *    +- ...                                    +- ...
+ * }}}
+ *
+ * The left child already answers the target, since it shuffles on the join 
key for its aggregate,
+ * so it keeps its layout and only the right child takes a shuffle.
+ *
+ * The operators between the shuffle and the union are looked through only 
under
+ * 
`spark.sql.adaptive.pushDownShuffleThroughUnion.lookThroughOperators.enabled`, 
and only when
+ * moving the shuffle past them preserves results: a deterministic project, a 
local sort, a
+ * filter, a partial aggregate and a partial window group limit. The reducing 
ones are why the
+ * gate exists: the child is then shuffled before the reduction, which can 
send more data than the
+ * one shuffle it replaces.
+ *
+ * Targets are hash layouts reported by the union's children, ranked by how 
many children offer
+ * them when size estimates are unavailable; otherwise estimated bytes kept 
decide first. The
+ * count must be at least the smaller of the replaced exchange's count and the 
effective initial
+ * shuffle partition count (`coalescePartitions.initialPartitionNum` when 
set). At least one
+ * child keeps its layout; others are retargeted or shuffled without changing 
child order.
+ * `spark.sql.adaptive.pushDownShuffleThroughUnion.maxMovedBranches` limits 
moved children, and
+ * `maxUnionChildren` limits all branches whose tails run within each consumer 
task.
+ * The rebuilt child must satisfy its consumer's distribution and pair with 
fixed siblings.
+ *
+ * Removing the union's exchange can reduce AQE join conversion and skew 
handling opportunities.
+ * Keeping a shuffled join child's layout can make skew join optimization or 
local shuffle reads
+ * invalidate an ancestor's distribution. AQE can discard the optimization for 
the round, or add
+ * another shuffle when skew optimization is forced. Join inputs and join 
branches are opt-in.
+ * Allowing targets via
+ * 
`spark.sql.adaptive.pushDownShuffleThroughUnion.keepNonCoalescibleBranches.enabled`
+ * can prevent coalescing of the consumer's compatible shuffle group. Forced 
skew optimization
+ * can restore a union exchange after moved branches have shuffled, shuffling 
them a second time.
+ *
+ * The query-wide subquery gate includes optimizer-injected runtime Bloom 
filter builds.
+ * Allowing subqueries can make pruning builds differ from main-query 
broadcast builds. With
+ * broadcast-only dynamic pruning, failed broadcast reuse removes the pruning 
filter and can scan
+ * all fact partitions; that cost can outweigh the saved build-side shuffle.
+ *
+ * The rule runs only in AQE preparation, after `EnsureRequirements` and before
+ * `StampUnionDecisions`. It preserves the root distribution and all user 
repartition layouts
+ * recorded when `EnsureRequirements` elides exchanges, including requirements 
below the root.
+ * Recording and push-down share the preparation's effective enablement on 
EnsureRequirements;
+ * classic and disabled preparations do not record these origins.
+ * Cost compensation belongs to SimpleCostEvaluator. Custom evaluators should 
account for the
+ * one-to-many exchange rewrite when this rule is enabled, or they may reject 
the entire re-plan.
+ *
+ * TODO: support push down of a keyed layout.
+ */
+case class PushDownShuffleExchangeThroughUnion(
+    ensureRequirements: EnsureRequirements) extends Rule[SparkPlan] {
+
+  override def apply(plan: SparkPlan): SparkPlan = {
+    if (!ensureRequirements.pushDownShuffleThroughUnionEnabled) {
+      return plan
+    }
+
+    val lookThrough =
+      
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_LOOK_THROUGH_OPERATORS_ENABLED)
+    val policy = LayoutPolicy(
+      allowNonCoalescible = !conf.getConf(SQLConf.COALESCE_PARTITIONS_ENABLED) 
|| conf.getConf(
+        
SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_KEEP_NON_COALESCIBLE_BRANCHES_ENABLED),
+      maxMovedBranches =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_MAX_MOVED_BRANCHES),
+      maxUnionChildren =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_MAX_UNION_CHILDREN),
+      keepJoinBranches =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_KEEP_JOIN_BRANCHES_ENABLED),
+      allowSubsetTargets = conf.getConf(SQLConf
+        
.PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_PARTITION_KEYS_SUBSET_OF_CLUSTER_KEYS_ENABLED))
+    val allowJoinInputs =
+      
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_JOIN_INPUTS_ENABLED)
+
+    // An exchange the plan reuses elsewhere is worth more than this rewrite: 
moving it below the
+    // union loses the stage the others reuse. See 
`AQEUtils.duplicatedShuffleForms`. With reuse
+    // off every exchange gets its own stage, so there is nothing to protect.
+    lazy val sharedExchanges: Set[SparkPlan] =
+      if (conf.exchangeReuseEnabled) AQEUtils.duplicatedShuffleForms(plan) 
else Set.empty
+
+    val pushed = plan.transformUp {
+      case join: ShuffledJoin if !allowJoinInputs => join
+      case operator: SparkPlan =>
+        val matched = operator.children.map { child =>
+          findPushableShuffle(child, lookThrough).filter { pushable =>
+            pushable.union.children.length <= policy.maxUnionChildren &&
+              (sharedExchanges.isEmpty ||
+                !sharedExchanges.contains(pushable.shuffle.canonicalized))
+          }
+        }
+        if (matched.forall(_.isEmpty)) {
+          operator
+        } else {
+          // Read requirements only after finding a site; some operators log 
when asked.
+          val required = operator.requiredChildDistribution
+          if (!required.forall(_.isInstanceOf[ClusteredDistribution])) {
+            operator
+          } else {
+            val requirements = 
required.map(_.asInstanceOf[ClusteredDistribution])
+            pushDownThroughUnion(
+              operator,
+              matched.zip(requirements),
+              sharedExchanges = sharedExchanges,
+              policy = policy.copy(allowNonCoalescible =
+                policy.allowNonCoalescible || 
!allExchangeInputs(operator))).getOrElse(operator)
+          }
+        }
+    }
+    if (pushed.fastEquals(plan)) {
+      plan
+    } else {
+      // Local checks do not cover ancestors or elided repartitions below the 
root. Reject the
+      // whole proposal if any plan requirement or recorded output layout is 
lost.
+      val satisfied = ensureRequirements.requiredDistribution match {
+        // A user repartition elided above was asked for on all its keys, so a 
layout on a subset
+        // of them does not stand in for it.
+        case Some(required: ClusteredDistribution) =>
+          ValidateRequirements.validate(pushed, 
required.copy(requireAllClusterKeys = true))
+        case Some(required) =>
+          ValidateRequirements.validate(pushed, required)
+        case None =>
+          ValidateRequirements.validate(pushed)
+      }
+      if (satisfied && !pushed.exists(node => 
!PartitioningOrigin.validateNode(node))) {
+        pushed
+      } else {
+        plan
+      }
+    }
+  }
+
+  /**
+   * One of an operator's children this rule can rewrite: the shuffle it moves 
and the union that
+   * shuffle is moved past.
+   */
+  private case class PushableChild(shuffle: ShuffleExchangeExec, union: 
UnionExec)
+
+  /**
+   * Layout selection settings, read once per pass.
+   */
+  private case class LayoutPolicy(
+      allowNonCoalescible: Boolean,
+      maxMovedBranches: Int,
+      maxUnionChildren: Int,
+      keepJoinBranches: Boolean,
+      allowSubsetTargets: Boolean)
+
+  /** Lazily discover and rank layouts once per site for both joint and 
fallback attempts. */
+  private class PreparedChild(val pushable: PushableChild, policy: 
LayoutPolicy) {
+    /**
+     * Eligible child layouts in child order, expressed in the union's output 
attributes.
+     * Collections offer their hash members; a child offering nothing takes a 
shuffle.
+     * Equality follows UnionExec's comparison after prepareOutputPartitioning 
rebinding.
+     */
+    lazy val layouts: Seq[Seq[Partitioning with Expression]] = {
+      val union = pushable.union
+      union.prepareOutputPartitioning().zip(union.children).map { case 
(partitioning, child) =>

Review Comment:
   Pre-existing `UnionExec` bug (SPARK-61211, #60271); this rule made it 
reachable for a union with an extra branch. #60271 drops unmapped members in 
`prepareOutputPartitioning`, which `PreparedChild.layouts` consumes. With that 
change applied locally, the three-branch repro returns 11 rows with `rn = 1` 
instead of 20. The duplicate-exprId guards stay because `rebind` is ambiguous 
when the union's own output repeats, which #60271 does not change.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/PushDownShuffleExchangeThroughUnion.scala:
##########
@@ -0,0 +1,614 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.adaptive
+
+import scala.annotation.tailrec
+import scala.collection.mutable
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap, 
Expression, SessionWindow, SortOrder}
+import org.apache.spark.sql.catalyst.expressions.aggregate.{Partial => 
PartialAggregate}
+import org.apache.spark.sql.catalyst.optimizer.BuildLeft
+import org.apache.spark.sql.catalyst.plans.physical.{ClusteredDistribution, 
HashPartitioning, HashShuffleSpec, Partitioning, PartitioningCollection}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreeNodeTag
+import org.apache.spark.sql.execution.{
+  FilterExec, ProjectExec, SortExec, SparkPlan, UnaryExecNode, UnionExec}
+import org.apache.spark.sql.execution.aggregate.BaseAggregateExec
+import org.apache.spark.sql.execution.exchange.{ENSURE_REQUIREMENTS, 
EnsureRequirements, Exchange, PartitioningOrigin, REPARTITION_BY_COL, 
REPARTITION_BY_NUM, ShuffleExchangeExec, ShuffleExchangeLike, 
ValidateRequirements}
+import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, 
BroadcastNestedLoopJoinExec, CartesianProductExec, ShuffledJoin}
+import org.apache.spark.sql.execution.window.{Partial => 
PartialWindowGroupLimit, WindowGroupLimitExec}
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * Push down a shuffle [[EnsureRequirements]] put above a [[UnionExec]] below 
the union. Eligible
+ * children already reporting the target retain their layouts; other children 
are retargeted or
+ * receive a new shuffle. Local sorts above the shuffle and operators between 
it and the union
+ * are rebuilt above the union:
+ *
+ * {{{
+ *   SortMergeJoin                             SortMergeJoin
+ *    :- Sort                                   :- Sort
+ *    :  +- Exchange                 =>         :  +- Union
+ *    :     +- Union                            :     :- HashAggregate
+ *    :        :- HashAggregate                 :     :  +- Exchange
+ *    :        :  +- Exchange                   :     :     +- Scan
+ *    :        :     +- Scan                    :     +- Exchange
+ *    :        +- Project                       :        +- Project
+ *    :           +- Scan                       :           +- Scan
+ *    +- ...                                    +- ...
+ * }}}
+ *
+ * The left child already answers the target, since it shuffles on the join 
key for its aggregate,
+ * so it keeps its layout and only the right child takes a shuffle.
+ *
+ * The operators between the shuffle and the union are looked through only 
under
+ * 
`spark.sql.adaptive.pushDownShuffleThroughUnion.lookThroughOperators.enabled`, 
and only when
+ * moving the shuffle past them preserves results: a deterministic project, a 
local sort, a
+ * filter, a partial aggregate and a partial window group limit. The reducing 
ones are why the
+ * gate exists: the child is then shuffled before the reduction, which can 
send more data than the
+ * one shuffle it replaces.
+ *
+ * Targets are hash layouts reported by the union's children, ranked by how 
many children offer
+ * them when size estimates are unavailable; otherwise estimated bytes kept 
decide first. The
+ * count must be at least the smaller of the replaced exchange's count and the 
effective initial
+ * shuffle partition count (`coalescePartitions.initialPartitionNum` when 
set). At least one
+ * child keeps its layout; others are retargeted or shuffled without changing 
child order.
+ * `spark.sql.adaptive.pushDownShuffleThroughUnion.maxMovedBranches` limits 
moved children, and
+ * `maxUnionChildren` limits all branches whose tails run within each consumer 
task.
+ * The rebuilt child must satisfy its consumer's distribution and pair with 
fixed siblings.
+ *
+ * Removing the union's exchange can reduce AQE join conversion and skew 
handling opportunities.
+ * Keeping a shuffled join child's layout can make skew join optimization or 
local shuffle reads
+ * invalidate an ancestor's distribution. AQE can discard the optimization for 
the round, or add
+ * another shuffle when skew optimization is forced. Join inputs and join 
branches are opt-in.
+ * Allowing targets via
+ * 
`spark.sql.adaptive.pushDownShuffleThroughUnion.keepNonCoalescibleBranches.enabled`
+ * can prevent coalescing of the consumer's compatible shuffle group. Forced 
skew optimization
+ * can restore a union exchange after moved branches have shuffled, shuffling 
them a second time.
+ *
+ * The query-wide subquery gate includes optimizer-injected runtime Bloom 
filter builds.
+ * Allowing subqueries can make pruning builds differ from main-query 
broadcast builds. With
+ * broadcast-only dynamic pruning, failed broadcast reuse removes the pruning 
filter and can scan
+ * all fact partitions; that cost can outweigh the saved build-side shuffle.
+ *
+ * The rule runs only in AQE preparation, after `EnsureRequirements` and before
+ * `StampUnionDecisions`. It preserves the root distribution and all user 
repartition layouts
+ * recorded when `EnsureRequirements` elides exchanges, including requirements 
below the root.
+ * Recording and push-down share the preparation's effective enablement on 
EnsureRequirements;
+ * classic and disabled preparations do not record these origins.
+ * Cost compensation belongs to SimpleCostEvaluator. Custom evaluators should 
account for the
+ * one-to-many exchange rewrite when this rule is enabled, or they may reject 
the entire re-plan.
+ *
+ * TODO: support push down of a keyed layout.
+ */
+case class PushDownShuffleExchangeThroughUnion(
+    ensureRequirements: EnsureRequirements) extends Rule[SparkPlan] {
+
+  override def apply(plan: SparkPlan): SparkPlan = {
+    if (!ensureRequirements.pushDownShuffleThroughUnionEnabled) {
+      return plan
+    }
+
+    val lookThrough =
+      
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_LOOK_THROUGH_OPERATORS_ENABLED)
+    val policy = LayoutPolicy(
+      allowNonCoalescible = !conf.getConf(SQLConf.COALESCE_PARTITIONS_ENABLED) 
|| conf.getConf(
+        
SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_KEEP_NON_COALESCIBLE_BRANCHES_ENABLED),
+      maxMovedBranches =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_MAX_MOVED_BRANCHES),
+      maxUnionChildren =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_MAX_UNION_CHILDREN),
+      keepJoinBranches =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_KEEP_JOIN_BRANCHES_ENABLED),
+      allowSubsetTargets = conf.getConf(SQLConf
+        
.PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_PARTITION_KEYS_SUBSET_OF_CLUSTER_KEYS_ENABLED))
+    val allowJoinInputs =
+      
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_JOIN_INPUTS_ENABLED)
+
+    // An exchange the plan reuses elsewhere is worth more than this rewrite: 
moving it below the
+    // union loses the stage the others reuse. See 
`AQEUtils.duplicatedShuffleForms`. With reuse
+    // off every exchange gets its own stage, so there is nothing to protect.
+    lazy val sharedExchanges: Set[SparkPlan] =
+      if (conf.exchangeReuseEnabled) AQEUtils.duplicatedShuffleForms(plan) 
else Set.empty
+
+    val pushed = plan.transformUp {
+      case join: ShuffledJoin if !allowJoinInputs => join
+      case operator: SparkPlan =>
+        val matched = operator.children.map { child =>
+          findPushableShuffle(child, lookThrough).filter { pushable =>
+            pushable.union.children.length <= policy.maxUnionChildren &&
+              (sharedExchanges.isEmpty ||
+                !sharedExchanges.contains(pushable.shuffle.canonicalized))
+          }
+        }
+        if (matched.forall(_.isEmpty)) {
+          operator
+        } else {
+          // Read requirements only after finding a site; some operators log 
when asked.
+          val required = operator.requiredChildDistribution
+          if (!required.forall(_.isInstanceOf[ClusteredDistribution])) {
+            operator
+          } else {
+            val requirements = 
required.map(_.asInstanceOf[ClusteredDistribution])
+            pushDownThroughUnion(
+              operator,
+              matched.zip(requirements),
+              sharedExchanges = sharedExchanges,
+              policy = policy.copy(allowNonCoalescible =
+                policy.allowNonCoalescible || 
!allExchangeInputs(operator))).getOrElse(operator)
+          }
+        }
+    }
+    if (pushed.fastEquals(plan)) {
+      plan
+    } else {
+      // Local checks do not cover ancestors or elided repartitions below the 
root. Reject the
+      // whole proposal if any plan requirement or recorded output layout is 
lost.
+      val satisfied = ensureRequirements.requiredDistribution match {
+        // A user repartition elided above was asked for on all its keys, so a 
layout on a subset
+        // of them does not stand in for it.
+        case Some(required: ClusteredDistribution) =>
+          ValidateRequirements.validate(pushed, 
required.copy(requireAllClusterKeys = true))
+        case Some(required) =>
+          ValidateRequirements.validate(pushed, required)
+        case None =>
+          ValidateRequirements.validate(pushed)
+      }
+      if (satisfied && !pushed.exists(node => 
!PartitioningOrigin.validateNode(node))) {
+        pushed
+      } else {
+        plan
+      }
+    }
+  }
+
+  /**
+   * One of an operator's children this rule can rewrite: the shuffle it moves 
and the union that
+   * shuffle is moved past.
+   */
+  private case class PushableChild(shuffle: ShuffleExchangeExec, union: 
UnionExec)
+
+  /**
+   * Layout selection settings, read once per pass.
+   */
+  private case class LayoutPolicy(
+      allowNonCoalescible: Boolean,
+      maxMovedBranches: Int,
+      maxUnionChildren: Int,
+      keepJoinBranches: Boolean,
+      allowSubsetTargets: Boolean)
+
+  /** Lazily discover and rank layouts once per site for both joint and 
fallback attempts. */
+  private class PreparedChild(val pushable: PushableChild, policy: 
LayoutPolicy) {
+    /**
+     * Eligible child layouts in child order, expressed in the union's output 
attributes.
+     * Collections offer their hash members; a child offering nothing takes a 
shuffle.
+     * Equality follows UnionExec's comparison after prepareOutputPartitioning 
rebinding.
+     */
+    lazy val layouts: Seq[Seq[Partitioning with Expression]] = {
+      val union = pushable.union
+      union.prepareOutputPartitioning().zip(union.children).map { case 
(partitioning, child) =>
+        if (keepableLayout(child, policy)) {
+          PartitioningCollection.flatten(partitioning).collect {
+            case hash: HashPartitioning => hash
+          }
+        } else {
+          Nil
+        }
+      }
+    }
+
+    lazy val targets: Seq[Partitioning with Expression] = {
+      val minPartitions = math.min(
+        pushable.shuffle.outputPartitioning.numPartitions, 
conf.numShufflePartitions)
+      candidateTargets(layouts, pushable.union.children, minPartitions)
+        .filter { target =>
+          layouts.count(child => !child.contains(target)) <= 
policy.maxMovedBranches
+        }
+    }
+  }
+
+  /**
+   * The shape this rule acts on below one of an operator's children: a 
pushable shuffle and the
+   * union it is moved past, with the operators in between looked through to 
reach it. Only a hash
+   * shuffle is pushable, since the layout that replaces it comes from the 
union's own children.
+   *
+   * What the child owes its operator is not read here: this is asked of every 
child of every
+   * operator, and `requiredChildDistribution` is not free of side effects: 
`WindowExecBase` logs
+   * from it for an unpartitioned window.
+   */
+  @tailrec
+  private def findPushableShuffle(
+      plan: SparkPlan,
+      lookThrough: Boolean,
+      found: Option[ShuffleExchangeExec] = None): Option[PushableChild] = {
+    plan match {
+      // A local sort above the shuffle is looked through, since the rebuilt 
child is sorted back
+      // to the same requirement; one below it is an operator in between and 
needs the gate.
+      case SortExec(_, false, child, _) if found.isEmpty || lookThrough =>
+        findPushableShuffle(child, lookThrough, found)
+      case shuffle @ ShuffleExchangeExec(partitioning, child, 
ENSURE_REQUIREMENTS, _, _)
+          if found.isEmpty && supportPushDown(partitioning) =>
+        findPushableShuffle(child, lookThrough, Some(shuffle))
+      // AttributeMap keeps the last duplicate exprId, while runtime binding 
uses the first.
+      // Positional rebinding is ambiguous for these unions, so retain their 
exchange.
+      case union: UnionExec if union.hasDuplicateChildOutputs => None
+      // Below it: the operators this rule can cross, down to the union the 
shuffle is moved past.
+      case union: UnionExec =>
+        found.map(PushableChild(_, union))
+      case operator: UnaryExecNode if found.isDefined && canCross(operator, 
lookThrough) =>
+        findPushableShuffle(operator.child, lookThrough, found)
+      case _ => None
+    }
+  }
+
+  /**
+   * Rewrites eligible children in order. If a joint rewrite fails, retry 
individual children
+   * against all current siblings, keeping children without a viable target 
unchanged.
+   *
+   * `pushable` holds one entry per child, in child order: what can be 
rewritten about it, and
+   * what it owes the operator.
+   */
+  private def pushDownThroughUnion(
+      operator: SparkPlan,
+      pushable: Seq[(Option[PushableChild], ClusteredDistribution)],
+      sharedExchanges: Set[SparkPlan],
+      policy: LayoutPolicy): Option[SparkPlan] = {
+    val children = operator.children
+    val requiredOrderings = operator.requiredChildOrdering
+
+    val prepared = pushable.map { case (site, _) => site.map { child =>
+      new PreparedChild(child, policy)
+    }}
+    val newChildren = new ArrayBuffer[SparkPlan](children.length)
+    def rebuildChildren(joint: Boolean): Boolean = {
+      newChildren.clear()
+      children.indices.forall { index =>
+        val (pushableChild, required) = pushable(index)
+        val rebuilt = prepared(index).flatMap { child =>
+          val fixed = children.indices.flatMap { position =>
+            val (sibling, distribution) = pushable(position)
+            Option.when(position != index && (!joint || position < index || 
sibling.isEmpty)) {
+              val fixedChild = if (position < index) {
+                newChildren(position)
+              } else {
+                children(position)
+              }
+              (fixedChild, distribution)
+            }
+          }
+          rebuildChild(child, required, fixed, sharedExchanges, policy)
+        }
+        if (joint && pushableChild.isDefined && rebuilt.isEmpty) {
+          false
+        } else {
+          newChildren += rebuilt.getOrElse(children(index))
+          true
+        }
+      }
+    }
+
+    // Joint selection lets later pushable siblings follow; the fallback fixes 
every sibling.
+    if (!rebuildChildren(joint = true)) {
+      rebuildChildren(joint = false)
+    }
+
+    // The ordering is put back once the layout is fixed. A local sort leaves 
the layout alone,
+    // and a child that already answers keeps its own.
+    val sorted = newChildren.toSeq.lazyZip(requiredOrderings).map {
+      case (child, ordering) => withOrdering(child, ordering)
+    }
+    Some(operator.withNewChildren(sorted))
+  }
+
+  /**
+   * Rebuild on the best eligible union layout satisfying the consumer and 
pairing with fixed
+   * siblings. Subset keys require both the rule's permission and the 
co-partition policy, even
+   * when a single-input consumer accepts a coarser distribution.
+   */
+  private def rebuildChild(
+      prepared: PreparedChild,
+      required: ClusteredDistribution,
+      fixed: Seq[(SparkPlan, ClusteredDistribution)],
+      sharedExchanges: Set[SparkPlan],
+      policy: LayoutPolicy): Option[SparkPlan] = {
+    val pushable = prepared.pushable
+    val layouts = prepared.layouts
+
+    // `findPushableShuffle` already checked this chain, including local sorts.
+    def rebuild(plan: SparkPlan, target: Partitioning with Expression): 
Option[SparkPlan] =
+      plan match {
+        case union: UnionExec => rebuildUnion(union, layouts, target, 
sharedExchanges)
+        case operator: UnaryExecNode =>
+          rebuild(operator.child, target).map { child =>
+            // A zero-move union can be structurally equal but must remain 
isolated for origin tags.
+            operator.mapChildrenWithReferenceEquality(_ => child)
+          }
+        case other =>
+          throw SparkException.internalError(
+            s"a shuffle push-down reached ${other.nodeName}, not the union it 
was found above")
+      }
+
+    prepared.targets.view
+      .flatMap(target => rebuild(pushable.shuffle.child, target))
+      .map(child => PartitioningOrigin.inherit(pushable.shuffle, child))
+      .find { child =>
+        val partitioning = child.outputPartitioning
+        partitioning.satisfies(required) &&
+          PartitioningCollection.specsForPairing(partitioning, 
required).exists { spec =>
+            spec.canCreatePartitioning && (policy.allowSubsetTargets || (spec 
match {
+              case hash: HashShuffleSpec =>
+                
required.areAllClusterKeysMatched(hash.partitioning.expressions)
+              case _ => false
+            }))
+          } &&
+          ValidateRequirements.satisfiesForPairing(
+            child +: fixed.map(_._1), required +: fixed.map(_._2))
+      }
+  }
+
+  private case class CandidateBenefit(
+      offeringChildren: Int,
+      keptBytes: BigInt,
+      firstOccurrence: Int)
+
+  /**
+   * Rank by estimated bytes kept when every child has a usable estimate. 
Missing estimates
+   * trigger count-based ranking rather than treating unknown branches as 
smaller. Partition
+   * count and first occurrence break ties deterministically.
+   */
+  private def candidateTargets(
+      layouts: Seq[Seq[Partitioning with Expression]],
+      children: Seq[SparkPlan],
+      minPartitions: Int): Seq[Partitioning with Expression] = {
+    val sizes = children.map { child =>
+      child.logicalLink.map(_.stats.sizeInBytes).filter(_ < 
conf.defaultSizeInBytes)
+    }
+    val useSizes = sizes.forall(_.isDefined)
+    val benefits = mutable.HashMap.empty[Partitioning with Expression, 
CandidateBenefit]
+    layouts.zip(sizes).foreach { case (reported, size) =>
+      reported.filter(_.numPartitions >= minPartitions).distinct.foreach { 
target =>
+        val previous = benefits.getOrElse(target, CandidateBenefit(0, 
BigInt(0), benefits.size))
+        benefits.update(target, previous.copy(
+          offeringChildren = previous.offeringChildren + 1,
+          keptBytes = previous.keptBytes + size.getOrElse(BigInt(0))))
+      }
+    }
+    benefits.toSeq.sortBy { case (target, benefit) =>
+        val keptBytes = if (useSizes) { benefit.keptBytes } else { BigInt(0) }
+        (-keptBytes, -benefit.offeringChildren, -target.numPartitions, 
benefit.firstOccurrence)
+      }
+      .map(_._1)
+  }
+
+  /**
+   * Whether every input path reaches an exchange boundary. Stop at unstaged 
exchanges as well
+   * as query stages: their input scans do not prevent coalescing. A 
non-exchange leaf already
+   * prevents the consumer's compatible group from coalescing, independently 
of target selection.
+   */
+  private def allExchangeInputs(plan: SparkPlan): Boolean = plan match {
+    case _: Exchange => true
+    case _: ExchangeQueryStageExec => true
+    case leaf if leaf.children.isEmpty => false
+    case other => other.children.forall(allExchangeInputs)
+  }
+
+  /**
+   * Whether a union child may retain its layout as a target. Check every 
input path, stopping at
+   * shuffle boundaries: requirement and column repartitions are eligible. 
Fixed-count shuffles
+   * and non-shuffle leaves require the non-coalescible allowance; other 
origins are rejected.
+   * Shuffled joins require the join-branch opt-in; exploding joins require 
the non-coalescible
+   * allowance. Unions with duplicate child output identities cannot supply a 
kept layout, even
+   * through wrappers, because their positional mapping is ambiguous.
+   * Broadcast joins inspect only their streamed side. 
`fromBroadcastHashJoinExec` follows that
+   * probe through wrappers and stages so requirement shuffles also require 
the join-branch opt-in,
+   * preserving opportunities for adaptive skew splitting and local reads.
+   */
+  private def keepableLayout(
+      plan: SparkPlan,
+      policy: LayoutPolicy,
+      fromBroadcastHashJoinExec: Boolean = false): Boolean = plan match {
+    case shuffle: ShuffleExchangeLike =>
+      shuffle.shuffleOrigin match {
+        case ENSURE_REQUIREMENTS => !fromBroadcastHashJoinExec || 
policy.keepJoinBranches
+        case REPARTITION_BY_COL => true
+        case REPARTITION_BY_NUM => policy.allowNonCoalescible
+        case _ => false
+      }
+    case stage: ShuffleQueryStageExec =>
+      keepableLayout(stage.shuffle, policy, fromBroadcastHashJoinExec)
+    case union: UnionExec if union.hasDuplicateChildOutputs => false
+    case _: ShuffledJoin if !policy.keepJoinBranches => false
+    case _: BroadcastNestedLoopJoinExec | _: CartesianProductExec
+        if !policy.allowNonCoalescible => false
+    case join: BroadcastHashJoinExec =>
+      val streamed = if (join.buildSide == BuildLeft) { join.right } else { 
join.left }
+      keepableLayout(streamed, policy, fromBroadcastHashJoinExec = true)
+    case join: BroadcastNestedLoopJoinExec =>
+      val streamed = if (join.buildSide == BuildLeft) { join.right } else { 
join.left }
+      keepableLayout(streamed, policy, fromBroadcastHashJoinExec)
+    case leaf if leaf.children.isEmpty => policy.allowNonCoalescible
+    case other => other.children.forall(keepableLayout(_, policy, 
fromBroadcastHashJoinExec))
+  }
+
+  /**
+   * `union` rebuilt on `target`: eligible children are kept, unshared movable 
exchanges are
+   * retargeted, and other children get an exchange. `None` if the union does 
not report `target`,
+   * which is stricter than satisfying the consumer's distribution. 
Retargeting is limited to
+   * direct, unshared requirement or column-repartition exchanges. Column 
repartitions retain their
+   * keys in order; a key change or recorded-layout violation keeps the 
exchange and adds one above.
+   * Tag newly added exchanges for cost accounting; retargeting adds no 
shuffle.
+   */
+  private def rebuildUnion(
+      union: UnionExec,
+      layouts: Seq[Seq[Partitioning with Expression]],
+      target: Partitioning with Expression,
+      sharedExchanges: Set[SparkPlan]): Option[SparkPlan] = {
+    def addExchange(partitioning: Partitioning, child: SparkPlan): 
ShuffleExchangeExec = {
+      val exchange = ShuffleExchangeExec(partitioning, child, 
ENSURE_REQUIREMENTS)
+      
exchange.setTagValue(PushDownShuffleExchangeThroughUnion.PUSHED_DOWN_SHUFFLE, 
true)
+      exchange
+    }
+    val newChildren = union.children.zip(layouts).map { case (child, reported) 
=>
+      if (reported.contains(target)) {
+        child
+      } else {
+        val partitioning = rebind(target, union, child)
+        child match {
+          case shuffle: ShuffleExchangeExec
+              if (shuffle.shuffleOrigin == ENSURE_REQUIREMENTS ||
+                  (shuffle.shuffleOrigin == REPARTITION_BY_COL &&
+                    samePartitionKeys(shuffle.outputPartitioning, 
partitioning))) &&
+                (sharedExchanges.isEmpty || 
!sharedExchanges.contains(shuffle.canonicalized)) =>
+            val retargeted = shuffle.copy(outputPartitioning = partitioning)
+            retargeted.copyTagsFrom(shuffle)
+            if (PartitioningOrigin.validateNode(retargeted)) {
+              retargeted
+            } else {
+              addExchange(partitioning, child)
+            }
+          case _ => addExchange(partitioning, child)
+        }
+      }
+    }
+    // Origin inheritance can tag a zero-move candidate, so it must not reuse 
the input union.
+    val rebuilt = union.copy(children = newChildren)
+    rebuilt.copyTagsFrom(union)
+    Option.when(
+      
PartitioningCollection.flatten(rebuilt.outputPartitioning).contains(target))(rebuilt)
+  }
+
+  private def samePartitionKeys(original: Partitioning, target: Partitioning): 
Boolean = {
+    (original, target) match {
+      case (from: HashPartitioning, to: HashPartitioning) =>
+        from.expressions.corresponds(to.expressions)(_.semanticEquals(_))
+      case _ => false
+    }
+  }
+
+  /**
+   * `target`, which is expressed in `union`'s output attributes, in `child`'s 
own: a child of a
+   * union holds the union's attributes under its own names, at the same 
positions.
+   *
+   * The child has to end up on this layout itself, not merely something that 
answers the same
+   * distribution, since `UnionExec` merges its children by equality. Hence 
the layout is rebound
+   * rather than rebuilt from a [[ShuffleSpec]] the way `EnsureRequirements` 
lays one child out on
+   * another's: a spec maps keys by their position in a distribution both 
children answer, and a
+   * union's children have no such distribution. A crossed projection can 
alias the key the
+   * consumer clusters on, leaving it absent from the layout altogether.
+   */
+  private def rebind(
+      target: Partitioning with Expression,
+      union: UnionExec,
+      child: SparkPlan): Partitioning = {
+    val attributes = AttributeMap(union.output.zip(child.output))
+    target.transform {
+      case attribute: Attribute if attributes.contains(attribute) => 
attributes(attribute)
+    }.asInstanceOf[Partitioning]
+  }
+
+  /**
+   * Whether moving the shuffle past `operator` preserves results. Only 
deterministic projects
+   * and filters, non-streaming grouped partial aggregates without session 
windows, and partial
+   * window group limits are admitted.
+   *
+   * The operators in between are all behind `lookThrough`, and the reducing 
ones are why: the
+   * rewrite shuffles a child before the reduction, which can be more data 
than the shuffle it
+   * replaces. These reducers stay above the union; target ranking does not 
estimate the lost
+   * reduction on moved branches.
+   *   - A project drops no row.
+   *   - A partial aggregate holds one buffer per group per input partition, 
and the aggregate
+   *     above the shuffle merges them, whichever way the rows arrived.
+   *   - A partial window group limit keeps the top rows per group per input 
partition, a superset
+   *     of the global top rows the final one then takes.
+   *   - A filter drops the same rows wherever they sit.
+   * A local sort is looked through by `findPushableShuffle` instead, since it 
re-establishes its
+   * ordering within whatever partitions it is handed; that is also why its 
ordering does not have
+   * to be deterministic.
+   */
+  private def canCross(operator: UnaryExecNode, lookThrough: Boolean): Boolean 
= operator match {
+    case project: ProjectExec =>
+      lookThrough && project.projectList.forall(_.deterministic)
+    case filter: FilterExec =>
+      lookThrough && filter.condition.deterministic
+    case aggregate: BaseAggregateExec =>
+      lookThrough &&
+        !aggregate.isStreaming &&
+        aggregate.requiredChildDistributionExpressions.isEmpty &&
+        aggregate.groupingExpressions.nonEmpty &&
+        
!aggregate.groupingExpressions.exists(_.metadata.contains(SessionWindow.marker))
 &&
+        aggregate.aggregateExpressions.forall(_.mode == PartialAggregate) &&
+        aggregate.expressions.forall(_.deterministic)
+    case limit: WindowGroupLimitExec =>
+      lookThrough && limit.mode == PartialWindowGroupLimit
+    case _ => false
+  }
+
+  /** Whether a shuffle on this partitioning is one the union's own children 
could replace. */
+  private def supportPushDown(partitioning: Partitioning): Boolean =
+    partitioning.isInstanceOf[HashPartitioning]
+
+  /**
+   * `child` sorted back to the ordering its operator requires, which is what 
put the local sort
+   * above the shuffle this rule moved, added the way [[EnsureRequirements]] 
adds it.
+   */
+  private def withOrdering(child: SparkPlan, requiredOrdering: 
Seq[SortOrder]): SparkPlan = {
+    if (SortOrder.orderingSatisfies(child.outputOrdering, requiredOrdering)) {
+      child
+    } else {
+      SortExec(requiredOrdering, global = false, child)
+    }
+  }
+}
+
+object PushDownShuffleExchangeThroughUnion {
+  private val PUSHED_DOWN_SHUFFLE = 
TreeNodeTag[Boolean]("unionPushedDownShuffle")
+
+  /**
+   * Whether this node carries the mark of a shuffle inserted below a union by 
push-down.
+   * Custom cost evaluators can distinguish these exchanges from ordinary 
shuffles;
+   * exchanges only retargeted by the rule are not newly marked.
+   */
+  def isPushedDownShuffle(plan: SparkPlan): Boolean = {
+    plan.getTagValue(PUSHED_DOWN_SHUFFLE).contains(true)
+  }
+
+  /**
+   * Whether this union represents one shuffle in the push-down cost model. 
Inspect the current
+   * shape on both sides of an adaptive cost comparison, rather than carrying 
a union tag across
+   * re-planning. Moved branches have direct requirement shuffles, or column 
repartitions whose
+   * counts differ from the effective default after retargeting; stages retain 
these properties.
+   * Column retargeting preserves keys in order. This is a shape inference, 
not provenance: pruning
+   * the only qualifying child makes a surviving union stop contributing its 
virtual shuffle cost.
+   * Update this check if the rewrite changes these shapes.
+   * Costs are compared before read optimization, which runs inside exchange 
or final result stages.
+   */
+  def hasPushedShuffle(union: UnionExec): Boolean = {
+    def pushed(plan: SparkPlan): Boolean = plan match {
+      case shuffle: ShuffleExchangeLike =>
+        isPushedDownShuffle(shuffle) || shuffle.shuffleOrigin == 
ENSURE_REQUIREMENTS ||

Review Comment:
   Fixed in `eb1baa2378b`: `hasPushedShuffle` counts only exchanges the rewrite 
marked. A union over requirement shuffles the rule did not create no longer 
counts, and the `REPARTITION_BY_COL` count-drift child is unmarked, so both 
shapes are gone. A branch served by reuse of an unmarked stage adds no shuffle 
and is not charged; a zero-move rewrite is not charged either.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/PushDownShuffleExchangeThroughUnion.scala:
##########
@@ -0,0 +1,614 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.adaptive
+
+import scala.annotation.tailrec
+import scala.collection.mutable
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap, 
Expression, SessionWindow, SortOrder}
+import org.apache.spark.sql.catalyst.expressions.aggregate.{Partial => 
PartialAggregate}
+import org.apache.spark.sql.catalyst.optimizer.BuildLeft
+import org.apache.spark.sql.catalyst.plans.physical.{ClusteredDistribution, 
HashPartitioning, HashShuffleSpec, Partitioning, PartitioningCollection}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreeNodeTag
+import org.apache.spark.sql.execution.{
+  FilterExec, ProjectExec, SortExec, SparkPlan, UnaryExecNode, UnionExec}
+import org.apache.spark.sql.execution.aggregate.BaseAggregateExec
+import org.apache.spark.sql.execution.exchange.{ENSURE_REQUIREMENTS, 
EnsureRequirements, Exchange, PartitioningOrigin, REPARTITION_BY_COL, 
REPARTITION_BY_NUM, ShuffleExchangeExec, ShuffleExchangeLike, 
ValidateRequirements}
+import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, 
BroadcastNestedLoopJoinExec, CartesianProductExec, ShuffledJoin}
+import org.apache.spark.sql.execution.window.{Partial => 
PartialWindowGroupLimit, WindowGroupLimitExec}
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * Push down a shuffle [[EnsureRequirements]] put above a [[UnionExec]] below 
the union. Eligible
+ * children already reporting the target retain their layouts; other children 
are retargeted or
+ * receive a new shuffle. Local sorts above the shuffle and operators between 
it and the union
+ * are rebuilt above the union:
+ *
+ * {{{
+ *   SortMergeJoin                             SortMergeJoin
+ *    :- Sort                                   :- Sort
+ *    :  +- Exchange                 =>         :  +- Union
+ *    :     +- Union                            :     :- HashAggregate
+ *    :        :- HashAggregate                 :     :  +- Exchange
+ *    :        :  +- Exchange                   :     :     +- Scan
+ *    :        :     +- Scan                    :     +- Exchange
+ *    :        +- Project                       :        +- Project
+ *    :           +- Scan                       :           +- Scan
+ *    +- ...                                    +- ...
+ * }}}
+ *
+ * The left child already answers the target, since it shuffles on the join 
key for its aggregate,
+ * so it keeps its layout and only the right child takes a shuffle.
+ *
+ * The operators between the shuffle and the union are looked through only 
under
+ * 
`spark.sql.adaptive.pushDownShuffleThroughUnion.lookThroughOperators.enabled`, 
and only when
+ * moving the shuffle past them preserves results: a deterministic project, a 
local sort, a
+ * filter, a partial aggregate and a partial window group limit. The reducing 
ones are why the
+ * gate exists: the child is then shuffled before the reduction, which can 
send more data than the
+ * one shuffle it replaces.
+ *
+ * Targets are hash layouts reported by the union's children, ranked by how 
many children offer
+ * them when size estimates are unavailable; otherwise estimated bytes kept 
decide first. The
+ * count must be at least the smaller of the replaced exchange's count and the 
effective initial
+ * shuffle partition count (`coalescePartitions.initialPartitionNum` when 
set). At least one
+ * child keeps its layout; others are retargeted or shuffled without changing 
child order.
+ * `spark.sql.adaptive.pushDownShuffleThroughUnion.maxMovedBranches` limits 
moved children, and
+ * `maxUnionChildren` limits all branches whose tails run within each consumer 
task.
+ * The rebuilt child must satisfy its consumer's distribution and pair with 
fixed siblings.
+ *
+ * Removing the union's exchange can reduce AQE join conversion and skew 
handling opportunities.
+ * Keeping a shuffled join child's layout can make skew join optimization or 
local shuffle reads
+ * invalidate an ancestor's distribution. AQE can discard the optimization for 
the round, or add
+ * another shuffle when skew optimization is forced. Join inputs and join 
branches are opt-in.
+ * Allowing targets via
+ * 
`spark.sql.adaptive.pushDownShuffleThroughUnion.keepNonCoalescibleBranches.enabled`
+ * can prevent coalescing of the consumer's compatible shuffle group. Forced 
skew optimization
+ * can restore a union exchange after moved branches have shuffled, shuffling 
them a second time.
+ *
+ * The query-wide subquery gate includes optimizer-injected runtime Bloom 
filter builds.
+ * Allowing subqueries can make pruning builds differ from main-query 
broadcast builds. With
+ * broadcast-only dynamic pruning, failed broadcast reuse removes the pruning 
filter and can scan
+ * all fact partitions; that cost can outweigh the saved build-side shuffle.
+ *
+ * The rule runs only in AQE preparation, after `EnsureRequirements` and before
+ * `StampUnionDecisions`. It preserves the root distribution and all user 
repartition layouts
+ * recorded when `EnsureRequirements` elides exchanges, including requirements 
below the root.
+ * Recording and push-down share the preparation's effective enablement on 
EnsureRequirements;
+ * classic and disabled preparations do not record these origins.
+ * Cost compensation belongs to SimpleCostEvaluator. Custom evaluators should 
account for the
+ * one-to-many exchange rewrite when this rule is enabled, or they may reject 
the entire re-plan.
+ *
+ * TODO: support push down of a keyed layout.
+ */
+case class PushDownShuffleExchangeThroughUnion(
+    ensureRequirements: EnsureRequirements) extends Rule[SparkPlan] {
+
+  override def apply(plan: SparkPlan): SparkPlan = {
+    if (!ensureRequirements.pushDownShuffleThroughUnionEnabled) {
+      return plan
+    }
+
+    val lookThrough =
+      
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_LOOK_THROUGH_OPERATORS_ENABLED)
+    val policy = LayoutPolicy(
+      allowNonCoalescible = !conf.getConf(SQLConf.COALESCE_PARTITIONS_ENABLED) 
|| conf.getConf(
+        
SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_KEEP_NON_COALESCIBLE_BRANCHES_ENABLED),
+      maxMovedBranches =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_MAX_MOVED_BRANCHES),
+      maxUnionChildren =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_MAX_UNION_CHILDREN),
+      keepJoinBranches =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_KEEP_JOIN_BRANCHES_ENABLED),
+      allowSubsetTargets = conf.getConf(SQLConf
+        
.PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_PARTITION_KEYS_SUBSET_OF_CLUSTER_KEYS_ENABLED))
+    val allowJoinInputs =
+      
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_JOIN_INPUTS_ENABLED)
+
+    // An exchange the plan reuses elsewhere is worth more than this rewrite: 
moving it below the
+    // union loses the stage the others reuse. See 
`AQEUtils.duplicatedShuffleForms`. With reuse
+    // off every exchange gets its own stage, so there is nothing to protect.
+    lazy val sharedExchanges: Set[SparkPlan] =
+      if (conf.exchangeReuseEnabled) AQEUtils.duplicatedShuffleForms(plan) 
else Set.empty
+
+    val pushed = plan.transformUp {
+      case join: ShuffledJoin if !allowJoinInputs => join
+      case operator: SparkPlan =>
+        val matched = operator.children.map { child =>
+          findPushableShuffle(child, lookThrough).filter { pushable =>
+            pushable.union.children.length <= policy.maxUnionChildren &&
+              (sharedExchanges.isEmpty ||
+                !sharedExchanges.contains(pushable.shuffle.canonicalized))
+          }
+        }
+        if (matched.forall(_.isEmpty)) {
+          operator
+        } else {
+          // Read requirements only after finding a site; some operators log 
when asked.
+          val required = operator.requiredChildDistribution
+          if (!required.forall(_.isInstanceOf[ClusteredDistribution])) {
+            operator
+          } else {
+            val requirements = 
required.map(_.asInstanceOf[ClusteredDistribution])
+            pushDownThroughUnion(
+              operator,
+              matched.zip(requirements),
+              sharedExchanges = sharedExchanges,
+              policy = policy.copy(allowNonCoalescible =
+                policy.allowNonCoalescible || 
!allExchangeInputs(operator))).getOrElse(operator)
+          }
+        }
+    }
+    if (pushed.fastEquals(plan)) {
+      plan
+    } else {
+      // Local checks do not cover ancestors or elided repartitions below the 
root. Reject the
+      // whole proposal if any plan requirement or recorded output layout is 
lost.
+      val satisfied = ensureRequirements.requiredDistribution match {
+        // A user repartition elided above was asked for on all its keys, so a 
layout on a subset
+        // of them does not stand in for it.
+        case Some(required: ClusteredDistribution) =>
+          ValidateRequirements.validate(pushed, 
required.copy(requireAllClusterKeys = true))
+        case Some(required) =>
+          ValidateRequirements.validate(pushed, required)
+        case None =>
+          ValidateRequirements.validate(pushed)
+      }
+      if (satisfied && !pushed.exists(node => 
!PartitioningOrigin.validateNode(node))) {
+        pushed
+      } else {
+        plan
+      }
+    }
+  }
+
+  /**
+   * One of an operator's children this rule can rewrite: the shuffle it moves 
and the union that
+   * shuffle is moved past.
+   */
+  private case class PushableChild(shuffle: ShuffleExchangeExec, union: 
UnionExec)
+
+  /**
+   * Layout selection settings, read once per pass.
+   */
+  private case class LayoutPolicy(
+      allowNonCoalescible: Boolean,
+      maxMovedBranches: Int,
+      maxUnionChildren: Int,
+      keepJoinBranches: Boolean,
+      allowSubsetTargets: Boolean)
+
+  /** Lazily discover and rank layouts once per site for both joint and 
fallback attempts. */
+  private class PreparedChild(val pushable: PushableChild, policy: 
LayoutPolicy) {
+    /**
+     * Eligible child layouts in child order, expressed in the union's output 
attributes.
+     * Collections offer their hash members; a child offering nothing takes a 
shuffle.
+     * Equality follows UnionExec's comparison after prepareOutputPartitioning 
rebinding.
+     */
+    lazy val layouts: Seq[Seq[Partitioning with Expression]] = {
+      val union = pushable.union
+      union.prepareOutputPartitioning().zip(union.children).map { case 
(partitioning, child) =>
+        if (keepableLayout(child, policy)) {
+          PartitioningCollection.flatten(partitioning).collect {
+            case hash: HashPartitioning => hash
+          }
+        } else {
+          Nil
+        }
+      }
+    }
+
+    lazy val targets: Seq[Partitioning with Expression] = {
+      val minPartitions = math.min(
+        pushable.shuffle.outputPartitioning.numPartitions, 
conf.numShufflePartitions)
+      candidateTargets(layouts, pushable.union.children, minPartitions)
+        .filter { target =>
+          layouts.count(child => !child.contains(target)) <= 
policy.maxMovedBranches
+        }
+    }
+  }
+
+  /**
+   * The shape this rule acts on below one of an operator's children: a 
pushable shuffle and the
+   * union it is moved past, with the operators in between looked through to 
reach it. Only a hash
+   * shuffle is pushable, since the layout that replaces it comes from the 
union's own children.
+   *
+   * What the child owes its operator is not read here: this is asked of every 
child of every
+   * operator, and `requiredChildDistribution` is not free of side effects: 
`WindowExecBase` logs
+   * from it for an unpartitioned window.
+   */
+  @tailrec
+  private def findPushableShuffle(
+      plan: SparkPlan,
+      lookThrough: Boolean,
+      found: Option[ShuffleExchangeExec] = None): Option[PushableChild] = {
+    plan match {
+      // A local sort above the shuffle is looked through, since the rebuilt 
child is sorted back
+      // to the same requirement; one below it is an operator in between and 
needs the gate.
+      case SortExec(_, false, child, _) if found.isEmpty || lookThrough =>
+        findPushableShuffle(child, lookThrough, found)
+      case shuffle @ ShuffleExchangeExec(partitioning, child, 
ENSURE_REQUIREMENTS, _, _)
+          if found.isEmpty && supportPushDown(partitioning) =>
+        findPushableShuffle(child, lookThrough, Some(shuffle))
+      // AttributeMap keeps the last duplicate exprId, while runtime binding 
uses the first.
+      // Positional rebinding is ambiguous for these unions, so retain their 
exchange.
+      case union: UnionExec if union.hasDuplicateChildOutputs => None
+      // Below it: the operators this rule can cross, down to the union the 
shuffle is moved past.
+      case union: UnionExec =>
+        found.map(PushableChild(_, union))
+      case operator: UnaryExecNode if found.isDefined && canCross(operator, 
lookThrough) =>
+        findPushableShuffle(operator.child, lookThrough, found)
+      case _ => None
+    }
+  }
+
+  /**
+   * Rewrites eligible children in order. If a joint rewrite fails, retry 
individual children
+   * against all current siblings, keeping children without a viable target 
unchanged.
+   *
+   * `pushable` holds one entry per child, in child order: what can be 
rewritten about it, and
+   * what it owes the operator.
+   */
+  private def pushDownThroughUnion(
+      operator: SparkPlan,
+      pushable: Seq[(Option[PushableChild], ClusteredDistribution)],
+      sharedExchanges: Set[SparkPlan],
+      policy: LayoutPolicy): Option[SparkPlan] = {
+    val children = operator.children
+    val requiredOrderings = operator.requiredChildOrdering
+
+    val prepared = pushable.map { case (site, _) => site.map { child =>
+      new PreparedChild(child, policy)
+    }}
+    val newChildren = new ArrayBuffer[SparkPlan](children.length)
+    def rebuildChildren(joint: Boolean): Boolean = {
+      newChildren.clear()
+      children.indices.forall { index =>
+        val (pushableChild, required) = pushable(index)
+        val rebuilt = prepared(index).flatMap { child =>
+          val fixed = children.indices.flatMap { position =>
+            val (sibling, distribution) = pushable(position)
+            Option.when(position != index && (!joint || position < index || 
sibling.isEmpty)) {
+              val fixedChild = if (position < index) {
+                newChildren(position)
+              } else {
+                children(position)
+              }
+              (fixedChild, distribution)
+            }
+          }
+          rebuildChild(child, required, fixed, sharedExchanges, policy)
+        }
+        if (joint && pushableChild.isDefined && rebuilt.isEmpty) {
+          false
+        } else {
+          newChildren += rebuilt.getOrElse(children(index))
+          true
+        }
+      }
+    }
+
+    // Joint selection lets later pushable siblings follow; the fallback fixes 
every sibling.
+    if (!rebuildChildren(joint = true)) {
+      rebuildChildren(joint = false)
+    }
+
+    // The ordering is put back once the layout is fixed. A local sort leaves 
the layout alone,
+    // and a child that already answers keeps its own.
+    val sorted = newChildren.toSeq.lazyZip(requiredOrderings).map {
+      case (child, ordering) => withOrdering(child, ordering)
+    }
+    Some(operator.withNewChildren(sorted))
+  }
+
+  /**
+   * Rebuild on the best eligible union layout satisfying the consumer and 
pairing with fixed
+   * siblings. Subset keys require both the rule's permission and the 
co-partition policy, even
+   * when a single-input consumer accepts a coarser distribution.
+   */
+  private def rebuildChild(
+      prepared: PreparedChild,
+      required: ClusteredDistribution,
+      fixed: Seq[(SparkPlan, ClusteredDistribution)],
+      sharedExchanges: Set[SparkPlan],
+      policy: LayoutPolicy): Option[SparkPlan] = {
+    val pushable = prepared.pushable
+    val layouts = prepared.layouts
+
+    // `findPushableShuffle` already checked this chain, including local sorts.
+    def rebuild(plan: SparkPlan, target: Partitioning with Expression): 
Option[SparkPlan] =
+      plan match {
+        case union: UnionExec => rebuildUnion(union, layouts, target, 
sharedExchanges)
+        case operator: UnaryExecNode =>
+          rebuild(operator.child, target).map { child =>
+            // A zero-move union can be structurally equal but must remain 
isolated for origin tags.
+            operator.mapChildrenWithReferenceEquality(_ => child)
+          }
+        case other =>
+          throw SparkException.internalError(
+            s"a shuffle push-down reached ${other.nodeName}, not the union it 
was found above")
+      }
+
+    prepared.targets.view
+      .flatMap(target => rebuild(pushable.shuffle.child, target))
+      .map(child => PartitioningOrigin.inherit(pushable.shuffle, child))
+      .find { child =>
+        val partitioning = child.outputPartitioning
+        partitioning.satisfies(required) &&
+          PartitioningCollection.specsForPairing(partitioning, 
required).exists { spec =>
+            spec.canCreatePartitioning && (policy.allowSubsetTargets || (spec 
match {
+              case hash: HashShuffleSpec =>
+                
required.areAllClusterKeysMatched(hash.partitioning.expressions)
+              case _ => false
+            }))
+          } &&
+          ValidateRequirements.satisfiesForPairing(
+            child +: fixed.map(_._1), required +: fixed.map(_._2))
+      }
+  }
+
+  private case class CandidateBenefit(
+      offeringChildren: Int,
+      keptBytes: BigInt,
+      firstOccurrence: Int)
+
+  /**
+   * Rank by estimated bytes kept when every child has a usable estimate. 
Missing estimates
+   * trigger count-based ranking rather than treating unknown branches as 
smaller. Partition
+   * count and first occurrence break ties deterministically.
+   */
+  private def candidateTargets(
+      layouts: Seq[Seq[Partitioning with Expression]],
+      children: Seq[SparkPlan],
+      minPartitions: Int): Seq[Partitioning with Expression] = {
+    val sizes = children.map { child =>
+      child.logicalLink.map(_.stats.sizeInBytes).filter(_ < 
conf.defaultSizeInBytes)
+    }
+    val useSizes = sizes.forall(_.isDefined)
+    val benefits = mutable.HashMap.empty[Partitioning with Expression, 
CandidateBenefit]
+    layouts.zip(sizes).foreach { case (reported, size) =>
+      reported.filter(_.numPartitions >= minPartitions).distinct.foreach { 
target =>
+        val previous = benefits.getOrElse(target, CandidateBenefit(0, 
BigInt(0), benefits.size))
+        benefits.update(target, previous.copy(
+          offeringChildren = previous.offeringChildren + 1,
+          keptBytes = previous.keptBytes + size.getOrElse(BigInt(0))))
+      }
+    }
+    benefits.toSeq.sortBy { case (target, benefit) =>
+        val keptBytes = if (useSizes) { benefit.keptBytes } else { BigInt(0) }
+        (-keptBytes, -benefit.offeringChildren, -target.numPartitions, 
benefit.firstOccurrence)
+      }
+      .map(_._1)
+  }
+
+  /**
+   * Whether every input path reaches an exchange boundary. Stop at unstaged 
exchanges as well
+   * as query stages: their input scans do not prevent coalescing. A 
non-exchange leaf already
+   * prevents the consumer's compatible group from coalescing, independently 
of target selection.
+   */
+  private def allExchangeInputs(plan: SparkPlan): Boolean = plan match {
+    case _: Exchange => true
+    case _: ExchangeQueryStageExec => true
+    case leaf if leaf.children.isEmpty => false
+    case other => other.children.forall(allExchangeInputs)
+  }
+
+  /**
+   * Whether a union child may retain its layout as a target. Check every 
input path, stopping at
+   * shuffle boundaries: requirement and column repartitions are eligible. 
Fixed-count shuffles
+   * and non-shuffle leaves require the non-coalescible allowance; other 
origins are rejected.
+   * Shuffled joins require the join-branch opt-in; exploding joins require 
the non-coalescible
+   * allowance. Unions with duplicate child output identities cannot supply a 
kept layout, even
+   * through wrappers, because their positional mapping is ambiguous.
+   * Broadcast joins inspect only their streamed side. 
`fromBroadcastHashJoinExec` follows that
+   * probe through wrappers and stages so requirement shuffles also require 
the join-branch opt-in,
+   * preserving opportunities for adaptive skew splitting and local reads.
+   */
+  private def keepableLayout(
+      plan: SparkPlan,
+      policy: LayoutPolicy,
+      fromBroadcastHashJoinExec: Boolean = false): Boolean = plan match {
+    case shuffle: ShuffleExchangeLike =>
+      shuffle.shuffleOrigin match {
+        case ENSURE_REQUIREMENTS => !fromBroadcastHashJoinExec || 
policy.keepJoinBranches
+        case REPARTITION_BY_COL => true
+        case REPARTITION_BY_NUM => policy.allowNonCoalescible
+        case _ => false
+      }
+    case stage: ShuffleQueryStageExec =>
+      keepableLayout(stage.shuffle, policy, fromBroadcastHashJoinExec)
+    case union: UnionExec if union.hasDuplicateChildOutputs => false
+    case _: ShuffledJoin if !policy.keepJoinBranches => false
+    case _: BroadcastNestedLoopJoinExec | _: CartesianProductExec
+        if !policy.allowNonCoalescible => false
+    case join: BroadcastHashJoinExec =>
+      val streamed = if (join.buildSide == BuildLeft) { join.right } else { 
join.left }
+      keepableLayout(streamed, policy, fromBroadcastHashJoinExec = true)
+    case join: BroadcastNestedLoopJoinExec =>
+      val streamed = if (join.buildSide == BuildLeft) { join.right } else { 
join.left }
+      keepableLayout(streamed, policy, fromBroadcastHashJoinExec)
+    case leaf if leaf.children.isEmpty => policy.allowNonCoalescible
+    case other => other.children.forall(keepableLayout(_, policy, 
fromBroadcastHashJoinExec))
+  }
+
+  /**
+   * `union` rebuilt on `target`: eligible children are kept, unshared movable 
exchanges are
+   * retargeted, and other children get an exchange. `None` if the union does 
not report `target`,
+   * which is stricter than satisfying the consumer's distribution. 
Retargeting is limited to
+   * direct, unshared requirement or column-repartition exchanges. Column 
repartitions retain their
+   * keys in order; a key change or recorded-layout violation keeps the 
exchange and adds one above.
+   * Tag newly added exchanges for cost accounting; retargeting adds no 
shuffle.
+   */
+  private def rebuildUnion(
+      union: UnionExec,
+      layouts: Seq[Seq[Partitioning with Expression]],
+      target: Partitioning with Expression,
+      sharedExchanges: Set[SparkPlan]): Option[SparkPlan] = {
+    def addExchange(partitioning: Partitioning, child: SparkPlan): 
ShuffleExchangeExec = {
+      val exchange = ShuffleExchangeExec(partitioning, child, 
ENSURE_REQUIREMENTS)
+      
exchange.setTagValue(PushDownShuffleExchangeThroughUnion.PUSHED_DOWN_SHUFFLE, 
true)
+      exchange
+    }
+    val newChildren = union.children.zip(layouts).map { case (child, reported) 
=>
+      if (reported.contains(target)) {
+        child
+      } else {
+        val partitioning = rebind(target, union, child)
+        child match {
+          case shuffle: ShuffleExchangeExec
+              if (shuffle.shuffleOrigin == ENSURE_REQUIREMENTS ||
+                  (shuffle.shuffleOrigin == REPARTITION_BY_COL &&
+                    samePartitionKeys(shuffle.outputPartitioning, 
partitioning))) &&
+                (sharedExchanges.isEmpty || 
!sharedExchanges.contains(shuffle.canonicalized)) =>
+            val retargeted = shuffle.copy(outputPartitioning = partitioning)
+            retargeted.copyTagsFrom(shuffle)
+            if (PartitioningOrigin.validateNode(retargeted)) {
+              retargeted
+            } else {
+              addExchange(partitioning, child)
+            }
+          case _ => addExchange(partitioning, child)

Review Comment:
   Fixed in `eb1baa2378b`: a target survives only when every direct 
`ShuffleQueryStageExec` child reports it 
(`stagedIndices.subsetOf(benefit.offeredBy)`). Covered by plan-level pin tests 
and an end-to-end test where the target flips after a stage reports runtime 
statistics.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala:
##########
@@ -116,7 +116,8 @@ case class AdaptiveSparkPlanExec(
         CostEvaluator.instantiate(className, 
context.session.sparkContext.getConf)
       case _ => SimpleCostEvaluator(
         conf.getConf(SQLConf.ADAPTIVE_FORCE_OPTIMIZE_SKEWED_JOIN),
-        conf.costEvaluatorCountLocalSortEnabled)
+        conf.costEvaluatorCountLocalSortEnabled,
+        conf.pushDownShuffleThroughUnionEnabled)

Review Comment:
   Fixed in `eb1baa2378b`: one effective `pushDownShuffleThroughUnionEnabled` 
value feeds `EnsureRequirements`, the rule and `SimpleCostEvaluator`; the 
evaluator no longer reads the raw conf.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/PushDownShuffleExchangeThroughUnion.scala:
##########
@@ -0,0 +1,614 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.adaptive
+
+import scala.annotation.tailrec
+import scala.collection.mutable
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap, 
Expression, SessionWindow, SortOrder}
+import org.apache.spark.sql.catalyst.expressions.aggregate.{Partial => 
PartialAggregate}
+import org.apache.spark.sql.catalyst.optimizer.BuildLeft
+import org.apache.spark.sql.catalyst.plans.physical.{ClusteredDistribution, 
HashPartitioning, HashShuffleSpec, Partitioning, PartitioningCollection}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreeNodeTag
+import org.apache.spark.sql.execution.{
+  FilterExec, ProjectExec, SortExec, SparkPlan, UnaryExecNode, UnionExec}
+import org.apache.spark.sql.execution.aggregate.BaseAggregateExec
+import org.apache.spark.sql.execution.exchange.{ENSURE_REQUIREMENTS, 
EnsureRequirements, Exchange, PartitioningOrigin, REPARTITION_BY_COL, 
REPARTITION_BY_NUM, ShuffleExchangeExec, ShuffleExchangeLike, 
ValidateRequirements}
+import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, 
BroadcastNestedLoopJoinExec, CartesianProductExec, ShuffledJoin}
+import org.apache.spark.sql.execution.window.{Partial => 
PartialWindowGroupLimit, WindowGroupLimitExec}
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * Push down a shuffle [[EnsureRequirements]] put above a [[UnionExec]] below 
the union. Eligible
+ * children already reporting the target retain their layouts; other children 
are retargeted or
+ * receive a new shuffle. Local sorts above the shuffle and operators between 
it and the union
+ * are rebuilt above the union:
+ *
+ * {{{
+ *   SortMergeJoin                             SortMergeJoin
+ *    :- Sort                                   :- Sort
+ *    :  +- Exchange                 =>         :  +- Union
+ *    :     +- Union                            :     :- HashAggregate
+ *    :        :- HashAggregate                 :     :  +- Exchange
+ *    :        :  +- Exchange                   :     :     +- Scan
+ *    :        :     +- Scan                    :     +- Exchange
+ *    :        +- Project                       :        +- Project
+ *    :           +- Scan                       :           +- Scan
+ *    +- ...                                    +- ...
+ * }}}
+ *
+ * The left child already answers the target, since it shuffles on the join 
key for its aggregate,
+ * so it keeps its layout and only the right child takes a shuffle.
+ *
+ * The operators between the shuffle and the union are looked through only 
under
+ * 
`spark.sql.adaptive.pushDownShuffleThroughUnion.lookThroughOperators.enabled`, 
and only when
+ * moving the shuffle past them preserves results: a deterministic project, a 
local sort, a
+ * filter, a partial aggregate and a partial window group limit. The reducing 
ones are why the
+ * gate exists: the child is then shuffled before the reduction, which can 
send more data than the
+ * one shuffle it replaces.
+ *
+ * Targets are hash layouts reported by the union's children, ranked by how 
many children offer
+ * them when size estimates are unavailable; otherwise estimated bytes kept 
decide first. The
+ * count must be at least the smaller of the replaced exchange's count and the 
effective initial
+ * shuffle partition count (`coalescePartitions.initialPartitionNum` when 
set). At least one
+ * child keeps its layout; others are retargeted or shuffled without changing 
child order.
+ * `spark.sql.adaptive.pushDownShuffleThroughUnion.maxMovedBranches` limits 
moved children, and
+ * `maxUnionChildren` limits all branches whose tails run within each consumer 
task.
+ * The rebuilt child must satisfy its consumer's distribution and pair with 
fixed siblings.
+ *
+ * Removing the union's exchange can reduce AQE join conversion and skew 
handling opportunities.
+ * Keeping a shuffled join child's layout can make skew join optimization or 
local shuffle reads
+ * invalidate an ancestor's distribution. AQE can discard the optimization for 
the round, or add
+ * another shuffle when skew optimization is forced. Join inputs and join 
branches are opt-in.
+ * Allowing targets via
+ * 
`spark.sql.adaptive.pushDownShuffleThroughUnion.keepNonCoalescibleBranches.enabled`
+ * can prevent coalescing of the consumer's compatible shuffle group. Forced 
skew optimization
+ * can restore a union exchange after moved branches have shuffled, shuffling 
them a second time.
+ *
+ * The query-wide subquery gate includes optimizer-injected runtime Bloom 
filter builds.
+ * Allowing subqueries can make pruning builds differ from main-query 
broadcast builds. With
+ * broadcast-only dynamic pruning, failed broadcast reuse removes the pruning 
filter and can scan
+ * all fact partitions; that cost can outweigh the saved build-side shuffle.
+ *
+ * The rule runs only in AQE preparation, after `EnsureRequirements` and before
+ * `StampUnionDecisions`. It preserves the root distribution and all user 
repartition layouts
+ * recorded when `EnsureRequirements` elides exchanges, including requirements 
below the root.
+ * Recording and push-down share the preparation's effective enablement on 
EnsureRequirements;
+ * classic and disabled preparations do not record these origins.
+ * Cost compensation belongs to SimpleCostEvaluator. Custom evaluators should 
account for the
+ * one-to-many exchange rewrite when this rule is enabled, or they may reject 
the entire re-plan.
+ *
+ * TODO: support push down of a keyed layout.
+ */
+case class PushDownShuffleExchangeThroughUnion(
+    ensureRequirements: EnsureRequirements) extends Rule[SparkPlan] {
+
+  override def apply(plan: SparkPlan): SparkPlan = {
+    if (!ensureRequirements.pushDownShuffleThroughUnionEnabled) {
+      return plan
+    }
+
+    val lookThrough =
+      
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_LOOK_THROUGH_OPERATORS_ENABLED)
+    val policy = LayoutPolicy(
+      allowNonCoalescible = !conf.getConf(SQLConf.COALESCE_PARTITIONS_ENABLED) 
|| conf.getConf(
+        
SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_KEEP_NON_COALESCIBLE_BRANCHES_ENABLED),
+      maxMovedBranches =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_MAX_MOVED_BRANCHES),
+      maxUnionChildren =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_MAX_UNION_CHILDREN),
+      keepJoinBranches =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_KEEP_JOIN_BRANCHES_ENABLED),
+      allowSubsetTargets = conf.getConf(SQLConf
+        
.PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_PARTITION_KEYS_SUBSET_OF_CLUSTER_KEYS_ENABLED))
+    val allowJoinInputs =
+      
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_JOIN_INPUTS_ENABLED)
+
+    // An exchange the plan reuses elsewhere is worth more than this rewrite: 
moving it below the
+    // union loses the stage the others reuse. See 
`AQEUtils.duplicatedShuffleForms`. With reuse
+    // off every exchange gets its own stage, so there is nothing to protect.
+    lazy val sharedExchanges: Set[SparkPlan] =
+      if (conf.exchangeReuseEnabled) AQEUtils.duplicatedShuffleForms(plan) 
else Set.empty
+
+    val pushed = plan.transformUp {
+      case join: ShuffledJoin if !allowJoinInputs => join
+      case operator: SparkPlan =>
+        val matched = operator.children.map { child =>
+          findPushableShuffle(child, lookThrough).filter { pushable =>
+            pushable.union.children.length <= policy.maxUnionChildren &&
+              (sharedExchanges.isEmpty ||
+                !sharedExchanges.contains(pushable.shuffle.canonicalized))
+          }
+        }
+        if (matched.forall(_.isEmpty)) {
+          operator
+        } else {
+          // Read requirements only after finding a site; some operators log 
when asked.
+          val required = operator.requiredChildDistribution
+          if (!required.forall(_.isInstanceOf[ClusteredDistribution])) {
+            operator
+          } else {
+            val requirements = 
required.map(_.asInstanceOf[ClusteredDistribution])
+            pushDownThroughUnion(
+              operator,
+              matched.zip(requirements),
+              sharedExchanges = sharedExchanges,
+              policy = policy.copy(allowNonCoalescible =
+                policy.allowNonCoalescible || 
!allExchangeInputs(operator))).getOrElse(operator)
+          }
+        }
+    }
+    if (pushed.fastEquals(plan)) {
+      plan
+    } else {
+      // Local checks do not cover ancestors or elided repartitions below the 
root. Reject the
+      // whole proposal if any plan requirement or recorded output layout is 
lost.
+      val satisfied = ensureRequirements.requiredDistribution match {
+        // A user repartition elided above was asked for on all its keys, so a 
layout on a subset
+        // of them does not stand in for it.
+        case Some(required: ClusteredDistribution) =>
+          ValidateRequirements.validate(pushed, 
required.copy(requireAllClusterKeys = true))
+        case Some(required) =>
+          ValidateRequirements.validate(pushed, required)
+        case None =>
+          ValidateRequirements.validate(pushed)
+      }
+      if (satisfied && !pushed.exists(node => 
!PartitioningOrigin.validateNode(node))) {

Review Comment:
   Fixed in `eb1baa2378b`: on validation failure the rewrite retries once with 
every site restricted to the removed exchange's own layout. The SMJ + window 
test reproduced the rollback before the retry and pushes down afterwards.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/PushDownShuffleExchangeThroughUnion.scala:
##########
@@ -0,0 +1,614 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.adaptive
+
+import scala.annotation.tailrec
+import scala.collection.mutable
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeMap, 
Expression, SessionWindow, SortOrder}
+import org.apache.spark.sql.catalyst.expressions.aggregate.{Partial => 
PartialAggregate}
+import org.apache.spark.sql.catalyst.optimizer.BuildLeft
+import org.apache.spark.sql.catalyst.plans.physical.{ClusteredDistribution, 
HashPartitioning, HashShuffleSpec, Partitioning, PartitioningCollection}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreeNodeTag
+import org.apache.spark.sql.execution.{
+  FilterExec, ProjectExec, SortExec, SparkPlan, UnaryExecNode, UnionExec}
+import org.apache.spark.sql.execution.aggregate.BaseAggregateExec
+import org.apache.spark.sql.execution.exchange.{ENSURE_REQUIREMENTS, 
EnsureRequirements, Exchange, PartitioningOrigin, REPARTITION_BY_COL, 
REPARTITION_BY_NUM, ShuffleExchangeExec, ShuffleExchangeLike, 
ValidateRequirements}
+import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, 
BroadcastNestedLoopJoinExec, CartesianProductExec, ShuffledJoin}
+import org.apache.spark.sql.execution.window.{Partial => 
PartialWindowGroupLimit, WindowGroupLimitExec}
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * Push down a shuffle [[EnsureRequirements]] put above a [[UnionExec]] below 
the union. Eligible
+ * children already reporting the target retain their layouts; other children 
are retargeted or
+ * receive a new shuffle. Local sorts above the shuffle and operators between 
it and the union
+ * are rebuilt above the union:
+ *
+ * {{{
+ *   SortMergeJoin                             SortMergeJoin
+ *    :- Sort                                   :- Sort
+ *    :  +- Exchange                 =>         :  +- Union
+ *    :     +- Union                            :     :- HashAggregate
+ *    :        :- HashAggregate                 :     :  +- Exchange
+ *    :        :  +- Exchange                   :     :     +- Scan
+ *    :        :     +- Scan                    :     +- Exchange
+ *    :        +- Project                       :        +- Project
+ *    :           +- Scan                       :           +- Scan
+ *    +- ...                                    +- ...
+ * }}}
+ *
+ * The left child already answers the target, since it shuffles on the join 
key for its aggregate,
+ * so it keeps its layout and only the right child takes a shuffle.
+ *
+ * The operators between the shuffle and the union are looked through only 
under
+ * 
`spark.sql.adaptive.pushDownShuffleThroughUnion.lookThroughOperators.enabled`, 
and only when
+ * moving the shuffle past them preserves results: a deterministic project, a 
local sort, a
+ * filter, a partial aggregate and a partial window group limit. The reducing 
ones are why the
+ * gate exists: the child is then shuffled before the reduction, which can 
send more data than the
+ * one shuffle it replaces.
+ *
+ * Targets are hash layouts reported by the union's children, ranked by how 
many children offer
+ * them when size estimates are unavailable; otherwise estimated bytes kept 
decide first. The
+ * count must be at least the smaller of the replaced exchange's count and the 
effective initial
+ * shuffle partition count (`coalescePartitions.initialPartitionNum` when 
set). At least one
+ * child keeps its layout; others are retargeted or shuffled without changing 
child order.
+ * `spark.sql.adaptive.pushDownShuffleThroughUnion.maxMovedBranches` limits 
moved children, and
+ * `maxUnionChildren` limits all branches whose tails run within each consumer 
task.
+ * The rebuilt child must satisfy its consumer's distribution and pair with 
fixed siblings.
+ *
+ * Removing the union's exchange can reduce AQE join conversion and skew 
handling opportunities.
+ * Keeping a shuffled join child's layout can make skew join optimization or 
local shuffle reads
+ * invalidate an ancestor's distribution. AQE can discard the optimization for 
the round, or add
+ * another shuffle when skew optimization is forced. Join inputs and join 
branches are opt-in.
+ * Allowing targets via
+ * 
`spark.sql.adaptive.pushDownShuffleThroughUnion.keepNonCoalescibleBranches.enabled`
+ * can prevent coalescing of the consumer's compatible shuffle group. Forced 
skew optimization
+ * can restore a union exchange after moved branches have shuffled, shuffling 
them a second time.
+ *
+ * The query-wide subquery gate includes optimizer-injected runtime Bloom 
filter builds.
+ * Allowing subqueries can make pruning builds differ from main-query 
broadcast builds. With
+ * broadcast-only dynamic pruning, failed broadcast reuse removes the pruning 
filter and can scan
+ * all fact partitions; that cost can outweigh the saved build-side shuffle.
+ *
+ * The rule runs only in AQE preparation, after `EnsureRequirements` and before
+ * `StampUnionDecisions`. It preserves the root distribution and all user 
repartition layouts
+ * recorded when `EnsureRequirements` elides exchanges, including requirements 
below the root.
+ * Recording and push-down share the preparation's effective enablement on 
EnsureRequirements;
+ * classic and disabled preparations do not record these origins.
+ * Cost compensation belongs to SimpleCostEvaluator. Custom evaluators should 
account for the
+ * one-to-many exchange rewrite when this rule is enabled, or they may reject 
the entire re-plan.
+ *
+ * TODO: support push down of a keyed layout.
+ */
+case class PushDownShuffleExchangeThroughUnion(
+    ensureRequirements: EnsureRequirements) extends Rule[SparkPlan] {
+
+  override def apply(plan: SparkPlan): SparkPlan = {
+    if (!ensureRequirements.pushDownShuffleThroughUnionEnabled) {
+      return plan
+    }
+
+    val lookThrough =
+      
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_LOOK_THROUGH_OPERATORS_ENABLED)
+    val policy = LayoutPolicy(
+      allowNonCoalescible = !conf.getConf(SQLConf.COALESCE_PARTITIONS_ENABLED) 
|| conf.getConf(
+        
SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_KEEP_NON_COALESCIBLE_BRANCHES_ENABLED),
+      maxMovedBranches =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_MAX_MOVED_BRANCHES),
+      maxUnionChildren =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_MAX_UNION_CHILDREN),
+      keepJoinBranches =
+        
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_KEEP_JOIN_BRANCHES_ENABLED),
+      allowSubsetTargets = conf.getConf(SQLConf
+        
.PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_PARTITION_KEYS_SUBSET_OF_CLUSTER_KEYS_ENABLED))
+    val allowJoinInputs =
+      
conf.getConf(SQLConf.PUSH_DOWN_SHUFFLE_THROUGH_UNION_ALLOW_JOIN_INPUTS_ENABLED)
+
+    // An exchange the plan reuses elsewhere is worth more than this rewrite: 
moving it below the
+    // union loses the stage the others reuse. See 
`AQEUtils.duplicatedShuffleForms`. With reuse
+    // off every exchange gets its own stage, so there is nothing to protect.
+    lazy val sharedExchanges: Set[SparkPlan] =
+      if (conf.exchangeReuseEnabled) AQEUtils.duplicatedShuffleForms(plan) 
else Set.empty
+
+    val pushed = plan.transformUp {
+      case join: ShuffledJoin if !allowJoinInputs => join
+      case operator: SparkPlan =>
+        val matched = operator.children.map { child =>
+          findPushableShuffle(child, lookThrough).filter { pushable =>
+            pushable.union.children.length <= policy.maxUnionChildren &&
+              (sharedExchanges.isEmpty ||
+                !sharedExchanges.contains(pushable.shuffle.canonicalized))
+          }
+        }
+        if (matched.forall(_.isEmpty)) {
+          operator
+        } else {
+          // Read requirements only after finding a site; some operators log 
when asked.
+          val required = operator.requiredChildDistribution
+          if (!required.forall(_.isInstanceOf[ClusteredDistribution])) {
+            operator
+          } else {
+            val requirements = 
required.map(_.asInstanceOf[ClusteredDistribution])
+            pushDownThroughUnion(
+              operator,
+              matched.zip(requirements),
+              sharedExchanges = sharedExchanges,
+              policy = policy.copy(allowNonCoalescible =
+                policy.allowNonCoalescible || 
!allExchangeInputs(operator))).getOrElse(operator)
+          }
+        }
+    }
+    if (pushed.fastEquals(plan)) {
+      plan
+    } else {
+      // Local checks do not cover ancestors or elided repartitions below the 
root. Reject the
+      // whole proposal if any plan requirement or recorded output layout is 
lost.
+      val satisfied = ensureRequirements.requiredDistribution match {
+        // A user repartition elided above was asked for on all its keys, so a 
layout on a subset
+        // of them does not stand in for it.
+        case Some(required: ClusteredDistribution) =>
+          ValidateRequirements.validate(pushed, 
required.copy(requireAllClusterKeys = true))
+        case Some(required) =>
+          ValidateRequirements.validate(pushed, required)
+        case None =>
+          ValidateRequirements.validate(pushed)
+      }
+      if (satisfied && !pushed.exists(node => 
!PartitioningOrigin.validateNode(node))) {
+        pushed
+      } else {
+        plan
+      }
+    }
+  }
+
+  /**
+   * One of an operator's children this rule can rewrite: the shuffle it moves 
and the union that
+   * shuffle is moved past.
+   */
+  private case class PushableChild(shuffle: ShuffleExchangeExec, union: 
UnionExec)
+
+  /**
+   * Layout selection settings, read once per pass.
+   */
+  private case class LayoutPolicy(
+      allowNonCoalescible: Boolean,
+      maxMovedBranches: Int,
+      maxUnionChildren: Int,
+      keepJoinBranches: Boolean,
+      allowSubsetTargets: Boolean)
+
+  /** Lazily discover and rank layouts once per site for both joint and 
fallback attempts. */
+  private class PreparedChild(val pushable: PushableChild, policy: 
LayoutPolicy) {
+    /**
+     * Eligible child layouts in child order, expressed in the union's output 
attributes.
+     * Collections offer their hash members; a child offering nothing takes a 
shuffle.
+     * Equality follows UnionExec's comparison after prepareOutputPartitioning 
rebinding.
+     */
+    lazy val layouts: Seq[Seq[Partitioning with Expression]] = {
+      val union = pushable.union
+      union.prepareOutputPartitioning().zip(union.children).map { case 
(partitioning, child) =>
+        if (keepableLayout(child, policy)) {
+          PartitioningCollection.flatten(partitioning).collect {
+            case hash: HashPartitioning => hash
+          }
+        } else {
+          Nil
+        }
+      }
+    }
+
+    lazy val targets: Seq[Partitioning with Expression] = {
+      val minPartitions = math.min(
+        pushable.shuffle.outputPartitioning.numPartitions, 
conf.numShufflePartitions)
+      candidateTargets(layouts, pushable.union.children, minPartitions)
+        .filter { target =>
+          layouts.count(child => !child.contains(target)) <= 
policy.maxMovedBranches
+        }
+    }
+  }
+
+  /**
+   * The shape this rule acts on below one of an operator's children: a 
pushable shuffle and the
+   * union it is moved past, with the operators in between looked through to 
reach it. Only a hash
+   * shuffle is pushable, since the layout that replaces it comes from the 
union's own children.
+   *
+   * What the child owes its operator is not read here: this is asked of every 
child of every
+   * operator, and `requiredChildDistribution` is not free of side effects: 
`WindowExecBase` logs
+   * from it for an unpartitioned window.
+   */
+  @tailrec
+  private def findPushableShuffle(
+      plan: SparkPlan,
+      lookThrough: Boolean,
+      found: Option[ShuffleExchangeExec] = None): Option[PushableChild] = {
+    plan match {
+      // A local sort above the shuffle is looked through, since the rebuilt 
child is sorted back
+      // to the same requirement; one below it is an operator in between and 
needs the gate.
+      case SortExec(_, false, child, _) if found.isEmpty || lookThrough =>
+        findPushableShuffle(child, lookThrough, found)
+      case shuffle @ ShuffleExchangeExec(partitioning, child, 
ENSURE_REQUIREMENTS, _, _)
+          if found.isEmpty && supportPushDown(partitioning) =>
+        findPushableShuffle(child, lookThrough, Some(shuffle))
+      // AttributeMap keeps the last duplicate exprId, while runtime binding 
uses the first.
+      // Positional rebinding is ambiguous for these unions, so retain their 
exchange.
+      case union: UnionExec if union.hasDuplicateChildOutputs => None
+      // Below it: the operators this rule can cross, down to the union the 
shuffle is moved past.
+      case union: UnionExec =>
+        found.map(PushableChild(_, union))
+      case operator: UnaryExecNode if found.isDefined && canCross(operator, 
lookThrough) =>
+        findPushableShuffle(operator.child, lookThrough, found)
+      case _ => None
+    }
+  }
+
+  /**
+   * Rewrites eligible children in order. If a joint rewrite fails, retry 
individual children
+   * against all current siblings, keeping children without a viable target 
unchanged.
+   *
+   * `pushable` holds one entry per child, in child order: what can be 
rewritten about it, and
+   * what it owes the operator.
+   */
+  private def pushDownThroughUnion(
+      operator: SparkPlan,
+      pushable: Seq[(Option[PushableChild], ClusteredDistribution)],
+      sharedExchanges: Set[SparkPlan],
+      policy: LayoutPolicy): Option[SparkPlan] = {
+    val children = operator.children
+    val requiredOrderings = operator.requiredChildOrdering
+
+    val prepared = pushable.map { case (site, _) => site.map { child =>
+      new PreparedChild(child, policy)
+    }}
+    val newChildren = new ArrayBuffer[SparkPlan](children.length)
+    def rebuildChildren(joint: Boolean): Boolean = {
+      newChildren.clear()
+      children.indices.forall { index =>
+        val (pushableChild, required) = pushable(index)
+        val rebuilt = prepared(index).flatMap { child =>
+          val fixed = children.indices.flatMap { position =>
+            val (sibling, distribution) = pushable(position)
+            Option.when(position != index && (!joint || position < index || 
sibling.isEmpty)) {
+              val fixedChild = if (position < index) {
+                newChildren(position)
+              } else {
+                children(position)
+              }
+              (fixedChild, distribution)
+            }
+          }
+          rebuildChild(child, required, fixed, sharedExchanges, policy)
+        }
+        if (joint && pushableChild.isDefined && rebuilt.isEmpty) {
+          false
+        } else {
+          newChildren += rebuilt.getOrElse(children(index))
+          true
+        }
+      }
+    }
+
+    // Joint selection lets later pushable siblings follow; the fallback fixes 
every sibling.
+    if (!rebuildChildren(joint = true)) {
+      rebuildChildren(joint = false)
+    }
+
+    // The ordering is put back once the layout is fixed. A local sort leaves 
the layout alone,
+    // and a child that already answers keeps its own.
+    val sorted = newChildren.toSeq.lazyZip(requiredOrderings).map {
+      case (child, ordering) => withOrdering(child, ordering)
+    }
+    Some(operator.withNewChildren(sorted))
+  }
+
+  /**
+   * Rebuild on the best eligible union layout satisfying the consumer and 
pairing with fixed
+   * siblings. Subset keys require both the rule's permission and the 
co-partition policy, even
+   * when a single-input consumer accepts a coarser distribution.
+   */
+  private def rebuildChild(
+      prepared: PreparedChild,
+      required: ClusteredDistribution,
+      fixed: Seq[(SparkPlan, ClusteredDistribution)],
+      sharedExchanges: Set[SparkPlan],
+      policy: LayoutPolicy): Option[SparkPlan] = {
+    val pushable = prepared.pushable
+    val layouts = prepared.layouts
+
+    // `findPushableShuffle` already checked this chain, including local sorts.
+    def rebuild(plan: SparkPlan, target: Partitioning with Expression): 
Option[SparkPlan] =
+      plan match {
+        case union: UnionExec => rebuildUnion(union, layouts, target, 
sharedExchanges)
+        case operator: UnaryExecNode =>
+          rebuild(operator.child, target).map { child =>
+            // A zero-move union can be structurally equal but must remain 
isolated for origin tags.
+            operator.mapChildrenWithReferenceEquality(_ => child)
+          }
+        case other =>
+          throw SparkException.internalError(
+            s"a shuffle push-down reached ${other.nodeName}, not the union it 
was found above")
+      }
+
+    prepared.targets.view
+      .flatMap(target => rebuild(pushable.shuffle.child, target))
+      .map(child => PartitioningOrigin.inherit(pushable.shuffle, child))
+      .find { child =>
+        val partitioning = child.outputPartitioning
+        partitioning.satisfies(required) &&
+          PartitioningCollection.specsForPairing(partitioning, 
required).exists { spec =>
+            spec.canCreatePartitioning && (policy.allowSubsetTargets || (spec 
match {
+              case hash: HashShuffleSpec =>
+                
required.areAllClusterKeysMatched(hash.partitioning.expressions)
+              case _ => false
+            }))
+          } &&
+          ValidateRequirements.satisfiesForPairing(
+            child +: fixed.map(_._1), required +: fixed.map(_._2))
+      }
+  }
+
+  private case class CandidateBenefit(
+      offeringChildren: Int,
+      keptBytes: BigInt,
+      firstOccurrence: Int)
+
+  /**
+   * Rank by estimated bytes kept when every child has a usable estimate. 
Missing estimates
+   * trigger count-based ranking rather than treating unknown branches as 
smaller. Partition
+   * count and first occurrence break ties deterministically.
+   */
+  private def candidateTargets(
+      layouts: Seq[Seq[Partitioning with Expression]],
+      children: Seq[SparkPlan],
+      minPartitions: Int): Seq[Partitioning with Expression] = {
+    val sizes = children.map { child =>
+      child.logicalLink.map(_.stats.sizeInBytes).filter(_ < 
conf.defaultSizeInBytes)
+    }
+    val useSizes = sizes.forall(_.isDefined)
+    val benefits = mutable.HashMap.empty[Partitioning with Expression, 
CandidateBenefit]
+    layouts.zip(sizes).foreach { case (reported, size) =>
+      reported.filter(_.numPartitions >= minPartitions).distinct.foreach { 
target =>

Review Comment:
   Fixed in `eb1baa2378b`: the target count is bounded above by 
`ceil(max(replaced, initial) * maxTargetPartitionNumFactor)` (default 1.0). A 
`REPARTITION(10000)` branch is rejected and the union exchange is kept.



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