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(insights): resolve group-property breakdown on retention (#69671)

Ben Lea08951fd03406
diff --git a/posthog/hogql_queries/insights/retention/test/test_retention_query_runner.py b/posthog/hogql_queries/insights/retention/test/test_retention_query_runner.py
index a0593540eacd..c0270c33450d 100644
--- a/posthog/hogql_queries/insights/retention/test/test_retention_query_runner.py
+++ b/posthog/hogql_queries/insights/retention/test/test_retention_query_runner.py
@@ -3920,6 +3920,61 @@ class TestRetention(RetentionBaseQueryVariantComparisonMixin, ClickhouseTestMixi
),
)
+ def test_retention_with_breakdown_with_group_properties(self):
+ create_group_type_mapping_without_created_at(
+ team=self.team, project_id=self.team.project_id, group_type="organization", group_type_index=0
+ )
+ create_group(
+ team_id=self.team.pk, group_type_index=0, group_key="org:finance", properties={"industry": "finance"}
+ )
+ create_group(
+ team_id=self.team.pk, group_type_index=0, group_key="org:tech", properties={"industry": "technology"}
+ )
+
+ create_person(team=self.team, distinct_ids=["person1"])
+ create_person(team=self.team, distinct_ids=["person2"])
+
+ _create_events(
+ self.team,
+ [
+ # finance org
+ ("person1", _date(0), {"$group_0": "org:finance"}),
+ ("person1", _date(1), {"$group_0": "org:finance"}),
+ ("person1", _date(3), {"$group_0": "org:finance"}),
+ # technology org
+ ("person2", _date(0), {"$group_0": "org:tech"}),
+ ("person2", _date(1), {"$group_0": "org:tech"}),
+ ],
+ )
+
+ # Breaking down by a group property must resolve the events->groups join
+ # (chain group_0, not groups_0), otherwise the query fails to resolve.
+ result = self.run_query(
+ query={
+ "dateRange": {"date_to": _date(5, hour=0)},
+ "retentionFilter": {"totalIntervals": 6, "period": "Day"},
+ "breakdownFilter": {"breakdowns": [{"property": "industry", "type": "group", "group_type_index": 0}]},
+ }
+ )
+
+ breakdown_values = {c.get("breakdown_value") for c in result}
+ self.assertEqual(breakdown_values, {"finance", "technology"})
+
+ finance_cohorts = pluck([c for c in result if c.get("breakdown_value") == "finance"], "values", "count")
+ self.assertEqual(
+ finance_cohorts,
+ pad(
+ [
+ [1, 1, 0, 1, 0, 0],
+ [1, 0, 1, 0, 0, 0],
+ [0, 0, 0, 0, 0, 0],
+ [1, 0, 0, 0, 0, 0],
+ [0, 0, 0, 0, 0, 0],
+ [0, 0, 0, 0, 0, 0],
+ ]
+ ),
+ )
+
def test_dwh_variant_breakdown_event_property_per_value_parity(self):
# Tracer bullet: the variant must mirror the legacy per-value cohort semantics
# for event-property breakdowns. person1 starts "clothing" then returns with a
diff --git a/posthog/hogql_queries/insights/retention/test/test_retention_utils.py b/posthog/hogql_queries/insights/retention/test/test_retention_utils.py
new file mode 100644
index 000000000000..c03bd8d45d22
--- /dev/null
+++ b/posthog/hogql_queries/insights/retention/test/test_retention_utils.py
@@ -0,0 +1,36 @@
+from typing import cast
+
+from django.test import SimpleTestCase
+
+from posthog.hogql import ast
+
+from posthog.hogql_queries.insights.retention.utils import breakdown_extract_expr
+
+
+def _chain(expr: ast.Expr) -> list:
+ # breakdown_extract_expr wraps the field in ifNull(toString(<field>), '')
+ to_string = cast(ast.Call, cast(ast.Call, expr).args[0])
+ return cast(ast.Field, to_string.args[0]).chain
+
+
+class TestBreakdownExtractExpr(SimpleTestCase):
+ def test_group_breakdown_uses_events_lazy_join_chain(self) -> None:
+ # Group properties are read via the events table lazy join `group_{index}`,
+ # not a standalone `groups_{index}` table.
+ expr = breakdown_extract_expr("industry", "group", group_type_index=0)
+ self.assertEqual(_chain(expr), ["group_0", "properties", "industry"])
+
+ def test_group_breakdown_coerces_float_index(self) -> None:
+ expr = breakdown_extract_expr("industry", "group", group_type_index=cast(int, 1.0))
+ self.assertEqual(_chain(expr), ["group_1", "properties", "industry"])
+
+ def test_group_breakdown_virtual_property_skips_properties_key(self) -> None:
+ # Virtual group properties are expression fields on the groups table, so the
+ # chain must be `group_{index}.$virt_x`, not `group_{index}.properties.$virt_x`.
+ expr = breakdown_extract_expr("$virt_revenue", "group", group_type_index=0)
+ self.assertEqual(_chain(expr), ["group_0", "$virt_revenue"])
+
+ def test_group_breakdown_without_index_raises_clear_error(self) -> None:
+ # A missing index must fail loudly rather than emit an unresolvable `group_None` field.
+ with self.assertRaises(ValueError):
+ breakdown_extract_expr("industry", "group", group_type_index=None)
diff --git a/posthog/hogql_queries/insights/retention/utils.py b/posthog/hogql_queries/insights/retention/utils.py
index f4e8e34a4447..e63dc267cc15 100644
--- a/posthog/hogql_queries/insights/retention/utils.py
+++ b/posthog/hogql_queries/insights/retention/utils.py
@@ -29,11 +29,14 @@ def breakdown_extract_expr(property_name: str, breakdown_type: str, group_type_i
elif breakdown_type == "data_warehouse_person_property":
properties_chain = ["person", *property_name.split(".")]
elif breakdown_type == "group":
+ if group_type_index is None:
+ raise ValueError("group_type_index is required for group breakdowns")
+ group_index = int(group_type_index)
if property_name.startswith("$virt_"):
# Virtual properties exist as expression fields on the groups table
- properties_chain = [f"groups_{group_type_index}", property_name]
+ properties_chain = [f"group_{group_index}", property_name]
else:
- properties_chain = [f"groups_{group_type_index}", "properties", property_name]
+ properties_chain = [f"group_{group_index}", "properties", property_name]
else:
# Default to event properties
properties_chain = ["events", "properties", property_name]