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): return can't-materialize on invalid hogql in preview (#117295)

posthog[bot]47c6c4231ce6
diff --git a/products/endpoints/backend/logic/materialization.py b/products/endpoints/backend/logic/materialization.py
index 5a5308c10600..51d0c20880cc 100644
--- a/products/endpoints/backend/logic/materialization.py
+++ b/products/endpoints/backend/logic/materialization.py
@@ -413,6 +413,18 @@ class EndpointMaterializationService:
bucket_overrides: dict[str, str] | None = None,
) -> MaterializationPreview:
"""Preview the materialization transform without enabling it."""
+ try:
+ return self._build_preview(endpoint, version, bucket_overrides)
+ except (ExposedHogQLError, MaterializationNotSupportedError) as e:
+ # A bad user query, not a system fault, so report it as unmaterializable.
+ return MaterializationPreview.cant_materialize(str(e))
+
+ def _build_preview(
+ self,
+ endpoint: Endpoint,
+ version: EndpointVersion,
+ bucket_overrides: dict[str, str] | None,
+ ) -> MaterializationPreview:
can_mat, reason = version.can_materialize()
if not can_mat:
return MaterializationPreview.cant_materialize(reason)
diff --git a/products/endpoints/backend/tests/test_endpoint.py b/products/endpoints/backend/tests/test_endpoint.py
index df81e806a104..d3f328a89899 100644
--- a/products/endpoints/backend/tests/test_endpoint.py
+++ b/products/endpoints/backend/tests/test_endpoint.py
@@ -1477,6 +1477,28 @@ class TestMaterializationPreview(ClickhouseTestMixin, APIBaseTest):
assert data["can_materialize"] is True
assert data["transformed_query"] is not None
+ def test_preview_invalid_hogql_returns_cant_materialize(self):
+ create_endpoint_with_version(
+ name="invalid-hogql-endpoint",
+ team=self.team,
+ query={
+ **self.variable_query,
+ "query": "SELECT substring(event) AS start_ts, count() FROM events "
+ "WHERE timestamp >= {variables.start_ts} GROUP BY start_ts",
+ },
+ created_by=self.user,
+ )
+
+ response = self.client.post(
+ f"/api/environments/{self.team.id}/endpoints/invalid-hogql-endpoint/materialization_preview/",
+ {},
+ format="json",
+ )
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ data = response.json()
+ assert data["can_materialize"] is False
+ assert "substring" in data["reason"]
+
def test_create_does_not_auto_materialize(self):
endpoint_data = self._create_endpoint_with_variables("no-auto-mat")