{"record":{"id":"02d9ee0611494b50","repo":"apache/druid","slug":"expected-key-s-to-be-an-integer-but-got-s","errorCode":null,"errorMessage":"Expected key [%s] to be an Integer, but got [%s]","messagePattern":"Expected key \\[(.+?)\\] to be an Integer, but got \\[(.+?)\\]","errorType":"validation","errorClass":"BadQueryContextException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/QueryContexts.java","lineNumber":383,"sourceCode":"    } else if (value instanceof String) {\n      try {\n        return Numbers.parseInt(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 Integer on request.\n        try {\n          return new BigDecimal((String) value).intValueExact();\n        }\n        catch (Exception nfe) {\n          // That didn't work either. Give up.\n          throw badValueException(key, \"in integer format\", value);\n        }\n      }\n    }\n\n    throw badTypeException(key, \"an Integer\", value);\n  }\n\n  /**\n   * Get the value of a context value as an {@code int}. The value is expected\n   * to be {@code null}, a string or a {@code Number} object.\n   */\n  public static int getAsInt(\n      final String key,\n      final Object value,\n      final int defaultValue\n  )\n  {\n    Integer val = getAsInt(key, value);\n    return val == null ? defaultValue : val;\n  }\n\n  @Nullable\n  public static Long getAsLong(String key, Object value)","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/QueryContexts.java#L365-L401","documentation":"QueryContexts.getAsInt(key, value) throws badTypeException with this message when the context value is neither null, a Number, nor a String — i.e. the type itself cannot even be attempted as an integer (Boolean, Map, List, etc.). Note this is distinct from the string-parse failure: here the value's class is simply wrong for an integer read.","triggerScenarios":"Calling QueryContexts.getAsInt(key, value) or query.getContextValue(key, int default) when the context map holds a Boolean, JSONArray, JSONObject, or other non-numeric, non-string object for an integer-typed context key.","commonSituations":"A client sends an integer context key as a JSON object or array (e.g. nested config pasted into the wrong key); a boolean flag accidentally placed under a numeric key; serialization frameworks that deserialized the value into a non-numeric type.","solutions":["Fix the query context JSON so the key holds a JSON number or numeric string.","If building contexts in code, put an Integer/Long: context.put(key, 100).","Verify the key name — a nested object under this key often means the config was placed under the wrong context key.","Catch the exception and fall back to the default value for the context key."],"exampleFix":"// before\n{\"maxScatterGatherBytes\": {\"bytes\": 100}}  // object -> IAE\n// after\n{\"maxScatterGatherBytes\": 100}","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, defaultInt);\n} catch (IllegalArgumentException e) {\n  LOG.warn(e, \"Wrong type for context key [%s], using default\", key);\n  return defaultInt;\n}","preventionTips":["Never place booleans, arrays, or nested objects under integer-typed context keys.","Use context.put(key, integerValue) with boxed numerics when building contexts in code.","Sanitize client-provided context maps against a per-key expected-type table."],"tags":["druid","query-context","type-mismatch","integer"],"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"}