Search in sources :

Example 1 with Aggregator

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

the class BaseTopNAlgorithm method makeAggregators.

public static Aggregator[] makeAggregators(Cursor cursor, List<AggregatorFactory> aggregatorSpecs) {
    Aggregator[] aggregators = new Aggregator[aggregatorSpecs.size()];
    int aggregatorIndex = 0;
    for (AggregatorFactory spec : aggregatorSpecs) {
        aggregators[aggregatorIndex] = spec.factorize(cursor.getColumnSelectorFactory());
        ++aggregatorIndex;
    }
    return aggregators;
}
Also used : Aggregator(org.apache.druid.query.aggregation.Aggregator) BufferAggregator(org.apache.druid.query.aggregation.BufferAggregator) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory)

Example 2 with Aggregator

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

the class NullableNumericTopNColumnAggregatesProcessor method scanAndAggregate.

@Override
public long scanAndAggregate(TopNQuery query, Selector selector, Cursor cursor, Aggregator[][] rowSelector) {
    long processedRows = 0;
    while (!cursor.isDone()) {
        if (hasNulls && selector.isNull()) {
            if (nullValueAggregates == null) {
                nullValueAggregates = BaseTopNAlgorithm.makeAggregators(cursor, query.getAggregatorSpecs());
            }
            for (Aggregator aggregator : nullValueAggregates) {
                aggregator.aggregate();
            }
        } else {
            Aggregator[] valueAggregates = getValueAggregators(query, selector, cursor);
            for (Aggregator aggregator : valueAggregates) {
                aggregator.aggregate();
            }
        }
        cursor.advance();
        processedRows++;
    }
    return processedRows;
}
Also used : Aggregator(org.apache.druid.query.aggregation.Aggregator)

Example 3 with Aggregator

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

the class LongLastAggregationTest method testLongLastCombiningAggregator.

@Test
public void testLongLastCombiningAggregator() {
    Aggregator agg = combiningAggFactory.factorize(colSelectorFactory);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    Pair<Long, Long> result = (Pair<Long, Long>) agg.get();
    Pair<Long, Long> expected = (Pair<Long, Long>) pairs[2];
    Assert.assertEquals(expected.lhs, result.lhs);
    Assert.assertEquals(expected.rhs, result.rhs);
    Assert.assertEquals(expected.rhs.longValue(), agg.getLong());
    Assert.assertEquals(expected.rhs, agg.getFloat(), 1);
}
Also used : Aggregator(org.apache.druid.query.aggregation.Aggregator) BufferAggregator(org.apache.druid.query.aggregation.BufferAggregator) SerializablePair(org.apache.druid.collections.SerializablePair) Pair(org.apache.druid.java.util.common.Pair) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 4 with Aggregator

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

the class DoubleMeanAggregationTest method testAggregateWithSize.

@Test
public void testAggregateWithSize() {
    Double[] values = new Double[] { 3.0, 1.0, 2.0 };
    TestObjectColumnSelector<Double> columnValueSelector = new TestObjectColumnSelector<>(values);
    ColumnSelectorFactory colSelectorFactory = EasyMock.mock(ColumnSelectorFactory.class);
    EasyMock.expect(colSelectorFactory.makeColumnValueSelector(EasyMock.anyString())).andReturn(columnValueSelector).anyTimes();
    EasyMock.replay(colSelectorFactory);
    DoubleMeanAggregatorFactory aggregatorFactory = new DoubleMeanAggregatorFactory("name", "fieldName");
    AggregatorAndSize aggregatorAndSize = aggregatorFactory.factorizeWithSize(colSelectorFactory);
    Assert.assertEquals(aggregatorFactory.getMaxIntermediateSize(), aggregatorAndSize.getInitialSizeBytes());
    Assert.assertTrue(aggregatorAndSize.getAggregator() instanceof DoubleMeanAggregator);
    Aggregator aggregator = aggregatorAndSize.getAggregator();
    for (int i = 0; i < values.length; ++i) {
        long sizeDelta = aggregator.aggregateWithSize();
        Assert.assertEquals(0L, sizeDelta);
        columnValueSelector.increment();
    }
    DoubleMeanHolder meanHolder = (DoubleMeanHolder) aggregator.get();
    Assert.assertEquals(2.0, meanHolder.mean(), 0.0);
}
Also used : AggregatorAndSize(org.apache.druid.query.aggregation.AggregatorAndSize) TestObjectColumnSelector(org.apache.druid.query.aggregation.TestObjectColumnSelector) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) Aggregator(org.apache.druid.query.aggregation.Aggregator) Test(org.junit.Test)

Example 5 with Aggregator

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

the class FloatLastAggregationTest method testFloatLastAggregator.

@Test
public void testFloatLastAggregator() {
    Aggregator agg = floatLastAggregatorFactory.factorize(colSelectorFactory);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    Pair<Long, Float> result = (Pair<Long, Float>) agg.get();
    Assert.assertEquals(times[0], result.lhs.longValue());
    Assert.assertEquals(floats[0], result.rhs, 0.0001);
    Assert.assertEquals((long) floats[0], agg.getLong());
    Assert.assertEquals(floats[0], agg.getFloat(), 0.0001);
}
Also used : Aggregator(org.apache.druid.query.aggregation.Aggregator) BufferAggregator(org.apache.druid.query.aggregation.BufferAggregator) SerializablePair(org.apache.druid.collections.SerializablePair) Pair(org.apache.druid.java.util.common.Pair) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

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