{"record":{"id":"eedd78b128a79dd4","repo":"apache/cassandra","slug":"keyspace-s-doesn-t-exist-eedd78","errorCode":null,"errorMessage":"Keyspace '%s' doesn't exist","messagePattern":"Keyspace '(.+?)' doesn't exist","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateFunctionStatement.java","lineNumber":119,"sourceCode":"\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();\n        KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);\n        if (null == keyspace)\n            throw ire(\"Keyspace '%s' doesn't exist\", keyspaceName);\n\n        List<AbstractType<?>> argumentTypes =\n            rawArgumentTypes.stream()\n                            .map(t -> t.prepare(keyspaceName, keyspace.types).getType().udfType())\n                            .collect(toList());\n        AbstractType<?> returnType = rawReturnType.prepare(keyspaceName, keyspace.types).getType().udfType();\n\n        UDFunction function =\n            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)","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateFunctionStatement.java#L101-L137","documentation":"CREATE FUNCTION (and similar schema statements) validates the target keyspace exists before preparing types. If schema.getNullable(keyspaceName) returns null, the statement throws this InvalidRequestException because a function cannot be created in a nonexistent keyspace.","triggerScenarios":"Executing CREATE FUNCTION ks.f(...) where ks does not exist; misspelled keyspace name; running DDL against a cluster that lacks the keyspace.","commonSituations":"Typos in keyspace names; environment drift (dev keyspace missing in prod); scripts run before the keyspace-creation migration; wrong cluster/contact point in cqlsh or driver config.","solutions":["Create the keyspace first with CREATE KEYSPACE, or add IF NOT EXISTS for the keyspace","Verify the keyspace name spelling (use DESCRIBE KEYSPACES)","Connect to the correct cluster/environment where the keyspace exists"],"exampleFix":"// before\nCREATE FUNCTION analytics.f(int) ...\n// after\nCREATE KEYSPACE IF NOT EXISTS analytics WITH replication = {'class':'SimpleStrategy','replication_factor':1};\nCREATE FUNCTION analytics.f(int) ...","handlingStrategy":"validation","validationCode":"ResultSet rs = session.execute(\"SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?\", ks);\nif (rs.all().isEmpty()) throw new IllegalStateException(\"Keyspace does not exist: \" + ks);","typeGuard":"boolean keyspaceExists(Session s, String ks) { return !s.execute(\"SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?\", ks).all().isEmpty(); }","tryCatchPattern":"try { session.execute(stmt); } catch (InvalidRequestException e) { if (e.getMessage().startsWith(\"Keyspace\") && e.getMessage().contains(\"doesn't exist\")) { /* create keyspace or abort */ } else throw e; }","preventionTips":["Check system_schema.keyspaces before DDL","Use identical keyspace names across environments via config","Order migrations: keyspaces, then tables, then functions/indexes"],"tags":["cql","keyspace","not-found"],"backgroundTag":"resource-not-found","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}