-
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 6 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.optimizer | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{NamedExpression, PredicateHelper} | ||
import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, OrderIrrelevantAggs} | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
|
||
/** | ||
* [[Sort]] without [[Limit]] in subquery is useless. For example, | ||
* | ||
* {{{ | ||
* SELECT * FROM | ||
* (SELECT f1 FROM tbl1 ORDER BY f2) temp1 | ||
* JOIN | ||
* (SELECT f3 FROM tbl2) temp2 | ||
* ON temp1.f1 = temp2.f3 | ||
* }}} | ||
* | ||
* is equal to | ||
* | ||
* {{{ | ||
* SELECT * FROM | ||
* (SELECT f1 FROM tbl1) temp1 | ||
* JOIN | ||
* (SELECT f3 FROM tbl2) temp2 | ||
* ON temp1.f1 = temp2.f3" | ||
* }}} | ||
* | ||
* This rule try to remove this kind of [[Sort]] operator. | ||
*/ | ||
object RemoveSortInSubquery extends Rule[LogicalPlan] with PredicateHelper { | ||
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. Should the existing RemoveRedundantSorts handle this as well ? The reason i ask is, i don't see any thing subquery specific in the new rule ? 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. +1 for merging the 2 rules. We can call the merged rule |
||
private def removeTopLevelSort(plan: LogicalPlan): LogicalPlan = { | ||
plan match { | ||
case Sort(_, _, child) => child | ||
case Project(fields, child) => Project(fields, removeTopLevelSort(child)) | ||
case other => other | ||
} | ||
} | ||
|
||
private def isOrderIrrelevantAggs(aggs: Seq[NamedExpression]): Boolean = { | ||
val aggExpressions = aggs.flatMap { e => | ||
e.collect { | ||
case ae: AggregateExpression => ae | ||
} | ||
} | ||
|
||
aggExpressions.forall(_.aggregateFunction.isInstanceOf[OrderIrrelevantAggs]) | ||
} | ||
|
||
def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
case j @ Join(originLeft, originRight, _, _, _) => | ||
j.copy(left = removeTopLevelSort(originLeft), right = removeTopLevelSort(originRight)) | ||
case g @ Aggregate(_, aggs, originChild) if isOrderIrrelevantAggs(aggs) => | ||
g.copy(child = removeTopLevelSort(originChild)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* 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.optimizer | ||
|
||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.dsl.plans._ | ||
import org.apache.spark.sql.catalyst.plans.{LeftOuter, PlanTest, RightOuter} | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
|
||
class RemoveSortInSubquerySuite extends PlanTest { | ||
object Optimize extends RuleExecutor[LogicalPlan] { | ||
val batches = | ||
Batch("Limit PushDown", FixedPoint(10), LimitPushDown) :: | ||
Batch("Remove Redundant Sorts", Once, RemoveSortInSubquery) :: Nil | ||
} | ||
|
||
object PushDownOptimizer extends RuleExecutor[LogicalPlan] { | ||
val batches = | ||
Batch("Limit PushDown", FixedPoint(10), LimitPushDown) :: Nil | ||
} | ||
|
||
val testRelation = LocalRelation('a.int, 'b.int, 'c.int) | ||
val testRelationB = LocalRelation('d.int) | ||
|
||
test("remove orderBy in groupBy subquery with count aggs") { | ||
val projectPlan = testRelation.select('a, 'b) | ||
val unnecessaryOrderByPlan = projectPlan.orderBy('a.asc, 'b.desc) | ||
val groupByPlan = unnecessaryOrderByPlan.groupBy('a)(count(1)) | ||
val optimized = Optimize.execute(groupByPlan.analyze) | ||
val correctAnswer = projectPlan.groupBy('a)(count(1)).analyze | ||
comparePlans(Optimize.execute(optimized), correctAnswer) | ||
} | ||
|
||
test("remove orderBy in groupBy subquery with sum aggs") { | ||
val projectPlan = testRelation.select('a, 'b) | ||
val unnecessaryOrderByPlan = projectPlan.orderBy('a.asc, 'b.desc) | ||
val groupByPlan = unnecessaryOrderByPlan.groupBy('a)(sum('a)) | ||
val optimized = Optimize.execute(groupByPlan.analyze) | ||
val correctAnswer = projectPlan.groupBy('a)(sum('a)).analyze | ||
comparePlans(Optimize.execute(optimized), correctAnswer) | ||
} | ||
|
||
test("remove orderBy in groupBy subquery with first aggs") { | ||
val projectPlan = testRelation.select('a, 'b) | ||
val orderByPlan = projectPlan.orderBy('a.asc, 'b.desc) | ||
val groupByPlan = orderByPlan.groupBy('a)(first('a)) | ||
val optimized = Optimize.execute(groupByPlan.analyze) | ||
val correctAnswer = groupByPlan.analyze | ||
comparePlans(Optimize.execute(optimized), correctAnswer) | ||
} | ||
|
||
test("remove orderBy in groupBy subquery with first and count aggs") { | ||
val projectPlan = testRelation.select('a, 'b) | ||
val orderByPlan = projectPlan.orderBy('a.asc, 'b.desc) | ||
val groupByPlan = orderByPlan.groupBy('a)(first('a), count(1)) | ||
val optimized = Optimize.execute(groupByPlan.analyze) | ||
val correctAnswer = groupByPlan.analyze | ||
comparePlans(Optimize.execute(optimized), correctAnswer) | ||
} | ||
|
||
test("should not remove orderBy with limit in groupBy subquery") { | ||
val projectPlan = testRelation.select('a, 'b) | ||
val orderByPlan = projectPlan.orderBy('a.asc, 'b.desc).limit(10) | ||
val groupByPlan = orderByPlan.groupBy('a)(count(1)) | ||
val optimized = Optimize.execute(groupByPlan.analyze) | ||
val correctAnswer = groupByPlan.analyze | ||
comparePlans(Optimize.execute(optimized), correctAnswer) | ||
} | ||
|
||
test("remove orderBy in join subquery") { | ||
val projectPlan = testRelation.select('a, 'b) | ||
val unnecessaryOrderByPlan = projectPlan.orderBy('a.asc, 'b.desc) | ||
val projectPlanB = testRelationB.select('d) | ||
val joinPlan = unnecessaryOrderByPlan.join(projectPlanB).select('a, 'd) | ||
val optimized = Optimize.execute(joinPlan.analyze) | ||
val correctAnswer = projectPlan.join(projectPlanB).select('a, 'd).analyze | ||
comparePlans(Optimize.execute(optimized), correctAnswer) | ||
} | ||
|
||
test("should not remove orderBy with limit in join subquery") { | ||
val projectPlan = testRelation.select('a, 'b) | ||
val orderByPlan = projectPlan.orderBy('a.asc, 'b.desc).limit(10) | ||
val projectPlanB = testRelationB.select('d) | ||
val joinPlan = orderByPlan.join(projectPlanB).select('a, 'd) | ||
val optimized = Optimize.execute(joinPlan.analyze) | ||
val correctAnswer = joinPlan.analyze | ||
comparePlans(Optimize.execute(optimized), correctAnswer) | ||
} | ||
|
||
test("should not remove orderBy in left join subquery if there is an outer limit") { | ||
val projectPlan = testRelation.select('a, 'b) | ||
val orderByPlan = projectPlan.orderBy('a.asc, 'b.desc) | ||
val projectPlanB = testRelationB.select('d) | ||
val joinPlan = orderByPlan | ||
.join(projectPlanB, LeftOuter) | ||
.limit(10) | ||
val optimized = Optimize.execute(joinPlan.analyze) | ||
val correctAnswer = PushDownOptimizer.execute(joinPlan.analyze) | ||
comparePlans(Optimize.execute(optimized), correctAnswer) | ||
} | ||
|
||
test("remove orderBy in right join subquery event if there is an outer limit") { | ||
val projectPlan = testRelation.select('a, 'b) | ||
val orderByPlan = projectPlan.orderBy('a.asc, 'b.desc) | ||
val projectPlanB = testRelationB.select('d) | ||
val joinPlan = orderByPlan | ||
.join(projectPlanB, RightOuter) | ||
.limit(10) | ||
val optimized = Optimize.execute(joinPlan.analyze) | ||
val noOrderByPlan = projectPlan | ||
.join(projectPlanB, RightOuter) | ||
.limit(10) | ||
val correctAnswer = PushDownOptimizer.execute(noOrderByPlan.analyze) | ||
comparePlans(Optimize.execute(optimized), correctAnswer) | ||
} | ||
} |
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.