prestodb/presto · error · IllegalArgumentException

array1 and array2 cannot be null and should have same length

Error message

array1 and array2 cannot be null and should have same length

What it means

BlockUtil.arraySame compares two Object[] arrays by reference identity at each position. It requires both arrays to be non-null and of equal length; if either is null or the lengths differ, the inputs are invalid for the comparison and the library throws this IllegalArgumentException instead of returning a value. It is a precondition check on the caller.

Source

Thrown at presto-common/src/main/java/com/facebook/presto/common/block/BlockUtil.java:241

        for (int i = 0; i < positions.length; i++) {
            int offsetStart = offsets[offsetBase + i];
            int offsetEnd = offsets[offsetBase + i + 1];
            if (positions[i]) {
                used += (offsetEnd - offsetStart);
                Arrays.fill(elementPositions, offsetStart, offsetEnd, true);
            }
        }
        return used;
    }

    /**
     * Returns <tt>true</tt> if the two specified arrays contain the same object in every position.
     * Unlike the {@link Arrays#equals(Object[], Object[])} method, this method compares using reference equals.
     */
    static boolean arraySame(Object[] array1, Object[] array2)
    {
        if (array1 == null || array2 == null || array1.length != array2.length) {
            throw new IllegalArgumentException("array1 and array2 cannot be null and should have same length");
        }

        for (int i = 0; i < array1.length; i++) {
            if (array1[i] != array2[i]) {
                return false;
            }
        }
        return true;
    }

    public static boolean internalPositionInRange(int internalPosition, int offset, int positionCount)
    {
        boolean withinRange = internalPosition >= offset && internalPosition < positionCount + offset;
        assert withinRange : format("internalPosition %s is not within range [%s, %s)", internalPosition, offset, positionCount + offset);
        return withinRange;
    }

    static boolean[] appendNullToIsNullArray(@Nullable boolean[] isNull, int offsetBase, int positionCount)

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Validate inputs before calling: ensure both arrays are non-null and have the same length; return false or handle the mismatch yourself when lengths legitimately differ.
  2. If lengths can legitimately differ, use a length check first and treat different lengths as 'not the same' rather than an error.
  3. If you own the calling code, ensure the arrays come from the same logical source so sizes always match (e.g., dictionaries from blocks with identical position counts).

Example fix

// before
boolean same = BlockUtil.arraySame(dict1, dict2);
// after
boolean same = (dict1 != null && dict2 != null && dict1.length == dict2.length)
        && BlockUtil.arraySame(dict1, dict2);
Defensive patterns

Strategy: validation

Validate before calling

if (array1 == null || array2 == null || array1.length != array2.length) {
    // treat as not-same or handle explicitly; do not call arraySame
    return false;
}
boolean same = BlockUtil.arraySame(array1, array2);

Type guard

boolean isComparable(Object[] a, Object[] b) {
    return a != null && b != null && a.length == b.length;
}

Try / catch

try {
    same = BlockUtil.arraySame(a, b);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("array1 and array2 cannot be null")) {
        same = false; // or rethrow if this indicates a bug
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: Calling BlockUtil.arraySame(null, someArray), arraySame(someArray, null), or arraySame(a, b) where a.length != b.length. Typically triggered in block-cloning/dedup code paths that compare dictionaries or instance arrays of mismatched sizes.

Common situations: Passing a partially initialized or filtered-out array (null) into a comparison; comparing dictionary-encoded blocks whose dictionary arrays were rebuilt with different sizes; a bug in custom block implementations that supply arrays of differing lengths to the comparison helper.

Understand the failure class

Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.

Related errors


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