Github user marmbrus commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8231#discussion_r37222545
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -463,12 +463,23 @@ class Analyzer(
             // Find sort attributes that are projected away so we can 
temporarily add them back in.
             val (newOrdering, missingAttr) = resolveAndFindMissing(ordering, 
a, groupingRelation)
     
    +        val existingNames = aggs.map(_.name)
    +        val prefix = {
    +          var init = "_aggOrdering"
    +          while (existingNames.exists(_.startsWith(init))) {
    +            init = "_" + init
    +          }
    +          init
    +        }
    +        var i = 0
             // Find aggregate expressions and evaluate them early, since they 
can't be evaluated in a
             // Sort.
             val (withAggsRemoved, aliasedAggregateList) = newOrdering.map {
    -          case aggOrdering if aggOrdering.collect { case a: 
AggregateExpression => a }.nonEmpty =>
    -            val aliased = Alias(aggOrdering.child, "_aggOrdering")()
    -            (aggOrdering.copy(child = aliased.toAttribute), Some(aliased))
    +          case aggOrdering if 
aggOrdering.find(_.isInstanceOf[AggregateExpression]).isDefined =>
    +            val aliasName = prefix + i
    +            i += 1
    +            val aliased = Alias(aggOrdering.child, aliasName)()
    +            (aggOrdering.copy(child = UnresolvedAttribute(aliasName)), 
Some(aliased))
    --- End diff --
    
    I would really like to avoid needing to make these names unique since that 
is redundant with expression IDs.  Instead I think we should add a field 
`resolvable` to `Attribute` and set it to false for any constructed aliases.  
We can then skip these attributes when doing resolution.


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