{"record":{"id":"153d0a19ca6429fd","repo":"apache/cassandra","slug":"type-error-cannot-assign-result-of-operation-s","errorCode":null,"errorMessage":"Type error: cannot assign result of operation %s (type %s) to %s (type %s)","messagePattern":"Type error: cannot assign result of operation (.+?) \\(type (.+?)\\) to (.+?) \\(type (.+?)\\)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/FunctionCall.java","lineNumber":173,"sourceCode":"            return new Raw(name, Collections.singletonList(raw));\n        }\n\n        public Term prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException\n        {\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            }","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/FunctionCall.java#L155-L191","documentation":"FunctionCall.Raw.prepare() validates that the resolved scalar function's return type is assignable to the expected receiver type. When it is not, and the function name is actually an operation (e.g. +, -, token-style operations registered in OperationFcts), a dedicated message naming the operator is thrown instead of the generic function type-error.","triggerScenarios":"An operation like `x + 1` used in a term position whose receiver type does not match the operation result, e.g. `WHERE textcol + 1 = 2` or assigning a numeric operation result to a non-numeric receiver.","commonSituations":"Arithmetic on text/date columns; adding incompatible numeric types (counter + float) where the result cannot be assigned to the compared column type.","solutions":["Ensure both operands of the operation have types the operator supports and the result matches the receiver (compare like with like)","Cast/convert the operand types, e.g. use bigint + bigint not text + int","Use the matching operation overload for the column type (e.g. date arithmetic via appropriate functions)"],"exampleFix":"// before\nSELECT * FROM t WHERE txt + 1 = 2;\n// after\nSELECT * FROM t WHERE numcol + 1 = 2;","handlingStrategy":"type-guard","validationCode":"if (typeof left !== typeof right) throw new Error('operation operands must have compatible types');","typeGuard":"const isNumeric = (v) => typeof v === 'number' || typeof v === 'bigint';","tryCatchPattern":"try { rs = session.execute(q); } catch (InvalidRequestException e) { if (e.getMessage().includes('cannot assign result of operation')) { /* fix operand types */ } else throw e; }","preventionTips":["Match operand types to column types before using arithmetic in filters","DESC TABLE to confirm column types before composing operations"],"tags":["cql","type-error","operations","functions"],"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"}