Search in sources :

Example 1 with IntervalDimFilter

use of org.apache.druid.query.filter.IntervalDimFilter in project druid by druid-io.

the class FiltrationTest method testNotIntervals.

@Test
public void testNotIntervals() {
    final Filtration filtration = Filtration.create(new NotDimFilter(new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, ImmutableList.of(Intervals.of("2000/2001"), Intervals.of("2002/2003")), null)), null).optimize(RowSignature.builder().add(ColumnHolder.TIME_COLUMN_NAME, ColumnType.LONG).build());
    Assert.assertEquals(ImmutableList.of(Filtration.eternity()), filtration.getIntervals());
    Assert.assertEquals(new NotDimFilter(new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, ImmutableList.of(Intervals.of("2000/2001"), Intervals.of("2002/2003")), null)), filtration.getDimFilter());
}
Also used : NotDimFilter(org.apache.druid.query.filter.NotDimFilter) IntervalDimFilter(org.apache.druid.query.filter.IntervalDimFilter) Test(org.junit.Test)

Example 2 with IntervalDimFilter

use of org.apache.druid.query.filter.IntervalDimFilter in project druid by druid-io.

the class TimeFilteringTest method testIntervalFilterOnStringDimension.

@Test
public void testIntervalFilterOnStringDimension() {
    assertFilterMatches(new IntervalDimFilter("dim0", Collections.singletonList(Intervals.of("1970-01-01T00:00:00.001Z/1970-01-01T00:00:00.005Z")), null), ImmutableList.of("1", "2", "3", "4"));
    assertFilterMatches(new IntervalDimFilter("dim0", Arrays.asList(Intervals.of("1970-01-01T00:00:00.000Z/1970-01-01T00:00:00.003Z"), Intervals.of("1970-01-01T00:00:00.004Z/1970-01-01T00:00:00.006Z")), null), ImmutableList.of("0", "1", "2", "4", "5"));
    assertFilterMatches(new IntervalDimFilter("dim0", Arrays.asList(Intervals.of("1970-01-01T00:00:00.000Z/1970-01-01T00:00:00.001Z"), Intervals.of("1970-01-01T00:00:00.003Z/1970-01-01T00:00:00.006Z"), Intervals.of("1970-01-01T00:00:00.002Z/1970-01-01T00:00:00.005Z")), null), ImmutableList.of("0", "2", "3", "4", "5"));
    assertFilterMatches(new IntervalDimFilter("dim1", Collections.singletonList(Intervals.of("1970-01-01T00:00:00.002Z/1970-01-01T00:00:00.011Z")), null), ImmutableList.of("1", "2"));
    // increment timestamp by 2 hours
    String timeBoosterJsFn = "function(x) { return(Number(x) + 7200000) }";
    ExtractionFn exFn = new JavaScriptExtractionFn(timeBoosterJsFn, true, JavaScriptConfig.getEnabledInstance());
    assertFilterMatchesSkipVectorize(new IntervalDimFilter("dim0", Collections.singletonList(Intervals.of("1970-01-01T02:00:00.001Z/1970-01-01T02:00:00.005Z")), exFn), ImmutableList.of("1", "2", "3", "4"));
}
Also used : ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) IntervalDimFilter(org.apache.druid.query.filter.IntervalDimFilter) Test(org.junit.Test)

Example 3 with IntervalDimFilter

use of org.apache.druid.query.filter.IntervalDimFilter in project druid by druid-io.

the class FilteredAggregatorFactory method optimizeForSegment.

@Override
public AggregatorFactory optimizeForSegment(PerSegmentQueryOptimizationContext optimizationContext) {
    if (dimFilter instanceof IntervalDimFilter) {
        IntervalDimFilter intervalDimFilter = ((IntervalDimFilter) dimFilter);
        if (intervalDimFilter.getExtractionFn() != null) {
            // no support for extraction functions right now
            return this;
        }
        if (!intervalDimFilter.getDimension().equals(ColumnHolder.TIME_COLUMN_NAME)) {
            // segment time boundary optimization only applies when we filter on __time
            return this;
        }
        Interval segmentInterval = optimizationContext.getSegmentDescriptor().getInterval();
        List<Interval> filterIntervals = intervalDimFilter.getIntervals();
        List<Interval> excludedFilterIntervals = new ArrayList<>();
        List<Interval> effectiveFilterIntervals = new ArrayList<>();
        boolean segmentIsCovered = false;
        for (Interval filterInterval : filterIntervals) {
            Interval overlap = filterInterval.overlap(segmentInterval);
            if (overlap == null) {
                excludedFilterIntervals.add(filterInterval);
                continue;
            }
            if (overlap.equals(segmentInterval)) {
                segmentIsCovered = true;
                break;
            } else {
                // clip the overlapping interval to the segment time boundaries
                effectiveFilterIntervals.add(overlap);
            }
        }
        // we can skip applying this filter, everything in the segment will match
        if (segmentIsCovered) {
            return delegate;
        }
        // we can skip this filter, nothing in the segment would match
        if (excludedFilterIntervals.size() == filterIntervals.size()) {
            return new SuppressedAggregatorFactory(delegate);
        }
        return new FilteredAggregatorFactory(delegate, new IntervalDimFilter(intervalDimFilter.getDimension(), effectiveFilterIntervals, intervalDimFilter.getExtractionFn(), intervalDimFilter.getFilterTuning()), this.name);
    } else {
        return this;
    }
}
Also used : ArrayList(java.util.ArrayList) IntervalDimFilter(org.apache.druid.query.filter.IntervalDimFilter) Interval(org.joda.time.Interval)

Example 4 with IntervalDimFilter

use of org.apache.druid.query.filter.IntervalDimFilter in project druid by druid-io.

the class TimeCompareBenchmark method setupQueries.

private void setupQueries() {
    // queries for the basic schema
    GeneratorSchemaInfo basicSchema = GeneratorBasicSchemas.SCHEMA_MAP.get("basic");
    QuerySegmentSpec intervalSpec = new MultipleIntervalSegmentSpec(Collections.singletonList(basicSchema.getDataInterval()));
    long startMillis = basicSchema.getDataInterval().getStartMillis();
    long endMillis = basicSchema.getDataInterval().getEndMillis();
    long half = (endMillis - startMillis) / 2;
    Interval recent = Intervals.utc(half, endMillis);
    Interval previous = Intervals.utc(startMillis, half);
    log.info("Recent interval: " + recent);
    log.info("Previous interval: " + previous);
    {
        // basic.topNTimeCompare
        List<AggregatorFactory> queryAggs = new ArrayList<>();
        queryAggs.add(new FilteredAggregatorFactory(// jsAgg1,
        new LongSumAggregatorFactory("sumLongSequential", "sumLongSequential"), new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, Collections.singletonList(recent), null)));
        queryAggs.add(new FilteredAggregatorFactory(new LongSumAggregatorFactory("_cmp_sumLongSequential", "sumLongSequential"), new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, Collections.singletonList(previous), null)));
        TopNQueryBuilder queryBuilderA = new TopNQueryBuilder().dataSource("blah").granularity(Granularities.ALL).dimension("dimUniform").metric("sumLongSequential").intervals(intervalSpec).aggregators(queryAggs).threshold(threshold);
        topNQuery = queryBuilderA.build();
        topNFactory = new TopNQueryRunnerFactory(new StupidPool<>("TopNBenchmark-compute-bufferPool", new OffheapBufferGenerator("compute", 250000000), 0, Integer.MAX_VALUE), new TopNQueryQueryToolChest(new TopNQueryConfig()), QueryBenchmarkUtil.NOOP_QUERYWATCHER);
    }
    {
        // basic.timeseriesTimeCompare
        List<AggregatorFactory> queryAggs = new ArrayList<>();
        queryAggs.add(new FilteredAggregatorFactory(new LongSumAggregatorFactory("sumLongSequential", "sumLongSequential"), new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, Collections.singletonList(recent), null)));
        queryAggs.add(new FilteredAggregatorFactory(new LongSumAggregatorFactory("_cmp_sumLongSequential", "sumLongSequential"), new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, Collections.singletonList(previous), null)));
        Druids.TimeseriesQueryBuilder timeseriesQueryBuilder = Druids.newTimeseriesQueryBuilder().dataSource("blah").granularity(Granularities.ALL).intervals(intervalSpec).aggregators(queryAggs).descending(false);
        timeseriesQuery = timeseriesQueryBuilder.build();
        timeseriesFactory = new TimeseriesQueryRunnerFactory(new TimeseriesQueryQueryToolChest(), new TimeseriesQueryEngine(), QueryBenchmarkUtil.NOOP_QUERYWATCHER);
    }
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) GeneratorSchemaInfo(org.apache.druid.segment.generator.GeneratorSchemaInfo) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) TimeseriesQueryEngine(org.apache.druid.query.timeseries.TimeseriesQueryEngine) TimeseriesQueryRunnerFactory(org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory) OffheapBufferGenerator(org.apache.druid.offheap.OffheapBufferGenerator) TopNQueryConfig(org.apache.druid.query.topn.TopNQueryConfig) TopNQueryRunnerFactory(org.apache.druid.query.topn.TopNQueryRunnerFactory) QuerySegmentSpec(org.apache.druid.query.spec.QuerySegmentSpec) List(java.util.List) ArrayList(java.util.ArrayList) TopNQueryQueryToolChest(org.apache.druid.query.topn.TopNQueryQueryToolChest) IntervalDimFilter(org.apache.druid.query.filter.IntervalDimFilter) Interval(org.joda.time.Interval)

Example 5 with IntervalDimFilter

use of org.apache.druid.query.filter.IntervalDimFilter 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)

Aggregations

IntervalDimFilter (org.apache.druid.query.filter.IntervalDimFilter)7 Test (org.junit.Test)5 Interval (org.joda.time.Interval)4 FilteredAggregatorFactory (org.apache.druid.query.aggregation.FilteredAggregatorFactory)3 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)3 ArrayList (java.util.ArrayList)2 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)2 SuppressedAggregatorFactory (org.apache.druid.query.aggregation.SuppressedAggregatorFactory)2 ExtractionFn (org.apache.druid.query.extraction.ExtractionFn)2 JavaScriptExtractionFn (org.apache.druid.query.extraction.JavaScriptExtractionFn)2 TimeFormatExtractionFn (org.apache.druid.query.extraction.TimeFormatExtractionFn)2 LookupExtractionFn (org.apache.druid.query.lookup.LookupExtractionFn)2 List (java.util.List)1 OffheapBufferGenerator (org.apache.druid.offheap.OffheapBufferGenerator)1 NotDimFilter (org.apache.druid.query.filter.NotDimFilter)1 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)1 QuerySegmentSpec (org.apache.druid.query.spec.QuerySegmentSpec)1 TimeseriesQueryEngine (org.apache.druid.query.timeseries.TimeseriesQueryEngine)1 TimeseriesQueryQueryToolChest (org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest)1 TimeseriesQueryRunnerFactory (org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory)1