: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
chore(tests): capture snapshot HogQL queries at the executor (#96051)
Thomas Obermüllerc02e6d550294
diff --git a/posthog/test/base.py b/posthog/test/base.pyindex 8ebbf8170f81..19565df6d6da 100644--- a/posthog/test/base.py+++ b/posthog/test/base.py@@ -6,9 +6,10 @@ import inspectimport loggingimport datetime as dtimport resource+import threadingfrom collections.abc import Callable, Generator, Iteratorfrom concurrent.futures import ThreadPoolExecutor, as_completed-from contextlib import ExitStack, contextmanager+from contextlib import AbstractContextManager, ExitStack, contextmanagerfrom functools import wrapsfrom typing import Any, Optional, Union, cast@@ -2305,6 +2306,9 @@ def snapshot_hogql_queries(fn_or_class):@wraps(fn_or_class)def wrapped(self, *args, **kwargs):captured_queries = []+ # A paginator call reaches the executor with the page limit added, so the executor skips it.+ # Thread-local because a runner may execute its series on worker threads.+ paginating = threading.local()# Patch the execute_hogql_query method on the paginator to capture queries before resolutionoriginal_paginator_method = HogQLHasMorePaginator.execute_hogql_query@@ -2314,53 +2318,30 @@ def snapshot_hogql_queries(fn_or_class):if isinstance(query, ast.SelectQuery | ast.SelectSetQuery):captured_queries.append(clone_expr(query))- return original_paginator_method(paginator_self, query=query, **exec_kwargs)-- # Patch the module-level execute_hogql_query function for direct calls- # We need to patch it in modules that import it directly- original_module_function = hogql_query_module.execute_hogql_query+ paginating.active = True+ try:+ return original_paginator_method(paginator_self, query=query, **exec_kwargs)+ finally:+ paginating.active = False- def capture_module_execute(*exec_args, **exec_kwargs):- # Extract the query parameter - it can be positional or keyword- query = exec_kwargs.get("query") if "query" in exec_kwargs else (exec_args[0] if exec_args else None)+ # Patch the executor every call reaches, whichever way its caller imported execute_hogql_query+ original_executor_method = hogql_query_module.HogQLQueryExecutor.execute+ def capture_executor_execute(executor_self):# Capture the query AST before it gets resolved- if query and isinstance(query, ast.SelectQuery | ast.SelectSetQuery):- captured_queries.append(clone_expr(query))+ if not getattr(paginating, "active", False) and isinstance(+ executor_self.query, ast.SelectQuery | ast.SelectSetQuery+ ):+ captured_queries.append(clone_expr(executor_self.query))- return original_module_function(*exec_args, **exec_kwargs)+ return original_executor_method(executor_self)- # Import modules that use execute_hogql_query directly- patches = [+ # Annotated because the two patches have different types, which join to object.+ patches: list[AbstractContextManager[Any]] = [patch.object(HogQLHasMorePaginator, "execute_hogql_query", capture_paginator_execute),- patch.object(hogql_query_module, "execute_hogql_query", capture_module_execute),+ patch.object(hogql_query_module.HogQLQueryExecutor, "execute", capture_executor_execute),]- # Add patches for modules that import execute_hogql_query directly- try:- from products.web_analytics.backend.hogql_queries import web_overview-- if hasattr(web_overview, "execute_hogql_query"):- patches.append(patch.object(web_overview, "execute_hogql_query", capture_module_execute))- except ImportError:- pass-- try:- from products.web_analytics.backend.hogql_queries import stats_table-- if hasattr(stats_table, "execute_hogql_query"):- patches.append(patch.object(stats_table, "execute_hogql_query", capture_module_execute))- except ImportError:- pass-- try:- from posthog.hogql_queries.insights.trends import trends_query_runner-- if hasattr(trends_query_runner, "execute_hogql_query"):- patches.append(patch.object(trends_query_runner, "execute_hogql_query", capture_module_execute))- except ImportError:- pass-# Apply all patcheswith ExitStack() as stack:for p in patches:diff --git a/products/model_crossing_uses_baseline.txt b/products/model_crossing_uses_baseline.txtindex aa413f481f04..289ce0af8219 100644--- a/products/model_crossing_uses_baseline.txt+++ b/products/model_crossing_uses_baseline.txt@@ -858,8 +858,6 @@ web_analytics:backend/hogql_queries/ posthog.tasks.alerts.test.test_alert_subscrweb_analytics:backend/hogql_queries/ posthog.tasks.alerts.test.test_metrics_alerts drives(TrendsQuery) 1web_analytics:backend/hogql_queries/ posthog.tasks.alerts.test.test_trends_absolute_alerts drives(TrendsQuery) 2web_analytics:backend/hogql_queries/ posthog.tasks.alerts.test.test_trends_relative_alerts drives(TrendsQuery) 1-web_analytics:backend/hogql_queries/ posthog.test.base drives(stats_table) 1-web_analytics:backend/hogql_queries/ posthog.test.base drives(web_overview) 1web_analytics:backend/hogql_queries/ products.actions.backend.api.test.test_action drives(TrendsQuery) 9web_analytics:backend/hogql_queries/ products.alerts.backend.tests.api.test_alert drives(TrendsQuery) 7web_analytics:backend/hogql_queries/ products.cohorts.backend.models.test.test_util drives(TrendsQuery) 1