apache/cassandra · error · MarshalException

Expected 1 or 0 byte value (%d)

Error message

Expected 1 or 0 byte value (%d)

What it means

BooleanSerializer.validate rejects any serialized boolean whose byte length is not 0 or 1; a boolean column must be a single byte (or empty for null). MarshalException("Expected 1 or 0 byte value (%d)") indicates the buffer written to a boolean column has the wrong length.

Source

Thrown at src/java/org/apache/cassandra/serializers/BooleanSerializer.java:49

    public <V> Boolean deserialize(V value, ValueAccessor<V> accessor)
    {
        if (value == null || accessor.isEmpty(value))
            return null;

        return accessor.getByte(value, 0) != 0;
    }

    public ByteBuffer serialize(Boolean value)
    {
        return (value == null) ? ByteBufferUtil.EMPTY_BYTE_BUFFER
                               : value ? TRUE : FALSE; // false
    }

    public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException
    {
        if (accessor.size(value) > 1)
            throw new MarshalException(String.format("Expected 1 or 0 byte value (%d)", accessor.size(value)));
    }

    public String toString(Boolean value)
    {
        return value == null ? "" : value.toString();
    }

    public Class<Boolean> getType()
    {
        return Boolean.class;
    }
}

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Send the value as a boolean type through the driver (1-byte encoding), not as text or int.
  2. If loading from external data, convert 'true'/'false' strings to real booleans before writing.
  3. Fix the schema if the column actually stores multi-byte values: alter the column to the correct type.
  4. For corrupt stored data, rewrite the partition with correctly typed values.

Example fix

// before
stmt.setString("v", "true"); // 4 bytes -> MarshalException on boolean column
// after
stmt.setBool("v", true); // encodes as single 0x01 byte
Defensive patterns

Strategy: type-guard

Validate before calling

if (v != null && v.size() > 1) throw new IllegalArgumentException("boolean must be empty or 1 byte");

Type guard

static boolean isValidBooleanValue(ByteBuffer v) { return v == null || v.remaining() <= 1; }

Try / catch

try { writeBoolean(stmt, flag); } catch (InvalidQueryException e) { /* value not encoded as 1-byte boolean */ }

Prevention

When it happens

Trigger: Writing an integer, string ("true"), or multi-byte value into a boolean column via a driver or tool that does not encode booleans as 1 byte; deserializing an SSTable cell typed boolean that was written with a different type (src/java/org/apache/cassandra/serializers/BooleanSerializer.java:49).

Common situations: INSERT with a string literal 'true' into boolean via a non-typing tool (cqlsh usually coerces, external loaders may not); sstableloader with mismatched schema; custom clients serializing Java Boolean.toString instead of the 1-byte wire format.

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/e4de2f10e39ec37c. Report an issue: GitHub.