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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/**
* 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/bson/bsonmisc.h"
#include "mongo/db/exec/document_value/document.h"
#include "mongo/db/exec/document_value/document_value_test_util.h"
#include "mongo/db/exec/document_value/value_comparator.h"
#include "mongo/db/json.h"
#include "mongo/db/pipeline/expression.h"
#include "mongo/db/pipeline/expression_context_for_test.h"
#include "mongo/dbtests/dbtests.h"
#include "mongo/unittest/unittest.h"
namespace mongo {
namespace ExpressionTests {
namespace {
using boost::intrusive_ptr;
/** Convert Value to a wrapped BSONObj with an empty string field name. */
static BSONObj toBson(const Value& value) {
BSONObjBuilder bob;
value.addToBsonObj(&bob, "");
return bob.obj();
}
/** Convert Document to BSON. */
static BSONObj toBson(const Document& document) {
return document.toBson();
}
/** Create a Document from a BSONObj. */
Document fromBson(BSONObj obj) {
return Document(obj);
}
namespace FieldPath {
/** The provided field path does not pass validation. */
class Invalid {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
ASSERT_THROWS(ExpressionFieldPath::deprecatedCreate(&expCtx, ""), AssertionException);
}
};
TEST(FieldPath, NoOptimizationForRootFieldPathWithDottedPath) {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<ExpressionFieldPath> expression =
ExpressionFieldPath::parse(&expCtx, "$$ROOT.x.y", expCtx.variablesParseState);
// An attempt to optimize returns the Expression itself.
ASSERT_EQUALS(expression, expression->optimize());
}
TEST(FieldPath, NoOptimizationForCurrentFieldPathWithDottedPath) {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<ExpressionFieldPath> expression =
ExpressionFieldPath::parse(&expCtx, "$$CURRENT.x.y", expCtx.variablesParseState);
// An attempt to optimize returns the Expression itself.
ASSERT_EQUALS(expression, expression->optimize());
}
TEST(FieldPath, RemoveOptimizesToMissingValue) {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<ExpressionFieldPath> expression =
ExpressionFieldPath::parse(&expCtx, "$$REMOVE", expCtx.variablesParseState);
auto optimizedExpr = expression->optimize();
ASSERT_VALUE_EQ(
Value(),
optimizedExpr->evaluate(Document(BSON("x" << BSON("y" << 123))), &expCtx.variables));
}
TEST(FieldPath, NoOptimizationOnNormalPath) {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression = ExpressionFieldPath::deprecatedCreate(&expCtx, "a");
// An attempt to optimize returns the Expression itself.
ASSERT_EQUALS(expression, expression->optimize());
}
TEST(FieldPath, OptimizeOnVariableWithConstantScalarValue) {
auto expCtx = ExpressionContextForTest{};
auto varId = expCtx.variablesParseState.defineVariable("userVar");
expCtx.variables.setConstantValue(varId, Value(123));
auto expr = ExpressionFieldPath::parse(&expCtx, "$$userVar", expCtx.variablesParseState);
ASSERT_TRUE(dynamic_cast<ExpressionFieldPath*>(expr.get()));
auto optimizedExpr = expr->optimize();
ASSERT_TRUE(dynamic_cast<ExpressionConstant*>(optimizedExpr.get()));
}
TEST(FieldPath, OptimizeOnVariableWithConstantArrayValue) {
auto expCtx = ExpressionContextForTest{};
auto varId = expCtx.variablesParseState.defineVariable("userVar");
expCtx.variables.setConstantValue(varId, Value(BSON_ARRAY(1 << 2 << 3)));
auto expr = ExpressionFieldPath::parse(&expCtx, "$$userVar", expCtx.variablesParseState);
ASSERT_TRUE(dynamic_cast<ExpressionFieldPath*>(expr.get()));
auto optimizedExpr = expr->optimize();
auto constantExpr = dynamic_cast<ExpressionConstant*>(optimizedExpr.get());
ASSERT_TRUE(constantExpr);
ASSERT_VALUE_EQ(Value(BSON_ARRAY(1 << 2 << 3)), constantExpr->getValue());
}
TEST(FieldPath, OptimizeToEmptyArrayOnNumericalPathComponentAndConstantArrayValue) {
auto expCtx = ExpressionContextForTest{};
auto varId = expCtx.variablesParseState.defineVariable("userVar");
expCtx.variables.setConstantValue(varId, Value(BSON_ARRAY(1 << 2 << 3)));
auto expr = ExpressionFieldPath::parse(&expCtx, "$$userVar.1", expCtx.variablesParseState);
ASSERT_TRUE(dynamic_cast<ExpressionFieldPath*>(expr.get()));
auto optimizedExpr = expr->optimize();
auto constantExpr = dynamic_cast<ExpressionConstant*>(optimizedExpr.get());
ASSERT_TRUE(constantExpr);
ASSERT_VALUE_EQ(Value(BSONArray()), constantExpr->getValue());
}
TEST(FieldPath, OptimizeOnVariableWithConstantValueAndDottedPath) {
auto expCtx = ExpressionContextForTest{};
auto varId = expCtx.variablesParseState.defineVariable("userVar");
expCtx.variables.setConstantValue(varId, Value(Document{{"x", Document{{"y", 123}}}}));
auto expr = ExpressionFieldPath::parse(&expCtx, "$$userVar.x.y", expCtx.variablesParseState);
ASSERT_TRUE(dynamic_cast<ExpressionFieldPath*>(expr.get()));
auto optimizedExpr = expr->optimize();
auto constantExpr = dynamic_cast<ExpressionConstant*>(optimizedExpr.get());
ASSERT_TRUE(constantExpr);
ASSERT_VALUE_EQ(Value(123), constantExpr->getValue());
}
TEST(FieldPath, NoOptimizationOnVariableWithNoValue) {
auto expCtx = ExpressionContextForTest{};
expCtx.variablesParseState.defineVariable("userVar");
auto expr = ExpressionFieldPath::parse(&expCtx, "$$userVar", expCtx.variablesParseState);
ASSERT_TRUE(dynamic_cast<ExpressionFieldPath*>(expr.get()));
auto optimizedExpr = expr->optimize();
ASSERT_FALSE(dynamic_cast<ExpressionConstant*>(optimizedExpr.get()));
}
TEST(FieldPath, NoOptimizationOnVariableWithMissingValue) {
auto expCtx = ExpressionContextForTest{};
auto varId = expCtx.variablesParseState.defineVariable("userVar");
expCtx.variables.setValue(varId, Value());
auto expr = ExpressionFieldPath::parse(&expCtx, "$$userVar", expCtx.variablesParseState);
ASSERT_TRUE(dynamic_cast<ExpressionFieldPath*>(expr.get()));
auto optimizedExpr = expr->optimize();
ASSERT_FALSE(dynamic_cast<ExpressionConstant*>(optimizedExpr.get()));
}
TEST(FieldPath, ScalarVariableWithDottedFieldPathOptimizesToConstantMissingValue) {
auto expCtx = ExpressionContextForTest{};
auto varId = expCtx.variablesParseState.defineVariable("userVar");
expCtx.variables.setConstantValue(varId, Value(123));
auto expr = ExpressionFieldPath::parse(&expCtx, "$$userVar.x.y", expCtx.variablesParseState);
ASSERT_TRUE(dynamic_cast<ExpressionFieldPath*>(expr.get()));
auto optimizedExpr = expr->optimize();
auto constantExpr = dynamic_cast<ExpressionConstant*>(optimizedExpr.get());
ASSERT_TRUE(constantExpr);
ASSERT_VALUE_EQ(Value(), constantExpr->getValue());
}
/** The field path itself is a dependency. */
class Dependencies {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
DepsTracker dependencies;
expression->addDependencies(&dependencies);
ASSERT_EQUALS(1U, dependencies.fields.size());
ASSERT_EQUALS(1U, dependencies.fields.count("a.b"));
ASSERT_EQUALS(false, dependencies.needWholeDocument);
ASSERT_EQUALS(false, dependencies.getNeedsAnyMetadata());
}
};
/** Field path target field is missing. */
class Missing {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression = ExpressionFieldPath::deprecatedCreate(&expCtx, "a");
ASSERT_BSONOBJ_BINARY_EQ(fromjson("{}"),
toBson(expression->evaluate({}, &expCtx.variables)));
}
};
/** Simple case where the target field is present. */
class Present {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression = ExpressionFieldPath::deprecatedCreate(&expCtx, "a");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{'':123}"),
toBson(expression->evaluate(fromBson(BSON("a" << 123)), &expCtx.variables)));
}
};
/** Target field parent is null. */
class NestedBelowNull {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{}"),
toBson(expression->evaluate(fromBson(fromjson("{a:null}")), &expCtx.variables)));
}
};
/** Target field parent is undefined. */
class NestedBelowUndefined {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{}"),
toBson(expression->evaluate(fromBson(fromjson("{a:undefined}")), &expCtx.variables)));
}
};
/** Target field parent is missing. */
class NestedBelowMissing {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{}"),
toBson(expression->evaluate(fromBson(fromjson("{z:1}")), &expCtx.variables)));
}
};
/** Target field parent is an integer. */
class NestedBelowInt {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{}"),
toBson(expression->evaluate(fromBson(BSON("a" << 2)), &expCtx.variables)));
}
};
/** A value in a nested object. */
class NestedValue {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(BSON("" << 55),
toBson(expression->evaluate(fromBson(BSON("a" << BSON("b" << 55))),
&expCtx.variables)));
}
};
/** Target field within an empty object. */
class NestedBelowEmptyObject {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{}"),
toBson(expression->evaluate(fromBson(BSON("a" << BSONObj())), &expCtx.variables)));
}
};
/** Target field within an empty array. */
class NestedBelowEmptyArray {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
BSON("" << BSONArray()),
toBson(expression->evaluate(fromBson(BSON("a" << BSONArray())), &expCtx.variables)));
}
};
/** Target field within an array containing null. */
class NestedBelowArrayWithNull {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{'':[]}"),
toBson(expression->evaluate(fromBson(fromjson("{a:[null]}")), &expCtx.variables)));
}
};
/** Target field within an array containing undefined. */
class NestedBelowArrayWithUndefined {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{'':[]}"),
toBson(expression->evaluate(fromBson(fromjson("{a:[undefined]}")), &expCtx.variables)));
}
};
/** Target field within an array containing an integer. */
class NestedBelowArrayWithInt {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{'':[]}"),
toBson(expression->evaluate(fromBson(fromjson("{a:[1]}")), &expCtx.variables)));
}
};
/** Target field within an array. */
class NestedWithinArray {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{'':[9]}"),
toBson(expression->evaluate(fromBson(fromjson("{a:[{b:9}]}")), &expCtx.variables)));
}
};
/** Multiple value types within an array. */
class MultipleArrayValues {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{'':[9,20]}"),
toBson(expression->evaluate(
fromBson(fromjson("{a:[{b:9},null,undefined,{g:4},{b:20},{}]}")),
&expCtx.variables)));
}
};
/** Expanding values within nested arrays. */
class ExpandNestedArrays {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b.c");
ASSERT_BSONOBJ_BINARY_EQ(
fromjson("{'':[[1,2],3,[4],[[5]],[6,7]]}"),
toBson(expression->evaluate(fromBson(fromjson("{a:[{b:[{c:1},{c:2}]},"
"{b:{c:3}},"
"{b:[{c:4}]},"
"{b:[{c:[5]}]},"
"{b:{c:[6,7]}}]}")),
&expCtx.variables)));
}
};
/** Add to a BSONObj. */
class AddToBsonObj {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b.c");
ASSERT_BSONOBJ_BINARY_EQ(BSON("foo"
<< "$a.b.c"),
BSON("foo" << expression->serialize(false)));
}
};
/** Add to a BSONArray. */
class AddToBsonArray {
public:
void run() {
auto expCtx = ExpressionContextForTest{};
intrusive_ptr<Expression> expression =
ExpressionFieldPath::deprecatedCreate(&expCtx, "a.b.c");
BSONArrayBuilder bab;
bab << expression->serialize(false);
ASSERT_BSONOBJ_BINARY_EQ(BSON_ARRAY("$a.b.c"), bab.arr());
}
};
} // namespace FieldPath
class All : public OldStyleSuiteSpecification {
public:
All() : OldStyleSuiteSpecification("expression") {}
void setupTests() {
add<FieldPath::Invalid>();
add<FieldPath::Dependencies>();
add<FieldPath::Missing>();
add<FieldPath::Present>();
add<FieldPath::NestedBelowNull>();
add<FieldPath::NestedBelowUndefined>();
add<FieldPath::NestedBelowMissing>();
add<FieldPath::NestedBelowInt>();
add<FieldPath::NestedValue>();
add<FieldPath::NestedBelowEmptyObject>();
add<FieldPath::NestedBelowEmptyArray>();
add<FieldPath::NestedBelowEmptyArray>();
add<FieldPath::NestedBelowArrayWithNull>();
add<FieldPath::NestedBelowArrayWithUndefined>();
add<FieldPath::NestedBelowArrayWithInt>();
add<FieldPath::NestedWithinArray>();
add<FieldPath::MultipleArrayValues>();
add<FieldPath::ExpandNestedArrays>();
add<FieldPath::AddToBsonObj>();
add<FieldPath::AddToBsonArray>();
}
};
OldStyleSuiteInitializer<All> fieldPathAll;
} // namespace
} // namespace ExpressionTests
} // namespace mongo
|