Skip to content
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 parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4810,6 +4810,7 @@ func (l *LimitByClause) Accept(visitor ASTVisitor) error {
type WindowExpr struct {
LeftParenPos Pos
RightParenPos Pos
WindowName *Ident
PartitionBy *PartitionByClause
OrderBy *OrderByClause
Frame *WindowFrameClause
Expand All @@ -4826,6 +4827,11 @@ func (w *WindowExpr) End() Pos {
func (w *WindowExpr) Accept(visitor ASTVisitor) error {
visitor.Enter(w)
defer visitor.Leave(w)
if w.WindowName != nil {
if err := w.WindowName.Accept(visitor); err != nil {
return err
}
}
if w.PartitionBy != nil {
if err := w.PartitionBy.Accept(visitor); err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions parser/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2755,7 +2755,14 @@ func (w *WindowClause) FormatSQL(formatter *Formatter) {
func (w *WindowExpr) FormatSQL(formatter *Formatter) {
formatter.WriteByte('(')
hasPart := false
if w.WindowName != nil {
formatter.WriteExpr(w.WindowName)
hasPart = true
}
if w.PartitionBy != nil {
if hasPart {
formatter.WriteByte(whitespace)
}
formatter.WriteExpr(w.PartitionBy)
hasPart = true
}
Expand Down
37 changes: 37 additions & 0 deletions parser/parser_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,14 @@ func (p *Parser) parseWindowCondition(pos Pos) (*WindowExpr, error) {
if err := p.expectTokenKind(TokenKindLParen); err != nil {
return nil, err
}
var windowName *Ident
if p.canParseWindowNameInParens() {
var err error
windowName, err = p.parseIdent()
if err != nil {
return nil, err
}
}
partitionBy, err := p.tryParsePartitionByClause(pos)
if err != nil {
return nil, err
Expand All @@ -843,12 +851,41 @@ func (p *Parser) parseWindowCondition(pos Pos) (*WindowExpr, error) {
return &WindowExpr{
LeftParenPos: pos,
RightParenPos: rightParenPos,
WindowName: windowName,
PartitionBy: partitionBy,
OrderBy: orderBy,
Frame: frame,
}, nil
}

func (p *Parser) canParseWindowNameInParens() bool {
if !p.matchTokenKind(TokenKindIdent) {
return false
}
if !p.matchTokenKind(TokenKindKeyword) {
return true
}

savedState := p.lexer.saveState()
defer p.lexer.restoreState(savedState)

switch {
case p.matchKeyword(KeywordPartition), p.matchKeyword(KeywordOrder):
_ = p.lexer.consumeToken()
return !p.matchKeyword(KeywordBy)
case p.matchKeyword(KeywordRows), p.matchKeyword(KeywordRange):
_ = p.lexer.consumeToken()
return !p.matchKeyword(KeywordBetween) &&
!p.matchKeyword(KeywordCurrent) &&
!p.matchKeyword(KeywordUnbounded) &&
!p.matchTokenKind(TokenKindInt) &&
!p.matchTokenKind(TokenKindLBrace) &&
!p.matchKeyword(KeywordInterval)
default:
return true
}
}

func (p *Parser) parseWindowClause(pos Pos) (*WindowClause, error) {
if err := p.expectKeyword(KeywordWindow); err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@
"OverExpr": {
"LeftParenPos": 306,
"RightParenPos": 347,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 306,
"Expr": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Origin SQL:
SELECT sum(x) OVER (order) AS sum_over_order
FROM t
WINDOW order AS (PARTITION BY team ORDER BY ts);


-- Beautify SQL:
SELECT
sum(x) OVER (order) AS sum_over_order
FROM
t
WINDOW order AS (PARTITION BY team ORDER BY
ts);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Origin SQL:
SELECT sum(x) OVER (w) AS sum_over_w
FROM t
WINDOW w AS (PARTITION BY y ORDER BY x);


-- Beautify SQL:
SELECT
sum(x) OVER (w) AS sum_over_w
FROM
t
WINDOW w AS (PARTITION BY y ORDER BY
x);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Origin SQL:
SELECT sum(x) OVER (w1 ORDER BY ts ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS rolling_sum,
avg(x) OVER (w2) AS avg_over_w2
FROM t
WINDOW w1 AS (PARTITION BY team),
w2 AS (w1 ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);


-- Beautify SQL:
SELECT
sum(x) OVER (w1 ORDER BY
ts ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS rolling_sum,
avg(x) OVER (w2) AS avg_over_w2
FROM
t
WINDOW w1 AS (PARTITION BY team), w2 AS (w1 ORDER BY
ts ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Origin SQL:
SELECT sum(x) OVER (order) AS sum_over_order
FROM t
WINDOW order AS (PARTITION BY team ORDER BY ts);


-- Format SQL:
SELECT sum(x) OVER (order) AS sum_over_order FROM t WINDOW order AS (PARTITION BY team ORDER BY ts);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Origin SQL:
SELECT sum(x) OVER (w) AS sum_over_w
FROM t
WINDOW w AS (PARTITION BY y ORDER BY x);


-- Format SQL:
SELECT sum(x) OVER (w) AS sum_over_w FROM t WINDOW w AS (PARTITION BY y ORDER BY x);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Origin SQL:
SELECT sum(x) OVER (w1 ORDER BY ts ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS rolling_sum,
avg(x) OVER (w2) AS avg_over_w2
FROM t
WINDOW w1 AS (PARTITION BY team),
w2 AS (w1 ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);


-- Format SQL:
SELECT sum(x) OVER (w1 ORDER BY ts ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS rolling_sum, avg(x) OVER (w2) AS avg_over_w2 FROM t WINDOW w1 AS (PARTITION BY team), w2 AS (w1 ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"OverExpr": {
"LeftParenPos": 105,
"RightParenPos": 238,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 105,
"Expr": {
Expand Down
1 change: 1 addition & 0 deletions parser/testdata/query/output/select_simple.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"OverExpr": {
"LeftParenPos": 57,
"RightParenPos": 89,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 57,
"Expr": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"OverExpr": {
"LeftParenPos": 212,
"RightParenPos": 272,
"WindowName": null,
"PartitionBy": null,
"OrderBy": {
"OrderPos": 213,
Expand Down Expand Up @@ -124,6 +125,7 @@
"OverExpr": {
"LeftParenPos": 356,
"RightParenPos": 424,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 356,
"Expr": {
Expand Down Expand Up @@ -356,6 +358,7 @@
"OverExpr": {
"LeftParenPos": 965,
"RightParenPos": 983,
"WindowName": null,
"PartitionBy": null,
"OrderBy": null,
"Frame": {
Expand Down Expand Up @@ -417,6 +420,7 @@
"OverExpr": {
"LeftParenPos": 1105,
"RightParenPos": 1154,
"WindowName": null,
"PartitionBy": null,
"OrderBy": null,
"Frame": {
Expand Down Expand Up @@ -481,6 +485,7 @@
"OverExpr": {
"LeftParenPos": 1259,
"RightParenPos": 1300,
"WindowName": null,
"PartitionBy": null,
"OrderBy": null,
"Frame": {
Expand Down Expand Up @@ -556,6 +561,7 @@
"OverExpr": {
"LeftParenPos": 1392,
"RightParenPos": 1435,
"WindowName": null,
"PartitionBy": null,
"OrderBy": null,
"Frame": {
Expand Down Expand Up @@ -625,6 +631,7 @@
"OverExpr": {
"LeftParenPos": 1528,
"RightParenPos": 1578,
"WindowName": null,
"PartitionBy": null,
"OrderBy": null,
"Frame": {
Expand Down Expand Up @@ -679,6 +686,7 @@
"OverExpr": {
"LeftParenPos": 1708,
"RightParenPos": 1734,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 1708,
"Expr": {
Expand Down Expand Up @@ -753,6 +761,7 @@
"OverExpr": {
"LeftParenPos": 1832,
"RightParenPos": 1858,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 1832,
"Expr": {
Expand Down Expand Up @@ -827,6 +836,7 @@
"OverExpr": {
"LeftParenPos": 1969,
"RightParenPos": 1995,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 1969,
"Expr": {
Expand Down Expand Up @@ -911,6 +921,7 @@
"OverExpr": {
"LeftParenPos": 2124,
"RightParenPos": 2207,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 2124,
"Expr": {
Expand Down Expand Up @@ -1012,6 +1023,7 @@
"OverExpr": {
"LeftParenPos": 2273,
"RightParenPos": 2356,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 2273,
"Expr": {
Expand Down Expand Up @@ -1122,6 +1134,7 @@
"OverExpr": {
"LeftParenPos": 2403,
"RightParenPos": 2429,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 2403,
"Expr": {
Expand Down Expand Up @@ -1215,6 +1228,7 @@
"OverExpr": {
"LeftParenPos": 2533,
"RightParenPos": 2559,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 2533,
"Expr": {
Expand Down Expand Up @@ -1289,6 +1303,7 @@
"OverExpr": {
"LeftParenPos": 2666,
"RightParenPos": 2692,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 2666,
"Expr": {
Expand Down Expand Up @@ -1504,6 +1519,7 @@
"OverExpr": {
"LeftParenPos": 3300,
"RightParenPos": 3355,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 3300,
"Expr": {
Expand Down Expand Up @@ -1618,6 +1634,7 @@
"OverExpr": {
"LeftParenPos": 3440,
"RightParenPos": 3533,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 3440,
"Expr": {
Expand Down Expand Up @@ -1739,6 +1756,7 @@
"OverExpr": {
"LeftParenPos": 3629,
"RightParenPos": 3732,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 3629,
"Expr": {
Expand Down Expand Up @@ -1875,6 +1893,7 @@
"OverExpr": {
"LeftParenPos": 3779,
"RightParenPos": 3859,
"WindowName": null,
"PartitionBy": null,
"OrderBy": {
"OrderPos": 3780,
Expand Down Expand Up @@ -1987,6 +2006,7 @@
"Expr": {
"LeftParenPos": 3917,
"RightParenPos": 3928,
"WindowName": null,
"PartitionBy": null,
"OrderBy": {
"OrderPos": 3918,
Expand Down Expand Up @@ -2021,6 +2041,7 @@
"Expr": {
"LeftParenPos": 3944,
"RightParenPos": 4019,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 3944,
"Expr": {
Expand Down Expand Up @@ -2089,6 +2110,7 @@
"Expr": {
"LeftParenPos": 4035,
"RightParenPos": 4102,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 4035,
"Expr": {
Expand Down Expand Up @@ -2162,6 +2184,7 @@
"Expr": {
"LeftParenPos": 4118,
"RightParenPos": 4194,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 4118,
"Expr": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
"OverExpr": {
"LeftParenPos": 364,
"RightParenPos": 413,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 364,
"Expr": {
Expand Down Expand Up @@ -448,6 +449,7 @@
"OverExpr": {
"LeftParenPos": 541,
"RightParenPos": 667,
"WindowName": null,
"PartitionBy": {
"PartitionPos": 541,
"Expr": {
Expand Down
Loading
Loading