Search in sources :

Example 1 with ScanQuery

use of org.apache.druid.query.scan.ScanQuery in project druid by druid-io.

the class DruidQuery method computeQuery.

/**
 * Return this query as some kind of Druid query. The returned query will either be {@link TopNQuery},
 * {@link TimeseriesQuery}, {@link GroupByQuery}, {@link ScanQuery}
 *
 * @return Druid query
 */
private Query computeQuery(final QueryFeatureInspector queryFeatureInspector) {
    if (dataSource instanceof QueryDataSource) {
        // If there is a subquery, then we prefer the outer query to be a groupBy if possible, since this potentially
        // enables more efficient execution. (The groupBy query toolchest can handle some subqueries by itself, without
        // requiring the Broker to inline results.)
        final GroupByQuery outerQuery = toGroupByQuery(queryFeatureInspector);
        if (outerQuery != null) {
            return outerQuery;
        }
    }
    final TimeseriesQuery tsQuery = toTimeseriesQuery(queryFeatureInspector);
    if (tsQuery != null) {
        return tsQuery;
    }
    final TopNQuery topNQuery = toTopNQuery(queryFeatureInspector);
    if (topNQuery != null) {
        return topNQuery;
    }
    final GroupByQuery groupByQuery = toGroupByQuery(queryFeatureInspector);
    if (groupByQuery != null) {
        return groupByQuery;
    }
    final ScanQuery scanQuery = toScanQuery(queryFeatureInspector);
    if (scanQuery != null) {
        return scanQuery;
    }
    throw new CannotBuildQueryException("Cannot convert query parts into an actual query");
}
Also used : GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) QueryDataSource(org.apache.druid.query.QueryDataSource) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) TopNQuery(org.apache.druid.query.topn.TopNQuery) ScanQuery(org.apache.druid.query.scan.ScanQuery)

Example 2 with ScanQuery

use of org.apache.druid.query.scan.ScanQuery in project druid by druid-io.

the class ClientQuerySegmentWalkerTest method testGroupByOnScanMultiValue.

@Test
public void testGroupByOnScanMultiValue() {
    ScanQuery subquery = new Druids.ScanQueryBuilder().dataSource(MULTI).columns("s", "n").intervals(new MultipleIntervalSegmentSpec(ImmutableList.of(Intervals.ETERNITY))).legacy(false).resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST).build();
    final GroupByQuery query = (GroupByQuery) GroupByQuery.builder().setDataSource(new QueryDataSource(subquery)).setGranularity(Granularities.ALL).setInterval(Intervals.ONLY_ETERNITY).setDimensions(DefaultDimensionSpec.of("s")).setAggregatorSpecs(new LongSumAggregatorFactory("sum_n", "n")).build().withId(DUMMY_QUERY_ID);
    testQuery(query, // GroupBy handles its own subqueries; only the inner one will go to the cluster.
    ImmutableList.of(ExpectedQuery.cluster(subquery.withId(DUMMY_QUERY_ID).withSubQueryId("1.1")), ExpectedQuery.local(query.withDataSource(InlineDataSource.fromIterable(ImmutableList.of(new Object[] { ImmutableList.of("a", "b"), 1 }, new Object[] { ImmutableList.of("a", "c"), 2 }, new Object[] { ImmutableList.of("b"), 3 }, new Object[] { ImmutableList.of("c"), 4 }), RowSignature.builder().add("s", null).add("n", null).build())))), ImmutableList.of(new Object[] { "a", 3L }, new Object[] { "b", 4L }, new Object[] { "c", 6L }));
    Assert.assertEquals(2, scheduler.getTotalRun().get());
    Assert.assertEquals(1, scheduler.getTotalPrioritizedAndLaned().get());
    Assert.assertEquals(2, scheduler.getTotalAcquired().get());
    Assert.assertEquals(2, scheduler.getTotalReleased().get());
}
Also used : GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) QueryDataSource(org.apache.druid.query.QueryDataSource) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) ScanQuery(org.apache.druid.query.scan.ScanQuery) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) Test(org.junit.Test)

Example 3 with ScanQuery

use of org.apache.druid.query.scan.ScanQuery in project druid by druid-io.

the class ClientQuerySegmentWalkerTest method testTopNScanMultiValue.

@Test
public void testTopNScanMultiValue() {
    ScanQuery subquery = new Druids.ScanQueryBuilder().dataSource(MULTI).columns("s", "n").intervals(new MultipleIntervalSegmentSpec(ImmutableList.of(Intervals.ETERNITY))).legacy(false).resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST).build();
    final TopNQuery query = (TopNQuery) new TopNQueryBuilder().dataSource(new QueryDataSource(subquery)).granularity(Granularities.ALL).intervals(Intervals.ONLY_ETERNITY).dimension(DefaultDimensionSpec.of("s")).metric("sum_n").threshold(100).aggregators(new LongSumAggregatorFactory("sum_n", "n")).build().withId(DUMMY_QUERY_ID);
    testQuery(query, // GroupBy handles its own subqueries; only the inner one will go to the cluster.
    ImmutableList.of(ExpectedQuery.cluster(subquery.withId(DUMMY_QUERY_ID).withSubQueryId("1.1")), ExpectedQuery.local(query.withDataSource(InlineDataSource.fromIterable(ImmutableList.of(new Object[] { ImmutableList.of("a", "b"), 1 }, new Object[] { ImmutableList.of("a", "c"), 2 }, new Object[] { ImmutableList.of("b"), 3 }, new Object[] { ImmutableList.of("c"), 4 }), RowSignature.builder().add("s", null).add("n", null).build())))), ImmutableList.of(new Object[] { Intervals.ETERNITY.getStartMillis(), "c", 6L }, new Object[] { Intervals.ETERNITY.getStartMillis(), "b", 4L }, new Object[] { Intervals.ETERNITY.getStartMillis(), "a", 3L }));
    Assert.assertEquals(2, scheduler.getTotalRun().get());
    Assert.assertEquals(1, scheduler.getTotalPrioritizedAndLaned().get());
    Assert.assertEquals(2, scheduler.getTotalAcquired().get());
    Assert.assertEquals(2, scheduler.getTotalReleased().get());
}
Also used : TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) QueryDataSource(org.apache.druid.query.QueryDataSource) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) ScanQuery(org.apache.druid.query.scan.ScanQuery) TopNQuery(org.apache.druid.query.topn.TopNQuery) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) Test(org.junit.Test)

Example 4 with ScanQuery

use of org.apache.druid.query.scan.ScanQuery in project druid by druid-io.

the class StreamAppenderatorTest method testQueryBySegments.

@Test
public void testQueryBySegments() throws Exception {
    try (final StreamAppenderatorTester tester = new StreamAppenderatorTester(2, true)) {
        final Appenderator appenderator = tester.getAppenderator();
        appenderator.startJob();
        appenderator.add(IDENTIFIERS.get(0), ir("2000", "foo", 1), Suppliers.ofInstance(Committers.nil()));
        appenderator.add(IDENTIFIERS.get(0), ir("2000", "foo", 2), Suppliers.ofInstance(Committers.nil()));
        appenderator.add(IDENTIFIERS.get(1), ir("2000", "foo", 4), Suppliers.ofInstance(Committers.nil()));
        appenderator.add(IDENTIFIERS.get(2), ir("2001", "foo", 8), Suppliers.ofInstance(Committers.nil()));
        appenderator.add(IDENTIFIERS.get(2), ir("2001T01", "foo", 16), Suppliers.ofInstance(Committers.nil()));
        appenderator.add(IDENTIFIERS.get(2), ir("2001T02", "foo", 32), Suppliers.ofInstance(Committers.nil()));
        appenderator.add(IDENTIFIERS.get(2), ir("2001T03", "foo", 64), Suppliers.ofInstance(Committers.nil()));
        // Query1: segment #2
        final TimeseriesQuery query1 = Druids.newTimeseriesQueryBuilder().dataSource(StreamAppenderatorTester.DATASOURCE).aggregators(Arrays.asList(new LongSumAggregatorFactory("count", "count"), new LongSumAggregatorFactory("met", "met"))).granularity(Granularities.DAY).intervals(new MultipleSpecificSegmentSpec(ImmutableList.of(new SegmentDescriptor(IDENTIFIERS.get(2).getInterval(), IDENTIFIERS.get(2).getVersion(), IDENTIFIERS.get(2).getShardSpec().getPartitionNum())))).build();
        final List<Result<TimeseriesResultValue>> results1 = QueryPlus.wrap(query1).run(appenderator, ResponseContext.createEmpty()).toList();
        Assert.assertEquals("query1", ImmutableList.of(new Result<>(DateTimes.of("2001"), new TimeseriesResultValue(ImmutableMap.of("count", 4L, "met", 120L)))), results1);
        // Query2: segment #2, partial
        final TimeseriesQuery query2 = Druids.newTimeseriesQueryBuilder().dataSource(StreamAppenderatorTester.DATASOURCE).aggregators(Arrays.asList(new LongSumAggregatorFactory("count", "count"), new LongSumAggregatorFactory("met", "met"))).granularity(Granularities.DAY).intervals(new MultipleSpecificSegmentSpec(ImmutableList.of(new SegmentDescriptor(Intervals.of("2001/PT1H"), IDENTIFIERS.get(2).getVersion(), IDENTIFIERS.get(2).getShardSpec().getPartitionNum())))).build();
        final List<Result<TimeseriesResultValue>> results2 = QueryPlus.wrap(query2).run(appenderator, ResponseContext.createEmpty()).toList();
        Assert.assertEquals("query2", ImmutableList.of(new Result<>(DateTimes.of("2001"), new TimeseriesResultValue(ImmutableMap.of("count", 1L, "met", 8L)))), results2);
        // Query3: segment #2, two disjoint intervals
        final TimeseriesQuery query3 = Druids.newTimeseriesQueryBuilder().dataSource(StreamAppenderatorTester.DATASOURCE).aggregators(Arrays.asList(new LongSumAggregatorFactory("count", "count"), new LongSumAggregatorFactory("met", "met"))).granularity(Granularities.DAY).intervals(new MultipleSpecificSegmentSpec(ImmutableList.of(new SegmentDescriptor(Intervals.of("2001/PT1H"), IDENTIFIERS.get(2).getVersion(), IDENTIFIERS.get(2).getShardSpec().getPartitionNum()), new SegmentDescriptor(Intervals.of("2001T03/PT1H"), IDENTIFIERS.get(2).getVersion(), IDENTIFIERS.get(2).getShardSpec().getPartitionNum())))).build();
        final List<Result<TimeseriesResultValue>> results3 = QueryPlus.wrap(query3).run(appenderator, ResponseContext.createEmpty()).toList();
        Assert.assertEquals("query3", ImmutableList.of(new Result<>(DateTimes.of("2001"), new TimeseriesResultValue(ImmutableMap.of("count", 2L, "met", 72L)))), results3);
        final ScanQuery query4 = Druids.newScanQueryBuilder().dataSource(StreamAppenderatorTester.DATASOURCE).intervals(new MultipleSpecificSegmentSpec(ImmutableList.of(new SegmentDescriptor(Intervals.of("2001/PT1H"), IDENTIFIERS.get(2).getVersion(), IDENTIFIERS.get(2).getShardSpec().getPartitionNum()), new SegmentDescriptor(Intervals.of("2001T03/PT1H"), IDENTIFIERS.get(2).getVersion(), IDENTIFIERS.get(2).getShardSpec().getPartitionNum())))).order(ScanQuery.Order.ASCENDING).batchSize(10).resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST).build();
        final List<ScanResultValue> results4 = QueryPlus.wrap(query4).run(appenderator, ResponseContext.createEmpty()).toList();
        // 2 segments, 1 row per segment
        Assert.assertEquals(2, results4.size());
        Assert.assertArrayEquals(new String[] { "__time", "dim", "count", "met" }, results4.get(0).getColumns().toArray());
        Assert.assertArrayEquals(new Object[] { DateTimes.of("2001").getMillis(), "foo", 1L, 8L }, ((List<Object>) ((List<Object>) results4.get(0).getEvents()).get(0)).toArray());
        Assert.assertArrayEquals(new String[] { "__time", "dim", "count", "met" }, results4.get(0).getColumns().toArray());
        Assert.assertArrayEquals(new Object[] { DateTimes.of("2001T03").getMillis(), "foo", 1L, 64L }, ((List<Object>) ((List<Object>) results4.get(1).getEvents()).get(0)).toArray());
    }
}
Also used : MultipleSpecificSegmentSpec(org.apache.druid.query.spec.MultipleSpecificSegmentSpec) TimeseriesResultValue(org.apache.druid.query.timeseries.TimeseriesResultValue) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) ScanQuery(org.apache.druid.query.scan.ScanQuery) Result(org.apache.druid.query.Result) SegmentDescriptor(org.apache.druid.query.SegmentDescriptor) ScanResultValue(org.apache.druid.query.scan.ScanResultValue) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 5 with ScanQuery

use of org.apache.druid.query.scan.ScanQuery in project druid by druid-io.

the class DoubleStorageTest method testSelectValues.

@Test
public void testSelectValues() {
    QueryRunner runner = QueryRunnerTestHelper.makeQueryRunner(SCAN_QUERY_RUNNER_FACTORY, SEGMENT_ID, new QueryableIndexSegment(index, SEGMENT_ID), null);
    ScanQuery query = newTestQuery().intervals(new LegacySegmentSpec(INTERVAL)).virtualColumns().build();
    Iterable<ScanResultValue> results = runner.run(QueryPlus.wrap(query)).toList();
    ScanResultValue expectedScanResult = new ScanResultValue(SEGMENT_ID.toString(), ImmutableList.of(TIME_COLUMN, DIM_NAME, DIM_FLOAT_NAME), getStreamOfEvents().collect(Collectors.toList()));
    List<ScanResultValue> expectedResults = Collections.singletonList(expectedScanResult);
    ScanQueryRunnerTest.verify(expectedResults, results);
}
Also used : QueryableIndexSegment(org.apache.druid.segment.QueryableIndexSegment) ScanResultValue(org.apache.druid.query.scan.ScanResultValue) ScanQuery(org.apache.druid.query.scan.ScanQuery) LegacySegmentSpec(org.apache.druid.query.spec.LegacySegmentSpec) ScanQueryRunnerTest(org.apache.druid.query.scan.ScanQueryRunnerTest) Test(org.junit.Test)

Aggregations

ScanQuery (org.apache.druid.query.scan.ScanQuery)13 Test (org.junit.Test)7 QueryDataSource (org.apache.druid.query.QueryDataSource)5 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)3 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 SegmentDescriptor (org.apache.druid.query.SegmentDescriptor)2 TableDataSource (org.apache.druid.query.TableDataSource)2 GroupByQuery (org.apache.druid.query.groupby.GroupByQuery)2 ScanResultValue (org.apache.druid.query.scan.ScanResultValue)2 MultipleSpecificSegmentSpec (org.apache.druid.query.spec.MultipleSpecificSegmentSpec)2 TimeseriesQuery (org.apache.druid.query.timeseries.TimeseriesQuery)2 TopNQuery (org.apache.druid.query.topn.TopNQuery)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 Iterables (com.google.common.collect.Iterables)1 Iterators (com.google.common.collect.Iterators)1 Ints (com.google.common.primitives.Ints)1