Skip to content

Commit

Permalink
Merge pull request #2252 from taozhi8833998/feat-window-range-snowflake
Browse files Browse the repository at this point in the history
feat: support range expr as window frame in snowflake
  • Loading branch information
taozhi8833998 authored Dec 5, 2024
2 parents 3667a44 + 7912fd5 commit dbe81e6
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 98 deletions.
22 changes: 16 additions & 6 deletions pegjs/athena.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,10 +1320,21 @@ window_specification_frameless

window_frame_clause
= kw:KW_ROWS __ s:(window_frame_following / window_frame_preceding) {
return `rows ${s.value}`
return {
type: 'rows',
expr: s
}
}
/ KW_ROWS __ KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
return `rows between ${p.value} and ${f.value}`
/ KW_ROWS __ op:KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
const left = {
type: 'origin',
value: 'rows',
}
const right = {
type: 'expr_list',
value: [p, f]
}
return createBinaryExpr(op, left, right)
}

window_frame_following
Expand All @@ -1344,14 +1355,13 @@ window_frame_preceding

window_frame_current_row
= 'CURRENT'i __ 'ROW'i {
// => { type: 'single_quote_string'; value: string }
return { type: 'single_quote_string', value: 'current row' }
return { type: 'origin', value: 'current row' }
}

window_frame_value
= s:'UNBOUNDED'i {
// => literal_string
return { type: 'single_quote_string', value: s.toUpperCase() }
return { type: 'origin', value: s.toUpperCase() }
}
/ literal_numeric

Expand Down
30 changes: 17 additions & 13 deletions pegjs/bigquery.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2022,16 +2022,22 @@ window_specification
}

window_frame_clause
= 'RANGE'i __ KW_BETWEEN 'UNBOUNDED'i __ 'PRECEDING'i __ KW_AND __ 'CURRENT'i __ 'ROW' {
return 'range between unbounded preceding and current row'
}
/ kw:KW_ROWS __ s:(window_frame_following / window_frame_preceding) {
// => string
return `rows ${s.value}`
= kw:KW_ROWS __ s:(window_frame_following / window_frame_preceding) {
return {
type: 'rows',
expr: s
}
}
/ KW_ROWS __ KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
// => string
return `rows between ${p.value} and ${f.value}`
/ k:(KW_ROWS / 'RANGE'i) __ op:KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
const left = {
type: 'origin',
value: k.toLowerCase(),
}
const right = {
type: 'expr_list',
value: [p, f]
}
return createBinaryExpr(op, left, right)
}

window_frame_following
Expand All @@ -2052,14 +2058,12 @@ window_frame_preceding

window_frame_current_row
= 'CURRENT'i __ 'ROW'i {
// => { type: 'single_quote_string'; value: string }
return { type: 'single_quote_string', value: 'current row', ...getLocationObject() }
return { type: 'origin', value: 'current row', ...getLocationObject() }
}

window_frame_value
= s:'UNBOUNDED'i {
// => literal_string
return { type: 'single_quote_string', value: s.toUpperCase(), ...getLocationObject() }
return { type: 'origin', value: s.toUpperCase(), ...getLocationObject() }
}
/ literal_numeric

Expand Down
23 changes: 16 additions & 7 deletions pegjs/hive.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,10 +1297,21 @@ window_specification_frameless

window_frame_clause
= kw:KW_ROWS __ s:(window_frame_following / window_frame_preceding) {
return `rows ${s.value}`
return {
type: 'rows',
expr: s
}
}
/ KW_ROWS __ KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
return `rows between ${p.value} and ${f.value}`
/ KW_ROWS __ op:KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
const left = {
type: 'origin',
value: 'rows',
}
const right = {
type: 'expr_list',
value: [p, f]
}
return createBinaryExpr(op, left, right)
}

window_frame_following
Expand All @@ -1321,14 +1332,12 @@ window_frame_preceding

window_frame_current_row
= 'CURRENT'i __ 'ROW'i {
// => { type: 'single_quote_string'; value: string }
return { type: 'single_quote_string', value: 'current row' }
return { type: 'origin', value: 'current row' }
}

window_frame_value
= s:'UNBOUNDED'i {
// => literal_string
return { type: 'single_quote_string', value: s.toUpperCase() }
return { type: 'origin', value: s.toUpperCase() }
}
/ literal_numeric

Expand Down
25 changes: 16 additions & 9 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3098,12 +3098,21 @@ window_specification_frameless

window_frame_clause
= kw:KW_ROWS __ s:(window_frame_following / window_frame_preceding) {
// => string
return `rows ${s.value}`
return {
type: 'rows',
expr: s
}
}
/ KW_ROWS __ KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
// => string
return `rows between ${p.value} and ${f.value}`
/ KW_ROWS __ op:KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
const left = {
type: 'origin',
value: 'rows',
}
const right = {
type: 'expr_list',
value: [p, f]
}
return createBinaryExpr(op, left, right)
}

window_frame_following
Expand All @@ -3124,14 +3133,12 @@ window_frame_preceding

window_frame_current_row
= 'CURRENT'i __ 'ROW'i {
// => { type: 'single_quote_string'; value: string }
return { type: 'single_quote_string', value: 'current row' }
return { type: 'origin', value: 'current row' }
}

window_frame_value
= s:'UNBOUNDED'i {
// => literal_string
return { type: 'single_quote_string', value: s.toUpperCase() }
return { type: 'origin', value: s.toUpperCase() }
}
/ literal_numeric

Expand Down
23 changes: 16 additions & 7 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3388,12 +3388,21 @@ window_specification_frameless

window_frame_clause
= kw:KW_ROWS __ s:(window_frame_following / window_frame_preceding) {
// => string
return `rows ${s.value}`
return {
type: 'rows',
expr: s
}
}
/ KW_ROWS __ KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
// => string
return `rows between ${p.value} and ${f.value}`
/ KW_ROWS __ op:KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
const left = {
type: 'origin',
value: 'rows',
}
const right = {
type: 'expr_list',
value: [p, f]
}
return createBinaryExpr(op, left, right)
}

window_frame_following
Expand All @@ -3414,12 +3423,12 @@ window_frame_preceding

window_frame_current_row
= 'CURRENT'i __ 'ROW'i {
return { type: 'single_quote_string', value: 'current row', ...getLocationObject() }
return { type: 'origin', value: 'current row', ...getLocationObject() }
}

window_frame_value
= s:'UNBOUNDED'i {
return { type: 'single_quote_string', value: s.toUpperCase(), ...getLocationObject() }
return { type: 'origin', value: s.toUpperCase(), ...getLocationObject() }
}
/ literal_numeric

Expand Down
24 changes: 16 additions & 8 deletions pegjs/noql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3224,12 +3224,21 @@ window_specification_frameless

window_frame_clause
= kw:KW_ROWS __ s:(window_frame_following / window_frame_preceding) {
// => string
return `rows ${s.value}`
return {
type: 'rows',
expr: s
}
}
/ KW_ROWS __ KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
// => string
return `rows between ${p.value} and ${f.value}`
/ KW_ROWS __ op:KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
const left = {
type: 'origin',
value: 'rows',
}
const right = {
type: 'expr_list',
value: [p, f]
}
return createBinaryExpr(op, left, right)
}

window_frame_following
Expand All @@ -3250,14 +3259,13 @@ window_frame_preceding

window_frame_current_row
= 'CURRENT'i __ 'ROW'i {
// => { type: 'single_quote_string'; value: string }
return { type: 'single_quote_string', value: 'current row' }
return { type: 'origin', value: 'current row' }
}

window_frame_value
= s:'UNBOUNDED'i {
// => literal_string
return { type: 'single_quote_string', value: s.toUpperCase() }
return { type: 'origin', value: s.toUpperCase() }
}
/ literal_numeric

Expand Down
29 changes: 20 additions & 9 deletions pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3758,12 +3758,23 @@ window_specification_frameless

window_frame_clause
= kw:KW_ROWS __ s:(window_frame_following / window_frame_preceding) {
// => string
return `rows ${s.value}`
// => { type: 'row'; expr: window_frame_following / window_frame_preceding }
return {
type: 'rows',
expr: s
}
}
/ KW_ROWS __ KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
// => string
return `rows between ${p.value} and ${f.value}`
/ KW_ROWS __ op:KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
// => binary_expr
const left = {
type: 'origin',
value: 'rows',
}
const right = {
type: 'expr_list',
value: [p, f]
}
return createBinaryExpr(op, left, right)
}

window_frame_following
Expand All @@ -3784,14 +3795,14 @@ window_frame_preceding

window_frame_current_row
= 'CURRENT'i __ 'ROW'i {
// => { type: 'single_quote_string'; value: string }
return { type: 'single_quote_string', value: 'current row' }
// => { type: 'origin'; value: string }
return { type: 'origin', value: 'current row' }
}

window_frame_value
= s:'UNBOUNDED'i {
// => literal_string
return { type: 'single_quote_string', value: s.toUpperCase() }
// => { type: 'origin'; value: string }
return { type: 'origin', value: s.toUpperCase() }
}
/ literal_numeric

Expand Down
25 changes: 16 additions & 9 deletions pegjs/redshift.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3253,12 +3253,21 @@ window_specification_frameless

window_frame_clause
= kw:KW_ROWS __ s:(window_frame_following / window_frame_preceding) {
// => string
return `rows ${s.value}`
return {
type: 'rows',
expr: s
}
}
/ KW_ROWS __ KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
// => string
return `rows between ${p.value} and ${f.value}`
/ KW_ROWS __ op:KW_BETWEEN __ p:window_frame_preceding __ KW_AND __ f:window_frame_following {
const left = {
type: 'origin',
value: 'rows',
}
const right = {
type: 'expr_list',
value: [p, f]
}
return createBinaryExpr(op, left, right)
}

window_frame_following
Expand All @@ -3279,14 +3288,12 @@ window_frame_preceding

window_frame_current_row
= 'CURRENT'i __ 'ROW'i {
// => { type: 'single_quote_string'; value: string }
return { type: 'single_quote_string', value: 'current row' }
return { type: 'origin', value: 'current row' }
}

window_frame_value
= s:'UNBOUNDED'i {
// => literal_string
return { type: 'single_quote_string', value: s.toUpperCase() }
return { type: 'origin', value: s.toUpperCase() }
}
/ literal_numeric

Expand Down
Loading

0 comments on commit dbe81e6

Please sign in to comment.