Search in sources :

Example 6 with GroupingAggregatorFactory

use of org.apache.druid.query.aggregation.GroupingAggregatorFactory in project druid by druid-io.

the class CalciteQueryTest method testMultipleExactCountDistinctWithGroupingAndOtherAggregators.

@Test
public void testMultipleExactCountDistinctWithGroupingAndOtherAggregators() throws Exception {
    requireMergeBuffers(4);
    testQuery(PLANNER_CONFIG_NO_HLL.withOverrides(ImmutableMap.of(PlannerConfig.CTX_KEY_USE_GROUPING_SET_FOR_EXACT_DISTINCT, "true")), "SELECT FLOOR(__time to day), COUNT(distinct city), COUNT(distinct user) FROM druid.visits GROUP BY 1", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(GroupByQuery.builder().setDataSource(new QueryDataSource(GroupByQuery.builder().setDataSource(CalciteTests.USERVISITDATASOURCE).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "timestamp_floor(\"__time\",'P1D',null,'UTC')", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0", ColumnType.LONG), new DefaultDimensionSpec("city", "d1"), new DefaultDimensionSpec("user", "d2"))).setAggregatorSpecs(aggregators(new GroupingAggregatorFactory("a0", Arrays.asList("v0", "city", "user")))).setSubtotalsSpec(ImmutableList.of(ImmutableList.of("d0", "d1"), ImmutableList.of("d0", "d2"))).setContext(withTimestampResultContext(QUERY_CONTEXT_DEFAULT, "d0", 0, Granularities.DAY)).build())).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setDimensions(dimensions(new DefaultDimensionSpec("d0", "_d0", ColumnType.LONG))).setAggregatorSpecs(aggregators(new FilteredAggregatorFactory(new CountAggregatorFactory("_a0"), and(not(selector("d1", null, null)), selector("a0", "1", null))), new FilteredAggregatorFactory(new CountAggregatorFactory("_a1"), and(not(selector("d2", null, null)), selector("a0", "2", null))))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 1609459200000L, 3L, 2L }, new Object[] { 1609545600000L, 3L, 4L }, new Object[] { 1609632000000L, 1L, 1L }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) QueryDataSource(org.apache.druid.query.QueryDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) GroupingAggregatorFactory(org.apache.druid.query.aggregation.GroupingAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 7 with GroupingAggregatorFactory

use of org.apache.druid.query.aggregation.GroupingAggregatorFactory in project druid by druid-io.

the class CalciteQueryTest method testGroupingAggregatorWithPostAggregator.

@Test
public void testGroupingAggregatorWithPostAggregator() throws Exception {
    List<Object[]> resultList;
    if (NullHandling.sqlCompatible()) {
        resultList = ImmutableList.of(new Object[] { NULL_STRING, 2L, 0L, NULL_STRING }, new Object[] { "", 1L, 0L, "" }, new Object[] { "a", 2L, 0L, "a" }, new Object[] { "abc", 1L, 0L, "abc" }, new Object[] { NULL_STRING, 6L, 1L, "ALL" });
    } else {
        resultList = ImmutableList.of(new Object[] { "", 3L, 0L, "" }, new Object[] { "a", 2L, 0L, "a" }, new Object[] { "abc", 1L, 0L, "abc" }, new Object[] { NULL_STRING, 6L, 1L, "ALL" });
    }
    testQuery("SELECT dim2, SUM(cnt), GROUPING(dim2), \n" + "CASE WHEN GROUPING(dim2) = 1 THEN 'ALL' ELSE dim2 END\n" + "FROM druid.foo\n" + "GROUP BY GROUPING SETS ( (dim2), () )", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setDimensions(dimensions(new DefaultDimensionSpec("dim2", "d0", ColumnType.STRING))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"), new GroupingAggregatorFactory("a1", Collections.singletonList("dim2")))).setSubtotalsSpec(ImmutableList.of(ImmutableList.of("d0"), ImmutableList.of())).setPostAggregatorSpecs(Collections.singletonList(new ExpressionPostAggregator("p0", "case_searched((\"a1\" == 1),'ALL',\"d0\")", null, ExprMacroTable.nil()))).setContext(QUERY_CONTEXT_DEFAULT).build()), resultList);
}
Also used : GroupingAggregatorFactory(org.apache.druid.query.aggregation.GroupingAggregatorFactory) ExpressionPostAggregator(org.apache.druid.query.aggregation.post.ExpressionPostAggregator) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 8 with GroupingAggregatorFactory

use of org.apache.druid.query.aggregation.GroupingAggregatorFactory in project druid by druid-io.

the class CalciteQueryTest method testExactCountDistinctWithFilter.

@Test
public void testExactCountDistinctWithFilter() throws Exception {
    final String sqlQuery = "SELECT COUNT(DISTINCT foo.dim1) FILTER(WHERE foo.cnt = 1), SUM(foo.cnt) FROM druid.foo";
    // to a bug in the Calcite's rule (AggregateExpandDistinctAggregatesRule)
    try {
        testQuery(PLANNER_CONFIG_NO_HLL.withOverrides(ImmutableMap.of(PlannerConfig.CTX_KEY_USE_GROUPING_SET_FOR_EXACT_DISTINCT, "false")), // Enable exact count distinct
        sqlQuery, CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(), ImmutableList.of());
        Assert.fail("query execution should fail");
    } catch (RuntimeException e) {
        Assert.assertTrue(e.getMessage().contains("Error while applying rule AggregateExpandDistinctAggregatesRule"));
    }
    requireMergeBuffers(3);
    testQuery(PLANNER_CONFIG_NO_HLL.withOverrides(ImmutableMap.of(PlannerConfig.CTX_KEY_USE_GROUPING_SET_FOR_EXACT_DISTINCT, "true")), sqlQuery, CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(GroupByQuery.builder().setDataSource(new QueryDataSource(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", NullHandling.replaceWithDefault() ? "(\"cnt\" == 1)" : "((\"cnt\" == 1) > 0)", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("dim1", "d0", ColumnType.STRING), new DefaultDimensionSpec("v0", "d1", ColumnType.LONG))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"), new GroupingAggregatorFactory("a1", Arrays.asList("dim1", "v0")))).setSubtotalsSpec(ImmutableList.of(ImmutableList.of("d0", "d1"), ImmutableList.of())).build())).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setAggregatorSpecs(aggregators(new FilteredAggregatorFactory(new CountAggregatorFactory("_a0"), and(not(selector("d0", null, null)), selector("a1", "0", null))), new FilteredAggregatorFactory(new LongMinAggregatorFactory("_a1", "a0"), selector("a1", "3", null)))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { NullHandling.replaceWithDefault() ? 5L : 6L, 6L }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) QueryDataSource(org.apache.druid.query.QueryDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) GroupingAggregatorFactory(org.apache.druid.query.aggregation.GroupingAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) LongMinAggregatorFactory(org.apache.druid.query.aggregation.LongMinAggregatorFactory) Test(org.junit.Test)

Aggregations

GroupingAggregatorFactory (org.apache.druid.query.aggregation.GroupingAggregatorFactory)8 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)6 Test (org.junit.Test)6 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)5 QueryDataSource (org.apache.druid.query.QueryDataSource)3 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)3 FilteredAggregatorFactory (org.apache.druid.query.aggregation.FilteredAggregatorFactory)3 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)2 LongMinAggregatorFactory (org.apache.druid.query.aggregation.LongMinAggregatorFactory)2 ExpressionPostAggregator (org.apache.druid.query.aggregation.post.ExpressionPostAggregator)2 BitSet (java.util.BitSet)1 Nullable (javax.annotation.Nullable)1 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)1 ResultRow (org.apache.druid.query.groupby.ResultRow)1 Aggregation (org.apache.druid.sql.calcite.aggregation.Aggregation)1 DateTime (org.joda.time.DateTime)1