Search in sources :

Example 21 with CardinalityAggregatorFactory

use of org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory 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 }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) ArithmeticPostAggregator(org.apache.druid.query.aggregation.post.ArithmeticPostAggregator) FinalizingFieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) QueryDataSource(org.apache.druid.query.QueryDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) HyperUniqueFinalizingPostAggregator(org.apache.druid.query.aggregation.hyperloglog.HyperUniqueFinalizingPostAggregator) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 22 with CardinalityAggregatorFactory

use of org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory in project druid by druid-io.

the class CalciteQueryTest method testCountDistinctOfLookup.

@Test
public void testCountDistinctOfLookup() throws Exception {
    // Cannot vectorize due to extraction dimension spec.
    cannotVectorize();
    final RegisteredLookupExtractionFn extractionFn = new RegisteredLookupExtractionFn(null, "lookyloo", false, null, null, true);
    testQuery("SELECT COUNT(DISTINCT LOOKUP(dim1, 'lookyloo')) FROM foo", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(aggregators(new CardinalityAggregatorFactory("a0", null, ImmutableList.of(new ExtractionDimensionSpec("dim1", null, extractionFn)), false, true))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { NullHandling.replaceWithDefault() ? 2L : 1L }));
}
Also used : RegisteredLookupExtractionFn(org.apache.druid.query.lookup.RegisteredLookupExtractionFn) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) Test(org.junit.Test)

Aggregations

CardinalityAggregatorFactory (org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory)22 Test (org.junit.Test)21 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)13 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)13 ExtractionDimensionSpec (org.apache.druid.query.dimension.ExtractionDimensionSpec)6 Result (org.apache.druid.query.Result)5 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)5 HyperUniquesAggregatorFactory (org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory)5 QueryDataSource (org.apache.druid.query.QueryDataSource)3 TableDataSource (org.apache.druid.query.TableDataSource)3 ExpressionLambdaAggregatorFactory (org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory)3 FilteredAggregatorFactory (org.apache.druid.query.aggregation.FilteredAggregatorFactory)3 HyperUniqueFinalizingPostAggregator (org.apache.druid.query.aggregation.hyperloglog.HyperUniqueFinalizingPostAggregator)3 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)3 SubstringDimExtractionFn (org.apache.druid.query.extraction.SubstringDimExtractionFn)3 ExpressionVirtualColumn (org.apache.druid.segment.virtual.ExpressionVirtualColumn)3 Parameters (junitparams.Parameters)2 GlobalTableDataSource (org.apache.druid.query.GlobalTableDataSource)2 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)2 LongAnyAggregatorFactory (org.apache.druid.query.aggregation.any.LongAnyAggregatorFactory)2