prestodb/presto · error · PrestoException

INVALID_FUNCTION_ARGUMENT

INVALID_FUNCTION_ARGUMENT

Error message

Index out of bounds

What it means

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.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/scalar/JsonExtract.java:269

            jsonParser.nextToken(); // Shift to first token of the value

            return delegate.extract(jsonParser, properties);
        }

        public T processJsonArray(JsonParser jsonParser, SqlFunctionProperties properties)
                throws IOException
        {
            int currentIndex = 0;
            while (true) {
                JsonToken token = jsonParser.nextToken();
                if (token == null) {
                    throw new JsonParseException(jsonParser, "Unexpected end of array");
                }
                if (token == END_ARRAY) {
                    // Index out of bounds
                    if (exceptionOnOutOfBounds) {
                        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Index out of bounds");
                    }
                    return null;
                }
                if (currentIndex == index) {
                    break;
                }
                currentIndex++;
                jsonParser.skipChildren(); // Skip nested structure if currently at the start of one
            }

            return delegate.extract(jsonParser, properties);
        }
    }

    public static class ScalarValueJsonExtractor
            extends PrestoJsonExtractor<Slice>
    {
        @Override

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Validate the array size with json_array_length before indexing, or use a CASE guard.
  2. Use json_extract / json_extract_scalar without the out-of-bounds exception mode so missing indexes return NULL.
  3. Use a subscript path with lenient semantics or json_query with on-empty/on-error handling.

Example fix

-- before
SELECT json_extract_scalar(tags, '$[2]') FROM t; -- fails when tags has < 3 elements
-- after
SELECT IF(json_array_length(tags) > 2, json_extract_scalar(tags, '$[2]'), NULL) FROM t;
Defensive patterns

Strategy: validation

Validate before calling

SELECT IF(json_array_length(j, '$.items') > idx, json_extract_scalar(j, '$.items[' || CAST(idx AS VARCHAR) || ']'), NULL) FROM t;

Try / catch

try { json_extract_scalar(j, '$[' || i || ']'); } catch (PrestoException e) { return null; }

Prevention

When it happens

Trigger: extract (e.g. json_extract_scalar with a $[n] path) where n >= array length, with exception-on-out-of-bounds mode enabled.

Common situations: 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.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/33abdfdfb96b28c8. Report an issue: GitHub.