{"record":{"id":"6d6aaecd03be82d0","repo":"apache/cassandra","slug":"cannot-assign-value-s-to-s-of-type-s","errorCode":null,"errorMessage":"Cannot assign value %s to %s of type %s","messagePattern":"Cannot assign value (.+?) to (.+?) of type (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/TypeCast.java","lineNumber":41,"sourceCode":"\npublic class TypeCast extends Term.Raw\n{\n    private final CQL3Type.Raw type;\n    private final Term.Raw term;\n\n    public TypeCast(CQL3Type.Raw type, Term.Raw term)\n    {\n        this.type = type;\n        this.term = term;\n    }\n\n    public Term prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException\n    {\n        if (!term.testAssignment(keyspace, castedSpecOf(keyspace, receiver)).isAssignable())\n            throw new InvalidRequestException(String.format(\"Cannot cast value %s to type %s\", term, type));\n\n        if (!testAssignment(keyspace, receiver).isAssignable())\n            throw new InvalidRequestException(String.format(\"Cannot assign value %s to %s of type %s\", this, receiver.name, receiver.type.asCQL3Type()));\n\n        return term.prepare(keyspace, receiver);\n    }\n\n    private ColumnSpecification castedSpecOf(String keyspace, ColumnSpecification receiver) throws InvalidRequestException\n    {\n        return new ColumnSpecification(receiver.ksName, receiver.cfName, new ColumnIdentifier(toString(), true), type.prepare(keyspace).getType());\n    }\n\n    public AssignmentTestable.TestResult testAssignment(String keyspace, ColumnSpecification receiver)\n    {\n        AbstractType<?> castedType = type.prepare(keyspace).getType();\n        if (receiver.type.equals(castedType))\n            return AssignmentTestable.TestResult.EXACT_MATCH;\n        else if (receiver.type.isValueCompatibleWith(castedType))\n            return AssignmentTestable.TestResult.WEAKLY_ASSIGNABLE;\n        else\n            return AssignmentTestable.TestResult.NOT_ASSIGNABLE;","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/TypeCast.java#L23-L59","documentation":"After verifying the inner cast, TypeCast.prepare also checks that the whole cast expression can be assigned to the receiver (the target column/selector expected by the statement). If not, it throws InvalidRequestException stating the value cannot be assigned to the named receiver of the receiver's CQL type.","triggerScenarios":"Executing CQL where a cast expression's result type does not match the receiver's expected type — e.g. INSERT INTO t (c) VALUES ((text)123) where c is an int column, or SELECT (int)x ... used where a different type is expected.","commonSituations":"Schema changed so the column type no longer matches existing queries' casts; mixing up the order of cast target and column type; driver-side type hints disagreeing with the schema; generated SQL with wrong cast placement.","solutions":["Make the cast target match the receiver's type (check DESCRIBE output for the column's CQL type)","Remove the redundant cast and pass a value of the column's type directly (or a bind marker with correct type)","Update prepared statements after schema type changes and re-prepare them","Chain casts/conversions so the final expression type equals the receiver type"],"exampleFix":"// before\nINSERT INTO t (int_col) VALUES ((text)123); // text assigned to int column\n// after\nINSERT INTO t (int_col) VALUES (123); // or ((int)123) to match int column","handlingStrategy":"validation","validationCode":"// ensure cast result type equals the receiver column type\nif (!castResultType.equals(receiverType)) throw new IllegalArgumentException(\"cast result \" + castResultType + \" not assignable to \" + receiverType);","typeGuard":null,"tryCatchPattern":"try { session.execute(cql); } catch (InvalidRequestException e) { if (e.getMessage().startsWith(\"Cannot assign value\")) alignCastWithReceiverTypeAndRetry(); else throw e; }","preventionTips":["Match cast expressions to the target column's CQL type","Re-prepare statements after schema type changes","Drop redundant casts and rely on typed bind markers"],"tags":["cql","type-cast","assignment","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}