@@ -57,6 +57,7 @@ class Analyzer(
5757 lazy val batches : Seq [Batch ] = Seq (
5858 Batch (" Substitution" , fixedPoint,
5959 CTESubstitution ::
60+ WindowsSubstitution ::
6061 Nil : _* ),
6162 Batch (" Resolution" , fixedPoint,
6263 ResolveRelations ::
@@ -101,6 +102,28 @@ class Analyzer(
101102 }
102103 }
103104
105+ /**
106+ * Substitute child plan with WindowSpecDefinitions.
107+ */
108+ object WindowsSubstitution extends Rule [LogicalPlan ] {
109+ def apply (plan : LogicalPlan ): LogicalPlan = plan transform {
110+ // Lookup WindowSpecDefinitions. This rule works with unresolved children.
111+ case WithWindowDefinition (windowDefinitions, child) =>
112+ child.transform {
113+ case plan => plan.transformExpressions {
114+ case UnresolvedWindowExpression (c, WindowSpecReference (windowName)) =>
115+ val errorMessage =
116+ s " Window specification $windowName is not defined in the WINDOW clause. "
117+ val windowSpecDefinition =
118+ windowDefinitions
119+ .get(windowName)
120+ .getOrElse(failAnalysis(errorMessage))
121+ WindowExpression (c, windowSpecDefinition)
122+ }
123+ }
124+ }
125+ }
126+
104127 /**
105128 * Removes no-op Alias expressions from the plan.
106129 */
@@ -678,21 +701,6 @@ class Analyzer(
678701 // We have to use transformDown at here to make sure the rule of
679702 // "Aggregate with Having clause" will be triggered.
680703 def apply (plan : LogicalPlan ): LogicalPlan = plan transformDown {
681- // Lookup WindowSpecDefinitions. This rule works with unresolved children.
682- case WithWindowDefinition (windowDefinitions, child) =>
683- child.transform {
684- case plan => plan.transformExpressions {
685- case UnresolvedWindowExpression (c, WindowSpecReference (windowName)) =>
686- val errorMessage =
687- s " Window specification $windowName is not defined in the WINDOW clause. "
688- val windowSpecDefinition =
689- windowDefinitions
690- .get(windowName)
691- .getOrElse(failAnalysis(errorMessage))
692- WindowExpression (c, windowSpecDefinition)
693- }
694- }
695-
696704 // Aggregate with Having clause. This rule works with an unresolved Aggregate because
697705 // a resolved Aggregate will not have Window Functions.
698706 case f @ Filter (condition, a @ Aggregate (groupingExprs, aggregateExprs, child))
0 commit comments