fix: subquery aggregates when a distinct is being added

This commit is contained in:
Zach Daniel 2023-04-22 01:17:38 -06:00
parent 903b1dd2e7
commit d417361012
2 changed files with 21 additions and 5 deletions

View file

@ -668,11 +668,21 @@ defmodule AshPostgres.DataLayer do
@impl true
def run_aggregate_query(query, aggregates, resource) do
query =
query
|> Ecto.Query.exclude(:select)
|> Ecto.Query.exclude(:order_by)
|> Map.put(:windows, [])
|> Ecto.Query.select(%{})
if query.distinct do
query =
query
|> Ecto.Query.exclude(:select)
|> Ecto.Query.exclude(:order_by)
|> Map.put(:windows, [])
from(row in subquery(query), as: ^0, select: %{})
else
query
|> Ecto.Query.exclude(:select)
|> Ecto.Query.exclude(:order_by)
|> Map.put(:windows, [])
|> Ecto.Query.select(%{})
end
query =
Enum.reduce(

View file

@ -953,6 +953,12 @@ defmodule AshPostgres.AggregateTest do
|> Api.read_one!()
end
test "a count with a filter that references a to many relationship can be aggregated at the query level" do
Post
|> Ash.Query.filter(comments.likes > 10)
|> Api.count!()
end
test "a count with a filter that references a relationship combined with another" do
post =
Post