{"record":{"id":"d6c62694a1059ac7","repo":"apache/cassandra","slug":"invalid-map-literal-for-s-key-s-is-not-of-type","errorCode":null,"errorMessage":"Invalid map literal for %s: key %s is not of type %s","messagePattern":"Invalid map literal for (.+?): key (.+?) is not of type (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Maps.java","lineNumber":219,"sourceCode":"                values.add(v);\n            }\n            MultiElements.DelayedValue value = new MultiElements.DelayedValue((MultiElementType<?>) receiver.type.unwrap(), values);\n            return allTerminal ? value.bind(QueryOptions.DEFAULT) : value;\n        }\n\n        private void validateAssignableTo(String keyspace, ColumnSpecification receiver) throws InvalidRequestException\n        {\n            AbstractType<?> type = receiver.type.unwrap();\n\n            if (!(type instanceof MapType))\n                throw new InvalidRequestException(String.format(\"Invalid map literal for %s of type %s\", receiver.name, receiver.type.asCQL3Type()));\n\n            ColumnSpecification keySpec = Maps.keySpecOf(receiver);\n            ColumnSpecification valueSpec = Maps.valueSpecOf(receiver);\n            for (Pair<Term.Raw, Term.Raw> entry : entries)\n            {\n                if (!entry.left.testAssignment(keyspace, keySpec).isAssignable())\n                    throw new InvalidRequestException(String.format(\"Invalid map literal for %s: key %s is not of type %s\", receiver.name, entry.left, keySpec.type.asCQL3Type()));\n                if (!entry.right.testAssignment(keyspace, valueSpec).isAssignable())\n                    throw new InvalidRequestException(String.format(\"Invalid map literal for %s: value %s is not of type %s\", receiver.name, entry.right, valueSpec.type.asCQL3Type()));\n            }\n        }\n\n        public AssignmentTestable.TestResult testAssignment(String keyspace, ColumnSpecification receiver)\n        {\n            return testMapAssignment(receiver, entries);\n        }\n\n        @Override\n        public AbstractType<?> getExactTypeIfKnown(String keyspace)\n        {\n            return getExactMapTypeIfKnown(entries, p -> p.getExactTypeIfKnown(keyspace));\n        }\n\n        @Override\n        public AbstractType<?> getCompatibleTypeIfKnown(String keyspace)","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Maps.java#L201-L237","documentation":"Fired by Maps literal preparation when a map literal key element's type is not assignable to the declared key type of the target map column. It is a CQL statement validation guard: the map literal itself parses, but the key expression (e.g. an int literal in a text-keyed map) fails AssignmentTestable.TestResult.NOT_ASSIGNABLE against MapType.getKeyType().","triggerScenarios":"Preparing e.g. `SET m = {'text-key': 1}` where the map's key type is int (map<int,text>), or mixing key types like timestamps vs strings in the literal.","commonSituations":"Quoting numbers so they become strings against int keys; schema changed key type; drivers sending JSON-ish literals with stringified keys.","solutions":["Make literal keys match the declared key type (e.g. `{1: 'a'}` for map<int,text>).","Verify the key type via system_schema and update the query.","Cast/convert values at the application layer before building the statement."],"exampleFix":"// before (map<int,text>)\n\"UPDATE t SET m = {'1':'a'} WHERE k=?\"\n// after\n\"UPDATE t SET m = {1:'a'} WHERE k=?\"","handlingStrategy":"validation","validationCode":"// keys must be literals assignable to the map key type, e.g. map<int,?>\nObject k = entry.getKey();\nif (!(k instanceof Integer)) throw new IllegalArgumentException(\"key must be int, got \" + k.getClass());","typeGuard":null,"tryCatchPattern":"try { session.prepare(sql); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"key\") && e.getMessage().contains(\"is not of type\")) { /* fix literal key types */ } else throw e; }","preventionTips":["Unquote numeric keys when the key type is numeric","Keep a schema reference handy when writing collection literals","Prefer bound whole-map parameters over hand-built literals"],"tags":["cql","map","type-mismatch","validation"],"backgroundTag":"type-mismatch","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}