Package org.apache.druid.query.groupby
Class GroupingEngine
java.lang.Object
org.apache.druid.query.groupby.GroupingEngine
Common code for processing
GroupByQuery.-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionGroupingEngine(DruidProcessingConfig processingConfig, com.google.common.base.Supplier<GroupByQueryConfig> configSupplier, GroupByResourcesReservationPool groupByResourcesReservationPool, com.fasterxml.jackson.databind.ObjectMapper jsonMapper, com.fasterxml.jackson.databind.ObjectMapper spillMapper, QueryWatcher queryWatcher, GroupByStatsProvider groupByStatsProvider) -
Method Summary
Modifier and TypeMethodDescriptionapplyPostProcessing(Sequence<ResultRow> results, GroupByQuery query) Apply theGroupByQuery"postProcessingFn", which is responsible for HavingSpec and LimitSpec.static voidconvertRowTypesToOutputTypes(List<DimensionSpec> dimensionSpecs, ResultRow resultRow, int resultRowDimensionStart) createMergeFn(Query<ResultRow> queryParam) SeeQueryToolChest.createMergeFn(Query)for details, allowsGroupByQueryQueryToolChestto delegate implementation to the strategycreateResultComparator(Query<ResultRow> queryParam) SeeQueryToolChest.createResultComparator(Query), allowsGroupByQueryQueryToolChestto delegate implementation to the strategystatic intgetCardinalityForArrayAggregation(GroupByQueryConfig querySpecificConfig, GroupByQuery query, ColumnInspector columnInspector, List<? extends GroupingSelector> groupingSelectors, ByteBuffer buffer) Returns the cardinality of array needed to do array-based aggregation, or -1 if array-based aggregation is impossible.static CursorBuildSpecmakeCursorBuildSpec(GroupByQuery query, QueryMetrics<?> queryMetrics) mergeResults(QueryRunner<ResultRow> baseRunner, GroupByQuery query, ResponseContext responseContext) Runs a providedQueryRunneron a providedGroupByQuery, which is assumed to return rows that are properly sorted (by timestamp and dimensions) but not necessarily fully merged (that is, there may be adjacent rows with the same timestamp and dimensions) and without PostAggregators computed.mergeRunners(QueryProcessingPool queryProcessingPool, Iterable<QueryRunner<ResultRow>> queryRunners) Merges a variety of single-segment query runners into a combined runner.prepareGroupByQuery(GroupByQuery query) static GroupByQueryResourcesprepareResource(GroupByQuery query, BlockingPool<ByteBuffer> mergeBufferPool, boolean usesGroupByMergingQueryRunner, GroupByQueryConfig groupByQueryConfig) Initializes resources required to runGroupByQueryQueryToolChest.mergeResults(QueryRunner)andGroupByMergingQueryRunnerfor a particular query.process(GroupByQuery query, CursorFactory cursorFactory, TimeBoundaryInspector timeBoundaryInspector, NonBlockingPool<ByteBuffer> bufferPool, GroupByQueryMetrics groupByQueryMetrics) Process a groupBy query on a singleCursorFactory.processSubqueryResult(GroupByQuery subquery, GroupByQuery query, GroupByQueryResources resource, Sequence<ResultRow> subqueryResult, boolean wasQueryPushedDown, GroupByStatsProvider.PerQueryStats perQueryStats) Called byGroupByQueryQueryToolChest.mergeResults(QueryRunner)when it needs to process a subquery.processSubtotalsSpec(GroupByQuery query, GroupByQueryResources resource, Sequence<ResultRow> queryResult, GroupByStatsProvider.PerQueryStats perQueryStats) Called byGroupByQueryQueryToolChest.mergeResults(QueryRunner)when it needs to generate subtotals.static booleanWhether a query should include a summary row.wrapSummaryRowIfNeeded(GroupByQuery query, Sequence<ResultRow> process) Wraps the sequence around if for this query a summary row might be needed in case the input becomes empty.
-
Field Details
-
CTX_KEY_FUDGE_TIMESTAMP
- See Also:
-
CTX_KEY_OUTERMOST
- See Also:
-
-
Constructor Details
-
GroupingEngine
@Inject public GroupingEngine(DruidProcessingConfig processingConfig, com.google.common.base.Supplier<GroupByQueryConfig> configSupplier, GroupByResourcesReservationPool groupByResourcesReservationPool, com.fasterxml.jackson.databind.ObjectMapper jsonMapper, com.fasterxml.jackson.databind.ObjectMapper spillMapper, QueryWatcher queryWatcher, GroupByStatsProvider groupByStatsProvider)
-
-
Method Details
-
prepareResource
public static GroupByQueryResources prepareResource(GroupByQuery query, BlockingPool<ByteBuffer> mergeBufferPool, boolean usesGroupByMergingQueryRunner, GroupByQueryConfig groupByQueryConfig) Initializes resources required to runGroupByQueryQueryToolChest.mergeResults(QueryRunner)andGroupByMergingQueryRunnerfor a particular query. The resources are to be acquired once throughout the execution of the query, or need to be re-acquired (if needed). Users must ensure that throughout the execution, a query already holding the resources shouldn't request for more resources, because that can cause deadlocks.This method throws an exception if it is not able to allocate sufficient resources required for the query to succeed
-
createResultComparator
SeeQueryToolChest.createResultComparator(Query), allowsGroupByQueryQueryToolChestto delegate implementation to the strategy -
createMergeFn
SeeQueryToolChest.createMergeFn(Query)for details, allowsGroupByQueryQueryToolChestto delegate implementation to the strategy -
prepareGroupByQuery
-
mergeResults
public Sequence<ResultRow> mergeResults(QueryRunner<ResultRow> baseRunner, GroupByQuery query, ResponseContext responseContext) Runs a providedQueryRunneron a providedGroupByQuery, which is assumed to return rows that are properly sorted (by timestamp and dimensions) but not necessarily fully merged (that is, there may be adjacent rows with the same timestamp and dimensions) and without PostAggregators computed. This method will fully merge the rows, apply PostAggregators, and return the resultingSequence. The query will be modified usingprepareGroupByQuery(GroupByQuery)before passing it down to the base runner. For example, "having" clauses will be removed and various context parameters will be adjusted. Despite the similar name, this method is much reduced in scope compared toGroupByQueryQueryToolChest.mergeResults(QueryRunner). That method does delegate to this one at some points, but has a truckload of other responsibility, including computing outer query results (if there are subqueries), computing subtotals (like GROUPING SETS), and computing the havingSpec and limitSpec.- Parameters:
baseRunner- base query runnerquery- the groupBy query to run inside the base query runnerresponseContext- the response context to pass to the base query runner- Returns:
- merged result sequence
-
mergeRunners
public QueryRunner<ResultRow> mergeRunners(QueryProcessingPool queryProcessingPool, Iterable<QueryRunner<ResultRow>> queryRunners) Merges a variety of single-segment query runners into a combined runner. Used byGroupByQueryRunnerFactory.mergeRunners(QueryProcessingPool, Iterable). In that sense, it is intended to go along withprocess(org.apache.druid.query.groupby.GroupByQuery, org.apache.druid.segment.CursorFactory, org.apache.druid.segment.TimeBoundaryInspector, org.apache.druid.collections.NonBlockingPool<java.nio.ByteBuffer>, org.apache.druid.query.groupby.GroupByQueryMetrics)(the runners created by that method will be fed into this method). This is primarily called on the data servers, to merge the results from processing on the segments. This method can also be called on the brokers if the query is operating on the local data sources, like the inline datasources. It usesGroupByMergingQueryRunnerwhich requires the merge buffers to be passed in the responseContext of the query that is run.- Parameters:
queryProcessingPool-QueryProcessingPoolservice used for parallel execution of the query runnersqueryRunners- collection of query runners to merge- Returns:
- merged query runner
-
process
public Sequence<ResultRow> process(GroupByQuery query, CursorFactory cursorFactory, @Nullable TimeBoundaryInspector timeBoundaryInspector, NonBlockingPool<ByteBuffer> bufferPool, @Nullable GroupByQueryMetrics groupByQueryMetrics) Process a groupBy query on a singleCursorFactory. This is used byGroupByQueryRunnerFactory.createRunner(org.apache.druid.segment.Segment)to create per-segment QueryRunners. This method is only called on data servers, like Historicals (not the Broker).- Parameters:
query- the groupBy querycursorFactory- cursor factory for the segment in questiontimeBoundaryInspector- time boundary inspector for the segment in questionbufferPool- processing buffer poolgroupByQueryMetrics- metrics instance, will be populated if nonnull- Returns:
- result sequence for the cursor factory
-
applyPostProcessing
Apply theGroupByQuery"postProcessingFn", which is responsible for HavingSpec and LimitSpec.- Parameters:
results- sequence of resultsquery- the groupBy query- Returns:
- post-processed results, with HavingSpec and LimitSpec applied
-
processSubqueryResult
public Sequence<ResultRow> processSubqueryResult(GroupByQuery subquery, GroupByQuery query, GroupByQueryResources resource, Sequence<ResultRow> subqueryResult, boolean wasQueryPushedDown, GroupByStatsProvider.PerQueryStats perQueryStats) Called byGroupByQueryQueryToolChest.mergeResults(QueryRunner)when it needs to process a subquery.- Parameters:
subquery- inner queryquery- outer queryresource- resources returned byprepareResource(GroupByQuery, BlockingPool, boolean, GroupByQueryConfig)subqueryResult- result rows from the subquerywasQueryPushedDown- true if the outer query was pushed down (so we only need to merge the outer query's results, not run it from scratch like a normal outer query)- Returns:
- results of the outer query
-
processSubtotalsSpec
public Sequence<ResultRow> processSubtotalsSpec(GroupByQuery query, GroupByQueryResources resource, Sequence<ResultRow> queryResult, GroupByStatsProvider.PerQueryStats perQueryStats) Called byGroupByQueryQueryToolChest.mergeResults(QueryRunner)when it needs to generate subtotals.- Parameters:
query- query that has a "subtotalsSpec"resource- resources returned byprepareResource(GroupByQuery, BlockingPool, boolean, GroupByQueryConfig)queryResult- result rows from the main query- Returns:
- results for each list of subtotals in the query, concatenated together
-
makeCursorBuildSpec
public static CursorBuildSpec makeCursorBuildSpec(GroupByQuery query, @Nullable QueryMetrics<?> queryMetrics) -
getCardinalityForArrayAggregation
public static int getCardinalityForArrayAggregation(GroupByQueryConfig querySpecificConfig, GroupByQuery query, ColumnInspector columnInspector, List<? extends GroupingSelector> groupingSelectors, ByteBuffer buffer) Returns the cardinality of array needed to do array-based aggregation, or -1 if array-based aggregation is impossible. -
convertRowTypesToOutputTypes
public static void convertRowTypesToOutputTypes(List<DimensionSpec> dimensionSpecs, ResultRow resultRow, int resultRowDimensionStart) -
wrapSummaryRowIfNeeded
public static Sequence<ResultRow> wrapSummaryRowIfNeeded(GroupByQuery query, Sequence<ResultRow> process) Wraps the sequence around if for this query a summary row might be needed in case the input becomes empty. -
summaryRowPreconditions
Whether a query should include a summary row. True for queries that correspond to SQL GROUP BY ().
-