-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-36194][SQL] Add a logical plan visitor to propagate the distinct attributes #35779
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
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e5af37c
Remove the aggregation from left semi/anti join if the same aggregati…
wangyum b703ae2
Add more test
wangyum 6c0bb58
grouping -> groupingExps
wangyum fc52571
Add DistinctAttributesVisitor
wangyum 8be677b
Fix test name
wangyum 16e55c1
Improve DistinctAttributesVisitor
wangyum e9f28d8
Fix test.
wangyum e9bf2fe
DistinctKeyVisitor
wangyum 200e042
Address comments
wangyum 0621360
Fix scala 2.13
wangyum e12fd14
Address comments
wangyum 33db6df
Address all comments
wangyum 19f7d72
Address all comments
wangyum a7ce14d
fix
wangyum 48a9a79
Merge remote-tracking branch 'upstream/master' into SPARK-36194-1234567
wangyum e662655
Remove a case
wangyum 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
140 changes: 140 additions & 0 deletions
140
...alyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DistinctKeyVisitor.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,140 @@ | ||
/* | ||
* 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.logical | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Alias, Expression, ExpressionSet, NamedExpression} | ||
import org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys | ||
import org.apache.spark.sql.catalyst.plans.{Inner, LeftOuter, LeftSemiOrAnti, RightOuter} | ||
|
||
/** | ||
* A visitor pattern for traversing a [[LogicalPlan]] tree and propagate the distinct attributes. | ||
*/ | ||
object DistinctKeyVisitor extends LogicalPlanVisitor[Set[ExpressionSet]] { | ||
|
||
private def projectDistinctKeys( | ||
keys: Set[ExpressionSet], projectList: Seq[NamedExpression]): Set[ExpressionSet] = { | ||
val outputSet = ExpressionSet(projectList.map(_.toAttribute)) | ||
val aliases = projectList.filter(_.isInstanceOf[Alias]) | ||
if (aliases.isEmpty) { | ||
keys.filter(_.subsetOf(outputSet)) | ||
} else { | ||
val aliasedDistinctKeys = keys.map { expressionSet => | ||
expressionSet.map { expression => | ||
expression transform { | ||
case expr: Expression => | ||
// TODO: Expand distinctKeys for redundant aliases on the same expression | ||
aliases | ||
.collectFirst { case a: Alias if a.child.semanticEquals(expr) => a.toAttribute } | ||
.getOrElse(expr) | ||
} | ||
} | ||
} | ||
aliasedDistinctKeys.collect { | ||
case es: ExpressionSet if es.subsetOf(outputSet) => ExpressionSet(es) | ||
} ++ keys.filter(_.subsetOf(outputSet)) | ||
}.filter(_.nonEmpty) | ||
} | ||
|
||
override def default(p: LogicalPlan): Set[ExpressionSet] = Set.empty[ExpressionSet] | ||
|
||
override def visitAggregate(p: Aggregate): Set[ExpressionSet] = { | ||
val groupingExps = ExpressionSet(p.groupingExpressions) // handle group by a, a | ||
projectDistinctKeys(Set(groupingExps), p.aggregateExpressions) | ||
} | ||
|
||
override def visitDistinct(p: Distinct): Set[ExpressionSet] = Set(ExpressionSet(p.output)) | ||
|
||
override def visitExcept(p: Except): Set[ExpressionSet] = | ||
if (!p.isAll) Set(ExpressionSet(p.output)) else default(p) | ||
|
||
override def visitExpand(p: Expand): Set[ExpressionSet] = default(p) | ||
|
||
override def visitFilter(p: Filter): Set[ExpressionSet] = p.child.distinctKeys | ||
|
||
override def visitGenerate(p: Generate): Set[ExpressionSet] = default(p) | ||
|
||
override def visitGlobalLimit(p: GlobalLimit): Set[ExpressionSet] = { | ||
p.maxRows match { | ||
case Some(value) if value <= 1 => Set(ExpressionSet(p.output)) | ||
case _ => p.child.distinctKeys | ||
} | ||
} | ||
|
||
override def visitIntersect(p: Intersect): Set[ExpressionSet] = { | ||
if (!p.isAll) Set(ExpressionSet(p.output)) else default(p) | ||
} | ||
|
||
override def visitJoin(p: Join): Set[ExpressionSet] = { | ||
p match { | ||
case Join(_, _, LeftSemiOrAnti(_), _, _) => | ||
p.left.distinctKeys | ||
case ExtractEquiJoinKeys(joinType, leftKeys, rightKeys, _, _, left, right, _) | ||
if left.distinctKeys.nonEmpty || right.distinctKeys.nonEmpty => | ||
val rightJoinKeySet = ExpressionSet(rightKeys) | ||
val leftJoinKeySet = ExpressionSet(leftKeys) | ||
joinType match { | ||
case Inner if left.distinctKeys.exists(_.subsetOf(leftJoinKeySet)) && | ||
right.distinctKeys.exists(_.subsetOf(rightJoinKeySet)) => | ||
left.distinctKeys ++ right.distinctKeys | ||
case Inner | LeftOuter if right.distinctKeys.exists(_.subsetOf(rightJoinKeySet)) => | ||
p.left.distinctKeys | ||
case Inner | RightOuter if left.distinctKeys.exists(_.subsetOf(leftJoinKeySet)) => | ||
p.right.distinctKeys | ||
case _ => | ||
default(p) | ||
} | ||
case _ => default(p) | ||
} | ||
} | ||
|
||
override def visitLocalLimit(p: LocalLimit): Set[ExpressionSet] = p.child.distinctKeys | ||
|
||
override def visitPivot(p: Pivot): Set[ExpressionSet] = default(p) | ||
|
||
override def visitProject(p: Project): Set[ExpressionSet] = { | ||
if (p.child.distinctKeys.nonEmpty) { | ||
projectDistinctKeys(p.child.distinctKeys, p.projectList) | ||
} else { | ||
default(p) | ||
} | ||
} | ||
|
||
override def visitRepartition(p: Repartition): Set[ExpressionSet] = p.child.distinctKeys | ||
|
||
override def visitRepartitionByExpr(p: RepartitionByExpression): Set[ExpressionSet] = | ||
p.child.distinctKeys | ||
|
||
override def visitSample(p: Sample): Set[ExpressionSet] = { | ||
if (!p.withReplacement) p.child.distinctKeys else default(p) | ||
} | ||
|
||
override def visitScriptTransform(p: ScriptTransformation): Set[ExpressionSet] = default(p) | ||
|
||
override def visitUnion(p: Union): Set[ExpressionSet] = default(p) | ||
|
||
override def visitWindow(p: Window): Set[ExpressionSet] = p.child.distinctKeys | ||
|
||
override def visitTail(p: Tail): Set[ExpressionSet] = p.child.distinctKeys | ||
|
||
override def visitSort(p: Sort): Set[ExpressionSet] = p.child.distinctKeys | ||
|
||
override def visitRebalancePartitions(p: RebalancePartitions): Set[ExpressionSet] = | ||
p.child.distinctKeys | ||
|
||
override def visitWithCTE(p: WithCTE): Set[ExpressionSet] = p.plan.distinctKeys | ||
} |
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
34 changes: 34 additions & 0 deletions
34
.../src/main/scala/org/apache/spark/sql/catalyst/plans/logical/LogicalPlanDistinctKeys.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,34 @@ | ||
/* | ||
* 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.logical | ||
|
||
import org.apache.spark.sql.catalyst.expressions.ExpressionSet | ||
import org.apache.spark.sql.internal.SQLConf.PROPAGATE_DISTINCT_KEYS_ENABLED | ||
|
||
/** | ||
* A trait to add distinct attributes to [[LogicalPlan]]. For example: | ||
* {{{ | ||
* SELECT a, b, SUM(c) FROM Tab1 GROUP BY a, b | ||
* // returns a, b | ||
* }}} | ||
*/ | ||
trait LogicalPlanDistinctKeys { self: LogicalPlan => | ||
lazy val distinctKeys: Set[ExpressionSet] = { | ||
if (conf.getConf(PROPAGATE_DISTINCT_KEYS_ENABLED)) DistinctKeyVisitor.visit(self) else Set.empty | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.