diff --git a/processing/src/main/java/io/druid/query/aggregation/hyperloglog/HyperUniquesAggregatorFactory.java b/processing/src/main/java/io/druid/query/aggregation/hyperloglog/HyperUniquesAggregatorFactory.java index 0f68989cce9..7b3d31189c5 100644 --- a/processing/src/main/java/io/druid/query/aggregation/hyperloglog/HyperUniquesAggregatorFactory.java +++ b/processing/src/main/java/io/druid/query/aggregation/hyperloglog/HyperUniquesAggregatorFactory.java @@ -111,6 +111,12 @@ public class HyperUniquesAggregatorFactory implements AggregatorFactory @Override public int compare(HyperLogLogCollector lhs, HyperLogLogCollector rhs) { + if(lhs == null) { + return -1; + } + if(rhs == null) { + return 1; + } return lhs.compareTo(rhs); } }; diff --git a/processing/src/test/java/io/druid/query/topn/TopNQueryRunnerTest.java b/processing/src/test/java/io/druid/query/topn/TopNQueryRunnerTest.java index acc1a7bb382..c3a40424c3b 100644 --- a/processing/src/test/java/io/druid/query/topn/TopNQueryRunnerTest.java +++ b/processing/src/test/java/io/druid/query/topn/TopNQueryRunnerTest.java @@ -37,6 +37,7 @@ import io.druid.query.aggregation.AggregatorFactory; import io.druid.query.aggregation.MaxAggregatorFactory; import io.druid.query.aggregation.MinAggregatorFactory; import io.druid.query.aggregation.PostAggregator; +import io.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory; import io.druid.query.dimension.ExtractionDimensionSpec; import io.druid.query.extraction.RegexDimExtractionFn; import io.druid.query.filter.AndDimFilter; @@ -303,6 +304,46 @@ public class TopNQueryRunnerTest TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); } + @Test + public void testTopNOverMissingUniques() + { + TopNQuery query = new TopNQueryBuilder() + .dataSource(QueryRunnerTestHelper.dataSource) + .granularity(QueryRunnerTestHelper.allGran) + .dimension(marketDimension) + .metric(QueryRunnerTestHelper.uniqueMetric) + .threshold(3) + .intervals(QueryRunnerTestHelper.fullOnInterval) + .aggregators( + Arrays.asList(new HyperUniquesAggregatorFactory("uniques", "missingUniques")) + ) + .build(); + + List> expectedResults = Arrays.asList( + new Result( + new DateTime("2011-01-12T00:00:00.000Z"), + new TopNResultValue( + Arrays.>asList( + ImmutableMap.builder() + .put("market", "total_market") + .put("uniques", 0) + .build(), + ImmutableMap.builder() + .put("market", "spot") + .put("uniques", 0) + .build(), + ImmutableMap.builder() + .put("market", "upfront") + .put("uniques", 0) + .build() + ) + ) + ) + ); + HashMap context = new HashMap(); + TestHelper.assertExpectedResults(expectedResults, runner.run(query, context)); + } + @Test public void testTopNBySegment()