{"record":{"id":"87c7c8f89a8be236","repo":"apache/cassandra","slug":"key-key-already-maps-to-value-previousvalue","errorCode":null,"errorMessage":"Key ${key} already maps to value ${previousValue}","messagePattern":"Key (.+?) already maps to value (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"warning","filePath":"src/java/org/apache/cassandra/utils/ByteBufferUtil.java","lineNumber":642,"sourceCode":"        else if (obj instanceof List)\n        {\n            List<?> list = (List<?>) obj;\n            // convert subtypes to BB\n            List<ByteBuffer> bbs = list.stream().map(ByteBufferUtil::objectToBytes).collect(Collectors.toList());\n            // decompose/serializer doesn't use the isMultiCell, so safe to do this\n            return ListType.getInstance(BytesType.instance, false).decompose(bbs);\n        }\n        else if (obj instanceof Map)\n        {\n            Map<?, ?> map = (Map<?, ?>) obj;\n            // convert subtypes to BB\n            Map<ByteBuffer, ByteBuffer> bbs = new LinkedHashMap<>();\n            for (Map.Entry<?, ?> e : map.entrySet())\n            {\n                Object key = e.getKey();\n                ByteBuffer previousValue = bbs.put(objectToBytes(key), objectToBytes(e.getValue()));\n                if (previousValue != null)\n                    throw new IllegalStateException(\"Key \" + key + \" already maps to value \" + previousValue);\n            }\n            // decompose/serializer doesn't use the isMultiCell, so safe to do this\n            return MapType.getInstance(BytesType.instance, BytesType.instance, false).decompose(bbs);\n        }\n        else if (obj instanceof Set)\n        {\n            Set<?> set = (Set<?>) obj;\n            // convert subtypes to BB\n            Set<ByteBuffer> bbs = new LinkedHashSet<>();\n            for (Object o : set)\n                if (!bbs.add(objectToBytes(o)))\n                    throw new IllegalStateException(\"Object \" + o + \" maps to a buffer that already exists in the set\");\n            // decompose/serializer doesn't use the isMultiCell, so safe to do this\n            return SetType.getInstance(BytesType.instance, false).decompose(bbs);\n        }\n        else if (obj instanceof Date)\n            return TimestampType.instance.decompose((Date) obj);\n        else","sourceCodeStart":624,"sourceCodeEnd":660,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/utils/ByteBufferUtil.java#L624-L660","documentation":"ByteBufferUtil.objectToBytes converts UDT/collection field objects (Map, Set, Date, etc.) into ByteBuffers for decomposition. When a Map contains two keys that serialize to the same ByteBuffer, the duplicate insertion is detected and IllegalStateException is thrown.","triggerScenarios":"Calling objectToBytes on a Map with keys that are equal after conversion (e.g., distinct objects whose byte representations collide), typically via UDT serialization of a map value.","commonSituations":"Building a UDT value in application code with keys differing in Java type but equal bytes (e.g., 1 as Integer vs 1L as Long both becoming the same buffer); custom serializers producing identical bytes for different keys.","solutions":["Deduplicate map keys before conversion so each key serializes uniquely","Ensure key objects are of a consistent type","Review custom serializers for keys to avoid producing identical byte output","Validate input maps before constructing UDTs"],"exampleFix":"// before\nMap<Object, Object> m = new HashMap<>();\nm.put(1, \"a\"); m.put(1L, \"b\"); // both -> same bytes\n// after\nm.put(1L, \"b\"); // one canonical key type only","handlingStrategy":"validation","validationCode":"// detect byte-colliding keys before conversion\nSet<ByteBuffer> seen = new HashSet<>();\nfor (Object k : map.keySet()) {\n    if (!seen.add(ByteBufferUtil.objectToBytes(k).duplicate()))\n        throw new IllegalArgumentException(\"duplicate serialized key: \" + k);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use one canonical key type per map","Deduplicate keys by serialized form before building UDT values","Test custom key serializers for injectivity"],"tags":["serialization","collections","bytebuffer"],"backgroundTag":"internal-invariant-violation","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"}