summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/document_source_match.cpp
blob: b6c6e005446a9ee957b7489e3ec5610cfbd80cdb (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
/**
*    Copyright (C) 2011 10gen Inc.
*
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*
*    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
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*    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 GNU Affero General 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/platform/basic.h"

#include <cctype>

#include "mongo/db/jsobj.h"
#include "mongo/db/matcher/matcher.h"
#include "mongo/db/pipeline/document.h"
#include "mongo/db/pipeline/document_source.h"
#include "mongo/db/pipeline/expression.h"
#include "mongo/util/stringutils.h"

namespace mongo {

    using boost::intrusive_ptr;
    using std::string;
    using std::vector;

    const char DocumentSourceMatch::matchName[] = "$match";

    const char *DocumentSourceMatch::getSourceName() const {
        return matchName;
    }

    Value DocumentSourceMatch::serialize(bool explain) const {
        return Value(DOC(getSourceName() << Document(getQuery())));
    }

    intrusive_ptr<DocumentSource> DocumentSourceMatch::optimize() {
        return getQuery().isEmpty() ? nullptr : this;
    }

    boost::optional<Document> DocumentSourceMatch::getNext() {
        pExpCtx->checkForInterrupt();

        // The user facing error should have been generated earlier.
        massert(17309, "Should never call getNext on a $match stage with $text clause",
                !_isTextQuery);

        while (boost::optional<Document> next = pSource->getNext()) {
            // The matcher only takes BSON documents, so we have to make one.
            if (matcher->matches(next->toBson()))
                return next;
        }

        // Nothing matched
        return boost::none;
    }

    bool DocumentSourceMatch::coalesce(const intrusive_ptr<DocumentSource>& nextSource) {
        DocumentSourceMatch* otherMatch = dynamic_cast<DocumentSourceMatch*>(nextSource.get());
        if (!otherMatch)
            return false;

        if (otherMatch->_isTextQuery) {
            // Non-initial text queries are disallowed (enforced by setSource below). This prevents
            // "hiding" a non-initial text query by combining it with another match.
            return false;

            // The rest of this block is for once we support non-initial text queries.

            if (_isTextQuery) {
                // The score should only come from the last $match. We can't combine since then this
                // match's score would impact otherMatch's.
                return false;
            }

            _isTextQuery = true;
        }

        // Replace our matcher with the $and of ours and theirs.
        matcher.reset(new Matcher(BSON("$and" << BSON_ARRAY(getQuery() 
                                              << otherMatch->getQuery())),
                                  MatchExpressionParser::WhereCallback()));

        return true;
    }

namespace {
    // This block contains the functions that make up the implementation of
    // DocumentSourceMatch::redactSafePortion(). They will only be called after
    // the Match expression has been successfully parsed so they can assume that
    // input is well formed.

    bool isAllDigits(StringData str) {
        if (str.empty())
            return false;

        for (size_t i=0; i < str.size(); i++) {
            if (!isdigit(str[i]))
                return false;
        }
        return true;
    }

    bool isFieldnameRedactSafe(StringData fieldName) {
        // Can't have numeric elements in the dotted path since redacting elements from an array
        // would change the indexes.

        const size_t dotPos = fieldName.find('.');
        if (dotPos == string::npos)
            return !isAllDigits(fieldName);

        const StringData part = fieldName.substr(0, dotPos);
        const StringData rest = fieldName.substr(dotPos + 1);
        return !isAllDigits(part) && isFieldnameRedactSafe(rest);
    }

    bool isTypeRedactSafeInComparison(BSONType type) {
        if (type == Array) return false;
        if (type == Object) return false;
        if (type == jstNULL) return false;
        if (type == Undefined) return false; // Currently a Matcher parse error.

        return true;
    }

    Document redactSafePortionTopLevel(BSONObj query); // mutually recursive with next function

    // Returns the redact-safe portion of an "inner" match expression. This is the layer like
    // {$gt: 5} which does not include the field name. Returns an empty document if none of the
    // expression can safely be promoted in front of a $redact.
    Document redactSafePortionDollarOps(BSONObj expr) {
        MutableDocument output;
        BSONForEach(field, expr) {
            if (field.fieldName()[0] != '$')
                continue;

            switch(BSONObj::MatchType(field.getGtLtOp(BSONObj::Equality))) {
            // These are always ok
            case BSONObj::opTYPE:
            case BSONObj::opREGEX:
            case BSONObj::opOPTIONS:
            case BSONObj::opMOD:
                output[field.fieldNameStringData()] = Value(field);
                break;

            // These are ok if the type of the rhs is allowed in comparisons
            case BSONObj::LTE:
            case BSONObj::GTE:
            case BSONObj::LT:
            case BSONObj::GT:
                if (isTypeRedactSafeInComparison(field.type()))
                    output[field.fieldNameStringData()] = Value(field);
                break;

            // $in must be all-or-nothing (like $or). Can't include subset of elements.
            case BSONObj::opIN: {
                bool allOk = true;
                BSONForEach(elem, field.Obj()) {
                    if (!isTypeRedactSafeInComparison(elem.type())) {
                        allOk = false;
                        break;
                    }
                }
                if (allOk) {
                    output[field.fieldNameStringData()] = Value(field);
                }

                break;
            }

            case BSONObj::opALL: {
                // $all can include subset of elements (like $and).
                vector<Value> matches;
                BSONForEach(elem, field.Obj()) {
                    // NOTE this currently doesn't allow {$all: [{$elemMatch: {...}}]}
                    if (isTypeRedactSafeInComparison(elem.type())) {
                        matches.push_back(Value(elem));
                    }
                }
                if (!matches.empty())
                    output[field.fieldNameStringData()] = Value(std::move(matches));

                break;
            }

            case BSONObj::opELEM_MATCH: {
                BSONObj subIn = field.Obj();
                Document subOut;
                if (subIn.firstElementFieldName()[0] == '$') {
                    subOut = redactSafePortionDollarOps(subIn);
                } else {
                    subOut = redactSafePortionTopLevel(subIn);
                }

                if (!subOut.empty())
                    output[field.fieldNameStringData()] = Value(subOut);

                break;
            }

            // These are never allowed
            case BSONObj::Equality: // This actually means unknown
            case BSONObj::opMAX_DISTANCE:
            case BSONObj::opNEAR:
            case BSONObj::NE:
            case BSONObj::opSIZE:
            case BSONObj::NIN:
            case BSONObj::opEXISTS:
            case BSONObj::opWITHIN:
            case BSONObj::opGEO_INTERSECTS:
                continue;
            }
        }
        return output.freeze();
    }

    // Returns the redact-safe portion of an "outer" match expression. This is the layer like
    // {fieldName: {...}} which does include the field name. Returns an empty document if none of
    // the expression can safely be promoted in front of a $redact.
    Document redactSafePortionTopLevel(BSONObj query) {
        MutableDocument output;
        BSONForEach(field, query) {
            if (field.fieldName()[0] == '$') {
                if (str::equals(field.fieldName(), "$or")) {
                    // $or must be all-or-nothing (line $in). Can't include subset of elements.
                    vector<Value> okClauses;
                    BSONForEach(elem, field.Obj()) {
                        Document clause = redactSafePortionTopLevel(elem.Obj());
                        if (clause.empty()) {
                            okClauses.clear();
                            break;
                        }
                        okClauses.push_back(Value(clause));
                    }

                    if (!okClauses.empty())
                        output["$or"] = Value(std::move(okClauses));
                }
                else if (str::equals(field.fieldName(), "$and")) {
                    // $and can include subset of elements (like $all).
                    vector<Value> okClauses;
                    BSONForEach(elem, field.Obj()) {
                        Document clause = redactSafePortionTopLevel(elem.Obj());
                        if (!clause.empty())
                            okClauses.push_back(Value(clause));
                    }
                    if (!okClauses.empty())
                        output["$and"] = Value(std::move(okClauses));
                }

                continue;
            }

            if (!isFieldnameRedactSafe(field.fieldNameStringData()))
                continue;

            switch (field.type()) {
            case Array: continue; // exact matches on arrays are never allowed
            case jstNULL: continue; // can't look for missing fields
            case Undefined: continue; // Currently a Matcher parse error.

            case Object: {
                Document sub = redactSafePortionDollarOps(field.Obj());
                if (!sub.empty())
                    output[field.fieldNameStringData()] = Value(sub);

                break;
            }

            // All other types are ok to pass through
            default:
                output[field.fieldNameStringData()] = Value(field);
                break;
            }
        }
        return output.freeze();
    }
}

    BSONObj DocumentSourceMatch::redactSafePortion() const {
        return redactSafePortionTopLevel(getQuery()).toBson();
    }

    void DocumentSourceMatch::setSource(DocumentSource* source) {
        uassert(17313, "$match with $text is only allowed as the first pipeline stage",
                !_isTextQuery);

        DocumentSource::setSource(source);
    }

    bool DocumentSourceMatch::isTextQuery(const BSONObj& query) {
        BSONForEach(e, query) {
            const StringData fieldName = e.fieldNameStringData();
            if (fieldName == StringData("$text", StringData::LiteralTag()))
                return true;

            if (e.isABSONObj() && isTextQuery(e.Obj()))
                return true;
        }
        return false;
    }

    static void uassertNoDisallowedClauses(BSONObj query) {
        BSONForEach(e, query) {
            // can't use the Matcher API because this would segfault the constructor
            uassert(16395, "$where is not allowed inside of a $match aggregation expression",
                    ! str::equals(e.fieldName(), "$where"));
            // geo breaks if it is not the first portion of the pipeline
            uassert(16424, "$near is not allowed inside of a $match aggregation expression",
                    ! str::equals(e.fieldName(), "$near"));
            uassert(16426, "$nearSphere is not allowed inside of a $match aggregation expression",
                    ! str::equals(e.fieldName(), "$nearSphere"));
            if (e.isABSONObj())
                uassertNoDisallowedClauses(e.Obj());
        }
    }

    intrusive_ptr<DocumentSource> DocumentSourceMatch::createFromBson(
            BSONElement elem,
            const intrusive_ptr<ExpressionContext> &pExpCtx) {
        uassert(15959, "the match filter must be an expression in an object",
                elem.type() == Object);

        uassertNoDisallowedClauses(elem.Obj());

        return new DocumentSourceMatch(elem.Obj(), pExpCtx);
    }

    BSONObj DocumentSourceMatch::getQuery() const {
        return *(matcher->getQuery());
    }

    DocumentSourceMatch::DocumentSourceMatch(const BSONObj &query,
                                             const intrusive_ptr<ExpressionContext> &pExpCtx)
        : DocumentSource(pExpCtx),
          matcher(new Matcher(query.getOwned(), MatchExpressionParser::WhereCallback())),
          _isTextQuery(isTextQuery(query))
    {}
}