|
| 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, LambdaFunction, Literal, MapFilter, Not, Or} |
| 21 | +import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLiteral} |
| 22 | +import org.apache.spark.sql.catalyst.plans.logical._ |
| 23 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 24 | +import org.apache.spark.sql.types.BooleanType |
| 25 | +import org.apache.spark.util.Utils |
| 26 | + |
| 27 | + |
| 28 | +object SimplifyConditionalsInPredicate extends Rule[LogicalPlan] { |
| 29 | + |
| 30 | + def apply(plan: LogicalPlan): LogicalPlan = plan transform { |
| 31 | + case f @ Filter(cond, _) => f.copy(condition = simplifyConditional(cond)) |
| 32 | + case j @ Join(_, _, _, Some(cond), _) => j.copy(condition = Some(simplifyConditional(cond))) |
| 33 | + case d @ DeleteFromTable(_, Some(cond)) => d.copy(condition = Some(simplifyConditional(cond))) |
| 34 | + case u @ UpdateTable(_, _, Some(cond)) => u.copy(condition = Some(simplifyConditional(cond))) |
| 35 | + case p: LogicalPlan => p transformExpressions { |
| 36 | + case i @ If(pred, _, _) => i.copy(predicate = simplifyConditional(pred)) |
| 37 | + case cw @ CaseWhen(branches, _) => |
| 38 | + val newBranches = branches.map { case (cond, value) => |
| 39 | + simplifyConditional(cond) -> value |
| 40 | + } |
| 41 | + cw.copy(branches = newBranches) |
| 42 | + case af @ ArrayFilter(_, lf @ LambdaFunction(func, _, _)) => |
| 43 | + val newLambda = lf.copy(function = simplifyConditional(func)) |
| 44 | + af.copy(function = newLambda) |
| 45 | + case ae @ ArrayExists(_, lf @ LambdaFunction(func, _, _), false) => |
| 46 | + val newLambda = lf.copy(function = simplifyConditional(func)) |
| 47 | + ae.copy(function = newLambda) |
| 48 | + case mf @ MapFilter(_, lf @ LambdaFunction(func, _, _)) => |
| 49 | + val newLambda = lf.copy(function = simplifyConditional(func)) |
| 50 | + mf.copy(function = newLambda) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private def simplifyConditional(e: Expression): Expression = e match { |
| 55 | + case Literal(null, BooleanType) => FalseLiteral |
| 56 | + case And(left, right) => And(simplifyConditional(left), simplifyConditional(right)) |
| 57 | + case Or(left, right) => Or(simplifyConditional(left), simplifyConditional(right)) |
| 58 | + case If(cond, t, FalseLiteral) => And(cond, t) |
| 59 | + case If(cond, t, TrueLiteral) => Or(Not(cond), t) |
| 60 | + case If(cond, FalseLiteral, f) => And(Not(cond), f) |
| 61 | + case If(cond, TrueLiteral, f) => Or(cond, f) |
| 62 | + case CaseWhen(Seq((cond, trueValue)), |
| 63 | + Some(FalseLiteral) | Some(Literal(null, BooleanType)) | None) => |
| 64 | + And(cond, trueValue) |
| 65 | + case CaseWhen(Seq((cond, trueValue)), Some(TrueLiteral)) => |
| 66 | + Or(Not(cond), trueValue) |
| 67 | + case CaseWhen(Seq((cond, FalseLiteral)), elseValue) => |
| 68 | + And(Not(cond), elseValue.getOrElse(Literal(null, BooleanType))) |
| 69 | + case CaseWhen(Seq((cond, TrueLiteral)), elseValue) => |
| 70 | + Or(cond, elseValue.getOrElse(Literal(null, BooleanType))) |
| 71 | + case e if e.dataType == BooleanType => e |
| 72 | + case e => |
| 73 | + val message = "Expected a Boolean type expression in simplifyConditional, " + |
| 74 | + s"but got the type `${e.dataType.catalogString}` in `${e.sql}`." |
| 75 | + if (Utils.isTesting) { |
| 76 | + throw new IllegalArgumentException(message) |
| 77 | + } else { |
| 78 | + logWarning(message) |
| 79 | + e |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments