{"record":{"id":"c3e465dc20aa7a81","repo":"prestodb/presto","slug":"array-subscript-out-of-bounds","errorCode":null,"errorMessage":"Array subscript out of bounds","messagePattern":"Array subscript out of bounds","errorType":"exception","errorClass":"InvalidFunctionArgumentException","httpStatus":null,"severity":"error","filePath":"presto-orc/src/main/java/com/facebook/presto/orc/reader/ListSelectiveStreamReader.java","lineNumber":627,"sourceCode":"\n    @Override\n    public void throwAnyError(int[] positions, int positionCount)\n    {\n        if (indexOutOfBounds == null) {\n            return;\n        }\n\n        int positionIndex = 0;\n        int nextPosition = positions[positionIndex];\n        for (int i = 0; i < outputPositionCount; i++) {\n            if (outputPositions[i] < nextPosition) {\n                continue;\n            }\n\n            assert outputPositions[i] == nextPosition;\n\n            if (indexOutOfBounds[i]) {\n                throw new InvalidFunctionArgumentException(\"Array subscript out of bounds\");\n            }\n\n            positionIndex++;\n            if (positionIndex >= positionCount) {\n                break;\n            }\n\n            nextPosition = positions[positionIndex];\n        }\n    }\n\n    private void closeBlockLease(BlockLease elementBlockLease)\n    {\n        elementBlockLease.close();\n        valuesInUse = false;\n    }\n\n    private void compactValues(int[] positions, int positionCount, boolean compactNulls)","sourceCodeStart":609,"sourceCodeEnd":645,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-orc/src/main/java/com/facebook/presto/orc/reader/ListSelectiveStreamReader.java#L609-L645","documentation":"ListSelectiveStreamReader throws InvalidFunctionArgumentException when it reads a position that was flagged as out of the array's bounds. This happens during selective list reading when a caller (e.g. the element_at function) requested an index beyond the number of elements in the list. The library deliberately surfaces invalid subscript usage as a function-argument error rather than silently returning null.","triggerScenarios":"A query such as element_at(list_col, n) where n exceeds the array length and the position is marked in indexOutOfBounds during output position processing in getBlock/evaluate.","commonSituations":"Queries computing array indexes from data (e.g. index derived from another column or cardinality arithmetic) that can exceed array length for some rows; off-by-one logic in UDFs that build output positions.","solutions":["Wrap index expressions in least(n, cardinality(arr)) or use a CASE to guard the subscript before calling element_at","Fix the caller that computes output positions to never emit positions flagged in indexOutOfBounds","If this is a UDF author issue, validate index bounds inside the function and throw a PrestoException with INVALID_FUNCTION_ARGUMENT and a meaningful message","Check data for arrays whose declared/expected size differs from actual element count"],"exampleFix":"// before\nSELECT element_at(arr, idx) FROM t;\n// after\nSELECT IF(idx <= cardinality(arr), element_at(arr, idx), NULL) FROM t;","handlingStrategy":"validation","validationCode":"// SQL-side guard before subscripting\nSELECT IF(idx BETWEEN 1 AND cardinality(arr), element_at(arr, idx), NULL) FROM t;","typeGuard":null,"tryCatchPattern":"// UDF author side\ntry { ... } catch (InvalidFunctionArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"array index out of bounds: \" + idx); }","preventionTips":["Always bound-check dynamic array indexes against cardinality() in SQL","Never emit output positions beyond the array length when writing selective readers","Add tests for empty arrays and index = length boundary cases","Use COALESCE/IF to define behavior for out-of-range indexes instead of relying on exceptions"],"tags":["orc","array-index","invalid-argument","presto"],"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"}