Skip to content

Commit

Permalink
Abstract property chain into a cached prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilbert09 committed Oct 12, 2023
1 parent 16febec commit c0211d9
Showing 1 changed file with 12 additions and 28 deletions.
40 changes: 12 additions & 28 deletions posthog/hogql_queries/insights/trends/breakdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,12 @@ def column_expr(self) -> ast.Expr:

return ast.Alias(
alias="breakdown_value",
expr=ast.Field(
chain=get_properties_chain(
breakdown_type=self.query.breakdown.breakdown_type,
breakdown_field=self.query.breakdown.breakdown,
group_type_index=self.query.breakdown.breakdown_group_type_index,
)
),
expr=ast.Field(chain=self._properties_chain),
)

def events_where_filter(self) -> ast.Expr:
return ast.CompareOperation(
left=ast.Field(
chain=get_properties_chain(
breakdown_type=self.query.breakdown.breakdown_type,
breakdown_field=self.query.breakdown.breakdown,
group_type_index=self.query.breakdown.breakdown_group_type_index,
)
),
left=ast.Field(chain=self._properties_chain),
op=ast.CompareOperationOp.In,
right=self._breakdown_values_ast,
)
Expand Down Expand Up @@ -118,24 +106,12 @@ def _get_breakdown_histogram_multi_if(self) -> ast.Expr:
ast.And(
exprs=[
ast.CompareOperation(
left=ast.Field(
chain=get_properties_chain(
breakdown_type=self.query.breakdown.breakdown_type,
breakdown_field=self.query.breakdown.breakdown,
group_type_index=self.query.breakdown.breakdown_group_type_index,
)
),
left=ast.Field(chain=self._properties_chain),
op=ast.CompareOperationOp.GtEq,
right=ast.Constant(value=lower_bound),
),
ast.CompareOperation(
left=ast.Field(
chain=get_properties_chain(
breakdown_type=self.query.breakdown.breakdown_type,
breakdown_field=self.query.breakdown.breakdown,
group_type_index=self.query.breakdown.breakdown_group_type_index,
)
),
left=ast.Field(chain=self._properties_chain),
op=ast.CompareOperationOp.Lt,
right=ast.Constant(value=upper_bound),
),
Expand All @@ -149,3 +125,11 @@ def _get_breakdown_histogram_multi_if(self) -> ast.Expr:
multi_if_exprs.append(ast.Constant(value='["",""]'))

return ast.Call(name="multiIf", args=multi_if_exprs)

@cached_property
def _properties_chain(self):
return get_properties_chain(
breakdown_type=self.query.breakdown.breakdown_type,
breakdown_field=self.query.breakdown.breakdown,
group_type_index=self.query.breakdown.breakdown_group_type_index,
)

0 comments on commit c0211d9

Please sign in to comment.