-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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-13658][SQL] BooleanSimplification rule is slow with large boolean expressions #11647
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,7 +152,10 @@ abstract class Expression extends TreeNode[Expression] { | |
* `deterministic` expressions where `this.canonicalized == other.canonicalized` will always | ||
* evaluate to the same result. | ||
*/ | ||
lazy val canonicalized: Expression = Canonicalize.execute(this) | ||
lazy val canonicalized: Expression = { | ||
val canonicalizedChildred = children.map(_.canonicalized) | ||
Canonicalize.execute(withNewChildren(canonicalizedChildred)) | ||
} | ||
|
||
/** | ||
* Returns true when two expressions will always compute the same result, even if they differ | ||
|
@@ -161,7 +164,7 @@ abstract class Expression extends TreeNode[Expression] { | |
* See [[Canonicalize]] for more details. | ||
*/ | ||
def semanticEquals(other: Expression): Boolean = | ||
deterministic && other.deterministic && canonicalized == other.canonicalized | ||
deterministic && other.deterministic && canonicalized == other.canonicalized | ||
|
||
/** | ||
* Returns a `hashCode` for the calculation performed by this expression. Unlike the standard | ||
|
@@ -598,3 +601,61 @@ abstract class TernaryExpression extends Expression { | |
} | ||
} | ||
} | ||
|
||
/** | ||
* Rewrites an expression using rules that are guaranteed preserve the result while attempting | ||
* to remove cosmetic variations. Deterministic expressions that are `equal` after canonicalization | ||
* will always return the same answer given the same input (i.e. false positives should not be | ||
* possible). However, it is possible that two canonical expressions that are not equal will in fact | ||
* return the same answer given any input (i.e. false negatives are possible). | ||
* | ||
* The following rules are applied: | ||
* - Names and nullability hints for [[org.apache.spark.sql.types.DataType]]s are stripped. | ||
* - Commutative and associative operations ([[Add]] and [[Multiply]]) have their children ordered | ||
* by `hashCode`. | ||
* - [[EqualTo]] and [[EqualNullSafe]] are reordered by `hashCode`. | ||
* - Other comparisons ([[GreaterThan]], [[LessThan]]) are reversed by `hashCode`. | ||
*/ | ||
object Canonicalize extends { | ||
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. fwiw, if you are keeping this class, i think we should just have it in its own file. The Expression.scala file is getting pretty long. 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. done. |
||
def execute(e: Expression): Expression = { | ||
expressionReorder(ignoreNamesTypes(e)) | ||
} | ||
|
||
/** Remove names and nullability from types. */ | ||
private def ignoreNamesTypes(e: Expression): Expression = e match { | ||
case a: AttributeReference => | ||
AttributeReference("none", a.dataType.asNullable)(exprId = a.exprId) | ||
case _ => e | ||
} | ||
|
||
/** Collects adjacent commutative operations. */ | ||
private def gatherCommutative( | ||
e: Expression, | ||
f: PartialFunction[Expression, Seq[Expression]]): Seq[Expression] = e match { | ||
case c if f.isDefinedAt(c) => f(c).flatMap(gatherCommutative(_, f)) | ||
case other => other :: Nil | ||
} | ||
|
||
/** Orders a set of commutative operations by their hash code. */ | ||
private def orderCommutative( | ||
e: Expression, | ||
f: PartialFunction[Expression, Seq[Expression]]): Seq[Expression] = | ||
gatherCommutative(e, f).sortBy(_.hashCode()) | ||
|
||
/** Rearrange expressions that are commutative or associative. */ | ||
private def expressionReorder(e: Expression): Expression = e match { | ||
case a: Add => orderCommutative(a, { case Add(l, r) => Seq(l, r) }).reduce(Add) | ||
case m: Multiply => orderCommutative(m, { case Multiply(l, r) => Seq(l, r) }).reduce(Multiply) | ||
|
||
case EqualTo(l, r) if l.hashCode() > r.hashCode() => EqualTo(r, l) | ||
case EqualNullSafe(l, r) if l.hashCode() > r.hashCode() => EqualNullSafe(r, l) | ||
|
||
case GreaterThan(l, r) if l.hashCode() > r.hashCode() => LessThan(r, l) | ||
case LessThan(l, r) if l.hashCode() > r.hashCode() => GreaterThan(r, l) | ||
|
||
case GreaterThanOrEqual(l, r) if l.hashCode() > r.hashCode() => LessThanOrEqual(r, l) | ||
case LessThanOrEqual(l, r) if l.hashCode() > r.hashCode() => GreaterThanOrEqual(r, l) | ||
|
||
case _ => 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.
nit: canonicalizedChildred -> canonicalizedChildren
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.
Ah, thanks. Since this is merged and it is small, I won't send a PR just for this, but I will include this fixing in other PR.