summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/parsed_find_command.cpp
blob: 2ef2e955c06a2d006372bad51519dc3075e9e17b (plain)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
 *    Copyright (C) 2023-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.
 */

#include "mongo/db/query/parsed_find_command.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kQuery

#include "mongo/db/cst/cst_parser.h"
#include "mongo/db/query/collation/collator_factory_interface.h"
#include "mongo/db/query/projection_ast_util.h"
#include "mongo/db/query/projection_parser.h"
#include "mongo/db/query/query_planner_common.h"
#include "mongo/db/query/query_request_helper.h"
#include "mongo/logv2/log.h"

namespace mongo {

namespace {
/**
 * Does 'root' have a subtree of type 'subtreeType' with a node of type 'childType' inside?
 */
bool hasNodeInSubtree(const MatchExpression* root,
                      MatchExpression::MatchType childType,
                      MatchExpression::MatchType subtreeType) {
    if (subtreeType == root->matchType()) {
        return QueryPlannerCommon::hasNode(root, childType);
    }
    for (size_t i = 0; i < root->numChildren(); ++i) {
        if (hasNodeInSubtree(root->getChild(i), childType, subtreeType)) {
            return true;
        }
    }
    return false;
}

bool parsingCanProduceNoopMatchNodes(const ExtensionsCallback& extensionsCallback,
                                     MatchExpressionParser::AllowedFeatureSet allowedFeatures) {
    return extensionsCallback.hasNoopExtensions() &&
        (allowedFeatures & MatchExpressionParser::AllowedFeatures::kText ||
         allowedFeatures & MatchExpressionParser::AllowedFeatures::kJavascript);
}

}  // namespace

std::unique_ptr<CollatorInterface> resolveCollator(
    OperationContext* opCtx, const std::unique_ptr<FindCommandRequest>& findCommand) {
    if (!findCommand->getCollation().isEmpty()) {
        return uassertStatusOKWithContext(CollatorFactoryInterface::get(opCtx->getServiceContext())
                                              ->makeFromBSON(findCommand->getCollation()),
                                          "unable to parse collation");
    }
    return nullptr;
}

/**
 * Helper for building 'out.' If there is a projection, parse it and add any metadata dependencies
 * it induces.
 *
 * Throws exceptions if there is an error parsing the projection.
 */
void setProjection(ParsedFindCommand* out,
                   const boost::intrusive_ptr<ExpressionContext>& expCtx,
                   const std::unique_ptr<FindCommandRequest>& findCommand,
                   const ProjectionPolicies& policies) {
    if (!findCommand->getProjection().isEmpty()) {
        out->savedProjectionPolicies.emplace(policies);
        out->proj.emplace(projection_ast::parseAndAnalyze(expCtx,
                                                          findCommand->getProjection(),
                                                          out->filter.get(),
                                                          findCommand->getFilter(),
                                                          policies));

        // This will throw if any of the projection's dependencies are unavailable.
        DepsTracker{out->unavailableMetadata}.requestMetadata(out->proj->metadataDeps());
    }
}

/**
 * Helper for building 'out.' If there is a sort, parse it and add any metadata dependencies it
 * induces.
 *
 * Throws exceptions if there is an error parsing the sort pattern.
 */
void setSort(ParsedFindCommand* out,
             const boost::intrusive_ptr<ExpressionContext>& expCtx,
             const std::unique_ptr<FindCommandRequest>& findCommand) {
    if (!findCommand->getSort().isEmpty()) {
        // A $natural sort is really a hint, and should be handled as such. Furthermore, the
        // downstream sort handling code may not expect a $natural sort.
        //
        // We have already validated that if there is a $natural sort and a hint, that the hint
        // also specifies $natural with the same direction. Therefore, it is safe to clear the
        // $natural sort and rewrite it as a $natural hint.
        if (findCommand->getSort()[query_request_helper::kNaturalSortField]) {
            findCommand->setHint(findCommand->getSort().getOwned());
            findCommand->setSort(BSONObj{});
        }
        if (getTestCommandsEnabled() && internalQueryEnableCSTParser.load()) {
            out->sort = cst::parseToSortPattern(findCommand->getSort(), expCtx);
        } else {
            out->sort.emplace(findCommand->getSort(), expCtx);
        }
    }
}

/**
 * Helper for building 'out.' If there is a sort, parse it and add any metadata dependencies it
 * induces.
 */
Status setSortAndProjection(ParsedFindCommand* out,
                            const boost::intrusive_ptr<ExpressionContext>& expCtx,
                            const std::unique_ptr<FindCommandRequest>& findCommand,
                            const ProjectionPolicies& policies) {
    try {
        setProjection(out, expCtx, findCommand, policies);
        setSort(out, expCtx, findCommand);
    } catch (const DBException& ex) {
        return ex.toStatus();
    }

    return Status::OK();
}

/**
 * Helper for building 'out.' Sets 'out->filter' and validates that it is well formed. In the
 * process, also populates 'out->unavailableMetadata.'
 */
Status setFilter(ParsedFindCommand* out,
                 std::unique_ptr<MatchExpression> filter,
                 const std::unique_ptr<FindCommandRequest>& findCommand) {
    // Verify the filter follows certain rules like there must be at most one text clause.
    auto swMeta = parsed_find_command::isValid(filter.get(), *findCommand);
    if (!swMeta.isOK()) {
        return swMeta.getStatus();
    }
    out->unavailableMetadata = swMeta.getValue();
    out->filter = std::move(filter);
    return Status::OK();
}


StatusWith<std::unique_ptr<ParsedFindCommand>> parseWithValidatedCollator(
    const boost::intrusive_ptr<ExpressionContext>& expCtx,
    std::unique_ptr<FindCommandRequest> findCommand,
    const ExtensionsCallback& extensionsCallback,
    MatchExpressionParser::AllowedFeatureSet allowedFeatures,
    const ProjectionPolicies& projectionPolicies) {
    auto out = std::make_unique<ParsedFindCommand>();

    tassert(5746107,
            "ntoreturn should not be set on the findCommand",
            findCommand->getNtoreturn() == boost::none);

    if (auto status = query_request_helper::validateFindCommandRequest(*findCommand);
        !status.isOK()) {
        return status;
    }

    // Parse the MatchExpression.
    StatusWithMatchExpression statusWithMatcher = [&]() -> StatusWithMatchExpression {
        if (getTestCommandsEnabled() && internalQueryEnableCSTParser.load()) {
            try {
                return cst::parseToMatchExpression(
                    findCommand->getFilter(), expCtx, extensionsCallback);
            } catch (const DBException& ex) {
                return ex.toStatus();
            }
        } else {
            return MatchExpressionParser::parse(
                findCommand->getFilter(), expCtx, extensionsCallback, allowedFeatures);
        }
    }();
    if (!statusWithMatcher.isOK()) {
        return statusWithMatcher.getStatus();
    }

    // Stop counting expressions after they have been parsed to exclude expressions created
    // during optimization and other processing steps.
    expCtx->stopExpressionCounters();
    out->canHaveNoopMatchNodes =
        parsingCanProduceNoopMatchNodes(extensionsCallback, allowedFeatures);

    if (auto status = setFilter(out.get(), std::move(statusWithMatcher.getValue()), findCommand);
        !status.isOK()) {
        return status;
    }

    if (auto status = setSortAndProjection(out.get(), expCtx, findCommand, projectionPolicies);
        !status.isOK()) {
        return status;
    }

    out->findCommandRequest = std::move(findCommand);
    return {std::move(out)};
}

StatusWith<std::unique_ptr<ParsedFindCommand>> ParsedFindCommand::withExistingFilter(
    const boost::intrusive_ptr<ExpressionContext>& expCtx,
    std::unique_ptr<CollatorInterface> collator,
    std::unique_ptr<MatchExpression> filter,
    std::unique_ptr<FindCommandRequest> findCommandRequest) {
    auto out = std::make_unique<ParsedFindCommand>();
    out->collator = std::move(collator);
    if (auto status = setFilter(out.get(), std::move(filter), findCommandRequest); !status.isOK()) {
        return status;
    }
    if (auto status = setSortAndProjection(
            out.get(), expCtx, findCommandRequest, ProjectionPolicies::findProjectionPolicies());
        !status.isOK()) {
        return status;
    }
    out->findCommandRequest = std::move(findCommandRequest);
    return std::move(out);
}

namespace parsed_find_command {
StatusWith<QueryMetadataBitSet> isValid(const MatchExpression* root,
                                        const FindCommandRequest& findCommand) {
    QueryMetadataBitSet unavailableMetadata{};

    // There can only be one TEXT.  If there is a TEXT, it cannot appear inside a NOR.
    //
    // Note that the query grammar (as enforced by the MatchExpression parser) forbids TEXT
    // inside of value-expression clauses like NOT, so we don't check those here.
    size_t numText = QueryPlannerCommon::countNodes(root, MatchExpression::TEXT);
    if (numText > 1) {
        return Status(ErrorCodes::BadValue, "Too many text expressions");
    } else if (1 == numText) {
        if (hasNodeInSubtree(root, MatchExpression::TEXT, MatchExpression::NOR)) {
            return Status(ErrorCodes::BadValue, "text expression not allowed in nor");
        }
    } else {
        // Text metadata is not available.
        unavailableMetadata.set(DocumentMetadataFields::kTextScore);
    }

    // There can only be one NEAR.  If there is a NEAR, it must be either the root or the root
    // must be an AND and its child must be a NEAR.
    size_t numGeoNear = QueryPlannerCommon::countNodes(root, MatchExpression::GEO_NEAR);
    if (numGeoNear > 1) {
        return Status(ErrorCodes::BadValue, "Too many geoNear expressions");
    } else if (1 == numGeoNear) {
        // Do nothing, we will perform extra checks in CanonicalQuery::isValidNormalized.
    } else {
        // Geo distance and geo point metadata are unavailable.
        unavailableMetadata |= DepsTracker::kAllGeoNearData;
    }

    const BSONObj& sortObj = findCommand.getSort();
    BSONElement sortNaturalElt = sortObj["$natural"];
    const BSONObj& hintObj = findCommand.getHint();
    BSONElement hintNaturalElt = hintObj["$natural"];

    if (sortNaturalElt && sortObj.nFields() != 1) {
        return Status(ErrorCodes::BadValue,
                      str::stream() << "Cannot include '$natural' in compound sort: " << sortObj);
    }

    if (hintNaturalElt && hintObj.nFields() != 1) {
        return Status(ErrorCodes::BadValue,
                      str::stream() << "Cannot include '$natural' in compound hint: " << hintObj);
    }

    // NEAR cannot have a $natural sort or $natural hint.
    if (numGeoNear > 0) {
        if (sortNaturalElt) {
            return Status(ErrorCodes::BadValue,
                          "geoNear expression not allowed with $natural sort order");
        }

        if (hintNaturalElt) {
            return Status(ErrorCodes::BadValue,
                          "geoNear expression not allowed with $natural hint");
        }
    }

    // TEXT and NEAR cannot both be in the query.
    if (numText > 0 && numGeoNear > 0) {
        return Status(ErrorCodes::BadValue, "text and geoNear not allowed in same query");
    }

    // TEXT and {$natural: ...} sort order cannot both be in the query.
    if (numText > 0 && sortNaturalElt) {
        return Status(ErrorCodes::BadValue, "text expression not allowed with $natural sort order");
    }

    // TEXT and hint cannot both be in the query.
    if (numText > 0 && !hintObj.isEmpty()) {
        return Status(ErrorCodes::BadValue, "text and hint not allowed in same query");
    }

    // TEXT and tailable are incompatible.
    if (numText > 0 && findCommand.getTailable()) {
        return Status(ErrorCodes::BadValue, "text and tailable cursor not allowed in same query");
    }

    // NEAR and tailable are incompatible.
    if (numGeoNear > 0 && findCommand.getTailable()) {
        return Status(ErrorCodes::BadValue,
                      "Tailable cursors and geo $near cannot be used together");
    }

    // $natural sort order must agree with hint.
    if (sortNaturalElt) {
        if (!hintObj.isEmpty() && !hintNaturalElt) {
            return Status(ErrorCodes::BadValue, "index hint not allowed with $natural sort order");
        }
        if (hintNaturalElt) {
            if (hintNaturalElt.numberInt() != sortNaturalElt.numberInt()) {
                return Status(ErrorCodes::BadValue,
                              "$natural hint must be in the same direction as $natural sort order");
            }
        }
    }

    return unavailableMetadata;
}

StatusWith<std::pair<boost::intrusive_ptr<ExpressionContext>, std::unique_ptr<ParsedFindCommand>>>
parse(OperationContext* opCtx,
      std::unique_ptr<FindCommandRequest> findCommand,
      const ExtensionsCallback& extensionsCallback,
      MatchExpressionParser::AllowedFeatureSet allowedFeatures,
      const ProjectionPolicies& projectionPolicies) {
    // Make the expCtx.
    invariant(findCommand->getNamespaceOrUUID().nss().has_value());
    auto expCtx = make_intrusive<ExpressionContext>(
        opCtx, *findCommand, resolveCollator(opCtx, findCommand), true /* mayDbProfile */);
    auto swResult = parseWithValidatedCollator(
        expCtx, std::move(findCommand), extensionsCallback, allowedFeatures, projectionPolicies);
    if (!swResult.isOK()) {
        return swResult.getStatus();
    }

    return std::pair{std::move(expCtx), std::move(swResult.getValue())};
}

StatusWith<std::unique_ptr<ParsedFindCommand>> parse(
    const boost::intrusive_ptr<ExpressionContext>& expCtx,
    std::unique_ptr<FindCommandRequest> findCommand,
    const ExtensionsCallback& extensionsCallback,
    MatchExpressionParser::AllowedFeatureSet allowedFeatures,
    const ProjectionPolicies& projectionPolicies) {
    // A collator can enter through both the FindCommandRequest and ExpressionContext arguments.
    // This invariant ensures that both collators are the same because downstream we
    // pull the collator from only one of the ExpressionContext carrier.
    auto collator = resolveCollator(expCtx->opCtx, findCommand);
    if (collator.get() && expCtx->getCollator()) {
        invariant(CollatorInterface::collatorsMatch(collator.get(), expCtx->getCollator()));
    }
    return parseWithValidatedCollator(
        expCtx, std::move(findCommand), extensionsCallback, allowedFeatures, projectionPolicies);
}
}  // namespace parsed_find_command
}  // namespace mongo