Ecto Filter/2 Why Not Dynamics?

filter is a PostgreSQL feature that allows you to aggregate with a conditional clause that is super handy in some scenarios. A contrived example like: sum(t.meal_price) FILTER WHERE t.meal_cost > 3 This lets you very easily partition or limit the aggregation of a column based on some other property of the data your selecting.

In the docs, you can see the example:

from p in Payment, select: filter(avg(p.value), p.value > 0 and p.value < 100)

The p.value > 0 and p.value < 100 seems like a perfect place to support dynamics to allow for customization of a base case through user inputs, but dynamics aren’t supported there.

I’m just curious if this is an explicit design decision or some other technical limitation of Ecto that prevents the use of dynamics in that case. I have a specific idea in mind that would suit this extremely well but can’t use it currently.

1 Like

I think it is simply not implemented yet, and the explanation for not having support for filter + dynamic is the same as in case of filter + exists: Raise on invalid filter, closes #3776 · elixir-ecto/ecto@ef442e2 · GitHub