Skip to content

Commit

Permalink
sql: fix checking for presence of window functions in some edge cases
Browse files Browse the repository at this point in the history
Previously, when a window function was a part of another expression
(say BinaryExpr) instead of being used directly within PARTITION BY
and ORDER BY clauses of another window function, we would incorrectly
not reject such a query and would actually crash while trying to
execute it.

Release note: None
  • Loading branch information
yuzefovich committed Apr 25, 2019
1 parent 9e17cb9 commit 4cc02b7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 6 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/window
Original file line number Diff line number Diff line change
Expand Up @@ -3008,6 +3008,12 @@ SELECT sum(a) OVER (PARTITION BY count(a) OVER ()) FROM x
statement error window functions are not allowed in window definitions
SELECT sum(a) OVER (ORDER BY count(a) OVER ()) FROM x

statement error window functions are not allowed in window definitions
SELECT sum(a) OVER (PARTITION BY count(a) OVER () + 1) FROM x

statement error window functions are not allowed in window definitions
SELECT sum(a) OVER (ORDER BY count(a) OVER () + 1) FROM x

statement error more than one row returned by a subquery used as an expression
SELECT sum(a) OVER (PARTITION BY (SELECT count(*) FROM x GROUP BY a)) FROM x

Expand Down
14 changes: 6 additions & 8 deletions pkg/sql/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ func (n *windowNode) extractWindowFunctions(s *renderNode) error {
func (p *planner) constructWindowDefinitions(
ctx context.Context, n *windowNode, sc *tree.SelectClause, s *renderNode,
) error {
var containsWindowVisitor transform.ContainsWindowVisitor

// Process each named window specification on the select clause.
namedWindowSpecs := make(map[string]*tree.WindowDef, len(sc.Window))
for _, windowDef := range sc.Window {
Expand Down Expand Up @@ -224,10 +226,8 @@ func (p *planner) constructWindowDefinitions(

// Validate PARTITION BY clause.
for _, partition := range windowDef.Partitions {
if funcExpr, ok := partition.(*tree.FuncExpr); ok {
if funcExpr.IsWindowFunctionApplication() {
return pgerror.Newf(pgerror.CodeWindowingError, "window functions are not allowed in window definitions")
}
if containsWindowVisitor.ContainsWindowFunc(partition) {
return pgerror.Newf(pgerror.CodeWindowingError, "window functions are not allowed in window definitions")
}
cols, exprs, _, err := p.computeRenderAllowingStars(ctx,
tree.SelectExpr{Expr: partition}, types.Any, s.sourceInfo, s.ivarHelper,
Expand All @@ -242,10 +242,8 @@ func (p *planner) constructWindowDefinitions(

// Validate ORDER BY clause.
for _, orderBy := range windowDef.OrderBy {
if funcExpr, ok := orderBy.Expr.(*tree.FuncExpr); ok {
if funcExpr.IsWindowFunctionApplication() {
return pgerror.Newf(pgerror.CodeWindowingError, "window functions are not allowed in window definitions")
}
if containsWindowVisitor.ContainsWindowFunc(orderBy.Expr) {
return pgerror.Newf(pgerror.CodeWindowingError, "window functions are not allowed in window definitions")
}
cols, exprs, _, err := p.computeRenderAllowingStars(ctx,
tree.SelectExpr{Expr: orderBy.Expr}, types.Any, s.sourceInfo, s.ivarHelper,
Expand Down

0 comments on commit 4cc02b7

Please sign in to comment.