AE
Ash Elixir•2y ago
kernel

Aggregates still not working correctly

I have a few custom aggregates that have a filter on them that don't seem to work anymore.
BackOffice.Vehicle
|> Ash.Query.aggregate(:pending_jobs_today_count, :count, :jobs, filter: expr(status == "pending"))
|> BackOffice.Api.read!(tenant: "prod")
BackOffice.Vehicle
|> Ash.Query.aggregate(:pending_jobs_today_count, :count, :jobs, filter: expr(status == "pending"))
|> BackOffice.Api.read!(tenant: "prod")
generates sql that doesn't include the filter at all
SELECT v0."id", v0."name", v0."enabled", v0."registration", v0."model", v0."inserted_at", v0."updated_at", v0."deleted_at", coalesce(s1."pending_jobs_today_count"::bigint, $1::bigint)::bigint FROM "prod"."vehicles" AS v0 LEFT OUTER JOIN LATERAL (SELECT coalesce(count(sj0."id"::uuid), $2::bigint)::bigint AS "pending_jobs_today_count", sj0."vehicle_id" AS "vehicle_id" FROM "prod"."jobs" AS sj0 WHERE ((sj0."deleted_at"::timestamptz IS NULL) = $3::boolean) AND (v0."id" = sj0."vehicle_id") GROUP BY sj0."vehicle_id") AS s1 ON TRUE WHERE ((v0."deleted_at"::timestamptz IS NULL) = $4::boolean) [0, 0, true, true]
SELECT v0."id", v0."name", v0."enabled", v0."registration", v0."model", v0."inserted_at", v0."updated_at", v0."deleted_at", coalesce(s1."pending_jobs_today_count"::bigint, $1::bigint)::bigint FROM "prod"."vehicles" AS v0 LEFT OUTER JOIN LATERAL (SELECT coalesce(count(sj0."id"::uuid), $2::bigint)::bigint AS "pending_jobs_today_count", sj0."vehicle_id" AS "vehicle_id" FROM "prod"."jobs" AS sj0 WHERE ((sj0."deleted_at"::timestamptz IS NULL) = $3::boolean) AND (v0."id" = sj0."vehicle_id") GROUP BY sj0."vehicle_id") AS s1 ON TRUE WHERE ((v0."deleted_at"::timestamptz IS NULL) = $4::boolean) [0, 0, true, true]
This had worked before, but I'm assuming with the changes made to aggregates was broken
9 Replies
ZachDaniel
ZachDaniel•2y ago
Can I see query.aggregates after you’ve added the aggregate?
kernel
kernelOP•2y ago
#Ash.Query<
resource: BackOffice.Vehicle,
aggregates: %{
pending_jobs_today_count: #count<
jobs from #Ash.Query<
resource: BackOffice.Job,
filter: #Ash.Filter<deleted_at is nil>,
calculations: %{
weights_missing?: #Ash.Resource.Calculation.Expression<[expr: completed? and track_weights? and is_nil(disposal_weight) and is_nil(:disposal_price_id) and is_nil(skipped_at)]>
}
>
>
}
>
#Ash.Query<
resource: BackOffice.Vehicle,
aggregates: %{
pending_jobs_today_count: #count<
jobs from #Ash.Query<
resource: BackOffice.Job,
filter: #Ash.Filter<deleted_at is nil>,
calculations: %{
weights_missing?: #Ash.Resource.Calculation.Expression<[expr: completed? and track_weights? and is_nil(disposal_weight) and is_nil(:disposal_price_id) and is_nil(skipped_at)]>
}
>
>
}
>
ZachDaniel
ZachDaniel•2y ago
and you're on the latest versions of everything?
kernel
kernelOP•2y ago
as of 2 days ago?
kernel
kernelOP•2y ago
No description
ZachDaniel
ZachDaniel•2y ago
Will look into this today Well the reason for this is because there is no filter option to Ash.Query.aggregate 🙂
BackOffice.Vehicle
|> Ash.Query.aggregate(:pending_jobs_today_count, :count, :jobs, query: [filter: expr(status == "pending")])
|> BackOffice.Api.read!(tenant: "prod")
BackOffice.Vehicle
|> Ash.Query.aggregate(:pending_jobs_today_count, :count, :jobs, query: [filter: expr(status == "pending")])
|> BackOffice.Api.read!(tenant: "prod")
there is a query option We had some bad examples doing this in the AshPostgres tests, although the docs do show the query option nope, they don't the function docs do, but the examples in the docs use filter 😢 fixed, will be updated on the next deploy
kernel
kernelOP•2y ago
filter used to work hahaa so use query, or use filter
ZachDaniel
ZachDaniel•2y ago
use query I think it might have been the change to the options list version of aggregate that used to be the positional query argument but now its opts? Honestly, I don't know. We'll clean it up in 3.0, sorry it got you
kernel
kernelOP•2y ago
🙂 all good, bug finder kernel!

Did you find this page helpful?