File: graph_simplification-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 (851 lines) | stat: -rw-r--r-- 33,794 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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/* Copyright (c) 2021, 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 <gtest/gtest.h>
#include <stdio.h>
#include <memory>
#include <random>
#include <utility>
#include <vector>

#include "my_alloc.h"
#include "scope_guard.h"
#include "sql/handler.h"
#include "sql/join_optimizer/access_path.h"
#include "sql/join_optimizer/bit_utils.h"
#include "sql/join_optimizer/graph_simplification.h"
#include "sql/join_optimizer/hypergraph.h"
#include "sql/join_optimizer/make_join_hypergraph.h"
#include "sql/join_optimizer/node_map.h"
#include "sql/join_optimizer/online_cycle_finder.h"
#include "sql/join_optimizer/relational_expression.h"
#include "sql/join_optimizer/subgraph_enumeration.h"
#include "sql/join_optimizer/trivial_receiver.h"
#include "sql/mem_root_array.h"
#include "sql/table.h"
#include "unittest/gunit/benchmark.h"
#include "unittest/gunit/fake_table.h"
#include "unittest/gunit/test_utils.h"

class Item;
class THD;

using hypergraph::NodeMap;
using testing::UnorderedElementsAre;

TEST(OnlineCycleFinderTest, SelfEdges) {
  MEM_ROOT mem_root;
  OnlineCycleFinder cycles(&mem_root, 10);
  EXPECT_TRUE(cycles.AddEdge(5, 5));
  EXPECT_TRUE(cycles.EdgeWouldCreateCycle(5, 5));
}

TEST(OnlineCycleFinderTest, Simple) {
  MEM_ROOT mem_root;
  OnlineCycleFinder cycles(&mem_root, 10);
  EXPECT_FALSE(cycles.EdgeWouldCreateCycle(3, 5));
  EXPECT_FALSE(cycles.EdgeWouldCreateCycle(5, 3));
  EXPECT_FALSE(cycles.AddEdge(3, 5));
  EXPECT_TRUE(cycles.EdgeWouldCreateCycle(5, 3));
}

TEST(OnlineCycleFinderTest, InverseOrderIsFine) {
  MEM_ROOT mem_root;
  OnlineCycleFinder cycles(&mem_root, 10);
  EXPECT_FALSE(cycles.AddEdge(5, 3));
  EXPECT_TRUE(cycles.EdgeWouldCreateCycle(3, 5));
}

TEST(OnlineCycleFinderTest, Transitive) {
  MEM_ROOT mem_root;
  OnlineCycleFinder cycles(&mem_root, 10);
  EXPECT_FALSE(cycles.AddEdge(1, 3));
  EXPECT_FALSE(cycles.AddEdge(3, 5));
  EXPECT_FALSE(cycles.AddEdge(5, 6));
  EXPECT_FALSE(cycles.AddEdge(5, 9));
  EXPECT_FALSE(cycles.EdgeWouldCreateCycle(7, 1));
  EXPECT_TRUE(cycles.EdgeWouldCreateCycle(6, 1));
  EXPECT_TRUE(cycles.EdgeWouldCreateCycle(9, 1));
  EXPECT_FALSE(cycles.EdgeWouldCreateCycle(1, 7));
  EXPECT_FALSE(cycles.EdgeWouldCreateCycle(1, 5));
}

static void AddEdge(THD *thd, RelationalExpression::Type join_type,
                    NodeMap left, NodeMap right, double selectivity,
                    MEM_ROOT *mem_root, JoinHypergraph *graph) {
  JoinPredicate pred;
  pred.selectivity = selectivity;
  pred.expr = new (mem_root) RelationalExpression(thd);
  pred.expr->type = join_type;
  pred.expr->nodes_in_subtree = left | right;
  pred.estimated_bytes_per_row = 0;  // To keep the compiler happy.
  graph->edges.push_back(std::move(pred));
  graph->graph.AddEdge(left, right);
}

namespace {

// Helper for destroying all the Fake_TABLE objects in a JoinHypergraph.
class DestroyNodes {
 public:
  explicit DestroyNodes(const JoinHypergraph *graph) : m_graph(graph) {}
  void operator()() const {
    for (const JoinHypergraph::Node &node : m_graph->nodes) {
      destroy(static_cast<Fake_TABLE *>(node.table));
    }
  }

 private:
  const JoinHypergraph *m_graph;
};

// RAII class which destroys Fake_TABLE objects when it goes out of scope.
using NodeGuard = Scope_guard<DestroyNodes>;

}  // namespace

[[nodiscard]] static NodeGuard AddNodes(int num_nodes, MEM_ROOT *mem_root,
                                        JoinHypergraph *g) {
  for (int i = 0; i < num_nodes; ++i) {
    TABLE *table =
        new (mem_root) Fake_TABLE(/*num_columns=*/1, /*nullable=*/true);
    table->file->stats.records = 1000;

    char *alias = mem_root->ArrayAlloc<char>(20);
    snprintf(alias, 20, "t%d", i + 1);
    table->alias = alias;

    g->nodes.push_back(JoinHypergraph::Node{table, {}, {}});
    g->graph.AddNode();
  }

  return {DestroyNodes(g)};
}

TEST(GraphSimplificationTest, SimpleStar) {
  my_testing::Server_initializer initializer;
  initializer.SetUp();

  // A simple star-join with four tables, similar to what's in the paper.
  MEM_ROOT mem_root;
  JoinHypergraph g(&mem_root, /*query_block=*/nullptr);

  NodeGuard node_guard = AddNodes(4, &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b10, 0.999,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b100, 0.5,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b1000,
          0.01, &mem_root, &g);

  GraphSimplifier s(&g, &mem_root);

  // Based on the selectivities, joining t1/t4 before t1/t2 will be the best
  // choice. This means we'll broaden the t1/t2 edge to {t1,t4}/t2.
  // (We could have put t4 on any side.)
  ASSERT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  EXPECT_EQ(0b1001, g.graph.edges[0].left);
  EXPECT_EQ(0b10, g.graph.edges[0].right);
  EXPECT_EQ(g.graph.edges[0].left, g.graph.edges[1].right);
  EXPECT_EQ(g.graph.edges[0].right, g.graph.edges[1].left);

  // The t1/t2 edge is no longer simple; verify we updated the graph right.
  EXPECT_EQ(0b1100, g.graph.nodes[0].simple_neighborhood);
  EXPECT_EQ(0b0000, g.graph.nodes[1].simple_neighborhood);
  EXPECT_THAT(g.graph.nodes[0].simple_edges, UnorderedElementsAre(2, 4));
  EXPECT_THAT(g.graph.nodes[0].complex_edges, UnorderedElementsAre(0));
  EXPECT_THAT(g.graph.nodes[1].simple_edges, UnorderedElementsAre());
  EXPECT_THAT(g.graph.nodes[1].complex_edges, UnorderedElementsAre(1));

  // Next, we'll do t1/t4 before t1/t3 (again based on selectivities),
  // broadening t1/t3 to {t1,t4}/t3.
  ASSERT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  EXPECT_EQ(0b1001, g.graph.edges[2].left);
  EXPECT_EQ(0b100, g.graph.edges[2].right);
  EXPECT_EQ(g.graph.edges[2].left, g.graph.edges[3].right);
  EXPECT_EQ(g.graph.edges[2].right, g.graph.edges[3].left);

  // Finally, t1-t3 before t1-t2, but these edges were already hyperedges.
  // So {t1,t4}-{t2} will be extended to {t1,t3,t4}-{t2}.
  ASSERT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  EXPECT_EQ(0b1101, g.graph.edges[0].left);
  EXPECT_EQ(0b10, g.graph.edges[0].right);
  EXPECT_EQ(g.graph.edges[0].left, g.graph.edges[1].right);
  EXPECT_EQ(g.graph.edges[0].right, g.graph.edges[1].left);

  // No further simplification should be possible.
  EXPECT_EQ(GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE,
            s.DoSimplificationStep());
}

TEST(GraphSimplificationTest, TwoCycles) {
  my_testing::Server_initializer initializer;
  initializer.SetUp();

  // Based on a real test case.
  //
  //    .--t1\             .
  //   /    | \            .
  //   |   t2  t4
  //   \    | /
  //    `--t3/
  //
  MEM_ROOT mem_root;
  JoinHypergraph g(&mem_root, /*query_block=*/nullptr);

  NodeGuard node_guard = AddNodes(4, &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b10, 0.999,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b10, 0b100, 0.5,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b100, 0.01,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b1000, 0.2,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b100, 0b1000,
          0.8, &mem_root, &g);

  // Do simplification steps until we can't do more. (The number doesn't matter
  // all that much, but it should definitely be more than one.)
  GraphSimplifier s(&g, &mem_root);
  ASSERT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  ASSERT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  ASSERT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  ASSERT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  ASSERT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  ASSERT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  ASSERT_EQ(GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE,
            s.DoSimplificationStep());

  // Finally, run DPhyp to make sure the graph is still consistent
  // enough to find a solution.
  TrivialReceiver receiver(g, &mem_root, /*subgraph_pair_limit=*/-1);
  EXPECT_FALSE(EnumerateAllConnectedPartitions(g.graph, &receiver));
  EXPECT_EQ(4, receiver.seen_nodes);
  EXPECT_EQ(5, receiver.seen_subgraph_pairs);
  EXPECT_TRUE(receiver.HasSeen(0b1111));
}

TEST(GraphSimplificationTest, ExistingHyperedge) {
  my_testing::Server_initializer initializer;
  initializer.SetUp();

  // Based on a real test case.
  //
  //   t1 --- t2 --- t3
  //     \   /
  //      \ /
  //       |
  //       |
  //      t4
  //
  MEM_ROOT mem_root;
  JoinHypergraph g(&mem_root, /*query_block=*/nullptr);

  NodeGuard node_guard = AddNodes(4, &mem_root, &g);
  g.nodes[0].table->file->stats.records = 690;
  g.nodes[1].table->file->stats.records = 6;
  g.nodes[2].table->file->stats.records = 1;
  g.nodes[3].table->file->stats.records = 1;

  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b10, 0.2,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b100, 0b10, 1.0,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b11, 0b1000,
          0.1, &mem_root, &g);

  GraphSimplifier s(&g, &mem_root);

  // First, one of t1-t2 and t2-t3 should come before the other.
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());

  // However, now all that can be done is to put t1-t2 before {t1,t2}-t4,
  // and that is already always the case, so no further simplifications
  // can be done.
  EXPECT_EQ(GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE,
            s.DoSimplificationStep());

  // Finally, run DPhyp to make sure the graph is still consistent
  // enough to find a solution, and that we are fully simplified.
  TrivialReceiver receiver(g, &mem_root, /*subgraph_pair_limit=*/-1);
  EXPECT_FALSE(EnumerateAllConnectedPartitions(g.graph, &receiver));
  EXPECT_EQ(4, receiver.seen_nodes);
  EXPECT_EQ(3, receiver.seen_subgraph_pairs);
  EXPECT_TRUE(receiver.HasSeen(0b1111));
}

TEST(GraphSimplificationTest, IndirectHierarcicalJoins) {
  my_testing::Server_initializer initializer;
  initializer.SetUp();

  // Based on yet another real test case.
  //
  //   t3 ---- t2 -.
  //     \     /    \             .
  //      \   /      \            .
  //       \ /        --- t1
  //        |        /
  //        |       /
  //        t4 ____/
  //
  // The only possible join order here is first the simple t2-t3 edge,
  // then join in t4, and then t1. But since t1 has zero rows, it seems
  // attractive to take the t1-{t2,t4} join first, and we need to disallow that.
  MEM_ROOT mem_root;
  JoinHypergraph g(&mem_root, /*query_block=*/nullptr);

  NodeGuard node_guard = AddNodes(4, &mem_root, &g);
  g.nodes[0].table->file->stats.records = 0;
  g.nodes[1].table->file->stats.records = 171;
  g.nodes[2].table->file->stats.records = 6;
  g.nodes[3].table->file->stats.records = 3824;

  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b10, 0b100, 0.2,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b110, 0b1000,
          1.0, &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b1010, 0.1,
          &mem_root, &g);

  GraphSimplifier s(&g, &mem_root);

  // No simplification steps should be possible, except for that we should
  // discover that t1-{t2,t4} must come late (see above).
  EXPECT_EQ(GraphSimplifier::APPLIED_NOOP, s.DoSimplificationStep());
  EXPECT_EQ(GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE,
            s.DoSimplificationStep());

  // Finally, run DPhyp to make sure the graph is still consistent
  // enough to find a solution, and that we are fully simplified.
  TrivialReceiver receiver(g, &mem_root, /*subgraph_pair_limit=*/-1);
  EXPECT_FALSE(EnumerateAllConnectedPartitions(g.graph, &receiver));
  EXPECT_EQ(4, receiver.seen_nodes);
  EXPECT_EQ(3, receiver.seen_subgraph_pairs);
  EXPECT_TRUE(receiver.HasSeen(0b1111));
}

TEST(GraphSimplificationTest, IndirectHierarcicalJoins2) {
  my_testing::Server_initializer initializer;
  initializer.SetUp();

  // An even more complicated case.
  //
  //      t1----.
  //     / |    |
  //    /  |   / \        .
  //   t5  |  t4-t3
  //    \  |  /
  //     \ | /
  //      \|/
  //       |
  //       |
  //      t2
  //
  // We need to understand that the join {t1,t4,t5}-t2 depends on the join t3-t4
  // (i.e., we cannot say it should be done before that join). This isn't
  // obvious at all; we need to understand that t3-t4 must be done before
  // t1-{t3,t4} and propagate that information up to the t1 joins.
  // (This is a case where our join inference algorithm fails, but we are being
  // saved by the impossibility check.)
  MEM_ROOT mem_root;
  JoinHypergraph g(&mem_root, /*query_block=*/nullptr);

  NodeGuard node_guard = AddNodes(5, &mem_root, &g);
  g.nodes[0].table->file->stats.records = 1;
  g.nodes[1].table->file->stats.records = 1;
  g.nodes[2].table->file->stats.records = 1;
  g.nodes[3].table->file->stats.records = 1;
  g.nodes[4].table->file->stats.records = 1;

  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b10000,
          0.1, &mem_root, &g);  // t1-t5.
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b100, 0b1000,
          1.0, &mem_root, &g);  // t3-t4.
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b1100, 0.1,
          &mem_root, &g);  // t1-{t3,t4}.
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b11001, 0b10,
          0.01, &mem_root, &g);  // {t1,t4,t5}-t2.

  GraphSimplifier s(&g, &mem_root);

  // We want first to put {t1,t4,t5}-t2 before t3-t4, but discover it is
  // impossible, so we apply the opposite.
  EXPECT_EQ(GraphSimplifier::APPLIED_NOOP, s.DoSimplificationStep());

  // t1-{t3,t4} can be ordered relative to {t1}-{t5}, but after that,
  // no further simplifications should be possible.
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  EXPECT_EQ(GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE,
            s.DoSimplificationStep());

  // Finally, run DPhyp to make sure the graph is still consistent
  // enough to find a solution, and that we are fully simplified.
  TrivialReceiver receiver(g, &mem_root, /*subgraph_pair_limit=*/-1);
  EXPECT_FALSE(EnumerateAllConnectedPartitions(g.graph, &receiver));
  EXPECT_EQ(5, receiver.seen_nodes);
  EXPECT_EQ(4, receiver.seen_subgraph_pairs);
  EXPECT_TRUE(receiver.HasSeen(0b11111));
}

TEST(GraphSimplificationTest, ConflictRules) {
  my_testing::Server_initializer initializer;
  initializer.SetUp();

  // We set up a simple t1-t2-t3 chain join, but with a twist;
  // we'd like to do t2-t3 before t1-t2 (because t3 has zero rows),
  // but we add a conflict rule {t2} → t1 on the edge to prevent that.
  // Naturally, in a real query, that conflict rule would be absorbed
  // into a hyperedge, but we specifically want to test our handling
  // of unabsorbed conflict rules here (which can occur
  // in more complex graphs).
  MEM_ROOT mem_root;
  JoinHypergraph g(&mem_root, /*query_block=*/nullptr);

  NodeGuard node_guard = AddNodes(3, &mem_root, &g);
  g.nodes[0].table->file->stats.records = 100;
  g.nodes[1].table->file->stats.records = 10000;
  g.nodes[2].table->file->stats.records = 0;

  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b10, 1.0,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b10, 0b100, 1.0,
          &mem_root, &g);

  g.edges[1].expr->conflict_rules.init(&mem_root);
  g.edges[1].expr->conflict_rules.push_back(ConflictRule{0b10, 0b1});

  GraphSimplifier s(&g, &mem_root);

  // It would be fine here to have one simplification step,
  // in theory (t1-t2 before t2-t3), because it's not immediately
  // obvious that it's a no-op. But our implementation chooses to
  // force-insert that as an edge when we try the failed “t2-t3
  // before t1-t2” simplification, so we get the opposite first.
  EXPECT_EQ(GraphSimplifier::APPLIED_NOOP, s.DoSimplificationStep());
  EXPECT_EQ(GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE,
            s.DoSimplificationStep());

  // Finally, run DPhyp to make sure the graph is still consistent
  // enough to find a solution, and that we are fully simplified.
  TrivialReceiver receiver(g, &mem_root, /*subgraph_pair_limit=*/-1);
  EXPECT_FALSE(EnumerateAllConnectedPartitions(g.graph, &receiver));
  EXPECT_EQ(3, receiver.seen_nodes);
  EXPECT_EQ(2, receiver.seen_subgraph_pairs);
  EXPECT_TRUE(receiver.HasSeen(0b111));
}

TEST(GraphSimplificationTest, Antijoin) {
  my_testing::Server_initializer initializer;
  initializer.SetUp();

  // (t1_100 JOIN t2_100) ANTIJOIN t3_10000. Normally, it would be better to
  // delay the t2-t3 join to get a more even cost, but since the antijoin
  // produces effectively zero rows, it should be taken immediately.
  MEM_ROOT mem_root;
  JoinHypergraph g(&mem_root, /*query_block=*/nullptr);

  NodeGuard node_guard = AddNodes(3, &mem_root, &g);
  g.nodes[0].table->file->stats.records = 100;
  g.nodes[1].table->file->stats.records = 100;
  g.nodes[2].table->file->stats.records = 10000;

  AddEdge(initializer.thd(), RelationalExpression::INNER_JOIN, 0b1, 0b10, 1.0,
          &mem_root, &g);
  AddEdge(initializer.thd(), RelationalExpression::ANTIJOIN, 0b10, 0b100, 1.0,
          &mem_root, &g);

  GraphSimplifier s(&g, &mem_root);

  // t1-t2 should be broadened to t1-{t2,t3}, so that t2-t3 is taken first.
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  EXPECT_EQ(0b1, g.graph.edges[0].left);
  EXPECT_EQ(0b110, g.graph.edges[0].right);
  EXPECT_EQ(g.graph.edges[0].left, g.graph.edges[1].right);
  EXPECT_EQ(g.graph.edges[0].right, g.graph.edges[1].left);

  EXPECT_EQ(GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE,
            s.DoSimplificationStep());
}

TEST(GraphSimplificationTest, CycleNeighboringHyperedges) {
  my_testing::Server_initializer initializer;
  initializer.SetUp();

  MEM_ROOT mem_root;
  JoinHypergraph g(&mem_root, /*query_block=*/nullptr);

  /*
   * Based on a real test case:
   *
   *            t1___
   *            |    \        t6
   *            |     \      /
   *           /|\     +-- t5
   *          / | \   /      \
   *         /  |  \ /        t7
   *       t2--t3--t4
   *         \____/
   *
   * The problem with simplifying this graph is that the initial set of
   * constraints says that all three of t2-t3, t2-t4 and t3-t4 must come
   * before t1-{t2,t3,t4}. So if we later try to add a constraint that makes the
   * latter join come before one of those three joins, the online cycle finder
   * will tell us it's impossible because we get a cycle in the before-after
   * relationship. Which is true, but it doesn't take into account that the
   * final plan will never use more than two of those joins in order to join t2,
   * t3 and t4. So the graph can still be joinable using two of the edges even
   * if the third edge is involved in a cycle in the before-after graph.
   */

  NodeGuard node_guard = AddNodes(7, &mem_root, &g);
  g.nodes[0].table->file->stats.records = 1500;
  g.nodes[1].table->file->stats.records = 6000;
  g.nodes[2].table->file->stats.records = 700;
  g.nodes[3].table->file->stats.records = 200;
  g.nodes[4].table->file->stats.records = 150;
  g.nodes[5].table->file->stats.records = 1000;
  g.nodes[6].table->file->stats.records = 1000;

  THD *thd = initializer.thd();
  AddEdge(thd, RelationalExpression::LEFT_JOIN, 0b1, 0b1110, 0.0007, &mem_root,
          &g);
  AddEdge(thd, RelationalExpression::INNER_JOIN, 0b10, 0b100, 0.005, &mem_root,
          &g);
  AddEdge(thd, RelationalExpression::INNER_JOIN, 0b10, 0b1000, 0.005, &mem_root,
          &g);
  AddEdge(thd, RelationalExpression::INNER_JOIN, 0b100, 0b1000, 0.005,
          &mem_root, &g);
  AddEdge(thd, RelationalExpression::INNER_JOIN, 0b1001, 0b010000, 0.01,
          &mem_root, &g);
  AddEdge(thd, RelationalExpression::INNER_JOIN, 0b10000, 0b100000, 0.02,
          &mem_root, &g);
  AddEdge(thd, RelationalExpression::INNER_JOIN, 0b10000, 0b1000000, 0.021,
          &mem_root, &g);

  // Simplify the above graph as much as possible. The exact steps are not all
  // that important. What matters, is that we're able to get past the third call
  // to DoSimplificationStep(), where we previously hit infinite recursion, and
  // continue to simplify the graph also after we hit the problematic condition.
  GraphSimplifier s(&g, &mem_root);

  // First two simplifications are applied by adding the following constraints:
  //
  // t3-t4 before t2-t3
  // t1-t5 before t2-t4
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());

  // Here we hit the condition that we want to exercise with this test case. We
  // want to add the constraint that {t1,t4}-t5 is before t1-{t2,t3,t4}, but we
  // detect that the graph is not joinable if we do that. Usually, when we
  // detect this, we would add the opposite constraint and return APPLIED_NOOP.
  // However, the online cycle finder detects that adding the opposite
  // constraint will cause a cycle in the before-after graph, and refuses to add
  // it (this is because the online cycle finder doesn't take into account that
  // a cyclic hypergraph contains redundant edges, so we won't end up following
  // all the edges). Since we can't apply the opposite constraint, we instead
  // remove the problematic constraint from the set of potential simplifications
  // and retry with the second most promising simplification. This step
  // completes successfully and adds the constraint t2-t3 is before t2-t4.
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());

  // The next two simplifications we try are:
  //
  // {t1,t4}-t5 before t3-t4
  // {t1,t4}-t5 before t2-t3
  //
  // Both of these make the resulting graph not joinable, so we reject both and
  // instead add the opposite constraint. This time, the opposite constraints
  // are added successfully (as seen by returning APPLIED_NOOP).
  EXPECT_EQ(GraphSimplifier::APPLIED_NOOP, s.DoSimplificationStep());
  EXPECT_EQ(GraphSimplifier::APPLIED_NOOP, s.DoSimplificationStep());

  // Attempts to add the following constraints are successful:
  //
  // t5-t6 before t2-t4
  // t5-t7 before t2-t4
  // t5-t6 before {t1,t4}-t5
  // t5-t7 before {t1,t4}-t5
  // t5-t6 before t5-t7
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());
  EXPECT_EQ(GraphSimplifier::APPLIED_SIMPLIFICATION, s.DoSimplificationStep());

  // Nothing more to simplify.
  EXPECT_EQ(GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE,
            s.DoSimplificationStep());

  // Verify that the simplified graph is consistent.
  TrivialReceiver receiver(g, &mem_root, /*subgraph_pair_limit=*/-1);
  EXPECT_FALSE(EnumerateAllConnectedPartitions(g.graph, &receiver));
  EXPECT_EQ(7, receiver.seen_nodes);
  EXPECT_EQ(6, receiver.seen_subgraph_pairs);
  EXPECT_TRUE(receiver.HasSeen(0b1111111));
}

TEST(GraphSimplificationTest, CyclicInformationSchemaView) {
  my_testing::Server_initializer initializer;
  initializer.SetUp();

  MEM_ROOT mem_root;
  JoinHypergraph g(&mem_root, /*query_block=*/nullptr);

  /*
    This test case is based on the INFORMATION_SCHEMA.STATISTICS view. When
    invoking graph simplification on it, the resulting simplified graph could in
    some circumstances (depending on table sizes and statistics) be impossible
    to join.
   */

  NodeGuard node_guard = AddNodes(8, &mem_root, &g);
  g.nodes[0].table->file->stats.records = 1399;
  g.nodes[1].table->file->stats.records = 4706;
  g.nodes[2].table->file->stats.records = 316;
  g.nodes[3].table->file->stats.records = 222;
  g.nodes[4].table->file->stats.records = 259;
  g.nodes[5].table->file->stats.records = 6;
  g.nodes[6].table->file->stats.records = 1;
  g.nodes[7].table->file->stats.records = 1;

  THD *thd = initializer.thd();
  // t0-t1
  AddEdge(thd, RelationalExpression::INNER_JOIN, TableBitmap(0), TableBitmap(1),
          0.000212, &mem_root, &g);
  // t3-t4
  AddEdge(thd, RelationalExpression::INNER_JOIN, TableBitmap(3), TableBitmap(4),
          0.00386, &mem_root, &g);
  // t5-t6
  AddEdge(thd, RelationalExpression::INNER_JOIN, TableBitmap(5), TableBitmap(6),
          1.0, &mem_root, &g);
  // t3-t5
  AddEdge(thd, RelationalExpression::INNER_JOIN, TableBitmap(3), TableBitmap(5),
          0.167, &mem_root, &g);
  // t2-t3
  AddEdge(thd, RelationalExpression::INNER_JOIN, TableBitmap(2), TableBitmap(3),
          0.0045, &mem_root, &g);
  // t0-{t2,t3}
  AddEdge(thd, RelationalExpression::INNER_JOIN, TableBitmap(0),
          TableBitmap(2, 3), 0.00316, &mem_root, &g);
  // {t1,t2,t3,t5}-t7
  AddEdge(thd, RelationalExpression::LEFT_JOIN, TableBitmap(1, 2, 3, 5),
          TableBitmap(7), 0.001, &mem_root, &g);
  // t0-t2
  AddEdge(thd, RelationalExpression::INNER_JOIN, TableBitmap(0), TableBitmap(2),
          0.00316, &mem_root, &g);

  // Simplify the above graph as much as possible. The exact steps are not all
  // that important. What matters, is that we're not making a hypergraph that is
  // not joinable.
  GraphSimplifier s(&g, &mem_root);
  while (s.DoSimplificationStep() !=
         GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE) {
  }

  // Verify that the simplified graph is joinable.
  TrivialReceiver receiver(g, &mem_root, /*subgraph_pair_limit=*/-1);
  EXPECT_FALSE(EnumerateAllConnectedPartitions(g.graph, &receiver));
  EXPECT_EQ(8, receiver.seen_nodes);
  EXPECT_TRUE(receiver.HasSeen(0b11111111));

  // And it should have limited the search space as much as possible (N-1 joins
  // considered to join together N tables).
  EXPECT_EQ(7, receiver.seen_subgraph_pairs);
}

[[nodiscard]] static NodeGuard CreateStarJoin(THD *thd, int graph_size,
                                              std::mt19937 *engine,
                                              MEM_ROOT *mem_root,
                                              JoinHypergraph *g) {
  std::uniform_int_distribution<int> table_size(1, 10000);
  NodeGuard node_guard = AddNodes(graph_size, mem_root, g);
  for (int node_idx = 0; node_idx < graph_size; ++node_idx) {
    g->nodes[node_idx].table->file->stats.records = table_size(*engine);
  }

  std::uniform_real_distribution<double> selectivity(0.001, 1.000);
  for (int node_idx = 1; node_idx < graph_size; ++node_idx) {
    AddEdge(thd, RelationalExpression::INNER_JOIN, 0b1, NodeMap{1} << node_idx,
            selectivity(*engine), mem_root, g);
  }

  return node_guard;
}

[[nodiscard]] static NodeGuard CreateCliqueJoin(THD *thd, int graph_size,
                                                std::mt19937 *engine,
                                                MEM_ROOT *mem_root,
                                                JoinHypergraph *g) {
  std::uniform_int_distribution<int> table_size(1, 10000);
  NodeGuard node_guard = AddNodes(graph_size, mem_root, g);
  for (int node_idx = 0; node_idx < graph_size; ++node_idx) {
    g->nodes[node_idx].table->file->stats.records = table_size(*engine);
  }

  std::uniform_real_distribution<double> selectivity(0.001, 1.000);
  for (int node1_idx = 0; node1_idx < graph_size; ++node1_idx) {
    for (int node2_idx = node1_idx + 1; node2_idx < graph_size; ++node2_idx) {
      AddEdge(thd, RelationalExpression::INNER_JOIN, NodeMap{1} << node1_idx,
              NodeMap{1} << node2_idx, selectivity(*engine), mem_root, g);
    }
  }

  return node_guard;
}

TEST(GraphSimplificationTest, UndoRedo) {
  my_testing::Server_initializer initializer;
  initializer.SetUp();

  // Get consistent seeds between runs and platforms.
  std::mt19937 engine(1234);

  MEM_ROOT mem_root;
  JoinHypergraph g(&mem_root, /*query_block=*/nullptr);
  NodeGuard node_guard = CreateStarJoin(initializer.thd(), /*graph_size=*/20,
                                        &engine, &mem_root, &g);
  GraphSimplifier s(&g, &mem_root);

  std::uniform_int_distribution<int> back_or_forward(0, 4);
  for (;;) {  // Termination condition within loop.
    if (s.num_steps_done() == 0) {
      // We can only go forward.
      ASSERT_NE(GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE,
                s.DoSimplificationStep());
    } else {
      // With 20% probability, undo a step. Otherwise, do one.
      // This ensures we get to try both undos and redos.
      if (back_or_forward(engine) == 0) {
        s.UndoSimplificationStep();
      } else {
        if (s.DoSimplificationStep() ==
            GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE) {
          // We're all simplified.
          break;
        }
      }
    }
  }

  // This is just an empirical number; it can change if the heuristics change.
  // But it shouldn't change if we remove the undo code or change the seed.
  EXPECT_EQ(171, s.num_steps_done());
}

static void BM_FullySimplifyStarJoin(int graph_size, size_t num_iterations) {
  StopBenchmarkTiming();

  // Get consistent seeds between runs and platforms.
  std::mt19937 engine(1234);

  for (size_t i = 0; i < num_iterations; ++i) {
    MEM_ROOT mem_root;
    JoinHypergraph g(&mem_root, /*query_block=*/nullptr);
    my_testing::Server_initializer initializer;
    initializer.SetUp();

    NodeGuard node_guard =
        CreateStarJoin(initializer.thd(), graph_size, &engine, &mem_root, &g);

    StartBenchmarkTiming();
    GraphSimplifier s(&g, &mem_root);
    while (s.DoSimplificationStep() !=
           GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE)
      ;
    StopBenchmarkTiming();
  }
}

static void BM_FullySimplifyStarJoin10(size_t num_iterations) {
  BM_FullySimplifyStarJoin(10, num_iterations);
}
static void BM_FullySimplifyStarJoin20(size_t num_iterations) {
  BM_FullySimplifyStarJoin(20, num_iterations);
}
static void BM_FullySimplifyStarJoin30(size_t num_iterations) {
  BM_FullySimplifyStarJoin(30, num_iterations);
}
static void BM_FullySimplifyStarJoin40(size_t num_iterations) {
  BM_FullySimplifyStarJoin(40, num_iterations);
}
static void BM_FullySimplifyStarJoin50(size_t num_iterations) {
  BM_FullySimplifyStarJoin(50, num_iterations);
}
BENCHMARK(BM_FullySimplifyStarJoin10)
BENCHMARK(BM_FullySimplifyStarJoin20)
BENCHMARK(BM_FullySimplifyStarJoin30)
BENCHMARK(BM_FullySimplifyStarJoin40)
BENCHMARK(BM_FullySimplifyStarJoin50)
// NOTE: 100-way star joins are quoted as 160 ms in the paper,
// but since MAX_TABLES == 61, we cannot compare directly.
// Extrapolation indicates that we are doing fairly well, though.

static void BM_FullySimplifyCliqueJoin(int graph_size, size_t num_iterations) {
  StopBenchmarkTiming();

  // Get consistent seeds between runs and platforms.
  std::mt19937 engine(1234);

  for (size_t i = 0; i < num_iterations; ++i) {
    MEM_ROOT mem_root;
    JoinHypergraph g(&mem_root, /*query_block=*/nullptr);
    my_testing::Server_initializer initializer;
    initializer.SetUp();

    NodeGuard node_guard =
        CreateCliqueJoin(initializer.thd(), graph_size, &engine, &mem_root, &g);

    StartBenchmarkTiming();
    GraphSimplifier s(&g, &mem_root);
    while (s.DoSimplificationStep() !=
           GraphSimplifier::NO_SIMPLIFICATION_POSSIBLE)
      ;
    StopBenchmarkTiming();
  }
}

static void BM_FullySimplifyCliqueJoin10(size_t num_iterations) {
  BM_FullySimplifyCliqueJoin(10, num_iterations);
}
static void BM_FullySimplifyCliqueJoin20(size_t num_iterations) {
  BM_FullySimplifyCliqueJoin(20, num_iterations);
}
static void BM_FullySimplifyCliqueJoin30(size_t num_iterations) {
  BM_FullySimplifyCliqueJoin(30, num_iterations);
}
// static void BM_FullySimplifyCliqueJoin40(size_t num_iterations) {
//   BM_FullySimplifyCliqueJoin(40, num_iterations);
// }
// static void BM_FullySimplifyCliqueJoin50(size_t num_iterations) {
//   BM_FullySimplifyCliqueJoin(50, num_iterations);
// }
BENCHMARK(BM_FullySimplifyCliqueJoin10)
BENCHMARK(BM_FullySimplifyCliqueJoin20)
BENCHMARK(BM_FullySimplifyCliqueJoin30)

// Too slow to run on every commit, but can be enabled manually without
// problems.
// BENCHMARK(BM_FullySimplifyCliqueJoin40)
// BENCHMARK(BM_FullySimplifyCliqueJoin50)