Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilbert09 committed Nov 10, 2023
1 parent 4b33ada commit cafd665
Show file tree
Hide file tree
Showing 5 changed files with 1,095 additions and 51 deletions.
49 changes: 42 additions & 7 deletions posthog/hogql_queries/insights/trends/aggregation_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,25 @@ def _parent_select_query(
placeholders={"inner_query": inner_query},
)

day_start = ast.Alias(
alias="day_start",
expr=ast.Call(
name=f"toStartOf{self.query_date_range.interval_name.title()}", args=[ast.Field(chain=["timestamp"])]
),
)

return parse_select(
"""
SELECT
counts AS total,
dateTrunc({interval}, timestamp) AS day_start
{day_start}
FROM {inner_query}
WHERE timestamp >= {date_from} AND timestamp <= {date_to}
""",
placeholders={
**self.query_date_range.to_placeholders(),
"inner_query": inner_query,
"day_start": day_start,
},
)

Expand Down Expand Up @@ -221,9 +229,9 @@ def _inner_select_query(
COUNT(DISTINCT actor_id) AS counts
FROM (
SELECT
toStartOfDay({date_to}) - toIntervalDay(number) AS timestamp
{date_to_start_of_interval} - {number_interval_period} AS timestamp
FROM
numbers(dateDiff('day', toStartOfDay({date_from} - {inclusive_lookback}), {date_to}))
numbers(dateDiff({interval}, {date_from_start_of_interval} - {inclusive_lookback}, {date_to}))
) d
CROSS JOIN {cross_join_select_query} e
WHERE
Expand All @@ -242,21 +250,48 @@ def _inner_select_query(
def _events_query(
self, events_where_clause: ast.Expr, sample_value: ast.RatioExpr
) -> ast.SelectQuery | ast.SelectUnionQuery:
date_filters = [
parse_expr(
"timestamp >= {date_from} - {inclusive_lookback}",
placeholders={
**self.query_date_range.to_placeholders(),
**self._interval_placeholders(),
},
),
parse_expr(
"timestamp <= {date_to}",
placeholders={
**self.query_date_range.to_placeholders(),
**self._interval_placeholders(),
},
),
]

where_clause_combined = ast.And(exprs=[events_where_clause, *date_filters])

if self._is_count_per_actor_variant():
day_start = ast.Alias(
alias="day_start",
expr=ast.Call(
name=f"toStartOf{self.query_date_range.interval_name.title()}",
args=[ast.Field(chain=["timestamp"])],
),
)

return parse_select(
"""
SELECT
count(e.uuid) AS total,
dateTrunc({interval}, timestamp) AS day_start
{day_start}
FROM events AS e
SAMPLE {sample}
WHERE {events_where_clause}
GROUP BY e.person_id, day_start
""",
placeholders={
**self.query_date_range.to_placeholders(),
"events_where_clause": events_where_clause,
"events_where_clause": where_clause_combined,
"sample": sample_value,
"day_start": day_start,
},
)

Expand All @@ -274,7 +309,7 @@ def _events_query(
actor_id
""",
placeholders={
"events_where_clause": events_where_clause,
"events_where_clause": where_clause_combined,
"sample": sample_value,
},
)
Expand Down
42 changes: 25 additions & 17 deletions posthog/hogql_queries/insights/trends/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _get_date_subqueries(self) -> List[ast.SelectQuery]:
"""
SELECT
0 AS total,
dateTrunc({interval}, {date_to}) - {number_interval_period} AS day_start
{date_to_start_of_interval} - {number_interval_period} AS day_start
FROM
numbers(
coalesce(dateDiff({interval}, {date_from}, {date_to}), 0)
Expand All @@ -81,7 +81,7 @@ def _get_date_subqueries(self) -> List[ast.SelectQuery]:
"""
SELECT
0 AS total,
{date_from} AS day_start
{date_from_start_of_interval} AS day_start
""",
placeholders={
**self.query_date_range.to_placeholders(),
Expand All @@ -101,7 +101,7 @@ def _get_date_subqueries(self) -> List[ast.SelectQuery]:
breakdown_value
FROM (
SELECT
dateTrunc({interval}, {date_to}) - {number_interval_period} AS day_start
{date_to_start_of_interval} - {number_interval_period} AS day_start
FROM
numbers(
coalesce(dateDiff({interval}, {date_from}, {date_to}), 0)
Expand All @@ -127,23 +127,30 @@ def _get_date_subqueries(self) -> List[ast.SelectQuery]:
]

def _get_events_subquery(self, no_modifications: Optional[bool]) -> ast.SelectQuery:
day_start = ast.Alias(
alias="day_start",
expr=ast.Call(
name=f"toStartOf{self.query_date_range.interval_name.title()}", args=[ast.Field(chain=["timestamp"])]
),
)

default_query = cast(
ast.SelectQuery,
parse_select(
"""
SELECT
{aggregation_operation} AS total,
dateTrunc({interval}, timestamp) AS day_start
{day_start}
FROM events AS e
SAMPLE {sample}
WHERE {events_filter}
GROUP BY day_start
""",
placeholders={
**self.query_date_range.to_placeholders(),
"events_filter": self._events_filter(),
"aggregation_operation": self._aggregation_operation.select_aggregation(),
"sample": self._sample_value(),
"day_start": day_start,
},
),
)
Expand Down Expand Up @@ -239,18 +246,19 @@ def _events_filter(self) -> ast.Expr:
filters: List[ast.Expr] = []

# Dates
filters.extend(
[
parse_expr(
"timestamp >= {date_from}",
placeholders=self.query_date_range.to_placeholders(),
),
parse_expr(
"timestamp <= {date_to}",
placeholders=self.query_date_range.to_placeholders(),
),
]
)
if not self._aggregation_operation.requires_query_orchestration():
filters.extend(
[
parse_expr(
"timestamp >= {date_from}",
placeholders=self.query_date_range.to_placeholders(),
),
parse_expr(
"timestamp <= {date_to}",
placeholders=self.query_date_range.to_placeholders(),
),
]
)

# Series
if series_event_name(self.series) is not None:
Expand Down
Loading

0 comments on commit cafd665

Please sign in to comment.