ARRAY_AGG¶
Collects the values of an expression across the rows of a group into a single array.
Syntax¶
Parameters¶
expressionThe value collected from each row of the group. May be of any type except
ARRAY. The result is an array of the argument’s type.ALLCollects every value of
expression, which is the default behavior when no set quantifier is present.IGNORE NULLSCauses
NULLvalues ofexpressionto be omitted from the resulting array.RESPECT NULLSCauses
NULLvalues ofexpressionto be collected as array elements. This is the default when no null-treatment clause is present. This behavior is subject to limitations; see the note onNULLhandling under Important Notes.
Returns¶
Returns an array whose elements are the values of expression in the group. The order of elements within the array is unspecified.
The element type of the array is non-nullable when IGNORE NULLS is used, or when expression itself is non-nullable. Otherwise (that is, for a nullable expression with RESPECT NULLS behavior, which is the default) the element type is nullable. (However, see the note below regarding a current limitation on NULL elements in arrays.)
The behavior on empty input depends on whether a GROUP BY clause is present:
Without
GROUP BY, aggregating over an empty input returns a single row whose array value isNULL.With
GROUP BY, aggregating over an empty input returns no rows.
A group that does contain rows, but whose expression values are all NULL, returns an empty array [] rather than NULL under IGNORE NULLS. This holds whether or not a GROUP BY clause is present.
Examples¶
Setup¶
For these examples, assume we have a sales table:
CREATE TABLE sales (
id BIGINT,
product STRING,
region STRING,
amount BIGINT,
PRIMARY KEY (id)
)
CREATE INDEX product_idx ON sales(product)
INSERT INTO sales VALUES
(1, 'Widget', 'North', 100),
(2, 'Widget', 'South', 150),
(3, 'Gadget', 'North', 200),
(4, 'Gadget', 'South', NULL),
(5, 'Widget', 'North', 120)
The product_idx index is needed for the GROUP BY product query to be planned; see the note on required indexes under Important Notes.
ARRAY_AGG() without GROUP BY¶
The following query collects the amounts across the whole table into a single array. IGNORE NULLS is used here, so that NULL amounts are skipped rather than collected.
SELECT ARRAY_AGG(amount IGNORE NULLS) AS amounts FROM sales
|
|---|
|
Note that the NULL amount in row 4 is therefore omitted from the array.
Note also that the elements do not appear in id order. They are collected in whatever order the rows happen to be read in, and here the query is served by a scan of product_idx, which visits the Gadget row before the Widget rows. Adding or removing an index may therefore change the order of the elements within the array.
ARRAY_AGG() with GROUP BY¶
The following query collects amounts per product.
SELECT product, ARRAY_AGG(amount IGNORE NULLS) AS amounts
FROM sales
GROUP BY product
|
|
|---|---|
|
|
|
|
The Gadget group contains two rows, but the NULL amount is omitted, so its array has a single element.
ARRAY_AGG() versus unnesting¶
Array aggregation can be viewed as the inverse operation of unnesting an array. The following example unnests an array literal into a stream of rows and then collects those rows back with ARRAY_AGG(), reproducing the original array elements (although the order in which they come back is not guaranteed).
SELECT ARRAY_AGG(x) AS numbers
FROM (SELECT a FROM VALUES ([2, 1, -2, 3, -2, 1, 2]) AS T(a)) AS sq,
sq.a AS x
|
|---|
|
See Unnesting for the unnesting syntax used by the inner query.
Important notes¶
Required indexes: In general,
GROUP BYqueries require an appropriate index to be executed. See Indexes for details on creating indexes that supportGROUP BYoperations.ARRAY_AGG() in indexes:
ARRAY_AGG()itself cannot currently be materialized in an index. Defining an index over it, as inCREATE INDEX idx AS SELECT ARRAY_AGG(val) FROM tab GROUP BY grp, raises anUNSUPPORTED_OPERATIONerror.Element order: The order of the elements within the returned array is unspecified. Elements are collected in whatever order the rows are read in, which depends on the plan used to execute the query—in particular on which index is used, if any. You therefore cannot rely on the order. There is currently no way to request a particular order, since an in-call
ORDER BYclause is not supported yet. This limitation is tracked by Issue #4498.NULL handling: An array cannot currently hold
NULLelements. This is due to a limitation at the level of the FDB Record Layer, tracked by Issue #3646. A query that uses the defaultRESPECT NULLSbehavior (including when no null-treatment clause is present) will fail at run time with anUNSUPPORTED_OPERATIONerror as soon as aNULLis encountered. To avoid this potential error, useIGNORE NULLSto omitNULLvalues from the array.Arrays of arrays: An
ARRAY-typed argument would produce an array of arrays, which is not supported.ARRAY_AGG()over anARRAYcolumn raises anUNSUPPORTED_OPERATIONerror. This limitation is tracked by Issue #4167. To collect nested collections, you can wrap the inner array in a struct, as inARRAY_AGG((rid, tags)).DISTINCT: The
DISTINCTset quantifier is not supported yet. The parser acceptsARRAY_AGG(DISTINCT «expression» …)but raises anUNSUPPORTED_QUERYerror. This limitation is tracked by Issue #4499.Subqueries:
ARRAY_AGG()may be used in a correlatedFROM-clause subquery, as shown in ARRAY_AGG() in a correlated subquery above, but not in a scalar subquery in theSELECTprojection list. The latter, for exampleSELECT p.pid, (SELECT ARRAY_AGG(c.val IGNORE NULLS) FROM child c WHERE c.pid = p.pid) FROM parent p, raises aSYNTAX_ERROR. That is a general limitation of scalar subqueries in projections, not specific toARRAY_AGG().