{"record":{"id":"375cc38c89f7d822","repo":"apache/druid","slug":"unknown-type-s-for-metric-s","errorCode":null,"errorMessage":"Unknown type[%s] for metric[%s]","messagePattern":"Unknown type\\[(.+?)\\] for metric\\[(.+?)\\]","errorType":"validation","errorClass":"IAE","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/topn/LegacyTopNMetricSpec.java","lineNumber":40,"sourceCode":"import com.fasterxml.jackson.annotation.JsonCreator;\nimport org.apache.druid.java.util.common.IAE;\n\nimport java.util.Map;\n\n/**\n */\npublic class LegacyTopNMetricSpec extends NumericTopNMetricSpec\n{\n  private static String convertValue(Object metric)\n  {\n    final String retVal;\n\n    if (metric instanceof String) {\n      retVal = (String) metric;\n    } else if (metric instanceof Map) {\n      retVal = (String) ((Map) metric).get(\"metric\");\n    } else {\n      throw new IAE(\"Unknown type[%s] for metric[%s]\", metric.getClass(), metric);\n    }\n\n    return retVal;\n  }\n\n  @JsonCreator\n  public LegacyTopNMetricSpec(Object metric)\n  {\n    super(convertValue(metric));\n  }\n}\n","sourceCodeStart":22,"sourceCodeEnd":52,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/topn/LegacyTopNMetricSpec.java#L22-L52","documentation":"LegacyTopNMetricSpec.getDimension converts the legacy topN 'metric' field into a dimension name string. It accepts either a plain String or a Map containing a 'metric' key; any other JSON shape (number, array, boolean, null-mapped object without the key handled elsewhere) hits the fallback IllegalArgumentException 'Unknown type[%s] for metric[%s]'. It exists because the legacy metric spec format is lenient but must ultimately resolve to a string.","triggerScenarios":"Calling convertValue (via LegacyDimensionSpec) with a topN metric field deserialized as a non-String, non-Map type, e.g. a numeric metric like \"metric\": 42, a list, or a JSON object missing the 'metric' key producing an unexpected shape in upstream deserialization.","commonSituations":"Hand-written native topN JSON queries where the metric was written as a number or nested array; clients generating query JSON programmatically with wrong types; queries migrated from older Druid versions where alternate metric encodings were tolerated.","solutions":["Change the query's topN 'metric' field to a plain string dimension name","If using an object form, ensure it is {\"type\":\"numeric\",\"metric\":\"<name>\"} or {\"type\":\"lexicographic\",\"metric\":\"<name>\"} with a string 'metric' value","Validate query JSON against the TopNQuery schema before submitting","If building queries in code, pass String instead of raw numbers/objects for the metric"],"exampleFix":"// before\n{\"queryType\":\"topN\",\"metric\": 42, ...}\n// after\n{\"queryType\":\"topN\",\"metric\":\"visits\", ...}","handlingStrategy":"validation","validationCode":"Object metric = queryJson.get(\"metric\");\nif (metric instanceof String) { /* ok */ }\nelse if (metric instanceof Map) {\n  Object m = ((Map<?, ?>) metric).get(\"metric\");\n  if (!(m instanceof String)) throw new IllegalArgumentException(\"metric must resolve to a string\");\n} else {\n  throw new IllegalArgumentException(\"topN metric must be a string or a map with a string 'metric' key\");\n}","typeGuard":"boolean isValidTopNMetric(Object metric) {\n  return metric instanceof String\n      || (metric instanceof Map && ((Map<?, ?>) metric).get(\"metric\") instanceof String);\n}","tryCatchPattern":null,"preventionTips":["Always emit topN 'metric' as a string or {\"type\":...,\"metric\":\"<string>\"}","Validate query JSON against the TopNQuery schema client-side before submission","Use Druid's query JSON deserialization round-trip in tests to catch malformed metric specs"],"tags":["query","topn","json-type-error","druid"],"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"}