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

  1. Ensure the value is exactly 8 bytes: use the serializer's serialize(Long) instead of hand-building the buffer.
  2. Pass null/empty buffer if you intend a null value, not a zero-length-padded buffer.
  3. Check the column type with DESCRIBE — you may be writing to a bigint column with int-sized bytes.
  4. 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

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


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)