Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(hogql): keep child queries on the parent's events route (#120297)

Vasco de Krijger3158a6056134
diff --git a/posthog/api/services/query.py b/posthog/api/services/query.py
index c345334187ba..b564c28e07af 100644
--- a/posthog/api/services/query.py
+++ b/posthog/api/services/query.py
@@ -64,6 +64,7 @@ from posthog.hogql_queries.query_runner import (
QueryResponse,
QueryRunner,
get_query_runner_or_none,
+ unwrapped_source,
)
from posthog.models import Team, User
from posthog.schema_migrations.upgrade import upgrade
@@ -683,7 +684,7 @@ def process_query_model(
if hasattr(query, "source") and isinstance(query.source, BaseModel):
return process_query_model(
team,
- query.source,
+ unwrapped_source(query, query.source),
dashboard_filters=dashboard_filters,
variables_override=variables_override,
limit_context=limit_context,
diff --git a/posthog/hogql_queries/actor_strategies.py b/posthog/hogql_queries/actor_strategies.py
index d5c7de1530ed..972ea90b37fb 100644
--- a/posthog/hogql_queries/actor_strategies.py
+++ b/posthog/hogql_queries/actor_strategies.py
@@ -5,7 +5,7 @@ from typing import Literal, Optional, cast
import orjson as json
import structlog
-from posthog.schema import ActorsQuery, InsightActorsQuery, TrendsQuery
+from posthog.schema import ActorsQuery, HogQLQueryModifiers, InsightActorsQuery, TrendsQuery
from posthog.hogql import ast
from posthog.hogql.parser import parse_expr, parse_select
@@ -34,11 +34,13 @@ class ActorStrategy:
query: ActorsQuery,
paginator: HogQLHasMorePaginator,
user: User | None = None,
+ modifiers: HogQLQueryModifiers | None = None,
):
self.team = team
self.paginator = paginator
self.query = query
self.user = user
+ self.modifiers = modifiers
def get_actors(self, actor_ids) -> dict[str, dict]:
raise NotImplementedError()
@@ -310,6 +312,7 @@ class SessionStrategy(ActorStrategy):
query=query,
team=self.team,
user=self.user,
+ modifiers=self.modifiers,
)
columns = response.columns or []
diff --git a/posthog/hogql_queries/actors_query_runner.py b/posthog/hogql_queries/actors_query_runner.py
index 6edc4385df00..a17a505bf368 100644
--- a/posthog/hogql_queries/actors_query_runner.py
+++ b/posthog/hogql_queries/actors_query_runner.py
@@ -49,7 +49,7 @@ class ActorsQueryRunner(AnalyticsQueryRunner[ActorsQueryResponse]):
if self.query.source:
self.source_query_runner = get_query_runner(
- self.query.source, self.team, self.timings, self.limit_context, user=self.user
+ self.query.source, self.team, self.timings, self.limit_context, self.modifiers, user=self.user
)
self.modifiers = self.source_query_runner.modifiers
else:
@@ -66,7 +66,13 @@ class ActorsQueryRunner(AnalyticsQueryRunner[ActorsQueryResponse]):
self.strategy = self.determine_strategy()
self.calculating = False
- self.user = None
+
+ def _child_runners(self) -> Iterable[QueryRunner]:
+ return [self.source_query_runner] if self.source_query_runner is not None else []
+
+ def _on_user_changed(self) -> None:
+ super()._on_user_changed()
+ self.strategy = self.determine_strategy()
def run(
self,
@@ -81,9 +87,7 @@ class ActorsQueryRunner(AnalyticsQueryRunner[ActorsQueryResponse]):
# This runner takes the run() user verbatim, including None. A bare assignment would
# hide the change from the base run(), leaving the shared database and access-control
# snapshot scoped to the previous user.
- if user is not self.user:
- self.user = user
- self._on_user_changed()
+ self.set_user(user)
return super().run(
execution_mode,
user,
@@ -119,7 +123,9 @@ class ActorsQueryRunner(AnalyticsQueryRunner[ActorsQueryResponse]):
)
if self.is_session_aggregation:
- return SessionStrategy(team=self.team, query=self.query, paginator=self.paginator, user=self.user)
+ return SessionStrategy(
+ team=self.team, query=self.query, paginator=self.paginator, user=self.user, modifiers=self.modifiers
+ )
return PersonStrategy(team=self.team, query=self.query, paginator=self.paginator, user=self.user)
diff --git a/posthog/hogql_queries/events_query_runner.py b/posthog/hogql_queries/events_query_runner.py
index 94f7957b9bbb..31a9a22bb984 100644
--- a/posthog/hogql_queries/events_query_runner.py
+++ b/posthog/hogql_queries/events_query_runner.py
@@ -1,4 +1,5 @@
import re
+from collections.abc import Iterable
from datetime import datetime, timedelta
from functools import cached_property
from itertools import batched
@@ -48,7 +49,7 @@ from posthog.dataclasses import frozen
from posthog.date_util import start_of_day
from posthog.hogql_queries.insight_actors_query_runner import InsightActorsQueryRunner
from posthog.hogql_queries.paginators import HogQLHasMorePaginator
-from posthog.hogql_queries.query_runner import AnalyticsQueryRunner, get_query_runner
+from posthog.hogql_queries.query_runner import AnalyticsQueryRunner, QueryRunner, get_query_runner
from posthog.hogql_queries.utils.person_display_name import person_display_name_property_exprs
from posthog.models import Person, PropertyDefinition
from posthog.models.element import chain_to_elements
@@ -216,6 +217,9 @@ class EventsQueryRunner(AnalyticsQueryRunner[EventsQueryResponse]):
)
self._cursor_eligible = False
+ def _child_runners(self) -> Iterable[QueryRunner]:
+ return [self.source_runner] if "source_runner" in self.__dict__ else []
+
@cached_property
def source_runner(self) -> InsightActorsQueryRunner:
if not self.query.source:
diff --git a/posthog/hogql_queries/insight_actors_query_options_runner.py b/posthog/hogql_queries/insight_actors_query_options_runner.py
index 9187014b4fa4..f6ad7d5fcf45 100644
--- a/posthog/hogql_queries/insight_actors_query_options_runner.py
+++ b/posthog/hogql_queries/insight_actors_query_options_runner.py
@@ -1,20 +1,16 @@
+from collections.abc import Iterable
from functools import cached_property
-from typing import Any, Optional, cast
+from typing import cast
from posthog.schema import (
CachedInsightActorsQueryOptionsResponse,
- HogQLQueryModifiers,
InsightActorsQueryOptions,
InsightActorsQueryOptionsResponse,
)
from posthog.hogql import ast
-from posthog.hogql.constants import LimitContext
-from posthog.hogql.timings import HogQLTimings
-from posthog.hogql_queries.query_runner import QueryRunner, get_query_runner, query_node_modifiers
-from posthog.models import Team
-from posthog.models.user import User
+from posthog.hogql_queries.query_runner import QueryRunner, get_query_runner
from products.product_analytics.backend.facade.queries import (
FunnelsQueryRunner,
@@ -27,26 +23,8 @@ class InsightActorsQueryOptionsRunner(QueryRunner):
query: InsightActorsQueryOptions
cached_response: CachedInsightActorsQueryOptionsResponse
- def __init__(
- self,
- query: InsightActorsQueryOptions | dict[str, Any],
- team: Team,
- timings: Optional[HogQLTimings] = None,
- modifiers: Optional[HogQLQueryModifiers] = None,
- limit_context: Optional[LimitContext] = None,
- query_id: Optional[str] = None,
- user: Optional[User] = None,
- ):
- super().__init__(
- query,
- team=team,
- timings=timings,
- modifiers=modifiers,
- limit_context=limit_context,
- query_id=query_id,
- extract_modifiers=lambda query: query_node_modifiers(query.source.source),
- user=user,
- )
+ def _child_runners(self) -> Iterable[QueryRunner]:
+ return [self.source_runner] if "source_runner" in self.__dict__ else []
@cached_property
def source_runner(self) -> QueryRunner:
diff --git a/posthog/hogql_queries/insight_actors_query_runner.py b/posthog/hogql_queries/insight_actors_query_runner.py
index b3d327eb3b09..ad87a9319abf 100644
--- a/posthog/hogql_queries/insight_actors_query_runner.py
+++ b/posthog/hogql_queries/insight_actors_query_runner.py
@@ -1,5 +1,6 @@
+from collections.abc import Iterable
from functools import cached_property
-from typing import Any, Optional, cast
+from typing import Optional, cast
from posthog.schema import (
ExperimentActorsQuery,
@@ -7,7 +8,6 @@ from posthog.schema import (
FunnelsActorsQuery,
FunnelsFilter,
FunnelsQuery,
- HogQLQueryModifiers,
HogQLQueryResponse,
InsightActorsQuery,
LifecycleQuery,
@@ -18,14 +18,10 @@ from posthog.schema import (
)
from posthog.hogql import ast
-from posthog.hogql.constants import LimitContext
from posthog.hogql.errors import ExposedHogQLError
from posthog.hogql.query import execute_hogql_query
-from posthog.hogql.timings import HogQLTimings
-from posthog.hogql_queries.query_runner import AnalyticsQueryRunner, QueryRunner, get_query_runner, query_node_modifiers
-from posthog.models import Team
-from posthog.models.user import User
+from posthog.hogql_queries.query_runner import AnalyticsQueryRunner, QueryRunner, get_query_runner
from posthog.types import InsightActorsQueryNode
from products.experiments.backend.hogql_queries.experiment_query_runner import ExperimentQueryRunner
@@ -43,26 +39,8 @@ from products.product_analytics.backend.facade.queries import (
class InsightActorsQueryRunner(AnalyticsQueryRunner[HogQLQueryResponse]):
query: InsightActorsQueryNode
- def __init__(
- self,
- query: InsightActorsQueryNode | dict[str, Any],
- team: Team,
- timings: Optional[HogQLTimings] = None,
- modifiers: Optional[HogQLQueryModifiers] = None,
- limit_context: Optional[LimitContext] = None,
- query_id: Optional[str] = None,
- user: Optional[User] = None,
- ):
- super().__init__(
- query,
- team=team,
- timings=timings,
- modifiers=modifiers,
- limit_context=limit_context,
- query_id=query_id,
- extract_modifiers=lambda query: query_node_modifiers(query.source),
- user=user,
- )
+ def _child_runners(self) -> Iterable[QueryRunner]:
+ return [self.source_runner] if "source_runner" in self.__dict__ else []
@cached_property
def source_runner(self) -> QueryRunner:
diff --git a/posthog/hogql_queries/query_context.py b/posthog/hogql_queries/query_context.py
index 245fd527e252..9b7da48eb750 100644
--- a/posthog/hogql_queries/query_context.py
+++ b/posthog/hogql_queries/query_context.py
@@ -40,14 +40,21 @@ class QueryContext(ABC):
self.timings = timings or HogQLTimings()
self.limit_context = limit_context or LimitContext.QUERY
self.modifiers = create_default_modifiers_for_team(team, modifiers)
- self.hogql_context = HogQLContext(
+ self.hogql_context = self._build_hogql_context()
+ self.now = now or datetime.now()
+
+ def set_user(self, user: Optional[User]) -> None:
+ self.user = user
+ self.hogql_context = self._build_hogql_context()
+
+ def _build_hogql_context(self) -> HogQLContext:
+ return HogQLContext(
team_id=self.team.pk,
enable_select_queries=True,
timings=self.timings,
modifiers=self.modifiers,
- user=user,
+ user=self.user,
)
- self.now = now or datetime.now()
class QueryContextProtocol(Protocol):
diff --git a/posthog/hogql_queries/query_runner.py b/posthog/hogql_queries/query_runner.py
index 15f43c00601a..9ef68d4f9e88 100644
--- a/posthog/hogql_queries/query_runner.py
+++ b/posthog/hogql_queries/query_runner.py
@@ -2,7 +2,7 @@ import hashlib
import inspect
import threading
from abc import ABC, abstractmethod
-from collections.abc import Sequence
+from collections.abc import Iterable, Sequence
from contextlib import ExitStack, suppress
from datetime import UTC, datetime, timedelta
from enum import StrEnum
@@ -16,6 +16,7 @@ from typing import (
NamedTuple,
Optional,
Protocol,
+ TypeAliasType,
TypeGuard,
TypeVar,
Union,
@@ -1813,6 +1814,7 @@ R = TypeVar("R", bound=BaseModel)
# CR (for CachedResponse) must be R extended with CachedQueryResponseMixin
# Unfortunately inheritance is also not a thing here, because we lose this info in the schema.ts->.json->.py journey
CR = TypeVar("CR", bound=GenericCachedQueryResponse)
+SourceT = TypeVar("SourceT", bound=BaseModel)
def resolve_series_custom_name(series: Any, raw_label: str | None) -> str | None:
@@ -1829,7 +1831,27 @@ def resolve_series_custom_name(series: Any, raw_label: str | None) -> str | None
def query_node_modifiers(query: BaseModel) -> Optional[HogQLQueryModifiers]:
- return getattr(query, "modifiers", None)
+ # A wrapper query (actors, correlation, actor options) runs its source insight, so it reads the same
+ # events table and person data as that insight. Modifiers set on the wrapper itself take precedence.
+ source = getattr(query, "source", None)
+ inherited = query_node_modifiers(source) if isinstance(source, BaseModel) else None
+ modifiers = getattr(query, "modifiers", None)
+ if inherited is not None and modifiers is not None:
+ modifiers = inherited.model_copy(update={key: value for key, value in modifiers if value is not None})
+ elif modifiers is None:
+ modifiers = inherited
+ return modifiers
+
+
+def unwrapped_source(wrapper: BaseModel, source: SourceT) -> SourceT:
+ """The source of a wrapper query, for code that runs the source in place of the wrapper.
+
+ It carries the modifiers the wrapper resolves to, so the source runs with the same settings a
+ runner for the wrapper would use.
+ """
+ if getattr(wrapper, "modifiers", None) is None:
+ return source
+ return source.model_copy(update={"modifiers": query_node_modifiers(wrapper)})
def team_configs_for_cache_key(team: Team) -> dict[str, dict | str]:
@@ -1922,13 +1944,29 @@ class QueryRunner(ABC, Generic[Q, R, CR]):
"""Called after init, can by overriden by subclasses. Should be idempotent. Also called after dashboard overrides are set."""
pass
+ def set_user(self, user: Optional[User]) -> None:
+ """Run as another user from now on, together with every runner this one built."""
+ if user is self.user:
+ return
+ self.user = user
+ self._on_user_changed()
+
+ def _child_runners(self) -> Iterable["QueryRunner"]:
+ """Runners this one built and runs, so a user change reaches them.
+
+ A lazy child that is not built yet reads the user when it is built, so it is not listed."""
+ return ()
+
def _on_user_changed(self) -> None:
- """Hook called by run() when self.user is updated after construction.
+ """Hook called by set_user() when self.user changes after construction.
Drops the lazily built shared database so the next access rebuilds it for the new
- user. Subclasses that override this to rebuild their own user-dependent state (e.g.
- a cached HogQLContext / Database) must call super()._on_user_changed()."""
+ user, and passes the user to the child runners. Subclasses that override this to
+ rebuild their own user-dependent state (e.g. a cached HogQLContext / Database) must
+ call super()._on_user_changed()."""
self._shared_database = None
+ for child in self._child_runners():
+ child.set_user(self.user)
@property
def user_access_control(self) -> Optional[UserAccessControl]:
@@ -1992,7 +2030,10 @@ class QueryRunner(ABC, Generic[Q, R, CR]):
@property
def query_type(self) -> Any:
- return self._class_annotation("query") # Enforcing the type annotation of `query` at runtime
+ query_type = self._class_annotation("query") # Enforcing the type annotation of `query` at runtime
+ while isinstance(query_type, TypeAliasType):
+ query_type = query_type.__value__
+ return query_type
@property
def cached_response_type(self) -> type[CR]:
@@ -2363,9 +2404,8 @@ class QueryRunner(ABC, Generic[Q, R, CR]):
# __init__; let them refresh it now that we know the real user. Only on an
# actual change - re-running for the same user would needlessly rebuild that
# state and drop a preloaded access-control snapshot.
- if user is not None and user is not self.user:
- self.user = user
- self._on_user_changed()
+ if user is not None:
+ self.set_user(user)
start_time = perf_counter()
cache_key = self.get_cache_key()
self._phase = RunPhase(name="prepare")
diff --git a/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py b/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py
index 8590a4ace473..ddb68f41bc12 100644
--- a/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py
+++ b/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py
@@ -26,7 +26,6 @@ from posthog.schema import (
from posthog.hogql import ast
from posthog.hogql.constants import HogQLGlobalSettings
-from posthog.hogql.modifiers import create_default_modifiers_for_team
from posthog.hogql.query import execute_hogql_query
from posthog.clickhouse.query_tagging import Product, tag_queries, tags_context
@@ -364,7 +363,7 @@ class ExperimentExposuresQueryRunner(ExperimentResultsCacheMixin, QueryRunner):
team=self.team,
user=self.user,
timings=self.timings,
- modifiers=create_default_modifiers_for_team(self.team),
+ modifiers=self.modifiers,
settings=HogQLGlobalSettings(max_execution_time=600),
)
diff --git a/products/experiments/backend/hogql_queries/experiment_query_runner.py b/products/experiments/backend/hogql_queries/experiment_query_runner.py
index 0a03b0110b34..2fcb7ca5027a 100644
--- a/products/experiments/backend/hogql_queries/experiment_query_runner.py
+++ b/products/experiments/backend/hogql_queries/experiment_query_runner.py
@@ -27,7 +27,6 @@ from posthog.hogql import ast
from posthog.hogql.constants import HogQLGlobalSettings
from posthog.hogql.context import HogQLContext
from posthog.hogql.database.database import Database
-from posthog.hogql.modifiers import create_default_modifiers_for_team
from posthog.hogql.query import execute_hogql_query
from posthog.clickhouse.query_tagging import Product, tag_queries, tags_context
@@ -414,16 +413,7 @@ class ExperimentQueryRunner(ExperimentResultsCacheMixin, QueryRunner):
experiment_scan_date_from=self.date_range.date_from,
experiment_scan_date_to=self.date_range.date_to,
)
- experiment_query_debug = get_experiment_query_debug(
- experiment_query_ast,
- self.team,
- user=self.user,
- bypass_warehouse_access_control=self.bypass_warehouse_access_control,
- )
- self.hogql = experiment_query_debug[0]
- self.clickhouse_sql = experiment_query_debug[1]
-
- modifiers = create_default_modifiers_for_team(self.team)
+ modifiers = self.modifiers.model_copy()
if posthoganalytics.feature_enabled(
"hogql-session-id-pushdown",
str(self.team.id),
@@ -434,6 +424,16 @@ class ExperimentQueryRunner(ExperimentResultsCacheMixin, QueryRunner):
):
modifiers.sessionIdPushdown = True
+ experiment_query_debug = get_experiment_query_debug(
+ experiment_query_ast,
+ self.team,
+ user=self.user,
+ bypass_warehouse_access_control=self.bypass_warehouse_access_control,
+ modifiers=modifiers,
+ )
+ self.hogql = experiment_query_debug[0]
+ self.clickhouse_sql = experiment_query_debug[1]
+
settings = HogQLGlobalSettings(
max_execution_time=self.max_execution_time,
max_bytes_before_external_group_by=MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY,
diff --git a/products/experiments/backend/hogql_queries/utils.py b/products/experiments/backend/hogql_queries/utils.py
index 7b6778c4f6fc..f404f2a7e344 100644
--- a/products/experiments/backend/hogql_queries/utils.py
+++ b/products/experiments/backend/hogql_queries/utils.py
@@ -16,6 +16,7 @@ from posthog.schema import (
ExperimentStatsValidationFailure,
ExperimentVariantResultBayesian,
ExperimentVariantResultFrequentist,
+ HogQLQueryModifiers,
)
from posthog.hogql import ast
@@ -55,6 +56,7 @@ def get_experiment_query_debug(
*,
user: Optional[User] = None,
bypass_warehouse_access_control: bool = False,
+ modifiers: HogQLQueryModifiers | None = None,
) -> tuple[str, str]:
"""
Return (hogql, clickhouse_sql) for debugging.
@@ -67,7 +69,7 @@ def get_experiment_query_debug(
team=team,
user=user,
bypass_warehouse_access_control=bypass_warehouse_access_control,
- modifiers=create_default_modifiers_for_team(team),
+ modifiers=create_default_modifiers_for_team(team, modifiers),
)
clickhouse_sql_with_params, clickhouse_context_with_values = executor.generate_clickhouse_sql()
clickhouse_sql = substitute_params_for_display(clickhouse_sql_with_params, clickhouse_context_with_values.values)
diff --git a/products/product_analytics/backend/hogql_queries/funnels/funnels_query_runner.py b/products/product_analytics/backend/hogql_queries/funnels/funnels_query_runner.py
index 8457315290c4..df7906ba66fe 100644
--- a/products/product_analytics/backend/hogql_queries/funnels/funnels_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/funnels/funnels_query_runner.py
@@ -98,6 +98,10 @@ class FunnelsQueryRunner(AnalyticsQueryRunner[FunnelsQueryResponse]):
user=user,
)
+ def _on_user_changed(self) -> None:
+ super()._on_user_changed()
+ self.context.set_user(self.user)
+
def validators(self) -> Sequence[QueryValidationRule[FunnelsQuery]]:
return (
RequireAtLeastTwoFunnelSteps(),
diff --git a/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_insight_actors.py b/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_insight_actors.py
index 6b29d9045057..a89004260a6f 100644
--- a/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_insight_actors.py
+++ b/products/product_analytics/backend/hogql_queries/funnels/test/test_funnel_insight_actors.py
@@ -1,4 +1,4 @@
-from typing import Any, Optional
+from typing import Any, Optional, cast
import time_machine
from posthog.test.base import (
@@ -8,14 +8,43 @@ from posthog.test.base import (
_create_person,
snapshot_clickhouse_queries,
)
+from unittest.mock import patch
-from posthog.schema import HogQLQueryModifiers
+from django.test import SimpleTestCase, override_settings
+from parameterized import parameterized
+from pydantic import BaseModel
+
+from posthog.schema import (
+ ActorsQuery,
+ EventsNode,
+ EventsQuery,
+ FunnelsActorsQuery,
+ FunnelsFilter,
+ FunnelsQuery,
+ HogQLQueryModifiers,
+ InsightActorsQuery,
+ InsightActorsQueryOptions,
+ PersonsOnEventsMode,
+)
+
+from posthog.hogql.context import HogQLContext
from posthog.hogql.query import execute_hogql_query
+from posthog.clickhouse.workload import Workload
+from posthog.hogql_queries.actor_strategies import SessionStrategy
+from posthog.hogql_queries.actors_query_runner import ActorsQueryRunner
+from posthog.hogql_queries.events_query_runner import EventsQueryRunner
+from posthog.hogql_queries.insight_actors_query_options_runner import InsightActorsQueryOptionsRunner
+from posthog.hogql_queries.insight_actors_query_runner import InsightActorsQueryRunner
+from posthog.hogql_queries.query_runner import QueryRunner, get_query_runner, get_query_runner_or_none
+from posthog.models import Organization, Team, User
from posthog.models.group.util import create_group
+from posthog.models.instance_setting import override_instance_config
from posthog.test.test_utils import create_group_type_mapping_without_created_at
+from products.product_analytics.backend.hogql_queries.funnels.funnels_query_runner import FunnelsQueryRunner
+
class TestFunnelInsightActors(ClickhouseTestMixin, APIBaseTest):
maxDiff = None
@@ -134,3 +163,130 @@ class TestFunnelInsightActors(ClickhouseTestMixin, APIBaseTest):
],
response.results,
)
+
+
+@override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=False, CLICKHOUSE_NATIVE_EVENTS_HOST="native.example.com")
+class TestNativeEventsActorRouting(SimpleTestCase):
+ def setUp(self) -> None:
+ self.team = Team(id=44, modifiers={}, organization=Organization(id=1))
+ self.user = User(id=123, is_staff=True, is_active=True)
+ self.modifiers = HogQLQueryModifiers(personsOnEventsMode=PersonsOnEventsMode.DISABLED)
+ self.enterContext(
+ patch(
+ "posthog.models.event.new_events_schema.get_instance_setting",
+ side_effect={
+ "CLICKHOUSE_NATIVE_EVENTS_TEAMS": [44],
+ "CLICKHOUSE_NATIVE_EVENTS_HOST": "native.example.com",
+ }.get,
+ )
+ )
+ self.enterContext(
+ patch(
+ "products.web_analytics.backend.hogql_queries.cookieless_flag.resolve_cookieless_traffic_is_regular_modifier",
+ return_value=True,
+ )
+ )
+
+ @parameterized.expand(
+ [(kind, True, None) for kind in ("InsightActorsQuery", "InsightActorsQueryOptions", "ActorsQuery")]
+ + [(kind, True, False) for kind in ("InsightActorsQuery", "ActorsQuery")]
+ + [(kind, None, True) for kind in ("InsightActorsQuery", "ActorsQuery")]
+ + [(kind, None, None) for kind in ("InsightActorsQuery", "ActorsQuery")]
+ + [("EventsQuery", choice, None) for choice in (None, False, True)]
+ )
+ def test_actor_queries_inherit_the_source_route_unless_explicitly_overridden(
+ self, kind: str, source_choice: bool | None, wrapper_choice: bool | None
+ ) -> None:
+ modifiers = self.modifiers.model_copy(update={"useNewEventsCluster": source_choice})
+ source = FunnelsQuery(series=[EventsNode(event="example"), EventsNode(event="finished")], modifiers=modifiers)
+ actors = FunnelsActorsQuery(source=source, funnelStep=1)
+ query: BaseModel
+ if kind == "InsightActorsQuery":
+ query = InsightActorsQuery(source=source, modifiers=HogQLQueryModifiers())
+ elif kind == "ActorsQuery":
+ query = ActorsQuery(source=actors, modifiers=HogQLQueryModifiers())
+ elif kind == "InsightActorsQueryOptions":
+ query = InsightActorsQueryOptions(source=actors)
+ else:
+ source.modifiers = (
+ modifiers.model_copy(update={"convertToProjectTimezone": True}) if source_choice is not None else None
+ )
+ self.team.timezone = "Europe/Amsterdam"
+ query = EventsQuery(
+ select=["timestamp"],
+ source=InsightActorsQuery(source=source),
+ modifiers=self.modifiers.model_copy(update={"convertToProjectTimezone": False}),
+ )
+ if wrapper_choice is not None:
+ assert isinstance(query, (ActorsQuery, InsightActorsQuery))
+ query.modifiers = self.modifiers.model_copy(
+ update={"useNewEventsSchema": wrapper_choice, "useNewEventsCluster": wrapper_choice}
+ )
+ original = query.model_dump()
+ runner = get_query_runner(query, self.team, user=self.user)
+ native = source_choice if wrapper_choice is None else wrapper_choice
+ self.assertEqual(runner.modifiers.personsOnEventsMode, PersonsOnEventsMode.DISABLED)
+ self.assertIs(bool(runner.modifiers.useNewEventsSchema), bool(native))
+ self.assertIs(runner.modifiers.useNewEventsCluster, native)
+ if kind == "EventsQuery":
+ self.assertIs(runner.modifiers.convertToProjectTimezone, False)
+ self.assertEqual(query.model_dump(), original)
+
+
+class TestActorExecutionUserContext(APIBaseTest):
+ def test_insight_actors_runner_builds_from_a_dict(self) -> None:
+ source = FunnelsQuery(series=[EventsNode(event="example"), EventsNode(event="finished")])
+ runner = get_query_runner(InsightActorsQuery(source=source).model_dump(), self.team, user=self.user)
+ self.assertIsInstance(runner.query, InsightActorsQuery)
+
+ @override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=False, CLICKHOUSE_NATIVE_EVENTS_HOST="native.example.com")
+ def test_session_actors_hydrate_on_the_selected_cluster(self) -> None:
+ source = FunnelsQuery(
+ series=[EventsNode(event="started"), EventsNode(event="finished")],
+ funnelsFilter=FunnelsFilter(funnelAggregateByHogQL="properties.$session_id"),
+ modifiers=HogQLQueryModifiers(useNewEventsSchema=True, useNewEventsCluster=True),
+ )
+ with (
+ override_instance_config("CLICKHOUSE_NATIVE_EVENTS_HOST", "native.example.com"),
+ override_instance_config("CLICKHOUSE_NATIVE_EVENTS_TEAMS", [self.team.pk]),
+ patch("posthog.hogql.query.sync_execute", return_value=([], [])) as execute,
+ ):
+ runner = get_query_runner(ActorsQuery(source=FunnelsActorsQuery(source=source, funnelStep=1)), self.team)
+ assert isinstance(runner, ActorsQueryRunner) and isinstance(runner.strategy, SessionStrategy)
+ runner.strategy.get_actors(["a-session"])
+ self.assertEqual(execute.call_args.kwargs["workload"], Workload.NEW_EVENTS_CLUSTER)
+
+ @parameterized.expand([("ActorsQuery",), ("InsightActorsQuery",), ("InsightActorsQueryOptions",), ("EventsQuery",)])
+ def test_nested_query_context_follows_execution_user(self, kind: str) -> None:
+ source = FunnelsQuery(series=[EventsNode(event="started"), EventsNode(event="finished")])
+ actors = FunnelsActorsQuery(source=source, funnelStep=1)
+ queries = {
+ "ActorsQuery": ActorsQuery(source=actors),
+ "InsightActorsQuery": InsightActorsQuery(source=source),
+ "InsightActorsQueryOptions": InsightActorsQueryOptions(source=actors),
+ "EventsQuery": EventsQuery(select=["*"], source=InsightActorsQuery(source=source)),
+ }
+ runner = get_query_runner_or_none(query=queries[kind], team=self.team, user=self.user)
+ assert runner is not None
+
+ def source_runner() -> QueryRunner:
+ if isinstance(runner, ActorsQueryRunner):
+ assert runner.source_query_runner is not None
+ return runner.source_query_runner
+ assert isinstance(runner, (InsightActorsQueryRunner, InsightActorsQueryOptionsRunner, EventsQueryRunner))
+ return runner.source_runner
+
+ def execution_context() -> HogQLContext:
+ child = source_runner()
+ if isinstance(runner, ActorsQueryRunner):
+ return child.build_hogql_context()
+ if isinstance(runner, EventsQueryRunner):
+ child = cast(InsightActorsQueryRunner, child).source_runner
+ return cast(FunnelsQueryRunner, child).context.hogql_context
+
+ child = source_runner()
+ assert execution_context().user is self.user
+ other_user = User.objects.create_and_join(self.organization, "[email protected]", None)
+ runner.set_user(other_user)
+ assert source_runner() is child
+ assert execution_context().user is other_user
diff --git a/products/product_analytics/backend/hogql_queries/trends/test/test_trend_validation_rules.py b/products/product_analytics/backend/hogql_queries/trends/test/test_trend_validation_rules.py
index e0a7dad0f81d..b2559f257124 100644
--- a/products/product_analytics/backend/hogql_queries/trends/test/test_trend_validation_rules.py
+++ b/products/product_analytics/backend/hogql_queries/trends/test/test_trend_validation_rules.py
@@ -1,5 +1,7 @@
from posthog.test.base import BaseTest
-from unittest.mock import MagicMock
+from unittest.mock import MagicMock, patch
+
+from django.test import override_settings
from parameterized import parameterized
from rest_framework.exceptions import ValidationError
@@ -13,13 +15,16 @@ from posthog.schema import (
DataWarehouseNode,
DateRange,
EventsNode,
+ HogQLQueryModifiers,
MultipleBreakdownType,
PropertyMathType,
TrendsFilter,
TrendsQuery,
)
+from posthog.clickhouse.workload import Workload
from posthog.hogql_queries.validation.validation import QueryValidationContext
+from posthog.models.instance_setting import override_instance_config
from products.product_analytics.backend.hogql_queries.trends.trend_validation_rules import (
DisallowDaysOfWeekWithSmoothing,
@@ -436,3 +441,23 @@ class TestTrendsQueryRunnerSeriesFanOut(BaseTest):
runner = TrendsQueryRunner(query=query, team=self.team)
self.assertEqual(len(runner.series), 150)
+
+ @parameterized.expand([(False,), (True,)])
+ @override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=False, CLICKHOUSE_NATIVE_EVENTS_HOST="native.example.com")
+ def test_actor_options_execute_on_the_selected_cluster(self, native: bool) -> None:
+ query = TrendsQuery(
+ series=[EventsNode(event="$pageview")],
+ dateRange=DateRange(date_from="2024-01-01", date_to="2024-01-08"),
+ breakdownFilter=BreakdownFilter(breakdown="$browser", breakdown_type=BreakdownType.EVENT),
+ modifiers=HogQLQueryModifiers(useNewEventsCluster=native, useNewEventsSchema=native),
+ )
+ with (
+ override_instance_config("CLICKHOUSE_NATIVE_EVENTS_HOST", "native.example.com"),
+ override_instance_config("CLICKHOUSE_NATIVE_EVENTS_TEAMS", [self.team.pk]),
+ patch("posthog.hogql.query.sync_execute", return_value=([], [])) as execute,
+ ):
+ result = TrendsQueryRunner(query=query, team=self.team).to_actors_query_options()
+ assert result.breakdown == []
+ execute.assert_called_once()
+ assert execute.call_args.kwargs["workload"] == (Workload.NEW_EVENTS_CLUSTER if native else Workload.DEFAULT)
+ assert ("events_json" in execute.call_args.args[0]) is native
diff --git a/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py b/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py
index 0cbbdb6c0f2f..d538643b8eae 100644
--- a/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/trends/trends_query_runner.py
@@ -358,8 +358,8 @@ class TrendsQueryRunner(AnalyticsQueryRunner[TrendsQueryResponse]):
workload=self.workload,
ch_user=self.ch_user,
settings=self.hogql_settings,
+ modifiers=self.modifiers,
# timings=timings,
- # modifiers=modifiers,
)
breakdown_values = [
diff --git a/products/web_analytics/backend/hogql_queries/page_url_search_query_runner.py b/products/web_analytics/backend/hogql_queries/page_url_search_query_runner.py
index e01fb2926fbb..976ac8e4a962 100644
--- a/products/web_analytics/backend/hogql_queries/page_url_search_query_runner.py
+++ b/products/web_analytics/backend/hogql_queries/page_url_search_query_runner.py
@@ -80,6 +80,7 @@ class PageUrlSearchQueryRunner(WebAnalyticsQueryRunner[WebPageURLSearchQueryResp
team=self.team,
user=self.user,
query_type="WebAnalyticsPageURLSearch",
+ modifiers=self.modifiers,
context=HogQLContext(
team_id=self.team.pk,
timings=self.timings,
diff --git a/products/web_analytics/backend/hogql_queries/test/test_web_vitals_timeseries_lazy_precompute.py b/products/web_analytics/backend/hogql_queries/test/test_web_vitals_timeseries_lazy_precompute.py
index 92651c4687c9..f90c7f6f94cf 100644
--- a/products/web_analytics/backend/hogql_queries/test/test_web_vitals_timeseries_lazy_precompute.py
+++ b/products/web_analytics/backend/hogql_queries/test/test_web_vitals_timeseries_lazy_precompute.py
@@ -17,6 +17,7 @@ from posthog.schema import (
DateRange,
EventPropertyFilter,
EventsNode,
+ HogQLQueryModifiers,
IntervalType,
PropertyOperator,
TrendsFilter,
@@ -26,9 +27,11 @@ from posthog.schema import (
WebVitalsQuery,
)
+from posthog.api.services.query import process_query_model
from posthog.clickhouse.client import sync_execute
from posthog.clickhouse.query_tagging import Feature, Product, reset_query_tags, tag_queries
from posthog.hogql_queries.query_runner import get_query_runner_or_none
+from posthog.models.instance_setting import override_instance_config
from products.analytics_platform.backend.models.preaggregation_job import PreaggregationJob
from products.product_analytics.backend.facade.queries import TrendsQueryRunner
@@ -74,6 +77,31 @@ def _vitals_query(**overrides: Any) -> WebVitalsQuery:
class TestVitalsTimeseriesGate(APIBaseTest):
+ @parameterized.expand([(path, outer) for path in ("runner", "source_fallback") for outer in (False, True)])
+ @override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=False, CLICKHOUSE_NATIVE_EVENTS_HOST="native.example.com")
+ def test_wrapper_cluster_choice_overrides_the_source(self, path: str, outer: bool) -> None:
+ query = _vitals_query(
+ modifiers=HogQLQueryModifiers(useNewEventsCluster=outer),
+ source_overrides={"modifiers": HogQLQueryModifiers(useNewEventsCluster=not outer)},
+ )
+ with (
+ override_instance_config("CLICKHOUSE_NATIVE_EVENTS_HOST", "native.example.com"),
+ override_instance_config("CLICKHOUSE_NATIVE_EVENTS_TEAMS", [self.team.pk]),
+ ):
+ if path == "runner":
+ runner = WebVitalsQueryRunner(query=query, team=self.team)
+ else:
+ with (
+ patch(
+ "products.web_analytics.backend.hogql_queries.web_vitals_timeseries_lazy_precompute.is_vitals_precompute_enabled_for_team",
+ return_value=False,
+ ),
+ patch("posthog.api.services.query._run_query_runner", return_value={}) as run_source,
+ ):
+ process_query_model(self.team, query)
+ runner = run_source.call_args.args[0]
+ self.assertIs(bool(runner.modifiers.useNewEventsCluster), outer)
+
def test_canonical_tab_shape_is_servable(self) -> None:
assert vitals_timeseries_percentile(_vitals_query()) == "p90"
# The tab query the frontend builds pins the line-graph display explicitly.
diff --git a/products/web_analytics/backend/hogql_queries/web_vitals_timeseries.py b/products/web_analytics/backend/hogql_queries/web_vitals_timeseries.py
index bbb4dcec31a4..8178e40d6f06 100644
--- a/products/web_analytics/backend/hogql_queries/web_vitals_timeseries.py
+++ b/products/web_analytics/backend/hogql_queries/web_vitals_timeseries.py
@@ -2,6 +2,7 @@ from typing import Any, cast
from posthog.schema import QueryTiming, ResolvedDateRangeResponse, TrendsQuery, TrendsQueryResponse, WebVitalsQuery
+from posthog.hogql_queries.query_runner import unwrapped_source
from posthog.schema_helpers import to_dict
from products.product_analytics.backend.facade.queries import TrendsQueryRunner
@@ -38,7 +39,7 @@ class WebVitalsQueryRunner(TrendsQueryRunner):
# sources fall through to the legacy source unwrap), so this guards
# against direct/invalid construction, not the dispatch path.
raise ValueError(f"WebVitalsQueryRunner requires a TrendsQuery source, got {type(source).__name__}")
- super().__init__(query=source, **kwargs)
+ super().__init__(query=unwrapped_source(vitals_query, source), **kwargs)
def get_cache_payload(self) -> dict:
payload = super().get_cache_payload()