diff --git a/processing/src/main/java/io/druid/query/BySegmentResultValue.java b/processing/src/main/java/io/druid/query/BySegmentResultValue.java index 3d2dc0802f3..fdc27d9c652 100644 --- a/processing/src/main/java/io/druid/query/BySegmentResultValue.java +++ b/processing/src/main/java/io/druid/query/BySegmentResultValue.java @@ -19,15 +19,17 @@ package io.druid.query; +import org.joda.time.Interval; + import java.util.List; /** */ public interface BySegmentResultValue { - public List> getResults(); + public List getResults(); public String getSegmentId(); - public String getIntervalString(); + public Interval getInterval(); } diff --git a/processing/src/main/java/io/druid/query/BySegmentResultValueClass.java b/processing/src/main/java/io/druid/query/BySegmentResultValueClass.java index 3a3544cf47a..0d779733e66 100644 --- a/processing/src/main/java/io/druid/query/BySegmentResultValueClass.java +++ b/processing/src/main/java/io/druid/query/BySegmentResultValueClass.java @@ -26,7 +26,7 @@ import java.util.List; /** */ -public class BySegmentResultValueClass +public class BySegmentResultValueClass implements BySegmentResultValue { private final List results; private final String segmentId; @@ -43,18 +43,21 @@ public class BySegmentResultValueClass this.interval = interval; } + @Override @JsonProperty("results") public List getResults() { return results; } + @Override @JsonProperty("segment") public String getSegmentId() { return segmentId; } + @Override @JsonProperty("interval") public Interval getInterval() { diff --git a/processing/src/main/java/io/druid/query/FinalizeResultsQueryRunner.java b/processing/src/main/java/io/druid/query/FinalizeResultsQueryRunner.java index 10bff0a65e2..1794024b60c 100644 --- a/processing/src/main/java/io/druid/query/FinalizeResultsQueryRunner.java +++ b/processing/src/main/java/io/druid/query/FinalizeResultsQueryRunner.java @@ -84,7 +84,7 @@ public class FinalizeResultsQueryRunner implements QueryRunner throw new ISE("Cannot have a null result!"); } - BySegmentResultValueClass resultsClass = result.getValue(); + BySegmentResultValue resultsClass = result.getValue(); return (T) new Result( result.getTimestamp(), diff --git a/processing/src/main/java/io/druid/query/search/BySegmentSearchResultValue.java b/processing/src/main/java/io/druid/query/search/BySegmentSearchResultValue.java index 0ecbdf509c2..c77b25f19bd 100644 --- a/processing/src/main/java/io/druid/query/search/BySegmentSearchResultValue.java +++ b/processing/src/main/java/io/druid/query/search/BySegmentSearchResultValue.java @@ -24,28 +24,30 @@ import com.fasterxml.jackson.annotation.JsonValue; import io.druid.query.BySegmentResultValue; import io.druid.query.Result; import io.druid.query.search.search.SearchHit; +import org.joda.time.Interval; import java.util.List; /** */ -public class BySegmentSearchResultValue extends SearchResultValue implements BySegmentResultValue +public class BySegmentSearchResultValue extends SearchResultValue + implements BySegmentResultValue> { private final List> results; private final String segmentId; - private final String intervalString; + private final Interval interval; public BySegmentSearchResultValue( @JsonProperty("results") List> results, @JsonProperty("segment") String segmentId, - @JsonProperty("interval") String intervalString + @JsonProperty("interval") Interval interval ) { super(null); this.results = results; this.segmentId = segmentId; - this.intervalString = intervalString; + this.interval = interval; } @Override @@ -71,9 +73,9 @@ public class BySegmentSearchResultValue extends SearchResultValue implements ByS @Override @JsonProperty("interval") - public String getIntervalString() + public Interval getInterval() { - return intervalString; + return interval; } @Override @@ -82,7 +84,7 @@ public class BySegmentSearchResultValue extends SearchResultValue implements ByS return "BySegmentSearchResultValue{" + "results=" + results + ", segmentId='" + segmentId + '\'' + - ", intervalString='" + intervalString + '\'' + + ", interval='" + interval.toString() + '\'' + '}'; } } diff --git a/processing/src/main/java/io/druid/query/search/SearchQueryQueryToolChest.java b/processing/src/main/java/io/druid/query/search/SearchQueryQueryToolChest.java index d6bcd61cfe1..79d9686cee8 100644 --- a/processing/src/main/java/io/druid/query/search/SearchQueryQueryToolChest.java +++ b/processing/src/main/java/io/druid/query/search/SearchQueryQueryToolChest.java @@ -329,7 +329,7 @@ public class SearchQueryQueryToolChest extends QueryToolChest +public class BySegmentTopNResultValue extends TopNResultValue implements BySegmentResultValue> { private final List> results; private final String segmentId; - private final String intervalString; + private final Interval interval; @JsonCreator public BySegmentTopNResultValue( @JsonProperty("results") List> results, @JsonProperty("segment") String segmentId, - @JsonProperty("interval") String intervalString + @JsonProperty("interval") Interval interval ) { super(null); this.results = results; this.segmentId = segmentId; - this.intervalString = intervalString; + this.interval = interval; } @Override @@ -73,9 +75,9 @@ public class BySegmentTopNResultValue extends TopNResultValue implements BySegme @Override @JsonProperty("interval") - public String getIntervalString() + public Interval getInterval() { - return intervalString; + return interval; } @Override @@ -84,7 +86,7 @@ public class BySegmentTopNResultValue extends TopNResultValue implements BySegme return "BySegmentTopNResultValue{" + "results=" + results + ", segmentId='" + segmentId + '\'' + - ", intervalString='" + intervalString + '\'' + + ", interval='" + interval.toString() + '\'' + '}'; } } diff --git a/processing/src/main/java/io/druid/query/topn/TopNQueryQueryToolChest.java b/processing/src/main/java/io/druid/query/topn/TopNQueryQueryToolChest.java index 165e3149456..b8babee2d12 100644 --- a/processing/src/main/java/io/druid/query/topn/TopNQueryQueryToolChest.java +++ b/processing/src/main/java/io/druid/query/topn/TopNQueryQueryToolChest.java @@ -35,6 +35,8 @@ import com.metamx.common.guava.nary.BinaryFn; import com.metamx.emitter.service.ServiceMetricEvent; import io.druid.collections.OrderedMergeSequence; import io.druid.granularity.QueryGranularity; +import io.druid.query.BySegmentResultValue; +import io.druid.query.BySegmentResultValueClass; import io.druid.query.CacheStrategy; import io.druid.query.IntervalChunkingQueryRunner; import io.druid.query.Query; @@ -433,7 +435,8 @@ public class TopNQueryQueryToolChest extends QueryToolChest apply(Result input) { if (isBySegment) { - BySegmentTopNResultValue value = (BySegmentTopNResultValue) input.getValue(); + BySegmentResultValue> value = (BySegmentResultValue>) input + .getValue(); return new Result( input.getTimestamp(), @@ -460,7 +463,7 @@ public class TopNQueryQueryToolChest extends QueryToolChest context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -231,7 +234,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -296,11 +299,89 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } + @Test + public void testTopNBySegment() + { + + final HashMap specialContext = new HashMap(); + specialContext.put("bySegment", "true"); + TopNQuery query = new TopNQueryBuilder() + .dataSource(QueryRunnerTestHelper.dataSource) + .granularity(QueryRunnerTestHelper.allGran) + .dimension(marketDimension) + .metric(QueryRunnerTestHelper.indexMetric) + .threshold(4) + .intervals(QueryRunnerTestHelper.firstToThird) + .aggregators(QueryRunnerTestHelper.commonAggregators) + .postAggregators(Arrays.asList(QueryRunnerTestHelper.addRowsIndexConstant)) + .context(specialContext) + .build(); + + + List> expectedResults = Arrays.asList( + new Result( + new DateTime("2011-04-01T00:00:00.000Z"), + new TopNResultValue( + Arrays.>asList( + ImmutableMap.of( + "addRowsIndexConstant", 5356.814697265625D, + "index", 5351.814697265625D, + marketDimension, "total_market", + "uniques", QueryRunnerTestHelper.UNIQUES_2, + "rows", 4L + ), + ImmutableMap.of( + "addRowsIndexConstant", 4880.669677734375D, + "index", 4875.669677734375D, + marketDimension, "upfront", + "uniques", QueryRunnerTestHelper.UNIQUES_2, + "rows", 4L + ), + ImmutableMap.of( + "addRowsIndexConstant", 2250.8768157958984D, + "index", 2231.8768157958984D, + marketDimension, "spot", + "uniques", QueryRunnerTestHelper.UNIQUES_9, + "rows", 18L + ) + ) + ) + ) + ); + Sequence> results = new TopNQueryQueryToolChest(new TopNQueryConfig()).postMergeQueryDecoration( + runner + ).run( + query, + specialContext + ); + List> resultList = Sequences.toList( + Sequences.map( + results, new Function, Result>() + { + + @Nullable + @Override + public Result apply( + Result input + ) + { + return new Result( + input.getTimestamp(), + (BySegmentTopNResultValue) input.getValue() + ); + } + } + ), + Lists.>newArrayList() + ); + TestHelper.assertExpectedResults(expectedResults, resultList.get(0).getValue().getResults()); + } + @Test public void testTopN() { @@ -346,7 +427,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -395,7 +476,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -444,7 +525,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -486,7 +567,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -521,7 +602,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -570,7 +651,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -623,7 +704,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -665,7 +746,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -683,7 +764,7 @@ public class TopNQueryRunnerTest .aggregators(QueryRunnerTestHelper.commonAggregators) .postAggregators(Arrays.asList(QueryRunnerTestHelper.addRowsIndexConstant)) .build(); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults( Lists.>newArrayList( new Result( @@ -722,7 +803,7 @@ public class TopNQueryRunnerTest .aggregators(QueryRunnerTestHelper.commonAggregators) .postAggregators(Arrays.asList(QueryRunnerTestHelper.addRowsIndexConstant)) .build(); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults( Lists.>newArrayList( new Result( @@ -748,7 +829,7 @@ public class TopNQueryRunnerTest .aggregators(QueryRunnerTestHelper.commonAggregators) .postAggregators(Arrays.asList(QueryRunnerTestHelper.addRowsIndexConstant)) .build(); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults( Sequences.toList( runner.run( @@ -783,7 +864,7 @@ public class TopNQueryRunnerTest .aggregators(QueryRunnerTestHelper.commonAggregators) .postAggregators(Arrays.asList(QueryRunnerTestHelper.addRowsIndexConstant)) .build(); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults( Sequences.toList( runner.run( @@ -843,7 +924,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -892,7 +973,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -948,7 +1029,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -996,7 +1077,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1037,7 +1118,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1078,7 +1159,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1119,7 +1200,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1160,7 +1241,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1212,7 +1293,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1264,7 +1345,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1316,7 +1397,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1361,7 +1442,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1407,7 +1488,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1452,7 +1533,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1501,7 +1582,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1586,7 +1667,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } @@ -1669,7 +1750,7 @@ public class TopNQueryRunnerTest ) ) ); - HashMap context = new HashMap(); + HashMap context = new HashMap(); TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } } diff --git a/server/src/main/java/io/druid/server/ClientQuerySegmentWalker.java b/server/src/main/java/io/druid/server/ClientQuerySegmentWalker.java index 38ec31c1bc8..f84607e659b 100644 --- a/server/src/main/java/io/druid/server/ClientQuerySegmentWalker.java +++ b/server/src/main/java/io/druid/server/ClientQuerySegmentWalker.java @@ -87,7 +87,7 @@ public class ClientQuerySegmentWalker implements QuerySegmentWalker final FinalizeResultsQueryRunner baseRunner = new FinalizeResultsQueryRunner( toolChest.postMergeQueryDecoration( toolChest.mergeResults( - new UnionQueryRunner<>( + new UnionQueryRunner( new MetricsEmittingQueryRunner( emitter, new Function, ServiceMetricEvent.Builder>()