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(hogql): keep property-access base column in projection pushdown (#72522)

posthog[bot]a015c455d305
diff --git a/posthog/hogql/transforms/projection_pushdown.py b/posthog/hogql/transforms/projection_pushdown.py
index 262e18d82d94..d8cff24f46e0 100644
--- a/posthog/hogql/transforms/projection_pushdown.py
+++ b/posthog/hogql/transforms/projection_pushdown.py
@@ -203,16 +203,24 @@ class ProjectionPushdownOptimizer(TraversingVisitor):
def visit_field(self, node: ast.Field) -> ast.Field:
"""Record demand when field references subquery or CTE column"""
+ field_type = node.type
+ # A property read (`col.some_key`) still demands its base blob column. Its node type is a
+ # PropertyType wrapping the base FieldType, so unwrap to that base — otherwise the column is
+ # never recorded as demanded and gets pruned from an asterisk-expanded subquery/CTE, leaving a
+ # dangling reference that later blows up property lowering.
+ if isinstance(field_type, ast.PropertyType):
+ field_type = field_type.field_type
+
# Handle both FieldType and ExpressionFieldType
- if not isinstance(node.type, ast.FieldType | ast.ExpressionFieldType):
+ if not isinstance(field_type, ast.FieldType | ast.ExpressionFieldType):
return node
- table_type = node.type.table_type
+ table_type = field_type.table_type
if isinstance(table_type, ast.SelectQueryType | ast.SelectQueryAliasType | ast.SelectSetQueryType):
subquery = self._get_subquery(table_type)
if subquery:
- self.demands[id(subquery)].add(node.type.name)
+ self.demands[id(subquery)].add(field_type.name)
elif isinstance(table_type, ast.CTETableType | ast.CTETableAliasType):
# For CTE types, get the underlying SelectQueryType and demand from it
cte_select_query_type = (
@@ -222,7 +230,7 @@ class ProjectionPushdownOptimizer(TraversingVisitor):
)
cte_query = self._get_subquery(cte_select_query_type)
if cte_query:
- self.demands[id(cte_query)].add(node.type.name)
+ self.demands[id(cte_query)].add(field_type.name)
return node
diff --git a/posthog/hogql/transforms/test/test_projection_pushdown.py b/posthog/hogql/transforms/test/test_projection_pushdown.py
index c358d8c3b04c..758300578433 100644
--- a/posthog/hogql/transforms/test/test_projection_pushdown.py
+++ b/posthog/hogql/transforms/test/test_projection_pushdown.py
@@ -1,6 +1,8 @@
import pytest
from posthog.test.base import BaseTest
+from parameterized import parameterized
+
from posthog.schema import HogQLQueryModifiers
from posthog.hogql import ast
@@ -611,3 +613,21 @@ class TestProjectionPushdown(BaseTest):
assert isinstance(base_cte, ast.SelectQuery)
base_cols = {self._col_name(col) for col in base_cte.select}
assert base_cols == {"a", "b", "d"}, f"CTE dropped columns demanded by sibling branches: got {base_cols}"
+
+ @parameterized.expand(
+ [
+ ("subquery", "SELECT event, properties.foo FROM (SELECT * FROM events) AS sub"),
+ ("cte", "WITH cte AS (SELECT * FROM events) SELECT event, properties.foo FROM cte"),
+ ]
+ )
+ def test_property_access_keeps_base_column(self, _name: str, query_str: str):
+ # A blob column read only via `properties.foo` still demands `properties`. Its node type is a
+ # PropertyType, not a FieldType, so if demand collection ignores it the `*` pruner drops
+ # `properties` (only `event` is demanded), leaving a dangling reference that later crashes
+ # property lowering with 'Field "properties" not found on table Table'.
+ optimized = self._optimize(query_str)
+
+ inner = optimized.ctes["cte"].expr if optimized.ctes else optimized.select_from.table
+ assert isinstance(inner, ast.SelectQuery)
+ column_names = {self._col_name(col) for col in inner.select}
+ assert column_names == {"event", "properties"}, f"pruner dropped the property base column: got {column_names}"