{"record":{"id":"24620514e1bf25ee","repo":"apache/cassandra","slug":"cannot-cast-value-s-to-type-s","errorCode":null,"errorMessage":"Cannot cast value %s to type %s","messagePattern":"Cannot cast value (.+?) to type (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/TypeCast.java","lineNumber":38,"sourceCode":"import org.apache.cassandra.cql3.terms.Term;\nimport org.apache.cassandra.db.marshal.AbstractType;\nimport org.apache.cassandra.exceptions.InvalidRequestException;\n\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))","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/TypeCast.java#L20-L56","documentation":"TypeCast.prepare validates that a term can be assigned to the casted type (e.g. (int)someValue). When the term's testAssignment against the casted column specification fails, Cassandra throws InvalidRequestException because the value cannot legally be cast to the requested CQL type.","triggerScenarios":"Executing CQL containing an explicit cast such as SELECT (int)col ... or WHERE k = (bigint)? where the source value's type is not assignable to the target cast type (e.g. casting a set to int, or text to int when not allowed).","commonSituations":"Migrating queries from other SQL dialects that allow more permissive casts; casting collection or blob columns to scalars; typos in the cast target type; schema changes that changed a column's type so old casts no longer line up.","solutions":["Remove or correct the cast so the term's natural type is used","Use an intermediate conversion that Cassandra supports (e.g. blobAsInt()/intAsBlob() family for blob conversions)","Check the actual column type with DESCRIBE and cast to a compatible type","Fix literals: e.g. write 3 as int directly instead of casting a text literal"],"exampleFix":"// before\nSELECT (int)user_id FROM t; // user_id is text, not castable\n// after\nSELECT userid_as_int FROM t; // or store as int / use blobAsInt(asBlob(user_id)) per allowed conversions","handlingStrategy":"validation","validationCode":"// only issue casts between known-compatible types\nif (!isCastable(sourceType, targetType)) throw new IllegalArgumentException(\"cannot cast \" + sourceType + \" to \" + targetType);","typeGuard":null,"tryCatchPattern":"try { session.execute(cqlWithCast); } catch (InvalidRequestException e) { if (e.getMessage().startsWith(\"Cannot cast value\")) removeCastAndRetry(); else throw e; }","preventionTips":["Check the column's actual type with DESCRIBE before casting","Only cast between documented-compatible CQL types","Use blob conversion functions (xAsBlob/blobAsX) for blob interop"],"tags":["cql","type-cast","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"}