Skip to content

Commit 63ec35d

Browse files
update
1 parent bcbd9fa commit 63ec35d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,14 +1052,18 @@ object CombineFilters extends Rule[LogicalPlan] with PredicateHelper {
10521052
* function is order irrelevant
10531053
*/
10541054
object EliminateSorts extends Rule[LogicalPlan] {
1055-
// transformUp is needed here to ensure idempotency of this rule when removing consecutive
1056-
// local sorts.
1057-
def apply(plan: LogicalPlan): LogicalPlan = plan transformUp {
1055+
def apply(plan: LogicalPlan): LogicalPlan = plan transform applyLocally
1056+
1057+
val applyLocally: PartialFunction[LogicalPlan, LogicalPlan] = {
10581058
case s @ Sort(orders, _, child) if orders.isEmpty || orders.exists(_.child.foldable) =>
10591059
val newOrders = orders.filterNot(_.child.foldable)
1060-
if (newOrders.isEmpty) child else s.copy(order = newOrders)
1060+
if (newOrders.isEmpty) {
1061+
applyLocally.lift(child).getOrElse(child)
1062+
} else {
1063+
s.copy(order = newOrders)
1064+
}
10611065
case Sort(orders, false, child) if SortOrder.orderingSatisfies(child.outputOrdering, orders) =>
1062-
child
1066+
applyLocally.lift(child).getOrElse(child)
10631067
case s @ Sort(_, _, child) => s.copy(child = recursiveRemoveSort(child))
10641068
case j @ Join(originLeft, originRight, _, cond, _) if cond.forall(_.deterministic) =>
10651069
j.copy(left = recursiveRemoveSort(originLeft), right = recursiveRemoveSort(originRight))

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/EliminateSortsSuite.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ class EliminateSortsSuite extends PlanTest {
9797
comparePlans(optimized, correctAnswer)
9898
}
9999

100-
test("SPARK-33183: remove redundant sortBy") {
100+
test("SPARK-33183: remove consecutive no-op sorts") {
101+
val plan = testRelation.orderBy().orderBy().orderBy()
102+
val optimized = Optimize.execute(plan.analyze)
103+
val correctAnswer = testRelation.analyze
104+
comparePlans(optimized, correctAnswer)
105+
}
106+
107+
test("SPARK-33183: remove redundant sort by") {
101108
val orderedPlan = testRelation.select('a, 'b).orderBy('a.asc, 'b.desc_nullsFirst)
102109
val unnecessaryReordered = orderedPlan.limit(2).select('a).sortBy('a.asc, 'b.desc_nullsFirst)
103110
val optimized = Optimize.execute(unnecessaryReordered.analyze)

0 commit comments

Comments
 (0)