Skip to content

fix(cohorts): apply full HogQL global settings to recalculation query - #76424

Draft
posthog[bot] wants to merge 1 commit into
masterfrom
posthog-self-driving/fixcohorts-apply-hogql-global-settings-d2c69c
Draft

fix(cohorts): apply full HogQL global settings to recalculation query#76424
posthog[bot] wants to merge 1 commit into
masterfrom
posthog-self-driving/fixcohorts-apply-hogql-global-settings-d2c69c

Conversation

@posthog

@posthog posthog Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Problem

  • Cohort recalculation failed with CHQueryErrorNotFoundColumnInBlock: the ClickHouse initiator wanted uniq(__table1.$session_id), shards produced the backtick-quoted equivalent — same expression, different identifier quoting.
  • This is the distributed new-analyzer header-mismatch bug class already worked around three times in HogQLGlobalSettings (posthog/hogql/constants.py).
  • The workarounds still don't apply on the cohort recalculation path: _recalculate_cohortpeople_for_team_hogql builds SQL with the HogQL printer, then calls sync_execute with a hand-rolled settings dict that only pulls max_ast_elements/max_expanded_ast_elements out of HogQLGlobalSettings() — every other analyzer bugfix setting is silently dropped.
  • Impact today is small (a single cohort, single occurrence), but the failure mode is a cohort that silently stops recalculating — feeding stale membership into feature flag targeting and messaging — and every HogQL cohort recalculation query is exposed to this class of ClickHouse bugs.

Changes

  • _recalculate_cohortpeople_for_team_hogql now builds its settings dict by spreading get_default_hogql_global_settings(team_id=...).model_dump(exclude_none=True), then layering the cohort-specific overrides (timeouts, optimize_on_insert, external group-by/sort ratios) on top.
  • Cohorts now inherit all current and future HogQL analyzer workarounds automatically instead of requiring a manual per-field addition each time a new one is added.

How did you test this code?

  • ruff check / ruff format --check pass on the changed file.
  • mypy passes on the changed file.
  • No dev ClickHouse/Postgres stack was available in this sandbox, so I could not run the existing products/cohorts/backend/models/test/test_util.py suite or add a new regression test that asserts on the settings dict contents. The change is a mechanical widening of an existing dict spread (same pattern used elsewhere, e.g. lazy_computation_executor.py) with no behavior change beyond adding the previously-dropped settings.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Docs update

🤖 Agent context

Autonomy: Fully autonomous

  • Investigated via error tracking issue 019fc2d0 (1 occurrence/1 user) pointing at products/cohorts/backend/models/util.py and the existing bugfix-workaround settings in posthog/hogql/constants.py.
  • Checked for prior/competing work: gh pr list/gh issue list searches for related keywords and file paths turned up nothing addressing this specific gap.
  • Considered also adding an explicit alias for the session aggregate column to stabilize its name independent of ClickHouse identifier quoting, but the settings fix alone addresses the root cause (missing analyzer workarounds) described in the report, so kept the change minimal.

Created with PostHog Desktop from this inbox report.

Cohort recalculation built its ClickHouse settings dict by hand, pulling only `max_ast_elements`/`max_expanded_ast_elements` out of `HogQLGlobalSettings` and dropping every other analyzer bugfix workaround (e.g. `optimize_rewrite_aggregate_function_with_if`, `optimize_min_inequality_conjunction_chain_length`). This left cohort recalculation queries exposed to distributed-analyzer header-mismatch bugs the rest of HogQL is protected from.

Now spreads `get_default_hogql_global_settings(team_id=...).model_dump(exclude_none=True)` into the settings dict, layering the cohort-specific timeout/output overrides on top, so cohorts inherit current and future analyzer workarounds automatically.

Generated-By: PostHog Code
Task-Id: a88c449c-6624-405c-9a08-aff8bd51b20c
@trunk-io

trunk-io Bot commented Aug 2, 2026

Copy link
Copy Markdown

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

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

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

0 participants