: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(aio): read session traces with global in (#121912)
Bernat Torresecc4442db056
diff --git a/posthog/hogql_queries/ai/session_query_runner.py b/posthog/hogql_queries/ai/session_query_runner.pyindex 4dc074c3858e..915f82431313 100644--- a/posthog/hogql_queries/ai/session_query_runner.py+++ b/posthog/hogql_queries/ai/session_query_runner.py@@ -30,7 +30,7 @@ from posthog.hogql_queries.ai.sentiment_evaluations import (get_sentiment_for_generation,load_generation_sentiment_evaluations_for_traces,)-from posthog.hogql_queries.ai.utils import merge_heavy_properties+from posthog.hogql_queries.ai.utils import merge_heavy_properties, with_global_in_subqueriesfrom posthog.hogql_queries.paginators import HogQLHasMorePaginatorfrom posthog.hogql_queries.query_runner import AnalyticsQueryRunnerfrom posthog.hogql_queries.utils.query_date_range import QueryDateRange@@ -282,7 +282,7 @@ class SessionQueryRunner(AnalyticsQueryRunner[SessionQueryResponse]):ORDER BY first_timestamp DESC""",)- return cast(ast.SelectQuery, query)+ return cast(ast.SelectQuery, with_global_in_subqueries(query))def get_cache_payload(self) -> dict[str, Any]:payload = {**super().get_cache_payload(), "schema_version": 2}diff --git a/posthog/hogql_queries/ai/test/test_session_query_runner.py b/posthog/hogql_queries/ai/test/test_session_query_runner.pyindex 4e744b6fd39a..5d94a6b8f705 100644--- a/posthog/hogql_queries/ai/test/test_session_query_runner.py+++ b/posthog/hogql_queries/ai/test/test_session_query_runner.py@@ -130,6 +130,7 @@ class TestSessionQueryRunner(ClickhouseTestMixin, BaseTest):select_queries = _select_queries_without_metadata(queries)self.assertEqual(len(select_queries), 1)self.assertIn("ai_events", select_queries[0])+ self.assertIn("globalIn(ai_events.trace_id, (SELECT", select_queries[0])self.assertEqual(len(response.results), 1)trace = response.results[0]self.assertEqual(trace.id, "trace-ai-events")diff --git a/posthog/hogql_queries/ai/utils.py b/posthog/hogql_queries/ai/utils.pyindex d11449f56920..d046ca6654d8 100644--- a/posthog/hogql_queries/ai/utils.py+++ b/posthog/hogql_queries/ai/utils.py@@ -8,6 +8,7 @@ import orjsonfrom posthog.schema import PropertyOperatorfrom posthog.hogql import ast+from posthog.hogql.visitor import TraversingVisitorfrom posthog.caching.utils import ThresholdMode, is_stale@@ -184,3 +185,19 @@ class TaxonomyCacheMixin(ABC):def cache_target_age(self, last_refresh: Optional[datetime], lazy: bool = False) -> Optional[datetime]:return None+++class _GlobalInSubqueries(TraversingVisitor):+ def visit_compare_operation(self, node: ast.CompareOperation) -> None:+ if node.op == ast.CompareOperationOp.In and isinstance(node.right, ast.SelectQuery):+ node.op = ast.CompareOperationOp.GlobalIn+ super().visit_compare_operation(node)+++def with_global_in_subqueries(+ query: ast.SelectQuery | ast.SelectSetQuery,+) -> ast.SelectQuery | ast.SelectSetQuery:+ """Turn `x IN (SELECT ...)` into GLOBAL IN. A plain IN subquery over the Distributed ai_events+ table does not narrow the outer read, so the outer read scans the whole table."""+ _GlobalInSubqueries().visit(query)+ return querydiff --git a/posthog/temporal/ai_observability/run_session_evaluation.py b/posthog/temporal/ai_observability/run_session_evaluation.pyindex eda5baab8559..7358c08a632c 100644--- a/posthog/temporal/ai_observability/run_session_evaluation.py+++ b/posthog/temporal/ai_observability/run_session_evaluation.py@@ -25,6 +25,7 @@ from posthog.clickhouse.query_tagging import Product, tags_contextfrom posthog.dataclasses import frozenfrom posthog.hogql_queries.ai.ai_table_resolver import query_ai_eventsfrom posthog.hogql_queries.ai.session_query_runner import SessionQueryRunner+from posthog.hogql_queries.ai.utils import with_global_in_subqueriesfrom posthog.models.team import Teamfrom posthog.sync import database_sync_to_asyncfrom posthog.temporal.ai_observability.evaluation_event_io import extract_event_io@@ -165,7 +166,7 @@ def _sum_session_payload_bytes() -> int:with tags_context(product=Product.LLM_ANALYTICS):result = query_ai_events(- query=parse_select(_SESSION_PAYLOAD_BYTES_SQL),+ query=with_global_in_subqueries(parse_select(_SESSION_PAYLOAD_BYTES_SQL)),placeholders={"session_id": ast.Constant(value=session_id),"date_from": ast.Constant(value=date_from),@@ -220,7 +221,7 @@ def _count_session_events("""with tags_context(product=Product.LLM_ANALYTICS):result = query_ai_events(- query=parse_select(_SESSION_EVENT_COUNT_SQL),+ query=with_global_in_subqueries(parse_select(_SESSION_EVENT_COUNT_SQL)),placeholders={"session_id": ast.Constant(value=session_id),"date_from": ast.Constant(value=date_from),diff --git a/posthog/temporal/ai_observability/test_run_session_evaluation.py b/posthog/temporal/ai_observability/test_run_session_evaluation.pyindex 7981289a4ace..4b7438a329db 100644--- a/posthog/temporal/ai_observability/test_run_session_evaluation.py+++ b/posthog/temporal/ai_observability/test_run_session_evaluation.py@@ -12,6 +12,7 @@ from temporalio.exceptions import ApplicationErrorfrom posthog.schema import LLMTrace, LLMTraceEventfrom posthog.hogql.constants import MAX_SELECT_TRACES_LIMIT_EXPORT+from posthog.hogql.placeholders import replace_placeholdersfrom posthog.temporal.ai_observability.evaluation_hog import run_hog_eval_for_eventfrom posthog.temporal.ai_observability.evaluation_llm_judge import (@@ -28,6 +29,7 @@ from posthog.temporal.ai_observability.run_session_evaluation import (SessionFetchOutcome,_count_session_events,_SessionEventCount,+ _sum_session_payload_bytes,build_session_hog_globals,execute_session_hog_eval_activity,execute_session_llm_judge_activity,@@ -235,6 +237,20 @@ class TestCountSessionEvents:assert result.first_seen == first_seenassert mock_query_ai_events.call_args.kwargs["fall_back_to_events"] is False+ @pytest.mark.parametrize(+ "fetch, rows",+ [(_count_session_events, [[7, datetime(2026, 7, 1, tzinfo=UTC)]]), (_sum_session_payload_bytes, [[100]])],+ )+ def test_reads_the_session_traces_with_global_in(self, fetch, rows):+ with patch(+ "posthog.temporal.ai_observability.run_session_evaluation.query_ai_events",+ return_value=Mock(results=rows),+ ) as mock_query_ai_events:+ fetch(Mock(), "s-1", FROZEN_NOW, FROZEN_NOW)++ kwargs = mock_query_ai_events.call_args.kwargs+ assert "globalIn(trace_id, (SELECT" in replace_placeholders(kwargs["query"], kwargs["placeholders"]).to_hogql()+class TestRunHogEvalOverRecentSessions:@time_machine.travel(FROZEN_NOW, tick=False)