apache/flink · error · RuntimeException
Could not initialize primitive {} array comparator.
Error message
Could not initialize primitive {} array comparator. What it means
PrimitiveArrayTypeInfo.createComparator reflectively instantiates the PrimitiveArrayComparator via comparatorClass.getConstructor(boolean.class).newInstance(ascending). If the comparator class lacks the required public boolean constructor or instantiation throws, a RuntimeException names the comparator class with the 'primitive array comparator' suffix. This is an internal contract: each primitive-array comparator must expose that constructor.
Source
Thrown at flink-core/src/main/java/org/apache/flink/api/common/typeinfo/PrimitiveArrayTypeInfo.java:273
static {
TYPES.put(boolean[].class, BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO);
TYPES.put(byte[].class, BYTE_PRIMITIVE_ARRAY_TYPE_INFO);
TYPES.put(short[].class, SHORT_PRIMITIVE_ARRAY_TYPE_INFO);
TYPES.put(int[].class, INT_PRIMITIVE_ARRAY_TYPE_INFO);
TYPES.put(long[].class, LONG_PRIMITIVE_ARRAY_TYPE_INFO);
TYPES.put(float[].class, FLOAT_PRIMITIVE_ARRAY_TYPE_INFO);
TYPES.put(double[].class, DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO);
TYPES.put(char[].class, CHAR_PRIMITIVE_ARRAY_TYPE_INFO);
}
@Override
@PublicEvolving
public PrimitiveArrayComparator<T, ?> createComparator(
boolean sortOrderAscending, ExecutionConfig executionConfig) {
try {
return comparatorClass.getConstructor(boolean.class).newInstance(sortOrderAscending);
} catch (Exception e) {
throw new RuntimeException(
"Could not initialize primitive "
+ comparatorClass.getName()
+ " array comparator.",
e);
}
}
}
View on GitHub (pinned to 2f3c205e92)
Solutions
- Ensure any custom primitive-array comparator class has a public constructor taking a single boolean.
- Use the built-in PrimitiveArrayTypeInfo instances rather than custom ones.
- On restricted JDKs, add the appropriate --add-opens so reflection into the comparator constructor succeeds.
Example fix
// before: custom comparator lacks boolean constructor
// after: ensure public constructor
public class MyIntArrayComparator extends PrimitiveArrayComparator<Integer, IntComparator> {
public MyIntArrayComparator(boolean ascending) { super(ascending); }
} Defensive patterns
Strategy: type-guard
Validate before calling
try {
comparatorClass.getConstructor(boolean.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(
comparatorClass + " must have a public (boolean) constructor", e);
} Type guard
static boolean hasBooleanCtor(Class<? extends PrimitiveArrayComparator<?,?>> c) {
try { c.getConstructor(boolean.class); return true; }
catch (NoSuchMethodException e) { return false; }
} Try / catch
try {
return ti.createComparator(asc, cfg);
} catch (RuntimeException e) {
throw new IllegalStateException(
"Primitive array comparator misconfigured: " + comparatorClass, e);
} Prevention
- Use the built-in PrimitiveArrayTypeInfo instances.
- Ensure custom primitive-array comparators expose a public (boolean) constructor.
- Add a reflection test if forking comparator classes.
When it happens
Trigger: A custom PrimitiveArrayTypeInfo with a comparatorClass missing the (boolean) constructor; reflective access blocked; comparator class made abstract or otherwise non-instantiable in a fork.
Common situations: Forking Flink and altering primitive-array comparators; JDK module restrictions blocking reflective construction; classpath issues loading a partial comparator implementation.
Related errors
- Could not initialize basic comparator {}
- Could not initialize comparator {}
- Could not initialize comparator {}
- The type {} cannot be used as a key.
- The given class is no array.
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/ca92efbb36924846.
Report an issue: GitHub.