All Products
Search
Document Center

MaxCompute:COMBINATIONS

Last Updated:Dec 05, 2025

Returns an array of all combinations of n elements from an input array.

Syntax

combinations(array(T), n)

Parameters

  • array: The input array.

  • n: The number of elements in each combination.

Return value

Returns an array that contains all combinations of n elements from the input array.

  • If the input array does not contain duplicate elements, this function returns an array of n-element subsets. The output is deterministic, but the order of the subsets and the elements within them is not guaranteed.

  • By default, the value of n cannot be greater than 5, and the total number of generated combinations cannot exceed 100,000. You can change these limits by setting 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

  • Example 1: An array of the STRING type.

    SELECT combinations(array('foo', 'bar', 'boo'),2);

    The following result is returned:

    [['foo', 'bar'],['foo', 'boo'],['bar', 'boo']]
  • Example 2: An array of a numeric type.

    SELECT combinations(array(1,2,3,4,5),3);

    The following result is returned:

    [[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]]
  • Example 3: An array that contains duplicate elements.

    SELECT combinations(array(1,2,2),2);

    The following result is returned:

    [[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.