Github user marmbrus commented on the pull request:

    https://github.com/apache/spark/pull/1497#issuecomment-49837238
  
    Instead of creating `UnresolvedAttribute`s, we can just get the 
`AttributeReference` from the created `Alias` directly using `toAttribute`.  
Maybe like this?
    
    ```scala
      /**
       * This rule finds expressions in HAVING clause filters that depend on
       * unresolved attributes.  It pushes these expressions down to the 
underlying
       * aggregates and then projects them away above the filter.
       */
      object UnresolvedHavingClauseAttributes extends Rule[LogicalPlan] {
        def apply(plan: LogicalPlan): LogicalPlan = plan transformUp {
          case filter @ Filter(havingCondition, aggregate @ Aggregate(_, 
originalAggExprs, _))
              if !filter.resolved && aggregate.resolved && 
containsAggregate(havingCondition) => {
            val evaluatedCondition = Alias(havingCondition, "havingCondition")()
            val aggExprsWithHaving = evaluatedCondition +: originalAggExprs
            
            Project(aggregate.output,
              Filter(evaluatedCondition.toAttribute,
                aggregate.copy(aggregateExpressions = aggExprsWithHaving)))
          }
        }
    
        protected def containsAggregate(condition: Expression): Boolean =
          condition
            .collect { case ae: AggregateExpression => ae }
            .nonEmpty
      }
    ```


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

Reply via email to