prestodb/presto · error · PrestoException

INVALID_FUNCTION_ARGUMENT

INVALID_FUNCTION_ARGUMENT

Error message

Number of combinations too large for array of size %s and combination length %s

What it means

The combinations() function computes C(n, k) = n! / (k!(n-k)!) incrementally with multiplyExact/divide; if the intermediate product overflows int (ArithmeticException) the function throws INVALID_FUNCTION_ARGUMENT stating the result count is too large. Presto caps combinations because materializing billions of tuples is infeasible.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArrayCombinationsFunction.java:103

    }

    @VisibleForTesting
    static int combinationCount(int arrayLength, int combinationLength)
    {
        try {
            /*
             * Then combinationCount(n, k) = combinationCount(n-1, k-1) * n/k (https://en.wikipedia.org/wiki/Combination#Number_of_k-combinations)
             * The formula is recursive. Here, instead of starting with k=combinationCount, n=arrayLength and recursing,
             * we start with k=0 n=(arrayLength-combinationLength) and proceed "bottom up".
             */
            int combinations = 1;
            for (int i = 1; i <= combinationLength; i++) {
                combinations = multiplyExact(combinations, arrayLength - combinationLength + i) / i;
            }
            return combinations;
        }
        catch (ArithmeticException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Number of combinations too large for array of size %s and combination length %s", arrayLength, combinationLength));
        }
    }

    private static int[] firstCombination(int combinationLength)
    {
        int[] combination = new int[combinationLength];
        setAll(combination, i -> i);
        return combination;
    }

    private static boolean nextCombination(int[] combination, int arrayLength)
    {
        for (int i = 0; i < combination.length - 1; i++) {
            if (combination[i] + 1 < combination[i + 1]) {
                combination[i]++;
                resetCombination(combination, i);
                return true;
            }

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Reduce the input array size or choose a smaller/larger k so the combination count fits.
  2. Pre-filter the array to only the elements you actually need to combine.
  3. Implement combination generation in a UDF or client code using long/big arithmetic with streaming output.
  4. Use a cross join with a limit if an approximation or sample suffices.

Example fix

-- before
SELECT combinations(seq, 20) FROM t -- 45-element array: overflow
-- after
SELECT combinations(filter(seq, x -> x.keep), 5)
Defensive patterns

Strategy: validation

Validate before calling

-- keep C(n,k) small; check cardinality first
SELECT cardinality(arr) FROM t -- ensure count of combinations < 2^31

Try / catch

try { combinations(arr, k); } catch (PrestoException e) { /* reduce array or k */ }

Prevention

When it happens

Trigger: combinations(array, k) where C(arrayLength, k) exceeds Integer.MAX_VALUE (~2.1 billion) — e.g. combinations of a 40+ element array with k around half its length.

Common situations: Generating test combinations or pairwise products over moderately large arrays; users underestimate factorial growth and assume 30-40 elements are fine.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/b6de3f0606d9096c. Report an issue: GitHub.