Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(trends): more hogql features #21176

Merged
merged 13 commits into from
Mar 27, 2024
2 changes: 1 addition & 1 deletion posthog/hogql_queries/hogql_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def to_actors_query(self) -> ast.SelectQuery:
def calculate(self) -> HogQLQueryResponse:
query = self.to_query()
paginator = None
if not query.limit:
if isinstance(query, ast.SelectQuery) and not query.limit:
paginator = HogQLHasMorePaginator.from_limit_context(limit_context=self.limit_context)
func = cast(
Callable[..., HogQLQueryResponse],
Expand Down
32 changes: 17 additions & 15 deletions posthog/hogql_queries/insights/trends/breakdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ def events_where_filter(self) -> ast.Expr | None:
if not self.is_histogram_breakdown:
left = hogql_to_string(left)

if self.query.breakdownFilter is not None:
hide_other = self.query.breakdownFilter.breakdown_hide_other_aggregation
breakdown_limit = self.query.breakdownFilter.breakdown_limit or 25
if len(self._breakdown_values) < breakdown_limit + (0 if hide_other else 1):
return ast.Constant(value=True)

compare_ops = []
for _value in self._breakdown_values:
value: Optional[str] = str(_value) # non-cohorts are always strings
Expand Down Expand Up @@ -172,21 +178,17 @@ def _get_breakdown_transform_func(self) -> ast.Call:
return self._get_breakdown_values_transform(ast.Field(chain=self._properties_chain))

def _get_breakdown_values_transform(self, node: ast.Expr) -> ast.Call:
breakdown_values = self._breakdown_values_ast
return ast.Call(
name="transform",
args=[
ast.Call(
name="ifNull",
args=[
hogql_to_string(node),
ast.Constant(value=BREAKDOWN_NULL_STRING_LABEL),
],
),
breakdown_values,
breakdown_values,
ast.Constant(value=BREAKDOWN_OTHER_STRING_LABEL),
],
return cast(
ast.Call,
parse_expr(
"transform(ifNull(nullIf(toString({node}), ''), {nil}), {values}, {values}, {other})",
placeholders={
"node": node,
"values": self._breakdown_values_ast,
"nil": ast.Constant(value=BREAKDOWN_NULL_STRING_LABEL),
"other": ast.Constant(value=BREAKDOWN_OTHER_STRING_LABEL),
},
),
)

@cached_property
Expand Down
1 change: 1 addition & 0 deletions posthog/hogql_queries/insights/trends/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ def _get_cumulative_query(self, inner_query: ast.SelectQuery, breakdown_enabled:
),
],
select_from=ast.JoinExpr(table=inner_query),
order_by=[ast.OrderExpr(expr=ast.Field(chain=["day_start"]), order="ASC")],
)
Loading
Loading