Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/verifiers/validate_check_constraints.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ defmodule AshPostgres.Verifiers.ValidateCheckConstraints do
:ok
end
end

33 changes: 33 additions & 0 deletions test/aggregate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,39 @@
end
end

test "multiple aggregates filtering on nested first aggregate" do

Check failure on line 1923 in test/aggregate_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test multiple aggregates filtering on nested first aggregate (AshSql.AggregateTest)

Check failure on line 1923 in test/aggregate_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test multiple aggregates filtering on nested first aggregate (AshSql.AggregateTest)

Check failure on line 1923 in test/aggregate_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test multiple aggregates filtering on nested first aggregate (AshSql.AggregateTest)
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
Expand Down
6 changes: 6 additions & 0 deletions test/support/resources/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading