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());
}
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);
}
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" }));
}
Aggregations