{"record":{"id":"f623f0d9cb1ea2b2","repo":"apache/cassandra","slug":"invalid-s-constant-s-for-s-of-type-s","errorCode":null,"errorMessage":"Invalid %s constant (%s) for \"%s\" of type %s","messagePattern":"Invalid (.+?) constant \\((.+?)\\) for \"(.+?)\" of type (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Constants.java","lineNumber":297,"sourceCode":"        {\n            return new Literal(Type.BOOLEAN, text);\n        }\n\n        public static Literal hex(String text)\n        {\n            return new Literal(Type.HEX, text);\n        }\n\n        public static Literal duration(String text)\n        {\n            return new Literal(Type.DURATION, text);\n        }\n\n        @Override\n        public Value prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException\n        {\n            if (!testAssignment(keyspace, receiver).isAssignable())\n                throw new InvalidRequestException(String.format(\"Invalid %s constant (%s) for \\\"%s\\\" of type %s\", type, text, receiver.name, receiver.type.asCQL3Type()));\n\n            return new Value(parsedValue(receiver.type));\n        }\n\n        private ByteBuffer parsedValue(AbstractType<?> validator) throws InvalidRequestException\n        {\n            if (validator instanceof ReversedType<?>)\n                validator = ((ReversedType<?>) validator).baseType;\n            try\n            {\n                if (type == Type.HEX)\n                    // Note that validator could be BytesType, but it could also be a custom type, so\n                    // we hardcode BytesType (rather than using 'validator') in the call below.\n                    // Further note that BytesType doesn't want it's input prefixed by '0x', hence the substring.\n                    return BytesType.instance.fromString(text.substring(2));\n\n                if (validator instanceof CounterColumnType)\n                    return LongType.instance.fromString(text);","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Constants.java#L279-L315","documentation":"Thrown when a literal constant cannot be assigned to the receiver column's type during prepare. The text parsed fine syntactically, but its declared/expected type does not match the target column, e.g. a string constant for an int column that no conversion can accept.","triggerScenarios":"`INSERT INTO t (k, v) VALUES (0, 'abc')` where v is an int and the literal fails testAssignment for that type; using a literal of one CQL type where an incompatible type is expected (e.g. a text constant for a uuid column).","commonSituations":"Copy-pasted CQL from tables with different schemas; schema migrations changed a column type while queries still use old literals; quotes forgotten around strings or wrongly added around numbers.","solutions":["Check the column type with DESCRIBE and make the literal match it (e.g. 42 for int, '42' semantics differ for text)","Cast explicitly where allowed, e.g. use type-appropriate literals: uuids unquoted, strings quoted, blobs as 0x…","Update queries after schema type changes"],"exampleFix":"// before\nINSERT INTO users (id, age) VALUES (uuid(), 'thirty'); // age is int\n// after\nINSERT INTO users (id, age) VALUES (uuid(), 30);","handlingStrategy":"validation","validationCode":"switch (columnType) { case \"int\": if (!text.matches(\"-?\\\\d+\")) throw ...; case \"uuid\": UUID.fromString(text); /* etc */ }","typeGuard":null,"tryCatchPattern":"try { session.execute(query); } catch (InvalidQueryException e) { if (e.getMessage().startsWith(\"Invalid \") && e.getMessage().contains(\"constant\")) { /* fix literal type */ } else throw e; }","preventionTips":["Check column types with DESCRIBE before writing literals","Use typed bind parameters instead of string literals","Re-verify queries after any column type migration"],"tags":["cql","type-mismatch","literal","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"}