summaryrefslogtreecommitdiff
path: root/src/mongo/db/exec/projection_executor.cpp
blob: 19a7fd7eb55c76127c0d5caf8bc8fca77944054e (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
/**
 *    Copyright (C) 2019-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/base/exact_cast.h"
#include "mongo/db/exec/projection_executor.h"
#include "mongo/db/pipeline/expression_find_internal.h"
#include "mongo/db/pipeline/parsed_exclusion_projection.h"
#include "mongo/db/pipeline/parsed_inclusion_projection.h"
#include "mongo/db/query/projection_ast_path_tracking_visitor.h"
#include "mongo/db/query/util/make_data_structure.h"

namespace mongo::projection_executor {
namespace {
using ParsedAggregationProjection = parsed_aggregation_projection::ParsedAggregationProjection;
using ParsedInclusionProjection = parsed_aggregation_projection::ParsedInclusionProjection;
using ParsedExclusionProjection = parsed_aggregation_projection::ParsedExclusionProjection;

constexpr auto kInclusion = projection_ast::ProjectType::kInclusion;
constexpr auto kExclusion = projection_ast::ProjectType::kExclusion;
constexpr auto kProjectionPostImageVarName =
    parsed_aggregation_projection::ParsedAggregationProjection::kProjectionPostImageVarName;

/**
 * Holds data used to built a projection executor while walking an AST tree. This struct is attached
 * to 'PathTrackingVisitorContext' and can be accessed by projection AST visitors to track the
 * current context.
 */
template <typename Executor>
struct ProjectionExecutorVisitorData {
    // A projection executor returned upon completion of AST traversal.
    std::unique_ptr<Executor> executor;
    boost::intrusive_ptr<ExpressionContext> expCtx;
    // A root replacement expression to be attached to the projection executor, if any. If there
    // are multiple root replacement expressions in the AST, they will be chained together, so that
    // one expression becomes an input to another.
    boost::intrusive_ptr<Expression> rootReplacementExpression;

    auto rootNode() const {
        return executor->getRoot();
    }

    void setRootReplacementExpression(boost::intrusive_ptr<Expression> expr) {
        rootReplacementExpression = expr;
        executor->setRootReplacementExpression(rootReplacementExpression);
    }
};

template <typename Executor>
using ProjectionExecutorVisitorContext =
    projection_ast::PathTrackingVisitorContext<ProjectionExecutorVisitorData<Executor>>;

template <typename Executor>
auto makeProjectionPreImageExpression(const ProjectionExecutorVisitorData<Executor>& data) {
    return ExpressionFieldPath::parse(data.expCtx, "$$ROOT", data.expCtx->variablesParseState);
}

template <typename Executor>
auto makeProjectionPostImageExpression(const ProjectionExecutorVisitorData<Executor>& data) {
    return data.rootReplacementExpression
        ? data.rootReplacementExpression
        : ExpressionFieldPath::parse(
              data.expCtx, "$$" + kProjectionPostImageVarName, data.expCtx->variablesParseState);
}

/**
 * Creates a find()-style positional expression from the given AST 'node' to be applied to the
 * 'path' on the input document. If the visitor 'data' already contains a root replacement
 * expression, it will be used as an input operand to the new root replacement expression, otherwise
 * a field path expressions will be created to access a projection post-image document.
 */
template <typename Executor>
auto createFindPositionalExpression(const projection_ast::ProjectionPositionalASTNode* node,
                                    const ProjectionExecutorVisitorData<Executor>& data,
                                    const FieldPath& path) {
    invariant(node);

    const auto& children = node->children();
    invariant(children.size() == 1UL);

    auto matchExprNode =
        exact_pointer_cast<projection_ast::MatchExpressionASTNode*>(children[0].get());
    invariant(matchExprNode);

    return make_intrusive<ExpressionInternalFindPositional>(data.expCtx,
                                                            makeProjectionPreImageExpression(data),
                                                            makeProjectionPostImageExpression(data),
                                                            path,
                                                            matchExprNode->matchExpression());
}

/**
 * Creates a find()-style $slice expression from the given AST 'node' to be applied to the
 * 'path' on the input document. If the visitor 'data' already contains a root replacement
 * expression, it will be used as an input operand to the new root replacement expression, otherwise
 * a field path expressions will be created to access a projection post-image document.
 */
template <typename Executor>
auto createFindSliceExpression(const projection_ast::ProjectionSliceASTNode* node,
                               const ProjectionExecutorVisitorData<Executor>& data,
                               const FieldPath& path) {
    invariant(node);

    return make_intrusive<ExpressionInternalFindSlice>(
        data.expCtx, makeProjectionPostImageExpression(data), path, node->skip(), node->limit());
}

/**
 * Creates a find()-style $elemMatch expression from the given AST 'node' to be applied at the
 * 'path' on the input document.
 */
template <typename Executor>
auto createFindElemMatchExpression(const projection_ast::ProjectionElemMatchASTNode* node,
                                   const ProjectionExecutorVisitorData<Executor>& data,
                                   const FieldPath& path) {
    invariant(node);

    const auto& children = node->children();
    invariant(children.size() == 1UL);

    auto matchExprNode =
        exact_pointer_cast<projection_ast::MatchExpressionASTNode*>(children[0].get());
    invariant(matchExprNode);

    return make_intrusive<ExpressionInternalFindElemMatch>(data.expCtx,
                                                           makeProjectionPreImageExpression(data),
                                                           path,
                                                           matchExprNode->matchExpression());
}

/**
 * A projection AST visitor which creates inclusion or exclusion nodes in the projection execution
 * tree by calling corresponding 'addProjectionForPath()' or 'addExpressionForPath()' on the root
 * node of the tree while traversing the AST. If the AST contains root-replacement expressions, they
 * will be attached to the projection executor.
 *
 * To track the current path in the projection, this visitor should be used with
 * 'PathTrackingWalker' which will help to maintain the current path via
 * 'PathTrackingVisitorContext'.
 */
template <typename Executor>
class ProjectionExecutorVisitor final : public projection_ast::ProjectionASTConstVisitor {
public:
    ProjectionExecutorVisitor(ProjectionExecutorVisitorContext<Executor>* context)
        : _context{context} {
        invariant(_context);
    }

    void visit(const projection_ast::ProjectionPositionalASTNode* node) final {
        constexpr auto isInclusion = std::is_same_v<Executor, ParsedInclusionProjection>;
        invariant(isInclusion);

        const auto& path = _context->fullPath();
        auto& userData = _context->data();

        userData.rootNode()->addProjectionForPath(path.fullPath());
        userData.setRootReplacementExpression(createFindPositionalExpression(node, userData, path));
    }

    void visit(const projection_ast::ProjectionSliceASTNode* node) final {
        const auto& path = _context->fullPath();
        auto& userData = _context->data();

        // A $slice expression can be applied to an exclusion projection. In this case we don't need
        // to project out the path to which $slice is applied, since it will already be included
        // into the output document.
        if constexpr (std::is_same_v<Executor, ParsedInclusionProjection>) {
            userData.rootNode()->addProjectionForPath(path.fullPath());
        }

        userData.setRootReplacementExpression(createFindSliceExpression(node, userData, path));
    }

    void visit(const projection_ast::ProjectionElemMatchASTNode* node) final {
        const auto& path = _context->fullPath();
        const auto& userData = _context->data();

        userData.rootNode()->addExpressionForPath(
            path.fullPath(), createFindElemMatchExpression(node, userData, path));
    }

    void visit(const projection_ast::ExpressionASTNode* node) final {
        const auto& path = _context->fullPath();
        const auto& userData = _context->data();

        userData.rootNode()->addExpressionForPath(path.fullPath(), node->expression());
    }

    void visit(const projection_ast::BooleanConstantASTNode* node) final {
        const auto& path = _context->fullPath();
        const auto& userData = _context->data();

        // In an inclusion projection only the _id field can be excluded from the result document.
        // If this is the case, then we don't need to include the field into the projection.
        if constexpr (std::is_same_v<Executor, ParsedInclusionProjection>) {
            const auto isIdField = path == "_id";
            if (isIdField && !node->value()) {
                return;
            }
            // In inclusion projection only _id field can be excluded, make sure this is the case.
            invariant(!isIdField || node->value());
        }

        userData.rootNode()->addProjectionForPath(path.fullPath());
    }

    void visit(const projection_ast::ProjectionPathASTNode* node) final {}
    void visit(const projection_ast::MatchExpressionASTNode* node) final {}

private:
    ProjectionExecutorVisitorContext<Executor>* _context;
};

/**
 * A helper function which creates a 'ProjectionExecutorWalker' to walk a projection AST,
 * starting at the node 'root', and build a projection executor of the specified type
 * 'Executor'.
 */
template <typename Executor>
auto buildProjectionExecutor(boost::intrusive_ptr<ExpressionContext> expCtx,
                             const projection_ast::ProjectionPathASTNode* root,
                             const ProjectionPolicies policies) {
    ProjectionExecutorVisitorContext<Executor> context{
        {std::make_unique<Executor>(expCtx, policies), expCtx}};
    ProjectionExecutorVisitor<Executor> executorVisitor{&context};
    projection_ast::PathTrackingWalker walker{&context, {&executorVisitor}, {}};
    projection_ast_walker::walk(&walker, root);
    context.data().executor->optimize();
    return std::move(context.data().executor);
}
}  // namespace

std::unique_ptr<ParsedAggregationProjection> buildProjectionExecutor(
    boost::intrusive_ptr<ExpressionContext> expCtx,
    const projection_ast::Projection* projection,
    const ProjectionPolicies policies) {
    invariant(projection);

    switch (projection->type()) {
        case kInclusion:
            return buildProjectionExecutor<ParsedInclusionProjection>(
                expCtx, projection->root(), policies);
        case kExclusion:
            return buildProjectionExecutor<ParsedExclusionProjection>(
                expCtx, projection->root(), policies);
        default:
            MONGO_UNREACHABLE;
    }
}
}  // namespace mongo::projection_executor