-
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 |
|---|---|---|
|
|
@@ -22,9 +22,27 @@ import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLite | |
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.types.BooleanType | ||
| import org.apache.spark.util.Utils | ||
|
|
||
|
|
||
| /** | ||
| * A rule that converting conditional expressions to predicate expressions, if possible, in the | ||
| * search condition of the WHERE/HAVING/ON(JOIN) clauses, which contain an implicit Boolean operator | ||
| * "(search condition) = TRUE". After this converting, we can potentially push the filter down to | ||
| * the data source. | ||
| * | ||
| * Supported cases are: | ||
| * - IF(cond, trueVal, false) => AND(cond, trueVal) | ||
| * - IF(cond, trueVal, true) => OR(NOT(cond), trueVal) | ||
| * - IF(cond, false, falseVal) => AND(NOT(cond), elseVal) | ||
| * - IF(cond, true, falseVal) => OR(cond, elseVal) | ||
| * - CASE WHEN cond THEN trueVal ELSE false END => AND(cond, trueVal) | ||
| * - CASE WHEN cond THEN trueVal END => AND(cond, trueVal) | ||
| * - 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 true ELSE elseVal END => OR(cond, elseVal) | ||
| * - CASE WHEN cond THEN true END => OR(cond, false) | ||
| */ | ||
| object SimplifyConditionalsInPredicate extends Rule[LogicalPlan] { | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
|
|
@@ -35,31 +53,26 @@ object SimplifyConditionalsInPredicate extends Rule[LogicalPlan] { | |
| } | ||
|
|
||
| private def simplifyConditional(e: Expression): Expression = e match { | ||
| case Literal(null, BooleanType) => FalseLiteral | ||
| case And(left, right) => And(simplifyConditional(left), simplifyConditional(right)) | ||
| case Or(left, right) => Or(simplifyConditional(left), simplifyConditional(right)) | ||
| case If(cond, t, FalseLiteral) => And(cond, t) | ||
| case If(cond, t, TrueLiteral) => Or(Not(cond), t) | ||
| case If(cond, FalseLiteral, f) => And(Not(cond), f) | ||
| case If(cond, TrueLiteral, f) => Or(cond, f) | ||
| case If(cond, trueValue, FalseLiteral) => And(cond, trueValue) | ||
| case If(cond, trueValue, TrueLiteral) => Or(Not(cond), trueValue) | ||
| case If(cond, FalseLiteral, falseValue) => And(Not(cond), falseValue) | ||
| case If(cond, TrueLiteral, falseValue) => Or(cond, falseValue) | ||
| case CaseWhen(Seq((cond, trueValue)), | ||
| Some(FalseLiteral) | Some(Literal(null, BooleanType)) | None) => | ||
| 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(Literal(null, BooleanType))) | ||
| And(Not(cond), elseValue.getOrElse(FalseLiteral)) | ||
| case CaseWhen(Seq((cond, TrueLiteral)), elseValue) => | ||
| Or(cond, elseValue.getOrElse(Literal(null, BooleanType))) | ||
| Or(cond, elseValue.getOrElse(FalseLiteral)) | ||
| 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 => | ||
| val message = "Expected a Boolean type expression in simplifyConditional, " + | ||
| s"but got the type `${e.dataType.catalogString}` in `${e.sql}`." | ||
| if (Utils.isTesting) { | ||
| throw new IllegalArgumentException(message) | ||
| } else { | ||
| logWarning(message) | ||
| e | ||
| } | ||
| assert(e.dataType != BooleanType, | ||
| "Expected a Boolean type expression in simplifyConditional, " + | ||
| s"but got the type `${e.dataType.catalogString}` in `${e.sql}`.") | ||
| e | ||
| } | ||
| } | ||
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.
that converting->that converts