mgaido91 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_r311889119
##########
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:
IIRC, because attributes may be referenced by the intermediate filter and
project
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]