From bb1e0aa2010e4c3c2c52774ae5fdd6d7d9c8740d Mon Sep 17 00:00:00 2001 From: Sandy Spicer Date: Tue, 7 May 2024 16:47:02 -0700 Subject: [PATCH] fix(insights): Pass Limit Context to breakdowns (#22169) --- .../insights/trends/breakdown_values.py | 3 +++ .../trends/test/test_trends_query_runner.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/posthog/hogql_queries/insights/trends/breakdown_values.py b/posthog/hogql_queries/insights/trends/breakdown_values.py index b15897b360fde..85502e7a31021 100644 --- a/posthog/hogql_queries/insights/trends/breakdown_values.py +++ b/posthog/hogql_queries/insights/trends/breakdown_values.py @@ -75,6 +75,7 @@ def __init__( self.breakdown_limit = breakdown_filter.breakdown_limit or get_breakdown_limit_for_context(limit_context) self.query_date_range = query_date_range self.modifiers = modifiers + self.limit_context = limit_context def get_breakdown_values(self) -> list[str | int]: if self.breakdown_type == "cohort": @@ -202,6 +203,7 @@ def get_breakdown_values(self) -> list[str | int]: query=query, team=self.team, modifiers=self.modifiers, + limit_context=self.limit_context, ) if response.results and len(response.results) > 0: values = response.results[0][0] @@ -215,6 +217,7 @@ def get_breakdown_values(self) -> list[str | int]: query=query, team=self.team, modifiers=self.modifiers, + limit_context=self.limit_context, ) value_index = (response.columns or []).index("value") values = [row[value_index] for row in response.results or []] diff --git a/posthog/hogql_queries/insights/trends/test/test_trends_query_runner.py b/posthog/hogql_queries/insights/trends/test/test_trends_query_runner.py index 772d71922727b..993aa84c8d96b 100644 --- a/posthog/hogql_queries/insights/trends/test/test_trends_query_runner.py +++ b/posthog/hogql_queries/insights/trends/test/test_trends_query_runner.py @@ -658,6 +658,24 @@ def test_formula_with_breakdown_and_no_data(self): ) self.assertEqual([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], response.results[0]["data"]) + @patch("posthog.hogql.query.sync_execute", wraps=sync_execute) + def test_breakdown_is_context_aware(self, mock_sync_execute: MagicMock): + self._create_test_events() + + self._run_trends_query( + self.default_date_from, + self.default_date_to, + IntervalType.day, + [EventsNode(event="$pageviewxxx"), EventsNode(event="$pageleavexxx")], + TrendsFilter(formula="A+2*B"), + BreakdownFilter(breakdown_type=BreakdownType.person, breakdown="$browser"), + limit_context=LimitContext.QUERY_ASYNC, + ) + + self.assertEqual(mock_sync_execute.call_count, 4) + for mock_execute_call_args in mock_sync_execute.call_args_list: + self.assertIn(f" max_execution_time={INCREASED_MAX_EXECUTION_TIME},", mock_execute_call_args[0][0]) + def test_trends_compare(self): self._create_test_events()