Search in sources :

Example 6 with CountAggregator

use of org.apache.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(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 7 with CountAggregator

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

the class ArithmeticPostAggregatorTest method testComparator.

@Test
public void testComparator() {
    final String aggName = "rows";
    ArithmeticPostAggregator arithmeticPostAggregator;
    CountAggregator agg = new CountAggregator();
    Map<String, Object> metricValues = new HashMap<>();
    metricValues.put(aggName, agg.get());
    List<PostAggregator> postAggregatorList = Lists.newArrayList(new ConstantPostAggregator("roku", 6D), new FieldAccessPostAggregator("rows", "rows"));
    arithmeticPostAggregator = new ArithmeticPostAggregator("add", "+", postAggregatorList);
    Comparator comp = arithmeticPostAggregator.getComparator();
    Object before = arithmeticPostAggregator.compute(metricValues);
    agg.aggregate();
    agg.aggregate();
    agg.aggregate();
    metricValues.put(aggName, agg.get());
    Object after = arithmeticPostAggregator.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) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 8 with CountAggregator

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

the class ArithmeticPostAggregatorTest method testCompute.

@Test
public void testCompute() {
    final String aggName = "rows";
    ArithmeticPostAggregator arithmeticPostAggregator;
    ExpressionPostAggregator expressionPostAggregator;
    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", "rows"));
    for (PostAggregator postAggregator : postAggregatorList) {
        metricValues.put(postAggregator.getName(), postAggregator.compute(metricValues));
    }
    arithmeticPostAggregator = new ArithmeticPostAggregator("add", "+", postAggregatorList);
    expressionPostAggregator = new ExpressionPostAggregator("add", "roku + rows", null, TestExprMacroTable.INSTANCE);
    Assert.assertEquals(9.0, arithmeticPostAggregator.compute(metricValues));
    Assert.assertEquals(9.0, expressionPostAggregator.compute(metricValues));
    arithmeticPostAggregator = new ArithmeticPostAggregator("subtract", "-", postAggregatorList);
    expressionPostAggregator = new ExpressionPostAggregator("add", "roku - rows", null, TestExprMacroTable.INSTANCE);
    Assert.assertEquals(3.0, arithmeticPostAggregator.compute(metricValues));
    Assert.assertEquals(3.0, expressionPostAggregator.compute(metricValues));
    arithmeticPostAggregator = new ArithmeticPostAggregator("multiply", "*", postAggregatorList);
    expressionPostAggregator = new ExpressionPostAggregator("add", "roku * rows", null, TestExprMacroTable.INSTANCE);
    Assert.assertEquals(18.0, arithmeticPostAggregator.compute(metricValues));
    Assert.assertEquals(18.0, expressionPostAggregator.compute(metricValues));
    arithmeticPostAggregator = new ArithmeticPostAggregator("divide", "/", postAggregatorList);
    expressionPostAggregator = new ExpressionPostAggregator("add", "roku / rows", null, TestExprMacroTable.INSTANCE);
    Assert.assertEquals(2.0, arithmeticPostAggregator.compute(metricValues));
    Assert.assertEquals(2.0, expressionPostAggregator.compute(metricValues));
}
Also used : PostAggregator(org.apache.druid.query.aggregation.PostAggregator) HashMap(java.util.HashMap) CountAggregator(org.apache.druid.query.aggregation.CountAggregator) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 9 with CountAggregator

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

the class DoubleGreatestPostAggregatorTest method testCompute.

@Test
public void testCompute() {
    final String aggName = "rows";
    DoubleGreatestPostAggregator 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 DoubleGreatestPostAggregator("greatest", postAggregatorList);
    Assert.assertEquals(6.0, 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 10 with CountAggregator

use of org.apache.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 = new HashMap<>();
    metricValues.put(aggName, agg.get());
    FinalizingFieldAccessPostAggregator postAgg = new FinalizingFieldAccessPostAggregator("final_rows", aggName);
    Assert.assertEquals(new Long(3L), postAgg.compute(metricValues));
}
Also used : HashMap(java.util.HashMap) CountAggregator(org.apache.druid.query.aggregation.CountAggregator) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) CountAggregator(org.apache.druid.query.aggregation.CountAggregator) Aggregator(org.apache.druid.query.aggregation.Aggregator) GroupByQueryRunnerTest(org.apache.druid.query.groupby.GroupByQueryRunnerTest) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) 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