Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: fix checking for presence of window functions in some edge cases #37130

Merged
merged 1 commit into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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