Skip to content

Commit

Permalink
columns
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Sep 26, 2023
1 parent 99add59 commit 20c42a6
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions posthog/hogql_queries/persons_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Optional, Any, Dict, List

from posthog.hogql import ast
from posthog.hogql.parser import parse_expr
from posthog.hogql.property import property_to_expr, has_aggregation
from posthog.hogql.query import execute_hogql_query
from posthog.hogql.timings import HogQLTimings
Expand Down Expand Up @@ -79,14 +80,23 @@ def filter_conditions(self) -> List[ast.Expr]:
)
return where_exprs

def columns(self) -> List[ast.Expr]:
return []

def to_query(self) -> ast.SelectQuery:
# adding +1 to the limit to check if there's a "next page" after the requested results
from posthog.hogql.constants import DEFAULT_RETURNED_ROWS, MAX_SELECT_RETURNED_ROWS

with self.timings.measure("filter_conditions"):
with self.timings.measure("columns"):
columns = [
ast.Field(chain=["id"]),
ast.Field(chain=["properties"]),
ast.Field(chain=["created_at"]),
ast.Field(chain=["is_identified"]),
*[parse_expr(expr) for expr in self.query.select or []],
]
group_by: List[ast.Expr] = [column for column in columns if not has_aggregation(column)]
aggregations: List[ast.Expr] = [column for column in columns if has_aggregation(column)]
has_any_aggregation = len(aggregations) > 0

with self.timings.measure("filters"):
filter_conditions = self.filter_conditions()
where_list = [expr for expr in filter_conditions if not has_aggregation(expr)]
if len(where_list) == 0:
Expand All @@ -113,11 +123,11 @@ def to_query(self) -> ast.SelectQuery:

with self.timings.measure("select"):
stmt = ast.SelectQuery(
select=self.columns(),
select=columns,
select_from=ast.JoinExpr(table=ast.Field(chain=["persons"])),
where=where,
having=having,
# group_by=group_by if has_any_aggregation else None,
group_by=group_by if has_any_aggregation else None,
# order_by=order_by,
limit=ast.Constant(value=limit),
offset=ast.Constant(value=offset),
Expand Down

0 comments on commit 20c42a6

Please sign in to comment.