-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-33861][SQL] Simplify conditional in predicate #30865
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 1 commit
f786f18
45fd7a5
448faf0
94551ea
c90227a
cccbaf1
8bd9ef9
878beb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -39,9 +39,9 @@ import org.apache.spark.sql.types.BooleanType | |||||||||
| * - CASE WHEN cond THEN trueVal ELSE null END => AND(cond, trueVal) | ||||||||||
| * - CASE WHEN cond THEN trueVal ELSE true END => OR(NOT(cond), trueVal) | ||||||||||
| * - CASE WHEN cond THEN false ELSE elseVal END => AND(NOT(cond), elseVal) | ||||||||||
| * - CASE WHEN cond THEN false END => AND(NOT(cond), false) | ||||||||||
| * - CASE WHEN cond THEN false END => false | ||||||||||
| * - CASE WHEN cond THEN true ELSE elseVal END => OR(cond, elseVal) | ||||||||||
| * - CASE WHEN cond THEN true END => OR(cond, false) | ||||||||||
| * - CASE WHEN cond THEN true END => cond | ||||||||||
| */ | ||||||||||
| object SimplifyConditionalsInPredicate extends Rule[LogicalPlan] { | ||||||||||
|
|
||||||||||
|
|
@@ -64,14 +64,18 @@ object SimplifyConditionalsInPredicate extends Rule[LogicalPlan] { | |||||||||
| And(cond, trueValue) | ||||||||||
| case CaseWhen(Seq((cond, trueValue)), Some(TrueLiteral)) => | ||||||||||
| Or(Not(cond), trueValue) | ||||||||||
| case CaseWhen(Seq((cond, FalseLiteral)), elseValue) => | ||||||||||
| And(Not(cond), elseValue.getOrElse(FalseLiteral)) | ||||||||||
| case CaseWhen(Seq((cond, TrueLiteral)), elseValue) => | ||||||||||
| Or(cond, elseValue.getOrElse(FalseLiteral)) | ||||||||||
| case CaseWhen(Seq((_, FalseLiteral)), Some(FalseLiteral) | None) => | ||||||||||
|
Contributor
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 skips evaluating the condition, and we should make sure the condition is deterministic. @wangyum can you fix it?
Member
Author
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. OK, will fix it later.
Member
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. and also it matters if
Member
Author
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. Do we need to make these condition deterministic? spark/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala Lines 295 to 298 in 26d8df3
Member
Author
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. |
||||||||||
| FalseLiteral | ||||||||||
| case CaseWhen(Seq((cond, FalseLiteral)), Some(elseValue)) => | ||||||||||
| And(Not(cond), elseValue) | ||||||||||
| case CaseWhen(Seq((cond, TrueLiteral)), Some(FalseLiteral) | None) => | ||||||||||
| cond | ||||||||||
| case CaseWhen(Seq((cond, TrueLiteral)), Some(elseValue)) => | ||||||||||
| Or(cond, elseValue) | ||||||||||
| case e if e.dataType == BooleanType => e | ||||||||||
|
Contributor
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 add an assert. The analyzer should guarantee it.
Contributor
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 mean something like
Contributor
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. It seems the added assert can never be triggered. |
||||||||||
| case e => | ||||||||||
| assert(e.dataType != BooleanType, | ||||||||||
| "Expected a Boolean type expression in simplifyConditional, " + | ||||||||||
| "Expected a Boolean type expression in SimplifyConditionalsInPredicate, " + | ||||||||||
| s"but got the type `${e.dataType.catalogString}` in `${e.sql}`.") | ||||||||||
| e | ||||||||||
| } | ||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.