apache/flink · error · RuntimeException

Could not initialize comparator {}

Error message

Could not initialize comparator {}

What it means

LocalTimeTypeInfo.instantiateComparator reflectively builds the java.time (LocalDate/LocalTime/LocalDateTime) comparator via comparatorClass.getConstructor(boolean.class).newInstance(ascending). If the comparator class lacks the required public boolean constructor or instantiation otherwise fails, a RuntimeException names the comparator class. This mirrors BasicTypeInfo/SqlTimeTypeInfo but is specific to the java.time type family.

Source

Thrown at flink-core/src/main/java/org/apache/flink/api/common/typeinfo/LocalTimeTypeInfo.java:162

            return false;
        }
    }

    @Override
    public String toString() {
        return clazz.getSimpleName();
    }

    // --------------------------------------------------------------------------------------------

    private static <X> TypeComparator<X> instantiateComparator(
            Class<? extends TypeComparator<X>> comparatorClass, boolean ascendingOrder) {
        try {
            Constructor<? extends TypeComparator<X>> constructor =
                    comparatorClass.getConstructor(boolean.class);
            return constructor.newInstance(ascendingOrder);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not initialize comparator " + comparatorClass.getName(), e);
        }
    }

    public static LocalTimeTypeInfo getInfoFor(Class type) {
        checkNotNull(type);

        if (type == LocalDate.class) {
            return LOCAL_DATE;
        } else if (type == LocalTime.class) {
            return LOCAL_TIME;
        } else if (type == LocalDateTime.class) {
            return LOCAL_DATE_TIME;
        }
        return null;
    }
}

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Ensure the comparator class for java.time types exposes a public constructor taking a single boolean.
  2. Avoid overriding LocalTimeTypeInfo/comparator classes in a fork; rely on the built-in implementations.
  3. On restricted JDKs, add the appropriate --add-opens to allow reflective access to the comparator package.

Example fix

// before: custom comparator missing boolean constructor

// after: use built-in LocalTimeTypeInfo.INFO_FOR LocalDate/LocalTime/LocalDateTime;
// or ensure comparator has: public MyComparator(boolean 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 TypeComparator<?>> c) {
    try { c.getConstructor(boolean.class); return true; }
    catch (NoSuchMethodException e) { return false; }
}

Try / catch

try {
    return LocalTimeTypeInfo.instantiateComparator(clazz, asc);
} catch (RuntimeException e) {
    throw new IllegalStateException(
        "LocalTime comparator " + clazz + " misconfigured", e);
}

Prevention

When it happens

Trigger: A custom LocalTimeTypeInfo with a comparatorClass missing the (boolean) constructor; reflective access blocked by JDK module settings; comparator class made abstract or interface by mistake in a fork.

Common situations: Forking Flink and altering the LocalTime comparator classes; running under a strict module path that blocks reflective construction; classpath corruption loading a partial comparator class.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/e29c72557c1e8c0b. Report an issue: GitHub.