Skip to content

Commit

Permalink
mamamamaa
Browse files Browse the repository at this point in the history
  • Loading branch information
aspicer committed Dec 4, 2024
1 parent a85c7ab commit be67be7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 50 deletions.
2 changes: 1 addition & 1 deletion posthog/hogql_queries/insights/funnels/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
)
from posthog.types import EntityNode, ExclusionEntityNode

JOIN_ALGOS = "direct,parallel_hash,hash,full_sorting_merge"
JOIN_ALGOS = "auto"


class FunnelBase(ABC):
Expand Down
14 changes: 13 additions & 1 deletion posthog/hogql_queries/insights/trends/breakdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from posthog.hogql.parser import parse_expr
from posthog.hogql.timings import HogQLTimings
from posthog.hogql_queries.insights.trends.display import TrendsDisplay
from posthog.hogql_queries.insights.trends.utils import get_properties_chain
from posthog.hogql_queries.insights.trends.utils import (
get_properties_chain,
is_breakdown_field_boolean,
convert_to_boolean,
)
from posthog.hogql_queries.utils.query_date_range import QueryDateRange
from posthog.models.filters.mixins.utils import cached_property
from posthog.models.team.team import Team
Expand Down Expand Up @@ -301,6 +305,14 @@ def _get_actors_query_where_expr(
]
)

is_boolean = is_breakdown_field_boolean(self.team, breakdown_value, breakdown_type, group_type_index)

if is_boolean:
return ast.CompareOperation(
left=left,
op=ast.CompareOperationOp.Eq,
right=ast.Constant(value=convert_to_boolean(lookup_value)),
)
if is_numeric_breakdown:
if lookup_value == BREAKDOWN_NUMERIC_ALL_VALUES_PLACEHOLDER:
return None
Expand Down
56 changes: 8 additions & 48 deletions posthog/hogql_queries/insights/trends/trends_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from posthog.hogql_queries.insights.trends.series_with_extras import SeriesWithExtras
from posthog.hogql_queries.insights.trends.trends_actors_query_builder import TrendsActorsQueryBuilder
from posthog.hogql_queries.insights.trends.trends_query_builder import TrendsQueryBuilder
from posthog.hogql_queries.insights.trends.utils import is_breakdown_field_boolean
from posthog.hogql_queries.query_runner import QueryRunner
from posthog.hogql_queries.utils.formula_ast import FormulaAST
from posthog.hogql_queries.utils.query_compare_to_date_range import QueryCompareToDateRange
Expand All @@ -42,7 +43,6 @@
from posthog.models.action.action import Action
from posthog.models.cohort.cohort import Cohort
from posthog.models.filters.mixins.utils import cached_property
from posthog.models.property_definition import PropertyDefinition
from posthog.queries.util import correct_result_for_sampling
from posthog.schema import (
ActionsNode,
Expand Down Expand Up @@ -910,59 +910,17 @@ def _is_breakdown_filter_field_boolean(self):
if field_type == "Bool":
return True

return self._is_breakdown_field_boolean(
return is_breakdown_field_boolean(
self.team,
self.query.breakdownFilter.breakdown,
self.query.breakdownFilter.breakdown_type,
self.query.breakdownFilter.breakdown_group_type_index,
)

def _is_breakdown_field_boolean(
self,
breakdown_value: str | int | list[str | int],
breakdown_type: BreakdownType | MultipleBreakdownType | None,
breakdown_group_type_index: int | None = None,
):
if breakdown_type == "hogql" or breakdown_type == "cohort" or breakdown_type == "session":
return False

if breakdown_type == "person":
property_type = PropertyDefinition.Type.PERSON
elif breakdown_type == "group":
property_type = PropertyDefinition.Type.GROUP
else:
property_type = PropertyDefinition.Type.EVENT

field_type = self._event_property(
str(breakdown_value),
property_type,
breakdown_group_type_index,
)

return field_type == "Boolean"

def _convert_boolean(self, value: Any):
bool_map = {1: "true", 0: "false", "": "", "1": "true", "0": "false"}
return bool_map.get(value) or value

def _event_property(
self,
field: str,
field_type: PropertyDefinition.Type,
group_type_index: Optional[int],
) -> str:
try:
return (
PropertyDefinition.objects.get(
name=field,
team=self.team,
type=field_type,
group_type_index=group_type_index if field_type == PropertyDefinition.Type.GROUP else None,
).property_type
or "String"
)
except PropertyDefinition.DoesNotExist:
return "String"

# TODO: Move this to posthog/hogql_queries/legacy_compatibility/query_to_filter.py
def _query_to_filter(self) -> dict[str, Any]:
filter_dict = {
Expand Down Expand Up @@ -1010,7 +968,9 @@ def _format_breakdown_label(self, breakdown_value: Any):
if self.query.breakdownFilter is not None and self.query.breakdownFilter.breakdowns is not None:
labels = []
for breakdown, label in zip(self.query.breakdownFilter.breakdowns, breakdown_value):
if self._is_breakdown_field_boolean(breakdown.property, breakdown.type, breakdown.group_type_index):
if is_breakdown_field_boolean(
self.team, breakdown.property, breakdown.type, breakdown.group_type_index
):
labels.append(self._convert_boolean(label))
else:
labels.append(label)
Expand Down Expand Up @@ -1053,8 +1013,8 @@ def _get_breakdown_items(
elif value == BREAKDOWN_NULL_STRING_LABEL:
label = BREAKDOWN_NULL_DISPLAY
elif (
self._is_breakdown_field_boolean(
breakdown_value, breakdown_type, breakdown_group_type_index=group_type_index
is_breakdown_field_boolean(
self.team, breakdown_value, breakdown_type, breakdown_group_type_index=group_type_index
)
or is_boolean_field
):
Expand Down
54 changes: 54 additions & 0 deletions posthog/hogql_queries/insights/trends/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Optional, Union

from posthog.models import PropertyDefinition, Team
from posthog.schema import ActionsNode, DataWarehouseNode, EventsNode, BreakdownType, MultipleBreakdownType


Expand Down Expand Up @@ -32,3 +34,55 @@ def get_properties_chain(
return ["person", *breakdown_field.split(".")]

return ["properties", breakdown_field]


def event_property(
team: Team,
field: str,
field_type: PropertyDefinition.Type,
group_type_index: Optional[int],
) -> str:
try:
return (
PropertyDefinition.objects.get(
name=field,
team=team,
type=field_type,
group_type_index=group_type_index if field_type == PropertyDefinition.Type.GROUP else None,
).property_type
or "String"
)
except PropertyDefinition.DoesNotExist:
return "String"


def is_breakdown_field_boolean(
team: Team,
breakdown_value: str | int | list[str | int],
breakdown_type: BreakdownType | MultipleBreakdownType | None,
breakdown_group_type_index: int | None = None,
):
if breakdown_type == "hogql" or breakdown_type == "cohort" or breakdown_type == "session":
return False

if breakdown_type == "person":
property_type = PropertyDefinition.Type.PERSON
elif breakdown_type == "group":
property_type = PropertyDefinition.Type.GROUP
else:
property_type = PropertyDefinition.Type.EVENT

field_type = event_property(
team,
str(breakdown_value),
property_type,
breakdown_group_type_index,
)

return field_type == "Boolean"


def convert_to_boolean(value: str):
if value.lower() in ("false", "0"):
return False
return True

0 comments on commit be67be7

Please sign in to comment.