Skip to content

Commit e132c30

Browse files
authored
Merge PR#280: Fix formatting of BigQuery FROM clause operators
2 parents 7dd5b94 + 761c6f0 commit e132c30

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

src/languages/bigquery.formatter.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ const reservedFunctions = {
544544
'TO_BASE64',
545545
],
546546
other: ['BQ.JOBS.CANCEL', 'BQ.REFRESH_MATERIALIZED_VIEW'],
547+
pivot: ['PIVOT', 'UNPIVOT'],
547548
};
548549

549550
/**
@@ -635,14 +636,14 @@ const reservedKeywords = {
635636
'SOME',
636637
// 'STRUCT',
637638
'TABLE',
638-
// 'TABLESAMPLE',
639+
'TABLESAMPLE SYSTEM',
639640
'THEN',
640641
'TO',
641642
'TREAT',
642643
'TRUE',
643644
'UNBOUNDED',
644645
// 'UNION',
645-
// 'UNNEST',
646+
'UNNEST',
646647
// 'USING',
647648
// 'WHEN',
648649
// 'WHERE',
@@ -689,10 +690,6 @@ const reservedCommands = [
689690
// DQL, https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax
690691
'SELECT',
691692
'FROM',
692-
'UNNEST',
693-
'PIVOT',
694-
'UNPIVOT',
695-
'TABLESAMPLE SYSTEM',
696693
'WHERE',
697694
'GROUP BY',
698695
'HAVING',

test/bigquery.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,53 @@ describe('BigQueryFormatter', () => {
220220
`);
221221
});
222222
});
223+
224+
// Issue #279
225+
describe('supports FROM clause operators:', () => {
226+
it('UNNEST operator', () => {
227+
expect(format('SELECT * FROM UNNEST ([1, 2, 3]);')).toBe(dedent`
228+
SELECT
229+
*
230+
FROM
231+
UNNEST ([1, 2, 3]);
232+
`);
233+
});
234+
235+
it('PIVOT operator', () => {
236+
expect(format(`SELECT * FROM Produce PIVOT(sales FOR quarter IN (Q1, Q2, Q3, Q4));`))
237+
.toBe(dedent`
238+
SELECT
239+
*
240+
FROM
241+
Produce PIVOT(
242+
sales
243+
FOR
244+
quarter IN (Q1, Q2, Q3, Q4)
245+
);
246+
`);
247+
});
248+
249+
it('UNPIVOT operator', () => {
250+
expect(format(`SELECT * FROM Produce UNPIVOT(sales FOR quarter IN (Q1, Q2, Q3, Q4));`))
251+
.toBe(dedent`
252+
SELECT
253+
*
254+
FROM
255+
Produce UNPIVOT(
256+
sales
257+
FOR
258+
quarter IN (Q1, Q2, Q3, Q4)
259+
);
260+
`);
261+
});
262+
263+
it('TABLESAMPLE SYSTEM operator', () => {
264+
expect(format(`SELECT * FROM dataset.my_table TABLESAMPLE SYSTEM (10 PERCENT);`)).toBe(dedent`
265+
SELECT
266+
*
267+
FROM
268+
dataset.my_table TABLESAMPLE SYSTEM (10 PERCENT);
269+
`);
270+
});
271+
});
223272
});

0 commit comments

Comments
 (0)