{"record":{"id":"4c848dae7c8acacd","repo":"prestodb/presto","slug":"invalid-function-argument-4c848d","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/ArrayElementAtFunction.java","lineNumber":122,"sourceCode":"        int position = checkedIndexToBlockPosition(array, index);\n        if (position == -1) {\n            return null;\n        }\n        if (array.isNull(position)) {\n            return null;\n        }\n\n        return (Block) elementType.getObject(array, position);\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        if (index > 0) {\n            return toIntExact(index - 1);\n        }\n        else {\n            return toIntExact(arrayLength + index);\n        }\n    }\n}\n","sourceCodeStart":104,"sourceCodeEnd":135,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArrayElementAtFunction.java#L104-L135","documentation":"element_at / array indexing is 1-based per the SQL standard. checkedIndexToBlockPosition validates the user index before mapping it to a 0-based block position and rejects index 0 outright with INVALID_FUNCTION_ARGUMENT 'SQL array indices start at 1'. Indices beyond the array length return -1 (caller yields NULL) rather than throwing.","triggerScenarios":"element_at(arr, 0) — passing a literal or computed index of exactly 0.","commonSituations":"Porting 0-based indexing habits from Python/JS/Java; computing an index arithmetically (e.g. i-1 in a loop) that can hit 0; off-by-one in generated SQL.","solutions":["Use 1-based indices: element_at(arr, 1) for the first element.","Clamp or shift computed indices: greatest(index, 1).","If 0-based semantics are needed, add 1 before calling: element_at(arr, zero_based + 1).","Guard NULL/zero inputs with COALESCE or CASE before indexing."],"exampleFix":"// before\nelement_at(a, idx) -- idx can be 0\n// after\nelement_at(a, greatest(idx, 1)) -- or CASE WHEN idx = 0 THEN NULL ...","handlingStrategy":"validation","validationCode":"-- reject zero before indexing\nSELECT CASE WHEN idx = 0 THEN NULL ELSE element_at(arr, idx) END","typeGuard":null,"tryCatchPattern":"try { element_at(arr, idx); } catch (PrestoException e) { /* INVALID_FUNCTION_ARGUMENT: idx was 0 */ }","preventionTips":["Remember SQL arrays are 1-based","Clamp computed indices with greatest(idx, 1)","Add +1 when converting 0-based offsets"],"tags":["array","indexing","sql-standard","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"}