{"record":{"id":"b9c71ab2e5c05be8","repo":"apache/flink","slug":"tuple-position-is-out-of-range-fieldpos","errorCode":null,"errorMessage":"Tuple position is out of range: {fieldPos}","messagePattern":"Tuple position is out of range: (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/operators/Keys.java","lineNumber":454,"sourceCode":"        public String toString() {\n            return \"ExpressionKeys: \" + StringUtils.join(keyFields, '.');\n        }\n\n        public static boolean isSortKey(int fieldPos, TypeInformation<?> type) {\n\n            if (!type.isTupleType() || !(type instanceof CompositeType)) {\n                throw new InvalidProgramException(\n                        \"Specifying keys via field positions is only valid \"\n                                + \"for tuple data types. Type: \"\n                                + type);\n            }\n            if (type.getArity() == 0) {\n                throw new InvalidProgramException(\n                        \"Tuple size must be greater than 0. Size: \" + type.getArity());\n            }\n\n            if (fieldPos < 0 || fieldPos >= type.getArity()) {\n                throw new IndexOutOfBoundsException(\"Tuple position is out of range: \" + fieldPos);\n            }\n\n            TypeInformation<?> sortKeyType = ((CompositeType<?>) type).getTypeAt(fieldPos);\n            return sortKeyType.isSortKeyType();\n        }\n\n        public static boolean isSortKey(String fieldExpr, TypeInformation<?> type) {\n\n            TypeInformation<?> sortKeyType;\n\n            fieldExpr = fieldExpr.trim();\n            if (SELECT_ALL_CHAR.equals(fieldExpr) || SELECT_ALL_CHAR_SCALA.equals(fieldExpr)) {\n                sortKeyType = type;\n            } else {\n                if (type instanceof CompositeType) {\n                    sortKeyType = ((CompositeType<?>) type).getTypeAt(fieldExpr);\n                } else {\n                    throw new InvalidProgramException(","sourceCodeStart":436,"sourceCodeEnd":472,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/operators/Keys.java#L436-L472","documentation":"Thrown by Keys.ExpressionKeys.isSortKey(int, TypeInformation) as an IndexOutOfBoundsException when the requested field position is negative or >= the tuple arity. This is an unchecked exception signalling an out-of-bounds index into the tuple schema.","triggerScenarios":"Calling isSortKey(5, type) on a Tuple3 (arity 3), or passing a negative position. Any positional keyBy/sort API that forwards an unchecked index.","commonSituations":"Off-by-one errors when computing key positions programmatically. Assuming a tuple has more fields than it does after a schema change.","solutions":["Validate fieldPos against type.getArity() before calling the API.","Use zero-based indexing and ensure fieldPos is in [0, arity-1].","Switch to field-name-based access if positions are error-prone."],"exampleFix":"// before\nint pos = 5;\nKeys.ExpressionKeys.isSortKey(pos, tupleTypeInfo);  // Tuple3 → out of range\n\n// after\nint pos = 5;\nif (pos < 0 || pos >= tupleTypeInfo.getArity()) {\n    throw new IllegalArgumentException(\"pos \" + pos + \" out of range\");\n}\nKeys.ExpressionKeys.isSortKey(pos, tupleTypeInfo);","handlingStrategy":"validation","validationCode":"if (fieldPos < 0 || fieldPos >= type.getArity()) {\n    throw new IllegalArgumentException(\n        \"Field position \" + fieldPos + \" out of range [0,\"\n            + (type.getArity() - 1) + \"]\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    Keys.ExpressionKeys.isSortKey(pos, type);\n} catch (IndexOutOfBoundsException e) {\n    throw new IllegalArgumentException(\"Invalid sort position \" + pos, e);\n}","preventionTips":["Always bounds-check positional indices against type.getArity().","Use zero-based indexing; verify against arity-1.","Prefer field-name access when positions are error-prone."],"tags":["keys","sorting","index-out-of-bounds","tuple","flink-core"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}