{"record":{"id":"bd3080a4e27835ea","repo":"apache/cassandra","slug":"key-may-not-be-empty","errorCode":null,"errorMessage":"Key may not be empty","messagePattern":"Key may not be empty","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/QueryProcessor.java","lineNumber":289,"sourceCode":"        preparedStatements.invalidate(id);\n        SystemKeyspace.removePreparedStatement(id);\n    }\n\n    public HashMap<MD5Digest, Prepared> getPreparedStatements()\n    {\n        return new HashMap<>(preparedStatements.asMap());\n    }\n\n    public Prepared getPrepared(MD5Digest id)\n    {\n        return preparedStatements.getIfPresent(id);\n    }\n\n    public static void validateKey(ByteBuffer key) throws InvalidRequestException\n    {\n        if (key == null || key.remaining() == 0)\n        {\n            throw new InvalidRequestException(\"Key may not be empty\");\n        }\n        if (key == ByteBufferUtil.UNSET_BYTE_BUFFER)\n            throw new InvalidRequestException(\"Key may not be unset\");\n\n        // check that key can be handled by ByteArrayUtil.writeWithShortLength and ByteBufferUtil.writeWithShortLength\n        if (key.remaining() > FBUtilities.MAX_UNSIGNED_SHORT)\n        {\n            throw new InvalidRequestException(\"Key length of \" + key.remaining() +\n                                              \" is longer than maximum of \" + FBUtilities.MAX_UNSIGNED_SHORT);\n        }\n    }\n\n    public ResultMessage processStatement(CQLStatement statement, QueryState queryState, QueryOptions options, Dispatcher.RequestTime requestTime)\n    throws RequestExecutionException, RequestValidationException\n    {\n        logger.trace(\"Process {} @CL.{}\", statement, options.getConsistency());\n        ClientState clientState = queryState.getClientState();\n        statement.authorize(clientState);","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/QueryProcessor.java#L271-L307","documentation":"QueryProcessor.validateKey() rejects partition keys that are null or zero-length, because Cassandra cannot store or route rows with an empty partition key under the storage format used (short-length-prefixed keys). The check runs before executing any statement that carries a key.","triggerScenarios":"Calling validateKey / executing a statement whose partition key ByteBuffer is null or has remaining()==0, e.g. binding an empty byte[] or a empty string key.","commonSituations":"Application serializes an empty string/byte array as the partition key; deserialization produced a zero-length buffer; token-aware routing code extracting an empty key.","solutions":["Validate the partition key is non-empty before binding/executing the query.","Fix upstream data or serialization code that produced an empty key.","If a sentinel key is needed, use a non-empty value instead of an empty buffer."],"exampleFix":"// before\nbyte[] key = new byte[0];\nstmt.bind(key);\n// after\nif (keyBytes == null || keyBytes.length == 0) throw new IllegalArgumentException(\"key required\");\nstmt.bind(keyBytes);","handlingStrategy":"validation","validationCode":"if (key == null || key.remaining() == 0) throw new IllegalArgumentException(\"partition key must be non-empty\");","typeGuard":"static boolean isValidKey(ByteBuffer k) { return k != null && k.remaining() > 0 && k != ByteBufferUtil.UNSET_BYTE_BUFFER && k.remaining() <= FBUtilities.MAX_UNSIGNED_SHORT; }","tryCatchPattern":"try { session.execute(stmt); } catch (InvalidRequestException e) { if (e.getMessage().equals(\"Key may not be empty\")) { /* fix key serialization */ } }","preventionTips":["Validate keys at API boundaries before binding","Reject empty strings/byte arrays intended as keys upstream","Unit-test key serialization paths"],"tags":["cql","partition-key","validation"],"backgroundTag":"empty-required-field","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}