{"record":{"id":"c11050e065bce081","repo":"apache/druid","slug":"failed-to-parse-array-element-s-as-a-long","errorCode":null,"errorMessage":"Failed to parse array element %s as a long","messagePattern":"Failed to parse array element (.+?) as a long","errorType":"validation","errorClass":"RE","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/math/expr/ExprListenerImpl.java","lineNumber":457,"sourceCode":"          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      }\n    }\n    nodes.put(ctx, new ArrayExpr(ExpressionType.LONG_ARRAY, values));\n  }\n\n  @Override\n  public void exitExplicitLongArray(ExprParser.ExplicitLongArrayContext ctx)\n  {\n    Object[] values = new Object[ctx.numericElement().size()];\n    for (int i = 0; i < values.length; i++) {\n      if (ctx.numericElement(i).NULL() != null) {\n        values[i] = null;\n      } else if (ctx.numericElement(i).LONG() != null) {\n        values[i] = Numbers.parseLongObject(ctx.numericElement(i).LONG().getText());\n      } else if (ctx.numericElement(i).DOUBLE() != null) {\n        values[i] = Numbers.parseLongObject(ctx.numericElement(i).DOUBLE().getText());\n      } else {\n        throw new RE(\"Failed to parse array element %s as a long\", ctx.numericElement(i).getText());","sourceCodeStart":439,"sourceCodeEnd":475,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/math/expr/ExprListenerImpl.java#L439-L475","documentation":"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.","triggerScenarios":"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).","commonSituations":"Decimals or floats inside long arrays; numbers written with thousands separators; copy-pasted arrays containing quoted numbers.","solutions":["Correct the array literal so all elements are valid integers or NULL, e.g. ARRAY[1, 2, NULL].","Cast string/numeric elements explicitly (e.g. CAST(x AS BIGINT)) before building the array.","Check quoted vs unquoted values — quoted strings are not LONG tokens."],"exampleFix":"// before\nARRAY<LONG>[1, 2.5]\n// after\nARRAY<LONG>[1, 2]  // or ARRAY<DOUBLE>[1, 2.5]","handlingStrategy":"validation","validationCode":"// Java: pre-check long literals\nstatic boolean isValidLongElement(String e) {\n  if (\"NULL\".equals(e)) return true;\n  try { Long.parseLong(e); return true; } catch (NumberFormatException ex) { return false; }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Emit integers only inside ARRAY<LONG>","Truncate or round decimals upstream, or switch to ARRAY<DOUBLE>","Reject values with formatting (commas, spaces) before emitting","Cover typed long arrays in generated-expression tests"],"tags":["druid","expression-parser","array-literal","number-format"],"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"}