Skip to content

Commit e7530aa

Browse files
authored
test: add failing test for aggregate filtering on nested first aggregate (#653)
1 parent d801d31 commit e7530aa

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

lib/verifiers/validate_check_constraints.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ defmodule AshPostgres.Verifiers.ValidateCheckConstraints do
3333
:ok
3434
end
3535
end
36-

test/aggregate_test.exs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,39 @@ defmodule AshSql.AggregateTest do
19201920
end
19211921
end
19221922

1923+
test "multiple aggregates filtering on nested first aggregate" do
1924+
post =
1925+
Post
1926+
|> Ash.Changeset.for_create(:create, %{title: "test"})
1927+
|> Ash.create!()
1928+
1929+
comment =
1930+
Comment
1931+
|> Ash.Changeset.for_create(:create, %{title: "comment"})
1932+
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
1933+
|> Ash.create!()
1934+
1935+
Rating
1936+
|> Ash.Changeset.for_create(:create, %{score: 10, resource_id: comment.id})
1937+
|> Ash.Changeset.set_context(%{data_layer: %{table: "comment_ratings"}})
1938+
|> Ash.create!()
1939+
1940+
# ERROR 42803 (grouping_error) aggregate functions are not allowed in FILTER
1941+
#
1942+
# Multiple Post aggregates filter on Comment.latest_rating_score (a first aggregate)
1943+
# AshPostgres includes ss1.latest_rating_score in SELECT but not in GROUP BY
1944+
# error: column "ss1.latest_rating_score" must appear in the GROUP BY clause
1945+
#
1946+
# This regression was introduced by ash_sql bb458d56
1947+
assert {:ok, _} =
1948+
Post
1949+
|> Ash.Query.load([
1950+
:count_of_comments_with_ratings,
1951+
:count_of_comments_with_high_ratings
1952+
])
1953+
|> Ash.read()
1954+
end
1955+
19231956
describe "page with count and aggregates with relationship-based calculations" do
19241957
test "loads relationship-based calculations correctly when using page(count: true) with aggregates" do
19251958
# This test reproduces the bug from https://github.com/ash-project/ash_sql/issues/191

test/support/resources/comment.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ defmodule AshPostgres.Test.Comment do
6060
list :linked_comment_post_ids, [:linked_comments, :post], :id do
6161
uniq?(true)
6262
end
63+
64+
first :latest_rating_score, :ratings, :score do
65+
sort(score: :desc)
66+
end
6367
end
6468

6569
calculations do
@@ -73,6 +77,8 @@ defmodule AshPostgres.Test.Comment do
7377
"hello"
7478
end)}
7579
end)
80+
81+
calculate(:has_rating, :boolean, expr(not is_nil(latest_rating_score)))
7682
end
7783

7884
relationships do

test/support/resources/post.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,14 @@ defmodule AshPostgres.Test.Post do
12781278
count :count_comments_with_modify_query, :comments do
12791279
read_action(:with_modify_query)
12801280
end
1281+
1282+
count :count_of_comments_with_ratings, :comments do
1283+
filter(expr(has_rating == true))
1284+
end
1285+
1286+
count :count_of_comments_with_high_ratings, :comments do
1287+
filter(expr(latest_rating_score > 5))
1288+
end
12811289
end
12821290
end
12831291

0 commit comments

Comments
 (0)