diff --git a/posthog/hogql_queries/insights/trends/test/test_trends_data_warehouse_query.py b/posthog/hogql_queries/insights/trends/test/test_trends_data_warehouse_query.py index 3ceaf1af473dd..4d4501578546c 100644 --- a/posthog/hogql_queries/insights/trends/test/test_trends_data_warehouse_query.py +++ b/posthog/hogql_queries/insights/trends/test/test_trends_data_warehouse_query.py @@ -17,8 +17,8 @@ TrendsQuery, TrendsFilter, ) -from posthog.test.base import BaseTest -from posthog.warehouse.models import DataWarehouseTable, DataWarehouseCredential +from posthog.test.base import BaseTest, _create_event +from posthog.warehouse.models import DataWarehouseTable, DataWarehouseCredential, DataWarehouseJoin from boto3 import resource from botocore.config import Config @@ -268,6 +268,82 @@ def test_trends_breakdown(self): assert response.results[3][1] == [0, 0, 0, 1, 0, 0, 0] assert response.results[3][2] == "d" + def test_trends_breakdown_with_event_property(self): + table_name = self.create_parquet_file() + + _create_event( + distinct_id="1", + event="a", + properties={"$feature/prop_1": "a"}, + timestamp="2023-01-01 00:00:00", + team=self.team, + ) + _create_event( + distinct_id="1", + event="b", + properties={"$feature/prop_1": "b"}, + timestamp="2023-01-01 00:00:00", + team=self.team, + ) + _create_event( + distinct_id="1", + event="c", + properties={"$feature/prop_1": "c"}, + timestamp="2023-01-01 00:00:00", + team=self.team, + ) + _create_event( + distinct_id="1", + event="d", + properties={"$feature/prop_1": "d"}, + timestamp="2023-01-01 00:00:00", + team=self.team, + ) + + DataWarehouseJoin.objects.create( + team=self.team, + source_table_name=table_name, + source_table_key="prop_1", + joining_table_name="events", + joining_table_key="event", + field_name="events", + ) + + trends_query = TrendsQuery( + kind="TrendsQuery", + dateRange=InsightDateRange(date_from="2023-01-01"), + series=[ + DataWarehouseNode( + id=table_name, + table_name=table_name, + id_field="id", + distinct_id_field="customer_email", + timestamp_field="created", + ) + ], + breakdownFilter=BreakdownFilter( + breakdown_type=BreakdownType.DATA_WAREHOUSE, breakdown="events.properties.$feature/prop_1" + ), + ) + + with freeze_time("2023-01-07"): + response = self.get_response(trends_query=trends_query) + + assert response.columns is not None + assert set(response.columns).issubset({"date", "total", "breakdown_value"}) + assert len(response.results) == 4 + assert response.results[0][1] == [1, 0, 0, 0, 0, 0, 0] + assert response.results[0][2] == "a" + + assert response.results[1][1] == [0, 1, 0, 0, 0, 0, 0] + assert response.results[1][2] == "b" + + assert response.results[2][1] == [0, 0, 1, 0, 0, 0, 0] + assert response.results[2][2] == "c" + + assert response.results[3][1] == [0, 0, 0, 1, 0, 0, 0] + assert response.results[3][2] == "d" + @snapshot_clickhouse_queries def test_trends_breakdown_on_view(self): from posthog.warehouse.models import DataWarehouseSavedQuery diff --git a/posthog/hogql_queries/insights/trends/trends_query_runner.py b/posthog/hogql_queries/insights/trends/trends_query_runner.py index d66695fbac984..668cd8b2afb48 100644 --- a/posthog/hogql_queries/insights/trends/trends_query_runner.py +++ b/posthog/hogql_queries/insights/trends/trends_query_runner.py @@ -888,7 +888,16 @@ def _is_breakdown_filter_field_boolean(self): if not table_or_view: raise ValueError(f"Table {series.table_name} not found") - field_type = dict(table_or_view.columns)[self.query.breakdownFilter.breakdown]["clickhouse"] + breakdown_key = ( + self.query.breakdownFilter.breakdown[0] + if isinstance(self.query.breakdownFilter.breakdown, list) + else self.query.breakdownFilter.breakdown + ) + + if breakdown_key not in dict(table_or_view.columns): + return False + + field_type = dict(table_or_view.columns)[breakdown_key]["clickhouse"] if field_type.startswith("Nullable("): field_type = field_type.replace("Nullable(", "")[:-1] diff --git a/posthog/hogql_queries/insights/trends/utils.py b/posthog/hogql_queries/insights/trends/utils.py index 09bbaa5b0e153..d6baf201e4dee 100644 --- a/posthog/hogql_queries/insights/trends/utils.py +++ b/posthog/hogql_queries/insights/trends/utils.py @@ -26,7 +26,7 @@ def get_properties_chain( raise Exception("group_type_index missing from params") if breakdown_type == "data_warehouse": - return [breakdown_field] + return [*breakdown_field.split(".")] if breakdown_type == "data_warehouse_person_property": return ["person", *breakdown_field.split(".")]