ulysses-you commented on code in PR #60553:
URL: https://github.com/apache/spark/pull/60553#discussion_r4611516640
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/PushDownShuffleExchangeThroughUnion.scala:
##########
@@ -142,47 +173,66 @@ case class PushDownShuffleExchangeThroughUnion(
lazy val sharedExchanges: Set[SparkPlan] =
if (conf.exchangeReuseEnabled) AQEUtils.duplicatedShuffleForms(plan)
else Set.empty
- // Whether every adopted target is the layout of the exchange this rewrite
removes. The retry
- // cannot help while this holds.
- var usedRemovedLayout = true
- // Whether an earlier site in this pass has rewritten the plan. Until then
its layouts are the
- // ones preparation produced and can be read without checking.
- var hasPushed = false
- def rewrite(preferRemovedLayout: Boolean): SparkPlan = {
- hasPushed = false
- plan.transformUp {
+ /**
+ * One pass over the plan. `sites` restricts the rewrite to the operators
it names, by their
+ * index in this pass's traversal; `None` rewrites every site. A site
named by
+ * `removedLayoutSites` may adopt only the layout of the exchange it
removes. Returns the
+ * rewritten plan, the indices of the sites the pass saw, including one it
could not act on
+ * because an earlier change left its child invalid, and whether every
target it adopted is
+ * the layout of the exchange it removes.
+ */
+ def rewrite(
+ removedLayoutSites: Set[Int],
+ sites: Option[Set[Int]]): (SparkPlan, Set[Int], Boolean) = {
+ // Whether an earlier site in this pass has rewritten the plan. Until
then its layouts are
+ // the ones preparation produced and can be read without checking.
+ var hasPushed = false
+ // Whether every target this pass adopts is the layout of the exchange
it removes.
+ var usedRemovedLayout = true
+ var index = -1
+ val candidateSites = mutable.Set.empty[Int]
+ val rewritten = plan.transformUp {
case join: ShuffledJoin if !allowJoinInputs => join
case operator: SparkPlan =>
- val matched = operator.children.map { child =>
- findPushableShuffle(child, lookThrough, policy)
- }
+ index += 1
+ val matched =
+ if (sites.forall(_.contains(index))) {
+ operator.children.map(child => findPushableShuffle(child,
lookThrough, policy))
+ } else {
+ Nil
+ }
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 if (hasPushed && operator.children.exists(child =>
- !ValidateRequirements.validate(child))) {
- // An earlier site in this pass can leave a child invalid; its
layout cannot be read.
- operator
} else {
- val requirements =
required.map(_.asInstanceOf[ClusteredDistribution])
- val (candidate, operatorUsedRemovedLayout) =
pushDownThroughUnion(
- operator,
- matched.zip(requirements),
- sharedExchangesFunc = sharedExchanges,
- policy = policy.copy(
- allowNonCoalescible =
- policy.allowNonCoalescible || !allExchangeInputs(operator),
- preferRemovedLayout = preferRemovedLayout))
- usedRemovedLayout = usedRemovedLayout &&
operatorUsedRemovedLayout
- hasPushed = hasPushed || (candidate ne operator)
- candidate
+ candidateSites += index
Review Comment:
Fixed in 38fa3834c67: 'the replay visits a site the first pass skipped'
builds that shape. The inner site's push breaks the join's pairing, the first
pass skips the outer site, and the replay adopts the outer site on hash(a, 3),
a layout other than the one it removes; the unrelated site on hash(r, 3) keeps
the whole-plan retry from running. Recording only the sites a pass acted on
fails the test.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/PushDownShuffleExchangeThroughUnion.scala:
##########
@@ -475,64 +582,154 @@ case class PushDownShuffleExchangeThroughUnion(
}
prepared.targets.view
- .flatMap { target =>
- rebuild(pushable.shuffle.child, target).map(child => (target, child))
+ .flatMap { case (target, offeredBy) =>
+ rebuild(pushable.shuffle.child, target, offeredBy).map(child =>
(target, child))
}
.map { case (target, child) =>
(target, PartitioningOrigin.inherit(pushable.shuffle, child))
}
- .find { case (_, child) =>
+ // Keep a rebuilt child only if it answers the required distribution as
rebuilt and pairs with
+ // the siblings. A keyed target's admission as a shuffle template was
settled with the
+ // candidates; what remains is the rule's own subset permission, which
reads the rebuilt
+ // layout's specs.
+ .filter { case (target, 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
- }))
+ // A keyed target was admitted as a template already; any other
target asks its spec.
+ (target.isInstanceOf[KeyedPartitioning] ||
spec.canCreatePartitioning) &&
+ (policy.allowSubsetTargets || (spec match {
+ case hash: HashShuffleSpec =>
+
required.areAllClusterKeysMatched(hash.partitioning.expressions)
+ case keyed: KeyedShuffleSpec =>
+ required.allClusterKeysAmong(keyed.partitioning.expressions)
Review Comment:
Fixed in 38fa3834c67: the allowPartitionKeysSubsetOfClusterKeys doc now
states that a keyed transform target needs the permission even when it covers
every clustering key, and that a keyed target naming part of the clustering
keys also needs requireAllClusterKeysForCoPartition off. The hash-target
wording is unchanged.
--
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]