/
LimitPushdownSuite.scala
197 lines (172 loc) · 8.3 KB
/
LimitPushdownSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* 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.optimizer
import org.apache.spark.sql.Row
import org.apache.spark.sql.catalyst.analysis.EliminateSubqueryAliases
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions.Add
import org.apache.spark.sql.catalyst.plans.{FullOuter, LeftOuter, PlanTest, RightOuter}
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules._
class LimitPushdownSuite extends PlanTest {
private object Optimize extends RuleExecutor[LogicalPlan] {
val batches =
Batch("Subqueries", Once,
EliminateSubqueryAliases) ::
Batch("Limit pushdown", FixedPoint(100),
LimitPushDown,
EliminateLimits,
ConstantFolding,
BooleanSimplification) :: Nil
}
private val testRelation = LocalRelation.fromExternalRows(
Seq("a".attr.int, "b".attr.int, "c".attr.int),
1.to(6).map(_ => Row(1, 2, 3)))
private val testRelation2 = LocalRelation.fromExternalRows(
Seq("d".attr.int, "e".attr.int, "f".attr.int),
1.to(6).map(_ => Row(1, 2, 3)))
private val x = testRelation.subquery('x)
private val y = testRelation.subquery('y)
// Union ---------------------------------------------------------------------------------------
test("Union: limit to each side") {
val unionQuery = Union(testRelation, testRelation2).limit(1)
val unionOptimized = Optimize.execute(unionQuery.analyze)
val unionCorrectAnswer =
Limit(1, Union(LocalLimit(1, testRelation), LocalLimit(1, testRelation2))).analyze
comparePlans(unionOptimized, unionCorrectAnswer)
}
test("Union: limit to each side with constant-foldable limit expressions") {
val unionQuery = Union(testRelation, testRelation2).limit(Add(1, 1))
val unionOptimized = Optimize.execute(unionQuery.analyze)
val unionCorrectAnswer =
Limit(2, Union(LocalLimit(2, testRelation), LocalLimit(2, testRelation2))).analyze
comparePlans(unionOptimized, unionCorrectAnswer)
}
test("Union: limit to each side with the new limit number") {
val unionQuery = Union(testRelation, testRelation2.limit(3)).limit(1)
val unionOptimized = Optimize.execute(unionQuery.analyze)
val unionCorrectAnswer =
Limit(1, Union(LocalLimit(1, testRelation), LocalLimit(1, testRelation2))).analyze
comparePlans(unionOptimized, unionCorrectAnswer)
}
test("Union: no limit to both sides if children having smaller limit values") {
val unionQuery =
Union(testRelation.limit(1), testRelation2.select('d, 'e, 'f).limit(1)).limit(2)
val unionOptimized = Optimize.execute(unionQuery.analyze)
val unionCorrectAnswer =
Union(testRelation.limit(1), testRelation2.select('d, 'e, 'f).limit(1)).analyze
comparePlans(unionOptimized, unionCorrectAnswer)
}
test("Union: limit to each sides if children having larger limit values") {
val unionQuery =
Union(testRelation.limit(3), testRelation2.select('d, 'e, 'f).limit(4)).limit(2)
val unionOptimized = Optimize.execute(unionQuery.analyze)
val unionCorrectAnswer =
Limit(2, Union(
LocalLimit(2, testRelation), LocalLimit(2, testRelation2.select('d, 'e, 'f)))).analyze
comparePlans(unionOptimized, unionCorrectAnswer)
}
// Outer join ----------------------------------------------------------------------------------
test("left outer join") {
val originalQuery = x.join(y, LeftOuter).limit(1)
val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer = Limit(1, LocalLimit(1, x).join(y, LeftOuter)).analyze
comparePlans(optimized, correctAnswer)
}
test("left outer join and left sides are limited") {
val originalQuery = x.limit(2).join(y, LeftOuter).limit(1)
val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer = Limit(1, LocalLimit(1, x).join(y, LeftOuter)).analyze
comparePlans(optimized, correctAnswer)
}
test("left outer join and right sides are limited") {
val originalQuery = x.join(y.limit(2), LeftOuter).limit(1)
val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer = Limit(1, LocalLimit(1, x).join(Limit(2, y), LeftOuter)).analyze
comparePlans(optimized, correctAnswer)
}
test("right outer join") {
val originalQuery = x.join(y, RightOuter).limit(1)
val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer = Limit(1, x.join(LocalLimit(1, y), RightOuter)).analyze
comparePlans(optimized, correctAnswer)
}
test("right outer join and right sides are limited") {
val originalQuery = x.join(y.limit(2), RightOuter).limit(1)
val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer = Limit(1, x.join(LocalLimit(1, y), RightOuter)).analyze
comparePlans(optimized, correctAnswer)
}
test("right outer join and left sides are limited") {
val originalQuery = x.limit(2).join(y, RightOuter).limit(1)
val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer = Limit(1, Limit(2, x).join(LocalLimit(1, y), RightOuter)).analyze
comparePlans(optimized, correctAnswer)
}
test("larger limits are not pushed on top of smaller ones in right outer join") {
val originalQuery = x.join(y.limit(5), RightOuter).limit(10)
val optimized = Optimize.execute(originalQuery.analyze)
val correctAnswer = Limit(10, x.join(Limit(5, y), RightOuter)).analyze
comparePlans(optimized, correctAnswer)
}
test("full outer join where neither side is limited and both sides have same statistics") {
assert(x.stats.sizeInBytes === y.stats.sizeInBytes)
val originalQuery = x.join(y, FullOuter).limit(1).analyze
val optimized = Optimize.execute(originalQuery)
// No pushdown for FULL OUTER JOINS.
comparePlans(optimized, originalQuery)
}
test("full outer join where neither side is limited and left side has larger statistics") {
val xBig = testRelation.copy(data = Seq.fill(10)(null)).subquery('x)
assert(xBig.stats.sizeInBytes > y.stats.sizeInBytes)
val originalQuery = xBig.join(y, FullOuter).limit(1).analyze
val optimized = Optimize.execute(originalQuery)
// No pushdown for FULL OUTER JOINS.
comparePlans(optimized, originalQuery)
}
test("full outer join where neither side is limited and right side has larger statistics") {
val yBig = testRelation.copy(data = Seq.fill(10)(null)).subquery('y)
assert(x.stats.sizeInBytes < yBig.stats.sizeInBytes)
val originalQuery = x.join(yBig, FullOuter).limit(1).analyze
val optimized = Optimize.execute(originalQuery)
// No pushdown for FULL OUTER JOINS.
comparePlans(optimized, originalQuery)
}
test("full outer join where both sides are limited") {
val originalQuery = x.limit(2).join(y.limit(2), FullOuter).limit(1).analyze
val optimized = Optimize.execute(originalQuery)
// No pushdown for FULL OUTER JOINS.
comparePlans(optimized, originalQuery)
}
test("SPARK-33433: Change Aggregate max rows to 1 if grouping is empty") {
val analyzed1 = Limit(1, Union(
x.groupBy()(count(1)),
y.groupBy()(count(1)))).analyze
val optimized1 = Optimize.execute(analyzed1)
comparePlans(analyzed1, optimized1)
// test push down
val analyzed2 = Limit(1, Union(
x.groupBy(Symbol("a"))(count(1)),
y.groupBy(Symbol("b"))(count(1)))).analyze
val optimized2 = Optimize.execute(analyzed2)
val expected2 = Limit(1, Union(
LocalLimit(1, x.groupBy(Symbol("a"))(count(1))),
LocalLimit(1, y.groupBy(Symbol("b"))(count(1))))).analyze
comparePlans(expected2, optimized2)
}
}