Search in sources :

Example 1 with DefaultLimitSpec

use of org.apache.druid.query.groupby.orderby.DefaultLimitSpec in project druid by druid-io.

the class CalciteQueryTest method testGroupingSetsWithOrderByAggregator.

@Test
public void testGroupingSetsWithOrderByAggregator() throws Exception {
    // Cannot vectorize due to virtual columns.
    cannotVectorize();
    testQuery("SELECT dim2, gran, SUM(cnt)\n" + "FROM (SELECT FLOOR(__time TO MONTH) AS gran, COALESCE(dim2, '') dim2, cnt FROM druid.foo) AS x\n" + "GROUP BY GROUPING SETS ( (), (dim2), (gran) )\n" + "ORDER BY SUM(cnt)\n", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(notnull(\"dim2\"),\"dim2\",'')", ColumnType.STRING), expressionVirtualColumn("v1", "timestamp_floor(\"__time\",'P1M',null,'UTC')", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"), new DefaultDimensionSpec("v1", "d1", ColumnType.LONG))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"))).setSubtotalsSpec(ImmutableList.of(ImmutableList.of("d0"), ImmutableList.of("d1"), ImmutableList.of())).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(new OrderByColumnSpec("a0", Direction.ASCENDING, StringComparators.NUMERIC)), Integer.MAX_VALUE)).setContext(withTimestampResultContext(QUERY_CONTEXT_DEFAULT, "d1", 1, Granularities.MONTH)).build()), ImmutableList.of(new Object[] { "abc", null, 1L }, new Object[] { "a", null, 2L }, new Object[] { "", null, 3L }, new Object[] { NULL_STRING, timestamp("2000-01-01"), 3L }, new Object[] { NULL_STRING, timestamp("2001-01-01"), 3L }, new Object[] { NULL_STRING, null, 6L }));
}
Also used : OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 2 with DefaultLimitSpec

use of org.apache.druid.query.groupby.orderby.DefaultLimitSpec in project druid by druid-io.

the class CalciteQueryTest method testGroupingSetsWithOrderByDimension.

@Test
public void testGroupingSetsWithOrderByDimension() throws Exception {
    // Cannot vectorize due to virtual columns.
    cannotVectorize();
    testQuery("SELECT dim2, gran, SUM(cnt)\n" + "FROM (SELECT FLOOR(__time TO MONTH) AS gran, COALESCE(dim2, '') dim2, cnt FROM druid.foo) AS x\n" + "GROUP BY GROUPING SETS ( (), (dim2), (gran) )\n" + "ORDER BY gran, dim2 DESC", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(notnull(\"dim2\"),\"dim2\",'')", ColumnType.STRING), expressionVirtualColumn("v1", "timestamp_floor(\"__time\",'P1M',null,'UTC')", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"), new DefaultDimensionSpec("v1", "d1", ColumnType.LONG))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"))).setSubtotalsSpec(ImmutableList.of(ImmutableList.of("d0"), ImmutableList.of("d1"), ImmutableList.of())).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(new OrderByColumnSpec("d1", Direction.ASCENDING, StringComparators.NUMERIC), new OrderByColumnSpec("d0", Direction.DESCENDING, StringComparators.LEXICOGRAPHIC)), Integer.MAX_VALUE)).setContext(withTimestampResultContext(QUERY_CONTEXT_DEFAULT, "d1", 1, Granularities.MONTH)).build()), ImmutableList.of(new Object[] { "abc", null, 1L }, new Object[] { "a", null, 2L }, new Object[] { "", null, 3L }, new Object[] { NULL_STRING, null, 6L }, new Object[] { NULL_STRING, timestamp("2000-01-01"), 3L }, new Object[] { NULL_STRING, timestamp("2001-01-01"), 3L }));
}
Also used : OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 3 with DefaultLimitSpec

use of org.apache.druid.query.groupby.orderby.DefaultLimitSpec in project druid by druid-io.

the class CalciteQueryTest method testGroupingSetsWithOrderByAggregatorWithLimit.

@Test
public void testGroupingSetsWithOrderByAggregatorWithLimit() throws Exception {
    // Cannot vectorize due to virtual columns.
    cannotVectorize();
    testQuery("SELECT dim2, gran, SUM(cnt)\n" + "FROM (SELECT FLOOR(__time TO MONTH) AS gran, COALESCE(dim2, '') dim2, cnt FROM druid.foo) AS x\n" + "GROUP BY GROUPING SETS ( (), (dim2), (gran) )\n" + "ORDER BY SUM(cnt)\n" + "LIMIT 1", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(notnull(\"dim2\"),\"dim2\",'')", ColumnType.STRING), expressionVirtualColumn("v1", "timestamp_floor(\"__time\",'P1M',null,'UTC')", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"), new DefaultDimensionSpec("v1", "d1", ColumnType.LONG))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"))).setSubtotalsSpec(ImmutableList.of(ImmutableList.of("d0"), ImmutableList.of("d1"), ImmutableList.of())).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(new OrderByColumnSpec("a0", Direction.ASCENDING, StringComparators.NUMERIC)), 1)).setContext(withTimestampResultContext(QUERY_CONTEXT_DEFAULT, "d1", 1, Granularities.MONTH)).build()), ImmutableList.of(new Object[] { "abc", null, 1L }));
}
Also used : OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 4 with DefaultLimitSpec

use of org.apache.druid.query.groupby.orderby.DefaultLimitSpec in project druid by druid-io.

the class CalciteQueryTest method testGroupByWithForceLimitPushDown.

@Test
public void testGroupByWithForceLimitPushDown() throws Exception {
    final Map<String, Object> context = new HashMap<>(QUERY_CONTEXT_DEFAULT);
    context.put(GroupByQueryConfig.CTX_KEY_FORCE_LIMIT_PUSH_DOWN, true);
    testQuery("SELECT dim1, dim2, COUNT(*) FROM druid.foo GROUP BY dim1, dim2 limit 1", context, ImmutableList.of(new GroupByQuery.Builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setDimensions(new DefaultDimensionSpec("dim1", "d0", ColumnType.STRING), new DefaultDimensionSpec("dim2", "d1", ColumnType.STRING)).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(), 1)).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setContext(context).build()), ImmutableList.of(new Object[] { "", "a", 1L }));
}
Also used : DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) HashMap(java.util.HashMap) TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 5 with DefaultLimitSpec

use of org.apache.druid.query.groupby.orderby.DefaultLimitSpec in project druid by druid-io.

the class CalciteQueryTest method testGroupByExtractYear.

@Test
public void testGroupByExtractYear() throws Exception {
    // Cannot vectorize due to virtual columns.
    cannotVectorize();
    testQuery("SELECT\n" + "  EXTRACT(YEAR FROM __time) AS \"year\",\n" + "  SUM(cnt)\n" + "FROM druid.foo\n" + "GROUP BY EXTRACT(YEAR FROM __time)\n" + "ORDER BY 1", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "timestamp_extract(\"__time\",'YEAR','UTC')", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0", ColumnType.LONG))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"))).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(new OrderByColumnSpec("d0", OrderByColumnSpec.Direction.ASCENDING, StringComparators.NUMERIC)), Integer.MAX_VALUE)).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 2000L, 3L }, new Object[] { 2001L, 3L }));
}
Also used : OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Aggregations

DefaultLimitSpec (org.apache.druid.query.groupby.orderby.DefaultLimitSpec)113 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)107 Test (org.junit.Test)105 OrderByColumnSpec (org.apache.druid.query.groupby.orderby.OrderByColumnSpec)100 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)79 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)47 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)15 GroupByQuery (org.apache.druid.query.groupby.GroupByQuery)13 FinalizeResultsQueryRunner (org.apache.druid.query.FinalizeResultsQueryRunner)11 QueryPlus (org.apache.druid.query.QueryPlus)11 QueryRunner (org.apache.druid.query.QueryRunner)11 ResponseContext (org.apache.druid.query.context.ResponseContext)11 ResultRow (org.apache.druid.query.groupby.ResultRow)11 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)10 GreaterThanHavingSpec (org.apache.druid.query.groupby.having.GreaterThanHavingSpec)9 QuerySegmentSpec (org.apache.druid.query.spec.QuerySegmentSpec)8 ArrayList (java.util.ArrayList)7 Sequence (org.apache.druid.java.util.common.guava.Sequence)7 QueryDataSource (org.apache.druid.query.QueryDataSource)7 GroupByQueryRunnerTest (org.apache.druid.query.groupby.GroupByQueryRunnerTest)7