apache/druid · error · IAE

Size[%d] should be between 0 and %d

Error message

Size[%d] should be between 0 and %d

What it means

ArrayBasedIndexedInts.setValues(int[], int) copies `size` elements from the given array into the internal expansion array. It throws IAE when size is negative or larger than the source array length, since copying would read out of bounds.

Source

Thrown at processing/src/main/java/org/apache/druid/segment/data/ArrayBasedIndexedInts.java:69

    }
  }

  public void setSize(int size)
  {
    if (size < 0 || size > expansion.length) {
      throw new IAE("Size[%d] > expansion.length[%d] or < 0", size, expansion.length);
    }
    this.size = size;
  }

  /**
   * Sets the values from the given array. The given values array is not reused and not prone to be mutated later.
   * Instead, the values from this array are copied into an array which is internal to ArrayBasedIndexedInts.
   */
  public void setValues(int[] values, int size)
  {
    if (size < 0 || size > values.length) {
      throw new IAE("Size[%d] should be between 0 and %d", size, values.length);
    }
    ensureSize(size);
    System.arraycopy(values, 0, expansion, 0, size);
    this.size = size;
  }

  public void setValue(int index, int value)
  {
    expansion[index] = value;
  }

  @Override
  public int size()
  {
    return size;
  }

  @Override

View on GitHub (pinned to 9b90983fd2)

Solutions

  1. Pass values.length (or a correctly computed count) as size
  2. Clamp size to [0, values.length] before calling
  3. Check that the array actually contains the expected number of entries

Example fix

// before
indexed.setValues(arr, expectedSize);
// after
indexed.setValues(arr, Math.min(arr.length, Math.max(0, expectedSize)));
Defensive patterns

Strategy: validation

Validate before calling

int safeSize = Math.min(Math.max(size, 0), values.length); indexed.setValues(values, safeSize);

Type guard

boolean validValues(int[] values, int size) { return values != null && size >= 0 && size <= values.length; }

Try / catch

try { indexed.setValues(values, size); } catch (IAE e) { indexed.setValues(values, values.length); }

Prevention

When it happens

Trigger: Calling setValues(values, size) where size < 0 or size > values.length, typically passing a default/oversized size with a shorter array.

Common situations: Using a stale or wrongly computed row length with a freshly allocated values array; misuse during column initialization (initColumnValues).

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/4bf4ebe9491aeaeb. Report an issue: GitHub.