Skip to content

Commit c71ea6f

Browse files
authored
fix: Sanitize distinct in joins (#168)
1 parent 610679c commit c71ea6f

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

lib/join.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ defmodule AshSql.Join do
697697
distinct =
698698
Enum.flat_map(sort, fn
699699
{attribute, direction} when is_atom(attribute) ->
700-
[{direction, attribute}]
700+
[{attribute, direction}]
701701

702702
{%Ash.Query.Calculation{} = calculation, direction} ->
703703
resource = joined_query.__ash_bindings__.resource
@@ -707,14 +707,16 @@ defmodule AshSql.Join do
707707

708708
case AshSql.Expr.dynamic_expr(calc_query, expression, calc_query.__ash_bindings__) do
709709
{result, _} when is_atom(result) -> []
710-
{dynamic_expr, _} -> [{direction, dynamic_expr}]
710+
{dynamic_expr, _} -> [{dynamic_expr, direction}]
711711
end
712712

713713
_ ->
714714
[]
715715
end) ++
716716
Ash.Resource.Info.primary_key(joined_query.__ash_bindings__.resource)
717717

718+
distinct = distinct |> AshSql.Sort.sanitize_sort()
719+
718720
if joined_query.__ash_bindings__.sql_behaviour.multicolumn_distinct?() do
719721
from(row in joined_query,
720722
distinct: ^distinct

lib/sort.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ defmodule AshSql.Sort do
463463
Map.update!(query, :__ash_bindings__, &Map.put(&1, :sort_applied?, true))
464464
end
465465

466-
defp sanitize_sort(sort) do
466+
def sanitize_sort(sort) do
467467
sort
468468
|> List.wrap()
469469
|> Enum.map(fn

0 commit comments

Comments
 (0)