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(data-modeling): compile v2 views with the team default modifiers (#90929)

Andy Zhao77785a88f8c0
diff --git a/posthog/temporal/data_modeling/activities/materialize_view.py b/posthog/temporal/data_modeling/activities/materialize_view.py
index c28a0ef2bb20..4b214dbff004 100644
--- a/posthog/temporal/data_modeling/activities/materialize_view.py
+++ b/posthog/temporal/data_modeling/activities/materialize_view.py
@@ -18,6 +18,7 @@ from posthog.hogql.constants import HogQLGlobalSettings
from posthog.hogql.context import HogQLContext
from posthog.hogql.database.database import Database
from posthog.hogql.errors import ParsingError
+from posthog.hogql.modifiers import create_default_modifiers_for_team
from posthog.hogql.parser import parse_select
from posthog.hogql.printer import prepare_ast_for_printing, print_prepared_ast
from posthog.hogql.visitor import CloningVisitor
@@ -542,10 +543,12 @@ async def hogql_table(
settings = HogQLGlobalSettings()
settings.max_execution_time = HOGQL_INCREASED_MAX_EXECUTION_TIME
+ modifiers = await database_sync_to_async_pool(create_default_modifiers_for_team)(team)
context = HogQLContext(
team=team,
enable_select_queries=True,
limit_top_select=False,
+ modifiers=modifiers,
)
# Userless materialization context; bypass warehouse HogQL access control so the model query
# can resolve its source tables/views.
diff --git a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
index a17c0c2f90cb..c70971d09309 100644
--- a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
+++ b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
@@ -37,6 +37,7 @@ from posthog.temporal.data_modeling.activities import (
)
from posthog.temporal.data_modeling.activities.materialize_view import (
LOGGER,
+ EmptyHogQLResponseColumnsError,
InvalidNodeTypeException,
get_aws_storage_options,
get_s3_client,
@@ -1695,6 +1696,45 @@ class _EmptyArrowClient:
yield _Response(body)
+class TestHogqlTableModifiers:
+ @pytest.mark.parametrize(
+ "query,team_modifiers,expected_sql",
+ [
+ ("SELECT $is_bounce FROM sessions LIMIT 1", {"bounceRateDurationSeconds": 123}, "123"),
+ (
+ "SELECT properties.plan FROM events LIMIT 1",
+ {"propertyGroupsMode": "optimized"},
+ "properties_group_custom",
+ ),
+ ],
+ )
+ async def test_compiles_the_view_with_the_team_default_modifiers(
+ self, ateam: Team, query: str, team_modifiers: dict[str, Any], expected_sql: str
+ ) -> None:
+ ateam.modifiers = team_modifiers
+ await database_sync_to_async(ateam.save)()
+ captured_sql: str | None = None
+
+ async def fake_astream_query_as_arrow(
+ _client: Any, query: str, *args: Any, **kwargs: Any
+ ) -> AsyncIterator[pa.RecordBatch]:
+ nonlocal captured_sql
+ captured_sql = query
+ return
+ yield # type: ignore[unreachable] # makes this an async generator that yields no batches
+
+ with (
+ unittest.mock.patch(
+ "posthog.temporal.common.clickhouse.ClickHouseClient.astream_query_as_arrow",
+ fake_astream_query_as_arrow,
+ ),
+ contextlib.suppress(EmptyHogQLResponseColumnsError),
+ ):
+ _ = [batch async for batch in hogql_table(query, ateam, LOGGER.bind())]
+
+ assert captured_sql is not None and expected_sql in captured_sql
+
+
class TestHogqlTableEmptyResults:
async def test_zero_row_query_uses_the_initial_stream_schema(self, ateam):
client = _EmptyArrowClient(pa.schema([pa.field("id", pa.int64())]))