Search in sources :

Example 21 with Aggregator

use of org.apache.druid.query.aggregation.Aggregator in project druid by druid-io.

the class FloatAnyAggregationTest method testFloatAnyCombiningAggregator.

@Test
public void testFloatAnyCombiningAggregator() {
    Aggregator agg = combiningAggFactory.factorize(colSelectorFactory);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    Float result = (Float) agg.get();
    Assert.assertEquals(objects[0], result, 0.0001);
    Assert.assertEquals(objects[0].longValue(), agg.getLong());
    Assert.assertEquals(objects[0], agg.getFloat(), 0.0001);
}
Also used : Aggregator(org.apache.druid.query.aggregation.Aggregator) BufferAggregator(org.apache.druid.query.aggregation.BufferAggregator) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 22 with Aggregator

use of org.apache.druid.query.aggregation.Aggregator in project druid by druid-io.

the class TimeseriesQueryEngine method processNonVectorized.

private Sequence<Result<TimeseriesResultValue>> processNonVectorized(final TimeseriesQuery query, final StorageAdapter adapter, @Nullable final Filter filter, final Interval queryInterval, final Granularity gran, final boolean descending) {
    final boolean skipEmptyBuckets = query.isSkipEmptyBuckets();
    final List<AggregatorFactory> aggregatorSpecs = query.getAggregatorSpecs();
    return QueryRunnerHelper.makeCursorBasedQuery(adapter, Collections.singletonList(queryInterval), filter, query.getVirtualColumns(), descending, gran, cursor -> {
        if (skipEmptyBuckets && cursor.isDone()) {
            return null;
        }
        Aggregator[] aggregators = new Aggregator[aggregatorSpecs.size()];
        String[] aggregatorNames = new String[aggregatorSpecs.size()];
        for (int i = 0; i < aggregatorSpecs.size(); i++) {
            aggregators[i] = aggregatorSpecs.get(i).factorize(cursor.getColumnSelectorFactory());
            aggregatorNames[i] = aggregatorSpecs.get(i).getName();
        }
        try {
            while (!cursor.isDone()) {
                for (Aggregator aggregator : aggregators) {
                    aggregator.aggregate();
                }
                cursor.advance();
            }
            TimeseriesResultBuilder bob = new TimeseriesResultBuilder(cursor.getTime());
            for (int i = 0; i < aggregatorSpecs.size(); i++) {
                bob.addMetric(aggregatorNames[i], aggregators[i].get());
            }
            return bob.build();
        } finally {
            // cleanup
            for (Aggregator agg : aggregators) {
                agg.close();
            }
        }
    });
}
Also used : Aggregator(org.apache.druid.query.aggregation.Aggregator) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory)

Example 23 with Aggregator

use of org.apache.druid.query.aggregation.Aggregator in project druid by druid-io.

the class TimeseriesQueryQueryToolChest method getNullTimeseriesResultValue.

private Result<TimeseriesResultValue> getNullTimeseriesResultValue(TimeseriesQuery query) {
    List<AggregatorFactory> aggregatorSpecs = query.getAggregatorSpecs();
    Aggregator[] aggregators = new Aggregator[aggregatorSpecs.size()];
    String[] aggregatorNames = new String[aggregatorSpecs.size()];
    RowSignature aggregatorsSignature = RowSignature.builder().addAggregators(aggregatorSpecs, RowSignature.Finalization.UNKNOWN).build();
    for (int i = 0; i < aggregatorSpecs.size(); i++) {
        aggregators[i] = aggregatorSpecs.get(i).factorize(RowBasedColumnSelectorFactory.create(RowAdapters.standardRow(), () -> new MapBasedRow(null, null), aggregatorsSignature, false));
        aggregatorNames[i] = aggregatorSpecs.get(i).getName();
    }
    final DateTime start = query.getIntervals().isEmpty() ? DateTimes.EPOCH : query.getIntervals().get(0).getStart();
    TimeseriesResultBuilder bob = new TimeseriesResultBuilder(start);
    for (int i = 0; i < aggregatorSpecs.size(); i++) {
        bob.addMetric(aggregatorNames[i], aggregators[i].get());
        aggregators[i].close();
    }
    return bob.build();
}
Also used : MapBasedRow(org.apache.druid.data.input.MapBasedRow) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) Aggregator(org.apache.druid.query.aggregation.Aggregator) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) RowSignature(org.apache.druid.segment.column.RowSignature) DateTime(org.joda.time.DateTime)

Example 24 with Aggregator

use of org.apache.druid.query.aggregation.Aggregator in project druid by druid-io.

the class StringTopNColumnAggregatesProcessor method scanAndAggregateWithCardinalityUnknown.

private long scanAndAggregateWithCardinalityUnknown(TopNQuery query, Cursor cursor, DimensionSelector selector) {
    long processedRows = 0;
    while (!cursor.isDone()) {
        final IndexedInts dimValues = selector.getRow();
        for (int i = 0, size = dimValues.size(); i < size; ++i) {
            final int dimIndex = dimValues.get(i);
            final Comparable<?> key = dimensionValueConverter.apply(selector.lookupName(dimIndex));
            Aggregator[] aggs = aggregatesStore.computeIfAbsent(key, k -> BaseTopNAlgorithm.makeAggregators(cursor, query.getAggregatorSpecs()));
            for (Aggregator aggregator : aggs) {
                aggregator.aggregate();
            }
        }
        cursor.advance();
        processedRows++;
    }
    return processedRows;
}
Also used : IndexedInts(org.apache.druid.segment.data.IndexedInts) Aggregator(org.apache.druid.query.aggregation.Aggregator)

Example 25 with Aggregator

use of org.apache.druid.query.aggregation.Aggregator in project druid by druid-io.

the class TimeExtractionTopNAlgorithm method scanAndAggregate.

@Override
protected long scanAndAggregate(TopNParams params, int[] dimValSelector, Map<Comparable<?>, Aggregator[]> aggregatesStore) {
    final Cursor cursor = params.getCursor();
    final DimensionSelector dimSelector = params.getDimSelector();
    long processedRows = 0;
    while (!cursor.isDone()) {
        final Comparable<?> key = dimensionValueConverter.apply(dimSelector.lookupName(dimSelector.getRow().get(0)));
        Aggregator[] theAggregators = aggregatesStore.computeIfAbsent(key, k -> makeAggregators(cursor, query.getAggregatorSpecs()));
        for (Aggregator aggregator : theAggregators) {
            aggregator.aggregate();
        }
        cursor.advance();
        processedRows++;
    }
    return processedRows;
}
Also used : DimensionSelector(org.apache.druid.segment.DimensionSelector) Aggregator(org.apache.druid.query.aggregation.Aggregator) Cursor(org.apache.druid.segment.Cursor)

Aggregations

Aggregator (org.apache.druid.query.aggregation.Aggregator)63 Test (org.junit.Test)50 BufferAggregator (org.apache.druid.query.aggregation.BufferAggregator)35 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)30 Pair (org.apache.druid.java.util.common.Pair)24 SerializablePair (org.apache.druid.collections.SerializablePair)18 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)18 HashMap (java.util.HashMap)12 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)12 TestDoubleColumnSelectorImpl (org.apache.druid.query.aggregation.TestDoubleColumnSelectorImpl)9 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)7 SerializablePairLongString (org.apache.druid.query.aggregation.SerializablePairLongString)6 TestObjectColumnSelector (org.apache.druid.query.aggregation.TestObjectColumnSelector)4 ArrayList (java.util.ArrayList)3 MapBasedInputRow (org.apache.druid.data.input.MapBasedInputRow)3 GroupByQueryRunnerTest (org.apache.druid.query.groupby.GroupByQueryRunnerTest)3 Cursor (org.apache.druid.segment.Cursor)3 InputRow (org.apache.druid.data.input.InputRow)2 MapBasedRow (org.apache.druid.data.input.MapBasedRow)2 ParseException (org.apache.druid.java.util.common.parsers.ParseException)2