Trouble Referencing Actor in Count Aggregate

I have a File resource with a relationship to a FileSeen resource:

relationships do
  has_many :files_seen, FileSeen
end

I’m trying to get a “seen count” with an aggregate like this:

aggregates do
  count :seen_count, :files_seen, filter: expr(user_id == ^actor(:id))
end

When I call it…

MyProject.File |> Ash.get!("File Id", actor: user, load: :seen_count)

… I get the following error:

** (Ash.Error.Unknown) 
Bread Crumbs:
  > Exception raised in: MyProject.File.read

Unknown Error

* ** (RuntimeError) Unsupported expression in Elixir.AshPostgres.SqlImplementation query: {:_actor, :id}
  (ash_sql 0.2.57) lib/expr.ex:2102: AshSql.Expr.default_dynamic_expr/6
  (ash_sql 0.2.57) lib/expr.ex:2861: AshSql.Expr.maybe_type_expr/6
  (ash_sql 0.2.57) lib/expr.ex:1006: AshSql.Expr.default_dynamic_expr/6
  (ash_sql 0.2.57) lib/filter.ex:37: anonymous fn/2 in AshSql.Filter.add_filter_expression/2
  (elixir 1.18.1) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
  (ash_sql 0.2.57) lib/filter.ex:22: AshSql.Filter.filter/4
  (ash_sql 0.2.57) lib/aggregate.ex:883: anonymous fn/6 in AshSql.Aggregate.maybe_filter_subquery/6
  (elixir 1.18.1) lib/enum.ex:4964: Enumerable.List.reduce/3
  (elixir 1.18.1) lib/enum.ex:2600: Enum.reduce_while/3
  (ash_sql 0.2.57) lib/aggregate.ex:369: anonymous fn/10 in AshSql.Aggregate.add_aggregates/6
  (ash_sql 0.2.57) lib/join.ex:290: AshSql.Join.related_subquery/3
  (ash_sql 0.2.57) lib/aggregate.ex:217: anonymous fn/7 in AshSql.Aggregate.add_aggregates/6
  (elixir 1.18.1) lib/enum.ex:4964: Enumerable.List.reduce/3
  (elixir 1.18.1) lib/enum.ex:2600: Enum.reduce_while/3
  (ash_sql 0.2.57) lib/aggregate.ex:88: AshSql.Aggregate.add_aggregates/6
  (ash 3.4.64) lib/ash/query/query.ex:3157: Ash.Query.data_layer_query/2
  (ash 3.4.64) lib/ash/actions/read/read.ex:581: anonymous fn/8 in Ash.Actions.Read.do_read/5
  (ash 3.4.64) lib/ash/actions/read/read.ex:927: Ash.Actions.Read.maybe_in_transaction/3
  (ash 3.4.64) lib/ash/actions/read/read.ex:308: Ash.Actions.Read.do_run/3
  (ash 3.4.64) lib/ash/actions/read/read.ex:82: anonymous fn/3 in Ash.Actions.Read.run/3
    (ash_sql 0.2.57) lib/expr.ex:2102: AshSql.Expr.default_dynamic_expr/6
    (ash_sql 0.2.57) lib/expr.ex:2861: AshSql.Expr.maybe_type_expr/6
    (ash_sql 0.2.57) lib/expr.ex:1006: AshSql.Expr.default_dynamic_expr/6
    (ash_sql 0.2.57) lib/filter.ex:37: anonymous fn/2 in AshSql.Filter.add_filter_expression/2
    (elixir 1.18.1) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ash_sql 0.2.57) lib/filter.ex:22: AshSql.Filter.filter/4
    (ash_sql 0.2.57) lib/aggregate.ex:883: anonymous fn/6 in AshSql.Aggregate.maybe_filter_subquery/6
    (elixir 1.18.1) lib/enum.ex:4964: Enumerable.List.reduce/3
    (elixir 1.18.1) lib/enum.ex:2600: Enum.reduce_while/3
    (ash_sql 0.2.57) lib/aggregate.ex:369: anonymous fn/10 in AshSql.Aggregate.add_aggregates/6
    (ash_sql 0.2.57) lib/join.ex:290: AshSql.Join.related_subquery/3
    (ash_sql 0.2.57) lib/aggregate.ex:217: anonymous fn/7 in AshSql.Aggregate.add_aggregates/6
    (elixir 1.18.1) lib/enum.ex:4964: Enumerable.List.reduce/3
    (elixir 1.18.1) lib/enum.ex:2600: Enum.reduce_while/3
    (ash_sql 0.2.57) lib/aggregate.ex:88: AshSql.Aggregate.add_aggregates/6
    (ash 3.4.64) lib/ash/query/query.ex:3157: Ash.Query.data_layer_query/2
    (ash 3.4.64) lib/ash/actions/read/read.ex:581: anonymous fn/8 in Ash.Actions.Read.do_read/5
    (ash 3.4.64) lib/ash/actions/read/read.ex:927: Ash.Actions.Read.maybe_in_transaction/3
    (ash 3.4.64) lib/ash/actions/read/read.ex:308: Ash.Actions.Read.do_run/3
    iex:3: (file)

It works as expected if I replace ^actor(:id) with the id of the actor as a string. I’m using ash: 3.4.64 ash_sql: 0.2.57 and ash_postgres: 2.5.5

Someone else just opened a bug for this that I will be looking into soon. In the meantime if you use an inline aggregate it should work:

calculate :seen_count, :integer, expr(count(files_seen, query: [filter: user_id == ^actor(:id)]))

The inline aggregate works as expected.

I’ll keep an eye on the Github Issue and update the implementation when the bug gets resolved.

Thanks for your time!