|
| 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.analysis |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.dsl.expressions._ |
| 21 | +import org.apache.spark.sql.catalyst.dsl.plans._ |
| 22 | +import org.apache.spark.sql.catalyst.expressions._ |
| 23 | +import org.apache.spark.sql.catalyst.plans._ |
| 24 | +import org.apache.spark.sql.catalyst.plans.logical.LocalRelation |
| 25 | +import org.apache.spark.sql.types.StringType |
| 26 | + |
| 27 | +class ResolveNaturalJoinSuite extends AnalysisTest { |
| 28 | + import org.apache.spark.sql.catalyst.analysis.TestRelations._ |
| 29 | + |
| 30 | + val t1 = testRelation2.select('a, 'b) |
| 31 | + val t2 = testRelation2.select('a, 'c) |
| 32 | + val a = testRelation2.output(0) |
| 33 | + val b = testRelation2.output(1) |
| 34 | + val c = testRelation2.output(2) |
| 35 | + val testRelation0 = LocalRelation( |
| 36 | + AttributeReference("a", StringType, nullable = false)(), |
| 37 | + AttributeReference("b", StringType, nullable = false)(), |
| 38 | + AttributeReference("c", StringType, nullable = false)()) |
| 39 | + val tt1 = testRelation0.select('a, 'b) |
| 40 | + val tt2 = testRelation0.select('a, 'c) |
| 41 | + val aa = testRelation0.output(0) |
| 42 | + val bb = testRelation0.output(1) |
| 43 | + val cc = testRelation0.output(2) |
| 44 | + val trueB = testRelation0.output(1).withNullability(true) |
| 45 | + val trueC = testRelation0.output(2).withNullability(true) |
| 46 | + |
| 47 | + test("natural inner join") { |
| 48 | + val plan = t1.join(t2, NaturalJoin(Inner), None) |
| 49 | + val expected = testRelation2.select(a, b).join( |
| 50 | + testRelation2.select(a, c), Inner, Some(EqualTo(a, a))).select(a, b, c) |
| 51 | + checkAnalysis(plan, expected) |
| 52 | + } |
| 53 | + |
| 54 | + test("natural left join") { |
| 55 | + val plan = t1.join(t2, NaturalJoin(LeftOuter), None) |
| 56 | + val expected = testRelation2.select(a, b).join( |
| 57 | + testRelation2.select(a, c), LeftOuter, Some(EqualTo(a, a))).select(a, b, c) |
| 58 | + checkAnalysis(plan, expected) |
| 59 | + } |
| 60 | + |
| 61 | + test("natural right join") { |
| 62 | + val plan = t1.join(t2, NaturalJoin(RightOuter), None) |
| 63 | + val expected = testRelation2.select(a, b).join( |
| 64 | + testRelation2.select(a, c), RightOuter, Some(EqualTo(a, a))).select(a, b, c) |
| 65 | + checkAnalysis(plan, expected) |
| 66 | + } |
| 67 | + |
| 68 | + test("natural full outer join") { |
| 69 | + val plan = t1.join(t2, NaturalJoin(FullOuter), None) |
| 70 | + val expected = testRelation2.select(a, b).join(testRelation2.select( |
| 71 | + a, c), FullOuter, Some(EqualTo(a, a))).select(Alias(Coalesce(Seq(a, a)), "a")(), b, c) |
| 72 | + checkAnalysis(plan, expected) |
| 73 | + } |
| 74 | + |
| 75 | + test("natural inner join with no nullability") { |
| 76 | + val plan = tt1.join(tt2, NaturalJoin(Inner), None) |
| 77 | + val expected = testRelation0.select(aa, bb).join( |
| 78 | + testRelation0.select(aa, cc), Inner, Some(EqualTo(aa, aa))).select(aa, bb, cc) |
| 79 | + checkAnalysis(plan, expected) |
| 80 | + } |
| 81 | + |
| 82 | + test("natural left join with no nullability") { |
| 83 | + val plan = tt1.join(tt2, NaturalJoin(LeftOuter), None) |
| 84 | + val expected = testRelation0.select(aa, bb).join( |
| 85 | + testRelation0.select(aa, cc), LeftOuter, Some(EqualTo(aa, aa))).select(aa, bb, trueC) |
| 86 | + checkAnalysis(plan, expected) |
| 87 | + } |
| 88 | + |
| 89 | + test("natural right join with no nullability") { |
| 90 | + val plan = tt1.join(tt2, NaturalJoin(RightOuter), None) |
| 91 | + val expected = testRelation0.select(aa, bb).join( |
| 92 | + testRelation0.select(aa, cc), RightOuter, Some(EqualTo(aa, aa))).select(aa, trueB, cc) |
| 93 | + checkAnalysis(plan, expected) |
| 94 | + } |
| 95 | + |
| 96 | + test("natural full outer join with no nullability") { |
| 97 | + val plan = tt1.join(tt2, NaturalJoin(FullOuter), None) |
| 98 | + val expected = testRelation0.select(aa, bb).join( |
| 99 | + testRelation0.select(aa, cc), FullOuter, Some(EqualTo(aa, aa))).select( |
| 100 | + Alias(Coalesce(Seq(aa, aa)), "a")(), trueB, trueC) |
| 101 | + checkAnalysis(plan, expected) |
| 102 | + } |
| 103 | +} |
0 commit comments