{"record":{"id":"c75546666197ee7b","repo":"apache/seatunnel","slug":"sparse-vector-index-cannot-be-negative-d","errorCode":null,"errorMessage":"Sparse vector index cannot be negative: %d","messagePattern":"Sparse vector index cannot be negative: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/VectorUtils.java","lineNumber":148,"sourceCode":"        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);\n        for (Map.Entry<?, ?> entry : sparseVector.entrySet()) {\n            Object key = entry.getKey();\n            Object value = entry.getValue();\n            if (!(value instanceof Number)) {\n                throw new IllegalArgumentException(\n                        String.format(\n                                \"Sparse vector value must be a Number, but got: %s\",","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/VectorUtils.java#L130-L166","documentation":"While converting a sparse vector to a float array, each Integer key is treated as an array index; negative indexes are rejected with IllegalArgumentException 'Sparse vector index cannot be negative: <index>' before they can corrupt the output array. (Keys above 1,000,000 are likewise rejected to prevent OOM.)","triggerScenarios":"A sparse vector map containing a negative Integer key, e.g. built by code that computed index-1 offsets or parsed signed values from input data.","commonSituations":"Downstream code subtracting 1 from 1-based indexes to make them 0-based when data was already 0-based, yielding -1; user-supplied data containing negative positions; bad parsers emitting negative placeholder keys.","solutions":["Correct the index computation upstream so keys are 0-based non-negative","Filter or clamp negative keys before calling convertSparseVectorToFloatArray","Validate input data at ingestion (reject negative indexes with a domain-appropriate message)","Check for double-conversion from 1-based to 0-based indexing in the producing code"],"exampleFix":"// before\nMap<Integer, Float> sparse = oneBased.entrySet().stream()\n    .collect(toMap(e -> e.getKey() - 1, Map.Entry::getValue)); // -1 when key==0\n// after\nMap<Integer, Float> sparse = new HashMap<>();\noneBased.forEach((k, v) -> { if (k - 1 >= 0) sparse.put(k - 1, v); });","handlingStrategy":"validation","validationCode":"boolean nonNegativeIndexes(Map<Integer, ?> m) {\n  return m.keySet().stream().allMatch(k -> k != null && k >= 0);\n}\nif (!nonNegativeIndexes(sparseVector)) { throw new IllegalArgumentException(\"sparse vector indexes must be >= 0\"); }","typeGuard":null,"tryCatchPattern":"try {\n  return VectorUtils.convertSparseVectorToFloatArray(sparse);\n} catch (IllegalArgumentException e) {\n  throw new IllegalArgumentException(\"Invalid sparse vector index: \" + e.getMessage(), e);\n}","preventionTips":["Audit 1-based vs 0-based index conversions (double conversion produces -1)","Validate index ranges at data ingestion time","Keep key size under the 1,000,000 cap or use dense arrays for large vectors","Add boundary tests with index 0 and empty maps"],"tags":["vector","index","validation"],"backgroundTag":"value-out-of-range","analyzedSha":"cf67b549a7a6c35fa0beb12d83c62892427ea919","analyzedAt":"2026-09-10T21:44:55.265Z","contentChangedAt":"2026-09-10T21:44:55.265Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}