{"record":{"id":"7b366f961e583df1","repo":"apache/druid","slug":"at-least-2-bins-expected-7b366f","errorCode":null,"errorMessage":"at least 2 bins expected","messagePattern":"at least 2 bins expected","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":500,"severity":"error","filePath":"extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/kll/KllFloatsSketchToHistogramPostAggregator.java","lineNumber":73,"sourceCode":"      @JsonProperty(\"numBins\") @Nullable final Integer numBins)\n  {\n    this.name = Preconditions.checkNotNull(name, \"name is null\");\n    this.field = Preconditions.checkNotNull(field, \"field is null\");\n    this.splitPoints = splitPoints;\n    this.numBins = numBins;\n    if (splitPoints != null && numBins != null) {\n      throw new IAE(\"Cannot accept both 'splitPoints' and 'numBins'\");\n    }\n  }\n\n  @Override\n  public Object compute(final Map<String, Object> combinedAggregators)\n  {\n    final KllFloatsSketch sketch = (KllFloatsSketch) field.compute(combinedAggregators);\n    final int numBins = this.splitPoints != null ? this.splitPoints.length + 1 :\n        (this.numBins != null ? this.numBins.intValue() : DEFAULT_NUM_BINS);\n    if (numBins < 2) {\n      throw new IAE(\"at least 2 bins expected\");\n    }\n    if (sketch.isEmpty()) {\n      final double[] histogram = new double[numBins];\n      Arrays.fill(histogram, Double.NaN);\n      return histogram;\n    }\n    final float[] splitPoints;\n    if (this.splitPoints != null) {\n      splitPoints = this.splitPoints;\n    } else {\n      final float min = sketch.getMinItem();\n      final float max = sketch.getMaxItem();\n      if (min == max) {\n        // if min is equal to max, we can't create an array of equally spaced points.\n        // all values would go into the first bucket anyway, and the remaining\n        // buckets are left as zero.\n        final double[] histogram = new double[numBins];\n        histogram[0] = sketch.getN();","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/kll/KllFloatsSketchToHistogramPostAggregator.java#L55-L91","documentation":"At compute time the histogram post-aggregator resolves the effective bin count (splitPoints.length + 1, numBins, or the default) and requires at least 2 bins — a histogram with 0 or 1 bins is meaningless. It throws IAE when the resolved count is below 2.","triggerScenarios":"Passing numBins <= 1, or supplying an empty splitPoints array (length 0, resolving to 1 bin). Note numBins=0/null falls back to the default and is fine; only numBins=1 or empty splitPoints trigger this.","commonSituations":"Dynamic query builders that compute numBins from user input or a formula that can evaluate to 1, or passing an empty array for splitPoints instead of null.","solutions":["Set numBins to at least 2 in the post-aggregator spec.","If using splitPoints, provide at least one split point (yielding 2 bins); pass null rather than an empty array if you want the default binning.","Clamp or validate the bin count in the calling code before building the query."],"exampleFix":"// before\nint numBins = userBins; // could be 1\n\n// after\nint numBins = Math.max(2, userBins);","handlingStrategy":"validation","validationCode":"int numBins = splitPoints != null ? splitPoints.length + 1 : (numBinsOpt != null ? numBinsOpt : 10);\nif (numBins < 2) throw new IllegalArgumentException(\"numBins must be >= 2\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp user-supplied bin counts to >= 2.","Pass null instead of an empty splitPoints array when you want default binning.","Validate formulas that compute numBins dynamically."],"tags":["druid","datasketches","kll","post-aggregator","validation"],"backgroundTag":"invalid-argument-value","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"}