prestodb/presto · error · IllegalArgumentException

Length longer than value length

Error message

Length longer than value length

What it means

AbstractVariableWidthBlock.compareTo requires that the requested length does not exceed the actual length of the slice at the given position. If getSliceLength(position) < length, the comparison would read past the value, so the library throws IllegalArgumentException "Length longer than value length".

Source

Thrown at presto-common/src/main/java/com/facebook/presto/common/block/AbstractVariableWidthBlock.java:114

    {
        checkReadablePosition(position);
        return getRawSlice(position).equals(getPositionOffset(position) + offset, length, otherSlice, otherOffset, length);
    }

    @Override
    public long hash(int position, int offset, int length)
    {
        checkReadablePosition(position);
        return XxHash64.hash(getRawSlice(position), getPositionOffset(position) + offset, length);
    }

    @Override
    public int compareTo(int position, int offset, int length, Block otherBlock, int otherPosition, int otherOffset, int otherLength)
    {
        checkReadablePosition(position);
        Slice rawSlice = getRawSlice(position);
        if (getSliceLength(position) < length) {
            throw new IllegalArgumentException("Length longer than value length");
        }
        return -otherBlock.bytesCompare(otherPosition, otherOffset, otherLength, rawSlice, getPositionOffset(position) + offset, length);
    }

    @Override
    public int bytesCompare(int position, int offset, int length, Slice otherSlice, int otherOffset, int otherLength)
    {
        checkReadablePosition(position);
        return getRawSlice(position).compareTo(getPositionOffset(position) + offset, length, otherSlice, otherOffset, otherLength);
    }

    @Override
    public void writeBytesTo(int position, int offset, int length, BlockBuilder blockBuilder)
    {
        checkReadablePosition(position);
        blockBuilder.writeBytes(getRawSlice(position), getPositionOffset(position) + offset, length);
    }

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Clamp length: use Math.min(length, getSliceLength(position) - offset) before calling compareTo, or compute length via block.getSliceLength(position).
  2. Fix the caller to derive lengths from the same block/position being compared.
  3. Check for CHAR/FIXED-width assumptions in code operating on VARCHAR blocks.
  4. If slices come from deserialization, validate slice lengths during the read path to catch corruption earlier.

Example fix

// before
int len = 10; // assumed fixed width
leftBlock.compareTo(pos, 0, len, rightBlock, otherPos, 0, len);
// after
int len = Math.min(10, leftBlock.getSliceLength(pos) - 0);
leftBlock.compareTo(pos, 0, len, rightBlock, otherPos, 0, len);
Defensive patterns

Strategy: validation

Validate before calling

static int safeCompareLength(Block varWidthBlock, int position, int requestedLength) {
    int valueLength = varWidthBlock.getSliceLength(position);
    checkArgument(requestedLength <= valueLength,
        "requested length %s exceeds value length %s", requestedLength, valueLength);
    return requestedLength;
}

Try / catch

try {
    int cmp = varBlock.compareTo(position, 0, length, other, otherPosition, 0, length);
} catch (IllegalArgumentException e) {
    if (e.getMessage() != null && e.getMessage().contains("Length longer than value length")) {
        throw new DataCorruptionException("comparison length exceeds slice length", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling compareTo(position, offset, length, ...) with a length greater than the variable-width value's stored length — e.g. a fixed-length assumption applied to a VARCHAR block, or a length computed from a different block.

Common situations: Custom operator code comparing slices using lengths from another block or from fixed-width assumptions; corrupted deserialized slices whose reported lengths shrank; mismatches when comparing CHAR vs VARCHAR encodings.

Related errors


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