{"record":{"id":"717c423f2db89883","repo":"apache/cassandra","slug":"aggregate-s-already-exists","errorCode":null,"errorMessage":"Aggregate '%s' already exists","messagePattern":"Aggregate '(.+?)' already exists","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateAggregateStatement.java","lineNumber":229,"sourceCode":"        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    }\n\n    private static boolean isNullOrEmpty(AbstractType<?> type, ByteBuffer bb)\n    {\n        return bb == null ||\n               (bb.remaining() == 0 && type.isEmptyValueMeaningless());\n    }","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateAggregateStatement.java#L211-L247","documentation":"Thrown when CREATE AGGREGATE targets a signature for which an aggregate already exists, IF NOT EXISTS was not specified, and OR REPLACE was not specified. Cassandra refuses to silently overwrite an existing aggregate.","triggerScenarios":"Re-running CREATE AGGREGATE for an existing (name, argumentTypes) signature without IF NOT EXISTS or OR REPLACE.","commonSituations":"Idempotent migration scripts re-executed against an existing schema; accidental duplicate DDL from parallel tooling.","solutions":["Add IF NOT EXISTS to skip creation when the aggregate already exists.","Add OR REPLACE to overwrite the existing aggregate.","DROP AGGREGATE the existing one first if you want a clean create.","Check existing definitions with DESC AGGREGATE or system_schema.functions."],"exampleFix":"// before\nCREATE AGGREGATE ks.avg(int) SFUNC s STYPE int FINALFUNC f;\n// after\nCREATE AGGREGATE IF NOT EXISTS ks.avg(int) SFUNC s STYPE int FINALFUNC f;","handlingStrategy":"validation","validationCode":"var existing = keyspace.userFunctions.find(fnName, argTypes);\nboolean skip = existing.isPresent() && existing.get().isAggregate() && useIfNotExists;\nif (existing.isPresent() && !useIfNotExists && !useOrReplace)\n    throw new IllegalStateException(fnName + \" already exists; use IF NOT EXISTS or OR REPLACE\");","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); }\ncatch (InvalidRequestException e) {\n    if (e.getMessage().contains(\"already exists\")) { /* rerun with IF NOT EXISTS / OR REPLACE */ }\n    else throw e;\n}","preventionTips":["Use IF NOT EXISTS in idempotent migration scripts.","Use OR REPLACE deliberately when overwriting is intended.","DESC AGGREGATE before creating in shared clusters."],"tags":["cql","duplicate","schema"],"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"}