|
| 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.Row |
| 21 | +import org.apache.spark.sql.catalyst.dsl.expressions._ |
| 22 | +import org.apache.spark.sql.catalyst.dsl.plans._ |
| 23 | +import org.apache.spark.sql.catalyst.expressions.Literal |
| 24 | +import org.apache.spark.sql.catalyst.plans._ |
| 25 | +import org.apache.spark.sql.catalyst.plans.logical.{Distinct, GlobalLimit, LocalLimit, LocalRelation, LogicalPlan, Project} |
| 26 | +import org.apache.spark.sql.catalyst.rules.RuleExecutor |
| 27 | +import org.apache.spark.sql.types.IntegerType |
| 28 | + |
| 29 | +// Test class to verify correct functioning of OptimizeLimitZero rule in various scenarios |
| 30 | +class OptimizeLimitZeroSuite extends PlanTest { |
| 31 | + object Optimize extends RuleExecutor[LogicalPlan] { |
| 32 | + val batches = |
| 33 | + Batch("OptimizeLimitZero", Once, |
| 34 | + ReplaceIntersectWithSemiJoin, |
| 35 | + OptimizeLimitZero, |
| 36 | + PropagateEmptyRelation) :: Nil |
| 37 | + } |
| 38 | + |
| 39 | + val testRelation1 = LocalRelation.fromExternalRows(Seq('a.int), data = Seq(Row(1))) |
| 40 | + val testRelation2 = LocalRelation.fromExternalRows(Seq('b.int), data = Seq(Row(1))) |
| 41 | + |
| 42 | + test("Limit 0: return empty local relation") { |
| 43 | + val query = testRelation1.limit(0) |
| 44 | + |
| 45 | + val optimized = Optimize.execute(query.analyze) |
| 46 | + val correctAnswer = LocalRelation('a.int) |
| 47 | + |
| 48 | + comparePlans(optimized, correctAnswer) |
| 49 | + } |
| 50 | + |
| 51 | + test("Limit 0: individual LocalLimit 0 node") { |
| 52 | + val query = LocalLimit(0, testRelation1) |
| 53 | + |
| 54 | + val optimized = Optimize.execute(query.analyze) |
| 55 | + val correctAnswer = LocalRelation('a.int) |
| 56 | + |
| 57 | + comparePlans(optimized, correctAnswer) |
| 58 | + } |
| 59 | + |
| 60 | + test("Limit 0: individual GlobalLimit 0 node") { |
| 61 | + val query = GlobalLimit(0, testRelation1) |
| 62 | + |
| 63 | + val optimized = Optimize.execute(query.analyze) |
| 64 | + val correctAnswer = LocalRelation('a.int) |
| 65 | + |
| 66 | + comparePlans(optimized, correctAnswer) |
| 67 | + } |
| 68 | + |
| 69 | + Seq( |
| 70 | + (Inner, LocalRelation('a.int, 'b.int)), |
| 71 | + (LeftOuter, Project(Seq('a, Literal(null).cast(IntegerType).as('b)), testRelation1).analyze), |
| 72 | + (RightOuter, LocalRelation('a.int, 'b.int)), |
| 73 | + (FullOuter, Project(Seq('a, Literal(null).cast(IntegerType).as('b)), testRelation1).analyze) |
| 74 | + ).foreach { case (jt, correctAnswer) => |
| 75 | + test(s"Limit 0: for join type $jt") { |
| 76 | + val query = testRelation1 |
| 77 | + .join(testRelation2.limit(0), joinType = jt, condition = Some('a.attr == 'b.attr)) |
| 78 | + |
| 79 | + val optimized = Optimize.execute(query.analyze) |
| 80 | + |
| 81 | + comparePlans(optimized, correctAnswer) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + test("Limit 0: 3-way join") { |
| 86 | + val testRelation3 = LocalRelation.fromExternalRows(Seq('c.int), data = Seq(Row(1))) |
| 87 | + |
| 88 | + val subJoinQuery = testRelation1 |
| 89 | + .join(testRelation2, joinType = Inner, condition = Some('a.attr == 'b.attr)) |
| 90 | + val query = subJoinQuery |
| 91 | + .join(testRelation3.limit(0), joinType = Inner, condition = Some('a.attr == 'c.attr)) |
| 92 | + |
| 93 | + val optimized = Optimize.execute(query.analyze) |
| 94 | + val correctAnswer = LocalRelation('a.int, 'b.int, 'c.int) |
| 95 | + |
| 96 | + comparePlans(optimized, correctAnswer) |
| 97 | + } |
| 98 | + |
| 99 | + test("Limit 0: intersect") { |
| 100 | + val query = testRelation1 |
| 101 | + .intersect(testRelation1.limit(0), isAll = false) |
| 102 | + |
| 103 | + val optimized = Optimize.execute(query.analyze) |
| 104 | + val correctAnswer = Distinct(LocalRelation('a.int)) |
| 105 | + |
| 106 | + comparePlans(optimized, correctAnswer) |
| 107 | + } |
| 108 | +} |
0 commit comments