spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hvanhovell <...@git.apache.org>
Subject [GitHub] spark pull request #17993: [SPARK-20758][SQL] Add Constant propagation optim...
Date Tue, 16 May 2017 16:35:59 GMT
Github user hvanhovell commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17993#discussion_r116793889
  
    --- 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 --
    
    What happens if I do something stupid like `i = 1 and ((j = 1) = (j = i))`? I think `j
= 1` might replaced by `1 = 1`.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message