{"record":{"id":"563cb2a6a43ea0a0","repo":"apache/druid","slug":"expected-key-s-to-be-a-float-but-got-s","errorCode":null,"errorMessage":"Expected key [%s] to be a Float, but got [%s]","messagePattern":"Expected key \\[(.+?)\\] to be a Float, but got \\[(.+?)\\]","errorType":"validation","errorClass":"BadQueryContextException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/QueryContexts.java","lineNumber":459,"sourceCode":"  /**\n   * Get the value of a context value as an {@code Float}. The value is expected\n   * to be {@code null}, a string or a {@code Number} object.\n   */\n  public static Float getAsFloat(final String key, final Object value)\n  {\n    if (value == null) {\n      return null;\n    } else if (value instanceof Number) {\n      return ((Number) value).floatValue();\n    } else if (value instanceof String) {\n      try {\n        return Float.parseFloat((String) value);\n      }\n      catch (NumberFormatException ignored) {\n        throw badValueException(key, \"in float format\", value);\n      }\n    }\n    throw badTypeException(key, \"a Float\", value);\n  }\n\n  public static float getAsFloat(\n      final String key,\n      final Object value,\n      final float defaultValue\n  )\n  {\n    Float val = getAsFloat(key, value);\n    return val == null ? defaultValue : val;\n  }\n\n  public static HumanReadableBytes getAsHumanReadableBytes(\n      final String key,\n      final Object value,\n      final HumanReadableBytes defaultValue\n  )\n  {","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/QueryContexts.java#L441-L477","documentation":"QueryContexts.getAsFloat(key, value) throws badTypeException with this message when the context value is neither null, a Number, nor a String — its class (Boolean, Map, List, ...) cannot be converted to a float. The exception reports the key and the value's actual runtime type.","triggerScenarios":"Calling QueryContexts.getAsFloat(key, value) or query.getContextValue(key, float default) when the context map stores a Boolean, JSON object/array, or other object under a float-typed context key.","commonSituations":"A boolean switch placed under a numeric threshold key; nested configuration objects accidentally assigned to a scalar context key; query builders passing raw JSON nodes into the context.","solutions":["Fix the query context JSON so the key holds a JSON number or numeric string.","In code, put a Float/Double: context.put(key, 0.5d).","Verify the key name to make sure the value wasn't meant for a different (object-typed) key.","Catch the exception and fall back to the default value."],"exampleFix":"// before\n{\"threshold\": [0.5]}  // array -> IAE\n// after\n{\"threshold\": 0.5}","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, defaultFloat);\n} catch (IllegalArgumentException e) {\n  LOG.warn(e, \"Wrong type for context key [%s], using default\", key);\n  return defaultFloat;\n}","preventionTips":["Do not put booleans, arrays, or objects under float-typed context keys.","Verify key names when a value has an unexpected shape.","Sanitize client-submitted contexts against an expected-type map before execution."],"tags":["druid","query-context","type-mismatch","float"],"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"}