Search in sources :

Example 11 with CountAggregator

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

the class LongGreatestPostAggregatorTest method testCompute.

@Test
public void testCompute() {
    final String aggName = "rows";
    LongGreatestPostAggregator greatestPostAggregator;
    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));
    greatestPostAggregator = new LongGreatestPostAggregator("greatest", postAggregatorList);
    Assert.assertEquals(Long.valueOf(6), greatestPostAggregator.compute(metricValues));
}
Also used : PostAggregator(org.apache.druid.query.aggregation.PostAggregator) HashMap(java.util.HashMap) CountAggregator(org.apache.druid.query.aggregation.CountAggregator) Test(org.junit.Test)

Example 12 with CountAggregator

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

the class LongGreatestPostAggregatorTest method testComparator.

@Test
public void testComparator() {
    final String aggName = "rows";
    LongGreatestPostAggregator greatestPostAggregator;
    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));
    greatestPostAggregator = new LongGreatestPostAggregator("greatest", postAggregatorList);
    Comparator comp = greatestPostAggregator.getComparator();
    Object before = greatestPostAggregator.compute(metricValues);
    agg.aggregate();
    agg.aggregate();
    agg.aggregate();
    metricValues.put(aggName, agg.get());
    Object after = greatestPostAggregator.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(org.apache.druid.query.aggregation.PostAggregator) HashMap(java.util.HashMap) CountAggregator(org.apache.druid.query.aggregation.CountAggregator) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 13 with CountAggregator

use of org.apache.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(Intervals.of("2012-01-01T00:00:00Z/P1D"), "version", 0);
    TimeseriesResultBuilder builder = new TimeseriesResultBuilder(DateTimes.of("2012-01-01T00:00:00Z"));
    CountAggregator rows = new CountAggregator();
    rows.aggregate();
    builder.addMetric("rows", rows.get());
    final Result<TimeseriesResultValue> value = builder.build();
    final SpecificSegmentQueryRunner queryRunner = new SpecificSegmentQueryRunner(new QueryRunner() {

        @Override
        public Sequence run(QueryPlus queryPlus, ResponseContext responseContext) {
            return Sequences.withEffect(Sequences.simple(Collections.singletonList(value)), new Runnable() {

                @Override
                public void run() {
                    throw new SegmentMissingException("FAILSAUCE");
                }
            }, Execs.directExecutor());
        }
    }, new SpecificSegmentSpec(descriptor));
    final ResponseContext responseContext = ResponseContext.createEmpty();
    TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource("foo").granularity(Granularities.ALL).intervals(ImmutableList.of(Intervals.of("2012-01-01T00:00:00Z/P1D"))).aggregators(ImmutableList.of(new CountAggregatorFactory("rows"))).build();
    Sequence results = queryRunner.run(QueryPlus.wrap(query), responseContext);
    List<Result<TimeseriesResultValue>> res = results.toList();
    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(org.apache.druid.query.timeseries.TimeseriesResultValue) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) TimeseriesResultBuilder(org.apache.druid.query.timeseries.TimeseriesResultBuilder) SegmentMissingException(org.apache.druid.segment.SegmentMissingException) Sequence(org.apache.druid.java.util.common.guava.Sequence) QueryRunner(org.apache.druid.query.QueryRunner) Result(org.apache.druid.query.Result) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) SegmentDescriptor(org.apache.druid.query.SegmentDescriptor) CountAggregator(org.apache.druid.query.aggregation.CountAggregator) ResponseContext(org.apache.druid.query.context.ResponseContext) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) QueryPlus(org.apache.druid.query.QueryPlus) Test(org.junit.Test)

Aggregations

CountAggregator (org.apache.druid.query.aggregation.CountAggregator)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)11 Comparator (java.util.Comparator)5 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)1 Sequence (org.apache.druid.java.util.common.guava.Sequence)1 QueryPlus (org.apache.druid.query.QueryPlus)1 QueryRunner (org.apache.druid.query.QueryRunner)1 Result (org.apache.druid.query.Result)1 SegmentDescriptor (org.apache.druid.query.SegmentDescriptor)1 Aggregator (org.apache.druid.query.aggregation.Aggregator)1 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)1 ResponseContext (org.apache.druid.query.context.ResponseContext)1 GroupByQueryRunnerTest (org.apache.druid.query.groupby.GroupByQueryRunnerTest)1 TimeseriesQuery (org.apache.druid.query.timeseries.TimeseriesQuery)1 TimeseriesResultBuilder (org.apache.druid.query.timeseries.TimeseriesResultBuilder)1 TimeseriesResultValue (org.apache.druid.query.timeseries.TimeseriesResultValue)1