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(aio): skip materialized person columns on ai_events reads (#128220)

Bernat Torres6ad2301a3f52
diff --git a/posthog/hogql_queries/ai/ai_table_resolver.py b/posthog/hogql_queries/ai/ai_table_resolver.py
index bda5e7a938cc..6d49fac21905 100644
--- a/posthog/hogql_queries/ai/ai_table_resolver.py
+++ b/posthog/hogql_queries/ai/ai_table_resolver.py
@@ -4,6 +4,8 @@ from typing import TYPE_CHECKING, Any
from prometheus_client import Counter, Histogram
+from posthog.schema import HogQLQueryModifiers, MaterializationMode
+
from posthog.hogql import ast
from posthog.hogql.context import HogQLContext
from posthog.hogql.database.database import Database
@@ -34,7 +36,6 @@ AI_EVENTS_QUERY_DURATION_SECONDS = Histogram(
if TYPE_CHECKING:
from posthog.hogql.constants import HogQLGlobalSettings, LimitContext
- from posthog.hogql.modifiers import HogQLQueryModifiers
from posthog.hogql.timings import HogQLTimings
from posthog.clickhouse.client.connection import Workload
@@ -70,6 +71,14 @@ class AIEventsNotFoundError(AIEventsUnavailableError):
"""The requested AI events were not found in either ai_events or the shared events table."""
+def _without_materialized_columns(modifiers: HogQLQueryModifiers | None) -> HogQLQueryModifiers:
+ # The materialized-column registry lists the columns of the main cluster. On the ai-events cluster, `person` is a
+ # Distributed shim created from the base table DDL, so it has no mat_*/pmat_* columns and a read of one fails.
+ ai_modifiers = modifiers.model_copy() if modifiers is not None else HogQLQueryModifiers()
+ ai_modifiers.materializationMode = MaterializationMode.DISABLED
+ return ai_modifiers
+
+
def query_ai_events(
query: ast.SelectQuery | ast.SelectSetQuery,
placeholders: dict[str, ast.Expr],
@@ -146,7 +155,11 @@ def query_ai_events(
tag_queries(ai_query_source="dedicated_table")
ai_placeholders = {k: rewrite_expr_for_ai_events_table(v) for k, v in placeholders.items()}
with AI_EVENTS_QUERY_DURATION_SECONDS.labels(source="dedicated_table").time():
- result = execute_hogql_query(query=query, placeholders=ai_placeholders, **kwargs)
+ result = execute_hogql_query(
+ query=query,
+ placeholders=ai_placeholders,
+ **{**kwargs, "modifiers": _without_materialized_columns(modifiers)},
+ )
if result.results:
AI_EVENTS_QUERY_TOTAL.labels(source="dedicated_table").inc()
return result
diff --git a/posthog/hogql_queries/ai/test/test_ai_table_resolver.py b/posthog/hogql_queries/ai/test/test_ai_table_resolver.py
index 8f8c278d70ea..4d29bfe5def0 100644
--- a/posthog/hogql_queries/ai/test/test_ai_table_resolver.py
+++ b/posthog/hogql_queries/ai/test/test_ai_table_resolver.py
@@ -1,7 +1,11 @@
import pytest
+from posthog.test.base import BaseTest, ClickhouseTestMixin, _create_person, materialized
from unittest.mock import Mock, patch
+from posthog.schema import HogQLQueryModifiers, MaterializationMode
+
from posthog.hogql import ast
+from posthog.hogql.parser import parse_select
from posthog.hogql_queries.ai.ai_table_resolver import (
AIEventsExpiredError,
@@ -9,6 +13,7 @@ from posthog.hogql_queries.ai.ai_table_resolver import (
is_ai_events_enabled,
query_ai_events,
)
+from posthog.models.ai_events.test_util import bulk_create_ai_events
class TestIsAiEventsEnabled:
@@ -195,7 +200,7 @@ class TestQueryAiEvents:
team = Mock(id=1, organization_id="org")
timings = Mock()
- modifiers = Mock()
+ modifiers = HogQLQueryModifiers(debug=True)
limit_context = Mock()
settings = Mock()
workload = Mock()
@@ -216,7 +221,7 @@ class TestQueryAiEvents:
kwargs = mock_execute.call_args.kwargs
assert kwargs["timings"] is timings
- assert kwargs["modifiers"] is modifiers
+ assert kwargs["modifiers"] == HogQLQueryModifiers(debug=True, materializationMode=MaterializationMode.DISABLED)
assert kwargs["limit_context"] is limit_context
assert kwargs["settings"] is settings
assert kwargs["workload"] is workload
@@ -238,8 +243,34 @@ class TestQueryAiEvents:
kwargs = mock_execute.call_args.kwargs
assert "timings" not in kwargs
- assert "modifiers" not in kwargs
+ assert kwargs["modifiers"].model_dump(exclude_none=True) == {"materializationMode": "disabled"}
assert "limit_context" not in kwargs
assert "settings" not in kwargs
assert "workload" not in kwargs
assert kwargs["user"] is None
+
+
+class TestQueryAiEventsPersonJoin(ClickhouseTestMixin, BaseTest):
+ def test_reads_person_properties_without_materialized_columns(self):
+ with materialized("person", "email"):
+ _create_person(distinct_ids=["person1"], team=self.team, properties={"email": "[email protected]"})
+ bulk_create_ai_events(
+ [
+ {
+ "event": "$ai_generation",
+ "team": self.team,
+ "distinct_id": "person1",
+ "properties": {"$ai_trace_id": "trace-1"},
+ }
+ ]
+ )
+
+ result = query_ai_events(
+ query=parse_select("SELECT person.properties.email FROM posthog.ai_events"),
+ placeholders={},
+ team=self.team,
+ query_type="TestQuery",
+ )
+
+ assert result.results == [("[email protected]",)]
+ assert "pmat_email" not in result.clickhouse