Search in sources :

Example 6 with BySegmentResultValue

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

the class TopNQueryRunnerTest method testTopNBySegment.

@Test
public void testTopNBySegment() {
    final HashMap<String, Object> specialContext = new HashMap<String, Object>();
    specialContext.put("bySegment", "true");
    TopNQuery query = new TopNQueryBuilder().dataSource(QueryRunnerTestHelper.dataSource).granularity(QueryRunnerTestHelper.allGran).dimension(QueryRunnerTestHelper.marketDimension).metric(QueryRunnerTestHelper.indexMetric).threshold(4).intervals(QueryRunnerTestHelper.firstToThird).aggregators(QueryRunnerTestHelper.commonAggregators).postAggregators(Arrays.<PostAggregator>asList(QueryRunnerTestHelper.addRowsIndexConstant)).context(specialContext).build();
    List<Result<TopNResultValue>> expectedResults = Arrays.asList(new Result<TopNResultValue>(new DateTime("2011-04-01T00:00:00.000Z"), new TopNResultValue(Arrays.<Map<String, Object>>asList(ImmutableMap.<String, Object>of("addRowsIndexConstant", 5356.814697265625D, "index", 5351.814697265625D, QueryRunnerTestHelper.marketDimension, "total_market", "uniques", QueryRunnerTestHelper.UNIQUES_2, "rows", 4L), ImmutableMap.<String, Object>of("addRowsIndexConstant", 4880.669677734375D, "index", 4875.669677734375D, QueryRunnerTestHelper.marketDimension, "upfront", "uniques", QueryRunnerTestHelper.UNIQUES_2, "rows", 4L), ImmutableMap.<String, Object>of("addRowsIndexConstant", 2250.8768157958984D, "index", 2231.8768157958984D, QueryRunnerTestHelper.marketDimension, "spot", "uniques", QueryRunnerTestHelper.UNIQUES_9, "rows", 18L)))));
    Sequence<Result<TopNResultValue>> results = runWithMerge(query, specialContext);
    List<Result<BySegmentTopNResultValue>> resultList = Sequences.toList(Sequences.map(results, new Function<Result<TopNResultValue>, Result<BySegmentTopNResultValue>>() {

        @Nullable
        @Override
        public Result<BySegmentTopNResultValue> apply(Result<TopNResultValue> input) {
            // Stupid type erasure
            Object val = input.getValue();
            if (val instanceof BySegmentResultValue) {
                BySegmentResultValue bySegVal = (BySegmentResultValue) val;
                List<?> results = bySegVal.getResults();
                return new Result<BySegmentTopNResultValue>(input.getTimestamp(), new BySegmentTopNResultValue(Lists.transform(results, new Function<Object, Result<TopNResultValue>>() {

                    @Nullable
                    @Override
                    public Result<TopNResultValue> apply(@Nullable Object input) {
                        if (Preconditions.checkNotNull(input) instanceof Result) {
                            Result result = (Result) input;
                            Object resVal = result.getValue();
                            if (resVal instanceof TopNResultValue) {
                                return new Result<TopNResultValue>(result.getTimestamp(), (TopNResultValue) resVal);
                            }
                        }
                        throw new IAE("Bad input: [%s]", input);
                    }
                }), bySegVal.getSegmentId(), bySegVal.getInterval()));
            }
            throw new ISE("Bad type");
        }
    }), Lists.<Result<BySegmentTopNResultValue>>newArrayList());
    Result<BySegmentTopNResultValue> result = resultList.get(0);
    TestHelper.assertExpectedResults(expectedResults, result.getValue().getResults());
}
Also used : BySegmentResultValue(io.druid.query.BySegmentResultValue) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IAE(io.druid.java.util.common.IAE) DateTime(org.joda.time.DateTime) Result(io.druid.query.Result) Function(com.google.common.base.Function) ISE(io.druid.java.util.common.ISE) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Aggregations

BySegmentResultValue (io.druid.query.BySegmentResultValue)6 Result (io.druid.query.Result)6 Test (org.junit.Test)6 BySegmentResultValueClass (io.druid.query.BySegmentResultValueClass)5 FinalizeResultsQueryRunner (io.druid.query.FinalizeResultsQueryRunner)5 QueryRunner (io.druid.query.QueryRunner)5 QueryToolChest (io.druid.query.QueryToolChest)5 ExecutorService (java.util.concurrent.ExecutorService)5 DateTime (org.joda.time.DateTime)5 PeriodGranularity (io.druid.java.util.common.granularity.PeriodGranularity)4 LongSumAggregatorFactory (io.druid.query.aggregation.LongSumAggregatorFactory)4 SelectorDimFilter (io.druid.query.filter.SelectorDimFilter)4 Interval (org.joda.time.Interval)4 Period (org.joda.time.Period)4 LookupExtractionFn (io.druid.query.lookup.LookupExtractionFn)3 DefaultDimensionSpec (io.druid.query.dimension.DefaultDimensionSpec)2 ExtractionDimensionSpec (io.druid.query.dimension.ExtractionDimensionSpec)2 MapLookupExtractor (io.druid.query.extraction.MapLookupExtractor)2 Function (com.google.common.base.Function)1 IAE (io.druid.java.util.common.IAE)1