summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/abt/transformer_visitor.cpp
blob: 364dc1172466d4f548e5930334af4073fd6b0af6 (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
/**
 *    Copyright (C) 2022-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/pipeline/abt/transformer_visitor.h"

#include "mongo/db/pipeline/abt/agg_expression_visitor.h"
#include "mongo/db/pipeline/abt/utils.h"
#include "mongo/db/pipeline/document_source_group.h"
#include "mongo/db/pipeline/document_source_replace_root.h"
#include "mongo/db/pipeline/visitors/transformer_interface_walker.h"

namespace mongo::optimizer {

void ABTTransformerVisitor::visit(
    const projection_executor::AddFieldsProjectionExecutor* transformer) {
    visitInclusionNode(transformer->getRoot(), true /*isAddingFields*/);
}

void ABTTransformerVisitor::visit(
    const projection_executor::ExclusionProjectionExecutor* transformer) {
    visitExclusionNode(*transformer->getRoot());
}

void ABTTransformerVisitor::visit(
    const projection_executor::InclusionProjectionExecutor* transformer) {
    visitInclusionNode(*transformer->getRoot(), false /*isAddingFields*/);
}

void ABTTransformerVisitor::visit(const GroupFromFirstDocumentTransformation* transformer) {
    unsupportedTransformer(transformer);
}

void ABTTransformerVisitor::visit(const ReplaceRootTransformation* transformer) {
    auto entry = _ctx.getNode();
    const ProjectionName projName{_ctx.getNextId("newRoot")};
    ABT expr = generateAggExpression(
        transformer->getExpression().get(), entry._rootProjection, _ctx.getPrefixId());

    _ctx.setNode<EvaluationNode>(projName, projName, std::move(expr), std::move(entry._node));
}

void ABTTransformerVisitor::generateCombinedProjection() const {
    auto result = _builder.generateABT();
    // ReplaceRootTransformer won't integrate any paths using the field map builder.
    if (!result) {
        return;
    }
    auto entry = _ctx.getNode();
    const ProjectionName projName = _ctx.getNextId("combinedProjection");
    _ctx.setNode<EvaluationNode>(projName, projName, std::move(*result), std::move(entry._node));
}

void ABTTransformerVisitor::unsupportedTransformer(const TransformerInterface* transformer) const {
    uasserted(ErrorCodes::InternalErrorNotSupported,
              str::stream() << "Transformer is not supported (code: "
                            << static_cast<int>(transformer->getType()) << ")");
}

void ABTTransformerVisitor::assertSupportedPath(const std::string& path) {
    uassert(ErrorCodes::InternalErrorNotSupported,
            "Projection contains unsupported numeric path component",
            !FieldRef(path).hasNumericPathComponents());
}

/**
 * Handles simple inclusion projections.
 */
void ABTTransformerVisitor::processProjectedPaths(const projection_executor::InclusionNode& node) {
    // For each preserved path, mark that each path element along the field path should be
    // included.
    OrderedPathSet preservedPaths;
    node.reportProjectedPaths(&preservedPaths);

    for (const std::string& preservedPathStr : preservedPaths) {
        assertSupportedPath(preservedPathStr);

        _builder.integrateFieldPath(FieldPath(preservedPathStr),
                                    [](const bool isLastElement, FieldMapEntry& entry) {
                                        entry._hasLeadingObj = true;
                                        entry._hasKeep = true;
                                    });
    }
}

/**
 * Handles renamed fields and computed projections.
 */
void ABTTransformerVisitor::processComputedPaths(const projection_executor::InclusionNode& node,
                                                 const ProjectionName& rootProjection,
                                                 const bool isAddingFields) {
    OrderedPathSet computedPaths;
    StringMap<std::string> renamedPaths;
    node.reportComputedPaths(&computedPaths, &renamedPaths);

    // Handle path renames: essentially single element FieldPath expression.
    for (const auto& renamedPathEntry : renamedPaths) {
        ABT path = translateFieldPath(
            FieldPath(renamedPathEntry.second),
            make<PathIdentity>(),
            [](FieldNameType fieldName, const bool isLastElement, ABT input) {
                return make<PathGet>(
                    std::move(fieldName),
                    isLastElement ? std::move(input)
                                  : make<PathTraverse>(PathTraverse::kUnlimited, std::move(input)));
            });

        auto entry = _ctx.getNode();
        const ProjectionName renamedProjName{_ctx.getNextId("projRenamedPath")};
        _ctx.setNode<EvaluationNode>(
            entry._rootProjection,
            renamedProjName,
            make<EvalPath>(std::move(path), make<Variable>(entry._rootProjection)),
            std::move(entry._node));

        _builder.integrateFieldPath(
            FieldPath(renamedPathEntry.first),
            [&renamedProjName, &isAddingFields](const bool isLastElement, FieldMapEntry& entry) {
                if (!isAddingFields) {
                    entry._hasKeep = true;
                }
                if (isLastElement) {
                    entry._constVarName = renamedProjName;
                    entry._hasTrailingDefault = true;
                }
            });
    }

    // Handle general expression projection.
    for (const std::string& computedPathStr : computedPaths) {
        assertSupportedPath(computedPathStr);

        const FieldPath computedPath(computedPathStr);

        auto entry = _ctx.getNode();
        const ProjectionName getProjName{_ctx.getNextId("projGetPath")};
        ABT getExpr = generateAggExpression(
            node.getExpressionForPath(computedPath).get(), rootProjection, _ctx.getPrefixId());

        _ctx.setNode<EvaluationNode>(std::move(entry._rootProjection),
                                     getProjName,
                                     std::move(getExpr),
                                     std::move(entry._node));

        _builder.integrateFieldPath(
            computedPath,
            [&getProjName, &isAddingFields](const bool isLastElement, FieldMapEntry& entry) {
                if (!isAddingFields) {
                    entry._hasKeep = true;
                }
                if (isLastElement) {
                    entry._constVarName = getProjName;
                    entry._hasTrailingDefault = true;
                }
            });
    }
}

void ABTTransformerVisitor::visitInclusionNode(const projection_executor::InclusionNode& node,
                                               const bool isAddingFields) {
    auto entry = _ctx.getNode();
    const ProjectionName rootProjection{entry._rootProjection};

    processProjectedPaths(node);
    processComputedPaths(node, rootProjection, isAddingFields);
}

void ABTTransformerVisitor::visitExclusionNode(const projection_executor::ExclusionNode& node) {
    // Handle simple exclusion projections: for each excluded path, mark that the last field
    // path element should be dropped.
    OrderedPathSet excludedPaths;
    node.reportProjectedPaths(&excludedPaths);
    for (const std::string& excludedPathStr : excludedPaths) {
        assertSupportedPath(excludedPathStr);
        _builder.integrateFieldPath(FieldPath(excludedPathStr),
                                    [](const bool isLastElement, FieldMapEntry& entry) {
                                        if (isLastElement) {
                                            entry._hasDrop = true;
                                        }
                                    });
    }
}

void translateProjection(AlgebrizerContext& ctx,
                         ProjectionName scanProjName,
                         boost::intrusive_ptr<ExpressionContext> expCtx,
                         const projection_ast::Projection* proj) {

    const auto projExecutor = projection_executor::buildProjectionExecutor(
        expCtx,
        proj,
        ProjectionPolicies::findProjectionPolicies(),
        projection_executor::BuilderParamsBitSet{projection_executor::kDefaultBuilderParams});

    FieldMapBuilder builder(scanProjName, true);
    ABTTransformerVisitor visitor(ctx, builder);
    TransformerInterfaceWalker walker(&visitor);
    walker.walk(projExecutor.get());
    visitor.generateCombinedProjection();
}

}  // namespace mongo::optimizer