OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M7-003): chart engine + chart.service + trpc chart into core (riskiest read path, part 1)
ralphc4760ac2c0b1
diff --git a/packages/core/package.json b/packages/core/package.jsonindex 9c037a5e4..49724655e 100644--- a/packages/core/package.json+++ b/packages/core/package.json@@ -48,6 +48,7 @@"elysia": "1.4.30","ipaddr.js": "^2.5.0","lru-cache": "^11.2.4",+ "mathjs": "^12.3.2","nanoid": "^5.1.6","pino": "catalog:","pino-pretty": "catalog:",diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex b886c06b2..5a9e8c648 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -237,6 +237,81 @@ export {verifyPasswordHash,verifyTotpCode,} from './modules/auth/auth.service';+// Dissolved from @openpanel/db's services/chart.service.ts and engine/+// (M7-003) — packages/trpc's chart router, apps/api's export controller,+// V1's funnel/conversion/sankey/retention/overview services and the+// mcp/assistant tools reach the engine and the field/filter compilers here.+// packages/db/src/services/chart.service.ts and src/engine/index.ts stay+// re-export shims.+export type {+ AggregateChartSqlInput,+ ChartBucketProfilesInput,+ ChartBucketProfilesRequest,+ ChartEventOption,+ ChartService,+ ChartSqlInput,+ CohortMetadata,+ ConcreteSeries,+ FilterTableScope,+ FunnelStepProfilesRequest,+ Plan,+ ProjectCardChartRow,+ ProjectCardMetrics,+ ProjectCardTrend,+ RetentionChartInput,+ SeriesDefinition,+ ShareableReportInput,+} from './modules/chart/chart.service';+export {+ AggregateChartEngine,+ buildAllCohortsLabelExpr,+ buildAllCohortsMembershipQuery,+ buildCohortMembershipQuery,+ buildInlineCohortJoin,+ CHART_TABLE,+ ChartCohortIdError,+ ChartEngine,+ collectBreakdownCohortIds,+ collectProfilePropertyKeys,+ createChartService,+ EVENT_FIELD_ALIASES,+ EVENT_TOP_LEVEL_COLUMNS,+ evaluateFormula,+ executeAggregateChart,+ executeChart,+ extractCohortId,+ fetchCohortsMetadata,+ fetchProjectCohorts,+ getAggregateChartSql,+ getChartBucketProfiles,+ getChartPropertyValues,+ getChartSql,+ getCohortAlias,+ getCohortCteName,+ getConversionChart,+ getEventFiltersWhereClause,+ getFunnelChart,+ getFunnelStepProfiles,+ getGroupPropertySelect,+ getGroupPropertySql,+ getProfilePropertySelect,+ getProjectCard,+ getRetentionChart,+ getSankeyChart,+ getSelectPropertyKey,+ InvalidFormulaError,+ isAllCohortsBreakdown,+ isKnownEventField,+ isNumericColumn,+ isValidFormula,+ listChartEvents,+ listChartProperties,+ normalizeEventField,+ profilePropertiesCteSelect,+ resolveReportInput,+ rewriteProfilePropertyRefs,+ transformPropertyKey,+} from './modules/chart/chart.service';// Dissolved from @openpanel/db's services/clients.service.ts (M6-002) —// packages/trpc's client router, apps/api's manage controller and mcp/utils// auth call these directly, the same way V1 reaches every other dissolveddiff --git a/packages/core/src/modules/chart/chart.rpc.ts b/packages/core/src/modules/chart/chart.rpc.tsnew file mode 100644index 000000000..4ca06f326--- /dev/null+++ b/packages/core/src/modules/chart/chart.rpc.ts@@ -0,0 +1,243 @@+// Ported from packages/trpc/src/routers/chart.ts (M7-003).+//+// Same arrangement as event.rpc.ts: V1's `protectedProcedure` / `cacheMiddleware`+// stacks land with auth (P6), so each procedure does its own "is anyone logged+// in" + `requireProjectAccess` off the `projectId` input. packages/trpc's+// chart router delegates its handler bodies onto `./chart.service` while+// keeping V1's own procedure stack and 60s response cache.+//+// V1's `chartProcedure` (funnel, conversion, chart, aggregate, cohort) also+// admits anonymous callers holding a valid share: `shareId` + `id` resolve the+// saved report, and the request renders that report (the caller may only move+// the date window). `resolveShare` is that middleware, inlined.++import type { IServiceReport } from '@openpanel/db/src/services/reports.service';+import {+ type IReportInput,+ zChartEventFilter,+ zChartSeries,+ zCriteria,+ zRange,+ zReportInput,+ zTimeInterval,+} from '@openpanel/validation';+import { z } from 'zod';+import { createTRPCRouter, procedure, type TrpcContext } from '../../rpc/base';+import { TRPCAccessError, TRPCForbiddenError } from '../../rpc/errors';+import { validateShareAccess } from '../share/share.service';+import {+ executeAggregateChart,+ executeChart,+ getChartBucketProfiles,+ getChartPropertyValues,+ getConversionChart,+ getFunnelChart,+ getFunnelStepProfiles,+ getProjectCard,+ getRetentionChart,+ getSankeyChart,+ listChartEvents,+ listChartProperties,+ resolveReportInput,+} from './chart.service';++const zShareable = z.object({+ shareId: z.string().optional(),+ id: z.string().optional(),+});++const zShareableReportInput = zReportInput.and(zShareable);++function loadAccessChecks() {+ return import('./src/access');+}++function loadReportsService() {+ return import('@openpanel/db/src/services/reports.service');+}++function requireLogin(userId: string | null | undefined): string {+ if (!userId) {+ throw new TRPCAccessError('Not authenticated');+ }+ return userId;+}++async function requireAccess(+ userId: string,+ projectId: string,+ level: 'read' | 'write'+) {+ const { requireProjectAccess } = await loadAccessChecks();+ await requireProjectAccess({ userId, projectId, level });+}++async function requireReadAccess(ctx: TrpcContext, projectId: string) {+ await requireAccess(requireLogin(ctx.session.userId), projectId, 'read');+}++/**+ * Share-aware access: with `shareId`, the share must be valid for the report+ * and the saved report is returned; without it, the caller must be a member.+ */+async function resolveShare(+ ctx: TrpcContext,+ input: { projectId: string; shareId?: string; id?: string }+): Promise<NonNullable<IServiceReport> | null> {+ if (!input.shareId) {+ await requireReadAccess(ctx, input.projectId);+ return null;+ }+ if (!input.id) {+ throw new Error('reportId required with shareId');+ }++ const shareValidation = await validateShareAccess(input.shareId, input.id, {+ cookies: ctx.cookies,+ session: ctx.session.userId ? { userId: ctx.session.userId } : undefined,+ });+ if (!shareValidation.isValid) {+ throw new TRPCForbiddenError('You do not have access to this share');+ }++ const { getReportById } = await loadReportsService();+ const report = await getReportById(input.id);+ if (!report) {+ throw new TRPCAccessError('Report not found');+ }+ return report;+}++async function resolveShareableReport(+ ctx: TrpcContext,+ input: IReportInput & { shareId?: string; id?: string }+): Promise<IReportInput> {+ return resolveReportInput(await resolveShare(ctx, input), input);+}++export const chartRouter = createTRPCRouter({+ projectCard: procedure+ .input(z.object({ projectId: z.string() }))+ .query(async ({ input, ctx }) => {+ await requireReadAccess(ctx, input.projectId);+ return getProjectCard(input.projectId);+ }),++ events: procedure+ .input(z.object({ projectId: z.string() }))+ .query(async ({ input, ctx }) => {+ await requireReadAccess(ctx, input.projectId);+ return listChartEvents(input.projectId);+ }),++ properties: procedure+ .input(z.object({ event: z.string().optional(), projectId: z.string() }))+ .query(async ({ input, ctx }) => {+ await requireReadAccess(ctx, input.projectId);+ return listChartProperties(input);+ }),++ values: procedure+ .input(+ z.object({+ event: z.string(),+ property: z.string(),+ projectId: z.string(),+ })+ )+ .query(async ({ input, ctx }) => {+ await requireReadAccess(ctx, input.projectId);+ return getChartPropertyValues(input);+ }),++ funnel: procedure+ .input(zShareableReportInput)+ .query(async ({ input, ctx }) =>+ getFunnelChart(await resolveShareableReport(ctx, input))+ ),++ conversion: procedure+ .input(zShareableReportInput)+ .query(async ({ input, ctx }) =>+ getConversionChart(await resolveShareableReport(ctx, input))+ ),++ sankey: procedure.input(zReportInput).query(async ({ input, ctx }) => {+ await requireReadAccess(ctx, input.projectId);+ return getSankeyChart(input);+ }),++ chart: procedure+ .input(zShareableReportInput)+ .query(async ({ input, ctx }) =>+ executeChart(await resolveShareableReport(ctx, input))+ ),++ aggregate: procedure+ .input(zShareableReportInput)+ .query(async ({ input, ctx }) =>+ executeAggregateChart(await resolveShareableReport(ctx, input))+ ),++ cohort: procedure+ .input(+ z+ .object({+ projectId: z.string(),+ firstEvent: z.array(z.string()).min(1),+ secondEvent: z.array(z.string()).min(1),+ criteria: zCriteria.default('on_or_after'),+ startDate: z.string().nullish(),+ endDate: z.string().nullish(),+ interval: zTimeInterval.default('day'),+ range: zRange,+ filters: z.array(zChartEventFilter).optional(),+ })+ .and(zShareable)+ )+ .query(async ({ input, ctx }) =>+ getRetentionChart(await resolveShare(ctx, input), input)+ ),++ getProfiles: procedure+ .input(+ z.object({+ projectId: z.string(),+ date: z.string().describe('The date for the data point (ISO string)'),+ interval: zTimeInterval.default('day'),+ series: zChartSeries,+ breakdowns: z.record(z.string(), z.string()).optional(),+ })+ )+ .query(async ({ input, ctx }) => {+ await requireReadAccess(ctx, input.projectId);+ return getChartBucketProfiles(input);+ }),++ getFunnelProfiles: procedure+ .input(+ z.object({+ projectId: z.string(),+ startDate: z.string().nullish(),+ endDate: z.string().nullish(),+ series: zChartSeries,+ stepIndex: z.number().describe('0-based index of the funnel step'),+ showDropoffs: z+ .boolean()+ .optional()+ .default(false)+ .describe(+ 'If true, show users who dropped off at this step. If false, show users who completed at least this step.'+ ),+ funnelWindow: z.number().optional(),+ funnelGroup: z.string().optional(),+ breakdowns: z.array(z.object({ name: z.string() })).optional(),+ breakdownValues: z.array(z.string()).optional(),+ range: zRange,+ })+ )+ .query(async ({ input, ctx }) => {+ await requireReadAccess(ctx, input.projectId);+ return getFunnelStepProfiles(input);+ }),+});diff --git a/packages/core/src/modules/chart/chart.service.test.ts b/packages/core/src/modules/chart/chart.service.test.tsnew file mode 100644index 000000000..6d7ecace8--- /dev/null+++ b/packages/core/src/modules/chart/chart.service.test.ts@@ -0,0 +1,399 @@+/**+ * Integration tests for the chart module against the isolated `openpanel_test`+ * databases (pinned by test/preload.ts) — the positive-row counterpart of+ * src/chart.sql.test.ts's EXPLAIN checks, and the one place the cohort and+ * all-cohorts paths run against seeded `cohort_members` + a Postgres cohort.+ *+ * Fixture (test/fixtures.ts, seeded per suite under its own project id so it+ * can run concurrently with V1's suites): Alice — 3 events 2 days ago+ * (Chrome, page_view /home); Charlie — 5 events 5 days ago (Firefox,+ * screen_view + page_view /shop, purchase 9900); Bob — no events. This suite+ * adds one static cohort ("Firefox users") holding Charlie.+ */++import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test';+import type { IChartEventItem, IReportInput } from '@openpanel/validation';++const TEST_PROJECT_ID = 'chart-integration-test';+const TEST_ORG_ID = 'chart-integration-org';+const COHORT_ID = 'dddddddd-0000-4000-8000-000000000001';+const COHORT_NAME = 'Firefox users';+const DAY_MS = 24 * 60 * 60 * 1000;+const PROJECT_CARD_DAYS = 90;++// Bypass Redis caching so a stale entry from another run cannot gate an+// assertion. Captured before mock.module runs — see mcp's tools.test.ts.+const actualRedis = await import('@openpanel/redis');+mock.module('@openpanel/redis', () => ({+ ...actualRedis,+ getCache: async <T>(_key: string, _ttl: number, fn: () => Promise<T>) => fn(),+}));++let FIXTURE: typeof import('../../../../../test/fixtures').FIXTURE;+let service: typeof import('./chart.service');+let ch: typeof import('@openpanel/db/src/clickhouse/client').ch;++type EventSeriesItem = Extract<IChartEventItem, { type: 'event' }>;++const pageView = (+ overrides: Partial<EventSeriesItem> = {}+): EventSeriesItem => ({+ type: 'event',+ id: 'A',+ name: 'page_view',+ segment: 'event',+ filters: [],+ ...overrides,+});++const report = (overrides: Partial<IReportInput> = {}): IReportInput => ({+ projectId: TEST_PROJECT_ID,+ chartType: 'linear',+ interval: 'day',+ series: [pageView()],+ breakdowns: [],+ range: '30d',+ previous: false,+ metric: 'sum',+ ...overrides,+});++beforeAll(async () => {+ const fixtures = await import('../../../../../test/fixtures');+ FIXTURE = fixtures.FIXTURE;+ ({ ch } = await import('@openpanel/db/src/clickhouse/client'));+ service = await import('./chart.service');++ const { bootstrapTestDatabases } = await import(+ '../../../../../test/bootstrap-databases'+ );+ await bootstrapTestDatabases();+ await fixtures.setupPostgresFixtures(TEST_PROJECT_ID, TEST_ORG_ID);+ await fixtures.setupFixtures(TEST_PROJECT_ID);++ const { db } = await import('@openpanel/db/src/prisma-client');+ await db.cohort.upsert({+ where: { id: COHORT_ID },+ create: {+ id: COHORT_ID,+ name: COHORT_NAME,+ projectId: TEST_PROJECT_ID,+ isStatic: true,+ },+ update: { name: COHORT_NAME, projectId: TEST_PROJECT_ID },+ });+ await ch.command({+ query: `DELETE FROM cohort_members WHERE project_id = '${TEST_PROJECT_ID}'`,+ });+ await ch.insert({+ table: 'cohort_members',+ values: [+ {+ project_id: TEST_PROJECT_ID,+ cohort_id: COHORT_ID,+ profile_id: FIXTURE.profiles.charlie,+ matching_properties: {},+ version: 1,+ },+ ],+ format: 'JSONEachRow',+ });+}, 30_000);++afterAll(async () => {+ const fixtures = await import('../../../../../test/fixtures');+ const { db } = await import('@openpanel/db/src/prisma-client');+ await ch.command({+ query: `DELETE FROM cohort_members WHERE project_id = '${TEST_PROJECT_ID}'`,+ });+ await db.cohort.deleteMany({ where: { id: COHORT_ID } });+ await fixtures.teardownFixtures(TEST_PROJECT_ID);+ await fixtures.teardownPostgresFixtures(TEST_PROJECT_ID, TEST_ORG_ID);+});++function seriesNamed<TSerie extends { names: string[] }>(+ chart: { series: TSerie[] },+ label: string+): TSerie | undefined {+ return chart.series.find((serie) => serie.names.includes(label));+}++describe('executeChart', () => {+ it('counts both page views over the last 30 days', async () => {+ const chart = await service.executeChart(report());++ expect(chart.series).toHaveLength(1);+ expect(chart.series[0]?.names).toEqual(['page_view']);+ expect(chart.metrics.sum).toBe(2);+ expect(+ chart.series[0]?.data.filter((point) => point.count > 0)+ ).toHaveLength(2);+ });++ it('breaks down by browser', async () => {+ const chart = await service.executeChart(+ report({ breakdowns: [{ id: 'b', name: 'browser' }] })+ );++ expect(seriesNamed(chart, 'Chrome')?.metrics.sum).toBe(1);+ expect(seriesNamed(chart, 'Firefox')?.metrics.sum).toBe(1);+ expect(seriesNamed(chart, 'Chrome')?.event.breakdowns).toEqual({+ browser: 'Chrome',+ });+ });++ it('filters on cohort membership (inCohort / notInCohort)', async () => {+ const inCohort = await service.executeChart(+ report({+ series: [+ pageView({+ filters: [+ {+ id: 'c',+ name: 'cohort',+ operator: 'inCohort',+ value: [],+ cohortIds: [COHORT_ID],+ },+ ],+ }),+ ],+ })+ );+ const notInCohort = await service.executeChart(+ report({+ series: [+ pageView({+ filters: [+ {+ id: 'c',+ name: 'cohort',+ operator: 'notInCohort',+ value: [],+ cohortIds: [COHORT_ID],+ },+ ],+ }),+ ],+ })+ );++ expect(inCohort.metrics.sum).toBe(1);+ expect(notInCohort.metrics.sum).toBe(1);+ });++ it('breaks down by a single cohort and by all cohorts', async () => {+ const single = await service.executeChart(+ report({ breakdowns: [{ id: 'b', name: `cohort:${COHORT_ID}` }] })+ );+ const all = await service.executeChart(+ report({ breakdowns: [{ id: 'b', name: 'cohort' }] })+ );++ // Alice is outside the cohort, Charlie inside — both rows must survive.+ expect(single.metrics.sum).toBe(2);+ expect(single.series).toHaveLength(2);+ // All-cohorts is an INNER JOIN on membership (V1): non-members drop out.+ expect(all.series.map((serie) => [serie.names, serie.metrics.sum])).toEqual(+ [[['page_view', COHORT_NAME], 1]]+ );+ });++ it('narrows profile properties into the profile CTE and still matches', async () => {+ const chart = await service.executeChart(+ report({+ series: [+ pageView({+ filters: [+ {+ id: 'p',+ name: 'profile.properties.browser',+ operator: 'is',+ value: ['Firefox'],+ },+ ],+ }),+ ],+ breakdowns: [{ id: 'b', name: 'profile.properties.country' }],+ })+ );++ expect(chart.metrics.sum).toBe(1);+ expect(seriesNamed(chart, 'US')?.metrics.sum).toBe(1);+ });+});++describe('executeAggregateChart', () => {+ it('sums revenue per browser without a time axis', async () => {+ const chart = await service.executeAggregateChart(+ report({+ chartType: 'bar',+ series: [+ {+ type: 'event',+ id: 'A',+ name: 'purchase',+ segment: 'property_sum',+ property: 'revenue',+ filters: [],+ },+ ],+ breakdowns: [{ id: 'b', name: 'browser' }],+ })+ );++ expect(chart.series).toHaveLength(1);+ expect(seriesNamed(chart, 'Firefox')?.metrics.sum).toBe(9900);+ expect(chart.series[0]?.data).toHaveLength(1);+ });+});++describe('picker queries', () => {+ it('lists event names with counts, `*` first', async () => {+ const events = await service.listChartEvents(TEST_PROJECT_ID);++ // Counts come from distinct_event_names_mv (V1): one row per name and+ // insert part, not one per event.+ expect(events[0]?.name).toBe('*');+ expect(events[0]?.count).toBe(+ events.slice(1).reduce((total, event) => total + event.count, 0)+ );+ expect(+ events+ .slice(1)+ .map((event) => event.name)+ .sort()+ ).toEqual([+ 'page_view',+ 'purchase',+ 'screen_view',+ 'session_end',+ 'session_start',+ ]);+ });++ it('lists filterable properties, `name` only for the wildcard event', async () => {+ const all = await service.listChartProperties({+ projectId: TEST_PROJECT_ID,+ event: '*',+ });+ const single = await service.listChartProperties({+ projectId: TEST_PROJECT_ID,+ event: 'page_view',+ });++ expect(all).toContain('name');+ expect(all).toContain('path');+ expect(all).toContain('profile.email');+ expect(new Set(all).size).toBe(all.length);+ expect(single).not.toContain('name');+ });++ it('lists values for a top-level column, a profile property and has_profile', async () => {+ const path = await service.getChartPropertyValues({+ projectId: TEST_PROJECT_ID,+ event: 'page_view',+ property: 'path',+ });+ const profileBrowser = await service.getChartPropertyValues({+ projectId: TEST_PROJECT_ID,+ event: '*',+ property: 'profile.properties.browser',+ });+ const hasProfile = await service.getChartPropertyValues({+ projectId: TEST_PROJECT_ID,+ event: '*',+ property: 'has_profile',+ });+ const unknown = await service.getChartPropertyValues({+ projectId: TEST_PROJECT_ID,+ event: '*',+ property: 'totally_made_up_column',+ });++ expect(path.values.sort()).toEqual(['/home', '/shop']);+ expect(profileBrowser.values.sort()).toEqual(['Chrome', 'Firefox']);+ expect(hasProfile.values).toEqual(['true', 'false']);+ expect(unknown.values).toEqual([]);+ });+});++describe('getProjectCard', () => {+ it('returns a filled 3-month chart, metrics and a trend', async () => {+ const card = await service.getProjectCard(TEST_PROJECT_ID);++ expect(card.chart.length).toBeGreaterThanOrEqual(PROJECT_CARD_DAYS);+ expect(card.chart[0]?.date).toBeInstanceOf(Date);+ expect(card.metrics?.months_3).toBe(2);+ expect(card.trend.direction).toBe('neutral');+ });+});++describe('getChartBucketProfiles', () => {+ it('returns the profiles behind one data point, honoring breakdowns', async () => {+ const twoDaysAgo = new Date(Date.now() - 2 * DAY_MS).toISOString();+ const profiles = await service.getChartBucketProfiles({+ projectId: TEST_PROJECT_ID,+ date: twoDaysAgo,+ interval: 'day',+ series: [pageView()],+ breakdowns: { browser: 'Chrome' },+ });+ const none = await service.getChartBucketProfiles({+ projectId: TEST_PROJECT_ID,+ date: twoDaysAgo,+ interval: 'day',+ series: [pageView()],+ breakdowns: { browser: 'Firefox' },+ });++ expect(profiles.map((profile) => profile.id)).toEqual([+ FIXTURE.profiles.alice,+ ]);+ expect(none).toEqual([]);+ });++ it('selects only whitelisted profile columns for profile.* references', async () => {+ const twoDaysAgo = new Date(Date.now() - 2 * DAY_MS).toISOString();+ const profiles = await service.getChartBucketProfiles({+ projectId: TEST_PROJECT_ID,+ date: twoDaysAgo,+ interval: 'day',+ series: [+ pageView({+ filters: [+ {+ id: 'p',+ name: 'profile.email',+ operator: 'is',+ value: ['[email protected]'],+ },+ ],+ }),+ ],+ });++ expect(profiles.map((profile) => profile.id)).toEqual([+ FIXTURE.profiles.alice,+ ]);+ await expect(+ service.getChartBucketProfiles({+ projectId: TEST_PROJECT_ID,+ date: twoDaysAgo,+ interval: 'day',+ series: [+ pageView({+ filters: [+ {+ id: 'p',+ name: 'profile.evil; DROP TABLE profiles',+ operator: 'is',+ value: ['x'],+ },+ ],+ }),+ ],+ })+ ).rejects.toThrow(/Refusing to inline identifier/);+ });+});diff --git a/packages/core/src/modules/chart/chart.service.ts b/packages/core/src/modules/chart/chart.service.tsnew file mode 100644index 000000000..2e052dae0--- /dev/null+++ b/packages/core/src/modules/chart/chart.service.ts@@ -0,0 +1,762 @@+// Ported from packages/db/src/services/chart.service.ts, packages/db/src/+// engine/* and the handler bodies of packages/trpc/src/routers/chart.ts+// (M7-003). Every ClickHouse statement this module runs is a `sql` fragment+// from src/chart.sql.ts, proven byte-equivalent to V1 in+// src/chart.sql.proof.md; V1's chart.service and engine are re-export shims+// onto this module and its router delegates here (DELEGATE PATTERN).+//+// `funnel`, `conversion`, `sankey`, `cohort` and `getFunnelProfiles` still+// run V1's funnel/conversion/sankey/retention services — those queries are+// M7-004/M7-005's, and are reached lazily through @openpanel/db until then.+//+// db/ch access is lazy (`load*`), as in the sibling modules: the barrel pulls+// this module into nearly every core test file, and constructing+// @openpanel/db's clients at import time costs a pino-pretty worker per file.++import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';+import {+ getChartPrevStartEndDate,+ getChartStartEndDate,+} from '@openpanel/db/src/services/date.service';+import type { IServiceReport } from '@openpanel/db/src/services/reports.service';+import type {+ FinalChart,+ IChartEventFilter,+ IChartRange,+ IChartSeries,+ ICriteria,+ IInterval,+ IReportInput,+} from '@openpanel/validation';+import { flatten, map, pipe, prop, sort, uniq } from 'ramda';+import sqlstring from 'sqlstring';+import type { ServiceDeps } from '../../services';+import { getEventMetasCached } from '../event/event.service';+import { getSettingsForProject } from '../organization/organization.service';+import {+ getProfilePropertyKeysCached,+ getProfilesCached,+ type IServiceProfile,+} from '../profile/profile.service';+import {+ chartBucketProfilesQuery,+ eventFieldValuesQuery,+ eventNamesWithCountQuery,+ eventPropertyKeysQuery,+ eventPropertyValuesQuery,+ groupPropertyValuesQuery,+ profilePropertyValuesQuery,+ projectCardChartQuery,+ projectCardMetricsQuery,+} from './src/chart.sql';+import { formatClickhouseDate } from './src/dates';+import { executeAggregateChart, executeChart } from './src/engine/execute';+import {+ getGroupPropertySelect,+ getProfilePropertySelect,+ getSelectPropertyKey,+ isKnownEventField,+ normalizeEventField,+} from './src/field-resolution';++export {+ type ChartBucketProfilesInput,+ ChartCohortIdError,+} from './src/chart.sql';+export {+ type AggregateChartSqlInput,+ type ChartSqlInput,+ fetchCohortsMetadata,+ fetchProjectCohorts,+ getAggregateChartSql,+ getChartSql,+} from './src/chart-statement';+export {+ AggregateChartEngine,+ ChartEngine,+ executeAggregateChart,+ executeChart,+} from './src/engine/execute';+export {+ evaluateFormula,+ InvalidFormulaError,+ isValidFormula,+} from './src/engine/formula';+export type {+ ConcreteSeries,+ Plan,+ SeriesDefinition,+} from './src/engine/types';+export {+ buildAllCohortsLabelExpr,+ buildAllCohortsMembershipQuery,+ buildCohortMembershipQuery,+ buildInlineCohortJoin,+ CHART_TABLE,+ type CohortMetadata,+ collectBreakdownCohortIds,+ collectProfilePropertyKeys,+ EVENT_FIELD_ALIASES,+ EVENT_TOP_LEVEL_COLUMNS,+ extractCohortId,+ getCohortAlias,+ getCohortCteName,+ getGroupPropertySelect,+ getGroupPropertySql,+ getProfilePropertySelect,+ getSelectPropertyKey,+ isAllCohortsBreakdown,+ isKnownEventField,+ isNumericColumn,+ normalizeEventField,+ profilePropertiesCteSelect,+ rewriteProfilePropertyRefs,+ transformPropertyKey,+} from './src/field-resolution';+export {+ type FilterTableScope,+ getEventFiltersWhereClause,+} from './src/filter-where';++/**+ * Cap on distinct event property keys returned to the picker. Projects in the+ * 8k range exist, so the previous 10k was reachable in normal use.+ */+const EVENT_PROPERTY_KEY_LIMIT = 50_000;++/**+ * Cap on distinct values returned per event property to the filter+ * autocomplete. High-cardinality keys (ids, urls, session tokens) can hold+ * millions of distinct values — most recent values win. Env-tunable via+ * EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT (positive integer; invalid values+ * keep the default).+ */+const DEFAULT_EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT = 500;+const EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT = (() => {+ const raw = process.env.EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT;+ const parsed = raw && /^\d+$/.test(raw) ? Number(raw) : Number.NaN;+ return Number.isSafeInteger(parsed) && parsed > 0+ ? parsed+ : DEFAULT_EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT;+})();++/** Profile lookups are batched so the `IN (...)` never exceeds max_query_size. */+const BUCKET_PROFILES_BATCH_SIZE = 200;+const FUNNEL_PROFILES_BATCH_SIZE = 500;+const FUNNEL_PROFILES_LIMIT = 1000;++const FIXED_FILTER_PROPERTIES = [+ 'revenue',+ 'has_profile',+ 'path',+ 'origin',+ 'referrer',+ 'referrer_name',+ 'created_at',+ 'country',+ 'city',+ 'region',+ 'os',+ 'os_version',+ 'browser',+ 'browser_version',+ 'device',+ 'brand',+ 'model',+ 'profile.id',+ 'profile.first_name',+ 'profile.last_name',+ 'profile.email',+ 'profile.created_at',+ 'profile.last_seen_at',+];++function loadChClient() {+ return import('@openpanel/db/src/clickhouse/client');+}++function loadFunnelService() {+ return import('@openpanel/db/src/services/funnel.service');+}++function loadConversionService() {+ return import('@openpanel/db/src/services/conversion.service');+}++function loadSankeyService() {+ return import('@openpanel/db/src/services/sankey.service');+}++function loadRetentionService() {+ return import('@openpanel/db/src/services/retention.service');+}++function loadReportsService() {+ return import('@openpanel/db/src/services/reports.service');+}++async function runQuery<T extends object>(+ statement: SqlFragment,+ timezone?: string+): Promise<T[]> {+ const { chQuery } = await loadChClient();+ return chQuery<T>(+ statement,+ timezone ? { session_timezone: timezone } : undefined+ );+}++async function getProfilesInBatches(+ ids: string[],+ projectId: string,+ batchSize: number+): Promise<IServiceProfile[]> {+ const profiles: IServiceProfile[] = [];+ for (let index = 0; index < ids.length; index += batchSize) {+ profiles.push(+ ...(await getProfilesCached(+ ids.slice(index, index + batchSize),+ projectId+ ))+ );+ }+ return profiles;+}++// --- shared report input -----------------------------------------------------++export type ShareableReportInput = IReportInput & {+ shareId?: string;+ id?: string;+};++/**+ * A shared report is rendered from its saved definition; the caller may only+ * move the date window.+ */+export function resolveReportInput(+ report: NonNullable<IServiceReport> | null,+ input: ShareableReportInput+): IReportInput {+ if (!report) {+ return input;+ }+ return {+ ...report,+ range: input.range ?? report.range,+ startDate: input.startDate ?? report.startDate,+ endDate: input.endDate ?? report.endDate,+ interval: input.interval ?? report.interval,+ };+}++// --- projectCard -------------------------------------------------------------++export interface ProjectCardChartRow {+ value: number;+ date: Date;+ revenue: number;+}+export interface ProjectCardMetrics {+ months_3: number;+ months_3_prev: number;+ month: number;+ day: number;+ day_prev: number;+ revenue: number;+}+export type ProjectCardTrend =+ | { direction: 'neutral'; percentage: number | null }+ | { direction: 'up' | 'down'; percentage: number };++function projectCardTrend(+ metrics: ProjectCardMetrics | undefined+): ProjectCardTrend {+ const change =+ metrics && metrics.months_3_prev > 0 && metrics.months_3 > 0+ ? Math.round(+ ((metrics.months_3 - metrics.months_3_prev) / metrics.months_3_prev) *+ 100+ )+ : null;+ if (change === null) {+ return { direction: 'neutral', percentage: null };+ }+ if (change > 0) {+ return { direction: 'up', percentage: change };+ }+ if (change < 0) {+ return { direction: 'down', percentage: Math.abs(change) };+ }+ return { direction: 'neutral', percentage: 0 };+}++export async function getProjectCard(projectId: string): Promise<{+ chart: ProjectCardChartRow[];+ metrics: ProjectCardMetrics | undefined;+ trend: ProjectCardTrend;+}> {+ const { timezone } = await getSettingsForProject(projectId);+ const [chart, [metrics]] = await Promise.all([+ runQuery<ProjectCardChartRow>(projectCardChartQuery(projectId), timezone),+ runQuery<ProjectCardMetrics>(projectCardMetricsQuery(projectId), timezone),+ ]);++ return {+ chart: chart.map((row) => ({ ...row, date: new Date(row.date) })),+ metrics,+ trend: projectCardTrend(metrics),+ };+}++// --- events / properties / values -------------------------------------------++export interface ChartEventOption {+ name: string;+ count: number;+ meta: Awaited<ReturnType<typeof getEventMetasCached>>[number] | undefined;+}++export async function listChartEvents(+ projectId: string+): Promise<ChartEventOption[]> {+ const [events, meta] = await Promise.all([+ runQuery<{ name: string; count: number }>(+ eventNamesWithCountQuery(projectId)+ ),+ getEventMetasCached(projectId),+ ]);++ return [+ {+ name: '*',+ count: events.reduce((total, event) => total + event.count, 0),+ meta: undefined,+ },+ ...events.map((event) => ({+ name: event.name,+ count: event.count,+ meta: meta.find((item) => item.name === event.name),+ })),+ ];+}++/** `items.0.price` -> `items[*].price`, so a picker entry covers every index. */+function toWildcardPropertyKey(key: string): string {+ return key.replace(/\.([0-9]+)\./g, '.*.').replace(/\.([0-9]+)/g, '[*]');+}++export async function listChartProperties(input: {+ projectId: string;+ event?: string;+}): Promise<string[]> {+ const { projectId, event } = input;+ const [profileKeys, eventKeys] = await Promise.all([+ getProfilePropertyKeysCached(projectId),+ runQuery<{ property_key: string; created_at: string }>(+ eventPropertyKeysQuery(projectId, event, EVENT_PROPERTY_KEY_LIMIT)+ ),+ ]);++ const properties = [+ ...eventKeys.map(+ (item) => `properties.${toWildcardPropertyKey(item.property_key)}`+ ),+ ...(event === '*' || !event ? ['name'] : []),+ ...FIXED_FILTER_PROPERTIES,+ ...profileKeys.map((key) => `profile.properties.${key}`),+ ];++ return pipe(+ sort<string>((a, b) => a.length - b.length),+ uniq+ )(properties);+}++function nonEmptyValues(rows: { values: string }[]): string[] {+ return rows.map((row) => String(row.values)).filter(Boolean);+}++export async function getChartPropertyValues(input: {+ projectId: string;+ event: string;+ property: string;+}): Promise<{ values: string[] }> {+ const { projectId, event, property } = input;++ if (property === 'has_profile') {+ return { values: ['true', 'false'] };+ }++ if (property.startsWith('properties.')) {+ const rows = await runQuery<{ property_value: string; created_at: string }>(+ eventPropertyValuesQuery(+ projectId,+ property.replace(/^properties\./, ''),+ event,+ EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT+ )+ );+ return { values: rows.map((row) => row.property_value) };+ }++ if (property.startsWith('profile.')) {+ const rows = await runQuery<{ values: string }>(+ profilePropertyValuesQuery(projectId, getProfilePropertySelect(property))+ );+ return { values: nonEmptyValues(rows) };+ }++ if (property.startsWith('group.')) {+ const rows = await runQuery<{ values: string }>(+ groupPropertyValuesQuery(projectId, getGroupPropertySelect(property))+ );+ return { values: nonEmptyValues(rows) };+ }++ // Cohort filters use a dedicated cohort multi-select on the client; a+ // `cohort:<uuid>` identifier would be a ClickHouse syntax error here.+ if (property === 'cohort' || property.startsWith('cohort:')) {+ return { values: [] };+ }++ // Unknown identifiers (saved-report typos, columns from older clients) get+ // an empty list rather than UNKNOWN_IDENTIFIER.+ const resolvedProperty = normalizeEventField(property);+ if (!isKnownEventField(resolvedProperty)) {+ return { values: [] };+ }+ const rows = await runQuery<{ values: string[] }>(+ eventFieldValuesQuery(+ projectId,+ getSelectPropertyKey(resolvedProperty),+ event+ )+ );+ return {+ values: pipe(+ (data: typeof rows) => map(prop('values'), data),+ flatten,+ uniq,+ sort((a, b) => a.length - b.length)+ )(rows),+ };+}++// --- funnel / conversion / sankey / cohort (V1 services, M7-004/M7-005) -----++async function currentAndPreviousPeriod(chartInput: IReportInput) {+ const { timezone } = await getSettingsForProject(chartInput.projectId);+ const currentPeriod = getChartStartEndDate(chartInput, timezone);+ const previousPeriod = getChartPrevStartEndDate(currentPeriod);+ return { timezone, currentPeriod, previousPeriod };+}++export async function getFunnelChart(chartInput: IReportInput) {+ const { funnelService } = await loadFunnelService();+ const { timezone, currentPeriod, previousPeriod } =+ await currentAndPreviousPeriod(chartInput);++ const [current, previous] = await Promise.all([+ funnelService.getFunnel({ ...chartInput, ...currentPeriod, timezone }),+ chartInput.previous+ ? funnelService.getFunnel({ ...chartInput, ...previousPeriod, timezone })+ : Promise.resolve(null),+ ]);++ return { current, previous };+}++export async function getConversionChart(chartInput: IReportInput) {+ const { conversionService } = await loadConversionService();+ const { timezone, currentPeriod, previousPeriod } =+ await currentAndPreviousPeriod(chartInput);+ const interval = chartInput.interval;++ const [current, previous] = await Promise.all([+ conversionService.getConversion({+ ...chartInput,+ ...currentPeriod,+ interval,+ timezone,+ }),+ chartInput.previous+ ? conversionService.getConversion({+ ...chartInput,+ ...previousPeriod,+ interval,+ timezone,+ })+ : Promise.resolve(null),+ ]);++ return {+ current: current.map((serie, serieIndex) => ({+ ...serie,+ data: serie.data.map((point, pointIndex) => ({+ ...point,+ previousRate: previous?.[serieIndex]?.data?.[pointIndex]?.rate,+ })),+ })),+ previous,+ };+}++export async function getSankeyChart(input: IReportInput) {+ const [{ sankeyService }, { mergeGlobalFilters, onlyReportEvents }] =+ await Promise.all([loadSankeyService(), loadReportsService()]);+ const { timezone } = await getSettingsForProject(input.projectId);+ const currentPeriod = getChartStartEndDate(input, timezone);++ const options = input.options;+ if (!options || options.type !== 'sankey') {+ throw new Error('Sankey options are required');+ }++ const eventSeries = onlyReportEvents(+ mergeGlobalFilters(input.series, input.globalFilters)+ );+ if (!eventSeries[0]) {+ throw new Error('Start and end events are required');+ }++ return sankeyService.getSankey({+ projectId: input.projectId,+ startDate: currentPeriod.startDate,+ endDate: currentPeriod.endDate,+ steps: options.steps,+ mode: options.mode,+ startEvent: eventSeries[0],+ endEvent: eventSeries[1],+ exclude: options.exclude || [],+ include: options.include,+ timezone,+ });+}++export interface RetentionChartInput {+ projectId: string;+ firstEvent: string[];+ secondEvent: string[];+ criteria: ICriteria;+ startDate?: string | null;+ endDate?: string | null;+ interval: IInterval;+ range: IChartRange;+ filters?: IChartEventFilter[];+}++/**+ * A shared retention report takes its events from the saved series: each+ * series' `filters[0]` is the event-name selector, everything else is an+ * audience filter.+ */+function retentionInputFromReport(+ report: NonNullable<IServiceReport>,+ input: RetentionChartInput+): RetentionChartInput {+ const retentionOptions =+ report.options?.type === 'retention' ? report.options : undefined;+ const eventSeries = onlyEventSeries(report.series);+ const firstEvent = (eventSeries[0]?.filters?.[0]?.value ?? []).map(String);+ const secondEvent = (eventSeries[1]?.filters?.[0]?.value ?? []).map(String);+ if (firstEvent.length === 0 || secondEvent.length === 0) {+ throw new Error('Report must have at least 2 event series');+ }++ return {+ projectId: report.projectId,+ firstEvent,+ secondEvent,+ criteria: retentionOptions?.criteria ?? input.criteria,+ range: input.range ?? report.range,+ startDate: input.startDate ?? report.startDate,+ endDate: input.endDate ?? report.endDate,+ interval: input.interval ?? report.interval,+ filters: [+ ...(report.globalFilters ?? []),+ ...eventSeries.flatMap((serie) =>+ (serie.filters ?? []).filter((filter) => filter.name !== 'name')+ ),+ ],+ };+}++function onlyEventSeries(series: NonNullable<IServiceReport>['series']) {+ return series.filter((item) => item.type === 'event');+}++export async function getRetentionChart(+ report: NonNullable<IServiceReport> | null,+ input: RetentionChartInput+) {+ const { getRetentionCohort } = await loadRetentionService();+ const resolved = report ? retentionInputFromReport(report, input) : input;++ const { timezone } = await getSettingsForProject(resolved.projectId);+ const dates = getChartStartEndDate(+ {+ range: resolved.range,+ startDate: resolved.startDate,+ endDate: resolved.endDate,+ },+ timezone+ );++ return getRetentionCohort({+ projectId: resolved.projectId,+ firstEvent: resolved.firstEvent,+ secondEvent: resolved.secondEvent,+ criteria: resolved.criteria,+ interval: resolved.interval,+ startDate: dates.startDate,+ endDate: dates.endDate,+ filters: resolved.filters ?? [],+ });+}++// --- drill-down profiles -----------------------------------------------------++export interface ChartBucketProfilesRequest {+ projectId: string;+ /** ISO string of the data point's bucket. */+ date: string;+ interval: IInterval;+ series: IChartSeries;+ breakdowns?: Record<string, string>;+}++/** Profiles behind one data point of a time-series chart (V1 `getProfiles`). */+export async function getChartBucketProfiles(+ input: ChartBucketProfilesRequest+): Promise<IServiceProfile[]> {+ const serie = input.series[0];+ if (!serie) {+ throw new Error('Series not found');+ }+ if (serie.type !== 'event') {+ throw new Error('Series must be an event');+ }++ const rows = await runQuery<{ profile_id: string }>(+ chartBucketProfilesQuery({+ projectId: input.projectId,+ bucketDate: formatClickhouseDate(new Date(input.date)),+ interval: input.interval,+ event: serie,+ breakdowns: input.breakdowns ?? {},+ })+ );+ const ids = rows.map((row) => row.profile_id).filter(Boolean);+ if (ids.length === 0) {+ return [];+ }+ return getProfilesInBatches(ids, input.projectId, BUCKET_PROFILES_BATCH_SIZE);+}++export interface FunnelStepProfilesRequest {+ projectId: string;+ startDate?: string | null;+ endDate?: string | null;+ series: IChartSeries;+ /** 0-based funnel step. */+ stepIndex: number;+ /** Users who dropped off at the step, instead of those who reached it. */+ showDropoffs?: boolean;+ funnelWindow?: number;+ funnelGroup?: string;+ breakdowns?: { name: string }[];+ breakdownValues?: string[];+ range: IChartRange;+}++/**+ * Profiles at (or dropping off at) one funnel step. Runs on V1's clix funnel+ * builder until the funnel module (M7-004) converts it: the CTE has to be the+ * chart's own, or breakdown expressions referencing a `profile` / `cohort_<id>`+ * alias this side never joined fail with UNKNOWN_IDENTIFIER.+ */+export async function getFunnelStepProfiles(+ input: FunnelStepProfilesRequest+): Promise<IServiceProfile[]> {+ const { funnelService, EMPTY_BREAKDOWN_LABEL } = await loadFunnelService();+ const { timezone } = await getSettingsForProject(input.projectId);+ const {+ projectId,+ series,+ stepIndex,+ showDropoffs = false,+ funnelWindow,+ funnelGroup,+ breakdowns: inputBreakdowns = [],+ breakdownValues = [],+ } = input;+ const { startDate, endDate } = getChartStartEndDate(input, timezone);+ // stepIndex is 0-based, level is 1-based.+ const targetLevel = stepIndex + 1;++ const { query, breakdowns } = await funnelService.buildFunnelBase({+ projectId,+ startDate,+ endDate,+ series,+ breakdowns: inputBreakdowns,+ funnelWindow,+ funnelGroup,+ timezone,+ });++ // Same shape as the chart's `funnel` CTE: windowFunnel is already computed+ // per primary key, so drop level=0 and select distinct profiles.+ query.with('funnel', 'SELECT * FROM session_funnel WHERE level != 0');+ query.select(['DISTINCT profile_id']).from('funnel');+ if (showDropoffs) {+ query.where('level', '=', targetLevel);+ } else {+ query.where('level', '>=', targetLevel);+ }++ // The clicked row carries DISPLAY labels (trimmed, empty/null shown as+ // EMPTY_BREAKDOWN_LABEL), so match against the same normalization.+ // toString/ifNull keep the comparison valid for numeric and Nullable+ // breakdown columns.+ breakdowns.forEach((_, index) => {+ const value = breakdownValues[index];+ if (value === undefined) {+ return;+ }+ const normalized = `trim(ifNull(toString(b_${index}), ''))`;+ if (value === EMPTY_BREAKDOWN_LABEL) {+ query.rawWhere(+ `(${normalized} = '' OR ${normalized} = ${sqlstring.escape(EMPTY_BREAKDOWN_LABEL)})`+ );+ } else {+ query.rawWhere(`${normalized} = ${sqlstring.escape(value)}`);+ }+ });++ query.limit(FUNNEL_PROFILES_LIMIT);++ const rows = (await query.execute()) as { profile_id: string }[];+ const ids = rows.map((row) => row.profile_id).filter(Boolean);+ if (ids.length === 0) {+ return [];+ }+ return getProfilesInBatches(ids, projectId, FUNNEL_PROFILES_BATCH_SIZE);+}++// --- service -----------------------------------------------------------------++export interface ChartService {+ execute(input: IReportInput): Promise<FinalChart>;+ executeAggregate(input: IReportInput): Promise<FinalChart>;+ bucketProfiles(input: ChartBucketProfilesRequest): Promise<IServiceProfile[]>;+}++export function createChartService(_deps: ServiceDeps): ChartService {+ return {+ execute: executeChart,+ executeAggregate: executeAggregateChart,+ bucketProfiles: getChartBucketProfiles,+ };+}diff --git a/packages/core/src/modules/chart/src/access.ts b/packages/core/src/modules/chart/src/access.tsnew file mode 100644index 000000000..3d8985453--- /dev/null+++ b/packages/core/src/modules/chart/src/access.ts@@ -0,0 +1,22 @@+// Binds core's shared/access.ts ladder to @openpanel/db's real lookups, for+// this module's own router — the same binding packages/trpc/src/access.ts+// does for V1, self-contained here because the general core -> db binding+// itself moves with auth (P6), not this wave. Same shape as+// session/src/access.ts and event/src/access.ts.++import {+ canWriteProject,+ getOrganizationAccess,+ getProjectAccess,+} from '@openpanel/db/src/services/access.service';+import { getProjectById } from '@openpanel/db/src/services/project.service';+import { createAccessChecks } from '../../../shared/access';++const checks = createAccessChecks({+ getProjectAccess,+ canWriteProject,+ getOrganizationAccess,+ getProjectById,+});++export const requireProjectAccess = checks.requireProjectAccess;diff --git a/packages/core/src/modules/chart/src/chart-statement.ts b/packages/core/src/modules/chart/src/chart-statement.tsnew file mode 100644index 000000000..5a4e81f99--- /dev/null+++ b/packages/core/src/modules/chart/src/chart-statement.ts@@ -0,0 +1,99 @@+// V1's `getChartSql` / `getAggregateChartSql`: the Postgres cohort lookups a+// chart needs, then the pure fragment builders in chart.sql.ts. Lives beside+// them (not in chart.service.ts) so the engine can import it without pulling+// the service — which imports the engine — into a cycle.++import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';+import type { IGetChartDataInput } from '@openpanel/validation';+import {+ aggregateChartQuery,+ chartSeriesQuery,+ knownBreakdowns,+ type ResolvedChartBreakdowns,+ requestsAllCohortsBreakdown,+ withoutEmptyAllCohortsBreakdown,+} from './chart.sql';+import type { CohortMetadata } from './field-resolution';+import { collectBreakdownCohortIds } from './field-resolution';++export type ChartSqlInput = IGetChartDataInput & { timezone: string };+export type AggregateChartSqlInput = Omit<+ IGetChartDataInput,+ 'interval' | 'chartType'+> & { timezone: string };++function loadDb() {+ return import('@openpanel/db/src/prisma-client').then((m) => m.db);+}++export async function fetchCohortsMetadata(+ cohortIds: string[]+): Promise<Map<string, CohortMetadata>> {+ if (cohortIds.length === 0) {+ return new Map();+ }+ const db = await loadDb();+ const cohorts = await db.cohort.findMany({+ where: { id: { in: cohortIds } },+ select: { id: true, name: true },+ });+ return new Map(cohorts.map((c) => [c.id, { id: c.id, name: c.name }]));+}++export async function fetchProjectCohorts(+ projectId: string+): Promise<CohortMetadata[]> {+ const db = await loadDb();+ return db.cohort.findMany({+ where: { projectId },+ select: { id: true, name: true },+ });+}++export async function resolveChartBreakdowns(+ projectId: string,+ requested: IGetChartDataInput['breakdowns']+): Promise<ResolvedChartBreakdowns> {+ const known = knownBreakdowns(requested);+ const allCohorts = requestsAllCohortsBreakdown(known)+ ? await fetchProjectCohorts(projectId)+ : [];+ const breakdowns = withoutEmptyAllCohortsBreakdown(known, allCohorts);+ const cohortMetadata = await fetchCohortsMetadata(+ collectBreakdownCohortIds(breakdowns)+ );+ return { breakdowns, allCohorts, cohortMetadata };+}++export async function getChartSql(input: ChartSqlInput): Promise<SqlFragment> {+ const resolved = await resolveChartBreakdowns(+ input.projectId,+ input.breakdowns+ );+ return chartSeriesQuery({+ ...resolved,+ event: input.event,+ interval: input.interval,+ startDate: input.startDate,+ endDate: input.endDate,+ projectId: input.projectId,+ timezone: input.timezone,+ });+}++export async function getAggregateChartSql(+ input: AggregateChartSqlInput+): Promise<SqlFragment> {+ const resolved = await resolveChartBreakdowns(+ input.projectId,+ input.breakdowns+ );+ return aggregateChartQuery({+ ...resolved,+ event: input.event,+ startDate: input.startDate,+ endDate: input.endDate,+ projectId: input.projectId,+ limit: input.limit,+ });+}diff --git a/packages/core/src/modules/chart/src/chart.sql.proof.md b/packages/core/src/modules/chart/src/chart.sql.proof.mdnew file mode 100644index 000000000..851e5a407--- /dev/null+++ b/packages/core/src/modules/chart/src/chart.sql.proof.md@@ -0,0 +1,910 @@+# chart.sql.ts — V1 → V2 result-set proof (M7-003)++Every query builder in `chart.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 `chart.sql.test.ts` on `openpanel_test`).+- **Machine**: single-node ClickHouse 26.1.3.52 on a 4-vCPU box — timings are directional only; production is 2 shards × 2 replicas (`docs/ENVIRONMENT.md`). The chart statements contain no `IN (subquery)`; cohort membership is a CTE joined on `profile_id`, unchanged from V1. `rows_read` is often lower for V2 (e.g. 245717 → 98299 on the multi-series case): ClickHouse's index analysis treats a bound `{p:String}` differently from an inline literal for the `created_at` range, with identical result sets — a perf note, not a semantic change.+- **Verdict**: 73 cases (28 `chartSeriesQuery`, 15 `aggregateChartQuery`, 30 router queries), all IDENTICAL. 5 of the 73 are **IDENTICAL ERROR**: V1 defects reproduced byte-for-byte on both sides, listed below, not fixed here (out of scope — the task is byte-equivalence, and each is recorded as debt in the task report).++Pre-existing V1 defects reproduced identically (both sides fail with the same ClickHouse error):++1. `chartSeriesQuery` — a `profile.properties.*` / `properties.__query.*` trailing-`.*` wildcard filter: `getSelectPropertyKey` produces `arrayMap(...)` while `getEventFiltersWhereClause` treats it as a scalar (no `%` → `isWildcard` false) → `Illegal type Array(String) of argument of function like` / `Array does not start with '['`. The `[*]` form works and is proven below.+2. `chartSeriesQuery` — all-cohorts (`cohort:*`) breakdown on the series shape: the `INNER JOIN _all_cohorts` makes the unqualified `uniqState(profile_id)` ambiguous; with `one_event_per_user` it is `Unknown expression _all_cohorts.cohort_id`. The aggregate shape works and is proven below.+3. `aggregateChartQuery` — `one_event_per_user` with a named event re-emits `WHERE e.name = …` outside the subquery → `Unknown expression or function identifier e.name`. Wildcard event works and is proven below.++The harness was a throwaway script (it imported V1 and V2 side by side); each statement below is complete and reproducible with `curl http://127.0.0.1:8123/?database=openpanel`, binding the V2 params as `param_pN=`. The projectCard chart statement differs in one deliberate way: V1 appended `SETTINGS session_timezone = '<tz>'` to the text, V2 passes the same value as `clickhouse_settings.session_timezone` — the proof runs both with that setting, and the row sets (92 daily buckets in `America/New_York`) are identical.++## chartSeriesQuery++### chartSeriesQuery — golden export-charts-basic (verdict, view_page, day)++**statement** — IDENTICAL; rows V1/V2 = 7/7; rows_read V1/V2 = 7649340/7470531; wall V1/V2 = 181 ms / 162 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'view_page' as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"view_page","p2":"verdict","p3":"view_page","p4":"2026-07-06 00:00:00","p5":"2026-07-12 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59"}+```+### chartSeriesQuery — golden export-charts-breakdown-country (secure-privacy, breakdown country)++**statement** — IDENTICAL; rows V1/V2 = 6/6; rows_read V1/V2 = 0/0; wall V1/V2 = 29 ms / 12 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1) as total_count FROM (SELECT 'view_page' as label_0, count(*) as count, toStartOfDay(created_at) as date, country as label_1, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'secure-privacy' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY date, label_1) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1) as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, country as label_1, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date, label_1) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"view_page","p2":"secure-privacy","p3":"view_page","p4":"2026-07-06 00:00:00","p5":"2026-07-12 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — golden export-charts-multi-series-filters (secure-privacy, screen_view, country is US)++**statement** — IDENTICAL; rows V1/V2 = 7/7; rows_read V1/V2 = 245717/98299; wall V1/V2 = 36 ms / 27 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'screen_view' as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE country = 'US' AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE country = 'US' AND project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"screen_view","p2":"secure-privacy","p3":"screen_view","p4":"2026-07-06 00:00:00","p5":"2026-07-12 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — golden export-charts-month-week-interval (verdict, week)++**statement** — IDENTICAL; rows V1/V2 = 5/5; rows_read V1/V2 = 24600197/24600197; wall V1/V2 = 460 ms / 442 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'view_page' as label_0, count(*) as count, toStartOfWeek(created_at, 1, 'UTC') as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-01 00:00:00') AND created_at <= toDateTime('2026-07-31 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfWeek(toDateTime('2026-07-01 00:00:00'), 1, 'UTC') TO toStartOfWeek(toDateTime('2026-07-31 23:59:59'), 1, 'UTC') STEP toIntervalWeek(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfWeek(created_at, 1, {p2:String}) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfWeek(toDateTime({p7:String}), 1, {p8:String}) TO toStartOfWeek(toDateTime({p9:String}), 1, {p10:String}) STEP toIntervalWeek(1)+-- V2 params: {"p1":"view_page","p2":"UTC","p3":"verdict","p4":"view_page","p5":"2026-07-01 00:00:00","p6":"2026-07-31 23:59:59","p7":"2026-07-01 00:00:00","p8":"UTC","p9":"2026-07-31 23:59:59","p10":"UTC"}+```++### chartSeriesQuery — golden export-charts-nyc-dst-spring (bayse, week, America/New_York)++**statement** — IDENTICAL; rows V1/V2 = 21/21; rows_read V1/V2 = 17710659/17595974; wall V1/V2 = 341 ms / 340 ms; clickhouse_settings: session_timezone=America/New_York (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'pm_order_open' as label_0, count(*) as count, toStartOfWeek(created_at, 1, 'America/New_York') as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'bayse' AND e.name = 'pm_order_open' AND created_at >= toDateTime('2026-03-01 00:00:00') AND created_at <= toDateTime('2026-07-15 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfWeek(toDateTime('2026-03-01 00:00:00'), 1, 'America/New_York') TO toStartOfWeek(toDateTime('2026-07-15 23:59:59'), 1, 'America/New_York') STEP toIntervalWeek(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfWeek(created_at, 1, {p2:String}) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfWeek(toDateTime({p7:String}), 1, {p8:String}) TO toStartOfWeek(toDateTime({p9:String}), 1, {p10:String}) STEP toIntervalWeek(1)+-- V2 params: {"p1":"pm_order_open","p2":"America/New_York","p3":"bayse","p4":"pm_order_open","p5":"2026-03-01 00:00:00","p6":"2026-07-15 23:59:59","p7":"2026-03-01 00:00:00","p8":"America/New_York","p9":"2026-07-15 23:59:59","p10":"America/New_York"}+```++### chartSeriesQuery — golden export-charts-nyc-dst-fall (bayse, day, America/New_York)++**statement** — IDENTICAL; rows V1/V2 = 99/99; rows_read V1/V2 = 12131671/12107095; wall V1/V2 = 216 ms / 209 ms; clickhouse_settings: session_timezone=America/New_York (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'pm_order_open' as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'bayse' AND e.name = 'pm_order_open' AND created_at >= toDateTime('2026-08-01 00:00:00') AND created_at <= toDateTime('2026-11-08 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-08-01 00:00:00')) TO toStartOfDay(toDateTime('2026-11-08 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"pm_order_open","p2":"bayse","p3":"pm_order_open","p4":"2026-08-01 00:00:00","p5":"2026-11-08 23:59:59","p6":"2026-08-01 00:00:00","p7":"2026-11-08 23:59:59"}+```++### chartSeriesQuery — golden export-charts-ist-hour (website-8103, hour, Asia/Kolkata)++**statement** — IDENTICAL; rows V1/V2 = 48/48; rows_read V1/V2 = 913793/890640; wall V1/V2 = 30 ms / 26 ms; clickhouse_settings: session_timezone=Asia/Kolkata (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'screen_view' as label_0, count(*) as count, toStartOfHour(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'website-8103' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-08-01 00:00:00') AND created_at <= toDateTime('2026-08-02 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfHour(toDateTime('2026-08-01 00:00:00')) TO toStartOfHour(toDateTime('2026-08-02 23:59:59')) STEP toIntervalHour(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfHour(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfHour(toDateTime({p6:String})) TO toStartOfHour(toDateTime({p7:String})) STEP toIntervalHour(1)+-- V2 params: {"p1":"screen_view","p2":"website-8103","p3":"screen_view","p4":"2026-08-01 00:00:00","p5":"2026-08-02 23:59:59","p6":"2026-08-01 00:00:00","p7":"2026-08-02 23:59:59"}+```++### chartSeriesQuery — golden export-charts-syd-dst (dream-mate, day, Australia/Sydney)++**statement** — IDENTICAL; rows V1/V2 = 91/91; rows_read V1/V2 = 7972226/7953964; wall V1/V2 = 171 ms / 171 ms; clickhouse_settings: session_timezone=Australia/Sydney (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'screen_view' as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'dream-mate' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-08-01 00:00:00') AND created_at <= toDateTime('2026-10-31 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-08-01 00:00:00')) TO toStartOfDay(toDateTime('2026-10-31 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"screen_view","p2":"dream-mate","p3":"screen_view","p4":"2026-08-01 00:00:00","p5":"2026-10-31 23:59:59","p6":"2026-08-01 00:00:00","p7":"2026-10-31 23:59:59"}+```++### chartSeriesQuery — minute interval (verdict, 1 hour)++**statement** — IDENTICAL; rows V1/V2 = 60/60; rows_read V1/V2 = 260132/24575; wall V1/V2 = 18 ms / 14 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'view_page' as label_0, count(*) as count, toStartOfMinute(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-06 01:00:00') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfMinute(toDateTime('2026-07-06 00:00:00')) TO toStartOfMinute(toDateTime('2026-07-06 01:00:00')) STEP toIntervalMinute(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfMinute(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfMinute(toDateTime({p6:String})) TO toStartOfMinute(toDateTime({p7:String})) STEP toIntervalMinute(1)+-- V2 params: {"p1":"view_page","p2":"verdict","p3":"view_page","p4":"2026-07-06 00:00:00","p5":"2026-07-06 01:00:00","p6":"2026-07-06 00:00:00","p7":"2026-07-06 01:00:00"}+```++### chartSeriesQuery — month interval with timezone (bayse, America/New_York)++**statement** — IDENTICAL; rows V1/V2 = 3/3; rows_read V1/V2 = 25083459/25034307; wall V1/V2 = 485 ms / 468 ms; clickhouse_settings: session_timezone=America/New_York (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'pm_order_open' as label_0, count(*) as count, toStartOfMonth(created_at, 'America/New_York') as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'bayse' AND e.name = 'pm_order_open' AND created_at >= toDateTime('2026-05-01 00:00:00') AND created_at <= toDateTime('2026-07-31 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfMonth(toDateTime('2026-05-01 00:00:00'), 'America/New_York') TO toStartOfMonth(toDateTime('2026-07-31 23:59:59'), 'America/New_York') STEP toIntervalMonth(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfMonth(created_at, {p2:String}) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfMonth(toDateTime({p7:String}), {p8:String}) TO toStartOfMonth(toDateTime({p9:String}), {p10:String}) STEP toIntervalMonth(1)+-- V2 params: {"p1":"pm_order_open","p2":"America/New_York","p3":"bayse","p4":"pm_order_open","p5":"2026-05-01 00:00:00","p6":"2026-07-31 23:59:59","p7":"2026-05-01 00:00:00","p8":"America/New_York","p9":"2026-07-31 23:59:59","p10":"America/New_York"}+```++### chartSeriesQuery — wildcard event '*' + segment user (verdict)++**statement** — IDENTICAL; rows V1/V2 = 3/3; rows_read V1/V2 = 1185604/990411; wall V1/V2 = 48 ms / 45 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT '*' as label_0, countDistinct(profile_id) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'verdict' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-08 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT '*' as label_0, countDistinct(profile_id) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p1:String} AND created_at >= toDateTime({p2:String}) AND created_at <= toDateTime({p3:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p4:String})) TO toStartOfDay(toDateTime({p5:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"verdict","p2":"2026-07-06 00:00:00","p3":"2026-07-08 23:59:59","p4":"2026-07-06 00:00:00","p5":"2026-07-08 23:59:59"}+```++### chartSeriesQuery — segment session (verdict)++**statement** — IDENTICAL; rows V1/V2 = 3/3; rows_read V1/V2 = 1185604/982645; wall V1/V2 = 45 ms / 37 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'view_page' as label_0, countDistinct(session_id) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-08 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, countDistinct(session_id) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"view_page","p2":"verdict","p3":"view_page","p4":"2026-07-06 00:00:00","p5":"2026-07-08 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-08 23:59:59"}+```++### chartSeriesQuery — segment user_average (verdict)++**statement** — IDENTICAL; rows V1/V2 = 3/3; rows_read V1/V2 = 982645/982645; wall V1/V2 = 33 ms / 33 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'view_page' as label_0, COUNT(*)::float / COUNT(DISTINCT profile_id)::float as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-08 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, COUNT(*)::float / COUNT(DISTINCT profile_id)::float as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"view_page","p2":"verdict","p3":"view_page","p4":"2026-07-06 00:00:00","p5":"2026-07-08 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-08 23:59:59"}+```++### chartSeriesQuery — segment one_event_per_user + breakdown path (verdict)++**statement** — IDENTICAL; rows V1/V2 = 3/3; rows_read V1/V2 = 982645/982645; wall V1/V2 = 49 ms / 52 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT 'view_page' as label_0, count(*) as count, toStartOfDay(created_at) as date, path as label_1 FROM ( SELECT DISTINCT ON (profile_id) * from events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') ORDER BY profile_id, created_at DESC ) as subQuery GROUP BY date, label_1 ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-08 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT {p1:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, path as label_1 FROM ( SELECT DISTINCT ON (profile_id) * from events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) ORDER BY profile_id, created_at DESC ) as subQuery GROUP BY date, label_1 ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"view_page","p2":"verdict","p3":"view_page","p4":"2026-07-06 00:00:00","p5":"2026-07-08 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-08 23:59:59"}+```++### chartSeriesQuery — segment group + breakdown group.name + filter group.type (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 44/44; rows_read V1/V2 = 249076/101658; wall V1/V2 = 26 ms / 23 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+WITH _g AS (SELECT id, name, type, properties FROM groups FINAL WHERE project_id = 'secure-privacy') SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1, label_2) as total_count FROM (SELECT 'screen_view' as label_0, countDistinct(_group_id) as count, toStartOfDay(created_at) as date, _g.name as label_1, _g.properties['employees'] as label_2, uniqState(profile_id) as _uc_state FROM events e ARRAY JOIN groups AS _group_id LEFT ANY JOIN _g ON _g.id = _group_id WHERE _g.type = 'company' AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY date, label_1, label_2) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+WITH _g AS (SELECT id, name, type, properties FROM groups FINAL WHERE project_id = {p1:String}) SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1, label_2) as total_count FROM (SELECT {p2:String} as label_0, countDistinct(_group_id) as count, toStartOfDay(created_at) as date, _g.name as label_1, _g.properties['employees'] as label_2, uniqState(profile_id) as _uc_state FROM events e ARRAY JOIN groups AS _group_id LEFT ANY JOIN _g ON _g.id = _group_id WHERE _g.type = 'company' AND project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) GROUP BY date, label_1, label_2) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p7:String})) TO toStartOfDay(toDateTime({p8:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"secure-privacy","p4":"screen_view","p5":"2026-07-06 00:00:00","p6":"2026-07-12 23:59:59","p7":"2026-07-06 00:00:00","p8":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — profile breakdown + profile.properties filter + narrowed CTE (secure-privacy)++**statement** — IDENTICAL (row order differs — see note); rows V1/V2 = 86/86; rows_read V1/V2 = 556693/433840; wall V1/V2 = 108 ms / 97 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+WITH profile AS (SELECT id as "profile.id", properties['country'] as `profile.properties.country`, properties['device'] as `profile.properties.device`, email as "profile.email", first_name as "profile.first_name" FROM profiles FINAL WHERE project_id = 'secure-privacy') SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1, label_2) as total_count FROM (SELECT 'screen_view' as label_0, count(*) as count, toStartOfDay(created_at) as date, `profile.properties.device` as label_1, profile.first_name as label_2, uniqState(profile_id) as _uc_state FROM events e LEFT ANY JOIN profile ON profile.id = profile_id WHERE (`profile.properties.country` != '' AND `profile.properties.country` IS NOT NULL) AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY date, label_1, label_2) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+WITH profile AS (SELECT id as "profile.id", properties['country'] as `profile.properties.country`, properties['device'] as `profile.properties.device`, email as "profile.email", first_name as "profile.first_name" FROM profiles FINAL WHERE project_id = {p1:String}) SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1, label_2) as total_count FROM (SELECT {p2:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, `profile.properties.device` as label_1, profile.first_name as label_2, uniqState(profile_id) as _uc_state FROM events e LEFT ANY JOIN profile ON profile.id = profile_id WHERE (`profile.properties.country` != '' AND `profile.properties.country` IS NOT NULL) AND project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) GROUP BY date, label_1, label_2) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p7:String})) TO toStartOfDay(toDateTime({p8:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"secure-privacy","p4":"screen_view","p5":"2026-07-06 00:00:00","p6":"2026-07-12 23:59:59","p7":"2026-07-06 00:00:00","p8":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — profile wildcard filter (full map) + math property_max on profile.properties.longitude (secure-privacy)++**statement** — IDENTICAL ERROR (pre-existing V1 defect, both sides fail with the same ClickHouse error).++```+V1 ERROR: Illegal type Array(String) of argument of function like: In scope SELECT 'screen_view' AS label_0, max(toFloat64OrNull(`profile.properties.longitude`)) AS count, toStartOfDay(created_at) AS date, uniqState(profile_id) AS _uc_state FROM events AS e ANY LEFT JOIN profile ON profile.id = profile_id WHERE (arrayMap(x -> trimBoth(x), mapValues(mapExtractKeyLike(profile.properties, 'profile.properties.*'))) LIKE '%Chrome%') AND (project_id = 'secure-privacy') AND (e.name = 'screen_view') AND (created_at >= toDateTime('2026-07-06 00:00:00')) AND (created_at <= toDateTime('2026-07-12 23:59:59')) AND (`profile.properties.longitude` IS NOT NULL) AND notEmpty(`profile.properties.longitude`) GROUP BY date.+V2 ERROR: Illegal type Array(String) of argument of function like: In scope SELECT 'screen_view' AS label_0, max(toFloat64OrNull(`profile.properties.longitude`)) AS count, toStartOfDay(created_at) AS date, uniqState(profile_id) AS _uc_state FROM events AS e ANY LEFT JOIN profile ON profile.id = profile_id WHERE (arrayMap(x -> trimBoth(x), mapValues(mapExtractKeyLike(profile.properties, 'profile.properties.*'))) LIKE '%Chrome%') AND (project_id = 'secure-privacy') AND (e.name = 'screen_view') AND (created_at >= toDateTime('2026-07-06 00:00:00')) AND (created_at <= toDateTime('2026-07-12 23:59:59')) AND (`profile.properties.longitude` IS NOT NULL) AND notEmpty(`profile.properties.longitude`) GROUP BY date.+```++```sql+-- V1+WITH profile AS (SELECT id as "profile.id", properties['longitude'] as `profile.properties.longitude`, properties as "profile.properties" FROM profiles FINAL WHERE project_id = 'secure-privacy') SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'screen_view' as label_0, max(toFloat64OrNull(`profile.properties.longitude`)) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e LEFT ANY JOIN profile ON profile.id = profile_id WHERE (arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(profile.properties, 'profile.properties.*'))) LIKE '%Chrome%') AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') AND `profile.properties.longitude` IS NOT NULL AND notEmpty(`profile.properties.longitude`) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+WITH profile AS (SELECT id as "profile.id", properties['longitude'] as `profile.properties.longitude`, properties as "profile.properties" FROM profiles FINAL WHERE project_id = {p1:String}) SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p2:String} as label_0, max(toFloat64OrNull(`profile.properties.longitude`)) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e LEFT ANY JOIN profile ON profile.id = profile_id WHERE (arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(profile.properties, 'profile.properties.*'))) LIKE '%Chrome%') AND project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) AND `profile.properties.longitude` IS NOT NULL AND notEmpty(`profile.properties.longitude`) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p7:String})) TO toStartOfDay(toDateTime({p8:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"secure-privacy","p4":"screen_view","p5":"2026-07-06 00:00:00","p6":"2026-07-12 23:59:59","p7":"2026-07-06 00:00:00","p8":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — properties breakdown __title + filters: properties.__query.gclid isNotNull, value gt, path regex, referrerName alias, utm_source bare (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 6/6; rows_read V1/V2 = 245717/0; wall V1/V2 = 40 ms / 18 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1, label_2, label_3) as total_count FROM (SELECT 'screen_view' as label_0, count(*) as count, toStartOfDay(created_at) as date, e.properties['__title'] as label_1, referrer_type as label_2, e.properties['__query.utm_medium'] as label_3, uniqState(profile_id) as _uc_state FROM events e WHERE (e.properties['__query.gclid'] != '' AND e.properties['__query.gclid'] IS NOT NULL) AND (toFloat64OrZero(e.properties['value']) > toFloat64('1')) AND (match(path, '^\\/.*')) AND (referrer_name = '' OR referrer_name IS NULL) AND (e.properties['__query.utm_source'] = '' OR e.properties['__query.utm_source'] IS NULL) AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY date, label_1, label_2, label_3) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1, label_2, label_3) as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, e.properties['__title'] as label_1, referrer_type as label_2, e.properties['__query.utm_medium'] as label_3, uniqState(profile_id) as _uc_state FROM events e WHERE (e.properties['__query.gclid'] != '' AND e.properties['__query.gclid'] IS NOT NULL) AND (toFloat64OrZero(e.properties['value']) > toFloat64('1')) AND (match(path, '^\\/.*')) AND (referrer_name = '' OR referrer_name IS NULL) AND (e.properties['__query.utm_source'] = '' OR e.properties['__query.utm_source'] IS NULL) AND project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date, label_1, label_2, label_3) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"screen_view","p2":"secure-privacy","p3":"screen_view","p4":"2026-07-06 00:00:00","p5":"2026-07-12 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — math property_sum on numeric column duration + has_profile true + wildcard properties.* is (secure-privacy)++**statement** — IDENTICAL ERROR (pre-existing V1 defect, both sides fail with the same ClickHouse error).++```+V1 ERROR: Array does not start with '[' character: while converting '' to Array(String): while executing function notEquals on arguments arrayMap(x String -> trimBoth(x), mapValues(mapExtractKeyLike(__table2.properties, '__query.*'_String))) Array(String) Array(size = 0, UInt64(size = 0), String(size = 0)), ''_String String Const(size = 0, String(size = 1)).+V2 ERROR: Array does not start with '[' character: while converting '' to Array(String): while executing function notEquals on arguments arrayMap(x String -> trimBoth(x), mapValues(mapExtractKeyLike(__table2.properties, '__query.*'_String))) Array(String) Array(size = 0, UInt64(size = 0), String(size = 0)), ''_String String Const(size = 0, String(size = 1)).+```++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'screen_view' as label_0, sum(duration) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE profile_id != device_id AND (arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(e.properties, '__query.*'))) != '' AND arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(e.properties, '__query.*'))) IS NOT NULL) AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') AND duration IS NOT NULL GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, sum(duration) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE profile_id != device_id AND (arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(e.properties, '__query.*'))) != '' AND arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(e.properties, '__query.*'))) IS NOT NULL) AND project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) AND duration IS NOT NULL GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"screen_view","p2":"secure-privacy","p3":"screen_view","p4":"2026-07-06 00:00:00","p5":"2026-07-12 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — math property_average on properties.ttfb + typed number filter (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 6/6; rows_read V1/V2 = 245717/0; wall V1/V2 = 35 ms / 10 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'screen_view' as label_0, avg(toFloat64OrNull(e.properties['ttfb'])) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE (toFloat64OrNull(toString(e.properties['renderDelay'])) >= toFloat64OrNull(toString('10'))) AND (toFloat64(duration) < toFloat64('100000')) AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') AND e.properties['ttfb'] IS NOT NULL AND notEmpty(e.properties['ttfb']) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, avg(toFloat64OrNull(e.properties['ttfb'])) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE (toFloat64OrNull(toString(e.properties['renderDelay'])) >= toFloat64OrNull(toString('10'))) AND (toFloat64(duration) < toFloat64('100000')) AND project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) AND e.properties['ttfb'] IS NOT NULL AND notEmpty(e.properties['ttfb']) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"screen_view","p2":"secure-privacy","p3":"screen_view","p4":"2026-07-06 00:00:00","p5":"2026-07-12 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — inCohort / notInCohort filters + cohort:<id> breakdown (unknown cohort, empty members) (verdict)++**statement** — IDENTICAL; rows V1/V2 = 2/2; rows_read V1/V2 = 0/0; wall V1/V2 = 22 ms / 16 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+WITH `cohort-33333333-3333-4333-8333-333333333333` AS ( SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = '33333333-3333-4333-8333-333333333333' AND project_id = 'verdict' ) SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1) as total_count FROM (SELECT 'view_page' as label_0, count(*) as count, toStartOfDay(created_at) as date, if(notEmpty(cohort_33333333_3333_4333_8333_333333333333.profile_id), 'In Cohort', 'Not In Cohort') as label_1, uniqState(profile_id) as _uc_state FROM events e LEFT ANY JOIN `cohort-33333333-3333-4333-8333-333333333333` AS cohort_33333333_3333_4333_8333_333333333333 ON cohort_33333333_3333_4333_8333_333333333333.profile_id = e.profile_id WHERE e.profile_id IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id IN ('11111111-1111-4111-8111-111111111111', '22222222-2222-4222-8222-222222222222') AND project_id = 'verdict') AND e.profile_id NOT IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id IN ('33333333-3333-4333-8333-333333333333') AND project_id = 'verdict') AND project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY date, label_1) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-08 23:59:59')) STEP toIntervalDay(1)+-- V2+WITH `cohort-33333333-3333-4333-8333-333333333333` AS ( SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = {p1:String} AND project_id = {p2:String} ) SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1) as total_count FROM (SELECT {p3:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, if(notEmpty(cohort_33333333_3333_4333_8333_333333333333.profile_id), {p4:String}, {p5:String}) as label_1, uniqState(profile_id) as _uc_state FROM events e LEFT ANY JOIN `cohort-33333333-3333-4333-8333-333333333333` AS cohort_33333333_3333_4333_8333_333333333333 ON cohort_33333333_3333_4333_8333_333333333333.profile_id = e.profile_id WHERE e.profile_id IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id IN ('11111111-1111-4111-8111-111111111111', '22222222-2222-4222-8222-222222222222') AND project_id = 'verdict') AND e.profile_id NOT IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id IN ('33333333-3333-4333-8333-333333333333') AND project_id = 'verdict') AND project_id = {p6:String} AND e.name = {p7:String} AND created_at >= toDateTime({p8:String}) AND created_at <= toDateTime({p9:String}) GROUP BY date, label_1) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p10:String})) TO toStartOfDay(toDateTime({p11:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"33333333-3333-4333-8333-333333333333","p2":"verdict","p3":"view_page","p4":"In Cohort","p5":"Not In Cohort","p6":"verdict","p7":"view_page","p8":"2026-07-06 00:00:00","p9":"2026-07-08 23:59:59","p10":"2026-07-06 00:00:00","p11":"2026-07-08 23:59:59"}+```++### chartSeriesQuery — notInCohort filter only + cohort:<id> breakdown with named cohort (authc-project-a1)++**statement** — IDENTICAL ERROR (pre-existing V1 defect, both sides fail with the same ClickHouse error).++```+V1 ERROR: JOIN INNER JOIN ... ON _all_cohorts.profile_id = e.profile_id ambiguous identifier 'profile_id'. In scope SELECT '*' AS label_0, count(*) AS count, toStartOfDay(created_at) AS date, if(notEmpty(cohort_cccccccc_0000_4000_8000_000000000001.profile_id), 'Auth contract cohort', 'Not Auth contract cohort') AS label_1, transform(_all_cohorts.cohort_id, ['cccccccc-0000-4000-8000-000000000001'], ['Auth contract cohort'], 'Unknown') AS label_2, uniqState(profile_id) AS _uc_state FROM events AS e INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id ANY LEFT JOIN `cohort-cccccccc-0000-4000-8000-000000000001` AS cohort_cccccccc_0000_4000_8000_000000000001 ON cohort_cccccccc_0000_4000_8000_000000000001.profile_id = e.profile_id WHERE (e.profile_id NOT IN (SELECT profile_id FROM cohort_members FINAL WHERE (cohort_id IN ('cccccccc-0000-4000-8000-000000000001')) AND (project_id = 'authc-project-a1'))) AND (project_id = 'authc-project-a1') AND (created_at >= toDateTime('2026-07-06 00:00:00')) AND (created_at <= toDateTime('2026-07-08 23:59:59')) GROUP BY date, label_1, label_2.+V2 ERROR: JOIN INNER JOIN ... ON _all_cohorts.profile_id = e.profile_id ambiguous identifier 'profile_id'. In scope SELECT '*' AS label_0, count(*) AS count, toStartOfDay(created_at) AS date, if(notEmpty(cohort_cccccccc_0000_4000_8000_000000000001.profile_id), 'Auth contract cohort', 'Not Auth contract cohort') AS label_1, transform(_all_cohorts.cohort_id, _CAST(['cccccccc-0000-4000-8000-000000000001'], 'Array(String)'), _CAST(['Auth contract cohort'], 'Array(String)'), 'Unknown') AS label_2, uniqState(profile_id) AS _uc_state FROM events AS e INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id ANY LEFT JOIN `cohort-cccccccc-0000-4000-8000-000000000001` AS cohort_cccccccc_0000_4000_8000_000000000001 ON cohort_cccccccc_0000_4000_8000_000000000001.profile_id = e.profile_id WHERE (e.profile_id NOT IN (SELECT profile_id FROM cohort_members FINAL WHERE (cohort_id IN ('cccccccc-0000-4000-8000-000000000001')) AND (project_id = 'authc-project-a1'))) AND (project_id = 'authc-project-a1') AND (created_at >= toDateTime('2026-07-06 00:00:00')) AND (created_at <= toDateTime('2026-07-08 23:59:59')) GROUP BY date, label_1, label_2.+```++```sql+-- V1+WITH _all_cohorts AS ( SELECT profile_id, cohort_id FROM cohort_members FINAL WHERE project_id = 'authc-project-a1' ), `cohort-cccccccc-0000-4000-8000-000000000001` AS ( SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = 'cccccccc-0000-4000-8000-000000000001' AND project_id = 'authc-project-a1' ) SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1, label_2) as total_count FROM (SELECT '*' as label_0, count(*) as count, toStartOfDay(created_at) as date, if(notEmpty(cohort_cccccccc_0000_4000_8000_000000000001.profile_id), 'Auth contract cohort', 'Not Auth contract cohort') as label_1, transform(_all_cohorts.cohort_id, ['cccccccc-0000-4000-8000-000000000001'], ['Auth contract cohort'], 'Unknown') as label_2, uniqState(profile_id) as _uc_state FROM events e INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id LEFT ANY JOIN `cohort-cccccccc-0000-4000-8000-000000000001` AS cohort_cccccccc_0000_4000_8000_000000000001 ON cohort_cccccccc_0000_4000_8000_000000000001.profile_id = e.profile_id WHERE e.profile_id NOT IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id IN ('cccccccc-0000-4000-8000-000000000001') AND project_id = 'authc-project-a1') AND project_id = 'authc-project-a1' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY date, label_1, label_2) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-08 23:59:59')) STEP toIntervalDay(1)+-- V2+WITH _all_cohorts AS ( SELECT profile_id, cohort_id FROM cohort_members FINAL WHERE project_id = {p1:String} ), `cohort-cccccccc-0000-4000-8000-000000000001` AS ( SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = {p2:String} AND project_id = {p3:String} ) SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1, label_2) as total_count FROM (SELECT '*' as label_0, count(*) as count, toStartOfDay(created_at) as date, if(notEmpty(cohort_cccccccc_0000_4000_8000_000000000001.profile_id), {p4:String}, {p5:String}) as label_1, transform(_all_cohorts.cohort_id, {p6:Array(String)}, {p7:Array(String)}, {p8:String}) as label_2, uniqState(profile_id) as _uc_state FROM events e INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id LEFT ANY JOIN `cohort-cccccccc-0000-4000-8000-000000000001` AS cohort_cccccccc_0000_4000_8000_000000000001 ON cohort_cccccccc_0000_4000_8000_000000000001.profile_id = e.profile_id WHERE e.profile_id NOT IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id IN ('cccccccc-0000-4000-8000-000000000001') AND project_id = 'authc-project-a1') AND project_id = {p9:String} AND created_at >= toDateTime({p10:String}) AND created_at <= toDateTime({p11:String}) GROUP BY date, label_1, label_2) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p12:String})) TO toStartOfDay(toDateTime({p13:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"authc-project-a1","p2":"cccccccc-0000-4000-8000-000000000001","p3":"authc-project-a1","p4":"Auth contract cohort","p5":"Not Auth contract cohort","p6":["cccccccc-0000-4000-8000-000000000001"],"p7":["Auth contract cohort"],"p8":"Unknown","p9":"authc-project-a1","p10":"2026-07-06 00:00:00","p11":"2026-07-08 23:59:59","p12":"2026-07-06 00:00:00","p13":"2026-07-08 23:59:59"}+```++### chartSeriesQuery — all-cohorts breakdown dropped when project has no cohorts (verdict)++**statement** — IDENTICAL; rows V1/V2 = 3/3; rows_read V1/V2 = 982645/982645; wall V1/V2 = 49 ms / 33 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'view_page' as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-08 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"view_page","p2":"verdict","p3":"view_page","p4":"2026-07-06 00:00:00","p5":"2026-07-08 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-08 23:59:59"}+```++### chartSeriesQuery — endDate before startDate: no WITH FILL (verdict)++**statement** — IDENTICAL; rows V1/V2 = 0/0; rows_read V1/V2 = 243749/0; wall V1/V2 = 13 ms / 7 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'view_page' as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-08 00:00:00') AND created_at <= toDateTime('2026-07-06 00:00:00') GROUP BY date) ORDER BY date ASC+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date) ORDER BY date ASC+-- V2 params: {"p1":"view_page","p2":"verdict","p3":"view_page","p4":"2026-07-08 00:00:00","p5":"2026-07-06 00:00:00"}+```+### chartSeriesQuery — profile wildcard filter profile.properties.x[*] (full map CTE) + math property_max on profile.properties.longitude (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 6/6; rows_read V1/V2 = 417465/417465; wall V1/V2 = 150 ms / 96 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+WITH profile AS (SELECT id as "profile.id", properties['longitude'] as `profile.properties.longitude`, properties as "profile.properties" FROM profiles FINAL WHERE project_id = 'secure-privacy') SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'screen_view' as label_0, max(toFloat64OrNull(`profile.properties.longitude`)) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e LEFT ANY JOIN profile ON profile.id = profile_id WHERE arrayExists(x -> x LIKE '%Chrome%', arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(profile.properties, 'profile.properties.browser.%')))) AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') AND `profile.properties.longitude` IS NOT NULL AND notEmpty(`profile.properties.longitude`) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+WITH profile AS (SELECT id as "profile.id", properties['longitude'] as `profile.properties.longitude`, properties as "profile.properties" FROM profiles FINAL WHERE project_id = {p1:String}) SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p2:String} as label_0, max(toFloat64OrNull(`profile.properties.longitude`)) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e LEFT ANY JOIN profile ON profile.id = profile_id WHERE arrayExists(x -> x LIKE '%Chrome%', arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(profile.properties, 'profile.properties.browser.%')))) AND project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) AND `profile.properties.longitude` IS NOT NULL AND notEmpty(`profile.properties.longitude`) GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p7:String})) TO toStartOfDay(toDateTime({p8:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"secure-privacy","p4":"screen_view","p5":"2026-07-06 00:00:00","p6":"2026-07-12 23:59:59","p7":"2026-07-06 00:00:00","p8":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — math property_sum on numeric column duration + has_profile true + wildcard properties.__query[*] isNotNull (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 7/7; rows_read V1/V2 = 245717/81916; wall V1/V2 = 27 ms / 31 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT 'screen_view' as label_0, sum(duration) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE profile_id != device_id AND arrayExists(x -> x != '' AND x IS NOT NULL, arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(e.properties, '__query.%')))) AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') AND duration IS NOT NULL GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER () as total_count FROM (SELECT {p1:String} as label_0, sum(duration) as count, toStartOfDay(created_at) as date, uniqState(profile_id) as _uc_state FROM events e WHERE profile_id != device_id AND arrayExists(x -> x != '' AND x IS NOT NULL, arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(e.properties, '__query.%')))) AND project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) AND duration IS NOT NULL GROUP BY date) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"screen_view","p2":"secure-privacy","p3":"screen_view","p4":"2026-07-06 00:00:00","p5":"2026-07-12 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — wildcard properties.__query[*] breakdown + is filter + has_profile false (secure-privacy)++**statement** — IDENTICAL (row order differs — see note); rows V1/V2 = 191/191; rows_read V1/V2 = 245717/98299; wall V1/V2 = 29 ms / 44 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1) as total_count FROM (SELECT 'screen_view' as label_0, count(*) as count, toStartOfDay(created_at) as date, arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(e.properties, '__query.%'))) as label_1, uniqState(profile_id) as _uc_state FROM events e WHERE profile_id = device_id AND arrayExists(x -> x = '1' OR x = '2', arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(e.properties, '__query.%')))) AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY date, label_1) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-12 23:59:59')) STEP toIntervalDay(1)+-- V2+SELECT * EXCEPT (_uc_state), uniqMerge(_uc_state) OVER (PARTITION BY label_1) as total_count FROM (SELECT {p1:String} as label_0, count(*) as count, toStartOfDay(created_at) as date, arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(e.properties, '__query.%'))) as label_1, uniqState(profile_id) as _uc_state FROM events e WHERE profile_id = device_id AND arrayExists(x -> x = '1' OR x = '2', arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(e.properties, '__query.%')))) AND project_id = {p2:String} AND e.name = {p3:String} AND created_at >= toDateTime({p4:String}) AND created_at <= toDateTime({p5:String}) GROUP BY date, label_1) ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p6:String})) TO toStartOfDay(toDateTime({p7:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"screen_view","p2":"secure-privacy","p3":"screen_view","p4":"2026-07-06 00:00:00","p5":"2026-07-12 23:59:59","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59"}+```++### chartSeriesQuery — all-cohorts breakdown + one_event_per_user (authc-project-a1)++**statement** — IDENTICAL ERROR (pre-existing V1 defect, both sides fail with the same ClickHouse error): `Unknown expression or function identifier `_all_cohorts.cohort_id` in scope WITH _all_cohorts AS (SELECT profile_id, cohort_id FROM cohort_members FINAL WHERE project_id = 'authc-project-a1') SELECT '*' AS label_0, count(*) AS count, toStartOfDay(created_at) AS date, transform(_all_cohorts.cohort_id, ['cccccccc-0000-4000-8000-000000000001'], ['Auth contract cohort'], 'Unknown') AS label_1 FROM (SELECT * FROM events AS e INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id WHERE (project_id = 'authc-project-a1') AND (created_at >= toDateTime('2026-07-06 00:00:00')) AND (created_at <= toDateTime('2026-07-08 23:59:59')) ORDER BY profile_id ASC, created_at DESC LIMIT 1 BY profile_id) AS subQuery GROUP BY date, label_1 ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-08 23:59:59')) STEP toIntervalDay(1). Maybe you meant: ['_all_cohorts.profile_id'].`++```sql+-- V1+WITH _all_cohorts AS ( SELECT profile_id, cohort_id FROM cohort_members FINAL WHERE project_id = 'authc-project-a1' ) SELECT '*' as label_0, count(*) as count, toStartOfDay(created_at) as date, transform(_all_cohorts.cohort_id, ['cccccccc-0000-4000-8000-000000000001'], ['Auth contract cohort'], 'Unknown') as label_1 FROM ( SELECT DISTINCT ON (profile_id) * from events e INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id WHERE project_id = 'authc-project-a1' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') ORDER BY profile_id, created_at DESC ) as subQuery GROUP BY date, label_1 ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime('2026-07-06 00:00:00')) TO toStartOfDay(toDateTime('2026-07-08 23:59:59')) STEP toIntervalDay(1)+-- V2+WITH _all_cohorts AS ( SELECT profile_id, cohort_id FROM cohort_members FINAL WHERE project_id = {p1:String} ) SELECT '*' as label_0, count(*) as count, toStartOfDay(created_at) as date, transform(_all_cohorts.cohort_id, {p2:Array(String)}, {p3:Array(String)}, {p4:String}) as label_1 FROM ( SELECT DISTINCT ON (profile_id) * from events e INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id WHERE project_id = {p5:String} AND created_at >= toDateTime({p6:String}) AND created_at <= toDateTime({p7:String}) ORDER BY profile_id, created_at DESC ) as subQuery GROUP BY date, label_1 ORDER BY date ASC WITH FILL FROM toStartOfDay(toDateTime({p8:String})) TO toStartOfDay(toDateTime({p9:String})) STEP toIntervalDay(1)+-- V2 params: {"p1":"authc-project-a1","p2":["cccccccc-0000-4000-8000-000000000001"],"p3":["Auth contract cohort"],"p4":"Unknown","p5":"authc-project-a1","p6":"2026-07-06 00:00:00","p7":"2026-07-08 23:59:59","p8":"2026-07-06 00:00:00","p9":"2026-07-08 23:59:59"}+```++## aggregateChartQuery++### aggregateChartQuery — basic (verdict, view_page, limit 10)++**statement** — IDENTICAL; rows V1/V2 = 1/1; rows_read V1/V2 = 7649340/7470531; wall V1/V2 = 101 ms / 69 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT 'view_page' as label_0, '2026-07-06 00:00:00' as date, count(*) as count FROM events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY label_0 ORDER BY count DESC LIMIT 10+-- V2+SELECT {p1:String} as label_0, {p2:String} as date, count(*) as count FROM events e WHERE project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) GROUP BY label_0 ORDER BY count DESC LIMIT {p7:UInt64}+-- V2 params: {"p1":"view_page","p2":"2026-07-06 00:00:00","p3":"verdict","p4":"view_page","p5":"2026-07-06 00:00:00","p6":"2026-07-12 23:59:59","p7":10}+```++### aggregateChartQuery — breakdown country + path, limit 25 (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 0/0; rows_read V1/V2 = 0/0; wall V1/V2 = 8 ms / 6 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT 'view_page' as label_0, '2026-07-06 00:00:00' as date, country as label_1, path as label_2, count(*) as count FROM events e WHERE project_id = 'secure-privacy' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY label_1, label_2, label_0 ORDER BY count DESC LIMIT 25+-- V2+SELECT {p1:String} as label_0, {p2:String} as date, country as label_1, path as label_2, count(*) as count FROM events e WHERE project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) GROUP BY label_1, label_2, label_0 ORDER BY count DESC LIMIT {p7:UInt64}+-- V2 params: {"p1":"view_page","p2":"2026-07-06 00:00:00","p3":"secure-privacy","p4":"view_page","p5":"2026-07-06 00:00:00","p6":"2026-07-12 23:59:59","p7":25}+```++### aggregateChartQuery — wildcard event + segment user + breakdown referrerName alias, no limit (verdict)++**statement** — IDENTICAL; rows V1/V2 = 1/1; rows_read V1/V2 = 1185604/998587; wall V1/V2 = 36 ms / 33 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT '*' as label_0, '2026-07-06 00:00:00' as date, referrer_name as label_1, countDistinct(profile_id) as count FROM events e WHERE project_id = 'verdict' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY label_1, label_0 ORDER BY count DESC+-- V2+SELECT '*' as label_0, {p1:String} as date, referrer_name as label_1, countDistinct(profile_id) as count FROM events e WHERE project_id = {p2:String} AND created_at >= toDateTime({p3:String}) AND created_at <= toDateTime({p4:String}) GROUP BY label_1, label_0 ORDER BY count DESC+-- V2 params: {"p1":"2026-07-06 00:00:00","p2":"verdict","p3":"2026-07-06 00:00:00","p4":"2026-07-08 23:59:59"}+```++### aggregateChartQuery — segment session + filter contains (verdict)++**statement** — IDENTICAL; rows V1/V2 = 0/0; rows_read V1/V2 = 1185604/0; wall V1/V2 = 21 ms / 7 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT 'view_page' as label_0, '2026-07-06 00:00:00' as date, countDistinct(session_id) as count FROM events e WHERE (path LIKE '%/%') AND project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY label_0 ORDER BY count DESC LIMIT 5+-- V2+SELECT {p1:String} as label_0, {p2:String} as date, countDistinct(session_id) as count FROM events e WHERE (path LIKE '%/%') AND project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) GROUP BY label_0 ORDER BY count DESC LIMIT {p7:UInt64}+-- V2 params: {"p1":"view_page","p2":"2026-07-06 00:00:00","p3":"verdict","p4":"view_page","p5":"2026-07-06 00:00:00","p6":"2026-07-08 23:59:59","p7":5}+```++### aggregateChartQuery — segment user_average + breakdown properties.__title (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 20/20; rows_read V1/V2 = 245717/98299; wall V1/V2 = 26 ms / 47 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT 'screen_view' as label_0, '2026-07-06 00:00:00' as date, e.properties['__title'] as label_1, COUNT(*)::float / COUNT(DISTINCT profile_id)::float as count FROM events e WHERE project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY label_1, label_0 ORDER BY count DESC LIMIT 20+-- V2+SELECT {p1:String} as label_0, {p2:String} as date, e.properties['__title'] as label_1, COUNT(*)::float / COUNT(DISTINCT profile_id)::float as count FROM events e WHERE project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) GROUP BY label_1, label_0 ORDER BY count DESC LIMIT {p7:UInt64}+-- V2 params: {"p1":"screen_view","p2":"2026-07-06 00:00:00","p3":"secure-privacy","p4":"screen_view","p5":"2026-07-06 00:00:00","p6":"2026-07-12 23:59:59","p7":20}+```++### aggregateChartQuery — segment group + breakdown group.name + group.properties.country + filter group.properties.employees gt (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 20/20; rows_read V1/V2 = 109848/109848; wall V1/V2 = 29 ms / 36 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+WITH _g AS (SELECT id, name, type, properties FROM groups FINAL WHERE project_id = 'secure-privacy') SELECT 'screen_view' as label_0, '2026-07-06 00:00:00' as date, _g.name as label_1, _g.properties['country'] as label_2, countDistinct(_group_id) as count FROM events e ARRAY JOIN groups AS _group_id LEFT ANY JOIN _g ON _g.id = _group_id WHERE (_g.properties['employees'] != '' AND _g.properties['employees'] IS NOT NULL) AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY label_1, label_2, label_0 ORDER BY count DESC LIMIT 20+-- V2+WITH _g AS (SELECT id, name, type, properties FROM groups FINAL WHERE project_id = {p1:String}) SELECT {p2:String} as label_0, {p3:String} as date, _g.name as label_1, _g.properties['country'] as label_2, countDistinct(_group_id) as count FROM events e ARRAY JOIN groups AS _group_id LEFT ANY JOIN _g ON _g.id = _group_id WHERE (_g.properties['employees'] != '' AND _g.properties['employees'] IS NOT NULL) AND project_id = {p4:String} AND e.name = {p5:String} AND created_at >= toDateTime({p6:String}) AND created_at <= toDateTime({p7:String}) GROUP BY label_1, label_2, label_0 ORDER BY count DESC LIMIT {p8:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"secure-privacy","p5":"screen_view","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":20}+```++### aggregateChartQuery — profile breakdown profile.properties.os + profile.last_name + filter profile.email endsWith (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 16/16; rows_read V1/V2 = 409275/409275; wall V1/V2 = 133 ms / 88 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+WITH profile AS (SELECT id as "profile.id", email as "profile.email", properties['os'] as `profile.properties.os`, last_name as "profile.last_name" FROM profiles FINAL WHERE project_id = 'secure-privacy') SELECT 'screen_view' as label_0, '2026-07-06 00:00:00' as date, `profile.properties.os` as label_1, profile.last_name as label_2, count(*) as count FROM events e LEFT ANY JOIN profile ON profile.id = profile_id WHERE project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') GROUP BY label_1, label_2, label_0 ORDER BY count DESC LIMIT 20+-- V2+WITH profile AS (SELECT id as "profile.id", email as "profile.email", properties['os'] as `profile.properties.os`, last_name as "profile.last_name" FROM profiles FINAL WHERE project_id = {p1:String}) SELECT {p2:String} as label_0, {p3:String} as date, `profile.properties.os` as label_1, profile.last_name as label_2, count(*) as count FROM events e LEFT ANY JOIN profile ON profile.id = profile_id WHERE project_id = {p4:String} AND e.name = {p5:String} AND created_at >= toDateTime({p6:String}) AND created_at <= toDateTime({p7:String}) GROUP BY label_1, label_2, label_0 ORDER BY count DESC LIMIT {p8:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"secure-privacy","p5":"screen_view","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":20}+```++### aggregateChartQuery — math property_sum on properties.value (project-resolved) + profile wildcard breakdown (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 0/0; rows_read V1/V2 = 556693/0; wall V1/V2 = 84 ms / 11 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+WITH profile AS (SELECT id as "profile.id", properties as "profile.properties" FROM profiles FINAL WHERE project_id = 'secure-privacy') SELECT 'screen_view' as label_0, '2026-07-06 00:00:00' as date, arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(profile.properties, 'profile.properties.browser.%'))) as label_1, sum(toFloat64OrNull(e.properties['value'])) as count FROM events e LEFT ANY JOIN profile ON profile.id = profile_id WHERE project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') AND e.properties['value'] IS NOT NULL AND notEmpty(e.properties['value']) GROUP BY label_1, label_0 ORDER BY count DESC LIMIT 20+-- V2+WITH profile AS (SELECT id as "profile.id", properties as "profile.properties" FROM profiles FINAL WHERE project_id = {p1:String}) SELECT {p2:String} as label_0, {p3:String} as date, arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(profile.properties, 'profile.properties.browser.%'))) as label_1, sum(toFloat64OrNull(e.properties['value'])) as count FROM events e LEFT ANY JOIN profile ON profile.id = profile_id WHERE project_id = {p4:String} AND e.name = {p5:String} AND created_at >= toDateTime({p6:String}) AND created_at <= toDateTime({p7:String}) AND e.properties['value'] IS NOT NULL AND notEmpty(e.properties['value']) GROUP BY label_1, label_0 ORDER BY count DESC LIMIT {p8:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"secure-privacy","p5":"screen_view","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59","p8":20}+```++### aggregateChartQuery — math property_average on duration (numeric column) + typed date filter on created_at (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 1/1; rows_read V1/V2 = 245717/98291; wall V1/V2 = 20 ms / 17 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT 'screen_view' as label_0, '2026-07-06 00:00:00' as date, avg(duration) as count FROM events e WHERE (toDate(parseDateTimeBestEffortOrNull(toString(created_at))) >= toDate(parseDateTimeBestEffortOrNull(toString('2026-07-08')))) AND project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') AND duration IS NOT NULL GROUP BY label_0 ORDER BY count DESC+-- V2+SELECT {p1:String} as label_0, {p2:String} as date, avg(duration) as count FROM events e WHERE (toDate(parseDateTimeBestEffortOrNull(toString(created_at))) >= toDate(parseDateTimeBestEffortOrNull(toString('2026-07-08')))) AND project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) AND duration IS NOT NULL GROUP BY label_0 ORDER BY count DESC+-- V2 params: {"p1":"screen_view","p2":"2026-07-06 00:00:00","p3":"secure-privacy","p4":"screen_view","p5":"2026-07-06 00:00:00","p6":"2026-07-12 23:59:59"}+```++### aggregateChartQuery — math property_max on group.properties.employees (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 1/1; rows_read V1/V2 = 109848/109848; wall V1/V2 = 28 ms / 59 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+WITH _g AS (SELECT id, name, type, properties FROM groups FINAL WHERE project_id = 'secure-privacy') SELECT 'screen_view' as label_0, '2026-07-06 00:00:00' as date, max(toFloat64OrNull(_g.properties['employees'])) as count FROM events e ARRAY JOIN groups AS _group_id LEFT ANY JOIN _g ON _g.id = _group_id WHERE project_id = 'secure-privacy' AND e.name = 'screen_view' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-12 23:59:59') AND _g.properties['employees'] IS NOT NULL AND notEmpty(_g.properties['employees']) GROUP BY label_0 ORDER BY count DESC+-- V2+WITH _g AS (SELECT id, name, type, properties FROM groups FINAL WHERE project_id = {p1:String}) SELECT {p2:String} as label_0, {p3:String} as date, max(toFloat64OrNull(_g.properties['employees'])) as count FROM events e ARRAY JOIN groups AS _group_id LEFT ANY JOIN _g ON _g.id = _group_id WHERE project_id = {p4:String} AND e.name = {p5:String} AND created_at >= toDateTime({p6:String}) AND created_at <= toDateTime({p7:String}) AND _g.properties['employees'] IS NOT NULL AND notEmpty(_g.properties['employees']) GROUP BY label_0 ORDER BY count DESC+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":"2026-07-06 00:00:00","p4":"secure-privacy","p5":"screen_view","p6":"2026-07-06 00:00:00","p7":"2026-07-12 23:59:59"}+```++### aggregateChartQuery — one_event_per_user + breakdown path (verdict) — V1 re-emits WHERE++**statement** — IDENTICAL ERROR (pre-existing V1 defect, both sides fail with the same ClickHouse error): `Unknown expression or function identifier `e.name` in scope SELECT 'view_page' AS label_0, '2026-07-06 00:00:00' AS date, path AS label_1, count(*) AS count FROM (SELECT * FROM events AS e WHERE (project_id = 'verdict') AND (e.name = 'view_page') AND (created_at >= toDateTime('2026-07-06 00:00:00')) AND (created_at <= toDateTime('2026-07-08 23:59:59')) ORDER BY profile_id ASC, created_at DESC LIMIT 1 BY profile_id) AS subQuery WHERE (project_id = 'verdict') AND (e.name = 'view_page') AND (created_at >= toDateTime('2026-07-06 00:00:00')) AND (created_at <= toDateTime('2026-07-08 23:59:59')) GROUP BY label_1, label_0.`++```sql+-- V1+SELECT 'view_page' as label_0, '2026-07-06 00:00:00' as date, path as label_1, count(*) as count FROM ( SELECT DISTINCT ON (profile_id) * from events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') ORDER BY profile_id, created_at DESC ) as subQuery WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY label_1, label_0+-- V2+SELECT {p1:String} as label_0, {p2:String} as date, path as label_1, count(*) as count FROM ( SELECT DISTINCT ON (profile_id) * from events e WHERE project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) ORDER BY profile_id, created_at DESC ) as subQuery WHERE project_id = {p7:String} AND e.name = {p8:String} AND created_at >= toDateTime({p9:String}) AND created_at <= toDateTime({p10:String}) GROUP BY label_1, label_0+-- V2 params: {"p1":"view_page","p2":"2026-07-06 00:00:00","p3":"verdict","p4":"view_page","p5":"2026-07-06 00:00:00","p6":"2026-07-08 23:59:59","p7":"verdict","p8":"view_page","p9":"2026-07-06 00:00:00","p10":"2026-07-08 23:59:59"}+```++### aggregateChartQuery — one_event_per_user, wildcard event, no breakdown (verdict)++**statement** — IDENTICAL; rows V1/V2 = 1/1; rows_read V1/V2 = 841552/622225; wall V1/V2 = 110 ms / 73 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT '*' as label_0, '2026-07-06 00:00:00' as date, count(*) as count FROM ( SELECT DISTINCT ON (profile_id) * from events e WHERE project_id = 'verdict' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-07 23:59:59') ORDER BY profile_id, created_at DESC ) as subQuery WHERE project_id = 'verdict' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-07 23:59:59') GROUP BY label_0+-- V2+SELECT '*' as label_0, {p1:String} as date, count(*) as count FROM ( SELECT DISTINCT ON (profile_id) * from events e WHERE project_id = {p2:String} AND created_at >= toDateTime({p3:String}) AND created_at <= toDateTime({p4:String}) ORDER BY profile_id, created_at DESC ) as subQuery WHERE project_id = {p5:String} AND created_at >= toDateTime({p6:String}) AND created_at <= toDateTime({p7:String}) GROUP BY label_0+-- V2 params: {"p1":"2026-07-06 00:00:00","p2":"verdict","p3":"2026-07-06 00:00:00","p4":"2026-07-07 23:59:59","p5":"verdict","p6":"2026-07-06 00:00:00","p7":"2026-07-07 23:59:59"}+```++### aggregateChartQuery — all-cohorts breakdown with named cohort + cohort:<id> breakdown (authc-project-a1)++**statement** — IDENTICAL; rows V1/V2 = 0/0; rows_read V1/V2 = 0/0; wall V1/V2 = 11 ms / 12 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+WITH _all_cohorts AS ( SELECT profile_id, cohort_id FROM cohort_members FINAL WHERE project_id = 'authc-project-a1' ), `cohort-cccccccc-0000-4000-8000-000000000001` AS ( SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = 'cccccccc-0000-4000-8000-000000000001' AND project_id = 'authc-project-a1' ) SELECT '*' as label_0, '2026-07-06 00:00:00' as date, transform(_all_cohorts.cohort_id, ['cccccccc-0000-4000-8000-000000000001'], ['Auth contract cohort'], 'Unknown') as label_1, if(notEmpty(cohort_cccccccc_0000_4000_8000_000000000001.profile_id), 'Auth contract cohort', 'Not Auth contract cohort') as label_2, count(*) as count FROM events e INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id LEFT ANY JOIN `cohort-cccccccc-0000-4000-8000-000000000001` AS cohort_cccccccc_0000_4000_8000_000000000001 ON cohort_cccccccc_0000_4000_8000_000000000001.profile_id = e.profile_id WHERE project_id = 'authc-project-a1' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY label_1, label_2, label_0 ORDER BY count DESC LIMIT 10+-- V2+WITH _all_cohorts AS ( SELECT profile_id, cohort_id FROM cohort_members FINAL WHERE project_id = {p1:String} ), `cohort-cccccccc-0000-4000-8000-000000000001` AS ( SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = {p2:String} AND project_id = {p3:String} ) SELECT '*' as label_0, {p4:String} as date, transform(_all_cohorts.cohort_id, {p5:Array(String)}, {p6:Array(String)}, {p7:String}) as label_1, if(notEmpty(cohort_cccccccc_0000_4000_8000_000000000001.profile_id), {p8:String}, {p9:String}) as label_2, count(*) as count FROM events e INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id LEFT ANY JOIN `cohort-cccccccc-0000-4000-8000-000000000001` AS cohort_cccccccc_0000_4000_8000_000000000001 ON cohort_cccccccc_0000_4000_8000_000000000001.profile_id = e.profile_id WHERE project_id = {p10:String} AND created_at >= toDateTime({p11:String}) AND created_at <= toDateTime({p12:String}) GROUP BY label_1, label_2, label_0 ORDER BY count DESC LIMIT {p13:UInt64}+-- V2 params: {"p1":"authc-project-a1","p2":"cccccccc-0000-4000-8000-000000000001","p3":"authc-project-a1","p4":"2026-07-06 00:00:00","p5":["cccccccc-0000-4000-8000-000000000001"],"p6":["Auth contract cohort"],"p7":"Unknown","p8":"Auth contract cohort","p9":"Not Auth contract cohort","p10":"authc-project-a1","p11":"2026-07-06 00:00:00","p12":"2026-07-08 23:59:59","p13":10}+```++### aggregateChartQuery — cohort:<id> breakdown unknown cohort + inCohort filter (verdict)++**statement** — IDENTICAL; rows V1/V2 = 0/0; rows_read V1/V2 = 0/0; wall V1/V2 = 11 ms / 11 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+WITH `cohort-44444444-4444-4444-8444-444444444444` AS ( SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = '44444444-4444-4444-8444-444444444444' AND project_id = 'verdict' ) SELECT 'view_page' as label_0, '2026-07-06 00:00:00' as date, if(notEmpty(cohort_44444444_4444_4444_8444_444444444444.profile_id), 'In Cohort', 'Not In Cohort') as label_1, count(*) as count FROM events e LEFT ANY JOIN `cohort-44444444-4444-4444-8444-444444444444` AS cohort_44444444_4444_4444_8444_444444444444 ON cohort_44444444_4444_4444_8444_444444444444.profile_id = e.profile_id WHERE e.profile_id IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id IN ('44444444-4444-4444-8444-444444444444') AND project_id = 'verdict') AND project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY label_1, label_0 ORDER BY count DESC LIMIT 10+-- V2+WITH `cohort-44444444-4444-4444-8444-444444444444` AS ( SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = {p1:String} AND project_id = {p2:String} ) SELECT {p3:String} as label_0, {p4:String} as date, if(notEmpty(cohort_44444444_4444_4444_8444_444444444444.profile_id), {p5:String}, {p6:String}) as label_1, count(*) as count FROM events e LEFT ANY JOIN `cohort-44444444-4444-4444-8444-444444444444` AS cohort_44444444_4444_4444_8444_444444444444 ON cohort_44444444_4444_4444_8444_444444444444.profile_id = e.profile_id WHERE e.profile_id IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id IN ('44444444-4444-4444-8444-444444444444') AND project_id = 'verdict') AND project_id = {p7:String} AND e.name = {p8:String} AND created_at >= toDateTime({p9:String}) AND created_at <= toDateTime({p10:String}) GROUP BY label_1, label_0 ORDER BY count DESC LIMIT {p11:UInt64}+-- V2 params: {"p1":"44444444-4444-4444-8444-444444444444","p2":"verdict","p3":"view_page","p4":"2026-07-06 00:00:00","p5":"In Cohort","p6":"Not In Cohort","p7":"verdict","p8":"view_page","p9":"2026-07-06 00:00:00","p10":"2026-07-08 23:59:59","p11":10}+```++### aggregateChartQuery — all-cohorts breakdown dropped (verdict, no cohorts) + unknown field dropped++**statement** — IDENTICAL; rows V1/V2 = 2/2; rows_read V1/V2 = 1185604/982645; wall V1/V2 = 36 ms / 32 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT 'view_page' as label_0, '2026-07-06 00:00:00' as date, if(profile_id != device_id, 'true', 'false') as label_1, count(*) as count FROM events e WHERE project_id = 'verdict' AND e.name = 'view_page' AND created_at >= toDateTime('2026-07-06 00:00:00') AND created_at <= toDateTime('2026-07-08 23:59:59') GROUP BY label_1, label_0 ORDER BY count DESC LIMIT 10+-- V2+SELECT {p1:String} as label_0, {p2:String} as date, if(profile_id != device_id, 'true', 'false') as label_1, count(*) as count FROM events e WHERE project_id = {p3:String} AND e.name = {p4:String} AND created_at >= toDateTime({p5:String}) AND created_at <= toDateTime({p6:String}) GROUP BY label_1, label_0 ORDER BY count DESC LIMIT {p7:UInt64}+-- V2 params: {"p1":"view_page","p2":"2026-07-06 00:00:00","p3":"verdict","p4":"view_page","p5":"2026-07-06 00:00:00","p6":"2026-07-08 23:59:59","p7":10}+```++## Router queries (projectCard, events, properties, values, getProfiles)++### projectCardChartQuery — projectCard chart (verdict, UTC)++**statement** — IDENTICAL; rows V1/V2 = 92/92; rows_read V1/V2 = 1273247/1273247; wall V1/V2 = 99 ms / 62 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT uniqHLL12(profile_id) as value, toStartOfDay(created_at) as date, sum(revenue * sign) as revenue FROM sessions WHERE project_id = 'verdict' AND created_at >= now() - interval '3 month' GROUP BY date ORDER BY date ASC WITH FILL FROM toStartOfDay(now() - interval '3 month') TO toStartOfDay(now()) STEP INTERVAL 1 day SETTINGS session_timezone = 'UTC'+-- V2+SELECT uniqHLL12(profile_id) as value, toStartOfDay(created_at) as date, sum(revenue * sign) as revenue FROM sessions WHERE project_id = {p1:String} AND created_at >= now() - interval '3 month' GROUP BY date ORDER BY date ASC WITH FILL FROM toStartOfDay(now() - interval '3 month') TO toStartOfDay(now()) STEP INTERVAL 1 day+-- V2 params: {"p1":"verdict"}+```++### projectCardMetricsQuery — projectCard metrics (verdict, UTC)++**statement** — IDENTICAL; rows V1/V2 = 1/1; rows_read V1/V2 = 1273247/1273247; wall V1/V2 = 67 ms / 60 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT uniqHLL12(if(created_at >= (now() - toIntervalMonth(3)), profile_id, null)) AS months_3, uniqHLL12(if(created_at >= (now() - toIntervalMonth(6)) AND created_at < (now() - toIntervalMonth(3)), profile_id, null)) AS months_3_prev, uniqHLL12(if(created_at >= (now() - toIntervalMonth(1)), profile_id, null)) AS month, uniqHLL12(if(created_at >= (now() - toIntervalDay(1)), profile_id, null)) AS day, uniqHLL12(if(created_at >= (now() - toIntervalDay(2)) AND created_at < (now() - toIntervalDay(1)), profile_id, null)) AS day_prev, sum(revenue * sign) as revenue FROM sessions WHERE project_id = 'verdict' AND created_at >= (now() - toIntervalMonth(6))+-- V2+SELECT uniqHLL12(if(created_at >= (now() - toIntervalMonth(3)), profile_id, null)) AS months_3, uniqHLL12(if(created_at >= (now() - toIntervalMonth(6)) AND created_at < (now() - toIntervalMonth(3)), profile_id, null)) AS months_3_prev, uniqHLL12(if(created_at >= (now() - toIntervalMonth(1)), profile_id, null)) AS month, uniqHLL12(if(created_at >= (now() - toIntervalDay(1)), profile_id, null)) AS day, uniqHLL12(if(created_at >= (now() - toIntervalDay(2)) AND created_at < (now() - toIntervalDay(1)), profile_id, null)) AS day_prev, sum(revenue * sign) as revenue FROM sessions WHERE project_id = {p1:String} AND created_at >= (now() - toIntervalMonth(6))+-- V2 params: {"p1":"verdict"}+```++### projectCardChartQuery — projectCard chart (bayse, America/New_York)++**statement** — IDENTICAL; rows V1/V2 = 92/92; rows_read V1/V2 = 835382/835382; wall V1/V2 = 46 ms / 65 ms; clickhouse_settings: session_timezone=America/New_York (both).++```sql+-- V1+SELECT uniqHLL12(profile_id) as value, toStartOfDay(created_at) as date, sum(revenue * sign) as revenue FROM sessions WHERE project_id = 'bayse' AND created_at >= now() - interval '3 month' GROUP BY date ORDER BY date ASC WITH FILL FROM toStartOfDay(now() - interval '3 month') TO toStartOfDay(now()) STEP INTERVAL 1 day SETTINGS session_timezone = 'America/New_York'+-- V2+SELECT uniqHLL12(profile_id) as value, toStartOfDay(created_at) as date, sum(revenue * sign) as revenue FROM sessions WHERE project_id = {p1:String} AND created_at >= now() - interval '3 month' GROUP BY date ORDER BY date ASC WITH FILL FROM toStartOfDay(now() - interval '3 month') TO toStartOfDay(now()) STEP INTERVAL 1 day+-- V2 params: {"p1":"bayse"}+```++### projectCardMetricsQuery — projectCard metrics (bayse, America/New_York)++**statement** — IDENTICAL; rows V1/V2 = 1/1; rows_read V1/V2 = 835382/835382; wall V1/V2 = 43 ms / 68 ms; clickhouse_settings: session_timezone=America/New_York (both).++```sql+-- V1+SELECT uniqHLL12(if(created_at >= (now() - toIntervalMonth(3)), profile_id, null)) AS months_3, uniqHLL12(if(created_at >= (now() - toIntervalMonth(6)) AND created_at < (now() - toIntervalMonth(3)), profile_id, null)) AS months_3_prev, uniqHLL12(if(created_at >= (now() - toIntervalMonth(1)), profile_id, null)) AS month, uniqHLL12(if(created_at >= (now() - toIntervalDay(1)), profile_id, null)) AS day, uniqHLL12(if(created_at >= (now() - toIntervalDay(2)) AND created_at < (now() - toIntervalDay(1)), profile_id, null)) AS day_prev, sum(revenue * sign) as revenue FROM sessions WHERE project_id = 'bayse' AND created_at >= (now() - toIntervalMonth(6))+-- V2+SELECT uniqHLL12(if(created_at >= (now() - toIntervalMonth(3)), profile_id, null)) AS months_3, uniqHLL12(if(created_at >= (now() - toIntervalMonth(6)) AND created_at < (now() - toIntervalMonth(3)), profile_id, null)) AS months_3_prev, uniqHLL12(if(created_at >= (now() - toIntervalMonth(1)), profile_id, null)) AS month, uniqHLL12(if(created_at >= (now() - toIntervalDay(1)), profile_id, null)) AS day, uniqHLL12(if(created_at >= (now() - toIntervalDay(2)) AND created_at < (now() - toIntervalDay(1)), profile_id, null)) AS day_prev, sum(revenue * sign) as revenue FROM sessions WHERE project_id = {p1:String} AND created_at >= (now() - toIntervalMonth(6))+-- V2 params: {"p1":"bayse"}+```++### eventNamesWithCountQuery — events (verdict)++**statement** — IDENTICAL; rows V1/V2 = 85/85; rows_read V1/V2 = 221184/221184; wall V1/V2 = 11 ms / 11 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT name, count(name) as count FROM distinct_event_names_mv WHERE project_id = 'verdict' GROUP BY name ORDER BY count DESC, name ASC+-- V2+SELECT name, count(name) as count FROM distinct_event_names_mv WHERE project_id = {p1:String} GROUP BY name ORDER BY count DESC, name ASC+-- V2 params: {"p1":"verdict"}+```++### eventPropertyKeysQuery — properties (secure-privacy, event=undefined)++**statement** — IDENTICAL; rows V1/V2 = 174/174; rows_read V1/V2 = 128264/128264; wall V1/V2 = 18 ms / 11 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct property_key, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = 'secure-privacy' GROUP BY property_key ORDER BY created_at DESC, property_key ASC LIMIT 50000+-- V2+SELECT distinct property_key, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = {p1:String} GROUP BY property_key ORDER BY created_at DESC, property_key ASC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":50000}+```++### eventPropertyKeysQuery — properties (secure-privacy, event=*)++**statement** — IDENTICAL; rows V1/V2 = 174/174; rows_read V1/V2 = 128264/128264; wall V1/V2 = 11 ms / 10 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct property_key, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = 'secure-privacy' GROUP BY property_key ORDER BY created_at DESC, property_key ASC LIMIT 50000+-- V2+SELECT distinct property_key, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = {p1:String} GROUP BY property_key ORDER BY created_at DESC, property_key ASC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":50000}+```++### eventPropertyKeysQuery — properties (secure-privacy, event=screen_view)++**statement** — IDENTICAL; rows V1/V2 = 156/156; rows_read V1/V2 = 128264/128264; wall V1/V2 = 13 ms / 11 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct property_key, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = 'secure-privacy' AND name = 'screen_view' GROUP BY property_key ORDER BY created_at DESC, property_key ASC LIMIT 50000+-- V2+SELECT distinct property_key, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = {p1:String} AND name = {p2:String} GROUP BY property_key ORDER BY created_at DESC, property_key ASC LIMIT {p3:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":50000}+```++### eventPropertyValuesQuery — values properties.__title (secure-privacy, event='*')++**statement** — IDENTICAL; rows V1/V2 = 500/500; rows_read V1/V2 = 270336/172032; wall V1/V2 = 30 ms / 14 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct property_value, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = 'secure-privacy' AND property_key = '__title' GROUP BY property_value ORDER BY created_at DESC, property_value ASC LIMIT 500+-- V2+SELECT distinct property_value, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = {p1:String} AND property_key = {p2:String} GROUP BY property_value ORDER BY created_at DESC, property_value ASC LIMIT {p3:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":"__title","p3":500}+```++### eventPropertyValuesQuery — values properties.__query.gclid (secure-privacy, event='screen_view')++**statement** — IDENTICAL; rows V1/V2 = 500/500; rows_read V1/V2 = 131072/114688; wall V1/V2 = 16 ms / 12 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct property_value, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = 'secure-privacy' AND property_key = '__query.gclid' AND name = 'screen_view' GROUP BY property_value ORDER BY created_at DESC, property_value ASC LIMIT 500+-- V2+SELECT distinct property_value, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = {p1:String} AND property_key = {p2:String} AND name = {p3:String} GROUP BY property_value ORDER BY created_at DESC, property_value ASC LIMIT {p4:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":"__query.gclid","p3":"screen_view","p4":500}+```++### eventPropertyValuesQuery — values properties.value (secure-privacy, event='')++**statement** — IDENTICAL; rows V1/V2 = 500/500; rows_read V1/V2 = 270336/221184; wall V1/V2 = 14 ms / 12 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct property_value, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = 'secure-privacy' AND property_key = 'value' GROUP BY property_value ORDER BY created_at DESC, property_value ASC LIMIT 500+-- V2+SELECT distinct property_value, max(created_at) as created_at FROM event_property_values_mv WHERE project_id = {p1:String} AND property_key = {p2:String} GROUP BY property_value ORDER BY created_at DESC, property_value ASC LIMIT {p3:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":"value","p3":500}+```++### profilePropertyValuesQuery — values profile.email (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 1251/1251; rows_read V1/V2 = 310976/310976; wall V1/V2 = 32 ms / 33 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct email as values FROM profiles FINAL WHERE project_id = 'secure-privacy' AND email != '' AND email IS NOT NULL ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct email as values FROM profiles FINAL WHERE project_id = {p1:String} AND email != '' AND email IS NOT NULL ORDER BY created_at DESC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":100000}+```++### profilePropertyValuesQuery — values profile.properties.os (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 12/12; rows_read V1/V2 = 310976/310976; wall V1/V2 = 82 ms / 71 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct properties['os'] as values FROM profiles FINAL WHERE project_id = 'secure-privacy' AND properties['os'] != '' AND properties['os'] IS NOT NULL ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct properties['os'] as values FROM profiles FINAL WHERE project_id = {p1:String} AND properties['os'] != '' AND properties['os'] IS NOT NULL ORDER BY created_at DESC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":100000}+```++### profilePropertyValuesQuery — values profile.properties.it's"odd (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 0/0; rows_read V1/V2 = 310976/310976; wall V1/V2 = 65 ms / 67 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct properties['it\'s\"odd'] as values FROM profiles FINAL WHERE project_id = 'secure-privacy' AND properties['it\'s\"odd'] != '' AND properties['it\'s\"odd'] IS NOT NULL ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct properties['it\'s\"odd'] as values FROM profiles FINAL WHERE project_id = {p1:String} AND properties['it\'s\"odd'] != '' AND properties['it\'s\"odd'] IS NOT NULL ORDER BY created_at DESC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":100000}+```++### profilePropertyValuesQuery — values profile.unknown (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 100000/100000; rows_read V1/V2 = 310976/310976; wall V1/V2 = 96 ms / 116 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct id as values FROM profiles FINAL WHERE project_id = 'secure-privacy' AND id != '' AND id IS NOT NULL ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct id as values FROM profiles FINAL WHERE project_id = {p1:String} AND id != '' AND id IS NOT NULL ORDER BY created_at DESC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":100000}+```++### groupPropertyValuesQuery — values group.name (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 345/345; rows_read V1/V2 = 3359/3359; wall V1/V2 = 8 ms / 8 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct name as values FROM groups FINAL WHERE project_id = 'secure-privacy' AND deleted = 0 AND name != '' AND name IS NOT NULL ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct name as values FROM groups FINAL WHERE project_id = {p1:String} AND deleted = 0 AND name != '' AND name IS NOT NULL ORDER BY created_at DESC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":100000}+```++### groupPropertyValuesQuery — values group.type (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 1/1; rows_read V1/V2 = 3359/3359; wall V1/V2 = 6 ms / 10 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct type as values FROM groups FINAL WHERE project_id = 'secure-privacy' AND deleted = 0 AND type != '' AND type IS NOT NULL ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct type as values FROM groups FINAL WHERE project_id = {p1:String} AND deleted = 0 AND type != '' AND type IS NOT NULL ORDER BY created_at DESC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":100000}+```++### groupPropertyValuesQuery — values group.properties.employees (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 3/3; rows_read V1/V2 = 3359/3359; wall V1/V2 = 9 ms / 9 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct properties['employees'] as values FROM groups FINAL WHERE project_id = 'secure-privacy' AND deleted = 0 AND properties['employees'] != '' AND properties['employees'] IS NOT NULL ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct properties['employees'] as values FROM groups FINAL WHERE project_id = {p1:String} AND deleted = 0 AND properties['employees'] != '' AND properties['employees'] IS NOT NULL ORDER BY created_at DESC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":100000}+```++### groupPropertyValuesQuery — values group.id (secure-privacy)++**statement** — IDENTICAL; rows V1/V2 = 347/347; rows_read V1/V2 = 3359/3359; wall V1/V2 = 7 ms / 7 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct id as values FROM groups FINAL WHERE project_id = 'secure-privacy' AND deleted = 0 AND id != '' AND id IS NOT NULL ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct id as values FROM groups FINAL WHERE project_id = {p1:String} AND deleted = 0 AND id != '' AND id IS NOT NULL ORDER BY created_at DESC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":100000}+```++### eventFieldValuesQuery — values country (secure-privacy, event='*')++**statement** — IDENTICAL; rows V1/V2 = 185/185; rows_read V1/V2 = 606120/606120; wall V1/V2 = 29 ms / 20 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct country as values FROM events WHERE project_id = 'secure-privacy' AND created_at > (now() - INTERVAL 6 MONTH) ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct country as values FROM events WHERE project_id = {p1:String} AND created_at > (now() - INTERVAL 6 MONTH) ORDER BY created_at DESC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":100000}+```++### eventFieldValuesQuery — values referrerName (secure-privacy, event='screen_view')++**statement** — IDENTICAL; rows V1/V2 = 826/826; rows_read V1/V2 = 606120/606120; wall V1/V2 = 29 ms / 25 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct referrer_name as values FROM events WHERE project_id = 'secure-privacy' AND created_at > (now() - INTERVAL 6 MONTH) AND name = 'screen_view' ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct referrer_name as values FROM events WHERE project_id = {p1:String} AND created_at > (now() - INTERVAL 6 MONTH) AND name = {p2:String} ORDER BY created_at DESC LIMIT {p3:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":100000}+```++### eventFieldValuesQuery — values utm_source (secure-privacy, event='screen_view')++**statement** — IDENTICAL; rows V1/V2 = 89/89; rows_read V1/V2 = 606120/606120; wall V1/V2 = 59 ms / 53 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct properties['__query.utm_source'] as values FROM events WHERE project_id = 'secure-privacy' AND created_at > (now() - INTERVAL 6 MONTH) AND name = 'screen_view' ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct properties['__query.utm_source'] as values FROM events WHERE project_id = {p1:String} AND created_at > (now() - INTERVAL 6 MONTH) AND name = {p2:String} ORDER BY created_at DESC LIMIT {p3:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":100000}+```++### eventFieldValuesQuery — values properties.__query[*] (secure-privacy, event='*')++**statement** — IDENTICAL; rows V1/V2 = 6532/6532; rows_read V1/V2 = 606120/606120; wall V1/V2 = 79 ms / 86 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(properties, '__query.%'))) as values FROM events WHERE project_id = 'secure-privacy' AND created_at > (now() - INTERVAL 6 MONTH) ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(properties, '__query.%'))) as values FROM events WHERE project_id = {p1:String} AND created_at > (now() - INTERVAL 6 MONTH) ORDER BY created_at DESC LIMIT {p2:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":100000}+```++### eventFieldValuesQuery — values path (secure-privacy, event='screen_view')++**statement** — IDENTICAL; rows V1/V2 = 6454/6454; rows_read V1/V2 = 606120/606120; wall V1/V2 = 49 ms / 41 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT distinct path as values FROM events WHERE project_id = 'secure-privacy' AND created_at > (now() - INTERVAL 6 MONTH) AND name = 'screen_view' ORDER BY created_at DESC LIMIT 100000+-- V2+SELECT distinct path as values FROM events WHERE project_id = {p1:String} AND created_at > (now() - INTERVAL 6 MONTH) AND name = {p2:String} ORDER BY created_at DESC LIMIT {p3:UInt64}+-- V2 params: {"p1":"secure-privacy","p2":"screen_view","p3":100000}+```++### chartBucketProfilesQuery — getProfiles day bucket, screen_view, no breakdowns++**statement** — IDENTICAL; rows V1/V2 = 4092/4092; rows_read V1/V2 = 212951/49148; wall V1/V2 = 18 ms / 17 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT DISTINCT profile_id FROM events e WHERE project_id = 'secure-privacy' AND toStartOfDay(created_at) = toDate('2026-07-07 00:00:00') AND name = 'screen_view'+-- V2+SELECT DISTINCT profile_id FROM events e WHERE project_id = {p1:String} AND toStartOfDay(created_at) = toDate({p2:String}) AND name = {p3:String}+-- V2 params: {"p1":"secure-privacy","p2":"2026-07-07 00:00:00","p3":"screen_view"}+```++### chartBucketProfilesQuery — getProfiles hour bucket, wildcard event, country filter++**statement** — IDENTICAL; rows V1/V2 = 181/181; rows_read V1/V2 = 212951/8192; wall V1/V2 = 17 ms / 6 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT DISTINCT profile_id FROM events e WHERE country = 'US' AND project_id = 'secure-privacy' AND toStartOfHour(created_at) = toDateTime('2026-07-07 10:00:00')+-- V2+SELECT DISTINCT profile_id FROM events e WHERE country = 'US' AND project_id = {p1:String} AND toStartOfHour(created_at) = toDateTime({p2:String})+-- V2 params: {"p1":"secure-privacy","p2":"2026-07-07 10:00:00"}+```++### chartBucketProfilesQuery — getProfiles minute bucket++**statement** — IDENTICAL; rows V1/V2 = 8/8; rows_read V1/V2 = 212951/8192; wall V1/V2 = 11 ms / 10 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT DISTINCT profile_id FROM events e WHERE project_id = 'secure-privacy' AND toStartOfMinute(created_at) = toDateTime('2026-07-07 10:15:00') AND name = 'screen_view'+-- V2+SELECT DISTINCT profile_id FROM events e WHERE project_id = {p1:String} AND toStartOfMinute(created_at) = toDateTime({p2:String}) AND name = {p3:String}+-- V2 params: {"p1":"secure-privacy","p2":"2026-07-07 10:15:00","p3":"screen_view"}+```++### chartBucketProfilesQuery — getProfiles week bucket + breakdowns country/path++**statement** — IDENTICAL; rows V1/V2 = 0/0; rows_read V1/V2 = 204759/0; wall V1/V2 = 16 ms / 6 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT DISTINCT profile_id FROM events e WHERE project_id = 'secure-privacy' AND toStartOfWeek(toDateTime(created_at)) = toDate('2026-07-06 00:00:00') AND name = 'screen_view' AND country = 'US' AND path = '/'+-- V2+SELECT DISTINCT profile_id FROM events e WHERE project_id = {p1:String} AND toStartOfWeek(toDateTime(created_at)) = toDate({p2:String}) AND name = {p3:String} AND country = {p4:String} AND path = {p5:String}+-- V2 params: {"p1":"secure-privacy","p2":"2026-07-06 00:00:00","p3":"screen_view","p4":"US","p5":"/"}+```++### chartBucketProfilesQuery — getProfiles month bucket + profile filter/breakdown join++**statement** — IDENTICAL; rows V1/V2 = 0/0; rows_read V1/V2 = 671345/671345; wall V1/V2 = 118 ms / 107 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT DISTINCT profile_id FROM events e LEFT ANY JOIN (SELECT id, email, properties, first_name FROM profiles FINAL WHERE project_id = 'secure-privacy') as profile on profile.id = profile_id WHERE project_id = 'secure-privacy' AND toStartOfMonth(toDateTime(created_at)) = toDate('2026-07-01 00:00:00') AND name = 'screen_view' AND profile.properties['os'] = 'Mac OS' AND profile.first_name = 'x'+-- V2+SELECT DISTINCT profile_id FROM events e LEFT ANY JOIN (SELECT id, email, properties, first_name FROM profiles FINAL WHERE project_id = {p1:String}) as profile on profile.id = profile_id WHERE project_id = {p2:String} AND toStartOfMonth(toDateTime(created_at)) = toDate({p3:String}) AND name = {p4:String} AND profile.properties['os'] = {p5:String} AND profile.first_name = {p6:String}+-- V2 params: {"p1":"secure-privacy","p2":"secure-privacy","p3":"2026-07-01 00:00:00","p4":"screen_view","p5":"Mac OS","p6":"x"}+```++### chartBucketProfilesQuery — getProfiles day bucket + group filter/breakdown join++**statement** — IDENTICAL; rows V1/V2 = 0/0; rows_read V1/V2 = 216310/3359; wall V1/V2 = 16 ms / 9 ms; clickhouse_settings: session_timezone=UTC (both).++```sql+-- V1+SELECT DISTINCT profile_id FROM events e ARRAY JOIN groups AS _group_id LEFT ANY JOIN (SELECT id, name, type, properties FROM groups FINAL WHERE project_id = 'secure-privacy') AS _g ON _g.id = _group_id WHERE _g.type = 'company' AND project_id = 'secure-privacy' AND toStartOfDay(created_at) = toDate('2026-07-07 00:00:00') AND name = 'screen_view' AND _g.name = 'Secureprivacy' AND properties['__title'] = 'Secure Privacy'+-- V2+SELECT DISTINCT profile_id FROM events e ARRAY JOIN groups AS _group_id LEFT ANY JOIN (SELECT id, name, type, properties FROM groups FINAL WHERE project_id = {p1:String}) AS _g ON _g.id = _group_id WHERE _g.type = 'company' AND project_id = {p2:String} AND toStartOfDay(created_at) = toDate({p3:String}) AND name = {p4:String} AND _g.name = {p5:String} AND properties['__title'] = {p6:String}+-- V2 params: {"p1":"secure-privacy","p2":"secure-privacy","p3":"2026-07-07 00:00:00","p4":"screen_view","p5":"Secureprivacy","p6":"Secure Privacy"}+```diff --git a/packages/core/src/modules/chart/src/chart.sql.test.ts b/packages/core/src/modules/chart/src/chart.sql.test.tsnew file mode 100644index 000000000..5183742b5--- /dev/null+++ b/packages/core/src/modules/chart/src/chart.sql.test.ts@@ -0,0 +1,610 @@+/**+ * SQL-shape tests for the chart statements. Ported from+ * packages/db/src/services/chart-sql.test.ts (M7-003); the overview+ * `getRawWhereClause` cases stayed there with their subject.+ *+ * Strategy: render the statement, then run `EXPLAIN <sql>` with its bound+ * params against the isolated `openpanel_test` ClickHouse (pinned by+ * test/preload.ts). EXPLAIN parses, resolves columns and builds the plan+ * without executing, so UNKNOWN_IDENTIFIER / AMBIGUOUS_IDENTIFIER / bad JOIN+ * ON expressions surface without seeded data. WITH FILL TO < FROM is a+ * runtime check, so it is covered by a plain string assertion instead.+ *+ * Text assertions look at the rendered `query`; chart-level values are+ * `{pN:Type}` placeholders there, filter/breakdown expressions are the+ * verbatim V1 text (see field-resolution.ts's header).+ *+ * The statements' only Postgres read (the project's cohorts, for the+ * all-cohorts breakdown) is stubbed to "no cohorts": that is the precondition+ * the all-cohorts cases assert on, and it keeps this file independent of+ * whichever partial `prisma-client` mock a module-root service test left+ * behind (bun runs every file in one shared registry without `--isolate`).+ * The real Postgres cohort path runs in ../chart.service.test.ts.+ */++import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test';+import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';+import type { IChartBreakdown, IChartEvent } from '@openpanel/validation';+import type { AggregateChartSqlInput, ChartSqlInput } from './chart-statement';++const cohortFindMany = mock(async () => []);+// Plain-object snapshot, not the live binding — restoring from the namespace+// import in afterAll would just re-apply the mock (see cohort.service.test.ts).+const actualPrismaClient = await import('@openpanel/db/src/prisma-client');+const realPrismaClient = { ...actualPrismaClient };+mock.module('@openpanel/db/src/prisma-client', () => ({+ ...realPrismaClient,+ db: { cohort: { findMany: cohortFindMany } },+}));++afterAll(() => {+ mock.module('@openpanel/db/src/prisma-client', () => realPrismaClient);+});++let ch: typeof import('@openpanel/db/src/clickhouse/client').ch;+let buildChartSql: typeof import('./chart-statement').getChartSql;+let buildAggregateChartSql: typeof import('./chart-statement').getAggregateChartSql;++// IGetChartDataInput has display-only fields (metric, chartType, previous,+// etc.) that the SQL builders ignore; loosen the input type here.+type LooseChartInput = Partial<ChartSqlInput> &+ Pick<ChartSqlInput, 'event' | 'projectId' | 'timezone'>;+type LooseAggregateInput = Partial<AggregateChartSqlInput> &+ Pick<AggregateChartSqlInput, 'event' | 'projectId' | 'timezone'>;++async function getChartSql(input: LooseChartInput) {+ return render(await buildChartSql(input as ChartSqlInput));+}++async function getAggregateChartSql(input: LooseAggregateInput) {+ return render(await buildAggregateChartSql(input as AggregateChartSqlInput));+}++interface Rendered {+ sql: string;+ params: Record<string, unknown>;+}++function render(fragment: SqlFragment): Rendered {+ const { query, query_params } = fragment.toStatement();+ return { sql: query, params: query_params };+}++async function explain({ sql, params }: Rendered): Promise<void> {+ await ch.command({ query: `EXPLAIN ${sql}`, query_params: params });+}++const PROJECT_ID = 'test-sql-validation';+const START = '2026-04-14 00:00:00';+const END = '2026-05-15 00:00:00';++const event = (overrides: Partial<IChartEvent> = {}): IChartEvent => ({+ id: 'A',+ name: 'screen_view',+ segment: 'event',+ filters: [],+ ...overrides,+});++const breakdown = (name: string): IChartBreakdown => ({ id: name, name });++const base = {+ interval: 'day' as const,+ startDate: START,+ endDate: END,+ projectId: PROJECT_ID,+ timezone: 'UTC',+};++beforeAll(async () => {+ ({ ch } = await import('@openpanel/db/src/clickhouse/client'));+ ({+ getChartSql: buildChartSql,+ getAggregateChartSql: buildAggregateChartSql,+ } = await import('./chart-statement'));+ // EXPLAIN needs the openpanel_test schema to resolve columns against.+ const { bootstrapTestDatabases } = await import(+ '../../../../../../test/bootstrap-databases'+ );+ await bootstrapTestDatabases();+}, 30_000);++describe('getChartSql', () => {+ it('qualifies properties[...] with `e.` when group join is present (fixes AMBIGUOUS_IDENTIFIER)', async () => {+ const rendered = await getChartSql({+ event: event({+ segment: 'session',+ filters: [+ { name: 'group.plan', operator: 'is', value: ['pro'] },+ {+ name: 'properties.__query.utm_source',+ operator: 'is',+ value: ['awn'],+ },+ ],+ }),+ breakdowns: [+ breakdown('country'),+ breakdown('properties.__query.utm_source'),+ ],+ ...base,+ });++ // Every map access on the events table must be aliased — the `_g` join+ // also exposes a `properties` column, so the bare form is ambiguous.+ expect(rendered.sql).not.toMatch(/(?<![._\w])properties\[/);+ expect(rendered.sql).toContain("e.properties['__query.utm_source']");++ await explain(rendered);+ });++ it('works without group join (qualified form is still valid when alone)', async () => {+ await explain(+ await getChartSql({+ event: event({+ filters: [+ {+ name: 'properties.__query.utm_source',+ operator: 'is',+ value: ['awn'],+ },+ ],+ }),+ breakdowns: [breakdown('properties.__query.utm_source')],+ ...base,+ })+ );+ });++ it('drops the all-cohorts breakdown when the project has 0 cohorts', async () => {+ cohortFindMany.mockClear();+ const rendered = await getChartSql({+ event: event({ segment: 'user' }),+ breakdowns: [breakdown('cohort')],+ ...base,+ });++ expect(cohortFindMany).toHaveBeenCalledWith(+ expect.objectContaining({ where: { projectId: PROJECT_ID } })+ );+ // The all-cohorts JOIN expanded to `_uc._uc_label_X = 'Unknown'`, a+ // constant predicate with no join key — CH rejects it. The fix is to+ // remove the breakdown entirely when there are no cohorts.+ expect(rendered.sql).not.toMatch(/_uc_label_\d+\s*=\s*'Unknown'/);+ expect(rendered.sql).not.toContain('_all_cohorts');++ await explain(rendered);+ });++ it('skips WITH FILL when endDate < startDate', async () => {+ const rendered = await getChartSql({+ event: event(),+ breakdowns: [],+ ...base,+ startDate: END, // inverted+ endDate: START,+ });++ expect(rendered.sql).not.toContain('WITH FILL');+ await explain(rendered);+ });++ it('skips WITH FILL when endDate equals startDate inverted (week)', async () => {+ const rendered = await getChartSql({+ event: event(),+ breakdowns: [],+ ...base,+ interval: 'week',+ startDate: END,+ endDate: START,+ });+ expect(rendered.sql).not.toContain('WITH FILL');+ });++ it('emits WITH FILL when the range is valid', async () => {+ const rendered = await getChartSql({+ event: event(),+ breakdowns: [],+ ...base,+ });+ expect(rendered.sql).toContain('WITH FILL');+ await explain(rendered);+ });++ it('property metric (property_sum) with group join is unambiguous', async () => {+ const rendered = await getChartSql({+ event: event({+ segment: 'property_sum',+ property: 'properties.revenue_amount',+ filters: [{ name: 'group.plan', operator: 'is', value: ['pro'] }],+ }),+ breakdowns: [],+ ...base,+ });+ expect(rendered.sql).toContain("e.properties['revenue_amount']");+ await explain(rendered);+ });++ // Filter value type casting (filter.type). A date property compared with+ // `gte` used to crash with `toFloat64('2019-01-01')`; declaring the type+ // routes both column and value through the matching ClickHouse cast.+ it('date-typed gte filter casts via best-effort toDate, not toFloat64', async () => {+ const rendered = await getChartSql({+ event: event({+ filters: [+ {+ name: 'properties.cook',+ operator: 'gte',+ value: ['2019-01-01'],+ type: 'date',+ },+ ],+ }),+ breakdowns: [],+ ...base,+ });+ expect(rendered.sql).toContain('toDate(parseDateTimeBestEffortOrNull');+ expect(rendered.sql).not.toContain('toFloat64');+ await explain(rendered);+ });++ it('number-typed gte filter casts via toFloat64OrNull', async () => {+ const rendered = await getChartSql({+ event: event({+ filters: [+ {+ name: 'properties.age',+ operator: 'gte',+ value: ['5'],+ type: 'number',+ },+ ],+ }),+ breakdowns: [],+ ...base,+ });+ expect(rendered.sql).toContain('toFloat64OrNull');+ await explain(rendered);+ });++ it('untyped gte filter keeps legacy toFloat64 casting', async () => {+ const rendered = await getChartSql({+ event: event({+ filters: [{ name: 'properties.age', operator: 'gte', value: ['5'] }],+ }),+ breakdowns: [],+ ...base,+ });+ expect(rendered.sql).toContain('toFloat64OrZero');+ await explain(rendered);+ });++ it('one_event_per_user segment still parses', async () => {+ await explain(+ await getChartSql({+ event: event({ segment: 'one_event_per_user' }),+ breakdowns: [],+ ...base,+ })+ );+ });++ // 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.+ it('normalizes camelCase filter alias (referrerName → referrer_name)', async () => {+ const rendered = await getChartSql({+ event: event({+ filters: [{ name: 'referrerName', operator: 'is', value: ['email'] }],+ }),+ breakdowns: [],+ ...base,+ });+ expect(rendered.sql).toContain('referrer_name');+ expect(rendered.sql).not.toMatch(/(?<![._\w])referrerName/);+ await explain(rendered);+ });++ it('routes bare utm_source filter through properties map', async () => {+ const rendered = await getChartSql({+ event: event({+ filters: [{ name: 'utm_source', operator: 'is', value: ['awn'] }],+ }),+ breakdowns: [],+ ...base,+ });+ expect(rendered.sql).toContain("properties['__query.utm_source']");+ // The unqualified `utm_source = …` form would fail with UNKNOWN_IDENTIFIER.+ expect(rendered.sql).not.toMatch(/(?<![._\w])utm_source\s*=/);+ await explain(rendered);+ });++ it('drops unknown breakdown rather than emitting invalid identifier', async () => {+ // `temple_name` is a custom property (lives in properties map) but got+ // saved as a top-level breakdown. The old code path emitted+ // `SELECT temple_name as _uc_label_1 FROM events`, failing parse.+ const rendered = await getChartSql({+ event: event(),+ breakdowns: [breakdown('temple_name')],+ ...base,+ });+ expect(rendered.sql).not.toMatch(/(?<![._\w])temple_name/);+ expect(rendered.sql).not.toContain('_uc_label_1');+ await explain(rendered);+ });++ it('drops unknown filter rather than emitting invalid identifier', async () => {+ const rendered = await getChartSql({+ event: event({+ filters: [+ { name: 'totally_made_up_column', operator: 'is', value: ['x'] },+ ],+ }),+ breakdowns: [],+ ...base,+ });+ expect(rendered.sql).not.toMatch(/totally_made_up_column/);+ await explain(rendered);+ });++ it('binds the chart-level values as parameters, never as literals', async () => {+ const rendered = await getChartSql({+ event: event({ name: "it's a 'quoted' name" }),+ breakdowns: [],+ ...base,+ projectId: "proj'ect",+ });+ expect(rendered.sql).not.toContain("proj'ect");+ expect(rendered.sql).not.toContain("'quoted'");+ expect(Object.values(rendered.params)).toContain("proj'ect");+ expect(Object.values(rendered.params)).toContain("it's a 'quoted' name");+ await explain(rendered);+ });+});++describe('getAggregateChartSql', () => {+ it('drops all-cohorts breakdown on empty cohort project', async () => {+ cohortFindMany.mockClear();+ const rendered = await getAggregateChartSql({+ event: event({ segment: 'user' }),+ breakdowns: [breakdown('cohort')],+ ...base,+ });+ expect(cohortFindMany).toHaveBeenCalledWith(+ expect.objectContaining({ where: { projectId: PROJECT_ID } })+ );+ expect(rendered.sql).not.toMatch(/_uc_label_\d+\s*=\s*'Unknown'/);+ expect(rendered.sql).not.toContain('_all_cohorts');+ await explain(rendered);+ });++ it('properties + group breakdown is unambiguous', async () => {+ const rendered = await getAggregateChartSql({+ event: event({+ filters: [{ name: 'group.plan', operator: 'is', value: ['pro'] }],+ }),+ breakdowns: [breakdown('properties.__query.utm_source')],+ ...base,+ });+ expect(rendered.sql).toContain("e.properties['__query.utm_source']");+ await explain(rendered);+ });+});++describe('profile-property narrowing', () => {+ const profileFilter = {+ id: 'f1',+ name: 'profile.properties.plan',+ operator: 'is' as const,+ value: ['pro'],+ };++ it('projects only the referenced keys as scalar columns in the profile CTE', async () => {+ const { sql } = await getChartSql({+ event: event({ filters: [profileFilter] }),+ breakdowns: [breakdown('profile.properties.experiment')],+ ...base,+ });++ // The CTE selects one scalar column per referenced key...+ expect(sql).toContain("properties['plan'] as `profile.properties.plan`");+ expect(sql).toContain(+ "properties['experiment'] as `profile.properties.experiment`"+ );+ // ...instead of every profile's whole Map...+ expect(sql).not.toContain('properties as "profile.properties"');+ // ...and every ref in the query is rewritten to the scalar alias.+ expect(sql).not.toContain("profile.properties['plan']");+ expect(sql).not.toContain("profile.properties['experiment']");+ });++ it('falls back to the full Map for wildcard refs', async () => {+ const { sql } = await getChartSql({+ event: event({+ filters: [+ {+ id: 'f1',+ name: 'profile.properties.experiments.*.name',+ operator: 'is' as const,+ value: ['a'],+ },+ ],+ }),+ breakdowns: [],+ ...base,+ });++ // mapExtractKeyLike needs the whole Map, so it must stay selected.+ expect(sql).toContain('properties as "profile.properties"');+ });++ it('never narrows identifier-unsafe keys (backtick falls back to the Map)', async () => {+ const { sql } = await getChartSql({+ event: event({+ filters: [+ {+ id: 'f1',+ name: 'profile.properties.plan`tier',+ operator: 'is' as const,+ value: ['a'],+ },+ ],+ }),+ breakdowns: [],+ ...base,+ });++ // The unsafe key keeps its original Map access against the full Map —+ // it must never be embedded in a backtick-quoted alias.+ expect(sql).toContain('properties as "profile.properties"');+ expect(sql).not.toContain('as `profile.properties.plan`tier`');+ });++ it('collects the math-metric property too', async () => {+ const { sql } = await getChartSql({+ event: event({+ segment: 'property_average',+ property: 'profile.properties.age',+ filters: [profileFilter],+ }),+ breakdowns: [],+ ...base,+ });++ // The metric's key must be narrowed alongside the filter's — otherwise+ // the metric keeps reading the Map that narrowing just removed.+ expect(sql).toContain("properties['age'] as `profile.properties.age`");+ expect(sql).not.toContain("profile.properties['age']");+ });++ it('creates the profile join for a metric-only profile property', async () => {+ // No profile filter or breakdown — the metric alone must still create+ // the CTE and join, or its scalar alias resolves against nothing.+ const { sql } = await getChartSql({+ event: event({+ segment: 'property_average',+ property: 'profile.properties.age',+ filters: [],+ }),+ breakdowns: [],+ ...base,+ });++ expect(sql).toContain('LEFT ANY JOIN profile ON profile.id = profile_id');+ expect(sql).toContain("properties['age'] as `profile.properties.age`");+ });++ it('metric-only profile property parses and resolves', async () => {+ await explain(+ await getChartSql({+ event: event({+ segment: 'property_average',+ property: 'profile.properties.age',+ filters: [],+ }),+ breakdowns: [],+ ...base,+ })+ );+ });++ it('math metric on a narrowed profile property parses and resolves', async () => {+ await explain(+ await getChartSql({+ event: event({+ segment: 'property_average',+ property: 'profile.properties.age',+ filters: [profileFilter],+ }),+ breakdowns: [],+ ...base,+ })+ );+ });++ it('narrowed chart SQL parses and resolves', async () => {+ await explain(+ await getChartSql({+ event: event({ filters: [profileFilter] }),+ breakdowns: [breakdown('profile.properties.experiment')],+ ...base,+ })+ );+ });++ it('narrowed aggregate chart SQL parses and resolves', async () => {+ const rendered = await getAggregateChartSql({+ event: event({ filters: [profileFilter] }),+ breakdowns: [breakdown('profile.properties.experiment')],+ ...base,+ });+ expect(rendered.sql).not.toContain("profile.properties['plan']");+ await explain(rendered);+ });+});++describe('single-pass total_count', () => {+ it('scans events exactly once and merges uniq states globally (no breakdown)', async () => {+ const { sql } = await getChartSql({+ event: event(),+ breakdowns: [],+ ...base,+ });+ expect(sql).not.toContain('_uc AS (');+ expect(sql).toContain('uniqState(profile_id) as _uc_state');+ expect(sql).toContain('uniqMerge(_uc_state) OVER () as total_count');+ expect(sql).toContain('* EXCEPT (_uc_state)');+ // exactly one scan of the events table+ expect(sql.match(/FROM events e/g)).toHaveLength(1);+ });++ it('partitions the merged uniq states by breakdown labels', async () => {+ const { sql } = await getChartSql({+ event: event(),+ breakdowns: [breakdown('properties.experiment')],+ ...base,+ });+ expect(sql).not.toContain('_uc AS (');+ expect(sql).not.toContain('LEFT ANY JOIN _uc');+ expect(sql).toContain(+ 'uniqMerge(_uc_state) OVER (PARTITION BY label_1) as total_count'+ );+ expect(sql.match(/FROM events e/g)).toHaveLength(1);+ });++ it('keeps ORDER BY and WITH FILL outside the aggregation subquery', async () => {+ const { sql } = await getChartSql({+ event: event(),+ breakdowns: [],+ ...base,+ });+ // GROUP BY belongs to the inner scan; ORDER BY/FILL follow its closing paren+ expect(sql).toMatch(/GROUP BY[^)]*\)\s*ORDER BY date ASC/);+ expect(sql).toContain('WITH FILL');+ });++ it('single-pass chart SQL parses and resolves (no breakdown)', async () => {+ await explain(+ await getChartSql({ event: event(), breakdowns: [], ...base })+ );+ });++ it('single-pass chart SQL parses and resolves (breakdown + cohort + profile)', async () => {+ await explain(+ await getChartSql({+ event: event({+ filters: [+ {+ id: 'f1',+ name: 'profile.properties.plan',+ operator: 'is' as const,+ value: ['pro'],+ },+ ],+ }),+ breakdowns: [breakdown('properties.experiment')],+ ...base,+ })+ );+ });+});diff --git a/packages/core/src/modules/chart/src/chart.sql.ts b/packages/core/src/modules/chart/src/chart.sql.tsnew file mode 100644index 000000000..f0ed4a4cd--- /dev/null+++ b/packages/core/src/modules/chart/src/chart.sql.ts@@ -0,0 +1,796 @@+// Every ClickHouse query the chart module runs, as pure `sql` fragments+// (ADR-013). Converted 1:1 from packages/db/src/services/chart.service.ts+// (`getChartSql`, `getAggregateChartSql`) and packages/trpc/src/routers/+// chart.ts (M7-003): the SQL text is V1's, with every chart-level value —+// project id, event name, dates, timezone, cohort ids and names, limits —+// bound as a `{pN:Type}` parameter instead of an escaped literal. Each+// builder's result set was diffed against V1's on the local prod-copy; the+// statements, params, row counts and timings are in chart.sql.proof.md.+//+// The field resolver and filter compiler still render text (see compiled.ts);+// their output is spliced, everything else is bound. Dates bind as V1's own+// strings: a String param in a DateTime position is parsed exactly like the+// literal it replaces.+//+// Cluster note (docs/ENVIRONMENT.md): `events`, `profiles`, `groups` and+// `cohort_members` are Distributed on Cloud. The cohort CTEs, the profile,+// group and cohort LEFT ANY JOINs and any `IN (SELECT ...)` a filter compiles+// keep V1's exact shape and run under the client's+// `distributed_product_mode: 'allow'` as before.+//+// The keyed part records below are a local, function-scoped builder (ADR-013+// R5): V1 assigned and re-assigned `sb.select.count`, `sb.where.property`,+// ... by key, and a keyed record keeps that exact clause order — which is+// what makes the proof diff a pure literal→param diff.++import { type SqlFragment, sql } from '@openpanel/db/src/clickhouse/sql';+import type {+ IChartBreakdown,+ IChartEvent,+ IInterval,+} from '@openpanel/validation';+import { compiledText, compiledTextWithProfileRefs } from './compiled';+import { formatClickhouseDate } from './dates';+import {+ CHART_TABLE,+ type CohortMetadata,+ collectBreakdownCohortIds,+ collectProfileCteFields,+ collectProfilePropertyKeys,+ extractCohortId,+ getCohortAlias,+ getSelectPropertyKey,+ isAllCohortsBreakdown,+ isKnownEventField,+ isNumericColumn,+ profilePropertiesCteSelect,+} from './field-resolution';+import { getEventFiltersWhereClause } from './filter-where';++const EVENTS_ALIAS = 'e';+const ALL_COHORTS_ALIAS = '_all_cohorts';+const ALL_COHORTS_UNKNOWN_LABEL = 'Unknown';+// Cohort ids are Postgres uuids; they also name a CTE and a join alias, so+// anything else is refused rather than inlined (V1 inlined it unchecked).+const COHORT_ID_PATTERN = /^[A-Za-z0-9_-]+$/;++const MATH_FUNCTION_BY_SEGMENT: Record<string, string> = {+ property_sum: 'sum',+ property_average: 'avg',+ property_max: 'max',+ property_min: 'min',+};++export const PROPERTY_VALUES_LIMIT = 100_000;+/** `profiles` columns a `profile.<field>` reference may select (ADR-013 R3). */+const PROFILE_COLUMNS = [+ 'id',+ 'is_external',+ 'first_name',+ 'last_name',+ 'email',+ 'avatar',+ 'properties',+ 'project_id',+ 'groups',+ 'created_at',+ 'last_seen_at',+] as const;++export type ChartQueryEvent = Pick<+ IChartEvent,+ 'name' | 'segment' | 'filters' | 'property'+>;++/** Breakdowns after `resolveChartBreakdowns`, plus the cohort rows they need. */+export interface ResolvedChartBreakdowns {+ breakdowns: IChartBreakdown[];+ allCohorts: CohortMetadata[];+ cohortMetadata: ReadonlyMap<string, CohortMetadata>;+}++export type ChartSeriesQueryInput = ResolvedChartBreakdowns & {+ event: ChartQueryEvent;+ interval: IInterval;+ startDate: string;+ endDate: string;+ projectId: string;+ timezone: string;+};++export type AggregateChartQueryInput = ResolvedChartBreakdowns & {+ event: ChartQueryEvent;+ startDate: string;+ endDate: string;+ projectId: string;+ limit?: number;+};++type Parts = Record<string, SqlFragment>;++export class ChartCohortIdError extends Error {+ constructor(cohortId: string) {+ super(`Refusing to inline cohort id ${JSON.stringify(cohortId)}`);+ this.name = 'ChartCohortIdError';+ }+}++function assertCohortId(cohortId: string): string {+ if (!COHORT_ID_PATTERN.test(cohortId)) {+ throw new ChartCohortIdError(cohortId);+ }+ return cohortId;+}++// --- breakdown resolution ---------------------------------------------------++/**+ * Drop breakdowns whose field name doesn't resolve to a known events column,+ * properties path, profile path, group path, or cohort — saved reports with+ * fields like `temple_name` (a property, not a column) reached ClickHouse as+ * `SELECT temple_name ...`, failing parse.+ */+export function knownBreakdowns(+ breakdowns: IChartBreakdown[]+): IChartBreakdown[] {+ return breakdowns.filter((breakdown) => isKnownEventField(breakdown.name));+}++export function requestsAllCohortsBreakdown(+ breakdowns: IChartBreakdown[]+): boolean {+ return breakdowns.some((breakdown) => isAllCohortsBreakdown(breakdown.name));+}++/**+ * Drop the all-cohorts breakdown when the project has no cohorts: its label+ * collapses to the literal 'Unknown', which ClickHouse rejects as a join key+ * ("Cannot determine join keys").+ */+export function withoutEmptyAllCohortsBreakdown(+ breakdowns: IChartBreakdown[],+ allCohorts: CohortMetadata[]+): IChartBreakdown[] {+ if (allCohorts.length > 0) {+ return breakdowns;+ }+ return breakdowns.filter(+ (breakdown) => !isAllCohortsBreakdown(breakdown.name)+ );+}++// --- clause rendering -------------------------------------------------------++function clauses(parts: Parts): SqlFragment[] {+ return Object.values(parts);+}++function withClause(ctes: Parts): SqlFragment {+ const entries = Object.entries(ctes);+ if (entries.length === 0) {+ return sql.empty;+ }+ const cteClauses = entries.map(+ ([name, query]) => sql`${compiledText(name)} AS (${query})`+ );+ return sql`WITH ${sql.join(cteClauses, ', ')} `;+}++function selectClause(select: Parts): SqlFragment {+ return sql`SELECT ${sql.join(clauses(select), ', ')}`;+}++function joinsClause(joins: Parts): SqlFragment {+ return sql.join(clauses(joins), ' ');+}++function whereClause(where: Parts): SqlFragment {+ const conditions = clauses(where);+ if (conditions.length === 0) {+ return sql.empty;+ }+ return sql`WHERE ${sql.join(conditions, ' AND ')}`;+}++function groupByClause(groupBy: Parts): SqlFragment {+ const keys = clauses(groupBy);+ if (keys.length === 0) {+ return sql.empty;+ }+ return sql`GROUP BY ${sql.join(keys, ', ')}`;+}++function orderByClause(orderBy: Parts): SqlFragment {+ const keys = clauses(orderBy);+ if (keys.length === 0) {+ return sql.empty;+ }+ return sql`ORDER BY ${sql.join(keys, ', ')}`;+}++// --- shared chart body ------------------------------------------------------++function cohortMembershipCte(cohortId: string, projectId: string): SqlFragment {+ return sql`+ SELECT profile_id+ FROM ${sql.id(CHART_TABLE.cohortMembers)} FINAL+ WHERE cohort_id = ${sql.string(cohortId)}+ AND project_id = ${sql.string(projectId)}+ `;+}++function allCohortsMembershipCte(projectId: string): SqlFragment {+ return sql`+ SELECT profile_id, cohort_id+ FROM ${sql.id(CHART_TABLE.cohortMembers)} FINAL+ WHERE project_id = ${sql.string(projectId)}+ `;+}++function allCohortsLabelExpr(cohorts: CohortMetadata[]): SqlFragment {+ if (cohorts.length === 0) {+ return sql`${sql.string(ALL_COHORTS_UNKNOWN_LABEL)}`;+ }+ const ids = sql.array(+ 'String',+ cohorts.map((cohort) => cohort.id)+ );+ const names = sql.array(+ 'String',+ cohorts.map((cohort) => cohort.name)+ );+ return sql`transform(${sql.id(`${ALL_COHORTS_ALIAS}.cohort_id`)}, ${ids}, ${names}, ${sql.string(ALL_COHORTS_UNKNOWN_LABEL)})`;+}++function cohortBreakdownLabel(+ cohortId: string,+ cohortName: string | undefined+): SqlFragment {+ const alias = sql.id(+ `${getCohortAlias(assertCohortId(cohortId))}.profile_id`+ );+ const inLabel = sql.string(cohortName ?? 'In Cohort');+ const notInLabel = sql.string(+ cohortName ? `Not ${cohortName}` : 'Not In Cohort'+ );+ return sql`if(notEmpty(${alias}), ${inLabel}, ${notInLabel})`;+}++function groupsCte(projectId: string): SqlFragment {+ return sql`SELECT id, name, type, properties FROM ${sql.id(CHART_TABLE.groups)} FINAL WHERE project_id = ${sql.string(projectId)}`;+}++function profileCteSelectField(+ field: string,+ profileKeys: string[],+ needsFullMap: boolean+): string {+ if (field === 'id') {+ return 'id as "profile.id"';+ }+ if (field === 'properties') {+ return profilePropertiesCteSelect(profileKeys, needsFullMap);+ }+ return `${field} as "profile.${field}"`;+}++function profileCte(selectFields: string[], projectId: string): SqlFragment {+ return sql`SELECT ${compiledText(selectFields.join(', '))}+ FROM ${sql.id(CHART_TABLE.profiles)} FINAL+ WHERE project_id = ${sql.string(projectId)}`;+}++function anyRefOn(prefix: string, refs: { name: string }[]): boolean {+ return refs.some((ref) => ref.name.startsWith(prefix));+}++interface ChartBody {+ ctes: Parts;+ joins: Parts;+ where: Parts;+ select: Parts;+ profileKeys: string[];+ /** Compiled (text) expressions go through this so V1's profile-ref rewrite still applies. */+ spliced: (text: string) => SqlFragment;+}++/**+ * The CTEs, JOINs, WHERE and `label_0` both chart shapes share, in V1's+ * clause order: cohort CTEs/joins, filters + project + event name, group+ * ARRAY JOIN, profile CTE.+ */+function chartBody({+ event,+ breakdowns,+ allCohorts,+ projectId,+}: {+ event: ChartQueryEvent;+ breakdowns: IChartBreakdown[];+ allCohorts: CohortMetadata[];+ projectId: string;+}): ChartBody {+ const hasAllCohortsBreakdown =+ requestsAllCohortsBreakdown(breakdowns) && allCohorts.length > 0;+ const cohortIds = collectBreakdownCohortIds(breakdowns).map(assertCohortId);++ const metricRef = event.property ? [{ name: event.property }] : [];+ // Math metrics reference event.property too — missing it here would strip+ // the Map the metric still reads from.+ const profileProps = collectProfilePropertyKeys([+ ...event.filters,+ ...breakdowns,+ ...metricRef,+ ]);+ const spliced = (text: string) =>+ compiledTextWithProfileRefs(text, profileProps.keys);++ const ctes: Parts = {};+ const joins: Parts = {};++ if (hasAllCohortsBreakdown) {+ ctes[ALL_COHORTS_ALIAS] = allCohortsMembershipCte(projectId);+ joins[ALL_COHORTS_ALIAS] =+ sql`INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id`;+ }++ for (const cohortId of cohortIds) {+ const cteName = `\`cohort-${cohortId}\``;+ const alias = getCohortAlias(cohortId);+ ctes[cteName] = cohortMembershipCte(cohortId, projectId);+ joins[`cohort_${cohortId}`] =+ sql`LEFT ANY JOIN ${compiledText(cteName)} AS ${sql.id(alias)} ON ${sql.id(`${alias}.profile_id`)} = e.profile_id`;+ }++ const where: Parts = {};+ const compiledFilters = getEventFiltersWhereClause(+ event.filters,+ projectId,+ EVENTS_ALIAS+ );+ for (const [key, clause] of Object.entries(compiledFilters)) {+ where[key] = spliced(clause);+ }+ where.projectId = sql`project_id = ${sql.string(projectId)}`;++ const select: Parts = {};+ if (event.name !== '*') {+ select.label_0 = sql`${sql.string(event.name)} as label_0`;+ where.eventName = sql`e.name = ${sql.string(event.name)}`;+ } else {+ select.label_0 = sql`'*' as label_0`;+ }++ const profileRefs = [...event.filters, ...breakdowns, ...metricRef];+ const needsProfileJoin = anyRefOn('profile.', profileRefs);+ const needsGroupArrayJoin =+ anyRefOn('group.', profileRefs) || event.segment === 'group';++ if (needsGroupArrayJoin) {+ ctes._g = groupsCte(projectId);+ joins.groups = sql`ARRAY JOIN groups AS _group_id`;+ joins.groups_table = sql`LEFT ANY JOIN _g ON _g.id = _group_id`;+ }++ if (needsProfileJoin) {+ const selectFields = collectProfileCteFields(profileRefs).map((field) =>+ profileCteSelectField(field, profileProps.keys, profileProps.needsFullMap)+ );+ ctes.profile = profileCte(selectFields, projectId);+ joins.profiles = sql`LEFT ANY JOIN profile ON profile.id = profile_id`;+ }++ return {+ ctes,+ joins,+ where,+ select,+ profileKeys: profileProps.keys,+ spliced,+ };+}++function breakdownLabel(+ breakdown: IChartBreakdown,+ {+ allCohorts,+ cohortMetadata,+ projectId,+ spliced,+ }: Pick<+ ChartSeriesQueryInput,+ 'allCohorts' | 'cohortMetadata' | 'projectId'+ > &+ Pick<ChartBody, 'spliced'>+): SqlFragment {+ if (isAllCohortsBreakdown(breakdown.name)) {+ return allCohortsLabelExpr(allCohorts);+ }+ const cohortId = extractCohortId(breakdown.name);+ if (cohortId) {+ return cohortBreakdownLabel(cohortId, cohortMetadata.get(cohortId)?.name);+ }+ return spliced(+ getSelectPropertyKey(+ breakdown.name,+ projectId,+ undefined,+ undefined,+ EVENTS_ALIAS+ )+ );+}++function addBreakdownLabels(+ input: ChartSeriesQueryInput | AggregateChartQueryInput,+ body: ChartBody,+ groupBy: Parts+): void {+ // Breakdowns start at label_1 (label_0 is reserved for event name).+ input.breakdowns.forEach((breakdown, index) => {+ const key = `label_${index + 1}`;+ const label = breakdownLabel(breakdown, {+ ...input,+ spliced: body.spliced,+ });+ body.select[key] = sql`${label} as ${compiledText(key)}`;+ groupBy[key] = compiledText(key);+ });+}++function countExpression(+ event: ChartQueryEvent,+ body: ChartBody,+ /** V1's aggregate shape resolved the metric property against the project; the series shape did not. */+ metricProjectId: string | undefined+): SqlFragment {+ if (event.segment === 'user') {+ return sql`countDistinct(profile_id) as count`;+ }+ if (event.segment === 'session') {+ return sql`countDistinct(session_id) as count`;+ }+ if (event.segment === 'group') {+ return sql`countDistinct(_group_id) as count`;+ }+ if (event.segment === 'user_average') {+ return sql`COUNT(*)::float / COUNT(DISTINCT profile_id)::float as count`;+ }+ const mathFunction = MATH_FUNCTION_BY_SEGMENT[event.segment];+ if (mathFunction && event.property) {+ const propertyKey = body.spliced(+ getSelectPropertyKey(+ event.property,+ metricProjectId,+ undefined,+ undefined,+ EVENTS_ALIAS+ )+ );+ const aggregate = compiledText(mathFunction);+ if (isNumericColumn(event.property)) {+ body.where.property = sql`${propertyKey} IS NOT NULL`;+ return sql`${aggregate}(${propertyKey}) as count`;+ }+ body.where.property = sql`${propertyKey} IS NOT NULL AND notEmpty(${propertyKey})`;+ return sql`${aggregate}(toFloat64OrNull(${propertyKey})) as count`;+ }+ return sql`count(*) as count`;+}++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`;+}++function dateRangeWhere(+ where: Parts,+ startDate: string,+ endDate: string+): void {+ if (startDate) {+ where.startDate = sql`created_at >= toDateTime(${sql.string(formatClickhouseDate(startDate))})`;+ }+ if (endDate) {+ where.endDate = sql`created_at <= toDateTime(${sql.string(formatClickhouseDate(endDate))})`;+ }+}++// --- series chart -----------------------------------------------------------++function intervalBucket(+ interval: IInterval,+ timezone: string+): { select: SqlFragment; fill: (start: string, end: string) => SqlFragment } {+ const tz = sql.string(timezone);+ switch (interval) {+ case 'minute':+ return {+ select: sql`toStartOfMinute(created_at) as date`,+ fill: (start, end) =>+ sql`FROM toStartOfMinute(toDateTime(${sql.string(start)})) TO toStartOfMinute(toDateTime(${sql.string(end)})) STEP toIntervalMinute(1)`,+ };+ case 'hour':+ return {+ select: sql`toStartOfHour(created_at) as date`,+ fill: (start, end) =>+ sql`FROM toStartOfHour(toDateTime(${sql.string(start)})) TO toStartOfHour(toDateTime(${sql.string(end)})) STEP toIntervalHour(1)`,+ };+ case 'day':+ return {+ select: sql`toStartOfDay(created_at) as date`,+ fill: (start, end) =>+ sql`FROM toStartOfDay(toDateTime(${sql.string(start)})) TO toStartOfDay(toDateTime(${sql.string(end)})) STEP toIntervalDay(1)`,+ };+ case 'week':+ return {+ select: sql`toStartOfWeek(created_at, 1, ${tz}) as date`,+ fill: (start, end) =>+ sql`FROM toStartOfWeek(toDateTime(${sql.string(start)}), 1, ${tz}) TO toStartOfWeek(toDateTime(${sql.string(end)}), 1, ${tz}) STEP toIntervalWeek(1)`,+ };+ case 'month':+ return {+ select: sql`toStartOfMonth(created_at, ${tz}) as date`,+ fill: (start, end) =>+ sql`FROM toStartOfMonth(toDateTime(${sql.string(start)}), ${tz}) TO toStartOfMonth(toDateTime(${sql.string(end)}), ${tz}) STEP toIntervalMonth(1)`,+ };+ default:+ throw new Error(`Unsupported chart interval: ${String(interval)}`);+ }+}++/** V1 `getChartSql`: one series, bucketed by interval, with a windowed unique total. */+export function chartSeriesQuery(input: ChartSeriesQueryInput): SqlFragment {+ const { event, interval, startDate, endDate, timezone } = input;+ const body = chartBody(input);+ const groupBy: Parts = {};+ const orderBy: Parts = {};++ body.select.count = sql`count(*) as count`;+ const bucket = intervalBucket(interval, timezone);+ // ClickHouse rejects WITH FILL when TO < FROM, so only emit a fill clause+ // for a valid range. The SELECT bucket truncation is always safe.+ const hasValidFillRange =+ !!startDate && !!endDate && new Date(endDate) >= new Date(startDate);+ const fill = hasValidFillRange+ ? sql`WITH FILL ${bucket.fill(startDate, endDate)}`+ : sql.empty;+ body.select.date = bucket.select;+ groupBy.date = sql`date`;+ orderBy.date = sql`date ASC`;++ dateRangeWhere(body.where, startDate, endDate);+ addBreakdownLabels(input, body, groupBy);+ 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.+ const from = oneEventPerUserFrom(body);+ return sql`${withClause(body.ctes)}${selectClause(body.select)} FROM ${from} ${groupByClause(groupBy)} ${orderByClause(orderBy)} ${fill}`;+ }++ // Single-pass total_count: aggregate uniqState(profile_id) alongside the+ // series in the same scan, then merge the per-group states with a window+ // aggregate in an outer select — PARTITION BY the breakdown labels, or an+ // empty partition for the global total.+ body.select.uc_state = sql`uniqState(profile_id) as _uc_state`;+ const partition = input.breakdowns.map((_, index) =>+ compiledText(`label_${index + 1}`)+ );+ const totalCount =+ partition.length > 0+ ? sql`uniqMerge(_uc_state) OVER (PARTITION BY ${sql.join(partition, ', ')}) as total_count`+ : sql`uniqMerge(_uc_state) OVER () as total_count`;++ return sql`${withClause(body.ctes)}SELECT * EXCEPT (_uc_state), ${totalCount} FROM (${selectClause(body.select)} FROM ${sql.id(CHART_TABLE.events)} e ${joinsClause(body.joins)} ${whereClause(body.where)} ${groupByClause(groupBy)}) ${orderByClause(orderBy)} ${fill}`;+}++// --- aggregate chart --------------------------------------------------------++/** V1 `getAggregateChartSql`: one row per breakdown combination over the whole range. */+export function aggregateChartQuery(+ input: AggregateChartQueryInput+): SqlFragment {+ const { event, startDate, endDate, projectId, limit } = input;+ const body = chartBody(input);+ const groupBy: Parts = {};+ const orderBy: Parts = {};++ dateRangeWhere(body.where, startDate, endDate);+ // A constant date field: groupByLabels expects one, and the whole range+ // aggregates into it.+ body.select.date = sql`${sql.string(startDate)} as date`;+ addBreakdownLabels(input, body, groupBy);+ groupBy.label_0 = sql`label_0`;+ body.select.count = countExpression(event, body, projectId);++ 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.+ const from = oneEventPerUserFrom(body);+ return sql`${head} FROM ${from} ${whereClause(body.where)} ${groupByClause(groupBy)}`;+ }++ orderBy.count = sql`count DESC`;+ const limitClause = limit ? sql`LIMIT ${sql.uint64(limit)}` : sql.empty;+ return sql`${head} FROM ${sql.id(CHART_TABLE.events)} e ${joinsClause(body.joins)} ${whereClause(body.where)} ${groupByClause(groupBy)} ${orderByClause(orderBy)} ${limitClause}`;+}++// --- router queries ---------------------------------------------------------++/**+ * projectCard: three months of daily unique visitors + revenue, filled.+ * The timezone travels as `clickhouse_settings.session_timezone` (the caller+ * passes it) instead of V1's inline `SETTINGS` clause — same setting, same+ * query scope, and a bindable position.+ */+export function projectCardChartQuery(projectId: string): SqlFragment {+ return sql`SELECT+ uniqHLL12(profile_id) as value,+ toStartOfDay(created_at) as date,+ sum(revenue * sign) as revenue+ FROM ${sql.id(CHART_TABLE.sessions)}+ WHERE+ project_id = ${sql.string(projectId)} AND+ created_at >= now() - interval '3 month'+ GROUP BY date+ ORDER BY date ASC+ WITH FILL FROM toStartOfDay(now() - interval '3 month')+ TO toStartOfDay(now())+ STEP INTERVAL 1 day`;+}++/** projectCard: rolling 3-month / 1-month / 1-day unique visitors and revenue. */+export function projectCardMetricsQuery(projectId: string): SqlFragment {+ return sql`SELECT uniqHLL12(if(created_at >= (now() - toIntervalMonth(3)), profile_id, null)) AS months_3, uniqHLL12(if(created_at >= (now() - toIntervalMonth(6)) AND created_at < (now() - toIntervalMonth(3)), profile_id, null)) AS months_3_prev, uniqHLL12(if(created_at >= (now() - toIntervalMonth(1)), profile_id, null)) AS month, uniqHLL12(if(created_at >= (now() - toIntervalDay(1)), profile_id, null)) AS day, uniqHLL12(if(created_at >= (now() - toIntervalDay(2)) AND created_at < (now() - toIntervalDay(1)), profile_id, null)) AS day_prev, sum(revenue * sign) as revenue FROM ${sql.id(CHART_TABLE.sessions)} WHERE project_id = ${sql.string(projectId)} AND created_at >= (now() - toIntervalMonth(6))`;+}++export function eventNamesWithCountQuery(projectId: string): SqlFragment {+ return sql`SELECT name, count(name) as count FROM ${sql.id(CHART_TABLE.eventNamesMv)} WHERE project_id = ${sql.string(projectId)} GROUP BY name ORDER BY count DESC, name ASC`;+}++function optionalEventName(event: string | undefined): SqlFragment {+ return event && event !== '*'+ ? sql` AND name = ${sql.string(event)}`+ : sql.empty;+}++export function eventPropertyKeysQuery(+ projectId: string,+ event: string | undefined,+ limit: number+): SqlFragment {+ return sql`SELECT distinct property_key, max(created_at) as created_at FROM ${sql.id(CHART_TABLE.eventPropertyValuesMv)} WHERE project_id = ${sql.string(projectId)}${optionalEventName(event)} GROUP BY property_key ORDER BY created_at DESC, property_key ASC LIMIT ${sql.uint64(limit)}`;+}++export function eventPropertyValuesQuery(+ projectId: string,+ propertyKey: string,+ event: string | undefined,+ limit: number+): SqlFragment {+ return sql`SELECT distinct property_value, max(created_at) as created_at FROM ${sql.id(CHART_TABLE.eventPropertyValuesMv)} WHERE project_id = ${sql.string(projectId)} AND property_key = ${sql.string(propertyKey)}${optionalEventName(event)} GROUP BY property_value ORDER BY created_at DESC, property_value ASC LIMIT ${sql.uint64(limit)}`;+}++/** Distinct values of a `profile.*` field straight off the profiles table. */+export function profilePropertyValuesQuery(+ projectId: string,+ selectExpression: string+): SqlFragment {+ const expression = compiledText(selectExpression);+ return sql`SELECT distinct ${expression} as values FROM ${sql.id(CHART_TABLE.profiles)} FINAL WHERE project_id = ${sql.string(projectId)} AND ${expression} != '' AND ${expression} IS NOT NULL ORDER BY created_at DESC LIMIT ${sql.uint64(PROPERTY_VALUES_LIMIT)}`;+}++/** Distinct values of a `group.*` field straight off the groups table. */+export function groupPropertyValuesQuery(+ projectId: string,+ selectExpression: string+): SqlFragment {+ const expression = compiledText(selectExpression);+ return sql`SELECT distinct ${expression} as values FROM ${sql.id(CHART_TABLE.groups)} FINAL WHERE project_id = ${sql.string(projectId)} AND deleted = 0 AND ${expression} != '' AND ${expression} IS NOT NULL ORDER BY created_at DESC LIMIT ${sql.uint64(PROPERTY_VALUES_LIMIT)}`;+}++/** Distinct values of an events column / property over the last six months. */+export function eventFieldValuesQuery(+ projectId: string,+ selectExpression: string,+ event: string+): SqlFragment {+ // V1 only skipped the name clause for `*` — an empty event name filters on ''.+ const eventName =+ event !== '*' ? sql` AND name = ${sql.string(event)}` : sql.empty;+ return sql`SELECT distinct ${compiledText(selectExpression)} as values FROM ${sql.id(CHART_TABLE.events)} WHERE project_id = ${sql.string(projectId)} AND created_at > (now() - INTERVAL 6 MONTH)${eventName} ORDER BY created_at DESC LIMIT ${sql.uint64(PROPERTY_VALUES_LIMIT)}`;+}++// --- getProfiles ------------------------------------------------------------++export interface ChartBucketProfilesInput {+ projectId: string;+ /** Bucket start as V1's `YYYY-MM-DD HH:mm:ss` string. */+ bucketDate: string;+ interval: IInterval;+ event: Pick<IChartEvent, 'name' | 'filters'>;+ breakdowns: Record<string, string>;+}++function bucketEquals(interval: IInterval, bucketDate: string): SqlFragment {+ const date = sql.string(bucketDate);+ switch (interval) {+ case 'minute':+ return sql`toStartOfMinute(created_at) = toDateTime(${date})`;+ case 'hour':+ return sql`toStartOfHour(created_at) = toDateTime(${date})`;+ case 'day':+ return sql`toStartOfDay(created_at) = toDate(${date})`;+ case 'week':+ return sql`toStartOfWeek(toDateTime(created_at)) = toDate(${date})`;+ case 'month':+ return sql`toStartOfMonth(toDateTime(created_at)) = toDate(${date})`;+ default:+ throw new Error(`Unsupported chart interval: ${String(interval)}`);+ }+}++/** Top-level profile columns a `profile.*` reference reads, in first-seen order. */+function profileFieldsToSelect(refs: string[]): SqlFragment[] {+ const fields: string[] = [];+ for (const ref of refs) {+ const field = ref.replace('profile.', '').split('.')[0];+ if (field && !fields.includes(field)) {+ fields.push(field);+ }+ }+ // ADR-013 R3: V1 spliced these unchecked (trpc chart.ts:781).+ return fields.map((field) => sql.id(field, PROFILE_COLUMNS));+}++/** Distinct profile ids behind one chart data point (V1 `getProfiles`). */+export function chartBucketProfilesQuery(+ input: ChartBucketProfilesInput+): SqlFragment {+ const { projectId, event, interval, bucketDate, breakdowns } = input;+ const breakdownKeys = Object.keys(breakdowns);++ const where: Parts = {};+ for (const [key, clause] of Object.entries(+ getEventFiltersWhereClause(event.filters, projectId)+ )) {+ where[key] = compiledText(clause);+ }+ where.projectId = sql`project_id = ${sql.string(projectId)}`;+ where.dateRange = bucketEquals(interval, bucketDate);+ if (event.name !== '*') {+ where.eventName = sql`name = ${sql.string(event.name)}`;+ }++ const joins: Parts = {};+ const profileRefs = [+ ...event.filters.map((filter) => filter.name),+ ...breakdownKeys,+ ].filter((name) => name.startsWith('profile.'));+ if (profileRefs.length > 0) {+ const fields = sql.join(profileFieldsToSelect(profileRefs), ', ');+ joins.profiles = sql`LEFT ANY JOIN (SELECT id, ${fields} FROM ${sql.id(CHART_TABLE.profiles)} FINAL WHERE project_id = ${sql.string(projectId)}) as profile on profile.id = profile_id`;+ }++ const needsGroupJoin = [+ ...event.filters.map((filter) => filter.name),+ ...breakdownKeys,+ ].some((name) => name.startsWith('group.'));+ if (needsGroupJoin) {+ joins.groups = sql`ARRAY JOIN groups AS _group_id`;+ joins.groups_cte = sql`LEFT ANY JOIN (${groupsCte(projectId)}) AS _g ON _g.id = _group_id`;+ }++ for (const [key, value] of Object.entries(breakdowns)) {+ const propertyKey = compiledText(getSelectPropertyKey(key, projectId));+ where[`breakdown_${key}`] = sql`${propertyKey} = ${sql.string(value)}`;+ }++ return sql`SELECT DISTINCT profile_id FROM ${sql.id(CHART_TABLE.events)} e ${joinsClause(joins)} ${whereClause(where)}`;+}diff --git a/packages/core/src/modules/chart/src/compiled.ts b/packages/core/src/modules/chart/src/compiled.tsnew file mode 100644index 000000000..93b50d6ad--- /dev/null+++ b/packages/core/src/modules/chart/src/compiled.ts@@ -0,0 +1,30 @@+// The ONE place the chart module splices pre-compiled SQL text.+//+// field-resolution.ts and filter-where.ts are V1's field resolver and filter+// compiler, still rendering sqlstring-escaped text: ADR-013 leaves the shared+// compilers as they are ("two behaviours, not two builders") until funnel,+// conversion, sankey, retention and overview (M7-004/M7-005) stop splicing+// them into text builders. Every chart-level value is bound in chart.sql.ts;+// only the compilers' output crosses here, through the same `SqlFragment`+// constructor `sql.id` uses — one grep away from deletion, not a `sql.raw()`.++import { SqlFragment } from '@openpanel/db/src/clickhouse/sql';+import { rewriteProfilePropertyRefs } from './field-resolution';++export function compiledText(text: string): SqlFragment {+ return new SqlFragment([text], []);+}++/**+ * V1 ran `rewriteProfilePropertyRefs` over the finished query text. Bound+ * params never matched it (an escaped literal cannot contain `['`), so+ * applying it to each compiled piece before splicing is the same rewrite.+ */+export function compiledTextWithProfileRefs(+ text: string,+ profilePropertyKeys: readonly string[]+): SqlFragment {+ return compiledText(+ rewriteProfilePropertyRefs(text, profilePropertyKeys as string[])+ );+}diff --git a/packages/core/src/modules/chart/src/dates.ts b/packages/core/src/modules/chart/src/dates.tsnew file mode 100644index 000000000..bcfcf8cb5--- /dev/null+++ b/packages/core/src/modules/chart/src/dates.ts@@ -0,0 +1,18 @@+// @openpanel/db's clickhouse/client.ts owns these, but importing it constructs+// a client and a pino logger at import time (see insight.service.ts's header),+// and the fragment builders here must stay pure and synchronous. Same choice+// cohort.service.ts made for its table map.++const CLICKHOUSE_DATE_SUFFIX = /(\.\d{3})?Z+$/;++/** `YYYY-MM-DD HH:mm:ss` in UTC — what V1 sent as a date literal. */+export function formatClickhouseDate(date: Date | string): string {+ return new Date(date)+ .toISOString()+ .replace('T', ' ')+ .replace(CLICKHOUSE_DATE_SUFFIX, '');+}++export function convertClickhouseDateToJs(date: string): Date {+ return new Date(`${date.replace(' ', 'T')}Z`);+}diff --git a/packages/db/src/engine/compute.ts b/packages/core/src/modules/chart/src/engine/compute.tssimilarity index 95%rename from packages/db/src/engine/compute.tsrename to packages/core/src/modules/chart/src/engine/compute.tsindex 4fc53452f..7c6d524fa 100644--- a/packages/db/src/engine/compute.ts+++ b/packages/core/src/modules/chart/src/engine/compute.ts@@ -14,7 +14,7 @@ export function compute(type: 'event' | 'formula';id?: string;formula?: string;- }>,+ }>): ConcreteSeries[] {const results: ConcreteSeries[] = [...fetchedSeries];@@ -69,13 +69,13 @@ export function compute(});const sortedDates = Array.from(allDates).sort(- (a, b) => new Date(a).getTime() - new Date(b).getTime(),+ (a, b) => new Date(a).getTime() - new Date(b).getTime());// Calculate total_count for the formula using the same formula applied to input series' total_count values// total_count is constant across all dates for a breakdown group, so compute it onceconst totalCountScope: Record<string, number> = {};- definitions.slice(0, formulaIndex).forEach((depDef, depIndex) => {+ definitions.slice(0, formulaIndex).forEach((_depDef, depIndex) => {const readableId = alphabetIds[depIndex];if (!readableId) {return;@@ -86,7 +86,7 @@ export function compute(if (depSeries) {// Get total_count from any data point (it's the same for all dates)const totalCount = depSeries.data.find(- (d) => d.total_count != null,+ (d) => d.total_count != null)?.total_count;totalCountScope[readableId] = totalCount ?? 0;} else {@@ -96,11 +96,11 @@ export function compute(s.definitionIndex === depIndex &&'type' in s.definition &&s.definition.type === 'formula' &&- s.name.slice(1).join(':::') === breakdownSignature,+ s.name.slice(1).join(':::') === breakdownSignature);if (formulaSerie) {const totalCount = formulaSerie.data.find(- (d) => d.total_count != null,+ (d) => d.total_count != null)?.total_count;totalCountScope[readableId] = totalCount ?? 0;} else {@@ -112,7 +112,7 @@ export function compute(// Evaluate formula for total_countconst totalCountResult = evaluateFormula(formula.formula,- totalCountScope,+ totalCountScope);const formulaTotalCount =totalCountResult === undefined ? undefined : round(totalCountResult, 2);@@ -122,7 +122,7 @@ export function compute(const scope: Record<string, number> = {};// Build scope using alphabet IDs (A, B, C, etc.)- definitions.slice(0, formulaIndex).forEach((depDef, depIndex) => {+ definitions.slice(0, formulaIndex).forEach((_depDef, depIndex) => {const readableId = alphabetIds[depIndex];if (!readableId) {return;@@ -141,7 +141,7 @@ export function compute(s.definitionIndex === depIndex &&'type' in s.definition &&s.definition.type === 'formula' &&- s.name.slice(1).join(':::') === breakdownSignature,+ s.name.slice(1).join(':::') === breakdownSignature);if (formulaSerie) {const dataPoint = formulaSerie.data.find((d) => d.date === date);diff --git a/packages/core/src/modules/chart/src/engine/execute.ts b/packages/core/src/modules/chart/src/engine/execute.tsnew file mode 100644index 000000000..61084e465--- /dev/null+++ b/packages/core/src/modules/chart/src/engine/execute.ts@@ -0,0 +1,104 @@+import { getChartPrevStartEndDate } from '@openpanel/db/src/services/date.service';+import type { FinalChart, IReportInput } from '@openpanel/validation';+import {+ getOrganizationSubscriptionChartEndDate,+ getSettingsForProject,+} from '../../../organization/organization.service';+import { compute } from './compute';+import { fetch, fetchAggregate } from './fetch';+import { format } from './format';+import { type NormalizedInput, normalize } from './normalize';+import { plan } from './plan';+import type { ConcreteSeries } from './types';++/** Normalize, then clamp the end date to the organization's subscription window. */+async function normalizeWithinSubscription(+ input: IReportInput+): Promise<NormalizedInput> {+ const normalized = await normalize(input);+ const endDate = await getOrganizationSubscriptionChartEndDate(+ input.projectId,+ normalized.endDate+ );+ if (endDate) {+ normalized.endDate = endDate;+ }+ return normalized;+}++/** Time-series chart: normalize -> plan -> fetch -> compute -> format. */+export async function executeChart(input: IReportInput): Promise<FinalChart> {+ const normalized = await normalizeWithinSubscription(input);+ const executionPlan = await plan(normalized);+ const computedSeries = compute(+ await fetch(executionPlan),+ executionPlan.definitions+ );++ let previousSeries: ConcreteSeries[] | null = null;+ if (input.previous) {+ const previousPlan = await plan({+ ...normalized,+ ...getChartPrevStartEndDate({+ startDate: normalized.startDate,+ endDate: normalized.endDate,+ }),+ });+ previousSeries = compute(+ await fetch(previousPlan),+ previousPlan.definitions+ );+ }++ return format(+ computedSeries,+ executionPlan.definitions,+ executionPlan.definitions.length > 1,+ previousSeries,+ normalized.limit+ );+}++/** Bar/pie chart without a time axis: normalize -> fetch aggregate -> format. */+export async function executeAggregateChart(+ input: IReportInput+): Promise<FinalChart> {+ const normalized = await normalizeWithinSubscription(input);+ const { timezone } = await getSettingsForProject(normalized.projectId);+ const currentPeriod = {+ startDate: normalized.startDate,+ endDate: normalized.endDate,+ };+ const computedSeries = compute(+ await fetchAggregate(normalized, currentPeriod, timezone),+ normalized.series+ );++ let previousSeries: ConcreteSeries[] | null = null;+ if (input.previous) {+ previousSeries = compute(+ await fetchAggregate(+ normalized,+ getChartPrevStartEndDate(currentPeriod),+ timezone+ ),+ normalized.series+ );+ }++ return format(+ computedSeries,+ normalized.series,+ normalized.series.length > 1,+ previousSeries,+ normalized.limit+ );+}++export const ChartEngine = {+ execute: executeChart,+};++export const AggregateChartEngine = {+ execute: executeAggregateChart,+};diff --git a/packages/core/src/modules/chart/src/engine/fetch.ts b/packages/core/src/modules/chart/src/engine/fetch.tsnew file mode 100644index 000000000..ed304c799--- /dev/null+++ b/packages/core/src/modules/chart/src/engine/fetch.ts@@ -0,0 +1,239 @@+import type { ISerieDataItem } from '@openpanel/common';+import { groupByLabels } from '@openpanel/common';+import { alphabetIds } from '@openpanel/constants';+import type {+ IChartBreakdown,+ IChartEventFilter,+ IChartEventItem,+ IGetChartDataInput,+} from '@openpanel/validation';+import { getAggregateChartSql, getChartSql } from '../chart-statement';+import type { NormalizedInput } from './normalize';+import type { ConcreteSeries, Plan, SeriesDefinition } from './types';++type EventDefinition = IChartEventItem & { type: 'event' };++function loadChClient() {+ return import('@openpanel/db/src/clickhouse/client');+}++async function runChartQuery(+ statement: Awaited<ReturnType<typeof getChartSql>>,+ timezone: string+): Promise<ISerieDataItem[]> {+ const { chQuery } = await loadChClient();+ return chQuery<ISerieDataItem>(statement, { session_timezone: timezone });+}++function breakdownFilters(+ breakdowns: IChartBreakdown[],+ nameParts: string[]+): IChartEventFilter[] {+ const filters: IChartEventFilter[] = [];+ breakdowns.forEach((breakdown, index) => {+ const value = nameParts[index + 1];+ if (value) {+ filters.push({+ id: `breakdown-${index}`,+ name: breakdown.name,+ operator: 'is',+ value: [value],+ });+ }+ });+ return filters;+}++function breakdownValues(+ breakdowns: IChartBreakdown[],+ nameParts: string[]+): Record<string, string> {+ const values: Record<string, string> = {};+ breakdowns.forEach((breakdown, index) => {+ const value = nameParts[index + 1];+ if (value) {+ values[breakdown.name] = value;+ }+ });+ return values;+}++/**+ * One ConcreteSeries per label group. `name[0]` is the event name, `name[1+]`+ * the breakdown values the row was grouped on.+ */+function expandGroupedRows({+ rows,+ breakdowns,+ event,+ definition,+ definitionIndex,+ seriesId,+}: {+ rows: ISerieDataItem[];+ breakdowns: IChartBreakdown[];+ event: EventDefinition;+ definition: SeriesDefinition;+ definitionIndex: number;+ seriesId: (nameParts: string[]) => string;+}): ConcreteSeries[] {+ return groupByLabels(rows).map((grouped) => {+ const hasBreakdownParts = breakdowns.length > 0 && grouped.name.length > 1;+ const breakdownValue = hasBreakdownParts+ ? grouped.name.slice(1).join(' - ')+ : undefined;+ return {+ id: seriesId(grouped.name),+ definitionId:+ definition.id ??+ alphabetIds[definitionIndex] ??+ `series-${definitionIndex}`,+ definitionIndex,+ name: grouped.name,+ context: {+ event: event.name,+ filters: breakdownValue+ ? [...event.filters, ...breakdownFilters(breakdowns, grouped.name)]+ : [...event.filters],+ breakdownValue,+ breakdowns: hasBreakdownParts+ ? breakdownValues(breakdowns, grouped.name)+ : undefined,+ },+ data: grouped.data.map((item) => ({+ date: item.date,+ count: item.count,+ total_count: item.total_count,+ })),+ definition,+ };+ });+}++function queryEvent(event: EventDefinition): IGetChartDataInput['event'] {+ return {+ id: event.id,+ name: event.name,+ segment: event.segment,+ filters: event.filters,+ displayName: event.displayName,+ property: event.property,+ };+}++/** Fetch every event series of the plan; breakdown expansion included. */+export async function fetch(plan: Plan): Promise<ConcreteSeries[]> {+ const results: ConcreteSeries[] = [];++ for (const [index, definition] of plan.definitions.entries()) {+ if (definition.type !== 'event') {+ continue;+ }+ const event = definition as EventDefinition;+ const placeholder = plan.concreteSeries.find(+ (series) => series.definitionId === definition.id+ );+ if (!placeholder) {+ continue;+ }++ const queryInput: IGetChartDataInput = {+ event: queryEvent(event),+ projectId: plan.input.projectId,+ startDate: plan.input.startDate,+ endDate: plan.input.endDate,+ breakdowns: plan.input.breakdowns,+ interval: plan.input.interval,+ chartType: plan.input.chartType,+ metric: plan.input.metric,+ previous: plan.input.previous ?? false,+ limit: plan.input.limit,+ offset: plan.input.offset,+ };++ let rows = await runChartQuery(+ await getChartSql({ ...queryInput, timezone: plan.timezone }),+ plan.timezone+ );+ // Nothing matched the breakdown: fall back to the plain series.+ if (rows.length === 0 && plan.input.breakdowns.length > 0) {+ rows = await runChartQuery(+ await getChartSql({+ ...queryInput,+ breakdowns: [],+ timezone: plan.timezone,+ }),+ plan.timezone+ );+ }++ results.push(+ ...expandGroupedRows({+ rows,+ breakdowns: plan.input.breakdowns,+ event,+ definition,+ definitionIndex: index,+ seriesId: (nameParts) => `${placeholder.id}-${nameParts.join('-')}`,+ })+ );+ }++ return results;+}++/**+ * Aggregate (bar/pie) counterpart of `fetch`: one row per label group with a+ * constant date, for the given period.+ */+export async function fetchAggregate(+ normalized: NormalizedInput,+ period: { startDate: string; endDate: string },+ timezone: string+): Promise<ConcreteSeries[]> {+ const results: ConcreteSeries[] = [];++ for (const [index, definition] of normalized.series.entries()) {+ if (definition.type !== 'event') {+ continue;+ }+ const event = definition as EventDefinition;++ const queryInput = {+ event: queryEvent(event),+ projectId: normalized.projectId,+ startDate: period.startDate,+ endDate: period.endDate,+ breakdowns: normalized.breakdowns,+ limit: normalized.limit,+ metric: normalized.metric,+ previous: normalized.previous,+ timezone,+ };++ let rows = await runChartQuery(+ await getAggregateChartSql(queryInput),+ timezone+ );+ if (rows.length === 0 && normalized.breakdowns.length > 0) {+ rows = await runChartQuery(+ await getAggregateChartSql({ ...queryInput, breakdowns: [] }),+ timezone+ );+ }++ results.push(+ ...expandGroupedRows({+ rows,+ breakdowns: normalized.breakdowns,+ event,+ definition,+ definitionIndex: index,+ seriesId: (nameParts) =>+ `${event.name}-${nameParts.join('-')}-${index}`,+ })+ );+ }++ return results;+}diff --git a/packages/db/src/engine/format.ts b/packages/core/src/modules/chart/src/engine/format.tssimilarity index 98%rename from packages/db/src/engine/format.tsrename to packages/core/src/modules/chart/src/engine/format.tsindex d569b06ab..430919721 100644--- a/packages/db/src/engine/format.ts+++ b/packages/core/src/modules/chart/src/engine/format.ts@@ -38,7 +38,9 @@ export function format(continue;}for (const alphaId of definition.hideSeries) {- const index = alphabetIds.indexOf(alphaId as (typeof alphabetIds)[number]);+ const index = alphabetIds.indexOf(+ alphaId as (typeof alphabetIds)[number]+ );if (index >= 0) {hiddenDefinitionIndices.add(index);}diff --git a/packages/db/src/engine/formula.test.ts b/packages/core/src/modules/chart/src/engine/formula.test.tssimilarity index 98%rename from packages/db/src/engine/formula.test.tsrename to packages/core/src/modules/chart/src/engine/formula.test.tsindex 5a11c413d..3d2bcc9eb 100644--- a/packages/db/src/engine/formula.test.ts+++ b/packages/core/src/modules/chart/src/engine/formula.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it } from 'vitest';+import { describe, expect, it } from 'bun:test';import { evaluateFormula, isValidFormula } from './formula';const scope = { A: 10, B: 4, C: 0 };diff --git a/packages/db/src/engine/formula.ts b/packages/core/src/modules/chart/src/engine/formula.tssimilarity index 97%rename from packages/db/src/engine/formula.tsrename to packages/core/src/modules/chart/src/engine/formula.tsindex e56a86a9f..55eff6c1c 100644--- a/packages/db/src/engine/formula.ts+++ b/packages/core/src/modules/chart/src/engine/formula.ts@@ -59,7 +59,9 @@ function assertAllowedNode(node: mathjs.MathNode): void {case 'ConstantNode': {const { value } = node as mathjs.ConstantNode;if (typeof value !== 'number' || !Number.isFinite(value)) {- throw new InvalidFormulaError('only finite numeric constants are allowed');+ throw new InvalidFormulaError(+ 'only finite numeric constants are allowed'+ );}break;}@@ -98,7 +100,6 @@ function assertAllowedNode(node: mathjs.MathNode): void {default:throw new InvalidFormulaError(`${node.type} is not allowed`);}-}/**@@ -173,7 +174,7 @@ export function isValidFormula(formula: string): boolean {*/export function evaluateFormula(formula: string,- scope: Record<string, number>,+ scope: Record<string, number>): number | undefined {let result: unknown;try {diff --git a/packages/core/src/modules/chart/src/engine/normalize.ts b/packages/core/src/modules/chart/src/engine/normalize.tsnew file mode 100644index 000000000..f20dfe92a--- /dev/null+++ b/packages/core/src/modules/chart/src/engine/normalize.ts@@ -0,0 +1,73 @@+import { alphabetIds } from '@openpanel/constants';+import { getChartStartEndDate } from '@openpanel/db/src/services/date.service';+import type {+ IChartEvent,+ IReportInput,+ IReportInputWithDates,+} from '@openpanel/validation';+import { getSettingsForProject } from '../../../organization/organization.service';+import type { SeriesDefinition } from './types';++export type NormalizedInput = IReportInputWithDates & {+ series: SeriesDefinition[];+};++// `mergeGlobalFilters` belongs to the report module (its own task); until it+// moves, reach V1's copy lazily — reports.service imports prisma at load.+function loadGlobalFilterMerge() {+ return import('@openpanel/db/src/services/reports.service').then(+ (m) => m.mergeGlobalFilters+ );+}++type LegacySeriesItem = Partial<IChartEvent> & { type?: string };++function toSeriesDefinition(+ item: LegacySeriesItem,+ index: number+): SeriesDefinition {+ const fallbackId = alphabetIds[index] ?? `series-${index}`;+ if (item && typeof item === 'object' && 'type' in item) {+ return { ...item, id: item.id ?? fallbackId } as SeriesDefinition;+ }+ // Pre-`type` payloads are events.+ return {+ type: 'event',+ id: item.id ?? fallbackId,+ name: item.name || 'unknown_event',+ segment: item.segment ?? 'event',+ filters: item.filters ?? [],+ displayName: item.displayName,+ property: item.property,+ } as SeriesDefinition;+}++/** Resolve the date range in the project's timezone and normalize every series item. */+export async function normalize(input: IReportInput): Promise<NormalizedInput> {+ const { timezone } = await getSettingsForProject(input.projectId);+ const { startDate, endDate } = getChartStartEndDate(+ {+ range: input.range,+ startDate: input.startDate ?? undefined,+ endDate: input.endDate ?? undefined,+ },+ timezone+ );++ // The schema already maps `events` to `series`; older callers may still send `events`.+ const rawSeries: LegacySeriesItem[] =+ (input as { series?: LegacySeriesItem[] }).series ??+ (input as { events?: LegacySeriesItem[] }).events ??+ [];+ const mergeGlobalFilters = await loadGlobalFilterMerge();++ return {+ ...input,+ series: mergeGlobalFilters(+ rawSeries.map(toSeriesDefinition),+ input.globalFilters+ ),+ startDate,+ endDate,+ };+}diff --git a/packages/core/src/modules/chart/src/engine/plan.ts b/packages/core/src/modules/chart/src/engine/plan.tsnew file mode 100644index 000000000..32dd240a8--- /dev/null+++ b/packages/core/src/modules/chart/src/engine/plan.ts@@ -0,0 +1,41 @@+import { slug } from '@openpanel/common';+import { alphabetIds } from '@openpanel/constants';+import type { IChartEventItem } from '@openpanel/validation';+import { getSettingsForProject } from '../../../organization/organization.service';+import type { NormalizedInput } from './normalize';+import type { ConcreteSeries, Plan } from './types';++/**+ * One placeholder per event definition; breakdown expansion happens in fetch.+ * Formulas are derived from event series, so they only exist after compute.+ */+export async function plan(normalized: NormalizedInput): Promise<Plan> {+ const { timezone } = await getSettingsForProject(normalized.projectId);++ const concreteSeries: ConcreteSeries[] = [];+ normalized.series.forEach((definition, index) => {+ if (definition.type !== 'event') {+ return;+ }+ const event = definition as IChartEventItem & { type: 'event' };+ concreteSeries.push({+ id: `${slug(event.name)}-${event.id ?? index}`,+ definitionId: event.id ?? alphabetIds[index] ?? `series-${index}`,+ definitionIndex: index,+ name: [event.displayName || event.name],+ context: {+ event: event.name,+ filters: [...event.filters],+ },+ data: [],+ definition,+ });+ });++ return {+ concreteSeries,+ definitions: normalized.series,+ input: normalized,+ timezone,+ };+}diff --git a/packages/core/src/modules/chart/src/engine/types.ts b/packages/core/src/modules/chart/src/engine/types.tsnew file mode 100644index 000000000..c5d4fe114--- /dev/null+++ b/packages/core/src/modules/chart/src/engine/types.ts@@ -0,0 +1,45 @@+import type {+ IChartEventFilter,+ IChartEventItem,+ IReportInputWithDates,+} from '@openpanel/validation';++/** What the user asked for: one event or formula series from the report. */+export type SeriesDefinition = IChartEventItem;++/**+ * One line/bar on the chart. With breakdowns a single SeriesDefinition+ * expands into several ConcreteSeries.+ */+export interface ConcreteSeries {+ id: string;+ /** Id of the SeriesDefinition this came from. */+ definitionId: string;+ /** Index in the report's series array — what formulas reference as A, B, C. */+ definitionIndex: number;+ /** Display name parts: ["Session Start", "Chrome"] or ["Formula 1"]. */+ name: string[];+ /** Everything a drill-down ("who are these users?") needs. */+ context: {+ event?: string;+ /** All filters including the breakdown values. */+ filters: IChartEventFilter[];+ /** @deprecated use `breakdowns` */+ breakdownValue?: string;+ /** `{ country: 'SE', path: '/pricing' }` */+ breakdowns?: Record<string, string>;+ };+ data: Array<{+ date: string;+ count: number;+ total_count?: number;+ }>;+ definition: SeriesDefinition;+}++export interface Plan {+ concreteSeries: ConcreteSeries[];+ definitions: SeriesDefinition[];+ input: IReportInputWithDates;+ timezone: string;+}diff --git a/packages/db/src/services/chart-field-resolution.test.ts b/packages/core/src/modules/chart/src/field-resolution.test.tssimilarity index 92%rename from packages/db/src/services/chart-field-resolution.test.tsrename to packages/core/src/modules/chart/src/field-resolution.test.tsindex dfd98e274..6d696dd72 100644--- a/packages/db/src/services/chart-field-resolution.test.ts+++ b/packages/core/src/modules/chart/src/field-resolution.test.ts@@ -14,12 +14,11 @@* were a top-level column.** All five errors logged in HyperDX 2026-05-14 → 2026-05-17 are covered here.+ *+ * Ported from packages/db/src/services/chart-field-resolution.test.ts (M7-003).*/-import { describe, expect, it } from 'vitest';-import {- isKnownEventField,- normalizeEventField,-} from './chart.service';+import { describe, expect, it } from 'bun:test';+import { isKnownEventField, normalizeEventField } from './field-resolution';describe('normalizeEventField', () => {it('rewrites camelCase aliases to their snake_case columns', () => {@@ -33,13 +32,13 @@ describe('normalizeEventField', () => {it('routes bare utm_* names into the properties.__query.* form', () => {expect(normalizeEventField('utm_source')).toBe(- 'properties.__query.utm_source',+ 'properties.__query.utm_source');expect(normalizeEventField('utm_medium')).toBe(- 'properties.__query.utm_medium',+ 'properties.__query.utm_medium');expect(normalizeEventField('utm_campaign')).toBe(- 'properties.__query.utm_campaign',+ 'properties.__query.utm_campaign');});diff --git a/packages/core/src/modules/chart/src/field-resolution.ts b/packages/core/src/modules/chart/src/field-resolution.tsnew file mode 100644index 000000000..beb8424cb--- /dev/null+++ b/packages/core/src/modules/chart/src/field-resolution.ts@@ -0,0 +1,487 @@+/** biome-ignore-all lint/style/useDefaultSwitchClause: switch cases are exhaustive by design */+// V1's chart field resolution, moved verbatim from+// packages/db/src/services/chart.service.ts (M7-003): how a report's field+// name (`referrerName`, `utm_source`, `properties.x.*`, `profile.email`,+// `group.name`, `cohort:<id>`, `has_profile`) becomes a ClickHouse+// expression, plus the profile-CTE narrowing helpers.+//+// These render TEXT with sqlstring on purpose. They are the SELECT/JOIN half+// of the filter compiler that funnel, conversion, sankey, retention and+// overview (M7-004/M7-005) still splice into their own builders, and ADR-013+// leaves the shared compilers as they are until their last consumer converts+// ("two behaviours, not two builders"). chart.sql.ts splices their output+// through compiled.ts — the one bridge — and binds every chart-level value+// (project id, event name, dates, timezone, cohort ids/names, limits) itself.++import type { IChartBreakdown } from '@openpanel/validation';+import sqlstring from 'sqlstring';++export const CHART_TABLE = {+ events: 'events',+ profiles: 'profiles',+ groups: 'groups',+ sessions: 'sessions',+ cohortMembers: 'cohort_members',+ eventNamesMv: 'distinct_event_names_mv',+ eventPropertyValuesMv: 'event_property_values_mv',+} as const;++// Top-level columns on the events table. Derived from the migration in+// packages/db/code-migrations/3-init-ch.ts (+ revenue added in 6-add-revenue-+// column.ts). Used to distinguish real columns from property keys and reject+// unknown identifiers before they reach ClickHouse.+export const EVENT_TOP_LEVEL_COLUMNS = new Set<string>([+ 'id',+ 'name',+ 'sdk_name',+ 'sdk_version',+ 'device_id',+ 'profile_id',+ 'project_id',+ 'session_id',+ 'path',+ 'origin',+ 'referrer',+ 'referrer_name',+ 'referrer_type',+ 'duration',+ 'revenue',+ 'created_at',+ 'country',+ 'city',+ 'region',+ 'longitude',+ 'latitude',+ 'os',+ 'os_version',+ 'browser',+ 'browser_version',+ 'device',+ 'brand',+ 'model',+ 'imported_at',+]);++// Older clients / saved reports send some field names in camelCase. Map them+// to the canonical snake_case ClickHouse column so they don't fall through as+// unknown identifiers. The bare values (`utm_source` etc.) actually live in+// the `properties` map — `normalizeEventField` rewrites those into the+// `properties.__query.utm_*` form.+export const EVENT_FIELD_ALIASES: Record<string, string> = {+ referrerName: 'referrer_name',+ referrerType: 'referrer_type',+ sessionId: 'session_id',+ deviceId: 'device_id',+ profileId: 'profile_id',+ projectId: 'project_id',+ osVersion: 'os_version',+ browserVersion: 'browser_version',+ sdkName: 'sdk_name',+ sdkVersion: 'sdk_version',+ createdAt: 'created_at',+ importedAt: 'imported_at',+};++const EVENT_UTM_BARE_COLUMNS = new Set<string>([+ 'utm_source',+ 'utm_medium',+ 'utm_campaign',+ 'utm_term',+ 'utm_content',+]);++const NUMERIC_EVENT_COLUMNS = ['duration', 'revenue', 'longitude', 'latitude'];++const PROFILE_CTE_SCALAR_FIELDS = [+ 'email',+ 'first_name',+ 'last_name',+ 'created_at',+ 'last_seen_at',+];++// Normalize an incoming field name into its canonical form. Returns a string+// suitable for `getSelectPropertyKey` / `getEventFiltersWhereClause` — i.e.+// either a top-level column name (`referrer_name`), a `properties.foo` /+// `profile.foo` / `group.foo` path, or `has_profile`. Unknown names are+// returned unchanged; callers must guard with `isKnownEventField` before+// inlining into SQL.+export function normalizeEventField(name: string): string {+ if (EVENT_FIELD_ALIASES[name]) {+ return EVENT_FIELD_ALIASES[name]!;+ }+ if (EVENT_UTM_BARE_COLUMNS.has(name)) {+ return `properties.__query.${name}`;+ }+ return name;+}++// Returns true if `name` resolves to something we can inline into SQL safely:+// a known top-level events column, a properties / profile / group path, a+// cohort breakdown, or `has_profile`. Used to drop unknown filters/breakdowns+// instead of emitting invalid `SELECT cohort` / `SELECT temple_name` queries.+export function isKnownEventField(name: string): boolean {+ if (name === 'has_profile') {+ return true;+ }+ if (isAllCohortsBreakdown(name)) {+ return true;+ }+ if (extractCohortId(name)) {+ return true;+ }+ if (name.startsWith('properties.')) {+ return true;+ }+ if (name.startsWith('profile.')) {+ return true;+ }+ if (name.startsWith('group.')) {+ return true;+ }+ const normalized = normalizeEventField(name);+ if (normalized.startsWith('properties.')) {+ return true;+ }+ if (EVENT_TOP_LEVEL_COLUMNS.has(normalized)) {+ return true;+ }+ return false;+}++export function isNumericColumn(columnName: string): boolean {+ return NUMERIC_EVENT_COLUMNS.includes(columnName);+}++export interface CohortMetadata {+ id: string;+ name: string;+}++export function getCohortCteName(cohortId: string): string {+ return `\`cohort-${cohortId}\``;+}++export function getCohortAlias(cohortId: string): string {+ return `cohort_${cohortId.replace(/-/g, '_')}`;+}++export function buildCohortMembershipQuery(+ cohortId: string,+ projectId: string+): string {+ return `+ SELECT profile_id+ FROM ${CHART_TABLE.cohortMembers} FINAL+ WHERE cohort_id = ${sqlstring.escape(cohortId)}+ AND project_id = ${sqlstring.escape(projectId)}+ `;+}++export function buildInlineCohortJoin(+ cohortId: string,+ projectId: string,+ tableAlias: string+): string {+ const cohortAlias = getCohortAlias(cohortId);+ const cohortQuery = buildCohortMembershipQuery(cohortId, projectId);+ return `LEFT ANY JOIN (${cohortQuery}) AS ${cohortAlias} ON ${cohortAlias}.profile_id = ${tableAlias}.profile_id`;+}++export function extractCohortId(breakdownName: string): string | null {+ if (breakdownName.startsWith('cohort:')) {+ return breakdownName.split(':')[1] ?? null;+ }+ return null;+}++export function isAllCohortsBreakdown(breakdownName: string): boolean {+ return breakdownName === 'cohort';+}++export function buildAllCohortsMembershipQuery(projectId: string): string {+ return `+ SELECT profile_id, cohort_id+ FROM ${CHART_TABLE.cohortMembers} FINAL+ WHERE project_id = ${sqlstring.escape(projectId)}+ `;+}++export function buildAllCohortsLabelExpr(+ cohorts: CohortMetadata[],+ alias = '_all_cohorts'+): string {+ if (cohorts.length === 0) {+ return "'Unknown'";+ }+ const ids = cohorts.map((c) => sqlstring.escape(c.id)).join(', ');+ const names = cohorts.map((c) => sqlstring.escape(c.name)).join(', ');+ return `transform(${alias}.cohort_id, [${ids}], [${names}], 'Unknown')`;+}++/**+ * Cohort IDs that need a `cohort_<id>` JOIN alias to be wired up by the+ * caller. After filter SQL became self-contained, only cohort *breakdowns*+ * require the JOIN — they reference `cohort_<id>.profile_id` in their+ * SELECT expression via `getSelectPropertyKey`.+ */+export function collectBreakdownCohortIds(+ breakdowns: IChartBreakdown[]+): string[] {+ const ids = new Set<string>();+ for (const breakdown of breakdowns) {+ const id = extractCohortId(breakdown.name);+ if (id) {+ ids.add(id);+ }+ }+ return Array.from(ids);+}++export function transformPropertyKey(property: string) {+ const propertyPatterns = ['properties', 'profile.properties'];+ const match = propertyPatterns.find((pattern) =>+ property.startsWith(`${pattern}.`)+ );++ if (!match) {+ return property;+ }++ if (property.includes('*')) {+ return property+ .replace(/^properties\./, '')+ .replace('.*.', '.%.')+ .replace(/\[\*\]$/, '.%')+ .replace(/\[\*\].?/, '.%.');+ }++ return `${match}['${property.replace(new RegExp(`^${match}.`), '')}']`;+}++// Returns a SQL expression for a group property via the _g JOIN alias+// property format: "group.name", "group.type", "group.properties.plan"+export function getGroupPropertySql(property: string): string {+ const withoutPrefix = property.replace(/^group\./, '');+ if (withoutPrefix === 'name') {+ return '_g.name';+ }+ if (withoutPrefix === 'type') {+ return '_g.type';+ }+ if (withoutPrefix.startsWith('properties.')) {+ const propKey = withoutPrefix.replace(/^properties\./, '');+ return `_g.properties[${sqlstring.escape(propKey)}]`;+ }+ return '_group_id';+}++// Returns the SELECT expression when querying the groups table directly (no join alias).+// Use for fetching distinct values for group.* properties.+export function getGroupPropertySelect(property: string): string {+ const withoutPrefix = property.replace(/^group\./, '');+ if (withoutPrefix === 'name') {+ return 'name';+ }+ if (withoutPrefix === 'type') {+ return 'type';+ }+ if (withoutPrefix === 'id') {+ return 'id';+ }+ if (withoutPrefix.startsWith('properties.')) {+ const propKey = withoutPrefix.replace(/^properties\./, '');+ return `properties[${sqlstring.escape(propKey)}]`;+ }+ return 'id';+}++// Returns the SELECT expression when querying the profiles table directly (no join alias).+// Use for fetching distinct values for profile.* properties.+// Lists the same profiles columns as PROFILE_COLUMNS in filter-where.service.ts,+// which resolves profile.* on the filter side; keep the two in sync.+export function getProfilePropertySelect(property: string): string {+ const withoutPrefix = property.replace(/^profile\./, '');+ if (withoutPrefix === 'id') {+ return 'id';+ }+ if (withoutPrefix === 'first_name') {+ return 'first_name';+ }+ if (withoutPrefix === 'last_name') {+ return 'last_name';+ }+ if (withoutPrefix === 'email') {+ return 'email';+ }+ if (withoutPrefix === 'avatar') {+ return 'avatar';+ }+ if (withoutPrefix === 'created_at') {+ return 'created_at';+ }+ if (withoutPrefix === 'last_seen_at') {+ return 'last_seen_at';+ }+ if (withoutPrefix.startsWith('properties.')) {+ const propKey = withoutPrefix.replace(/^properties\./, '');+ return `properties[${sqlstring.escape(propKey)}]`;+ }+ return 'id';+}++export function getSelectPropertyKey(+ rawProperty: string,+ projectId?: string,+ cohortId?: string,+ cohortName?: string,+ /**+ * When set, the events table's `properties` map is qualified with this+ * alias (e.g. `e.properties[...]`). Required in any query where another+ * joined table also exposes a `properties` column (such as the groups+ * `_g` join), otherwise ClickHouse rejects with "ambiguous identifier".+ */+ eventsAlias?: string+) {+ // Map camelCase aliases (`referrerName` → `referrer_name`) and bare UTM+ // names (`utm_source` → `properties.__query.utm_source`) into their+ // canonical form before doing any pattern matching. The fallback at the+ // bottom of this function returns `property` verbatim, so without this+ // normalization an alias would leak into the generated SQL and fail with+ // UNKNOWN_IDENTIFIER.+ const property = normalizeEventField(rawProperty);+ const extractedCohortId = cohortId || extractCohortId(property);++ if (extractedCohortId && projectId) {+ const cohortAlias = getCohortAlias(extractedCohortId);+ const inLabel = cohortName ? sqlstring.escape(cohortName) : "'In Cohort'";+ const notInLabel = cohortName+ ? sqlstring.escape(`Not ${cohortName}`)+ : "'Not In Cohort'";+ return `if(notEmpty(${cohortAlias}.profile_id), ${inLabel}, ${notInLabel})`;+ }++ if (property === 'has_profile') {+ return "if(profile_id != device_id, 'true', 'false')";+ }++ // Handle group properties — requires ARRAY JOIN + _g JOIN to be present in query+ if (property.startsWith('group.') && projectId) {+ return getGroupPropertySql(property);+ }++ const propertyPatterns = ['properties', 'profile.properties'];++ const match = propertyPatterns.find((pattern) =>+ property.startsWith(`${pattern}.`)+ );+ if (!match) {+ return property;+ }++ // Only the events table's bare `properties` map needs aliasing —+ // `profile.properties` already routes through the profile join alias.+ const aliasPrefix =+ match === 'properties' && eventsAlias ? `${eventsAlias}.` : '';++ if (property.includes('*')) {+ return `arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(${aliasPrefix}${match}, ${sqlstring.escape(+ transformPropertyKey(property)+ )})))`;+ }++ return `${aliasPrefix}${match}['${property.replace(new RegExp(`^${match}.`), '')}']`;+}++// --- profile-property CTE narrowing (perf) ---------------------------------+// profile.properties.<key> refs render as Map lookups `profile.properties['<key>']`.+// Pulling the whole `properties` Map into the profile CTE makes the LEFT ANY+// JOIN hash carry the full Map per profile — roughly a kilobyte each on real+// data — and OOMs at scale. Instead we project ONLY the referenced keys as+// scalar columns in the CTE and rewrite the refs to those columns — identical+// results at a fraction of the memory. Wildcard refs (mapExtractKeyLike) still+// need the full Map, so those fall back to selecting it.++const PROFILE_PROP_PREFIX = 'profile.properties.';++export function collectProfilePropertyKeys(refs: { name: string }[]): {+ keys: string[];+ needsFullMap: boolean;+} {+ const keys = new Set<string>();+ let needsFullMap = false;+ for (const { name } of refs) {+ if (!name.startsWith(PROFILE_PROP_PREFIX)) {+ continue;+ }+ // Wildcard refs render as mapExtractKeyLike over the whole Map.+ if (name.includes('*')) {+ needsFullMap = true;+ continue;+ }+ const key = name.slice(PROFILE_PROP_PREFIX.length);+ // A backtick or backslash in the key can't be embedded in the+ // backtick-quoted scalar alias. Such keys are never narrowed: the full+ // Map stays selected and their refs keep the original Map access.+ if (/[`\\]/.test(key)) {+ needsFullMap = true;+ continue;+ }+ keys.add(key);+ }+ return { keys: Array.from(keys), needsFullMap };+}++// The profile-CTE SELECT expression for the `properties` field: one scalar+// column per referenced key, plus the full Map only when a wildcard ref needs+// it (or when nothing specific was referenced).+export function profilePropertiesCteSelect(+ keys: string[],+ needsFullMap: boolean+): string {+ const cols = keys.map(+ (k) => `properties[${sqlstring.escape(k)}] as \`profile.properties.${k}\``+ );+ if (needsFullMap || cols.length === 0) {+ cols.push('properties as "profile.properties"');+ }+ return cols.join(', ');+}++// Rewrite `profile.properties['<key>']` -> `` `profile.properties.<key>` ``+// for the narrowed keys. Matches the raw render from getSelectPropertyKey /+// the filter builders; never matches the CTE's own `properties['<key>']`,+// which has no `profile.` prefix. No-op when keys is empty.+export function rewriteProfilePropertyRefs(+ sql: string,+ keys: string[]+): string {+ let out = sql;+ for (const k of keys) {+ out = out+ .split(`profile.properties['${k}']`)+ .join(`\`profile.properties.${k}\``);+ }+ return out;+}++/**+ * The top-level profiles columns a chart's `profile.*` references read —+ * `id` always (the join key), then `properties` / the scalar columns V1's+ * `getProfileFields` recognised, in first-seen order.+ */+export function collectProfileCteFields(refs: { name: string }[]): string[] {+ const fields = new Set<string>(['id']);+ for (const { name } of refs) {+ if (!name.startsWith('profile.')) {+ continue;+ }+ const fieldName = name.replace('profile.', '').split('.')[0];+ if (fieldName === 'properties') {+ fields.add('properties');+ } else if (fieldName && PROFILE_CTE_SCALAR_FIELDS.includes(fieldName)) {+ fields.add(fieldName);+ }+ }+ return Array.from(fields);+}diff --git a/packages/core/src/modules/chart/src/filter-where.ts b/packages/core/src/modules/chart/src/filter-where.tsnew file mode 100644index 000000000..354572919--- /dev/null+++ b/packages/core/src/modules/chart/src/filter-where.ts@@ -0,0 +1,557 @@+/** biome-ignore-all lint/style/useDefaultSwitchClause: operator switches are exhaustive by design */+// V1's event filter compiler (`getEventFiltersWhereClause`), moved verbatim+// from packages/db/src/services/chart.service.ts (M7-003). Renders TEXT with+// sqlstring: ADR-013 keeps the shared filter compilers as they are until+// funnel, conversion, sankey, retention and overview (M7-004/M7-005) stop+// splicing their output into text builders. Core splices it through+// compiled.ts, the one text bridge.++import { stripLeadingAndTrailingSlashes } from '@openpanel/common';+import {+ buildTypedClause,+ hasTypedCast,+ isTypedOperator,+} from '@openpanel/db/src/services/filter-cast';+import type { IChartEventFilter } from '@openpanel/validation';+import { getCohortIds } from '@openpanel/validation';+import sqlstring from 'sqlstring';+import {+ CHART_TABLE,+ EVENT_FIELD_ALIASES,+ EVENT_TOP_LEVEL_COLUMNS,+ getGroupPropertySql,+ getSelectPropertyKey,+ isNumericColumn,+ normalizeEventField,+} from './field-resolution';++export type FilterTableScope = 'events' | 'sessions';++export function getEventFiltersWhereClause(+ filters: IChartEventFilter[],+ projectId?: string,+ /**+ * See `getSelectPropertyKey`. When the surrounding query joins another+ * table that has a `properties` column (e.g. the `_g` groups join), the+ * events table must be aliased and passed here so we can emit+ * `e.properties[...]` instead of the ambiguous `properties[...]`.+ */+ eventsAlias?: string,+ /**+ * Which physical table the WHERE clause is being built for. Affects which+ * names count as "top-level columns" and whether bare `utm_*` gets routed+ * into the events-specific `properties.__query.utm_*` map. Defaults to+ * 'events' because that's where the vast majority of callers (chart,+ * funnel, conversion, sankey, event services) target — OverviewService+ * sets it to 'sessions' when querying the sessions table.+ */+ tableScope: FilterTableScope = 'events'+) {+ const where: Record<string, string> = {};+ filters.forEach((filter, index) => {+ const id = `f${index}`;+ const { value, operator } = filter;+ // Normalize camelCase aliases (`referrerName` → `referrer_name`) on both+ // tables — both events and sessions schemas use snake_case. The bare-+ // utm rewrite only applies to events because sessions stores utm_* as+ // real top-level columns; doing it for sessions would emit+ // `properties['__query.utm_source']` against a table that has no+ // `properties` column.+ const name =+ tableScope === 'sessions'+ ? (EVENT_FIELD_ALIASES[filter.name] ?? filter.name)+ : normalizeEventField(filter.name);++ if ((operator === 'inCohort' || operator === 'notInCohort') && projectId) {+ // Self-contained membership subselect — no caller JOIN wiring needed.+ // Cohort filters and cohort breakdowns are decoupled: the breakdown+ // path (getSelectPropertyKey) still uses a JOIN alias for SELECT+ // expressions, but filters never depend on it.+ const cohortIds = getCohortIds(filter);+ if (cohortIds.length === 0) {+ return;+ }+ const profileIdExpr = eventsAlias+ ? `${eventsAlias}.profile_id`+ : 'profile_id';+ const op = operator === 'notInCohort' ? 'NOT IN' : 'IN';+ const escapedIds = cohortIds.map((c) => sqlstring.escape(c)).join(', ');+ where[id] =+ `${profileIdExpr} ${op} (SELECT profile_id FROM ${CHART_TABLE.cohortMembers} FINAL WHERE cohort_id IN (${escapedIds}) AND project_id = ${sqlstring.escape(projectId)})`;+ return;+ }++ if (+ value.length === 0 &&+ operator !== 'isNull' &&+ operator !== 'isNotNull'+ ) {+ return;+ }++ if (name === 'has_profile') {+ if (value.includes('true')) {+ where[id] = 'profile_id != device_id';+ } else {+ where[id] = 'profile_id = device_id';+ }+ return;+ }++ // Handle group. prefixed filters (requires ARRAY JOIN + _g JOIN in query)+ if (name.startsWith('group.') && projectId) {+ const whereFrom = getGroupPropertySql(name);+ if (hasTypedCast(filter.type) && isTypedOperator(operator)) {+ where[id] = buildTypedClause(whereFrom, operator, value, filter.type);+ return;+ }+ switch (operator) {+ case 'is': {+ if (value.length === 1) {+ where[id] =+ `${whereFrom} = ${sqlstring.escape(String(value[0]).trim())}`;+ } else {+ where[id] =+ `${whereFrom} IN (${value.map((val) => sqlstring.escape(String(val).trim())).join(', ')})`;+ }+ break;+ }+ case 'isNot': {+ if (value.length === 1) {+ where[id] =+ `${whereFrom} != ${sqlstring.escape(String(value[0]).trim())}`;+ } else {+ where[id] =+ `${whereFrom} NOT IN (${value.map((val) => sqlstring.escape(String(val).trim())).join(', ')})`;+ }+ break;+ }+ case 'contains': {+ where[id] =+ `(${value.map((val) => `${whereFrom} LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`).join(' OR ')})`;+ break;+ }+ case 'doesNotContain': {+ where[id] =+ `(${value.map((val) => `${whereFrom} NOT LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`).join(' OR ')})`;+ break;+ }+ case 'startsWith': {+ where[id] =+ `(${value.map((val) => `${whereFrom} LIKE ${sqlstring.escape(`${String(val).trim()}%`)}`).join(' OR ')})`;+ break;+ }+ case 'endsWith': {+ where[id] =+ `(${value.map((val) => `${whereFrom} LIKE ${sqlstring.escape(`%${String(val).trim()}`)}`).join(' OR ')})`;+ break;+ }+ case 'isNull': {+ where[id] = `(${whereFrom} = '' OR ${whereFrom} IS NULL)`;+ break;+ }+ case 'isNotNull': {+ where[id] = `(${whereFrom} != '' AND ${whereFrom} IS NOT NULL)`;+ break;+ }+ case 'regex': {+ where[id] =+ `(${value.map((val) => `match(${whereFrom}, ${sqlstring.escape(String(val).trim())})`).join(' OR ')})`;+ break;+ }+ }+ return;+ }++ if (+ name.startsWith('properties.') ||+ name.startsWith('profile.properties.')+ ) {+ const propertyKey = getSelectPropertyKey(+ name,+ undefined,+ undefined,+ undefined,+ eventsAlias+ );+ const isWildcard = propertyKey.includes('%');+ const whereFrom = propertyKey;++ // Typed cast (number/date/datetime/boolean) short-circuit. Casts both the+ // column and each value so e.g. `>= '2019-01-01'` compares as dates+ // instead of crashing `toFloat64('2019-01-01')`. Untyped/string filters+ // fall through to the legacy switch below.+ if (hasTypedCast(filter.type) && isTypedOperator(operator)) {+ where[id] = isWildcard+ ? `arrayExists(x -> ${buildTypedClause('x', operator, value, filter.type)}, ${whereFrom})`+ : buildTypedClause(whereFrom, operator, value, filter.type);+ return;+ }++ switch (operator) {+ case 'is': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map((val) => `x = ${sqlstring.escape(String(val).trim())}`)+ .join(' OR ')}, ${whereFrom})`;+ } else if (value.length === 1) {+ where[id] =+ `${whereFrom} = ${sqlstring.escape(String(value[0]).trim())}`;+ } else {+ where[id] = `${whereFrom} IN (${value+ .map((val) => sqlstring.escape(String(val).trim()))+ .join(', ')})`;+ }+ break;+ }+ case 'isNot': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map((val) => `x != ${sqlstring.escape(String(val).trim())}`)+ .join(' OR ')}, ${whereFrom})`;+ } else if (value.length === 1) {+ where[id] =+ `${whereFrom} != ${sqlstring.escape(String(value[0]).trim())}`;+ } else {+ where[id] = `${whereFrom} NOT IN (${value+ .map((val) => sqlstring.escape(String(val).trim()))+ .join(', ')})`;+ }+ break;+ }+ case 'contains': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map(+ (val) => `x LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`+ )+ .join(' OR ')}, ${whereFrom})`;+ } else {+ where[id] = `(${value+ .map(+ (val) =>+ `${whereFrom} LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`+ )+ .join(' OR ')})`;+ }+ break;+ }+ case 'doesNotContain': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map(+ (val) =>+ `x NOT LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`+ )+ .join(' OR ')}, ${whereFrom})`;+ } else {+ where[id] = `(${value+ .map(+ (val) =>+ `${whereFrom} NOT LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`+ )+ .join(' OR ')})`;+ }+ break;+ }+ case 'startsWith': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map(+ (val) => `x LIKE ${sqlstring.escape(`${String(val).trim()}%`)}`+ )+ .join(' OR ')}, ${whereFrom})`;+ } else {+ where[id] = `(${value+ .map(+ (val) =>+ `${whereFrom} LIKE ${sqlstring.escape(`${String(val).trim()}%`)}`+ )+ .join(' OR ')})`;+ }+ break;+ }+ case 'endsWith': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map(+ (val) => `x LIKE ${sqlstring.escape(`%${String(val).trim()}`)}`+ )+ .join(' OR ')}, ${whereFrom})`;+ } else {+ where[id] = `(${value+ .map(+ (val) =>+ `${whereFrom} LIKE ${sqlstring.escape(`%${String(val).trim()}`)}`+ )+ .join(' OR ')})`;+ }+ break;+ }+ case 'regex': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map((val) => `match(x, ${sqlstring.escape(String(val).trim())})`)+ .join(' OR ')}, ${whereFrom})`;+ } else {+ where[id] = `(${value+ .map(+ (val) =>+ `match(${whereFrom}, ${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')})`;+ }+ break;+ }+ case 'isNull': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> x = '' OR x IS NULL, ${whereFrom})`;+ } else {+ where[id] = `(${whereFrom} = '' OR ${whereFrom} IS NULL)`;+ }+ break;+ }+ case 'isNotNull': {+ if (isWildcard) {+ where[id] =+ `arrayExists(x -> x != '' AND x IS NOT NULL, ${whereFrom})`;+ } else {+ where[id] = `(${whereFrom} != '' AND ${whereFrom} IS NOT NULL)`;+ }+ break;+ }+ case 'gt': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map(+ (val) =>+ `toFloat64OrZero(x) > toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')}, ${whereFrom})`;+ } else {+ where[id] = `(${value+ .map(+ (val) =>+ `toFloat64OrZero(${whereFrom}) > toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')})`;+ }+ break;+ }+ case 'lt': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map(+ (val) =>+ `toFloat64OrZero(x) < toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')}, ${whereFrom})`;+ } else {+ where[id] = `(${value+ .map(+ (val) =>+ `toFloat64OrZero(${whereFrom}) < toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')})`;+ }+ break;+ }+ case 'gte': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map(+ (val) =>+ `toFloat64OrZero(x) >= toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')}, ${whereFrom})`;+ } else {+ where[id] = `(${value+ .map(+ (val) =>+ `toFloat64OrZero(${whereFrom}) >= toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')})`;+ }+ break;+ }+ case 'lte': {+ if (isWildcard) {+ where[id] = `arrayExists(x -> ${value+ .map(+ (val) =>+ `toFloat64OrZero(x) <= toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')}, ${whereFrom})`;+ } else {+ where[id] = `(${value+ .map(+ (val) =>+ `toFloat64OrZero(${whereFrom}) <= toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')})`;+ }+ break;+ }+ }+ } else {+ // Bare-column branch. For events queries: enforce that `name` is one+ // of the known top-level columns (anything else would crash parse with+ // UNKNOWN_IDENTIFIER). For sessions queries: skip the guard because+ // the sessions table has its own column set (utm_*, entry_path, etc.)+ // that OverviewService.getRawWhereClause already vets via its+ // WHITELISTED_FILTERS pre-pass.+ if (tableScope === 'events' && !EVENT_TOP_LEVEL_COLUMNS.has(name)) {+ return;+ }+ // Typed cast short-circuit (see property branch above). Supersedes the+ // `isNumericColumn` auto-detect when the user declared an explicit type.+ if (hasTypedCast(filter.type) && isTypedOperator(operator)) {+ where[id] = buildTypedClause(name, operator, value, filter.type);+ return;+ }+ switch (operator) {+ case 'is': {+ if (value.length === 1) {+ where[id] =+ `${name} = ${sqlstring.escape(String(value[0]).trim())}`;+ } else {+ where[id] = `${name} IN (${value+ .map((val) => sqlstring.escape(String(val).trim()))+ .join(', ')})`;+ }+ break;+ }+ case 'isNull': {+ where[id] = `(${name} = '' OR ${name} IS NULL)`;+ break;+ }+ case 'isNotNull': {+ where[id] = `(${name} != '' AND ${name} IS NOT NULL)`;+ break;+ }+ case 'isNot': {+ if (value.length === 1) {+ where[id] =+ `${name} != ${sqlstring.escape(String(value[0]).trim())}`;+ } else {+ where[id] = `${name} NOT IN (${value+ .map((val) => sqlstring.escape(String(val).trim()))+ .join(', ')})`;+ }+ break;+ }+ case 'contains': {+ where[id] = `(${value+ .map(+ (val) =>+ `${name} LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`+ )+ .join(' OR ')})`;+ break;+ }+ case 'doesNotContain': {+ where[id] = `(${value+ .map(+ (val) =>+ `${name} NOT LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`+ )+ .join(' OR ')})`;+ break;+ }+ case 'startsWith': {+ where[id] = `(${value+ .map(+ (val) =>+ `${name} LIKE ${sqlstring.escape(`${String(val).trim()}%`)}`+ )+ .join(' OR ')})`;+ break;+ }+ case 'endsWith': {+ where[id] = `(${value+ .map(+ (val) =>+ `${name} LIKE ${sqlstring.escape(`%${String(val).trim()}`)}`+ )+ .join(' OR ')})`;+ break;+ }+ case 'regex': {+ where[id] = `(${value+ .map(+ (val) =>+ `match(${name}, ${sqlstring.escape(stripLeadingAndTrailingSlashes(String(val)).trim())})`+ )+ .join(' OR ')})`;+ break;+ }+ case 'gt': {+ if (isNumericColumn(name)) {+ where[id] = `(${value+ .map(+ (val) =>+ `toFloat64(${name}) > toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')})`;+ } else {+ where[id] = `(${value+ .map((val) => `${name} > ${sqlstring.escape(String(val).trim())}`)+ .join(' OR ')})`;+ }+ break;+ }+ case 'lt': {+ if (isNumericColumn(name)) {+ where[id] = `(${value+ .map(+ (val) =>+ `toFloat64(${name}) < toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')})`;+ } else {+ where[id] = `(${value+ .map((val) => `${name} < ${sqlstring.escape(String(val).trim())}`)+ .join(' OR ')})`;+ }+ break;+ }+ case 'gte': {+ if (isNumericColumn(name)) {+ where[id] = `(${value+ .map(+ (val) =>+ `toFloat64(${name}) >= toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')})`;+ } else {+ where[id] = `(${value+ .map(+ (val) => `${name} >= ${sqlstring.escape(String(val).trim())}`+ )+ .join(' OR ')})`;+ }+ break;+ }+ case 'lte': {+ if (isNumericColumn(name)) {+ where[id] = `(${value+ .map(+ (val) =>+ `toFloat64(${name}) <= toFloat64(${sqlstring.escape(String(val).trim())})`+ )+ .join(' OR ')})`;+ } else {+ where[id] = `(${value+ .map(+ (val) => `${name} <= ${sqlstring.escape(String(val).trim())}`+ )+ .join(' OR ')})`;+ }+ break;+ }+ }+ }+ });++ return where;+}diff --git a/packages/core/src/modules/cohort/cohort.jobs.test.ts b/packages/core/src/modules/cohort/cohort.jobs.test.tsindex bbd2fb0d2..f2b57302e 100644--- a/packages/core/src/modules/cohort/cohort.jobs.test.ts+++ b/packages/core/src/modules/cohort/cohort.jobs.test.ts@@ -42,6 +42,7 @@ function stubJobCtx(cohort: Partial<CohortService>, jobName = 'test'): JobCtx {event: {} as Services['event'],profile: {} as Services['profile'],group: {} as Services['group'],+ chart: {} as Services['chart'],};return {db: {},diff --git a/packages/core/src/modules/event/event.service.ts b/packages/core/src/modules/event/event.service.tsindex dd3d4eaac..8e5925b38 100644--- a/packages/core/src/modules/event/event.service.ts+++ b/packages/core/src/modules/event/event.service.ts@@ -28,6 +28,7 @@ import { cacheable } from '@openpanel/redis';import type { IChartEventFilter } from '@openpanel/validation';import { clone, mergeDeepRight, uniq } from 'ramda';import type { ServiceDeps } from '../../services';+import { getEventFiltersWhereClause } from '../chart/src/filter-where';import {getProfileById,getProfilesCached,@@ -108,10 +109,6 @@ function loadCache() {return import('@openpanel/redis').then((m) => m.getCache);}-function loadEventFilterCompiler() {- return import('@openpanel/db/src/services/chart.service');-}-function loadFilterCompiler() {return import('@openpanel/db/src/services/filter-where.service');}@@ -702,7 +699,6 @@ async function compileEventFilters(if (!filters) {return { filterClauses: {}, joins: NO_FILTER_JOINS };}- const { getEventFiltersWhereClause } = await loadEventFilterCompiler();return {filterClauses: getEventFiltersWhereClause(filters, projectId, EVENTS_ALIAS),joins: eventFilterJoins(filters),diff --git a/packages/core/src/modules/gsc/gsc.jobs.test.ts b/packages/core/src/modules/gsc/gsc.jobs.test.tsindex ce0a7245b..03e6a68f5 100644--- a/packages/core/src/modules/gsc/gsc.jobs.test.ts+++ b/packages/core/src/modules/gsc/gsc.jobs.test.ts@@ -38,6 +38,7 @@ function stubJobCtx(gsc: Partial<GscService>, jobName = 'test'): JobCtx {event: {} as Services['event'],profile: {} as Services['profile'],group: {} as Services['group'],+ chart: {} as Services['chart'],};return {db: {},@@ -142,6 +143,7 @@ test('gscSync fans out one gscProjectSync enqueue per connected project', asyncevent: {} as Services['event'],profile: {} as Services['profile'],group: {} as Services['group'],+ chart: {} as Services['chart'],},requestId: 'req_1',job: { id: 'job_1', attempt: 0, queue: 'cron', name: 'gscSync' },diff --git a/packages/core/src/modules/import/import.jobs.test.ts b/packages/core/src/modules/import/import.jobs.test.tsindex 078f1cfbf..99320b565 100644--- a/packages/core/src/modules/import/import.jobs.test.ts+++ b/packages/core/src/modules/import/import.jobs.test.ts@@ -38,6 +38,7 @@ function stubJobCtx(importService: Partial<ImportService>): JobCtx {event: {} as Services['event'],profile: {} as Services['profile'],group: {} as Services['group'],+ chart: {} as Services['chart'],};return {db: {},diff --git a/packages/core/src/modules/insight/insight.jobs.test.ts b/packages/core/src/modules/insight/insight.jobs.test.tsindex da48d5ac7..1e1b7e6f5 100644--- a/packages/core/src/modules/insight/insight.jobs.test.ts+++ b/packages/core/src/modules/insight/insight.jobs.test.ts@@ -49,6 +49,7 @@ function stubJobCtx(event: {} as Services['event'],profile: {} as Services['profile'],group: {} as Services['group'],+ chart: {} as Services['chart'],};return {db: {},@@ -136,6 +137,7 @@ test('insightsDaily fans out one insightsProject enqueue per candidate, jobId deevent: {} as Services['event'],profile: {} as Services['profile'],group: {} as Services['group'],+ chart: {} as Services['chart'],},requestId: 'req_1',job: { id: 'job_1', attempt: 0, queue: 'cron', name: 'insightsDaily' },diff --git a/packages/core/src/modules/notification/notification.jobs.test.ts b/packages/core/src/modules/notification/notification.jobs.test.tsindex c7a4700c7..b28515759 100644--- a/packages/core/src/modules/notification/notification.jobs.test.ts+++ b/packages/core/src/modules/notification/notification.jobs.test.ts@@ -38,6 +38,7 @@ function stubJobCtx(notification: Partial<NotificationService>): JobCtx {event: {} as Services['event'],profile: {} as Services['profile'],group: {} as Services['group'],+ chart: {} as Services['chart'],};return {db: {},diff --git a/packages/core/src/modules/onboarding/onboarding.jobs.test.ts b/packages/core/src/modules/onboarding/onboarding.jobs.test.tsindex c293149fc..7dc578af7 100644--- a/packages/core/src/modules/onboarding/onboarding.jobs.test.ts+++ b/packages/core/src/modules/onboarding/onboarding.jobs.test.ts@@ -41,6 +41,7 @@ function stubJobCtx(event: {} as Services['event'],profile: {} as Services['profile'],group: {} as Services['group'],+ chart: {} as Services['chart'],};return {db: {},diff --git a/packages/core/src/modules/organization/organization.jobs.test.ts b/packages/core/src/modules/organization/organization.jobs.test.tsindex 3968c351f..b2921e786 100644--- a/packages/core/src/modules/organization/organization.jobs.test.ts+++ b/packages/core/src/modules/organization/organization.jobs.test.ts@@ -44,6 +44,7 @@ function stubJobCtx(event: {} as Services['event'],profile: {} as Services['profile'],group: {} as Services['group'],+ chart: {} as Services['chart'],};return {db: {},diff --git a/packages/core/src/rpc.router.ts b/packages/core/src/rpc.router.tsindex d0161de2a..5b0972818 100644--- a/packages/core/src/rpc.router.ts+++ b/packages/core/src/rpc.router.ts@@ -6,6 +6,7 @@import { chatRouter } from './modules/assistant/assistant.rpc';import { authRouter } from './modules/auth/auth.rpc';+import { chartRouter } from './modules/chart/chart.rpc';import { clientRouter } from './modules/client/client.rpc';import { cohortRouter } from './modules/cohort/cohort.rpc';import { conversationRouter } from './modules/conversation/conversation.rpc';@@ -55,6 +56,7 @@ export const appRouter = createTRPCRouter({event: eventRouter,profile: profileRouter,group: groupRouter,+ chart: chartRouter,});export type AppRouter = typeof appRouter;diff --git a/packages/core/src/services.ts b/packages/core/src/services.tsindex c1411ff6d..19c555008 100644--- a/packages/core/src/services.ts+++ b/packages/core/src/services.ts@@ -7,6 +7,10 @@ import {type AuthService,createAuthService,} from './modules/auth/auth.service';+import {+ type ChartService,+ createChartService,+} from './modules/chart/chart.service';import {type CohortService,createCohortService,@@ -77,6 +81,7 @@ export interface Services {event: EventService;profile: ProfileService;group: GroupService;+ chart: ChartService;}export function createServices(deps: ServiceDeps): Services {@@ -96,5 +101,6 @@ export function createServices(deps: ServiceDeps): Services {event: createEventService(deps),profile: createProfileService(deps),group: createGroupService(deps),+ chart: createChartService(deps),};}diff --git a/packages/db/src/engine/fetch.ts b/packages/db/src/engine/fetch.tsdeleted file mode 100644index 6e8056dde..000000000--- a/packages/db/src/engine/fetch.ts+++ /dev/null@@ -1,148 +0,0 @@-import type { ISerieDataItem } from '@openpanel/common';-import { groupByLabels } from '@openpanel/common';-import { alphabetIds } from '@openpanel/constants';-import type { IGetChartDataInput } from '@openpanel/validation';-import { chQuery } from '../clickhouse/client';-import { getChartSql } from '../services/chart.service';-import type { ConcreteSeries, Plan } from './types';--/**- * Fetch data for all event series in the plan- * This handles breakdown expansion automatically via groupByLabels- */-export async function fetch(plan: Plan): Promise<ConcreteSeries[]> {- const results: ConcreteSeries[] = [];-- // Process each event definition- for (let i = 0; i < plan.definitions.length; i++) {- const definition = plan.definitions[i]!;-- if (definition.type !== 'event') {- // Skip formulas - they'll be handled in compute stage- continue;- }-- const event = definition as typeof definition & { type: 'event' };-- // Find the corresponding concrete series placeholder- const placeholder = plan.concreteSeries.find(- (cs) => cs.definitionId === definition.id,- );-- if (!placeholder) {- continue;- }-- // Build query input- const queryInput: IGetChartDataInput = {- event: {- id: event.id,- name: event.name,- segment: event.segment,- filters: event.filters,- displayName: event.displayName,- property: event.property,- },- projectId: plan.input.projectId,- startDate: plan.input.startDate,- endDate: plan.input.endDate,- breakdowns: plan.input.breakdowns,- interval: plan.input.interval,- chartType: plan.input.chartType,- metric: plan.input.metric,- previous: plan.input.previous ?? false,- limit: plan.input.limit,- offset: plan.input.offset,- };-- // Execute query- let queryResult = await chQuery<ISerieDataItem>(- await getChartSql({ ...queryInput, timezone: plan.timezone }),- {- session_timezone: plan.timezone,- },- );-- // Fallback: if no results with breakdowns, try without breakdowns- if (queryResult.length === 0 && plan.input.breakdowns.length > 0) {- queryResult = await chQuery<ISerieDataItem>(- await getChartSql({- ...queryInput,- breakdowns: [],- timezone: plan.timezone,- }),- {- session_timezone: plan.timezone,- },- );- }-- // Group by labels (handles breakdown expansion)- const groupedSeries = groupByLabels(queryResult);-- // Create concrete series for each grouped result- groupedSeries.forEach((grouped) => {- // Extract breakdown value from name array- // If breakdowns exist, name[0] is event name, name[1+] are breakdown values- const breakdownValue =- plan.input.breakdowns.length > 0 && grouped.name.length > 1- ? grouped.name.slice(1).join(' - ')- : undefined;-- // Build breakdowns object: { country: 'SE', path: '/ewoqmepwq' }- const breakdowns: Record<string, string> | undefined =- plan.input.breakdowns.length > 0 && grouped.name.length > 1- ? {}- : undefined;-- if (breakdowns) {- plan.input.breakdowns.forEach((breakdown, idx) => {- const breakdownNamePart = grouped.name[idx + 1];- if (breakdownNamePart) {- breakdowns[breakdown.name] = breakdownNamePart;- }- });- }-- // Build filters including breakdown value- const filters = [...event.filters];- if (breakdownValue && plan.input.breakdowns.length > 0) {- // Add breakdown filter- plan.input.breakdowns.forEach((breakdown, idx) => {- const breakdownNamePart = grouped.name[idx + 1];- if (breakdownNamePart) {- filters.push({- id: `breakdown-${idx}`,- name: breakdown.name,- operator: 'is',- value: [breakdownNamePart],- });- }- });- }-- const concrete: ConcreteSeries = {- id: `${placeholder.id}-${grouped.name.join('-')}`,- definitionId: definition.id ?? alphabetIds[i] ?? `series-${i}`,- definitionIndex: i,- name: grouped.name,- context: {- event: event.name,- filters,- breakdownValue,- breakdowns,- },- data: grouped.data.map((item) => ({- date: item.date,- count: item.count,- total_count: item.total_count,- })),- definition,- };-- results.push(concrete);- });- }-- return results;-}diff --git a/packages/db/src/engine/index.ts b/packages/db/src/engine/index.tsindex 88a7e7bd6..bb75ead24 100644--- a/packages/db/src/engine/index.ts+++ b/packages/db/src/engine/index.ts@@ -1,360 +1,14 @@-import type { ISerieDataItem } from '@openpanel/common';-import { groupByLabels } from '@openpanel/common';-import { alphabetIds } from '@openpanel/constants';-import type {- FinalChart,- IChartEventItem,- IReportInput,-} from '@openpanel/validation';-import { chQuery } from '../clickhouse/client';-import { getAggregateChartSql } from '../services/chart.service';-import { getChartPrevStartEndDate } from '../services/date.service';-import {- getOrganizationSubscriptionChartEndDate,- getSettingsForProject,-} from '../services/organization.service';-import { compute } from './compute';-import { fetch } from './fetch';-import { format } from './format';-import { normalize } from './normalize';-import { plan } from './plan';-import type { ConcreteSeries } from './types';--/**- * Chart Engine - Main entry point- * Executes the pipeline: normalize -> plan -> fetch -> compute -> format- */-export async function executeChart(input: IReportInput): Promise<FinalChart> {- // Stage 1: Normalize input- const normalized = await normalize(input);-- // Handle subscription end date limit- const endDate = await getOrganizationSubscriptionChartEndDate(- input.projectId,- normalized.endDate- );- if (endDate) {- normalized.endDate = endDate;- }-- // Stage 2: Create execution plan- const executionPlan = await plan(normalized);-- // Stage 3: Fetch data for event series (current period)- const fetchedSeries = await fetch(executionPlan);-- // Stage 4: Compute formula series- const computedSeries = compute(fetchedSeries, executionPlan.definitions);-- // Stage 5: Fetch previous period if requested- let previousSeries: ConcreteSeries[] | null = null;- if (input.previous) {- const currentPeriod = {- startDate: normalized.startDate,- endDate: normalized.endDate,- };- const previousPeriod = getChartPrevStartEndDate(currentPeriod);-- const previousPlan = await plan({- ...normalized,- ...previousPeriod,- });-- const previousFetched = await fetch(previousPlan);- previousSeries = compute(previousFetched, previousPlan.definitions);- }-- // Stage 6: Format final output with previous period data- const includeAlphaIds = executionPlan.definitions.length > 1;- const response = format(- computedSeries,- executionPlan.definitions,- includeAlphaIds,- previousSeries,- normalized.limit- );-- return response;-}--/**- * Aggregate Chart Engine - Optimized for bar/pie charts without time series- * Executes a simplified pipeline: normalize -> fetch aggregate -> format- */-export async function executeAggregateChart(- input: IReportInput-): Promise<FinalChart> {- // Stage 1: Normalize input- const normalized = await normalize(input);-- // Handle subscription end date limit- const endDate = await getOrganizationSubscriptionChartEndDate(- input.projectId,- normalized.endDate- );- if (endDate) {- normalized.endDate = endDate;- }-- const { timezone } = await getSettingsForProject(normalized.projectId);-- // Stage 2: Fetch aggregate data for current period (event series only)- const fetchedSeries: ConcreteSeries[] = [];-- for (let i = 0; i < normalized.series.length; i++) {- const definition = normalized.series[i]!;-- if (definition.type !== 'event') {- // Skip formulas - they'll be computed in the next stage- continue;- }-- const event = definition as IChartEventItem & { type: 'event' };-- // Build query input- const queryInput = {- event: {- id: event.id,- name: event.name,- segment: event.segment,- filters: event.filters,- displayName: event.displayName,- property: event.property,- },- projectId: normalized.projectId,- startDate: normalized.startDate,- endDate: normalized.endDate,- breakdowns: normalized.breakdowns,- limit: normalized.limit,- metric: normalized.metric,- previous: normalized.previous,- timezone,- };-- // Execute aggregate query- let queryResult = await chQuery<ISerieDataItem>(- await getAggregateChartSql(queryInput),- {- session_timezone: timezone,- }- );-- // Fallback: if no results with breakdowns, try without breakdowns- if (queryResult.length === 0 && normalized.breakdowns.length > 0) {- queryResult = await chQuery<ISerieDataItem>(- await getAggregateChartSql({- ...queryInput,- breakdowns: [],- }),- {- session_timezone: timezone,- }- );- }-- // Group by labels (handles breakdown expansion)- const groupedSeries = groupByLabels(queryResult);-- // Create concrete series for each grouped result- groupedSeries.forEach((grouped) => {- // Extract breakdown value from name array- const breakdownValue =- normalized.breakdowns.length > 0 && grouped.name.length > 1- ? grouped.name.slice(1).join(' - ')- : undefined;-- // Build breakdowns object- const breakdowns: Record<string, string> | undefined =- normalized.breakdowns.length > 0 && grouped.name.length > 1- ? {}- : undefined;-- if (breakdowns) {- normalized.breakdowns.forEach((breakdown, idx) => {- const breakdownNamePart = grouped.name[idx + 1];- if (breakdownNamePart) {- breakdowns[breakdown.name] = breakdownNamePart;- }- });- }-- // Build filters including breakdown value- const filters = [...event.filters];- if (breakdownValue && normalized.breakdowns.length > 0) {- normalized.breakdowns.forEach((breakdown, idx) => {- const breakdownNamePart = grouped.name[idx + 1];- if (breakdownNamePart) {- filters.push({- id: `breakdown-${idx}`,- name: breakdown.name,- operator: 'is',- value: [breakdownNamePart],- });- }- });- }-- // For aggregate charts, grouped.data should have a single data point- // (since we use a constant date in the query)- const concrete: ConcreteSeries = {- id: `${event.name}-${grouped.name.join('-')}-${i}`,- definitionId: definition.id ?? alphabetIds[i] ?? `series-${i}`,- definitionIndex: i,- name: grouped.name,- context: {- event: event.name,- filters,- breakdownValue,- breakdowns,- },- data: grouped.data,- definition,- };-- fetchedSeries.push(concrete);- });- }-- // Stage 3: Compute formula series from fetched event series- const computedSeries = compute(fetchedSeries, normalized.series);-- // Stage 4: Fetch previous period if requested- let previousSeries: ConcreteSeries[] | null = null;- if (input.previous) {- const currentPeriod = {- startDate: normalized.startDate,- endDate: normalized.endDate,- };- const previousPeriod = getChartPrevStartEndDate(currentPeriod);-- const previousFetchedSeries: ConcreteSeries[] = [];-- for (let i = 0; i < normalized.series.length; i++) {- const definition = normalized.series[i]!;-- if (definition.type !== 'event') {- continue;- }-- const event = definition as IChartEventItem & { type: 'event' };-- const queryInput = {- event: {- id: event.id,- name: event.name,- segment: event.segment,- filters: event.filters,- displayName: event.displayName,- property: event.property,- },- projectId: normalized.projectId,- startDate: previousPeriod.startDate,- endDate: previousPeriod.endDate,- breakdowns: normalized.breakdowns,- limit: normalized.limit,- metric: normalized.metric,- previous: normalized.previous,- timezone,- };-- let queryResult = await chQuery<ISerieDataItem>(- await getAggregateChartSql(queryInput),- {- session_timezone: timezone,- }- );-- if (queryResult.length === 0 && normalized.breakdowns.length > 0) {- queryResult = await chQuery<ISerieDataItem>(- await getAggregateChartSql({- ...queryInput,- breakdowns: [],- }),- {- session_timezone: timezone,- }- );- }-- const groupedSeries = groupByLabels(queryResult);-- groupedSeries.forEach((grouped) => {- const breakdownValue =- normalized.breakdowns.length > 0 && grouped.name.length > 1- ? grouped.name.slice(1).join(' - ')- : undefined;-- const breakdowns: Record<string, string> | undefined =- normalized.breakdowns.length > 0 && grouped.name.length > 1- ? {}- : undefined;-- if (breakdowns) {- normalized.breakdowns.forEach((breakdown, idx) => {- const breakdownNamePart = grouped.name[idx + 1];- if (breakdownNamePart) {- breakdowns[breakdown.name] = breakdownNamePart;- }- });- }-- const filters = [...event.filters];- if (breakdownValue && normalized.breakdowns.length > 0) {- normalized.breakdowns.forEach((breakdown, idx) => {- const breakdownNamePart = grouped.name[idx + 1];- if (breakdownNamePart) {- filters.push({- id: `breakdown-${idx}`,- name: breakdown.name,- operator: 'is',- value: [breakdownNamePart],- });- }- });- }-- const concrete: ConcreteSeries = {- id: `${event.name}-${grouped.name.join('-')}-${i}`,- definitionId: definition.id ?? alphabetIds[i] ?? `series-${i}`,- definitionIndex: i,- name: grouped.name,- context: {- event: event.name,- filters,- breakdownValue,- breakdowns,- },- data: grouped.data,- definition,- };-- previousFetchedSeries.push(concrete);- });- }-- // Compute formula series for previous period- previousSeries = compute(previousFetchedSeries, normalized.series);- }-- // Stage 5: Format final output with previous period data- const includeAlphaIds = normalized.series.length > 1;- const response = format(- computedSeries,- normalized.series,- includeAlphaIds,- previousSeries,- normalized.limit- );-- return response;-}--// Export as ChartEngine for backward compatibility-export const ChartEngine = {- execute: executeChart,-};--// Export aggregate chart engine-export const AggregateChartEngine = {- execute: executeAggregateChart,-};+// The chart engine lives in @openpanel/core now (M7-003). Re-exported here+// for existing `@openpanel/db` importers (apps/api's export controller,+// reports.service, core's mcp/assistant tools) — same shape as+// packages/db/src/services/chart.service.ts.+export type { ConcreteSeries, Plan, SeriesDefinition } from '@openpanel/core';+export {+ AggregateChartEngine,+ ChartEngine,+ evaluateFormula,+ executeAggregateChart,+ executeChart,+ InvalidFormulaError,+ isValidFormula,+} from '@openpanel/core';diff --git a/packages/db/src/engine/normalize.ts b/packages/db/src/engine/normalize.tsdeleted file mode 100644index 4c93bff95..000000000--- a/packages/db/src/engine/normalize.ts+++ /dev/null@@ -1,67 +0,0 @@-import { alphabetIds } from '@openpanel/constants';-import type {- IChartEvent,- IChartEventItem,- IReportInput,- IReportInputWithDates,-} from '@openpanel/validation';-import { getChartStartEndDate } from '../services/date.service';-import { getSettingsForProject } from '../services/organization.service';-import { mergeGlobalFilters } from '../services/reports.service';-import type { SeriesDefinition } from './types';--export type NormalizedInput = Awaited<ReturnType<typeof normalize>>;--/**- * Normalize a chart input into a clean structure with dates and normalized series- */-export async function normalize(- input: IReportInput,-): Promise<IReportInputWithDates & { series: SeriesDefinition[] }> {- const { timezone } = await getSettingsForProject(input.projectId);- const { startDate, endDate } = getChartStartEndDate(- {- range: input.range,- startDate: input.startDate ?? undefined,- endDate: input.endDate ?? undefined,- },- timezone,- );-- // Get series from input (handles both 'series' and 'events' fields)- // The schema preprocessing should have already converted 'events' to 'series', but handle both for safety- const rawSeries = (input as any).series ?? (input as any).events ?? [];-- // Normalize each series item- const normalizedSeries: SeriesDefinition[] = rawSeries.map(- (item: any, index: number) => {- // If item already has type field, it's the new format- if (item && typeof item === 'object' && 'type' in item) {- return {- ...item,- id: item.id ?? alphabetIds[index] ?? `series-${index}`,- } as SeriesDefinition;- }-- // Old format without type field - assume it's an event- const event = item as Partial<IChartEvent>;- return {- type: 'event',- id: event.id ?? alphabetIds[index] ?? `series-${index}`,- name: event.name || 'unknown_event',- segment: event.segment ?? 'event',- filters: event.filters ?? [],- displayName: event.displayName,- property: event.property,- } as SeriesDefinition;- },- );-- return {- ...input,- series: mergeGlobalFilters(normalizedSeries, input.globalFilters),- startDate,- endDate,- };-}-diff --git a/packages/db/src/engine/plan.ts b/packages/db/src/engine/plan.tsdeleted file mode 100644index 1e184c153..000000000--- a/packages/db/src/engine/plan.ts+++ /dev/null@@ -1,50 +0,0 @@-import { slug } from '@openpanel/common';-import { alphabetIds } from '@openpanel/constants';-import type { IChartEventItem } from '@openpanel/validation';-import { getSettingsForProject } from '../services/organization.service';-import type { NormalizedInput } from './normalize';-import type { ConcreteSeries, Plan } from './types';--/**- * Create an execution plan from normalized input- * This sets up ConcreteSeries placeholders - actual breakdown expansion happens during fetch- */-export async function plan(normalized: NormalizedInput): Promise<Plan> {- const { timezone } = await getSettingsForProject(normalized.projectId);-- const concreteSeries: ConcreteSeries[] = [];-- // Create concrete series placeholders for each definition- normalized.series.forEach((definition, index) => {- if (definition.type === 'event') {- const event = definition as IChartEventItem & { type: 'event' };-- // For events, create a placeholder- // If breakdowns exist, fetch will return multiple series (one per breakdown value)- // If no breakdowns, fetch will return one series- const concrete: ConcreteSeries = {- id: `${slug(event.name)}-${event.id ?? index}`,- definitionId: event.id ?? alphabetIds[index] ?? `series-${index}`,- definitionIndex: index,- name: [event.displayName || event.name],- context: {- event: event.name,- filters: [...event.filters],- },- data: [], // Will be populated by fetch stage- definition,- };- concreteSeries.push(concrete);- } else {- // For formulas, we'll create placeholders during compute stage- // Formulas depend on event series, so we skip them here- }- });-- return {- concreteSeries,- definitions: normalized.series,- input: normalized,- timezone,- };-}diff --git a/packages/db/src/engine/types.ts b/packages/db/src/engine/types.tsdeleted file mode 100644index 774755bea..000000000--- a/packages/db/src/engine/types.ts+++ /dev/null@@ -1,85 +0,0 @@-import type {- IChartBreakdown,- IChartEvent,- IChartEventFilter,- IChartEventItem,- IChartFormula,- IReportInput,- IReportInputWithDates,-} from '@openpanel/validation';--/**- * Series Definition - The input representation of what the user wants- * This is what comes from the frontend (events or formulas)- */-export type SeriesDefinition = IChartEventItem;--/**- * Concrete Series - A resolved series that will be displayed as a line/bar on the chart- * When breakdowns exist, one SeriesDefinition can expand into multiple ConcreteSeries- */-export type ConcreteSeries = {- id: string;- definitionId: string; // ID of the SeriesDefinition this came from- definitionIndex: number; // Index in the original series array (for A, B, C references)- name: string[]; // Display name parts: ["Session Start", "Chrome"] or ["Formula 1"]-- // Context for Drill-down / Profiles- // This contains everything needed to query 'who are these users?'- context: {- event?: string; // Event name (if this is an event series)- filters: IChartEventFilter[]; // All filters including breakdown value- breakdownValue?: string; // The breakdown value for this concrete series (deprecated, use breakdowns instead)- breakdowns?: Record<string, string>; // Breakdown keys and values: { country: 'SE', path: '/ewoqmepwq' }- };-- // Data points for this series- data: Array<{- date: string;- count: number;- total_count?: number;- }>;-- // The original definition (event or formula)- definition: SeriesDefinition;-};--/**- * Plan - The execution plan after normalization and expansion- */-export type Plan = {- concreteSeries: ConcreteSeries[];- definitions: SeriesDefinition[];- input: IReportInputWithDates;- timezone: string;-};--/**- * Chart Response - The final output format- */-export type ChartResponse = {- series: Array<{- id: string;- name: string[];- data: Array<{- date: string;- value: number;- previous?: number;- }>;- summary: {- total: number;- average: number;- min: number;- max: number;- count?: number;- };- context?: ConcreteSeries['context']; // Include context for drill-down- }>;- summary: {- total: number;- average: number;- min: number;- max: number;- };-};-diff --git a/packages/db/src/services/chart-sql.test.ts b/packages/db/src/services/chart-sql.test.tsdeleted file mode 100644index f9c997a7e..000000000--- a/packages/db/src/services/chart-sql.test.ts+++ /dev/null@@ -1,715 +0,0 @@-/**- * SQL-syntax tests for chart / overview query builders.- *- * Strategy: build the SQL string, then run `EXPLAIN <sql>` against the local- * ClickHouse instance. EXPLAIN parses the query, resolves columns, and builds- * the query plan without executing it — so we catch UNKNOWN_IDENTIFIER,- * AMBIGUOUS_IDENTIFIER and bad JOIN ON expressions without needing seeded- * data. WITH FILL TO < FROM is a runtime check, so it's covered by a plain- * string assertion instead.- *- * Requires `pnpm dock:up` (or any locally reachable CH at- * http://localhost:8123/openpanel). All tests auto-skip if CH is unreachable.- */-import type { IChartBreakdown, IChartEvent } from '@openpanel/validation';-import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';-import { ch } from '../clickhouse/client';-import {- getAggregateChartSql as _getAggregateChartSql,- getChartSql as _getChartSql,-} from './chart.service';-import { OverviewService } from './overview.service';--// IGetChartDataInput has display-only fields (metric, chartType, previous,-// etc.) that the SQL builders ignore. Tests only care about SQL output, so-// loosen the input type here.-const getChartSql: (input: any) => Promise<string> = _getChartSql as any;-const getAggregateChartSql: (input: any) => Promise<string> =- _getAggregateChartSql as any;--const PROJECT_ID = 'test-sql-validation';-const START = '2026-04-14 00:00:00';-const END = '2026-05-15 00:00:00';--const event = (overrides: Partial<IChartEvent> = {}): IChartEvent => ({- id: 'A',- name: 'screen_view',- segment: 'event',- filters: [],- ...overrides,-});--const breakdown = (name: string): IChartBreakdown => ({ id: name, name });--let chReachable = false;--async function explain(sql: string): Promise<void> {- // EXPLAIN runs the parser + analyzer and builds the query plan, which- // catches UNKNOWN_IDENTIFIER and AMBIGUOUS_IDENTIFIER. It does not execute.- await ch.command({ query: `EXPLAIN ${sql}` });-}--beforeAll(async () => {- // The chart service is chatty; mute log spam during the test run.- vi.spyOn(console, 'log').mockImplementation(() => {});- try {- await ch.command({ query: 'SELECT 1' });- chReachable = true;- } catch {- // CH not running locally — skip every test.- chReachable = false;- }-});--afterAll(() => {- vi.restoreAllMocks();-});--const itCH = (name: string, fn: () => Promise<void>) =>- it(name, async () => {- if (!chReachable) {- console.warn(- '[chart-sql] skipping: ClickHouse not reachable at CLICKHOUSE_URL',- );- return;- }- await fn();- });--describe('chart.service / getChartSql', () => {- itCH(- 'qualifies properties[...] with `e.` when group join is present (fixes AMBIGUOUS_IDENTIFIER)',- async () => {- const sql = await getChartSql({- event: event({- segment: 'session',- filters: [- { name: 'group.plan', operator: 'is', value: ['pro'] },- {- name: 'properties.__query.utm_source',- operator: 'is',- value: ['awn'],- },- ],- }),- breakdowns: [- breakdown('country'),- breakdown('properties.__query.utm_source'),- ],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });-- // Every map access on the events table must be aliased — the `_g` join- // also exposes a `properties` column, so the bare form is ambiguous.- expect(sql).not.toMatch(/(?<![._\w])properties\[/);- expect(sql).toContain("e.properties['__query.utm_source']");-- await explain(sql);- },- );-- itCH(- 'works without group join (qualified form is still valid when alone)',- async () => {- const sql = await getChartSql({- event: event({- filters: [- {- name: 'properties.__query.utm_source',- operator: 'is',- value: ['awn'],- },- ],- }),- breakdowns: [breakdown('properties.__query.utm_source')],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- await explain(sql);- },- );-- itCH(- 'drops the all-cohorts breakdown when the project has 0 cohorts',- async () => {- const sql = await getChartSql({- event: event({ segment: 'user' }),- breakdowns: [breakdown('cohort')],- interval: 'day',- startDate: START,- endDate: END,- // Use a project that almost certainly has no cohorts in PG.- projectId: PROJECT_ID,- timezone: 'UTC',- });-- // The all-cohorts JOIN expanded to `_uc._uc_label_X = 'Unknown'`, a- // constant predicate with no join key — CH rejects it. The fix is to- // remove the breakdown entirely when there are no cohorts.- expect(sql).not.toMatch(/_uc_label_\d+\s*=\s*'Unknown'/);- expect(sql).not.toContain('_all_cohorts');-- await explain(sql);- },- );-- itCH('skips WITH FILL when endDate < startDate', async () => {- const sql = await getChartSql({- event: event(),- breakdowns: [],- interval: 'day',- startDate: END, // inverted- endDate: START,- projectId: PROJECT_ID,- timezone: 'UTC',- });-- expect(sql).not.toContain('WITH FILL');- // Plain query without FILL should still parse.- await explain(sql);- });-- itCH('skips WITH FILL when endDate equals startDate inverted (week)', async () => {- const sql = await getChartSql({- event: event(),- breakdowns: [],- interval: 'week',- startDate: END,- endDate: START,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).not.toContain('WITH FILL');- });-- itCH('emits WITH FILL when the range is valid', async () => {- const sql = await getChartSql({- event: event(),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).toContain('WITH FILL');- await explain(sql);- });-- itCH('property metric (property_sum) with group join is unambiguous', async () => {- const sql = await getChartSql({- event: event({- segment: 'property_sum',- property: 'properties.revenue_amount',- filters: [{ name: 'group.plan', operator: 'is', value: ['pro'] }],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).toContain("e.properties['revenue_amount']");- await explain(sql);- });-- // Filter value type casting (filter.type). A date property compared with- // `gte` used to crash with `toFloat64('2019-01-01')`; declaring the type- // routes both column and value through the matching ClickHouse cast.- itCH('date-typed gte filter casts via best-effort toDate, not toFloat64', async () => {- const sql = await getChartSql({- event: event({- filters: [- {- name: 'properties.cook',- operator: 'gte',- value: ['2019-01-01'],- type: 'date',- },- ],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).toContain('toDate(parseDateTimeBestEffortOrNull');- expect(sql).not.toContain('toFloat64');- await explain(sql);- });-- itCH('number-typed gte filter casts via toFloat64OrNull', async () => {- const sql = await getChartSql({- event: event({- filters: [- {- name: 'properties.age',- operator: 'gte',- value: ['5'],- type: 'number',- },- ],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).toContain('toFloat64OrNull');- await explain(sql);- });-- itCH('untyped gte filter keeps legacy toFloat64 casting', async () => {- const sql = await getChartSql({- event: event({- filters: [- { name: 'properties.age', operator: 'gte', value: ['5'] },- ],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).toContain('toFloat64OrZero');- await explain(sql);- });-- itCH('one_event_per_user segment still parses', async () => {- const sql = await getChartSql({- event: event({ segment: 'one_event_per_user' }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- await explain(sql);- });-- // 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.- itCH(- 'normalizes camelCase filter alias (referrerName → referrer_name)',- async () => {- const sql = await getChartSql({- event: event({- filters: [- { name: 'referrerName', operator: 'is', value: ['email'] },- ],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).toContain('referrer_name');- expect(sql).not.toMatch(/(?<![._\w])referrerName/);- await explain(sql);- },- );-- itCH(- 'routes bare utm_source filter through properties map',- async () => {- const sql = await getChartSql({- event: event({- filters: [{ name: 'utm_source', operator: 'is', value: ['awn'] }],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).toContain("properties['__query.utm_source']");- // The unqualified `utm_source = …` form would fail with UNKNOWN_IDENTIFIER.- expect(sql).not.toMatch(/(?<![._\w])utm_source\s*=/);- await explain(sql);- },- );-- itCH(- 'drops unknown breakdown rather than emitting invalid identifier',- async () => {- // `temple_name` is a custom property (lives in properties map) but- // got saved as a top-level breakdown. The old code path emitted- // `SELECT temple_name as _uc_label_1 FROM events`, failing parse.- const sql = await getChartSql({- event: event(),- breakdowns: [breakdown('temple_name')],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).not.toMatch(/(?<![._\w])temple_name/);- expect(sql).not.toContain('_uc_label_1');- await explain(sql);- },- );-- itCH('drops unknown filter rather than emitting invalid identifier', async () => {- const sql = await getChartSql({- event: event({- filters: [- { name: 'totally_made_up_column', operator: 'is', value: ['x'] },- ],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).not.toMatch(/totally_made_up_column/);- await explain(sql);- });-});--describe('chart.service / getAggregateChartSql', () => {- itCH('drops all-cohorts breakdown on empty cohort project', async () => {- const sql = await getAggregateChartSql({- event: event({ segment: 'user' }),- breakdowns: [breakdown('cohort')],- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).not.toMatch(/_uc_label_\d+\s*=\s*'Unknown'/);- await explain(sql);- });-- itCH('properties + group breakdown is unambiguous', async () => {- const sql = await getAggregateChartSql({- event: event({- filters: [{ name: 'group.plan', operator: 'is', value: ['pro'] }],- }),- breakdowns: [breakdown('properties.__query.utm_source')],- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).toContain("e.properties['__query.utm_source']");- await explain(sql);- });-});--describe('overview.service / getRawWhereClause (UTM remapping)', () => {- const svc = new OverviewService(ch);-- it('rewrites utm_* to properties[__query.utm_*] for the events table', () => {- const where = svc.getRawWhereClause('events', [- { name: 'utm_source', operator: 'is', value: ['awn'] },- ]);- expect(where).toContain("properties['__query.utm_source']");- expect(where).not.toMatch(/(?<![._\w])utm_source\s*=/);- });-- it('keeps utm_* as a top-level column for the sessions table', () => {- const where = svc.getRawWhereClause('sessions', [- { name: 'utm_source', operator: 'is', value: ['awn'] },- ]);- expect(where).toMatch(/(?<![._\w])utm_source\s*=/);- expect(where).not.toContain("properties['__query.utm_source']");- });-- it('drops non-whitelisted filters', () => {- const where = svc.getRawWhereClause('events', [- { name: 'malicious_column', operator: 'is', value: ['x'] },- ]);- expect(where).toBe('');- });-- itCH('events utm_source filter parses against real events table', async () => {- const where = svc.getRawWhereClause('events', [- { name: 'utm_source', operator: 'is', value: ['awn'] },- ]);- expect(where).toBeTruthy();- await explain(- `SELECT count() FROM events WHERE project_id = '${PROJECT_ID}' AND ${where}`,- );- });-- itCH('sessions utm_source filter parses against real sessions table', async () => {- const where = svc.getRawWhereClause('sessions', [- { name: 'utm_source', operator: 'is', value: ['awn'] },- ]);- expect(where).toBeTruthy();- await explain(- `SELECT count() FROM sessions WHERE project_id = '${PROJECT_ID}' AND ${where}`,- );- });-});--describe('chart.service / profile-property narrowing', () => {- const profileFilter = {- id: 'f1',- name: 'profile.properties.plan',- operator: 'is' as const,- value: ['pro'],- };-- it('projects only the referenced keys as scalar columns in the profile CTE', async () => {- const sql = await getChartSql({- event: event({ filters: [profileFilter] }),- breakdowns: [breakdown('profile.properties.experiment')],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });-- // The CTE selects one scalar column per referenced key...- expect(sql).toContain("properties['plan'] as `profile.properties.plan`");- expect(sql).toContain(- "properties['experiment'] as `profile.properties.experiment`",- );- // ...instead of every profile's whole Map...- expect(sql).not.toContain('properties as "profile.properties"');- // ...and every ref in the query is rewritten to the scalar alias.- expect(sql).not.toContain("profile.properties['plan']");- expect(sql).not.toContain("profile.properties['experiment']");- });-- it('falls back to the full Map for wildcard refs', async () => {- const sql = await getChartSql({- event: event({- filters: [- {- id: 'f1',- name: 'profile.properties.experiments.*.name',- operator: 'is' as const,- value: ['a'],- },- ],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });-- // mapExtractKeyLike needs the whole Map, so it must stay selected.- expect(sql).toContain('properties as "profile.properties"');- });-- it('never narrows identifier-unsafe keys (backtick falls back to the Map)', async () => {- const sql = await getChartSql({- event: event({- filters: [- {- id: 'f1',- name: 'profile.properties.plan`tier',- operator: 'is' as const,- value: ['a'],- },- ],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });-- // The unsafe key keeps its original Map access against the full Map —- // it must never be embedded in a backtick-quoted alias.- expect(sql).toContain('properties as "profile.properties"');- expect(sql).not.toContain('as `profile.properties.plan`tier`');- });-- it('collects the math-metric property too', async () => {- const sql = await getChartSql({- event: event({- segment: 'property_average',- property: 'profile.properties.age',- filters: [profileFilter],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });-- // The metric's key must be narrowed alongside the filter's — otherwise- // the metric keeps reading the Map that narrowing just removed.- expect(sql).toContain("properties['age'] as `profile.properties.age`");- expect(sql).not.toContain("profile.properties['age']");- });-- it('creates the profile join for a metric-only profile property', async () => {- // No profile filter or breakdown — the metric alone must still create- // the CTE and join, or its scalar alias resolves against nothing.- const sql = await getChartSql({- event: event({- segment: 'property_average',- property: 'profile.properties.age',- filters: [],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });-- expect(sql).toContain('LEFT ANY JOIN profile ON profile.id = profile_id');- expect(sql).toContain("properties['age'] as `profile.properties.age`");- });-- itCH('metric-only profile property parses and resolves', async () => {- const sql = await getChartSql({- event: event({- segment: 'property_average',- property: 'profile.properties.age',- filters: [],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- await explain(sql);- });-- itCH('math metric on a narrowed profile property parses and resolves', async () => {- const sql = await getChartSql({- event: event({- segment: 'property_average',- property: 'profile.properties.age',- filters: [profileFilter],- }),- breakdowns: [],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- await explain(sql);- });-- itCH('narrowed chart SQL parses and resolves', async () => {- const sql = await getChartSql({- event: event({ filters: [profileFilter] }),- breakdowns: [breakdown('profile.properties.experiment')],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- await explain(sql);- });-- itCH('narrowed aggregate chart SQL parses and resolves', async () => {- const sql = await getAggregateChartSql({- event: event({ filters: [profileFilter] }),- breakdowns: [breakdown('profile.properties.experiment')],- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- });- expect(sql).not.toContain("profile.properties['plan']");- await explain(sql);- });-});--describe('chart.service / single-pass total_count', () => {- const base = {- interval: 'day',- startDate: START,- endDate: END,- projectId: PROJECT_ID,- timezone: 'UTC',- };-- it('scans events exactly once and merges uniq states globally (no breakdown)', async () => {- const sql = await getChartSql({ event: event(), breakdowns: [], ...base });- expect(sql).not.toContain('_uc AS (');- expect(sql).toContain('uniqState(profile_id) as _uc_state');- expect(sql).toContain('uniqMerge(_uc_state) OVER () as total_count');- expect(sql).toContain('* EXCEPT (_uc_state)');- // exactly one scan of the events table- expect(sql.match(/FROM events e/g)).toHaveLength(1);- });-- it('partitions the merged uniq states by breakdown labels', async () => {- const sql = await getChartSql({- event: event(),- breakdowns: [breakdown('properties.experiment')],- ...base,- });- expect(sql).not.toContain('_uc AS (');- expect(sql).not.toContain('LEFT ANY JOIN _uc');- expect(sql).toContain(- 'uniqMerge(_uc_state) OVER (PARTITION BY label_1) as total_count',- );- expect(sql.match(/FROM events e/g)).toHaveLength(1);- });-- it('keeps ORDER BY and WITH FILL outside the aggregation subquery', async () => {- const sql = await getChartSql({ event: event(), breakdowns: [], ...base });- // GROUP BY belongs to the inner scan; ORDER BY/FILL follow its closing paren- expect(sql).toMatch(/GROUP BY[^)]*\)\s*ORDER BY date ASC/);- expect(sql).toContain('WITH FILL');- });-- itCH('single-pass chart SQL parses and resolves (no breakdown)', async () => {- const sql = await getChartSql({ event: event(), breakdowns: [], ...base });- await explain(sql);- });-- itCH('single-pass chart SQL parses and resolves (breakdown + cohort + profile)', async () => {- const sql = await getChartSql({- event: event({- filters: [- {- id: 'f1',- name: 'profile.properties.plan',- operator: 'is' as const,- value: ['pro'],- },- ],- }),- breakdowns: [breakdown('properties.experiment')],- ...base,- });- await explain(sql);- });-});diff --git a/packages/db/src/services/chart.service.ts b/packages/db/src/services/chart.service.tsindex 745d496b1..633def545 100644--- a/packages/db/src/services/chart.service.ts+++ b/packages/db/src/services/chart.service.ts@@ -1,1718 +1,37 @@-/** biome-ignore-all lint/style/useDefaultSwitchClause: switch cases are exhaustive by design */-import { stripLeadingAndTrailingSlashes } from '@openpanel/common';-import {- type CohortDefinition,- getCohortIds,- type IChartBreakdown,- type IChartEventFilter,- type IGetChartDataInput,- type IReportInput,-} from '@openpanel/validation';-import sqlstring from 'sqlstring';-import { formatClickhouseDate, TABLE_NAMES } from '../clickhouse/client';-import { db } from '../prisma-client';-import { createSqlBuilder } from '../sql-builder';-import { buildTypedClause, hasTypedCast, isTypedOperator } from './filter-cast';--// Top-level columns on the events table. Derived from the migration in-// packages/db/code-migrations/3-init-ch.ts (+ revenue added in 6-add-revenue--// column.ts). Used by `resolveEventColumn` to distinguish real columns from-// property keys and reject unknown identifiers before they reach ClickHouse.-const EVENT_TOP_LEVEL_COLUMNS = new Set<string>([- 'id',- 'name',- 'sdk_name',- 'sdk_version',- 'device_id',- 'profile_id',- 'project_id',- 'session_id',- 'path',- 'origin',- 'referrer',- 'referrer_name',- 'referrer_type',- 'duration',- 'revenue',- 'created_at',- 'country',- 'city',- 'region',- 'longitude',- 'latitude',- 'os',- 'os_version',- 'browser',- 'browser_version',- 'device',- 'brand',- 'model',- 'imported_at',-]);--// Older clients / saved reports send some field names in camelCase. Map them-// to the canonical snake_case ClickHouse column so they don't fall through as-// unknown identifiers. The bare values (`utm_source` etc.) actually live in-// the `properties` map — `normalizeEventField` rewrites those into the-// `properties.__query.utm_*` form.-const EVENT_FIELD_ALIASES: Record<string, string> = {- referrerName: 'referrer_name',- referrerType: 'referrer_type',- sessionId: 'session_id',- deviceId: 'device_id',- profileId: 'profile_id',- projectId: 'project_id',- osVersion: 'os_version',- browserVersion: 'browser_version',- sdkName: 'sdk_name',- sdkVersion: 'sdk_version',- createdAt: 'created_at',- importedAt: 'imported_at',-};--const EVENT_UTM_BARE_COLUMNS = new Set<string>([- 'utm_source',- 'utm_medium',- 'utm_campaign',- 'utm_term',- 'utm_content',-]);--// Normalize an incoming field name into its canonical form. Returns a string-// suitable for `getSelectPropertyKey` / `getEventFiltersWhereClause` — i.e.-// either a top-level column name (`referrer_name`), a `properties.foo` /-// `profile.foo` / `group.foo` path, or `has_profile`. Unknown names are-// returned unchanged; callers must guard with `isKnownEventField` before-// inlining into SQL.-export function normalizeEventField(name: string): string {- if (EVENT_FIELD_ALIASES[name]) {- return EVENT_FIELD_ALIASES[name]!;- }- if (EVENT_UTM_BARE_COLUMNS.has(name)) {- return `properties.__query.${name}`;- }- return name;-}--// Returns true if `name` resolves to something we can inline into SQL safely:-// a known top-level events column, a properties / profile / group path, a-// cohort breakdown, or `has_profile`. Used to drop unknown filters/breakdowns-// instead of emitting invalid `SELECT cohort` / `SELECT temple_name` queries.-export function isKnownEventField(name: string): boolean {- if (name === 'has_profile') return true;- if (isAllCohortsBreakdown(name)) return true;- if (extractCohortId(name)) return true;- if (name.startsWith('properties.')) return true;- if (name.startsWith('profile.')) return true;- if (name.startsWith('group.')) return true;- const normalized = normalizeEventField(name);- if (normalized.startsWith('properties.')) return true;- if (EVENT_TOP_LEVEL_COLUMNS.has(normalized)) return true;- return false;-}--export type CohortMetadata = {- id: string;- name: string;-};--export async function fetchCohortsMetadata(- cohortIds: string[],-): Promise<Map<string, CohortMetadata>> {- if (cohortIds.length === 0) {- return new Map();- }-- const cohorts = await db.cohort.findMany({- where: { id: { in: cohortIds } },- select: { id: true, name: true },- });-- return new Map(- cohorts.map((c) => [c.id, { id: c.id, name: c.name }]),- );-}--export function getCohortCteName(cohortId: string): string {- return `\`cohort-${cohortId}\``;-}--export function getCohortAlias(cohortId: string): string {- return `cohort_${cohortId.replace(/-/g, '_')}`;-}--export function buildCohortMembershipQuery(- cohortId: string,- projectId: string,-): string {- return `- SELECT profile_id- FROM ${TABLE_NAMES.cohort_members} FINAL- WHERE cohort_id = ${sqlstring.escape(cohortId)}- AND project_id = ${sqlstring.escape(projectId)}- `;-}--export function buildInlineCohortJoin(- cohortId: string,- projectId: string,- tableAlias: string,-): string {- const cohortAlias = getCohortAlias(cohortId);- const cohortQuery = buildCohortMembershipQuery(cohortId, projectId);- return `LEFT ANY JOIN (${cohortQuery}) AS ${cohortAlias} ON ${cohortAlias}.profile_id = ${tableAlias}.profile_id`;-}--export function extractCohortId(breakdownName: string): string | null {- if (breakdownName.startsWith('cohort:')) {- return breakdownName.split(':')[1] ?? null;- }- return null;-}--export function isAllCohortsBreakdown(breakdownName: string): boolean {- return breakdownName === 'cohort';-}--export async function fetchProjectCohorts(- projectId: string,-): Promise<CohortMetadata[]> {- return db.cohort.findMany({- where: { projectId },- select: { id: true, name: true },- });-}--export function buildAllCohortsMembershipQuery(- projectId: string,-): string {- return `- SELECT profile_id, cohort_id- FROM ${TABLE_NAMES.cohort_members} FINAL- WHERE project_id = ${sqlstring.escape(projectId)}- `;-}--export function buildAllCohortsLabelExpr(- cohorts: CohortMetadata[],- alias = '_all_cohorts',-): string {- if (cohorts.length === 0) {- return "'Unknown'";- }- const ids = cohorts.map((c) => sqlstring.escape(c.id)).join(', ');- const names = cohorts.map((c) => sqlstring.escape(c.name)).join(', ');- return `transform(${alias}.cohort_id, [${ids}], [${names}], 'Unknown')`;-}--/**- * Cohort IDs that need a `cohort_<id>` JOIN alias to be wired up by the- * caller. After filter SQL became self-contained, only cohort *breakdowns*- * require the JOIN — they reference `cohort_<id>.profile_id` in their- * SELECT expression via `getSelectPropertyKey`.- */-export function collectBreakdownCohortIds(- breakdowns: IChartBreakdown[],-): string[] {- const ids = new Set<string>();- for (const breakdown of breakdowns) {- const id = extractCohortId(breakdown.name);- if (id) {- ids.add(id);- }- }- return Array.from(ids);-}--export function transformPropertyKey(property: string) {- const propertyPatterns = ['properties', 'profile.properties'];- const match = propertyPatterns.find((pattern) =>- property.startsWith(`${pattern}.`)- );-- if (!match) {- return property;- }-- if (property.includes('*')) {- return property- .replace(/^properties\./, '')- .replace('.*.', '.%.')- .replace(/\[\*\]$/, '.%')- .replace(/\[\*\].?/, '.%.');- }-- return `${match}['${property.replace(new RegExp(`^${match}.`), '')}']`;-}--// Returns a SQL expression for a group property via the _g JOIN alias-// property format: "group.name", "group.type", "group.properties.plan"-export function getGroupPropertySql(property: string): string {- const withoutPrefix = property.replace(/^group\./, '');- if (withoutPrefix === 'name') {- return '_g.name';- }- if (withoutPrefix === 'type') {- return '_g.type';- }- if (withoutPrefix.startsWith('properties.')) {- const propKey = withoutPrefix.replace(/^properties\./, '');- return `_g.properties[${sqlstring.escape(propKey)}]`;- }- return '_group_id';-}--// Returns the SELECT expression when querying the groups table directly (no join alias).-// Use for fetching distinct values for group.* properties.-export function getGroupPropertySelect(property: string): string {- const withoutPrefix = property.replace(/^group\./, '');- if (withoutPrefix === 'name') {- return 'name';- }- if (withoutPrefix === 'type') {- return 'type';- }- if (withoutPrefix === 'id') {- return 'id';- }- if (withoutPrefix.startsWith('properties.')) {- const propKey = withoutPrefix.replace(/^properties\./, '');- return `properties[${sqlstring.escape(propKey)}]`;- }- return 'id';-}--// Returns the SELECT expression when querying the profiles table directly (no join alias).-// Use for fetching distinct values for profile.* properties.-// Lists the same profiles columns as PROFILE_COLUMNS in filter-where.service.ts,-// which resolves profile.* on the filter side; keep the two in sync.-export function getProfilePropertySelect(property: string): string {- const withoutPrefix = property.replace(/^profile\./, '');- if (withoutPrefix === 'id') {- return 'id';- }- if (withoutPrefix === 'first_name') {- return 'first_name';- }- if (withoutPrefix === 'last_name') {- return 'last_name';- }- if (withoutPrefix === 'email') {- return 'email';- }- if (withoutPrefix === 'avatar') {- return 'avatar';- }- if (withoutPrefix === 'created_at') {- return 'created_at';- }- if (withoutPrefix === 'last_seen_at') {- return 'last_seen_at';- }- if (withoutPrefix.startsWith('properties.')) {- const propKey = withoutPrefix.replace(/^properties\./, '');- return `properties[${sqlstring.escape(propKey)}]`;- }- return 'id';-}--export function getSelectPropertyKey(- rawProperty: string,- projectId?: string,- cohortId?: string,- cohortName?: string,- /**- * When set, the events table's `properties` map is qualified with this- * alias (e.g. `e.properties[...]`). Required in any query where another- * joined table also exposes a `properties` column (such as the groups- * `_g` join), otherwise ClickHouse rejects with "ambiguous identifier".- */- eventsAlias?: string,-) {- // Map camelCase aliases (`referrerName` → `referrer_name`) and bare UTM- // names (`utm_source` → `properties.__query.utm_source`) into their- // canonical form before doing any pattern matching. The fallback at the- // bottom of this function returns `property` verbatim, so without this- // normalization an alias would leak into the generated SQL and fail with- // UNKNOWN_IDENTIFIER.- const property = normalizeEventField(rawProperty);- const extractedCohortId = cohortId || extractCohortId(property);-- if (extractedCohortId && projectId) {- const cohortAlias = getCohortAlias(extractedCohortId);- const inLabel = cohortName- ? sqlstring.escape(cohortName)- : "'In Cohort'";- const notInLabel = cohortName- ? sqlstring.escape(`Not ${cohortName}`)- : "'Not In Cohort'";- return `if(notEmpty(${cohortAlias}.profile_id), ${inLabel}, ${notInLabel})`;- }-- if (property === 'has_profile') {- return `if(profile_id != device_id, 'true', 'false')`;- }-- // Handle group properties — requires ARRAY JOIN + _g JOIN to be present in query- if (property.startsWith('group.') && projectId) {- return getGroupPropertySql(property);- }-- const propertyPatterns = ['properties', 'profile.properties'];-- const match = propertyPatterns.find((pattern) =>- property.startsWith(`${pattern}.`)- );- if (!match) {- return property;- }-- // Only the events table's bare `properties` map needs aliasing —- // `profile.properties` already routes through the profile join alias.- const aliasPrefix = match === 'properties' && eventsAlias- ? `${eventsAlias}.`- : '';-- if (property.includes('*')) {- return `arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(${aliasPrefix}${match}, ${sqlstring.escape(- transformPropertyKey(property)- )})))`;- }-- return `${aliasPrefix}${match}['${property.replace(new RegExp(`^${match}.`), '')}']`;-}---// --- profile-property CTE narrowing (perf) ----------------------------------// profile.properties.<key> refs render as Map lookups `profile.properties['<key>']`.-// Pulling the whole `properties` Map into the profile CTE makes the LEFT ANY-// JOIN hash carry the full Map per profile — roughly a kilobyte each on real-// data — and OOMs at scale. Instead we project ONLY the referenced keys as-// scalar columns in the CTE and rewrite the refs to those columns — identical-// results at a fraction of the memory. Wildcard refs (mapExtractKeyLike) still-// need the full Map, so those fall back to selecting it.--const PROFILE_PROP_PREFIX = 'profile.properties.';--export function collectProfilePropertyKeys(refs: { name: string }[]): {- keys: string[];- needsFullMap: boolean;-} {- const keys = new Set<string>();- let needsFullMap = false;- for (const { name } of refs) {- if (!name.startsWith(PROFILE_PROP_PREFIX)) {- continue;- }- // Wildcard refs render as mapExtractKeyLike over the whole Map.- if (name.includes('*')) {- needsFullMap = true;- continue;- }- const key = name.slice(PROFILE_PROP_PREFIX.length);- // A backtick or backslash in the key can't be embedded in the- // backtick-quoted scalar alias. Such keys are never narrowed: the full- // Map stays selected and their refs keep the original Map access.- if (/[`\\]/.test(key)) {- needsFullMap = true;- continue;- }- keys.add(key);- }- return { keys: Array.from(keys), needsFullMap };-}--// The profile-CTE SELECT expression for the `properties` field: one scalar-// column per referenced key, plus the full Map only when a wildcard ref needs-// it (or when nothing specific was referenced).-export function profilePropertiesCteSelect(- keys: string[],- needsFullMap: boolean,-): string {- const cols = keys.map(- (k) => `properties[${sqlstring.escape(k)}] as \`profile.properties.${k}\``,- );- if (needsFullMap || cols.length === 0) {- cols.push('properties as "profile.properties"');- }- return cols.join(', ');-}--// Rewrite `profile.properties['<key>']` -> `` `profile.properties.<key>` ``-// for the narrowed keys. Matches the raw render from getSelectPropertyKey /-// the filter builders; never matches the CTE's own `properties['<key>']`,-// which has no `profile.` prefix. No-op when keys is empty.-export function rewriteProfilePropertyRefs(sql: string, keys: string[]): string {- let out = sql;- for (const k of keys) {- out = out- .split(`profile.properties['${k}']`)- .join(`\`profile.properties.${k}\``);- }- return out;-}--export async function getChartSql({- event,- breakdowns: initialBreakdowns,- interval,- startDate,- endDate,- projectId,- timezone,-}: IGetChartDataInput & { timezone: string }) {- const {- sb,- join,- getWhere,- getFrom,- getJoins,- getSelect,- getOrderBy,- getGroupBy,- getFill,- getWith,- with: addCte,- } = createSqlBuilder();-- // Drop breakdowns whose field name doesn't resolve to a known events- // column, properties path, profile path, group path, or cohort. The chart- // service used to inline whatever the dashboard sent — saved reports with- // fields like `temple_name` (a property, not a column) reached the _uc CTE- // as `SELECT temple_name as _uc_label_1 FROM events`, failing parse.- let breakdowns = initialBreakdowns.filter((b) => isKnownEventField(b.name));- const requestedAllCohortsBreakdown = breakdowns.some((b) =>- isAllCohortsBreakdown(b.name),- );- const allCohorts = requestedAllCohortsBreakdown- ? await fetchProjectCohorts(projectId)- : [];- // Drop the all-cohorts breakdown when the project has no cohorts: the label- // expression collapses to the literal 'Unknown', making the _uc JOIN ON it- // a constant comparison with no join key (ClickHouse rejects with- // "Cannot determine join keys").- if (requestedAllCohortsBreakdown && allCohorts.length === 0) {- breakdowns = breakdowns.filter((b) => !isAllCohortsBreakdown(b.name));- }- const hasAllCohortsBreakdown =- requestedAllCohortsBreakdown && allCohorts.length > 0;-- const cohortIds = collectBreakdownCohortIds(breakdowns);- const cohortMetadata = await fetchCohortsMetadata(cohortIds);-- const profileProps = collectProfilePropertyKeys([- ...event.filters,- ...breakdowns,- // Math metrics (property_sum/avg/min/max) reference event.property too —- // missing it here would strip the Map the metric still reads from.- ...(event.property ? [{ name: event.property }] : []),- ]);-- // Add CTE + JOIN for "all cohorts" breakdown- if (hasAllCohortsBreakdown) {- addCte('_all_cohorts', buildAllCohortsMembershipQuery(projectId));- sb.joins._all_cohorts =- 'INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id';- }-- // Add individual cohort CTEs (for single-cohort filters)- for (const cohortId of cohortIds) {- addCte(- getCohortCteName(cohortId),- buildCohortMembershipQuery(cohortId, projectId),- );- sb.joins[`cohort_${cohortId}`] =- `LEFT ANY JOIN ${getCohortCteName(cohortId)} AS ${getCohortAlias(cohortId)} ON ${getCohortAlias(cohortId)}.profile_id = e.profile_id`;- }-- sb.where = getEventFiltersWhereClause(event.filters, projectId, 'e');- sb.where.projectId = `project_id = ${sqlstring.escape(projectId)}`;-- if (event.name !== '*') {- sb.select.label_0 = `${sqlstring.escape(event.name)} as label_0`;- sb.where.eventName = `e.name = ${sqlstring.escape(event.name)}`;- } else {- sb.select.label_0 = `'*' as label_0`;- }-- const anyFilterOnProfile = event.filters.some((filter) =>- filter.name.startsWith('profile.')- );- const anyBreakdownOnProfile = breakdowns.some((breakdown) =>- breakdown.name.startsWith('profile.')- );- // Math metrics (property_sum/avg/min/max) can target a profile property- // too — the join must exist for the metric alone, not only for filters- // and breakdowns.- const anyMetricOnProfile = !!event.property?.startsWith('profile.');- const anyFilterOnGroup = event.filters.some((filter) =>- filter.name.startsWith('group.')- );- const anyBreakdownOnGroup = breakdowns.some((breakdown) =>- breakdown.name.startsWith('group.')- );- const anyMetricOnGroup = !!event.property?.startsWith('group.');- const needsGroupArrayJoin =- anyFilterOnGroup ||- anyBreakdownOnGroup ||- anyMetricOnGroup ||- event.segment === 'group';-- if (needsGroupArrayJoin) {- addCte(- '_g',- `SELECT id, name, type, properties FROM ${TABLE_NAMES.groups} FINAL WHERE project_id = ${sqlstring.escape(projectId)}`- );- sb.joins.groups = 'ARRAY JOIN groups AS _group_id';- sb.joins.groups_table = 'LEFT ANY JOIN _g ON _g.id = _group_id';- }-- // Collect all profile fields used in filters and breakdowns- // Extract top-level field names (e.g., 'properties' from 'profile.properties.os')- const getProfileFields = () => {- const fields = new Set<string>();-- // Always need id for the join- fields.add('id');-- // Collect from filters- event.filters- .filter((f) => f.name.startsWith('profile.'))- .forEach((f) => {- const fieldName = f.name.replace('profile.', '').split('.')[0];- if (fieldName && fieldName === 'properties') {- fields.add('properties');- } else if (- fieldName &&- [- 'email',- 'first_name',- 'last_name',- 'created_at',- 'last_seen_at',- ].includes(fieldName)- ) {- fields.add(fieldName);- }- });-- // Collect from breakdowns- breakdowns- .filter((b) => b.name.startsWith('profile.'))- .forEach((b) => {- const fieldName = b.name.replace('profile.', '').split('.')[0];- if (fieldName && fieldName === 'properties') {- fields.add('properties');- } else if (- fieldName &&- [- 'email',- 'first_name',- 'last_name',- 'created_at',- 'last_seen_at',- ].includes(fieldName)- ) {- fields.add(fieldName);- }- });-- // Collect from the math metric- if (event.property?.startsWith('profile.')) {- const fieldName = event.property.replace('profile.', '').split('.')[0];- if (fieldName && fieldName === 'properties') {- fields.add('properties');- } else if (- fieldName &&- [- 'email',- 'first_name',- 'last_name',- 'created_at',- 'last_seen_at',- ].includes(fieldName)- ) {- fields.add(fieldName);- }- }-- return Array.from(fields);- };-- // Create profiles CTE if profiles are needed (to avoid duplicating the heavy profile join)- // Only select the fields that are actually used- const profilesJoinRef =- anyFilterOnProfile || anyBreakdownOnProfile || anyMetricOnProfile- ? 'LEFT ANY JOIN profile ON profile.id = profile_id'- : '';-- if (anyFilterOnProfile || anyBreakdownOnProfile || anyMetricOnProfile) {- const profileFields = getProfileFields();- const selectFields = profileFields.map((field) => {- if (field === 'id') {- return 'id as "profile.id"';- }- if (field === 'properties') {- return profilePropertiesCteSelect(- profileProps.keys,- profileProps.needsFullMap,- );- }- if (field === 'email') {- return 'email as "profile.email"';- }- if (field === 'first_name') {- return 'first_name as "profile.first_name"';- }- if (field === 'last_name') {- return 'last_name as "profile.last_name"';- }- if (field === 'created_at') {- return 'created_at as "profile.created_at"';- }- if (field === 'last_seen_at') {- return 'last_seen_at as "profile.last_seen_at"';- }- return field;- });-- // Add profiles CTE using the builder- addCte(- 'profile',- `SELECT ${selectFields.join(', ')}- FROM ${TABLE_NAMES.profiles} FINAL- WHERE project_id = ${sqlstring.escape(projectId)}`- );-- // Use the CTE reference in the main query- sb.joins.profiles = profilesJoinRef;- }-- sb.select.count = 'count(*) as count';- // ClickHouse rejects WITH FILL when TO < FROM, so only emit a fill clause- // for a valid range. The SELECT bucket truncation is always safe.- const hasValidFillRange =- !!startDate && !!endDate && new Date(endDate) >= new Date(startDate);- switch (interval) {- case 'minute': {- if (hasValidFillRange) {- sb.fill = `FROM toStartOfMinute(toDateTime('${startDate}')) TO toStartOfMinute(toDateTime('${endDate}')) STEP toIntervalMinute(1)`;- }- sb.select.date = 'toStartOfMinute(created_at) as date';- break;- }- case 'hour': {- if (hasValidFillRange) {- sb.fill = `FROM toStartOfHour(toDateTime('${startDate}')) TO toStartOfHour(toDateTime('${endDate}')) STEP toIntervalHour(1)`;- }- sb.select.date = 'toStartOfHour(created_at) as date';- break;- }- case 'day': {- if (hasValidFillRange) {- sb.fill = `FROM toStartOfDay(toDateTime('${startDate}')) TO toStartOfDay(toDateTime('${endDate}')) STEP toIntervalDay(1)`;- }- sb.select.date = 'toStartOfDay(created_at) as date';- break;- }- case 'week': {- if (hasValidFillRange) {- sb.fill = `FROM toStartOfWeek(toDateTime('${startDate}'), 1, '${timezone}') TO toStartOfWeek(toDateTime('${endDate}'), 1, '${timezone}') STEP toIntervalWeek(1)`;- }- sb.select.date = `toStartOfWeek(created_at, 1, '${timezone}') as date`;- break;- }- case 'month': {- if (hasValidFillRange) {- sb.fill = `FROM toStartOfMonth(toDateTime('${startDate}'), '${timezone}') TO toStartOfMonth(toDateTime('${endDate}'), '${timezone}') STEP toIntervalMonth(1)`;- }- sb.select.date = `toStartOfMonth(created_at, '${timezone}') as date`;- break;- }- }- sb.groupBy.date = 'date';- sb.orderBy.date = 'date ASC';-- if (startDate) {- sb.where.startDate = `created_at >= toDateTime('${formatClickhouseDate(startDate)}')`;- }-- if (endDate) {- sb.where.endDate = `created_at <= toDateTime('${formatClickhouseDate(endDate)}')`;- }-- breakdowns.forEach((breakdown, index) => {- // Breakdowns start at label_1 (label_0 is reserved for event name)- const key = `label_${index + 1}`;-- if (isAllCohortsBreakdown(breakdown.name)) {- sb.select[key] = `${buildAllCohortsLabelExpr(allCohorts)} as ${key}`;- } else {- const breakdownCohortId = extractCohortId(breakdown.name);- const breakdownCohortName = breakdownCohortId- ? cohortMetadata.get(breakdownCohortId)?.name- : undefined;- sb.select[key] =- `${getSelectPropertyKey(breakdown.name, projectId, breakdownCohortId ?? undefined, breakdownCohortName, 'e')} as ${key}`;- }- sb.groupBy[key] = `${key}`;- });-- if (event.segment === 'user') {- sb.select.count = 'countDistinct(profile_id) as count';- }-- if (event.segment === 'session') {- sb.select.count = 'countDistinct(session_id) as count';- }-- if (event.segment === 'group') {- sb.select.count = 'countDistinct(_group_id) as count';- }-- if (event.segment === 'user_average') {- sb.select.count =- 'COUNT(*)::float / COUNT(DISTINCT profile_id)::float as count';- }-- const mathFunction = {- property_sum: 'sum',- property_average: 'avg',- property_max: 'max',- property_min: 'min',- }[event.segment as string];-- if (mathFunction && event.property) {- const propertyKey = getSelectPropertyKey(- event.property,- undefined,- undefined,- undefined,- 'e',- );-- if (isNumericColumn(event.property)) {- sb.select.count = `${mathFunction}(${propertyKey}) as count`;- sb.where.property = `${propertyKey} IS NOT NULL`;- } else {- sb.select.count = `${mathFunction}(toFloat64OrNull(${propertyKey})) as count`;- sb.where.property = `${propertyKey} IS NOT NULL AND notEmpty(${propertyKey})`;- }- }-- if (event.segment === 'one_event_per_user') {- sb.from = `(- SELECT DISTINCT ON (profile_id) * from ${TABLE_NAMES.events} e ${getJoins()} WHERE ${join(- sb.where,- ' AND '- )}- ORDER BY profile_id, created_at DESC- ) as subQuery`;- sb.joins = {};- // Filters were already applied inside the subquery, and the outer query- // selects from `subQuery` — the `e` alias used in sb.where is no longer- // in scope, so re-emitting WHERE would produce- // "Unknown identifier `e.name`". Clear it.- sb.where = {};-- const sql = rewriteProfilePropertyRefs(- `${getWith()}${getSelect()} ${getFrom()} ${getJoins()} ${getWhere()} ${getGroupBy()} ${getOrderBy()} ${getFill()}`,- profileProps.keys,- );- console.log('-- Report --');- console.log(sql.replaceAll(/[\n\r]/g, ' '));- console.log('-- End --');- return sql;- }-- // Single-pass total_count: aggregate uniqState(profile_id) alongside the- // series in the same scan, then merge the per-group states with a window- // aggregate in an outer select — PARTITION BY the breakdown labels, or an- // empty partition for the global total. The previous shape re-ran the- // chart's entire WHERE in a second full scan (a `_uc` CTE joined back per- // label / injected as a scalar subquery), doubling every chart's read- // cost. uniq states merge losslessly, so the result matches the two-scan- // form exactly. (The old _uc scan nominally excluded a `bar` filter, but- // nothing sets sb.where.bar anymore — the exclusion was dead code.)- sb.select.uc_state = 'uniqState(profile_id) as _uc_state';-- const totalCountPartition = breakdowns- .map((_, index) => `label_${index + 1}`)- .join(', ');- const totalCountSelect = `uniqMerge(_uc_state) OVER (${totalCountPartition ? `PARTITION BY ${totalCountPartition}` : ''}) as total_count`;-- const sql = rewriteProfilePropertyRefs(- `${getWith()}SELECT * EXCEPT (_uc_state), ${totalCountSelect} FROM (${getSelect()} ${getFrom()} ${getJoins()} ${getWhere()} ${getGroupBy()}) ${getOrderBy()} ${getFill()}`,- profileProps.keys,- );- console.log('-- Report --');- console.log(sql.replaceAll(/[\n\r]/g, ' '));- console.log('-- End --');- return sql;-}--export async function getAggregateChartSql({- event,- breakdowns: initialBreakdowns,- startDate,- endDate,- projectId,- limit,-}: Omit<IGetChartDataInput, 'interval' | 'chartType'> & {- timezone: string;-}) {- const { sb, join, getJoins, with: addCte, getSql } = createSqlBuilder();-- // Drop breakdowns whose field name doesn't resolve to a known events- // column, properties path, profile path, group path, or cohort. The chart- // service used to inline whatever the dashboard sent — saved reports with- // fields like `temple_name` (a property, not a column) reached the _uc CTE- // as `SELECT temple_name as _uc_label_1 FROM events`, failing parse.- let breakdowns = initialBreakdowns.filter((b) => isKnownEventField(b.name));- const requestedAllCohortsBreakdown = breakdowns.some((b) =>- isAllCohortsBreakdown(b.name),- );- const allCohorts = requestedAllCohortsBreakdown- ? await fetchProjectCohorts(projectId)- : [];- // See getChartSql for rationale.- if (requestedAllCohortsBreakdown && allCohorts.length === 0) {- breakdowns = breakdowns.filter((b) => !isAllCohortsBreakdown(b.name));- }- const hasAllCohortsBreakdown =- requestedAllCohortsBreakdown && allCohorts.length > 0;-- const cohortIds = collectBreakdownCohortIds(breakdowns);- const cohortMetadata = await fetchCohortsMetadata(cohortIds);-- const profileProps = collectProfilePropertyKeys([- ...event.filters,- ...breakdowns,- // Math metrics (property_sum/avg/min/max) reference event.property too —- // missing it here would strip the Map the metric still reads from.- ...(event.property ? [{ name: event.property }] : []),- ]);-- // Add CTE + JOIN for "all cohorts" breakdown- if (hasAllCohortsBreakdown) {- addCte('_all_cohorts', buildAllCohortsMembershipQuery(projectId));- sb.joins._all_cohorts =- 'INNER JOIN _all_cohorts ON _all_cohorts.profile_id = e.profile_id';- }-- // Add individual cohort CTEs (for single-cohort filters)- for (const cohortId of cohortIds) {- addCte(- getCohortCteName(cohortId),- buildCohortMembershipQuery(cohortId, projectId),- );- sb.joins[`cohort_${cohortId}`] =- `LEFT ANY JOIN ${getCohortCteName(cohortId)} AS ${getCohortAlias(cohortId)} ON ${getCohortAlias(cohortId)}.profile_id = e.profile_id`;- }-- sb.where = getEventFiltersWhereClause(event.filters, projectId, 'e');- sb.where.projectId = `project_id = ${sqlstring.escape(projectId)}`;-- if (event.name !== '*') {- sb.select.label_0 = `${sqlstring.escape(event.name)} as label_0`;- sb.where.eventName = `e.name = ${sqlstring.escape(event.name)}`;- } else {- sb.select.label_0 = `'*' as label_0`;- }-- const anyFilterOnProfile = event.filters.some((filter) =>- filter.name.startsWith('profile.')- );- const anyBreakdownOnProfile = breakdowns.some((breakdown) =>- breakdown.name.startsWith('profile.')- );- // Math metrics (property_sum/avg/min/max) can target a profile property- // too — the join must exist for the metric alone, not only for filters- // and breakdowns.- const anyMetricOnProfile = !!event.property?.startsWith('profile.');- const anyFilterOnGroup = event.filters.some((filter) =>- filter.name.startsWith('group.')- );- const anyBreakdownOnGroup = breakdowns.some((breakdown) =>- breakdown.name.startsWith('group.')- );- const anyMetricOnGroup = !!event.property?.startsWith('group.');- const needsGroupArrayJoin =- anyFilterOnGroup ||- anyBreakdownOnGroup ||- anyMetricOnGroup ||- event.segment === 'group';-- if (needsGroupArrayJoin) {- addCte(- '_g',- `SELECT id, name, type, properties FROM ${TABLE_NAMES.groups} FINAL WHERE project_id = ${sqlstring.escape(projectId)}`- );- sb.joins.groups = 'ARRAY JOIN groups AS _group_id';- sb.joins.groups_table = 'LEFT ANY JOIN _g ON _g.id = _group_id';- }-- // Collect all profile fields used in filters and breakdowns- const getProfileFields = () => {- const fields = new Set<string>();-- // Always need id for the join- fields.add('id');-- // Collect from filters- event.filters- .filter((f) => f.name.startsWith('profile.'))- .forEach((f) => {- const fieldName = f.name.replace('profile.', '').split('.')[0];- if (fieldName && fieldName === 'properties') {- fields.add('properties');- } else if (- fieldName &&- [- 'email',- 'first_name',- 'last_name',- 'created_at',- 'last_seen_at',- ].includes(fieldName)- ) {- fields.add(fieldName);- }- });-- // Collect from breakdowns- breakdowns- .filter((b) => b.name.startsWith('profile.'))- .forEach((b) => {- const fieldName = b.name.replace('profile.', '').split('.')[0];- if (fieldName && fieldName === 'properties') {- fields.add('properties');- } else if (- fieldName &&- [- 'email',- 'first_name',- 'last_name',- 'created_at',- 'last_seen_at',- ].includes(fieldName)- ) {- fields.add(fieldName);- }- });-- // Collect from the math metric- if (event.property?.startsWith('profile.')) {- const fieldName = event.property.replace('profile.', '').split('.')[0];- if (fieldName && fieldName === 'properties') {- fields.add('properties');- } else if (- fieldName &&- [- 'email',- 'first_name',- 'last_name',- 'created_at',- 'last_seen_at',- ].includes(fieldName)- ) {- fields.add(fieldName);- }- }-- return Array.from(fields);- };-- // Create profiles CTE if profiles are needed- const profilesJoinRef =- anyFilterOnProfile || anyBreakdownOnProfile || anyMetricOnProfile- ? 'LEFT ANY JOIN profile ON profile.id = profile_id'- : '';-- if (anyFilterOnProfile || anyBreakdownOnProfile || anyMetricOnProfile) {- const profileFields = getProfileFields();- const selectFields = profileFields.map((field) => {- if (field === 'id') {- return 'id as "profile.id"';- }- if (field === 'properties') {- return profilePropertiesCteSelect(- profileProps.keys,- profileProps.needsFullMap,- );- }- if (field === 'email') {- return 'email as "profile.email"';- }- if (field === 'first_name') {- return 'first_name as "profile.first_name"';- }- if (field === 'last_name') {- return 'last_name as "profile.last_name"';- }- if (field === 'created_at') {- return 'created_at as "profile.created_at"';- }- if (field === 'last_seen_at') {- return 'last_seen_at as "profile.last_seen_at"';- }- return field;- });-- addCte(- 'profile',- `SELECT ${selectFields.join(', ')}- FROM ${TABLE_NAMES.profiles} FINAL- WHERE project_id = ${sqlstring.escape(projectId)}`- );-- sb.joins.profiles = profilesJoinRef;- }-- // Date range filters- if (startDate) {- sb.where.startDate = `created_at >= toDateTime('${formatClickhouseDate(startDate)}')`;- }-- if (endDate) {- sb.where.endDate = `created_at <= toDateTime('${formatClickhouseDate(endDate)}')`;- }-- // Add a constant date field for aggregate charts (groupByLabels expects it)- // Use startDate as the date value since we're aggregating across the entire range- sb.select.date = `${sqlstring.escape(startDate)} as date`;-- // Add breakdowns to SELECT and GROUP BY- breakdowns.forEach((breakdown, index) => {- // Breakdowns start at label_1 (label_0 is reserved for event name)- const key = `label_${index + 1}`;-- if (isAllCohortsBreakdown(breakdown.name)) {- sb.select[key] = `${buildAllCohortsLabelExpr(allCohorts)} as ${key}`;- } else {- const breakdownCohortId = extractCohortId(breakdown.name);- const breakdownCohortName = breakdownCohortId- ? cohortMetadata.get(breakdownCohortId)?.name- : undefined;- sb.select[key] =- `${getSelectPropertyKey(breakdown.name, projectId, breakdownCohortId ?? undefined, breakdownCohortName, 'e')} as ${key}`;- }- sb.groupBy[key] = `${key}`;- });-- // Always group by label_0 (event name) for aggregate charts- sb.groupBy.label_0 = 'label_0';-- // Default count aggregation- sb.select.count = 'count(*) as count';-- // Handle different segments- if (event.segment === 'user') {- sb.select.count = 'countDistinct(profile_id) as count';- }-- if (event.segment === 'session') {- sb.select.count = 'countDistinct(session_id) as count';- }-- if (event.segment === 'group') {- sb.select.count = 'countDistinct(_group_id) as count';- }-- if (event.segment === 'user_average') {- sb.select.count =- 'COUNT(*)::float / COUNT(DISTINCT profile_id)::float as count';- }-- const mathFunction = {- property_sum: 'sum',- property_average: 'avg',- property_max: 'max',- property_min: 'min',- }[event.segment as string];-- if (mathFunction && event.property) {- const propertyKey = getSelectPropertyKey(- event.property,- projectId,- undefined,- undefined,- 'e',- );-- if (isNumericColumn(event.property)) {- sb.select.count = `${mathFunction}(${propertyKey}) as count`;- sb.where.property = `${propertyKey} IS NOT NULL`;- } else {- sb.select.count = `${mathFunction}(toFloat64OrNull(${propertyKey})) as count`;- sb.where.property = `${propertyKey} IS NOT NULL AND notEmpty(${propertyKey})`;- }- }-- if (event.segment === 'one_event_per_user') {- sb.from = `(- SELECT DISTINCT ON (profile_id) * from ${TABLE_NAMES.events} e ${getJoins()} WHERE ${join(- sb.where,- ' AND '- )}- ORDER BY profile_id, created_at DESC- ) as subQuery`;- sb.joins = {};-- const sql = rewriteProfilePropertyRefs(getSql(), profileProps.keys);- console.log('-- Aggregate Chart --');- console.log(sql.replaceAll(/[\n\r]/g, ' '));- console.log('-- End --');- return sql;- }-- // Order by count DESC (biggest first) for aggregate charts- sb.orderBy.count = 'count DESC';-- // Apply limit if specified- if (limit) {- sb.limit = limit;- }-- const sql = rewriteProfilePropertyRefs(getSql(), profileProps.keys);- console.log('-- Aggregate Chart --');- console.log(sql.replaceAll(/[\n\r]/g, ' '));- console.log('-- End --');- return sql;-}--function isNumericColumn(columnName: string): boolean {- const numericColumns = ['duration', 'revenue', 'longitude', 'latitude'];- return numericColumns.includes(columnName);-}--export function getEventFiltersWhereClause(- filters: IChartEventFilter[],- projectId?: string,- /**- * See `getSelectPropertyKey`. When the surrounding query joins another- * table that has a `properties` column (e.g. the `_g` groups join), the- * events table must be aliased and passed here so we can emit- * `e.properties[...]` instead of the ambiguous `properties[...]`.- */- eventsAlias?: string,- /**- * Which physical table the WHERE clause is being built for. Affects which- * names count as "top-level columns" and whether bare `utm_*` gets routed- * into the events-specific `properties.__query.utm_*` map. Defaults to- * 'events' because that's where the vast majority of callers (chart,- * funnel, conversion, sankey, event services) target — OverviewService- * sets it to 'sessions' when querying the sessions table.- */- tableScope: 'events' | 'sessions' = 'events',-) {- const where: Record<string, string> = {};- filters.forEach((filter, index) => {- const id = `f${index}`;- const { value, operator } = filter;- // Normalize camelCase aliases (`referrerName` → `referrer_name`) on both- // tables — both events and sessions schemas use snake_case. The bare-- // utm rewrite only applies to events because sessions stores utm_* as- // real top-level columns; doing it for sessions would emit- // `properties['__query.utm_source']` against a table that has no- // `properties` column.- const name =- tableScope === 'sessions'- ? EVENT_FIELD_ALIASES[filter.name] ?? filter.name- : normalizeEventField(filter.name);-- if (- (operator === 'inCohort' || operator === 'notInCohort') &&- projectId- ) {- // Self-contained membership subselect — no caller JOIN wiring needed.- // Cohort filters and cohort breakdowns are decoupled: the breakdown- // path (getSelectPropertyKey) still uses a JOIN alias for SELECT- // expressions, but filters never depend on it.- const cohortIds = getCohortIds(filter);- if (cohortIds.length === 0) return;- const profileIdExpr = eventsAlias- ? `${eventsAlias}.profile_id`- : 'profile_id';- const op = operator === 'notInCohort' ? 'NOT IN' : 'IN';- const escapedIds = cohortIds- .map((c) => sqlstring.escape(c))- .join(', ');- where[id] = `${profileIdExpr} ${op} (SELECT profile_id FROM ${TABLE_NAMES.cohort_members} FINAL WHERE cohort_id IN (${escapedIds}) AND project_id = ${sqlstring.escape(projectId)})`;- return;- }-- if (- value.length === 0 &&- operator !== 'isNull' &&- operator !== 'isNotNull'- ) {- return;- }-- if (name === 'has_profile') {- if (value.includes('true')) {- where[id] = 'profile_id != device_id';- } else {- where[id] = 'profile_id = device_id';- }- return;- }-- // Handle group. prefixed filters (requires ARRAY JOIN + _g JOIN in query)- if (name.startsWith('group.') && projectId) {- const whereFrom = getGroupPropertySql(name);- if (hasTypedCast(filter.type) && isTypedOperator(operator)) {- where[id] = buildTypedClause(whereFrom, operator, value, filter.type);- return;- }- switch (operator) {- case 'is': {- if (value.length === 1) {- where[id] =- `${whereFrom} = ${sqlstring.escape(String(value[0]).trim())}`;- } else {- where[id] =- `${whereFrom} IN (${value.map((val) => sqlstring.escape(String(val).trim())).join(', ')})`;- }- break;- }- case 'isNot': {- if (value.length === 1) {- where[id] =- `${whereFrom} != ${sqlstring.escape(String(value[0]).trim())}`;- } else {- where[id] =- `${whereFrom} NOT IN (${value.map((val) => sqlstring.escape(String(val).trim())).join(', ')})`;- }- break;- }- case 'contains': {- where[id] =- `(${value.map((val) => `${whereFrom} LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`).join(' OR ')})`;- break;- }- case 'doesNotContain': {- where[id] =- `(${value.map((val) => `${whereFrom} NOT LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`).join(' OR ')})`;- break;- }- case 'startsWith': {- where[id] =- `(${value.map((val) => `${whereFrom} LIKE ${sqlstring.escape(`${String(val).trim()}%`)}`).join(' OR ')})`;- break;- }- case 'endsWith': {- where[id] =- `(${value.map((val) => `${whereFrom} LIKE ${sqlstring.escape(`%${String(val).trim()}`)}`).join(' OR ')})`;- break;- }- case 'isNull': {- where[id] = `(${whereFrom} = '' OR ${whereFrom} IS NULL)`;- break;- }- case 'isNotNull': {- where[id] = `(${whereFrom} != '' AND ${whereFrom} IS NOT NULL)`;- break;- }- case 'regex': {- where[id] =- `(${value.map((val) => `match(${whereFrom}, ${sqlstring.escape(String(val).trim())})`).join(' OR ')})`;- break;- }- }- return;- }-- if (- name.startsWith('properties.') ||- name.startsWith('profile.properties.')- ) {- const propertyKey = getSelectPropertyKey(- name,- undefined,- undefined,- undefined,- eventsAlias,- );- const isWildcard = propertyKey.includes('%');- const whereFrom = propertyKey;-- // Typed cast (number/date/datetime/boolean) short-circuit. Casts both the- // column and each value so e.g. `>= '2019-01-01'` compares as dates- // instead of crashing `toFloat64('2019-01-01')`. Untyped/string filters- // fall through to the legacy switch below.- if (hasTypedCast(filter.type) && isTypedOperator(operator)) {- where[id] = isWildcard- ? `arrayExists(x -> ${buildTypedClause('x', operator, value, filter.type)}, ${whereFrom})`- : buildTypedClause(whereFrom, operator, value, filter.type);- return;- }-- switch (operator) {- case 'is': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map((val) => `x = ${sqlstring.escape(String(val).trim())}`)- .join(' OR ')}, ${whereFrom})`;- } else if (value.length === 1) {- where[id] =- `${whereFrom} = ${sqlstring.escape(String(value[0]).trim())}`;- } else {- where[id] = `${whereFrom} IN (${value- .map((val) => sqlstring.escape(String(val).trim()))- .join(', ')})`;- }- break;- }- case 'isNot': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map((val) => `x != ${sqlstring.escape(String(val).trim())}`)- .join(' OR ')}, ${whereFrom})`;- } else if (value.length === 1) {- where[id] =- `${whereFrom} != ${sqlstring.escape(String(value[0]).trim())}`;- } else {- where[id] = `${whereFrom} NOT IN (${value- .map((val) => sqlstring.escape(String(val).trim()))- .join(', ')})`;- }- break;- }- case 'contains': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map(- (val) => `x LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`- )- .join(' OR ')}, ${whereFrom})`;- } else {- where[id] = `(${value- .map(- (val) =>- `${whereFrom} LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`- )- .join(' OR ')})`;- }- break;- }- case 'doesNotContain': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map(- (val) =>- `x NOT LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`- )- .join(' OR ')}, ${whereFrom})`;- } else {- where[id] = `(${value- .map(- (val) =>- `${whereFrom} NOT LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`- )- .join(' OR ')})`;- }- break;- }- case 'startsWith': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map(- (val) => `x LIKE ${sqlstring.escape(`${String(val).trim()}%`)}`- )- .join(' OR ')}, ${whereFrom})`;- } else {- where[id] = `(${value- .map(- (val) =>- `${whereFrom} LIKE ${sqlstring.escape(`${String(val).trim()}%`)}`- )- .join(' OR ')})`;- }- break;- }- case 'endsWith': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map(- (val) => `x LIKE ${sqlstring.escape(`%${String(val).trim()}`)}`- )- .join(' OR ')}, ${whereFrom})`;- } else {- where[id] = `(${value- .map(- (val) =>- `${whereFrom} LIKE ${sqlstring.escape(`%${String(val).trim()}`)}`- )- .join(' OR ')})`;- }- break;- }- case 'regex': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map((val) => `match(x, ${sqlstring.escape(String(val).trim())})`)- .join(' OR ')}, ${whereFrom})`;- } else {- where[id] = `(${value- .map(- (val) =>- `match(${whereFrom}, ${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')})`;- }- break;- }- case 'isNull': {- if (isWildcard) {- where[id] = `arrayExists(x -> x = '' OR x IS NULL, ${whereFrom})`;- } else {- where[id] = `(${whereFrom} = '' OR ${whereFrom} IS NULL)`;- }- break;- }- case 'isNotNull': {- if (isWildcard) {- where[id] =- `arrayExists(x -> x != '' AND x IS NOT NULL, ${whereFrom})`;- } else {- where[id] = `(${whereFrom} != '' AND ${whereFrom} IS NOT NULL)`;- }- break;- }- case 'gt': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map(- (val) =>- `toFloat64OrZero(x) > toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')}, ${whereFrom})`;- } else {- where[id] = `(${value- .map(- (val) =>- `toFloat64OrZero(${whereFrom}) > toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')})`;- }- break;- }- case 'lt': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map(- (val) =>- `toFloat64OrZero(x) < toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')}, ${whereFrom})`;- } else {- where[id] = `(${value- .map(- (val) =>- `toFloat64OrZero(${whereFrom}) < toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')})`;- }- break;- }- case 'gte': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map(- (val) =>- `toFloat64OrZero(x) >= toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')}, ${whereFrom})`;- } else {- where[id] = `(${value- .map(- (val) =>- `toFloat64OrZero(${whereFrom}) >= toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')})`;- }- break;- }- case 'lte': {- if (isWildcard) {- where[id] = `arrayExists(x -> ${value- .map(- (val) =>- `toFloat64OrZero(x) <= toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')}, ${whereFrom})`;- } else {- where[id] = `(${value- .map(- (val) =>- `toFloat64OrZero(${whereFrom}) <= toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')})`;- }- break;- }- }- } else {- // Bare-column branch. For events queries: enforce that `name` is one- // of the known top-level columns (anything else would crash parse with- // UNKNOWN_IDENTIFIER). For sessions queries: skip the guard because- // the sessions table has its own column set (utm_*, entry_path, etc.)- // that OverviewService.getRawWhereClause already vets via its- // WHITELISTED_FILTERS pre-pass.- if (tableScope === 'events' && !EVENT_TOP_LEVEL_COLUMNS.has(name)) {- return;- }- // Typed cast short-circuit (see property branch above). Supersedes the- // `isNumericColumn` auto-detect when the user declared an explicit type.- if (hasTypedCast(filter.type) && isTypedOperator(operator)) {- where[id] = buildTypedClause(name, operator, value, filter.type);- return;- }- switch (operator) {- case 'is': {- if (value.length === 1) {- where[id] =- `${name} = ${sqlstring.escape(String(value[0]).trim())}`;- } else {- where[id] = `${name} IN (${value- .map((val) => sqlstring.escape(String(val).trim()))- .join(', ')})`;- }- break;- }- case 'isNull': {- where[id] = `(${name} = '' OR ${name} IS NULL)`;- break;- }- case 'isNotNull': {- where[id] = `(${name} != '' AND ${name} IS NOT NULL)`;- break;- }- case 'isNot': {- if (value.length === 1) {- where[id] =- `${name} != ${sqlstring.escape(String(value[0]).trim())}`;- } else {- where[id] = `${name} NOT IN (${value- .map((val) => sqlstring.escape(String(val).trim()))- .join(', ')})`;- }- break;- }- case 'contains': {- where[id] = `(${value- .map(- (val) =>- `${name} LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`- )- .join(' OR ')})`;- break;- }- case 'doesNotContain': {- where[id] = `(${value- .map(- (val) =>- `${name} NOT LIKE ${sqlstring.escape(`%${String(val).trim()}%`)}`- )- .join(' OR ')})`;- break;- }- case 'startsWith': {- where[id] = `(${value- .map(- (val) =>- `${name} LIKE ${sqlstring.escape(`${String(val).trim()}%`)}`- )- .join(' OR ')})`;- break;- }- case 'endsWith': {- where[id] = `(${value- .map(- (val) =>- `${name} LIKE ${sqlstring.escape(`%${String(val).trim()}`)}`- )- .join(' OR ')})`;- break;- }- case 'regex': {- where[id] = `(${value- .map(- (val) =>- `match(${name}, ${sqlstring.escape(stripLeadingAndTrailingSlashes(String(val)).trim())})`- )- .join(' OR ')})`;- break;- }- case 'gt': {- if (isNumericColumn(name)) {- where[id] = `(${value- .map(- (val) =>- `toFloat64(${name}) > toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')})`;- } else {- where[id] = `(${value- .map((val) => `${name} > ${sqlstring.escape(String(val).trim())}`)- .join(' OR ')})`;- }- break;- }- case 'lt': {- if (isNumericColumn(name)) {- where[id] = `(${value- .map(- (val) =>- `toFloat64(${name}) < toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')})`;- } else {- where[id] = `(${value- .map((val) => `${name} < ${sqlstring.escape(String(val).trim())}`)- .join(' OR ')})`;- }- break;- }- case 'gte': {- if (isNumericColumn(name)) {- where[id] = `(${value- .map(- (val) =>- `toFloat64(${name}) >= toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')})`;- } else {- where[id] = `(${value- .map(- (val) => `${name} >= ${sqlstring.escape(String(val).trim())}`- )- .join(' OR ')})`;- }- break;- }- case 'lte': {- if (isNumericColumn(name)) {- where[id] = `(${value- .map(- (val) =>- `toFloat64(${name}) <= toFloat64(${sqlstring.escape(String(val).trim())})`- )- .join(' OR ')})`;- } else {- where[id] = `(${value- .map(- (val) => `${name} <= ${sqlstring.escape(String(val).trim())}`- )- .join(' OR ')})`;- }- break;- }- }- }- });-- return where;-}+// The chart service — field resolution, the event filter compiler, the chart+// statements and the engine — lives in @openpanel/core now (M7-003).+// Re-exported here for existing `@openpanel/db` importers (packages/trpc's+// chart router, funnel/conversion/sankey/retention/overview services) — same+// shape as packages/db/src/services/organization.service.ts since M6-001.+export type {+ AggregateChartSqlInput,+ ChartSqlInput,+ CohortMetadata,+ FilterTableScope,+} from '@openpanel/core';+export {+ buildAllCohortsLabelExpr,+ buildAllCohortsMembershipQuery,+ buildCohortMembershipQuery,+ buildInlineCohortJoin,+ collectBreakdownCohortIds,+ collectProfilePropertyKeys,+ extractCohortId,+ fetchCohortsMetadata,+ fetchProjectCohorts,+ getAggregateChartSql,+ getChartSql,+ getCohortAlias,+ getCohortCteName,+ getEventFiltersWhereClause,+ getGroupPropertySelect,+ getGroupPropertySql,+ getProfilePropertySelect,+ getSelectPropertyKey,+ isAllCohortsBreakdown,+ isKnownEventField,+ normalizeEventField,+ profilePropertiesCteSelect,+ rewriteProfilePropertyRefs,+ transformPropertyKey,+} from '@openpanel/core';diff --git a/packages/db/src/services/overview-sql.test.ts b/packages/db/src/services/overview-sql.test.tsnew file mode 100644index 000000000..cbafce004--- /dev/null+++ b/packages/db/src/services/overview-sql.test.ts@@ -0,0 +1,108 @@+/**+ * SQL-syntax tests for the overview query builder. The chart cases moved to+ * packages/core/src/modules/chart/src/chart.sql.test.ts with their subject+ * (M7-003).+ *+ * Strategy: build the SQL string, then run `EXPLAIN <sql>` against the local+ * ClickHouse instance. EXPLAIN parses the query, resolves columns, and builds+ * the query plan without executing it — so we catch UNKNOWN_IDENTIFIER,+ * AMBIGUOUS_IDENTIFIER and bad JOIN ON expressions without needing seeded+ * data. WITH FILL TO < FROM is a runtime check, so it's covered by a plain+ * string assertion instead.+ *+ * Requires `pnpm dock:up` (or any locally reachable CH at+ * http://localhost:8123/openpanel). All tests auto-skip if CH is unreachable.+ */+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';+import { ch } from '../clickhouse/client';+import { OverviewService } from './overview.service';++const PROJECT_ID = 'test-sql-validation';++let chReachable = false;++async function explain(sql: string): Promise<void> {+ // EXPLAIN runs the parser + analyzer and builds the query plan, which+ // catches UNKNOWN_IDENTIFIER and AMBIGUOUS_IDENTIFIER. It does not execute.+ await ch.command({ query: `EXPLAIN ${sql}` });+}++beforeAll(async () => {+ // The chart service is chatty; mute log spam during the test run.+ vi.spyOn(console, 'log').mockImplementation(() => {});+ try {+ await ch.command({ query: 'SELECT 1' });+ chReachable = true;+ } catch {+ // CH not running locally — skip every test.+ chReachable = false;+ }+});++afterAll(() => {+ vi.restoreAllMocks();+});++const itCH = (name: string, fn: () => Promise<void>) =>+ it(name, async () => {+ if (!chReachable) {+ console.warn(+ '[overview-sql] skipping: ClickHouse not reachable at CLICKHOUSE_URL'+ );+ return;+ }+ await fn();+ });++describe('overview.service / getRawWhereClause (UTM remapping)', () => {+ const svc = new OverviewService(ch);++ it('rewrites utm_* to properties[__query.utm_*] for the events table', () => {+ const where = svc.getRawWhereClause('events', [+ { name: 'utm_source', operator: 'is', value: ['awn'] },+ ]);+ expect(where).toContain("properties['__query.utm_source']");+ expect(where).not.toMatch(/(?<![._\w])utm_source\s*=/);+ });++ it('keeps utm_* as a top-level column for the sessions table', () => {+ const where = svc.getRawWhereClause('sessions', [+ { name: 'utm_source', operator: 'is', value: ['awn'] },+ ]);+ expect(where).toMatch(/(?<![._\w])utm_source\s*=/);+ expect(where).not.toContain("properties['__query.utm_source']");+ });++ it('drops non-whitelisted filters', () => {+ const where = svc.getRawWhereClause('events', [+ { name: 'malicious_column', operator: 'is', value: ['x'] },+ ]);+ expect(where).toBe('');+ });++ itCH(+ 'events utm_source filter parses against real events table',+ async () => {+ const where = svc.getRawWhereClause('events', [+ { name: 'utm_source', operator: 'is', value: ['awn'] },+ ]);+ expect(where).toBeTruthy();+ await explain(+ `SELECT count() FROM events WHERE project_id = '${PROJECT_ID}' AND ${where}`+ );+ }+ );++ itCH(+ 'sessions utm_source filter parses against real sessions table',+ async () => {+ const where = svc.getRawWhereClause('sessions', [+ { name: 'utm_source', operator: 'is', value: ['awn'] },+ ]);+ expect(where).toBeTruthy();+ await explain(+ `SELECT count() FROM sessions WHERE project_id = '${PROJECT_ID}' AND ${where}`+ );+ }+ );+});diff --git a/packages/trpc/src/routers/chart.ts b/packages/trpc/src/routers/chart.tsindex aea2980cc..435078577 100644--- a/packages/trpc/src/routers/chart.ts+++ b/packages/trpc/src/routers/chart.ts@@ -1,37 +1,27 @@+// Dissolved into @openpanel/core's chart module (M7-003): the ClickHouse+// queries moved to packages/core/src/modules/chart/src/chart.sql.ts, the+// engine to src/engine/ and the handler bodies to chart.service.ts. This+// router stays (DELEGATE PATTERN) — it keeps V1's protectedProcedure /+// chartProcedure (share) / cacheMiddleware stacks and delegates every handler+// body to core's chart functions, same as event.ts.+import {- AggregateChartEngine,- ChartEngine,- ch,- chQuery,- clix,- conversionService,- createSqlBuilder,- EMPTY_BREAKDOWN_LABEL,- formatClickhouseDate,- funnelService,- getChartPrevStartEndDate,- getChartStartEndDate,- getEventFiltersWhereClause,- getEventMetasCached,- getGroupPropertySelect,- getProfilePropertyKeysCached,- getProfilePropertySelect,- getProfilesCached,- getReportById,- getRetentionCohort,- getSelectPropertyKey,- getSettingsForProject,- type IServiceProfile,- isKnownEventField,- mergeGlobalFilters,- normalizeEventField,- onlyReportEvents,- sankeyService,- TABLE_NAMES,- validateShareAccess,-} from '@openpanel/db';+ executeAggregateChart,+ executeChart,+ getChartBucketProfiles,+ getChartPropertyValues,+ getConversionChart,+ getFunnelChart,+ getFunnelStepProfiles,+ getProjectCard,+ getRetentionChart,+ getSankeyChart,+ listChartEvents,+ listChartProperties,+ resolveReportInput,+} from '@openpanel/core';+import { getReportById, validateShareAccess } from '@openpanel/db';import {- type IChartEvent,zChartEventFilter,zChartSeries,zCriteria,@@ -39,8 +29,6 @@ import {zReportInput,zTimeInterval,} from '@openpanel/validation';-import { flatten, map, pipe, prop, sort, uniq } from 'ramda';-import sqlstring from 'sqlstring';import { z } from 'zod';import { getProjectAccess } from '../access';import { TRPCAccessError, TRPCForbiddenError } from '../errors';@@ -53,32 +41,12 @@ import {const cacher = cacheMiddleware(60);-/**- * Cap on distinct event property keys returned to the picker. Projects in the- * 8k range exist, so the previous 10k was reachable in normal use.- */-const EVENT_PROPERTY_KEY_LIMIT = 50_000;+const zShareable = z.object({+ shareId: z.string().optional(),+ id: z.string().optional(),+});-/**- * Cap on distinct values returned per event property to the filter- * autocomplete. High-cardinality keys (ids, urls, session tokens) can hold- * millions of distinct values — returning them all is useless for a picker- * and heavy for ClickHouse and the browser alike. Most recent values win.- * Env-tunable via EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT (positive- * integer; invalid values keep the default).- */-const EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT_RAW =- process.env.EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT;-const EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT_PARSED =- EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT_RAW &&- /^\d+$/.test(EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT_RAW)- ? Number(EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT_RAW)- : Number.NaN;-const EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT =- Number.isSafeInteger(EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT_PARSED) &&- EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT_PARSED > 0- ? EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT_PARSED- : 500;+const zShareableReportInput = zReportInput.and(zShareable);const chartProcedure = publicProcedure.use(async ({ ctx, next, getRawInput }) => {@@ -150,80 +118,7 @@ export const chartRouter = createTRPCRouter({projectId: z.string(),}))- .query(async ({ input: { projectId } }) => {- const { timezone } = await getSettingsForProject(projectId);- const chartPromise = chQuery<{- value: number;- date: Date;- revenue: number;- }>(- `SELECT- uniqHLL12(profile_id) as value,- toStartOfDay(created_at) as date,- sum(revenue * sign) as revenue- FROM ${TABLE_NAMES.sessions}- WHERE- project_id = ${sqlstring.escape(projectId)} AND- created_at >= now() - interval '3 month'- GROUP BY date- ORDER BY date ASC- WITH FILL FROM toStartOfDay(now() - interval '3 month')- TO toStartOfDay(now())- STEP INTERVAL 1 day- SETTINGS session_timezone = '${timezone}'- `- );-- const metricsPromise = clix(ch, timezone)- .select<{- months_3: number;- months_3_prev: number;- month: number;- day: number;- day_prev: number;- revenue: number;- }>([- 'uniqHLL12(if(created_at >= (now() - toIntervalMonth(3)), profile_id, null)) AS months_3',- 'uniqHLL12(if(created_at >= (now() - toIntervalMonth(6)) AND created_at < (now() - toIntervalMonth(3)), profile_id, null)) AS months_3_prev',- 'uniqHLL12(if(created_at >= (now() - toIntervalMonth(1)), profile_id, null)) AS month',- 'uniqHLL12(if(created_at >= (now() - toIntervalDay(1)), profile_id, null)) AS day',- 'uniqHLL12(if(created_at >= (now() - toIntervalDay(2)) AND created_at < (now() - toIntervalDay(1)), profile_id, null)) AS day_prev',- 'sum(revenue * sign) as revenue',- ])- .from(TABLE_NAMES.sessions)- .where('project_id', '=', projectId)- .where('created_at', '>=', clix.exp('now() - toIntervalMonth(6)'))- .execute();-- const [chart, [metrics]] = await Promise.all([- chartPromise,- metricsPromise,- ]);-- const change =- metrics && metrics.months_3_prev > 0 && metrics.months_3 > 0- ? Math.round(- ((metrics.months_3 - metrics.months_3_prev) /- metrics.months_3_prev) *- 100- )- : null;-- const trend =- change === null- ? { direction: 'neutral' as const, percentage: null as number | null }- : change > 0- ? { direction: 'up' as const, percentage: change }- : change < 0- ? { direction: 'down' as const, percentage: Math.abs(change) }- : { direction: 'neutral' as const, percentage: 0 };-- return {- chart: chart.map((d) => ({ ...d, date: new Date(d.date) })),- metrics,- trend,- };- }),+ .query(({ input: { projectId } }) => getProjectCard(projectId)),events: protectedProcedure.input(@@ -231,112 +126,16 @@ export const chartRouter = createTRPCRouter({projectId: z.string(),}))- .query(async ({ input: { projectId } }) => {- const [events, meta] = await Promise.all([- chQuery<{ name: string; count: number }>(- `SELECT name, count(name) as count FROM ${TABLE_NAMES.event_names_mv} WHERE project_id = ${sqlstring.escape(projectId)} GROUP BY name ORDER BY count DESC, name ASC`- ),- getEventMetasCached(projectId),- ]);-- return [- {- name: '*',- count: events.reduce((acc, event) => acc + event.count, 0),- meta: undefined,- },- ...events.map((event) => ({- name: event.name,- count: event.count,- meta: meta.find((m) => m.name === event.name),- })),- ];- }),+ .query(({ input: { projectId } }) => listChartEvents(projectId)),properties: protectedProcedure- // Aggregating every profile's property keys costs more than the old- // 10k-row sample. It's still sub-second on millions of profiles, and this- // list barely moves, so a short cache absorbs it.- .use(cacheMiddleware(60)).input(z.object({event: z.string().optional(),projectId: z.string(),}))- .query(async ({ input: { projectId, event } }) => {- const profileProperties = (- await getProfilePropertyKeysCached(projectId)- ).map((key) => `profile.properties.${key}`);-- const query = clix(ch)- .select<{ property_key: string; created_at: string }>([- 'distinct property_key',- 'max(created_at) as created_at',- ])- .from(TABLE_NAMES.event_property_values_mv)- .where('project_id', '=', projectId)- .groupBy(['property_key'])- // Order by recency, not by key length. The cap has to drop *something*- // on projects with very many distinct keys, and dropping the longest- // keys first meant losing the most descriptive ones. `property_key`- // breaks ties so the cap can't cut an arbitrary side of a tied group —- // an unstable list is the bug this whole change is about.- .orderBy('created_at', 'DESC')- .orderBy('property_key', 'ASC')- .limit(EVENT_PROPERTY_KEY_LIMIT);-- if (event && event !== '*') {- query.where('name', '=', event);- }-- const res = await query.execute();-- const eventProperties = res.map((item) => {- const key = item.property_key- .replace(/\.([0-9]+)\./g, '.*.')- .replace(/\.([0-9]+)/g, '[*]');- return `properties.${key}`;- });-- const fixedProperties = [- 'revenue',- 'has_profile',- 'path',- 'origin',- 'referrer',- 'referrer_name',- 'created_at',- 'country',- 'city',- 'region',- 'os',- 'os_version',- 'browser',- 'browser_version',- 'device',- 'brand',- 'model',- 'profile.id',- 'profile.first_name',- 'profile.last_name',- 'profile.email',- 'profile.created_at',- 'profile.last_seen_at',- ];-- const properties = [- ...eventProperties,- ...(event === '*' || !event ? ['name'] : []),- ...fixedProperties,- ...profileProperties,- ];-- return pipe(- sort<string>((a, b) => a.length - b.length),- uniq- )(properties);- }),+ .query(({ input }) => listChartProperties(input)),values: protectedProcedure.input(@@ -346,292 +145,39 @@ export const chartRouter = createTRPCRouter({projectId: z.string(),}))- .query(async ({ input: { event, property, projectId } }) => {- if (property === 'has_profile') {- return {- values: ['true', 'false'],- };- }-- const values: string[] = [];-- if (property.startsWith('properties.')) {- const query = clix(ch)- .select<{- property_value: string;- created_at: string;- }>(['distinct property_value', 'max(created_at) as created_at'])- .from(TABLE_NAMES.event_property_values_mv)- .where('project_id', '=', projectId)- .where('property_key', '=', property.replace(/^properties\./, ''))- .groupBy(['property_value'])- // Recency + key tie-break for a stable list under the cap — same- // rationale as the key picker above.- .orderBy('created_at', 'DESC')- .orderBy('property_value', 'ASC')- .limit(EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT);-- if (event && event !== '*') {- query.where('name', '=', event);- }-- const res = await query.execute();-- values.push(...res.map((e) => e.property_value));- } else if (property.startsWith('profile.')) {- const selectExpr = getProfilePropertySelect(property);- const query = clix(ch)- .select<{ values: string }>([`distinct ${selectExpr} as values`])- .from(TABLE_NAMES.profiles, true)- .where('project_id', '=', projectId)- .where(selectExpr, '!=', '')- .where(selectExpr, 'IS NOT NULL', null)- .orderBy('created_at', 'DESC')- .limit(100_000);-- const res = await query.execute();- values.push(...res.map((r) => String(r.values)).filter(Boolean));- } else if (property.startsWith('group.')) {- const selectExpr = getGroupPropertySelect(property);- const query = clix(ch)- .select<{ values: string }>([`distinct ${selectExpr} as values`])- .from(TABLE_NAMES.groups, true)- .where('project_id', '=', projectId)- .where('deleted', '=', 0)- .where(selectExpr, '!=', '')- .where(selectExpr, 'IS NOT NULL', null)- .orderBy('created_at', 'DESC')- .limit(100_000);-- const res = await query.execute();- values.push(...res.map((r) => String(r.values)).filter(Boolean));- } else if (property === 'cohort' || property.startsWith('cohort:')) {- // Cohort filters use a dedicated cohort multi-select on the client- // (ComboboxAdvanced over all cohorts) — values aren't sourced from- // an event-column distinct query. Without this guard, the events- // SELECT would emit a literal `cohort:<uuid>` identifier and crash- // with a ClickHouse syntax error.- return { values: [] };- } else {- // Normalize bare utm_* names to `properties.__query.utm_*` and rewrite- // camelCase aliases (`referrerName`) to their snake_case columns.- // Unknown identifiers (saved-report typos like `temple_name`, or- // misnamed columns from older clients) get an empty value list rather- // than crashing the autocomplete query with UNKNOWN_IDENTIFIER.- const resolvedProperty = normalizeEventField(property);- if (!isKnownEventField(resolvedProperty)) {- return { values: [] };- }- const query = clix(ch)- .select<{ values: string[] }>([- `distinct ${getSelectPropertyKey(resolvedProperty)} as values`,- ])- .from(TABLE_NAMES.events)- .where('project_id', '=', projectId)- .where('created_at', '>', clix.exp('now() - INTERVAL 6 MONTH'))- .orderBy('created_at', 'DESC')- .limit(100_000);-- if (event !== '*') {- query.where('name', '=', event);- }-- const events = await query.execute();-- values.push(- ...pipe(- (data: typeof events) => map(prop('values'), data),- flatten,- uniq,- sort((a, b) => a.length - b.length)- )(events)- );- }-- return {- values,- };- }),+ .query(({ input }) => getChartPropertyValues(input)),funnel: chartProcedure.use(cacher)- .input(- zReportInput.and(- z.object({- shareId: z.string().optional(),- id: z.string().optional(),- })- )- )- .query(async ({ input, ctx }) => {- const chartInput = ctx.report- ? {- ...ctx.report,- range: input.range ?? ctx.report.range,- startDate: input.startDate ?? ctx.report.startDate,- endDate: input.endDate ?? ctx.report.endDate,- interval: input.interval ?? ctx.report.interval,- }- : input;-- const { timezone } = await getSettingsForProject(chartInput.projectId);- const currentPeriod = getChartStartEndDate(chartInput, timezone);- const previousPeriod = getChartPrevStartEndDate(currentPeriod);-- const [current, previous] = await Promise.all([- funnelService.getFunnel({ ...chartInput, ...currentPeriod, timezone }),- chartInput.previous- ? funnelService.getFunnel({- ...chartInput,- ...previousPeriod,- timezone,- })- : Promise.resolve(null),- ]);-- return {- current,- previous,- };- }),+ .input(zShareableReportInput)+ .query(({ input, ctx }) =>+ getFunnelChart(resolveReportInput(ctx.report, input))+ ),conversion: chartProcedure.use(cacher)- .input(- zReportInput.and(- z.object({- shareId: z.string().optional(),- id: z.string().optional(),- })- )- )- .query(async ({ input, ctx }) => {- const chartInput = ctx.report- ? {- ...ctx.report,- range: input.range ?? ctx.report.range,- startDate: input.startDate ?? ctx.report.startDate,- endDate: input.endDate ?? ctx.report.endDate,- interval: input.interval ?? ctx.report.interval,- }- : input;+ .input(zShareableReportInput)+ .query(({ input, ctx }) =>+ getConversionChart(resolveReportInput(ctx.report, input))+ ),- const { timezone } = await getSettingsForProject(chartInput.projectId);- const currentPeriod = getChartStartEndDate(chartInput, timezone);- const previousPeriod = getChartPrevStartEndDate(currentPeriod);-- const interval = chartInput.interval;-- const [current, previous] = await Promise.all([- conversionService.getConversion({- ...chartInput,- ...currentPeriod,- interval,- timezone,- }),- chartInput.previous- ? conversionService.getConversion({- ...chartInput,- ...previousPeriod,- interval,- timezone,- })- : Promise.resolve(null),- ]);-- return {- current: current.map((serie, sIndex) => ({- ...serie,- data: serie.data.map((d, dIndex) => ({- ...d,- previousRate: previous?.[sIndex]?.data?.[dIndex]?.rate,- })),- })),- previous,- };- }),-- sankey: protectedProcedure.input(zReportInput).query(async ({ input }) => {- const { timezone } = await getSettingsForProject(input.projectId);- const currentPeriod = getChartStartEndDate(input, timezone);-- // Extract sankey options- const options = input.options;-- if (!options || options.type !== 'sankey') {- throw new Error('Sankey options are required');- }-- // Extract start/end events from series based on mode- const eventSeries = onlyReportEvents(- mergeGlobalFilters(input.series, input.globalFilters),- );-- if (!eventSeries[0]) {- throw new Error('Start and end events are required');- }-- return sankeyService.getSankey({- projectId: input.projectId,- startDate: currentPeriod.startDate,- endDate: currentPeriod.endDate,- steps: options.steps,- mode: options.mode,- startEvent: eventSeries[0],- endEvent: eventSeries[1],- exclude: options.exclude || [],- include: options.include,- timezone,- });- }),+ sankey: protectedProcedure+ .input(zReportInput)+ .query(({ input }) => getSankeyChart(input)),chart: chartProcedure.use(cacher)- .input(- zReportInput.and(- z.object({- shareId: z.string().optional(),- id: z.string().optional(),- })- )- )- .query(({ input, ctx }) => {- const chartInput = ctx.report- ? {- ...ctx.report,- range: input.range ?? ctx.report.range,- startDate: input.startDate ?? ctx.report.startDate,- endDate: input.endDate ?? ctx.report.endDate,- interval: input.interval ?? ctx.report.interval,- }- : input;-- return ChartEngine.execute(chartInput);- }),+ .input(zShareableReportInput)+ .query(({ input, ctx }) =>+ executeChart(resolveReportInput(ctx.report, input))+ ),aggregate: chartProcedure.use(cacher)- .input(- zReportInput.and(- z.object({- shareId: z.string().optional(),- id: z.string().optional(),- })- )- )- .query(({ input, ctx }) => {- const chartInput = ctx.report- ? {- ...ctx.report,- range: input.range ?? ctx.report.range,- startDate: input.startDate ?? ctx.report.startDate,- endDate: input.endDate ?? ctx.report.endDate,- interval: input.interval ?? ctx.report.interval,- }- : input;-- return AggregateChartEngine.execute(chartInput);- }),+ .input(zShareableReportInput)+ .query(({ input, ctx }) =>+ executeAggregateChart(resolveReportInput(ctx.report, input))+ ),cohort: chartProcedure.use(cacher)@@ -650,82 +196,7 @@ export const chartRouter = createTRPCRouter({id: z.string().optional(),}))- .query(async ({ input, ctx }) => {- const projectId = ctx.report?.projectId ?? input.projectId;- let firstEvent = input.firstEvent;- let secondEvent = input.secondEvent;- let criteria = input.criteria;- // Property/cohort filters scoping the retention audience. These live- // alongside the event-name selector (filters[0]) on each retention- // series; everything except that name selector is an audience filter.- let filters = input.filters ?? [];- const dateRange = ctx.report- ? (input.range ?? ctx.report.range)- : input.range;- const startDate = ctx.report- ? (input.startDate ?? ctx.report.startDate)- : input.startDate;- const endDate = ctx.report- ? (input.endDate ?? ctx.report.endDate)- : input.endDate;- const interval = ctx.report- ? (input.interval ?? ctx.report.interval)- : input.interval;-- // Extract events from report series if shared- if (ctx.report) {- const retentionOptions =- ctx.report.options?.type === 'retention'- ? ctx.report.options- : undefined;- criteria = retentionOptions?.criteria ?? criteria;-- const eventSeries = onlyReportEvents(ctx.report.series);- const extractedFirstEvent = (- eventSeries[0]?.filters?.[0]?.value ?? []- ).map(String);- const extractedSecondEvent = (- eventSeries[1]?.filters?.[0]?.value ?? []- ).map(String);-- if (- extractedFirstEvent.length === 0 ||- extractedSecondEvent.length === 0- ) {- throw new Error('Report must have at least 2 event series');- }-- firstEvent = extractedFirstEvent;- secondEvent = extractedSecondEvent;- filters = [- ...(ctx.report.globalFilters ?? []),- ...eventSeries.flatMap((serie) =>- (serie.filters ?? []).filter((filter) => filter.name !== 'name')- ),- ];- }-- const { timezone } = await getSettingsForProject(projectId);- const dates = getChartStartEndDate(- {- range: dateRange,- startDate,- endDate,- },- timezone- );-- return getRetentionCohort({- projectId,- firstEvent,- secondEvent,- criteria,- interval,- startDate: dates.startDate,- endDate: dates.endDate,- filters,- });- }),+ .query(({ input, ctx }) => getRetentionChart(ctx.report, input)),getProfiles: protectedProcedure.input(@@ -737,90 +208,7 @@ export const chartRouter = createTRPCRouter({breakdowns: z.record(z.string(), z.string()).optional(),}))- .query(async ({ input }) => {- const { projectId, date, series } = input;- const serie = series[0];-- if (!serie) {- throw new Error('Series not found');- }-- if (serie.type !== 'event') {- throw new Error('Series must be an event');- }-- // Build the date range for the specific interval bucket- const dateObj = new Date(date);- // Build query to get unique profile_ids for this time bucket- const { sb, getSql } = createSqlBuilder();-- sb.select.profile_id = 'DISTINCT profile_id';- sb.where = getEventFiltersWhereClause(serie.filters, projectId);- sb.where.projectId = `project_id = ${sqlstring.escape(projectId)}`;- sb.where.dateRange = `${clix.toStartOf('created_at', input.interval)} = ${clix.toDate(sqlstring.escape(formatClickhouseDate(dateObj)), input.interval)}`;- if (serie.name !== '*') {- sb.where.eventName = `name = ${sqlstring.escape(serie.name)}`;- }-- // Collect profile fields from filters and breakdowns- const profileFields = [- ...serie.filters- .filter((f) => f.name.startsWith('profile.'))- .map((f) => f.name.replace('profile.', '')),- ...(input.breakdowns- ? Object.keys(input.breakdowns)- .filter((key) => key.startsWith('profile.'))- .map((key) => key.replace('profile.', ''))- : []),- ];-- if (profileFields.length > 0) {- // Extract top-level field names and select only what's needed- const fieldsToSelect = uniq(- profileFields.map((f) => f.split('.')[0])- ).join(', ');- sb.joins.profiles = `LEFT ANY JOIN (SELECT id, ${fieldsToSelect} FROM ${TABLE_NAMES.profiles} FINAL WHERE project_id = ${sqlstring.escape(projectId)}) as profile on profile.id = profile_id`;- }-- // Check for group filters/breakdowns and add ARRAY JOIN if needed- const anyFilterOnGroup = serie.filters.some((f) =>- f.name.startsWith('group.')- );- const anyBreakdownOnGroup = input.breakdowns- ? Object.keys(input.breakdowns).some((key) => key.startsWith('group.'))- : false;- if (anyFilterOnGroup || anyBreakdownOnGroup) {- sb.joins.groups = 'ARRAY JOIN groups AS _group_id';- sb.joins.groups_cte = `LEFT ANY JOIN (SELECT id, name, type, properties FROM ${TABLE_NAMES.groups} FINAL WHERE project_id = ${sqlstring.escape(projectId)}) AS _g ON _g.id = _group_id`;- }-- if (input.breakdowns) {- Object.entries(input.breakdowns).forEach(([key, value]) => {- // Transform property keys (e.g., properties.method -> properties['method'])- const propertyKey = getSelectPropertyKey(key, projectId);- sb.where[`breakdown_${key}`] =- `${propertyKey} = ${sqlstring.escape(value)}`;- });- }-- // Get unique profile IDs- const profileIds = await chQuery<{ profile_id: string }>(getSql());- if (profileIds.length === 0) {- return [];- }-- // Fetch profile details in batches to avoid exceeding ClickHouse max_query_size- const ids = profileIds.map((p) => p.profile_id).filter(Boolean);- const BATCH_SIZE = 200;- const profiles: IServiceProfile[] = [];- for (let i = 0; i < ids.length; i += BATCH_SIZE) {- const batch = ids.slice(i, i + BATCH_SIZE);- const batchProfiles = await getProfilesCached(batch, projectId);- profiles.push(...batchProfiles);- }-- return profiles;- }),+ .query(({ input }) => getChartBucketProfiles(input)),getFunnelProfiles: protectedProcedure.input(@@ -844,99 +232,5 @@ export const chartRouter = createTRPCRouter({range: zRange,}))- .query(async ({ input }) => {- const { timezone } = await getSettingsForProject(input.projectId);- const {- projectId,- series,- stepIndex,- showDropoffs = false,- funnelWindow,- funnelGroup,- breakdowns: inputBreakdowns = [],- breakdownValues = [],- } = input;-- const { startDate, endDate } = getChartStartEndDate(input, timezone);-- // stepIndex is 0-based, but level is 1-based, so we need level >= stepIndex + 1- const targetLevel = stepIndex + 1;-- // Reuse the chart's own funnel builder rather than re-deriving the CTE- // here. The two copies used to drift — breakdown expressions referencing- // a `profile` or `cohort_<id>` alias whose join this side never added,- // which failed with UNKNOWN_IDENTIFIER and surfaced as "No users found".- const { query, breakdowns } = await funnelService.buildFunnelBase({- projectId,- startDate,- endDate,- series,- breakdowns: inputBreakdowns,- funnelWindow,- funnelGroup,- timezone,- });-- // Same shape as the chart's `funnel` CTE: windowFunnel is already- // computed per primary key (with breakdowns attributed at the entry- // step, so each group carries one deterministic b_N value), so drop- // level=0 and select distinct profiles.- query.with('funnel', 'SELECT * FROM session_funnel WHERE level != 0');-- query.select(['DISTINCT profile_id']).from('funnel');-- if (showDropoffs) {- query.where('level', '=', targetLevel);- } else {- query.where('level', '>=', targetLevel);- }-- // Filter by specific breakdown values when a breakdown row was clicked.- // The clicked row carries DISPLAY labels — trimmed, with empty/null- // shown as EMPTY_BREAKDOWN_LABEL (see toSeries/normalizeBreakdownValue)- // — so match against the same normalization, not the raw column, or- // "Not set" rows and values with stray whitespace return no users.- // toString/ifNull make the comparison safe for numeric breakdown- // columns (trim on a number is a type error) and for Nullable ones- // (trim(NULL) = '' is NULL, never true).- breakdowns.forEach((_, index) => {- const value = breakdownValues[index];- if (value === undefined) {- return;- }- const normalized = `trim(ifNull(toString(b_${index}), ''))`;- if (value === EMPTY_BREAKDOWN_LABEL) {- query.rawWhere(- `(${normalized} = '' OR ${normalized} = ${sqlstring.escape(EMPTY_BREAKDOWN_LABEL)})`- );- } else {- query.rawWhere(`${normalized} = ${sqlstring.escape(value)}`);- }- });-- // Cap the number of profiles to avoid exceeding ClickHouse max_query_size- // when passing IDs to the next query- query.limit(1000);-- const profileIdsResult = (await query.execute()) as {- profile_id: string;- }[];-- if (profileIdsResult.length === 0) {- return [];- }-- // Fetch profile details in batches to avoid exceeding ClickHouse max_query_size- // when there are many profile IDs to pass in the IN(...) clause- const ids = profileIdsResult.map((p) => p.profile_id).filter(Boolean);- const BATCH_SIZE = 500;- const profiles: IServiceProfile[] = [];- for (let i = 0; i < ids.length; i += BATCH_SIZE) {- const batch = ids.slice(i, i + BATCH_SIZE);- const batchProfiles = await getProfilesCached(batch, projectId);- profiles.push(...batchProfiles);- }-- return profiles;- }),+ .query(({ input }) => getFunnelStepProfiles(input)),});diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex f137d33b5..db7e295eb 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1172,6 +1172,9 @@ importers:lru-cache:specifier: ^11.2.4version: 11.5.1+ mathjs:+ specifier: ^12.3.2+ version: 12.3.2nanoid:specifier: ^5.1.6version: 5.1.6