{"record":{"id":"62102f074a9b128b","repo":"apache/druid","slug":"comparing-arrays-of-mean-values-is-not-supported","errorCode":null,"errorMessage":"Comparing arrays of mean values is not supported","messagePattern":"Comparing arrays of mean values is not supported","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/tuple/ArrayOfDoublesSketchToMeansPostAggregator.java","lineNumber":81,"sourceCode":"      for (int i = 0; i < values.length; i++) {\n        stats[i].addValue(values[i]);\n      }\n    }\n    final double[] means = new double[sketch.getNumValues()];\n    Arrays.setAll(means, i -> stats[i].getMean());\n    return means;\n  }\n\n  @Override\n  public ColumnType getType(ColumnInspector signature)\n  {\n    return ColumnType.DOUBLE_ARRAY;\n  }\n\n  @Override\n  public Comparator<double[]> getComparator()\n  {\n    throw new IAE(\"Comparing arrays of mean values is not supported\");\n  }\n\n  @Override\n  public byte[] getCacheKey()\n  {\n    return new CacheKeyBuilder(AggregatorUtil.ARRAY_OF_DOUBLES_SKETCH_TO_MEANS_CACHE_TYPE_ID)\n        .appendCacheable(getField())\n        .build();\n  }\n\n}\n","sourceCodeStart":63,"sourceCodeEnd":93,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/tuple/ArrayOfDoublesSketchToMeansPostAggregator.java#L63-L93","documentation":"ArrayOfDoublesSketchToMeansPostAggregator outputs a double[] of per-value-column means. Since multi-element mean arrays have no defined comparison, getComparator() throws IAE unconditionally; sorting on this post-aggregator is unsupported.","triggerScenarios":"Calling getComparator(), typically when results are ordered by this post-aggregator in a groupBy orderBy spec or when framework code asks for a comparator for every post-aggregator.","commonSituations":"Users attempting to rank rows by mean vectors; generic query builders that add sort specs on all post-aggregators.","solutions":["Sort by an individual mean extracted via a different post-aggregator or expression (e.g. element access)","Remove the ordering clause on this post-aggregator","Perform ranking in application code with a custom comparator"],"exampleFix":"// before\n.addOrderBy(new OrderByColumnSpec(\"means\"))\n// after\n.addOrderBy(new OrderByColumnSpec(\"meanOfFirstValue\")) // numeric post-agg/expression","handlingStrategy":"try-catch","validationCode":"if (orderByColumns.stream().anyMatch(c -> \"means\".equals(c.getDimension()))) {\n  throw new IllegalArgumentException(\"Cannot order by means array output\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  return queryClient.run(query);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"mean values\")) {\n    return runQueryWithAlternateOrdering(query, \"meanOfFirstValue\");\n  }\n  throw e;\n}","preventionTips":["Expose individual means via numeric post-aggregators/expressions if sorting is required","Never use array-producing post-aggregators as orderBy keys","Validate sort keys against post-aggregator output types before submission"],"tags":["druid","datasketches","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}