File: dphyp-t.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (816 lines) | stat: -rw-r--r-- 27,439 bytes parent folder | download
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   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
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <bitset>
#include <unordered_set>
#include <vector>

#include <gmock/gmock.h>
#include "my_compiler.h"
#include "sql/join_optimizer/subgraph_enumeration.h"
#include "unittest/gunit/benchmark.h"

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Expectation;
using ::testing::Return;
using ::testing::StrictMock;

using hypergraph::Hypergraph;
using hypergraph::NodeMap;
using hypergraph::PrintSet;

class MockReceiver {
 public:
  MOCK_METHOD1(HasSeen, bool(NodeMap));
  MOCK_METHOD1(FoundSingleNode, bool(int));
  MOCK_METHOD3(FoundSubgraphPair, bool(NodeMap, NodeMap, int));
};

TEST(DPhypTest, ExampleHypergraph) {
  MEM_ROOT mem_root;
  /*
    The example graph from the DPhyp paper. One large
    hyperedge and four simple edges.

      R1-.   ,-R4
      |   \ /   |
      R2---x---R5
      |   / \   |
      R3-'   `-R6
   */
  Hypergraph g(&mem_root);
  g.AddNode();                    // R1
  g.AddNode();                    // R2
  g.AddNode();                    // R3
  g.AddNode();                    // R4
  g.AddNode();                    // R5
  g.AddNode();                    // R6
  g.AddEdge(0b000001, 0b000010);  // R1-R2
  g.AddEdge(0b000010, 0b000100);  // R2-R3
  g.AddEdge(0b001000, 0b010000);  // R4-R5
  g.AddEdge(0b010000, 0b100000);  // R5-R6
  g.AddEdge(0b000111, 0b111000);  // {R1,R2,R3}-{R4,R5,R6}

  StrictMock<MockReceiver> mr;
  EXPECT_CALL(mr, FoundSingleNode(0));
  EXPECT_CALL(mr, FoundSingleNode(1));
  EXPECT_CALL(mr, FoundSingleNode(2));
  EXPECT_CALL(mr, FoundSingleNode(3));
  EXPECT_CALL(mr, FoundSingleNode(4));
  EXPECT_CALL(mr, FoundSingleNode(5));

  // Fallback matcher.
  EXPECT_CALL(mr, HasSeen(_)).WillRepeatedly(Return(false));

  // Right side of the graph:

  // Found link between R5 and R6.
  Expectation seen_r5_r6 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b010000, 0b100000, 3));
  EXPECT_CALL(mr, HasSeen(0b110000))
      .After(seen_r5_r6)
      .WillRepeatedly(Return(true));

  // Found link between R4 and R5.
  Expectation seen_r4_r5 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b001000, 0b010000, 2));
  EXPECT_CALL(mr, HasSeen(0b011000))
      .After(seen_r4_r5)
      .WillRepeatedly(Return(true));

  // Found link between R4 and {R5,R6}, through the R4-R5 edge.
  Expectation seen_r4_r5r6 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b001000, 0b110000, 2));

  // Found like between {R4,R5} and {R6}, through the R5-R6 edge.
  Expectation seen_r4r5_r6 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b011000, 0b100000, 3));

  // {R4,R5,R6} is connected (called only after we've seen its components).
  EXPECT_CALL(mr, HasSeen(0b111000))
      .After(seen_r4_r5r6)
      .After(seen_r4r5_r6)
      .WillRepeatedly(Return(true));

  // Very similar, left side of the graph:

  // Found link between R2 and R3.
  Expectation seen_r2_r3 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b000010, 0b000100, 1));
  EXPECT_CALL(mr, HasSeen(0b000110))
      .After(seen_r2_r3)
      .WillRepeatedly(Return(true));

  // Found link between R1 and R2.
  Expectation seen_r1_r2 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b000001, 0b000010, 0));
  EXPECT_CALL(mr, HasSeen(0b000011))
      .After(seen_r1_r2)
      .WillRepeatedly(Return(true));

  // Found link between R1 and {R2,R3}, through the R1-R2 edge.
  Expectation seen_r1_r2r3 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b000001, 0b000110, 0));

  // Found like between {R1,R2} and {R3}, through the R2-R3 edge.
  Expectation seen_r1r2_r3 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b000011, 0b000100, 1));

  // {R1,R2,R3} is connected (called only after we've seen its components).
  EXPECT_CALL(mr, HasSeen(0b000111))
      .After(seen_r1_r2r3)
      .After(seen_r1r2_r3)
      .WillRepeatedly(Return(true));

  // Found link between {R1,R2,R3} and {R4,R5,R6}.
  EXPECT_CALL(mr, FoundSubgraphPair(0b000111, 0b111000, 4))
      .After(seen_r1_r2r3)
      .After(seen_r1r2_r3)
      .After(seen_r4_r5r6)
      .After(seen_r4r5_r6);

  EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &mr));
}

TEST(DPhypTest, Loop) {
  MEM_ROOT mem_root;
  /*
    Shows that we can go around a loop and connect R1 to {R2,R3,R4,R5}
    graph through {R2,R5}, even though R5 was not part of R1's
    neighborhood (ie., R2 was chosen as the representative node).
    This requires that we remember that R5 was a part of R1's full
    neighborhood.

            R2----R3
            /     |
           /      |
       R1--       |
           \      |
            \     |
            R5----R4
   */
  Hypergraph g(&mem_root);
  g.AddNode();                  // R1
  g.AddNode();                  // R2
  g.AddNode();                  // R3
  g.AddNode();                  // R4
  g.AddNode();                  // R5
  g.AddEdge(0b00001, 0b10010);  // R1-{R2,R5}
  g.AddEdge(0b00010, 0b00100);  // R2-R3
  g.AddEdge(0b00100, 0b01000);  // R3-R4
  g.AddEdge(0b01000, 0b10000);  // R4-R5

  StrictMock<MockReceiver> mr;
  EXPECT_CALL(mr, FoundSingleNode(0));
  EXPECT_CALL(mr, FoundSingleNode(1));
  EXPECT_CALL(mr, FoundSingleNode(2));
  EXPECT_CALL(mr, FoundSingleNode(3));
  EXPECT_CALL(mr, FoundSingleNode(4));

  // Fallback matcher.
  EXPECT_CALL(mr, HasSeen(_)).WillRepeatedly(Return(false));

  // Found link between R4 and R5.
  Expectation seen_r4_r5 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b01000, 0b10000, 3));
  EXPECT_CALL(mr, HasSeen(0b11000))
      .After(seen_r4_r5)
      .WillRepeatedly(Return(true));

  // Found link between R3 and R4.
  Expectation seen_r3_r4 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b00100, 0b01000, 2));
  EXPECT_CALL(mr, HasSeen(0b01100))
      .After(seen_r3_r4)
      .WillRepeatedly(Return(true));

  // Found link between R3 and {R4,R5}, through the R3-R4 edge.
  Expectation seen_r3_r4r5 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b00100, 0b11000, 2)).After(seen_r4_r5);

  // Found link between {R3,R4} and R5, through the R4-R5 edge.
  Expectation seen_r3r4_r5 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b01100, 0b10000, 3)).After(seen_r3_r4);

  // {R3,R4,R5} is connected (called only after we've seen its components).
  EXPECT_CALL(mr, HasSeen(0b11100))
      .After(seen_r3_r4r5)
      .After(seen_r3r4_r5)
      .WillRepeatedly(Return(true));

  // Found link between R2 and R3.
  Expectation seen_r2_r3 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b00010, 0b00100, 1));
  EXPECT_CALL(mr, HasSeen(0b00110)).WillRepeatedly(Return(true));

  // Found link between R2 and {R3,R4}, through the R2-R3 edge.
  Expectation seen_r2_r3r4 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b00010, 0b01100, 1)).After(seen_r3_r4);

  // Found link between {R2,R3} and R4, through the R3-R4 edge.
  Expectation seen_r2r3_r4 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b00110, 0b01000, 2)).After(seen_r2_r3);

  // {R2,R3,R4} is connected (called only after we've seen its components).
  EXPECT_CALL(mr, HasSeen(0b01110))
      .After(seen_r2_r3r4)
      .After(seen_r2r3_r4)
      .WillRepeatedly(Return(true));

  // Found link between R2 and {R3,R4,R5}, through the R2-R3 edge.
  Expectation seen_r2_r3r4r5 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b00010, 0b11100, 1))
          .After(seen_r3_r4r5)
          .After(seen_r3r4_r5);

  // Found link between {R2,R3} and {R4,R5}, through the R3-R4 edge.
  Expectation seen_r2r3_r4r5 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b00110, 0b11000, 2))
          .After(seen_r2_r3)
          .After(seen_r4_r5);

  // Found link between {R2,R3,R4} and R5, through the R4-R5 edge.
  Expectation seen_r2r3r4_r5 =
      EXPECT_CALL(mr, FoundSubgraphPair(0b01110, 0b10000, 3))
          .After(seen_r2r3_r4)
          .After(seen_r2_r3r4);

  // {R2,R3,R4,R5} is connected (called only after we've seen its components).
  EXPECT_CALL(mr, HasSeen(0b11110))
      .After(seen_r2_r3r4r5)
      .After(seen_r2r3_r4r5)
      .After(seen_r2r3r4_r5)
      .WillRepeatedly(Return(true));

  // Finally, found link between R1 and {R2,R3,R4,R5}, through the R1-{R2,R5}
  // edge.
  EXPECT_CALL(mr, FoundSubgraphPair(0b00001, 0b11110, 0))
      .After(seen_r2_r3r4r5)
      .After(seen_r2r3_r4r5)
      .After(seen_r2r3r4_r5);

  EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &mr));
}

TEST(DPhypTest, AbortWithError) {
  MEM_ROOT mem_root;
  /*
    A simple chain.

      R1--R2--R3
   */
  Hypergraph g(&mem_root);
  g.AddNode();                    // R1
  g.AddNode();                    // R2
  g.AddNode();                    // R3
  g.AddEdge(0b000001, 0b000010);  // R1-R2
  g.AddEdge(0b000010, 0b000100);  // R2-R3

  StrictMock<MockReceiver> mr;
  EXPECT_CALL(mr, FoundSingleNode(1));
  EXPECT_CALL(mr, FoundSingleNode(2));

  // Fallback matcher.
  EXPECT_CALL(mr, HasSeen(_)).WillRepeatedly(Return(false));

  // Found link between R2 and R3. We return true (error) here,
  // so the algorithm should abort without ever seeing R1
  // or any of the links to it.
  EXPECT_CALL(mr, FoundSubgraphPair(0b000010, 0b000100, 1))
      .WillOnce(Return(true));

  EXPECT_TRUE(EnumerateAllConnectedPartitions(g, &mr));
}

// A Receiver used for unit tests. It records all subgraph pairs we see,
// allowing us to check afterwards that the correct ones were discovered
// (and no others). It also verifies correct ordering of HasSeen() calls.
struct AccumulatingReceiver {
  struct Subplan {
    NodeMap left, right;
    int edge_idx;
  };

  bool HasSeen(NodeMap subgraph) {
    if (seen_subplans.find(subgraph) == seen_subplans.end()) {
      has_returned_nonconnected.insert(subgraph);
      return false;
    } else {
      assert(has_returned_nonconnected.count(subgraph) == 0);
      return true;
    }
  }

  bool FoundSingleNode(int node_idx) {
    NodeMap map = TableBitmap(node_idx);

    // We must always see all enumerations for a subset before we can
    // use that subset.
    assert(used_in_larger_subset.count(map) == 0);

    // Should be called only once.
    assert(seen_subplans.count(map) == 0);

    seen_subplans.emplace(map, Subplan{0, 0, -1});
    return false;
  }

  bool FoundSubgraphPair(NodeMap left, NodeMap right, int edge_idx) {
    printf("Found connection between %s and %s along edge %d\n",
           PrintSet(left).c_str(), PrintSet(right).c_str(), edge_idx);

    // We must always see all enumerations for a subset before we can
    // use that subset.
    assert(used_in_larger_subset.count(left | right) == 0);
    used_in_larger_subset.insert(left);
    used_in_larger_subset.insert(right);

    // Additional test that in practice tests the same thing.
    assert(has_returned_nonconnected.count(left | right) == 0);

    // We should only get a given subgraph pair once.
    EXPECT_FALSE(SeenSubgraphPair(left, right, edge_idx))
        << "Duplicate connection between " << PrintSet(left) << " and "
        << PrintSet(right) << " along edge " << edge_idx;

    seen_subplans.emplace(left | right, Subplan{left, right, edge_idx});
    return false;
  }

  // Checks whether FoundSubgraphPair() was called with the given arguments.
  // Fairly slow for large graphs.
  bool SeenSubgraphPair(NodeMap left, NodeMap right, int edge_idx) {
    const auto subset_subplans = seen_subplans.equal_range(left | right);
    for (auto it = subset_subplans.first; it != subset_subplans.second; ++it) {
      if (it->second.left == left && it->second.right == right &&
          it->second.edge_idx == edge_idx) {
        return true;
      }
    }
    return false;
  }

  std::unordered_set<NodeMap> has_returned_nonconnected;
  std::unordered_set<NodeMap> used_in_larger_subset;
  std::multimap<NodeMap, Subplan> seen_subplans;
};

// A very simple receiver used during benchmarking only, used to isolate
// away receiver performance from the algorithm itself. Probably the fastest
// imaginable implementation; does nothing useful except remember which
// subgraphs are connected, as required for HasSeen().
template <int Size>
struct BenchmarkReceiver {
  bool HasSeen(NodeMap subgraph) { return seen_subplans[subgraph]; }

  bool FoundSingleNode(int node_idx) {
    NodeMap map = TableBitmap(node_idx);
    seen_subplans.set(map);
    return false;
  }

  bool FoundSubgraphPair(NodeMap left, NodeMap right, int) {
    seen_subplans.set(left | right);
    return false;
  }

  static constexpr int num_elements = 1 << Size;
  std::bitset<num_elements> seen_subplans;
};

// Creates a simple chain A-B-C-D-..., and verifies that we get all possible
// permutations.
TEST(DPhypTest, Chain) {
  constexpr int num_elements = 20;

  MEM_ROOT mem_root;
  Hypergraph g(&mem_root);
  for (int i = 0; i < num_elements; ++i) {
    g.AddNode();
    if (i != 0) {
      g.AddEdge(TableBitmap(i - 1), TableBitmap(i));
    }
  }

  AccumulatingReceiver receiver;
  EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &receiver));

  // Look at all possible subchains of the chain.
  int expected_subplans = 0;
  for (int start_idx = 0; start_idx < num_elements; ++start_idx) {
    for (int end_idx = start_idx + 1; end_idx <= num_elements; ++end_idx) {
      NodeMap subset = TablesBetween(start_idx, end_idx);

      if (end_idx == start_idx + 1) {
        // Single node, so should have a single single-node subplan.
        ASSERT_EQ(1, receiver.seen_subplans.count(subset));
        EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.left);
        EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.right);
        EXPECT_EQ(-1, receiver.seen_subplans.find(subset)->second.edge_idx);
        ++expected_subplans;
        continue;
      }

      // This subchain should be splittable along all possible midpoints.
      for (int split_after_idx = start_idx; split_after_idx < end_idx - 1;
           ++split_after_idx) {
        NodeMap left = TablesBetween(start_idx, split_after_idx + 1);
        NodeMap right = TablesBetween(split_after_idx + 1, end_idx);
        int edge_idx = split_after_idx;

        EXPECT_TRUE(receiver.SeenSubgraphPair(left, right, edge_idx))
            << "Subset " << PrintSet(subset) << " should be splittable into "
            << PrintSet(left) << " and " << PrintSet(right) << " along edge "
            << edge_idx;
        ++expected_subplans;
      }

      EXPECT_TRUE(receiver.seen_subplans.count(subset));
    }
  }

  // We should have no other subplans than the ones we checked for earlier.
  EXPECT_EQ(expected_subplans, receiver.seen_subplans.size());
}

// Demonstrates that we need to grow neighborhoods carefully when looking for
// complement seeds. Specifically, when starting with {R1} (which has
// neighborhood {R2,R3,R4}) and growing it with R2, we'd normally only consider
// the neighborhood of R2, since R3 and R4 are now in the forbidden set.
// However, when looking for seeds for the complement of {R1,R2}, we need to
// take R3 and R4 back into account, since they are not forbidden in this
// context.
//
// This test doesn't test precise call ordering, only that we get all the
// expected sets.
TEST(DPhypTest, SmallStar) {
  MEM_ROOT mem_root;
  /*
     R2
     |
     |
     R1---R3
     |
     |
     R4
   */
  Hypergraph g(&mem_root);
  g.AddNode();                  // R1
  g.AddNode();                  // R2
  g.AddNode();                  // R3
  g.AddNode();                  // R4
  g.AddEdge(0b00001, 0b00010);  // R1-R2
  g.AddEdge(0b00001, 0b00100);  // R1-R3
  g.AddEdge(0b00001, 0b01000);  // R1-R4

  StrictMock<MockReceiver> mr;
  EXPECT_CALL(mr, FoundSingleNode(0));
  EXPECT_CALL(mr, FoundSingleNode(1));
  EXPECT_CALL(mr, FoundSingleNode(2));
  EXPECT_CALL(mr, FoundSingleNode(3));

  for (int i = 1; i < 16; ++i) {
    if (IsSingleBitSet(i)) {
      EXPECT_CALL(mr, HasSeen(i))
          .Times(AnyNumber())
          .WillRepeatedly(Return(true));
    } else {
      // Anything containing R1 is connected, anything else is not.
      EXPECT_CALL(mr, HasSeen(i))
          .Times(AnyNumber())
          .WillRepeatedly(Return(i & 1));
    }
  }

  EXPECT_CALL(mr, FoundSubgraphPair(0b0001, 0b0010, 0));  // R1-R2.
  EXPECT_CALL(mr, FoundSubgraphPair(0b0001, 0b0100, 1));  // R1-R3.
  EXPECT_CALL(mr, FoundSubgraphPair(0b0001, 0b1000, 2));  // R1-R4.

  EXPECT_CALL(mr,
              FoundSubgraphPair(0b0011, 0b0100, 1));  // {R1,R2}-R3 along R1-R3.
  EXPECT_CALL(mr,
              FoundSubgraphPair(0b0011, 0b1000, 2));  // {R1,R2}-R4 along R1-R4.

  EXPECT_CALL(mr,
              FoundSubgraphPair(0b0101, 0b0010, 0));  // {R1,R3}-R2 along R1-R2.
  EXPECT_CALL(mr,
              FoundSubgraphPair(0b0101, 0b1000, 2));  // {R1,R3}-R4 along R1-R4.

  EXPECT_CALL(mr,
              FoundSubgraphPair(0b1001, 0b0010, 0));  // {R1,R4}-R2 along R1-R2.
  EXPECT_CALL(mr,
              FoundSubgraphPair(0b1001, 0b0100, 1));  // {R1,R4}-R3 along R1-R3.

  EXPECT_CALL(
      mr, FoundSubgraphPair(0b0111, 0b1000, 2));  // {R1,R2,R3}-R4 along R1-R4.
  EXPECT_CALL(
      mr, FoundSubgraphPair(0b1011, 0b0100, 1));  // {R1,R2,R4}-R3 along R1-R3.
  EXPECT_CALL(
      mr, FoundSubgraphPair(0b1101, 0b0010, 0));  // {R1,R2,R4}-R3 along R1-R2.

  EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &mr));
}

// Creates a clique (everything connected to everything, with simple edges)
// and checks that we get every possible permutation, along every relevant edge.
TEST(DPhypTest, Clique) {
  constexpr int num_elements = 6;

  int edge_indexes[num_elements][num_elements];

  MEM_ROOT mem_root;
  Hypergraph g(&mem_root);
  for (int i = 0; i < num_elements; ++i) {
    g.AddNode();
    for (int j = 0; j < i; ++j) {
      g.AddEdge(TableBitmap(i), TableBitmap(j));
      edge_indexes[i][j] = edge_indexes[j][i] = (g.edges.size() - 1) / 2;
    }
  }

  AccumulatingReceiver receiver;
  EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &receiver));

  int expected_subplans = 0;

  // Look at all possible non-zero subsets of the clique.
  for (NodeMap subset = 1; subset < (NodeMap{1} << num_elements); ++subset) {
    if (IsSingleBitSet(subset)) {
      // Single node, so should have a single single-node subplan.
      ASSERT_EQ(1, receiver.seen_subplans.count(subset));
      EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.left);
      EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.right);
      EXPECT_EQ(-1, receiver.seen_subplans.find(subset)->second.edge_idx);
      ++expected_subplans;
      continue;
    }

    // Find all possible two-way partitions of this subset.
    for (NodeMap left : NonzeroSubsetsOf(subset)) {
      if (left == subset) continue;
      NodeMap right = subset & ~left;
      if (IsolateLowestBit(left) > IsolateLowestBit(right)) continue;

      for (size_t left_idx : BitsSetIn(left)) {
        for (size_t right_idx : BitsSetIn(right)) {
          int edge_idx = edge_indexes[left_idx][right_idx];
          EXPECT_TRUE(receiver.SeenSubgraphPair(left, right, edge_idx))
              << "Subset " << PrintSet(subset) << " should be splittable into "
              << PrintSet(left) << " and " << PrintSet(right) << " along edge "
              << edge_idx;
          ++expected_subplans;
        }
      }
    }
  }

  // We should have no other subplans than the ones we checked for earlier.
  EXPECT_EQ(expected_subplans, receiver.seen_subplans.size());
}

// Constructs a hypergraph of A LEFT JOIN (B LEFT JOIN (C LEFT JOIN ...)),
// for null-tolerant joins; ie., no reordering is possible and only one
// possible plan should exist.
TEST(DPhypTest, OuterJoinChain) {
  constexpr int num_nodes = 5;

  MEM_ROOT mem_root;
  Hypergraph g(&mem_root);
  g.AddNode();                  // R1
  g.AddNode();                  // R2
  g.AddNode();                  // R3
  g.AddNode();                  // R4
  g.AddNode();                  // R5
  g.AddEdge(0b11110, 0b00001);  // R1-{R2,R3,R4,R5}
  g.AddEdge(0b11100, 0b00010);  // R2-{R3,R4,R5}
  g.AddEdge(0b11000, 0b00100);  // R3-{R4,R5}
  g.AddEdge(0b10000, 0b01000);  // R4-R5

  AccumulatingReceiver receiver;
  EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &receiver));

  int expected_subplans = 0;

  for (size_t node_idx = 0; node_idx < num_nodes; ++node_idx) {
    NodeMap subset = TableBitmap(node_idx);
    ASSERT_EQ(1, receiver.seen_subplans.count(subset));
    EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.left);
    EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.right);
    EXPECT_EQ(-1, receiver.seen_subplans.find(subset)->second.edge_idx);
    ++expected_subplans;
  }

  for (size_t edge_idx = 0; edge_idx < num_nodes - 1; ++edge_idx) {
    NodeMap subset = g.edges[edge_idx * 2].left | g.edges[edge_idx * 2].right;
    ASSERT_EQ(1, receiver.seen_subplans.count(subset));

    // NOTE: The edges come out flipped compared to the order we added them,
    // due to the ordering properties.
    EXPECT_EQ(g.edges[edge_idx * 2].right,
              receiver.seen_subplans.find(subset)->second.left);
    EXPECT_EQ(g.edges[edge_idx * 2].left,
              receiver.seen_subplans.find(subset)->second.right);

    EXPECT_EQ(edge_idx, receiver.seen_subplans.find(subset)->second.edge_idx);
    ++expected_subplans;
  }

  // We should have no other subplans than the ones we checked for earlier.
  EXPECT_EQ(expected_subplans, receiver.seen_subplans.size());
}

static void BM_Chain20(size_t num_iterations) {
  StopBenchmarkTiming();
  constexpr int num_nodes = 20;

  MEM_ROOT mem_root;
  Hypergraph g(&mem_root);
  for (int i = 0; i < num_nodes; ++i) {
    g.AddNode();
    if (i != 0) {
      g.AddEdge(TableBitmap(i - 1), TableBitmap(i));
    }
  }

  for (size_t i = 0; i < num_iterations; ++i) {
    BenchmarkReceiver<num_nodes> receiver;

    StartBenchmarkTiming();
    EnumerateAllConnectedPartitions(g, &receiver);
    StopBenchmarkTiming();
  }
}
BENCHMARK(BM_Chain20)

// Like the OuterJoinChain test, just as a benchmark.
//
// Note that even though we only emit one possible plan, this test is not
// that much faster then BM_Chain20. The reason is that even though the
// number of subsets go down from O(n³) to O(n²), each node also is touched
// by more hyperedges (on the order of O(n)), so neighborhood finding has to
// sift through more edges. It would be nice if we had some way of culling
// these “obviously wrong” edges without a linear search (e.g., it is
// meaningless for R5 to traverse a hyperedge to R1 in the neighborhood
// calculation when expanding subgraphs, since it goes “backwards”), but in the
// presence of cycles, there does not seem to be an obvious way of codifying
// this.
static void BM_NestedOuterJoin20(size_t num_iterations) {
  StopBenchmarkTiming();
  constexpr int num_nodes = 20;

  MEM_ROOT mem_root;
  Hypergraph g(&mem_root);
  for (int i = 0; i < num_nodes; ++i) {
    g.AddNode();
  }
  for (int i = 0; i < num_nodes - 1; ++i) {
    g.AddEdge(TableBitmap(i), TablesBetween(i + 1, num_nodes));
  }

  for (size_t i = 0; i < num_iterations; ++i) {
    BenchmarkReceiver<num_nodes> receiver;

    StartBenchmarkTiming();
    EnumerateAllConnectedPartitions(g, &receiver);
    StopBenchmarkTiming();
  }
}
BENCHMARK(BM_NestedOuterJoin20)

// Benchmark from the DPhyp paper. We only implement the version
// with hyperedges split into cardinality-2 hypernodes.
static void BM_HyperCycle16(size_t num_iterations) {
  StopBenchmarkTiming();
  constexpr int num_nodes = 16;  // A multiple of four.

  MEM_ROOT mem_root;
  Hypergraph g(&mem_root);
  for (int i = 0; i < num_nodes; ++i) {
    g.AddNode();
  }

  // Add the simple edges that create the cycle.
  for (int i = 0; i < num_nodes; ++i) {
    g.AddEdge(TableBitmap(i), TableBitmap((i + 1) % num_nodes));
  }

  // Add some hyperedges.
  for (int i = 0; i < num_nodes; i += 4) {
    g.AddEdge(TablesBetween(i, i + 2), TablesBetween(i + 2, i + 4));
  }

  for (size_t i = 0; i < num_iterations; ++i) {
    BenchmarkReceiver<num_nodes> receiver;

    StartBenchmarkTiming();
    EnumerateAllConnectedPartitions(g, &receiver);
    StopBenchmarkTiming();
  }
}
BENCHMARK(BM_HyperCycle16)

static void BM_Star17(size_t num_iterations) {
  StopBenchmarkTiming();
  constexpr int num_nodes = 17;

  MEM_ROOT mem_root;
  Hypergraph g(&mem_root);
  g.AddNode();  // The central node.
  for (int i = 1; i < num_nodes; ++i) {
    g.AddNode();
    g.AddEdge(TableBitmap(0), TableBitmap(i));
  }

  for (size_t i = 0; i < num_iterations; ++i) {
    BenchmarkReceiver<num_nodes> receiver;

    StartBenchmarkTiming();
    EnumerateAllConnectedPartitions(g, &receiver);
    StopBenchmarkTiming();
  }
}
BENCHMARK(BM_Star17)

// Benchmark from the DPhyp paper. This is the version with hyperedges split
// into cardinality-2 hypernodes.
static void BM_HyperStar17_ManyHyperedges(size_t num_iterations) {
  StopBenchmarkTiming();
  constexpr int num_nodes = 17;  // A multiple of four, plus one.

  MEM_ROOT mem_root;
  Hypergraph g(&mem_root);
  g.AddNode();  // The central node.
  for (int i = 1; i < num_nodes; ++i) {
    g.AddNode();
    g.AddEdge(TableBitmap(0), TableBitmap(i));
  }

  // Add some hyperedges.
  constexpr int half = (num_nodes - 1) / 2;
  for (int i = 0; i < half; i += 2) {
    g.AddEdge(TablesBetween(i + 1, i + 3),
              TablesBetween(i + half + 1, i + half + 3));
  }

  for (size_t i = 0; i < num_iterations; ++i) {
    BenchmarkReceiver<num_nodes> receiver;

    StartBenchmarkTiming();
    EnumerateAllConnectedPartitions(g, &receiver);
    StopBenchmarkTiming();
  }
}
BENCHMARK(BM_HyperStar17_ManyHyperedges)

// Benchmark from the DPhyp paper. This is the version with no hyperedge
// split (only one large hyperedge).
static void BM_HyperStar17_SingleLargeHyperedge(size_t num_iterations) {
  StopBenchmarkTiming();
  constexpr int num_nodes = 17;  // A multiple of two, plus one.

  MEM_ROOT mem_root;
  Hypergraph g(&mem_root);
  g.AddNode();  // The central node.
  for (int i = 1; i < num_nodes; ++i) {
    g.AddNode();
    g.AddEdge(TableBitmap(0), TableBitmap(i));
  }

  // Add a single large hyperedge.
  constexpr int half = (num_nodes - 1) / 2;
  g.AddEdge(TablesBetween(1, half + 1), TablesBetween(half + 1, num_nodes));

  for (size_t i = 0; i < num_iterations; ++i) {
    BenchmarkReceiver<num_nodes> receiver;

    StartBenchmarkTiming();
    EnumerateAllConnectedPartitions(g, &receiver);
    StopBenchmarkTiming();
  }
}
BENCHMARK(BM_HyperStar17_SingleLargeHyperedge)