Skip to content

Commit

Permalink
Merge PR #311: Add support for WINDOW clause
Browse files Browse the repository at this point in the history
  • Loading branch information
nene authored Jul 13, 2022
2 parents 0592b01 + b8bf279 commit ad0afb3
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/languages/bigquery.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ const reservedCommands = [
'ORDER BY',
'QUALIFY',
'WINDOW',
'PARTITION BY',
'LIMIT',
'OFFSET',
'WITH',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/hive.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,8 @@ const reservedCommands = [
'VALUES',
'WHERE',
'WITH',
'WINDOW',
'PARTITION BY',

// newline keywords
'STORED AS',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/mysql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,8 @@ const reservedCommands = [
'OFFSET',
'ORDER BY',
'WHERE',
'WINDOW',
'PARTITION BY',
];

const reservedBinaryCommands = [
Expand Down
2 changes: 2 additions & 0 deletions src/languages/n1ql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ const reservedCommands = [
'VALUES',
'WHERE',
'WITH',
'WINDOW',
'PARTITION BY',
];

const reservedBinaryCommands = [
Expand Down
2 changes: 2 additions & 0 deletions src/languages/postgresql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,8 @@ const reservedCommands = [
'ORDER BY',
'WHERE',
'WITH',
'WINDOW',
'PARTITION BY',
];

const reservedBinaryCommands = [
Expand Down
3 changes: 1 addition & 2 deletions src/languages/spark.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,6 @@ const reservedCommands = [
'WITH',
'CLUSTER BY',
'DISTRIBUTE BY',
'PARTITION BY', // verify
'GROUP BY',
'HAVING',
'VALUES',
Expand Down Expand Up @@ -711,7 +710,7 @@ const reservedCommands = [
'INSERT',
'LATERAL VIEW',
'UPDATE',
'WINDOW', // verify
'WINDOW',
];

const reservedBinaryCommands = [
Expand Down
2 changes: 2 additions & 0 deletions src/languages/sql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ const reservedCommands = [
'VALUES',
'WHERE',
'WITH',
'WINDOW',
'PARTITION BY',
];

const reservedBinaryCommands = [
Expand Down
2 changes: 2 additions & 0 deletions src/languages/sqlite.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ const reservedCommands = [
'VALUES',
'WHERE',
'WITH',
'WINDOW',
'PARTITION BY',
];

const reservedBinaryCommands = [
Expand Down
2 changes: 2 additions & 0 deletions src/languages/tsql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,8 @@ const reservedCommands = [
'VALUES',
'WHERE',
'WITH',
'WINDOW',
'PARTITION BY',
];

const reservedBinaryCommands = [
Expand Down
2 changes: 2 additions & 0 deletions test/bigquery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import supportsDeleteFrom from './features/deleteFrom';
import supportsComments from './features/comments';
import supportsIdentifiers from './features/identifiers';
import supportsParams from './options/param';
import supportsWindow from './features/window';

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

// Note: BigQuery supports single dashes inside identifiers, so my-ident would be
// detected as identifier, while other SQL dialects would detect it as
Expand Down
47 changes: 47 additions & 0 deletions test/features/window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import dedent from 'dedent-js';

import { FormatFn } from 'src/sqlFormatter';

export default function supportsWindow(format: FormatFn) {
it('formats WINDOW clause at top level', () => {
const result = format(
'SELECT *, LAG(value) OVER wnd AS next_value FROM tbl WINDOW wnd AS (PARTITION BY id ORDER BY time);'
);
expect(result).toBe(dedent`
SELECT
*,
LAG(value) OVER wnd AS next_value
FROM
tbl
WINDOW
wnd AS (
PARTITION BY
id
ORDER BY
time
);
`);
});

it('formats multiple WINDOW specifications', () => {
const result = format(
'SELECT * FROM table1 WINDOW w1 AS (PARTITION BY col1), w2 AS (PARTITION BY col1, col2);'
);
expect(result).toBe(dedent`
SELECT
*
FROM
table1
WINDOW
w1 AS (
PARTITION BY
col1
),
w2 AS (
PARTITION BY
col1,
col2
);
`);
});
}
2 changes: 2 additions & 0 deletions test/hive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import supportsOperators from './features/operators';
import supportsArrayAndMapAccessors from './features/arrayAndMapAccessors';
import supportsComments from './features/comments';
import supportsIdentifiers from './features/identifiers';
import supportsWindow from './features/window';

describe('HiveFormatter', () => {
const language = 'hive';
Expand All @@ -31,6 +32,7 @@ describe('HiveFormatter', () => {
supportsJoin(format, { without: ['NATURAL JOIN'] });
supportsOperators(format, HiveFormatter.operators);
supportsArrayAndMapAccessors(format);
supportsWindow(format);

it('throws error when params option used', () => {
expect(() => format('SELECT *', { params: ['1', '2', '3'] })).toThrow(
Expand Down
2 changes: 2 additions & 0 deletions test/mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import MySqlFormatter from 'src/languages/mysql.formatter';
import behavesLikeMariaDbFormatter from './behavesLikeMariaDbFormatter';

import supportsOperators from './features/operators';
import supportsWindow from './features/window';

describe('MySqlFormatter', () => {
const language = 'mysql';
Expand All @@ -13,6 +14,7 @@ describe('MySqlFormatter', () => {
behavesLikeMariaDbFormatter(format);

supportsOperators(format, MySqlFormatter.operators, ['AND', 'OR', 'XOR']);
supportsWindow(format);

// TODO: disabled for now
it.skip('supports @@ system variables', () => {
Expand Down
2 changes: 2 additions & 0 deletions test/n1ql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import supportsArrayLiterals from './features/arrayLiterals';
import supportsComments from './features/comments';
import supportsIdentifiers from './features/identifiers';
import supportsParams from './options/param';
import supportsWindow from './features/window';

describe('N1qlFormatter', () => {
const language = 'n1ql';
Expand All @@ -34,6 +35,7 @@ describe('N1qlFormatter', () => {
supportsJoin(format, { without: ['FULL', 'CROSS', 'NATURAL'] });
supportsReturning(format);
supportsParams(format, { positional: true, numbered: ['$'], named: ['$'] });
supportsWindow(format);

it('formats INSERT with {} object literal', () => {
const result = format(
Expand Down
2 changes: 2 additions & 0 deletions test/postgresql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import supportsComments from './features/comments';
import supportsIdentifiers from './features/identifiers';
import supportsParams from './options/param';
import supportsArrayAndMapAccessors from './features/arrayAndMapAccessors';
import supportsWindow from './features/window';

describe('PostgreSqlFormatter', () => {
const language = 'postgresql';
Expand All @@ -41,6 +42,7 @@ describe('PostgreSqlFormatter', () => {
supportsJoin(format);
supportsReturning(format);
supportsParams(format, { numbered: ['$'] });
supportsWindow(format);

it('allows $ character as part of identifiers', () => {
expect(format('SELECT foo$, some$$ident')).toBe(dedent`
Expand Down
20 changes: 8 additions & 12 deletions test/spark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,19 @@ describe('SparkFormatter', () => {
],
});

it('formats WINDOW specification as top level', () => {
const result = format(
'SELECT *, LAG(value) OVER wnd AS next_value FROM tbl WINDOW wnd as (PARTITION BY id ORDER BY time);'
);
it('formats basic WINDOW clause', () => {
const result = format(`SELECT * FROM tbl WINDOW win1, WINDOW win2, WINDOW win3;`);
expect(result).toBe(dedent`
SELECT
*,
LAG(value) OVER wnd AS next_value
*
FROM
tbl
WINDOW
wnd as (
PARTITION BY
id
ORDER BY
time
);
win1,
WINDOW
win2,
WINDOW
win3;
`);
});

Expand Down
2 changes: 2 additions & 0 deletions test/sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import supportsDeleteFrom from './features/deleteFrom';
import supportsComments from './features/comments';
import supportsIdentifiers from './features/identifiers';
import supportsParams from './options/param';
import supportsWindow from './features/window';

describe('SqlFormatter', () => {
const language = 'sql';
Expand All @@ -34,6 +35,7 @@ describe('SqlFormatter', () => {
supportsJoin(format);
supportsOperators(format, SqlFormatter.operators);
supportsParams(format, { positional: true });
supportsWindow(format);

it('formats FETCH FIRST like LIMIT', () => {
const result = format('SELECT * FETCH FIRST 2 ROWS ONLY;');
Expand Down
2 changes: 2 additions & 0 deletions test/sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import supportsDeleteFrom from './features/deleteFrom';
import supportsComments from './features/comments';
import supportsIdentifiers from './features/identifiers';
import supportsParams from './options/param';
import supportsWindow from './features/window';

describe('SqliteFormatter', () => {
const language = 'sqlite';
Expand All @@ -37,6 +38,7 @@ describe('SqliteFormatter', () => {
});
supportsOperators(format, SqliteFormatter.operators);
supportsParams(format, { positional: true, numbered: ['?'], named: [':', '$', '@'] });
supportsWindow(format);

it('formats FETCH FIRST like LIMIT', () => {
const result = format('SELECT * FETCH FIRST 2 ROWS ONLY;');
Expand Down
2 changes: 2 additions & 0 deletions test/tsql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import supportsDeleteFrom from './features/deleteFrom';
import supportsComments from './features/comments';
import supportsIdentifiers from './features/identifiers';
import supportsParams from './options/param';
import supportsWindow from './features/window';

describe('TSqlFormatter', () => {
const language = 'tsql';
Expand All @@ -37,6 +38,7 @@ describe('TSqlFormatter', () => {
);
supportsJoin(format, { without: ['NATURAL'] });
supportsParams(format, { named: ['@'], quoted: ['@""', '@[]'] });
supportsWindow(format);

// TODO: The following are duplicated from StandardSQLFormatter test

Expand Down

0 comments on commit ad0afb3

Please sign in to comment.