Skip to content

Commit b723ac4

Browse files
committed
Added limit folding tests
1 parent a0ff7c4 commit b723ac4

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/dsl/package.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ package object dsl {
175175

176176
def where(condition: Expression) = Filter(condition, logicalPlan)
177177

178+
def limit(limitExpr: Expression) = Limit(limitExpr, logicalPlan)
179+
178180
def join(
179181
otherPlan: LogicalPlan,
180182
joinType: JoinType = Inner,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import org.apache.spark.sql.catalyst.types._
2929

3030
object Optimizer extends RuleExecutor[LogicalPlan] {
3131
val batches =
32-
Batch("LimitFolding", FixedPoint(100),
32+
Batch("Combine Limits", FixedPoint(100),
3333
CombineLimits) ::
3434
Batch("ConstantFolding", FixedPoint(100),
3535
NullPropagation,
@@ -371,6 +371,7 @@ object SimplifyCasts extends Rule[LogicalPlan] {
371371
*/
372372
object CombineLimits extends Rule[LogicalPlan] {
373373
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
374-
case ll @ Limit(le, nl @ Limit(ne, grandChild)) => Limit(If(LessThan(ne, le), ne, le), grandChild)
374+
case ll @ Limit(le, nl @ Limit(ne, grandChild)) =>
375+
Limit(If(LessThan(ne, le), ne, le), grandChild)
375376
}
376377
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.catalyst.optimizer
19+
20+
import org.apache.spark.sql.catalyst.plans.logical._
21+
import org.apache.spark.sql.catalyst.rules._
22+
import org.apache.spark.sql.catalyst.dsl.plans._
23+
import org.apache.spark.sql.catalyst.dsl.expressions._
24+
25+
class CombiningLimitsSuite extends OptimizerTest {
26+
27+
object Optimize extends RuleExecutor[LogicalPlan] {
28+
val batches =
29+
Batch("Combine Limit", FixedPoint(2),
30+
CombineLimits) ::
31+
Batch("Constant Folding", FixedPoint(3),
32+
NullPropagation,
33+
ConstantFolding,
34+
BooleanSimplification) :: Nil
35+
}
36+
37+
val testRelation = LocalRelation('a.int, 'b.int, 'c.int)
38+
39+
test("limits: combines two limits") {
40+
val originalQuery =
41+
testRelation
42+
.select('a)
43+
.limit(10).analyze
44+
.limit(5).analyze
45+
46+
val optimized = Optimize(originalQuery)
47+
val correctAnswer =
48+
testRelation
49+
.select('a)
50+
.limit(5).analyze
51+
52+
comparePlans(optimized, correctAnswer)
53+
}
54+
55+
test("limits: combines three limits") {
56+
val originalQuery =
57+
testRelation
58+
.select('a)
59+
.limit(2).analyze
60+
.limit(7).analyze
61+
.limit(5).analyze
62+
63+
val optimized = Optimize(originalQuery)
64+
val correctAnswer =
65+
testRelation
66+
.select('a)
67+
.limit(2).analyze
68+
69+
comparePlans(optimized, correctAnswer)
70+
}
71+
}

0 commit comments

Comments
 (0)