apache/cassandra · error · InvalidRequestException

No receiver keyspace has been specified for function

Error message

No receiver keyspace has been specified for function 

What it means

token() arguments depend on the target table's partition-key columns, so the function is created lazily using the receiver's keyspace. If the receiver keyspace is unknown (null) at resolution time, Cassandra cannot determine the table and throws this InvalidRequestException.

Source

Thrown at src/java/org/apache/cassandra/cql3/functions/TokenFct.java:84

            if (bb == null)
                return null;
            builder.add(bb);
        }
        return metadata.partitioner.getTokenFactory().toByteArray(metadata.partitioner.getToken(builder.build().serializeAsPartitionKey()));
    }

    public static void addFunctionsTo(NativeFunctions functions)
    {
        functions.add(new FunctionFactory("token")
        {
            @Override
            public NativeFunction getOrCreateFunction(List<? extends AssignmentTestable> args,
                                                      AbstractType<?> receiverType,
                                                      String receiverKeyspace,
                                                      String receiverTable)
            {
                if (receiverKeyspace == null)
                    throw new InvalidRequestException("No receiver keyspace has been specified for function " + name);

                if (receiverTable == null)
                    throw new InvalidRequestException("No receiver table has been specified for function " + name);

                TableMetadata metadata = Schema.instance.getTableMetadata(receiverKeyspace, receiverTable);
                if (metadata == null)
                    throw new InvalidRequestException(String.format("The receiver table %s.%s specified by call to " +
                                                                    "function %s hasn't been found",
                                                                    receiverKeyspace, receiverTable, name));

                return new TokenFct(metadata);
            }

            @Override
            protected NativeFunction doGetOrCreateFunction(List<AbstractType<?>> argTypes, AbstractType<?> receiverType)
            {
                throw new AssertionError("Should be unreachable");
            }

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Set the keyspace explicitly: USE my_ks; before running the query, or fully qualify the table (my_ks.my_table)
  2. In drivers, set the keyspace on the session/connection or per-statement
  3. Ensure the query targets a real table so the receiver keyspace can be derived

Example fix

// before
cqlsh> SELECT * FROM t WHERE token(pk) > 0;
// after
cqlsh> USE my_ks;
cqlsh> SELECT * FROM t WHERE token(pk) > 0;
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a keyspace is bound before executing token() queries:
if (session.getKeyspace().isEmpty() && !query.toLowerCase().contains("."))
    query = "USE my_ks;" + query; // or fully-qualify the table

Try / catch

try { session.execute(query); }
catch (InvalidRequestException e) {
  if (e.getMessage().contains("No receiver keyspace")) { /* set keyspace and retry */ }
  else throw e;
}

Prevention

When it happens

Trigger: Using token() in a context where no keyspace can be inferred from the statement and no keyspace was set with USE — e.g. dynamic resolution against a null receiver.

Common situations: Client drivers issuing statements without a keyspace and without USE; tooling building queries programmatically against a null receiver; system/aggregate contexts lacking table context.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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