Returns an array of all combinations of n elements from an input array.
Syntax
combinations(array(T), n) -> array(array(T))
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
n |
The number of elements in each combination. |
Return value
Returns an array of arrays, where each inner array is an n-element combination drawn from the input. If the input array does not contain duplicate elements, the output is deterministic, but the order of combinations and the elements within them is not guaranteed.
By default, n cannot exceed 5 and the total number of generated combinations cannot exceed 100,000. Adjust these limits with the odps.sql.max.combination.length and odps.sql.max.combinations parameters. If n is greater than the number of elements in the input array, the function returns an empty array.
Examples
STRING array
SELECT combinations(array('foo', 'bar', 'boo'), 2);
-- [['foo', 'bar'], ['foo', 'boo'], ['bar', 'boo']]
Numeric array
SELECT combinations(array(1, 2, 3, 4, 5), 3);
-- [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4], [1, 2, 5], [1, 3, 5], [2, 3, 5], [1, 4, 5], [2, 4, 5], [3, 4, 5]]
Array with duplicate elements
SELECT combinations(array(1, 2, 2), 2);
-- [[1, 2], [1, 2], [2, 2]]
Related functions
combinations is a complex type function. For more information about functions that process complex data types such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.