{"record":{"id":"cfbce57e8dea0bee","repo":"apache/druid","slug":"expected-key-s-to-be-a-long-but-got-s","errorCode":null,"errorMessage":"Expected key [%s] to be a Long, but got [%s]","messagePattern":"Expected key \\[(.+?)\\] to be a Long, but got \\[(.+?)\\]","errorType":"validation","errorClass":"BadQueryContextException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/QueryContexts.java","lineNumber":424,"sourceCode":"      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;\n  }\n\n  /**\n   * Get the value of a context value as an {@code Float}. The value is expected","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/QueryContexts.java#L406-L442","documentation":"QueryContexts.getAsLong(key, value) throws badTypeException with this message when the context value's class cannot be interpreted as a Long at all — it is neither null, a Number, nor a String (e.g. Boolean, Map, List). The exception includes the key and the value's actual type.","triggerScenarios":"Calling QueryContexts.getAsLong(key, value) or query.getContextValue(key, long default) when the context map stores a Boolean, JSON object/array, or other non-numeric non-string object under a long-typed context key.","commonSituations":"A boolean or nested-config value placed under a numeric key by mistake; clients sending JSON objects where a number is expected; earlier code paths that stored raw deserialized JSON trees into the context.","solutions":["Correct the context JSON so the key holds a JSON number or numeric string.","In code, put a Long/Integer: context.put(key, 123456789L).","Check for key-name typos that cause a value of the wrong shape to land under this key.","Catch the exception and fall back to the default value."],"exampleFix":"// before\n{\"timeout\": false}   // boolean under numeric key -> IAE\n// after\n{\"timeout\": 30000}","handlingStrategy":"type-guard","validationCode":"Object v = context.get(key);\nif (v != null && !(v instanceof Number) && !(v instanceof String)) {\n  throw new IllegalArgumentException(\"Context key '\" + key + \"' must be numeric, got: \" + v.getClass().getSimpleName());\n}","typeGuard":"boolean isContextNumber(Object v) { return v == null || v instanceof Number || v instanceof String; }","tryCatchPattern":"try {\n  return query.getContextValue(key, defaultLong);\n} catch (IllegalArgumentException e) {\n  LOG.warn(e, \"Wrong type for context key [%s], using default\", key);\n  return defaultLong;\n}","preventionTips":["Keep boolean flags and numeric limits under distinct, correctly named context keys.","Validate deserialized context maps before handing them to query execution.","Use typed context builders rather than raw Map<String, Object> puts."],"tags":["druid","query-context","type-mismatch","long"],"backgroundTag":"type-mismatch","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"}