viirya commented on a change in pull request #23701: [SPARK-26741][SQL] Allow using aggregate expressions in ORDER BY clause URL: https://github.com/apache/spark/pull/23701#discussion_r311321824
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala ########## @@ -1722,22 +1723,28 @@ class Analyzer( case ae: AnalysisException => f } - case sort @ Sort(sortOrder, global, aggregate: Aggregate) if aggregate.resolved => - + case sort @ Sort(sortOrder, global, child) + if child.resolved && relatedAggregate(child).isDefined => + // The Aggregate plan may be not a direct child of sort when there is a HAVING clause. + // In that case a `Filter` and/or a `Project` can be present between the `Sort` and the + // related `Aggregate`. + val aggregate = relatedAggregate(child).get // Try resolving the ordering as though it is in the aggregate clause. try { // If a sort order is unresolved, containing references not in aggregate, or containing // `AggregateExpression`, we need to push down it to the underlying aggregate operator. val unresolvedSortOrders = sortOrder.filter { s => - !s.resolved || !s.references.subsetOf(aggregate.outputSet) || containsAggregate(s) + !s.resolved || !s.references.subsetOf(child.outputSet) || containsAggregate(s) } - val aliasedOrdering = - unresolvedSortOrders.map(o => Alias(o.child, "aggOrder")()) - val aggregatedOrdering = aggregate.copy(aggregateExpressions = aliasedOrdering) + val namedExpressionsOrdering = + unresolvedSortOrders.map(_.child match { + case a: Attribute => a Review comment: This `Attribute` case is new. Previously we just alias aggregate expression, why need for attribute here? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org