# Is this a bug at DateHistogram aggregation?

**URL:** https://discuss.elastic.co/t/is-this-a-bug-at-datehistogram-aggregation/383069
**Category:** Elasticsearch
**Created:** [October 29, 2025, 10:20am UTC](https://discuss.elastic.co/t/is-this-a-bug-at-datehistogram-aggregation/383069 "2025-10-29T10:20:45Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![vicety](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vicety/32/145542_2.png) [@vicety](https://discuss.elastic.co/u/vicety)
#### Post date: [October 29, 2025, 10:20am UTC](https://discuss.elastic.co/t/is-this-a-bug-at-datehistogram-aggregation/383069/1 "2025-10-29T10:20:46Z")

</div>

I was reading the source code of histogram aggregation and found this.

```auto
// server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalDateHistogram.java 

// (line 358)
    private void addEmptyBuckets(List<Bucket> list, AggregationReduceContext reduceContext) {
        /*
         * Make sure we have space for the empty buckets we're going to add by
         * counting all of the empties we plan to add and firing them into
         * consumeBucketsAndMaybeBreak.
         */
        class Counter implements LongConsumer {
            private int size;

            @Override
            public void accept(long key) {
                size++;
                if (size >= REPORT_EMPTY_EVERY) {
                    reduceContext.consumeBucketsAndMaybeBreak(size);
                    size = 0;
                }
            }
        }
        Counter counter = new Counter();
        iterateEmptyBuckets(list, list.listIterator(), counter);
        reduceContext.consumeBucketsAndMaybeBreak(counter.size);

        InternalAggregations reducedEmptySubAggs = InternalAggregations.reduce(emptyBucketInfo.subAggregations, reduceContext);
        ListIterator<Bucket> iter = list.listIterator();
        iterateEmptyBuckets(list, iter, new LongConsumer() {
            private int size = 0;

            @Override
            public void accept(long key) {
                size++;
                if (size >= REPORT_EMPTY_EVERY) {
                    reduceContext.consumeBucketsAndMaybeBreak(size);
                    size = 0;
                }
                iter.add(new InternalDateHistogram.Bucket(key, 0, format, reducedEmptySubAggs));
            }
        });
    }

```

It seems that the first `iterateEmptyBuckets` call is to make sure we won’t have too many empty buckets that causes OOM (by calling `reduceContext.consumeBucketsAndMaybeBreak`), and the second `iterateEmptyBuckets` call is the one that actually puts empty buckets into the result.

My question is, why is `reduceContext.consumeBucketsAndMaybeBreak` called in both the first and second call to `iterateEmptyBuckets`? Shouldn’t we only keep this in the first call to `iterateEmptyBuckets` but not the second one?
