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.

perf(notebooks): share one HogQL context across a frame's print passes (#83140)

Sinan Kucukkoseler9b75348f572e
diff --git a/products/notebooks/backend/temporal/frame_materialize.py b/products/notebooks/backend/temporal/frame_materialize.py
index 8d258a9ff3b7..9634651fb01e 100644
--- a/products/notebooks/backend/temporal/frame_materialize.py
+++ b/products/notebooks/backend/temporal/frame_materialize.py
@@ -52,9 +52,11 @@ from posthog.schema import QueryStatus
from posthog.hogql import ast
from posthog.hogql.constants import HogQLGlobalSettings, LimitContext
+from posthog.hogql.context import HogQLContext
from posthog.hogql.errors import ExposedHogQLError
from posthog.hogql.parser import parse_select
from posthog.hogql.query import HogQLQueryExecutor
+from posthog.hogql.visitor import clone_expr
from posthog.clickhouse.client import sync_execute
from posthog.clickhouse.client.connection import (
@@ -384,19 +386,37 @@ class _GeneratedSQL:
print_ast_seconds: float
+def _printing_context(team: Team, user: User | None) -> HogQLContext:
+ """One context for every print pass of a materialization.
+
+ A frame is printed twice whenever a column needs stringifying, and the second pass used
+ to build its own context, database and access controls from scratch. They depend only on
+ (team, user), which do not change between passes, so the passes share them — the executor
+ reuses a database already on the context and documents that callers may do this.
+ """
+ return HogQLContext(
+ team_id=team.pk,
+ user=user,
+ user_access_control=UserAccessControl(user=user, team=team) if user else None,
+ )
+
+
def _generate_sql(
team: Team,
user: User | None,
- query: "str | ast.SelectQuery | ast.SelectSetQuery",
+ query: "ast.SelectQuery | ast.SelectSetQuery",
*,
output_format: str | None,
+ context: HogQLContext,
) -> _GeneratedSQL:
"""Print HogQL to guarded ClickHouse SQL with the standing caps applied."""
executor = HogQLQueryExecutor(
- query=query,
+ # Cloned because the executor applies the row ceiling to the node it is handed, in
+ # place, and the wrapper pass nests the same node.
+ query=clone_expr(query, True),
team=team,
user=user,
- user_access_control=UserAccessControl(user=user, team=team) if user else None,
+ context=context,
limit_context=LimitContext.NOTEBOOK_MATERIALIZE,
settings=_capped_settings(team.pk),
pretty=False,
@@ -404,12 +424,25 @@ def _generate_sql(
if output_format:
executor.context.output_format = output_format
started = time.perf_counter()
- sql, context = executor.generate_clickhouse_sql()
+ # A placeholder is named `hogql_val_{len(values)}`, and the executor shares this dict by
+ # reference rather than resetting it. Carrying a previous pass's entries would number this
+ # pass's placeholders from where that one stopped and ship its dead values to ClickHouse, so
+ # each pass prints against an empty dict and keeps the numbering a single pass would produce.
+ context.values.clear()
+ sql, resolved = executor.generate_clickhouse_sql()
seconds = time.perf_counter() - started
+ # The executor keeps the database it built on its own `dataclasses.replace` copy, so
+ # without this hand-back the shared context still looks empty and the next pass builds a
+ # second one. It resets the per-execution state it shares by reference, so a database
+ # carried across passes is the reuse it already accounts for.
+ if context.database is None:
+ context.database = resolved.database
recorded = executor.timings.to_dict()
return _GeneratedSQL(
sql=sql,
- values=context.values,
+ # Copied because the shared dict is cleared for the next pass, and a caller holds this
+ # pass's SQL and values together (the DESCRIBE between the passes reads them).
+ values=dict(resolved.values),
seconds=seconds,
parse_seconds=_hogql_leaf_seconds(recorded, "query"),
resolve_seconds=_hogql_leaf_seconds(recorded, "prepare_ast_for_printing"),
@@ -518,7 +551,11 @@ def _print_clickhouse_sql(
where the s3() function argument defines the object format and a FORMAT clause would
be invalid inside the INSERT.
"""
- plain = _generate_sql(team, user, query, output_format=None)
+ # Parsed once and reused: the wrapper pass nests this node instead of re-parsing the
+ # source, which is the other half of what the second pass used to redo from scratch.
+ parsed = parse_select(query)
+ context = _printing_context(team, user)
+ plain = _generate_sql(team, user, parsed, output_format=None, context=context)
describe_started = time.perf_counter()
described = describe(plain.sql, plain.values)
describe_seconds = time.perf_counter() - describe_started
@@ -538,15 +575,15 @@ def _print_clickhouse_sql(
if not any(function for _name, function in conversions):
if output_format is None:
return printed(None)
- return printed(_generate_sql(team, user, query, output_format=output_format))
+ return printed(_generate_sql(team, user, parsed, output_format=output_format, context=context))
select_fields: list[ast.Expr] = [
ast.Alias(expr=ast.Call(name=function, args=[ast.Field(chain=[name])]), alias=name)
if function
else ast.Field(chain=[name])
for name, function in conversions
]
- stringified = ast.SelectQuery(select=select_fields, select_from=ast.JoinExpr(table=parse_select(query)))
- return printed(_generate_sql(team, user, stringified, output_format=output_format))
+ stringified = ast.SelectQuery(select=select_fields, select_from=ast.JoinExpr(table=parsed))
+ return printed(_generate_sql(team, user, stringified, output_format=output_format, context=context))
class FrameTooLargeError(Exception):
diff --git a/products/notebooks/backend/test/test_frame_materialize.py b/products/notebooks/backend/test/test_frame_materialize.py
index a2ce823c5e50..94447aff0b37 100644
--- a/products/notebooks/backend/test/test_frame_materialize.py
+++ b/products/notebooks/backend/test/test_frame_materialize.py
@@ -1,4 +1,5 @@
import io
+import re
import uuid
from types import SimpleNamespace
@@ -16,6 +17,9 @@ from temporalio.worker import UnsandboxedWorkflowRunner, Worker
from posthog.schema import QueryStatus
+from posthog.hogql.database.database import Database
+from posthog.hogql.parser import parse_select
+
from posthog.clickhouse.client.execute import _KILL_SWITCH_SETTINGS, KillSwitchLevel
from posthog.clickhouse.client.execute_async import QueryStatusManager
from posthog.errors import ExposedCHQueryError, InternalCHQueryError
@@ -463,7 +467,13 @@ class TestFrameMaterializeKillSwitchCaps(APIBaseTest):
)
def test_printed_sql_carries_kill_switch_ceilings(self, _name: str, overrides: dict[str, int]):
with patch.object(frame_materialize, "kill_switch_overrides", return_value=overrides):
- sql = frame_materialize._generate_sql(self.team, self.user, "select 1", output_format=None).sql
+ sql = frame_materialize._generate_sql(
+ self.team,
+ self.user,
+ parse_select("select 1"),
+ output_format=None,
+ context=frame_materialize._printing_context(self.team, self.user),
+ ).sql
memory_ceiling = overrides.get("max_memory_usage")
if memory_ceiling is None:
@@ -495,6 +505,39 @@ class TestFrameMaterializePrintPasses(APIBaseTest):
assert printed.passes == expected_passes
assert ("toString" in printed.sql) is (expected_passes == 2)
+ def test_both_print_passes_share_one_database(self):
+ # The executor keeps the database it builds on a private copy of the context, so the
+ # shared context only carries one if it is handed back. Getting that wrong is
+ # invisible in the output — identical SQL, built twice — so assert on the build.
+ with patch.object(Database, "create_for", wraps=Database.create_for) as create_for:
+ printed = frame_materialize._print_clickhouse_sql(
+ lambda _sql, _values: [("uuid", "UUID")],
+ self.team,
+ self.user,
+ "select 1 as uuid",
+ output_format=None,
+ )
+
+ assert printed.passes == 2
+ assert create_for.call_count == 1
+
+ def test_each_pass_numbers_its_own_placeholders(self):
+ # The executor shares the context's `values` dict by reference and never resets it, while
+ # a placeholder is named from that dict's length. Without a per-pass reset the wrapper pass
+ # numbers its placeholders from where the first pass stopped, and the first pass's dead
+ # values ride along to ClickHouse.
+ printed = frame_materialize._print_clickhouse_sql(
+ lambda _sql, _values: [("a", "String"), ("uuid", "UUID")],
+ self.team,
+ self.user,
+ "select 'alpha' as a, 1 as uuid",
+ output_format=None,
+ )
+
+ assert printed.passes == 2
+ assert set(re.findall(r"%\((hogql_val_\w+)\)s", printed.sql)) == set(printed.values)
+ assert "hogql_val_0" in printed.values
+
def test_resolve_time_is_actually_recorded(self):
# The reported split reads leaf keys out of HogQLQueryExecutor's own timings, so a
# renamed or relocated span downgrades the field to a silent zero rather than an