Skip to content

Commit d1101f5

Browse files
committed
Add support for WINDOW clause in multiple dialects
1 parent 0592b01 commit d1101f5

18 files changed

+80
-20
lines changed

src/languages/bigquery.formatter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ const reservedCommands = [
696696
'ORDER BY',
697697
'QUALIFY',
698698
'WINDOW',
699+
'PARTITION BY',
699700
'LIMIT',
700701
'OFFSET',
701702
'WITH',

src/languages/hive.formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ const reservedCommands = [
572572
'VALUES',
573573
'WHERE',
574574
'WITH',
575+
'WINDOW',
576+
'PARTITION BY',
575577

576578
// newline keywords
577579
'STORED AS',

src/languages/mysql.formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,8 @@ const reservedCommands = [
12741274
'OFFSET',
12751275
'ORDER BY',
12761276
'WHERE',
1277+
'WINDOW',
1278+
'PARTITION BY',
12771279
];
12781280

12791281
const reservedBinaryCommands = [

src/languages/n1ql.formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ const reservedCommands = [
472472
'VALUES',
473473
'WHERE',
474474
'WITH',
475+
'WINDOW',
476+
'PARTITION BY',
475477
];
476478

477479
const reservedBinaryCommands = [

src/languages/postgresql.formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,8 @@ const reservedCommands = [
15801580
'ORDER BY',
15811581
'WHERE',
15821582
'WITH',
1583+
'WINDOW',
1584+
'PARTITION BY',
15831585
];
15841586

15851587
const reservedBinaryCommands = [

src/languages/sql.formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ const reservedCommands = [
347347
'VALUES',
348348
'WHERE',
349349
'WITH',
350+
'WINDOW',
351+
'PARTITION BY',
350352
];
351353

352354
const reservedBinaryCommands = [

src/languages/sqlite.formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ const reservedCommands = [
239239
'VALUES',
240240
'WHERE',
241241
'WITH',
242+
'WINDOW',
243+
'PARTITION BY',
242244
];
243245

244246
const reservedBinaryCommands = [

src/languages/tsql.formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,8 @@ const reservedCommands = [
11891189
'VALUES',
11901190
'WHERE',
11911191
'WITH',
1192+
'WINDOW',
1193+
'PARTITION BY',
11921194
];
11931195

11941196
const reservedBinaryCommands = [

test/bigquery.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import supportsDeleteFrom from './features/deleteFrom';
1515
import supportsComments from './features/comments';
1616
import supportsIdentifiers from './features/identifiers';
1717
import supportsParams from './options/param';
18+
import supportsWindow from './features/window';
1819

1920
describe('BigQueryFormatter', () => {
2021
const language = 'bigquery';
@@ -32,6 +33,7 @@ describe('BigQueryFormatter', () => {
3233
supportsJoin(format, { without: ['NATURAL JOIN'] });
3334
supportsOperators(format, BigQueryFormatter.operators);
3435
supportsParams(format, { positional: true, named: ['@'], quoted: ['@``'] });
36+
supportsWindow(format);
3537

3638
// Note: BigQuery supports single dashes inside identifiers, so my-ident would be
3739
// detected as identifier, while other SQL dialects would detect it as

test/features/window.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import dedent from 'dedent-js';
2+
3+
import { FormatFn } from 'src/sqlFormatter';
4+
5+
export default function supportsWindow(format: FormatFn) {
6+
it('formats WINDOW clause at top level', () => {
7+
const result = format(
8+
'SELECT *, LAG(value) OVER wnd AS next_value FROM tbl WINDOW wnd AS (PARTITION BY id ORDER BY time);'
9+
);
10+
expect(result).toBe(dedent`
11+
SELECT
12+
*,
13+
LAG(value) OVER wnd AS next_value
14+
FROM
15+
tbl
16+
WINDOW
17+
wnd AS (
18+
PARTITION BY
19+
id
20+
ORDER BY
21+
time
22+
);
23+
`);
24+
});
25+
26+
it('formats multiple WINDOW specifications', () => {
27+
const result = format(
28+
'SELECT * FROM table1 WINDOW w1 AS (PARTITION BY col1), w2 AS (PARTITION BY col1, col2);'
29+
);
30+
expect(result).toBe(dedent`
31+
SELECT
32+
*
33+
FROM
34+
table1
35+
WINDOW
36+
w1 AS (
37+
PARTITION BY
38+
col1
39+
),
40+
w2 AS (
41+
PARTITION BY
42+
col1,
43+
col2
44+
);
45+
`);
46+
});
47+
}

0 commit comments

Comments
 (0)