{"record":{"id":"f9ecb4c7b3881502","repo":"prestodb/presto","slug":"invalid-function-argument-f9ecb4","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"ip_prefix_collapse does not support null elements","messagePattern":"ip_prefix_collapse does not support null elements","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/IpPrefixFunctions.java","lineNumber":234,"sourceCode":"    public static Block collapseIpPrefixes(@SqlType(\"array(IPPREFIX)\") Block unsortedIpPrefixArray)\n    {\n        int inputPrefixCount = unsortedIpPrefixArray.getPositionCount();\n\n        // If we get an empty array or an array non-null single element, just return the original array.\n        if (inputPrefixCount == 0 || (inputPrefixCount == 1 && !unsortedIpPrefixArray.isNull(0))) {\n            return unsortedIpPrefixArray;\n        }\n\n        // Sort prefixes. lessThanFunction is never used. NULLs are placed at the end.\n        // Prefixes are ordered by first IP and then prefix length.\n        // Example:\n        //  Input:  10.0.0.0/8, 9.255.255.0/24, 10.0.0.0/7, 10.1.0.0/24, 10.10.0.0/16\n        //  Output: 9.255.255.0/24, 10.0.0.0/7, 10.0.0.0/8, 10.1.0.0/24, 10.10.0.0/16\n        Block ipPrefixArray = sort(null, IPPREFIX, unsortedIpPrefixArray);\n\n        // throw if anything is null\n        if (ipPrefixArray.isNull(0) || ipPrefixArray.isNull(inputPrefixCount - 1)) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"ip_prefix_collapse does not support null elements\");\n        }\n\n        // check the first and last prefixes in the array to make sure their IP versions match.\n        Slice firstIpPrefix = IPPREFIX.getSlice(ipPrefixArray, 0);\n        boolean v4 = isIpv4(firstIpPrefix);\n        Slice lastIpPrefix = IPPREFIX.getSlice(ipPrefixArray, inputPrefixCount - 1);\n        if (isIpv4(lastIpPrefix) != v4) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"All IPPREFIX elements must be the same IP version.\");\n        }\n\n        List<List<Slice>> outputIpPrefixes = new ArrayList<>();\n        int outputPrefixCount = 0;\n        int ipMaxBitLength = v4 ? 32 : 128;\n\n        List<BigInteger[]> mergedIpRanges = mergeIpRanges(ipPrefixArray);\n        for (BigInteger[] ipRange : mergedIpRanges) {\n            List<Slice> ipPrefixes = generateMinIpPrefixes(ipRange[0], ipRange[1], ipMaxBitLength);\n            outputIpPrefixes.add(ipPrefixes);","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/IpPrefixFunctions.java#L216-L252","documentation":"ip_prefix_collapse(array(IPPREFIX)) sorts its input and then checks whether the first or last element (after sorting) is null; since a null anywhere in the sorted array ends up at an end, any null element causes an INVALID_FUNCTION_ARGUMENT error. The function refuses to collapse arrays containing nulls.","triggerScenarios":"Calling ip_prefix_collapse(array) where the array contains at least one NULL IPPREFIX element.","commonSituations":"Aggregating prefix lists from partially populated columns; joins or array constructors that introduce NULLs; ETL output with missing prefixes.","solutions":["Filter out nulls before collapsing: ip_prefix_collapse(array_remove(prefixes, NULL)) or a FILTER clause.","Replace nulls with a default prefix if semantically acceptable.","Fix upstream data so the prefix array is fully populated."],"exampleFix":"// before\nSELECT ip_prefix_collapse(prefixes)\n// after\nSELECT ip_prefix_collapse(ARRAY_REMOVE(prefixes, NULL))","handlingStrategy":"validation","validationCode":"-- strip nulls before collapsing\nSELECT ip_prefix_collapse(ARRAY_REMOVE(prefixes, NULL)) FROM t;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Apply array_remove(x, NULL) or FILTER ... WHERE p IS NOT NULL before ip_prefix_collapse","Enforce NOT NULL on IPPREFIX columns used for collapsing","Check COALESCE on upstream joins that can introduce nulls"],"tags":["presto","sql","null","ip-prefix","invalid-function-argument"],"backgroundTag":"null-array-element","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"}