: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(endpoints): refuse to rebuild a version that no longer materializes (#128241)
Jovan Sakovicb781b5bb699e
diff --git a/products/endpoints/backend/materialization_transforms.py b/products/endpoints/backend/materialization_transforms.pyindex e4abee685794..5e14ff96634b 100644--- a/products/endpoints/backend/materialization_transforms.py+++ b/products/endpoints/backend/materialization_transforms.py@@ -1742,6 +1742,19 @@ def replace_breakdown_sentinels_in_query(hogql_query: dict) -> dict:return {**hogql_query, "query": query_text}+def _has_variable_placeholders(hogql_query: dict[str, Any]) -> bool:+ query_str = hogql_query.get("query")+ if not isinstance(query_str, str) or not query_str:+ return False+ try:+ ast_node = parse_select(query_str)+ except Exception:+ return False+ finder = VariablePlaceholderFinder()+ finder.visit(ast_node)+ return bool(finder.variable_placeholders)++def build_endpoint_hogql(insight_query: dict,team: Team,@@ -1761,9 +1774,12 @@ def build_endpoint_hogql()if insight_query.get("variables"):- can_materialize, _reason, variable_infos = analyze_variables_for_materialization(+ can_materialize, reason, variable_infos = analyze_variables_for_materialization(insight_query, bucket_overrides=bucket_overrides)+ # Untransformed placeholders can't resolve at materialization time, so the run would fail.+ if not can_materialize and _has_variable_placeholders(insight_query):+ raise MaterializationNotSupportedError(reason)if can_materialize and variable_infos:hogql_query = transform_query_for_materialization(hogql_query,diff --git a/products/endpoints/backend/tests/test_endpoint_materialization.py b/products/endpoints/backend/tests/test_endpoint_materialization.pyindex 95803ad590b9..70ad5cb178c2 100644--- a/products/endpoints/backend/tests/test_endpoint_materialization.py+++ b/products/endpoints/backend/tests/test_endpoint_materialization.py@@ -39,7 +39,7 @@ from products.endpoints.backend.logic.materialization import (OrphanedEndpointSavedQueryError,prepare_executable_query,)-from products.endpoints.backend.materialization_transforms import build_endpoint_hogql+from products.endpoints.backend.materialization_transforms import MaterializationNotSupportedError, build_endpoint_hogqlfrom products.endpoints.backend.models import EndpointVersionfrom products.endpoints.backend.rate_limit import is_endpoint_materialization_ready, set_endpoint_materialization_readyfrom products.endpoints.backend.tests.conftest import create_endpoint_with_version@@ -2238,6 +2238,44 @@ class TestEndpointMaterialization(ClickhouseTestMixin, APIBaseTest):with self.assertRaises(OrphanedEndpointSavedQueryError):prepare_executable_query(saved_query)+ @parameterized.expand(+ [+ ("multi_arg_concat_equality", "properties.label = concat('x-', {variables.item})"),+ ("like_concat", "properties.$current_url LIKE concat('%', {variables.item}, '%')"),+ ("multi_arg_range_wrapper", "timestamp >= toDateTime64({variables.item}, 6, 'UTC')"),+ ]+ )+ def test_prepare_executable_query_refuses_version_that_no_longer_materializes(self, _name: str, predicate: str):+ version_query = {+ "kind": "HogQLQuery",+ "query": f"SELECT count() AS c FROM events WHERE event = '$pageview' AND {predicate}",+ "variables": {"var-1": {"variableId": "var-1", "code_name": "item", "value": "1"}},+ }+ endpoint = create_endpoint_with_version(+ name=f"legacy_{_name}",+ team=self.team,+ query=version_query,+ created_by=self.user,+ is_active=True,+ )+ previously_built = {"kind": "HogQLQuery", "query": "SELECT count() AS c, properties.label AS item FROM events"}+ saved_query = DataWarehouseSavedQuery.objects.create(+ name=f"legacy_{_name}_v1",+ team=self.team,+ query=previously_built,+ created_by=self.user,+ origin=DataWarehouseSavedQuery.Origin.ENDPOINT,+ )+ version = endpoint.versions.first()+ version.saved_query = saved_query+ version.save(update_fields=["saved_query"])++ with self.assertRaises(MaterializationNotSupportedError):+ prepare_executable_query(saved_query)++ saved_query.refresh_from_db()+ self.assertEqual(saved_query.query, previously_built)+def test_enable_materialization_links_version_before_immediate_run(self):endpoint = create_endpoint_with_version(name="hundred",