17
17
18
18
package org .apache .spark .sql .catalyst .optimizer
19
19
20
- import org .apache .spark .sql .catalyst .analysis .{Analyzer , EmptyFunctionRegistry }
21
- import org .apache .spark .sql .catalyst .catalog .{InMemoryCatalog , SessionCatalog }
22
20
import org .apache .spark .sql .catalyst .dsl .expressions ._
23
21
import org .apache .spark .sql .catalyst .dsl .plans ._
24
22
import org .apache .spark .sql .catalyst .expressions ._
25
23
import org .apache .spark .sql .catalyst .plans ._
26
24
import org .apache .spark .sql .catalyst .plans .logical ._
27
25
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 }
30
26
31
27
class RemoveRedundantSortsSuite extends PlanTest {
32
28
@@ -42,15 +38,15 @@ class RemoveRedundantSortsSuite extends PlanTest {
42
38
43
39
test(" remove redundant order by" ) {
44
40
val orderedPlan = testRelation.select(' a , ' b ).orderBy(' a .asc, ' b .desc_nullsFirst)
45
- val unnecessaryReordered = orderedPlan.select(' a ).orderBy(' a .asc, ' b .desc_nullsFirst)
41
+ val unnecessaryReordered = orderedPlan.limit( 2 ). select(' a ).orderBy(' a .asc, ' b .desc_nullsFirst)
46
42
val optimized = Optimize .execute(unnecessaryReordered.analyze)
47
- val correctAnswer = orderedPlan.select(' a ).analyze
43
+ val correctAnswer = orderedPlan.limit( 2 ). select(' a ).analyze
48
44
comparePlans(Optimize .execute(optimized), correctAnswer)
49
45
}
50
46
51
47
test(" do not remove sort if the order is different" ) {
52
48
val orderedPlan = testRelation.select(' a , ' b ).orderBy(' a .asc, ' b .desc_nullsFirst)
53
- val reorderedDifferently = orderedPlan.select(' a ).orderBy(' a .asc, ' b .desc)
49
+ val reorderedDifferently = orderedPlan.limit( 2 ). select(' a ).orderBy(' a .asc, ' b .desc)
54
50
val optimized = Optimize .execute(reorderedDifferently.analyze)
55
51
val correctAnswer = reorderedDifferently.analyze
56
52
comparePlans(optimized, correctAnswer)
@@ -72,6 +68,14 @@ class RemoveRedundantSortsSuite extends PlanTest {
72
68
comparePlans(optimized, correctAnswer)
73
69
}
74
70
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
+
75
79
test(" range is already sorted" ) {
76
80
val inputPlan = Range (1L , 1000L , 1 , 10 )
77
81
val orderedPlan = inputPlan.orderBy(' id .asc)
@@ -98,4 +102,37 @@ class RemoveRedundantSortsSuite extends PlanTest {
98
102
val correctAnswer = groupedAndResorted.analyze
99
103
comparePlans(optimized, correctAnswer)
100
104
}
105
+
106
+ test(" remove two consecutive sorts" ) {
107
+ val orderedTwice = testRelation.orderBy(' a .asc).orderBy(' b .desc)
108
+ val optimized = Optimize .execute(orderedTwice.analyze)
109
+ val correctAnswer = testRelation.orderBy(' b .desc).analyze
110
+ comparePlans(optimized, correctAnswer)
111
+ }
112
+
113
+ test(" remove sorts separated by Filter/Project operators" ) {
114
+ val orderedTwiceWithProject = testRelation.orderBy(' a .asc).select(' b ).orderBy(' b .desc)
115
+ val optimizedWithProject = Optimize .execute(orderedTwiceWithProject.analyze)
116
+ val correctAnswerWithProject = testRelation.select(' b ).orderBy(' b .desc).analyze
117
+ comparePlans(optimizedWithProject, correctAnswerWithProject)
118
+
119
+ val orderedTwiceWithFilter =
120
+ testRelation.orderBy(' a .asc).where(' b > Literal (0 )).orderBy(' b .desc)
121
+ val optimizedWithFilter = Optimize .execute(orderedTwiceWithFilter.analyze)
122
+ val correctAnswerWithFilter = testRelation.where(' b > Literal (0 )).orderBy(' b .desc).analyze
123
+ comparePlans(optimizedWithFilter, correctAnswerWithFilter)
124
+
125
+ val orderedTwiceWithBoth =
126
+ testRelation.orderBy(' a .asc).select(' b ).where(' b > Literal (0 )).orderBy(' b .desc)
127
+ val optimizedWithBoth = Optimize .execute(orderedTwiceWithBoth.analyze)
128
+ val correctAnswerWithBoth =
129
+ testRelation.select(' b ).where(' b > Literal (0 )).orderBy(' b .desc).analyze
130
+ 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)
137
+ }
101
138
}
0 commit comments