{"record":{"id":"a5bb2a616f7fd1cd","repo":"prestodb/presto","slug":"invalid-cast-argument-a5bb2a","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"map key is null","messagePattern":"map key is null","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/MapToMapCast.java","lineNumber":166,"sourceCode":"        else if (javaType == Boolean.class) {\n            return CHECK_BOOLEAN_IS_NOT_NULL;\n        }\n        else if (javaType == Slice.class) {\n            return CHECK_SLICE_IS_NOT_NULL;\n        }\n        else if (javaType == Block.class) {\n            return CHECK_BLOCK_IS_NOT_NULL;\n        }\n        else {\n            throw new IllegalArgumentException(\"Unknown java type \" + javaType);\n        }\n    }\n\n    @UsedByGeneratedCode\n    public static long checkLongIsNotNull(Long value)\n    {\n        if (value == null) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, \"map key is null\");\n        }\n        return value;\n    }\n\n    @UsedByGeneratedCode\n    public static double checkDoubleIsNotNull(Double value)\n    {\n        if (value == null) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, \"map key is null\");\n        }\n        return value;\n    }\n\n    @UsedByGeneratedCode\n    public static boolean checkBooleanIsNotNull(Boolean value)\n    {\n        if (value == null) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, \"map key is null\");","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/MapToMapCast.java#L148-L184","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Find the offending rows: run a query selecting entries where CAST(key AS new_key_type) IS NULL before casting.","Use a filter or COALESCE to eliminate rows whose casted key would be NULL.","Cast keys to a wider/nullable-safe type (e.g. VARCHAR) instead of a type that nulls them.","Clean the source data so keys are always representable in the target type."],"exampleFix":"// before\nSELECT CAST(m AS MAP(INTEGER, VARCHAR)) FROM t;\n// after\nSELECT transform_values_and_keys(m, k -> CAST(CAST(k AS INTEGER) AS BIGINT), v -> v) FILTER keys_not_null;\n-- or pre-clean:\nSELECT map_filter(m, (k, v) -> CAST(k AS INTEGER) IS NOT NULL) ...","handlingStrategy":"validation","validationCode":"-- run before casting: find rows where casted key is NULL\nSELECT * FROM t WHERE EXISTS (\n  SELECT 1 FROM UNNEST(m) AS u(k, v) WHERE TRY(CAST(k AS target_key_type)) IS NULL\n);","typeGuard":"-- SQL-side guard: only keep maps whose keys all cast non-null\nSELECT map_filter(m, (k, v) -> TRY(CAST(k AS INTEGER)) IS NOT NULL) AS safe_m FROM t;","tryCatchPattern":"-- tolerate bad rows instead of failing the query\nSELECT TRY(CAST(m AS MAP(INTEGER, VARCHAR))) FROM t;","preventionTips":["Probe with TRY(CAST(key AS new_type)) IS NULL before migrating a map column's key type.","Enforce non-null key constraints in ingestion pipelines.","Prefer key types with total casts (VARCHAR) when unsure."],"tags":["presto","sql","map-cast","null-key"],"backgroundTag":"null-map-key-cast","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}