prestodb/presto · error · IllegalArgumentException

values length is less than positionCount

Error message

values length is less than positionCount

What it means

IntArrayBlock checks that the backing int[] values array has enough room: values.length - arrayOffset must be >= positionCount. If the array cannot supply that many positions from arrayOffset onward, the constructor throws IllegalArgumentException to prevent out-of-bounds reads later.

Source

Thrown at presto-common/src/main/java/com/facebook/presto/common/block/IntArrayBlock.java:68

    public IntArrayBlock(int positionCount, Optional<boolean[]> valueIsNull, int[] values)
    {
        this(0, positionCount, valueIsNull.orElse(null), values);
    }

    IntArrayBlock(int arrayOffset, int positionCount, boolean[] valueIsNull, int[] values)
    {
        if (arrayOffset < 0) {
            throw new IllegalArgumentException("arrayOffset is negative");
        }
        this.arrayOffset = arrayOffset;
        if (positionCount < 0) {
            throw new IllegalArgumentException("positionCount is negative");
        }
        this.positionCount = positionCount;

        if (values.length - arrayOffset < positionCount) {
            throw new IllegalArgumentException("values length is less than positionCount");
        }
        this.values = values;

        if (valueIsNull != null && valueIsNull.length - arrayOffset < positionCount) {
            throw new IllegalArgumentException("isNull length is less than positionCount");
        }
        this.valueIsNull = valueIsNull;

        retainedSizeInBytes = INSTANCE_SIZE + sizeOf(valueIsNull) + sizeOf(values);
    }

    @Override
    public long getSizeInBytes()
    {
        return SIZE_IN_BYTES_PER_POSITION * (long) positionCount;
    }

    @Override

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Ensure values.length >= arrayOffset + positionCount before constructing (grow the array or reduce positionCount).
  2. Recompute positionCount as the actual number of ints available (values.length - arrayOffset) when the array is the source of truth.
  3. Verify the array wasn't reallocated/truncated after the count was computed.

Example fix

// before
IntArrayBlock block = new IntArrayBlock(offset, values.length + 1, nulls, values);
// after
IntArrayBlock block = new IntArrayBlock(offset, values.length - offset, nulls, values);
Defensive patterns

Strategy: validation

Validate before calling

if (values.length - arrayOffset < positionCount) {
    throw new IllegalArgumentException("values array too small for offset+count");
}
IntArrayBlock block = new IntArrayBlock(arrayOffset, positionCount, valueIsNull, values);

Type guard

boolean valuesFit(int[] values, int arrayOffset, int positionCount) {
    return values != null && values.length - arrayOffset >= positionCount;
}

Try / catch

try {
    block = new IntArrayBlock(offset, count, nulls, values);
} catch (IllegalArgumentException e) {
    block = new IntArrayBlock(offset, values.length - offset, nulls, values); // clamp count
}

Prevention

When it happens

Trigger: Constructing the block with a values array shorter than arrayOffset + positionCount — e.g. shrinking a buffer without adjusting positionCount, or reusing a constructor call's stale count after compacting the array.

Common situations: Buffer reuse pools where the array was reallocated smaller; off-by-one in capacity math when appending; callers passing positionCount that includes entries trimmed from the array.

Related errors


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