Search in sources :

Example 1 with UnionDataSource

use of io.druid.query.UnionDataSource in project druid by druid-io.

the class TimeSeriesUnionQueryRunnerTest method testUnionResultMerging.

@Test
public void testUnionResultMerging() {
    TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource(new UnionDataSource(Lists.newArrayList(new TableDataSource("ds1"), new TableDataSource("ds2")))).granularity(QueryRunnerTestHelper.dayGran).intervals(QueryRunnerTestHelper.firstToThird).aggregators(Arrays.<AggregatorFactory>asList(QueryRunnerTestHelper.rowsCount, new LongSumAggregatorFactory("idx", "index"))).descending(descending).build();
    QueryToolChest toolChest = new TimeseriesQueryQueryToolChest(QueryRunnerTestHelper.NoopIntervalChunkingQueryRunnerDecorator());
    final List<Result<TimeseriesResultValue>> ds1 = Lists.newArrayList(new Result<>(new DateTime("2011-04-02"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("rows", 1L, "idx", 2L))), new Result<>(new DateTime("2011-04-03"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("rows", 3L, "idx", 4L))));
    final List<Result<TimeseriesResultValue>> ds2 = Lists.newArrayList(new Result<>(new DateTime("2011-04-01"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("rows", 5L, "idx", 6L))), new Result<>(new DateTime("2011-04-02"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("rows", 7L, "idx", 8L))), new Result<>(new DateTime("2011-04-04"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("rows", 9L, "idx", 10L))));
    QueryRunner mergingrunner = toolChest.mergeResults(new UnionQueryRunner<>(new QueryRunner<Result<TimeseriesResultValue>>() {

        @Override
        public Sequence<Result<TimeseriesResultValue>> run(Query<Result<TimeseriesResultValue>> query, Map<String, Object> responseContext) {
            if (query.getDataSource().equals(new TableDataSource("ds1"))) {
                return Sequences.simple(descending ? Lists.reverse(ds1) : ds1);
            } else {
                return Sequences.simple(descending ? Lists.reverse(ds2) : ds2);
            }
        }
    }));
    List<Result<TimeseriesResultValue>> expectedResults = Arrays.asList(new Result<>(new DateTime("2011-04-01"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("rows", 5L, "idx", 6L))), new Result<>(new DateTime("2011-04-02"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("rows", 8L, "idx", 10L))), new Result<>(new DateTime("2011-04-03"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("rows", 3L, "idx", 4L))), new Result<>(new DateTime("2011-04-04"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("rows", 9L, "idx", 10L))));
    Iterable<Result<TimeseriesResultValue>> results = Sequences.toList(mergingrunner.run(query, Maps.<String, Object>newHashMap()), Lists.<Result<TimeseriesResultValue>>newArrayList());
    assertExpectedResults(expectedResults, results);
}
Also used : Query(io.druid.query.Query) LongSumAggregatorFactory(io.druid.query.aggregation.LongSumAggregatorFactory) QueryToolChest(io.druid.query.QueryToolChest) UnionDataSource(io.druid.query.UnionDataSource) DateTime(org.joda.time.DateTime) QueryRunner(io.druid.query.QueryRunner) UnionQueryRunner(io.druid.query.UnionQueryRunner) Result(io.druid.query.Result) TableDataSource(io.druid.query.TableDataSource) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 Query (io.druid.query.Query)1 QueryRunner (io.druid.query.QueryRunner)1 QueryToolChest (io.druid.query.QueryToolChest)1 Result (io.druid.query.Result)1 TableDataSource (io.druid.query.TableDataSource)1 UnionDataSource (io.druid.query.UnionDataSource)1 UnionQueryRunner (io.druid.query.UnionQueryRunner)1 LongSumAggregatorFactory (io.druid.query.aggregation.LongSumAggregatorFactory)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.Test)1