apache/cassandra · error · InvalidRequestException

Keyspace '%s' doesn't exist

Error message

Keyspace '%s' doesn't exist

What it means

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.

Source

Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CreateFunctionStatement.java:119

        if (!FunctionName.isNameValid(functionName))
            throw ire("Function name '%s' is invalid", functionName);

        if (new HashSet<>(argumentNames).size() != argumentNames.size())
            throw ire("Duplicate argument names for given function %s with argument names %s", functionName, argumentNames);

        rawArgumentTypes.stream()
                        .filter(raw -> !raw.isImplicitlyFrozen() && raw.isFrozen())
                        .findFirst()
                        .ifPresent(t -> { throw ire("Argument '%s' cannot be frozen; remove frozen<> modifier from '%s'", t, t); });

        if (!rawReturnType.isImplicitlyFrozen() && rawReturnType.isFrozen())
            throw ire("Return type '%s' cannot be frozen; remove frozen<> modifier from '%s'", rawReturnType, rawReturnType);

        Keyspaces schema = metadata.schema.getKeyspaces();
        KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
        if (null == keyspace)
            throw ire("Keyspace '%s' doesn't exist", keyspaceName);

        List<AbstractType<?>> argumentTypes =
            rawArgumentTypes.stream()
                            .map(t -> t.prepare(keyspaceName, keyspace.types).getType().udfType())
                            .collect(toList());
        AbstractType<?> returnType = rawReturnType.prepare(keyspaceName, keyspace.types).getType().udfType();

        UDFunction function =
            UDFunction.create(new FunctionName(keyspaceName, functionName),
                              argumentNames,
                              argumentTypes,
                              returnType,
                              calledOnNullInput,
                              language,
                              body);

        UserFunction existingFunction = keyspace.userFunctions.find(function.name(), argumentTypes).orElse(null);
        if (null != existingFunction)

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Create the keyspace first with CREATE KEYSPACE, or add IF NOT EXISTS for the keyspace
  2. Verify the keyspace name spelling (use DESCRIBE KEYSPACES)
  3. Connect to the correct cluster/environment where the keyspace exists

Example fix

// before
CREATE FUNCTION analytics.f(int) ...
// after
CREATE KEYSPACE IF NOT EXISTS analytics WITH replication = {'class':'SimpleStrategy','replication_factor':1};
CREATE FUNCTION analytics.f(int) ...
Defensive patterns

Strategy: validation

Validate before calling

ResultSet rs = session.execute("SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?", ks);
if (rs.all().isEmpty()) throw new IllegalStateException("Keyspace does not exist: " + ks);

Type guard

boolean keyspaceExists(Session s, String ks) { return !s.execute("SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?", ks).all().isEmpty(); }

Try / catch

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; }

Prevention

When it happens

Trigger: Executing CREATE FUNCTION ks.f(...) where ks does not exist; misspelled keyspace name; running DDL against a cluster that lacks the keyspace.

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/eedd78b128a79dd4. Report an issue: GitHub.