Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 29.4k
[WIP][Spark-SQL] Optimize the Constant Folding for Expression#482
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
2645d4f3c045c7536c0059cf0396543ef9d9ccefdbb28e03a27ea3d780f9f1850444cc29c816668b9fad2f14b50File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -26,6 +26,7 @@ import org.apache.spark.sql.catalyst.types._ | ||
| object Optimizer extends RuleExecutor[LogicalPlan] { | ||
| val batches = | ||
| Batch("ConstantFolding", Once, | ||
| NullPropagation, | ||
| ConstantFolding, | ||
| BooleanSimplification, | ||
| SimplifyFilters, | ||
| @@ -85,6 +86,72 @@ object ColumnPruning extends Rule[LogicalPlan] { | ||
| } | ||
| } | ||
| /** | ||
| * Replaces [[catalyst.expressions.Expression Expressions]] that can be statically evaluated with | ||
| * equivalent [[catalyst.expressions.Literal Literal]] values. This rule is more specific with | ||
| * Null value propagation from bottom to top of the expression tree. | ||
| */ | ||
| object NullPropagation extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
| case q: LogicalPlan => q transformExpressionsUp { | ||
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. Do we want ContributorAuthor 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.
| ||
| case e @ Count(Literal(null, _)) => Literal(0, e.dataType) | ||
| case e @ Sum(Literal(c, _)) if c == 0 => Literal(0, e.dataType) | ||
| case e @ Average(Literal(c, _)) if c == 0 => Literal(0.0, e.dataType) | ||
| case e @ IsNull(c) if c.nullable == false => Literal(false, BooleanType) | ||
| case e @ IsNotNull(c) if c.nullable == false => Literal(true, BooleanType) | ||
| case e @ GetItem(Literal(null, _), _) => Literal(null, e.dataType) | ||
| case e @ GetItem(_, Literal(null, _)) => Literal(null, e.dataType) | ||
| case e @ GetField(Literal(null, _), _) => Literal(null, e.dataType) | ||
| case e @ Coalesce(children) => { | ||
| val newChildren = children.filter(c => c match { | ||
| case Literal(null, _) => false | ||
| case _ => true | ||
| }) | ||
| if (newChildren.length == 0) { | ||
| Literal(null, e.dataType) | ||
| } else if (newChildren.length == 1) { | ||
| newChildren(0) | ||
| } else { | ||
| Coalesce(newChildren) | ||
| } | ||
| } | ||
| case e @ If(Literal(v, _), trueValue, falseValue) => if (v == true) trueValue else falseValue | ||
| case e @ In(Literal(v, _), list) if (list.exists(c => c match { | ||
| case Literal(candidate, _) if candidate == v => true | ||
| case _ => false | ||
| })) => Literal(true, BooleanType) | ||
| case e: UnaryMinus => e.child match { | ||
| case Literal(null, _) => Literal(null, e.dataType) | ||
| case _ => e | ||
| } | ||
| case e: Cast => e.child match { | ||
| case Literal(null, _) => Literal(null, e.dataType) | ||
| case _ => e | ||
| } | ||
| case e: Not => e.child match { | ||
| case Literal(null, _) => Literal(null, e.dataType) | ||
| case _ => e | ||
| } | ||
| // Put exceptional cases above if any | ||
| case e: BinaryArithmetic => e.children match { | ||
| case Literal(null, _) :: right :: Nil => Literal(null, e.dataType) | ||
| case left :: Literal(null, _) :: Nil => Literal(null, e.dataType) | ||
| case _ => e | ||
| } | ||
| case e: BinaryComparison => e.children match { | ||
| case Literal(null, _) :: right :: Nil => Literal(null, e.dataType) | ||
| case left :: Literal(null, _) :: Nil => Literal(null, e.dataType) | ||
| case _ => e | ||
| } | ||
| case e: StringRegexExpression => e.children match { | ||
| case Literal(null, _) :: right :: Nil => Literal(null, e.dataType) | ||
| case left :: Literal(null, _) :: Nil => Literal(null, e.dataType) | ||
| case _ => e | ||
| } | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Replaces [[catalyst.expressions.Expression Expressions]] that can be statically evaluated with | ||
| * equivalent [[catalyst.expressions.Literal Literal]] values. | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
What is the rationale for changing all of these?
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.
I think "def boolean" / "def string" should return an Attribute with nullability = true, that does not harm for the correctness, otherwise, it may bring a wrong hint for the optimization of rule NullPropagation.
For example:
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.
Good point.