{"record":{"id":"baeebabdca76a651","repo":"apache/cassandra","slug":"cannot-use-both-or-replace-and-if-not-exists-d-baeeba","errorCode":null,"errorMessage":"Cannot use both 'OR REPLACE' and 'IF NOT EXISTS' directives","messagePattern":"Cannot use both 'OR REPLACE' and 'IF NOT EXISTS' directives","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateFunctionStatement.java","lineNumber":98,"sourceCode":"        this.rawReturnType = rawReturnType;\n        this.calledOnNullInput = calledOnNullInput;\n        this.language = language;\n        this.body = body;\n        this.orReplace = orReplace;\n        this.ifNotExists = ifNotExists;\n    }\n\n    @Override\n    public boolean compatibleWith(ClusterMetadata metadata)\n    {\n        return metadata.directory.commonSerializationVersion.isAtLeast(Version.V0);\n    }\n\n    // TODO: replace affected aggregates !!\n    public Keyspaces apply(ClusterMetadata metadata)\n    {\n        if (ifNotExists && orReplace)\n            throw ire(\"Cannot use both 'OR REPLACE' and 'IF NOT EXISTS' directives\");\n\n        UDFunction.assertUdfsEnabled(language);\n\n        if (!FunctionName.isNameValid(functionName))\n            throw ire(\"Function name '%s' is invalid\", functionName);\n\n        if (new HashSet<>(argumentNames).size() != argumentNames.size())\n            throw ire(\"Duplicate argument names for given function %s with argument names %s\", functionName, argumentNames);\n\n        rawArgumentTypes.stream()\n                        .filter(raw -> !raw.isImplicitlyFrozen() && raw.isFrozen())\n                        .findFirst()\n                        .ifPresent(t -> { throw ire(\"Argument '%s' cannot be frozen; remove frozen<> modifier from '%s'\", t, t); });\n\n        if (!rawReturnType.isImplicitlyFrozen() && rawReturnType.isFrozen())\n            throw ire(\"Return type '%s' cannot be frozen; remove frozen<> modifier from '%s'\", rawReturnType, rawReturnType);\n\n        Keyspaces schema = metadata.schema.getKeyspaces();","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateFunctionStatement.java#L80-L116","documentation":"Thrown by CreateFunctionStatement.apply (also exercised via CREATE AGGREGATE's shared parsing path) when a statement specifies both OR REPLACE and IF NOT EXISTS. The two directives are mutually exclusive: one forces overwrite, the other forbids overwrite.","triggerScenarios":"CREATE FUNCTION (or CREATE AGGREGATE) ... OR REPLACE ... IF NOT EXISTS ... in the same statement.","commonSituations":"Script generators appending both flags defensively; hand-merged DDL from two templates; macro/ORM code adding IF NOT EXISTS to an existing OR REPLACE statement.","solutions":["Remove IF NOT EXISTS if the intent is to overwrite.","Remove OR REPLACE if the intent is create-if-absent.","Make the DDL generator emit exactly one of the two flags."],"exampleFix":"// before\nCREATE FUNCTION OR REPLACE IF NOT EXISTS ks.f(i int) ...;\n// after\nCREATE FUNCTION OR REPLACE ks.f(i int) ...;","handlingStrategy":"validation","validationCode":"if (orReplace && ifNotExists)\n    throw new IllegalArgumentException(\"Use either OR REPLACE or IF NOT EXISTS, not both\");","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); }\ncatch (InvalidRequestException e) {\n    if (e.getMessage().contains(\"both 'OR REPLACE' and 'IF NOT EXISTS'\")) { /* strip one flag and retry */ }\n    else throw e;\n}","preventionTips":["Emit at most one directive from DDL generators.","Grep generated migration files for the co-occurrence of both flags.","Prefer IF NOT EXISTS for idempotency, OR REPLACE only for deliberate overwrites."],"tags":["cql","conflicting-directives","ddl"],"backgroundTag":"mutually-exclusive-flags","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"}