Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/languages/clickhouse/clickhouse.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ export const clickhouse: DialectOptions = {
* for SELECT GRANTs
* 2. Formats SET(100) as RESERVED_FUNCTION_NAME instead of RESERVED_KEYWORD
* so it appears as a function rather than a statement.
* 3. Formats RANGE(...) as RESERVED_FUNCTION_NAME instead of RESERVED_KEYWORD
* so keywordCase does not rewrite the case-sensitive range() function.
* RANGE BETWEEN stays a reserved keyword phrase for window frames.
*/
function postProcess(tokens: Token[]): Token[] {
return tokens.map((token, i) => {
Expand Down Expand Up @@ -333,6 +336,15 @@ function postProcess(tokens: Token[]): Token[] {
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}

// We should format `range(32)` as-is rather than `RANGE (32)`
if (
token.type === TokenType.RESERVED_KEYWORD &&
token.text === 'RANGE' &&
nextToken.type === TokenType.OPEN_PAREN
) {
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}

return token;
});
}
28 changes: 28 additions & 0 deletions test/clickhouse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,34 @@ describe('ClickhouseFormatter', () => {
value ASC;
`);
});

it('formats SELECT with window function and RANGE BETWEEN', () => {
expect(
format(
'SELECT part_key, value, groupArray(value) OVER (ORDER BY seq RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM wf_frame;',
{ keywordCase: 'upper' }
)
).toBe(dedent`
SELECT
part_key,
value,
groupArray(value) OVER (
ORDER BY
seq RANGE BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW
)
FROM
wf_frame;
`);
});
});

// Regression test for issue #980
it('does not uppercase the case-sensitive range() function', () => {
expect(format('SELECT range(32);', { keywordCase: 'upper' })).toBe(dedent`
SELECT
range(32);
`);
});

// https://clickhouse.com/docs/sql-reference/statements/create
Expand Down
Loading