-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-33798][SQL] Add new rule to push down the foldable expressions through CaseWhen/If #30790
Changes from 1 commit
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,10 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper { | |
case _ => false | ||
} | ||
|
||
private def isAlwaysFalse(exps: Seq[Expression], equalTo: Literal): Boolean = { | ||
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 +527,15 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper { | |
} else { | ||
e.copy(branches = branches.take(i).map(branch => (branch._1, elseValue))) | ||
} | ||
|
||
case EqualTo(i @ If(_, trueValue: Literal, falseValue: Literal), right: Literal) | ||
if i.deterministic && isAlwaysFalse(trueValue :: falseValue :: Nil, right) => | ||
FalseLiteral | ||
|
||
case EqualTo(c @ CaseWhen(branches, elseValue), right: Literal) if c.deterministic && | ||
(branches.map(_._2) ++ elseValue).forall(_.isInstanceOf[Literal]) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can do the "is literal check" inside |
||
isAlwaysFalse(branches.map(_._2) ++ elseValue, right) => | ||
FalseLiteral | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,4 +199,97 @@ 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make sure the test expression is valid. |
||
|
||
assertEquivalent(EqualTo(ifExp, Literal(4)), FalseLiteral) | ||
assertEquivalent(EqualTo(ifExp, Literal(3)), EqualTo(ifExp, Literal(3))) | ||
assertEquivalent(EqualTo(ifExp, Literal("4")), FalseLiteral) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto, let's don't test invalid expressions, |
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a case that we should optimize. But we need to distinguish between non-deterministic and has-side-effect. While we can skip running non-deterministic expressions, we can't skip running has-side-effect expressions. This is not related to this PR, but is something worth considering. cc @viirya @dbtsai @maropu @rednaxelafx |
||
assert(!nonDeterministic.deterministic) | ||
assertEquivalent(EqualTo(nonDeterministic, Literal(-1)), EqualTo(nonDeterministic, Literal(-1))) | ||
|
||
// null check, SPARK-33798 will not change these behaviors. | ||
assertEquivalent( | ||
EqualTo(If(FalseLiteral, Literal(null, IntegerType), Literal(1)), Literal(1)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The if condition is a literal, does it really go through the new branch? will it be optimized earlier by some other logics? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to still use |
||
TrueLiteral) | ||
assertEquivalent( | ||
EqualTo(If(TrueLiteral, Literal(null, IntegerType), Literal(1)), Literal(1)), | ||
Literal(null, BooleanType)) | ||
assertEquivalent( | ||
EqualTo(If(FalseLiteral, Literal(null, IntegerType), Literal(null, IntegerType)), Literal(1)), | ||
Literal(null, BooleanType)) | ||
|
||
assertEquivalent( | ||
EqualTo(If(FalseLiteral, Literal(1), Literal(2)), Literal(null, IntegerType)), | ||
Literal(null, BooleanType)) | ||
assertEquivalent( | ||
EqualTo(If(TrueLiteral, Literal(1), Literal(2)), Literal(null, IntegerType)), | ||
Literal(null, BooleanType)) | ||
wangyum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SPARK-33798 will change these behaviors as expected. |
||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. SPARK-33798 will not change these behaviors. |
||
} | ||
} |
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.
isAlwaysFalse
seems to be mismatched a little with the behavior of this function. Technically, this function resemblescontains
function whereexpr
is a list andequalTo
literal 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
notEqualForAll
instead ofisAlwaysFalse
?