{"record":{"id":"e16755e9851c01e0","repo":"apache/druid","slug":"cannot-parse-string-to-long","errorCode":null,"errorMessage":"Cannot parse string to long","messagePattern":"Cannot parse string to long","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/java/util/common/Numbers.java","lineNumber":207,"sourceCode":"   * Like {@link #tryParseLong} but does not produce a primitive and will explode if unable to produce a Long\n   * similar to {@link Long#parseLong}\n   */\n  @Nullable\n  public static Long parseLongObject(@Nullable String val)\n  {\n    if (val == null) {\n      return null;\n    }\n    Long lobj = Longs.tryParse(val);\n    if (lobj != null) {\n      return lobj;\n    }\n    // try as a double, for \"ddd.dd\" , Longs.tryParse(..) returns null\n    Double dobj = Doubles.tryParse(val);\n    if (dobj != null) {\n      return dobj.longValue();\n    }\n    throw new NumberFormatException(\"Cannot parse string to long\");\n  }\n\n  private Numbers()\n  {\n  }\n}\n","sourceCodeStart":189,"sourceCodeEnd":214,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/java/util/common/Numbers.java#L189-L214","documentation":"Numbers.parseLongObject(String) parses a String to a boxed Long: it first tries Longs.tryParse, then falls back to parsing as a double and truncating via longValue() (to support \"ddd.dd\"). If neither works, it throws NumberFormatException \"Cannot parse string to long\".","triggerScenarios":"Calling Numbers.parseLongObject(s) with a string that is neither an integer nor a double, e.g. \"abc\", \"\", \"12abc\", or numbers with groupings like \"1,000,000\".","commonSituations":"Parsing string-typed dimension values or config values expected to be integral; timestamp/ID fields arriving with units or separators.","solutions":["Normalize the input first: trim, strip grouping commas and units, then parse.","Use the lenient Numbers.tryParseLong(s, nullValue) and check for the sentinel instead of throwing.","Catch NumberFormatException and substitute a default/null.","Correct upstream data so the field is a plain integer string."],"exampleFix":"// before\nLong l = Numbers.parseLongObject(str);\n// after\nLong l = null;\ntry {\n  l = Numbers.parseLongObject(str.trim());\n} catch (NumberFormatException e) {\n  l = 0L; /* or log and skip */\n}","handlingStrategy":"try-catch","validationCode":"boolean isLongString(String s) { return s != null && (Longs.tryParse(s.trim()) != null || Doubles.tryParse(s.trim()) != null); }","typeGuard":"boolean isLongString(String s) { return s != null && Longs.tryParse(s.trim()) != null; }","tryCatchPattern":"Long l;\ntry { l = Numbers.parseLongObject(s); } catch (NumberFormatException e) { LOG.warn(\"Unparseable long: %s\", s); l = null; }","preventionTips":["Strip grouping commas and units from numeric strings before parsing","Use tryParseLong with a sentinel for tolerant parsing","Enforce numeric typing at ingestion instead of at query time"],"tags":["numbers","number-format","parse-failure"],"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"}