prestodb/presto · error · TProtocolException

Can not serialize the data

Error message

Can not serialize the data

What it means

ThriftCodecUtils.toThrift() serializes a value of type T using the given ThriftCodec into a TMemoryBuffer via TBinaryProtocol. Any exception thrown by the codec's write() is rethrown as TProtocolException with this message. It indicates the value does not conform to the Thrift schema (missing required fields, invalid types) or the codec itself fails.

Source

Thrown at presto-thrift-connector-toolkit/src/main/java/com/facebook/presto/thrift/codec/ThriftCodecUtils.java:50

            TBinaryProtocol protocol = new TBinaryProtocol(transport);
            return thriftCodec.read(protocol);
        }
        catch (Exception e) {
            throw new TProtocolException("Can not deserialize the data", e);
        }
    }

    public static <T> byte[] toThrift(T value, ThriftCodec<T> thriftCodec)
            throws TProtocolException
    {
        TMemoryBufferWriteOnly transport = new TMemoryBufferWriteOnly(1024);
        TBinaryProtocol protocol = new TBinaryProtocol(transport);
        try {
            thriftCodec.write(value, protocol);
            return transport.getBytes();
        }
        catch (Exception e) {
            throw new TProtocolException("Can not serialize the data", e);
        }
    }
}

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Ensure the value is fully populated and matches the codec's Thrift struct definition (required fields non-null)
  2. Rebuild the value via the codec's expected domain type constructors/converters
  3. Check for schema drift after Thrift IDL changes and regenerate/redeploy both sides
  4. Catch TProtocolException at the call site and log the value's shape for diagnosis

Example fix

// before
byte[] bytes = ThriftCodecUtils.toThrift(partialValue, codec); // TProtocolException
// after
if (partialValue == null || partialValue.getRequiredField() == null) {
    throw new IllegalArgumentException("required Thrift field missing");
}
byte[] bytes = ThriftCodecUtils.toThrift(partialValue, codec);
Defensive patterns

Strategy: validation

Validate before calling

if (value == null) {
    throw new IllegalArgumentException("cannot serialize null value");
}
// verify required Thrift fields are populated before write

Prevention

When it happens

Trigger: Calling ThriftCodecUtils.toThrift(value, codec) with a value that violates the Thrift struct definition (null required field, wrong field type, out-of-range value) or with a codec that throws during write.

Common situations: Objects constructed outside normal connector code paths missing required Thrift fields; schema/IDL updates making existing values invalid; passing a value of the wrong generic type to the codec.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/d9d2bc166ff1c5ef. Report an issue: GitHub.