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) 2023-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/query/query_shape/find_cmd_shape.h"
#include "mongo/db/query/projection_ast_util.h"
#include "mongo/db/query/query_shape/shape_helpers.h"
namespace mongo::query_shape {
namespace {
BSONObj projectionShape(const boost::optional<projection_ast::Projection>& proj,
const SerializationOptions& opts =
SerializationOptions::kRepresentativeQueryShapeSerializeOptions) {
return proj ? projection_ast::serialize(*proj->root(), opts) : BSONObj();
}
BSONObj sortShape(const boost::optional<SortPattern>& sort,
const SerializationOptions& opts =
SerializationOptions::kRepresentativeQueryShapeSerializeOptions) {
return sort
? sort->serialize(SortPattern::SortKeySerialization::kForPipelineSerialization, opts)
.toBson()
: BSONObj();
}
void maybeAddWithName(const OptionalBool& optBool, BSONObjBuilder& bob, StringData name) {
if (optBool.has_value()) {
bob.append(name, bool(optBool));
}
}
void addRemainingFindCommandFields(const FindCmdShapeComponents& components, BSONObjBuilder& bob) {
maybeAddWithName(components.singleBatch, bob, FindCommandRequest::kSingleBatchFieldName);
maybeAddWithName(components.allowDiskUse, bob, FindCommandRequest::kAllowDiskUseFieldName);
maybeAddWithName(components.returnKey, bob, FindCommandRequest::kReturnKeyFieldName);
maybeAddWithName(components.showRecordId, bob, FindCommandRequest::kShowRecordIdFieldName);
maybeAddWithName(components.tailable, bob, FindCommandRequest::kTailableFieldName);
maybeAddWithName(components.awaitData, bob, FindCommandRequest::kAwaitDataFieldName);
maybeAddWithName(components.oplogReplay, bob, FindCommandRequest::kOplogReplayFieldName);
}
} // namespace
FindCmdShapeComponents::FindCmdShapeComponents(
const ParsedFindCommand& request,
const boost::intrusive_ptr<ExpressionContext>& expCtx,
const SerializationOptions& opts)
: filter(request.filter->serialize(opts)),
projection(projectionShape(request.proj, opts)),
sort(sortShape(request.sort, opts)),
min(shape_helpers::extractMinOrMaxShape(request.findCommandRequest->getMin(), opts)),
max(shape_helpers::extractMinOrMaxShape(request.findCommandRequest->getMax(), opts)),
singleBatch(request.findCommandRequest->getSingleBatch()),
allowDiskUse(request.findCommandRequest->getAllowDiskUse().has_value()
? boost::optional<bool>(bool(request.findCommandRequest->getAllowDiskUse()))
: boost::none),
returnKey(request.findCommandRequest->getReturnKey()),
showRecordId(request.findCommandRequest->getShowRecordId()),
tailable(request.findCommandRequest->getTailable()),
awaitData(request.findCommandRequest->getAwaitData()),
oplogReplay(request.findCommandRequest->getOplogReplay()),
hasField(),
serializationOpts(opts) {
hasField.projection = request.proj.has_value();
hasField.sort = request.sort.has_value();
hasField.limit = request.findCommandRequest->getLimit().has_value();
hasField.skip = request.findCommandRequest->getSkip().has_value();
}
void FindCmdShapeComponents::appendTo(BSONObjBuilder& bob) const {
bob.append("command", "find");
std::unique_ptr<MatchExpression> filterExpr;
// Filter.
bob.append(FindCommandRequest::kFilterFieldName, filter);
if (hasField.projection) {
bob.append(FindCommandRequest::kProjectionFieldName, projection);
}
if (!max.isEmpty()) {
bob.append(FindCommandRequest::kMaxFieldName, max);
}
if (!min.isEmpty()) {
bob.append(FindCommandRequest::kMinFieldName, min);
}
// Sort.
if (hasField.sort) {
bob.append(FindCommandRequest::kSortFieldName, sort);
}
// The values here don't matter (assuming we're not using the 'kUnchanged' policy).
tassert(7973601,
"Serialization policy not supported - original values have been discarded",
serializationOpts.literalPolicy != LiteralSerializationPolicy::kUnchanged);
if (hasField.limit) {
serializationOpts.appendLiteral(&bob, FindCommandRequest::kLimitFieldName, 1ll);
}
if (hasField.skip) {
serializationOpts.appendLiteral(&bob, FindCommandRequest::kSkipFieldName, 1ll);
}
// Add the fields that require no transformation.
addRemainingFindCommandFields(*this, bob);
}
void FindCmdShapeComponents::HashValue(absl::HashState state) const {
absl::HashState::combine(std::move(state),
simpleHash(filter),
simpleHash(projection),
simpleHash(sort),
simpleHash(min),
simpleHash(max),
singleBatch,
allowDiskUse,
returnKey,
showRecordId,
tailable,
awaitData,
oplogReplay,
hasField);
}
std::unique_ptr<FindCommandRequest> FindCmdShape::toFindCommandRequest() const {
auto fcr = std::make_unique<FindCommandRequest>(nssOrUUID);
fcr->setFilter(components.filter);
if (components.hasField.projection)
fcr->setProjection(components.projection);
if (components.hasField.sort)
fcr->setSort(components.sort);
fcr->setMin(components.min);
fcr->setMax(components.max);
// Doesn't matter what value to use for limit and skip in the context of a shape.
if (components.hasField.limit)
fcr->setLimit(1ll);
if (components.hasField.skip)
fcr->setSkip(1ll);
// All the booleans.
if (components.singleBatch.has_value())
fcr->setSingleBatch(bool(components.singleBatch));
if (components.allowDiskUse.has_value())
fcr->setAllowDiskUse(bool(components.allowDiskUse));
if (components.returnKey.has_value())
fcr->setReturnKey(bool(components.returnKey));
if (components.showRecordId.has_value())
fcr->setShowRecordId(bool(components.showRecordId));
if (components.tailable.has_value())
fcr->setTailable(bool(components.tailable));
if (components.awaitData.has_value())
fcr->setAwaitData(bool(components.awaitData));
if (components.oplogReplay.has_value())
fcr->setOplogReplay(bool(components.oplogReplay));
// Common shape components.
if (_let.hasLet)
fcr->setLet(_let.shapifiedLet);
if (!collation.isEmpty())
fcr->setCollation(collation);
return fcr;
}
FindCmdShape::FindCmdShape(const ParsedFindCommand& findRequest,
const boost::intrusive_ptr<ExpressionContext>& expCtx)
: CmdWithLetShape(findRequest.findCommandRequest->getLet(),
expCtx,
components,
findRequest.findCommandRequest->getNamespaceOrUUID(),
findRequest.findCommandRequest->getCollation()),
components(findRequest, expCtx) {}
void FindCmdShape::appendLetCmdSpecificShapeComponents(
BSONObjBuilder& bob,
const boost::intrusive_ptr<ExpressionContext>& expCtx,
const SerializationOptions& opts) const {
if (opts == SerializationOptions::kRepresentativeQueryShapeSerializeOptions) {
// Fast path: we already have this.
return components.appendTo(bob);
} else {
// Slow path: we need to re-parse from our representative shapes.
auto request = uassertStatusOKWithContext(
parsed_find_command::parse(expCtx,
toFindCommandRequest(),
ExtensionsCallbackNoop(),
MatchExpressionParser::kAllowAllSpecialFeatures),
"Could not re-parse a representative query shape");
// This constructor will shapify according to the options.
FindCmdShapeComponents{*request, expCtx, opts}.appendTo(bob);
}
}
} // namespace mongo::query_shape
|