|
| 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.analysis.UnresolvedAttribute |
| 21 | +import org.apache.spark.sql.catalyst.dsl.expressions._ |
| 22 | +import org.apache.spark.sql.catalyst.dsl.plans._ |
| 23 | +import org.apache.spark.sql.catalyst.expressions._ |
| 24 | +import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLiteral} |
| 25 | +import org.apache.spark.sql.catalyst.plans.PlanTest |
| 26 | +import org.apache.spark.sql.catalyst.plans.logical._ |
| 27 | +import org.apache.spark.sql.catalyst.rules._ |
| 28 | +import org.apache.spark.sql.types.{BooleanType, IntegerType} |
| 29 | + |
| 30 | + |
| 31 | +class PushFoldableIntoBranchesSuite |
| 32 | + extends PlanTest with ExpressionEvalHelper with PredicateHelper { |
| 33 | + |
| 34 | + object Optimize extends RuleExecutor[LogicalPlan] { |
| 35 | + val batches = Batch("PushFoldableIntoBranches", FixedPoint(50), |
| 36 | + BooleanSimplification, ConstantFolding, SimplifyConditionals, PushFoldableIntoBranches) :: Nil |
| 37 | + } |
| 38 | + |
| 39 | + private val relation = LocalRelation('a.int, 'b.int, 'c.boolean) |
| 40 | + private val a = EqualTo(UnresolvedAttribute("a"), Literal(100)) |
| 41 | + private val b = UnresolvedAttribute("b") |
| 42 | + private val c = EqualTo(UnresolvedAttribute("c"), Literal(true)) |
| 43 | + private val ifExp = If(a, Literal(2), Literal(3)) |
| 44 | + private val caseWhen = CaseWhen(Seq((a, Literal(1)), (c, Literal(2))), Some(Literal(3))) |
| 45 | + |
| 46 | + protected def assertEquivalent(e1: Expression, e2: Expression): Unit = { |
| 47 | + val correctAnswer = Project(Alias(e2, "out")() :: Nil, relation).analyze |
| 48 | + val actual = Optimize.execute(Project(Alias(e1, "out")() :: Nil, relation).analyze) |
| 49 | + comparePlans(actual, correctAnswer) |
| 50 | + } |
| 51 | + |
| 52 | + private val normalBranch = (NonFoldableLiteral(true), Literal(10)) |
| 53 | + |
| 54 | + test("SPARK-33798: Push down EqualTo through If") { |
| 55 | + assertEquivalent(EqualTo(ifExp, Literal(4)), FalseLiteral) |
| 56 | + assertEquivalent(EqualTo(ifExp, Literal(3)), If(a, FalseLiteral, TrueLiteral)) |
| 57 | + assertEquivalent(EqualTo(ifExp, Literal("4")), FalseLiteral) |
| 58 | + assertEquivalent(EqualTo(ifExp, Literal("3")), If(a, FalseLiteral, TrueLiteral)) |
| 59 | + |
| 60 | + // Do not simplify if it contains non foldable expressions. |
| 61 | + assertEquivalent( |
| 62 | + EqualTo(If(a, b, Literal(2)), Literal(2)), |
| 63 | + EqualTo(If(a, b, Literal(2)), Literal(2))) |
| 64 | + |
| 65 | + // Do not simplify if it contains non-deterministic expressions. |
| 66 | + val nonDeterministic = If(LessThan(Rand(1), Literal(0.5)), Literal(1), Literal(1)) |
| 67 | + assert(!nonDeterministic.deterministic) |
| 68 | + assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1))) |
| 69 | + |
| 70 | + // Handle Null values. |
| 71 | + assertEquivalent( |
| 72 | + EqualTo(If(a, Literal(null, IntegerType), Literal(1)), Literal(1)), |
| 73 | + If(a, Literal(null, BooleanType), TrueLiteral)) |
| 74 | + assertEquivalent( |
| 75 | + EqualTo(If(a, Literal(null, IntegerType), Literal(1)), Literal(2)), |
| 76 | + If(a, Literal(null, BooleanType), FalseLiteral)) |
| 77 | + assertEquivalent( |
| 78 | + EqualTo(If(a, Literal(1), Literal(2)), Literal(null, IntegerType)), |
| 79 | + Literal(null, BooleanType)) |
| 80 | + assertEquivalent( |
| 81 | + EqualTo(If(a, Literal(null, IntegerType), Literal(null, IntegerType)), Literal(1)), |
| 82 | + Literal(null, BooleanType)) |
| 83 | + } |
| 84 | + |
| 85 | + test("SPARK-33798: Push down other BinaryComparison through If") { |
| 86 | + assertEquivalent(EqualNullSafe(ifExp, Literal(4)), FalseLiteral) |
| 87 | + assertEquivalent(GreaterThan(ifExp, Literal(4)), FalseLiteral) |
| 88 | + assertEquivalent(GreaterThanOrEqual(ifExp, Literal(4)), FalseLiteral) |
| 89 | + assertEquivalent(LessThan(ifExp, Literal(4)), TrueLiteral) |
| 90 | + assertEquivalent(LessThanOrEqual(ifExp, Literal(4)), TrueLiteral) |
| 91 | + } |
| 92 | + |
| 93 | + test("SPARK-33798: Push down EqualTo through CaseWhen") { |
| 94 | + assertEquivalent(EqualTo(caseWhen, Literal(4)), FalseLiteral) |
| 95 | + assertEquivalent(EqualTo(caseWhen, Literal(3)), |
| 96 | + CaseWhen(Seq((a, FalseLiteral), (c, FalseLiteral)), Some(TrueLiteral))) |
| 97 | + assertEquivalent(EqualTo(caseWhen, Literal("4")), FalseLiteral) |
| 98 | + assertEquivalent(EqualTo(caseWhen, Literal("3")), |
| 99 | + CaseWhen(Seq((a, FalseLiteral), (c, FalseLiteral)), Some(TrueLiteral))) |
| 100 | + assertEquivalent( |
| 101 | + EqualTo(CaseWhen(Seq((a, Literal("1")), (c, Literal("2"))), None), Literal("4")), |
| 102 | + CaseWhen(Seq((a, FalseLiteral), (c, FalseLiteral)), None)) |
| 103 | + |
| 104 | + assertEquivalent( |
| 105 | + And(EqualTo(caseWhen, Literal(5)), EqualTo(caseWhen, Literal(6))), |
| 106 | + FalseLiteral) |
| 107 | + |
| 108 | + // Do not simplify if it contains non foldable expressions. |
| 109 | + assertEquivalent(EqualTo(caseWhen, NonFoldableLiteral(true)), |
| 110 | + EqualTo(caseWhen, NonFoldableLiteral(true))) |
| 111 | + val nonFoldable = CaseWhen(Seq(normalBranch, (a, b)), None) |
| 112 | + assertEquivalent(EqualTo(nonFoldable, Literal(1)), EqualTo(nonFoldable, Literal(1))) |
| 113 | + |
| 114 | + // Do not simplify if it contains non-deterministic expressions. |
| 115 | + val nonDeterministic = CaseWhen(Seq((LessThan(Rand(1), Literal(0.5)), Literal(1))), Some(b)) |
| 116 | + assert(!nonDeterministic.deterministic) |
| 117 | + assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1))) |
| 118 | + |
| 119 | + // Handle Null values. |
| 120 | + assertEquivalent( |
| 121 | + EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(2)), |
| 122 | + CaseWhen(Seq((a, Literal(null, BooleanType))), Some(FalseLiteral))) |
| 123 | + assertEquivalent( |
| 124 | + EqualTo(CaseWhen(Seq((a, Literal(1))), Some(Literal(2))), Literal(null, IntegerType)), |
| 125 | + Literal(null, BooleanType)) |
| 126 | + assertEquivalent( |
| 127 | + EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(1)), |
| 128 | + CaseWhen(Seq((a, Literal(null, BooleanType))), Some(TrueLiteral))) |
| 129 | + assertEquivalent( |
| 130 | + EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(null, IntegerType))), |
| 131 | + Literal(1)), |
| 132 | + Literal(null, BooleanType)) |
| 133 | + assertEquivalent( |
| 134 | + EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(null, IntegerType))), |
| 135 | + Literal(null, IntegerType)), |
| 136 | + Literal(null, BooleanType)) |
| 137 | + } |
| 138 | + |
| 139 | + test("SPARK-33798: Push down other BinaryComparison through CaseWhen") { |
| 140 | + assertEquivalent(EqualNullSafe(caseWhen, Literal(4)), FalseLiteral) |
| 141 | + assertEquivalent(GreaterThan(caseWhen, Literal(4)), FalseLiteral) |
| 142 | + assertEquivalent(GreaterThanOrEqual(caseWhen, Literal(4)), FalseLiteral) |
| 143 | + assertEquivalent(LessThan(caseWhen, Literal(4)), TrueLiteral) |
| 144 | + assertEquivalent(LessThanOrEqual(caseWhen, Literal(4)), TrueLiteral) |
| 145 | + } |
| 146 | +} |
0 commit comments