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.

chore(insights): verify retention aggregation legacy/variant parity (#76518)

Thomas Obermüllere1858c1cc68b
diff --git a/posthog/hogql_queries/insights/retention/retention_base_query_fixed.py b/posthog/hogql_queries/insights/retention/retention_base_query_fixed.py
index 1ab6802b251e..f679f6666b19 100644
--- a/posthog/hogql_queries/insights/retention/retention_base_query_fixed.py
+++ b/posthog/hogql_queries/insights/retention/retention_base_query_fixed.py
@@ -275,8 +275,11 @@ class RetentionFixedIntervalBaseQueryBuilder(RetentionBaseQueryBuilder):
def _can_single_scan(self) -> bool:
# Events-only, non-property-aggregating series read the same `events` source on both arms, so the
# start and return timestamp arrays can be computed in one pass. Property aggregation stays on the
- # UNION because collapsing it into the single scan is known to diverge from legacy results; the
- # UNION shape itself matches legacy (covered by the aggregation tests in
+ # UNION only because this builder does not collect the (interval, value, timestamp) tuple arrays
+ # (_start_event_data / _return_event_data) that _get_intervals_from_base_exprs reads in
+ # aggregation mode, so routing it through the single scan fails to resolve those fields.
+ # Collecting them inline, the way build_base_query_legacy does on one scan, is a legitimate perf
+ # follow-up; the UNION shape matches legacy results (compared by the aggregation tests in
# test_retention_query_runner.py). A data-warehouse entity is a genuinely different source and
# cannot collapse here.
return (
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 faa8b1191b23..f2c8ff835a75 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
@@ -5866,6 +5866,168 @@ class TestRetention(RetentionBaseQueryVariantComparisonMixin, ClickhouseTestMixi
# Interval 0: (50 + 30) / 2 = 40 — avg of return events after start events per user
self.assertEqual(day0_values[0]["aggregation_value"], 40)
+ def test_retention_aggregation_first_ever_occurrence_different_events(self):
+ create_person(team=self.team, distinct_ids=["user1"])
+ create_person(team=self.team, distinct_ids=["user2"])
+
+ # user1's first-ever signup predates the window, so user1 joins no cohort even
+ # though it signs up again inside the window and its purchases carry revenue.
+ _create_events(self.team, [("user1", _date(-2)), ("user1", _date(0, hour=10))], event="signed_up")
+ _create_events(
+ self.team,
+ [
+ ("user1", _date(0, hour=12), {"revenue": 50}),
+ ("user1", _date(1), {"revenue": 100}),
+ ],
+ event="purchased",
+ )
+ # user2's first-ever signup is inside the window; the same-interval purchase counts
+ # because it happens after the signup, and the day-4 purchase lands in interval 3.
+ _create_events(self.team, [("user2", _date(1, hour=9))], event="signed_up")
+ _create_events(
+ self.team,
+ [
+ ("user2", _date(1, hour=11), {"revenue": 8}),
+ ("user2", _date(4), {"revenue": 2}),
+ ],
+ event="purchased",
+ )
+
+ flush_persons_and_events()
+
+ result = self.run_query(
+ query={
+ "dateRange": {"date_from": _date(0, hour=0), "date_to": _date(6)},
+ "retentionFilter": {
+ "totalIntervals": 7,
+ "retentionType": RETENTION_FIRST_EVER_OCCURRENCE,
+ "targetEntity": {"id": "signed_up", "type": "events"},
+ "returningEntity": {"id": "purchased", "type": "events"},
+ "aggregationType": "sum",
+ "aggregationProperty": "revenue",
+ },
+ }
+ )
+
+ self.assertEqual(
+ pluck(result, "values", "aggregation_value"),
+ pad(
+ [
+ [0, 0, 0, 0, 0, 0, 0],
+ [8, 0, 0, 2, 0, 0],
+ [0, 0, 0, 0, 0],
+ [0, 0, 0, 0],
+ [0, 0, 0],
+ [0, 0],
+ [0],
+ ]
+ ),
+ )
+ self.assertEqual(pluck(result, "values", "count")[1], [1, 0, 0, 1, 0, 0, 0])
+
+ def test_retention_aggregation_with_event_value_breakdown(self):
+ create_person(team=self.team, distinct_ids=["user1"])
+ create_person(team=self.team, distinct_ids=["user2"])
+
+ # user1 starts on Chrome and returns on Firefox: the Firefox revenue must count
+ # toward the Firefox day-1 cohort, not toward user1's Chrome day-0 cohort.
+ _create_events(
+ self.team,
+ [
+ ("user1", _date(0), {"revenue": 10, "$browser": "Chrome"}),
+ ("user1", _date(1), {"revenue": 20, "$browser": "Firefox"}),
+ ("user1", _date(2), {"revenue": 30, "$browser": "Chrome"}),
+ ("user2", _date(0), {"revenue": 40, "$browser": "Firefox"}),
+ ("user2", _date(1), {"revenue": 50, "$browser": "Firefox"}),
+ ],
+ )
+
+ flush_persons_and_events()
+
+ result = self.run_query(
+ query={
+ "dateRange": {"date_from": _date(0, hour=0), "date_to": _date(6)},
+ "retentionFilter": {
+ "totalIntervals": 7,
+ "aggregationType": "sum",
+ "aggregationProperty": "revenue",
+ },
+ "breakdownFilter": {"breakdowns": [{"type": "event", "property": "$browser"}]},
+ }
+ )
+
+ chrome_results = [r for r in result if r["breakdown_value"] == "Chrome"]
+ firefox_results = [r for r in result if r["breakdown_value"] == "Firefox"]
+
+ self.assertEqual(
+ pluck(chrome_results, "values", "aggregation_value"),
+ pad(
+ [
+ [10, 0, 30, 0, 0, 0, 0],
+ [0, 0, 0, 0, 0, 0],
+ [30, 0, 0, 0, 0],
+ [0, 0, 0, 0],
+ [0, 0, 0],
+ [0, 0],
+ [0],
+ ]
+ ),
+ )
+ self.assertEqual(
+ pluck(firefox_results, "values", "aggregation_value"),
+ pad(
+ [
+ [40, 50, 0, 0, 0, 0, 0],
+ [70, 0, 0, 0, 0, 0],
+ [0, 0, 0, 0, 0],
+ [0, 0, 0, 0],
+ [0, 0, 0],
+ [0, 0],
+ [0],
+ ]
+ ),
+ )
+
+ def test_retention_aggregation_ignores_minimum_occurrences(self):
+ create_person(team=self.team, distinct_ids=["user1"])
+ create_person(team=self.team, distinct_ids=["user2"])
+
+ _create_events(
+ self.team,
+ [
+ ("user1", _date(0), {"revenue": 10}),
+ ("user1", _date(1), {"revenue": 20}),
+ ("user2", _date(0), {"revenue": 40}),
+ ("user2", _date(2, hour=4), {"revenue": 9}),
+ ("user2", _date(2, hour=6), {"revenue": 1}),
+ ],
+ )
+
+ flush_persons_and_events()
+
+ date_range = {"date_from": _date(0, hour=0), "date_to": _date(6)}
+ aggregation_filter = {
+ "totalIntervals": 7,
+ "aggregationType": "sum",
+ "aggregationProperty": "revenue",
+ }
+
+ without_threshold = self.run_query(query={"dateRange": date_range, "retentionFilter": aggregation_filter})
+ with_threshold = self.run_query(
+ query={
+ "dateRange": date_range,
+ "retentionFilter": {**aggregation_filter, "minimumOccurrences": 3},
+ }
+ )
+
+ # Property aggregation ignores the minimum-occurrences threshold on both base-query
+ # implementations: user2's two day-2 events stay counted even though 2 < 3.
+ self.assertEqual(with_threshold, without_threshold)
+ self.assertEqual(
+ pluck(without_threshold, "values", "aggregation_value")[0],
+ [50, 20, 10, 0, 0, 0, 0],
+ )
+
def test_retention_aggregation_person_property_sum(self):
"""Aggregating on a person property reads person.properties, not event.properties."""
create_person(team=self.team, distinct_ids=["high_value"], properties={"account_value": 100})