{"record":{"id":"7869ce8783e21c75","repo":"apache/cassandra","slug":"invalid-value-for-cql-type-todatatype-getname","errorCode":null,"errorMessage":"Invalid value for CQL type ${toDataType().getName()}","messagePattern":"Invalid value for CQL type (.+?)","errorType":"exception","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/UDFDataType.java","lineNumber":201,"sourceCode":"\n        return typeCodec.deserialize(buffer, protocolVersion);\n    }\n\n    /**\n     * Serialized the specified oject.\n     *\n     * @param protocolVersion the protocol version\n     * @param value the value to serialize\n     * @return the serialized object\n     */\n    @SuppressWarnings(\"unchecked\")\n    public ByteBuffer decompose(ProtocolVersion protocolVersion, Object value)\n    {\n        if (value == null)\n            return null;\n\n        if (!toJavaClass().isAssignableFrom(value.getClass()))\n            throw new InvalidTypeException(\"Invalid value for CQL type \" + toDataType().getName());\n\n        return ((TypeCodec<Object>) typeCodec).serialize(value, protocolVersion);\n    }\n\n    /**\n     * Serialized the specified byte.\n     *\n     * @param protocolVersion the protocol version\n     * @param value the value to serialize\n     * @return the serialized byte\n     */\n    public ByteBuffer decompose(ProtocolVersion protocolVersion, byte value)\n    {\n        if (!(typeCodec instanceof PrimitiveByteCodec))\n            throw new InvalidTypeException(\"Invalid value for CQL type \" + toDataType().getName());\n\n        return ((PrimitiveByteCodec) typeCodec).serializeNoBoxing(value, protocolVersion);\n    }","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/UDFDataType.java#L183-L219","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Align the UDF body's return type with the declared CQL return type in CREATE FUNCTION","Add an explicit cast/conversion in the UDF before returning (e.g. return (long) value;)","OR REPLACE the function with a return type matching what the body actually returns"],"exampleFix":"// before\npublic Object call(int x) { return x; } // declared RETURNS bigint\n// after\npublic Long call(int x) { return (long) x; }","handlingStrategy":"type-guard","validationCode":"if (value != null && !expectedJavaClass.isInstance(value)) throw new IllegalArgumentException(\"returned \" + value.getClass() + \" but declared \" + toDataType().getName());","typeGuard":"boolean matchesDecl = value == null || expectedJavaClass.isInstance(value);","tryCatchPattern":"try { ByteBuffer buf = udfDataType.decompose(protocolVersion, result); } catch (InvalidTypeException e) { log.error(\"UDF return type mismatch: {}\", e.getMessage()); throw e; }","preventionTips":["Declare the Java method return type to match the CQL RETURNS clause exactly","Use boxed types (Long, Integer) matching the CQL type mapping","After editing a UDF body, re-verify the declared return type"],"tags":["udf","java","type-mismatch","serialization"],"backgroundTag":"type-mismatch","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}