{"record":{"id":"3bc967de2178c619","repo":"apache/cassandra","slug":"invalid-32-bits-float-value-expecting-4-bytes-but","errorCode":null,"errorMessage":"Invalid 32-bits float value, expecting 4 bytes but got ","messagePattern":"Invalid 32-bits float value, expecting 4 bytes but got ","errorType":"validation","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":1472,"sourceCode":"        {\n            if (value == null) return \"NULL\";\n            return Float.toString(value);\n        }\n\n        @Override\n        public ByteBuffer serializeNoBoxing(float value, ProtocolVersion protocolVersion)\n        {\n            ByteBuffer bb = ByteBuffer.allocate(4);\n            bb.putFloat(0, value);\n            return bb;\n        }\n\n        @Override\n        public float deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion)\n        {\n            if (bytes == null || bytes.remaining() == 0) return 0;\n            if (bytes.remaining() != 4)\n                throw new InvalidTypeException(\n                \"Invalid 32-bits float value, expecting 4 bytes but got \" + bytes.remaining());\n\n            return bytes.getFloat(bytes.position());\n        }\n    }\n\n    /**\n     * This codec maps a CQL {@link DataType#inet()} to a Java {@link InetAddress}.\n     */\n    private static class InetCodec extends TypeCodec<InetAddress>\n    {\n\n        private static final InetCodec instance = new InetCodec();\n\n        private InetCodec()\n        {\n            super(DataType.inet(), InetAddress.class);\n        }","sourceCodeStart":1454,"sourceCodeEnd":1490,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L1454-L1490","documentation":"Thrown by FloatCodec.deserializeNoBoxing when the ByteBuffer does not contain exactly 4 bytes, the wire size of a CQL float. Empty or null buffers yield 0, but any other length is rejected since a 32-bit IEEE-754 float cannot be read from a different-size buffer.","triggerScenarios":"Calling FloatCodec.deserializeNoBoxing(bytes, protocolVersion) with a ByteBuffer whose remaining() is not 4, e.g. deserializing a double (8 bytes), a smallint, or a truncated payload as a float.","commonSituations":"CQL type drift between environments (column changed from float to double), wrong codec selected in manual UDT/collection parsing, or corrupt values in SSTable/legacy data imports.","solutions":["Confirm the target column's CQL type is 'float' (4 bytes) and not 'double' (8 bytes).","Log bytes.remaining() when the error occurs to identify what type is actually encoded.","Use the DoubleCodec when the underlying type is double, or re-migrate the column to the intended type.","Use row.getFloat/getDouble via the typed API so codec selection is automatic."],"exampleFix":"// before\nfloat f = new FloatCodec().deserializeNoBoxing(bytes, ProtocolVersion.V4); // bytes is 8\n// after\nif (bytes.remaining() == 8) {\n    double d = new DoubleCodec().deserializeNoBoxing(bytes, ProtocolVersion.V4);\n} else {\n    float f = new FloatCodec().deserializeNoBoxing(bytes, ProtocolVersion.V4);\n}","handlingStrategy":"validation","validationCode":"if (bytes == null || bytes.remaining() == 0) return 0f;\nif (bytes.remaining() != 4)\n    throw new IllegalArgumentException(\"Expected 4 bytes for float, got \" + bytes.remaining());","typeGuard":"boolean isFloatSized(ByteBuffer b) { return b != null && b.remaining() == 4; }","tryCatchPattern":"try { return codec.deserializeNoBoxing(bytes, protocolVersion); }\ncatch (InvalidTypeException e) {\n    log.warn(\"Float deserialization failed: {} (remaining={})\", e.getMessage(), bytes.remaining());\n    return Float.NaN;\n}","preventionTips":["Match codec to the column's DataType (float=4 bytes, double=8).","Use row.getFloat/getDouble instead of manual deserialization.","Detect schema drift with a startup schema check.","Log remaining() when deserialization fails."],"tags":["serialization","bytebuffer","type-codec","deserialization"],"backgroundTag":"type-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"}