{"record":{"id":"c9f9b1ec4faae049","repo":"apache/druid","slug":"expected-key-s-to-be-in-long-format-but-got","errorCode":null,"errorMessage":"Expected key [%s] to be in long format, but got [%s]","messagePattern":"Expected key \\[(.+?)\\] to be in long format, but got \\[(.+?)\\]","errorType":"validation","errorClass":"BadQueryContextException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/QueryContexts.java","lineNumber":420,"sourceCode":"  {\n    if (value == null) {\n      return null;\n    } else if (value instanceof Number) {\n      return ((Number) value).longValue();\n    } else if (value instanceof String) {\n      try {\n        return Numbers.parseLong(value);\n      }\n      catch (NumberFormatException ignored) {\n\n        // Attempt to handle trivial decimal values: 12.00, etc.\n        // This mimics how Jackson will convert \"12.00\" to a Long on request.\n        try {\n          return new BigDecimal((String) value).longValueExact();\n        }\n        catch (Exception nfe) {\n          // That didn't work either. Give up.\n          throw badValueException(key, \"in long format\", value);\n        }\n      }\n    }\n    throw badTypeException(key, \"a Long\", value);\n  }\n\n  /**\n   * Get the value of a context value as an {@code long}. The value is expected\n   * to be {@code null}, a string or a {@code Number} object.\n   */\n  public static long getAsLong(\n      final String key,\n      final Object value,\n      final long defaultValue\n  )\n  {\n    Long val = getAsLong(key, value);\n    return val == null ? defaultValue : val;","sourceCodeStart":402,"sourceCodeEnd":438,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/QueryContexts.java#L402-L438","documentation":"QueryContexts.getAsLong(key, value) throws badValueException with this message when the context value is a String that cannot be parsed as a long. The getter tries Numbers.parseLong, then BigDecimal.longValueExact() to accept trivial decimals like \"12.00\" (mirroring Jackson coercion); failure of both produces this error naming the key and raw value.","triggerScenarios":"Calling QueryContexts.getAsLong(key, value) or query.getContextValue(key, long default) with a String like \"abc\", \"12.5\", \"1e10\", empty string, or values with units/separators that are not exactly representable as a long.","commonSituations":"Byte-size or timestamp context values written as \"1GB\", \"2,000,000,000\", or fractional numbers where a plain long is required; copy-pasted config containing units; locale-specific number formatting.","solutions":["Change the context value to a plain long literal or JSON number: {\"key\": 9007199254740992} or \"9007199254740992\".","Use a HumanReadableBytes-typed context key (getAsHumanReadableBytes) if you need units like \"1GB\".","Strip units, commas, and whitespace before inserting the value.","Catch the exception around getContextValue and use the documented default."],"exampleFix":"// before\n{\"maxScatterGatherBytes\": \"800MB\"}  // units -> IAE\n// after\n{\"maxScatterGatherBytes\": 838860800}\n// or use a bytes-aware parameter elsewhere","handlingStrategy":"validation","validationCode":"Object v = context.get(key);\nif (v instanceof String) {\n  try { new java.math.BigDecimal((String) v).longValueExact(); }\n  catch (Exception e) { throw new IllegalArgumentException(\"Context key '\" + key + \"' must be a long, got: \" + v); }\n}","typeGuard":"boolean isParsableLong(Object v) {\n  if (!(v instanceof String)) return v instanceof Number || v == null;\n  try { new java.math.BigDecimal((String) v).longValueExact(); return true; } catch (Exception e) { return false; }\n}","tryCatchPattern":"try {\n  return query.getContextValue(key, defaultLong);\n} catch (IllegalArgumentException e) {\n  LOG.warn(e, \"Non-long value for context key [%s], using default\", key);\n  return defaultLong;\n}","preventionTips":["Send large numeric context values (byte limits, timeouts) as JSON numbers.","Use HumanReadableBytes context keys when users need to express sizes with units.","Avoid strings with units, commas, or decimals for long-typed keys."],"tags":["druid","query-context","long","parse-error"],"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-14T05:17:10.506Z"}