Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package io.druid.benchmark.query;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
Expand Down Expand Up @@ -49,6 +51,7 @@
import io.druid.query.select.EventHolder;
import io.druid.query.select.PagingSpec;
import io.druid.query.select.SelectQuery;
import io.druid.query.select.SelectQueryConfig;
import io.druid.query.select.SelectQueryEngine;
import io.druid.query.select.SelectQueryQueryToolChest;
import io.druid.query.select.SelectQueryRunnerFactory;
Expand Down Expand Up @@ -227,12 +230,15 @@ public void setup() throws IOException
qIndexes.add(qIndex);
}

final Supplier<SelectQueryConfig> selectConfigSupplier = Suppliers.ofInstance(new SelectQueryConfig(true));

factory = new SelectQueryRunnerFactory(
new SelectQueryQueryToolChest(
JSON_MAPPER,
QueryBenchmarkUtil.NoopIntervalChunkingQueryRunnerDecorator()
QueryBenchmarkUtil.NoopIntervalChunkingQueryRunnerDecorator(),
selectConfigSupplier
),
new SelectQueryEngine(),
new SelectQueryEngine(selectConfigSupplier),
QueryBenchmarkUtil.NOOP_QUERYWATCHER
);
}
Expand Down
66 changes: 57 additions & 9 deletions docs/content/querying/select-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,78 @@ The results above include:
},
```

This can be used with the next query's pagingSpec:
### Result Pagination

The PagingSpec allows the user to specify that the results of a select query should be returned as a paginated set.

The `threshold` option controls how many rows are returned in each block of paginated results.

To initiate a paginated query, the user should specify a PagingSpec with a `threshold` set and a blank `pagingIdentifiers` field, e.g.:

```json
"pagingSpec":{"pagingIdentifiers": {}, "threshold":5}
```

When the query returns, the results will contain a `pagingIndentifers` field indicating the current pagination point in the result set (an identifier and an offset).

To retrieve the next part of the result set, the user should issue the same select query, but replace the `pagingIdentifiers` field of the query with the `pagingIdentifiers` from the previous result.

When an empty result set is received, all rows have been returned.

#### fromNext Backwards Compatibility

In older versions of Druid, when using paginated select queries, it was necessary for the user to manually increment the paging offset by 1 in each `pagingIdentifiers` before submitting the next query to retrieve the next set of results. This offset increment happens automatically in the current version of Druid by default, the user does not need to modify the `pagingIdentifiers` offset to retrieve the next result set.

Setting the `fromNext` field of the PagingSpec to false instructs Druid to operate in the older mode where the user must manually increment the offset (or decrement for descending queries).

For example, suppose the user issues the following initial paginated query, with `fromNext` false:

```json
{
"queryType": "select",
"dataSource": "wikipedia",
"descending": "false",
"dimensions":[],
"metrics":[],
"granularity": "all",
"intervals": [
"2013-01-01/2013-01-02"
],
"pagingSpec":{"pagingIdentifiers": {"wikipedia_2012-12-29T00:00:00.000Z_2013-01-10T08:00:00.000Z_2013-01-10T08:13:47.830Z_v9" : 5}, "threshold":5}

"pagingSpec":{"fromNext": "false", "pagingIdentifiers": {}, "threshold":5}
}
```

Note that in the second query, an offset is specified and that it is 1 greater than the largest offset found in the initial results. To return the next "page", this offset must be incremented by 1 (should be decremented by 1 for descending query), with each new query, but with option `fromNext` enabled, this operation is not needed. When an empty results set is received, the very last page has been returned.

`fromNext` options is in pagingSpec:
The paginated query with `fromNext` set to false returns a result set with the following `pagingIdentifiers`:

```json
{
...
"pagingSpec":{"pagingIdentifiers": {}, "threshold":5, "fromNext": true}
}
"pagingIdentifiers" : {
"wikipedia_2012-12-29T00:00:00.000Z_2013-01-10T08:00:00.000Z_2013-01-10T08:13:47.830Z_v9" : 4
},
```

To retrieve the next result set, the next query must be sent with the paging offset (4) incremented by 1.

```json
{
"queryType": "select",
"dataSource": "wikipedia",
"dimensions":[],
"metrics":[],
"granularity": "all",
"intervals": [
"2013-01-01/2013-01-02"
],
"pagingSpec":{"fromNext": "false", "pagingIdentifiers": {"wikipedia_2012-12-29T00:00:00.000Z_2013-01-10T08:00:00.000Z_2013-01-10T08:13:47.830Z_v9" : 5}, "threshold":5}
}
```

Note that specifying the `fromNext` option in the `pagingSpec` overrides the default value set by `druid.query.select.enableFromNextDefault` in the server configuration. See [Server configuration](#server-configuration) for more details.

### Server configuration

The following runtime properties apply to select queries:

|Property|Description|Default|
|--------|-----------|-------|
|`druid.query.select.enableFromNextDefault`|If the `fromNext` property in a query's `pagingSpec` is left unspecified, the system will use the value of this property as the default value for `fromNext`. This option is true by default: the option of setting `fromNext` to false by default is intended to support backwards compatibility for deployments where some users may still expect behavior from older versions of Druid.|true|
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package io.druid.segment;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
Expand All @@ -37,6 +39,7 @@
import io.druid.query.select.EventHolder;
import io.druid.query.select.PagingSpec;
import io.druid.query.select.SelectQuery;
import io.druid.query.select.SelectQueryConfig;
import io.druid.query.select.SelectQueryEngine;
import io.druid.query.select.SelectQueryQueryToolChest;
import io.druid.query.select.SelectQueryRunnerFactory;
Expand Down Expand Up @@ -69,12 +72,15 @@ public class MapVirtualColumnTest
@Parameterized.Parameters
public static Iterable<Object[]> constructorFeeder() throws IOException
{
final Supplier<SelectQueryConfig> selectConfigSupplier = Suppliers.ofInstance(new SelectQueryConfig(true));

SelectQueryRunnerFactory factory = new SelectQueryRunnerFactory(
new SelectQueryQueryToolChest(
new DefaultObjectMapper(),
QueryRunnerTestHelper.NoopIntervalChunkingQueryRunnerDecorator()
QueryRunnerTestHelper.NoopIntervalChunkingQueryRunnerDecorator(),
selectConfigSupplier
),
new SelectQueryEngine(),
new SelectQueryEngine(selectConfigSupplier),
QueryRunnerTestHelper.NOOP_QUERYWATCHER
);

Expand Down
17 changes: 14 additions & 3 deletions processing/src/main/java/io/druid/query/select/PagingSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.druid.query.select;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -59,22 +60,32 @@ public static Map<String, Integer> next(Map<String, Integer> cursor, boolean des
private final Map<String, Integer> pagingIdentifiers;
private final int threshold;
private final boolean fromNext;
private final SelectQueryConfig config;

@JsonCreator
public PagingSpec(
@JsonProperty("pagingIdentifiers") Map<String, Integer> pagingIdentifiers,
@JsonProperty("threshold") int threshold,
@JsonProperty("fromNext") boolean fromNext
@JsonProperty("fromNext") Boolean fromNext,
@JacksonInject SelectQueryConfig config
)
{
this.pagingIdentifiers = pagingIdentifiers == null ? Maps.<String, Integer>newHashMap() : pagingIdentifiers;
this.threshold = threshold;
this.fromNext = fromNext;
this.config = config;

boolean defaultFromNext = config.getEnableFromNextDefault();
this.fromNext = fromNext == null ? defaultFromNext : fromNext;
}

public PagingSpec(Map<String, Integer> pagingIdentifiers, int threshold)
{
this(pagingIdentifiers, threshold, false);
this(pagingIdentifiers, threshold, null, new SelectQueryConfig(true));
}

public PagingSpec(Map<String, Integer> pagingIdentifiers, int threshold, Boolean fromNext)
{
this(pagingIdentifiers, threshold, fromNext, new SelectQueryConfig(true));
}

@JsonProperty
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.query.select;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class SelectQueryConfig
{
public static String ENABLE_FROM_NEXT_DEFAULT = "enableFromNextDefault";

@JsonProperty
private boolean enableFromNextDefault = true;

@JsonCreator
public SelectQueryConfig(
@JsonProperty("enableFromNextDefault") Boolean enableFromNextDefault
)
{
if (enableFromNextDefault != null) {
this.enableFromNextDefault = enableFromNextDefault.booleanValue();
}
}

public boolean getEnableFromNextDefault()
{
return enableFromNextDefault;
}

public void setEnableFromNextDefault(boolean enableFromNextDefault)
{
this.enableFromNextDefault = enableFromNextDefault;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import io.druid.java.util.common.IAE;
import io.druid.java.util.common.ISE;
import io.druid.java.util.common.guava.Sequence;
Expand Down Expand Up @@ -158,6 +160,16 @@ public void addRowValuesToSelectResult(
}
}

private final Supplier<SelectQueryConfig> configSupplier;

@Inject
public SelectQueryEngine(
Supplier<SelectQueryConfig> configSupplier
)
{
this.configSupplier = configSupplier;
}

public Sequence<Result<SelectResultValue>> process(final SelectQuery query, final Segment segment)
{
final StorageAdapter adapter = segment.asStorageAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -77,15 +78,19 @@ public class SelectQueryQueryToolChest extends QueryToolChest<Result<SelectResul
};

private final ObjectMapper jsonMapper;

private final IntervalChunkingQueryRunnerDecorator intervalChunkingQueryRunnerDecorator;
private final Supplier<SelectQueryConfig> configSupplier;

@Inject
public SelectQueryQueryToolChest(ObjectMapper jsonMapper,
IntervalChunkingQueryRunnerDecorator intervalChunkingQueryRunnerDecorator)
public SelectQueryQueryToolChest(
ObjectMapper jsonMapper,
IntervalChunkingQueryRunnerDecorator intervalChunkingQueryRunnerDecorator,
Supplier<SelectQueryConfig> configSupplier
)
{
this.jsonMapper = jsonMapper;
this.intervalChunkingQueryRunnerDecorator = intervalChunkingQueryRunnerDecorator;
this.configSupplier = configSupplier;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -56,6 +58,7 @@
import io.druid.query.groupby.GroupByQueryConfig;
import io.druid.query.groupby.GroupByQueryRunnerFactory;
import io.druid.query.groupby.GroupByQueryRunnerTest;
import io.druid.query.select.SelectQueryConfig;
import io.druid.query.select.SelectQueryEngine;
import io.druid.query.select.SelectQueryQueryToolChest;
import io.druid.query.select.SelectQueryRunnerFactory;
Expand Down Expand Up @@ -164,18 +167,30 @@ public static final AggregationTestHelper createSelectQueryAggregationTestHelper
)
{
ObjectMapper mapper = new DefaultObjectMapper();
mapper.setInjectableValues(
new InjectableValues.Std().addValue(
SelectQueryConfig.class,
new SelectQueryConfig(true)
)
);

Supplier<SelectQueryConfig> configSupplier = Suppliers.ofInstance(new SelectQueryConfig(true));

SelectQueryQueryToolChest toolchest = new SelectQueryQueryToolChest(
new DefaultObjectMapper(),
QueryRunnerTestHelper.NoopIntervalChunkingQueryRunnerDecorator()
QueryRunnerTestHelper.NoopIntervalChunkingQueryRunnerDecorator(),
configSupplier
);

SelectQueryRunnerFactory factory = new SelectQueryRunnerFactory(
new SelectQueryQueryToolChest(
new DefaultObjectMapper(),
QueryRunnerTestHelper.NoopIntervalChunkingQueryRunnerDecorator()
QueryRunnerTestHelper.NoopIntervalChunkingQueryRunnerDecorator(),
configSupplier
),
new SelectQueryEngine(
configSupplier
),
new SelectQueryEngine(),
QueryRunnerTestHelper.NOOP_QUERYWATCHER
);

Expand Down
Loading