{"record":{"id":"50f63781dacdf67e","repo":"apache/flink","slug":"the-grouping-fields-must-not-be-empty","errorCode":null,"errorMessage":"The grouping fields must not be empty.","messagePattern":"The grouping fields must not be empty\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/operators/Keys.java","lineNumber":250,"sourceCode":"        public ExpressionKeys(int[] keyPositions, TypeInformation<T> type) {\n            this(keyPositions, type, false);\n        }\n\n        /** Create int-based (non-nested) field position keys on a tuple type. */\n        public ExpressionKeys(int[] keyPositions, TypeInformation<T> type, boolean allowEmpty) {\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            if (!allowEmpty && (keyPositions == null || keyPositions.length == 0)) {\n                throw new IllegalArgumentException(\"The grouping fields must not be empty.\");\n            }\n\n            this.keyFields = new ArrayList<>();\n\n            if (keyPositions == null || keyPositions.length == 0) {\n                // use all tuple fields as key fields\n                keyPositions = createIncrIntArray(type.getArity());\n            } else {\n                rangeCheckFields(keyPositions, type.getArity() - 1);\n            }\n\n            checkArgument(\n                    keyPositions.length > 0, \"Grouping fields can not be empty at this point\");\n\n            // extract key field types\n            CompositeType<T> cType = (CompositeType<T>) type;\n            this.keyFields = new ArrayList<>(type.getTotalFields());\n","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/operators/Keys.java#L232-L268","documentation":"Thrown by ExpressionKeys(int[], TypeInformation, boolean) when keyPositions is null or empty and allowEmpty is false. Grouping/keying requires at least one field; an empty position array (without explicit allowance) means no key was supplied.","triggerScenarios":"Calling groupBy() with an empty int[] (e.g. groupBy(new int[0])); passing a null positions array; building positions from a list that turned out empty.","commonSituations":"Programmatic key construction where the field list is conditionally empty; passing a config-driven field array that resolved to zero entries; refactors that strip all fields but leave the call.","solutions":["Ensure the positions array has at least one entry.","If empty is genuinely valid in your flow, pass allowEmpty=true only when you handle the all-fields-selected semantics.","Validate the array is non-empty before constructing ExpressionKeys."],"exampleFix":"// before\nint[] keys = fieldsFromConfig(); // possibly empty\nds.groupBy(keys);\n// after\nint[] keys = fieldsFromConfig();\nif (keys.length == 0) throw new IllegalArgumentException(\"no key fields configured\");\nds.groupBy(keys);","handlingStrategy":"validation","validationCode":"if (keyPositions == null || keyPositions.length == 0) {\n    throw new IllegalArgumentException(\"At least one grouping field is required\");\n}\nnew ExpressionKeys<>(keyPositions, type, false);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate the positions array is non-empty before building ExpressionKeys.","Pass allowEmpty=true only when you intend the all-fields-selected semantics.","Reject empty config-driven field lists with a clear domain error."],"tags":["flink-core","keys","expression-keys","validation","grouping"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}