{"record":{"id":"a4e8ef6efdd61b25","repo":"apache/cassandra","slug":"no-receiver-keyspace-has-been-specified-for-functi","errorCode":null,"errorMessage":"No receiver keyspace has been specified for function ","messagePattern":"No receiver keyspace has been specified for function ","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/TokenFct.java","lineNumber":84,"sourceCode":"            if (bb == null)\n                return null;\n            builder.add(bb);\n        }\n        return metadata.partitioner.getTokenFactory().toByteArray(metadata.partitioner.getToken(builder.build().serializeAsPartitionKey()));\n    }\n\n    public static void addFunctionsTo(NativeFunctions functions)\n    {\n        functions.add(new FunctionFactory(\"token\")\n        {\n            @Override\n            public NativeFunction getOrCreateFunction(List<? extends AssignmentTestable> args,\n                                                      AbstractType<?> receiverType,\n                                                      String receiverKeyspace,\n                                                      String receiverTable)\n            {\n                if (receiverKeyspace == null)\n                    throw new InvalidRequestException(\"No receiver keyspace has been specified for function \" + name);\n\n                if (receiverTable == null)\n                    throw new InvalidRequestException(\"No receiver table has been specified for function \" + name);\n\n                TableMetadata metadata = Schema.instance.getTableMetadata(receiverKeyspace, receiverTable);\n                if (metadata == null)\n                    throw new InvalidRequestException(String.format(\"The receiver table %s.%s specified by call to \" +\n                                                                    \"function %s hasn't been found\",\n                                                                    receiverKeyspace, receiverTable, name));\n\n                return new TokenFct(metadata);\n            }\n\n            @Override\n            protected NativeFunction doGetOrCreateFunction(List<AbstractType<?>> argTypes, AbstractType<?> receiverType)\n            {\n                throw new AssertionError(\"Should be unreachable\");\n            }","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/TokenFct.java#L66-L102","documentation":"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.","triggerScenarios":"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.","commonSituations":"Client drivers issuing statements without a keyspace and without USE; tooling building queries programmatically against a null receiver; system/aggregate contexts lacking table context.","solutions":["Set the keyspace explicitly: USE my_ks; before running the query, or fully qualify the table (my_ks.my_table)","In drivers, set the keyspace on the session/connection or per-statement","Ensure the query targets a real table so the receiver keyspace can be derived"],"exampleFix":"// before\ncqlsh> SELECT * FROM t WHERE token(pk) > 0;\n// after\ncqlsh> USE my_ks;\ncqlsh> SELECT * FROM t WHERE token(pk) > 0;","handlingStrategy":"validation","validationCode":"// Ensure a keyspace is bound before executing token() queries:\nif (session.getKeyspace().isEmpty() && !query.toLowerCase().contains(\".\"))\n    query = \"USE my_ks;\" + query; // or fully-qualify the table","typeGuard":null,"tryCatchPattern":"try { session.execute(query); }\ncatch (InvalidRequestException e) {\n  if (e.getMessage().contains(\"No receiver keyspace\")) { /* set keyspace and retry */ }\n  else throw e;\n}","preventionTips":["Always set a keyspace on the session or use USE before token() queries","Fully qualify table names (ks.table) in generated SQL"],"tags":["cql","function","token","keyspace"],"backgroundTag":"missing-required-argument","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"}