{"record":{"id":"f106bf40825a95eb","repo":"prestodb/presto","slug":"invalid-function-argument-f106bf","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"SQL array indices start at 1","messagePattern":"SQL array indices start at 1","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArrayFindFirstIndexWithOffsetFunction.java","lineNumber":230,"sourceCode":"            if (!arrayBlock.isNull(i)) {\n                element = elementType.getBoolean(arrayBlock, i);\n            }\n            Boolean match = function.apply(element);\n            if (TRUE.equals(match)) {\n                return Long.valueOf(i + 1);\n            }\n        }\n        return null;\n    }\n\n    /**\n     * @return PrestoException if the index is 0, -1 if the index is out of range (to tell the calling function to return null), and the element position otherwise.\n     */\n    private static int checkedIndexToBlockPosition(Block block, long index)\n    {\n        int arrayLength = block.getPositionCount();\n        if (index == 0) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"SQL array indices start at 1\");\n        }\n        if (Math.abs(index) > arrayLength) {\n            return -1; // -1 indicates that the element is out of range and \"ELEMENT_AT\" should return null\n        }\n        index = index > 0 ? index - 1 : arrayLength + index;\n        return toIntExact(index);\n    }\n}\n","sourceCodeStart":212,"sourceCodeEnd":239,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArrayFindFirstIndexWithOffsetFunction.java#L212-L239","documentation":"ArrayFindFirstIndexWithOffsetFunction shares the same helper: checkedIndexToBlockPosition converts a user-supplied start position (1-based, negatives count from the end) to a 0-based block offset. Index 0 is invalid under SQL semantics and throws INVALID_FUNCTION_ARGUMENT 'SQL array indices start at 1'; out-of-range magnitudes return -1 so startPosition yields no match.","triggerScenarios":"Invoking the offset variant of array find-first (e.g. array_find_first_index_with_offset style function) with start position 0.","commonSituations":"Computing a start offset from another query result that is 0-based or NULL-coalesced to 0; loops that decrement the offset to 0; mixing element_at (1-based) conventions across functions.","solutions":["Pass a start position >= 1 (or negative to search from the end).","Clamp: greatest(start_offset, 1) before calling.","Handle the zero case explicitly with CASE to return NULL/no-match.","Audit upstream expressions producing the offset for 0-based origins."],"exampleFix":"// before\nfind_first_with_offset(a, pred, off) -- off may be 0\n// after\nfind_first_with_offset(a, pred, CASE WHEN off = 0 THEN 1 ELSE off END)","handlingStrategy":"validation","validationCode":"SELECT CASE WHEN off = 0 THEN NULL ELSE find_first_index_with_offset(arr, pred, off) END","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate start offsets >= 1 in application code","Audit upstream computations for 0-based origins","Handle NULL offsets before the function call"],"tags":["array","indexing","offset","invalid-argument"],"backgroundTag":"array-index-out-of-bounds","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"}