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

  1. Sanitize the map before encoding: remove or replace null keys (e.g. filter them out or map to a sentinel).
  2. Change the upstream producer (Hive UDF/ETL) so map keys are never null.
  3. If null keys must be represented, change the schema to a struct/array-of-rows representation instead of a map.
  4. 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

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


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