Github user chenghao-intel commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7446#discussion_r34855419
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
 ---
    @@ -513,18 +513,28 @@ object SimplifyFilters extends Rule[LogicalPlan] {
     object PushPredicateThroughProject extends Rule[LogicalPlan] {
       def apply(plan: LogicalPlan): LogicalPlan = plan transform {
         case filter @ Filter(condition, project @ Project(fields, grandChild)) 
=>
    -      val sourceAliases = fields.collect { case a @ Alias(c, _) =>
    -        (a.toAttribute: Attribute) -> c
    -      }.toMap
    -      project.copy(child = filter.copy(
    -        replaceAlias(condition, sourceAliases),
    -        grandChild))
    -  }
    +      // Create a map of Aliases to their values from the child projection.
    +      // e.g., 'SELECT a + b AS c, d ...' produces Map(c -> a + b).
    +      val aliasMap = AttributeMap(fields.collect {
    +        case a: Alias => (a.toAttribute, a.child)
    +      })
     
    -  private def replaceAlias(condition: Expression, sourceAliases: 
Map[Attribute, Expression]) = {
    -    condition transform {
    -      case a: AttributeReference => sourceAliases.getOrElse(a, a)
    -    }
    +      // We only push down filter if their overlapped expressions are all
    +      // deterministic.
    +      val hasNondeterministic = condition.collect {
    +        case a: Attribute if aliasMap.contains(a) => aliasMap(a)
    +      }.exists(_.find(!_.deterministic).isDefined)
    +
    +      if (hasNondeterministic) {
    +        filter
    --- End diff --
    
    We still can push down the deterministic part. For example:
    ```
    testRelation
      .where('a > 5)
      .select(Rand(10).as('rand), 'a)
      .where('rand > 5 and 'a > 8)
    ```
    ==>
    ```
    testRelation
      .where('a > 5)
      .where('a>8)
      .select(Rand(10).as('rand), 'a)
      .where('rand > 5)
    ```


---
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]

Reply via email to