Search in sources :

Example 1 with RectangularBound

use of org.apache.druid.collections.spatial.search.RectangularBound in project druid by druid-io.

the class SpatialFilterBonusTest method testSpatialQueryFilteredAggregator.

@Test
public void testSpatialQueryFilteredAggregator() {
    TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource("test").granularity(Granularities.DAY).intervals(Collections.singletonList(Intervals.of("2013-01-01/2013-01-07"))).aggregators(Arrays.asList(new CountAggregatorFactory("rows"), new FilteredAggregatorFactory(new LongSumAggregatorFactory("valFiltered", "val"), new SpatialDimFilter("dim.geo", new RectangularBound(new float[] { 0.0f, 0.0f }, new float[] { 9.0f, 9.0f }))), new LongSumAggregatorFactory("val", "val"))).build();
    List<Result<TimeseriesResultValue>> expectedResults = Arrays.asList(new Result<>(DateTimes.of("2013-01-01T00:00:00.000Z"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("rows", 4995L).put("val", 12497502L).put("valFiltered", 17L).build())), new Result<>(DateTimes.of("2013-01-02T00:00:00.000Z"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("rows", 1L).put("val", 29L).put("valFiltered", 29L).build())), new Result<>(DateTimes.of("2013-01-03T00:00:00.000Z"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("rows", 1L).put("val", 13L).put("valFiltered", 13L).build())), new Result<>(DateTimes.of("2013-01-04T00:00:00.000Z"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("rows", 1L).put("val", 91L).put("valFiltered", 91L).build())), new Result<>(DateTimes.of("2013-01-05T00:00:00.000Z"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("rows", 2L).put("val", 548L).put("valFiltered", 47L).build())));
    try {
        TimeseriesQueryRunnerFactory factory = new TimeseriesQueryRunnerFactory(new TimeseriesQueryQueryToolChest(), new TimeseriesQueryEngine(), QueryRunnerTestHelper.NOOP_QUERYWATCHER);
        QueryRunner runner = new FinalizeResultsQueryRunner(factory.createRunner(segment), factory.getToolchest());
        TestHelper.assertExpectedResults(expectedResults, runner.run(QueryPlus.wrap(query)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) TimeseriesResultValue(org.apache.druid.query.timeseries.TimeseriesResultValue) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) QueryRunner(org.apache.druid.query.QueryRunner) FinalizeResultsQueryRunner(org.apache.druid.query.FinalizeResultsQueryRunner) IOException(java.io.IOException) RectangularBound(org.apache.druid.collections.spatial.search.RectangularBound) Result(org.apache.druid.query.Result) TimeseriesQueryEngine(org.apache.druid.query.timeseries.TimeseriesQueryEngine) SpatialDimFilter(org.apache.druid.query.filter.SpatialDimFilter) TimeseriesQueryRunnerFactory(org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) FinalizeResultsQueryRunner(org.apache.druid.query.FinalizeResultsQueryRunner) Test(org.junit.Test)

Example 2 with RectangularBound

use of org.apache.druid.collections.spatial.search.RectangularBound in project druid by druid-io.

the class ImmutableRTreeTest method testSearchWithSplit2Roaring.

@Test
public void testSearchWithSplit2Roaring() {
    BitmapFactory bf = new RoaringBitmapFactory();
    RTree tree = new RTree(2, new LinearGutmanSplitStrategy(0, 50, bf), bf);
    tree.insert(new float[] { 0.0f, 0.0f }, 0);
    tree.insert(new float[] { 1.0f, 3.0f }, 1);
    tree.insert(new float[] { 4.0f, 2.0f }, 2);
    tree.insert(new float[] { 7.0f, 3.0f }, 3);
    tree.insert(new float[] { 8.0f, 6.0f }, 4);
    Random rand = ThreadLocalRandom.current();
    for (int i = 5; i < 5000; i++) {
        tree.insert(new float[] { (float) (rand.nextDouble() * 10 + 10.0), (float) (rand.nextDouble() * 10 + 10.0) }, i);
    }
    ImmutableRTree searchTree = ImmutableRTree.newImmutableFromMutable(tree);
    Iterable<ImmutableBitmap> points = searchTree.search(new RectangularBound(new float[] { 0, 0 }, new float[] { 9, 9 }));
    ImmutableBitmap finalSet = bf.union(points);
    Assert.assertTrue(finalSet.size() >= 5);
    Set<Integer> expected = Sets.newHashSet(0, 1, 2, 3, 4);
    IntIterator iter = finalSet.iterator();
    while (iter.hasNext()) {
        Assert.assertTrue(expected.contains(iter.next()));
    }
}
Also used : IntIterator(org.roaringbitmap.IntIterator) ImmutableBitmap(org.apache.druid.collections.bitmap.ImmutableBitmap) LinearGutmanSplitStrategy(org.apache.druid.collections.spatial.split.LinearGutmanSplitStrategy) RectangularBound(org.apache.druid.collections.spatial.search.RectangularBound) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BitmapFactory(org.apache.druid.collections.bitmap.BitmapFactory) ConciseBitmapFactory(org.apache.druid.collections.bitmap.ConciseBitmapFactory) RoaringBitmapFactory(org.apache.druid.collections.bitmap.RoaringBitmapFactory) RoaringBitmapFactory(org.apache.druid.collections.bitmap.RoaringBitmapFactory) Test(org.junit.Test)

Example 3 with RectangularBound

use of org.apache.druid.collections.spatial.search.RectangularBound in project druid by druid-io.

the class ImmutableRTreeTest method testSearchWithSplit2.

@Test
public void testSearchWithSplit2() {
    BitmapFactory bf = new ConciseBitmapFactory();
    RTree tree = new RTree(2, new LinearGutmanSplitStrategy(0, 50, bf), bf);
    tree.insert(new float[] { 0.0f, 0.0f }, 0);
    tree.insert(new float[] { 1.0f, 3.0f }, 1);
    tree.insert(new float[] { 4.0f, 2.0f }, 2);
    tree.insert(new float[] { 7.0f, 3.0f }, 3);
    tree.insert(new float[] { 8.0f, 6.0f }, 4);
    Random rand = ThreadLocalRandom.current();
    for (int i = 5; i < 5000; i++) {
        tree.insert(new float[] { (float) (rand.nextDouble() * 10 + 10.0), (float) (rand.nextDouble() * 10 + 10.0) }, i);
    }
    ImmutableRTree searchTree = ImmutableRTree.newImmutableFromMutable(tree);
    Iterable<ImmutableBitmap> points = searchTree.search(new RectangularBound(new float[] { 0, 0 }, new float[] { 9, 9 }));
    ImmutableBitmap finalSet = bf.union(points);
    Assert.assertTrue(finalSet.size() >= 5);
    Set<Integer> expected = Sets.newHashSet(0, 1, 2, 3, 4);
    IntIterator iter = finalSet.iterator();
    while (iter.hasNext()) {
        Assert.assertTrue(expected.contains(iter.next()));
    }
}
Also used : ConciseBitmapFactory(org.apache.druid.collections.bitmap.ConciseBitmapFactory) IntIterator(org.roaringbitmap.IntIterator) ImmutableBitmap(org.apache.druid.collections.bitmap.ImmutableBitmap) LinearGutmanSplitStrategy(org.apache.druid.collections.spatial.split.LinearGutmanSplitStrategy) RectangularBound(org.apache.druid.collections.spatial.search.RectangularBound) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BitmapFactory(org.apache.druid.collections.bitmap.BitmapFactory) ConciseBitmapFactory(org.apache.druid.collections.bitmap.ConciseBitmapFactory) RoaringBitmapFactory(org.apache.druid.collections.bitmap.RoaringBitmapFactory) Test(org.junit.Test)

Example 4 with RectangularBound

use of org.apache.druid.collections.spatial.search.RectangularBound in project druid by druid-io.

the class ImmutableRTreeTest method showBenchmarksBoundWithLimits.

// TODO rewrite to JMH and move to the benchmarks project
@SuppressWarnings("unused")
public void showBenchmarksBoundWithLimits() {
    // final int start = 1;
    final int start = 10000000;
    final int factor = 10;
    final int end = 10000000;
    for (int numPoints = start; numPoints <= end; numPoints *= factor) {
        try {
            BitmapFactory bf = new ConciseBitmapFactory();
            RTree tree = new RTree(2, new LinearGutmanSplitStrategy(0, 50, bf), bf);
            Stopwatch stopwatch = Stopwatch.createStarted();
            Random rand = ThreadLocalRandom.current();
            for (int i = 0; i < numPoints; i++) {
                tree.insert(new float[] { (float) (rand.nextDouble() * 100), (float) (rand.nextDouble() * 100) }, i);
            }
            long stop = stopwatch.elapsed(TimeUnit.MILLISECONDS);
            System.out.printf(Locale.ENGLISH, "[%,d]: insert = %,d ms%n", numPoints, stop);
            stopwatch.reset().start();
            ImmutableRTree searchTree = ImmutableRTree.newImmutableFromMutable(tree);
            stop = stopwatch.elapsed(TimeUnit.MILLISECONDS);
            System.out.printf(Locale.ENGLISH, "[%,d]: size = %,d bytes%n", numPoints, searchTree.toBytes().length);
            System.out.printf(Locale.ENGLISH, "[%,d]: buildImmutable = %,d ms%n", numPoints, stop);
            stopwatch.reset().start();
            Iterable<ImmutableBitmap> points = searchTree.search(new RectangularBound(new float[] { 40, 40 }, new float[] { 60, 60 }, 100));
            Iterables.size(points);
            stop = stopwatch.elapsed(TimeUnit.MILLISECONDS);
            System.out.printf(Locale.ENGLISH, "[%,d]: search = %,dms%n", numPoints, stop);
            stopwatch.reset().start();
            ImmutableBitmap finalSet = bf.union(points);
            stop = stopwatch.elapsed(TimeUnit.MILLISECONDS);
            System.out.printf(Locale.ENGLISH, "[%,d]: union of %,d points in %,d ms%n", numPoints, finalSet.size(), stop);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : ConciseBitmapFactory(org.apache.druid.collections.bitmap.ConciseBitmapFactory) ImmutableBitmap(org.apache.druid.collections.bitmap.ImmutableBitmap) Stopwatch(com.google.common.base.Stopwatch) LinearGutmanSplitStrategy(org.apache.druid.collections.spatial.split.LinearGutmanSplitStrategy) RectangularBound(org.apache.druid.collections.spatial.search.RectangularBound) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BitmapFactory(org.apache.druid.collections.bitmap.BitmapFactory) ConciseBitmapFactory(org.apache.druid.collections.bitmap.ConciseBitmapFactory) RoaringBitmapFactory(org.apache.druid.collections.bitmap.RoaringBitmapFactory)

Example 5 with RectangularBound

use of org.apache.druid.collections.spatial.search.RectangularBound in project druid by druid-io.

the class SpatialFilterBonusTest method testSpatialQueryMorePoints.

@Test
public void testSpatialQueryMorePoints() {
    TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource("test").granularity(Granularities.DAY).intervals(Collections.singletonList(Intervals.of("2013-01-01/2013-01-07"))).filters(new SpatialDimFilter("dim.geo", new RectangularBound(new float[] { 0.0f, 0.0f }, new float[] { 9.0f, 9.0f }))).aggregators(Arrays.asList(new CountAggregatorFactory("rows"), new LongSumAggregatorFactory("val", "val"))).build();
    List<Result<TimeseriesResultValue>> expectedResults = Arrays.asList(new Result<TimeseriesResultValue>(DateTimes.of("2013-01-01T00:00:00.000Z"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("rows", 1L).put("val", 17L).build())), new Result<TimeseriesResultValue>(DateTimes.of("2013-01-02T00:00:00.000Z"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("rows", 1L).put("val", 29L).build())), new Result<TimeseriesResultValue>(DateTimes.of("2013-01-03T00:00:00.000Z"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("rows", 1L).put("val", 13L).build())), new Result<TimeseriesResultValue>(DateTimes.of("2013-01-04T00:00:00.000Z"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("rows", 1L).put("val", 91L).build())), new Result<TimeseriesResultValue>(DateTimes.of("2013-01-05T00:00:00.000Z"), new TimeseriesResultValue(ImmutableMap.<String, Object>builder().put("rows", 1L).put("val", 47L).build())));
    try {
        TimeseriesQueryRunnerFactory factory = new TimeseriesQueryRunnerFactory(new TimeseriesQueryQueryToolChest(), new TimeseriesQueryEngine(), QueryRunnerTestHelper.NOOP_QUERYWATCHER);
        QueryRunner runner = new FinalizeResultsQueryRunner(factory.createRunner(segment), factory.getToolchest());
        TestHelper.assertExpectedResults(expectedResults, runner.run(QueryPlus.wrap(query)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : TimeseriesResultValue(org.apache.druid.query.timeseries.TimeseriesResultValue) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) QueryRunner(org.apache.druid.query.QueryRunner) FinalizeResultsQueryRunner(org.apache.druid.query.FinalizeResultsQueryRunner) IOException(java.io.IOException) RectangularBound(org.apache.druid.collections.spatial.search.RectangularBound) Result(org.apache.druid.query.Result) TimeseriesQueryEngine(org.apache.druid.query.timeseries.TimeseriesQueryEngine) SpatialDimFilter(org.apache.druid.query.filter.SpatialDimFilter) TimeseriesQueryRunnerFactory(org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) FinalizeResultsQueryRunner(org.apache.druid.query.FinalizeResultsQueryRunner) Test(org.junit.Test)

Aggregations

RectangularBound (org.apache.druid.collections.spatial.search.RectangularBound)7 Test (org.junit.Test)6 IOException (java.io.IOException)4 FinalizeResultsQueryRunner (org.apache.druid.query.FinalizeResultsQueryRunner)4 QueryRunner (org.apache.druid.query.QueryRunner)4 Result (org.apache.druid.query.Result)4 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)4 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)4 SpatialDimFilter (org.apache.druid.query.filter.SpatialDimFilter)4 TimeseriesQuery (org.apache.druid.query.timeseries.TimeseriesQuery)4 TimeseriesQueryEngine (org.apache.druid.query.timeseries.TimeseriesQueryEngine)4 TimeseriesQueryQueryToolChest (org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest)4 TimeseriesQueryRunnerFactory (org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory)4 TimeseriesResultValue (org.apache.druid.query.timeseries.TimeseriesResultValue)4 Random (java.util.Random)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 BitmapFactory (org.apache.druid.collections.bitmap.BitmapFactory)3 ConciseBitmapFactory (org.apache.druid.collections.bitmap.ConciseBitmapFactory)3 ImmutableBitmap (org.apache.druid.collections.bitmap.ImmutableBitmap)3 RoaringBitmapFactory (org.apache.druid.collections.bitmap.RoaringBitmapFactory)3