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(endpoints): avoid "Unknown table" in materialization preview (#64846)

Jovan Sakovic270abf15fefa
diff --git a/products/endpoints/backend/services/materialization.py b/products/endpoints/backend/services/materialization.py
index 4bcc920f9b0f..81c388cb7236 100644
--- a/products/endpoints/backend/services/materialization.py
+++ b/products/endpoints/backend/services/materialization.py
@@ -393,8 +393,23 @@ class EndpointMaterializationService:
)
return q
- # Each call builds a fresh SelectQuery, so WHERE mutations don't leak between calls
- execution_query_str = to_printed_hogql(_build_exec_preview(version.materialized_view_name), team=self.team)
+ # Each call builds a fresh SelectQuery, so WHERE mutations don't leak between calls.
+ # Type resolution (to_printed_hogql) needs the materialized table to exist in the
+ # database, which only holds once materialization has completed. Previewing a
+ # not-yet-materialized version means that table is absent, so we'd otherwise hit
+ # "Unknown table". Fall back to printing without type resolution in that case — the
+ # frontend uses execution_query only as a presence flag and renders the display variant.
+ if version.is_materialized:
+ execution_query_str = to_printed_hogql(
+ _build_exec_preview(version.materialized_view_name), team=self.team
+ )
+ else:
+ execution_query_str = print_prepared_ast(
+ node=_build_exec_preview(version.materialized_view_name),
+ context=HogQLContext(team_id=self.team.pk, enable_select_queries=True),
+ dialect="hogql",
+ pretty=True,
+ )
# Display variant uses the friendly endpoint name — printed without type resolution
# since the friendly name isn't a real table in the database
diff --git a/products/endpoints/backend/tests/test_endpoint.py b/products/endpoints/backend/tests/test_endpoint.py
index b16ac0dce164..4254e9bbd08b 100644
--- a/products/endpoints/backend/tests/test_endpoint.py
+++ b/products/endpoints/backend/tests/test_endpoint.py
@@ -1297,6 +1297,12 @@ class TestMaterializationPreview(ClickhouseTestMixin, APIBaseTest):
assert data["range_pairs"][0]["column"] == "timestamp"
assert set(data["range_pairs"][0]["variables"]) == {"start_ts", "end_ts"}
+ # The endpoint isn't materialized yet, so its backing table doesn't exist in the database.
+ # The execution-query preview must still be produced (printed without type resolution)
+ # rather than failing with "Unknown table".
+ assert data["execution_query"] is not None
+ assert data["display_execution_query"] is not None
+
def test_preview_with_bucket_override(self):
self._create_endpoint_with_variables()