Github user tejasapatil commented on a diff in the pull request:
https://github.com/apache/spark/pull/17993#discussion_r117618800
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala
---
@@ -54,6 +54,59 @@ object ConstantFolding extends Rule[LogicalPlan] {
}
}
+/**
+ * Substitutes [[Attribute Attributes]] which can be statically evaluated
with their corresponding
+ * value in conjunctive [[Expression Expressions]]
+ * eg.
+ * {{{
+ * SELECT * FROM table WHERE i = 5 AND j = i + 3
+ * ==> SELECT * FROM table WHERE i = 5 AND j = 8
+ * }}}
+ */
+object ConstantPropagation extends Rule[LogicalPlan] with PredicateHelper {
+
+ def containsNonConjunctionPredicates(expression: Expression): Boolean =
expression match {
+ case Not(_) => true
+ case Or(_, _) => true
+ case _ =>
+ var result = false
+ expression.children.foreach {
+ case Not(_) => result = true
+ case Or(_, _) => result = true
+ case other => result = result ||
containsNonConjunctionPredicates(other)
+ }
+ result
+ }
+
+ def apply(plan: LogicalPlan): LogicalPlan = plan transform {
+ case q: LogicalPlan => q transformExpressionsUp {
+ case and @ (left And right)
+ if !containsNonConjunctionPredicates(left) &&
!containsNonConjunctionPredicates(right) =>
+
+ val leftEntries = left.collect {
+ case e @ EqualTo(left: AttributeReference, right: Literal) =>
((left, right), e)
+ case e @ EqualTo(left: Literal, right: AttributeReference) =>
((right, left), e)
+ }
+ val rightEntries = right.collect {
+ case e @ EqualTo(left: AttributeReference, right: Literal) =>
((left, right), e)
+ case e @ EqualTo(left: Literal, right: AttributeReference) =>
((right, left), e)
+ }
+ val constantsMap = AttributeMap(leftEntries.map(_._1) ++
rightEntries.map(_._1))
+ val predicates = (leftEntries.map(_._2) ++
rightEntries.map(_._2)).toSet
+
+ def replaceConstants(expression: Expression) = expression
transform {
+ case a: AttributeReference if constantsMap.contains(a) =>
--- End diff --
Nice catch !!! I changed the logic to handle that.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]