{"record":{"id":"b6de3f0606d9096c","repo":"prestodb/presto","slug":"invalid-function-argument-b6de3f","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"Number of combinations too large for array of size %s and combination length %s","messagePattern":"Number of combinations too large for array of size (.+?) and combination length (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArrayCombinationsFunction.java","lineNumber":103,"sourceCode":"    }\n\n    @VisibleForTesting\n    static int combinationCount(int arrayLength, int combinationLength)\n    {\n        try {\n            /*\n             * Then combinationCount(n, k) = combinationCount(n-1, k-1) * n/k (https://en.wikipedia.org/wiki/Combination#Number_of_k-combinations)\n             * The formula is recursive. Here, instead of starting with k=combinationCount, n=arrayLength and recursing,\n             * we start with k=0 n=(arrayLength-combinationLength) and proceed \"bottom up\".\n             */\n            int combinations = 1;\n            for (int i = 1; i <= combinationLength; i++) {\n                combinations = multiplyExact(combinations, arrayLength - combinationLength + i) / i;\n            }\n            return combinations;\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format(\"Number of combinations too large for array of size %s and combination length %s\", arrayLength, combinationLength));\n        }\n    }\n\n    private static int[] firstCombination(int combinationLength)\n    {\n        int[] combination = new int[combinationLength];\n        setAll(combination, i -> i);\n        return combination;\n    }\n\n    private static boolean nextCombination(int[] combination, int arrayLength)\n    {\n        for (int i = 0; i < combination.length - 1; i++) {\n            if (combination[i] + 1 < combination[i + 1]) {\n                combination[i]++;\n                resetCombination(combination, i);\n                return true;\n            }","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArrayCombinationsFunction.java#L85-L121","documentation":"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.","triggerScenarios":"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.","commonSituations":"Generating test combinations or pairwise products over moderately large arrays; users underestimate factorial growth and assume 30-40 elements are fine.","solutions":["Reduce the input array size or choose a smaller/larger k so the combination count fits.","Pre-filter the array to only the elements you actually need to combine.","Implement combination generation in a UDF or client code using long/big arithmetic with streaming output.","Use a cross join with a limit if an approximation or sample suffices."],"exampleFix":"-- before\nSELECT combinations(seq, 20) FROM t -- 45-element array: overflow\n-- after\nSELECT combinations(filter(seq, x -> x.keep), 5)","handlingStrategy":"validation","validationCode":"-- keep C(n,k) small; check cardinality first\nSELECT cardinality(arr) FROM t -- ensure count of combinations < 2^31","typeGuard":null,"tryCatchPattern":"try { combinations(arr, k); } catch (PrestoException e) { /* reduce array or k */ }","preventionTips":["Estimate C(n,k) before calling","Filter arrays to needed elements first","Avoid combinations on arrays longer than ~30 elements"],"tags":["array","combinatorics","overflow"],"backgroundTag":"result-too-large","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}