-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-33798][SQL] Add new rule to push down the foldable expressions through CaseWhen/If #30790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
19b0a83
831bf66
859893d
db584ef
55f4528
0a48048
f9f622f
21ef0c3
d5135ec
8ccc3c1
45b56fc
cfac0e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -470,6 +470,11 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper { | |
| case _ => false | ||
| } | ||
|
|
||
| private def isAlwaysFalse(exps: Seq[Expression], equalTo: Literal): Boolean = { | ||
| exps.forall(_.isInstanceOf[Literal]) && | ||
| exps.forall(!EqualTo(_, equalTo).eval(EmptyRow).asInstanceOf[Boolean]) | ||
|
||
| } | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
| case q: LogicalPlan => q transformExpressionsUp { | ||
| case If(TrueLiteral, trueValue, _) => trueValue | ||
|
|
@@ -523,6 +528,14 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper { | |
| } else { | ||
| e.copy(branches = branches.take(i).map(branch => (branch._1, elseValue))) | ||
| } | ||
|
|
||
| case EqualTo(i @ If(_, trueValue, falseValue), right: Literal) | ||
| if i.deterministic && isAlwaysFalse(trueValue :: falseValue :: Nil, right) => | ||
| FalseLiteral | ||
|
|
||
| case EqualTo(c @ CaseWhen(branches, elseValue), right: Literal) | ||
|
||
| if c.deterministic && isAlwaysFalse(branches.map(_._2) ++ elseValue, right) => | ||
| FalseLiteral | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,4 +199,98 @@ class SimplifyConditionalSuite extends PlanTest with ExpressionEvalHelper with P | |
| If(Factorial(5) > 100L, b, nullLiteral).eval(EmptyRow)) | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-33798: simplify EqualTo(If, Literal) always false") { | ||
| val a = EqualTo(UnresolvedAttribute("a"), Literal(100)) | ||
| val ifExp = If(a === Literal(1), Literal(2), Literal(3)) | ||
|
||
|
|
||
| assertEquivalent(EqualTo(ifExp, Literal(4)), FalseLiteral) | ||
| assertEquivalent(EqualTo(ifExp, Literal(3)), EqualTo(ifExp, Literal(3))) | ||
| assertEquivalent(EqualTo(ifExp, Literal("4")), FalseLiteral) | ||
|
||
| assertEquivalent(EqualTo(ifExp, Literal("3")), EqualTo(ifExp, Literal(3))) | ||
|
|
||
| // Do not simplify if it contains non foldable expressions. | ||
| assertEquivalent(EqualTo(ifExp, NonFoldableLiteral(true)), | ||
| EqualTo(ifExp, NonFoldableLiteral(true))) | ||
| val nonFoldable = If(NonFoldableLiteral(true), Literal(1), Literal(2)) | ||
|
||
| assertEquivalent(EqualTo(nonFoldable, Literal(1)), EqualTo(nonFoldable, Literal(1))) | ||
|
|
||
| // Do not simplify if it contains non-deterministic expressions. | ||
| val nonDeterministic = If(LessThan(Rand(1), Literal(0.5)), Literal(1), Literal(1)) | ||
|
||
| assert(!nonDeterministic.deterministic) | ||
| assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1))) | ||
|
|
||
| // null check, SPARK-33798 will change the following two behaviors. | ||
| assertEquivalent( | ||
| EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(2)), | ||
| FalseLiteral) | ||
wangyum marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assertEquivalent( | ||
| EqualTo(If(a =!= Literal(1), Literal(1), Literal(2)), Literal(null, IntegerType)), | ||
| FalseLiteral) | ||
|
|
||
| assertEquivalent( | ||
| EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)), | ||
| EqualTo(If(a === Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1))) | ||
| assertEquivalent( | ||
| EqualTo(If(a =!= Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1)), | ||
| EqualTo(If(a =!= Literal(1), Literal(null, IntegerType), Literal(1)), Literal(1))) | ||
| assertEquivalent( | ||
| EqualTo(If(a =!= Literal(1), Literal(null, IntegerType), Literal(null, IntegerType)), | ||
| Literal(1)), | ||
| Literal(null, BooleanType)) | ||
| } | ||
|
|
||
| test("SPARK-33798: simplify EqualTo(CaseWhen, Literal) always false") { | ||
| val a = EqualTo(UnresolvedAttribute("a"), Literal(100)) | ||
| val b = UnresolvedAttribute("b") | ||
| val c = EqualTo(UnresolvedAttribute("c"), Literal(true)) | ||
| val caseWhen = CaseWhen(Seq((a, Literal(1)), (c, Literal(2))), Some(Literal(3))) | ||
|
|
||
| assertEquivalent(EqualTo(caseWhen, Literal(4)), FalseLiteral) | ||
| assertEquivalent(EqualTo(caseWhen, Literal(3)), EqualTo(caseWhen, Literal(3))) | ||
| assertEquivalent(EqualTo(caseWhen, Literal("4")), FalseLiteral) | ||
| assertEquivalent(EqualTo(caseWhen, Literal("3")), EqualTo(caseWhen, Literal(3))) | ||
| assertEquivalent( | ||
| EqualTo(CaseWhen(Seq((a, Literal("1")), (c, Literal("2"))), None), Literal("4")), | ||
| FalseLiteral) | ||
|
|
||
| assertEquivalent( | ||
| And(EqualTo(caseWhen, Literal(5)), EqualTo(caseWhen, Literal(6))), | ||
| FalseLiteral) | ||
|
|
||
| assertEquivalent( | ||
| EqualTo(CaseWhen(Seq(normalBranch, (a, Literal(1)), (c, Literal(1))), None), Literal(-1)), | ||
| FalseLiteral) | ||
|
|
||
| // Do not simplify if it contains non foldable expressions. | ||
| assertEquivalent(EqualTo(caseWhen, NonFoldableLiteral(true)), | ||
| EqualTo(caseWhen, NonFoldableLiteral(true))) | ||
| val nonFoldable = CaseWhen(Seq(normalBranch, (a, b)), None) | ||
| assertEquivalent(EqualTo(nonFoldable, Literal(1)), EqualTo(nonFoldable, Literal(1))) | ||
|
|
||
| // Do not simplify if it contains non-deterministic expressions. | ||
| val nonDeterministic = CaseWhen(Seq((LessThan(Rand(1), Literal(0.5)), Literal(1))), Some(b)) | ||
| assert(!nonDeterministic.deterministic) | ||
| assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1))) | ||
|
|
||
| // null check, SPARK-33798 will change the following two behaviors. | ||
| assertEquivalent( | ||
| EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(2)), | ||
| FalseLiteral) | ||
| assertEquivalent( | ||
| EqualTo(CaseWhen(Seq((a, Literal(1))), Some(Literal(2))), Literal(null, IntegerType)), | ||
| FalseLiteral) | ||
|
||
|
|
||
| assertEquivalent( | ||
| EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(1)), | ||
| EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(1))), Literal(1))) | ||
| assertEquivalent( | ||
| EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(null, IntegerType))), | ||
| Literal(1)), | ||
| Literal(null, BooleanType)) | ||
| assertEquivalent( | ||
| EqualTo(CaseWhen(Seq((a, Literal(null, IntegerType))), Some(Literal(null, IntegerType))), | ||
| Literal(null, IntegerType)), | ||
| Literal(null, BooleanType)) | ||
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isAlwaysFalseseems to be mismatched a little with the behavior of this function. Technically, this function resemblescontainsfunction whereexpris a list andequalToliteral is the item to be check the containment.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, do you have a recommended function name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, something like
notEqualForAllinstead ofisAlwaysFalse?