apache/cassandra · error · InvalidTypeException

Invalid value for CQL type ${toDataType().getName()}

Error message

Invalid value for CQL type ${toDataType().getName()}

What it means

UDFDataType.decompose(Object) validates that the runtime class of the value being serialized is assignable to the Java class mapped to the CQL return type before delegating to the driver TypeCodec. A class mismatch means the UDF returned a value whose Java type does not match the declared CQL return type.

Source

Thrown at src/java/org/apache/cassandra/cql3/functions/UDFDataType.java:201

        return typeCodec.deserialize(buffer, protocolVersion);
    }

    /**
     * Serialized the specified oject.
     *
     * @param protocolVersion the protocol version
     * @param value the value to serialize
     * @return the serialized object
     */
    @SuppressWarnings("unchecked")
    public ByteBuffer decompose(ProtocolVersion protocolVersion, Object value)
    {
        if (value == null)
            return null;

        if (!toJavaClass().isAssignableFrom(value.getClass()))
            throw new InvalidTypeException("Invalid value for CQL type " + toDataType().getName());

        return ((TypeCodec<Object>) typeCodec).serialize(value, protocolVersion);
    }

    /**
     * Serialized the specified byte.
     *
     * @param protocolVersion the protocol version
     * @param value the value to serialize
     * @return the serialized byte
     */
    public ByteBuffer decompose(ProtocolVersion protocolVersion, byte value)
    {
        if (!(typeCodec instanceof PrimitiveByteCodec))
            throw new InvalidTypeException("Invalid value for CQL type " + toDataType().getName());

        return ((PrimitiveByteCodec) typeCodec).serializeNoBoxing(value, protocolVersion);
    }

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Align the UDF body's return type with the declared CQL return type in CREATE FUNCTION
  2. Add an explicit cast/conversion in the UDF before returning (e.g. return (long) value;)
  3. OR REPLACE the function with a return type matching what the body actually returns

Example fix

// before
public Object call(int x) { return x; } // declared RETURNS bigint
// after
public Long call(int x) { return (long) x; }
Defensive patterns

Strategy: type-guard

Validate before calling

if (value != null && !expectedJavaClass.isInstance(value)) throw new IllegalArgumentException("returned " + value.getClass() + " but declared " + toDataType().getName());

Type guard

boolean matchesDecl = value == null || expectedJavaClass.isInstance(value);

Try / catch

try { ByteBuffer buf = udfDataType.decompose(protocolVersion, result); } catch (InvalidTypeException e) { log.error("UDF return type mismatch: {}", e.getMessage()); throw e; }

Prevention

When it happens

Trigger: A Java UDF body returns an object (e.g. String, Date, java.math.BigInteger) whose class is not assignable to the Java class mapped from the declared CQL return type; executed during decompose when the UDF result is serialized for the protocol.

Common situations: UDF declares RETURNS bigint but returns a boxed Integer; returning wrong object type for collections/UDTs; refactoring a UDF body so its return type drifted from the declaration.

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/7869ce8783e21c75. Report an issue: GitHub.