: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(experiments): zero-fill variants when a breakdown metric has no rows (#73952)
Andersab96ab101de3
diff --git a/products/experiments/backend/hogql_queries/experiment_query_runner.py b/products/experiments/backend/hogql_queries/experiment_query_runner.pyindex e70c8777f4f6..86a65ff4e3a8 100644--- a/products/experiments/backend/hogql_queries/experiment_query_runner.py+++ b/products/experiments/backend/hogql_queries/experiment_query_runner.py@@ -730,20 +730,18 @@ class ExperimentQueryRunner(QueryRunner):"""variants_seen = [v.key for _, v in variants]- has_breakdown = (- self.metric.breakdownFilter is not None- and self.metric.breakdownFilter.breakdowns- and len(self.metric.breakdownFilter.breakdowns) > 0- )+ # Fan out over the breakdown combinations actually present in the results, not over the+ # metric's breakdown config: a metric can declare breakdowns and still come back with no+ # rows at all (no data yet), and there is then nothing to fan out over — fall back to the+ # single breakdown-less zero row so the baseline variant still exists.+ breakdown_tuples = {bv for bv, _ in variants if bv is not None}# Type annotation required for empty list so mypy knows the expected element type:# list of tuples containing (breakdown_values, stats) where breakdown_values can be Nonevariants_missing: list[tuple[tuple[str, ...] | None, ExperimentStatsBase]] = []for key in self.variants:if key not in variants_seen:- if has_breakdown:- # Extract all breakdown value combinations that exist in the results- breakdown_tuples = {bv for bv, _ in variants if bv is not None}+ if breakdown_tuples:# Use extend to add MULTIPLE tuples - one for each breakdown combination# Each missing variant needs to appear across ALL breakdown values to maintain consistencyvariants_missing.extend(diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_add_missing_variants.py b/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_add_missing_variants.pyindex 2dc8c368fb64..ca2283363ab5 100644--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_add_missing_variants.py+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_add_missing_variants.py@@ -4,6 +4,8 @@ from freezegun import freeze_timefrom django.test import override_settings+from parameterized import parameterized+from posthog.schema import (Breakdown,BreakdownFilter,@@ -162,11 +164,18 @@ class TestAddMissingVariants(ExperimentQueryRunnerBaseTest):assert ("MacOS", "Chrome") in breakdown_valuesassert ("Windows", "Firefox") in breakdown_values+ @parameterized.expand(+ [+ ("without_breakdown", None),+ ("with_breakdown", BreakdownFilter(breakdowns=[Breakdown(property="$browser")])),+ ]+ )@freeze_time("2020-01-01T12:00:00Z")- def test_empty_variants_list(self):+ def test_empty_variants_list(self, _name, breakdown_filter):"""Should add all configured variants when input is empty."""metric = ExperimentMeanMetric(source=EventsNode(event="purchase", math=ExperimentMetricMathType.TOTAL),+ breakdownFilter=breakdown_filter,)query = ExperimentQuery(experiment_id=self.experiment.id, kind="ExperimentQuery", metric=metric)runner = ExperimentQueryRunner(query=query, team=self.team)