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(retention): fix dwh variant custom bracket shift east of utc (#79002)

Thomas Obermüllerf7a7289f70f1
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 f679f6666b19..135e652c942f 100644
--- a/posthog/hogql_queries/insights/retention/retention_base_query_fixed.py
+++ b/posthog/hogql_queries/insights/retention/retention_base_query_fixed.py
@@ -623,11 +623,21 @@ class RetentionFixedIntervalBaseQueryBuilder(RetentionBaseQueryBuilder):
# mode the only element ever read is start_event_timestamps[1] (the minimum), so a single-element array is
# equivalent. A null anchor (first-ever occurrence not matching filters) or an out-of-window anchor both
# fail the window check and yield an empty array, excluding the actor.
+ bucketed_anchor: ast.Expr = self.query_date_range.date_to_start_of_interval_hogql(anchor_expr)
+ if self.query_date_range.interval_name in ("hour", "day"):
+ # The legacy shape builds start_event_timestamps via groupUniqArrayIf, whose result type drops
+ # the DateTime timezone (Array(DateTime) in the server default, UTC on Cloud). This array literal
+ # would instead keep the team timezone carried by the anchor. dateDiff — the custom-brackets
+ # bucketing — evaluates each operand's calendar day in its own timezone, so for teams east of UTC
+ # a team-tz-typed anchor lands every return one bracket early (next-day returns collapse into
+ # day 0 and get dropped). Pin the aggregate's type so both shapes bucket identically. Week/month
+ # buckets are Date-typed (timezoneless) in both shapes and toTimeZone would not accept them.
+ bucketed_anchor = ast.Call(name="toTimeZone", args=[bucketed_anchor, ast.Constant(value="UTC")])
return parse_expr(
"if({within_window}, [{bucketed_anchor}], [])",
{
"within_window": self.events_timestamp_filter(field=anchor_expr),
- "bucketed_anchor": self.query_date_range.date_to_start_of_interval_hogql(anchor_expr),
+ "bucketed_anchor": bucketed_anchor,
},
)
diff --git a/posthog/hogql_queries/insights/retention/test/__snapshots__/test_retention_query_runner.ambr b/posthog/hogql_queries/insights/retention/test/__snapshots__/test_retention_query_runner.ambr
index e997f5779136..f1b2cd02db98 100644
--- a/posthog/hogql_queries/insights/retention/test/__snapshots__/test_retention_query_runner.ambr
+++ b/posthog/hogql_queries/insights/retention/test/__snapshots__/test_retention_query_runner.ambr
@@ -1384,7 +1384,7 @@
count(DISTINCT actor_activity.actor_id) AS count
FROM
(SELECT events.person_id AS actor_id,
- if(and(greaterOrEquals(minIf(events.timestamp, equals(events.matches_start, 1)), toStartOfInterval(assumeNotNull(toDateTime('2020-06-10 00:00:00', 'UTC')), toIntervalDay(1))), less(minIf(events.timestamp, equals(events.matches_start, 1)), toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), [toStartOfInterval(minIf(events.timestamp, equals(events.matches_start, 1)), toIntervalDay(1))], []) AS start_event_timestamps,
+ if(and(greaterOrEquals(minIf(events.timestamp, equals(events.matches_start, 1)), toStartOfInterval(assumeNotNull(toDateTime('2020-06-10 00:00:00', 'UTC')), toIntervalDay(1))), less(minIf(events.timestamp, equals(events.matches_start, 1)), toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), [toTimeZone(toStartOfInterval(minIf(events.timestamp, equals(events.matches_start, 1)), toIntervalDay(1)), 'UTC')], []) AS start_event_timestamps,
arrayMap(x -> plus(toStartOfInterval(assumeNotNull(toDateTime('2020-06-10 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(x)), range(0, 6)) AS date_range,
arraySort(groupUniqArrayIf(toStartOfDay(events.timestamp), and(equals(events.is_return, 1), and(greaterOrEquals(events.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2020-06-10 00:00:00', 'UTC')), toIntervalDay(1))), less(events.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')))))) AS return_event_timestamps,
arrayJoin(arrayFilter(x -> ifNull(greater(x, -1), 0), arrayMap((interval_index, interval_date, _start_event_timestamps) -> if(ifNull(equals(_start_event_timestamps[1], interval_date), isNull(_start_event_timestamps[1])
@@ -1510,7 +1510,7 @@
count(DISTINCT actor_activity.actor_id) AS count
FROM
(SELECT events.person_id AS actor_id,
- if(and(ifNull(greaterOrEquals(if(equals(minIf(events.timestamp, equals(events.is_start, 1)), minIf(events.timestamp, equals(events.matches_start, 1))), minIf(events.timestamp, equals(events.is_start, 1)), NULL), toStartOfInterval(assumeNotNull(toDateTime('2020-06-10 00:00:00', 'UTC')), toIntervalDay(1))), 0), ifNull(less(if(equals(minIf(events.timestamp, equals(events.is_start, 1)), minIf(events.timestamp, equals(events.matches_start, 1))), minIf(events.timestamp, equals(events.is_start, 1)), NULL), toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), 0)), [toStartOfInterval(if(equals(minIf(events.timestamp, equals(events.is_start, 1)), minIf(events.timestamp, equals(events.matches_start, 1))), minIf(events.timestamp, equals(events.is_start, 1)), NULL), toIntervalDay(1))], []) AS start_event_timestamps,
+ if(and(ifNull(greaterOrEquals(if(equals(minIf(events.timestamp, equals(events.is_start, 1)), minIf(events.timestamp, equals(events.matches_start, 1))), minIf(events.timestamp, equals(events.is_start, 1)), NULL), toStartOfInterval(assumeNotNull(toDateTime('2020-06-10 00:00:00', 'UTC')), toIntervalDay(1))), 0), ifNull(less(if(equals(minIf(events.timestamp, equals(events.is_start, 1)), minIf(events.timestamp, equals(events.matches_start, 1))), minIf(events.timestamp, equals(events.is_start, 1)), NULL), toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), 0)), [toTimeZone(toStartOfInterval(if(equals(minIf(events.timestamp, equals(events.is_start, 1)), minIf(events.timestamp, equals(events.matches_start, 1))), minIf(events.timestamp, equals(events.is_start, 1)), NULL), toIntervalDay(1)), 'UTC')], []) AS start_event_timestamps,
arrayMap(x -> plus(toStartOfInterval(assumeNotNull(toDateTime('2020-06-10 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(x)), range(0, 6)) AS date_range,
arraySort(groupUniqArrayIf(toStartOfDay(events.timestamp), and(equals(events.is_return, 1), and(greaterOrEquals(events.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2020-06-10 00:00:00', 'UTC')), toIntervalDay(1))), less(events.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')))))) AS return_event_timestamps,
arrayJoin(arrayFilter(x -> ifNull(greater(x, -1), 0), arrayMap((interval_index, interval_date, _start_event_timestamps) -> if(ifNull(equals(_start_event_timestamps[1], interval_date), isNull(_start_event_timestamps[1])
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 e613c9ee0d31..e5717b3e5b74 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
@@ -7909,6 +7909,32 @@ class TestClickhouseRetentionGroupAggregation(
day2_row = next(r for r in result if r["label"] == "Day 2")
self.assertEqual([v["count"] for v in day2_row["values"]], [1, 1, 1])
+ def test_custom_brackets_first_time_east_of_utc_timezone(self):
+ """Next-day returns must land in the first bracket for teams east of UTC (mixed-timezone dateDiff)"""
+ self.team.timezone = "Asia/Kolkata"
+ self.team.save()
+
+ _create_person(team_id=self.team.pk, distinct_ids=["person1"])
+ _create_events(
+ self.team,
+ [("person1", _date(0)), ("person1", _date(1)), ("person1", _date(6))],
+ )
+
+ result = self.run_query(
+ query={
+ "dateRange": {"date_to": _date(10)},
+ "retentionFilter": {
+ "period": "Day",
+ "totalIntervals": 11,
+ "retentionType": RETENTION_FIRST_OCCURRENCE_MATCHING_FILTERS,
+ "retentionCustomBrackets": [4, 5], # Day 1-4, Day 5-9
+ },
+ }
+ )
+
+ day0_row = next(r for r in result if r["label"] == "Day 0")
+ self.assertEqual([v["count"] for v in day0_row["values"]], [1, 1, 1])
+
def test_custom_brackets_with_minimum_occurrences(self):
"""Test custom brackets with minimum occurrences (counted per day within bracket)"""
_create_person(team_id=self.team.pk, distinct_ids=["person1"])