apache/hadoop · error · IllegalArgumentException

Undefined for " + x

Error message

Undefined for " + x

What it means

QuickSort.getMaxDepth(x) computes the quicksort recursion budget (4 * ceil(log2 x) via 32 - numberOfLeadingZeros(x - 1)) and rejects x <= 0 with IllegalArgumentException("Undefined for N") because the log-based formula is meaningless there. The public sort() invokes it as getMaxDepth(r - p), so this throw means the sort range is empty or inverted — r <= p (r is exclusive).

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/QuickSort.java:49

  public QuickSort() { }

  private static void fix(IndexedSortable s, int p, int r) {
    if (s.compare(p, r) > 0) {
      s.swap(p, r);
    }
  }

  /**
   * Deepest recursion before giving up and doing a heapsort.
   * Returns 2 * ceil(log(n)).
   *
   * @param x x.
   * @return MaxDepth.
   */
  protected static int getMaxDepth(int x) {
    if (x <= 0)
      throw new IllegalArgumentException("Undefined for " + x);
    return (32 - Integer.numberOfLeadingZeros(x - 1)) << 2;
  }

  /**
   * Sort the given range of items using quick sort.
   * {@inheritDoc} If the recursion depth falls below {@link #getMaxDepth},
   * then switch to {@link HeapSort}.
   */
  @Override
  public void sort(IndexedSortable s, int p, int r) {
    sort(s, p, r, null);
  }

  @Override
  public void sort(final IndexedSortable s, int p, int r,
      final Progressable rep) {
    sortInternal(s, p, r, rep, getMaxDepth(r - p));
  }

View on GitHub (pinned to 2add963021)

Solutions

  1. Skip sorting for empty ranges: if (r - p < 2) return;
  2. Fix off-by-one bounds — pass the exclusive end (array length, not length - 1)
  3. Validate p >= 0 && r > p at your wrapper boundary before invoking QuickSort

Example fix

// before
quickSort.sort(indexed, 0, records.length - 1);

// after
if (records.length > 1) {
  quickSort.sort(indexed, 0, records.length); // r is exclusive
}
Defensive patterns

Strategy: validation

Validate before calling

if (p < 0 || r <= p) { return; } // nothing to sort; r is exclusive
quickSort.sort(indexed, p, r);

Prevention

When it happens

Trigger: sort(sortable, 0, 0) — empty range, r - p == 0; sort(s, 5, 2) with p > r; calling getMaxDepth(0) directly; an off-by-one passing array.length - 1 as r instead of array.length.

Common situations: Generic sort wrappers forwarding user-supplied offsets without validation; index arithmetic on empty arrays; unit tests sorting zero-element ranges; refactors that changed whether the end index is inclusive or exclusive.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/c28dacd74649b6e4. Report an issue: GitHub.