Skip to content

Commit

Permalink
improvement: support aggregates as get_path subject
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Dec 23, 2023
1 parent fb7f7ad commit 46118a7
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/expr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,48 @@ defmodule AshPostgres.Expr do
{Ecto.Query.dynamic(fragment("(?)", datetime_add(^date, ^amount, ^to_string(interval)))), acc}
end

defp do_dynamic_expr(
query,
%GetPath{
arguments: [
%Ref{attribute: %Ash.Resource.Aggregate{} = aggregate, resource: resource} = left,
right
],
embedded?: pred_embedded?
},
bindings,
embedded?,
acc,
_
)
when is_list(right) do
attribute =
if aggregate.field do
related = Ash.Resource.Info.related(resource, aggregate.relationship_path)
Ash.Resource.Info.attribute(related, aggregate.field)
end

attribute_type =
if attribute do
attribute.type
end

attribute_constraints =
if attribute do
attribute.constraints
end

{:ok, type, constraints} =
Ash.Query.Aggregate.kind_to_type(aggregate.kind, attribute_type, attribute_constraints)

type
|> Ash.Resource.Info.aggregate_type(aggregate)
|> split_at_paths(constraints, right)
|> Enum.reduce(do_dynamic_expr(query, left, bindings, embedded?, acc), fn data, {expr, acc} ->
do_get_path(query, expr, data, bindings, embedded?, pred_embedded?, acc)
end)
end

defp do_dynamic_expr(
query,
%GetPath{
Expand Down

0 comments on commit 46118a7

Please sign in to comment.