|
| 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.catalyst.expressions.{And, ArrayExists, ArrayFilter, CaseWhen, Expression, If} |
| 21 | +import org.apache.spark.sql.catalyst.expressions.{LambdaFunction, Literal, MapFilter, Or} |
| 22 | +import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral |
| 23 | +import org.apache.spark.sql.catalyst.plans.logical.{Filter, Join, LogicalPlan} |
| 24 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 25 | +import org.apache.spark.sql.types.BooleanType |
| 26 | +import org.apache.spark.util.Utils |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * A rule that replaces `Literal(null, BooleanType)` with `FalseLiteral`, if possible, in the search |
| 31 | + * condition of the WHERE/HAVING/ON(JOIN) clauses, which contain an implicit Boolean operator |
| 32 | + * "(search condition) = TRUE". The replacement is only valid when `Literal(null, BooleanType)` is |
| 33 | + * semantically equivalent to `FalseLiteral` when evaluating the whole search condition. |
| 34 | + * |
| 35 | + * Please note that FALSE and NULL are not exchangeable in most cases, when the search condition |
| 36 | + * contains NOT and NULL-tolerant expressions. Thus, the rule is very conservative and applicable |
| 37 | + * in very limited cases. |
| 38 | + * |
| 39 | + * For example, `Filter(Literal(null, BooleanType))` is equal to `Filter(FalseLiteral)`. |
| 40 | + * |
| 41 | + * Another example containing branches is `Filter(If(cond, FalseLiteral, Literal(null, _)))`; |
| 42 | + * this can be optimized to `Filter(If(cond, FalseLiteral, FalseLiteral))`, and eventually |
| 43 | + * `Filter(FalseLiteral)`. |
| 44 | + * |
| 45 | + * Moreover, this rule also transforms predicates in all [[If]] expressions as well as branch |
| 46 | + * conditions in all [[CaseWhen]] expressions, even if they are not part of the search conditions. |
| 47 | + * |
| 48 | + * For example, `Project(If(And(cond, Literal(null)), Literal(1), Literal(2)))` can be simplified |
| 49 | + * into `Project(Literal(2))`. |
| 50 | + */ |
| 51 | +object ReplaceNullWithFalseInPredicate extends Rule[LogicalPlan] { |
| 52 | + |
| 53 | + def apply(plan: LogicalPlan): LogicalPlan = plan transform { |
| 54 | + case f @ Filter(cond, _) => f.copy(condition = replaceNullWithFalse(cond)) |
| 55 | + case j @ Join(_, _, _, Some(cond)) => j.copy(condition = Some(replaceNullWithFalse(cond))) |
| 56 | + case p: LogicalPlan => p transformExpressions { |
| 57 | + case i @ If(pred, _, _) => i.copy(predicate = replaceNullWithFalse(pred)) |
| 58 | + case cw @ CaseWhen(branches, _) => |
| 59 | + val newBranches = branches.map { case (cond, value) => |
| 60 | + replaceNullWithFalse(cond) -> value |
| 61 | + } |
| 62 | + cw.copy(branches = newBranches) |
| 63 | + case af @ ArrayFilter(_, lf @ LambdaFunction(func, _, _)) => |
| 64 | + val newLambda = lf.copy(function = replaceNullWithFalse(func)) |
| 65 | + af.copy(function = newLambda) |
| 66 | + case ae @ ArrayExists(_, lf @ LambdaFunction(func, _, _)) => |
| 67 | + val newLambda = lf.copy(function = replaceNullWithFalse(func)) |
| 68 | + ae.copy(function = newLambda) |
| 69 | + case mf @ MapFilter(_, lf @ LambdaFunction(func, _, _)) => |
| 70 | + val newLambda = lf.copy(function = replaceNullWithFalse(func)) |
| 71 | + mf.copy(function = newLambda) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Recursively traverse the Boolean-type expression to replace |
| 77 | + * `Literal(null, BooleanType)` with `FalseLiteral`, if possible. |
| 78 | + * |
| 79 | + * Note that `transformExpressionsDown` can not be used here as we must stop as soon as we hit |
| 80 | + * an expression that is not [[CaseWhen]], [[If]], [[And]], [[Or]] or |
| 81 | + * `Literal(null, BooleanType)`. |
| 82 | + */ |
| 83 | + private def replaceNullWithFalse(e: Expression): Expression = e match { |
| 84 | + case Literal(null, BooleanType) => |
| 85 | + FalseLiteral |
| 86 | + case And(left, right) => |
| 87 | + And(replaceNullWithFalse(left), replaceNullWithFalse(right)) |
| 88 | + case Or(left, right) => |
| 89 | + Or(replaceNullWithFalse(left), replaceNullWithFalse(right)) |
| 90 | + case cw: CaseWhen if cw.dataType == BooleanType => |
| 91 | + val newBranches = cw.branches.map { case (cond, value) => |
| 92 | + replaceNullWithFalse(cond) -> replaceNullWithFalse(value) |
| 93 | + } |
| 94 | + val newElseValue = cw.elseValue.map(replaceNullWithFalse) |
| 95 | + CaseWhen(newBranches, newElseValue) |
| 96 | + case i @ If(pred, trueVal, falseVal) if i.dataType == BooleanType => |
| 97 | + If(replaceNullWithFalse(pred), replaceNullWithFalse(trueVal), replaceNullWithFalse(falseVal)) |
| 98 | + case e if e.dataType == BooleanType => |
| 99 | + e |
| 100 | + case e => |
| 101 | + val message = "Expected a Boolean type expression in replaceNullWithFalse, " + |
| 102 | + s"but got the type `${e.dataType.catalogString}` in `${e.sql}`." |
| 103 | + if (Utils.isTesting) { |
| 104 | + throw new IllegalArgumentException(message) |
| 105 | + } else { |
| 106 | + logWarning(message) |
| 107 | + e |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments