use of org.apache.druid.query.aggregation.post.ArithmeticPostAggregator in project druid by druid-io.
the class TopNQueryQueryToolChestTest method testComputeResultLevelCacheKeyWithDifferentPostAgg.
@Test
public void testComputeResultLevelCacheKeyWithDifferentPostAgg() {
final TopNQuery query1 = new TopNQuery(new TableDataSource("dummy"), VirtualColumns.EMPTY, new DefaultDimensionSpec("test", "test"), new LegacyTopNMetricSpec("metric1"), 3, new MultipleIntervalSegmentSpec(ImmutableList.of(Intervals.of("2015-01-01T18:00:00/2015-01-02T18:00:00"))), null, Granularities.ALL, ImmutableList.of(new LongSumAggregatorFactory("metric1", "metric1"), new LongSumAggregatorFactory("metric2", "metric2")), ImmutableList.of(new ArithmeticPostAggregator("post1", "/", ImmutableList.of(new FieldAccessPostAggregator("metric1", "metric1"), new FieldAccessPostAggregator("metric2", "metric2")))), null);
final TopNQuery query2 = new TopNQuery(new TableDataSource("dummy"), VirtualColumns.EMPTY, new DefaultDimensionSpec("test", "test"), new LegacyTopNMetricSpec("metric1"), 3, new MultipleIntervalSegmentSpec(ImmutableList.of(Intervals.of("2015-01-01T18:00:00/2015-01-02T18:00:00"))), null, Granularities.ALL, ImmutableList.of(new LongSumAggregatorFactory("metric1", "metric1"), new LongSumAggregatorFactory("metric2", "metric2")), ImmutableList.of(new ArithmeticPostAggregator("post2", "+", ImmutableList.of(new FieldAccessPostAggregator("metric1", "metric1"), new FieldAccessPostAggregator("metric2", "metric2")))), null);
final CacheStrategy<Result<TopNResultValue>, Object, TopNQuery> strategy1 = new TopNQueryQueryToolChest(null, null).getCacheStrategy(query1);
final CacheStrategy<Result<TopNResultValue>, Object, TopNQuery> strategy2 = new TopNQueryQueryToolChest(null, null).getCacheStrategy(query2);
// segment level cache key excludes postaggregates in topn
Assert.assertTrue(Arrays.equals(strategy1.computeCacheKey(query1), strategy2.computeCacheKey(query2)));
Assert.assertFalse(Arrays.equals(strategy1.computeCacheKey(query1), strategy1.computeResultLevelCacheKey(query1)));
Assert.assertFalse(Arrays.equals(strategy1.computeResultLevelCacheKey(query1), strategy2.computeResultLevelCacheKey(query2)));
}
use of org.apache.druid.query.aggregation.post.ArithmeticPostAggregator in project druid by druid-io.
the class QuantileSqlAggregatorTest method testQuantileOnInnerQuery.
@Test
public void testQuantileOnInnerQuery() throws Exception {
final List<Object[]> expectedResults;
if (NullHandling.replaceWithDefault()) {
expectedResults = ImmutableList.of(new Object[] { 7.0, 8.26386833190918 });
} else {
expectedResults = ImmutableList.of(new Object[] { 5.25, 6.59091854095459 });
}
testQuery("SELECT AVG(x), APPROX_QUANTILE(x, 0.98)\n" + "FROM (SELECT dim2, SUM(m1) AS x FROM foo GROUP BY dim2)", ImmutableList.of(GroupByQuery.builder().setDataSource(new QueryDataSource(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(new MultipleIntervalSegmentSpec(ImmutableList.of(Filtration.eternity()))).setGranularity(Granularities.ALL).setDimensions(new DefaultDimensionSpec("dim2", "d0")).setAggregatorSpecs(ImmutableList.of(new DoubleSumAggregatorFactory("a0", "m1"))).setContext(QUERY_CONTEXT_DEFAULT).build())).setInterval(new MultipleIntervalSegmentSpec(ImmutableList.of(Filtration.eternity()))).setGranularity(Granularities.ALL).setAggregatorSpecs(new DoubleSumAggregatorFactory("_a0:sum", "a0"), new CountAggregatorFactory("_a0:count"), new ApproximateHistogramAggregatorFactory("_a1:agg", "a0", null, null, null, null, false)).setPostAggregatorSpecs(ImmutableList.of(new ArithmeticPostAggregator("_a0", "quotient", ImmutableList.of(new FieldAccessPostAggregator(null, "_a0:sum"), new FieldAccessPostAggregator(null, "_a0:count"))), new QuantilePostAggregator("_a1", "_a1:agg", 0.98f))).setContext(QUERY_CONTEXT_DEFAULT).build()), expectedResults);
}
use of org.apache.druid.query.aggregation.post.ArithmeticPostAggregator in project druid by druid-io.
the class AggregatorUtilTest method testNullPostAggregatorNames.
@Test
public void testNullPostAggregatorNames() {
AggregatorFactory agg1 = new DoubleSumAggregatorFactory("agg1", "value");
AggregatorFactory agg2 = new DoubleSumAggregatorFactory("agg2", "count");
PostAggregator postAgg1 = new ArithmeticPostAggregator(null, "*", Lists.newArrayList(new FieldAccessPostAggregator(null, "agg1"), new FieldAccessPostAggregator(null, "agg2")));
PostAggregator postAgg2 = new ArithmeticPostAggregator("postAgg", "/", Lists.newArrayList(new FieldAccessPostAggregator(null, "agg1"), new FieldAccessPostAggregator(null, "agg2")));
Assert.assertEquals(new Pair<>(Lists.newArrayList(agg1, agg2), Collections.singletonList(postAgg2)), AggregatorUtil.condensedAggregators(Lists.newArrayList(agg1, agg2), Lists.newArrayList(postAgg1, postAgg2), "postAgg"));
}
use of org.apache.druid.query.aggregation.post.ArithmeticPostAggregator in project druid by druid-io.
the class AggregatorUtilTest method testOutOfOrderPruneDependentPostAgg.
@Test
public void testOutOfOrderPruneDependentPostAgg() {
PostAggregator agg1 = new ArithmeticPostAggregator("abc", "+", Lists.newArrayList(new ConstantPostAggregator("1", 1L), new ConstantPostAggregator("2", 2L)));
PostAggregator dependency1 = new ArithmeticPostAggregator("dep1", "+", Lists.newArrayList(new ConstantPostAggregator("1", 1L), new ConstantPostAggregator("4", 4L)));
PostAggregator agg2 = new FieldAccessPostAggregator("def", "def");
PostAggregator dependency2 = new FieldAccessPostAggregator("dep2", "dep2");
PostAggregator aggregator = new ArithmeticPostAggregator("finalAgg", "+", Lists.newArrayList(new FieldAccessPostAggregator("dep1", "dep1"), new FieldAccessPostAggregator("dep2", "dep2")));
List<PostAggregator> prunedAgg = AggregatorUtil.pruneDependentPostAgg(Lists.newArrayList(agg1, dependency1, // dependency is added later than the aggregator
aggregator, agg2, dependency2), aggregator.getName());
Assert.assertEquals(Lists.newArrayList(dependency1, aggregator), prunedAgg);
}
use of org.apache.druid.query.aggregation.post.ArithmeticPostAggregator 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