{"record":{"id":"c7929958b22b219a","repo":"prestodb/presto","slug":"invalid-function-argument-c79299","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/ArrayFindFirstWithOffsetFunction.java","lineNumber":240,"sourceCode":"            Boolean match = function.apply(element);\n            if (TRUE.equals(match)) {\n                if (element == null) {\n                    checkCondition(false, INVALID_FUNCTION_ARGUMENT, \"FIND_FIRST finds NULL as match, which is not supported.\");\n                }\n                return element;\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":222,"sourceCodeEnd":249,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArrayFindFirstWithOffsetFunction.java#L222-L249","documentation":"ArrayFindFirstWithOffsetFunction uses an identical checkedIndexToBlockPosition helper for its start-position argument. A start index of 0 violates 1-based SQL array indexing and throws INVALID_FUNCTION_ARGUMENT 'SQL array indices start at 1'; absolute values exceeding the array length return -1 (no match found) instead of erroring.","triggerScenarios":"Calling the array find-first-with-offset function with startPosition = 0 — often from a computed or bound parameter that evaluates to 0.","commonSituations":"Applications keeping 0-based offsets from client code (JS/Java arrays) and passing them unconverted; streaming/resume logic that starts at position 0 of a page of array data.","solutions":["Convert to 1-based before calling: passed_offset + 1 for 0-based inputs.","Clamp with greatest(offset, 1).","Short-circuit in application code when offset is 0 and treat it as 'start at beginning'.","Validate bound parameters in the driver/app layer before issuing the query."],"exampleFix":"// before\nfind_first(a, pred, resume_offset) -- resume_offset is 0-based\n// after\nfind_first(a, pred, resume_offset + 1)","handlingStrategy":"validation","validationCode":"SELECT CASE WHEN start_pos = 0 THEN NULL ELSE find_first(arr, pred, start_pos) END","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Convert 0-based resume offsets with +1 before calling","Clamp with greatest(start_pos, 1)","Standardize on 1-based positions across all array helpers"],"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"}