apache/cassandra · error · MarshalException
Expected 8 or 0 byte long
Error message
Expected 8 or 0 byte long (%d)
What it means
LongSerializer.validate enforces that a serialized long is either exactly 8 bytes or empty (null). Any other size is rejected with this MarshalException naming the offending byte count. LongType columns (and anything backed by long encoding) must carry a full 8-byte value.
Solutions
- Ensure the value is exactly 8 bytes: use the serializer's serialize(Long) instead of hand-building the buffer.
- Pass null/empty buffer if you intend a null value, not a zero-length-padded buffer.
- Check the column type with DESCRIBE — you may be writing to a bigint column with int-sized bytes.
- Catch MarshalException in custom write paths and report which value has the wrong width.
Example fix
// before ByteBuffer bad = ByteBuffer.allocate(4).putInt(42); // 4 bytes // after ByteBuffer good = LongSerializer.instance.serialize(42L); // 8 bytes
Defensive patterns
Strategy: validation
Validate before calling
// Java
public static void requireLongBytes(ByteBuffer v) {
int n = v == null ? 0 : v.remaining();
if (n != 8 && n != 0)
throw new IllegalArgumentException("bigint value must be 8 bytes or empty, got " + n);
} Type guard
boolean isLongSized(ByteBuffer v) { int n = (v == null) ? 0 : v.remaining(); return n == 8 || n == 0; } Try / catch
try { longSerializer.validate(value, accessor); } catch (MarshalException e) { throw new InvalidRequestException("Bad bigint value: " + e.getMessage()); } Prevention
- Use LongSerializer.serialize(Long) instead of wrapping ints or blobs.
- Check column types with DESCRIBE before programmatic writes.
- For null values pass an empty buffer or null, never a partial buffer.
When it happens
Trigger: Binding a value of 0, 1-7, or >8 bytes to a bigint/LongType column — e.g. inserting a blob of the wrong length via CQL, or programmatic writes using the wrong serializer (int vs long).
Common situations: Inserting blob literals into bigint columns, client code writing ints (4 bytes) where longs are expected, migration scripts loading wrong-width data, hand-built buffers in tests.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cannot convert value
- Expected 1 byte for a tinyint
- Expected 1 or 0 byte value
- Expected a bigint value, but got a
- Expected instance of
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/c96f04d0b9231d2a.
Report an issue: GitHub.
Appendix: source
Thrown at src/java/org/apache/cassandra/serializers/LongSerializer.java:43
public class LongSerializer extends TypeSerializer<Long>
{
public static final LongSerializer instance = new LongSerializer();
public <V> Long deserialize(V value, ValueAccessor<V> accessor)
{
return accessor.isEmpty(value) ? null : accessor.toLong(value);
}
public ByteBuffer serialize(Long value)
{
return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value);
}
public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException
{
if (accessor.size(value) != 8 && !accessor.isEmpty(value))
throw new MarshalException(String.format("Expected 8 or 0 byte long (%d)", accessor.size(value)));
}
public String toString(Long value)
{
return value == null ? "" : String.valueOf(value);
}
public Class<Long> getType()
{
return Long.class;
}
}
View on GitHub (pinned to 88fd0f6a0e)