{"record":{"id":"20c0d6546dcbf688","repo":"apache/flink","slug":"the-positions-and-types-must-be-of-the-same-length","errorCode":null,"errorMessage":"The positions and types must be of the same length","messagePattern":"The positions and types must be of the same length","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/io/GenericCsvInputFormat.java","lineNumber":530,"sourceCode":"            // field is not quoted\n            while (i < delimLimit && !FieldParser.delimiterNext(bytes, i, delim)) {\n                i++;\n            }\n\n            if (i >= delimLimit) {\n                // no delimiter found. We are at the end of the record\n                return limit;\n            } else {\n                // delimiter found.\n                return i + delim.length;\n            }\n        }\n    }\n\n    @SuppressWarnings(\"unused\")\n    protected static void checkAndCoSort(int[] positions, Class<?>[] types) {\n        if (positions.length != types.length) {\n            throw new IllegalArgumentException(\n                    \"The positions and types must be of the same length\");\n        }\n\n        TreeMap<Integer, Class<?>> map = new TreeMap<Integer, Class<?>>();\n\n        for (int i = 0; i < positions.length; i++) {\n            if (positions[i] < 0) {\n                throw new IllegalArgumentException(\n                        \"The field \" + \" (\" + positions[i] + \") is invalid.\");\n            }\n            if (types[i] == null) {\n                throw new IllegalArgumentException(\"The type \" + i + \" is invalid (null)\");\n            }\n\n            if (map.containsKey(positions[i])) {\n                throw new IllegalArgumentException(\n                        \"The position \" + positions[i] + \" occurs multiple times.\");\n            }","sourceCodeStart":512,"sourceCodeEnd":548,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/io/GenericCsvInputFormat.java#L512-L548","documentation":"Thrown by the static helper checkAndCoSort(int[] positions, Class<?>[] types) when the positions array and the types array have different lengths. The method co-sorts both arrays by position, so they must be parallel arrays of equal length. This is a programming-error guard, not a data-quality check.","triggerScenarios":"A subclass of GenericCsvInputFormat (or custom input format) calls checkAndCoSort with mismatched-length positions and types arrays, e.g. positions = {0,2,4} but types = {Integer.class, String.class}.","commonSituations":"Hand-constructed parallel arrays in a custom CSV subclass where one array was edited but not the other; refactor that added a column to one array only; copy-paste from another format with a different arity.","solutions":["Ensure positions.length == types.length before calling checkAndCoSort; build both arrays from a single source of truth (e.g. a list of (index, type) pairs).","Add an assertion/unit test that the two arrays are the same length.","Pass a single ordered structure (List<Map.Entry<Integer,Class<?>>> or a small record) and split into the two arrays in one place."],"exampleFix":"// before\ncheckAndCoSort(new int[]{0,2,4}, new Class<?>[]{Integer.class, String.class});\n// after\ncheckAndCoSort(new int[]{0,2,4}, new Class<?>[]{Integer.class, String.class, Double.class});","handlingStrategy":"validation","validationCode":"private static void assertParallel(int[] positions, Class<?>[] types) {\n    if (positions.length != types.length) {\n        throw new IllegalArgumentException(\n            \"positions.length (\" + positions.length + \") != types.length (\" + types.length + \")\");\n    }\n}\n// call before checkAndCoSort","typeGuard":"// Bundle index+type so they cannot diverge\nrecord Col(int index, Class<?> type) {}\nList<Col> cols = List.of(new Col(0, Integer.class), new Col(2, String.class));\nint[] positions = cols.stream().mapToInt(Col::index).toArray();\nClass<?>[] types = cols.stream().map(Col::type).toArray(Class<?>[]::new);","tryCatchPattern":"try {\n    GenericCsvInputFormat.checkAndCoSort(positions, types);\n} catch (IllegalArgumentException e) {\n    throw new IllegalArgumentException(\"positions/types length mismatch in CSV config\", e);\n}","preventionTips":["Build positions and types from a single list of (index,type) pairs.","Unit-test that the two arrays are equal length for every CSV configuration.","Never hand-edit one array without updating the other."],"tags":["csv","input-format","validation","api-misuse"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}