Skip to content

Commit

Permalink
remove Sort operator separated by 0...n Project/Filter operators
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaido91 committed Apr 16, 2018
1 parent 6ba4186 commit ff7d412
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -738,13 +738,20 @@ object EliminateSorts extends Rule[LogicalPlan] {
/**
* Removes redundant Sort operation. This can happen:
* 1) if the child is already sorted
* 2) if the next operator is a Sort itself
* 2) if the there is another Sort operator separated by 0...n Project/Filter operators
*/
object RemoveRedundantSorts extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case Sort(orders, true, child) if SortOrder.orderingSatisfies(child.outputOrdering, orders) =>
child
case s @ Sort(_, _, Sort(_, _, child)) => s.copy(child = child)
case s @ Sort(_, _, child) => s.copy(child = recursiveRemoveSort(child))
}

def recursiveRemoveSort(plan: LogicalPlan): LogicalPlan = plan match {
case Project(fields, child) => Project(fields, recursiveRemoveSort(child))
case Filter(condition, child) => Filter(condition, recursiveRemoveSort(child))
case Sort(_, _, child) => recursiveRemoveSort(child)
case _ => plan
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,24 @@ class RemoveRedundantSortsSuite extends PlanTest {
val correctAnswer = testRelation.orderBy('b.desc).analyze
comparePlans(optimized, correctAnswer)
}

test("remove sorts separated by Filter/Project operators") {
val orderedTwiceWithProject = testRelation.orderBy('a.asc).select('b).orderBy('b.desc)
val optimizedWithProject = Optimize.execute(orderedTwiceWithProject.analyze)
val correctAnswerWithProject = testRelation.select('b).orderBy('b.desc).analyze
comparePlans(optimizedWithProject, correctAnswerWithProject)

val orderedTwiceWithFilter =
testRelation.orderBy('a.asc).where('b > Literal(0)).orderBy('b.desc)
val optimizedWithFilter = Optimize.execute(orderedTwiceWithFilter.analyze)
val correctAnswerWithFilter = testRelation.where('b > Literal(0)).orderBy('b.desc).analyze
comparePlans(optimizedWithFilter, correctAnswerWithFilter)

val orderedTwiceWithBoth =
testRelation.orderBy('a.asc).select('b).where('b > Literal(0)).orderBy('b.desc)
val optimizedWithBoth = Optimize.execute(orderedTwiceWithBoth.analyze)
val correctAnswerWithBoth =
testRelation.select('b).where('b > Literal(0)).orderBy('b.desc).analyze
comparePlans(optimizedWithBoth, correctAnswerWithBoth)
}
}

0 comments on commit ff7d412

Please sign in to comment.