{"record":{"id":"0382e69fa9c2a845","repo":"apache/cassandra","slug":"invalid-boolean-value-expecting-1-byte-but-got","errorCode":null,"errorMessage":"Invalid boolean value, expecting 1 byte but got ","messagePattern":"Invalid boolean value, expecting 1 byte but got ","errorType":"validation","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":1279,"sourceCode":"        @Override\n        public String format(Boolean value)\n        {\n            if (value == null) return \"NULL\";\n            return value ? \"true\" : \"false\";\n        }\n\n        @Override\n        public ByteBuffer serializeNoBoxing(boolean value, ProtocolVersion protocolVersion)\n        {\n            return value ? TRUE.duplicate() : FALSE.duplicate();\n        }\n\n        @Override\n        public boolean deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion)\n        {\n            if (bytes == null || bytes.remaining() == 0) return false;\n            if (bytes.remaining() != 1)\n                throw new InvalidTypeException(\n                \"Invalid boolean value, expecting 1 byte but got \" + bytes.remaining());\n\n            return bytes.get(bytes.position()) != 0;\n        }\n    }\n\n    /**\n     * This codec maps a CQL {@link DataType#decimal()} to a Java {@link BigDecimal}.\n     */\n    private static class DecimalCodec extends TypeCodec<BigDecimal>\n    {\n\n        private static final DecimalCodec instance = new DecimalCodec();\n\n        private DecimalCodec()\n        {\n            super(DataType.decimal(), BigDecimal.class);\n        }","sourceCodeStart":1261,"sourceCodeEnd":1297,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L1261-L1297","documentation":"The boolean codec's deserializeNoBoxing() expects exactly 1 byte on the wire (any nonzero byte is true). A buffer whose remaining size differs from 1 means the bytes are not a CQL boolean, so the codec throws instead of guessing.","triggerScenarios":"Calling booleanCodec.deserializeNoBoxing(bytes, protocolVersion) where bytes.remaining() != 1 — e.g. decoding an int, tinyint, or text column's bytes with the boolean codec.","commonSituations":"Column type changed between boolean and tinyint/int; storing \"true\"/\"false\" text in a column read as boolean; manual byte buffers that put more than one byte (e.g. putInt); custom protocol implementations writing padded values.","solutions":["Encode booleans as exactly one byte: ByteBuffer.allocate(1).put((byte)(b ? 1 : 0)).","Match the codec to the actual column type (tinyint codec for 1-byte signed ints, etc.).","Refresh cached prepared statements after schema type changes.","If bytes come from a custom format, slice/repack to 1 byte before handing them to the codec."],"exampleFix":"// before\nByteBuffer b = ByteBuffer.allocate(2).putShort((short)1);\nbooleanCodec.deserializeNoBoxing(b, version); // throws: got 2 bytes\n// after\nByteBuffer b = ByteBuffer.allocate(1).put((byte)1);\nbooleanCodec.deserializeNoBoxing(b, version); // true","handlingStrategy":"type-guard","validationCode":"if (bytes == null || bytes.remaining() != 1)\n    throw new IllegalArgumentException(\"boolean needs exactly 1 byte, got \" + (bytes == null ? 0 : bytes.remaining()));","typeGuard":"boolean isBooleanBuffer(ByteBuffer b) {\n    return b != null && b.remaining() == 1;\n}","tryCatchPattern":"try {\n    return booleanCodec.deserializeNoBoxing(bytes, protocolVersion);\n} catch (InvalidTypeException e) {\n    log.warn(\"Non-boolean payload ({} bytes), treating as false\", bytes.remaining());\n    return false;\n}","preventionTips":["Encode booleans with put((byte)(b ? 1 : 0)), never putInt/putShort.","Match codec to the actual column type; use tinyint codec for 1-byte signed ints.","Slice per-value buffers so extra bytes from adjacent fields do not leak into remaining()."],"tags":["cql","boolean","codec","deserialization","byte-size"],"backgroundTag":"shape-mismatch","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"}