prestodb/presto · error · PrestoException
INVALID_FUNCTION_ARGUMENT
INVALID_FUNCTION_ARGUMENT
Error message
map key cannot be null
What it means
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.
Source
Thrown at presto-hive-function-namespace/src/main/java/com/facebook/presto/hive/functions/type/ObjectEncoders.java:258
ObjectEncoder valueEncoder = createEncoder(valueType, inspector.getMapValueObjectInspector());
this.keyWriter = requireNonNull(createBlockObjectWriter(keyEncoder, keyType), "keyWriter is null");
this.valueWriter = requireNonNull(createBlockObjectWriter(valueEncoder, valueType), "valueWriter is null");
}
@Override
public Object encode(Object object)
{
if (object == null) {
return null;
}
Map<?, ?> rawMap = mapObjectInspector.getMap(object);
MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, rawMap.size());
BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
for (Entry<?, ?> entry : rawMap.entrySet()) {
if (entry.getKey() == null) {
mapBlockBuilder.closeEntry();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
// TODO check indeterminate
keyWriter.write(blockBuilder, entry.getKey());
valueWriter.write(blockBuilder, entry.getValue());
}
try {
mapBlockBuilder.closeEntryStrict(mapType.getKeyBlockEquals(), mapType.getKeyBlockHashCode());
}
catch (DuplicateMapKeyException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
}
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
}
public static class StructObjectEncoder
implements ObjectEncoder
{View on GitHub (pinned to 55bb57d202)
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.
Example fix
// before
Map<Object, Object> raw = ...; // may contain null keys
encoder.encode(raw, mapType);
// after
Map<Object, Object> sanitized = raw.entrySet().stream()
.filter(e -> e.getKey() != null)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
encoder.encode(sanitized, mapType); Defensive patterns
Strategy: validation
Validate before calling
boolean hasNullKey(Map<?, ?> m) {
return m.keySet().stream().anyMatch(Objects::isNull);
} Type guard
static <K, V> Map<K, V> withoutNullKeys(Map<K, V> raw) {
return raw.entrySet().stream()
.filter(e -> e.getKey() != null)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new));
} Try / catch
try {
return encoder.encode(raw, mapType);
} catch (PrestoException e) {
if (e.getErrorCode().getName().equals("INVALID_FUNCTION_ARGUMENT")) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map contains null key: " + raw, e);
}
throw e;
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- INVALID_PROCEDURE_ARGUMENT
- INVALID_FUNCTION_ARGUMENT
- not supported
- Map key is null at position: " + position
- Map keys must not be null
AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04).
Data as JSON: /api/errors/5d8d1a9ee227c7ce.
Report an issue: GitHub.