apache/cassandra · error · InvalidRequestException

Cannot assign value to of type

Error message

Cannot assign value %s to %s of type %s

What it means

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.

Solutions

  1. Make the cast target match the receiver's type (check DESCRIBE output for the column's CQL type)
  2. Remove the redundant cast and pass a value of the column's type directly (or a bind marker with correct type)
  3. Update prepared statements after schema type changes and re-prepare them
  4. Chain casts/conversions so the final expression type equals the receiver type

Example fix

// before
INSERT INTO t (int_col) VALUES ((text)123); // text assigned to int column
// after
INSERT INTO t (int_col) VALUES (123); // or ((int)123) to match int column
Defensive patterns

Strategy: validation

Validate before calling

// ensure cast result type equals the receiver column type
if (!castResultType.equals(receiverType)) throw new IllegalArgumentException("cast result " + castResultType + " not assignable to " + receiverType);

Try / catch

try { session.execute(cql); } catch (InvalidRequestException e) { if (e.getMessage().startsWith("Cannot assign value")) alignCastWithReceiverTypeAndRetry(); else throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/6d6aaecd03be82d0. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/TypeCast.java:41

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))
            return AssignmentTestable.TestResult.WEAKLY_ASSIGNABLE;
        else
            return AssignmentTestable.TestResult.NOT_ASSIGNABLE;

View on GitHub (pinned to 88fd0f6a0e)