1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-16 06:01:02 +03:00

Support arrays as input to array_agg() and ARRAY(SELECT ...).

These cases formerly failed with errors about "could not find array type
for data type".  Now they yield arrays of the same element type and one
higher dimension.

The implementation involves creating functions with API similar to the
existing accumArrayResult() family.  I (tgl) also extended the base family
by adding an initArrayResult() function, which allows callers to avoid
special-casing the zero-inputs case if they just want an empty array as
result.  (Not all do, so the previous calling convention remains valid.)
This allowed simplifying some existing code in xml.c and plperl.c.

Ali Akbar, reviewed by Pavel Stehule, significantly modified by me
This commit is contained in:
Tom Lane
2014-11-25 12:21:22 -05:00
parent 25976710df
commit bac27394a1
18 changed files with 797 additions and 115 deletions

View File

@ -12035,7 +12035,7 @@ NULL baz</literallayout>(3 rows)</entry>
<function>array_agg(<replaceable class="parameter">expression</replaceable>)</function>
</entry>
<entry>
any
any non-array type
</entry>
<entry>
array of the argument type
@ -12043,6 +12043,21 @@ NULL baz</literallayout>(3 rows)</entry>
<entry>input values, including nulls, concatenated into an array</entry>
</row>
<row>
<entry>
<function>array_agg(<replaceable class="parameter">expression</replaceable>)</function>
</entry>
<entry>
any array type
</entry>
<entry>
same as argument data type
</entry>
<entry>input arrays concatenated into array of one higher dimension
(inputs must all have same dimensionality,
and cannot be empty or NULL)</entry>
</row>
<row>
<entry>
<indexterm>

View File

@ -2239,11 +2239,22 @@ SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%');
-----------------------------------------------------------------------
{2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31,2412,2413}
(1 row)
SELECT ARRAY(SELECT ARRAY[i, i*2] FROM generate_series(1,5) AS a(i));
array
----------------------------------
{{1,2},{2,4},{3,6},{4,8},{5,10}}
(1 row)
</programlisting>
The subquery must return a single column. The resulting
The subquery must return a single column.
If the subquery's output column is of a non-array type, the resulting
one-dimensional array will have an element for each row in the
subquery result, with an element type matching that of the
subquery's output column.
If the subquery's output column is of an array type, the result will be
an array of the same type but one higher dimension; in this case all
the subquery rows must yield arrays of identical dimensionality, else
the result would not be rectangular.
</para>
<para>

View File

@ -359,12 +359,12 @@ SELECT attrelid::regclass, array_accum(atttypid::regtype)
aggregate <function>array_agg</> is equivalent to
<programlisting>
CREATE FUNCTION array_agg_transfn(internal, anyelement)
CREATE FUNCTION array_agg_transfn(internal, anynonarray)
RETURNS internal ...;
CREATE FUNCTION array_agg_finalfn(internal, anyelement)
CREATE FUNCTION array_agg_finalfn(internal, anynonarray)
RETURNS anyarray ...;
CREATE AGGREGATE array_agg (anyelement)
CREATE AGGREGATE array_agg (anynonarray)
(
sfunc = array_agg_transfn,
stype = internal,
@ -376,7 +376,7 @@ CREATE AGGREGATE array_agg (anyelement)
Here, the <literal>finalfunc_extra</> option specifies that the final
function receives, in addition to the state value, extra dummy
argument(s) corresponding to the aggregate's input argument(s).
The extra <type>anyelement</> argument allows the declaration
The extra <type>anynonarray</> argument allows the declaration
of <function>array_agg_finalfn</> to be valid.
</para>