@Nonnull
    @Override
    public CreatedAggregations<AggregationBuilder> doCreateAggregation(Direction direction, String name, Pivot pivot, Time timeSpec, OSGeneratedQueryContext queryContext, Query query) {
        AggregationBuilder root = null;
        AggregationBuilder leaf = null;
        final Interval interval = timeSpec.interval();
        final TimeRange timerange = query.timerange();
        if (interval instanceof AutoInterval autoInterval
                && isAllMessages(timerange)) {
            for (String timeField : timeSpec.fields()) {
                final AutoDateHistogramAggregationBuilder builder = new AutoDateHistogramAggregationBuilder(name)
                        .field(timeField)
                        .setNumBuckets((int) (BASE_NUM_BUCKETS / autoInterval.scaling()))
                        .format(DATE_TIME_FORMAT);

                if (root == null && leaf == null) {
                    root = builder;
                    leaf = builder;
                } else {
                    leaf.subAggregation(builder);
                    leaf = builder;
                }
            }
        } else {
            for (String timeField : timeSpec.fields()) {
                final DateHistogramInterval dateHistogramInterval = new DateHistogramInterval(interval.toDateInterval(query.effectiveTimeRange(pivot)).toString());
                final List<BucketOrder> ordering = orderListForPivot(pivot, queryContext, defaultOrder);
                final DateHistogramAggregationBuilder builder = AggregationBuilders.dateHistogram(name)
                        .field(timeField)
                        .order(ordering)
                        .format(DATE_TIME_FORMAT);

                setInterval(builder, dateHistogramInterval);

                if (root == null && leaf == null) {
                    root = builder;
                    leaf = builder;
                } else {
                    leaf.subAggregation(builder);
                    leaf = builder;
                }
            }
        }

        return CreatedAggregations.create(root, leaf);
    }
@Test
    public void autoDateHistogramAggregationBuilderUsedForAutoIntervalAndAllMessages() {
        final AutoInterval interval = mock(AutoInterval.class);
        final RelativeRange allMessagesRange = RelativeRange.allTime();
        doReturn(allMessagesRange).when(query).timerange();
        when(time.interval()).thenReturn(interval);

        final BucketSpecHandler.CreatedAggregations<AggregationBuilder> createdAggregations = this.osTimeHandler.doCreateAggregation(BucketSpecHandler.Direction.Row, "foobar", pivot, time, queryContext, query);
        assertEquals(createdAggregations.root(), createdAggregations.leaf());
        assertTrue(createdAggregations.root() instanceof AutoDateHistogramAggregationBuilder);
        assertEquals("foobar", createdAggregations.root().getName());
        assertEquals("foobar", ((AutoDateHistogramAggregationBuilder) createdAggregations.root()).field());
        assertEquals(DATE_TIME_FORMAT, ((AutoDateHistogramAggregationBuilder) createdAggregations.root()).format());

    }