apache/cassandra · error · InvalidRequestException

Invalid timestamp value: <tval>

Error message

Invalid timestamp value: <tval>

What it means

After binding, the timestamp byte value must be a valid LongType-encoded 8-byte value. If LongType.instance.validate fails (wrong byte length/format), Attributes.getTimestamp throws InvalidRequestException 'Invalid timestamp value'. This catches protocol-level corruption or wrong-typed bound values rather than null/unset cases.

Source

Thrown at src/java/org/apache/cassandra/cql3/Attributes.java:103

    public long getTimestamp(long now, FunctionContext context) throws InvalidRequestException
    {
        if (timestamp == null)
            return now;

        ByteBuffer tval = timestamp.bindAndGet(context);
        if (tval == null)
            throw new InvalidRequestException("Invalid null value of timestamp");

        if (tval == ByteBufferUtil.UNSET_BYTE_BUFFER)
            return now;

        try
        {
            LongType.instance.validate(tval);
        }
        catch (MarshalException e)
        {
            throw new InvalidRequestException("Invalid timestamp value: " + tval);
        }

        return LongType.instance.compose(tval);
    }

    public int getTimeToLive(FunctionContext context, TableMetadata metadata) throws InvalidRequestException
    {
        if (timeToLive == null)
        {
            ExpirationDateOverflowHandling.maybeApplyExpirationDateOverflowPolicy(metadata, metadata.params.defaultTimeToLive, true);
            return metadata.params.defaultTimeToLive;
        }

        ByteBuffer tval = timeToLive.bindAndGet(context);
        if (tval == null)
            return 0;

        if (tval == ByteBufferUtil.UNSET_BYTE_BUFFER)

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Bind the timestamp as a Java long via the driver instead of raw byte buffers
  2. Ensure custom code encodes timestamps with LongType.instance.decompose(value)
  3. Check that the value passed is 8 bytes and in big-endian long format
  4. Validate inputs client-side (is it a number in the plausible range?) before binding

Example fix

// before
ByteBuffer bad = ByteBuffer.wrap("1577836800000".getBytes());
// after
ByteBuffer ok = LongType.instance.decompose(1577836800000L);
Defensive patterns

Strategy: validation

Validate before calling

void validateTimestampBytes(ByteBuffer tval) {
    try { org.apache.cassandra.db.marshal.LongType.instance.validate(tval); }
    catch (org.apache.cassandra.serializers.MarshalException e) {
        throw new IllegalArgumentException("timestamp must be a long-encoded buffer", e);
    }
}

Type guard

boolean isLongEncoded(ByteBuffer b) { return b != null && b.remaining() == 8; }

Try / catch

try {
    session.execute(bs);
} catch (InvalidQueryException e) {
    if (e.getMessage().startsWith("Invalid timestamp value"))
        log.error("Bind timestamps as long via the driver, not raw/string buffers");
    throw e;
}

Prevention

When it happens

Trigger: 'USING TIMESTAMP ?' bound with bytes that are not a valid long serialization (e.g. string bytes, wrong-sized buffer) supplied through a raw/legacy code path using typed values.

Common situations: Hand-crafting ByteBuffers for term values in custom BatchStatement/QueryHandler code; using string encodings for numeric bind values; older thrift/custom drivers sending wrong byte widths.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/340a0f6b5842740f. Report an issue: GitHub.