prestodb/presto · error · IllegalArgumentException

map keys cannot be null

Error message

map keys cannot be null

What it means

getHashPosition computes the hash bucket for a key by invoking the type's hashCode MethodHandle; a null key position would crash the hash computation, so it throws IllegalArgumentException 'map keys cannot be null' first. This is an internal invariant check reached whenever any map key hashing happens on a null position.

Source

Thrown at presto-common/src/main/java/com/facebook/presto/common/block/MapBlockBuilder.java:605

                    throw new NotSupportedException("map key cannot be null or contain nulls");
                }

                if (isDuplicateKey) {
                    throw new DuplicateMapKeyException(keyBlock, keyOffset + i);
                }

                hash++;
                if (hash == hashTableSize) {
                    hash = 0;
                }
            }
        }
    }

    private static int getHashPosition(Block keyBlock, int position, MethodHandle keyBlockHashCode, int hashTableSize)
    {
        if (keyBlock.isNull(position)) {
            throw new IllegalArgumentException("map keys cannot be null");
        }

        long hashCode;
        try {
            hashCode = (long) keyBlockHashCode.invokeExact(keyBlock, position);
        }
        catch (RuntimeException e) {
            throw e;
        }
        catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }

        return computePosition(hashCode, hashTableSize);
    }

    // This function reduces the 64 bit hashcode to [0, hashTableSize) uniformly. It first reduces the hashcode to 32 bit
    // integer x then normalize it to x / 2^32 * hashSize to reduce the range of x from [0, 2^32) to [0, hashTableSize)

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Guarantee non-null keys before any map build/hashing
  2. Validate/scrub source blocks so key positions are never null
  3. Trace which producer wrote the null key and fix at that boundary

Example fix

// before
int hash = getHashPosition(keyBlock, i, hashCode, tableSize);
// after
checkState(!keyBlock.isNull(i), "null key at %d", i);
int hash = getHashPosition(keyBlock, i, hashCode, tableSize);
Defensive patterns

Strategy: validation

Validate before calling

if (keyBlock.isNull(position)) {
    throw new IllegalArgumentException("null map key at " + position);
}
int hash = hash(keyBlock, position, ...);

Try / catch

try {
    int pos = getHashPosition(keyBlock, i, hashCode, size);
} catch (IllegalArgumentException e) {
    // skip or repair the entry with a null key
}

Prevention

When it happens

Trigger: Hashing a key at position p where keyBlock.isNull(p) is true — reached through hash() during hash-table construction or lookups on a block containing a null key.

Common situations: Same family as null-key errors: corrupted or foreign-built map blocks with null keys, or producers that bypassed validation.

Related errors


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