{"record":{"id":"466bcf624e25fb0c","repo":"prestodb/presto","slug":"map-keys-cannot-be-null","errorCode":null,"errorMessage":"map keys cannot be null","messagePattern":"map keys cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/MapBlockBuilder.java","lineNumber":605,"sourceCode":"                    throw new NotSupportedException(\"map key cannot be null or contain nulls\");\n                }\n\n                if (isDuplicateKey) {\n                    throw new DuplicateMapKeyException(keyBlock, keyOffset + i);\n                }\n\n                hash++;\n                if (hash == hashTableSize) {\n                    hash = 0;\n                }\n            }\n        }\n    }\n\n    private static int getHashPosition(Block keyBlock, int position, MethodHandle keyBlockHashCode, int hashTableSize)\n    {\n        if (keyBlock.isNull(position)) {\n            throw new IllegalArgumentException(\"map keys cannot be null\");\n        }\n\n        long hashCode;\n        try {\n            hashCode = (long) keyBlockHashCode.invokeExact(keyBlock, position);\n        }\n        catch (RuntimeException e) {\n            throw e;\n        }\n        catch (Throwable throwable) {\n            throw new RuntimeException(throwable);\n        }\n\n        return computePosition(hashCode, hashTableSize);\n    }\n\n    // This function reduces the 64 bit hashcode to [0, hashTableSize) uniformly. It first reduces the hashcode to 32 bit\n    // integer x then normalize it to x / 2^32 * hashSize to reduce the range of x from [0, 2^32) to [0, hashTableSize)","sourceCodeStart":587,"sourceCodeEnd":623,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/MapBlockBuilder.java#L587-L623","documentation":"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.","triggerScenarios":"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.","commonSituations":"Same family as null-key errors: corrupted or foreign-built map blocks with null keys, or producers that bypassed validation.","solutions":["Guarantee non-null keys before any map build/hashing","Validate/scrub source blocks so key positions are never null","Trace which producer wrote the null key and fix at that boundary"],"exampleFix":"// before\nint hash = getHashPosition(keyBlock, i, hashCode, tableSize);\n// after\ncheckState(!keyBlock.isNull(i), \"null key at %d\", i);\nint hash = getHashPosition(keyBlock, i, hashCode, tableSize);","handlingStrategy":"validation","validationCode":"if (keyBlock.isNull(position)) {\n    throw new IllegalArgumentException(\"null map key at \" + position);\n}\nint hash = hash(keyBlock, position, ...);","typeGuard":null,"tryCatchPattern":"try {\n    int pos = getHashPosition(keyBlock, i, hashCode, size);\n} catch (IllegalArgumentException e) {\n    // skip or repair the entry with a null key\n}","preventionTips":["Validate key blocks non-null before hashing","Fix producers that emit null keys","Fuzz-test deserialization with null-heavy inputs"],"tags":["illegal-argument","map-block","null-key","hashing"],"backgroundTag":"null-map-key","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}