WindowsSubstitution Logical Evaluation Rule¶
WindowsSubstitution is a logical evaluation rule (Rule[LogicalPlan]) that the Logical Analyzer uses to resolve (aka substitute) WithWindowDefinition unary logical operators with UnresolvedWindowExpression to their corresponding spark-sql-Expression-WindowExpression.md[WindowExpression] with resolved spark-sql-Expression-WindowSpecDefinition.md[WindowSpecDefinition].
WindowsSubstitution is part of Substitution fixed-point batch of rules.
NOTE: It appears that WindowsSubstitution is exclusively used for pure SQL queries because WithWindowDefinition.md[WithWindowDefinition] unary logical operator is created exclusively when AstBuilder WithWindowDefinition.md#creating-instance[parses window definitions].
If a window specification is not found, WindowsSubstitution fails analysis with the following error:
Window specification [windowName] is not defined in the WINDOW clause.
NOTE: The analysis failure is unlikely to happen given AstBuilder sql/AstBuilder.md#withWindows[builds a lookup table of all the named window specifications] defined in a SQL text and reports a ParseException when a WindowSpecReference is not available earlier.
For every WithWindowDefinition, WindowsSubstitution takes the child logical plan and transforms its UnresolvedWindowExpression expressions to be a spark-sql-Expression-WindowExpression.md[WindowExpression] with a window specification from the WINDOW clause (see spark-sql-Expression-WindowExpression.md#WithWindowDefinition-example[WithWindowDefinition Example]).