Skip to content

Commit ec14185

Browse files
committed
Changes from feedback
1 parent b912e09 commit ec14185

3 files changed

Lines changed: 27 additions & 20 deletions

File tree

src/languages/clickhouse/clickhouse.formatter.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export const clickhouse: DialectOptions = {
258258
// https://clickhouse.com/docs/sql-reference/syntax#defining-and-using-query-parameters
259259
custom: [
260260
{
261-
regex: String.raw`\{\s*[^:]+:[^}]+\}`,
261+
regex: String.raw`\{[^:]+:[^}]+\}`,
262262
key: v => {
263263
const match = /\{([^:]+):/.exec(v);
264264
return match ? match[1].trim() : v;
@@ -267,13 +267,18 @@ export const clickhouse: DialectOptions = {
267267
],
268268
},
269269
operators: [
270-
// Arithmetic
270+
// Strings, arithmetic
271271
'%', // modulo
272+
'||', // string concatenation
272273

273274
// Ternary
274275
'?',
275276
':',
276277

278+
// Comparison
279+
'==',
280+
'<=>', // null-safe equal
281+
277282
// Lambda creation
278283
'->',
279284
],
@@ -286,14 +291,10 @@ export const clickhouse: DialectOptions = {
286291
};
287292

288293
/**
289-
* Converts IN and ANY from RESERVED_FUNCTION_NAME to RESERVED_KEYWORD
290-
* when they are used as operators/modifiers (not function calls).
291-
*
292-
* IN operator: foo IN (1, 2, 3) - IN comes after an identifier/expression
293-
* IN function: IN(foo, 1, 2, 3) - IN comes at start or after operators/keywords
294-
*
295-
* ANY join modifier: ANY LEFT JOIN, ANY JOIN - ANY comes after an operator
296-
* any() aggregate function: any(column) - selects first encountered value
294+
* 1. Formats GRANT statements to use RESERVED_KEYWORD instead of RESERVED_SELECT
295+
* for SELECT GRANTs
296+
* 2. Formats SET(100) as RESERVED_FUNCTION_NAME instead of RESERVED_KEYWORD
297+
* so it appears as a function rather than a statement.
297298
*/
298299
function postProcess(tokens: Token[]): Token[] {
299300
return tokens.map((token, i) => {

src/lexer/token.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export const testToken =
8080
export const isToken = {
8181
ARRAY: testToken({ text: 'ARRAY', type: TokenType.RESERVED_DATA_TYPE }),
8282
BY: testToken({ text: 'BY', type: TokenType.RESERVED_KEYWORD }),
83-
IN: testToken({ text: 'IN', type: TokenType.RESERVED_KEYWORD }),
8483
SET: testToken({ text: 'SET', type: TokenType.RESERVED_CLAUSE }),
8584
STRUCT: testToken({ text: 'STRUCT', type: TokenType.RESERVED_DATA_TYPE }),
8685
WINDOW: testToken({ text: 'WINDOW', type: TokenType.RESERVED_CLAUSE }),

test/clickhouse.test.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,25 @@ describe('ClickhouseFormatter', () => {
173173
it('supports the ternary operator', () => {
174174
// NOTE: Ternary operators have a missing space because
175175
// ExpressionFormatter's `formatOperator` method special-cases `:`.
176-
expect(format('SELECT foo?bar: baz;')).toBe('SELECT\n foo ? bar: baz;');
176+
expect(format('SELECT foo?bar: baz;')).toBe(dedent`
177+
SELECT
178+
foo ? bar: baz;
179+
`);
177180
});
178181

179182
// Should support the lambda creation operator
180183
it('supports the lambda creation operator', () => {
181-
expect(format('SELECT arrayMap(x->2*x, [1,2,3,4]) AS result;')).toBe(
182-
'SELECT\n arrayMap(x -> 2 * x, [1, 2, 3, 4]) AS result;'
183-
);
184+
expect(format('SELECT arrayMap(x->2*x, [1,2,3,4]) AS result;')).toBe(dedent`
185+
SELECT
186+
arrayMap(x -> 2 * x, [1, 2, 3, 4]) AS result;
187+
`);
184188
});
185189

186190
it('should support parameters', () => {
187-
expect(format('SELECT {foo:Uint64};', { params: { foo: "'123'" } })).toBe("SELECT\n '123';");
191+
expect(format('SELECT {foo:Uint64};', { params: { foo: "'123'" } })).toBe(dedent`
192+
SELECT
193+
'123';
194+
`);
188195
expect(format('SELECT {foo:Map(String, String)};', { params: { foo: "{'bar': 'baz'}" } })).toBe(
189196
dedent`
190197
SELECT
@@ -568,9 +575,8 @@ describe('ClickhouseFormatter', () => {
568575
// https://clickhouse.com/docs/sql-reference/statements/alter/order-by
569576
describe('ALTER ORDER BY statements', () => {
570577
it('formats ALTER TABLE MODIFY ORDER BY', () => {
571-
expect(
572-
format('ALTER TABLE db.events ON CLUSTER prod MODIFY ORDER BY (user_id, timestamp);')
573-
).toBe(dedent`
578+
expect(format('ALTER TABLE db.events ON CLUSTER prod MODIFY ORDER BY (user_id, timestamp);'))
579+
.toBe(dedent`
574580
ALTER TABLE db.events
575581
ON CLUSTER prod
576582
MODIFY ORDER BY (user_id, timestamp);
@@ -937,7 +943,8 @@ describe('ClickhouseFormatter', () => {
937943
});
938944

939945
it('formats DROP SETTINGS PROFILE', () => {
940-
expect(format('DROP SETTINGS PROFILE IF EXISTS profile1, profile2 ON CLUSTER my_cluster;')).toBe(dedent`
946+
expect(format('DROP SETTINGS PROFILE IF EXISTS profile1, profile2 ON CLUSTER my_cluster;'))
947+
.toBe(dedent`
941948
DROP SETTINGS PROFILE IF EXISTS
942949
profile1,
943950
profile2

0 commit comments

Comments
 (0)