use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteSelectQueryTest method testSelectCountStar.
@Test
public void testSelectCountStar() throws Exception {
// timeseries with all granularity have a single group, so should return default results for given aggregators
// which for count is 0 and sum is null in sql compatible mode or 0.0 in default mode.
testQuery(PLANNER_CONFIG_DEFAULT, QUERY_CONTEXT_DEFAULT, "SELECT exp(count(*)) + 10, sum(m2) FROM druid.foo WHERE dim2 = 0", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).filters(bound("dim2", "0", "0", false, false, null, StringComparators.NUMERIC)).granularity(Granularities.ALL).aggregators(aggregators(new CountAggregatorFactory("a0"), new DoubleSumAggregatorFactory("a1", "m2"))).postAggregators(expressionPostAgg("p0", "(exp(\"a0\") + 10)")).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 11.0, NullHandling.defaultDoubleValue() }));
testQuery(PLANNER_CONFIG_DEFAULT, QUERY_CONTEXT_DEFAULT, "SELECT exp(count(*)) + 10, sum(m2) FROM druid.foo WHERE __time >= TIMESTAMP '2999-01-01 00:00:00'", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Intervals.of("2999-01-01T00:00:00.000Z/146140482-04-24T15:36:27.903Z"))).granularity(Granularities.ALL).aggregators(aggregators(new CountAggregatorFactory("a0"), new DoubleSumAggregatorFactory("a1", "m2"))).postAggregators(expressionPostAgg("p0", "(exp(\"a0\") + 10)")).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 11.0, NullHandling.defaultDoubleValue() }));
// this behavior was not always correct, so make sure legacy behavior can be retained by skipping empty buckets
// explicitly in the context which causes these timeseries queries to return no results
testQuery(PLANNER_CONFIG_DEFAULT, TIMESERIES_CONTEXT_BY_GRAN, "SELECT COUNT(*) FROM foo WHERE dim1 = 'nonexistent'", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).filters(selector("dim1", "nonexistent", null)).granularity(Granularities.ALL).aggregators(aggregators(new CountAggregatorFactory("a0"))).context(TIMESERIES_CONTEXT_BY_GRAN).build()), ImmutableList.of());
// timeseries with a granularity is grouping by the time expression, so matching nothing returns no results
testQuery("SELECT COUNT(*) FROM foo WHERE dim1 = 'nonexistent' GROUP BY FLOOR(__time TO DAY)", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).filters(selector("dim1", "nonexistent", null)).granularity(Granularities.DAY).aggregators(aggregators(new CountAggregatorFactory("a0"))).context(getTimeseriesContextWithFloorTime(TIMESERIES_CONTEXT_BY_GRAN, "d0")).build()), ImmutableList.of());
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testSelfJoin.
@Test
public void testSelfJoin() throws Exception {
// Cannot vectorize due to virtual columns.
cannotVectorize();
testQuery("SELECT COUNT(*) FROM druid.foo x, druid.foo y\n", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(join(new TableDataSource(CalciteTests.DATASOURCE1), new QueryDataSource(newScanQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).columns(ImmutableList.of("__time", "cnt", "dim1", "dim2", "dim3", "m1", "m2", "unique_dim1")).resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST).context(QUERY_CONTEXT_DEFAULT).build()), "j0.", "1", JoinType.INNER)).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(aggregators(new CountAggregatorFactory("a0"))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 36L }));
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testViewAndJoin.
@Test
public void testViewAndJoin() throws Exception {
cannotVectorize();
Map<String, Object> queryContext = withLeftDirectAccessEnabled(QUERY_CONTEXT_DEFAULT);
testQuery("SELECT COUNT(*) FROM view.cview as a INNER JOIN druid.foo d on d.dim2 = a.dim2 WHERE a.dim1_firstchar <> 'z' ", queryContext, ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(join(join(new TableDataSource(CalciteTests.DATASOURCE1), new QueryDataSource(newScanQueryBuilder().dataSource(CalciteTests.DATASOURCE3).intervals(querySegmentSpec(Filtration.eternity())).columns("dim2").context(queryContext).build()), "j0.", "(\"dim2\" == \"j0.dim2\")", JoinType.INNER, bound("dim2", "a", "a", false, false, null, null)), new QueryDataSource(newScanQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).columns("dim2").context(queryContext).build()), "_j0.", "('a' == \"_j0.dim2\")", JoinType.INNER)).intervals(querySegmentSpec(Filtration.eternity())).filters(not(selector("dim1", "z", new SubstringDimExtractionFn(0, 1)))).granularity(Granularities.ALL).aggregators(aggregators(new CountAggregatorFactory("a0"))).context(withLeftDirectAccessEnabled(QUERY_CONTEXT_DEFAULT)).build()), ImmutableList.of(new Object[] { 8L }));
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testGroupByLimitPushdownExtraction.
@Test
public void testGroupByLimitPushdownExtraction() throws Exception {
cannotVectorize();
testQuery("SELECT dim4, substring(dim5, 1, 1), count(*) FROM druid.numfoo WHERE dim4 = 'a' GROUP BY 1,2 LIMIT 2", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE3).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setDimensions(dimensions(new DefaultDimensionSpec("v0", "_d0"), new ExtractionDimensionSpec("dim5", "_d1", new SubstringDimExtractionFn(0, 1)))).setVirtualColumns(expressionVirtualColumn("v0", "'a'", ColumnType.STRING)).setDimFilter(selector("dim4", "a", null)).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(), 2)).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { "a", "a", 2L }, new Object[] { "a", "b", 1L }));
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testSillyQuarters.
@Test
public void testSillyQuarters() throws Exception {
// Like FLOOR(__time TO QUARTER) but silly.
// Cannot vectorize due to virtual columns.
cannotVectorize();
testQuery("SELECT CAST((EXTRACT(MONTH FROM __time) - 1 ) / 3 + 1 AS INTEGER) AS quarter, COUNT(*)\n" + "FROM foo\n" + "GROUP BY CAST((EXTRACT(MONTH FROM __time) - 1 ) / 3 + 1 AS INTEGER)", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "(((timestamp_extract(\"__time\",'MONTH','UTC') - 1) / 3) + 1)", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0", ColumnType.LONG))).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 1, 6L }));
}
Aggregations