0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 09:16:49 +01:00
posthog/ee/clickhouse/queries/groups_join_query.py
Eric Duong 4e0e69ac91
[actors] Stickiness actor pattern (#7440)
* separate class

* filter typing

* remove repeat

* refactor test

* add limits

* remove ee

* working tests

* working group tests for stickiness

* remove inits

* suggested changes

* typing

* adjust typing

* add correct params

* remove repeat
2021-12-02 11:43:20 -05:00

55 lines
2.0 KiB
Python

from typing import Dict, Optional, Tuple, Union
from ee.clickhouse.queries.column_optimizer import ColumnOptimizer
from posthog.models import Filter
from posthog.models.filters.path_filter import PathFilter
from posthog.models.filters.retention_filter import RetentionFilter
from posthog.models.filters.stickiness_filter import StickinessFilter
class GroupsJoinQuery:
"""
Query class responsible for joining with `groups` clickhouse table based on filters
"""
_filter: Union[Filter, PathFilter, RetentionFilter, StickinessFilter]
_team_id: int
_column_optimizer: ColumnOptimizer
def __init__(
self,
filter: Union[Filter, PathFilter, RetentionFilter, StickinessFilter],
team_id: int,
column_optimizer: Optional[ColumnOptimizer] = None,
join_key: Optional[str] = None,
) -> None:
self._filter = filter
self._team_id = team_id
self._column_optimizer = column_optimizer or ColumnOptimizer(self._filter, self._team_id)
self._join_key = join_key
def get_join_query(self) -> Tuple[str, Dict]:
join_queries, params = [], {}
for group_type_index in self._column_optimizer.group_types_to_query:
var = f"group_index_{group_type_index}"
group_join_key = self._join_key or f"$group_{group_type_index}"
join_queries.append(
f"""
INNER JOIN (
SELECT
group_key,
argMax(group_properties, _timestamp) AS group_properties_{group_type_index}
FROM groups
WHERE team_id = %(team_id)s AND group_type_index = %({var})s
GROUP BY group_key
) groups_{group_type_index}
ON {group_join_key} == groups_{group_type_index}.group_key
"""
)
params["team_id"] = self._team_id
params[var] = group_type_index
return "\n".join(join_queries), params