{"record":{"id":"396df36071c85b7f","repo":"apache/cassandra","slug":"expected-4-byte-long-for-date-d","errorCode":null,"errorMessage":"Expected 4 byte long for date (%d)","messagePattern":"Expected 4 byte long for date \\((.+?)\\)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/SimpleDateSerializer.java","lineNumber":121,"sourceCode":"        {\n            throw new MarshalException(String.format(\"Unable to make unsigned int (for date) from: '%s'\", source), e);\n        }\n    }\n\n    public static int timeInMillisToDay(long millis)\n    {\n        return (int) (Duration.ofMillis(millis).toDays() - Integer.MIN_VALUE);\n    }\n\n    public static long dayToTimeInMillis(int days)\n    {\n        return Duration.ofDays(days + Integer.MIN_VALUE).toMillis();\n    }\n\n    public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException\n    {\n        if (accessor.size(value) != 4)\n            throw new MarshalException(String.format(\"Expected 4 byte long for date (%d)\", accessor.size(value)));\n    }\n\n    public String toString(Integer value)\n    {\n        if (value == null)\n            return \"\";\n\n        return Instant.ofEpochMilli(dayToTimeInMillis(value)).atZone(UTC).format(formatter);\n    }\n\n    public Class<Integer> getType()\n    {\n        return Integer.class;\n    }\n\n    @Override\n    public boolean shouldQuoteCQLLiterals()\n    {","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/SimpleDateSerializer.java#L103-L139","documentation":"SimpleDateSerializer.validate checks that the binary value of a 'date' column is exactly 4 bytes, matching the unsigned-int day encoding. This error is thrown when deserializing/validating a value whose byte length differs, meaning the stored or transmitted bytes are not a valid date encoding.","triggerScenarios":"Binding a 0-byte, 8-byte (long/timestamp bytes), or truncated buffer as a date value; reading a date column from a table written by a different type; passing a Java Date's 8-byte time encoding where 4-byte date encoding is required.","commonSituations":"Schema drift after ALTER TYPE or migration from timestamp to date; driver/client sending full timestamps into date columns; corrupted sstable data or hand-crafted byte payloads in custom code.","solutions":["Ensure exactly 4 bytes are supplied: encode via SimpleDateSerializer (timeInMillisToDay result as unsigned int)","Do not pass a java.util.Date/long (8 bytes) where a date is expected; convert days encoding instead","Check the column's actual type with the driver's table metadata before binding","If legacy 0/empty values exist, handle empties before calling validate/deserialize"],"exampleFix":"// before\nByteBuffer v = ByteBuffer.allocate(8).putLong(System.currentTimeMillis());\n// after\nint days = SimpleDateSerializer.timeInMillisToDay(System.currentTimeMillis());\nByteBuffer v = TypeCodec.bigint().serialize(...); // or dateCodec.serialize(\"2011-02-03\")","handlingStrategy":"type-guard","validationCode":"if (buf == null || buf.remaining() != 4) throw new IllegalArgumentException(\"date value must be exactly 4 bytes, got \" + (buf == null ? 0 : buf.remaining()));","typeGuard":"boolean isWellFormedDateBytes(ByteBuffer v) { return v != null && v.remaining() == 4; }","tryCatchPattern":"try {\n  serializer.validate(value, byteBufferAccessor);\n} catch (MarshalException e) {\n  throw new DataCorruptionException(\"non-4-byte value in date column\", e);\n}","preventionTips":["Always produce date bytes through SimpleDateSerializer, never by hand","Check table metadata column types before binding raw buffers","Guard against schema drift between environments","Validate external binary data at ingestion"],"tags":["cassandra","serialization","date","byte-size"],"backgroundTag":"type-mismatch","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}