use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testGroupByCaseWhen.
@Test
public void testGroupByCaseWhen() throws Exception {
// Cannot vectorize due to virtual columns.
cannotVectorize();
testQuery("SELECT\n" + " CASE EXTRACT(DAY FROM __time)\n" + " WHEN m1 THEN 'match-m1'\n" + " WHEN cnt THEN 'match-cnt'\n" + " WHEN 0 THEN 'zero'" + " END," + " COUNT(*)\n" + "FROM druid.foo\n" + "GROUP BY" + " CASE EXTRACT(DAY FROM __time)\n" + " WHEN m1 THEN 'match-m1'\n" + " WHEN cnt THEN 'match-cnt'\n" + " WHEN 0 THEN 'zero'" + " END", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(" + "(CAST(timestamp_extract(\"__time\",'DAY','UTC'), 'DOUBLE') == \"m1\")," + "'match-m1'," + "(timestamp_extract(\"__time\",'DAY','UTC') == \"cnt\")," + "'match-cnt'," + "(timestamp_extract(\"__time\",'DAY','UTC') == 0)," + "'zero'," + DruidExpression.nullLiteral() + ")", ColumnType.STRING)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"))).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { NullHandling.defaultStringValue(), 2L }, new Object[] { "match-cnt", 1L }, new Object[] { "match-m1", 3L }));
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testRegexpExtractFilterViaNotNullCheck.
@Test
public void testRegexpExtractFilterViaNotNullCheck() throws Exception {
// Cannot vectorize due to extractionFn in dimension spec.
cannotVectorize();
testQuery("SELECT COUNT(*)\n" + "FROM foo\n" + "WHERE REGEXP_EXTRACT(dim1, '^1') IS NOT NULL OR REGEXP_EXTRACT('Z' || dim1, '^Z2') IS NOT NULL", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).virtualColumns(expressionVirtualColumn("v0", "regexp_extract(concat('Z',\"dim1\"),'^Z2')", ColumnType.STRING)).filters(or(not(selector("dim1", null, new RegexDimExtractionFn("^1", 0, true, null))), not(selector("v0", null, null)))).aggregators(new CountAggregatorFactory("a0")).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 3L }));
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testGroupingWithNullInFilter.
@Test
public void testGroupingWithNullInFilter() throws Exception {
// HashJoinSegmentStorageAdapter is not vectorizable
cannotVectorize();
testQuery("SELECT COUNT(*) FROM foo WHERE dim1 IN (NULL)", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(JoinDataSource.create(new TableDataSource(CalciteTests.DATASOURCE1), InlineDataSource.fromIterable(ImmutableList.of(new Object[] { null }), RowSignature.builder().add("ROW_VALUE", ColumnType.STRING).build()), "j0.", "(\"dim1\" == \"j0.ROW_VALUE\")", JoinType.INNER, null, ExprMacroTable.nil())).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(aggregators(new CountAggregatorFactory("a0"))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 0L }));
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testExactCountDistinctUsingSubqueryWithWherePushDown.
@Test
public void testExactCountDistinctUsingSubqueryWithWherePushDown() throws Exception {
testQuery("SELECT\n" + " SUM(cnt),\n" + " COUNT(*)\n" + "FROM (SELECT dim2, SUM(cnt) AS cnt FROM druid.foo GROUP BY dim2)\n" + "WHERE dim2 <> ''", ImmutableList.of(GroupByQuery.builder().setDataSource(new QueryDataSource(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setDimFilter(not(selector("dim2", "", null))).setGranularity(Granularities.ALL).setDimensions(dimensions(new DefaultDimensionSpec("dim2", "d0"))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"))).setContext(QUERY_CONTEXT_DEFAULT).build())).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("_a0", "a0"), new CountAggregatorFactory("_a1"))).setContext(QUERY_CONTEXT_DEFAULT).build()), NullHandling.replaceWithDefault() ? ImmutableList.of(new Object[] { 3L, 2L }) : ImmutableList.of(new Object[] { 5L, 3L }));
testQuery("SELECT\n" + " SUM(cnt),\n" + " COUNT(*)\n" + "FROM (SELECT dim2, SUM(cnt) AS cnt FROM druid.foo GROUP BY dim2)\n" + "WHERE dim2 IS NOT NULL", ImmutableList.of(GroupByQuery.builder().setDataSource(new QueryDataSource(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setDimFilter(not(selector("dim2", null, null))).setGranularity(Granularities.ALL).setDimensions(dimensions(new DefaultDimensionSpec("dim2", "d0"))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"))).setContext(QUERY_CONTEXT_DEFAULT).build())).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("_a0", "a0"), new CountAggregatorFactory("_a1"))).setContext(QUERY_CONTEXT_DEFAULT).build()), NullHandling.replaceWithDefault() ? ImmutableList.of(new Object[] { 3L, 2L }) : ImmutableList.of(new Object[] { 4L, 3L }));
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class GroupByQueryRunnerTest method testGroupByNumericStringsAsNumericWithDecoration.
@Test
public void testGroupByNumericStringsAsNumericWithDecoration() {
// Cannot vectorize due to regex-filtered dimension spec.
cannotVectorize();
if (config.getDefaultStrategy().equals(GroupByStrategySelector.STRATEGY_V1)) {
expectedException.expect(UnsupportedOperationException.class);
expectedException.expectMessage("GroupBy v1 only supports dimensions with an outputType of STRING.");
}
// rows with `technology` have `170000` in the qualityNumericString field
RegexFilteredDimensionSpec regexSpec = new RegexFilteredDimensionSpec(new DefaultDimensionSpec("qualityNumericString", "ql", ColumnType.LONG), "170000");
ListFilteredDimensionSpec listFilteredSpec = new ListFilteredDimensionSpec(new DefaultDimensionSpec("qualityNumericString", "qf", ColumnType.FLOAT), Sets.newHashSet("170000"), true);
GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(regexSpec, listFilteredSpec).setDimFilter(new InDimFilter("quality", Arrays.asList("entertainment", "technology"), null)).setAggregatorSpecs(new CountAggregatorFactory("count")).setGranularity(QueryRunnerTestHelper.ALL_GRAN).addOrderByColumn("ql").build();
List<ResultRow> expectedResults;
// "entertainment" rows are excluded by the decorated specs, they become empty rows
expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "ql", NullHandling.defaultLongValue(), "qf", NullHandling.defaultDoubleValue(), "count", 2L), makeRow(query, "2011-04-01", "ql", 170000L, "qf", 170000.0, "count", 2L));
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "numeric-string");
}
Aggregations