prestodb/presto · error · PrestoException

INVALID_CAST_ARGUMENT

INVALID_CAST_ARGUMENT

Error message

map key is null

What it means

Presto throws this INVALID_CAST_ARGUMENT error when a map-to-map cast produces a NULL key. During CAST(map<K,V> AS map<K2,V2>), each key is cast via a generated processor; when the key cast function is nullable, checkLongIsNotNull wraps it and rejects a null result because map keys in the target type are not nullable. The cast fails rather than silently dropping or storing a null key.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/scalar/MapToMapCast.java:166

        else if (javaType == Boolean.class) {
            return CHECK_BOOLEAN_IS_NOT_NULL;
        }
        else if (javaType == Slice.class) {
            return CHECK_SLICE_IS_NOT_NULL;
        }
        else if (javaType == Block.class) {
            return CHECK_BLOCK_IS_NOT_NULL;
        }
        else {
            throw new IllegalArgumentException("Unknown java type " + javaType);
        }
    }

    @UsedByGeneratedCode
    public static long checkLongIsNotNull(Long value)
    {
        if (value == null) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, "map key is null");
        }
        return value;
    }

    @UsedByGeneratedCode
    public static double checkDoubleIsNotNull(Double value)
    {
        if (value == null) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, "map key is null");
        }
        return value;
    }

    @UsedByGeneratedCode
    public static boolean checkBooleanIsNotNull(Boolean value)
    {
        if (value == null) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, "map key is null");

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Find the offending rows: run a query selecting entries where CAST(key AS new_key_type) IS NULL before casting.
  2. Use a filter or COALESCE to eliminate rows whose casted key would be NULL.
  3. Cast keys to a wider/nullable-safe type (e.g. VARCHAR) instead of a type that nulls them.
  4. Clean the source data so keys are always representable in the target type.

Example fix

// before
SELECT CAST(m AS MAP(INTEGER, VARCHAR)) FROM t;
// after
SELECT transform_values_and_keys(m, k -> CAST(CAST(k AS INTEGER) AS BIGINT), v -> v) FILTER keys_not_null;
-- or pre-clean:
SELECT map_filter(m, (k, v) -> CAST(k AS INTEGER) IS NOT NULL) ...
Defensive patterns

Strategy: validation

Validate before calling

-- run before casting: find rows where casted key is NULL
SELECT * FROM t WHERE EXISTS (
  SELECT 1 FROM UNNEST(m) AS u(k, v) WHERE TRY(CAST(k AS target_key_type)) IS NULL
);

Type guard

-- SQL-side guard: only keep maps whose keys all cast non-null
SELECT map_filter(m, (k, v) -> TRY(CAST(k AS INTEGER)) IS NOT NULL) AS safe_m FROM t;

Try / catch

-- tolerate bad rows instead of failing the query
SELECT TRY(CAST(m AS MAP(INTEGER, VARCHAR))) FROM t;

Prevention

When it happens

Trigger: Executing CAST(m AS MAP(bigger_key_type, v)) where casting an existing key to the target key type yields NULL (e.g. a bigint key 99999999999999999999 cast to integer-overflowing target, or a NULL-producing cast chain on a key).

Common situations: Widening/narrowing map key types in ETL SQL where source keys can be null-producing under the new type; changing key type to varchar on data whose keys cast to null in some encoding; queries against tables whose map columns contain keys that fail the new key type's cast.

Related errors


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