{"record":{"id":"7ca37074d451f93e","repo":"prestodb/presto","slug":"invalid-function-argument-7ca370","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/ArraySubscriptOperator.java","lineNumber":154,"sourceCode":"        return elementType.getSlice(array, position);\n    }\n\n    @UsedByGeneratedCode\n    public static Object objectSubscript(Type elementType, Block array, long index)\n    {\n        checkIndex(array, index);\n        int position = toIntExact(index - 1);\n        if (array.isNull(position)) {\n            return null;\n        }\n\n        return elementType.getObject(array, position);\n    }\n\n    public static void checkArrayIndex(long index)\n    {\n        if (index == 0) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"SQL array indices start at 1\");\n        }\n        if (index < 0) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"Array subscript is negative\");\n        }\n    }\n\n    public static void checkIndex(Block array, long index)\n    {\n        checkArrayIndex(index);\n        if (index > array.getPositionCount()) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"Array subscript out of bounds\");\n        }\n    }\n}\n","sourceCodeStart":136,"sourceCodeEnd":169,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArraySubscriptOperator.java#L136-L169","documentation":"Array subscript error fired by checkArrayIndex when the index is 0. SQL arrays in Presto are 1-based, so index 0 is explicitly rejected (negative and too-large indices are handled separately) to distinguish 'wrong base' from 'out of range'. The offending input is the literal 0 passed as the array index.","triggerScenarios":"Evaluating arr[0] on any Presto array, often from code translated from 0-indexed languages or from dynamic indices computed as size - i patterns that reach 0.","commonSituations":"Porting Python/Java/JS array access to SQL; computing an index with arithmetic that yields 0 (e.g. element_at-style loops); off-by-one in slice/subscript expressions.","solutions":["Use 1-based indexing: arr[1] for the first element","Recompute the index expression to add 1 where translating from 0-based code","Use element_at(arr, 0) semantics check — note element_at also expects valid positions; guard the index","Filter rows where the computed index is < 1 before subscripting"],"exampleFix":"// before\nSELECT my_array[0];\n// after\nSELECT my_array[1];","handlingStrategy":"validation","validationCode":"SELECT CASE WHEN idx >= 1 AND idx <= cardinality(arr) THEN arr[idx] ELSE NULL END FROM t;","typeGuard":"CASE WHEN idx >= 1 THEN arr[idx] END","tryCatchPattern":"try(arr[idx]) -- returns NULL instead of throwing for index 0 or out of bounds","preventionTips":["Remember Presto arrays are 1-indexed; audit any arr[...] with computed indices","When translating from 0-based languages, add 1 to the index","Prefer element_at(arr, k) which returns NULL instead of erroring","Add bounds assertions in data tests for index-producing expressions"],"tags":["presto","sql","array-subscript","indexing"],"backgroundTag":"sql-array-index-out-of-range","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"}