Skip to content

Commit

Permalink
feat(hogql): Allow breakdowns for lazy-joined tables (#26302)
Browse files Browse the repository at this point in the history
Co-authored-by: eric <[email protected]>
  • Loading branch information
danielbachhuber and EDsCODE authored Nov 20, 2024
1 parent 4ff32c5 commit 010e353
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion posthog/hogql_queries/insights/trends/trends_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql_queries/insights/trends/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")]
Expand Down

0 comments on commit 010e353

Please sign in to comment.