: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
chore(endpoints): materialize compare mode trends for every team (#122792)
Jovan Sakovica97cacd43da0
diff --git a/products/endpoints/backend/logic/validation.py b/products/endpoints/backend/logic/validation.pyindex 45bc5da80353..01381207f2c1 100644--- a/products/endpoints/backend/logic/validation.py+++ b/products/endpoints/backend/logic/validation.py@@ -334,7 +334,7 @@ def validate_update_request(data.query.model_dump() if data.query is not None else (endpoint.get_version().query if endpoint else None))if effective_query is not None:- can_materialize, reason = can_materialize_query(effective_query, team)+ can_materialize, reason = can_materialize_query(effective_query)if not can_materialize:raise ValidationError(f"Cannot materialize endpoint. Reason: {reason}")diff --git a/products/endpoints/backend/models.py b/products/endpoints/backend/models.pyindex a9ee20bfa310..0b64dcab96c9 100644--- a/products/endpoints/backend/models.py+++ b/products/endpoints/backend/models.py@@ -10,8 +10,6 @@ from django.db import modelsfrom django.db.models import Qfrom django.utils import timezone-import posthoganalytics-from posthog.hogql import astfrom posthog.hogql.parser import parse_selectfrom posthog.hogql.visitor import CloningVisitor@@ -26,28 +24,6 @@ from posthog.schema_enums import ProductKeylogger = logging.getLogger(__name__)-COMPARE_MODE_MATERIALIZATION_FLAG = "endpoints-materialized-compare-mode"---def compare_mode_materialization_enabled(team: Team) -> bool:- try:- return bool(- posthoganalytics.feature_enabled(- COMPARE_MODE_MATERIALIZATION_FLAG,- str(team.uuid),- groups={"organization": str(team.organization_id), "project": str(team.id)},- group_properties={- "organization": {"id": str(team.organization_id)},- "project": {"id": str(team.id)},- },- only_evaluate_locally=True,- send_feature_flag_events=False,- )- )- except Exception as error:- capture_exception(error)- return False-class _ReplacePlaceholdersWithDummies(CloningVisitor):"""Replace all {variables.foo} placeholders with empty string constants."""@@ -98,7 +74,7 @@ def _clickhouse_type_to_serialized_type(ch_type: str) -> str:return "unknown"-def can_materialize_query(query: dict | None, team: Team | None = None) -> tuple[bool, str]:+def can_materialize_query(query: dict | None) -> tuple[bool, str]:"""Check whether an endpoint query can be materialized.Returns: (can_materialize: bool, reason: str)@@ -122,12 +98,8 @@ def can_materialize_query(query: dict | None, team: Team | None = None) -> tupleassert query is not Nonecompare_filter = query.get("compareFilter") or {}- if compare_filter.get("compare"):- compare_mode_supported = (- query_kind == "TrendsQuery" and team is not None and compare_mode_materialization_enabled(team)- )- if not compare_mode_supported:- return False, "Compare mode is not supported for materialized endpoints."+ if compare_filter.get("compare") and query_kind != "TrendsQuery":+ return False, "Compare mode is only supported for materialized Trends endpoints."# Block cohort breakdowns — they produce a UNION ALL across cohorts, which# inject_series_index tags as separate series, causing a mismatch at read time.@@ -341,7 +313,7 @@ class EndpointVersion(UpdatedMetaFields, models.Model):Returns: (can_materialize: bool, reason: str)"""- return can_materialize_query(self.query, self.team)+ return can_materialize_query(self.query)@staticmethoddef extract_columns(query: dict, team_id: int) -> list[dict]:diff --git a/products/endpoints/backend/tests/test_endpoint_materialization.py b/products/endpoints/backend/tests/test_endpoint_materialization.pyindex 67b94f8356c4..fc04779c1f67 100644--- a/products/endpoints/backend/tests/test_endpoint_materialization.py+++ b/products/endpoints/backend/tests/test_endpoint_materialization.py@@ -492,29 +492,6 @@ class TestEndpointMaterialization(ClickhouseTestMixin, APIBaseTest):self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.json())self.assertIn("Unresolved placeholder", str(response.json()))- def test_compare_mode_materialization_requires_rollout_flag(self):- endpoint = create_endpoint_with_version(- name="test_compare_mode_rollout",- team=self.team,- query=self._trends_query(compareFilter={"compare": True}),- created_by=self.user,- )-- with mock.patch(- "products.endpoints.backend.models.posthoganalytics.feature_enabled",- return_value=False,- ) as feature_enabled_mock:- response = self.client.patch(- f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/",- {"is_materialized": True, "data_freshness_seconds": 86400},- format="json",- )-- feature_enabled_mock.assert_called_once()- self.assertEqual(feature_enabled_mock.call_args.args[0], "endpoints-materialized-compare-mode")- self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)- self.assertIn("Compare mode is not supported", response.json()["detail"])-@parameterized.expand([(diff --git a/products/endpoints/backend/tests/test_insight_response_parity.py b/products/endpoints/backend/tests/test_insight_response_parity.pyindex 957caff40079..6460ae9a2735 100644--- a/products/endpoints/backend/tests/test_insight_response_parity.py+++ b/products/endpoints/backend/tests/test_insight_response_parity.py@@ -110,13 +110,6 @@ class TestInsightResponseParity(ClickhouseTestMixin, APIBaseTest):def _trends_query(self, **kwargs: Any) -> dict[str, Any]:return TrendsQuery(**kwargs).model_dump()- def _compare_mode_rollout(self):- """Eligibility is re-checked on every materialized read, not just when materializing."""- return mock.patch(- "products.endpoints.backend.models.posthoganalytics.feature_enabled",- return_value=True,- )-# =========================================================================# TRENDS# =========================================================================@@ -443,8 +436,7 @@ class TestInsightResponseParity(ClickhouseTestMixin, APIBaseTest):inline_response = self._run_endpoint(endpoint)assert inline_response.status_code == status.HTTP_200_OKinline_compare_range = inline_response.json()["resolved_compare_date_range"]- with self._compare_mode_rollout():- saved_query = self._materialize_endpoint(endpoint, data_freshness_seconds=604800)+ saved_query = self._materialize_endpoint(endpoint, data_freshness_seconds=604800)flat_response = process_query_model(self.team,@@ -455,7 +447,6 @@ class TestInsightResponseParity(ClickhouseTestMixin, APIBaseTest):with (time_machine.travel(materialized_at + timedelta(days=6), tick=False),- self._compare_mode_rollout(),mock.patch("products.endpoints.backend.logic.execution.process_query_model",return_value=flat_response,diff --git a/products/endpoints/skills/creating-an-endpoint/references/materializing.md b/products/endpoints/skills/creating-an-endpoint/references/materializing.mdindex e7f1e3d5dd89..3985d102c8e1 100644--- a/products/endpoints/skills/creating-an-endpoint/references/materializing.md+++ b/products/endpoints/skills/creating-an-endpoint/references/materializing.md@@ -25,9 +25,9 @@ If two or more of these apply, materialise. If none apply, don't.- **Low-traffic endpoint.** If it's called once a day, the materialisation refresh costs morethan the inline reads would.- **Cohort breakdowns (insight endpoints).** Regular property breakdowns materialise fine, but- cohort breakdowns are rejected. Compare mode can materialise for eligible Trends endpoints- during its controlled rollout. It stores and computes both periods, so expect about twice the- storage and refresh work. Use `endpoints-materialization-preview` to confirm eligibility.+ cohort breakdowns are rejected. Compare mode can materialise for Trends endpoints. It stores+ and computes both periods, so expect about twice the storage and refresh work. Use+ `endpoints-materialization-preview` to confirm eligibility.- **Query reads `now()` / `today()` directly.** Replace with a variable; otherwise thematerialised result is anchored to the refresh time, not the call time.@@ -39,7 +39,7 @@ rejection reasons:| Reason | What it means | Fix || ---------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- || `Cohort breakdowns are not supported` | Cohort breakdowns produce a UNION ALL the transform can't tag by series | Use a property breakdown, or split into separate endpoints, one per cohort |-| `Compare mode is not supported` | The query is not a Trends query, or the project is not in the compare-mode materialisation rollout | Keep it inline, drop compare mode, or expose the comparison window as a variable |+| `Compare mode is only supported` | Compare mode is set on a query that is not a Trends query | Keep it inline, drop compare mode, or expose the comparison window as a variable || `Query has unresolved variables` | A variable in the query has no default and the materialisation can't pick a value | Set defaults for all variables || `Query references non-deterministic functions` | `now()`, `today()`, `rand()` change between refresh runs | Replace with a `date_from` / `date_to` variable || `CTE variables with JOINs … not supported` | A variable filter combined with a top-level `JOIN` changes joined-row cardinality, silently producing wrong results (e.g. `LEFT JOIN` non-matches lose the variable column) | Filter inside a subquery/CTE, then join the result — don't apply the variable across the JOIN |diff --git a/products/endpoints/skills/diagnosing-endpoint-performance/SKILL.md b/products/endpoints/skills/diagnosing-endpoint-performance/SKILL.mdindex 4399635571b9..13a364307d03 100644--- a/products/endpoints/skills/diagnosing-endpoint-performance/SKILL.md+++ b/products/endpoints/skills/diagnosing-endpoint-performance/SKILL.md@@ -86,8 +86,9 @@ match it to the bullets below — they summarise the common cases, not every cheso the raw reason wins when the two disagree. Propose the rewrite by hand:- **Cohort breakdown / compare mode rejection** → regular property breakdowns materialise fine;- only cohort breakdowns and compare mode are blocked. Swap a cohort breakdown for a property- breakdown, or drop compare mode (expose the comparison window as a variable instead).+ only cohort breakdowns are blocked. Swap a cohort breakdown for a property breakdown. Compare+ mode materialises for Trends endpoints; on any other query kind, drop compare mode (expose the+ comparison window as a variable instead).- **JOINs combined with variables** → a top-level `JOIN` plus a variable filter is rejected formaterialisation, because applying the variable changes the joined row cardinality and silentlyproduces wrong results (e.g. `LEFT JOIN` non-matches lose the variable column). Restructure so the