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): show the execution query in the materialized preview (#110846)

Alex Lider1b7ad38f0579
diff --git a/products/endpoints/backend/logic/materialization.py b/products/endpoints/backend/logic/materialization.py
index f5bf6b162bf2..5a5308c10600 100644
--- a/products/endpoints/backend/logic/materialization.py
+++ b/products/endpoints/backend/logic/materialization.py
@@ -528,7 +528,11 @@ class EndpointMaterializationService:
# 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
+ _build_exec_preview(version.materialized_view_name),
+ team=self.team,
+ # Display-only text for the endpoint's own materialized view, and no rows are read.
+ # Without the bypass, the userless database denies the view and the print fails.
+ bypass_warehouse_access_control=True,
)
else:
execution_query_str = print_prepared_ast(
diff --git a/products/endpoints/backend/tests/test_endpoint.py b/products/endpoints/backend/tests/test_endpoint.py
index 19e8867694eb..c88e2d4e93a7 100644
--- a/products/endpoints/backend/tests/test_endpoint.py
+++ b/products/endpoints/backend/tests/test_endpoint.py
@@ -1347,6 +1347,42 @@ class TestMaterializationPreview(ClickhouseTestMixin, APIBaseTest):
assert data["execution_query"] is not None
assert data["display_execution_query"] is not None
+ def test_preview_of_materialized_version_returns_execution_query(self):
+ endpoint = create_endpoint_with_version(
+ name="orders_summary",
+ team=self.team,
+ query={"kind": "HogQLQuery", "query": "SELECT count() AS total FROM events"},
+ created_by=self.user,
+ )
+ version = endpoint.get_version()
+ saved_query = DataWarehouseSavedQuery.objects.create(
+ team=self.team,
+ name=version.materialized_view_name,
+ query=version.query,
+ is_materialized=True,
+ origin=DataWarehouseSavedQuery.Origin.ENDPOINT,
+ table=DataWarehouseTable.objects.create(
+ team=self.team,
+ name="orders_summary_backing_table",
+ columns={"total": {"hogql": "IntegerDatabaseField", "clickhouse": "Int64", "valid": True}},
+ format=DataWarehouseTable.TableFormat.Parquet,
+ url_pattern="s3://test-bucket/orders_summary/*.parquet",
+ ),
+ )
+ endpoint.versions.update(saved_query=saved_query)
+
+ with mock.patch("posthog.hogql.database.database._evaluate_warehouse_access_control_flag", return_value=True):
+ response = self.client.post(
+ f"/api/environments/{self.team.id}/endpoints/orders_summary/materialization_preview/",
+ {},
+ format="json",
+ )
+
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ execution_query = response.json()["execution_query"]
+ assert execution_query is not None
+ assert "orders_summary_v1" in execution_query
+
def test_preview_with_bucket_override(self):
self._create_endpoint_with_variables()