I was trying to use Ecto.Query with jsonb_to_recordset() function from postgres and it's kinda impossible. I think either subquery or a new macro(idk, raw?) could allow to pass an entire raw sql that only could be composed as a subquery or cte.
i'm trying to have the following query written with Ecto.Query api:
update table as t
join jsonb_to_recordset(...) as r(id1 uuid, id2 uuid) on
t.id = r.id1
set t.external_id = r.id2, updated_at = now()
i tried:
from(r in fragment("jsonb_to_recordset(?)", ^list_of_maps), as: :"r(id1 uuid, id2 uuid)", select: %{id1: r.id1, id2: r.id2})
and
from(r in fragment("jsonb_to_recordset(?) as r(id1 uuid, id2 uuid)", ^list_of_maps),, select: %{id1: r.id1, id2: r.id2})
but the as: option on ecto is just for the ecto api, doesn't become part of the query, and the second one produce wrong syntax sql(with 2 as statements).