Skip to content

Commit 2a3480a

Browse files
committed
Fix window function parsing for named window references with clauses
- Fix OVER (name clauses...) to not return early after parsing the name - Add named window reference handling in WINDOW clause definitions (e.g., w1 AS (w0 ORDER BY ...)) Fixes 01591_window_functions (4 statements) and 02378_analyzer_projection_names.
1 parent 41994ce commit 2a3480a

4 files changed

Lines changed: 12 additions & 10 deletions

File tree

parser/expression.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -749,16 +749,16 @@ func (p *Parser) parseWindowSpec() *ast.WindowSpec {
749749
return spec
750750
}
751751

752-
// Check for named window reference inside parentheses: OVER (w0)
752+
// Check for named window reference inside parentheses: OVER (w0) or OVER (w0 ORDER BY ...)
753753
// This happens when the identifier is not a known clause keyword
754754
if p.currentIs(token.IDENT) {
755755
upper := strings.ToUpper(p.current.Value)
756756
// If it's not a window clause keyword, it's a named window reference
757757
if upper != "PARTITION" && upper != "ORDER" && upper != "ROWS" && upper != "RANGE" && upper != "GROUPS" {
758758
spec.Name = p.current.Value
759759
p.nextToken()
760-
p.expect(token.RPAREN)
761-
return spec
760+
// Don't return early - there may be more clauses after the window name
761+
// e.g., OVER (w1 ROWS UNBOUNDED PRECEDING)
762762
}
763763
}
764764

parser/parser.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6720,6 +6720,15 @@ func (p *Parser) parseWindowDefinitions() []*ast.WindowDefinition {
67206720
Position: p.current.Pos,
67216721
}
67226722

6723+
// Check for named window reference (e.g., w1 as (w0 ORDER BY ...))
6724+
if p.currentIs(token.IDENT) {
6725+
upper := strings.ToUpper(p.current.Value)
6726+
if upper != "PARTITION" && upper != "ORDER" && upper != "ROWS" && upper != "RANGE" && upper != "GROUPS" {
6727+
spec.Name = p.current.Value
6728+
p.nextToken()
6729+
}
6730+
}
6731+
67236732
// Parse PARTITION BY
67246733
if p.currentIs(token.PARTITION) {
67256734
p.nextToken()
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
{
2-
"explain_todo": {
3-
"stmt107": true,
4-
"stmt108": true,
5-
"stmt109": true,
6-
"stmt110": true
7-
}
82
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt185": true,
43
"stmt67": true
54
}
65
}

0 commit comments

Comments
 (0)