-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-42049][SQL] Improve AliasAwareOutputExpression #39556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
125 changes: 125 additions & 0 deletions
125
...alyst/src/main/scala/org/apache/spark/sql/catalyst/plans/AliasAwareOutputExpression.scala
This file contains hidden or 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,125 @@ | ||
| /* | ||
| * 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 scala.collection.mutable.ArrayBuffer | ||
|
|
||
| import org.apache.spark.sql.catalyst.SQLConfHelper | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeSet, Empty2Null, Expression, ExpressionSet, NamedExpression, SortOrder} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| /** | ||
| * A trait that provides functionality to handle aliases in the `outputExpressions`. | ||
| */ | ||
| trait AliasAwareOutputExpression extends SQLConfHelper { | ||
| private val aliasCandidateLimit = conf.getConf(SQLConf.EXPRESSION_PROJECTION_CANDIDATE_LIMIT) | ||
| private var _hasAlias = false | ||
| protected def outputExpressions: Seq[NamedExpression] | ||
|
|
||
| /** | ||
| * This method is 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 | ||
|
|
||
| protected lazy val aliasMap: Map[Expression, ArrayBuffer[Attribute]] = { | ||
| if (aliasCandidateLimit < 1) { | ||
| Map.empty | ||
| } else { | ||
| val outputExpressionSet = AttributeSet(outputExpressions.map(_.toAttribute)) | ||
| val exprWithAliasMap = new mutable.HashMap[Expression, ArrayBuffer[Attribute]]() | ||
|
|
||
| def updateAttrWithAliasMap(key: Expression, target: Attribute): Unit = { | ||
| val aliasArray = exprWithAliasMap.getOrElseUpdate( | ||
| strip(key).canonicalized, new ArrayBuffer[Attribute]()) | ||
| // pre-filter if the number of alias exceed candidate limit | ||
| if (aliasArray.size < aliasCandidateLimit) { | ||
| aliasArray.append(target) | ||
| } | ||
| } | ||
|
|
||
| outputExpressions.foreach { | ||
| case a @ Alias(child, _) => | ||
| _hasAlias = true | ||
| updateAttrWithAliasMap(child, a.toAttribute) | ||
| case a: Attribute if outputExpressionSet.contains(a) => | ||
| updateAttrWithAliasMap(a, a) | ||
| case _ => | ||
| } | ||
| exprWithAliasMap.toMap | ||
| } | ||
| } | ||
|
|
||
| protected def hasAlias: Boolean = { | ||
| aliasMap | ||
| _hasAlias | ||
| } | ||
|
|
||
| /** | ||
| * Return a set of Expression which normalize the original expression to the aliased. | ||
| */ | ||
| protected def normalizeExpression(expr: Expression): Seq[Expression] = { | ||
| val normalizedCandidates = expr.multiTransformDown { | ||
| case e: Expression if aliasMap.contains(e.canonicalized) => | ||
| val candidates = aliasMap(e.canonicalized) | ||
| (candidates :+ e).toStream | ||
| }.take(aliasCandidateLimit) | ||
|
|
||
| if (normalizedCandidates.isEmpty) { | ||
| expr :: Nil | ||
| } else { | ||
| normalizedCandidates.toSeq | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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) { | ||
| orderingExpressions.map { sortOrder => | ||
| val normalized = normalizeExpression(sortOrder) | ||
| assert(normalized.forall(_.isInstanceOf[SortOrder])) | ||
| val pruned = ExpressionSet(normalized.flatMap { | ||
| case s: SortOrder => s.children.filter(_.references.subsetOf(outputSet)) | ||
| }) | ||
| if (pruned.isEmpty) { | ||
| sortOrder | ||
| } else { | ||
| // All expressions after pruned are semantics equality, so just use head to build a new | ||
| // SortOrder and use tail as the sameOrderExpressions. | ||
| SortOrder(pruned.head, sortOrder.direction, sortOrder.nullOrdering, pruned.tail.toSeq) | ||
| } | ||
| } | ||
| } else { | ||
| orderingExpressions | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -435,6 +435,15 @@ object SQLConf { | |||||||||||||
| .booleanConf | ||||||||||||||
| .createWithDefault(true) | ||||||||||||||
|
|
||||||||||||||
| val EXPRESSION_PROJECTION_CANDIDATE_LIMIT = | ||||||||||||||
| buildConf("spark.sql.optimizer.expressionProjectionCandidateLimit") | ||||||||||||||
| .doc("The maximum number of the candidate of out put expressions whose alias are replaced." + | ||||||||||||||
| " It can preserve the output partitioning and ordering." + | ||||||||||||||
| " Negative value means disable this optimization.") | ||||||||||||||
|
||||||||||||||
| .doc("The maximum number of the candidate of out put expressions whose alias are replaced." + | |
| " It can preserve the output partitioning and ordering." + | |
| " Negative value means disable this optimization.") | |
| .doc("The maximum number of candidates for output expressions whose aliases are replaced." + | |
| " This can preserve the output partitioning and ordering." + | |
| " A negative value means to disable this optimization.") |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -16,48 +16,41 @@ | |
| */ | ||
| package org.apache.spark.sql.execution | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{Alias, Expression, NamedExpression, SortOrder} | ||
| import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, Partitioning, PartitioningCollection, UnknownPartitioning} | ||
|
|
||
| /** | ||
| * A trait that provides functionality to handle aliases in the `outputExpressions`. | ||
| */ | ||
| trait AliasAwareOutputExpression extends UnaryExecNode { | ||
| protected def outputExpressions: Seq[NamedExpression] | ||
|
|
||
| private lazy val aliasMap = outputExpressions.collect { | ||
| case a @ Alias(child, _) => child.canonicalized -> a.toAttribute | ||
| }.toMap | ||
|
|
||
| protected def hasAlias: Boolean = aliasMap.nonEmpty | ||
|
|
||
| protected def normalizeExpression(exp: Expression): Expression = { | ||
| exp.transformDown { | ||
| case e: Expression => aliasMap.getOrElse(e.canonicalized, e) | ||
| } | ||
| } | ||
| } | ||
| import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionSet} | ||
| import org.apache.spark.sql.catalyst.plans.{AliasAwareOutputExpression, AliasAwareQueryOutputOrdering} | ||
| import org.apache.spark.sql.catalyst.plans.physical.{Partitioning, PartitioningCollection, UnknownPartitioning} | ||
|
|
||
| /** | ||
| * A trait that handles aliases in the `outputExpressions` to produce `outputPartitioning` that | ||
| * satisfies distribution requirements. | ||
| */ | ||
| trait AliasAwareOutputPartitioning extends AliasAwareOutputExpression { | ||
| trait AliasAwareOutputPartitioning extends UnaryExecNode | ||
| with AliasAwareOutputExpression { | ||
|
|
||
| final override def outputPartitioning: Partitioning = { | ||
| val normalizedOutputPartitioning = if (hasAlias) { | ||
| child.outputPartitioning match { | ||
| case e: Expression => | ||
| normalizeExpression(e).asInstanceOf[Partitioning] | ||
| val normalized = normalizeExpression(e) | ||
| if (normalized.isEmpty) { | ||
| UnknownPartitioning(child.outputPartitioning.numPartitions) | ||
| } else if (normalized.size == 1) { | ||
| normalized.head.asInstanceOf[Partitioning] | ||
| } else { | ||
| PartitioningCollection(normalized.asInstanceOf[Seq[Partitioning]]) | ||
| } | ||
| case other => other | ||
| } | ||
| } else { | ||
| child.outputPartitioning | ||
| } | ||
|
|
||
| flattenPartitioning(normalizedOutputPartitioning).filter { | ||
| case hashPartitioning: HashPartitioning => hashPartitioning.references.subsetOf(outputSet) | ||
| val (partitionWithExpr, other) = flattenPartitioning(normalizedOutputPartitioning).filter { | ||
| case e: Expression => e.references.subsetOf(outputSet) | ||
|
||
| case _ => true | ||
| } match { | ||
| }.partition(_.isInstanceOf[Expression]) | ||
| val pruned = ExpressionSet(partitionWithExpr.asInstanceOf[Seq[Expression]]) | ||
| (pruned.toSeq.asInstanceOf[Seq[Partitioning]] ++ other) match { | ||
| case Seq() => UnknownPartitioning(child.outputPartitioning.numPartitions) | ||
| case Seq(singlePartitioning) => singlePartitioning | ||
| case seqWithMultiplePartitionings => PartitioningCollection(seqWithMultiplePartitionings) | ||
|
|
@@ -74,18 +67,4 @@ trait AliasAwareOutputPartitioning extends AliasAwareOutputExpression { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * A trait that handles aliases in the `orderingExpressions` to produce `outputOrdering` that | ||
| * satisfies ordering requirements. | ||
| */ | ||
| trait AliasAwareOutputOrdering extends AliasAwareOutputExpression { | ||
| protected def orderingExpressions: Seq[SortOrder] | ||
|
|
||
| final override def outputOrdering: Seq[SortOrder] = { | ||
| if (hasAlias) { | ||
| orderingExpressions.map(normalizeExpression(_).asInstanceOf[SortOrder]) | ||
| } else { | ||
| orderingExpressions | ||
| } | ||
| } | ||
| } | ||
| trait AliasAwareOutputOrdering extends UnaryExecNode with AliasAwareQueryOutputOrdering[SparkPlan] | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this means we don't do any alias replacement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no it just specifies it's outputExpressions to
AliasAwareOutputExpressionso thatAliasAwareOutputExpressioncan build alias mapThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but
child.outputhas no alias at all, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Projecthas overridden it..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which subclass uses the default implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filter/Limitetc..