{"record":{"id":"340a0f6b5842740f","repo":"apache/cassandra","slug":"invalid-timestamp-value-tval","errorCode":null,"errorMessage":"Invalid timestamp value: <tval>","messagePattern":"Invalid timestamp value: <tval>","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/Attributes.java","lineNumber":103,"sourceCode":"    public long getTimestamp(long now, FunctionContext context) throws InvalidRequestException\n    {\n        if (timestamp == null)\n            return now;\n\n        ByteBuffer tval = timestamp.bindAndGet(context);\n        if (tval == null)\n            throw new InvalidRequestException(\"Invalid null value of timestamp\");\n\n        if (tval == ByteBufferUtil.UNSET_BYTE_BUFFER)\n            return now;\n\n        try\n        {\n            LongType.instance.validate(tval);\n        }\n        catch (MarshalException e)\n        {\n            throw new InvalidRequestException(\"Invalid timestamp value: \" + tval);\n        }\n\n        return LongType.instance.compose(tval);\n    }\n\n    public int getTimeToLive(FunctionContext context, TableMetadata metadata) throws InvalidRequestException\n    {\n        if (timeToLive == null)\n        {\n            ExpirationDateOverflowHandling.maybeApplyExpirationDateOverflowPolicy(metadata, metadata.params.defaultTimeToLive, true);\n            return metadata.params.defaultTimeToLive;\n        }\n\n        ByteBuffer tval = timeToLive.bindAndGet(context);\n        if (tval == null)\n            return 0;\n\n        if (tval == ByteBufferUtil.UNSET_BYTE_BUFFER)","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/Attributes.java#L85-L121","documentation":"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.","triggerScenarios":"'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.","commonSituations":"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.","solutions":["Bind the timestamp as a Java long via the driver instead of raw byte buffers","Ensure custom code encodes timestamps with LongType.instance.decompose(value)","Check that the value passed is 8 bytes and in big-endian long format","Validate inputs client-side (is it a number in the plausible range?) before binding"],"exampleFix":"// before\nByteBuffer bad = ByteBuffer.wrap(\"1577836800000\".getBytes());\n// after\nByteBuffer ok = LongType.instance.decompose(1577836800000L);","handlingStrategy":"validation","validationCode":"void validateTimestampBytes(ByteBuffer tval) {\n    try { org.apache.cassandra.db.marshal.LongType.instance.validate(tval); }\n    catch (org.apache.cassandra.serializers.MarshalException e) {\n        throw new IllegalArgumentException(\"timestamp must be a long-encoded buffer\", e);\n    }\n}","typeGuard":"boolean isLongEncoded(ByteBuffer b) { return b != null && b.remaining() == 8; }","tryCatchPattern":"try {\n    session.execute(bs);\n} catch (InvalidQueryException e) {\n    if (e.getMessage().startsWith(\"Invalid timestamp value\"))\n        log.error(\"Bind timestamps as long via the driver, not raw/string buffers\");\n    throw e;\n}","preventionTips":["Always bind numeric USING TIMESTAMP via driver typed setters","Avoid hand-rolled ByteBuffer encoding for term values","Use LongType.instance.decompose() when building raw values","Add unit tests for custom query-handler term binding"],"tags":["cql","timestamp","type-mismatch"],"backgroundTag":"invalid-argument-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}