apache/druid · error · RE

Failed to parse array element

Error message

Failed to parse array element %s as a %s

What it means

Thrown while building an explicitly typed array literal when an element cannot be parsed as the declared element type (e.g. a non-long token in ARRAY<LONG>, non-numeric in ARRAY<DOUBLE>). The grammar matched the literal structurally but coercion to the declared type failed in the switch default.

Solutions

  1. Make each element match the declared array type or use NULL
  2. Change the array type annotation to match the actual element values (e.g. ARRAY<STRING> for text)
  3. Pre-convert values in the producing code before emitting the expression string

Example fix

// before
ARRAY<LONG>[1, '2', abc]
// after
ARRAY<LONG>[1, 2]
Defensive patterns

Strategy: validation

Validate before calling

// Java: verify each element parses as the declared type before building the literal
static boolean elementsMatchDeclaredType(String declared, java.util.List<String> elems) {
  for (String e : elems) {
    if (e.equals("NULL")) continue;
    try {
      switch (declared) {
        case "LONG": Long.parseLong(e); break;
        case "DOUBLE": Double.parseDouble(e); break;
        case "STRING": if (!(e.startsWith("'") && e.endsWith("'"))) return false; break;
        default: return false;
      }
    } catch (NumberFormatException nfe) { return false; }
  }
  return true;
}

Prevention

When it happens

Trigger: Parsing ARRAY<LONG>[abc] or ARRAY<DOUBLE>[true] — elements that are neither NULL nor parseable as the declared type via Numbers.parseLongObject/parseDoubleObject.

Common situations: Mixing strings into numeric typed arrays; decimal values in ARRAY<LONG> that parseLongObject rejects; generated expressions from tools that do not enforce element types.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at processing/src/main/java/org/apache/druid/math/expr/ExprListenerImpl.java:440

        // if value is a string, escape quoting
        final String toParse;
        if (elementContext.STRING() != null) {
          toParse = escapeStringLiteral(elementContext.STRING().getText());
        } else {
          toParse = elementContext.getText();
        }
        switch (type.getElementType().getType()) {
          case LONG:
            values[i] = Numbers.parseLongObject(toParse);
            break;
          case DOUBLE:
            values[i] = Numbers.parseDoubleObject(toParse);
            break;
          case STRING:
            values[i] = toParse;
            break;
          default:
            throw new RE("Failed to parse array element %s as a %s", toParse, type.getElementType().asTypeString());
        }
      }
    }
    nodes.put(ctx, new ArrayExpr(type, values));
  }

  @Override
  public void exitLongArray(ExprParser.LongArrayContext ctx)
  {
    Object[] values = new Object[ctx.longElement().size()];
    for (int i = 0; i < values.length; i++) {
      if (ctx.longElement(i).NULL() != null) {
        values[i] = null;
      } else if (ctx.longElement(i).LONG() != null) {
        values[i] = Long.parseLong(ctx.longElement(i).LONG().getText());
      } else {
        throw new RE("Failed to parse array element %s as a long", ctx.longElement(i).getText());
      }

View on GitHub (pinned to 9b90983fd2)