{"record":{"id":"dbdfdf2d1ceeae8e","repo":"apache/druid","slug":"failed-to-parse-array-element-s-as-a-s","errorCode":null,"errorMessage":"Failed to parse array element %s as a %s","messagePattern":"Failed to parse array element (.+?) as a (.+?)","errorType":"validation","errorClass":"RE","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/math/expr/ExprListenerImpl.java","lineNumber":440,"sourceCode":"        // if value is a string, escape quoting\n        final String toParse;\n        if (elementContext.STRING() != null) {\n          toParse = escapeStringLiteral(elementContext.STRING().getText());\n        } else {\n          toParse = elementContext.getText();\n        }\n        switch (type.getElementType().getType()) {\n          case LONG:\n            values[i] = Numbers.parseLongObject(toParse);\n            break;\n          case DOUBLE:\n            values[i] = Numbers.parseDoubleObject(toParse);\n            break;\n          case STRING:\n            values[i] = toParse;\n            break;\n          default:\n            throw new RE(\"Failed to parse array element %s as a %s\", toParse, type.getElementType().asTypeString());\n        }\n      }\n    }\n    nodes.put(ctx, new ArrayExpr(type, values));\n  }\n\n  @Override\n  public void exitLongArray(ExprParser.LongArrayContext ctx)\n  {\n    Object[] values = new Object[ctx.longElement().size()];\n    for (int i = 0; i < values.length; i++) {\n      if (ctx.longElement(i).NULL() != null) {\n        values[i] = null;\n      } else if (ctx.longElement(i).LONG() != null) {\n        values[i] = Long.parseLong(ctx.longElement(i).LONG().getText());\n      } else {\n        throw new RE(\"Failed to parse array element %s as a long\", ctx.longElement(i).getText());\n      }","sourceCodeStart":422,"sourceCodeEnd":458,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/math/expr/ExprListenerImpl.java#L422-L458","documentation":"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.","triggerScenarios":"Parsing ARRAY<LONG>[abc] or ARRAY<DOUBLE>[true] — elements that are neither NULL nor parseable as the declared type via Numbers.parseLongObject/parseDoubleObject.","commonSituations":"Mixing strings into numeric typed arrays; decimal values in ARRAY<LONG> that parseLongObject rejects; generated expressions from tools that do not enforce element types.","solutions":["Make each element match the declared array type or use NULL","Change the array type annotation to match the actual element values (e.g. ARRAY<STRING> for text)","Pre-convert values in the producing code before emitting the expression string"],"exampleFix":"// before\nARRAY<LONG>[1, '2', abc]\n// after\nARRAY<LONG>[1, 2]","handlingStrategy":"validation","validationCode":"// Java: verify each element parses as the declared type before building the literal\nstatic boolean elementsMatchDeclaredType(String declared, java.util.List<String> elems) {\n  for (String e : elems) {\n    if (e.equals(\"NULL\")) continue;\n    try {\n      switch (declared) {\n        case \"LONG\": Long.parseLong(e); break;\n        case \"DOUBLE\": Double.parseDouble(e); break;\n        case \"STRING\": if (!(e.startsWith(\"'\") && e.endsWith(\"'\"))) return false; break;\n        default: return false;\n      }\n    } catch (NumberFormatException nfe) { return false; }\n  }\n  return true;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Match element types to the declared ARRAY<T> annotation","Use NULL instead of placeholders for missing entries","Generate typed arrays from typed data, not mixed collections","Test expression generation with representative data samples"],"tags":["druid","expression-parser","array-literal","type-mismatch"],"backgroundTag":"invalid-argument-format","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}