apache/druid · error · RE

Failed to parse array element

Error message

Failed to parse array element %s as a long

What it means

Raised in ExprListenerImpl.exitLongArray when an element of a native-expression LONG array literal (e.g. ARRAY[1,2,3]) is neither NULL nor a recognized LONG token, so the ANTLR listener cannot convert it to a long. The expression's grammar context contains an invalid element for a long array, indicating malformed or mistyped array input.

Solutions

  1. Correct the array literal so all elements are valid integers or NULL, e.g. ARRAY[1, 2, NULL].
  2. Cast string/numeric elements explicitly (e.g. CAST(x AS BIGINT)) before building the array.
  3. Check quoted vs unquoted values — quoted strings are not LONG tokens.

Example fix

// before
ARRAY<LONG>[1, 2.5]
// after
ARRAY<LONG>[1, 2]  // or ARRAY<DOUBLE>[1, 2.5]
Defensive patterns

Strategy: validation

Validate before calling

// Java: pre-check long literals
static boolean isValidLongElement(String e) {
  if ("NULL".equals(e)) return true;
  try { Long.parseLong(e); return true; } catch (NumberFormatException ex) { return false; }
}

Prevention

When it happens

Trigger: Parsing expressions like ARRAY<LONG>[1.5] handled by the long-array grammar path, or elements containing signs/whitespace formats Long.parseLong rejects (e.g. hex or trailing characters).

Common situations: Decimals or floats inside long arrays; numbers written with thousands separators; copy-pasted arrays containing quoted numbers.

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

Appendix: source

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

          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());
      }
    }
    nodes.put(ctx, new ArrayExpr(ExpressionType.LONG_ARRAY, values));
  }

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

View on GitHub (pinned to 9b90983fd2)