Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/6816#discussion_r32391112
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -397,19 +398,31 @@ class Analyzer(
             }
           case s @ Sort(ordering, global, a @ Aggregate(grouping, aggs, child))
               if !s.resolved && a.resolved =>
    -        val unresolved = ordering.flatMap(_.collect { case 
UnresolvedAttribute(name) => name })
             // A small hack to create an object that will allow us to resolve 
any references that
             // refer to named expressions that are present in the grouping 
expressions.
             val groupingRelation = LocalRelation(
               grouping.collect { case ne: NamedExpression => ne.toAttribute }
             )
     
    -        val (resolvedOrdering, missing) = resolveAndFindMissing(ordering, 
a, groupingRelation)
    +        // Find sort attributes that are projected away so we can 
temporarily add them back in.
    +        val (resolvedOrdering, unresolved) = 
resolveAndFindMissing(ordering, a, groupingRelation)
    +
    +        // Find aggregate expressions and evaluate them early, since they 
can't be evaluated in a
    +        // Sort.
    +        val (withAggsRemoved, aliasedAggregateList) = resolvedOrdering.map 
{
    +          case aggOrdering if aggOrdering.collect { case a: 
AggregateExpression => a }.nonEmpty =>
    +            val aliased = Alias(aggOrdering.child, "_aggOrdering")()
    --- End diff --
    
    For something like `SELECT a, sum(b) FROM orderByData GROUP BY a ORDER BY 
sum(b)`, we don't need to add a extra projection. Should we handle this case?


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