|
| 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.api.python.PythonEvalType |
| 21 | +import org.apache.spark.sql.AnalysisException |
| 22 | +import org.apache.spark.sql.catalyst.dsl.expressions._ |
| 23 | +import org.apache.spark.sql.catalyst.dsl.plans._ |
| 24 | +import org.apache.spark.sql.catalyst.expressions.PythonUDF |
| 25 | +import org.apache.spark.sql.catalyst.plans._ |
| 26 | +import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} |
| 27 | +import org.apache.spark.sql.catalyst.rules.RuleExecutor |
| 28 | +import org.apache.spark.sql.internal.SQLConf._ |
| 29 | +import org.apache.spark.sql.types.{BooleanType, IntegerType} |
| 30 | + |
| 31 | +class PullOutPythonUDFInJoinConditionSuite extends PlanTest { |
| 32 | + |
| 33 | + object Optimize extends RuleExecutor[LogicalPlan] { |
| 34 | + val batches = |
| 35 | + Batch("Extract PythonUDF From JoinCondition", Once, |
| 36 | + PullOutPythonUDFInJoinCondition) :: |
| 37 | + Batch("Check Cartesian Products", Once, |
| 38 | + CheckCartesianProducts) :: Nil |
| 39 | + } |
| 40 | + |
| 41 | + val attrA = 'a.int |
| 42 | + val attrB = 'b.int |
| 43 | + val attrC = 'c.int |
| 44 | + val attrD = 'd.int |
| 45 | + |
| 46 | + val testRelationLeft = LocalRelation(attrA, attrB) |
| 47 | + val testRelationRight = LocalRelation(attrC, attrD) |
| 48 | + |
| 49 | + // This join condition refers to attributes from 2 tables, but the PythonUDF inside it only |
| 50 | + // refer to attributes from one side. |
| 51 | + val evaluableJoinCond = { |
| 52 | + val pythonUDF = PythonUDF("evaluable", null, |
| 53 | + IntegerType, |
| 54 | + Seq(attrA), |
| 55 | + PythonEvalType.SQL_BATCHED_UDF, |
| 56 | + udfDeterministic = true) |
| 57 | + pythonUDF === attrC |
| 58 | + } |
| 59 | + |
| 60 | + // This join condition is a PythonUDF which refers to attributes from 2 tables. |
| 61 | + val unevaluableJoinCond = PythonUDF("unevaluable", null, |
| 62 | + BooleanType, |
| 63 | + Seq(attrA, attrC), |
| 64 | + PythonEvalType.SQL_BATCHED_UDF, |
| 65 | + udfDeterministic = true) |
| 66 | + |
| 67 | + val unsupportedJoinTypes = Seq(LeftOuter, RightOuter, FullOuter, LeftAnti) |
| 68 | + |
| 69 | + private def comparePlanWithCrossJoinEnable(query: LogicalPlan, expected: LogicalPlan): Unit = { |
| 70 | + // AnalysisException thrown by CheckCartesianProducts while spark.sql.crossJoin.enabled=false |
| 71 | + val exception = intercept[AnalysisException] { |
| 72 | + Optimize.execute(query.analyze) |
| 73 | + } |
| 74 | + assert(exception.message.startsWith("Detected implicit cartesian product")) |
| 75 | + |
| 76 | + // pull out the python udf while set spark.sql.crossJoin.enabled=true |
| 77 | + withSQLConf(CROSS_JOINS_ENABLED.key -> "true") { |
| 78 | + val optimized = Optimize.execute(query.analyze) |
| 79 | + comparePlans(optimized, expected) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + test("inner join condition with python udf") { |
| 84 | + val query1 = testRelationLeft.join( |
| 85 | + testRelationRight, |
| 86 | + joinType = Inner, |
| 87 | + condition = Some(unevaluableJoinCond)) |
| 88 | + val expected1 = testRelationLeft.join( |
| 89 | + testRelationRight, |
| 90 | + joinType = Inner, |
| 91 | + condition = None).where(unevaluableJoinCond).analyze |
| 92 | + comparePlanWithCrossJoinEnable(query1, expected1) |
| 93 | + |
| 94 | + // evaluable PythonUDF will not be touched |
| 95 | + val query2 = testRelationLeft.join( |
| 96 | + testRelationRight, |
| 97 | + joinType = Inner, |
| 98 | + condition = Some(evaluableJoinCond)) |
| 99 | + comparePlans(Optimize.execute(query2), query2) |
| 100 | + } |
| 101 | + |
| 102 | + test("left semi join condition with python udf") { |
| 103 | + val query1 = testRelationLeft.join( |
| 104 | + testRelationRight, |
| 105 | + joinType = LeftSemi, |
| 106 | + condition = Some(unevaluableJoinCond)) |
| 107 | + val expected1 = testRelationLeft.join( |
| 108 | + testRelationRight, |
| 109 | + joinType = Inner, |
| 110 | + condition = None).where(unevaluableJoinCond).select('a, 'b).analyze |
| 111 | + comparePlanWithCrossJoinEnable(query1, expected1) |
| 112 | + |
| 113 | + // evaluable PythonUDF will not be touched |
| 114 | + val query2 = testRelationLeft.join( |
| 115 | + testRelationRight, |
| 116 | + joinType = LeftSemi, |
| 117 | + condition = Some(evaluableJoinCond)) |
| 118 | + comparePlans(Optimize.execute(query2), query2) |
| 119 | + } |
| 120 | + |
| 121 | + test("unevaluable python udf and common condition") { |
| 122 | + val query = testRelationLeft.join( |
| 123 | + testRelationRight, |
| 124 | + joinType = Inner, |
| 125 | + condition = Some(unevaluableJoinCond && 'a.attr === 'c.attr)) |
| 126 | + val expected = testRelationLeft.join( |
| 127 | + testRelationRight, |
| 128 | + joinType = Inner, |
| 129 | + condition = Some('a.attr === 'c.attr)).where(unevaluableJoinCond).analyze |
| 130 | + val optimized = Optimize.execute(query.analyze) |
| 131 | + comparePlans(optimized, expected) |
| 132 | + } |
| 133 | + |
| 134 | + test("unevaluable python udf or common condition") { |
| 135 | + val query = testRelationLeft.join( |
| 136 | + testRelationRight, |
| 137 | + joinType = Inner, |
| 138 | + condition = Some(unevaluableJoinCond || 'a.attr === 'c.attr)) |
| 139 | + val expected = testRelationLeft.join( |
| 140 | + testRelationRight, |
| 141 | + joinType = Inner, |
| 142 | + condition = None).where(unevaluableJoinCond || 'a.attr === 'c.attr).analyze |
| 143 | + comparePlanWithCrossJoinEnable(query, expected) |
| 144 | + } |
| 145 | + |
| 146 | + test("pull out whole complex condition with multiple unevaluable python udf") { |
| 147 | + val pythonUDF1 = PythonUDF("pythonUDF1", null, |
| 148 | + BooleanType, |
| 149 | + Seq(attrA, attrC), |
| 150 | + PythonEvalType.SQL_BATCHED_UDF, |
| 151 | + udfDeterministic = true) |
| 152 | + val condition = (unevaluableJoinCond || 'a.attr === 'c.attr) && pythonUDF1 |
| 153 | + |
| 154 | + val query = testRelationLeft.join( |
| 155 | + testRelationRight, |
| 156 | + joinType = Inner, |
| 157 | + condition = Some(condition)) |
| 158 | + val expected = testRelationLeft.join( |
| 159 | + testRelationRight, |
| 160 | + joinType = Inner, |
| 161 | + condition = None).where(condition).analyze |
| 162 | + comparePlanWithCrossJoinEnable(query, expected) |
| 163 | + } |
| 164 | + |
| 165 | + test("partial pull out complex condition with multiple unevaluable python udf") { |
| 166 | + val pythonUDF1 = PythonUDF("pythonUDF1", null, |
| 167 | + BooleanType, |
| 168 | + Seq(attrA, attrC), |
| 169 | + PythonEvalType.SQL_BATCHED_UDF, |
| 170 | + udfDeterministic = true) |
| 171 | + val condition = (unevaluableJoinCond || pythonUDF1) && 'a.attr === 'c.attr |
| 172 | + |
| 173 | + val query = testRelationLeft.join( |
| 174 | + testRelationRight, |
| 175 | + joinType = Inner, |
| 176 | + condition = Some(condition)) |
| 177 | + val expected = testRelationLeft.join( |
| 178 | + testRelationRight, |
| 179 | + joinType = Inner, |
| 180 | + condition = Some('a.attr === 'c.attr)).where(unevaluableJoinCond || pythonUDF1).analyze |
| 181 | + val optimized = Optimize.execute(query.analyze) |
| 182 | + comparePlans(optimized, expected) |
| 183 | + } |
| 184 | + |
| 185 | + test("pull out unevaluable python udf when it's mixed with evaluable one") { |
| 186 | + val query = testRelationLeft.join( |
| 187 | + testRelationRight, |
| 188 | + joinType = Inner, |
| 189 | + condition = Some(evaluableJoinCond && unevaluableJoinCond)) |
| 190 | + val expected = testRelationLeft.join( |
| 191 | + testRelationRight, |
| 192 | + joinType = Inner, |
| 193 | + condition = Some(evaluableJoinCond)).where(unevaluableJoinCond).analyze |
| 194 | + val optimized = Optimize.execute(query.analyze) |
| 195 | + comparePlans(optimized, expected) |
| 196 | + } |
| 197 | + |
| 198 | + test("throw an exception for not support join type") { |
| 199 | + for (joinType <- unsupportedJoinTypes) { |
| 200 | + val e = intercept[AnalysisException] { |
| 201 | + val query = testRelationLeft.join( |
| 202 | + testRelationRight, |
| 203 | + joinType, |
| 204 | + condition = Some(unevaluableJoinCond)) |
| 205 | + Optimize.execute(query.analyze) |
| 206 | + } |
| 207 | + assert(e.message.contentEquals( |
| 208 | + s"Using PythonUDF in join condition of join type $joinType is not supported.")) |
| 209 | + |
| 210 | + val query2 = testRelationLeft.join( |
| 211 | + testRelationRight, |
| 212 | + joinType, |
| 213 | + condition = Some(evaluableJoinCond)) |
| 214 | + comparePlans(Optimize.execute(query2), query2) |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments