|
| 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.scalatest.Matchers._ |
| 21 | + |
| 22 | +import org.apache.spark.sql.AnalysisException |
| 23 | +import org.apache.spark.sql.catalyst.dsl.expressions._ |
| 24 | +import org.apache.spark.sql.catalyst.dsl.plans._ |
| 25 | +import org.apache.spark.sql.catalyst.expressions.Expression |
| 26 | +import org.apache.spark.sql.catalyst.plans._ |
| 27 | +import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} |
| 28 | +import org.apache.spark.sql.catalyst.rules.RuleExecutor |
| 29 | +import org.apache.spark.sql.internal.SQLConf.CROSS_JOINS_ENABLED |
| 30 | + |
| 31 | +class CheckCartesianProductsSuite extends PlanTest { |
| 32 | + |
| 33 | + object Optimize extends RuleExecutor[LogicalPlan] { |
| 34 | + val batches = Batch("Check Cartesian Products", Once, CheckCartesianProducts) :: Nil |
| 35 | + } |
| 36 | + |
| 37 | + val testRelation1 = LocalRelation('a.int, 'b.int) |
| 38 | + val testRelation2 = LocalRelation('c.int, 'd.int) |
| 39 | + |
| 40 | + val joinTypesWithRequiredCondition = Seq(Inner, LeftOuter, RightOuter, FullOuter) |
| 41 | + val joinTypesWithoutRequiredCondition = Seq(LeftSemi, LeftAnti, ExistenceJoin('exists)) |
| 42 | + |
| 43 | + test("CheckCartesianProducts doesn't throw an exception if cross joins are enabled)") { |
| 44 | + withSQLConf(CROSS_JOINS_ENABLED.key -> "true") { |
| 45 | + noException should be thrownBy { |
| 46 | + for (joinType <- joinTypesWithRequiredCondition ++ joinTypesWithoutRequiredCondition) { |
| 47 | + performCartesianProductCheck(joinType) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + test("CheckCartesianProducts throws an exception for join types that require a join condition") { |
| 54 | + withSQLConf(CROSS_JOINS_ENABLED.key -> "false") { |
| 55 | + for (joinType <- joinTypesWithRequiredCondition) { |
| 56 | + val thrownException = the [AnalysisException] thrownBy { |
| 57 | + performCartesianProductCheck(joinType) |
| 58 | + } |
| 59 | + assert(thrownException.message.contains("Detected cartesian product")) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + test("CheckCartesianProducts doesn't throw an exception if a join condition is present") { |
| 65 | + withSQLConf(CROSS_JOINS_ENABLED.key -> "false") { |
| 66 | + for (joinType <- joinTypesWithRequiredCondition) { |
| 67 | + noException should be thrownBy { |
| 68 | + performCartesianProductCheck(joinType, Some('a === 'd)) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + test("CheckCartesianProducts doesn't throw an exception if join types don't require conditions") { |
| 75 | + withSQLConf(CROSS_JOINS_ENABLED.key -> "false") { |
| 76 | + for (joinType <- joinTypesWithoutRequiredCondition) { |
| 77 | + noException should be thrownBy { |
| 78 | + performCartesianProductCheck(joinType) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private def performCartesianProductCheck( |
| 85 | + joinType: JoinType, |
| 86 | + condition: Option[Expression] = None): Unit = { |
| 87 | + val analyzedPlan = testRelation1.join(testRelation2, joinType, condition).analyze |
| 88 | + val optimizedPlan = Optimize.execute(analyzedPlan) |
| 89 | + comparePlans(analyzedPlan, optimizedPlan) |
| 90 | + } |
| 91 | +} |
0 commit comments