|
| 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.AnalysisException |
| 21 | +import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute |
| 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.{And, CaseWhen, Expression, If, IsNotNull, Literal, Or} |
| 25 | +import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLiteral} |
| 26 | +import org.apache.spark.sql.catalyst.plans.{Inner, PlanTest} |
| 27 | +import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, LocalRelation, LogicalPlan, UpdateTable} |
| 28 | +import org.apache.spark.sql.catalyst.rules.RuleExecutor |
| 29 | +import org.apache.spark.sql.types.{BooleanType, IntegerType} |
| 30 | + |
| 31 | +class SimplifyConditionalsInPredicateSuite extends PlanTest { |
| 32 | + |
| 33 | + object Optimize extends RuleExecutor[LogicalPlan] { |
| 34 | + val batches = |
| 35 | + Batch("SimplifyConditionalsInPredicate", FixedPoint(10), |
| 36 | + NullPropagation, |
| 37 | + ConstantFolding, |
| 38 | + BooleanSimplification, |
| 39 | + SimplifyConditionals, |
| 40 | + SimplifyConditionalsInPredicate) :: Nil |
| 41 | + } |
| 42 | + |
| 43 | + private val testRelation = |
| 44 | + LocalRelation('i.int, 'b.boolean, 'a.array(IntegerType), 'm.map(IntegerType, IntegerType)) |
| 45 | + private val anotherTestRelation = LocalRelation('d.int) |
| 46 | + |
| 47 | + test("if(cond, trueVal, false) => And(cond, trueVal)") { |
| 48 | + val originalCond = If( |
| 49 | + UnresolvedAttribute("i") > Literal(10), |
| 50 | + UnresolvedAttribute("b"), |
| 51 | + FalseLiteral) |
| 52 | + val expectedCond = And( |
| 53 | + UnresolvedAttribute("i") > Literal(10), |
| 54 | + UnresolvedAttribute("b")) |
| 55 | + testFilter(originalCond, expectedCond = expectedCond) |
| 56 | + testJoin(originalCond, expectedCond = expectedCond) |
| 57 | + testDelete(originalCond, expectedCond = expectedCond) |
| 58 | + testUpdate(originalCond, expectedCond = expectedCond) |
| 59 | + testProjection(originalCond, expectedExpr = originalCond) |
| 60 | + } |
| 61 | + |
| 62 | + test("if(cond, trueVal, true) => or(not(cond), trueVal)") { |
| 63 | + val originalCond = If( |
| 64 | + UnresolvedAttribute("i") > Literal(10), |
| 65 | + UnresolvedAttribute("b"), |
| 66 | + TrueLiteral) |
| 67 | + val expectedCond = Or( |
| 68 | + UnresolvedAttribute("i") <= Literal(10), |
| 69 | + UnresolvedAttribute("b")) |
| 70 | + testFilter(originalCond, expectedCond = expectedCond) |
| 71 | + testJoin(originalCond, expectedCond = expectedCond) |
| 72 | + testDelete(originalCond, expectedCond = expectedCond) |
| 73 | + testUpdate(originalCond, expectedCond = expectedCond) |
| 74 | + testProjection(originalCond, expectedExpr = originalCond) |
| 75 | + } |
| 76 | + |
| 77 | + test("if(cond, false, falseVal) => and(not(cond), falseVal)") { |
| 78 | + val originalCond = If( |
| 79 | + UnresolvedAttribute("i") > Literal(10), |
| 80 | + FalseLiteral, |
| 81 | + UnresolvedAttribute("b")) |
| 82 | + val expectedCond = And( |
| 83 | + UnresolvedAttribute("i") <= Literal(10), |
| 84 | + UnresolvedAttribute("b")) |
| 85 | + testFilter(originalCond, expectedCond = expectedCond) |
| 86 | + testJoin(originalCond, expectedCond = expectedCond) |
| 87 | + testDelete(originalCond, expectedCond = expectedCond) |
| 88 | + testUpdate(originalCond, expectedCond = expectedCond) |
| 89 | + testProjection(originalCond, expectedExpr = originalCond) |
| 90 | + } |
| 91 | + |
| 92 | + test("if(cond, true, falseVal) => or(cond, falseVal)") { |
| 93 | + val originalCond = If( |
| 94 | + UnresolvedAttribute("i") > Literal(10), |
| 95 | + TrueLiteral, |
| 96 | + UnresolvedAttribute("b")) |
| 97 | + val expectedCond = Or( |
| 98 | + UnresolvedAttribute("i") > Literal(10), |
| 99 | + UnresolvedAttribute("b")) |
| 100 | + testFilter(originalCond, expectedCond = expectedCond) |
| 101 | + testJoin(originalCond, expectedCond = expectedCond) |
| 102 | + testDelete(originalCond, expectedCond = expectedCond) |
| 103 | + testUpdate(originalCond, expectedCond = expectedCond) |
| 104 | + testProjection(originalCond, expectedExpr = originalCond) |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + test("case when cond then trueVal else false end => And(cond, trueVal)") { |
| 109 | + Seq(Some(FalseLiteral), None, Some(Literal(null, BooleanType))).foreach { elseExp => |
| 110 | + val originalCond = CaseWhen( |
| 111 | + Seq((UnresolvedAttribute("i") > Literal(10), UnresolvedAttribute("b"))), |
| 112 | + elseExp) |
| 113 | + val expectedCond = And( |
| 114 | + UnresolvedAttribute("i") > Literal(10), |
| 115 | + UnresolvedAttribute("b")) |
| 116 | + testFilter(originalCond, expectedCond = expectedCond) |
| 117 | + testJoin(originalCond, expectedCond = expectedCond) |
| 118 | + testDelete(originalCond, expectedCond = expectedCond) |
| 119 | + testUpdate(originalCond, expectedCond = expectedCond) |
| 120 | + testProjection(originalCond, expectedExpr = originalCond) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + test("case when cond then trueVal else true end => or(not(cond), trueVal)") { |
| 125 | + val originalCond = CaseWhen( |
| 126 | + Seq((UnresolvedAttribute("i") > Literal(10), UnresolvedAttribute("b"))), |
| 127 | + TrueLiteral) |
| 128 | + val expectedCond = Or( |
| 129 | + UnresolvedAttribute("i") <= Literal(10), |
| 130 | + UnresolvedAttribute("b")) |
| 131 | + testFilter(originalCond, expectedCond = expectedCond) |
| 132 | + testJoin(originalCond, expectedCond = expectedCond) |
| 133 | + testDelete(originalCond, expectedCond = expectedCond) |
| 134 | + testUpdate(originalCond, expectedCond = expectedCond) |
| 135 | + testProjection(originalCond, expectedExpr = originalCond) |
| 136 | + } |
| 137 | + |
| 138 | + test("case when cond then false else elseValue end => and(not(cond), elseValue)") { |
| 139 | + Seq() |
| 140 | + val originalCond = CaseWhen( |
| 141 | + Seq((UnresolvedAttribute("i") > Literal(10), FalseLiteral)), |
| 142 | + UnresolvedAttribute("b")) |
| 143 | + val expectedCond = And( |
| 144 | + UnresolvedAttribute("i") <= Literal(10), |
| 145 | + UnresolvedAttribute("b")) |
| 146 | + testFilter(originalCond, expectedCond = expectedCond) |
| 147 | + testJoin(originalCond, expectedCond = expectedCond) |
| 148 | + testDelete(originalCond, expectedCond = expectedCond) |
| 149 | + testUpdate(originalCond, expectedCond = expectedCond) |
| 150 | + testProjection(originalCond, expectedExpr = originalCond) |
| 151 | + } |
| 152 | + |
| 153 | + test("case when cond then true else elseValue end => or(cond, elseValue)") { |
| 154 | + val originalCond = CaseWhen( |
| 155 | + Seq((UnresolvedAttribute("i") > Literal(10), TrueLiteral)), |
| 156 | + UnresolvedAttribute("b")) |
| 157 | + val expectedCond = Or( |
| 158 | + UnresolvedAttribute("i") > Literal(10), |
| 159 | + UnresolvedAttribute("b")) |
| 160 | + testFilter(originalCond, expectedCond = expectedCond) |
| 161 | + testJoin(originalCond, expectedCond = expectedCond) |
| 162 | + testDelete(originalCond, expectedCond = expectedCond) |
| 163 | + testUpdate(originalCond, expectedCond = expectedCond) |
| 164 | + testProjection(originalCond, expectedExpr = originalCond) |
| 165 | + } |
| 166 | + |
| 167 | + test("case when cond then true end => or(cond, null)") { |
| 168 | + val originalCond = CaseWhen( |
| 169 | + Seq((UnresolvedAttribute("i") > Literal(10), TrueLiteral))) |
| 170 | + val expectedCond = UnresolvedAttribute("i") > Literal(10) |
| 171 | + testFilter(originalCond, expectedCond = expectedCond) |
| 172 | + testJoin(originalCond, expectedCond = expectedCond) |
| 173 | + testDelete(originalCond, expectedCond = expectedCond) |
| 174 | + testUpdate(originalCond, expectedCond = expectedCond) |
| 175 | + testProjection(originalCond, expectedExpr = originalCond) |
| 176 | + } |
| 177 | + |
| 178 | + test("Simplify conditional in conditions of CaseWhen inside another CaseWhen") { |
| 179 | + val nestedCaseWhen = CaseWhen( |
| 180 | + Seq((UnresolvedAttribute("i") > Literal(10)) -> UnresolvedAttribute("b")), |
| 181 | + FalseLiteral) |
| 182 | + val originalCond = CaseWhen(Seq(IsNotNull(nestedCaseWhen) -> FalseLiteral)) |
| 183 | + val expectedCond = FalseLiteral |
| 184 | + |
| 185 | + testFilter(originalCond, expectedCond = expectedCond) |
| 186 | + testJoin(originalCond, expectedCond = expectedCond) |
| 187 | + testDelete(originalCond, expectedCond = expectedCond) |
| 188 | + testUpdate(originalCond, expectedCond = expectedCond) |
| 189 | + testProjection(originalCond, expectedExpr = originalCond) |
| 190 | + } |
| 191 | + |
| 192 | + test("Not expected type - simplifyConditional") { |
| 193 | + val e = intercept[AnalysisException] { |
| 194 | + testFilter(originalCond = Literal(null, IntegerType), expectedCond = FalseLiteral) |
| 195 | + }.getMessage |
| 196 | + assert(e.contains("'CAST(NULL AS INT)' of type int is not a boolean")) |
| 197 | + } |
| 198 | + |
| 199 | + private def testFilter(originalCond: Expression, expectedCond: Expression): Unit = { |
| 200 | + test((rel, exp) => rel.where(exp), originalCond, expectedCond) |
| 201 | + } |
| 202 | + |
| 203 | + private def testJoin(originalCond: Expression, expectedCond: Expression): Unit = { |
| 204 | + test((rel, exp) => rel.join(anotherTestRelation, Inner, Some(exp)), originalCond, expectedCond) |
| 205 | + } |
| 206 | + |
| 207 | + private def testProjection(originalExpr: Expression, expectedExpr: Expression): Unit = { |
| 208 | + test((rel, exp) => rel.select(exp), originalExpr, expectedExpr) |
| 209 | + } |
| 210 | + |
| 211 | + private def testDelete(originalCond: Expression, expectedCond: Expression): Unit = { |
| 212 | + test((rel, expr) => DeleteFromTable(rel, Some(expr)), originalCond, expectedCond) |
| 213 | + } |
| 214 | + |
| 215 | + private def testUpdate(originalCond: Expression, expectedCond: Expression): Unit = { |
| 216 | + test((rel, expr) => UpdateTable(rel, Seq.empty, Some(expr)), originalCond, expectedCond) |
| 217 | + } |
| 218 | + |
| 219 | + private def test( |
| 220 | + func: (LogicalPlan, Expression) => LogicalPlan, |
| 221 | + originalExpr: Expression, |
| 222 | + expectedExpr: Expression): Unit = { |
| 223 | + |
| 224 | + val originalPlan = func(testRelation, originalExpr).analyze |
| 225 | + val optimizedPlan = Optimize.execute(originalPlan) |
| 226 | + val expectedPlan = func(testRelation, expectedExpr).analyze |
| 227 | + comparePlans(optimizedPlan, expectedPlan) |
| 228 | + } |
| 229 | +} |
0 commit comments