{"record":{"id":"e6c3bda308b319ab","repo":"apache/cassandra","slug":"type-error-cannot-assign-result-of-function-s-t","errorCode":null,"errorMessage":"Type error: cannot assign result of function %s (type %s) to %s (type %s)","messagePattern":"Type error: cannot assign result of function (.+?) \\(type (.+?)\\) to (.+?) \\(type (.+?)\\)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/FunctionCall.java","lineNumber":177,"sourceCode":"        {\n            Function fun = FunctionResolver.get(keyspace, name, terms, receiver.ksName, receiver.cfName, receiver.type, UserFunctions.getCurrentUserFunctions(name, keyspace));\n            if (fun == null)\n                throw invalidRequest(\"Unknown function %s called\", name);\n            if (fun.isAggregate())\n                throw invalidRequest(\"Aggregation function are not supported in the where clause\");\n\n            ScalarFunction scalarFun = (ScalarFunction) fun;\n\n            // Functions.get() will complain if no function \"name\" type check with the provided arguments.\n            // We still have to validate that the return type matches however\n            if (!scalarFun.testAssignment(keyspace, receiver).isAssignable())\n            {\n                if (OperationFcts.isOperation(name))\n                    throw invalidRequest(\"Type error: cannot assign result of operation %s (type %s) to %s (type %s)\",\n                                         OperationFcts.getOperator(scalarFun.name()), scalarFun.returnType().asCQL3Type(),\n                                         receiver.name, receiver.type.asCQL3Type());\n\n                throw invalidRequest(\"Type error: cannot assign result of function %s (type %s) to %s (type %s)\",\n                                     scalarFun.name(), scalarFun.returnType().asCQL3Type(),\n                                     receiver.name, receiver.type.asCQL3Type());\n            }\n\n            if (fun.argTypes().size() != terms.size())\n                throw invalidRequest(\"Incorrect number of arguments specified for function %s (expected %d, found %d)\",\n                                     fun, fun.argTypes().size(), terms.size());\n\n            List<Term> parameters = new ArrayList<>(terms.size());\n            for (int i = 0; i < terms.size(); i++)\n            {\n                Term t = terms.get(i).prepare(keyspace, FunctionResolver.makeArgSpec(receiver.ksName, receiver.cfName, scalarFun, i));\n                parameters.add(t);\n            }\n\n            return new FunctionCall(scalarFun, parameters);\n        }\n","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/FunctionCall.java#L159-L195","documentation":"Same receiver-assignability check as the operation variant, but for regular (non-operation) scalar functions: the resolved function's return type cannot be assigned to the receiver column/spec type, so prepare() throws this type error naming the function, its return type, and the receiver.","triggerScenarios":"e.g. `SELECT * FROM t WHERE intcol = uuidFunc()` — the function returns UUID but the compared column is int; or a UDF returning text compared against a timestamp column.","commonSituations":"UDF return type changed after creation while queries still assume the old type; comparing function results against columns of a different type; misusing native functions like toTimestamp vs toDate.","solutions":["Make the function return type match the receiver type (ALTER/RECREATE the UDF or pick a different native function)","Compare against a column of the function's return type","Wrap the result in a converting function (e.g. toTimestamp(...)) so types line up"],"exampleFix":"// before\nSELECT * FROM t WHERE intcol = someUuidFunc();\n// after\nSELECT * FROM t WHERE uuidcol = someUuidFunc();","handlingStrategy":"validation","validationCode":"const fnReturn = await getFunctionReturnType(keyspace, fname); // from system_schema.functions\nif (fnReturn !== receiverColumnType) throw new Error(`function returns ${fnReturn}, receiver expects ${receiverColumnType}`);","typeGuard":null,"tryCatchPattern":"try { rs = session.execute(q); } catch (InvalidRequestException e) { if (e.getMessage().includes('cannot assign result of function')) { /* align return type with receiver */ } else throw e; }","preventionTips":["Keep UDF return types synchronized with the queries that use them","Wrap results in conversion functions (toTimestamp, toInt) when types differ"],"tags":["cql","type-error","functions","udf"],"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"}