{"record":{"id":"cd1646be28280b6d","repo":"apache/cassandra","slug":"cannot-use-both-or-replace-and-if-not-exists-d","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/CreateAggregateStatement.java","lineNumber":106,"sourceCode":"        this.rawStateType = rawStateType;\n        this.stateFunctionName = stateFunctionName;\n        this.finalFunctionName = finalFunctionName;\n        this.rawInitialValue = rawInitialValue;\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    @Override\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        if (!FunctionName.isNameValid(aggregateName))\n            throw ire(\"Aggregate name '%s' is invalid\", aggregateName);\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 (!rawStateType.isImplicitlyFrozen() && rawStateType.isFrozen())\n            throw ire(\"State type '%s' cannot be frozen; remove frozen<> modifier from '%s'\", rawStateType, rawStateType);\n\n        Keyspaces schema = metadata.schema.getKeyspaces();\n        KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);\n        if (null == keyspace)\n            throw ire(\"Keyspace '%s' doesn't exist\", keyspaceName);\n\n        /*","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateAggregateStatement.java#L88-L124","documentation":"Syntax-validation guard in CREATE AGGREGATE: the CQL grammar permits both 'OR REPLACE' and 'IF NOT EXISTS', but they are mutually exclusive directives, so applying the schema transformation rejects statements that specify both.","triggerScenarios":"Executing CREATE AGGREGATE [IF NOT EXISTS] ... [OR REPLACE] ... with both flags parsed true; checked first in CreateAggregateStatement.apply.","commonSituations":"Copy-pasting template DDL that combined both idempotence clauses; merging two scripts each adding one directive.","solutions":["Remove either OR REPLACE or IF NOT EXISTS from the CREATE AGGREGATE statement","Use OR REPLACE to unconditionally replace an existing aggregate","Use IF NOT EXISTS to skip creation when it already exists"],"exampleFix":"// before\nCREATE AGGREGATE IF NOT EXISTS OR REPLACE ks.avg(int);\n// after\nCREATE AGGREGATE OR REPLACE ks.avg(int);","handlingStrategy":"validation","validationCode":"if (ddl.matches(\"(?s)IF\\\\s+NOT\\\\s+EXISTS\") && ddl.matches(\"(?s)OR\\\\s+REPLACE\"))\n    throw new IllegalArgumentException(\"Use either IF NOT EXISTS or OR REPLACE, not both\");","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); }\ncatch (InvalidRequestException e) { if (e.getMessage().contains(\"both 'OR REPLACE' and 'IF NOT EXISTS'\")) stripFlagsAndRetry(ddl); else throw e; }","preventionTips":["Pick one idempotence strategy per DDL template and stick to it","Lint generated DDL for duplicate directives","Never concatenate two partial DDL templates that each add a directive"],"tags":["cql","aggregate","ddl","conflicting-directives"],"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"}