: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-modeling): don't charge a clickhouse describe to the resolver deadline (#73677)
Jovan Sakovic2722419a9732
diff --git a/posthog/temporal/data_modeling/activities/materialize_view.py b/posthog/temporal/data_modeling/activities/materialize_view.pyindex e168ea95d531..7c0f3ed67dcf 100644--- a/posthog/temporal/data_modeling/activities/materialize_view.py+++ b/posthog/temporal/data_modeling/activities/materialize_view.py@@ -373,13 +373,15 @@ async def hogql_table(query: str, team: Team, logger: FilteringBoundLogger, viewcontext.output_format = "ArrowStream"settings.preferred_block_size_bytes = MB_100_IN_BYTES+ # each prepare pass owns its deadline clock, so the DESCRIBE round trip above is not charged+ # to view resolutionarrow_prepared_hogql_query = await database_sync_to_async_pool(prepare_ast_for_printing)(query_node,context=context,dialect="clickhouse",stack=[],settings=settings,- resolver_factory=factory,+ resolver_factory=bounded_resolver_factory_for_view(view_name),)if arrow_prepared_hogql_query is None:diff --git a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.pyindex dc62464e2337..234601917c4b 100644--- a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py+++ b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py@@ -1,5 +1,6 @@+import asyncioimport contextlib-from collections.abc import Collection, Iterable+from collections.abc import AsyncIterator, Collection, Iterablefrom typing import Any, castimport pytest@@ -12,6 +13,8 @@ import pyarrow as paimport deltalakeimport pytest_asyncio+from posthog.hogql.resolver import ResolverFactory+from posthog.sync import database_sync_to_asyncfrom posthog.temporal.data_modeling.activities import (CreateDataModelingJobInputs,@@ -33,6 +36,7 @@ from posthog.temporal.data_modeling.activities.materialize_view import ()from products.data_modeling.backend.facade.api import compute_enrichment_hash+from products.data_modeling.backend.facade.modeling import bounded_resolver_factory_for_viewfrom products.data_modeling.backend.facade.models import (DAG,DataModelingJob,@@ -1058,6 +1062,8 @@ class TestMaterializeViewActivity:class _EmptyArrowClient:+ describe_body = b"id\tInt64\n"+def __init__(self, schema: pa.Schema):self.schema = schemaself.arrow_query_calls = 0@@ -1073,7 +1079,7 @@ class _EmptyArrowClient:@contextlib.asynccontextmanagerasync def apost_query(self, query, *data, query_parameters=None, query_id=None):if query.startswith("DESCRIBE TABLE"):- body = b"id\tInt64\n"+ body = self.describe_bodyelse:self.schema_query_calls += 1buffer = pa.BufferOutputStream()@@ -1109,3 +1115,48 @@ class TestHogqlTableEmptyResults:assert batches[0][0].num_rows == 0assert client.arrow_query_calls == 1assert client.schema_query_calls == 0+++class _SlowDescribeClient(_EmptyArrowClient):+ describe_body = b"ts\tDateTime\n"++ def __init__(self, schema: pa.Schema, describe_seconds: float):+ super().__init__(schema)+ self.describe_seconds = describe_seconds++ @contextlib.asynccontextmanager+ async def apost_query(self, query, *data, query_parameters=None, query_id=None):+ await asyncio.sleep(self.describe_seconds)+ async with super().apost_query(query, *data, query_parameters=query_parameters, query_id=query_id) as response:+ yield response+++class TestHogqlTableResolutionDeadline:+ async def test_slow_describe_does_not_exhaust_the_resolution_deadline(self, ateam):+ # regression: a DateTime column sends hogql_table back through prepare_ast_for_printing to+ # wrap the select in toTimeZone. That second pass used to share the first pass's deadline+ # anchor, so a DESCRIBE slower than the deadline made it raise ResolutionTimeoutError.+ deadline_seconds = 1.0+ client = _SlowDescribeClient(+ pa.schema([pa.field("ts", pa.timestamp("us"))]), describe_seconds=deadline_seconds + 0.2+ )++ @contextlib.asynccontextmanager+ async def fake_get_client(**kwargs: Any) -> AsyncIterator[_SlowDescribeClient]:+ yield client++ def short_deadline_factory(view_name: str | None, **kwargs: Any) -> ResolverFactory:+ return bounded_resolver_factory_for_view(view_name, **{**kwargs, "deadline_seconds": deadline_seconds})++ with (+ unittest.mock.patch(+ "posthog.temporal.data_modeling.activities.materialize_view.get_clickhouse_client", fake_get_client+ ),+ unittest.mock.patch(+ "posthog.temporal.data_modeling.activities.materialize_view.bounded_resolver_factory_for_view",+ short_deadline_factory,+ ),+ ):+ batches = [batch async for batch in hogql_table("SELECT now() AS ts", ateam, LOGGER.bind())]++ assert [name for name, _ in batches[0][1]] == ["ts"]