Search in sources :

Example 6 with TopNQueryBuilder

use of org.apache.druid.query.topn.TopNQueryBuilder in project druid by druid-io.

the class CalciteQueryTest method testQueryContextOuterLimit.

@Test
public void testQueryContextOuterLimit() throws Exception {
    Map<String, Object> outerLimitContext = new HashMap<>(QUERY_CONTEXT_DEFAULT);
    outerLimitContext.put(PlannerContext.CTX_SQL_OUTER_LIMIT, 4);
    TopNQueryBuilder baseBuilder = new TopNQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).dimension(new DefaultDimensionSpec("dim1", "d0")).metric(new InvertedTopNMetricSpec(new DimensionTopNMetricSpec(null, StringComparators.LEXICOGRAPHIC))).context(outerLimitContext);
    List<Object[]> results1;
    if (NullHandling.replaceWithDefault()) {
        results1 = ImmutableList.of(new Object[] { "" }, new Object[] { "def" }, new Object[] { "abc" }, new Object[] { "2" });
    } else {
        results1 = ImmutableList.of(new Object[] { "def" }, new Object[] { "abc" }, new Object[] { "2" }, new Object[] { "10.1" });
    }
    // no existing limit
    testQuery(PLANNER_CONFIG_DEFAULT, outerLimitContext, "SELECT dim1 FROM druid.foo GROUP BY dim1 ORDER BY dim1 DESC", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(baseBuilder.threshold(4).build()), results1);
    // existing limit greater than context limit, override existing limit
    testQuery(PLANNER_CONFIG_DEFAULT, outerLimitContext, "SELECT dim1 FROM druid.foo GROUP BY dim1 ORDER BY dim1 DESC LIMIT 9", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(baseBuilder.threshold(4).build()), results1);
    List<Object[]> results2;
    if (NullHandling.replaceWithDefault()) {
        results2 = ImmutableList.of(new Object[] { "" }, new Object[] { "def" });
    } else {
        results2 = ImmutableList.of(new Object[] { "def" }, new Object[] { "abc" });
    }
    // existing limit less than context limit, keep existing limit
    testQuery(PLANNER_CONFIG_DEFAULT, outerLimitContext, "SELECT dim1 FROM druid.foo GROUP BY dim1 ORDER BY dim1 DESC LIMIT 2", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(baseBuilder.threshold(2).build()), results2);
}
Also used : TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) InvertedTopNMetricSpec(org.apache.druid.query.topn.InvertedTopNMetricSpec) DimensionTopNMetricSpec(org.apache.druid.query.topn.DimensionTopNMetricSpec) HashMap(java.util.HashMap) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 7 with TopNQueryBuilder

use of org.apache.druid.query.topn.TopNQueryBuilder in project druid by druid-io.

the class CalciteQueryTest method testOrderByEarliestFloat.

@Test
public void testOrderByEarliestFloat() throws Exception {
    // Cannot vectorize EARLIEST aggregator.
    skipVectorize();
    List<Object[]> expected;
    if (NullHandling.replaceWithDefault()) {
        expected = ImmutableList.of(new Object[] { "1", 0.0f }, new Object[] { "2", 0.0f }, new Object[] { "abc", 0.0f }, new Object[] { "def", 0.0f }, new Object[] { "10.1", 0.1f }, new Object[] { "", 1.0f });
    } else {
        expected = ImmutableList.of(new Object[] { "1", null }, new Object[] { "abc", null }, new Object[] { "def", null }, new Object[] { "2", 0.0f }, new Object[] { "10.1", 0.1f }, new Object[] { "", 1.0f });
    }
    testQuery("SELECT dim1, EARLIEST(f1) FROM druid.numfoo GROUP BY 1 ORDER BY 2 LIMIT 10", ImmutableList.of(new TopNQueryBuilder().dataSource(CalciteTests.DATASOURCE3).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).dimension(new DefaultDimensionSpec("dim1", "_d0")).aggregators(aggregators(new FloatFirstAggregatorFactory("a0", "f1", null))).metric(new InvertedTopNMetricSpec(new NumericTopNMetricSpec("a0"))).threshold(10).context(QUERY_CONTEXT_DEFAULT).build()), expected);
}
Also used : TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) FloatFirstAggregatorFactory(org.apache.druid.query.aggregation.first.FloatFirstAggregatorFactory) InvertedTopNMetricSpec(org.apache.druid.query.topn.InvertedTopNMetricSpec) NumericTopNMetricSpec(org.apache.druid.query.topn.NumericTopNMetricSpec) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 8 with TopNQueryBuilder

use of org.apache.druid.query.topn.TopNQueryBuilder in project druid by druid-io.

the class CachingClusteredClientTest method testOutOfOrderSequenceMerging.

@Test
public void testOutOfOrderSequenceMerging() {
    List<Sequence<Result<TopNResultValue>>> sequences = ImmutableList.of(Sequences.simple(makeTopNResultsWithoutRename(DateTimes.of("2011-01-07"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-08"), "a", 50, 4988, "b", 50, 4987, "c", 50, 4986, DateTimes.of("2011-01-09"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983)), Sequences.simple(makeTopNResultsWithoutRename(DateTimes.of("2011-01-06T01"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-07T01"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-08T01"), "a", 50, 4988, "b", 50, 4987, "c", 50, 4986, DateTimes.of("2011-01-09T01"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983)));
    TestHelper.assertExpectedResults(makeTopNResultsWithoutRename(DateTimes.of("2011-01-06T01"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-07"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-07T01"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-08"), "a", 50, 4988, "b", 50, 4987, "c", 50, 4986, DateTimes.of("2011-01-08T01"), "a", 50, 4988, "b", 50, 4987, "c", 50, 4986, DateTimes.of("2011-01-09"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983, DateTimes.of("2011-01-09T01"), "a", 50, 4985, "b", 50, 4984, "c", 50, 4983), mergeSequences(new TopNQueryBuilder().dataSource("test").intervals("2011-01-06/2011-01-10").dimension("a").metric("b").threshold(3).aggregators(new CountAggregatorFactory("b")).randomQueryId().build(), sequences));
}
Also used : TopNResultValue(org.apache.druid.query.topn.TopNResultValue) TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) Sequence(org.apache.druid.java.util.common.guava.Sequence) Test(org.junit.Test)

Example 9 with TopNQueryBuilder

use of org.apache.druid.query.topn.TopNQueryBuilder in project druid by druid-io.

the class CachingClusteredClientTest method testTopNCaching.

@Test
@SuppressWarnings("unchecked")
public void testTopNCaching() {
    final TopNQueryBuilder builder = new TopNQueryBuilder().dataSource(DATA_SOURCE).dimension(TOP_DIM).metric("imps").threshold(3).intervals(SEG_SPEC).filters(DIM_FILTER).granularity(GRANULARITY).aggregators(AGGS).postAggregators(POST_AGGS).context(CONTEXT);
    QueryRunner runner = new FinalizeResultsQueryRunner(getDefaultQueryRunner(), new TopNQueryQueryToolChest(new TopNQueryConfig()));
    testQueryCaching(runner, builder.randomQueryId().build(), Intervals.of("2011-01-01/2011-01-02"), makeTopNResultsWithoutRename(DateTimes.of("2011-01-01"), "a", 50, 5000, "b", 50, 4999, "c", 50, 4998), Intervals.of("2011-01-02/2011-01-03"), makeTopNResultsWithoutRename(DateTimes.of("2011-01-02"), "a", 50, 4997, "b", 50, 4996, "c", 50, 4995), Intervals.of("2011-01-05/2011-01-10"), makeTopNResultsWithoutRename(DateTimes.of("2011-01-05"), "a", 50, 4994, "b", 50, 4993, "c", 50, 4992, DateTimes.of("2011-01-06"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-07"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-08"), "a", 50, 4988, "b", 50, 4987, "c", 50, 4986, DateTimes.of("2011-01-09"), "c1", 50, 4985, "b", 50, 4984, "c", 50, 4983), Intervals.of("2011-01-05/2011-01-10"), makeTopNResultsWithoutRename(DateTimes.of("2011-01-05T01"), "a", 50, 4994, "b", 50, 4993, "c", 50, 4992, DateTimes.of("2011-01-06T01"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-07T01"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-08T01"), "a", 50, 4988, "b", 50, 4987, "c", 50, 4986, DateTimes.of("2011-01-09T01"), "c2", 50, 4985, "b", 50, 4984, "c", 50, 4983));
    TopNQuery query = builder.intervals("2011-01-01/2011-01-10").metric("imps").aggregators(RENAMED_AGGS).postAggregators(DIFF_ORDER_POST_AGGS).randomQueryId().build();
    TestHelper.assertExpectedResults(makeRenamedTopNResults(DateTimes.of("2011-01-01"), "a", 50, 5000, "b", 50, 4999, "c", 50, 4998, DateTimes.of("2011-01-02"), "a", 50, 4997, "b", 50, 4996, "c", 50, 4995, DateTimes.of("2011-01-05"), "a", 50, 4994, "b", 50, 4993, "c", 50, 4992, DateTimes.of("2011-01-05T01"), "a", 50, 4994, "b", 50, 4993, "c", 50, 4992, DateTimes.of("2011-01-06"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-06T01"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-07"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-07T01"), "a", 50, 4991, "b", 50, 4990, "c", 50, 4989, DateTimes.of("2011-01-08"), "a", 50, 4988, "b", 50, 4987, "c", 50, 4986, DateTimes.of("2011-01-08T01"), "a", 50, 4988, "b", 50, 4987, "c", 50, 4986, DateTimes.of("2011-01-09"), "c1", 50, 4985, "b", 50, 4984, "c", 50, 4983, DateTimes.of("2011-01-09T01"), "c2", 50, 4985, "b", 50, 4984, "c", 50, 4983), runner.run(QueryPlus.wrap(query)));
}
Also used : TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) FinalizeResultsQueryRunner(org.apache.druid.query.FinalizeResultsQueryRunner) TopNQueryConfig(org.apache.druid.query.topn.TopNQueryConfig) TopNQuery(org.apache.druid.query.topn.TopNQuery) TopNQueryQueryToolChest(org.apache.druid.query.topn.TopNQueryQueryToolChest) FinalizeResultsQueryRunner(org.apache.druid.query.FinalizeResultsQueryRunner) QueryRunner(org.apache.druid.query.QueryRunner) Test(org.junit.Test)

Example 10 with TopNQueryBuilder

use of org.apache.druid.query.topn.TopNQueryBuilder in project druid by druid-io.

the class CachingQueryRunnerTest method testNullCacheKeyPrefix.

@Test
public void testNullCacheKeyPrefix() {
    Query query = new TopNQueryBuilder().dataSource("ds").dimension("top_dim").metric("imps").threshold(3).intervals("2011-01-05/2011-01-10").aggregators(AGGS).granularity(Granularities.ALL).build();
    QueryToolChest toolchest = new TopNQueryQueryToolChest(new TopNQueryConfig());
    Cache cache = EasyMock.mock(Cache.class);
    EasyMock.replay(cache);
    CachingQueryRunner queryRunner = makeCachingQueryRunner(null, cache, toolchest, Sequences.empty());
    Assert.assertFalse(queryRunner.canPopulateCache(query, toolchest.getCacheStrategy(query)));
    Assert.assertFalse(queryRunner.canUseCache(query, toolchest.getCacheStrategy(query)));
    queryRunner.run(QueryPlus.wrap(query));
    EasyMock.verifyUnexpectedCalls(cache);
}
Also used : TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) Query(org.apache.druid.query.Query) TopNQueryConfig(org.apache.druid.query.topn.TopNQueryConfig) TopNQueryQueryToolChest(org.apache.druid.query.topn.TopNQueryQueryToolChest) TopNQueryQueryToolChest(org.apache.druid.query.topn.TopNQueryQueryToolChest) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) QueryToolChest(org.apache.druid.query.QueryToolChest) MapCache(org.apache.druid.client.cache.MapCache) Cache(org.apache.druid.client.cache.Cache) Test(org.junit.Test)

Aggregations

TopNQueryBuilder (org.apache.druid.query.topn.TopNQueryBuilder)55 Test (org.junit.Test)44 TopNQuery (org.apache.druid.query.topn.TopNQuery)25 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)22 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)16 InvertedTopNMetricSpec (org.apache.druid.query.topn.InvertedTopNMetricSpec)14 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)13 TopNQueryQueryToolChest (org.apache.druid.query.topn.TopNQueryQueryToolChest)13 Result (org.apache.druid.query.Result)12 TopNQueryConfig (org.apache.druid.query.topn.TopNQueryConfig)12 TopNResultValue (org.apache.druid.query.topn.TopNResultValue)11 DoubleMinAggregatorFactory (org.apache.druid.query.aggregation.DoubleMinAggregatorFactory)9 DimensionTopNMetricSpec (org.apache.druid.query.topn.DimensionTopNMetricSpec)9 NumericTopNMetricSpec (org.apache.druid.query.topn.NumericTopNMetricSpec)9 DoubleMaxAggregatorFactory (org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory)7 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)7 ByteBuffer (java.nio.ByteBuffer)5 QueryRunner (org.apache.druid.query.QueryRunner)5 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)5 HashMap (java.util.HashMap)4