-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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
[SPARK-29343][SQL] Eliminate sorts without limit in the subquery of Join/Aggregation #26011
Changes from 7 commits
75b43f5
e29b323
25a3bb8
d21c683
d2328a0
a9e9be9
425f76d
9072db7
8082324
f2d9ec1
4eccd2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.expressions.aggregate | ||
|
||
/** | ||
* An [[OrderIrrelevantAggs]] trait denotes those aggregate functions that its result | ||
* has nothing to do with the order of input data. | ||
* For example, [[Sum]] is [[OrderIrrelevantAggs]] while [[First]] is not. | ||
*/ | ||
trait OrderIrrelevantAggs extends AggregateFunction { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -967,12 +967,18 @@ object EliminateSorts extends Rule[LogicalPlan] { | |
* Removes redundant Sort operation. This can happen: | ||
* 1) if the child is already sorted | ||
* 2) if there is another Sort operator separated by 0...n Project/Filter operators | ||
* 3) if the Sort operator is within Join and without Limit | ||
* 4) if the Sort operator is within GroupBy and the aggregate function is order irrelevant | ||
*/ | ||
object RemoveRedundantSorts extends Rule[LogicalPlan] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rename it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is another rule named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea let's merge them. |
||
def apply(plan: LogicalPlan): LogicalPlan = plan transformDown { | ||
case Sort(orders, true, child) if SortOrder.orderingSatisfies(child.outputOrdering, orders) => | ||
child | ||
case s @ Sort(_, _, child) => s.copy(child = recursiveRemoveSort(child)) | ||
case j @ Join(originLeft, originRight, _, _, _) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we make sure the join condition is deterministic? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
j.copy(left = recursiveRemoveSort(originLeft), right = recursiveRemoveSort(originRight)) | ||
case g @ Aggregate(_, aggs, originChild) if isOrderIrrelevantAggs(aggs) => | ||
g.copy(child = recursiveRemoveSort(originChild)) | ||
} | ||
|
||
def recursiveRemoveSort(plan: LogicalPlan): LogicalPlan = plan match { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (This is not related to this pr though...) nit: private? |
||
|
@@ -987,6 +993,16 @@ object RemoveRedundantSorts extends Rule[LogicalPlan] { | |
case f: Filter => f.condition.deterministic | ||
case _ => false | ||
} | ||
|
||
def isOrderIrrelevantAggs(aggs: Seq[NamedExpression]): Boolean = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: private? |
||
val aggExpressions = aggs.flatMap { e => | ||
e.collect { | ||
case ae: AggregateExpression => ae | ||
} | ||
} | ||
|
||
aggExpressions.forall(_.aggregateFunction.isInstanceOf[OrderIrrelevantAggs]) | ||
} | ||
} | ||
|
||
/** | ||
|
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.
I think this trait approach doesn't work for
ScalaUDAF
. Is this expected?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.
Yes, it's not suitable for
ScalaUDAF
. In fact, I usedOrderIrrelevantAggs
as a mixin trait, and I only apply this trait tomin
,max
and several other agg funciton, not onScalaUDAF
. Hope I understand you correctly.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.
Maybe
OrderIrrelevantAggs
don't need to extendsAggregateFunction
.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.
If there is not too many agg functions for this optimization, can we list up them inside the rule like the others? e.g., https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnAliasing.scala#L162
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.
OK, I think your suggestion is better.