From bac27394a1c69c20ec904729c593e59485c75c69 Mon Sep 17 00:00:00 2001
From: Tom Lane
Date: Tue, 25 Nov 2014 12:21:22 -0500
Subject: 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
---
doc/src/sgml/func.sgml | 17 ++++++++++++++++-
doc/src/sgml/syntax.sgml | 13 ++++++++++++-
doc/src/sgml/xaggr.sgml | 8 ++++----
3 files changed, 32 insertions(+), 6 deletions(-)
(limited to 'doc/src')
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 90a3460a712..baf81ee0404 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -12035,7 +12035,7 @@ NULL baz(3 rows)
array_agg(expression)
- any
+ any non-array type
array of the argument type
@@ -12043,6 +12043,21 @@ NULL baz(3 rows)
input values, including nulls, concatenated into an array
+
+
+ array_agg(expression)
+
+
+ any array type
+
+
+ same as argument data type
+
+ input arrays concatenated into array of one higher dimension
+ (inputs must all have same dimensionality,
+ and cannot be empty or NULL)
+
+
diff --git a/doc/src/sgml/syntax.sgml b/doc/src/sgml/syntax.sgml
index 399ae070759..6f8b7e8b28e 100644
--- a/doc/src/sgml/syntax.sgml
+++ b/doc/src/sgml/syntax.sgml
@@ -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)
- 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.
diff --git a/doc/src/sgml/xaggr.sgml b/doc/src/sgml/xaggr.sgml
index cc8ec64f235..ef7cff48794 100644
--- a/doc/src/sgml/xaggr.sgml
+++ b/doc/src/sgml/xaggr.sgml
@@ -359,12 +359,12 @@ SELECT attrelid::regclass, array_accum(atttypid::regtype)
aggregate array_agg> is equivalent to
-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 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 anyelement> argument allows the declaration
+ The extra anynonarray> argument allows the declaration
of array_agg_finalfn> to be valid.
--
cgit v1.2.3