Skip to content

Commit 46118a7

Browse files
committed
improvement: support aggregates as get_path subject
1 parent fb7f7ad commit 46118a7

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

lib/expr.ex

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,48 @@ defmodule AshPostgres.Expr do
263263
{Ecto.Query.dynamic(fragment("(?)", datetime_add(^date, ^amount, ^to_string(interval)))), acc}
264264
end
265265

266+
defp do_dynamic_expr(
267+
query,
268+
%GetPath{
269+
arguments: [
270+
%Ref{attribute: %Ash.Resource.Aggregate{} = aggregate, resource: resource} = left,
271+
right
272+
],
273+
embedded?: pred_embedded?
274+
},
275+
bindings,
276+
embedded?,
277+
acc,
278+
_
279+
)
280+
when is_list(right) do
281+
attribute =
282+
if aggregate.field do
283+
related = Ash.Resource.Info.related(resource, aggregate.relationship_path)
284+
Ash.Resource.Info.attribute(related, aggregate.field)
285+
end
286+
287+
attribute_type =
288+
if attribute do
289+
attribute.type
290+
end
291+
292+
attribute_constraints =
293+
if attribute do
294+
attribute.constraints
295+
end
296+
297+
{:ok, type, constraints} =
298+
Ash.Query.Aggregate.kind_to_type(aggregate.kind, attribute_type, attribute_constraints)
299+
300+
type
301+
|> Ash.Resource.Info.aggregate_type(aggregate)
302+
|> split_at_paths(constraints, right)
303+
|> Enum.reduce(do_dynamic_expr(query, left, bindings, embedded?, acc), fn data, {expr, acc} ->
304+
do_get_path(query, expr, data, bindings, embedded?, pred_embedded?, acc)
305+
end)
306+
end
307+
266308
defp do_dynamic_expr(
267309
query,
268310
%GetPath{

0 commit comments

Comments
 (0)