Re: Can ARRAY( ... ) generate text[][]?

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: greg(at)ngender(dot)net
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Can ARRAY( ... ) generate text[][]?
Date: 2010-06-11 11:20:41
Message-ID: AANLkTinxdtaxkciIxK48M_ILED_NkNF3qVADijmrRumf@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jun 10, 2010 at 6:59 PM, J. Greg Davidson <greg(at)ngender(dot)net> wrote:
> Hi fellow PostgreSQL hackers!
>
> I tried to write an SQL glue function to turn an array
> of alternating key/value pairs into an array of arrays
> and got the message
>
> ERROR:  42704: could not find array type for data type text[]

I do it like this:

create type pair_t as (key text, value text);

> -- BEGIN CODE
>
> -- Here's a simplified example:
>
> CREATE OR REPLACE
> FUNCTION text__(variadic text[]) RETURNS text[][] AS $$
> SELECT ARRAY(
>      SELECT ARRAY[ $1[i], $1[i+1] ]
>      FROM generate_series(1, array_upper($1,1), 2) i
> )
> $$ LANGUAGE sql;

create or replace function pairs(variadic text[]) returns pair_t[] as
$$
select array(select ($1[i], $1[i+1])::pair_t
FROM generate_series(1, array_upper($1,1), 2) i)
$$ language sql immutable;

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sergey Konoplev 2010-06-11 11:54:47 Re: Partial indexes instead of partitions
Previous Message Raymond O'Donnell 2010-06-11 10:36:16 Re: Moving a live production database to different server and postgres release