apache/cassandra · error · InvalidRequestException
Invalid %s constant (%s) for "%s" of type %s
Error message
Invalid %s constant (%s) for "%s" of type %s
What it means
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.
Source
Thrown at src/java/org/apache/cassandra/cql3/terms/Constants.java:297
{
return new Literal(Type.BOOLEAN, text);
}
public static Literal hex(String text)
{
return new Literal(Type.HEX, text);
}
public static Literal duration(String text)
{
return new Literal(Type.DURATION, text);
}
@Override
public Value prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
if (!testAssignment(keyspace, receiver).isAssignable())
throw new InvalidRequestException(String.format("Invalid %s constant (%s) for \"%s\" of type %s", type, text, receiver.name, receiver.type.asCQL3Type()));
return new Value(parsedValue(receiver.type));
}
private ByteBuffer parsedValue(AbstractType<?> validator) throws InvalidRequestException
{
if (validator instanceof ReversedType<?>)
validator = ((ReversedType<?>) validator).baseType;
try
{
if (type == Type.HEX)
// Note that validator could be BytesType, but it could also be a custom type, so
// we hardcode BytesType (rather than using 'validator') in the call below.
// Further note that BytesType doesn't want it's input prefixed by '0x', hence the substring.
return BytesType.instance.fromString(text.substring(2));
if (validator instanceof CounterColumnType)
return LongType.instance.fromString(text);View on GitHub (pinned to 88fd0f6a0e)
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
Example fix
// before INSERT INTO users (id, age) VALUES (uuid(), 'thirty'); // age is int // after INSERT INTO users (id, age) VALUES (uuid(), 30);
Defensive patterns
Strategy: validation
Validate before calling
switch (columnType) { case "int": if (!text.matches("-?\\d+")) throw ...; case "uuid": UUID.fromString(text); /* etc */ } Try / catch
try { session.execute(query); } catch (InvalidQueryException e) { if (e.getMessage().startsWith("Invalid ") && e.getMessage().contains("constant")) { /* fix literal type */ } else throw e; } Prevention
- Check column types with DESCRIBE before writing literals
- Use typed bind parameters instead of string literals
- Re-verify queries after any column type migration
When it happens
Trigger: `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).
Common situations: 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.
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
- Invalid map literal for %s: key %s is not of type %s
- Invalid map literal for %s: value %s is not of type %s
- Invalid tuple literal for %s: component %d is not of type %s
- REVOKE operation is not supported by AllowAllAuthorizer
- Invalid timestamp value: <tval>
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/f623f0d9cb1ea2b2.
Report an issue: GitHub.