use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class BaseFilterTest method selectCountUsingVectorizedFilteredAggregator.
private long selectCountUsingVectorizedFilteredAggregator(final DimFilter dimFilter) {
Preconditions.checkState(makeFilter(dimFilter).canVectorizeMatcher(adapter), "Cannot vectorize filter: %s", dimFilter);
try (final VectorCursor cursor = makeVectorCursor(null)) {
final FilteredAggregatorFactory aggregatorFactory = new FilteredAggregatorFactory(new CountAggregatorFactory("count"), maybeOptimize(dimFilter));
final VectorAggregator aggregator = aggregatorFactory.factorizeVector(cursor.getColumnSelectorFactory());
final ByteBuffer buf = ByteBuffer.allocate(aggregatorFactory.getMaxIntermediateSizeWithNulls() * 2);
// Use two slots: one for each form of aggregate.
aggregator.init(buf, 0);
aggregator.init(buf, aggregatorFactory.getMaxIntermediateSizeWithNulls());
for (; !cursor.isDone(); cursor.advance()) {
aggregator.aggregate(buf, 0, 0, cursor.getCurrentVectorSize());
final int[] positions = new int[cursor.getCurrentVectorSize()];
Arrays.fill(positions, aggregatorFactory.getMaxIntermediateSizeWithNulls());
final int[] allRows = new int[cursor.getCurrentVectorSize()];
for (int i = 0; i < allRows.length; i++) {
allRows[i] = i;
}
aggregator.aggregate(buf, cursor.getCurrentVectorSize(), positions, allRows, 0);
}
final long val1 = (long) aggregator.get(buf, 0);
final long val2 = (long) aggregator.get(buf, aggregatorFactory.getMaxIntermediateSizeWithNulls());
if (val1 != val2) {
throw new ISE("Oh no, val1[%d] != val2[%d]", val1, val2);
}
return val1;
}
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class IncrementalIndexTest method testgetDimensions.
@Test
public void testgetDimensions() {
final IncrementalIndex incrementalIndex = indexCreator.createIndex((builder, args) -> builder.setIndexSchema(new IncrementalIndexSchema.Builder().withMetrics(new CountAggregatorFactory("count")).withDimensionsSpec(new DimensionsSpec(DimensionsSpec.getDefaultSchemas(Arrays.asList("dim0", "dim1")))).build()).setMaxRowCount(1000000).build());
Assert.assertEquals(Arrays.asList("dim0", "dim1"), incrementalIndex.getDimensionNames());
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testGroupByCaseWhenOfTripleAnd.
@Test
public void testGroupByCaseWhenOfTripleAnd() throws Exception {
// Cannot vectorize due to virtual columns.
cannotVectorize();
testQuery("SELECT\n" + " CASE WHEN m1 > 1 AND m1 < 5 AND cnt = 1 THEN 'x' ELSE NULL END," + " COUNT(*)\n" + "FROM druid.foo\n" + "GROUP BY 1", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(((\"m1\" > 1) && (\"m1\" < 5) && (\"cnt\" == 1)),'x',null)", ColumnType.STRING)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"))).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { NullHandling.defaultStringValue(), 3L }, new Object[] { "x", 3L }));
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testCountAndAverageByConstantVirtualColumn.
@Test
public void testCountAndAverageByConstantVirtualColumn() throws Exception {
List<VirtualColumn> virtualColumns;
List<AggregatorFactory> aggs;
if (useDefault) {
aggs = ImmutableList.of(new FilteredAggregatorFactory(new CountAggregatorFactory("a0"), not(selector("v0", null, null))), new LongSumAggregatorFactory("a1:sum", null, "325323", TestExprMacroTable.INSTANCE), new CountAggregatorFactory("a1:count"));
virtualColumns = ImmutableList.of(expressionVirtualColumn("v0", "'10.1'", ColumnType.STRING));
} else {
aggs = ImmutableList.of(new FilteredAggregatorFactory(new CountAggregatorFactory("a0"), not(selector("v0", null, null))), new LongSumAggregatorFactory("a1:sum", "v1"), new FilteredAggregatorFactory(new CountAggregatorFactory("a1:count"), not(selector("v1", null, null))));
virtualColumns = ImmutableList.of(expressionVirtualColumn("v0", "'10.1'", ColumnType.STRING), expressionVirtualColumn("v1", "325323", ColumnType.LONG));
}
testQuery("SELECT dim5, COUNT(dim1), AVG(l1) FROM druid.numfoo WHERE dim1 = '10.1' AND l1 = 325323 GROUP BY dim5", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE3).setInterval(querySegmentSpec(Filtration.eternity())).setDimFilter(and(selector("dim1", "10.1", null), selector("l1", "325323", null))).setGranularity(Granularities.ALL).setVirtualColumns(VirtualColumns.create(virtualColumns)).setDimensions(new DefaultDimensionSpec("dim5", "_d0", ColumnType.STRING)).setAggregatorSpecs(aggs).setPostAggregatorSpecs(ImmutableList.of(new ArithmeticPostAggregator("a1", "quotient", ImmutableList.of(new FieldAccessPostAggregator(null, "a1:sum"), new FieldAccessPostAggregator(null, "a1:count"))))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { "ab", 1L, 325323L }));
}
use of org.apache.druid.query.aggregation.CountAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testRequireTimeConditionPositive.
@Test
public void testRequireTimeConditionPositive() throws Exception {
// simple timeseries
testQuery(PLANNER_CONFIG_REQUIRE_TIME_CONDITION, "SELECT SUM(cnt), gran FROM (\n" + " SELECT __time as t, floor(__time TO month) AS gran,\n" + " cnt FROM druid.foo\n" + ") AS x\n" + "WHERE t >= '2000-01-01' and t < '2002-01-01'" + "GROUP BY gran\n" + "ORDER BY gran", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Intervals.of("2000-01-01/2002-01-01"))).granularity(Granularities.MONTH).aggregators(aggregators(new LongSumAggregatorFactory("a0", "cnt"))).context(getTimeseriesContextWithFloorTime(TIMESERIES_CONTEXT_BY_GRAN, "d0")).build()), ImmutableList.of(new Object[] { 3L, timestamp("2000-01-01") }, new Object[] { 3L, timestamp("2001-01-01") }));
// nested groupby only requires time condition for inner most query
testQuery(PLANNER_CONFIG_REQUIRE_TIME_CONDITION, "SELECT\n" + " SUM(cnt),\n" + " COUNT(*)\n" + "FROM (SELECT dim2, SUM(cnt) AS cnt FROM druid.foo WHERE __time >= '2000-01-01' GROUP BY dim2)", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(GroupByQuery.builder().setDataSource(new QueryDataSource(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Intervals.utc(DateTimes.of("2000-01-01").getMillis(), JodaUtils.MAX_INSTANT))).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[] { 6L, 3L }) : ImmutableList.of(new Object[] { 6L, 4L }));
// Cannot vectorize next test due to extraction dimension spec.
cannotVectorize();
// semi-join requires time condition on both left and right query
testQuery(PLANNER_CONFIG_REQUIRE_TIME_CONDITION, "SELECT COUNT(*) FROM druid.foo\n" + "WHERE __time >= '2000-01-01' AND SUBSTRING(dim2, 1, 1) IN (\n" + " SELECT SUBSTRING(dim1, 1, 1) FROM druid.foo\n" + " WHERE dim1 <> '' AND __time >= '2000-01-01'\n" + ")", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(join(new TableDataSource(CalciteTests.DATASOURCE1), new QueryDataSource(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Intervals.utc(DateTimes.of("2000-01-01").getMillis(), JodaUtils.MAX_INSTANT))).setDimFilter(not(selector("dim1", NullHandling.sqlCompatible() ? "" : null, null))).setGranularity(Granularities.ALL).setDimensions(new ExtractionDimensionSpec("dim1", "d0", ColumnType.STRING, new SubstringDimExtractionFn(0, 1))).setContext(QUERY_CONTEXT_DEFAULT).build()), "j0.", equalsCondition(makeExpression("substring(\"dim2\", 0, 1)"), DruidExpression.ofColumn(ColumnType.STRING, "j0.d0")), JoinType.INNER)).intervals(querySegmentSpec(Intervals.utc(DateTimes.of("2000-01-01").getMillis(), JodaUtils.MAX_INSTANT))).granularity(Granularities.ALL).aggregators(aggregators(new CountAggregatorFactory("a0"))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 3L }));
}
Aggregations