{"record":{"id":"2aa35d0bd70bf0f6","repo":"apache/flink","slug":"tuple-field-expression-out-of-bounds-of","errorCode":null,"errorMessage":"Tuple field expression \"{}\" out of bounds of {}.","messagePattern":"Tuple field expression \"(.+?)\" out of bounds of (.+?)\\.","errorType":"exception","errorClass":"InvalidFieldReferenceException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/java/typeutils/TupleTypeInfoBase.java","lineNumber":139,"sourceCode":"                    keyPosition += cType.getTotalFields() - 1;\n                } else {\n                    result.add(new FlatFieldDescriptor(offset + keyPosition, type));\n                }\n                keyPosition++;\n            }\n        } else {\n            String fieldStr = matcher.group(1);\n            Matcher fieldMatcher = PATTERN_FIELD.matcher(fieldStr);\n\n            if (!fieldMatcher.matches()) {\n                throw new RuntimeException(\"Invalid matcher pattern\");\n            }\n\n            field = fieldMatcher.group(2);\n            int fieldPos = Integer.valueOf(field);\n\n            if (fieldPos >= this.getArity()) {\n                throw new InvalidFieldReferenceException(\n                        \"Tuple field expression \\\"\"\n                                + fieldStr\n                                + \"\\\" out of bounds of \"\n                                + this.toString()\n                                + \".\");\n            }\n            TypeInformation<?> fieldType = this.getTypeAt(fieldPos);\n            String tail = matcher.group(5);\n            if (tail == null) {\n                if (fieldType instanceof CompositeType) {\n                    // forward offsets\n                    for (int i = 0; i < fieldPos; i++) {\n                        offset += this.getTypeAt(i).getTotalFields();\n                    }\n                    // add all fields of composite type\n                    ((CompositeType<?>) fieldType).getFlatFields(\"*\", offset, result);\n                } else {\n                    // we found the field to add","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/java/typeutils/TupleTypeInfoBase.java#L121-L157","documentation":"Thrown by TupleTypeInfoBase.getFlatFields when the numeric field position parsed from the expression exceeds or equals the tuple arity. For example, referencing 'f3' on a Tuple2 (arity 2, valid indices 0-1). This is an InvalidFieldReferenceException.","triggerScenarios":"Calling tupleType.getFlatFields(\"f5\") on a Tuple3, or keyBy(\"f2\") on a Tuple2. The Integer.parseInt of the field number at line 136 produces a value >= getArity().","commonSituations":"Developer assumes a tuple has more fields than it does, or the data model changed (Tuple3 -> Tuple2) without updating key references. Common after refactoring tuple schemas or when key indices come from a config file.","solutions":["Check the tuple arity and use only valid indices 0 to arity-1.","Print TypeInformation.of(MyTuple.class).getArity() to confirm field count.","If using config-driven keys, validate the index against arity before calling keyBy."],"exampleFix":"// before\ntupleStream.keyBy(\"f3\"); // Tuple2<String, Integer>\n// after\ntupleStream.keyBy(\"f1\");","handlingStrategy":"validation","validationCode":"int arity = TypeInformation.of(MyTuple.class).getArity();\nint pos = Integer.parseInt(fieldExpr.replaceAll(\"^f\", \"\"));\nif (pos < 0 || pos >= arity) {\n    throw new IllegalArgumentException(\n        \"Field position \" + pos + \" out of range [0, \" + (arity - 1) + \"]\");\n}","typeGuard":"boolean isValidTupleIndex(TupleTypeInfoBase<?> type, int pos) {\n    return pos >= 0 && pos < type.getArity();\n}","tryCatchPattern":null,"preventionTips":["Check the tuple arity before referencing high field indices.","When using config-driven key positions, validate against getArity()."],"tags":["tuple","out-of-bounds","field-reference","type-system","flink-core"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}