summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/projection_ast.h
blob: 1304c68efe560da1e1a714dce430385d42492606 (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
/**
 *    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.
 */

#pragma once

#include "mongo/db/jsobj.h"
#include "mongo/db/matcher/copyable_match_expression.h"
#include "mongo/db/matcher/expression_parser.h"
#include "mongo/db/pipeline/dependencies.h"
#include "mongo/db/pipeline/expression.h"
#include "mongo/db/query/projection_ast_visitor.h"

namespace mongo {
namespace projection_ast {
/*
 * A tree representation of a projection. The main purpose of this class is to offer a typed,
 * walkable representation of a projection. It's mostly meant to be used while doing validation and
 * dependency analysis. It is not designed for executing a projection.
 */
class ASTNode {
public:
    using ASTNodeVector = std::vector<std::unique_ptr<ASTNode>>;

    ASTNode() {}
    ASTNode(ASTNodeVector children) : _children(std::move(children)) {
        for (auto&& child : _children) {
            child->_parent = this;
        }
    }

    ASTNode(const ASTNode& other) : _parent(nullptr) {
        // It is the responsibility of this node's parent to set _parent on this node correctly.
        _children.reserve(other._children.size());
        for (auto&& child : other._children) {
            addChildToInternalVector(child->clone());
        }
    }
    ASTNode(ASTNode&&) = default;

    virtual ~ASTNode() = default;

    virtual std::unique_ptr<ASTNode> clone() const = 0;

    virtual void acceptVisitor(ProjectionASTMutableVisitor* visitor) = 0;
    virtual void acceptVisitor(ProjectionASTConstVisitor* visitor) const = 0;

    const ASTNodeVector& children() const {
        return _children;
    }

    ASTNode* child(size_t index) const {
        invariant(index < _children.size());
        return _children[index].get();
    }

    const ASTNode* parent() const {
        return _parent;
    }

    bool isRoot() const {
        return !_parent;
    }

protected:
    virtual void addChildToInternalVector(std::unique_ptr<ASTNode> node) {
        node->_parent = this;
        _children.push_back(std::move(node));
    }

    // nullptr if this is the root.
    ASTNode* _parent = nullptr;
    ASTNodeVector _children;
};

inline auto begin(ASTNode& node) {
    return node.children().begin();
}

inline auto begin(const ASTNode& node) {
    return node.children().begin();
}

inline auto end(ASTNode& node) {
    return node.children().end();
}

inline auto end(const ASTNode& node) {
    return node.children().end();
}

class MatchExpressionASTNode final : public ASTNode {
public:
    MatchExpressionASTNode(CopyableMatchExpression matchExpr) : _matchExpr{matchExpr} {}

    MatchExpressionASTNode(const MatchExpressionASTNode& other)
        : ASTNode{other}, _matchExpr{other._matchExpr} {}

    std::unique_ptr<ASTNode> clone() const override final {
        return std::make_unique<MatchExpressionASTNode>(*this);
    }

    void acceptVisitor(ProjectionASTMutableVisitor* visitor) override {
        visitor->visit(this);
    }

    void acceptVisitor(ProjectionASTConstVisitor* visitor) const override {
        visitor->visit(this);
    }

    CopyableMatchExpression matchExpression() const {
        return _matchExpr;
    }

private:
    CopyableMatchExpression _matchExpr;
};

class ProjectionPathASTNode final : public ASTNode {
public:
    ProjectionPathASTNode() {}

    ProjectionPathASTNode(ASTNodeVector children, std::vector<std::string> fieldNames)
        : ASTNode(std::move(children)), _fieldNames(std::move(fieldNames)) {
        invariant(_children.size() == _fieldNames.size());
    }

    void acceptVisitor(ProjectionASTMutableVisitor* visitor) override {
        visitor->visit(this);
    }

    void acceptVisitor(ProjectionASTConstVisitor* visitor) const override {
        visitor->visit(this);
    }

    std::unique_ptr<ASTNode> clone() const override final {
        return std::make_unique<ProjectionPathASTNode>(*this);
    }

    ASTNode* getChild(StringData fieldName) const {
        invariant(_fieldNames.size() == _children.size());
        for (size_t i = 0; i < _fieldNames.size(); ++i) {
            if (_fieldNames[i] == fieldName) {
                return _children[i].get();
            }
        }
        return nullptr;
    }

    void addChild(StringData fieldName, std::unique_ptr<ASTNode> node) {
        addChildToInternalVector(std::move(node));
        _fieldNames.push_back(fieldName.toString());
    }

    /**
     * Remove a node which is a direct child of this tree. Returns true if anything was removed,
     * false otherwise.
     */
    bool removeChild(StringData fieldName) {
        if (auto it = std::find(_fieldNames.begin(), _fieldNames.end(), fieldName);
            it != _fieldNames.end()) {
            _children.erase(_children.begin() + std::distance(_fieldNames.begin(), it));
            _fieldNames.erase(it);
            return true;
        }

        return false;
    }

    const std::vector<std::string>& fieldNames() const {
        return _fieldNames;
    }

private:
    // Names associated with the child nodes. Must be same size as _children.
    std::vector<std::string> _fieldNames;
};

class ProjectionPositionalASTNode final : public ASTNode {
public:
    ProjectionPositionalASTNode(std::unique_ptr<MatchExpressionASTNode> child) {
        invariant(child);
        addChildToInternalVector(std::move(child));
    }

    void acceptVisitor(ProjectionASTMutableVisitor* visitor) override {
        visitor->visit(this);
    }

    void acceptVisitor(ProjectionASTConstVisitor* visitor) const override {
        visitor->visit(this);
    }

    std::unique_ptr<ASTNode> clone() const override final {
        return std::make_unique<ProjectionPositionalASTNode>(*this);
    }
};

class ProjectionSliceASTNode final : public ASTNode {
public:
    ProjectionSliceASTNode(boost::optional<int> skip, int limit) : _skip(skip), _limit(limit) {}

    void acceptVisitor(ProjectionASTMutableVisitor* visitor) override {
        visitor->visit(this);
    }

    void acceptVisitor(ProjectionASTConstVisitor* visitor) const override {
        visitor->visit(this);
    }

    std::unique_ptr<ASTNode> clone() const override final {
        return std::make_unique<ProjectionSliceASTNode>(*this);
    }

    int limit() const {
        return _limit;
    }

    boost::optional<int> skip() const {
        return _skip;
    }

private:
    boost::optional<int> _skip;
    int _limit = 0;
};

class ProjectionElemMatchASTNode final : public ASTNode {
public:
    ProjectionElemMatchASTNode(std::unique_ptr<MatchExpressionASTNode> child) {
        invariant(child);
        addChildToInternalVector(std::move(child));
    }

    void acceptVisitor(ProjectionASTMutableVisitor* visitor) override {
        visitor->visit(this);
    }

    void acceptVisitor(ProjectionASTConstVisitor* visitor) const override {
        visitor->visit(this);
    }

    std::unique_ptr<ASTNode> clone() const override final {
        return std::make_unique<ProjectionElemMatchASTNode>(*this);
    }
};

class ExpressionASTNode final : public ASTNode {
public:
    ExpressionASTNode(boost::intrusive_ptr<Expression> expr) : _expr(expr) {}
    ExpressionASTNode(const ExpressionASTNode& other) : ASTNode(other) {
        BSONObjBuilder bob;
        bob << "" << other._expr->serialize();

        // TODO SERVER-31003: add a clone() method to Expression.
        // Temporary stop expression counters while processing the cloned expression.
        auto otherCtx = other._expr->getExpressionContext();
        auto activeCounting = otherCtx->expressionCountersAreActive();
        if (activeCounting) {
            otherCtx->enabledCounters = false;
        }
        boost::intrusive_ptr<Expression> clonedExpr = Expression::parseOperand(
            otherCtx, bob.obj().firstElement(), otherCtx->variablesParseState);
        if (activeCounting) {
            otherCtx->enabledCounters = true;
        }
        _expr = clonedExpr;
    }

    void acceptVisitor(ProjectionASTMutableVisitor* visitor) override {
        visitor->visit(this);
    }

    void acceptVisitor(ProjectionASTConstVisitor* visitor) const override {
        visitor->visit(this);
    }

    std::unique_ptr<ASTNode> clone() const override final {
        return std::make_unique<ExpressionASTNode>(*this);
    }

    Expression* expressionRaw() const {
        return _expr.get();
    }

    boost::intrusive_ptr<Expression> expression() const {
        return _expr;
    }

    void optimize() {
        _expr = _expr->optimize();
    }

private:
    boost::intrusive_ptr<Expression> _expr;
};

class BooleanConstantASTNode final : public ASTNode {
public:
    BooleanConstantASTNode(bool val) : _val(val) {}

    void acceptVisitor(ProjectionASTMutableVisitor* visitor) override {
        visitor->visit(this);
    }

    void acceptVisitor(ProjectionASTConstVisitor* visitor) const override {
        visitor->visit(this);
    }

    std::unique_ptr<ASTNode> clone() const override final {
        return std::make_unique<BooleanConstantASTNode>(*this);
    }

    bool value() const {
        return _val;
    }

private:
    bool _val;
};
}  // namespace projection_ast
}  // namespace mongo