Search in sources :

Example 1 with OrderByColumnSpec

use of org.apache.druid.query.groupby.orderby.OrderByColumnSpec 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 OrderByColumnSpec

use of org.apache.druid.query.groupby.orderby.OrderByColumnSpec 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 OrderByColumnSpec

use of org.apache.druid.query.groupby.orderby.OrderByColumnSpec 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 OrderByColumnSpec

use of org.apache.druid.query.groupby.orderby.OrderByColumnSpec 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)

Example 5 with OrderByColumnSpec

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

the class CalciteQueryTest method testTimeseriesUsingTimeFloorWithTimeShift.

@Test
public void testTimeseriesUsingTimeFloorWithTimeShift() throws Exception {
    // Cannot vectorize due to virtual columns.
    cannotVectorize();
    testQuery("SELECT SUM(cnt), gran FROM (\n" + "  SELECT TIME_FLOOR(TIME_SHIFT(__time, 'P1D', -1), 'P1M') AS gran,\n" + "  cnt FROM druid.foo\n" + ") AS x\n" + "GROUP BY gran\n" + "ORDER BY gran", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "timestamp_floor(timestamp_shift(\"__time\",'P1D',-1,'UTC'),'P1M',null,'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[] { 1L, timestamp("1999-12-01") }, new Object[] { 2L, timestamp("2000-01-01") }, new Object[] { 1L, timestamp("2000-12-01") }, new Object[] { 2L, timestamp("2001-01-01") }));
}
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

OrderByColumnSpec (org.apache.druid.query.groupby.orderby.OrderByColumnSpec)111 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)105 Test (org.junit.Test)104 DefaultLimitSpec (org.apache.druid.query.groupby.orderby.DefaultLimitSpec)99 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)79 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)46 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)15 FinalizeResultsQueryRunner (org.apache.druid.query.FinalizeResultsQueryRunner)11 QueryPlus (org.apache.druid.query.QueryPlus)10 QueryRunner (org.apache.druid.query.QueryRunner)10 ResponseContext (org.apache.druid.query.context.ResponseContext)10 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)9 GreaterThanHavingSpec (org.apache.druid.query.groupby.having.GreaterThanHavingSpec)8 QuerySegmentSpec (org.apache.druid.query.spec.QuerySegmentSpec)8 ExpressionVirtualColumn (org.apache.druid.segment.virtual.ExpressionVirtualColumn)7 ListFilteredVirtualColumn (org.apache.druid.segment.virtual.ListFilteredVirtualColumn)7 ArrayList (java.util.ArrayList)6 Sequence (org.apache.druid.java.util.common.guava.Sequence)6 QueryDataSource (org.apache.druid.query.QueryDataSource)6 GroupByQuery (org.apache.druid.query.groupby.GroupByQuery)6