{"record":{"id":"cfd338b00b8ea767","repo":"apache/cassandra","slug":"function-s-already-exists","errorCode":null,"errorMessage":"Function '%s' already exists","messagePattern":"Function '(.+?)' already exists","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateFunctionStatement.java","lineNumber":146,"sourceCode":"            UDFunction.create(new FunctionName(keyspaceName, functionName),\n                              argumentNames,\n                              argumentTypes,\n                              returnType,\n                              calledOnNullInput,\n                              language,\n                              body);\n\n        UserFunction existingFunction = keyspace.userFunctions.find(function.name(), argumentTypes).orElse(null);\n        if (null != existingFunction)\n        {\n            if (existingFunction.isAggregate())\n                throw ire(\"Function '%s' cannot replace an aggregate\", functionName);\n\n            if (ifNotExists)\n                return schema;\n\n            if (!orReplace)\n                throw ire(\"Function '%s' already exists\", functionName);\n\n            if (calledOnNullInput != ((UDFunction) existingFunction).isCalledOnNullInput())\n            {\n                throw ire(\"Function '%s' must have %s directive\",\n                          functionName,\n                          calledOnNullInput ? \"CALLED ON NULL INPUT\" : \"RETURNS NULL ON NULL INPUT\");\n            }\n\n            if (!returnType.isCompatibleWith(existingFunction.returnType()))\n            {\n                throw ire(\"Cannot replace function '%s', the new return type %s is not compatible with the return type %s of existing function\",\n                          functionName,\n                          returnType.asCQL3Type(),\n                          existingFunction.returnType().asCQL3Type());\n            }\n\n            // TODO: update dependent aggregates\n        }","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateFunctionStatement.java#L128-L164","documentation":"A CREATE FUNCTION statement attempted to create a function that already exists (same name and argument types) without IF NOT EXISTS or OR REPLACE. The statement returns early for ifNotExists, but otherwise throws this InvalidRequestException when orReplace is false.","triggerScenarios":"Re-running CREATE FUNCTION for an existing signature without OR REPLACE/IF NOT EXISTS; idempotent migration scripts that omit OR REPLACE.","commonSituations":"Replaying DDL migration scripts; concurrent deployments both creating the same function; recreating a function after a partial rollback.","solutions":["Add OR REPLACE to update the existing function","Add IF NOT EXISTS to make the statement a no-op when the function exists","Drop the function first with DROP FUNCTION if a clean recreate is intended"],"exampleFix":"// before\nCREATE FUNCTION ks.f(int) RETURNS NULL ON NULL INPUT RETURNS int ...\n// after\nCREATE OR REPLACE FUNCTION ks.f(int) RETURNS NULL ON NULL INPUT RETURNS int ...","handlingStrategy":"validation","validationCode":"ResultSet rs = session.execute(\"SELECT argument_types FROM system_schema.functions WHERE keyspace_name = ? AND function_name = ?\", ks, fn);\nif (!rs.all().isEmpty() && !stmtUsesOrReplace) throw new IllegalStateException(\"Function already exists: \" + fn);","typeGuard":null,"tryCatchPattern":"try { session.execute(stmt); } catch (InvalidRequestException e) { if (e.getMessage().endsWith(\"already exists\")) { /* add OR REPLACE/IF NOT EXISTS or skip */ } else throw e; }","preventionTips":["Use IF NOT EXISTS for idempotent migrations","Check system_schema.functions before creating","Serialize DDL deployments so concurrent jobs don't race"],"tags":["cql","udf","duplicate","conflict"],"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"}