Search in sources :

Example 1 with CountAggregator

use of io.druid.query.aggregation.CountAggregator in project druid by druid-io.

the class LongLeastPostAggregatorTest method testComparator.

@Test
public void testComparator() {
    final String aggName = "rows";
    LongLeastPostAggregator leastPostAggregator;
    CountAggregator agg = new CountAggregator();
    Map<String, Object> metricValues = new HashMap<String, Object>();
    metricValues.put(aggName, agg.get());
    List<PostAggregator> postAggregatorList = Lists.newArrayList(new ConstantPostAggregator("roku", 2D), new FieldAccessPostAggregator("rows", aggName));
    leastPostAggregator = new LongLeastPostAggregator("leastPostAggregator", postAggregatorList);
    Comparator comp = leastPostAggregator.getComparator();
    Object before = leastPostAggregator.compute(metricValues);
    agg.aggregate();
    agg.aggregate();
    agg.aggregate();
    metricValues.put(aggName, agg.get());
    Object after = leastPostAggregator.compute(metricValues);
    Assert.assertEquals(-1, comp.compare(before, after));
    Assert.assertEquals(0, comp.compare(before, before));
    Assert.assertEquals(0, comp.compare(after, after));
    Assert.assertEquals(1, comp.compare(after, before));
}
Also used : PostAggregator(io.druid.query.aggregation.PostAggregator) HashMap(java.util.HashMap) CountAggregator(io.druid.query.aggregation.CountAggregator) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 2 with CountAggregator

use of io.druid.query.aggregation.CountAggregator in project druid by druid-io.

the class LongLeastPostAggregatorTest method testCompute.

@Test
public void testCompute() {
    final String aggName = "rows";
    LongLeastPostAggregator leastPostAggregator;
    CountAggregator agg = new CountAggregator();
    agg.aggregate();
    agg.aggregate();
    agg.aggregate();
    Map<String, Object> metricValues = new HashMap<String, Object>();
    metricValues.put(aggName, agg.get());
    List<PostAggregator> postAggregatorList = Lists.newArrayList(new ConstantPostAggregator("roku", 6D), new FieldAccessPostAggregator("rows", aggName));
    leastPostAggregator = new LongLeastPostAggregator("leastPostAggregator", postAggregatorList);
    Assert.assertEquals(Long.valueOf(3), leastPostAggregator.compute(metricValues));
}
Also used : PostAggregator(io.druid.query.aggregation.PostAggregator) HashMap(java.util.HashMap) CountAggregator(io.druid.query.aggregation.CountAggregator) Test(org.junit.Test)

Example 3 with CountAggregator

use of io.druid.query.aggregation.CountAggregator in project druid by druid-io.

the class DoubleLeastPostAggregatorTest method testComparator.

@Test
public void testComparator() {
    final String aggName = "rows";
    DoubleLeastPostAggregator leastPostAggregator;
    CountAggregator agg = new CountAggregator();
    Map<String, Object> metricValues = new HashMap<String, Object>();
    metricValues.put(aggName, agg.get());
    List<PostAggregator> postAggregatorList = Lists.newArrayList(new ConstantPostAggregator("roku", 2D), new FieldAccessPostAggregator("rows", aggName));
    leastPostAggregator = new DoubleLeastPostAggregator("least", postAggregatorList);
    Comparator comp = leastPostAggregator.getComparator();
    Object before = leastPostAggregator.compute(metricValues);
    agg.aggregate();
    agg.aggregate();
    agg.aggregate();
    metricValues.put(aggName, agg.get());
    Object after = leastPostAggregator.compute(metricValues);
    Assert.assertEquals(-1, comp.compare(before, after));
    Assert.assertEquals(0, comp.compare(before, before));
    Assert.assertEquals(0, comp.compare(after, after));
    Assert.assertEquals(1, comp.compare(after, before));
}
Also used : PostAggregator(io.druid.query.aggregation.PostAggregator) HashMap(java.util.HashMap) CountAggregator(io.druid.query.aggregation.CountAggregator) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 4 with CountAggregator

use of io.druid.query.aggregation.CountAggregator in project druid by druid-io.

the class FinalizingFieldAccessPostAggregatorTest method testComputeWithoutFinalizing.

@Test(expected = UnsupportedOperationException.class)
public void testComputeWithoutFinalizing() {
    String aggName = "rows";
    Aggregator agg = new CountAggregator();
    agg.aggregate();
    agg.aggregate();
    agg.aggregate();
    Map<String, Object> metricValues = Maps.newHashMap();
    metricValues.put(aggName, agg.get());
    FinalizingFieldAccessPostAggregator postAgg = new FinalizingFieldAccessPostAggregator("final_rows", aggName);
    Assert.assertEquals(new Long(3L), postAgg.compute(metricValues));
}
Also used : CountAggregator(io.druid.query.aggregation.CountAggregator) CountAggregator(io.druid.query.aggregation.CountAggregator) PostAggregator(io.druid.query.aggregation.PostAggregator) Aggregator(io.druid.query.aggregation.Aggregator) Test(org.junit.Test) GroupByQueryRunnerTest(io.druid.query.groupby.GroupByQueryRunnerTest)

Example 5 with CountAggregator

use of io.druid.query.aggregation.CountAggregator in project druid by druid-io.

the class SpecificSegmentQueryRunnerTest method testRetry2.

@SuppressWarnings("unchecked")
@Test
public void testRetry2() throws Exception {
    final ObjectMapper mapper = new DefaultObjectMapper();
    SegmentDescriptor descriptor = new SegmentDescriptor(new Interval("2012-01-01T00:00:00Z/P1D"), "version", 0);
    TimeseriesResultBuilder builder = new TimeseriesResultBuilder(new DateTime("2012-01-01T00:00:00Z"));
    CountAggregator rows = new CountAggregator();
    rows.aggregate();
    builder.addMetric("rows", rows);
    final Result<TimeseriesResultValue> value = builder.build();
    final SpecificSegmentQueryRunner queryRunner = new SpecificSegmentQueryRunner(new QueryRunner() {

        @Override
        public Sequence run(Query query, Map responseContext) {
            return Sequences.withEffect(Sequences.simple(Arrays.asList(value)), new Runnable() {

                @Override
                public void run() {
                    throw new SegmentMissingException("FAILSAUCE");
                }
            }, MoreExecutors.sameThreadExecutor());
        }
    }, new SpecificSegmentSpec(descriptor));
    final Map<String, Object> responseContext = Maps.newHashMap();
    TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource("foo").granularity(Granularities.ALL).intervals(ImmutableList.of(new Interval("2012-01-01T00:00:00Z/P1D"))).aggregators(ImmutableList.<AggregatorFactory>of(new CountAggregatorFactory("rows"))).build();
    Sequence results = queryRunner.run(query, responseContext);
    List<Result<TimeseriesResultValue>> res = Sequences.toList(results, Lists.<Result<TimeseriesResultValue>>newArrayList());
    Assert.assertEquals(1, res.size());
    Result<TimeseriesResultValue> theVal = res.get(0);
    Assert.assertTrue(1L == theVal.getValue().getLongMetric("rows"));
    validate(mapper, descriptor, responseContext);
}
Also used : TimeseriesResultValue(io.druid.query.timeseries.TimeseriesResultValue) TimeseriesQuery(io.druid.query.timeseries.TimeseriesQuery) Query(io.druid.query.Query) SegmentMissingException(io.druid.segment.SegmentMissingException) DateTime(org.joda.time.DateTime) Result(io.druid.query.Result) SegmentDescriptor(io.druid.query.SegmentDescriptor) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TimeseriesQuery(io.druid.query.timeseries.TimeseriesQuery) TimeseriesResultBuilder(io.druid.query.timeseries.TimeseriesResultBuilder) Sequence(io.druid.java.util.common.guava.Sequence) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory) CountAggregatorFactory(io.druid.query.aggregation.CountAggregatorFactory) QueryRunner(io.druid.query.QueryRunner) CountAggregatorFactory(io.druid.query.aggregation.CountAggregatorFactory) CountAggregator(io.druid.query.aggregation.CountAggregator) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Map(java.util.Map) Interval(org.joda.time.Interval) Test(org.junit.Test)

Aggregations

CountAggregator (io.druid.query.aggregation.CountAggregator)13 Test (org.junit.Test)13 PostAggregator (io.druid.query.aggregation.PostAggregator)11 HashMap (java.util.HashMap)11 Comparator (java.util.Comparator)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)1 Sequence (io.druid.java.util.common.guava.Sequence)1 Query (io.druid.query.Query)1 QueryRunner (io.druid.query.QueryRunner)1 Result (io.druid.query.Result)1 SegmentDescriptor (io.druid.query.SegmentDescriptor)1 Aggregator (io.druid.query.aggregation.Aggregator)1 AggregatorFactory (io.druid.query.aggregation.AggregatorFactory)1 CountAggregatorFactory (io.druid.query.aggregation.CountAggregatorFactory)1 GroupByQueryRunnerTest (io.druid.query.groupby.GroupByQueryRunnerTest)1 TimeseriesQuery (io.druid.query.timeseries.TimeseriesQuery)1 TimeseriesResultBuilder (io.druid.query.timeseries.TimeseriesResultBuilder)1 TimeseriesResultValue (io.druid.query.timeseries.TimeseriesResultValue)1 SegmentMissingException (io.druid.segment.SegmentMissingException)1