Skip to content

one_event_per_user segment + breakdown fails: Unknown expression or function identifier e.properties #502

Description

@dima-dencep

Any chart with a series segment one_event_per_user and a property breakdown fails. Reproduced on current main (SQL below uses the DISTINCT ON form from #476).

Repro

Pie chart, series: event game_auth_verified, segment "One event per user", breakdown properties.linked, range 30d, no filters.

chart.aggregate input (trimmed):

{"chartType":"pie","interval":"day","breakdowns":[{"id":"Jv-4","name":"properties.linked"}],"globalFilters":[],"series":[{"type":"event","segment":"one_event_per_user","filters":[],"id":"EyUz","name":"game_auth_verified"}],"range":"30d","metric":"sum","limit":500}

Generated SQL (-- Aggregate Chart --):

SELECT 'game_auth_verified' as label_0, '2026-08-15 00:00:00' as date, e.properties['linked'] as label_1, count(*) as count
FROM (
  SELECT DISTINCT ON (profile_id) * from events e
  WHERE project_id = '<project>' AND e.name = 'game_auth_verified'
    AND created_at >= toDateTime('2026-08-15 00:00:00') AND created_at <= toDateTime('2026-09-15 00:00:00')
  ORDER BY profile_id, created_at DESC
) as subQuery
WHERE (project_id = '<project>') AND (e.name = 'game_auth_verified')
  AND (created_at >= toDateTime('2026-08-15 00:00:00')) AND (created_at <= toDateTime('2026-09-15 00:00:00'))
GROUP BY label_1, label_0

ClickHouse error:

Code 47: Unknown expression or function identifier `e.properties` in scope SELECT ...

Root cause (packages/db/src/services/chart.service.ts)

  • Breakdown SELECT expressions are always built with the events alias: getSelectPropertyKey(breakdown.name, …, 'e')e.properties['linked'].
  • For one_event_per_user the FROM is replaced with a subquery aliased subQuery, so e is not in scope in the outer query.
  • The series path clears sb.where to avoid e.name (comment there), but the SELECT still references e.properties. The aggregate path (chart.aggregate) clears nothing, so both e.properties and e.name are out of scope.

Suggested fix

Alias the subquery as e instead of subQuery in both places () as e). The subquery does SELECT * from events e, so e.properties[...]/e.name in the outer SELECT/WHERE resolve; the duplicated outer WHERE becomes redundant but valid. Alternatively pass no alias to getSelectPropertyKey in the one_event_per_user branch. A test case in chart-sql.test.ts for one_event_per_user + breakdown would catch this.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions