Skip to content

Commit

Permalink
Correct the matching used in building a distinct expression
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanrborn committed Jan 26, 2024
1 parent 29da61d commit 8611270
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2573,7 +2573,7 @@ defmodule AshPostgres.DataLayer do

def distinct(query, distinct_on, resource) do
case get_distinct_statement(query, distinct_on) do
{:ok, distinct_statement, query} ->
{:ok, {distinct_statement, query}} ->
%{query | distinct: distinct_statement}
|> apply_sort(query.__ash_bindings__[:sort], resource)

Expand Down Expand Up @@ -2692,7 +2692,7 @@ defmodule AshPostgres.DataLayer do

distinct_on, {[order_by | rest_order_by], distinct_statement, params, count, query} ->
case order_by do
{^distinct_on, order} ->
{distinct_on, order} = ^distinct_on ->
{distinct_expr, params, count, query} =
distinct_on_expr(query, distinct_on, params, count)

Expand All @@ -2710,11 +2710,11 @@ defmodule AshPostgres.DataLayer do

{_, result, params, _, query} ->
{:ok,
%{
distinct
| expr: distinct.expr ++ Enum.reverse(result),
params: distinct.params ++ Enum.reverse(params)
}, query}
{%{
distinct
| expr: distinct.expr ++ Enum.reverse(result),
params: distinct.params ++ Enum.reverse(params)
}, query}}
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/distinct_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,13 @@ defmodule AshPostgres.DistinctTest do
%{title: "title", negative_score: -1}
] = results
end

test "distinct used on it's own" do
results =
Post
|> Ash.Query.distinct(:title)
|> Api.read!()

assert [_, _] = results
end
end

0 comments on commit 8611270

Please sign in to comment.