{"record":{"id":"600a5a80c901de00","repo":"apache/cassandra","slug":"aggregate-s-cannot-replace-a-function","errorCode":null,"errorMessage":"Aggregate '%s' cannot replace a function","messagePattern":"Aggregate '(.+?)' cannot replace a function","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateAggregateStatement.java","lineNumber":223,"sourceCode":"        }\n\n        /*\n         * Create or replace\n         */\n\n        UDAggregate aggregate =\n            new UDAggregate(new FunctionName(keyspaceName, aggregateName),\n                            argumentTypes,\n                            returnType,\n                            (ScalarFunction) stateFunction,\n                            (ScalarFunction) finalFunction,\n                            initialValue);\n\n        UserFunction existingAggregate = keyspace.userFunctions.find(aggregate.name(), argumentTypes).orElse(null);\n        if (null != existingAggregate)\n        {\n            if (!existingAggregate.isAggregate())\n                throw ire(\"Aggregate '%s' cannot replace a function\", aggregateName);\n\n            if (ifNotExists)\n                return schema;\n\n            if (!orReplace)\n                throw ire(\"Aggregate '%s' already exists\", aggregateName);\n\n            if (!returnType.isCompatibleWith(existingAggregate.returnType()))\n            {\n                throw ire(\"Cannot replace aggregate '%s', the new return type %s isn't compatible with the return type %s of existing function\",\n                          aggregateName,\n                          returnType.asCQL3Type(),\n                          existingAggregate.returnType().asCQL3Type());\n            }\n        }\n\n        return schema.withAddedOrUpdated(keyspace.withSwapped(keyspace.userFunctions.withAddedOrUpdated(aggregate)));\n    }","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateAggregateStatement.java#L205-L241","documentation":"Thrown when creating an aggregate whose signature (name + argument types) collides with an existing scalar FUNCTION in the same keyspace. Functions and aggregates share one namespace per signature, so an aggregate cannot replace a plain function.","triggerScenarios":"CREATE AGGREGATE ks.f(...) where a scalar function ks.f with identical argument types already exists (without OR REPLACE semantics applying, which would still be rejected because the existing object is not an aggregate).","commonSituations":"Name reuse between UDF and UDA; scripted DDL that created the object first as a function; refactoring a function into an aggregate without dropping the original.","solutions":["DROP FUNCTION ks.f (with the same argument types) before creating the aggregate.","Choose a different name for the aggregate.","Note that OR REPLACE does not help here — replacing a function with an aggregate is never permitted."],"exampleFix":"// before\nCREATE AGGREGATE ks.f(int) SFUNC s STYPE int; -- ks.f(int) is a scalar function\n// after\nDROP FUNCTION ks.f(int);\nCREATE AGGREGATE ks.f(int) SFUNC s STYPE int;","handlingStrategy":"validation","validationCode":"var existing = keyspace.userFunctions.find(fnName, argTypes);\nif (existing.isPresent() && !existing.get().isAggregate())\n    throw new IllegalStateException(\"A function named \" + fnName + \" already exists; drop it before creating the aggregate\");","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); }\ncatch (InvalidRequestException e) {\n    if (e.getMessage().contains(\"cannot replace a function\")) { /* DROP FUNCTION then retry */ }\n    else throw e;\n}","preventionTips":["Keep separate naming conventions for UDFs and UDAs.","Check system_schema.functions before creating objects with shared names.","Remember OR REPLACE never converts a function into an aggregate."],"tags":["cql","namespace-collision","user-defined-function"],"backgroundTag":"file-already-exists","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"}