apache/cassandra · error · InvalidRequestException
State function return type must be the same as the first…
Error message
State function %s return type must be the same as the first argument type - check STYPE, argument and return types
What it means
The resolved state function's return type must equal the aggregate's STYPE (which is also the first argument type). A mismatch means the accumulator's type would be inconsistent, so the CREATE AGGREGATE is rejected.
Solutions
- Align STYPE with the state function's declared return type
- Alter the state function (recreate with RETURNS) to return the STYPE
- Adjust the argument types so the first argument type matches the function's return
Example fix
// before CREATE AGGREGATE ks.sum(int) SFUNC addf ... -- addf RETURNS bigint // after CREATE AGGREGATE ks.sum(bigint) SFUNC addf ... -- STYPE matches addf return type
Defensive patterns
Strategy: validation
Validate before calling
// compare SFUNC return type with STYPE before executing DDL
Row f = session.execute("SELECT return_type FROM system_schema.functions WHERE keyspace_name=? AND function_name=?", ks, sfunc).one();
if (f != null && !f.getString("return_type").equalsIgnoreCase(stype))
throw new IllegalStateException("SFUNC return type " + f.getString("return_type") + " != STYPE " + stype); Try / catch
try { session.execute(ddl); }
catch (InvalidRequestException e) { if (e.getMessage().contains("return type must be the same")) alignStypeWithSfunc(ddl); else throw e; } Prevention
- Declare SFUNC return type and STYPE together in one place in your schema tooling
- Re-derive STYPE from the function metadata instead of hardcoding it
- Review aggregates whenever the underlying state function's signature changes
When it happens
Trigger: !stateFunction.returnType().equals(stateType) after resolving the state function in CreateAggregateStatement.apply.
Common situations: SFUNC returning a different type than declared STYPE (e.g. function returns bigint, STYPE int); changing STYPE after the function was written; copy-pasted aggregate templates.
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
- Aggregate name ' ' is invalid
- Argument ' ' cannot be frozen; remove frozen<> modifier from
- Cannot alter user type
- Cannot assign value to of type
- Cannot assign value to of type
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/e598844cf2bde72e.
Report an issue: GitHub.
Appendix: source
Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CreateAggregateStatement.java:145
List<AbstractType<?>> argumentTypes =
rawArgumentTypes.stream()
.map(t -> t.prepare(keyspaceName, keyspace.types).getType().udfType())
.collect(toList());
AbstractType<?> stateType = rawStateType.prepare(keyspaceName, keyspace.types).getType().udfType();
List<AbstractType<?>> stateFunctionArguments = Lists.newArrayList(concat(singleton(stateType), argumentTypes));
UserFunction stateFunction =
keyspace.userFunctions
.find(stateFunctionName, stateFunctionArguments)
.orElseThrow(() -> ire("State function %s doesn't exist", stateFunctionString()));
if (stateFunction.isAggregate())
throw ire("State function %s isn't a scalar function", stateFunctionString());
if (!stateFunction.returnType().equals(stateType))
{
throw ire("State function %s return type must be the same as the first argument type - check STYPE, argument and return types",
stateFunctionString());
}
/*
* Resolve the final function and return type
*/
UserFunction finalFunction = null;
AbstractType<?> returnType = stateFunction.returnType();
if (null != finalFunctionName)
{
finalFunction = keyspace.userFunctions.find(finalFunctionName, singletonList(stateType)).orElse(null);
if (null == finalFunction)
throw ire("Final function %s doesn't exist", finalFunctionString());
if (finalFunction.isAggregate())
throw ire("Final function %s isn't a scalar function", finalFunctionString());View on GitHub (pinned to 88fd0f6a0e)