use of org.apache.druid.query.aggregation.FilteredAggregatorFactory in project druid by druid-io.
the class FixedBucketsHistogramQuantileSqlAggregatorTest method testGroupByAggregatorDefaultValues.
@Test
public void testGroupByAggregatorDefaultValues() throws Exception {
cannotVectorize();
testQuery("SELECT\n" + "dim2,\n" + "APPROX_QUANTILE_FIXED_BUCKETS(fbhist_m1, 0.01, 20, 0.0, 10.0) FILTER(WHERE dim1 = 'nonexistent'),\n" + "APPROX_QUANTILE_FIXED_BUCKETS(m1, 0.01, 20, 0.0, 10.0) FILTER(WHERE dim1 = 'nonexistent')\n" + "FROM foo WHERE dim2 = 'a' GROUP BY dim2", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setDimFilter(selector("dim2", "a", null)).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "'a'", ColumnType.STRING)).setDimensions(new DefaultDimensionSpec("v0", "d0", ColumnType.STRING)).setAggregatorSpecs(aggregators(new FilteredAggregatorFactory(new FixedBucketsHistogramAggregatorFactory("a0:agg", "fbhist_m1", 20, 0.0, 10.0, FixedBucketsHistogram.OutlierHandlingMode.IGNORE, false), selector("dim1", "nonexistent", null)), new FilteredAggregatorFactory(new FixedBucketsHistogramAggregatorFactory("a1:agg", "m1", 20, 0.0, 10.0, FixedBucketsHistogram.OutlierHandlingMode.IGNORE, false), selector("dim1", "nonexistent", null)))).setPostAggregatorSpecs(ImmutableList.of(new QuantilePostAggregator("a0", "a0:agg", 0.01f), new QuantilePostAggregator("a1", "a1:agg", 0.01f))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { "a", 0.0, 0.0 }));
}
use of org.apache.druid.query.aggregation.FilteredAggregatorFactory in project druid by druid-io.
the class IncrementalIndexTest method testFilteredAggregators.
@Test
public void testFilteredAggregators() throws Exception {
long timestamp = System.currentTimeMillis();
IncrementalIndex index = indexCreator.createIndex((Object) new AggregatorFactory[] { new CountAggregatorFactory("count"), new FilteredAggregatorFactory(new CountAggregatorFactory("count_selector_filtered"), new SelectorDimFilter("dim2", "2", null)), new FilteredAggregatorFactory(new CountAggregatorFactory("count_bound_filtered"), new BoundDimFilter("dim2", "2", "3", false, true, null, null, StringComparators.NUMERIC)), new FilteredAggregatorFactory(new CountAggregatorFactory("count_multivaldim_filtered"), new SelectorDimFilter("dim3", "b", null)), new FilteredAggregatorFactory(new CountAggregatorFactory("count_numeric_filtered"), new SelectorDimFilter("met1", "11", null)) });
index.add(new MapBasedInputRow(timestamp, Arrays.asList("dim1", "dim2", "dim3"), ImmutableMap.of("dim1", "1", "dim2", "2", "dim3", Lists.newArrayList("b", "a"), "met1", 10)));
index.add(new MapBasedInputRow(timestamp, Arrays.asList("dim1", "dim2", "dim3"), ImmutableMap.of("dim1", "3", "dim2", "4", "dim3", Lists.newArrayList("c", "d"), "met1", 11)));
Assert.assertEquals(Arrays.asList("dim1", "dim2", "dim3"), index.getDimensionNames());
Assert.assertEquals(Arrays.asList("count", "count_selector_filtered", "count_bound_filtered", "count_multivaldim_filtered", "count_numeric_filtered"), index.getMetricNames());
Assert.assertEquals(2, index.size());
final Iterator<Row> rows = index.iterator();
Row row = rows.next();
Assert.assertEquals(timestamp, row.getTimestampFromEpoch());
Assert.assertEquals(Collections.singletonList("1"), row.getDimension("dim1"));
Assert.assertEquals(Collections.singletonList("2"), row.getDimension("dim2"));
Assert.assertEquals(Arrays.asList("a", "b"), row.getDimension("dim3"));
Assert.assertEquals(1L, row.getMetric("count"));
Assert.assertEquals(1L, row.getMetric("count_selector_filtered"));
Assert.assertEquals(1L, row.getMetric("count_bound_filtered"));
Assert.assertEquals(1L, row.getMetric("count_multivaldim_filtered"));
Assert.assertEquals(0L, row.getMetric("count_numeric_filtered"));
row = rows.next();
Assert.assertEquals(timestamp, row.getTimestampFromEpoch());
Assert.assertEquals(Collections.singletonList("3"), row.getDimension("dim1"));
Assert.assertEquals(Collections.singletonList("4"), row.getDimension("dim2"));
Assert.assertEquals(Arrays.asList("c", "d"), row.getDimension("dim3"));
Assert.assertEquals(1L, row.getMetric("count"));
Assert.assertEquals(0L, row.getMetric("count_selector_filtered"));
Assert.assertEquals(0L, row.getMetric("count_bound_filtered"));
Assert.assertEquals(0L, row.getMetric("count_multivaldim_filtered"));
Assert.assertEquals(1L, row.getMetric("count_numeric_filtered"));
}
use of org.apache.druid.query.aggregation.FilteredAggregatorFactory in project druid by druid-io.
the class BaseFilterTest method selectCountUsingFilteredAggregator.
private long selectCountUsingFilteredAggregator(final DimFilter filter) {
final Sequence<Cursor> cursors = makeCursorSequence(null);
Sequence<Aggregator> aggSeq = Sequences.map(cursors, cursor -> {
Aggregator agg = new FilteredAggregatorFactory(new CountAggregatorFactory("count"), maybeOptimize(filter)).factorize(cursor.getColumnSelectorFactory());
for (; !cursor.isDone(); cursor.advance()) {
agg.aggregate();
}
return agg;
});
return aggSeq.toList().get(0).getLong();
}
use of org.apache.druid.query.aggregation.FilteredAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testStringAgg.
@Test
public void testStringAgg() throws Exception {
cannotVectorize();
testQuery("SELECT STRING_AGG(dim1,','), STRING_AGG(DISTINCT dim1, ','), STRING_AGG(DISTINCT dim1,',') FILTER(WHERE dim1 = 'shazbot') FROM foo WHERE dim1 is not null", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).filters(not(selector("dim1", null, null))).aggregators(aggregators(new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a0", ImmutableSet.of("dim1"), "__acc", "[]", "[]", true, false, false, "array_append(\"__acc\", \"dim1\")", "array_concat(\"__acc\", \"a0\")", null, "if(array_length(o) == 0, null, array_to_string(o, ','))", ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), not(selector("dim1", null, null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a1", ImmutableSet.of("dim1"), "__acc", "[]", "[]", true, false, false, "array_set_add(\"__acc\", \"dim1\")", "array_set_add_all(\"__acc\", \"a1\")", null, "if(array_length(o) == 0, null, array_to_string(o, ','))", ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), not(selector("dim1", null, null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a2", ImmutableSet.of("dim1"), "__acc", "[]", "[]", true, false, false, "array_set_add(\"__acc\", \"dim1\")", "array_set_add_all(\"__acc\", \"a2\")", null, "if(array_length(o) == 0, null, array_to_string(o, ','))", ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), and(not(selector("dim1", null, null)), selector("dim1", "shazbot", null))))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(useDefault ? new Object[] { "10.1,2,1,def,abc", "1,10.1,2,abc,def", "" } : new Object[] { ",10.1,2,1,def,abc", ",1,10.1,2,abc,def", null }));
}
use of org.apache.druid.query.aggregation.FilteredAggregatorFactory in project druid by druid-io.
the class CalciteQueryTest method testAvgDailyCountDistinct.
@Test
public void testAvgDailyCountDistinct() throws Exception {
// Cannot vectorize outer query due to inlined inner query.
cannotVectorize();
testQuery("SELECT\n" + " AVG(u)\n" + "FROM (SELECT FLOOR(__time TO DAY), APPROX_COUNT_DISTINCT(cnt) AS u FROM druid.foo GROUP BY 1)", ImmutableList.of(GroupByQuery.builder().setDataSource(new QueryDataSource(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(new PeriodGranularity(Period.days(1), null, DateTimeZone.UTC)).aggregators(new CardinalityAggregatorFactory("a0:a", null, dimensions(new DefaultDimensionSpec("cnt", "cnt", ColumnType.LONG)), false, true)).postAggregators(ImmutableList.of(new HyperUniqueFinalizingPostAggregator("a0", "a0:a"))).context(getTimeseriesContextWithFloorTime(TIMESERIES_CONTEXT_BY_GRAN, "d0")).build())).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setAggregatorSpecs(useDefault ? aggregators(new LongSumAggregatorFactory("_a0:sum", "a0"), new CountAggregatorFactory("_a0:count")) : aggregators(new LongSumAggregatorFactory("_a0:sum", "a0"), new FilteredAggregatorFactory(new CountAggregatorFactory("_a0:count"), not(selector("a0", null, null))))).setPostAggregatorSpecs(ImmutableList.of(new ArithmeticPostAggregator("_a0", "quotient", ImmutableList.of(new FieldAccessPostAggregator(null, "_a0:sum"), new FieldAccessPostAggregator(null, "_a0:count"))))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 1L }));
}
Aggregations