{"record":{"id":"66906f3eefd32383","repo":"apache/cassandra","slug":"state-function-s-isn-t-a-scalar-function","errorCode":null,"errorMessage":"State function %s isn't a scalar function","messagePattern":"State function (.+?) isn't a scalar function","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateAggregateStatement.java","lineNumber":141,"sourceCode":"\n        /*\n         * Resolve the state function\n         */\n\n        List<AbstractType<?>> argumentTypes =\n            rawArgumentTypes.stream()\n                            .map(t -> t.prepare(keyspaceName, keyspace.types).getType().udfType())\n                            .collect(toList());\n        AbstractType<?> stateType = rawStateType.prepare(keyspaceName, keyspace.types).getType().udfType();\n        List<AbstractType<?>> stateFunctionArguments = Lists.newArrayList(concat(singleton(stateType), argumentTypes));\n\n        UserFunction stateFunction =\n            keyspace.userFunctions\n                    .find(stateFunctionName, stateFunctionArguments)\n                    .orElseThrow(() -> ire(\"State function %s doesn't exist\", stateFunctionString()));\n\n        if (stateFunction.isAggregate())\n            throw ire(\"State function %s isn't a scalar function\", stateFunctionString());\n\n        if (!stateFunction.returnType().equals(stateType))\n        {\n            throw ire(\"State function %s return type must be the same as the first argument type - check STYPE, argument and return types\",\n                      stateFunctionString());\n        }\n\n        /*\n         * Resolve the final function and return type\n         */\n\n        UserFunction finalFunction = null;\n        AbstractType<?> returnType = stateFunction.returnType();\n\n        if (null != finalFunctionName)\n        {\n            finalFunction = keyspace.userFunctions.find(finalFunctionName, singletonList(stateType)).orElse(null);\n            if (null == finalFunction)","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateAggregateStatement.java#L123-L159","documentation":"The state function resolved for the aggregate is itself an aggregate, not a scalar (non-aggregate) function. An aggregate's state function must be a plain user function; nesting aggregates is not allowed.","triggerScenarios":"keyspace.userFunctions.find(stateFunctionName, stateFunctionArguments) returns a UserFunction whose isAggregate() is true, in CreateAggregateStatement.apply.","commonSituations":"Using an existing aggregate name as the SFUNC of a new aggregate; accidentally creating a function with the same signature as an aggregate earlier.","solutions":["Point SFUNC at a scalar function with a matching signature (CREATE FUNCTION if missing)","Drop/rename the conflicting aggregate so the name resolves to the scalar function","Qualify with a different keyspace where the name refers to a scalar function"],"exampleFix":"// before\nCREATE FUNCTION ks.avg AS ... ; CREATE AGGREGATE ks.total(int) SFUNC avg ...\n// after\nCREATE FUNCTION ks.avg_state(int,int) ...\nCREATE AGGREGATE ks.total(int) SFUNC avg_state ...","handlingStrategy":"validation","validationCode":"// verify the SFUNC resolves to a scalar function before CREATE AGGREGATE\nRow f = session.execute(\"SELECT argument_types, return_type FROM system_schema.functions WHERE keyspace_name=? AND function_name=?\", ks, sfunc).one();\nif (f == null) throw new IllegalStateException(\"SFUNC not found: \" + sfunc);","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); }\ncatch (InvalidRequestException e) { if (e.getMessage().contains(\"isn't a scalar function\")) pickScalarFunctionFor(sfuncName); else throw e; }","preventionTips":["Name aggregates and functions distinctly to avoid signature collisions","Query system_schema.functions/aggregates to confirm which object a name resolves to","Never use an aggregate's name as the SFUNC of another aggregate"],"tags":["cql","aggregate","function-resolution"],"backgroundTag":"incompatible-source-type","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}