{"record":{"id":"9304e4a8f6212a70","repo":"apache/flink","slug":"cannot-create-comparator-for-typeclass-getcanonic","errorCode":null,"errorMessage":"Cannot create Comparator for {typeClass.getCanonicalName()}. Class does not implement Comparable interface.","messagePattern":"Cannot create Comparator for (.+?)\\. Class does not implement Comparable interface\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"flink-connectors/flink-hadoop-compatibility/src/main/java/org/apache/flink/api/java/typeutils/WritableTypeInfo.java","lineNumber":70,"sourceCode":"    @PublicEvolving\n    public WritableTypeInfo(Class<T> typeClass) {\n        this.typeClass = checkNotNull(typeClass);\n\n        checkArgument(\n                Writable.class.isAssignableFrom(typeClass) && !typeClass.equals(Writable.class),\n                \"WritableTypeInfo can only be used for subclasses of %s\",\n                Writable.class.getName());\n    }\n\n    @SuppressWarnings({\"rawtypes\", \"unchecked\"})\n    @Override\n    @PublicEvolving\n    public TypeComparator<T> createComparator(\n            boolean sortOrderAscending, ExecutionConfig executionConfig) {\n        if (Comparable.class.isAssignableFrom(typeClass)) {\n            return new WritableComparator(sortOrderAscending, typeClass);\n        } else {\n            throw new UnsupportedOperationException(\n                    \"Cannot create Comparator for \"\n                            + typeClass.getCanonicalName()\n                            + \". \"\n                            + \"Class does not implement Comparable interface.\");\n        }\n    }\n\n    @Override\n    @PublicEvolving\n    public boolean isBasicType() {\n        return false;\n    }\n\n    @Override\n    @PublicEvolving\n    public boolean isTupleType() {\n        return false;\n    }","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-connectors/flink-hadoop-compatibility/src/main/java/org/apache/flink/api/java/typeutils/WritableTypeInfo.java#L52-L88","documentation":"Thrown by WritableTypeInfo.createComparator() when the Writable type does not also implement java.lang.Comparable. Flink can only build a TypeComparator (needed for grouping/sorting/keyed state) for Writable types that are Comparable; otherwise no ordering exists and createComparator refuses with UnsupportedOperationException. The message reports the offending type's canonical name.","triggerScenarios":"Produced when a Writable type is used as a key in a keyed/sorted/grouped operation (keyBy, groupBy, join/coGroup key, order-by, or any path that calls createComparator on its TypeInformation) and that Writable class does not implement Comparable — e.g. keying a stream by a Writable that lacks Comparable.","commonSituations":"Using a custom Writable as a key without implementing Comparable; keying by a Writable type that was only meant to be a value; calling distinct/groupBy/sort on a Writable field whose class is not Comparable; type extraction picking a Writable as the key type unexpectedly.","solutions":["Make the Writable key class implement java.lang.Comparable (and ideally also WritableComparable), supplying a consistent ordering.","If the Writable cannot be made Comparable, key/group on a different (comparable) field instead.","Provide an explicit KeySelector that extracts a comparable key rather than using the whole Writable object as the key.","Verify isKeyType() (which checks Comparable) on the WritableTypeInfo before using the type as a key."],"exampleFix":"// before — Writable used as key, not Comparable\npublic class MyWritable implements Writable { ... }\nds.keyBy(w -> w); // createComparator throws\n// after — implement Comparable\npublic class MyWritable implements Writable, Comparable<MyWritable> {\n    @Override public int compareTo(MyWritable o) { ... }\n}","handlingStrategy":"type-guard","validationCode":"// Before using a Writable as a key, check it is Comparable\nClass<?> keyClass = myWritableKey.getClass();\nif (!Comparable.class.isAssignableFrom(keyClass)) {\n    throw new IllegalStateException(keyClass.getName()\n        + \" is not Comparable; cannot be used as a key. Implement Comparable/WritableComparable\");\n}","typeGuard":"Comparable.class.isAssignableFrom(myWritableKey.getClass())","tryCatchPattern":null,"preventionTips":["Make Writable key types implement Comparable (preferably WritableComparable).","Check isKeyType()/Comparable before keyBy/groupBy/sort on a Writable.","Use a KeySelector extracting a comparable key when the Writable cannot be ordered.","Do not key by a Writable that was only designed as a value."],"tags":["hadoop","type-info","comparator","writable","keyed"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}