Search in sources :

Example 11 with TopNResultValue

use of io.druid.query.topn.TopNResultValue in project druid by druid-io.

the class SchemalessTestSimpleTest method testFullOnTopN.

//  @Test TODO: Handling of null values is inconsistent right now, need to make it all consistent and re-enable test
// TODO: Complain to Eric when you see this.  It shouldn't be like this...
public void testFullOnTopN() {
    TopNQuery query = new TopNQueryBuilder().dataSource(dataSource).granularity(allGran).dimension(marketDimension).metric(indexMetric).threshold(3).intervals(fullOnInterval).aggregators(Lists.<AggregatorFactory>newArrayList(Iterables.concat(commonAggregators, Lists.newArrayList(new DoubleMaxAggregatorFactory("maxIndex", "index"), new DoubleMinAggregatorFactory("minIndex", "index"))))).postAggregators(Arrays.<PostAggregator>asList(addRowsIndexConstant)).build();
    List<Result<TopNResultValue>> expectedResults = Arrays.asList(new Result<TopNResultValue>(new DateTime("2011-01-12T00:00:00.000Z"), new TopNResultValue(Arrays.<DimensionAndMetricValueExtractor>asList(new DimensionAndMetricValueExtractor(ImmutableMap.<String, Object>builder().put("market", "spot").put("rows", 4L).put("index", 400.0D).put("addRowsIndexConstant", 405.0D).put("uniques", 1.0002442201269182D).put("maxIndex", 100.0).put("minIndex", 100.0).build()), new DimensionAndMetricValueExtractor(ImmutableMap.<String, Object>builder().put("market", "").put("rows", 2L).put("index", 200.0D).put("addRowsIndexConstant", 203.0D).put("uniques", 0.0).put("maxIndex", 100.0D).put("minIndex", 100.0D).build()), new DimensionAndMetricValueExtractor(ImmutableMap.<String, Object>builder().put("market", "total_market").put("rows", 2L).put("index", 200.0D).put("addRowsIndexConstant", 203.0D).put("uniques", 1.0002442201269182D).put("maxIndex", 100.0D).put("minIndex", 100.0D).build())))));
    QueryRunner runner = TestQueryRunners.makeTopNQueryRunner(segment);
    HashMap<String, Object> context = new HashMap<String, Object>();
    TestHelper.assertExpectedResults(expectedResults, runner.run(query, context));
}
Also used : TopNQueryBuilder(io.druid.query.topn.TopNQueryBuilder) TopNResultValue(io.druid.query.topn.TopNResultValue) DoubleMaxAggregatorFactory(io.druid.query.aggregation.DoubleMaxAggregatorFactory) PostAggregator(io.druid.query.aggregation.PostAggregator) FieldAccessPostAggregator(io.druid.query.aggregation.post.FieldAccessPostAggregator) ArithmeticPostAggregator(io.druid.query.aggregation.post.ArithmeticPostAggregator) ConstantPostAggregator(io.druid.query.aggregation.post.ConstantPostAggregator) HashMap(java.util.HashMap) DoubleMinAggregatorFactory(io.druid.query.aggregation.DoubleMinAggregatorFactory) DateTime(org.joda.time.DateTime) QueryRunner(io.druid.query.QueryRunner) Result(io.druid.query.Result) TopNQuery(io.druid.query.topn.TopNQuery) DimensionAndMetricValueExtractor(io.druid.query.topn.DimensionAndMetricValueExtractor)

Example 12 with TopNResultValue

use of io.druid.query.topn.TopNResultValue in project druid by druid-io.

the class AppendTest method testTopNSeries.

@Test
public void testTopNSeries() {
    List<Result<TopNResultValue>> expectedResults = Arrays.asList(new Result<TopNResultValue>(new DateTime("2011-01-12T00:00:00.000Z"), new TopNResultValue(Arrays.<Map<String, Object>>asList(ImmutableMap.<String, Object>builder().put("market", "spot").put("rows", 3L).put("index", 300.0D).put("addRowsIndexConstant", 304.0D).put("uniques", 0.0D).put("maxIndex", 100.0).put("minIndex", 100.0).build(), new HashMap<String, Object>() {

        {
            put("market", null);
            put("rows", 3L);
            put("index", 200.0D);
            put("addRowsIndexConstant", 204.0D);
            put("uniques", 0.0D);
            put("maxIndex", 100.0);
            put("minIndex", 0.0);
        }
    }, ImmutableMap.<String, Object>builder().put("market", "total_market").put("rows", 2L).put("index", 200.0D).put("addRowsIndexConstant", 203.0D).put("uniques", 1.0002442201269182D).put("maxIndex", 100.0D).put("minIndex", 100.0D).build()))));
    TopNQuery query = makeTopNQuery();
    QueryRunner runner = TestQueryRunners.makeTopNQueryRunner(segment);
    HashMap<String, Object> context = new HashMap<String, Object>();
    TestHelper.assertExpectedResults(expectedResults, runner.run(query, context));
}
Also used : TopNResultValue(io.druid.query.topn.TopNResultValue) HashMap(java.util.HashMap) TopNQuery(io.druid.query.topn.TopNQuery) DateTime(org.joda.time.DateTime) QueryRunner(io.druid.query.QueryRunner) Result(io.druid.query.Result) Test(org.junit.Test)

Example 13 with TopNResultValue

use of io.druid.query.topn.TopNResultValue in project druid by druid-io.

the class AppendTest method testTopNSeries2.

@Test
public void testTopNSeries2() {
    List<Result<TopNResultValue>> expectedResults = Arrays.asList(new Result<TopNResultValue>(new DateTime("2011-01-12T00:00:00.000Z"), new TopNResultValue(Arrays.<Map<String, Object>>asList(ImmutableMap.<String, Object>builder().put("market", "total_market").put("rows", 3L).put("index", 300.0D).put("addRowsIndexConstant", 304.0D).put("uniques", 0.0D).put("maxIndex", 100.0D).put("minIndex", 100.0D).build(), new HashMap<String, Object>() {

        {
            put("market", null);
            put("rows", 3L);
            put("index", 100.0D);
            put("addRowsIndexConstant", 104.0D);
            put("uniques", 0.0D);
            put("maxIndex", 100.0);
            put("minIndex", 0.0);
        }
    }, ImmutableMap.<String, Object>builder().put("market", "spot").put("rows", 1L).put("index", 100.0D).put("addRowsIndexConstant", 102.0D).put("uniques", 0.0D).put("maxIndex", 100.0).put("minIndex", 100.0).build()))));
    TopNQuery query = makeTopNQuery();
    QueryRunner runner = TestQueryRunners.makeTopNQueryRunner(segment2);
    HashMap<String, Object> context = new HashMap<String, Object>();
    TestHelper.assertExpectedResults(expectedResults, runner.run(query, context));
}
Also used : TopNResultValue(io.druid.query.topn.TopNResultValue) HashMap(java.util.HashMap) TopNQuery(io.druid.query.topn.TopNQuery) DateTime(org.joda.time.DateTime) QueryRunner(io.druid.query.QueryRunner) Result(io.druid.query.Result) Test(org.junit.Test)

Example 14 with TopNResultValue

use of io.druid.query.topn.TopNResultValue in project druid by druid-io.

the class QueryMaker method executeTopN.

private Sequence<Object[]> executeTopN(final DruidQueryBuilder queryBuilder, final TopNQuery query) {
    final List<RelDataTypeField> fieldList = queryBuilder.getRowType().getFieldList();
    Hook.QUERY_PLAN.run(query);
    return Sequences.concat(Sequences.map(query.run(walker, Maps.<String, Object>newHashMap()), new Function<Result<TopNResultValue>, Sequence<Object[]>>() {

        @Override
        public Sequence<Object[]> apply(final Result<TopNResultValue> result) {
            final List<DimensionAndMetricValueExtractor> rows = result.getValue().getValue();
            final List<Object[]> retVals = new ArrayList<>(rows.size());
            for (DimensionAndMetricValueExtractor row : rows) {
                final Object[] retVal = new Object[fieldList.size()];
                for (final RelDataTypeField field : fieldList) {
                    final String outputName = queryBuilder.getRowOrder().get(field.getIndex());
                    retVal[field.getIndex()] = coerce(row.getMetric(outputName), field.getType().getSqlTypeName());
                }
                retVals.add(retVal);
            }
            return Sequences.simple(retVals);
        }
    }));
}
Also used : TopNResultValue(io.druid.query.topn.TopNResultValue) Function(com.google.common.base.Function) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) NlsString(org.apache.calcite.util.NlsString) DimensionAndMetricValueExtractor(io.druid.query.topn.DimensionAndMetricValueExtractor) Result(io.druid.query.Result)

Example 15 with TopNResultValue

use of io.druid.query.topn.TopNResultValue in project druid by druid-io.

the class DistinctCountTopNQueryTest method testTopNWithDistinctCountAgg.

@Test
public void testTopNWithDistinctCountAgg() throws Exception {
    TopNQueryEngine engine = new TopNQueryEngine(new StupidPool<ByteBuffer>("TopNQueryEngine-bufferPool", new Supplier<ByteBuffer>() {

        @Override
        public ByteBuffer get() {
            return ByteBuffer.allocate(1024 * 1024);
        }
    }));
    IncrementalIndex index = new OnheapIncrementalIndex(0, Granularities.SECOND, new AggregatorFactory[] { new CountAggregatorFactory("cnt") }, 1000);
    String visitor_id = "visitor_id";
    String client_type = "client_type";
    DateTime time = new DateTime("2016-03-04T00:00:00.000Z");
    long timestamp = time.getMillis();
    index.add(new MapBasedInputRow(timestamp, Lists.newArrayList(visitor_id, client_type), ImmutableMap.<String, Object>of(visitor_id, "0", client_type, "iphone")));
    index.add(new MapBasedInputRow(timestamp, Lists.newArrayList(visitor_id, client_type), ImmutableMap.<String, Object>of(visitor_id, "1", client_type, "iphone")));
    index.add(new MapBasedInputRow(timestamp, Lists.newArrayList(visitor_id, client_type), ImmutableMap.<String, Object>of(visitor_id, "2", client_type, "android")));
    TopNQuery query = new TopNQueryBuilder().dataSource(QueryRunnerTestHelper.dataSource).granularity(QueryRunnerTestHelper.allGran).intervals(QueryRunnerTestHelper.fullOnInterval).dimension(client_type).metric("UV").threshold(10).aggregators(Lists.newArrayList(QueryRunnerTestHelper.rowsCount, new DistinctCountAggregatorFactory("UV", visitor_id, null))).build();
    final Iterable<Result<TopNResultValue>> results = Sequences.toList(engine.query(query, new IncrementalIndexStorageAdapter(index)), Lists.<Result<TopNResultValue>>newLinkedList());
    List<Result<TopNResultValue>> expectedResults = Arrays.asList(new Result<>(time, new TopNResultValue(Arrays.<Map<String, Object>>asList(ImmutableMap.<String, Object>of(client_type, "iphone", "UV", 2L, "rows", 2L), ImmutableMap.<String, Object>of(client_type, "android", "UV", 1L, "rows", 1L)))));
    TestHelper.assertExpectedResults(expectedResults, results);
}
Also used : TopNQueryBuilder(io.druid.query.topn.TopNQueryBuilder) TopNResultValue(io.druid.query.topn.TopNResultValue) IncrementalIndex(io.druid.segment.incremental.IncrementalIndex) OnheapIncrementalIndex(io.druid.segment.incremental.OnheapIncrementalIndex) OnheapIncrementalIndex(io.druid.segment.incremental.OnheapIncrementalIndex) ByteBuffer(java.nio.ByteBuffer) DateTime(org.joda.time.DateTime) TopNQueryEngine(io.druid.query.topn.TopNQueryEngine) Result(io.druid.query.Result) CountAggregatorFactory(io.druid.query.aggregation.CountAggregatorFactory) IncrementalIndexStorageAdapter(io.druid.segment.incremental.IncrementalIndexStorageAdapter) TopNQuery(io.druid.query.topn.TopNQuery) Supplier(com.google.common.base.Supplier) MapBasedInputRow(io.druid.data.input.MapBasedInputRow) Test(org.junit.Test)

Aggregations

TopNResultValue (io.druid.query.topn.TopNResultValue)32 Result (io.druid.query.Result)30 QueryableIndexSegment (io.druid.segment.QueryableIndexSegment)15 Benchmark (org.openjdk.jmh.annotations.Benchmark)15 BenchmarkMode (org.openjdk.jmh.annotations.BenchmarkMode)15 OutputTimeUnit (org.openjdk.jmh.annotations.OutputTimeUnit)15 DateTime (org.joda.time.DateTime)14 TopNQuery (io.druid.query.topn.TopNQuery)11 Test (org.junit.Test)11 HashMap (java.util.HashMap)10 TopNQueryBuilder (io.druid.query.topn.TopNQueryBuilder)9 QueryRunner (io.druid.query.QueryRunner)6 DoubleMaxAggregatorFactory (io.druid.query.aggregation.DoubleMaxAggregatorFactory)5 DoubleMinAggregatorFactory (io.druid.query.aggregation.DoubleMinAggregatorFactory)5 PostAggregator (io.druid.query.aggregation.PostAggregator)5 CountAggregatorFactory (io.druid.query.aggregation.CountAggregatorFactory)4 ArithmeticPostAggregator (io.druid.query.aggregation.post.ArithmeticPostAggregator)3 ConstantPostAggregator (io.druid.query.aggregation.post.ConstantPostAggregator)3 FieldAccessPostAggregator (io.druid.query.aggregation.post.FieldAccessPostAggregator)3 GroupByQueryRunnerTest (io.druid.query.groupby.GroupByQueryRunnerTest)3