2022-03-22 21:27:11 +01:00
|
|
|
from typing import Counter, List, Set, cast
|
2021-08-19 09:17:40 +02:00
|
|
|
|
2021-10-07 14:24:25 +02:00
|
|
|
from posthog.constants import TREND_FILTER_TYPE_ACTIONS, FunnelCorrelationType
|
2022-03-22 21:27:11 +01:00
|
|
|
from posthog.models.action.util import get_action_tables_and_properties
|
2021-08-19 09:17:40 +02:00
|
|
|
from posthog.models.entity import Entity
|
|
|
|
from posthog.models.filters.mixins.utils import cached_property
|
2021-11-17 11:49:49 +01:00
|
|
|
from posthog.models.filters.stickiness_filter import StickinessFilter
|
2021-12-02 14:21:15 +01:00
|
|
|
from posthog.models.filters.utils import GroupTypeIndex
|
2022-03-22 21:27:11 +01:00
|
|
|
from posthog.models.property import PropertyIdentifier
|
2022-06-17 15:31:54 +02:00
|
|
|
from posthog.models.property.util import box_value, extract_tables_and_properties
|
2022-06-22 23:24:03 +02:00
|
|
|
from posthog.queries.column_optimizer.foss_column_optimizer import FOSSColumnOptimizer
|
2021-08-19 09:17:40 +02:00
|
|
|
|
|
|
|
|
2022-06-22 23:24:03 +02:00
|
|
|
class EnterpriseColumnOptimizer(FOSSColumnOptimizer):
|
2021-11-03 19:43:22 +01:00
|
|
|
@cached_property
|
|
|
|
def group_types_to_query(self) -> Set[GroupTypeIndex]:
|
|
|
|
used_properties = self._used_properties_with_type("group")
|
2021-12-02 14:21:15 +01:00
|
|
|
return set(cast(GroupTypeIndex, group_type_index) for _, _, group_type_index in used_properties)
|
2021-11-03 19:43:22 +01:00
|
|
|
|
2021-08-19 09:17:40 +02:00
|
|
|
@cached_property
|
2021-11-03 19:43:22 +01:00
|
|
|
def properties_used_in_filter(self) -> Counter[PropertyIdentifier]:
|
2021-10-13 16:00:47 +02:00
|
|
|
"Returns collection of properties + types that this query would use"
|
2022-03-01 19:54:13 +01:00
|
|
|
counter: Counter[PropertyIdentifier] = extract_tables_and_properties(self.filter.property_groups.flat)
|
2021-08-19 09:17:40 +02:00
|
|
|
|
2021-11-17 11:49:49 +01:00
|
|
|
if not isinstance(self.filter, StickinessFilter):
|
|
|
|
# Some breakdown types read properties
|
|
|
|
#
|
|
|
|
# See ee/clickhouse/queries/trends/breakdown.py#get_query or
|
|
|
|
# ee/clickhouse/queries/breakdown_props.py#get_breakdown_prop_values
|
|
|
|
if self.filter.breakdown_type in ["event", "person"]:
|
2021-11-22 12:20:01 +01:00
|
|
|
boxed_breakdown = box_value(self.filter.breakdown)
|
|
|
|
for b in boxed_breakdown:
|
|
|
|
if isinstance(b, str):
|
|
|
|
counter[(b, self.filter.breakdown_type, self.filter.breakdown_group_type_index)] += 1
|
2021-11-17 11:49:49 +01:00
|
|
|
elif self.filter.breakdown_type == "group":
|
|
|
|
# :TRICKY: We only support string breakdown for group properties
|
|
|
|
assert isinstance(self.filter.breakdown, str)
|
|
|
|
counter[
|
|
|
|
(self.filter.breakdown, self.filter.breakdown_type, self.filter.breakdown_group_type_index)
|
|
|
|
] += 1
|
2021-08-19 09:17:40 +02:00
|
|
|
|
2021-12-07 16:05:03 +01:00
|
|
|
# If we have a breakdowns attribute then make sure we pull in everything we
|
|
|
|
# need to calculate it
|
|
|
|
for breakdown in self.filter.breakdowns or []:
|
|
|
|
counter[(breakdown["property"], breakdown["type"], self.filter.breakdown_group_type_index)] += 1
|
|
|
|
|
2021-08-19 09:17:40 +02:00
|
|
|
# Both entities and funnel exclusions can contain nested property filters
|
|
|
|
for entity in self.filter.entities + cast(List[Entity], self.filter.exclusions):
|
2022-03-01 19:54:13 +01:00
|
|
|
counter += extract_tables_and_properties(entity.property_groups.flat)
|
2021-08-19 09:17:40 +02:00
|
|
|
|
|
|
|
# Math properties are also implicitly used.
|
|
|
|
#
|
|
|
|
# See ee/clickhouse/queries/trends/util.py#process_math
|
|
|
|
if entity.math_property:
|
2021-11-03 19:43:22 +01:00
|
|
|
counter[(entity.math_property, "event", None)] += 1
|
2021-08-19 09:17:40 +02:00
|
|
|
|
2021-11-05 12:08:32 +01:00
|
|
|
# If groups are involved, they're also used
|
|
|
|
#
|
|
|
|
# See ee/clickhouse/queries/trends/util.py#process_math
|
|
|
|
if entity.math == "unique_group":
|
|
|
|
counter[(f"$group_{entity.math_group_type_index}", "event", None)] += 1
|
|
|
|
|
2022-06-21 18:13:36 +02:00
|
|
|
if entity.math == "unique_session":
|
|
|
|
counter[(f"$session_id", "event", None)] += 1
|
|
|
|
|
2021-08-19 09:17:40 +02:00
|
|
|
# :TRICKY: If action contains property filters, these need to be included
|
|
|
|
#
|
|
|
|
# See ee/clickhouse/models/action.py#format_action_filter for an example
|
|
|
|
if entity.type == TREND_FILTER_TYPE_ACTIONS:
|
2021-10-13 16:00:47 +02:00
|
|
|
counter += get_action_tables_and_properties(entity.get_action())
|
2021-08-19 09:17:40 +02:00
|
|
|
|
2021-11-17 11:49:49 +01:00
|
|
|
if (
|
|
|
|
not isinstance(self.filter, StickinessFilter)
|
|
|
|
and self.filter.correlation_type == FunnelCorrelationType.PROPERTIES
|
|
|
|
and self.filter.correlation_property_names
|
|
|
|
):
|
2021-11-12 12:32:55 +01:00
|
|
|
|
|
|
|
if self.filter.aggregation_group_type_index is not None:
|
|
|
|
for prop_value in self.filter.correlation_property_names:
|
|
|
|
counter[(prop_value, "group", self.filter.aggregation_group_type_index)] += 1
|
|
|
|
else:
|
|
|
|
for prop_value in self.filter.correlation_property_names:
|
|
|
|
counter[(prop_value, "person", None)] += 1
|
2021-10-13 16:00:47 +02:00
|
|
|
|
|
|
|
return counter
|