apache/druid · error · NumberFormatException
Cannot parse string to long
Error message
Cannot parse string to long
What it means
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".
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.
Example fix
// before
Long l = Numbers.parseLongObject(str);
// after
Long l = null;
try {
l = Numbers.parseLongObject(str.trim());
} catch (NumberFormatException e) {
l = 0L; /* or log and skip */
} Defensive patterns
Strategy: try-catch
Validate before calling
boolean isLongString(String s) { return s != null && (Longs.tryParse(s.trim()) != null || Doubles.tryParse(s.trim()) != null); } Type guard
boolean isLongString(String s) { return s != null && Longs.tryParse(s.trim()) != null; } Try / catch
Long l;
try { l = Numbers.parseLongObject(s); } catch (NumberFormatException e) { LOG.warn("Unparseable long: %s", s); l = null; } Prevention
- 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
When it happens
Trigger: 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".
Common situations: Parsing string-typed dimension values or config values expected to be integral; timestamp/ID fields arriving with units or separators.
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
- Cannot parse string to double
- Failed to parse array element
- Invalid format or out of range of long
- Key[ ] should be a long, was[ ]
- Key[ ] should be an int, was[ ]
AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07).
Data as JSON: /api/errors/e16755e9851c01e0.
Report an issue: GitHub.
Appendix: source
Thrown at processing/src/main/java/org/apache/druid/java/util/common/Numbers.java:207
* Like {@link #tryParseLong} but does not produce a primitive and will explode if unable to produce a Long
* similar to {@link Long#parseLong}
*/
@Nullable
public static Long parseLongObject(@Nullable String val)
{
if (val == null) {
return null;
}
Long lobj = Longs.tryParse(val);
if (lobj != null) {
return lobj;
}
// try as a double, for "ddd.dd" , Longs.tryParse(..) returns null
Double dobj = Doubles.tryParse(val);
if (dobj != null) {
return dobj.longValue();
}
throw new NumberFormatException("Cannot parse string to long");
}
private Numbers()
{
}
}
View on GitHub (pinned to 9b90983fd2)