{"record":{"id":"e83ad55149d8d6e7","repo":"apache/cassandra","slug":"expected-1-byte-for-a-tinyint-d","errorCode":null,"errorMessage":"Expected 1 byte for a tinyint (%d)","messagePattern":"Expected 1 byte for a tinyint \\((.+?)\\)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/ByteSerializer.java","lineNumber":43,"sourceCode":"\npublic class ByteSerializer extends TypeSerializer<Byte>\n{\n    public static final ByteSerializer instance = new ByteSerializer();\n\n    public <V> Byte deserialize(V value, ValueAccessor<V> accessor)\n    {\n        return value == null || accessor.isEmpty(value) ? null : accessor.toByte(value);\n    }\n\n    public ByteBuffer serialize(Byte value)\n    {\n        return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value);\n    }\n\n    public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException\n    {\n        if (accessor.size(value) != 1)\n            throw new MarshalException(String.format(\"Expected 1 byte for a tinyint (%d)\", accessor.size(value)));\n    }\n\n    public String toString(Byte value)\n    {\n        return value == null ? \"\" : String.valueOf(value);\n    }\n\n    public Class<Byte> getType()\n    {\n        return Byte.class;\n    }\n}\n","sourceCodeStart":25,"sourceCodeEnd":56,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/ByteSerializer.java#L25-L56","documentation":"ByteSerializer.validate requires a tinyint column's serialized value to be exactly 1 byte; MarshalException(\"Expected 1 byte for a tinyint (%d)\") is thrown for any other length. The stored/written bytes do not match the tinyint wire format.","triggerScenarios":"Writing a short/int/long or text value into a tinyint column with a tool that doesn't apply type encoding; reading a cell typed tinyint that was written as another type; custom byte buffers handed to the serializer (src/java/org/apache/cassandra/serializers/ByteSerializer.java:43).","commonSituations":"External loaders (spark-connector misconfig, sstableloader with wrong schema) writing ints into tinyint columns; hand-built ByteBuffers of the wrong size; schema changed under existing data.","solutions":["Use driver typed setters (setByte/setInt on a tinyint bound statement) so the value encodes as exactly 1 byte.","Fix the ingestion pipeline to convert numeric types to byte before writing.","Alter the column to the type actually being stored (e.g. int) if data is wider: ALTER TABLE t ALTER c TYPE int; (check compatibility).","Rewrite corrupt/mismatched partitions with correctly typed values."],"exampleFix":"// before\nstmt.setInt(\"v\", 42); // if driver encodes as 4 bytes into tinyint column\n// after\nstmt.setByte(\"v\", (byte) 42); // exactly 1 byte","handlingStrategy":"type-guard","validationCode":"if (buf != null && buf.remaining() != 1) throw new IllegalArgumentException(\"tinyint must be exactly 1 byte\");","typeGuard":"static boolean isValidTinyint(ByteBuffer v) { return v == null || v.remaining() == 1; }","tryCatchPattern":"try { writeTinyint(stmt, num); } catch (InvalidQueryException e) { /* wrong width encoding for tinyint */ }","preventionTips":["Use setByte/setInt(byte-typed) on tinyint columns","Verify numeric ranges fit in a byte before casting","Match ingestion schema to target column types exactly","Bulk-load a sample partition and validate before full import"],"tags":["serialization","tinyint","type-mismatch","validation"],"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"}