Re: is any reason why only one columns subselect are allowed in array()? - Mailing list pgsql-hackers

From Sam Mason
Subject Re: is any reason why only one columns subselect are allowed in array()?
Date
Msg-id 20081118184140.GV2459@frubble.xen.chris-lamb.co.uk
Whole thread Raw
In response to Re: is any reason why only one columns subselect are allowed in array()?  (Sam Mason <sam@samason.me.uk>)
Responses Re: is any reason why only one columns subselect are allowed in array()?  ("Pavel Stehule" <pavel.stehule@gmail.com>)
List pgsql-hackers
On Tue, Nov 18, 2008 at 06:22:21PM +0000, Sam Mason wrote:
> I've always been taught to design things so that the that the basic
> semantics should be as simple as possible which maintaining useful

this should of course be "while"!           ^^^^^ 

> performance.

>   CREATE FUNCTION array_concat_(ANYARRAY,ANYARRAY) RETURNS ANYARRAY
>     AS $$ SELECT array_cat($1,ARRAY[$2]); $$
>     LANGUAGE SQL
>     IMMUTABLE;
> 
>   CREATE AGGREGATE array_concat (ANYARRAY) (
>       sfunc = array_concat_,
>       stype = ANYARRAY,
>       initcond = '{}'
>   );
> 
> A demo query being:
> 
>   SELECT array_concat(a) FROM (VALUES
>     (ARRAY[1,2,3]),
>     (ARRAY[5,6,7]),
>     (ARRAY[7,8,9])) x(a);
> 
> is that somewhat correct?

I've just realized that this doesn't work in simple cases like the
following:
 SELECT (SELECT array_concat(a) FROM (VALUES   (1), (5), (7)) x(a));

To do this, another pair of function and aggregate is needed, this time
parametrized on ANYNONARRAY rather than ANYARRAY.  It would be nice if
PG's type system was sufficiently clever to allow the above definition
to be parametrized on ANYELEMENT (or maybe something else) and it all
work out, but never mind...


Another point came to mind as well; why does array_accum pretty much
duplicate the behavior of array(subquery)?  The following looks like a
useful and general transform:
 SELECT accum_fn(subquery);

to:
 SELECT (accum_fn(c1..cn) FROM (subquery) x(c1..cn));

What have I missed?

 Sam


pgsql-hackers by date:

Previous
From: "Pavel Stehule"
Date:
Subject: Re: is any reason why only one columns subselect are allowed in array()?
Next
From: Tom Lane
Date:
Subject: Re: is any reason why only one columns subselect are allowed in array()?