summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/parsed_inclusion_projection.cpp
blob: 57d0df56759ef2ce410c1febcf6d86f1cce40ad9 (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

/**
 *    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.
 */

#include "mongo/platform/basic.h"

#include "mongo/db/pipeline/parsed_inclusion_projection.h"

#include <algorithm>

namespace mongo {

namespace parsed_aggregation_projection {

//
// InclusionNode
//

InclusionNode::InclusionNode(ProjectionPolicies policies, std::string pathToNode)
    : ProjectionNode(policies, std::move(pathToNode)) {}

InclusionNode* InclusionNode::addOrGetChild(const std::string& field) {
    return static_cast<InclusionNode*>(ProjectionNode::addOrGetChild(field));
}

void InclusionNode::reportDependencies(DepsTracker* deps) const {
    for (auto&& includedField : _projectedFields) {
        deps->fields.insert(FieldPath::getFullyQualifiedPath(_pathToNode, includedField));
    }

    if (!_pathToNode.empty() && !_expressions.empty()) {
        // The shape of any computed fields in the output will change depending on if the field is
        // an array or not, so in addition to any dependencies of the expression itself, we need to
        // add this field to our dependencies.
        deps->fields.insert(_pathToNode);
    }

    for (auto&& expressionPair : _expressions) {
        expressionPair.second->addDependencies(deps);
    }
    for (auto&& childPair : _children) {
        childPair.second->reportDependencies(deps);
    }
}

//
// ParsedInclusionProjection
//

void ParsedInclusionProjection::parse(const BSONObj& spec) {
    // It is illegal to specify an inclusion with no output fields.
    bool atLeastOneFieldInOutput = false;

    // Tracks whether or not we should apply the default _id projection policy.
    bool idSpecified = false;

    for (auto elem : spec) {
        auto fieldName = elem.fieldNameStringData();
        idSpecified = idSpecified || fieldName == "_id"_sd || fieldName.startsWith("_id."_sd);
        if (fieldName == "_id") {
            const bool idIsExcluded = (!elem.trueValue() && (elem.isNumber() || elem.isBoolean()));
            if (idIsExcluded) {
                // Ignoring "_id" here will cause it to be excluded from result documents.
                _idExcluded = true;
                continue;
            }

            // At least part of "_id" is included or a computed field. Fall through to below to
            // parse what exactly "_id" was specified as.
        }

        atLeastOneFieldInOutput = true;
        switch (elem.type()) {
            case BSONType::Bool:
            case BSONType::NumberInt:
            case BSONType::NumberLong:
            case BSONType::NumberDouble:
            case BSONType::NumberDecimal: {
                // This is an inclusion specification.
                invariant(elem.trueValue());
                _root->addProjectionForPath(FieldPath(elem.fieldName()));
                break;
            }
            case BSONType::Object: {
                // This is either an expression, or a nested specification.
                if (parseObjectAsExpression(fieldName, elem.Obj(), _expCtx->variablesParseState)) {
                    // It was an expression.
                    break;
                }

                // The field name might be a dotted path. If so, we need to keep adding children
                // to our tree until we create a child that represents that path.
                auto remainingPath = FieldPath(elem.fieldName());
                auto* child = _root.get();
                while (remainingPath.getPathLength() > 1) {
                    child = child->addOrGetChild(remainingPath.getFieldName(0).toString());
                    remainingPath = remainingPath.tail();
                }
                // It is illegal to construct an empty FieldPath, so the above loop ends one
                // iteration too soon. Add the last path here.
                child = child->addOrGetChild(remainingPath.fullPath());

                parseSubObject(elem.Obj(), _expCtx->variablesParseState, child);
                break;
            }
            default: {
                // This is a literal value.
                _root->addExpressionForPath(
                    FieldPath(elem.fieldName()),
                    Expression::parseOperand(_expCtx, elem, _expCtx->variablesParseState));
            }
        }
    }

    if (!idSpecified) {
        // _id wasn't specified, so apply the default _id projection policy here.
        if (_policies.idPolicy == ProjectionPolicies::DefaultIdPolicy::kExcludeId) {
            _idExcluded = true;
        } else {
            atLeastOneFieldInOutput = true;
            _root->addProjectionForPath(FieldPath("_id"));
        }
    }

    uassert(16403,
            str::stream() << "$project requires at least one output field: " << spec.toString(),
            atLeastOneFieldInOutput);
}

Document ParsedInclusionProjection::applyProjection(const Document& inputDoc) const {
    // All expressions will be evaluated in the context of the input document, before any
    // transformations have been applied.
    return _root->applyToDocument(inputDoc);
}

bool ParsedInclusionProjection::parseObjectAsExpression(
    StringData pathToObject,
    const BSONObj& objSpec,
    const VariablesParseState& variablesParseState) {
    if (objSpec.firstElementFieldName()[0] == '$') {
        // This is an expression like {$add: [...]}. We have already verified that it has only one
        // field.
        invariant(objSpec.nFields() == 1);
        _root->addExpressionForPath(
            pathToObject, Expression::parseExpression(_expCtx, objSpec, variablesParseState));
        return true;
    }
    return false;
}

void ParsedInclusionProjection::parseSubObject(const BSONObj& subObj,
                                               const VariablesParseState& variablesParseState,
                                               InclusionNode* node) {
    for (auto elem : subObj) {
        invariant(elem.fieldName()[0] != '$');
        // Dotted paths in a sub-object have already been disallowed in
        // ParsedAggregationProjection's parsing.
        invariant(elem.fieldNameStringData().find('.') == std::string::npos);

        switch (elem.type()) {
            case BSONType::Bool:
            case BSONType::NumberInt:
            case BSONType::NumberLong:
            case BSONType::NumberDouble:
            case BSONType::NumberDecimal: {
                // This is an inclusion specification.
                invariant(elem.trueValue());
                node->addProjectionForPath(FieldPath(elem.fieldName()));
                break;
            }
            case BSONType::Object: {
                // This is either an expression, or a nested specification.
                auto fieldName = elem.fieldNameStringData().toString();
                if (parseObjectAsExpression(
                        FieldPath::getFullyQualifiedPath(node->getPath(), fieldName),
                        elem.Obj(),
                        variablesParseState)) {
                    break;
                }
                auto* child = node->addOrGetChild(fieldName);
                parseSubObject(elem.Obj(), variablesParseState, child);
                break;
            }
            default: {
                // This is a literal value.
                node->addExpressionForPath(
                    FieldPath(elem.fieldName()),
                    Expression::parseOperand(_expCtx, elem, variablesParseState));
            }
        }
    }
}

bool ParsedInclusionProjection::isSubsetOfProjection(const BSONObj& proj) const {
    std::set<std::string> preservedPaths;
    _root->reportProjectedPaths(&preservedPaths);
    for (auto&& includedField : preservedPaths) {
        if (!proj.hasField(includedField))
            return false;
    }

    // If the inclusion has any computed fields or renamed fields, then it's not a subset.
    std::set<std::string> computedPaths;
    StringMap<std::string> renamedPaths;
    _root->reportComputedPaths(&computedPaths, &renamedPaths);
    return computedPaths.empty() && renamedPaths.empty();
}

}  // namespace parsed_aggregation_projection
}  // namespace mongo