You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm sorry to re-open this old issue. But it seems than the "unnest" example from the Postgres documentation is not working with sqlc.
I'm wondering if it's something can be fixed or no.
I have had this same issue. My workaround was to use JSONB instead of a pair of ARRAYs, but it's ugly.
Hi @carlmjohnson do you have an example? Because I've tried something like this but it's not working
WITH to_update as (
SELECT x
FROM JSON_TO_RECORDSET(@auth_users::JSON)
AS x("id"BIGINT, "label"VARCHAR(55))
)
UPDATE auth_users
SET label =to_update.labelFROM to_update
WHEREauth_users.id=to_update.idANDauth_users.user_id= @user_id;
Going to close this out as we have a pretty good workaround.
Reacted by Brennan Lamey, Brendan Lo, Lauren, Triwandha Febian, Patrick, Nikolay Latyshev, Alexandr Kalmyk, Jack Carmichael, yihaoye, Illia Dymura and 10 more
beside jsonb_array, there are another 2 way which maybe better in performance if array too large for json marshal in code
postgresql 10+ could do the following uses parallel arrays evaluated in lockstep, it will return [(1, a), (2, b), (3, null), (4, null)]
SELECT
unnest(ARRAY[1, 2, 3, 4]) as id,
unnest(ARRAY['a', 'b']) as name;
there is 3rd way to work around by WITH ORDINALITY + JOIN
WITH pairs AS (
SELECTa.val::bigintAS num,
b.val::uuid AS id,
c.val::varcharAS description
FROM unnest(@array_a::bigint[]) WITH ORDINALITY AS a(val, ord)
JOIN unnest(@array_b::uuid[]) WITH ORDINALITY AS b(val, ord) ONa.ord=b.ordJOIN unnest(@array_c::varchar[]) WITH ORDINALITY AS c(val, ord) ONo.ord=c.ord
)
SELECTpairs.num,
pairs.id,
pairs.description,
FROM pairs
Activity
maratori commentedon Mar 30, 2021
I successfully use following query with
unnesttcastelly commentedon Mar 30, 2021
Thank you for your answer.
I can compile your query.
In fact I can compile this:
But because I need multiple arrays in
unnestit looks like I have to use:and here it's broken:
maratori commentedon Mar 30, 2021
Have you tried multiple
unnests for multiple arrays? Something like this should work:tcastelly commentedon Mar 30, 2021
Oh yes thank you :)
tcastelly commentedon Jun 17, 2022
I'm sorry to re-open this old issue. But it seems than the "unnest" example from the Postgres documentation is not working with
sqlc.I'm wondering if it's something can be fixed or no.
https://www.postgresql.org/docs/current/functions-array.html
result: https://play.sqlc.dev/p/29816032c2fa5f604524ad3089121ef2284bf59c84d871ede995f78bcd4f11c1
Split unsset can be build by
sqlcbut it's not a solution because the result of the query is not the same:Thank you very much.
earthboundkid commentedon Mar 10, 2023
I have had this same issue. My workaround was to use JSONB instead of a pair of ARRAYs, but it's ugly.
tcastelly commentedon Apr 5, 2023
Hi @carlmjohnson do you have an example? Because I've tried something like this but it's not working
Issue: #2191
Thank you :)
earthboundkid commentedon Apr 5, 2023
https://github.com/spotlightpa/almanack/blob/d48b9e2167754e56b729788fe04d1270f00c38af/sql/queries/gdocs_image.sql#L6
kyleconroy commentedon Sep 22, 2023
Going to close this out as we have a pretty good workaround.
yihaoye commentedon Jan 16, 2026
beside jsonb_array, there are another 2 way which maybe better in performance if array too large for json marshal in code
postgresql 10+ could do the following uses parallel arrays evaluated in lockstep, it will return
[(1, a), (2, b), (3, null), (4, null)]there is 3rd way to work around by
WITH ORDINALITY+JOIN