1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include "mongo/db/catalog/index_catalog_entry.h"
#include "mongo/db/ops/delete_request_gen.h"
#include "mongo/db/ops/parsed_delete.h"
#include "mongo/db/ops/parsed_update.h"
#include "mongo/db/ops/update_request.h"
#include "mongo/db/query/canonical_query.h"
#include "mongo/db/query/count_command_gen.h"
#include "mongo/db/query/parsed_distinct.h"
#include "mongo/db/query/plan_executor.h"
#include "mongo/db/query/query_planner_params.h"
#include "mongo/db/query/query_settings.h"
#include "mongo/db/query/query_solution.h"
#include "mongo/db/update/update_driver.h"
namespace mongo {
class Collection;
class CountRequest;
/**
* Make an ExpressionContext to be used for non-aggregate commands. The result of this can be passed
* into any of the getExecutor* functions.
*
* Note that the getExecutor* functions may change the collation on the returned ExpressionContext
* if the collection has a default collation and no collation was specifically requested
* ('requestCollation' is empty).
*/
boost::intrusive_ptr<ExpressionContext> makeExpressionContextForGetExecutor(
OperationContext* opCtx, const BSONObj& requestCollation, const NamespaceString& nss);
/**
* Filter indexes retrieved from index catalog by
* allowed indices in query settings.
* Used by getExecutor().
* This function is public to facilitate testing.
*/
void filterAllowedIndexEntries(const AllowedIndicesFilter& allowedIndicesFilter,
std::vector<IndexEntry>* indexEntries);
/**
* Fill out the provided 'plannerParams' for the 'canonicalQuery' operating on the collection
* 'collection'. Exposed for testing.
*/
void fillOutPlannerParams(OperationContext* opCtx,
Collection* collection,
CanonicalQuery* canonicalQuery,
QueryPlannerParams* plannerParams);
/**
* Return whether or not any component of the path 'path' is multikey given an index key pattern
* and multikeypaths. If no multikey metdata is available for the index, and the index is marked
* multikey, conservatively assumes that a component of 'path' _is_ multikey. The 'isMultikey'
* property of an index is false for indexes that definitely have no multikey paths.
*/
bool isAnyComponentOfPathMultikey(const BSONObj& indexKeyPattern,
bool isMultikey,
const MultikeyPaths& indexMultikeyInfo,
StringData path);
/**
* Converts the catalog metadata for an index into an IndexEntry, which is a format that is meant to
* be consumed by the query planner. This function can perform index reads and should not be called
* unless access to the storage engine is permitted.
*
* When 'canonicalQuery' is not null, only multikey metadata paths that intersect with the query
* field set will be retrieved for a multikey wildcard index. Otherwise all multikey metadata paths
* will be retrieved.
*/
IndexEntry indexEntryFromIndexCatalogEntry(OperationContext* opCtx,
const IndexCatalogEntry& ice,
const CanonicalQuery* canonicalQuery = nullptr);
/**
* Determines whether or not to wait for oplog visibility for a query. This is only used for
* collection scans on the oplog.
*/
bool shouldWaitForOplogVisibility(OperationContext* opCtx,
const Collection* collection,
bool tailable);
/**
* Get a plan executor for a query.
*
* If the query is valid and an executor could be created, returns a StatusWith with the
* PlanExecutor.
*
* If the query cannot be executed, returns a Status indicating why.
*/
StatusWith<std::unique_ptr<PlanExecutor, PlanExecutor::Deleter>> getExecutor(
OperationContext* opCtx,
Collection* collection,
std::unique_ptr<CanonicalQuery> canonicalQuery,
PlanExecutor::YieldPolicy yieldPolicy,
size_t plannerOptions = 0);
/**
* Get a plan executor for a .find() operation. The executor will have a 'YIELD_AUTO' yield policy
* unless a false value for 'permitYield' or being part of a multi-document transaction forces it to
* have a 'NO_INTERRUPT' yield policy.
*
* If the query is valid and an executor could be created, returns a StatusWith with the
* PlanExecutor.
*
* If the query cannot be executed, returns a Status indicating why.
*/
StatusWith<std::unique_ptr<PlanExecutor, PlanExecutor::Deleter>> getExecutorFind(
OperationContext* opCtx,
Collection* collection,
std::unique_ptr<CanonicalQuery> canonicalQuery,
bool permitYield = false,
size_t plannerOptions = QueryPlannerParams::DEFAULT);
/**
* Returns a plan executor for a legacy OP_QUERY find.
*/
StatusWith<std::unique_ptr<PlanExecutor, PlanExecutor::Deleter>> getExecutorLegacyFind(
OperationContext* opCtx,
Collection* collection,
std::unique_ptr<CanonicalQuery> canonicalQuery);
/**
* If possible, turn the provided QuerySolution into a QuerySolution that uses a DistinctNode
* to provide results for the distinct command.
*
* When 'strictDistinctOnly' is false, any resulting QuerySolution will limit the number of
* documents that need to be examined to compute the results of a distinct command, but it may not
* guarantee that there are no duplicate values for the distinct field.
*
* If the provided solution could be mutated successfully, returns true, otherwise returns
* false.
*/
bool turnIxscanIntoDistinctIxscan(QuerySolution* soln,
const std::string& field,
bool strictDistinctOnly);
/**
* Get an executor that potentially uses a DISTINCT_SCAN, intended for either a "distinct" command
* or an aggregation pipeline that uses a $group stage with distinct-like semantics.
*
* Distinct is unique in that it doesn't care about getting all the results; it just wants all
* possible values of a certain field. As such, we can skip lots of data in certain cases (see body
* of method for detail).
*
* A $group stage on a single field behaves similarly to a distinct command. If it has no
* accumulators or only $first accumulators, the $group command only needs to visit one document for
* each distinct value of the grouped-by (_id) field to compute its result. When there is a sort
* order specified in parsedDistinct->getQuery()->getQueryRequest.getSort(), the DISTINCT_SCAN will
* follow that sort order, ensuring that it chooses the correct document from each group to compute
* any $first accumulators.
*
* Specify the QueryPlannerParams::STRICT_DISTINCT_ONLY flag in the 'params' argument to ensure that
* any resulting plan _guarantees_ it will return exactly one document per value of the distinct
* field. Without this flag, getExecutorDistinct() may use a plan that takes advantage of
* DISTINCT_SCAN to filter some but not all duplicates (so that de-duplication is still necessary
* after query execution), or it may fall back to a regular IXSCAN.
*
* Providing QueryPlannerParams::STRICT_DISTINCT_ONLY also implies that the resulting plan may not
* "unwind" arrays. That is, it will not return separate values for each element in an array. For
* example, in a collection with documents {a: [10, 11]}, {a: 12}, a distinct command on field 'a'
* can process the "unwound" values 10, 11, and 12, but a $group by 'a' needs to see documents for
* the original [10, 11] and 12 values. In the latter case (in which the caller provides a
* STRICT_DISTINCT_ONLY), a DISTINCT_SCAN is not possible, and the caller would have to fall back
* to a different plan.
*
* Note that this function uses the projection in 'parsedDistinct' to produce a covered query when
* possible, but when a covered query is not possible, the resulting plan may elide the projection
* stage (instead returning entire fetched documents).
*
* For example, a distinct query on field 'b' could use a DISTINCT_SCAN over index {a: 1, b: 1}.
* This plan will reduce the output set by filtering out documents that are equal on both the 'a'
* and 'b' fields, but it could still output documents with equal 'b' values if their 'a' fields are
* distinct.
*/
StatusWith<std::unique_ptr<PlanExecutor, PlanExecutor::Deleter>> getExecutorDistinct(
Collection* collection, size_t plannerOptions, ParsedDistinct* parsedDistinct);
/*
* Get a PlanExecutor for a query executing as part of a count command.
*
* Count doesn't care about actually examining its results; it just wants to walk through them.
* As such, with certain covered queries, we can skip the overhead of fetching etc. when
* executing a count.
*/
StatusWith<std::unique_ptr<PlanExecutor, PlanExecutor::Deleter>> getExecutorCount(
const boost::intrusive_ptr<ExpressionContext>& expCtx,
Collection* collection,
const CountCommand& request,
bool explain,
const NamespaceString& nss);
/**
* Get a PlanExecutor for a delete operation. 'parsedDelete' describes the query predicate
* and delete flags like 'isMulti'. The caller must hold the appropriate MODE_X or MODE_IX
* locks, and must not release these locks until after the returned PlanExecutor is deleted.
*
* 'opDebug' Optional argument. When not null, will be used to record operation statistics.
*
* If the delete operation is executed in explain mode, the 'verbosity' parameter should be
* set to the requested verbosity level, or boost::none otherwise.
*
* The returned PlanExecutor will used the YieldPolicy returned by parsedDelete->yieldPolicy().
*
* Does not take ownership of its arguments.
*
* If the query is valid and an executor could be created, returns a StatusWith with the
* PlanExecutor.
*
* If the query cannot be executed, returns a Status indicating why.
*/
StatusWith<std::unique_ptr<PlanExecutor, PlanExecutor::Deleter>> getExecutorDelete(
OpDebug* opDebug,
Collection* collection,
ParsedDelete* parsedDelete,
boost::optional<ExplainOptions::Verbosity> verbosity);
/**
* Get a PlanExecutor for an update operation. 'parsedUpdate' describes the query predicate
* and update modifiers. The caller must hold the appropriate MODE_X or MODE_IX locks prior
* to calling this function, and must not release these locks until after the returned
* PlanExecutor is deleted.
*
* 'opDebug' Optional argument. When not null, will be used to record operation statistics.
*
* If the delete operation is executed in explain mode, the 'verbosity' parameter should be
* set to the requested verbosity level, or boost::none otherwise.
*
* The returned PlanExecutor will used the YieldPolicy returned by parsedUpdate->yieldPolicy().
*
* Does not take ownership of its arguments.
*
* If the query is valid and an executor could be created, returns a StatusWith with the
* PlanExecutor.
*
* If the query cannot be executed, returns a Status indicating why.
*/
StatusWith<std::unique_ptr<PlanExecutor, PlanExecutor::Deleter>> getExecutorUpdate(
OpDebug* opDebug,
Collection* collection,
ParsedUpdate* parsedUpdate,
boost::optional<ExplainOptions::Verbosity> verbosity);
} // namespace mongo
|