Skip site navigation (1) Skip section navigation (2)

Re: unnest on multi-dimensional arrays

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Zev Benjamin <zev-pgsql(at)strangersgate(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: unnest on multi-dimensional arrays
Date: 2013-11-28 07:28:19
Message-ID: CAFj8pRD3DrqCa68NjHUcC8=yT1=7XvtYkxUMYFuuDuboixDcjw@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-general
Hello

postgres=# CREATE OR REPLACE FUNCTION public.reduce_dim(anyarray)
 RETURNS SETOF anyarray
 LANGUAGE plpgsql
AS $function$
DECLARE s $1%type;
BEGIN
  FOREACH s SLICE 1  IN ARRAY $1 LOOP
      RETURN NEXT s;
  END LOOP;
RETURN;
END;
$function$;
CREATE FUNCTION

postgres=# select reduce_dim(array[array[1, 2], array[2, 3]]);
 reduce_dim
------------
 {1,2}
 {2,3}
(2 rows)

Regards

Pavel Stehule

2013/11/28 Zev Benjamin <zev-pgsql(at)strangersgate(dot)com>

> It appears that unnest, when called on a multi-dimensional array,
> effectively flattens the array first.  For example:
>
> => select * from unnest(array[array[1, 2], array[2, 3]]);
>  unnest
> --------
>       1
>       2
>       2
>       3
> (4 rows)
>
> while I would have expect something like the following:
>
> => select * from unnest(array[array[1, 2], array[2, 3]]);
>  unnest
> --------
>    {1, 2}
>    {2, 3}
> (2 rows)
>
> Is there any way to get the latter behavior?
>
>
> Zev
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Responses

pgsql-general by date

Next:From: David JohnstonDate: 2013-11-28 07:35:01
Subject: Re: unnest on multi-dimensional arrays
Previous:From: Zev BenjaminDate: 2013-11-28 07:06:08
Subject: unnest on multi-dimensional arrays

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group