OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
chart: alias the one_event_per_user subquery as the events alias
Carl-Gerhard Lindesvärd4d40c637d57f
diff --git a/packages/core/src/modules/chart/src/sql.proof.md b/packages/core/src/modules/chart/src/sql.proof.mdindex 5ac7400c7..855d9e88f 100644--- a/packages/core/src/modules/chart/src/sql.proof.md+++ b/packages/core/src/modules/chart/src/sql.proof.md@@ -1,5 +1,17 @@# sql.ts — V1 → V2 result-set proof (M7-003)+> **Amended 2026-09-23 (port of main `a2cbf2e8`).** Two of the five "pre-existing+> V1 defects" below are no longer reproduced: `one_event_per_user` now aliases+> its subquery `e` instead of `subQuery`, and the aggregate shape no longer+> re-emits the outer WHERE. Defects 2 and 3 in the list below, and the cases at+> "chartSeriesQuery — all-cohorts breakdown + one_event_per_user" and+> "aggregateChartQuery — one_event_per_user + breakdown path", therefore record+> V1's behaviour and V2's behaviour *before* that port. Upstream fixed them in+> main after this proof was taken; the entries are kept as the record of what+> the conversion itself did, not as a description of the current code.+> Coverage for the new behaviour is `sql.test.ts`'s two `one_event_per_user`+> cases, which EXPLAIN against local ClickHouse and fail against the old alias.+Every query builder in `sql.ts` was executed twice against the same data — once as V1 (the `git HEAD` `packages/db/src/services/chart.service.ts` `getChartSql` / `getAggregateChartSql`, and the `packages/trpc/src/routers/chart.ts` clix / `createSqlBuilder` texts, `f1573b1a`) and once as V2 (the fragment returned by the core builder, bound through `query_params`) — through one `@clickhouse/client` with `format: 'JSON'` and the same `session_timezone`. The two responses were compared on `data` (every row, every column, as JSON) and `meta` (column names and types). Chart statements were compared as row **sets**: V1's `ORDER BY date ASC WITH FILL` leaves rows sharing a `date` in arbitrary order, and three V1-vs-V1 runs of the 191-row wildcard-breakdown case produced three different orders; the engine groups rows by label, so order is not part of the contract. Statements without an `ORDER BY` (values lists, getProfiles) were compared as sets for the same reason.- **Date**: 2026-09-04. **Data**: local prod-copy `openpanel` (319,499,000 events, static). Projects: `verdict` / `secure-privacy` (UTC), `bayse` (America/New_York), `website-8103` (Asia/Kolkata), `dream-mate` (Australia/Sydney), `authc-project-a1` (the only project with a Postgres cohort; `cohort_members` is empty in the prod-copy, so cohort cases prove statement equivalence on 0 rows — positive-row coverage is `sql.test.ts` on `openpanel_test`).diff --git a/packages/core/src/modules/chart/src/sql.test.ts b/packages/core/src/modules/chart/src/sql.test.tsindex ff43ef4db..3285068a7 100644--- a/packages/core/src/modules/chart/src/sql.test.ts+++ b/packages/core/src/modules/chart/src/sql.test.ts@@ -322,6 +322,20 @@ describe('getChartSql', () => {);});+ // main a2cbf2e8: the subquery was aliased `subQuery`, so the `e` every+ // breakdown SELECT is built with was out of scope and the chart failed+ // with UNKNOWN_IDENTIFIER on `e.properties`.+ it('one_event_per_user + a property breakdown resolves the events alias', async () => {+ const rendered = await getChartSql({+ event: event({ segment: 'one_event_per_user' }),+ breakdowns: [breakdown('properties.__query.utm_source')],+ ...base,+ });+ expect(rendered.sql).toContain(') as e');+ expect(rendered.sql).not.toContain('subQuery');+ await explain(rendered);+ });+// Regressions from HyperDX 2026-05-14 → 2026-05-17 ClickHouse error log.// Saved reports / older clients send field names that don't match the events// schema; the chart service used to inline them verbatim, crashing parse.@@ -411,6 +425,19 @@ describe('getAggregateChartSql', () => {await explain(rendered);});+ it('one_event_per_user + a group filter drops the outer WHERE', async () => {+ const rendered = await getAggregateChartSql({+ event: event({+ segment: 'one_event_per_user',+ filters: [{ name: 'group.plan', operator: 'is', value: ['pro'] }],+ }),+ breakdowns: [breakdown('properties.__query.utm_source')],+ ...base,+ });+ expect(rendered.sql).toContain(') as e');+ await explain(rendered);+ });+it('properties + group breakdown is unambiguous', async () => {const rendered = await getAggregateChartSql({event: event({diff --git a/packages/core/src/modules/chart/src/sql.ts b/packages/core/src/modules/chart/src/sql.tsindex 896b4f849..0f5113c80 100644--- a/packages/core/src/modules/chart/src/sql.ts+++ b/packages/core/src/modules/chart/src/sql.ts@@ -434,11 +434,17 @@ function countExpression(return sql`count(*) as count`;}+// Aliased `e`, not `subQuery`: breakdown SELECT expressions are always built+// with the events alias, so `e.properties` was out of scope in the outer+// query and any chart combining this segment with a property breakdown failed+// with UNKNOWN_IDENTIFIER. The subquery already does `SELECT * FROM events e`,+// so every `e.`-qualified reference outside it resolves unchanged+// (main a2cbf2e8).function oneEventPerUserFrom(body: ChartBody): SqlFragment {return sql`(SELECT DISTINCT ON (profile_id) * from ${sql.id(CHART_TABLE.events)} e ${joinsClause(body.joins)} WHERE ${sql.join(clauses(body.where), ' AND ')}ORDER BY profile_id, created_at DESC- ) as subQuery`;+ ) as e`;}function dateRangeWhere(@@ -522,8 +528,8 @@ export function chartSeriesQuery(input: ChartSeriesQueryInput): SqlFragment {body.select.count = countExpression(event, body, undefined);if (event.segment === 'one_event_per_user') {- // Filters were applied inside the subquery and the `e` alias is out of- // scope outside it, so neither joins nor WHERE are re-emitted.+ // Filters were applied inside the subquery, so neither joins nor WHERE+ // are re-emitted; the subquery carries the `e` alias outward.const from = oneEventPerUserFrom(body);return sql`${withClause(body.ctes)}${selectClause(body.select)} FROM ${from} ${groupByClause(groupBy)} ${orderByClause(orderBy)} ${fill}`;}@@ -566,10 +572,12 @@ export function aggregateChartQuery(const head = sql`${withClause(body.ctes)}${selectClause(body.select)}`;if (event.segment === 'one_event_per_user') {- // V1 re-emitted its WHERE after the subquery here (unlike the series- // shape); kept as is — converting a query is not the place to change it.+ // The filters were applied inside the subquery; re-emitting the outer+ // WHERE made it reference aliases the subquery dropped (a profile or+ // group filter, or the ARRAY JOIN's _group_id). Same shape as the series+ // branch below (main a2cbf2e8).const from = oneEventPerUserFrom(body);- return sql`${head} FROM ${from} ${whereClause(body.where)} ${groupByClause(groupBy)}`;+ return sql`${head} FROM ${from} ${groupByClause(groupBy)}`;}orderBy.count = sql`count DESC`;