apache/druid · error · IllegalArgumentException

Size[%d] must be non-negative

Error message

Size[%d] must be non-negative

What it means

Validation guard on the reusable RangeIndexedInts setter: setSize is meant to configure the reusable [0..N-1] sequence before use, and a negative size would make size()/get() nonsensical. It fires when a cursor factory or test helper (getRow, mockSelectors) configures the reusable instance with a negative row size, which would indicate a corrupted row-length source or a misconfigured selector; the row size handed to the cursor must be >= 0.

Source

Thrown at processing/src/main/java/org/apache/druid/segment/data/RangeIndexedInts.java:39

import org.apache.druid.java.util.common.IAE;
import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector;

/**
 * Reusable IndexedInts that returns sequences [0, 1, ..., N].
 */
public class RangeIndexedInts implements IndexedInts
{
  private int size;

  public RangeIndexedInts()
  {
  }

  public void setSize(int size)
  {
    if (size < 0) {
      throw new IAE("Size[%d] must be non-negative", size);
    }
    this.size = size;
  }

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

  @Override
  public int get(int index)
  {
    if (index < 0 || index >= size) {
      throw new IAE("index[%d] >= size[%d] or < 0", index, size);
    }
    return index;
  }

View on GitHub (pinned to 9b90983fd2)

Solutions

  1. Clamp or validate the computed row count to >= 0 before calling setSize
  2. Fix the upstream computation that produced the negative size (e.g. max(0, end - start))
  3. If this comes from getRow(), check the cursor's range/end offsets for overflow

Example fix

// before
rangeIndexedInts.setSize(end - start);
// after
rangeIndexedInts.setSize(Math.max(0, end - start));
Defensive patterns

Strategy: validation

Validate before calling

int size = end - start; if (size < 0) size = 0; rangeIndexedInts.setSize(size);

Prevention

When it happens

Trigger: Calling setSize(n) with a negative n, typically from a row-holder (getRow) that copied a negative row count from an upstream source or from mock selectors in tests.

Common situations: Cursor/row arithmetic producing a negative count when reading near the end of a segment; test mocks wired with bad sizes.

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/d84f6214d1f6c317. Report an issue: GitHub.