{"record":{"id":"12971c1fcff8c1e9","repo":"apache/flink","slug":"the-number-of-specified-keys-is-different","errorCode":null,"errorMessage":"The number of specified keys is different.","messagePattern":"The number of specified keys is different\\.","errorType":"exception","errorClass":"IncompatibleKeysException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/operators/Keys.java","lineNumber":66,"sourceCode":"    public abstract TypeInformation<?>[] getKeyFieldTypes();\n\n    public abstract TypeInformation<?>[] getOriginalKeyFieldTypes();\n\n    public abstract <E> void validateCustomPartitioner(\n            Partitioner<E> partitioner, TypeInformation<E> typeInfo);\n\n    public boolean isEmpty() {\n        return getNumberOfKeyFields() == 0;\n    }\n\n    /** Check if two sets of keys are compatible to each other (matching types, key counts) */\n    public boolean areCompatible(Keys<?> other) throws IncompatibleKeysException {\n\n        TypeInformation<?>[] thisKeyFieldTypes = this.getKeyFieldTypes();\n        TypeInformation<?>[] otherKeyFieldTypes = other.getKeyFieldTypes();\n\n        if (thisKeyFieldTypes.length != otherKeyFieldTypes.length) {\n            throw new IncompatibleKeysException(IncompatibleKeysException.SIZE_MISMATCH_MESSAGE);\n        } else {\n            for (int i = 0; i < thisKeyFieldTypes.length; i++) {\n                if (!thisKeyFieldTypes[i].equals(otherKeyFieldTypes[i])) {\n                    throw new IncompatibleKeysException(\n                            thisKeyFieldTypes[i], otherKeyFieldTypes[i]);\n                }\n            }\n        }\n        return true;\n    }\n\n    // --------------------------------------------------------------------------------------------\n    //  Specializations for expression-based / extractor-based grouping\n    // --------------------------------------------------------------------------------------------\n\n    public static class SelectorFunctionKeys<T, K> extends Keys<T> {\n\n        private final KeySelector<T, K> keyExtractor;","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/operators/Keys.java#L48-L84","documentation":"Thrown by Keys.areCompatible() as IncompatibleKeysException(SIZE_MISMATCH_MESSAGE) when two key specifications declare a different number of key fields. Flink requires join/coGroup/group keys on both sides to have matching arity so the runtime can pair key fields positionally; a size mismatch means the two sides cannot be key-aligned.","triggerScenarios":"Calling datasetA.join(datasetB).where(0,1).equalTo(0) (2 key fields vs 1); mixing a tuple-position key with a KeySelector returning a single-field type in a join; using different key expressions on left and right of a coGroup.","commonSituations":"Joining two DataSets with mismatched key arity (e.g. .where(0,1).equalTo(0)); evolving one side's schema to add a key field without updating the other; copy-paste of join declarations with a different field count.","solutions":["Make the number of key fields identical on both sides of where(...) and equalTo(...).","If the keys are semantically the same but split across fields, wrap one side in a KeySelector returning a composite key matching the other side's arity.","Re-read the stack trace to confirm which pair of key specs disagree (left vs right)."],"exampleFix":"// before\ndsA.join(dsB).where(0, 1).equalTo(0);\n// after\ndsA.join(dsB).where(0, 1).equalTo(0, 1);","handlingStrategy":"validation","validationCode":"int[] left = ...; int[] right = ...;\nif (left.length != right.length) {\n    throw new IllegalArgumentException(\n        \"Join key arity mismatch: \" + left.length + \" vs \" + right.length);\n}","typeGuard":null,"tryCatchPattern":"try {\n    a.join(b).where(leftKeys).equalTo(rightKeys);\n} catch (Exception e) {\n    if (e.getCause() instanceof Keys.IncompatibleKeysException) { /* fix key arity */ }\n    throw e;\n}","preventionTips":["Keep join/coGroup key arity equal on both sides; assert it in tests.","When evolving a schema, update both sides of every join key declaration.","Prefer KeySelectors returning a single typed key to avoid positional-arity bugs."],"tags":["flink-core","keys","join","cogroup","dataset-api","type-compatibility"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}