UNNEST and multidimensional arrays

From: Mike Martin <redtux1(at)gmail(dot)com>
To: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: UNNEST and multidimensional arrays
Date: 2020-07-30 13:28:17
Message-ID: CAOwYNKbSq7QCWV0x1vqBc85vDQT+aH7AYNZ39h5rWzWyzSTG_g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi
Is there anyway to control how many levels unnest unnests?
ie: if I have (in perl) an array of arrays for unnest to only unnest the
top level, leaving the sub levels as an array, so a select would show
{val1,val2}
{val1,val2}
{val1,val2}
rather than
val1
val2
val1
val2
val1
val2

Currently I am using a variation of the fooling code from postgres wiki
(from 2013)

https://wiki.postgresql.org/wiki/Unnest_multidimensional_array

CREATE OR REPLACE FUNCTION public.reduce_dim(
anyarray)
RETURNS SETOF anyarray
LANGUAGE 'plpgsql'

COST 100
VOLATILE
ROWS 1000
AS $BODY$
DECLARE s $1%type;
BEGIN
IF cardinality ($1::text[]) >0 THEN
FOREACH s SLICE 1 IN ARRAY $1 LOOP
RETURN NEXT s;
END LOOP;
END IF;
RETURN;
END;
$BODY$;

thanks in advance

Mike

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Pavel Stehule 2020-07-30 15:05:59 Re: UNNEST and multidimensional arrays
Previous Message karsten 2020-07-25 21:50:44 RE: plphyton function - return each list value as a row ?