Skip to content

Commit 4cc079e

Browse files
committed
Fix ROWS BETWEEN causing crash in BigQuery
Fixes #415
1 parent c18fd8b commit 4cc079e

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/languages/bigquery/bigquery.formatter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,16 @@ const reservedJoins = expandPhrases([
127127
'{INNER | CROSS} JOIN',
128128
]);
129129

130-
const reservedPhrases = [
130+
const reservedPhrases = expandPhrases([
131131
// https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#tablesample_operator
132132
'TABLESAMPLE SYSTEM',
133133
// From DDL: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language
134134
'ANY TYPE',
135135
'ALL COLUMNS',
136136
'NOT DETERMINISTIC',
137-
];
137+
// https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls#def_over_clause
138+
'{ROWS | RANGE} BETWEEN',
139+
]);
138140

139141
// https://cloud.google.com/bigquery/docs/reference/#standard-sql-reference
140142
export default class BigQueryFormatter extends Formatter {

test/bigquery.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import supportsComments from './features/comments';
1717
import supportsIdentifiers from './features/identifiers';
1818
import supportsParams from './options/param';
1919
import supportsWindow from './features/window';
20+
import supportsWindowFunctions from './features/windowFunctions';
2021
import supportsSetOperations from './features/setOperations';
2122
import supportsLimiting from './features/limiting';
2223
import supportsInsertInto from './features/insertInto';
@@ -59,6 +60,7 @@ describe('BigQueryFormatter', () => {
5960
supportsOperators(format, BigQueryFormatter.operators);
6061
supportsParams(format, { positional: true, named: ['@'], quoted: ['@``'] });
6162
supportsWindow(format);
63+
supportsWindowFunctions(format);
6264
supportsLimiting(format, { limit: true, offset: true });
6365

6466
// Note: BigQuery supports single dashes inside identifiers, so my-ident would be

test/features/windowFunctions.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import dedent from 'dedent-js';
2+
3+
import { FormatFn } from 'src/sqlFormatter';
4+
5+
export default function supportsWindowFunctions(format: FormatFn) {
6+
it('supports ROWS BETWEEN in window functions', () => {
7+
expect(
8+
format(`
9+
SELECT
10+
AVG(amount) OVER (
11+
PARTITION BY explosion
12+
ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
13+
) AS amount
14+
FROM
15+
tbl
16+
`)
17+
).toBe(dedent`
18+
SELECT
19+
AVG(amount) OVER (
20+
PARTITION BY
21+
explosion
22+
ORDER BY
23+
day ROWS BETWEEN 6 PRECEDING
24+
AND CURRENT ROW
25+
) AS amount
26+
FROM
27+
tbl
28+
`);
29+
});
30+
}

0 commit comments

Comments
 (0)