apache/druid · error · IllegalArgumentException

Cannot make array element selector, negative array index…

Error message

Cannot make array element selector, negative array index not supported

What it means

When a nested field path is just a root array element (e.g. '$[0]') on an array-typed column, makeDimensionSelectorUndecorated extracts that element; a negative index cannot name an array element, so it throws IAE. This guards against malformed path specs reaching selector creation.

Solutions

  1. Use a non-negative array index (0-based) in the path, e.g. '$[0]'
  2. Compute the index defensively: Math.max(0, index) or validate before building NestedPathArrayElement
  3. If negative-from-end indexing was intended, rewrite the expression to compute the element differently (e.g. ARRAY_ORDINAL with reverse)

Example fix

// before
new NestedFieldVirtualColumn("arr", "first", null, Collections.singletonList(new NestedPathArrayElement(-1)), null, null, null);
// after
new NestedFieldVirtualColumn("arr", "first", null, Collections.singletonList(new NestedPathArrayElement(0)), null, null, null);
Defensive patterns

Strategy: validation

Validate before calling

int idx = ((NestedPathArrayElement) parts.get(0)).getIndex();
if (idx < 0) throw new IllegalArgumentException("Array index must be >= 0");

Type guard

boolean ok = part instanceof NestedPathArrayElement && ((NestedPathArrayElement) part).getIndex() >= 0;

Try / catch

try { col.makeDimensionSelector(spec, offset); } catch (IllegalArgumentException e) { /* clamp index and retry */ }

Prevention

When it happens

Trigger: NestedFieldVirtualColumn with pathParts = [NestedPathArrayElement(-1)] (or jq path resolving to negative index) used in a dimension selector query on an array column.

Common situations: Programmatic path construction computing an index that can be negative (e.g. subtracting to index from the end); hand-written jq path where the parser produced a negative part index.

Related errors


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

Appendix: source

Thrown at processing/src/main/java/org/apache/druid/segment/virtual/NestedFieldVirtualColumn.java:318

      if (theColumn instanceof DictionaryEncodedColumn) {
        final DictionaryEncodedColumn<?> column = (DictionaryEncodedColumn<?>) theColumn;
        return new BestEffortCastingValueSelector(column.makeDimensionSelector(offset, extractionFn));
      } else {
        // for non-dictionary encoded columns, wrap a value selector to make it appear as a dimension selector
        return ValueTypes.makeNumericWrappingDimensionSelector(
            holder.getCapabilities().getType(),
            selectorFactory.makeColumnValueSelector(fieldSpec.columnName),
            extractionFn
        );
      }
    }

    if (isRootArrayElementPathAndArrayColumn(theColumn)) {
      final VariantColumn<?> arrayColumn = (VariantColumn<?>) theColumn;
      ColumnValueSelector<?> arraySelector = arrayColumn.makeColumnValueSelector(offset);
      final int elementNumber = ((NestedPathArrayElement) fieldSpec.parts.get(0)).getIndex();
      if (elementNumber < 0) {
        throw new IAE("Cannot make array element selector, negative array index not supported");
      }
      return new BaseSingleValueDimensionSelector()
      {
        @Nullable
        @Override
        protected String getValue()
        {
          Object o = arraySelector.getObject();
          if (o instanceof Object[]) {
            Object[] array = (Object[]) o;
            if (elementNumber < array.length) {
              Object element = array[elementNumber];
              if (element == null) {
                return null;
              }
              return String.valueOf(element);
            }
          }

View on GitHub (pinned to 9b90983fd2)