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
|
/**
* 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.
*/
#pragma once
#include "mongo/platform/decimal128.h"
#include <ostream>
#include "mongo/db/exec/sbe/values/value.h"
#include "mongo/db/query/optimizer/syntax/syntax.h"
namespace mongo::optimizer {
/**
* Marker class for expressions. Mutually exclusive with paths and nodes.
*/
class ExpressionSyntaxSort {};
/**
* Holds a constant SBE value with corresponding type tag.
*/
class Constant final : public ABTOpFixedArity<0>, public ExpressionSyntaxSort {
public:
Constant(sbe::value::TypeTags tag, sbe::value::Value val);
static ABT createFromCopy(sbe::value::TypeTags tag, sbe::value::Value val);
static ABT str(StringData str);
static ABT int32(int32_t valueInt32);
static ABT int64(int64_t valueInt64);
static ABT fromDouble(double value);
static ABT fromDecimal(const Decimal128& value);
static ABT emptyObject();
static ABT emptyArray();
static ABT timestamp(const Timestamp& t);
static ABT date(const Date_t& d);
static ABT nothing();
static ABT null();
static ABT boolean(bool b);
static ABT minKey();
static ABT maxKey();
~Constant();
Constant(const Constant& other);
Constant(Constant&& other) noexcept;
bool operator==(const Constant& other) const;
auto get() const {
return std::pair{_tag, _val};
}
bool isString() const;
StringData getString() const;
bool isValueInt64() const;
int64_t getValueInt64() const;
bool isValueInt32() const;
int32_t getValueInt32() const;
bool isValueDouble() const;
double getValueDouble() const;
bool isValueDecimal() const;
Decimal128 getValueDecimal() const;
bool isValueBool() const;
bool getValueBool() const;
bool isNumber() const {
return sbe::value::isNumber(_tag);
}
bool isNothing() const {
return _tag == sbe::value::TypeTags::Nothing;
}
bool isNull() const {
return _tag == sbe::value::TypeTags::Null;
}
bool isObject() const {
return _tag == sbe::value::TypeTags::Object;
}
bool isArray() const {
return _tag == sbe::value::TypeTags::Array;
}
private:
sbe::value::TypeTags _tag;
sbe::value::Value _val;
};
/**
* Represents a reference to a binding. The binding is specified by identifier (string). The logic
* for checking that the reference is valid (e.g., that the referenced binding is in scope) happens
* elsewhere.
*/
class Variable final : public ABTOpFixedArity<0>, public ExpressionSyntaxSort {
ProjectionName _name;
public:
Variable(ProjectionName inName) : _name(std::move(inName)) {}
bool operator==(const Variable& other) const {
return _name == other._name;
}
auto& name() const {
return _name;
}
};
/**
* Models arithmetic and other operations that accept a single argument, for instance negate.
*/
class UnaryOp final : public ABTOpFixedArity<1>, public ExpressionSyntaxSort {
using Base = ABTOpFixedArity<1>;
Operations _op;
public:
UnaryOp(Operations inOp, ABT inExpr) : Base(std::move(inExpr)), _op(inOp) {
tassert(6684501, "Unary op expected", isUnaryOp(_op));
assertExprSort(getChild());
}
bool operator==(const UnaryOp& other) const {
return _op == other._op && getChild() == other.getChild();
}
auto op() const {
return _op;
}
const ABT& getChild() const {
return get<0>();
}
ABT& getChild() {
return get<0>();
}
};
/**
* Models arithmetic, comparison, or logical operations that take two arguments, for instance add or
* subtract.
*/
class BinaryOp final : public ABTOpFixedArity<2>, public ExpressionSyntaxSort {
using Base = ABTOpFixedArity<2>;
Operations _op;
public:
BinaryOp(Operations inOp, ABT inLhs, ABT inRhs)
: Base(std::move(inLhs), std::move(inRhs)), _op(inOp) {
tassert(6684502, "Binary op expected", isBinaryOp(_op));
assertExprSort(getLeftChild());
assertExprSort(getRightChild());
}
bool operator==(const BinaryOp& other) const {
return _op == other._op && getLeftChild() == other.getLeftChild() &&
getRightChild() == other.getRightChild();
}
auto op() const {
return _op;
}
const ABT& getLeftChild() const {
return get<0>();
}
const ABT& getRightChild() const {
return get<1>();
}
};
/**
* Branching operator with a condition expression, "then" expression, and an "else" expression.
*/
class If final : public ABTOpFixedArity<3>, public ExpressionSyntaxSort {
using Base = ABTOpFixedArity<3>;
public:
If(ABT inCond, ABT inThen, ABT inElse)
: Base(std::move(inCond), std::move(inThen), std::move(inElse)) {
assertExprSort(getCondChild());
assertExprSort(getThenChild());
assertExprSort(getElseChild());
}
bool operator==(const If& other) const {
return getCondChild() == other.getCondChild() && getThenChild() == other.getThenChild() &&
getElseChild() == other.getElseChild();
}
const ABT& getCondChild() const {
return get<0>();
}
const ABT& getThenChild() const {
return get<1>();
}
const ABT& getElseChild() const {
return get<2>();
}
};
/**
* Defines a variable from one expression and a specified name which is available to be referenced
* in a second expression.
*/
class Let final : public ABTOpFixedArity<2>, public ExpressionSyntaxSort {
using Base = ABTOpFixedArity<2>;
ProjectionName _varName;
public:
Let(ProjectionName var, ABT inBind, ABT inExpr)
: Base(std::move(inBind), std::move(inExpr)), _varName(std::move(var)) {
assertExprSort(bind());
assertExprSort(in());
}
bool operator==(const Let& other) const {
return _varName == other._varName && bind() == other.bind() && in() == other.in();
}
auto& varName() const {
return _varName;
}
const ABT& bind() const {
return get<0>();
}
const ABT& in() const {
return get<1>();
}
};
/**
* Represents a single argument lambda. Defines a local variable (representing the argument) which
* can be referenced within the lambda. The variable takes on the value to which LambdaAbstraction
* is applied by its parent.
*/
class LambdaAbstraction final : public ABTOpFixedArity<1>, public ExpressionSyntaxSort {
using Base = ABTOpFixedArity<1>;
ProjectionName _varName;
public:
LambdaAbstraction(ProjectionName var, ABT inBody)
: Base(std::move(inBody)), _varName(std::move(var)) {
assertExprSort(getBody());
}
bool operator==(const LambdaAbstraction& other) const {
return _varName == other._varName && getBody() == other.getBody();
}
auto& varName() const {
return _varName;
}
const ABT& getBody() const {
return get<0>();
}
ABT& getBody() {
return get<0>();
}
};
/**
* Evaluates an expression representing a function over an expression representing the argument to
* the function.
*/
class LambdaApplication final : public ABTOpFixedArity<2>, public ExpressionSyntaxSort {
using Base = ABTOpFixedArity<2>;
public:
LambdaApplication(ABT inLambda, ABT inArgument)
: Base(std::move(inLambda), std::move(inArgument)) {
assertExprSort(getLambda());
assertExprSort(getArgument());
}
bool operator==(const LambdaApplication& other) const {
return getLambda() == other.getLambda() && getArgument() == other.getArgument();
}
const ABT& getLambda() const {
return get<0>();
}
const ABT& getArgument() const {
return get<1>();
}
};
/**
* Dynamic arity operator which passes its children as arguments to a function specified by SBE
* function expression name.
*/
class FunctionCall final : public ABTOpDynamicArity<0>, public ExpressionSyntaxSort {
using Base = ABTOpDynamicArity<0>;
std::string _name;
public:
FunctionCall(std::string inName, ABTVector inArgs)
: Base(std::move(inArgs)), _name(std::move(inName)) {
for (auto& a : nodes()) {
assertExprSort(a);
}
}
bool operator==(const FunctionCall& other) const {
return _name == other._name && nodes() == other.nodes();
}
auto& name() const {
return _name;
}
};
/**
* EvalPath defines one context for path behavior used for manipulation and replacement. Some path
* elements have special behavior under this context.
*
* EvalPath evaluates its path child according to its context over the result of its expression
* child. That is, the expression is evaluated first, and it is used as an input value to the root
* path element, which can then continue the computation recursively.
*/
class EvalPath final : public ABTOpFixedArity<2>, public ExpressionSyntaxSort {
using Base = ABTOpFixedArity<2>;
public:
EvalPath(ABT inPath, ABT inInput) : Base(std::move(inPath), std::move(inInput)) {
assertPathSort(getPath());
assertExprSort(getInput());
}
bool operator==(const EvalPath& other) const {
return getPath() == other.getPath() && getInput() == other.getInput();
}
const ABT& getPath() const {
return get<0>();
}
ABT& getPath() {
return get<0>();
}
const ABT& getInput() const {
return get<1>();
}
};
/**
* EvalFilter defines a context for path behavior used to evaluate boolean conditions for the
* purposes of filtering. Some path elements have special behavior under this context.
*
* The intermediate result of all path operators in the path child of an EvalFilter should evaluate
* to true, false, or Nothing. It is assumed that that Nothing is equivalent to false within an
* EvalFilter context.
*/
class EvalFilter final : public ABTOpFixedArity<2>, public ExpressionSyntaxSort {
using Base = ABTOpFixedArity<2>;
public:
EvalFilter(ABT inPath, ABT inInput) : Base(std::move(inPath), std::move(inInput)) {
assertPathSort(getPath());
assertExprSort(getInput());
}
bool operator==(const EvalFilter& other) const {
return getPath() == other.getPath() && getInput() == other.getInput();
}
const ABT& getPath() const {
return get<0>();
}
ABT& getPath() {
return get<0>();
}
const ABT& getInput() const {
return get<1>();
}
ABT& getInput() {
return get<1>();
}
};
/**
* Represents a source of values typically from a collection.
*/
class Source final : public ABTOpFixedArity<0>, public ExpressionSyntaxSort {
public:
bool operator==(const Source& other) const {
return true;
}
};
} // namespace mongo::optimizer
|