prestodb/presto · error · IllegalArgumentException

Initial capacity '%s' is negative

Error message

Initial capacity '%s' is negative

What it means

IntArrayList's package-private constructor validates the requested initial capacity and throws IllegalArgumentException when it is negative. A backing int[] cannot be allocated with a negative length, so the library fails fast before any allocation. This is a caller bug: the capacity value passed in is computed or configured incorrectly.

Source

Thrown at presto-common/src/main/java/com/facebook/presto/common/block/IntArrayList.java:33

import java.util.Arrays;

import static com.facebook.presto.common.block.BlockUtil.MAX_ARRAY_SIZE;
import static java.lang.String.format;

/**
 * A simplified version of fastutils IntArrayList for the purpose of positions copying.
 */
class IntArrayList
{
    private static final int DEFAULT_INITIAL_CAPACITY = 16;
    private int[] array;
    private int size;

    IntArrayList(int initialCapacity)
    {
        if (initialCapacity < 0) {
            throw new IllegalArgumentException(format("Initial capacity '%s' is negative", initialCapacity));
        }
        array = new int[initialCapacity];
    }

    IntArrayList()
    {
        this(DEFAULT_INITIAL_CAPACITY);
    }

    int[] elements()
    {
        return array;
    }

    private void grow(int newCapacity)
    {
        if (array.length == MAX_ARRAY_SIZE) {
            throw new IllegalStateException("Array reached maximum size");

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Fix the calculation that produces the capacity so it cannot be negative
  2. Clamp the capacity: int capacity = Math.max(0, estimatedSize)
  3. Use the no-arg IntArrayList() constructor if no meaningful estimate exists

Example fix

// before
IntArrayList list = new IntArrayList(expectedSize - otherSize);
// after
IntArrayList list = new IntArrayList(Math.max(0, expectedSize - otherSize));
Defensive patterns

Strategy: validation

Validate before calling

if (initialCapacity < 0) {
    throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity);
}
IntArrayList list = new IntArrayList(initialCapacity);

Try / catch

try {
    new IntArrayList(estimated);
} catch (IllegalArgumentException e) {
    // fall back to default capacity
}

Prevention

When it happens

Trigger: Calling the IntArrayList(int initialCapacity) constructor with any value < 0, e.g. a size estimate that subtracted a larger value from a smaller one.

Common situations: Computing expected capacity from statistics or row counts that turned out negative (integer underflow, uninitialized counter, wrong unit conversion).

Related errors


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