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);
}
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();
}
}
});
}
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();
}
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;
}
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;
}
Aggregations