Repository navigation
ClickHouse: format aggregate functions with combinators as function calls (countMerge, sumIf, ...) - #988
Closed
MunMunMiao wants to merge 1 commit into
Closed
ClickHouse: format aggregate functions with combinators as function calls (countMerge, sumIf, ...)#988MunMunMiao wants to merge 1 commit into
MunMunMiao wants to merge 1 commit into
Conversation
ClickHouse builds names like `sumIf`, `countMerge` or `uniqExactIfState` from an aggregate function and combinators, so they are not in the list of functions (it comes from `system.functions`). They were treated as identifiers and printed with a space before the parenthesis: `countMerge (x)`. Treat an identifier that is a known aggregate function with combinators appended, and is followed by a parenthesis, as a function name.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Reclassified functions do not honor the standard uppercase functionCase option.
2 open findings
What changed in this PR
Adds recognition of ClickHouse aggregate-function combinators so calls format without unwanted spaces.
Changes:
- Detects known functions with appended aggregate combinators.
- Adds representative combinator-formatting coverage.
| File | Description |
|---|---|
src/languages/clickhouse/clickhouse.formatter.ts |
Recognizes combinator-based aggregate calls. |
test/clickhouse.test.ts |
Tests representative combinator names. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| nextToken.type === TokenType.OPEN_PAREN && | ||
| isAggregateWithCombinators(token.text) | ||
| ) { | ||
| return { ...token, type: TokenType.RESERVED_FUNCTION_NAME }; |
| }); | ||
|
|
||
| // https://clickhouse.com/docs/sql-reference/window-functions | ||
| describe('Aggregate function combinators', () => { |
Author
|
Closing for now: I'm going to handle the formatting on my side instead. Thanks for the library. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Problem
In the ClickHouse dialect, the name of an aggregate function with a combinator appended (
sumIf,countMerge,uniqExactIfState, ...) is not in the list of functions, because the list comes fromsystem.functionsand ClickHouse builds these names from the aggregate function and the combinators. They are treated as plain identifiers, so there is a space before the parenthesis, unlikesum(x):Before (15.9.0,
language: 'clickhouse'):After:
Combinators are very common in materialized views and in queries on
AggregatingMergeTreetables (...Statewhen writing,...Mergewhen reading).Change
postProcessin the ClickHouse dialect already turnsSETinto a function name when a parenthesis follows. This does the same for an identifier thatThe combinators are the ones in the documentation. Requiring the base name to be a known function keeps the change narrow: an identifier like
userState(x)is formatted as before.Not in this PR
A parametric aggregate call
quantile(0.9)(x)is still formatted asquantile(0.9) (x), also for functions that are in the list. That comes from how a second parenthesis after a function call is parsed, not from the dialect, so I left it out.Tests
Added a test for the new names.
pnpm exec jestpasses for all dialects (5886 passed, 1 skipped), andtsc --noEmit,prettier --checkandeslintare clean.Fixes #990.
This change was prepared with Claude Code (an AI coding assistant) and checked by running the full test suite locally.