Skip to content

Commit

Permalink
fix: retention query returns wrong results with target_first_time (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
aspicer authored Sep 11, 2024
1 parent 0321d97 commit 959b17f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
7 changes: 6 additions & 1 deletion posthog/hogql_queries/insights/retention_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ def actor_query(self, breakdown_values_filter: Optional[int] = None) -> ast.Sele
{
"target_timestamps": target_timestamps,
"min_timestamp": self.query_date_range.date_to_start_of_interval_hogql(
parse_expr("min(events.timestamp)")
parse_expr(
"minIf(events.timestamp, {target_entity_expr})",
{
"target_entity_expr": target_entity_expr,
},
)
),
},
)
Expand Down
58 changes: 58 additions & 0 deletions posthog/hogql_queries/insights/test/test_retention_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,64 @@ def test_all_events(self):
],
)

def test_all_events_target_first_time(self):
_create_person(team_id=self.team.pk, distinct_ids=["person1"])

_create_events(
self.team,
[
("person1", _date(0)),
("person1", _date(1)),
("person1", _date(2)),
],
"event1",
)

_create_events(
self.team,
[
("person1", _date(2)),
("person1", _date(3)),
],
"event2",
)

result_all_events = self.run_query(
query={
"dateRange": {"date_to": _date(2, hour=6)},
"retentionFilter": {
"retentionType": "retention_first_time",
"totalIntervals": 4,
"targetEntity": {"id": "event2", "type": "events"},
"returningEntity": {"id": None, "name": "All events"},
},
}
)

result_specific_event = self.run_query(
query={
"dateRange": {"date_to": _date(2, hour=6)},
"retentionFilter": {
"retentionType": "retention_first_time",
"totalIntervals": 4,
"targetEntity": {"id": "event2", "type": "events"},
"returningEntity": {"id": "event2", "type": "events"},
},
}
)

self.assertEqual(
pluck(result_specific_event, "values", "count"),
[
[0, 0, 0, 0],
[0, 0, 0],
[0, 0],
[1],
],
)

self.assertEqual(result_specific_event, result_all_events)

def test_retention_people_basic(self):
person1 = _create_person(team_id=self.team.pk, distinct_ids=["person1", "alias1"])
_create_person(team_id=self.team.pk, distinct_ids=["person2"])
Expand Down

0 comments on commit 959b17f

Please sign in to comment.