Skip to content

fix(funnel): qualify has_profile columns to avoid ClickHouse ILLEGAL_AGGREGATION - #671

Open
muhammad-wasif-code wants to merge 1 commit into
Openpanel-dev:mainfrom
muhammad-wasif-code:fix/663-has-profile-funnel-alias
Open

muhammad-wasif-code wants to merge 1 commit into
Openpanel-dev:mainfrom
muhammad-wasif-code:fix/663-has-profile-funnel-alias

Conversation

@muhammad-wasif-code

@muhammad-wasif-code muhammad-wasif-code commented Nov 14, 2026 •

Copy link
Copy Markdown

Summary

Fixes #663

Session-grouped funnel queries failed with ClickHouse Code 184 when a step used the built-in has_profile filter.

Root cause

For funnelGroup = session_id, the funnel CTE selects argMax(profile_id, created_at) AS profile_id and places step filter predicates in the same SELECT's WHERE. The has_profile branch in getEventFiltersWhereClause emitted unqualified profile_id != device_id / profile_id = device_id even when the funnel already passed the events table qualifier, so ClickHouse resolved profile_id to the aggregate alias (ILLEGAL_AGGREGATION).

Change

Apply the provided events table qualifier to both columns in the has_profile branch (events.profile_id / events.device_id). When no qualifier is passed, behaviour is unchanged (unqualified columns).

Tests

  • filter-where.test.ts: asserts qualified predicates for true and false when an alias is given, and unqualified when it is not.
  • funnel.sql.test.ts: session-grouped funnel SQL with has_profile must contain qualified comparisons and must not contain unqualified profile_id / device_id comparisons in the WHERE prefilter.

How tested

  • Confirmed the new tests fail on current main (fix stashed) and pass after the fix.
  • bun test --isolate src/modules/chart/src/filter-where.test.ts
  • bun test --isolate src/modules/chart/src/funnel.sql.test.ts -t "has_profile with session grouping"
  • npx ultracite check on the touched files
  • bun run --filter @openpanel/core typecheck

Summary by CodeRabbit

  • Bug Fixes
    • Corrected profile-filter queries in charts and funnels to qualify profile and device columns when needed, preventing ambiguous column references.

Session-grouped funnels select argMax(profile_id, created_at) AS profile_id
and put step filters in the same WHERE. Unqualified has_profile predicates
resolved to that aggregate alias and raised ClickHouse Code 184. Apply the
existing events qualifier to both profile_id and device_id when provided.
@CLAassistant

CLAassistant commented Nov 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The has_profile filter now qualifies profile_id and device_id with the supplied events alias. Tests cover aliased and unaliased predicates and session-grouped funnel SQL for both filter values.

Changes: has_profile filter qualification

Layer / File(s) Summary
Predicate qualification and regression tests
packages/core/src/modules/chart/src/filter-where.ts, packages/core/src/modules/chart/src/filter-where.test.ts, packages/core/src/modules/chart/src/funnel.sql.test.ts
The filter compiler qualifies profile_id and device_id when an events alias is supplied. Tests cover true and false values, the no-alias case, and session-grouped funnel SQL. The comparison operators remain unchanged.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: lindesvard

Merge Risk: Minimal · up to db520

This fixes the ambiguous column reference in session-grouped funnel queries with has_profile filters. The change is small, covered by SQL-generation tests, and shows no remaining merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the fix (qualifying has_profile columns) and the error it avoids (ClickHouse ILLEGAL_AGGREGATION). This matches the main change: filter-where.ts now qualifies profile_id and device_id …
Linked Issues check Passed Issue #663 requires that the session-grouped funnel has_profile predicates compare input event columns, not the aggregate alias, for both true (profile_id != device_id) and false (profile_id = device_…
Out of Scope Changes check Passed The PR changes are limited to the has_profile branch in filter-where.ts, its helper test filter-where.test.ts, and the new session-grouping test in funnel.sql.test.ts. Each change directly implements …
Full details: Title check

Explanation

The title names the fix (qualifying has_profile columns) and the error it avoids (ClickHouse ILLEGAL_AGGREGATION). This matches the main change: filter-where.ts now qualifies profile_id and device_id with the events alias, and funnel tests cover session-grouped queries. The title is specific and concise, and the scope is accurate.

Full details: Linked Issues check

Explanation

Issue #663 requires that the session-grouped funnel has_profile predicates compare input event columns, not the aggregate alias, for both true (profile_id != device_id) and false (profile_id = device_id) values. At head, getEventFiltersWhereClause in packages/core/src/modules/chart/src/filter-where.ts builds the has_profile branch with sql.id(${eventsAlias}.profile_id) and sql.id(${eventsAlias}.device_id) when an alias is passed. Without an alias, it keeps the unqualified columns, so profile-grouped behavior is unchanged. funnel.sql.ts already passes the 'events' alias in funnelStepConditions, which covers both the windowFunnel conditions and the WHERE prefilter. The PR adds SQL-generation tests in filter-where.test.ts and funnel.sql.test.ts that assert qualified predicates. Gap: the issue also asks for a database-backed execution test using prefer_column_name_to_alias = 0 and verification of funnel results. The PR does not add that test, and the PR reports no database-backed run. The core code requirement is met. The missing database-backed regression test is noted but does not change this verdict.

Full details: Out of Scope Changes check

Explanation

The PR changes are limited to the has_profile branch in filter-where.ts, its helper test filter-where.test.ts, and the new session-grouping test in funnel.sql.test.ts. Each change directly implements or verifies the #663 requirement. No unrelated change is identified in the summary or in the inspected head content. The base-to-head text diff for filter-where.ts could not be read because changed blobs were missing from the checkout, so this verdict relies on the PR summary and the head file content, which match each other.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5abf2a09-3d43-4bd2-83c1-25d26f81f10e
Commits

Reviewing files that changed from the base of the PR and between a5a6eb7 and db520b3.

Files selected for processing: 3

  • packages/core/src/modules/chart/src/filter-where.test.ts
  • packages/core/src/modules/chart/src/filter-where.ts
  • packages/core/src/modules/chart/src/funnel.sql.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Funnel has_profile filter conflicts with the profile_id aggregate alias (ClickHouse Code 184)

2 participants