Skip to content

Commit d3060a4

Browse files
committed
Parens, map literals
1 parent ec14185 commit d3060a4

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/languages/clickhouse/clickhouse.formatter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export const clickhouse: DialectOptions = {
248248
reservedKeywords: keywords,
249249
reservedDataTypes: dataTypes,
250250
reservedFunctionNames: functions,
251-
extraParens: ['[]'],
251+
extraParens: ['[]', '{}'],
252252
lineCommentTypes: ['#', '--'],
253253
nestedBlockComments: false,
254254
underscoresInNumbers: true,
@@ -258,7 +258,12 @@ export const clickhouse: DialectOptions = {
258258
// https://clickhouse.com/docs/sql-reference/syntax#defining-and-using-query-parameters
259259
custom: [
260260
{
261-
regex: String.raw`\{[^:]+:[^}]+\}`,
261+
// Parameters are like {foo:Uint64} or {foo:Map(String, String)}
262+
// We include `'` in the negated character class to be a little sneaky:
263+
// map literals have quoted keys, and we use this to avoid confusing
264+
// them for named parameters. This means that the map literal `{'foo':1}`
265+
// will be formatted as `{'foo': 1}` rather than `{foo: 1}`.
266+
regex: String.raw`\{[^:']+:[^}]+\}`,
262267
key: v => {
263268
const match = /\{([^:]+):/.exec(v);
264269
return match ? match[1].trim() : v;

test/clickhouse.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,13 @@ describe('ClickhouseFormatter', () => {
169169
supportsArrayAndMapAccessors(format);
170170
supportsDataTypeCase(format);
171171

172-
// Should support the ternary operator
172+
it('supports map literals', () => {
173+
expect(format(`SELECT {'foo':1,'bar':10,'baz':2,'zap':8};`)).toBe(dedent`
174+
SELECT
175+
{'foo': 1, 'bar': 10, 'baz': 2, 'zap': 8};
176+
`);
177+
});
178+
173179
it('supports the ternary operator', () => {
174180
// NOTE: Ternary operators have a missing space because
175181
// ExpressionFormatter's `formatOperator` method special-cases `:`.
@@ -179,7 +185,6 @@ describe('ClickhouseFormatter', () => {
179185
`);
180186
});
181187

182-
// Should support the lambda creation operator
183188
it('supports the lambda creation operator', () => {
184189
expect(format('SELECT arrayMap(x->2*x, [1,2,3,4]) AS result;')).toBe(dedent`
185190
SELECT

0 commit comments

Comments
 (0)