Skip to content
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

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import org.apache.spark.sql.types._
1.5
""",
since = "1.0.0")
case class Average(child: Expression) extends DeclarativeAggregate with ImplicitCastInputTypes {
case class Average(child: Expression) extends DeclarativeAggregate with ImplicitCastInputTypes
with OrderIrrelevantAggs {

override def prettyName: String = "avg"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import org.apache.spark.sql.types._
* @param child to compute central moments of.
*/
abstract class CentralMomentAgg(child: Expression)
extends DeclarativeAggregate with ImplicitCastInputTypes {
extends DeclarativeAggregate with ImplicitCastInputTypes with OrderIrrelevantAggs {

/**
* The central moment order to be computed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import org.apache.spark.sql.types._
""",
since = "1.0.0")
// scalastyle:on line.size.limit
case class Count(children: Seq[Expression]) extends DeclarativeAggregate {
case class Count(children: Seq[Expression]) extends DeclarativeAggregate with OrderIrrelevantAggs {
override def nullable: Boolean = false

// Return data type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.apache.spark.sql.types._
50
""",
since = "1.0.0")
case class Max(child: Expression) extends DeclarativeAggregate {
case class Max(child: Expression) extends DeclarativeAggregate with OrderIrrelevantAggs {

override def children: Seq[Expression] = child :: Nil

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.apache.spark.sql.types._
-1
""",
since = "1.0.0")
case class Min(child: Expression) extends DeclarativeAggregate {
case class Min(child: Expression) extends DeclarativeAggregate with OrderIrrelevantAggs {

override def children: Seq[Expression] = child :: Nil

Expand Down
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 {
Copy link
Member

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?

Copy link
Contributor Author

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 used OrderIrrelevantAggs as a mixin trait, and I only apply this trait to min, max and several other agg funciton, not on ScalaUDAF. Hope I understand you correctly.

Copy link
Contributor Author

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 extends AggregateFunction.

Copy link
Member

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

Copy link
Contributor Author

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.

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import org.apache.spark.sql.types._
NULL
""",
since = "1.0.0")
case class Sum(child: Expression) extends DeclarativeAggregate with ImplicitCastInputTypes {
case class Sum(child: Expression) extends DeclarativeAggregate with ImplicitCastInputTypes
with OrderIrrelevantAggs {

override def children: Seq[Expression] = child :: Nil

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename it to EliminateSorts? Some sorts are not redundant but we remove them as well according to the SQL semantic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another rule named EliminateSorts. Can we merge it together?

Copy link
Contributor

Choose a reason for hiding this comment

The 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, _, _, _) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we make sure the join condition is deterministic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This is not related to this pr though...) nit: private?

Expand All @@ -987,6 +993,16 @@ object RemoveRedundantSorts extends Rule[LogicalPlan] {
case f: Filter => f.condition.deterministic
case _ => false
}

def isOrderIrrelevantAggs(aggs: Seq[NamedExpression]): Boolean = {
Copy link
Member

Choose a reason for hiding this comment

The 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])
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ class RemoveRedundantSortsSuite extends PlanTest {

object Optimize extends RuleExecutor[LogicalPlan] {
val batches =
Batch("Limit PushDown", Once,
LimitPushDown) ::
Batch("Remove Redundant Sorts", Once,
RemoveRedundantSorts) ::
Batch("Collapse Project", Once,
CollapseProject) :: 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 redundant order by") {
val orderedPlan = testRelation.select('a, 'b).orderBy('a.asc, 'b.desc_nullsFirst)
Expand Down Expand Up @@ -96,7 +104,7 @@ class RemoveRedundantSortsSuite extends PlanTest {
}

test("sort should not be removed when there is a node which doesn't guarantee any order") {
val orderedPlan = testRelation.select('a, 'b).orderBy('a.asc)
val orderedPlan = testRelation.select('a, 'b)
val groupedAndResorted = orderedPlan.groupBy('a)(sum('a)).orderBy('a.asc)
val optimized = Optimize.execute(groupedAndResorted.analyze)
val correctAnswer = groupedAndResorted.analyze
Expand Down Expand Up @@ -135,4 +143,96 @@ class RemoveRedundantSortsSuite extends PlanTest {
.select(('b + 1).as('c)).orderBy('c.asc).analyze
comparePlans(optimizedThrice, correctAnswerThrice)
}

test("remove orderBy in groupBy clause 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 clause 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 clause 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 clause 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 clause") {
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 clause") {
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 clause") {
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 clause 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 clause 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1080,9 +1080,8 @@ class SubquerySuite extends QueryTest with SharedSparkSession {
| HAVING max(c2) > 0
| ORDER BY c1)
""".stripMargin
// The rule to remove redundant sorts is not able to remove the inner sort under
// an Aggregate operator. We only remove the top level sort.
assert(getNumSortsInQuery(query6) == 1)

assert(getNumSortsInQuery(query6) == 0)

// Cases when sort is not removed from the plan
// Limit on top of sort
Expand Down