Skip to content

Commit

Permalink
Extract compare to child calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeelaudibert committed Dec 3, 2024
1 parent a8e5ce6 commit cbcb8a9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
49 changes: 23 additions & 26 deletions posthog/hogql_queries/web_analytics/web_analytics_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,29 @@ def property_filters_without_pathname(
) -> list[Union[EventPropertyFilter, PersonPropertyFilter, SessionPropertyFilter]]:
return [p for p in self.query.properties if p.key != "$pathname"]

def period_aggregate(self, function_name, column_name, start, end, alias=None, params=None, compare=False):
if compare:
expr = ast.Call(
name=function_name + "If",
params=params,
args=[
ast.Field(chain=[column_name]),
ast.Call(
name="and",
args=[
ast.CompareOperation(
op=ast.CompareOperationOp.GtEq,
left=ast.Field(chain=["start_timestamp"]),
right=start,
),
ast.CompareOperation(
op=ast.CompareOperationOp.Lt,
left=ast.Field(chain=["start_timestamp"]),
right=end,
),
],
),
],
)
else:
expr = ast.Call(name=function_name, params=params, args=[ast.Field(chain=[column_name])])
def period_aggregate(self, function_name, column_name, start, end, alias=None, params=None):
expr = ast.Call(
name=function_name + "If",
params=params,
args=[
ast.Field(chain=[column_name]),
ast.Call(
name="and",
args=[
ast.CompareOperation(
op=ast.CompareOperationOp.GtEq,
left=ast.Field(chain=["start_timestamp"]),
right=start,
),
ast.CompareOperation(
op=ast.CompareOperationOp.Lt,
left=ast.Field(chain=["start_timestamp"]),
right=end,
),
],
),
],
)

if alias is not None:
return ast.Alias(alias=alias, expr=expr)
Expand Down
14 changes: 8 additions & 6 deletions posthog/hogql_queries/web_analytics/web_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,16 @@ def outer_select(self) -> ast.SelectQuery:
end = self.query_date_range.date_to_as_hogql()

def current_period_aggregate(function_name, column_name, alias, params=None):
return self.period_aggregate(
function_name, column_name, mid, end, alias=alias, params=params, compare=self.query.compare
)
if not self.query.compare:
return ast.Call(name=function_name, params=params, args=[ast.Field(chain=[column_name])])

return self.period_aggregate(function_name, column_name, mid, end, alias=alias, params=params)

def previous_period_aggregate(function_name, column_name, alias, params=None):
return self.period_aggregate(
function_name, column_name, start, mid, alias=alias, params=params, compare=self.query.compare
)
if not self.query.compare:
return ast.Alias(alias=alias, expr=ast.Constant(value=None))

return self.period_aggregate(function_name, column_name, start, mid, alias=alias, params=params)

if self.query.conversionGoal:
select = [
Expand Down

0 comments on commit cbcb8a9

Please sign in to comment.