Search in sources :

Example 56 with Aggregator

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

the class DoublesSketchToHistogramPostAggregatorTest method numBins.

@Test
public void numBins() {
    final double[] values = new double[] { 1, 2, 3, 4, 5, 6 };
    final TestDoubleColumnSelectorImpl selector = new TestDoubleColumnSelectorImpl(values);
    final Aggregator agg = new DoublesSketchBuildAggregator(selector, 8);
    // noinspection ForLoopReplaceableByForEach
    for (int i = 0; i < values.length; i++) {
        agg.aggregate();
        selector.increment();
    }
    final Map<String, Object> fields = new HashMap<>();
    fields.put("sketch", agg.get());
    final PostAggregator postAgg = new DoublesSketchToHistogramPostAggregator("histogram", new FieldAccessPostAggregator("field", "sketch"), null, // two bins of equal mass
    2);
    final double[] histogram = (double[]) postAgg.compute(fields);
    Assert.assertNotNull(histogram);
    Assert.assertEquals(2, histogram.length);
    Assert.assertEquals(3.0, histogram[0], 0);
    Assert.assertEquals(3.0, histogram[1], 0);
}
Also used : FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) HashMap(java.util.HashMap) Aggregator(org.apache.druid.query.aggregation.Aggregator) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) TestDoubleColumnSelectorImpl(org.apache.druid.query.aggregation.TestDoubleColumnSelectorImpl) Test(org.junit.Test)

Example 57 with Aggregator

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

the class DoublesSketchToQuantilesPostAggregatorTest method normalCase.

@Test
public void normalCase() {
    final double[] values = new double[] { 1, 2, 3, 4, 5 };
    final TestDoubleColumnSelectorImpl selector = new TestDoubleColumnSelectorImpl(values);
    final Aggregator agg = new DoublesSketchBuildAggregator(selector, 8);
    // noinspection ForLoopReplaceableByForEach
    for (int i = 0; i < values.length; i++) {
        agg.aggregate();
        selector.increment();
    }
    final Map<String, Object> fields = new HashMap<>();
    fields.put("sketch", agg.get());
    final PostAggregator postAgg = new DoublesSketchToQuantilesPostAggregator("quantiles", new FieldAccessPostAggregator("field", "sketch"), new double[] { 0, 0.5, 1 });
    final double[] quantiles = (double[]) postAgg.compute(fields);
    Assert.assertNotNull(quantiles);
    Assert.assertEquals(3, quantiles.length);
    Assert.assertEquals(1.0, quantiles[0], 0);
    Assert.assertEquals(3.0, quantiles[1], 0);
    Assert.assertEquals(5.0, quantiles[2], 0);
}
Also used : FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) HashMap(java.util.HashMap) Aggregator(org.apache.druid.query.aggregation.Aggregator) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) TestDoubleColumnSelectorImpl(org.apache.druid.query.aggregation.TestDoubleColumnSelectorImpl) Test(org.junit.Test)

Example 58 with Aggregator

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

the class LongAnyAggregationTest method testLongAnyAggregator.

@Test
public void testLongAnyAggregator() {
    Aggregator agg = longAnyAggFactory.factorize(colSelectorFactory);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    Long result = (Long) agg.get();
    Assert.assertEquals((Long) longs[0], result);
    Assert.assertEquals((long) longs[0], agg.getLong());
    Assert.assertEquals(longs[0], agg.getLong(), 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 59 with Aggregator

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

the class StringAnyAggregationTest method testStringAnyAggregatorWithNullFirst.

@Test
public void testStringAnyAggregatorWithNullFirst() {
    valueSelector = new TestObjectColumnSelector<>(stringsWithNullFirst);
    colSelectorFactory = EasyMock.createMock(ColumnSelectorFactory.class);
    EasyMock.expect(colSelectorFactory.getColumnCapabilities("nilly")).andReturn(new ColumnCapabilitiesImpl().setType(ColumnType.STRING));
    EasyMock.expect(colSelectorFactory.makeColumnValueSelector("nilly")).andReturn(valueSelector);
    EasyMock.replay(colSelectorFactory);
    Aggregator agg = stringAnyAggFactory.factorize(colSelectorFactory);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    String result = (String) agg.get();
    Assert.assertNull(result);
}
Also used : ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) BufferAggregator(org.apache.druid.query.aggregation.BufferAggregator) Aggregator(org.apache.druid.query.aggregation.Aggregator) ColumnCapabilitiesImpl(org.apache.druid.segment.column.ColumnCapabilitiesImpl) Test(org.junit.Test)

Example 60 with Aggregator

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

the class StringAnyAggregationTest method testStringAnyCombiningAggregator.

@Test
public void testStringAnyCombiningAggregator() {
    Aggregator agg = combiningAggFactory.factorize(colSelectorFactory);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    aggregate(agg);
    String result = (String) agg.get();
    Assert.assertEquals(strings[0], result);
    Assert.assertEquals(strings[0], result);
}
Also used : BufferAggregator(org.apache.druid.query.aggregation.BufferAggregator) Aggregator(org.apache.druid.query.aggregation.Aggregator) 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