{"record":{"id":"4d1641245fa06f9b","repo":"prestodb/presto","slug":"map-key-is-null-at-position-position","errorCode":null,"errorMessage":"Map key is null at position: \" + position","messagePattern":"Map key is null at position: \" \\+ position","errorType":"validation","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/AbstractSingleMapBlock.java","lineNumber":45,"sourceCode":"    abstract Block getRawKeyBlock();\n\n    abstract Block getRawValueBlock();\n\n    private int getAbsolutePosition(int position)\n    {\n        if (position < 0 || position >= getPositionCount()) {\n            throw new IllegalArgumentException(\"position is not valid: \" + position);\n        }\n        return position + getOffset();\n    }\n\n    @Override\n    public boolean isNull(int position)\n    {\n        position = getAbsolutePosition(position);\n        if (position % 2 == 0) {\n            if (getRawKeyBlock().isNull(position / 2)) {\n                throw new IllegalStateException(\"Map key is null at position: \" + position);\n            }\n            return false;\n        }\n        else {\n            return getRawValueBlock().isNull(position / 2);\n        }\n    }\n\n    @Override\n    public byte getByte(int position)\n    {\n        return getByteUnchecked(getAbsolutePosition(position));\n    }\n\n    @Override\n    public short getShort(int position)\n    {\n        return getShortUnchecked(getAbsolutePosition(position));","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/AbstractSingleMapBlock.java#L27-L63","documentation":"When reading position 0 (even position = the key slot) of a single-map block, AbstractSingleMapBlock.isNull checks the key block. A null key inside a map is illegal in Presto's map semantics, so the library throws IllegalStateException rather than reporting isNull=true for the key position.","triggerScenarios":"Reading a map block whose underlying key block contains a null at the key position — produced by code that built the map without enforcing non-null keys (e.g. a connector or function writing null keys), then accessed via isNull/element getters.","commonSituations":"UDFs or connectors constructing maps from user data containing null keys; missing validation between map construction (which normally rejects null keys) and a block assembled through the low-level single-map path.","solutions":["Enforce non-null keys when building maps: filter out or reject rows with null keys before constructing the map block.","If data is already corrupted, re-validate with map_null_key_reraise / map constructor checks or rebuild the block with null keys removed.","Trace the producer of the block and fix it to use the standard MapBuilder, which rejects null keys."],"exampleFix":"// before\nkeyBlockBuilder.appendNull(); // null map key\nvalueBlockBuilder.appendLong(v);\n// after\nif (keyIsNull) {\n    throw new InvalidCastException(\"map key cannot be null\");\n}\nkeyBlockBuilder.appendLong(key);\nvalueBlockBuilder.appendLong(v);","handlingStrategy":"validation","validationCode":"static Block ensureNoNullKeys(MapType mapType, Block rawKeyBlock, int keyCount) {\n    for (int i = 0; i < keyCount; i++) {\n        if (rawKeyBlock.isNull(i)) {\n            throw new IllegalStateException(\"map key is null at \" + i);\n        }\n    }\n    return rawKeyBlock;\n}","typeGuard":null,"tryCatchPattern":"try {\n    boolean isNull = singleMapBlock.isNull(position);\n} catch (IllegalStateException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Map key is null\")) {\n        throw new DataCorruptionException(\"map contains a null key\", e);\n    }\n    throw e;\n}","preventionTips":["Always build maps via MapBuilder, which rejects null keys.","Sanitize source data (filter or error on null keys) before map construction.","Validate blocks after deserialization from external connectors."],"tags":["presto","block","map-block","null-key","illegal-state"],"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"}