Skip to content

Commit c2717eb

Browse files
authored
fix: only merge already-computed aggregates when select? is true (#193)
Fixes a GROUP BY error introduced in bb458d5 where already-computed aggregates were being merged into the SELECT clause unconditionally. When aggregates filter on nested first aggregates the nested aggregate was incorrectly added to the SELECT clause in GROUP BY contexts, causing PostgreSQL grouping_error "column must appear in the GROUP BY clause or be used in an aggregate function" The fix adds a select? guard to only merge already-computed aggregates when we're in a select context. In internal subqueries with GROUP BY, aggregates remain available in bindings for FILTER clauses without being selected, avoiding the conflict. A test demonstrating the bug was added to ash_postgres/test/aggregate_test.exs test "multiple aggregates filtering on nested first aggregate"
1 parent f6e2028 commit c2717eb

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/aggregate.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ defmodule AshSql.Aggregate do
7373
|> Enum.split_with(&already_added?(&1, query.__ash_bindings__, []))
7474

7575
query =
76-
if Enum.any?(already_computed_aggregates) do
76+
if Enum.any?(already_computed_aggregates) && select? do
7777
query.__ash_bindings__.bindings
7878
|> Enum.filter(fn
7979
{_binding, %{type: :aggregate}} -> true

0 commit comments

Comments
 (0)