{"record":{"id":"2e938c845dad8109","repo":"apache/seatunnel","slug":"sparse-vector-key-must-be-integer-but-got-s","errorCode":null,"errorMessage":"Sparse vector key must be Integer, but got: %s,","messagePattern":"Sparse vector key must be Integer, but got: (.+?),","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/VectorUtils.java","lineNumber":141,"sourceCode":"    public static Integer[] toIntArray(ByteBuffer byteBuffer) {\n        Integer[] intArray = new Integer[byteBuffer.capacity() / 4];\n\n        for (int i = 0; i < intArray.length; i++) {\n            intArray[i] = byteBuffer.getInt();\n        }\n\n        return intArray;\n    }\n\n    public static Float[] convertSparseVectorToFloatArray(Map<?, ?> sparseVector) {\n        if (sparseVector.isEmpty()) {\n            return new Float[0];\n        }\n        int maxIndex = -1;\n        for (Map.Entry<?, ?> entry : sparseVector.entrySet()) {\n            Object key = entry.getKey();\n            if (!(key instanceof Integer)) {\n                throw new IllegalArgumentException(\n                        String.format(\n                                \"Sparse vector key must be Integer, but got: %s,\",\n                                key.getClass().getName()));\n            }\n            int index = (Integer) key;\n            if (index < 0) {\n                throw new IllegalArgumentException(\n                        String.format(\"Sparse vector index cannot be negative: %d\", index));\n            }\n            // prevent OOM\n            if (index > 1000000) {\n                throw new IllegalArgumentException(\n                        String.format(\"Sparse vector index too large: %d\", index));\n            }\n            maxIndex = Math.max(maxIndex, index);\n        }\n        Float[] denseVector = new Float[maxIndex + 1];\n        Arrays.fill(denseVector, 0.0f);","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/VectorUtils.java#L123-L159","documentation":"VectorUtils.convertSparseVectorToFloatArray requires sparse vector keys to be Integer indexes. When a map key is any other type it throws IllegalArgumentException 'Sparse vector key must be Integer, but got: <class name>'. The raw key class name is included in the message for diagnosis.","triggerScenarios":"Passing a Map<?, ?> representing a sparse vector whose keys are e.g. String (\"0\", \"1\"), Long, or Double instead of Integer — usually from JSON deserialization producing String or Long keys, or a user-built map with the wrong key type.","commonSituations":"JSON payloads like {\"0\": 1.5} deserialized to Map<String, Object>; config/transform inputs where keys were read as Long from a numeric parser; mixing dense/sparse helper APIs with differently-typed maps.","solutions":["Convert keys to Integer before calling: map.entrySet -> Integer.valueOf(key.toString())","Ensure JSON parsing produces Integer keys (use a typed Map<Integer, Float> target or custom deserializer)","Fix upstream producers to emit integer keys","Validate key types in a pre-check loop and coerce or reject early with a clearer error"],"exampleFix":"// before\nMap<String, Float> sparse = parseJson(json); // keys are Strings\nfloat[] arr = VectorUtils.convertSparseVectorToFloatArray(sparse);\n// after\nMap<Integer, Float> sparse = new HashMap<>();\nparseJson(json).forEach((k, v) -> sparse.put(Integer.valueOf(k), v));\nfloat[] arr = VectorUtils.convertSparseVectorToFloatArray(sparse);","handlingStrategy":"type-guard","validationCode":"boolean validSparseKeys(Map<?, ?> m) {\n  return m.keySet().stream().allMatch(k -> k instanceof Integer && (Integer) k >= 0);\n}\nif (!validSparseKeys(sparseVector)) { throw new IllegalArgumentException(\"sparse keys must be non-negative Integers\"); }","typeGuard":"Map<Integer, Float> asIntegerKeyed(Map<?, ?> m) {\n  Map<Integer, Float> out = new java.util.HashMap<>();\n  m.forEach((k, v) -> out.put(Integer.valueOf(k.toString()), ((Number) v).floatValue()));\n  return out;\n}","tryCatchPattern":"try {\n  return VectorUtils.convertSparseVectorToFloatArray(sparse);\n} catch (IllegalArgumentException e) {\n  throw new IllegalArgumentException(\"Bad sparse vector input: \" + e.getMessage(), e);\n}","preventionTips":["Type collections as Map<Integer, Float> so the compiler enforces key type","Configure JSON deserializers to produce Integer keys, or coerce after parsing","Reject/coerce String and Long keys at the ingestion boundary","Add unit tests for JSON-derived sparse vectors"],"tags":["vector","type-mismatch","sparse-vector"],"backgroundTag":"type-mismatch","analyzedSha":"cf67b549a7a6c35fa0beb12d83c62892427ea919","analyzedAt":"2026-09-10T21:44:55.265Z","contentChangedAt":"2026-09-10T21:44:55.265Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}