From 24d4ab6fdf5510d45189c10362ec0a17567cbea2 Mon Sep 17 00:00:00 2001 From: Alan Heywood Date: Tue, 18 Nov 2025 16:24:58 +1000 Subject: [PATCH] test: add failing test for aggregate filtering on nested first aggregate --- lib/verifiers/validate_check_constraints.ex | 1 - test/aggregate_test.exs | 33 +++++++++++++++++++++ test/support/resources/comment.ex | 6 ++++ test/support/resources/post.ex | 8 +++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/verifiers/validate_check_constraints.ex b/lib/verifiers/validate_check_constraints.ex index 19debcf7..8ae8a960 100644 --- a/lib/verifiers/validate_check_constraints.ex +++ b/lib/verifiers/validate_check_constraints.ex @@ -33,4 +33,3 @@ defmodule AshPostgres.Verifiers.ValidateCheckConstraints do :ok end end - diff --git a/test/aggregate_test.exs b/test/aggregate_test.exs index f0fb2844..649b8e98 100644 --- a/test/aggregate_test.exs +++ b/test/aggregate_test.exs @@ -1920,6 +1920,39 @@ defmodule AshSql.AggregateTest do end end + test "multiple aggregates filtering on nested first aggregate" do + post = + Post + |> Ash.Changeset.for_create(:create, %{title: "test"}) + |> Ash.create!() + + comment = + Comment + |> Ash.Changeset.for_create(:create, %{title: "comment"}) + |> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove) + |> Ash.create!() + + Rating + |> Ash.Changeset.for_create(:create, %{score: 10, resource_id: comment.id}) + |> Ash.Changeset.set_context(%{data_layer: %{table: "comment_ratings"}}) + |> Ash.create!() + + # ERROR 42803 (grouping_error) aggregate functions are not allowed in FILTER + # + # Multiple Post aggregates filter on Comment.latest_rating_score (a first aggregate) + # AshPostgres includes ss1.latest_rating_score in SELECT but not in GROUP BY + # error: column "ss1.latest_rating_score" must appear in the GROUP BY clause + # + # This regression was introduced by ash_sql bb458d56 + assert {:ok, _} = + Post + |> Ash.Query.load([ + :count_of_comments_with_ratings, + :count_of_comments_with_high_ratings + ]) + |> Ash.read() + end + describe "page with count and aggregates with relationship-based calculations" do test "loads relationship-based calculations correctly when using page(count: true) with aggregates" do # This test reproduces the bug from https://github.com/ash-project/ash_sql/issues/191 diff --git a/test/support/resources/comment.ex b/test/support/resources/comment.ex index 133005db..8d95fa08 100644 --- a/test/support/resources/comment.ex +++ b/test/support/resources/comment.ex @@ -60,6 +60,10 @@ defmodule AshPostgres.Test.Comment do list :linked_comment_post_ids, [:linked_comments, :post], :id do uniq?(true) end + + first :latest_rating_score, :ratings, :score do + sort(score: :desc) + end end calculations do @@ -73,6 +77,8 @@ defmodule AshPostgres.Test.Comment do "hello" end)} end) + + calculate(:has_rating, :boolean, expr(not is_nil(latest_rating_score))) end relationships do diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index a4f04f7d..ca823c36 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -1278,6 +1278,14 @@ defmodule AshPostgres.Test.Post do count :count_comments_with_modify_query, :comments do read_action(:with_modify_query) end + + count :count_of_comments_with_ratings, :comments do + filter(expr(has_rating == true)) + end + + count :count_of_comments_with_high_ratings, :comments do + filter(expr(latest_rating_score > 5)) + end end end