Skip to content

Commit a7aa86a

Browse files
committed
fix: over window frame current row in snwoflake
1 parent 2ab89f5 commit a7aa86a

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

pegjs/snowflake.pegjs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,15 @@ window_specification_frameless
26782678
}
26792679
}
26802680

2681+
interval_expr_preceding
2682+
= i:interval_expr __ 'PRECEDING'i {
2683+
const suffix = {
2684+
type: 'origin',
2685+
value: 'preceding',
2686+
}
2687+
i.suffix = suffix;
2688+
return i;
2689+
}
26812690
window_frame_clause
26822691
= kw:KW_ROWS __ s:(window_frame_following / window_frame_preceding) {
26832692
return {
@@ -2696,17 +2705,11 @@ window_frame_clause
26962705
}
26972706
return createBinaryExpr(op, left, right)
26982707
}
2699-
/ 'RANGE'i __ op:KW_BETWEEN __ p:interval_expr __ 'PRECEDING'i __ KW_AND __ f:interval_expr __ 'PRECEDING'i {
2708+
/ 'RANGE'i __ op:KW_BETWEEN __ p:interval_expr_preceding __ KW_AND __ f:(interval_expr_preceding / window_frame_current_row) {
27002709
const left = {
27012710
type: 'origin',
27022711
value: 'range',
27032712
}
2704-
const suffix = {
2705-
type: 'origin',
2706-
value: 'preceding',
2707-
}
2708-
p.suffix = suffix
2709-
f.suffix = suffix
27102713
const right = {
27112714
type: 'expr_list',
27122715
value: [p, f]

test/snowflake.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,22 @@ describe('snowflake', () => {
484484
`SELECT "user_id", date("derived_tstamp") AS "event_date", "price_point", MAX("price_point") OVER (PARTITION BY "user_id" ORDER BY date("derived_tstamp") ASC RANGE BETWEEN INTERVAL '29 DAYS' PRECEDING AND INTERVAL '1 DAY' PRECEDING) AS "max_price_point_last_30_days" FROM "some_table"`
485485
]
486486
},
487+
{
488+
title: 'window function with range',
489+
sql: [
490+
`SELECT
491+
SUM(id) OVER (
492+
PARTITION BY
493+
name
494+
ORDER BY
495+
created_at ASC RANGE BETWEEN INTERVAL '9 DAYS' PRECEDING
496+
AND CURRENT ROW
497+
) last10Dmatches
498+
FROM
499+
model1`,
500+
`SELECT SUM("id") OVER (PARTITION BY "name" ORDER BY "created_at" ASC RANGE BETWEEN INTERVAL '9 DAYS' PRECEDING AND CURRENT ROW) AS "last10Dmatches" FROM "model1"`
501+
]
502+
},
487503
]
488504
SQL_LIST.forEach(sqlInfo => {
489505
const { title, sql } = sqlInfo

0 commit comments

Comments
 (0)