{"record":{"id":"02a63852e10e7eb0","repo":"apache/cassandra","slug":"the-data-cannot-be-deserialized-as-a-getcollecti","errorCode":null,"errorMessage":"The data cannot be deserialized as a + getCollectionName()","messagePattern":"The data cannot be deserialized as a \\+ getCollectionName\\(\\)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/CollectionSerializer.java","lineNumber":80,"sourceCode":"        for (V value : values)\n        {\n            writeValue(result, value, accessor);\n        }\n        return accessor.valueOf((ByteBuffer) result.flip());\n    }\n\n    public List<ByteBuffer> unpack(ByteBuffer input)\n    {\n        return unpack(input, ByteBufferAccessor.instance);\n    }\n\n    public <V> List<V> unpack(V input, ValueAccessor<V> accessor)\n    {\n        try\n        {\n            int elements = numberOfSerializedElements(readCollectionSize(input, accessor));\n            if (elements < 0)\n                throw new MarshalException(\"The data cannot be deserialized as a \" + getCollectionName());\n\n            // If the received bytes are not corresponding to a collection, the size might be a huge number.\n            // In such a case we do not want to initialize the list with that size as it can result\n            // in an OOM. On the other hand we do not want to have to resize the list\n            // if we can avoid it, so we put a reasonable limit on the initialCapacity.\n            List<V> values = new ArrayList<>(Math.min(elements, 256));\n            int offset = sizeOfCollectionSize();\n\n            for (int i = 0; i < elements; i++)\n            {\n                V value = readValue(input, accessor, offset);\n                offset += sizeOfValue(value, accessor);\n\n                values.add(value);\n            }\n\n            if (!accessor.isEmptyFromOffset(input, offset))\n                throw new MarshalException(\"Unexpected extraneous bytes after \" + getCollectionName() + \" value\");","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/CollectionSerializer.java#L62-L98","documentation":"CollectionSerializer.unpack reads the collection's element-count header and throws MarshalException(\"The data cannot be deserialized as a <collection>\") when the count is negative, meaning the bytes are not a valid serialized collection of this type. This guards against garbage or wrongly-typed values whose first bytes decode to a bogus (often huge/negative) size.","triggerScenarios":"Calling unpack (directly or via deserialize) on a buffer that is not a serialized list/set/map of the expected type — e.g. a scalar value parsed as a collection, bytes written by a different Cassandra version or serializer, or corrupted cell data (src/java/org/apache/cassandra/serializers/CollectionSerializer.java:80).","commonSituations":"Schema mismatch: column altered between list/set/map or between collection and scalar; data loaded via sstableloader with wrong schema; corrupted SSTable bytes; custom code reinterpreting stored values.","solutions":["Confirm the cell's declared type in the schema matches the serializer used (list vs set vs map vs scalar).","Repair/scrub the table if stored bytes are corrupted.","If data came from a migration/loader, re-ingest with the correct schema.","Catch MarshalException at read sites and log hex of the value for diagnosis instead of crashing."],"exampleFix":"// before\nList<ByteBuffer> vals = listType.unpack(value, ByteBufferAccessor.instance); // MarshalException on wrong type\n// after\ntry {\n    vals = listType.unpack(value, ByteBufferAccessor.instance);\n} catch (MarshalException e) {\n    logger.warn(\"Value is not a valid list: {}\", ByteBufferUtil.bytesToHex(value));\n}","handlingStrategy":"validation","validationCode":"if (value == null || value.remaining() < 4) return false; // cannot be a serialized collection\nint n = value.getInt(value.position()); // header must be a sane non-negative count","typeGuard":"static boolean looksLikeCollection(ByteBuffer v) { return v != null && v.remaining() >= 4 && v.getInt(v.position()) >= 0; }","tryCatchPattern":"try { list = collectionType.deserialize(value); } catch (MarshalException e) { logger.warn(\"Not a valid collection: {}\", ByteBufferUtil.bytesToHex(value), e); }","preventionTips":["Never reinterpret cells with a different type than declared in the schema","Keep loader/migration schemas in sync with the target table","Treat MarshalException on reads as a data-corruption signal and run repair","Avoid manual construction of collection serialized bytes"],"tags":["serialization","collection","corrupt-data","marshal"],"backgroundTag":"schema-validation-failed","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}