[HDX-5079] Render formulas in the composed metric query - #2908
Conversation
Compile the validated formula AST (HDX-5078) into the final SELECT projection over the pivoted per-series columns produced by the composed multi-series metric query (HDX-5077). - compileFormulaAst: letter refs resolve to the pivot expressions with ratio-consistent semantics (missing operand -> 0, division by zero or missing denominator -> NULL, rendered as a gap) - Formula columns append after the operand value columns (select order), preserving the useChartNumberFormats positional meta contract; showOperandSeries: false emits only the formula column(s) - Single-series metric charts with a formula route through the composed path; formulas take precedence over seriesReturnType: 'ratio' - builderToRawSql rejects formula configs (same limitation as multi-series metric charts)
🦋 Changeset detectedLatest commit: 8d9e37e The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
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 |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThe PR renders validated metric formulas in composed ClickHouse queries while preserving operand ordering and missing-data semantics.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| Filename | Overview |
|---|---|
| packages/common-utils/src/core/formula.ts | Adds an AST-to-SQL compiler with coalesced operands and guarded division denominators. |
| packages/common-utils/src/core/renderChartConfig.ts | Integrates formulas into composed metric projections and correctly completes the previously requested alias-escaping fix. |
| packages/common-utils/src/core/builderToRawSql.ts | Rejects formula metric configurations that cannot be represented by the raw-SQL conversion path. |
| packages/common-utils/src/tests/renderChartConfig.test.ts | Covers formula projection, routing, validation, collisions, precedence, and escaped aliases. |
| packages/common-utils/src/tests/queryChartConfig.int.test.ts | Exercises formula execution across missing data, grouping, mixed metric types, and chart shapes. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart LR
Config[Metric chart config] --> Validate[Validate formula AST]
Validate --> Branches[Render one query branch per operand]
Branches --> Union[UNION ALL with series index]
Union --> Pivot[Pivot operand values]
Pivot --> Formula[Compile formula projection]
Formula --> Result[Formula and optional operand columns]
Reviews (3): Last reviewed commit: "chore: drop ticket references from formu..." | Re-trigger Greptile
E2E Test Results✅ All tests passed • 291 passed • 1 skipped • 846s
Tests ran across 4 shards in parallel. |
🔵 Tier 2 — Low RiskSmall, isolated change with no API route or data model modifications. Why this tier:
Additional context: touches the query rendering engine lightly (130 lines, under the 150-line bar for Tier 4) Review process: AI review + quick human skim (target: 5–15 min). Reviewer validates AI assessment and checks for domain-specific concerns. Stats
|
A formula or series alias containing a double quote terminated the double-quoted identifier early (AS "bad"name") and failed the query with a ClickHouse syntax error. Escape by doubling the quote at SQL-emission time across all composed-projection sites (formula, operand, and ratio columns); collision dedup and result meta keep the raw name. Addresses greptile P1 review comment.
Deep ReviewRendering metric formulas ( ✅ No critical issues found. No P0/P1 defects; the diff is safe to merge. One test gap and a couple of small maintainability nits below. 🟡 P2 — recommended
🔵 P3 nitpicks (2)
Reviewers (8 dispatched): correctness, security, adversarial, testing, maintainability, kieran-typescript, api-contract, learnings-researcher. Graded findings reflect the testing and maintainability reviewers plus direct orchestrator analysis of the correctness, security, and API-contract dimensions (all clean); the learnings researcher found no prior Testing gaps:
|
Summary
Renders metric formulas (HDX-5078's
formulasconfig) in the composed multi-series metric query (HDX-5077), so a derived series likeA / (A + B + C) * 100is computed by ClickHouse as part of the single composed query instead of not rendering at all.compileFormulaAst(core/formula.ts): compiles the validated letter-ref AST into a SQL expression over per-series value expressions. Never splices user text into SQL — only the parsed/validated AST is walked.coalesce(<pivot>, 0)(a missing operand counts as 0, so a zero-error group reads 0%, not N/A), and every division denominator is wrapped innullif(..., 0)(zero or missing denominator → NULL → rendered gap, never 0 or an error).renderMultiSeriesMetricChartConfig): operand value columns first in select order, then formula columns in formulas order, ahead of the group/bucket passthrough columns — preserving theuseChartNumberFormatspositional meta contract.showOperandSeries: falsedrops the operand columns so only the formula column(s) are returned.A * 100) now routes through the composed path (single-branch union pivot). Per-series branches stripformulasto avoid recursion.seriesReturnType: 'ratio'(the two are mutually exclusive in the editor; the renderer stays deterministic on a hand-built config carrying both).builderToRawSql: formula configs are rejected from "convert to SQL" with a clear message (same limitation as multi-series metric charts).Alerts on formula tiles work with no changes since they query through
queryChartConfig→renderChartConfig.Testing
make ci-lint,make ci-unitpass.compileFormulaAst(precedence, nested divisions, unary minus, HDX-4938 example) and SQL snapshot tests for the formula projection (grouped, hidden operands, single-series routing, formula-vs-ratio precedence, alias collision suffixing, invalid-formula error).queryChartConfig.int.test.ts(all passing against the docker ClickHouse, with the HDX-5076/5077 regression baseline unchanged):A / (A + B + C) * 100seriesReturnType: 'ratio'test for drop-in parity)How to test on Vercel preview
N/A — query-rendering change in common-utils; the chart editor UI for formulas lands in HDX-5080.
References