apache/cassandra · error · InvalidRequestException

Type error: cannot assign result of operation %s (type %s) t

Error message

Type error: cannot assign result of operation %s (type %s) to %s (type %s)

What it means

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.

Source

Thrown at src/java/org/apache/cassandra/cql3/functions/FunctionCall.java:173

            return new Raw(name, Collections.singletonList(raw));
        }

        public Term prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
        {
            Function fun = FunctionResolver.get(keyspace, name, terms, receiver.ksName, receiver.cfName, receiver.type, UserFunctions.getCurrentUserFunctions(name, keyspace));
            if (fun == null)
                throw invalidRequest("Unknown function %s called", name);
            if (fun.isAggregate())
                throw invalidRequest("Aggregation function are not supported in the where clause");

            ScalarFunction scalarFun = (ScalarFunction) fun;

            // Functions.get() will complain if no function "name" type check with the provided arguments.
            // We still have to validate that the return type matches however
            if (!scalarFun.testAssignment(keyspace, receiver).isAssignable())
            {
                if (OperationFcts.isOperation(name))
                    throw invalidRequest("Type error: cannot assign result of operation %s (type %s) to %s (type %s)",
                                         OperationFcts.getOperator(scalarFun.name()), scalarFun.returnType().asCQL3Type(),
                                         receiver.name, receiver.type.asCQL3Type());

                throw invalidRequest("Type error: cannot assign result of function %s (type %s) to %s (type %s)",
                                     scalarFun.name(), scalarFun.returnType().asCQL3Type(),
                                     receiver.name, receiver.type.asCQL3Type());
            }

            if (fun.argTypes().size() != terms.size())
                throw invalidRequest("Incorrect number of arguments specified for function %s (expected %d, found %d)",
                                     fun, fun.argTypes().size(), terms.size());

            List<Term> parameters = new ArrayList<>(terms.size());
            for (int i = 0; i < terms.size(); i++)
            {
                Term t = terms.get(i).prepare(keyspace, FunctionResolver.makeArgSpec(receiver.ksName, receiver.cfName, scalarFun, i));
                parameters.add(t);
            }

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Ensure both operands of the operation have types the operator supports and the result matches the receiver (compare like with like)
  2. Cast/convert the operand types, e.g. use bigint + bigint not text + int
  3. Use the matching operation overload for the column type (e.g. date arithmetic via appropriate functions)

Example fix

// before
SELECT * FROM t WHERE txt + 1 = 2;
// after
SELECT * FROM t WHERE numcol + 1 = 2;
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof left !== typeof right) throw new Error('operation operands must have compatible types');

Type guard

const isNumeric = (v) => typeof v === 'number' || typeof v === 'bigint';

Try / catch

try { rs = session.execute(q); } catch (InvalidRequestException e) { if (e.getMessage().includes('cannot assign result of operation')) { /* fix operand types */ } else throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: Arithmetic on text/date columns; adding incompatible numeric types (counter + float) where the result cannot be assigned to the compared column type.

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/153d0a19ca6429fd. Report an issue: GitHub.