Search in sources :

Example 11 with ExpressionPostAggregator

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

the class CalciteQueryTest method testReturnEmptyRowWhenGroupByIsConvertedToTimeseriesWithSingleConstantDimension.

// When optimization in Grouping#applyProject is applied, and it reduces a Group By query to a timeseries, we
// want it to return empty bucket if no row matches
@Test
public void testReturnEmptyRowWhenGroupByIsConvertedToTimeseriesWithSingleConstantDimension() throws Exception {
    skipVectorize();
    testQuery("SELECT 'A' from foo WHERE m1 = 50 AND dim1 = 'wat' GROUP BY 'foobar'", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).filters(and(selector("m1", "50", null), selector("dim1", "wat", null))).granularity(Granularities.ALL).postAggregators(new ExpressionPostAggregator("p0", "'A'", null, ExprMacroTable.nil())).context(QUERY_CONTEXT_DO_SKIP_EMPTY_BUCKETS).build()), ImmutableList.of());
    // dim1 is not getting reduced to 'wat' in this case in Calcite (ProjectMergeRule is not getting applied),
    // therefore the query is not optimized to a timeseries query
    testQuery("SELECT 'A' from foo WHERE dim1 = 'wat' GROUP BY dim1", ImmutableList.of(GroupByQuery.builder().setDataSource("foo").setInterval(querySegmentSpec(Intervals.ETERNITY)).setGranularity(Granularities.ALL).addDimension(new DefaultDimensionSpec("dim1", "d0", ColumnType.STRING)).setDimFilter(selector("dim1", "wat", null)).setPostAggregatorSpecs(ImmutableList.of(new ExpressionPostAggregator("p0", "'A'", null, ExprMacroTable.nil()))).build()), ImmutableList.of());
}
Also used : ExpressionPostAggregator(org.apache.druid.query.aggregation.post.ExpressionPostAggregator) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 12 with ExpressionPostAggregator

use of org.apache.druid.query.aggregation.post.ExpressionPostAggregator 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 13 with ExpressionPostAggregator

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

the class CalciteQueryTest method testReturnEmptyRowWhenGroupByIsConvertedToTimeseriesWithMutlipleConstantDimensions.

@Test
public void testReturnEmptyRowWhenGroupByIsConvertedToTimeseriesWithMutlipleConstantDimensions() throws Exception {
    skipVectorize();
    testQuery("SELECT 'A', dim1 from foo WHERE m1 = 50 AND dim1 = 'wat' GROUP BY dim1", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).filters(and(selector("m1", "50", null), selector("dim1", "wat", null))).granularity(Granularities.ALL).postAggregators(new ExpressionPostAggregator("p0", "'A'", null, ExprMacroTable.nil()), new ExpressionPostAggregator("p1", "'wat'", null, ExprMacroTable.nil())).context(QUERY_CONTEXT_DO_SKIP_EMPTY_BUCKETS).build()), ImmutableList.of());
    // Sanity test, that even when dimensions are reduced, but should produce a valid result (i.e. when filters are
    // correct, then they should
    testQuery("SELECT 'A', dim1 from foo WHERE m1 = 2.0 AND dim1 = '10.1' GROUP BY dim1", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).filters(and(selector("m1", "2.0", null), selector("dim1", "10.1", null))).granularity(Granularities.ALL).postAggregators(new ExpressionPostAggregator("p0", "'A'", null, ExprMacroTable.nil()), new ExpressionPostAggregator("p1", "'10.1'", null, ExprMacroTable.nil())).context(QUERY_CONTEXT_DO_SKIP_EMPTY_BUCKETS).build()), ImmutableList.of(new Object[] { "A", "10.1" }));
}
Also used : ExpressionPostAggregator(org.apache.druid.query.aggregation.post.ExpressionPostAggregator) Test(org.junit.Test)

Aggregations

ExpressionPostAggregator (org.apache.druid.query.aggregation.post.ExpressionPostAggregator)13 Test (org.junit.Test)12 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)10 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)9 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)7 DefaultLimitSpec (org.apache.druid.query.groupby.orderby.DefaultLimitSpec)5 OrderByColumnSpec (org.apache.druid.query.groupby.orderby.OrderByColumnSpec)5 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)3 GreaterThanHavingSpec (org.apache.druid.query.groupby.having.GreaterThanHavingSpec)3 GroupingAggregatorFactory (org.apache.druid.query.aggregation.GroupingAggregatorFactory)2 DimFilterHavingSpec (org.apache.druid.query.groupby.having.DimFilterHavingSpec)2 EqualToHavingSpec (org.apache.druid.query.groupby.having.EqualToHavingSpec)2 OrHavingSpec (org.apache.druid.query.groupby.having.OrHavingSpec)2 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)2 PeriodGranularity (org.apache.druid.java.util.common.granularity.PeriodGranularity)1 MergeSequence (org.apache.druid.java.util.common.guava.MergeSequence)1 Sequence (org.apache.druid.java.util.common.guava.Sequence)1 ChainedExecutionQueryRunner (org.apache.druid.query.ChainedExecutionQueryRunner)1 FinalizeResultsQueryRunner (org.apache.druid.query.FinalizeResultsQueryRunner)1 QueryDataSource (org.apache.druid.query.QueryDataSource)1