summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/optimizer/algebra/algebra_test.cpp
blob: 013c5a67568eb2efe8ba15d8fbb723deeede22f8 (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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/**
 *    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/query/optimizer/algebra/operator.h"
#include "mongo/db/query/optimizer/algebra/polyvalue.h"
#include "mongo/unittest/unittest.h"

namespace mongo::optimizer::algebra {

namespace {

class Leaf;
class BinaryNode;
class NaryNode;
class AtLeastBinaryNode;
using Tree = PolyValue<Leaf, BinaryNode, NaryNode, AtLeastBinaryNode>;

/**
 * A leaf in the tree. Just contains data - in this case a double.
 */
class Leaf : public OpSpecificArity<Tree, 0> {
public:
    Leaf(double x) : x(x) {}

    double x;
};

/**
 * An inner node in the tree with exactly two children.
 */
class BinaryNode : public OpSpecificArity<Tree, 2> {
public:
    BinaryNode(Tree left, Tree right)
        : OpSpecificArity<Tree, 2>(std::move(left), std::move(right)) {}
};

/**
 * An inner node in the tree with any number of children, zero or greater.
 */
class NaryNode : public OpSpecificDynamicArity<Tree, 0> {
public:
    NaryNode(std::vector<Tree> children) : OpSpecificDynamicArity<Tree, 0>(std::move(children)) {}
};

/**
 * An inner node in the tree with 2 or more nodes.
 */
class AtLeastBinaryNode : public OpSpecificDynamicArity<Tree, 2> {
public:
    /**
     * Notice the required number of nodes are given as separate arguments from the vector.
     */
    AtLeastBinaryNode(std::vector<Tree> children, Tree left, Tree right)
        : OpSpecificDynamicArity<Tree, 2>(std::move(children), std::move(left), std::move(right)) {}
};

/**
 * A visitor of the tree with methods to visit each kind of node.
 *
 * This is a very basic visitor to just demonstrate the transport() API - all it does is sum up
 * doubles in the leaf nodes of the tree.
 *
 * Notice that each kind of node did not need to fill out some boilerplate "visit()" method or
 * anything like that. The PolyValue templating magic took care of all the boilerplate for us, and
 * the operator classes (e.g. OpSpecificArity) exposes the tree structure and children.
 */
class NodeTransporter {
public:
    double transport(Leaf& leaf) {
        return leaf.x;
    }
    double transport(BinaryNode& node, double child0, double child1) {
        return child0 + child1;
    }
    double transport(NaryNode& node, std::vector<double> children) {
        return std::accumulate(children.begin(), children.end(), 0.0);
    }
    double transport(AtLeastBinaryNode& node,
                     std::vector<double> children,
                     double child0,
                     double child1) {
        return child0 + child1 + std::accumulate(children.begin(), children.end(), 0.0);
    }
};

/**
 * A visitor of the tree with methods to visit each kind of node. This visitor also takes a
 * reference to the Tree itself. Unused here, this reference can be used to mutate or replace the
 * node itself while the walking takes place.
 */
class TreeTransporter {
public:
    double transport(Tree& tree, Leaf& leaf) {
        return leaf.x;
    }
    double transport(Tree& tree, BinaryNode& node, double child0, double child1) {
        return child0 + child1;
    }
    double transport(Tree& tree, NaryNode& node, std::vector<double> children) {
        return std::accumulate(children.begin(), children.end(), 0.0);
    }
    double transport(Tree& tree,
                     AtLeastBinaryNode& node,
                     std::vector<double> children,
                     double child0,
                     double child1) {
        return child0 + child1 + std::accumulate(children.begin(), children.end(), 0.0);
    }
};

TEST(PolyValueTest, SumTransportFixedArity) {
    NodeTransporter nodeTransporter;
    TreeTransporter treeTransporter;
    {
        Tree simple = Tree::make<BinaryNode>(Tree::make<Leaf>(2.0), Tree::make<Leaf>(1.0));
        // Notice the template parameter true or false matches whether the walker expects to have a
        // Tree& parameter first in the transport implementations.
        double result = transport<false>(simple, nodeTransporter);
        ASSERT_EQ(result, 3.0);
        // This 'true' template means we expect the 'Tree&' argument to come first in all the
        // 'transport()' implementations.
        result = transport<true>(simple, treeTransporter);
        ASSERT_EQ(result, 3.0);
    }

    {
        Tree deeper = Tree::make<BinaryNode>(
            Tree::make<BinaryNode>(Tree::make<Leaf>(2.0), Tree::make<Leaf>(1.0)),
            Tree::make<BinaryNode>(Tree::make<Leaf>(2.0), Tree::make<Leaf>(1.0)));
        double result = transport<false>(deeper, nodeTransporter);
        ASSERT_EQ(result, 6.0);
        result = transport<true>(deeper, treeTransporter);
        ASSERT_EQ(result, 6.0);
    }
}

/**
 * Prove out that the walking/visiting can hit the variadic NaryNode.
 */
TEST(PolyValueTest, SumTransportVariadic) {
    NodeTransporter nodeTransporter;
    TreeTransporter treeTransporter;
    Tree naryDemoTree = Tree::make<NaryNode>(
        std::vector<Tree>{Tree::make<Leaf>(6.0),
                          Tree::make<Leaf>(5.0),
                          Tree::make<NaryNode>(std::vector<Tree>{
                              Tree::make<Leaf>(4.0), Tree::make<Leaf>(3.0), Tree::make<Leaf>(2.0)}),
                          Tree::make<Leaf>(1.0)});

    double result = transport<false>(naryDemoTree, nodeTransporter);
    ASSERT_EQ(result, 21.0);
    result = transport<true>(naryDemoTree, treeTransporter);
    ASSERT_EQ(result, 21.0);
}

TEST(PolyValueTest, SumTransportAtLeast2Children) {
    NodeTransporter nodeTransporter;
    TreeTransporter treeTransporter;
    Tree demoTree = Tree::make<AtLeastBinaryNode>(
        std::vector<Tree>{Tree::make<Leaf>(7.0), Tree::make<Leaf>(6.0)},
        Tree::make<Leaf>(5.0),
        Tree::make<AtLeastBinaryNode>(
            std::vector<Tree>{Tree::make<Leaf>(4.0), Tree::make<Leaf>(3.0)},
            Tree::make<Leaf>(2.0),
            Tree::make<Leaf>(1.0)));
    double result = transport<false>(demoTree, nodeTransporter);
    ASSERT_EQ(result, 28.0);
    result = transport<true>(demoTree, treeTransporter);
    ASSERT_EQ(result, 28.0);
}

/**
 * A visitor of the tree like those above but which takes const references so is forbidden from
 * modifying the tree or nodes.
 *
 * This visitor creates a copy of the tree but with the values at the leaves doubled.
 */
class ConstTransporterCopyAndDouble {
public:
    Tree transport(const Leaf& leaf) {
        return Tree::make<Leaf>(2 * leaf.x);
    }
    Tree transport(const BinaryNode& node, Tree child0, Tree child1) {
        return Tree::make<BinaryNode>(std::move(child0), std::move(child1));
    }
    Tree transport(const NaryNode& node, std::vector<Tree> children) {
        return Tree::make<NaryNode>(std::move(children));
    }
    Tree transport(const AtLeastBinaryNode& node,
                   std::vector<Tree> children,
                   Tree child0,
                   Tree child1) {
        return Tree::make<AtLeastBinaryNode>(
            std::move(children), std::move(child0), std::move(child1));
    }

    // Add all the same walkers with the optional 'tree' argument. Note this is also const.
    Tree transport(const Tree& tree, const Leaf& leaf) {
        return Tree::make<Leaf>(2 * leaf.x);
    }
    Tree transport(const Tree& tree, const BinaryNode& node, Tree child0, Tree child1) {
        return Tree::make<BinaryNode>(std::move(child0), std::move(child1));
    }
    Tree transport(const Tree& tree, const NaryNode& node, std::vector<Tree> children) {
        return Tree::make<NaryNode>(std::move(children));
    }
    Tree transport(const Tree& tree,
                   const AtLeastBinaryNode& node,
                   std::vector<Tree> children,
                   Tree child0,
                   Tree child1) {
        return Tree::make<AtLeastBinaryNode>(
            std::move(children), std::move(child0), std::move(child1));
    }
};

TEST(PolyValueTest, CopyAndDoubleTreeConst) {
    // Test that we can create a copy of a tree and walk with a const transporter to provide extra
    // proof that it's actually a deep copy.
    ConstTransporterCopyAndDouble transporter;
    {
        const Tree simple = Tree::make<BinaryNode>(Tree::make<Leaf>(2.0), Tree::make<Leaf>(1.0));
        // Notice 'simple' is const.
        Tree result = transport<false>(simple, transporter);
        BinaryNode* newRoot = result.cast<BinaryNode>();
        ASSERT(newRoot);
        Leaf* newLeafLeft = newRoot->get<0>().cast<Leaf>();
        ASSERT(newLeafLeft);
        ASSERT_EQ(newLeafLeft->x, 4.0);

        Leaf* newLeafRight = newRoot->get<1>().cast<Leaf>();
        ASSERT(newLeafRight);
        ASSERT_EQ(newLeafRight->x, 2.0);
    }
    {
        // Do the same test but walk with the tree reference (pass 'true' to transport).
        const Tree simple = Tree::make<BinaryNode>(Tree::make<Leaf>(2.0), Tree::make<Leaf>(1.0));
        // Notice 'simple' is const.
        Tree result = transport<true>(simple, transporter);
        BinaryNode* newRoot = result.cast<BinaryNode>();
        ASSERT(newRoot);
        Leaf* newLeafLeft = newRoot->get<0>().cast<Leaf>();
        ASSERT(newLeafLeft);
        ASSERT_EQ(newLeafLeft->x, 4.0);

        Leaf* newLeafRight = newRoot->get<1>().cast<Leaf>();
        ASSERT(newLeafRight);
        ASSERT_EQ(newLeafRight->x, 2.0);
    }
}

/**
 * A walker which accumulates all nodes into a std::set to demonstrate which nodes are visited.
 *
 * The order of the visitation is not guaranteed, except that we visit "bottom-up" so leaves must
 * happen before parents. This much must be true since the API to visit a node depends on the
 * results of its children being pre-computed.
 */
class AccumulateToSetTransporter {
public:
    std::set<double> transport(Leaf& leaf) {
        return {leaf.x};
    }

    std::set<double> transport(BinaryNode& node,
                               std::set<double> visitedChild0,
                               std::set<double> visitedChild1) {
        // 'visistedChild0' and 'visitedChild1' represent the accumulated results of their visited
        // numbers. Here we just merge the two.
        std::set<double> merged;
        std::merge(visitedChild0.begin(),
                   visitedChild0.end(),
                   visitedChild1.begin(),
                   visitedChild1.end(),
                   std::inserter(merged, merged.begin()));
        return merged;
    }

    std::set<double> transport(NaryNode& node, std::vector<std::set<double>> childrenVisitedSets) {
        return std::accumulate(childrenVisitedSets.begin(),
                               childrenVisitedSets.end(),
                               std::set<double>{},
                               [](auto&& visited1, auto&& visited2) {
                                   std::set<double> merged;
                                   std::merge(visited1.begin(),
                                              visited1.end(),
                                              visited2.begin(),
                                              visited2.end(),
                                              std::inserter(merged, merged.begin()));
                                   return merged;
                               });
    }

    std::set<double> transport(AtLeastBinaryNode& node,
                               std::vector<std::set<double>> childrenVisitedSets,
                               std::set<double> visitedChild0,
                               std::set<double> visitedChild1) {
        std::set<double> merged;
        std::merge(visitedChild0.begin(),
                   visitedChild0.end(),
                   visitedChild1.begin(),
                   visitedChild1.end(),
                   std::inserter(merged, merged.begin()));

        return std::accumulate(childrenVisitedSets.begin(),
                               childrenVisitedSets.end(),
                               merged,
                               [](auto&& visited1, auto&& visited2) {
                                   std::set<double> merged;
                                   std::merge(visited1.begin(),
                                              visited1.end(),
                                              visited2.begin(),
                                              visited2.end(),
                                              std::inserter(merged, merged.begin()));
                                   return merged;
                               });
    }
};

/**
 * Here we see a test which walks all the various types of nodes at once, and in this case
 * accumulates into a std::set any visited leaves.
 */
TEST(PolyValueTest, AccumulateAllDoubles) {
    AccumulateToSetTransporter nodeTransporter;

    {
        Tree simple = Tree::make<AtLeastBinaryNode>(
            std::vector<Tree>{Tree::make<Leaf>(4.0)},
            Tree::make<Leaf>(3.0),
            Tree::make<BinaryNode>(Tree::make<Leaf>(2.0),
                                   Tree::make<NaryNode>(std::vector<Tree>{Tree::make<Leaf>(1.0)})));
        std::set<double> result = transport<false>(simple, nodeTransporter);
        ASSERT_EQ(result.size(), 4UL);
        ASSERT_EQ(result.count(1.0), 1UL);
        ASSERT_EQ(result.count(2.0), 1UL);
        ASSERT_EQ(result.count(3.0), 1UL);
        ASSERT_EQ(result.count(4.0), 1UL);
    }
    {
        Tree complex = Tree::make<AtLeastBinaryNode>(
            std::vector<Tree>{Tree::make<Leaf>(1.0), Tree::make<Leaf>(2.0)},
            Tree::make<Leaf>(3.0),
            Tree::make<BinaryNode>(
                Tree::make<Leaf>(4.0),
                Tree::make<NaryNode>(std::vector<Tree>{
                    Tree::make<Leaf>(5.0),
                    Tree::make<BinaryNode>(Tree::make<Leaf>(6.0), Tree::make<Leaf>(7.0))})));
        std::set<double> result = transport<false>(complex, nodeTransporter);
        ASSERT_EQ(result.size(), 7UL);
        ASSERT_EQ(result.count(1.0), 1UL);
        ASSERT_EQ(result.count(2.0), 1UL);
        ASSERT_EQ(result.count(3.0), 1UL);
        ASSERT_EQ(result.count(4.0), 1UL);
        ASSERT_EQ(result.count(5.0), 1UL);
        ASSERT_EQ(result.count(6.0), 1UL);
        ASSERT_EQ(result.count(7.0), 1UL);
    }
}


/**
 * A walker which accepts an extra 'multiplier' argument to each transport call.
 */
class NodeTransporterWithExtraArg {
public:
    double transport(Leaf& leaf, double multiplier) {
        return leaf.x * multiplier;
    }
    double transport(BinaryNode& node, double multiplier, double child0, double child1) {
        return child0 +
            child1;  // No need to apply multiplier here, would be applied in the children already.
    }
    double transport(NaryNode& node, double multiplier, std::vector<double> children) {
        return std::accumulate(children.begin(), children.end(), 0);
    }
    double transport(AtLeastBinaryNode& node,
                     double multiplier,
                     std::vector<double> children,
                     double child0,
                     double child1) {
        return child0 + child1 + std::accumulate(children.begin(), children.end(), 0);
    }
};

TEST(PolyValueTest, TransporterWithAnExtrArgument) {
    NodeTransporterWithExtraArg nodeTransporter;

    Tree simple = Tree::make<AtLeastBinaryNode>(
        std::vector<Tree>{Tree::make<Leaf>(4.0)},
        Tree::make<Leaf>(3.0),
        Tree::make<BinaryNode>(Tree::make<Leaf>(2.0),
                               Tree::make<NaryNode>(std::vector<Tree>{Tree::make<Leaf>(1.0)})));
    double result = transport<false>(simple, nodeTransporter, 2.0);
    ASSERT_EQ(result, 20.0);
}

/**
 * A simple walker which trackes whether it has seen a zero. While the task is simple, this walker
 * demosntrates:
 *  - A walker with state attached ('iHaveSeenAZero'). Note it could be done without tracking state
 *    also.
 *  - The capability of 'transport' to return void.
 *  - You can add a templated 'transport()' to avoid needing to fill in each and every instantiation
 * for the PolyValue.
 */
class TemplatedNodeTransporterWithContext {
public:
    bool iHaveSeenAZero = false;

    void transport(Leaf& leaf) {
        if (leaf.x == 0.0) {
            iHaveSeenAZero = true;
        }
    }

    /**
     * Template to handle all other cases - we don't care or need to do anything here, so we knock
     * out all the other required implementations at once with this template.
     */
    template <typename T, typename... Args>
    void transport(T&& node, Args&&... args) {
        return;
    }
};

TEST(PolyValueTest, TransporterTrackingState) {
    TemplatedNodeTransporterWithContext templatedNodeTransporter;

    Tree noZero = Tree::make<AtLeastBinaryNode>(
        std::vector<Tree>{Tree::make<Leaf>(4.0)},
        Tree::make<Leaf>(3.0),
        Tree::make<BinaryNode>(Tree::make<Leaf>(2.0),
                               Tree::make<NaryNode>(std::vector<Tree>{Tree::make<Leaf>(1.0)})));
    transport<false>(noZero, templatedNodeTransporter);
    ASSERT_EQ(templatedNodeTransporter.iHaveSeenAZero, false);

    Tree yesZero = Tree::make<AtLeastBinaryNode>(
        std::vector<Tree>{Tree::make<Leaf>(3.0)},
        Tree::make<Leaf>(2.0),
        Tree::make<BinaryNode>(Tree::make<Leaf>(1.0),
                               Tree::make<NaryNode>(std::vector<Tree>{Tree::make<Leaf>(0.0)})));
    transport<false>(yesZero, templatedNodeTransporter);
    ASSERT_EQ(templatedNodeTransporter.iHaveSeenAZero, true);
}

/**
 * A walker demonstrating the 'prepare()' API which tracks the depth and weights things deeper in
 * the tree at factors of 10 higher. So the top level is worth 1x, second level 10x, third level
 * 100x, etc.
 */
class NodeTransporterTrackingDepth {
    int _depthMultiplier = 1;

public:
    double transport(Leaf& leaf) {
        return leaf.x * _depthMultiplier;
    }

    void prepare(Leaf&) {
        // Noop. Just here to prevent from yet another 10x multiplication if we were to fall into
        // the generic 'prepare()'.
    }

    /**
     * 'prepare()' is called as we descend the tree before we walk/visit the children.
     */
    template <typename T, typename... Args>
    void prepare(T&& node, Args&&... args) {
        _depthMultiplier *= 10;
    }

    double transport(BinaryNode& node, double child0, double child1) {
        _depthMultiplier /= 10;
        return child0 + child1;
    }
    double transport(NaryNode& node, std::vector<double> children) {
        _depthMultiplier /= 10;
        return std::accumulate(children.begin(), children.end(), 0);
    }
    double transport(AtLeastBinaryNode& node,
                     std::vector<double> children,
                     double child0,
                     double child1) {
        _depthMultiplier /= 10;
        return child0 + child1 + std::accumulate(children.begin(), children.end(), 0);
    }
};

TEST(PolyValueTest, TransporterUsingPrepare) {
    NodeTransporterTrackingDepth nodeTransporter;

    Tree demoTree = Tree::make<AtLeastBinaryNode>(
        std::vector<Tree>{Tree::make<Leaf>(4.0)},
        Tree::make<Leaf>(3.0),
        Tree::make<BinaryNode>(Tree::make<Leaf>(2.0),
                               Tree::make<NaryNode>(std::vector<Tree>{Tree::make<Leaf>(1.0)})));
    const double result = transport<false>(demoTree, nodeTransporter);
    /*
    demoTree
    1x level:     root
                 / | \
    10x level:  4  3  binary
                       / \
    100x level:       2   nary
                            \
    1000x level:             1
    */
    ASSERT_EQ(result, 1270.0);
}

class NodeWalkerIsLeaf {
public:
    bool walk(Leaf& leaf) {
        return true;
    }

    bool walk(BinaryNode& node, Tree& leftChild, Tree& rightChild) {
        return false;
    }

    bool walk(AtLeastBinaryNode& node,
              std::vector<Tree>& extraChildren,
              Tree& leftChild,
              Tree& rightChild) {
        return false;
    }

    bool walk(NaryNode& node, std::vector<Tree>& children) {
        return false;
    }
};

TEST(PolyValueTest, WalkerBasic) {
    NodeWalkerIsLeaf walker;
    auto tree = Tree::make<BinaryNode>(Tree::make<Leaf>(1.0), Tree::make<Leaf>(2.0));
    ASSERT(!walk<false>(tree, walker));
    ASSERT(walk<false>(tree.cast<BinaryNode>()->get<0>(), walker));
    ASSERT(walk<false>(tree.cast<BinaryNode>()->get<1>(), walker));
}

}  // namespace
}  // namespace mongo::optimizer::algebra