apache/flink · error · IllegalArgumentException

The elements in the collection are not all subclasses of {vi

Error message

The elements in the collection are not all subclasses of {viewedAs.getCanonicalName()}

What it means

Thrown by checkIterable in IndexLookupGeneratorFunction when an element's runtime class is not assignable to the class derived from the provided TypeInformation. The message includes the canonical name of the expected type, helping pinpoint the type mismatch.

Source

Thrown at flink-connectors/flink-connector-datagen/src/main/java/org/apache/flink/connector/datagen/functions/IndexLookupGeneratorFunction.java:120

        return lookupMap.get(index);
    }

    /**
     * Verifies that all elements in the iterable are non-null, and are of the given class, or a
     * subclass thereof.
     *
     * @param elements The iterable to check.
     * @param viewedAs The class to which the elements must be assignable to.
     */
    private void checkIterable(Iterable<OUT> elements, Class<?> viewedAs) {
        for (OUT elem : elements) {
            numElements++;
            if (elem == null) {
                throw new IllegalArgumentException("The collection contains a null element");
            }

            if (!viewedAs.isAssignableFrom(elem.getClass())) {
                throw new IllegalArgumentException(
                        "The elements in the collection are not all subclasses of "
                                + viewedAs.getCanonicalName());
            }
        }
    }

    private void serializeElements(Iterable<OUT> elements) throws IOException {
        Preconditions.checkState(serializer != null, "serializer not set");
        LOG.info("Serializing elements using  {}", serializer);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputViewStreamWrapper wrapper = new DataOutputViewStreamWrapper(baos);

        try {
            for (OUT element : elements) {
                serializer.serialize(element, wrapper);
            }
        } catch (Exception e) {
            throw new IOException("Serializing the source elements failed: " + e.getMessage(), e);

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Ensure all elements match the declared TypeInformation's type class.
  2. Use Flink's Types utility to specify TypeInformation explicitly and unambiguously.
  3. Inspect the canonical name in the error message to identify the expected type, then fix the mismatched element.

Example fix

// before: expected Long but list contains a String
List<Object> mixed = Arrays.asList(1L, "two", 3L);
new IndexLookupGeneratorFunction<>(Types.LONG, mixed);

// after: use consistent types
List<Long> elements = Arrays.asList(1L, 2L, 3L);
new IndexLookupGeneratorFunction<>(Types.LONG, elements);
Defensive patterns

Strategy: validation

Validate before calling

// Verify type assignability before constructing
Class<?> expected = typeInfo.getTypeClass();
for (OUT elem : elements) {
    if (elem == null || !expected.isAssignableFrom(elem.getClass())) {
        throw new IllegalArgumentException("Type mismatch: " + elem);
    }
}
new IndexLookupGeneratorFunction<>(typeInfo, elements);

Type guard

static <T> boolean allAssignableToType(Iterable<T> elements, TypeInformation<?> typeInfo) {
    Class<?> expected = typeInfo.getTypeClass();
    for (T elem : elements) {
        if (elem == null || !expected.isAssignableFrom(elem.getClass())) return false;
    }
    return true;
}

Prevention

When it happens

Trigger: Passing a TypeInformation for one class but including elements of an incompatible runtime type in the collection; heterogeneous raw-type list pollution at construction time.

Common situations: Type erasure accepting a heterogeneous list; accidental type widening; TypeInformation auto-inference resolving to the wrong class.

Related errors


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