fix: handle ClickHouse parametric aggregate functions in alias extraction - #2446
fix: handle ClickHouse parametric aggregate functions in alias extraction#2446karl-power wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: c6523ed The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
🔵 Tier 2 — Low RiskSmall, isolated change with no API route or data model modifications. Why this tier:
Review process: AI review + quick human skim (target: 5–15 min). Reviewer validates AI assessment and checks for domain-specific concerns. Stats
|
Greptile SummaryThis PR fixes
Confidence Score: 5/5Safe to merge — the change is additive and well-tested, with no modifications to existing parsing logic beyond wiring in the new pre-processing step. The new replaceParametricAggregates helper is a pure transformation with no side-effects, follows the same token-and-restore pattern as the existing replaceJsonExpressions, and is covered by 13 tests. The integration into chSqlToAliasMap is minimal and the catch block is unchanged. No existing behavior is altered for SQL that does not contain parametric aggregates. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[chSqlToAliasMap input] --> B[parameterizedQueryToSql]
B --> C[extractSettingsClauseFromEnd]
C --> D["replaceParametricAggregates\nfunc(p)(args) token"]
D --> E["replaceJsonExpressions\ncol.key token"]
E --> F[node-sql-parser astify]
F --> G{column has alias?}
G -->|column_ref| H[extract token name]
G -->|expr with loc| I[slice tokenized SQL]
G -->|other| J[console.error]
H --> K[merge json + parametric maps]
I --> K
K --> L[restore tokens in alias values]
L --> M[return aliasMap]
Reviews (2): Last reviewed commit: "fix: handle ClickHouse parametric aggreg..." | Re-trigger Greptile |
c2f0849 to
ab99e7c
Compare
E2E Test Results✅ All tests passed • 198 passed • 3 skipped • 1420s
Tests ran across 4 shards in parallel. |
ab99e7c to
c6523ed
Compare
|
Closing in favour of reverting the problematic PR |
Summary
chSqlToAliasMap(packages/common-utils/src/clickhouse/index.ts) parses SQL withnode-sql-parser(PostgreSQL dialect) to recover SELECT-list aliases. That parser can't understand ClickHouse parametric aggregate functions — the double-parenfunc(params)(args)form — and throws at the second(.This started surfacing after #2422, which added
extractSelectAliasesinrenderChartConfig.ts. For a string-form select list, that helper feeds the rendered SQL tochSqlToAliasMap. The value-autocomplete path (Metadata.getKeyValues) builds exactly that shape —SELECT groupUniqArray(20)(param0) AS param0, groupUniqArray(20)(param1) AS param1, … FROM t— so every key-value autocomplete fetch hit the parser's failure, landed in the catch block, and spammed the console withconsole.trace()+Error parsing alias map with JSON removed.chSqlToAliasMapis a shared utility (useAliasMapFromChartConfig,DBRowTable, the CLI, alert checking), and parametric aggregates likequantile(0.9)(Duration)are perfectly valid in user-authored selects — so this is a general parser gap, not specific togetKeyValues.What changed
replaceParametricAggregateshelper (core/utils.ts), mirroring the existingreplaceJsonExpressionstoken+restore pattern. A single quote-aware, balanced-paren scan replaces eachidentifier(...)(...)span with a bare-identifier placeholder token (__hdx_paramagg_replacement_N) thatnode-sql-parserparses fine as a column reference, and returns aMap<token, originalExpr>for restoration. Only the genuine double-paren form matches:count(),sum(if(x, 1, 0)), bracketedResourceAttributes['x'], and unbalanced input are left untouched, and parens inside string/identifier quotes are ignored.chSqlToAliasMap: runs beforereplaceJsonExpressions(so a parametric aggregate's dotted/JSON args stay intact inside the saved span, and the dotless token is inert to JSON detection), and both replacement maps are merged in the restoration loop so the alias value is restored to the full original expression.clickhouse.test.ts(including the exactgetKeyValuesrepro and acount()-isn't-parametric guard) and 8 unit tests forreplaceParametricAggregatesinutils.test.ts(single/multiple matches, nested parens, quoted parens, whitespace tolerance, unbalanced bail-out, single-paren no-match).How to test on Vercel preview
Preview routes:
/searchSteps:
References