From 46118a77caeddf3fe0a0542a6da4466eccf5087c Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Fri, 22 Dec 2023 22:23:49 -0500 Subject: [PATCH] improvement: support aggregates as `get_path` subject --- lib/expr.ex | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/expr.ex b/lib/expr.ex index cc799f1e..2d8ea867 100644 --- a/lib/expr.ex +++ b/lib/expr.ex @@ -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{