{"record":{"id":"d4b89ccaf5017d85","repo":"apache/druid","slug":"histogramaggregator-does-not-support-getlong","errorCode":null,"errorMessage":"HistogramAggregator does not support getLong()","messagePattern":"HistogramAggregator does not support getLong\\(\\)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/aggregation/HistogramAggregator.java","lineNumber":74,"sourceCode":"    histogram.offer(selector.getFloat());\n  }\n\n  @Override\n  public Object get()\n  {\n    return this.histogram;\n  }\n\n  @Override\n  public float getFloat()\n  {\n    throw new UnsupportedOperationException(\"HistogramAggregator does not support getFloat()\");\n  }\n\n  @Override\n  public long getLong()\n  {\n    throw new UnsupportedOperationException(\"HistogramAggregator does not support getLong()\");\n  }\n\n  @Override\n  public double getDouble()\n  {\n    throw new UnsupportedOperationException(\"HistogramAggregator does not support getDouble()\");\n  }\n\n  @Override\n  public void close()\n  {\n    // no resources to cleanup\n  }\n}\n","sourceCodeStart":56,"sourceCodeEnd":89,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/aggregation/HistogramAggregator.java#L56-L89","documentation":"Apache Druid's HistogramAggregator only supports its native aggregate type (a Histogram object) via agg.get(). The Aggregator interface exposes getFloat/getLong/getDouble, and HistogramAggregator deliberately throws UnsupportedOperationException from these because a histogram cannot be meaningfully coerced to a single scalar numeric value. The error means your query is asking a 'histogram' aggregator to deliver its result as a long.","triggerScenarios":"A native query or SQL query uses a 'histogram' aggregator with a post-aggregator, SQL layer, or result format that requests the metric as a LONG (e.g. SQL type coercion of the aggregation output to BIGINT, or 'longLast'/'longFirst' style accessors, or a numeric post-aggregator applied to a histogram metric).","commonSituations":"Running SQL like SELECT HISTOGRAM(col) ... CAST/numeric aggregation over it; using a histogram aggregator in a groupBy with a numeric post-aggregator (arithmetic, greatest, etc.); older configs migrating from numeric aggregators to histogram; query layers that assume every aggregator can yield long/double/float.","solutions":["Do not access the histogram aggregator's result via getLong(); use agg.get() which returns the Histogram object.","Use an aggregator whose output is numeric (e.g. longSum, doubleSum, filtered aggregators) if a LONG result is needed.","If you need statistics from the histogram, wrap it with post-aggregators designed for histograms or compute bins after retrieval.","If the metric was misconfigured as 'histogram' in the ingestion/query spec, correct the aggregator type."],"exampleFix":"// before\nAggregatorFactory factory = new HistogramAggregatorFactory(\"h\", \"col\", breaks);\nlong value = selectorFor(\"h\").getLong(); // throws\n// after\nHistogram hist = (Histogram) agg.get();\nlong approxCount = (long) hist.count();","handlingStrategy":"type-guard","validationCode":"if (aggregator instanceof HistogramAggregator) {\n  // must use agg.get(); never call getLong()\n}","typeGuard":"boolean supportsNumeric(Aggregator agg) {\n  return !(agg instanceof HistogramAggregator);\n}","tryCatchPattern":"try {\n  value = agg.getLong();\n} catch (UnsupportedOperationException e) {\n  Histogram h = (Histogram) agg.get();\n}","preventionTips":["Know each aggregator's output type before selecting an accessor","Never apply numeric post-aggregators to 'histogram' metrics","Check the AggregatorFactory's getType() at query-construction time","Use agg.get() as the universal accessor for object-typed aggregators"],"tags":["druid","aggregation","unsupported-operation","histogram"],"backgroundTag":"unsupported-operation","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"}