apache/cassandra · error · InvalidRequestException
Cannot cast value to type
Error message
Cannot cast value %s to type %s
What it means
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.
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
Example fix
// before SELECT (int)user_id FROM t; // user_id is text, not castable // after SELECT userid_as_int FROM t; // or store as int / use blobAsInt(asBlob(user_id)) per allowed conversions
Defensive patterns
Strategy: validation
Validate before calling
// only issue casts between known-compatible types
if (!isCastable(sourceType, targetType)) throw new IllegalArgumentException("cannot cast " + sourceType + " to " + targetType); Try / catch
try { session.execute(cqlWithCast); } catch (InvalidRequestException e) { if (e.getMessage().startsWith("Cannot cast value")) removeCastAndRetry(); else throw e; } Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cannot assign value to of type
- Cannot assign value to of type
- Cannot replace aggregate
- Cannot replace function
- Constraint ' ' can be used only for columns of type but it…
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/24620514e1bf25ee.
Report an issue: GitHub.
Appendix: source
Thrown at src/java/org/apache/cassandra/cql3/TypeCast.java:38
import org.apache.cassandra.cql3.terms.Term;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.exceptions.InvalidRequestException;
public class TypeCast extends Term.Raw
{
private final CQL3Type.Raw type;
private final Term.Raw term;
public TypeCast(CQL3Type.Raw type, Term.Raw term)
{
this.type = type;
this.term = term;
}
public Term prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
if (!term.testAssignment(keyspace, castedSpecOf(keyspace, receiver)).isAssignable())
throw new InvalidRequestException(String.format("Cannot cast value %s to type %s", term, type));
if (!testAssignment(keyspace, receiver).isAssignable())
throw new InvalidRequestException(String.format("Cannot assign value %s to %s of type %s", this, receiver.name, receiver.type.asCQL3Type()));
return term.prepare(keyspace, receiver);
}
private ColumnSpecification castedSpecOf(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
return new ColumnSpecification(receiver.ksName, receiver.cfName, new ColumnIdentifier(toString(), true), type.prepare(keyspace).getType());
}
public AssignmentTestable.TestResult testAssignment(String keyspace, ColumnSpecification receiver)
{
AbstractType<?> castedType = type.prepare(keyspace).getType();
if (receiver.type.equals(castedType))
return AssignmentTestable.TestResult.EXACT_MATCH;
else if (receiver.type.isValueCompatibleWith(castedType))View on GitHub (pinned to 88fd0f6a0e)