-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-40086][SPARK-42049][SQL] Improve AliasAwareOutputPartitioning …
…and AliasAwareQueryOutputOrdering to take all aliases into account ### What changes were proposed in this pull request? Currently `AliasAwareOutputPartitioning` and `AliasAwareQueryOutputOrdering` takes only the last alias by aliased expressions into account. We could avoid some extra shuffles and sorts with better alias handling. ### Why are the changes needed? Performance improvement and this also fix the issue in #39475. ### Does this PR introduce _any_ user-facing change? Yes, this PR fixes the issue in #39475. ### How was this patch tested? Added new UT. Closes #37525 from peter-toth/SPARK-40086-fix-aliasawareoutputexpression. Lead-authored-by: Peter Toth <[email protected]> Co-authored-by: ulysses-you <[email protected]> Signed-off-by: Wenchen Fan <[email protected]> (cherry picked from commit 6341b06) Signed-off-by: Wenchen Fan <[email protected]>
- Loading branch information
Showing
19 changed files
with
464 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...alyst/src/main/scala/org/apache/spark/sql/catalyst/plans/AliasAwareOutputExpression.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* 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.catalyst.plans | ||
|
||
import scala.collection.mutable | ||
|
||
import org.apache.spark.sql.catalyst.SQLConfHelper | ||
import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeSet, Empty2Null, Expression, NamedExpression, SortOrder} | ||
import org.apache.spark.sql.internal.SQLConf | ||
|
||
/** | ||
* A trait that provides functionality to handle aliases in the `outputExpressions`. | ||
*/ | ||
trait AliasAwareOutputExpression extends SQLConfHelper { | ||
protected val aliasCandidateLimit = conf.getConf(SQLConf.EXPRESSION_PROJECTION_CANDIDATE_LIMIT) | ||
protected def outputExpressions: Seq[NamedExpression] | ||
/** | ||
* This method can be used to strip expression which does not affect the result, for example: | ||
* strip the expression which is ordering agnostic for output ordering. | ||
*/ | ||
protected def strip(expr: Expression): Expression = expr | ||
|
||
// Build an `Expression` -> `Attribute` alias map. | ||
// There can be multiple alias defined for the same expressions but it doesn't make sense to store | ||
// more than `aliasCandidateLimit` attributes for an expression. In those cases the old logic | ||
// handled only the last alias so we need to make sure that we give precedence to that. | ||
// If the `outputExpressions` contain simple attributes we need to add those too to the map. | ||
private lazy val aliasMap = { | ||
val aliases = mutable.Map[Expression, mutable.ArrayBuffer[Attribute]]() | ||
outputExpressions.reverse.foreach { | ||
case a @ Alias(child, _) => | ||
val buffer = aliases.getOrElseUpdate(strip(child).canonicalized, mutable.ArrayBuffer.empty) | ||
if (buffer.size < aliasCandidateLimit) { | ||
buffer += a.toAttribute | ||
} | ||
case _ => | ||
} | ||
outputExpressions.foreach { | ||
case a: Attribute if aliases.contains(a.canonicalized) => | ||
val buffer = aliases(a.canonicalized) | ||
if (buffer.size < aliasCandidateLimit) { | ||
buffer += a | ||
} | ||
case _ => | ||
} | ||
aliases | ||
} | ||
|
||
protected def hasAlias: Boolean = aliasMap.nonEmpty | ||
|
||
/** | ||
* Return a stream of expressions in which the original expression is projected with `aliasMap`. | ||
*/ | ||
protected def projectExpression(expr: Expression): Stream[Expression] = { | ||
val outputSet = AttributeSet(outputExpressions.map(_.toAttribute)) | ||
expr.multiTransformDown { | ||
// Mapping with aliases | ||
case e: Expression if aliasMap.contains(e.canonicalized) => | ||
aliasMap(e.canonicalized).toSeq ++ (if (e.containsChild.nonEmpty) Seq(e) else Seq.empty) | ||
|
||
// Prune if we encounter an attribute that we can't map and it is not in output set. | ||
// This prune will go up to the closest `multiTransformDown()` call and returns `Stream.empty` | ||
// there. | ||
case a: Attribute if !outputSet.contains(a) => Seq.empty | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A trait that handles aliases in the `orderingExpressions` to produce `outputOrdering` that | ||
* satisfies ordering requirements. | ||
*/ | ||
trait AliasAwareQueryOutputOrdering[T <: QueryPlan[T]] | ||
extends AliasAwareOutputExpression { self: QueryPlan[T] => | ||
protected def orderingExpressions: Seq[SortOrder] | ||
|
||
override protected def strip(expr: Expression): Expression = expr match { | ||
case e: Empty2Null => strip(e.child) | ||
case _ => expr | ||
} | ||
|
||
override final def outputOrdering: Seq[SortOrder] = { | ||
if (hasAlias) { | ||
// Take the first `SortOrder`s only until they can be projected. | ||
// E.g. we have child ordering `Seq(SortOrder(a), SortOrder(b))` then | ||
// if only `a AS x` can be projected then we can return Seq(SortOrder(x))` | ||
// but if only `b AS y` can be projected we can't return `Seq(SortOrder(y))`. | ||
orderingExpressions.iterator.map { sortOrder => | ||
val orderingSet = mutable.Set.empty[Expression] | ||
val sameOrderings = sortOrder.children.toStream | ||
.flatMap(projectExpression) | ||
.filter(e => orderingSet.add(e.canonicalized)) | ||
.take(aliasCandidateLimit) | ||
if (sameOrderings.nonEmpty) { | ||
Some(sortOrder.copy(child = sameOrderings.head, | ||
sameOrderExpressions = sameOrderings.tail)) | ||
} else { | ||
None | ||
} | ||
}.takeWhile(_.isDefined).flatten.toSeq | ||
} else { | ||
orderingExpressions | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.