Search in sources :

Example 1 with LogicalSegment

use of org.apache.druid.timeline.LogicalSegment in project druid by druid-io.

the class SegmentMetadataQueryTest method testDefaultIntervalAndFiltering.

@Test
public void testDefaultIntervalAndFiltering() {
    SegmentMetadataQuery testQuery = Druids.newSegmentMetadataQueryBuilder().dataSource("testing").toInclude(new ListColumnIncluderator(Collections.singletonList("placement"))).merge(true).build();
    /* No interval specified, should use default interval */
    Assert.assertTrue(testQuery.isUsingDefaultInterval());
    Assert.assertEquals(Intervals.ETERNITY, testQuery.getIntervals().get(0));
    Assert.assertEquals(testQuery.getIntervals().size(), 1);
    List<LogicalSegment> testSegments = Arrays.asList(new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2012-01-01/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2012-01-01T01/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2013-01-05/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2013-05-20/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2014-01-05/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2014-02-05/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2015-01-19T01/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2015-01-20T02/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    });
    /* Test default period filter */
    List<LogicalSegment> filteredSegments = new SegmentMetadataQueryQueryToolChest(new SegmentMetadataQueryConfig()).filterSegments(testQuery, testSegments);
    List<LogicalSegment> expectedSegments = Arrays.asList(new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2015-01-19T01/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2015-01-20T02/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    });
    Assert.assertEquals(filteredSegments.size(), 2);
    for (int i = 0; i < filteredSegments.size(); i++) {
        Assert.assertEquals(expectedSegments.get(i).getInterval(), filteredSegments.get(i).getInterval());
    }
    /* Test 2 year period filtering */
    SegmentMetadataQueryConfig twoYearPeriodCfg = new SegmentMetadataQueryConfig("P2Y");
    List<LogicalSegment> filteredSegments2 = new SegmentMetadataQueryQueryToolChest(twoYearPeriodCfg).filterSegments(testQuery, testSegments);
    List<LogicalSegment> expectedSegments2 = Arrays.asList(new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2013-05-20/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2014-01-05/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2014-02-05/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2015-01-19T01/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2015-01-20T02/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    });
    Assert.assertEquals(filteredSegments2.size(), 5);
    for (int i = 0; i < filteredSegments2.size(); i++) {
        Assert.assertEquals(expectedSegments2.get(i).getInterval(), filteredSegments2.get(i).getInterval());
    }
}
Also used : LogicalSegment(org.apache.druid.timeline.LogicalSegment) SegmentMetadataQuery(org.apache.druid.query.metadata.metadata.SegmentMetadataQuery) ListColumnIncluderator(org.apache.druid.query.metadata.metadata.ListColumnIncluderator) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 2 with LogicalSegment

use of org.apache.druid.timeline.LogicalSegment in project druid by druid-io.

the class DataSourceMetadataQueryTest method testFilterSegments.

@Test
public void testFilterSegments() {
    GenericQueryMetricsFactory queryMetricsFactory = DefaultGenericQueryMetricsFactory.instance();
    DataSourceQueryQueryToolChest toolChest = new DataSourceQueryQueryToolChest(queryMetricsFactory);
    List<LogicalSegment> segments = toolChest.filterSegments(null, Arrays.asList(new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2012-01-01/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2012-01-01T01/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2013-01-01/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2013-01-01T01/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2013-01-01T02/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }));
    Assert.assertEquals(segments.size(), 2);
    // should only have the latest segments.
    List<LogicalSegment> expected = Arrays.asList(new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2013-01-01/P1D");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2013-01-01T02/PT1H");
        }

        @Override
        public Interval getTrueInterval() {
            return getInterval();
        }
    });
    for (int i = 0; i < segments.size(); i++) {
        Assert.assertEquals(expected.get(i).getInterval(), segments.get(i).getInterval());
    }
}
Also used : LogicalSegment(org.apache.druid.timeline.LogicalSegment) GenericQueryMetricsFactory(org.apache.druid.query.GenericQueryMetricsFactory) DefaultGenericQueryMetricsFactory(org.apache.druid.query.DefaultGenericQueryMetricsFactory) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 3 with LogicalSegment

use of org.apache.druid.timeline.LogicalSegment in project druid by druid-io.

the class SegmentMetadataQueryQueryToolChest method filterSegments.

@Override
public <T extends LogicalSegment> List<T> filterSegments(SegmentMetadataQuery query, List<T> segments) {
    if (!query.isUsingDefaultInterval()) {
        return segments;
    }
    if (segments.size() <= 1) {
        return segments;
    }
    final T max = segments.get(segments.size() - 1);
    DateTime targetEnd = max.getInterval().getEnd();
    final Interval targetInterval = new Interval(config.getDefaultHistory(), targetEnd);
    return Lists.newArrayList(Iterables.filter(segments, input -> (input.getInterval().overlaps(targetInterval))));
}
Also used : Iterables(com.google.common.collect.Iterables) QueryPlus(org.apache.druid.query.QueryPlus) Arrays(java.util.Arrays) Granularity(org.apache.druid.java.util.common.granularity.Granularity) Comparators(org.apache.druid.java.util.common.guava.Comparators) QueryMetrics(org.apache.druid.query.QueryMetrics) Inject(com.google.inject.Inject) GenericQueryMetricsFactory(org.apache.druid.query.GenericQueryMetricsFactory) HashMap(java.util.HashMap) TimestampSpec(org.apache.druid.data.input.impl.TimestampSpec) CacheKeyBuilder(org.apache.druid.query.cache.CacheKeyBuilder) ArrayList(java.util.ArrayList) AggregatorFactoryNotMergeableException(org.apache.druid.query.aggregation.AggregatorFactoryNotMergeableException) Interval(org.joda.time.Interval) Lists(com.google.common.collect.Lists) JodaUtils(org.apache.druid.java.util.common.JodaUtils) ImmutableList(com.google.common.collect.ImmutableList) Query(org.apache.druid.query.Query) LogicalSegment(org.apache.druid.timeline.LogicalSegment) SegmentAnalysis(org.apache.druid.query.metadata.metadata.SegmentAnalysis) Map(java.util.Map) QueryRunner(org.apache.druid.query.QueryRunner) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MetricManipulationFn(org.apache.druid.query.aggregation.MetricManipulationFn) Functions(com.google.common.base.Functions) Sequence(org.apache.druid.java.util.common.guava.Sequence) BySegmentSkippingQueryRunner(org.apache.druid.query.BySegmentSkippingQueryRunner) Function(com.google.common.base.Function) ResponseContext(org.apache.druid.query.context.ResponseContext) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) MappedSequence(org.apache.druid.java.util.common.guava.MappedSequence) DateTime(org.joda.time.DateTime) QueryToolChest(org.apache.druid.query.QueryToolChest) Set(java.util.Set) Sets(com.google.common.collect.Sets) BinaryOperator(java.util.function.BinaryOperator) ColumnAnalysis(org.apache.druid.query.metadata.metadata.ColumnAnalysis) SegmentMetadataQuery(org.apache.druid.query.metadata.metadata.SegmentMetadataQuery) List(java.util.List) CombiningSequence(org.apache.druid.common.guava.CombiningSequence) CacheStrategy(org.apache.druid.query.CacheStrategy) Ordering(com.google.common.collect.Ordering) TreeMap(java.util.TreeMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DefaultGenericQueryMetricsFactory(org.apache.druid.query.DefaultGenericQueryMetricsFactory) Comparator(java.util.Comparator) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval)

Example 4 with LogicalSegment

use of org.apache.druid.timeline.LogicalSegment in project druid by druid-io.

the class DataSourceMetadataQueryTest method testFilterOverlappingSegments.

@Test
public void testFilterOverlappingSegments() {
    final GenericQueryMetricsFactory queryMetricsFactory = DefaultGenericQueryMetricsFactory.instance();
    final DataSourceQueryQueryToolChest toolChest = new DataSourceQueryQueryToolChest(queryMetricsFactory);
    final List<LogicalSegment> segments = toolChest.filterSegments(null, ImmutableList.of(new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2015/2016-08-01");
        }

        @Override
        public Interval getTrueInterval() {
            return Intervals.of("2015/2016-08-01");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2016-08-01/2017");
        }

        @Override
        public Interval getTrueInterval() {
            return Intervals.of("2016-08-01/2017");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2017/2017-08-01");
        }

        @Override
        public Interval getTrueInterval() {
            return Intervals.of("2017/2018");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2017-08-01/2017-08-02");
        }

        @Override
        public Interval getTrueInterval() {
            return Intervals.of("2017-08-01/2017-08-02");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2017-08-02/2018");
        }

        @Override
        public Interval getTrueInterval() {
            return Intervals.of("2017/2018");
        }
    }));
    final List<LogicalSegment> expected = ImmutableList.of(new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2017/2017-08-01");
        }

        @Override
        public Interval getTrueInterval() {
            return Intervals.of("2017/2018");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2017-08-01/2017-08-02");
        }

        @Override
        public Interval getTrueInterval() {
            return Intervals.of("2017-08-01/2017-08-02");
        }
    }, new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of("2017-08-02/2018");
        }

        @Override
        public Interval getTrueInterval() {
            return Intervals.of("2017/2018");
        }
    });
    Assert.assertEquals(expected.size(), segments.size());
    for (int i = 0; i < expected.size(); i++) {
        Assert.assertEquals(expected.get(i).getInterval(), segments.get(i).getInterval());
        Assert.assertEquals(expected.get(i).getTrueInterval(), segments.get(i).getTrueInterval());
    }
}
Also used : LogicalSegment(org.apache.druid.timeline.LogicalSegment) GenericQueryMetricsFactory(org.apache.druid.query.GenericQueryMetricsFactory) DefaultGenericQueryMetricsFactory(org.apache.druid.query.DefaultGenericQueryMetricsFactory) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 5 with LogicalSegment

use of org.apache.druid.timeline.LogicalSegment in project druid by druid-io.

the class SegmentMetadataQueryQueryToolChestTest method testFilterSegments.

@Test
public void testFilterSegments() {
    final SegmentMetadataQueryConfig config = new SegmentMetadataQueryConfig();
    final SegmentMetadataQueryQueryToolChest toolChest = new SegmentMetadataQueryQueryToolChest(config);
    final List<LogicalSegment> filteredSegments = toolChest.filterSegments(Druids.newSegmentMetadataQueryBuilder().dataSource("foo").merge(true).build(), ImmutableList.of("2000-01-01/P1D", "2000-01-04/P1D", "2000-01-09/P1D", "2000-01-09/P1D").stream().map(interval -> new LogicalSegment() {

        @Override
        public Interval getInterval() {
            return Intervals.of(interval);
        }

        @Override
        public Interval getTrueInterval() {
            return Intervals.of(interval);
        }
    }).collect(Collectors.toList()));
    Assert.assertEquals(Period.weeks(1), config.getDefaultHistory());
    Assert.assertEquals(ImmutableList.of(Intervals.of("2000-01-04/P1D"), Intervals.of("2000-01-09/P1D"), Intervals.of("2000-01-09/P1D")), filteredSegments.stream().map(LogicalSegment::getInterval).collect(Collectors.toList()));
}
Also used : LogicalSegment(org.apache.druid.timeline.LogicalSegment) Interval(org.joda.time.Interval) Test(org.junit.Test)

Aggregations

LogicalSegment (org.apache.druid.timeline.LogicalSegment)5 Interval (org.joda.time.Interval)5 Test (org.junit.Test)4 DefaultGenericQueryMetricsFactory (org.apache.druid.query.DefaultGenericQueryMetricsFactory)3 GenericQueryMetricsFactory (org.apache.druid.query.GenericQueryMetricsFactory)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Function (com.google.common.base.Function)1 Functions (com.google.common.base.Functions)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 Ordering (com.google.common.collect.Ordering)1 Sets (com.google.common.collect.Sets)1 Inject (com.google.inject.Inject)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1