diff --git a/processing/src/main/java/io/druid/query/dimension/ListFilteredDimensionSpec.java b/processing/src/main/java/io/druid/query/dimension/ListFilteredDimensionSpec.java index e17e3f70f35..0c0cfb70144 100644 --- a/processing/src/main/java/io/druid/query/dimension/ListFilteredDimensionSpec.java +++ b/processing/src/main/java/io/druid/query/dimension/ListFilteredDimensionSpec.java @@ -110,7 +110,7 @@ public class ListFilteredDimensionSpec extends BaseFilteredDimensionSpec @Override public int getValueCardinality() { - return matched.size(); + return selector.getValueCardinality(); } @Override diff --git a/processing/src/main/java/io/druid/query/dimension/RegexFilteredDimensionSpec.java b/processing/src/main/java/io/druid/query/dimension/RegexFilteredDimensionSpec.java index 07cda27c04e..afd6af309e8 100644 --- a/processing/src/main/java/io/druid/query/dimension/RegexFilteredDimensionSpec.java +++ b/processing/src/main/java/io/druid/query/dimension/RegexFilteredDimensionSpec.java @@ -95,7 +95,7 @@ public class RegexFilteredDimensionSpec extends BaseFilteredDimensionSpec @Override public int getValueCardinality() { - return bitSetOfIds.cardinality(); + return selector.getValueCardinality(); } @Override diff --git a/processing/src/test/java/io/druid/query/MultiValuedDimensionTest.java b/processing/src/test/java/io/druid/query/MultiValuedDimensionTest.java index 054692025ac..4b52062b850 100644 --- a/processing/src/test/java/io/druid/query/MultiValuedDimensionTest.java +++ b/processing/src/test/java/io/druid/query/MultiValuedDimensionTest.java @@ -23,6 +23,7 @@ import com.fasterxml.jackson.databind.Module; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.google.common.io.Files; import com.metamx.common.guava.Sequence; import com.metamx.common.guava.Sequences; @@ -37,11 +38,18 @@ import io.druid.query.aggregation.AggregatorFactory; import io.druid.query.aggregation.CountAggregatorFactory; import io.druid.query.dimension.DefaultDimensionSpec; import io.druid.query.dimension.DimensionSpec; +import io.druid.query.dimension.ListFilteredDimensionSpec; import io.druid.query.dimension.RegexFilteredDimensionSpec; import io.druid.query.filter.SelectorDimFilter; import io.druid.query.groupby.GroupByQuery; import io.druid.query.groupby.GroupByQueryRunnerTestHelper; import io.druid.query.spec.LegacySegmentSpec; +import io.druid.query.topn.TopNQuery; +import io.druid.query.topn.TopNQueryBuilder; +import io.druid.query.topn.TopNQueryConfig; +import io.druid.query.topn.TopNQueryQueryToolChest; +import io.druid.query.topn.TopNQueryRunnerFactory; +import io.druid.query.topn.TopNResultValue; import io.druid.segment.IncrementalIndexSegment; import io.druid.segment.IndexSpec; import io.druid.segment.QueryableIndex; @@ -50,6 +58,7 @@ import io.druid.segment.TestHelper; import io.druid.segment.incremental.IncrementalIndex; import io.druid.segment.incremental.OnheapIncrementalIndex; import org.apache.commons.io.FileUtils; +import org.joda.time.DateTime; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -58,6 +67,7 @@ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; /** */ @@ -250,6 +260,63 @@ public class MultiValuedDimensionTest TestHelper.assertExpectedObjects(expectedResults, Sequences.toList(result, new ArrayList()), ""); } + @Test + public void testTopNWithDimFilterAndWithFilteredDimSpec() throws Exception + { + TopNQuery query = new TopNQueryBuilder() + .dataSource("xx") + .granularity(QueryGranularity.ALL) + .dimension(new ListFilteredDimensionSpec( + new DefaultDimensionSpec("tags", "tags"), + ImmutableList.of("t3"), + null + )) + .metric("count") + .intervals(QueryRunnerTestHelper.fullOnInterval) + .aggregators( + Arrays.asList( + new AggregatorFactory[] + { + new CountAggregatorFactory("count") + } + )) + .threshold(5) + .filters(new SelectorDimFilter("tags", "t3")).build(); + + QueryRunnerFactory factory = new TopNQueryRunnerFactory( + TestQueryRunners.getPool(), + new TopNQueryQueryToolChest( + new TopNQueryConfig(), + QueryRunnerTestHelper.NoopIntervalChunkingQueryRunnerDecorator() + ), + QueryRunnerTestHelper.NOOP_QUERYWATCHER + ); + QueryRunner> runner = QueryRunnerTestHelper.makeQueryRunner( + factory, + new QueryableIndexSegment("sid1", queryableIndex) + ); + Map context = Maps.newHashMap(); + Sequence> result = runner.run(query, context); + List> expectedResults = Arrays.asList( + new Result( + new DateTime("2011-01-12T00:00:00.000Z"), + new TopNResultValue( + Arrays.>asList( + ImmutableMap.of( + "tags", "t3", + "count", 2L + ) + ) + ) + ) + ); + TestHelper.assertExpectedObjects( + expectedResults, + Sequences.toList(result, new ArrayList>()), + "" + ); + } + @AfterClass public static void cleanup() throws Exception {