{"record":{"id":"33abdfdfb96b28c8","repo":"prestodb/presto","slug":"invalid-function-argument-33abdf","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"Index out of bounds","messagePattern":"Index out of bounds","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/JsonExtract.java","lineNumber":269,"sourceCode":"\n            jsonParser.nextToken(); // Shift to first token of the value\n\n            return delegate.extract(jsonParser, properties);\n        }\n\n        public T processJsonArray(JsonParser jsonParser, SqlFunctionProperties properties)\n                throws IOException\n        {\n            int currentIndex = 0;\n            while (true) {\n                JsonToken token = jsonParser.nextToken();\n                if (token == null) {\n                    throw new JsonParseException(jsonParser, \"Unexpected end of array\");\n                }\n                if (token == END_ARRAY) {\n                    // Index out of bounds\n                    if (exceptionOnOutOfBounds) {\n                        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"Index out of bounds\");\n                    }\n                    return null;\n                }\n                if (currentIndex == index) {\n                    break;\n                }\n                currentIndex++;\n                jsonParser.skipChildren(); // Skip nested structure if currently at the start of one\n            }\n\n            return delegate.extract(jsonParser, properties);\n        }\n    }\n\n    public static class ScalarValueJsonExtractor\n            extends PrestoJsonExtractor<Slice>\n    {\n        @Override","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/JsonExtract.java#L251-L287","documentation":"Thrown by JsonExtract's processJsonArray when the JSON array has fewer elements than the requested index and exceptionOnOutOfBounds is enabled. Instead of returning null (the default lenient behavior), it raises INVALID_FUNCTION_ARGUMENT as soon as END_ARRAY is reached before the target index.","triggerScenarios":"extract (e.g. json_extract_scalar with a $[n] path) where n >= array length, with exception-on-out-of-bounds mode enabled.","commonSituations":"Querying arrays whose length varies per row; assuming a fixed schema where an array is sometimes empty; legacy compatibility flag turned on for out-of-bounds handling.","solutions":["Validate the array size with json_array_length before indexing, or use a CASE guard.","Use json_extract / json_extract_scalar without the out-of-bounds exception mode so missing indexes return NULL.","Use a subscript path with lenient semantics or json_query with on-empty/on-error handling."],"exampleFix":"-- before\nSELECT json_extract_scalar(tags, '$[2]') FROM t; -- fails when tags has < 3 elements\n-- after\nSELECT IF(json_array_length(tags) > 2, json_extract_scalar(tags, '$[2]'), NULL) FROM t;","handlingStrategy":"validation","validationCode":"SELECT IF(json_array_length(j, '$.items') > idx, json_extract_scalar(j, '$.items[' || CAST(idx AS VARCHAR) || ']'), NULL) FROM t;","typeGuard":null,"tryCatchPattern":"try { json_extract_scalar(j, '$[' || i || ']'); } catch (PrestoException e) { return null; }","preventionTips":["Check json_array_length before indexing variable-length arrays.","Prefer NULL-on-missing semantics instead of exception-on-out-of-bounds mode.","Never assume a fixed array size across rows."],"tags":["presto","json","index-out-of-bounds"],"backgroundTag":"json-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"}