{"record":{"id":"9e45a44730eb6094","repo":"apache/druid","slug":"expected-key-s-to-be-of-type-s-but-got-s","errorCode":null,"errorMessage":"Expected key [%s] to be of type [%s], but got [%s]","messagePattern":"Expected key \\[(.+?)\\] to be of type \\[(.+?)\\], but got \\[(.+?)\\]","errorType":"validation","errorClass":"BadQueryContextException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/QueryContexts.java","lineNumber":581,"sourceCode":"        return Enum.valueOf(clazz, StringUtils.toUpperCase((String) value));\n      } else if (value instanceof Boolean) {\n        return Enum.valueOf(clazz, StringUtils.toUpperCase(String.valueOf(value)));\n      }\n    }\n    catch (IllegalArgumentException e) {\n      throw badValueException(\n          key,\n          StringUtils.format(\n              \"referring to one of the values [%s] of enum [%s]\",\n              Arrays.stream(clazz.getEnumConstants()).map(Enum::name).collect(\n                  Collectors.joining(\",\")),\n              clazz.getSimpleName()\n          ),\n          value\n      );\n    }\n\n    throw badTypeException(\n        key,\n        StringUtils.format(\"of type [%s]\", clazz.getSimpleName()),\n        value\n    );\n  }\n\n  public static BadQueryContextException badValueException(\n      final String key,\n      final String expected,\n      final Object actual\n  )\n  {\n    return new BadQueryContextException(\n        StringUtils.format(\n            \"Expected key [%s] to be %s, but got [%s]\",\n            key,\n            expected,\n            actual","sourceCodeStart":563,"sourceCodeEnd":599,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/QueryContexts.java#L563-L599","documentation":"QueryContexts.getAsEnum only converts String and Boolean context values to enums; any other type (number, map, list) reaches the fall-through badTypeException. Druid throws this BadQueryContextException because the context value's type cannot possibly represent an enum constant, so it aborts query submission with an explicit expected-vs-actual type message.","triggerScenarios":"Passing a non-string/non-boolean value for an enum-typed query context key, e.g. context {\"vectorize\": 1} or {\"someEnumKey\": {\"a\":1}} passed to QueryContexts.getEnum via DruidQuery.execute or a native query JSON body.","commonSituations":"Programmatic query builders using raw Object values for context; JSON APIs where numbers are submitted where strings are expected; templating tools that interpolate numbers (0/1) for enum flags.","solutions":["Quote the context value as a string matching an enum constant name, e.g. \"vectorize\": \"true\" instead of 1.","Use QueryContexts's typed setters or ensure your query builder serializes the field as a JSON string.","Change boolean-like numbers to real JSON booleans or enum constant names before submitting."],"exampleFix":"// before\ncontext.put(\"vectorize\", 1);\n// after\ncontext.put(\"vectorize\", \"true\");","handlingStrategy":"type-guard","validationCode":"// Java\nif (!(value instanceof String) && !(value instanceof Boolean)) {\n  throw new IllegalArgumentException(\"context enum values must be String or Boolean\");\n}","typeGuard":"static boolean isEnumConvertible(Object v) {\n  return v instanceof String || v instanceof Boolean;\n}","tryCatchPattern":"try {\n  QueryContexts.getEnum(context, key, MyEnum.class);\n} catch (BadQueryContextException e) {\n  log.error(\"Context key {} has wrong type: {}\", key, e.getMessage());\n}","preventionTips":["Serialize enum context values as JSON strings in query builders.","Avoid interpolating numbers for boolean/enum flags in templates.","Add unit tests asserting the JSON shape of emitted query context."],"tags":["druid","query-context","type-mismatch","validation"],"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"}