prestodb/presto · error · PrestoException

INVALID_FUNCTION_ARGUMENT

INVALID_FUNCTION_ARGUMENT

Error message

SQL array indices start at 1

What it means

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.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/scalar/ArraySubscriptOperator.java:154

        return elementType.getSlice(array, position);
    }

    @UsedByGeneratedCode
    public static Object objectSubscript(Type elementType, Block array, long index)
    {
        checkIndex(array, index);
        int position = toIntExact(index - 1);
        if (array.isNull(position)) {
            return null;
        }

        return elementType.getObject(array, position);
    }

    public static void checkArrayIndex(long index)
    {
        if (index == 0) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "SQL array indices start at 1");
        }
        if (index < 0) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Array subscript is negative");
        }
    }

    public static void checkIndex(Block array, long index)
    {
        checkArrayIndex(index);
        if (index > array.getPositionCount()) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Array subscript out of bounds");
        }
    }
}

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Use 1-based indexing: arr[1] for the first element
  2. Recompute the index expression to add 1 where translating from 0-based code
  3. Use element_at(arr, 0) semantics check — note element_at also expects valid positions; guard the index
  4. Filter rows where the computed index is < 1 before subscripting

Example fix

// before
SELECT my_array[0];
// after
SELECT my_array[1];
Defensive patterns

Strategy: validation

Validate before calling

SELECT CASE WHEN idx >= 1 AND idx <= cardinality(arr) THEN arr[idx] ELSE NULL END FROM t;

Type guard

CASE WHEN idx >= 1 THEN arr[idx] END

Try / catch

try(arr[idx]) -- returns NULL instead of throwing for index 0 or out of bounds

Prevention

When it happens

Trigger: 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.

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

Related errors


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