Skip to content
Closed
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
12 changes: 12 additions & 0 deletions lib/aggregate/lateral.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,18 @@ defmodule AshSql.Aggregate.Lateral do
:return
)

# `DISTINCT` needs each `ORDER BY` to match the aggregated expression exactly,
# including its cast, so order by `field` itself rather than the bare column.
sort_expr =
if Map.get(aggregate, :uniq?) && !dedupe_sorted? do
sort
|> AshSql.Sort.sanitize_sort()
|> Enum.map(fn {order, _} -> {order, field} end)
|> AshSql.Sort.order_to_fragments()
else
sort_expr
end

question_marks = Enum.map(sort_expr, fn _ -> " ? " end)

distinct =
Expand Down
Loading