{"record":{"id":"19bcd1394411c164","repo":"apache/flink","slug":"comparator-specifies-an-invalid-length-for-the","errorCode":null,"errorMessage":"Comparator {} specifies an invalid length for the normalized key: {}","messagePattern":"Comparator (.+?) specifies an invalid length for the normalized key: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoComparator.java","lineNumber":106,"sourceCode":"                throw new IllegalArgumentException(\"One of the passed reflection fields is null\");\n            }\n\n            // as long as the leading keys support normalized keys, we can build up the composite\n            // key\n            if (k.supportsNormalizedKey()) {\n                if (i == 0) {\n                    // the first comparator decides whether we need to invert the key direction\n                    inverted = k.invertNormalizedKey();\n                } else if (k.invertNormalizedKey() != inverted) {\n                    // if a successor does not agree on the inversion direction, it cannot be part\n                    // of the normalized key\n                    break;\n                }\n\n                nKeys++;\n                final int len = k.getNormalizeKeyLen();\n                if (len < 0) {\n                    throw new RuntimeException(\n                            \"Comparator \"\n                                    + k.getClass().getName()\n                                    + \" specifies an invalid length for the normalized key: \"\n                                    + len);\n                }\n                this.normalizedKeyLengths[i] = len;\n                nKeyLen += this.normalizedKeyLengths[i];\n\n                if (nKeyLen < 0) {\n                    // overflow, which means we are out of budget for normalized key space anyways\n                    nKeyLen = Integer.MAX_VALUE;\n                    break;\n                }\n            } else {\n                break;\n            }\n        }\n        this.numLeadingNormalizableKeys = nKeys;","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoComparator.java#L88-L124","documentation":"PojoComparator's constructor builds a normalized-key layout from its field comparators; if any comparator's getNormalizeKeyLen() returns a negative value, it throws this RuntimeException naming the offending comparator class. Negative means 'infinite/variable-length key' (e.g. String-type comparators before normalization support), which cannot fit the fixed normalized-key byte budget.","triggerScenarios":"Constructing a PojoComparator whose key fields include a type whose comparator reports getNormalizeKeyLen() < 0 - typically string/byte-array keys in combinations that cannot be truncated into the normalized key - during sort/group operator setup.","commonSituations":"groupBy()/sortPartition() on a POJO field of String or similar variable-length type in older Flink versions where the comparator reported -1; custom TypeComparator implementations that forget to implement getNormalizeKeyLen() and return the default -1.","solutions":["If writing a custom TypeComparator, implement getNormalizeKeyLen() to return the fixed normalized-key byte length (>= 0) when supportsNormalizedKey() is true","Check the exception message: it names the exact comparator class to fix","For String keys, use key types whose comparators support finite normalized keys, or upgrade Flink to a version where string comparators report a finite length"],"exampleFix":"// before (custom comparator)\n@Override\npublic int getNormalizeKeyLen() {\n    return -1; // infinite key -> PojoConstructor throws\n}\n\n// after\n@Override\npublic int getNormalizeKeyLen() {\n    return 8; // fixed normalized key length in bytes\n}","handlingStrategy":"validation","validationCode":"for (TypeComparator<?> c : fieldComparators) {\n    if (c.supportsNormalizedKey() && c.getNormalizeKeyLen() < 0) {\n        throw new IllegalStateException(\n            c.getClass().getName() + \" reports infinite normalized key length\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Implement getNormalizeKeyLen() correctly in custom comparators (>= 0 when normalized keys are supported)","Prefer key types with finite normalized keys for POJO sorting/grouping","Read the exception message - it names the offending comparator class"],"tags":["comparator","pojo","normalized-key","sort"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}