{"record":{"id":"5d8d1a9ee227c7ce","repo":"prestodb/presto","slug":"invalid-function-argument-5d8d1a","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"map key cannot be null","messagePattern":"map key cannot be null","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-hive-function-namespace/src/main/java/com/facebook/presto/hive/functions/type/ObjectEncoders.java","lineNumber":258,"sourceCode":"            ObjectEncoder valueEncoder = createEncoder(valueType, inspector.getMapValueObjectInspector());\n            this.keyWriter = requireNonNull(createBlockObjectWriter(keyEncoder, keyType), \"keyWriter is null\");\n            this.valueWriter = requireNonNull(createBlockObjectWriter(valueEncoder, valueType), \"valueWriter is null\");\n        }\n\n        @Override\n        public Object encode(Object object)\n        {\n            if (object == null) {\n                return null;\n            }\n            Map<?, ?> rawMap = mapObjectInspector.getMap(object);\n\n            MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, rawMap.size());\n            BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();\n            for (Entry<?, ?> entry : rawMap.entrySet()) {\n                if (entry.getKey() == null) {\n                    mapBlockBuilder.closeEntry();\n                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"map key cannot be null\");\n                }\n                // TODO check indeterminate\n                keyWriter.write(blockBuilder, entry.getKey());\n                valueWriter.write(blockBuilder, entry.getValue());\n            }\n            try {\n                mapBlockBuilder.closeEntryStrict(mapType.getKeyBlockEquals(), mapType.getKeyBlockHashCode());\n            }\n            catch (DuplicateMapKeyException e) {\n                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);\n            }\n            return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);\n        }\n    }\n\n    public static class StructObjectEncoder\n            implements ObjectEncoder\n    {","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-hive-function-namespace/src/main/java/com/facebook/presto/hive/functions/type/ObjectEncoders.java#L240-L276","documentation":"ObjectEncoders' MapObjectEncoder.encode() converts a raw Java map into a Presto MapBlock. Map keys in Presto cannot be null, so when an entry has a null key the encoder closes the partially built entry and throws PrestoException with INVALID_FUNCTION_ARGUMENT. This is an intentional validation, since Presto's map type forbids null keys even though Java maps (e.g. HashMap) allow them.","triggerScenarios":"Encoding a raw java.util.Map that contains a null key into a Presto map type via ObjectEncoders' map encoder (e.g. binding a Hive map value or function argument with a null key).","commonSituations":"Hive data or UDF output containing null map keys being cast/converted to Presto map types; user code building HashMaps with null keys and passing them to Presto functions; deserialized records missing key values.","solutions":["Sanitize the map before encoding: remove or replace null keys (e.g. filter them out or map to a sentinel).","Change the upstream producer (Hive UDF/ETL) so map keys are never null.","If null keys must be represented, change the schema to a struct/array-of-rows representation instead of a map.","Catch the PrestoException and surface a clear message identifying the offending field."],"exampleFix":"// before\nMap<Object, Object> raw = ...; // may contain null keys\nencoder.encode(raw, mapType);\n// after\nMap<Object, Object> sanitized = raw.entrySet().stream()\n    .filter(e -> e.getKey() != null)\n    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));\nencoder.encode(sanitized, mapType);","handlingStrategy":"validation","validationCode":"boolean hasNullKey(Map<?, ?> m) {\n    return m.keySet().stream().anyMatch(Objects::isNull);\n}","typeGuard":"static <K, V> Map<K, V> withoutNullKeys(Map<K, V> raw) {\n    return raw.entrySet().stream()\n        .filter(e -> e.getKey() != null)\n        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new));\n}","tryCatchPattern":"try {\n    return encoder.encode(raw, mapType);\n} catch (PrestoException e) {\n    if (e.getErrorCode().getName().equals(\"INVALID_FUNCTION_ARGUMENT\")) {\n        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"map contains null key: \" + raw, e);\n    }\n    throw e;\n}","preventionTips":["Never build maps with null keys for Presto map types.","Filter null keys at the data source or UDF boundary.","Prefer array-of-row types when null keys are semantically required."],"tags":["hive","map-encoding","null-key","invalid-argument"],"backgroundTag":"map-key-cannot-be-null","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"}