COMBINATIONS()
All functions > ARRAY > COMBINATIONS()
Returns all 2-element sub-groups of the input array as an array of 2-element arrays.
Signatures
Returns: All unordered pairs of elements (with replacement when duplicates appear in the input)
COMBINATIONS(array: ARRAY<T>) → ARRAY<ARRAY<T>> sql
| Parameter | Type | Required | Description |
|---|---|---|---|
array | ARRAY<T> | Yes | Input array (scalar or ROW elements) |
Notes
- Always selects pairs of two elements (fixed arity)
- Supports scalar arrays, arrays of rows (
ARRAY<ROW(...)>), and nested arrays (ARRAY<ARRAY<T>>) - When the input has no duplicate values, each pair is an unordered subset
- Duplicate input values produce duplicate pairs in the result
- Returns an empty array when fewer than two elements are available
Availability
| Status | Backends |
|---|---|
| Not available | bigquery, mysql |
See also
Examples
Nested arrays
FeatureQL
RETURN
-- String pairs without duplicates
f1 := COMBINATIONS(ARRAY('foo', 'bar', 'baz')),
-- Integer pairs
f2 := COMBINATIONS(ARRAY(1, 2, 3)),
-- Duplicate input values
f3 := COMBINATIONS(ARRAY(1, 2, 2)),
-- Empty input array
f4 := COMBINATIONS(ARRAY()::ARRAY<BIGINT>),
-- Pairs of rows from ZIP
f5 := COMBINATIONS(ZIP(ARRAY(1, 2, 3) AS x, ARRAY('a', 'b', 'c') AS y))
;Result
| f1 ARRAY | f2 ARRAY | f3 ARRAY | f4 ARRAY | f5 ARRAY |
|---|---|---|---|---|
| [[foo, bar], [foo, baz], [bar, baz]] | [[1, 2], [1, 3], [2, 3]] | [[1, 2], [1, 2], [2, 2]] | [] | [[{x: 1, y: a}, {x: 2, y: b}], [{x: 1, y: a}, {x: 3, y: c}], [{x: 2, y: b}, {x: 3, y: c}]] |