Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/11846#discussion_r56945839
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
---
@@ -659,6 +669,40 @@ class Analyzer(
}
Sort(newOrders, global, child)
+ // Replace the index with the corresponding expression in
aggregateExpressions. The index is
+ // a 1-base position of aggregateExpressions, which is output
columns (select expression)
+ case a @ Aggregate(groups, aggs, child)
+ if conf.groupByOrdinal && child.resolved &&
aggs.forall(_.resolved) &&
+ groups.exists(IntegerIndex.unapply(_).nonEmpty) =>
+ val newGroups = groups.map {
+ case IntegerIndex(index) if index > 0 && index <= aggs.size =>
+ aggs(index - 1) match {
+ case e if ResolveAggregateFunctions.containsAggregate(e) =>
+ throw new UnresolvedException(a,
+ s"Group by position: the '$index'th column in the select
contains an " +
+ s"aggregate function: ${e.sql}. Aggregate functions are
not allowed in GROUP BY")
+ // Group by clause is unable to use the alias defined in
aggregateExpressions
+ case Alias(c, _) => c
+ case o => o
+ }
+ case IntegerIndex(index) =>
+ throw new UnresolvedException(a,
+ s"Group by position: '$index' exceeds the size of the select
list '${aggs.size}'.")
+ case o => o
+ }
+ Aggregate(newGroups, aggs, child)
+ }
+ }
+
+ /**
+ * In many dialects of SQL it is valid to sort by attributes that are
not present in the SELECT
+ * clause. This rule detects such queries and adds the required
attributes to the original
+ * projection, so that they will be available during sorting. Another
projection is added to
+ * remove these attributes after sorting.
+ */
+ object ResolveSortReferences extends Rule[LogicalPlan] {
+ def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ case s: Sort if !s.child.resolved => s
--- End diff --
This case is no longer needed.
---
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]