Search in sources :

Example 41 with FilteredAggregatorFactory

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

the class TopNQueryRunnerTest method testTopNWithExtractionFilterAndFilteredAggregatorCaseNoExistingValue.

@Test
public void testTopNWithExtractionFilterAndFilteredAggregatorCaseNoExistingValue() {
    Map<String, String> extractionMap = new HashMap<>();
    MapLookupExtractor mapLookupExtractor = new MapLookupExtractor(extractionMap, false);
    LookupExtractionFn lookupExtractionFn;
    if (NullHandling.replaceWithDefault()) {
        lookupExtractionFn = new LookupExtractionFn(mapLookupExtractor, false, null, true, false);
        extractionMap.put("", "NULL");
    } else {
        lookupExtractionFn = new LookupExtractionFn(mapLookupExtractor, false, "NULL", true, false);
    }
    DimFilter extractionFilter = new ExtractionDimFilter("null_column", "NULL", lookupExtractionFn, null);
    TopNQueryBuilder topNQueryBuilder = new TopNQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.ALL_GRAN).dimension("null_column").metric(QueryRunnerTestHelper.INDEX_METRIC).threshold(4).intervals(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).aggregators(Lists.newArrayList(Iterables.concat(commonAggregators, Lists.newArrayList(new FilteredAggregatorFactory(new DoubleMaxAggregatorFactory("maxIndex", "index"), extractionFilter), new DoubleMinAggregatorFactory("minIndex", "index"))))).postAggregators(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT);
    TopNQuery topNQueryWithNULLValueExtraction = topNQueryBuilder.filters(extractionFilter).build();
    Map<String, Object> map = new HashMap<>();
    map.put("null_column", null);
    map.put("rows", 1209L);
    map.put("index", 503332.5071372986D);
    map.put("addRowsIndexConstant", 504542.5071372986D);
    map.put("uniques", QueryRunnerTestHelper.UNIQUES_9);
    map.put("maxIndex", 1870.061029D);
    map.put("minIndex", 59.02102279663086D);
    List<Result<TopNResultValue>> expectedResults = Collections.singletonList(new Result<>(DateTimes.of("2011-01-12T00:00:00.000Z"), new TopNResultValue(Collections.singletonList(map))));
    assertExpectedResults(expectedResults, topNQueryWithNULLValueExtraction);
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) DoubleMaxAggregatorFactory(org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory) HashMap(java.util.HashMap) ExtractionDimFilter(org.apache.druid.query.filter.ExtractionDimFilter) DoubleMinAggregatorFactory(org.apache.druid.query.aggregation.DoubleMinAggregatorFactory) Result(org.apache.druid.query.Result) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) MapLookupExtractor(org.apache.druid.query.extraction.MapLookupExtractor) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) ExtractionDimFilter(org.apache.druid.query.filter.ExtractionDimFilter) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 42 with FilteredAggregatorFactory

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

the class PerSegmentQueryOptimizeTest method testFilteredAggregatorOptimize.

@Test
public void testFilteredAggregatorOptimize() {
    LongSumAggregatorFactory longSumAggregatorFactory = new LongSumAggregatorFactory("test", "test");
    FilteredAggregatorFactory aggregatorFactory = new FilteredAggregatorFactory(longSumAggregatorFactory, new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, Collections.singletonList(Intervals.utc(1000, 2000)), null));
    Interval exclude = Intervals.utc(2000, 3000);
    Interval include = Intervals.utc(1500, 1600);
    Interval partial = Intervals.utc(1500, 2500);
    AggregatorFactory excludedAgg = aggregatorFactory.optimizeForSegment(getOptimizationContext(exclude));
    AggregatorFactory expectedSuppressedAgg = new SuppressedAggregatorFactory(longSumAggregatorFactory);
    Assert.assertEquals(expectedSuppressedAgg, excludedAgg);
    AggregatorFactory includedAgg = aggregatorFactory.optimizeForSegment(getOptimizationContext(include));
    Assert.assertEquals(longSumAggregatorFactory, includedAgg);
    AggregatorFactory partialAgg = aggregatorFactory.optimizeForSegment(getOptimizationContext(partial));
    AggregatorFactory expectedPartialFilteredAgg = new FilteredAggregatorFactory(longSumAggregatorFactory, new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, Collections.singletonList(Intervals.utc(1500, 2000)), null));
    Assert.assertEquals(expectedPartialFilteredAgg, partialAgg);
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) SuppressedAggregatorFactory(org.apache.druid.query.aggregation.SuppressedAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) IntervalDimFilter(org.apache.druid.query.filter.IntervalDimFilter) SuppressedAggregatorFactory(org.apache.druid.query.aggregation.SuppressedAggregatorFactory) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 43 with FilteredAggregatorFactory

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

the class PerSegmentQueryOptimizeTest method testFilteredAggregatorDontOptimizeOnNonTimeColumn.

@Test
public void testFilteredAggregatorDontOptimizeOnNonTimeColumn() {
    // Filter is not on __time, so no optimizations should be made.
    LongSumAggregatorFactory longSumAggregatorFactory = new LongSumAggregatorFactory("test", "test");
    FilteredAggregatorFactory aggregatorFactory = new FilteredAggregatorFactory(longSumAggregatorFactory, new IntervalDimFilter("not_time", Collections.singletonList(Intervals.utc(1000, 2000)), null));
    Interval exclude = Intervals.utc(2000, 3000);
    Interval include = Intervals.utc(1500, 1600);
    Interval partial = Intervals.utc(1500, 2500);
    AggregatorFactory excludedAgg = aggregatorFactory.optimizeForSegment(getOptimizationContext(exclude));
    Assert.assertEquals(aggregatorFactory, excludedAgg);
    AggregatorFactory includedAgg = aggregatorFactory.optimizeForSegment(getOptimizationContext(include));
    Assert.assertEquals(aggregatorFactory, includedAgg);
    AggregatorFactory partialAgg = aggregatorFactory.optimizeForSegment(getOptimizationContext(partial));
    Assert.assertEquals(aggregatorFactory, partialAgg);
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) SuppressedAggregatorFactory(org.apache.druid.query.aggregation.SuppressedAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) IntervalDimFilter(org.apache.druid.query.filter.IntervalDimFilter) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 44 with FilteredAggregatorFactory

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

the class TimeseriesQueryRunnerTest method testTimeSeriesWithFilteredAgg.

@Test
public void testTimeSeriesWithFilteredAgg() {
    TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.ALL_GRAN).intervals(QueryRunnerTestHelper.FIRST_TO_THIRD).aggregators(Lists.newArrayList(Iterables.concat(aggregatorFactoryList, ImmutableList.of(new FilteredAggregatorFactory(new CountAggregatorFactory("filteredAgg"), new SelectorDimFilter(QueryRunnerTestHelper.MARKET_DIMENSION, "spot", null)))))).postAggregators(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT).descending(descending).context(makeContext()).build();
    Iterable<Result<TimeseriesResultValue>> actualResults = runner.run(QueryPlus.wrap(query)).toList();
    List<Result<TimeseriesResultValue>> expectedResults = Collections.singletonList(new Result<>(DateTimes.of("2011-04-01"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("filteredAgg", 18L).put("addRowsIndexConstant", 12486.361190795898d).put("index", 12459.361190795898d).put("uniques", 9.019833517963864d).put("rows", 26L).build())));
    assertExpectedResults(expectedResults, actualResults);
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) Result(org.apache.druid.query.Result) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 45 with FilteredAggregatorFactory

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

the class BloomFilterSqlAggregatorTest method testGroupByAggregatorDefaultValues.

@Test
public void testGroupByAggregatorDefaultValues() throws Exception {
    // makes empty bloom filters
    cannotVectorize();
    BloomKFilter expected1 = new BloomKFilter(TEST_NUM_ENTRIES);
    BloomKFilter expected2 = new BloomKFilter(TEST_NUM_ENTRIES);
    testQuery("SELECT\n" + "dim2,\n" + "BLOOM_FILTER(dim1, 1000) FILTER(WHERE dim1 = 'nonexistent'),\n" + "BLOOM_FILTER(l1, 1000) FILTER(WHERE dim1 = 'nonexistent')\n" + "FROM numfoo WHERE dim2 = 'a' GROUP BY dim2", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE3).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 BloomFilterAggregatorFactory("a0:agg", new DefaultDimensionSpec("dim1", "a0:dim1"), TEST_NUM_ENTRIES), selector("dim1", "nonexistent", null)), new FilteredAggregatorFactory(new BloomFilterAggregatorFactory("a1:agg", new DefaultDimensionSpec("l1", "a1:l1", ColumnType.LONG), TEST_NUM_ENTRIES), selector("dim1", "nonexistent", null)))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { "a", CalciteTests.getJsonMapper().writeValueAsString(expected1), CalciteTests.getJsonMapper().writeValueAsString(expected2) }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) BloomFilterAggregatorFactory(org.apache.druid.query.aggregation.bloom.BloomFilterAggregatorFactory) BloomKFilter(org.apache.druid.query.filter.BloomKFilter) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) BaseCalciteQueryTest(org.apache.druid.sql.calcite.BaseCalciteQueryTest) Test(org.junit.Test)

Aggregations

FilteredAggregatorFactory (org.apache.druid.query.aggregation.FilteredAggregatorFactory)63 Test (org.junit.Test)52 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)28 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)23 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)23 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)20 QueryDataSource (org.apache.druid.query.QueryDataSource)14 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)14 ExpressionLambdaAggregatorFactory (org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory)13 NotDimFilter (org.apache.druid.query.filter.NotDimFilter)11 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)10 BaseCalciteQueryTest (org.apache.druid.sql.calcite.BaseCalciteQueryTest)10 ArithmeticPostAggregator (org.apache.druid.query.aggregation.post.ArithmeticPostAggregator)8 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)8 BoundDimFilter (org.apache.druid.query.filter.BoundDimFilter)8 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)8 Result (org.apache.druid.query.Result)7 LongMaxAggregatorFactory (org.apache.druid.query.aggregation.LongMaxAggregatorFactory)7 TableDataSource (org.apache.druid.query.TableDataSource)6 DimFilter (org.apache.druid.query.filter.DimFilter)6