apache/flink · error · IndexOutOfBoundsException

{index}

Error message

{index}

What it means

Thrown by Ordering.getFieldNumber(int index) as an IndexOutOfBoundsException when the requested ordinal position is outside [0, numberOfFields-1]. This indexes into the list of ordering requirements (not the tuple fields themselves), returning the actual field position stored at that ordinal.

Source

Thrown at flink-core/src/main/java/org/apache/flink/api/common/operators/Ordering.java:98

            this.orders.add(order);
        }

        return this;
    }

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

    public int getNumberOfFields() {
        return this.indexes.size();
    }

    public FieldList getInvolvedIndexes() {
        return this.indexes;
    }

    public Integer getFieldNumber(int index) {
        if (index < 0 || index >= this.indexes.size()) {
            throw new IndexOutOfBoundsException(String.valueOf(index));
        }
        return this.indexes.get(index);
    }

    public Class<? extends Comparable<?>> getType(int index) {
        if (index < 0 || index >= this.types.size()) {
            throw new IndexOutOfBoundsException(String.valueOf(index));
        }
        return this.types.get(index);
    }

    public Order getOrder(int index) {
        if (index < 0 || index >= this.types.size()) {
            throw new IndexOutOfBoundsException(String.valueOf(index));
        }
        return orders.get(index);
    }

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Bound-check against ordering.getNumberOfFields() before calling getFieldNumber.
  2. Use getInvolvedIndexes() to get the full FieldList instead of indexing one at a time.
  3. Ensure loops use < getNumberOfFields() as the upper bound.

Example fix

// before
for (int i = 0; i <= ordering.getNumberOfFields(); i++) {
    int pos = ordering.getFieldNumber(i);  // off-by-one → IOOBE
}

// after
for (int i = 0; i < ordering.getNumberOfFields(); i++) {
    int pos = ordering.getFieldNumber(i);
}
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index >= ordering.getNumberOfFields()) {
    throw new IllegalArgumentException(
        "Ordinal " + index + " out of range [0,"
            + (ordering.getNumberOfFields() - 1) + "]");
}
ordering.getFieldNumber(index);

Try / catch

try {
    return ordering.getFieldNumber(index);
} catch (IndexOutOfBoundsException e) {
    throw new IllegalArgumentException("Invalid ordering ordinal", e);
}

Prevention

When it happens

Trigger: Calling getFieldNumber(5) on an Ordering with only 2 appended fields. Looping past getNumberOfFields().

Common situations: Assuming the index parameter refers to tuple field positions rather than the ordinal of ordering entries. Loop bounds computed from the wrong source.

Related errors


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