{"record":{"id":"76818ab410ca2aa6","repo":"apache/flink","slug":"the-key-index-must-not-be-negative","errorCode":null,"errorMessage":"The key index must not be negative.","messagePattern":"The key index must not be negative\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/operators/Ordering.java","lineNumber":67,"sourceCode":"     * @param order\n     */\n    public Ordering(int index, Class<? extends Comparable<?>> type, Order order) {\n        appendOrdering(index, type, order);\n    }\n\n    /**\n     * Extends this ordering by appending an additional order requirement. If the index has been\n     * previously appended then the unmodified Ordering is returned.\n     *\n     * @param index Field index of the appended order requirement.\n     * @param type Type of the appended order requirement.\n     * @param order Order of the appended order requirement.\n     * @return This ordering with an additional appended order requirement.\n     */\n    public Ordering appendOrdering(\n            Integer index, Class<? extends Comparable<?>> type, Order order) {\n        if (index < 0) {\n            throw new IllegalArgumentException(\"The key index must not be negative.\");\n        }\n        if (order == null) {\n            throw new NullPointerException();\n        }\n        if (order == Order.NONE) {\n            throw new IllegalArgumentException(\n                    \"An ordering must not be created with a NONE order.\");\n        }\n\n        if (!this.indexes.contains(index)) {\n            this.indexes = this.indexes.addField(index);\n            this.types.add(type);\n            this.orders.add(order);\n        }\n\n        return this;\n    }\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/operators/Ordering.java#L49-L85","documentation":"Thrown by Ordering.appendOrdering when the supplied field index is negative. Field indexes reference positions in a tuple/record schema and must be non-negative. A negative index is always a programming error.","triggerScenarios":"Calling new Ordering(-1, type, Order.ASCENDING) or ordering.appendOrdering(-1, ...). Computing an index from subtraction that underflows to negative.","commonSituations":"Arithmetic that produces a negative index (e.g. pos - 1 when pos is 0). Passing unchecked user-supplied indices into ordering construction.","solutions":["Validate that index >= 0 before calling appendOrdering.","Fix the upstream calculation that produced the negative value.","Use guard clauses or Preconditions.checkArgument(index >= 0, ...)."],"exampleFix":"// before\nordering.appendOrdering(pos - 1, Integer.class, Order.ASCENDING);\n\n// after\nint safePos = Math.max(0, pos - 1);\nordering.appendOrdering(safePos, Integer.class, Order.ASCENDING);","handlingStrategy":"validation","validationCode":"if (index < 0) {\n    throw new IllegalArgumentException(\"Field index must be >= 0, got \" + index);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate index >= 0 before calling appendOrdering.","Guard arithmetic that could underflow to negative.","Use Preconditions.checkArgument(index >= 0, ...)."],"tags":["ordering","validation","index","flink-core"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}