Skip to content

Commit 1d6ca1e

Browse files
committed
add test cases + fix typo
1 parent ac03bed commit 1d6ca1e

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ object EliminateSorts extends Rule[LogicalPlan] {
738738
/**
739739
* Removes redundant Sort operation. This can happen:
740740
* 1) if the child is already sorted
741-
* 2) if the there is another Sort operator separated by 0...n Project/Filter operators
741+
* 2) if there is another Sort operator separated by 0...n Project/Filter operators
742742
*/
743743
object RemoveRedundantSorts extends Rule[LogicalPlan] {
744744
def apply(plan: LogicalPlan): LogicalPlan = plan transform {

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717

1818
package org.apache.spark.sql.catalyst.optimizer
1919

20-
import org.apache.spark.sql.catalyst.analysis.{Analyzer, EmptyFunctionRegistry}
21-
import org.apache.spark.sql.catalyst.catalog.{InMemoryCatalog, SessionCatalog}
2220
import org.apache.spark.sql.catalyst.dsl.expressions._
2321
import org.apache.spark.sql.catalyst.dsl.plans._
2422
import org.apache.spark.sql.catalyst.expressions._
2523
import org.apache.spark.sql.catalyst.plans._
2624
import org.apache.spark.sql.catalyst.plans.logical._
2725
import org.apache.spark.sql.catalyst.rules._
28-
import org.apache.spark.sql.internal.SQLConf
29-
import org.apache.spark.sql.internal.SQLConf.{CASE_SENSITIVE, ORDER_BY_ORDINAL}
3026

3127
class RemoveRedundantSortsSuite extends PlanTest {
3228

@@ -72,6 +68,14 @@ class RemoveRedundantSortsSuite extends PlanTest {
7268
comparePlans(optimized, correctAnswer)
7369
}
7470

71+
test("different sorts are not simplified if limit is in between") {
72+
val orderedPlan = testRelation.select('a, 'b).orderBy('b.desc).limit(Literal(10))
73+
.orderBy('a.asc)
74+
val optimized = Optimize.execute(orderedPlan.analyze)
75+
val correctAnswer = orderedPlan.analyze
76+
comparePlans(optimized, correctAnswer)
77+
}
78+
7579
test("range is already sorted") {
7680
val inputPlan = Range(1L, 1000L, 1, 10)
7781
val orderedPlan = inputPlan.orderBy('id.asc)
@@ -124,5 +128,11 @@ class RemoveRedundantSortsSuite extends PlanTest {
124128
val correctAnswerWithBoth =
125129
testRelation.select('b).where('b > Literal(0)).orderBy('b.desc).analyze
126130
comparePlans(optimizedWithBoth, correctAnswerWithBoth)
131+
132+
val orderedThrice = orderedTwiceWithBoth.select(('b + 1).as('c)).orderBy('c.asc)
133+
val optimizedThrice = Optimize.execute(orderedThrice.analyze)
134+
val correctAnswerThrice = testRelation.select('b).where('b > Literal(0))
135+
.select(('b + 1).as('c)).orderBy('c.asc).analyze
136+
comparePlans(optimizedThrice, correctAnswerThrice)
127137
}
128138
}

0 commit comments

Comments
 (0)