|
| 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