Github user gatorsmile commented on a diff in the pull request:
https://github.com/apache/spark/pull/10678#discussion_r49356534
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
---
@@ -521,16 +521,31 @@ class Analyzer(
*/
object ResolveSortReferences extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
- case s @ Sort(ordering, global, p @ Project(projectList, child))
- if !s.resolved && p.resolved =>
- val (newOrdering, missing) = resolveAndFindMissing(ordering, p,
child)
+ case s @ Sort(_, _, p @ Project(_, _)) if !s.resolved && p.resolved
=>
+ val (newOrdering, missing, newChild): (Seq[SortOrder],
Seq[Attribute], LogicalPlan) =
+ p.child match {
+ // Case 1: when WINDOW functions are used in the SELECT clause.
+ // Example: SELECT sum(col1) OVER() FROM table1 ORDER BY col2
+ case p1 @ Project(_, w @ Window(_, _, _, _, p2: Project)) =>
+ val (newOrdering, missingAttrs) =
resolveAndFindMissing(s.order, p2, p2.child)
+ (newOrdering, missingAttrs,
+ Project(p1.projectList ++ missingAttrs,
+ Window(w.projectList ++ missingAttrs,
+ w.windowExpressions, w.partitionSpec, w.orderSpec,
+ Project(p2.projectList ++ missingAttrs, p2.child))))
+ // Case 2: the other cases
+ // Example: SELECT col1 FROM table1 ORDER BY col2
+ case _ =>
+ val (newOrdering, missingAttrs) =
resolveAndFindMissing(s.order, p, p.child)
+ (newOrdering, missingAttrs, p.child)
+ }
// If this rule was not a no-op, return the transformed plan,
otherwise return the original.
if (missing.nonEmpty) {
// Add missing attributes and then project them away after the
sort.
Project(p.output,
- Sort(newOrdering, global,
- Project(projectList ++ missing, child)))
+ Sort(newOrdering, s.global,
--- End diff --
This is a good suggestion!
---
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]