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

  1. Check the column type with DESCRIBE and make the literal match it (e.g. 42 for int, '42' semantics differ for text)
  2. Cast explicitly where allowed, e.g. use type-appropriate literals: uuids unquoted, strings quoted, blobs as 0x…
  3. 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

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


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