File: connect_joins-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 (761 lines) | stat: -rw-r--r-- 33,691 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
/* 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 <string>
#include <unordered_map>
#include <vector>

#include "my_table_map.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/join_optimizer/access_path.h"
#include "sql/join_optimizer/explain_access_path.h"
#include "sql/join_optimizer/relational_expression.h"
#include "sql/mem_root_array.h"
#include "sql/sql_class.h"
#include "sql/sql_executor.h"
#include "sql/sql_lex.h"
#include "sql/sql_opt_exec_shared.h"
#include "sql/sql_optimizer.h"
#include "sql/sql_select.h"
#include "unittest/gunit/fake_table.h"
#include "unittest/gunit/optimizer_test.h"

using optimizer_test::Table;
using std::vector;
using ConnectJoinTest = OptimizerTestBase;

// Tests a semijoin access path with two tables.
TEST_F(ConnectJoinTest, SemiJoin) {
  Query_block *query_block =
      ParseAndResolve("SELECT 1 FROM t1 WHERE t1.x IN (SELECT t2.x FROM t2)",
                      /*nullable=*/true);

  query_block->join->const_tables = 0;
  query_block->join->primary_tables = query_block->join->tables = 2;

  // Set up plan for two table join - t1 semijoin t2. prefix_tables
  // is unused for this query plan.
  vector<Table> tables;
  tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b01));
  tables.push_back(Table("t2", /*plan_idx=*/1, /*prefix_tables=*/0b11));

  SetUpQEPTabs(query_block, /*num_tables=*/2, tables);

  // Set up the semijoin path, by setting "firstmatch_return" to the table
  // where the semijoin iterator will be created. Also attach the join
  // condition.
  JOIN *join = query_block->join;
  join->qep_tab[1].firstmatch_return = 0;
  join->qep_tab[1].set_condition(query_block->join->where_cond);

  qep_tab_map unhandled_duplicates = 0;
  qep_tab_map conditions_depend_on_outer_tables = 0;
  vector<PendingInvalidator> pending_invalidators;

  // This will set up the access paths.
  AccessPath *root = ConnectJoins(
      /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
      /*last_idx=*/2, join->qep_tab, m_thd,
      /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
      &pending_invalidators, /*pending_join_conditions=*/nullptr,
      &unhandled_duplicates, &conditions_depend_on_outer_tables);

  // Prints out the query plan on failure.
  SCOPED_TRACE(PrintQueryPlan(0, root, join,
                              /*is_root_of_join=*/true));

  // Verify that we have t1 hash-semijoin t2 on t1.x = t2.x.
  ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
  EXPECT_EQ(RelationalExpression::SEMIJOIN,
            root->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &join_conditions =
      root->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, join_conditions.size());
  EXPECT_EQ("(t1.x = t2.x)", ItemToString(join_conditions[0]));

  AccessPath *outer = root->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, outer->type);
  EXPECT_EQ(m_fake_tables["t1"], outer->table_scan().table);

  AccessPath *inner = root->hash_join().inner;
  ASSERT_EQ(AccessPath::TABLE_SCAN, inner->type);
  EXPECT_EQ(m_fake_tables["t2"], inner->table_scan().table);
}

// We test a semijoin with two tables on its inner side (no multiple
// equalities).
TEST_F(ConnectJoinTest, SemiJoinWithInnerJoin) {
  Query_block *query_block = ParseAndResolve(
      "SELECT 1 FROM t1 WHERE t1.x IN (SELECT t2.x FROM t2 JOIN t3 "
      "ON t2.y=t3.y)",
      /*nullable=*/true);

  query_block->join->const_tables = 0;
  query_block->join->primary_tables = query_block->join->tables = 3;

  // Set up the plan for three table join of the above query.
  // Plan would be t1 SEMIJOIN (t2 JOIN t3). As the optimizer generates plan
  // for an NLJ, the table order would be t1->t3->t2.
  vector<Table> tables;
  tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
  tables.push_back(Table("t3", /*plan_idx=*/1, /*prefix_tables=*/0b011));
  tables.push_back(Table("t2", /*plan_idx=*/2, /*prefix_tables=*/0b111));
  SetUpQEPTabs(query_block, /*num_tables=*/3, tables);

  // Setup the semijoin.
  JOIN *join = query_block->join;
  join->qep_tab[2].firstmatch_return = 0;
  join->qep_tab[2].set_condition(query_block->join->where_cond);

  qep_tab_map unhandled_duplicates = 0;
  qep_tab_map conditions_depend_on_outer_tables = 0;
  vector<PendingInvalidator> pending_invalidators;

  // Create access paths now.
  AccessPath *root = ConnectJoins(
      /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
      /*last_idx=*/3, join->qep_tab, m_thd,
      /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
      &pending_invalidators, /*pending_join_conditions=*/nullptr,
      &unhandled_duplicates, &conditions_depend_on_outer_tables);

  SCOPED_TRACE(PrintQueryPlan(0, root, join,
                              /*is_root_of_join=*/true));

  // Verify that we have t1 hash-semijoin (t2 hash join t3 on t2.y = t3.y)
  // on t1.x = t2.x
  ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
  EXPECT_EQ(RelationalExpression::SEMIJOIN,
            root->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &semijoin_conditions =
      root->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, semijoin_conditions.size());
  EXPECT_EQ("(t1.x = t2.x)", ItemToString(semijoin_conditions[0]));

  AccessPath *semi_outer = root->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, semi_outer->type);
  EXPECT_EQ(m_fake_tables["t1"], semi_outer->table_scan().table);

  AccessPath *semi_inner = root->hash_join().inner;
  ASSERT_EQ(AccessPath::HASH_JOIN, semi_inner->type);
  EXPECT_EQ(RelationalExpression::INNER_JOIN,
            semi_inner->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &inner_join_conditions =
      semi_inner->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, inner_join_conditions.size());
  EXPECT_EQ("(t2.y = t3.y)", ItemToString(inner_join_conditions[0]));

  AccessPath *first_table_inner = semi_inner->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_inner->type);
  EXPECT_EQ(m_fake_tables["t2"], first_table_inner->table_scan().table);

  AccessPath *second_table_inner = semi_inner->hash_join().inner;
  ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_inner->type);
  EXPECT_EQ(m_fake_tables["t3"], second_table_inner->table_scan().table);
}

// We test a semijoin with two tables on its inner side with multiple
// equalities.
TEST_F(ConnectJoinTest, SemiJoinWithMultiEqual) {
  Query_block *query_block = ParseAndResolve(
      "SELECT 1 FROM t1 WHERE t1.x IN (SELECT t2.x FROM t2 JOIN t3 "
      "ON t2.x=t3.x)",
      /*nullable=*/true);

  query_block->join->const_tables = 0;
  query_block->join->primary_tables = query_block->join->tables = 3;

  // Set up the plan for three table join of the above query.
  // Plan would be t1 SEMIJOIN (t2 JOIN t3). As the optimizer generates plan
  // for an NLJ, the table order would be t1->t3->t2.
  // JOIN_TAB indexing will be based on the position in the table list which is
  // t1,t2,t3. However the planner would pick - t1,t3,t2
  vector<Table> tables;
  tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
  tables.push_back(Table("t2", /*plan_idx=*/2, /*prefix_tables=*/0b111));
  tables.push_back(Table("t3", /*plan_idx=*/1, /*prefix_tables=*/0b101));
  SetUpJoinTabs(query_block, /*num_tables=*/3, tables);

  // As mentioned before, the planner at the end would pick - t1,t3,t2
  // QEP_TAB indexing is based on the final plan and not the pos_in_table_list.
  tables.clear();
  tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
  tables.push_back(Table("t3", /*plan_idx=*/1, /*prefix_tables=*/0b011));
  tables.push_back(Table("t2", /*plan_idx=*/2, /*prefix_tables=*/0b111));
  SetUpQEPTabs(query_block, /*num_tables*/ 3, tables);

  COND_EQUAL *cond_equal = nullptr;
  // Generate multi-equalities.
  EXPECT_FALSE(optimize_cond(m_thd, query_block->where_cond_ref(), &cond_equal,
                             &query_block->m_table_nest,
                             &query_block->cond_value));
  JOIN_TAB *map2table[3];

  // Using the multi-equalities and based on the table order picked, create
  // join conditions.
  for (unsigned int i = 0; i < query_block->join->primary_tables; i++)
    map2table[i] = &query_block->join->join_tab[i];
  Item **where_cond = &query_block->join->where_cond;
  *where_cond =
      substitute_for_best_equal_field(m_thd, query_block->join->where_cond,
                                      query_block->join->cond_equal, map2table);
  EXPECT_EQ("((t3.x = t1.x) and (t2.x = t1.x))", ItemToString(*where_cond));

  JOIN *join = query_block->join;
  // Attach conditions to tables.
  Item *cond = nullptr;
  for (unsigned int i = 0; i < query_block->join->primary_tables; i++) {
    JOIN_TAB *join_tab = &query_block->join->join_tab[i];
    table_map used_tables = join_tab->prefix_tables();
    table_map current_map = 1ULL << i;
    cond = make_cond_for_table(m_thd, *where_cond, used_tables, current_map,
                               /*exclude_expensive_cond=*/false);
    if (cond) join->qep_tab[join_tab->idx()].set_condition(cond);
  }

  // Setup semijoin path.
  join->qep_tab[2].firstmatch_return = 0;

  // Finally create access paths.
  qep_tab_map unhandled_duplicates = 0;
  qep_tab_map conditions_depend_on_outer_tables = 0;
  vector<PendingInvalidator> pending_invalidators;

  AccessPath *root = ConnectJoins(
      /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
      /*last_idx=*/3, join->qep_tab, m_thd,
      /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
      &pending_invalidators, /*pending_join_conditions=*/nullptr,
      &unhandled_duplicates, &conditions_depend_on_outer_tables);

  SCOPED_TRACE(PrintQueryPlan(0, root, join,
                              /*is_root_of_join=*/true));

  // Verify that we have t1 hash-semijoin (t2 hash join t3 on t2.x = t3.x)
  // on t1.x = t3.x
  ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
  EXPECT_EQ(RelationalExpression::SEMIJOIN,
            root->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &semijoin_conditions =
      root->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, semijoin_conditions.size());
  EXPECT_EQ("(t3.x = t1.x)", ItemToString(semijoin_conditions[0]));

  AccessPath *semi_outer = root->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, semi_outer->type);
  EXPECT_EQ(m_fake_tables["t1"], semi_outer->table_scan().table);

  AccessPath *semi_inner = root->hash_join().inner;
  ASSERT_EQ(AccessPath::HASH_JOIN, semi_inner->type);
  EXPECT_EQ(RelationalExpression::INNER_JOIN,
            semi_inner->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &inner_join_conditions =
      semi_inner->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, inner_join_conditions.size());
  EXPECT_EQ("(t2.x = t3.x)", ItemToString(inner_join_conditions[0]));

  AccessPath *first_table_inner = semi_inner->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_inner->type);
  EXPECT_EQ(m_fake_tables["t2"], first_table_inner->table_scan().table);

  AccessPath *second_table_inner = semi_inner->hash_join().inner;
  ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_inner->type);
  EXPECT_EQ(m_fake_tables["t3"], second_table_inner->table_scan().table);
}

// Test outer join
TEST_F(ConnectJoinTest, OuterJoin) {
  Query_block *query_block = ParseAndResolve(
      "SELECT 1 FROM t1 JOIN t2 ON t1.x = t2.x LEFT JOIN t3 "
      "ON t2.x=t3.x",
      /*nullable=*/true);

  query_block->join->const_tables = 0;
  query_block->join->primary_tables = query_block->join->tables = 3;

  // Set up the plan for three table join of the above query.
  // Plan would be t1 JOIN t2 LEFT JOIN t3.
  vector<Table> tables;
  tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
  tables.push_back(Table("t2", /*plan_idx=*/1, /*prefix_tables=*/0b011));
  tables.push_back(Table("t3", /*plan_idx=*/2, /*prefix_tables=*/0b111));
  SetUpJoinTabs(query_block, /*num_tables=*/3, tables);
  // Setup outer join info
  // first inner table for including outer join
  query_block->join->join_tab[2].set_first_inner(2);
  // last table for embedding outer join
  query_block->join->join_tab[2].set_last_inner(2);
  // first inner table for embedding outer join
  query_block->join->join_tab[2].set_first_upper(1);

  // Set up QEP_TABs now
  SetUpQEPTabs(query_block, /*num_tables=*/3, tables);
  query_block->join->qep_tab[2].set_first_inner(2);
  query_block->join->qep_tab[2].set_last_inner(2);
  query_block->join->qep_tab[2].set_first_upper(1);

  COND_EQUAL *cond_equal = nullptr;
  // Generate multi-equalities
  EXPECT_FALSE(optimize_cond(m_thd, query_block->where_cond_ref(), &cond_equal,
                             &query_block->m_table_nest,
                             &query_block->cond_value));
  JOIN_TAB *map2table[3];

  // Using the multi-equalities and based on the table order picked, create
  // join conditions for each of the joins.
  for (unsigned int i = 0; i < query_block->join->primary_tables; i++)
    map2table[i] = &query_block->join->join_tab[i];
  Item **where_cond = &query_block->join->where_cond;
  *where_cond =
      substitute_for_best_equal_field(m_thd, query_block->join->where_cond,
                                      query_block->join->cond_equal, map2table);
  EXPECT_EQ("(t1.x = t2.x)", ItemToString(*where_cond));

  JOIN *join = query_block->join;
  // Attach conditions to tables
  Item *cond = nullptr;
  for (unsigned int i = 0; i < query_block->join->primary_tables; i++) {
    JOIN_TAB *join_tab = &query_block->join->join_tab[i];
    table_map used_tables = join_tab->prefix_tables();
    table_map current_map = 1ULL << i;
    cond = make_cond_for_table(m_thd, *where_cond, used_tables, current_map,
                               /*exclude_expensive_cond=*/false);
    if (cond)
      join->qep_tab[join_tab->idx()].set_condition(cond);
    else if (join->qep_tab[join_tab->idx()].condition() !=
             join_tab->condition())
      join->qep_tab[join_tab->idx()].set_condition(join_tab->condition());
  }

  // Add is_not_null_compl condition for the outer join condition attached
  // to t3
  Item *outer_join_cond = new (m_thd->mem_root) Item_func_trig_cond(
      join->qep_tab[2].condition(), /*f - trigger variable=*/nullptr,
      query_block->join, join->qep_tab[2].first_inner(),
      Item_func_trig_cond::IS_NOT_NULL_COMPL);
  outer_join_cond->quick_fix_field();
  join->qep_tab[2].set_condition(outer_join_cond);

  // Finally create access paths.
  qep_tab_map unhandled_duplicates = 0;
  qep_tab_map conditions_depend_on_outer_tables = 0;
  vector<PendingInvalidator> pending_invalidators;

  AccessPath *root = ConnectJoins(
      /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
      /*last_idx=*/3, join->qep_tab, m_thd,
      /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
      &pending_invalidators, /*pending_join_conditions=*/nullptr,
      &unhandled_duplicates, &conditions_depend_on_outer_tables);

  SCOPED_TRACE(PrintQueryPlan(0, root, join,
                              /*is_root_of_join=*/true));

  ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
  EXPECT_EQ(RelationalExpression::LEFT_JOIN,
            root->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &leftjoin_conditions =
      root->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, leftjoin_conditions.size());
  EXPECT_EQ("(t2.x = t3.x)", ItemToString(leftjoin_conditions[0]));

  AccessPath *left_outer = root->hash_join().outer;
  ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
  EXPECT_EQ(RelationalExpression::INNER_JOIN,
            left_outer->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &inner_join_conditions =
      left_outer->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, inner_join_conditions.size());
  EXPECT_EQ("(t1.x = t2.x)", ItemToString(inner_join_conditions[0]));

  AccessPath *first_table_outer = left_outer->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_outer->type);
  EXPECT_EQ(m_fake_tables["t2"], first_table_outer->table_scan().table);

  AccessPath *second_table_outer = left_outer->hash_join().inner;
  ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_outer->type);
  EXPECT_EQ(m_fake_tables["t1"], second_table_outer->table_scan().table);

  AccessPath *left_inner = root->hash_join().inner;
  ASSERT_EQ(AccessPath::TABLE_SCAN, left_inner->type);
  EXPECT_EQ(m_fake_tables["t3"], left_inner->table_scan().table);
}

// Test semijoin with outer join on its inner side
TEST_F(ConnectJoinTest, OuterJoinInSemiJoin) {
  Query_block *query_block = ParseAndResolve(
      "SELECT 1 FROM t1 WHERE t1.x IN (SELECT t2.x FROM t2 LEFT "
      "JOIN t3"
      " ON t2.x=t3.x)",
      /*nullable=*/true);

  query_block->join->const_tables = 0;
  query_block->join->primary_tables = query_block->join->tables = 3;

  // Set up the plan for three table join of the above query.
  // Plan would be t1 SEMIJOIN t2 LEFT JOIN t3.
  vector<Table> tables;
  tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
  tables.push_back(Table("t2", /*plan_idx=*/1, /*prefix_tables=*/0b011));
  tables.push_back(Table("t3", /*plan_idx=*/2, /*prefix_tables=*/0b111));
  SetUpJoinTabs(query_block, /*num_tables=*/3, tables);

  // Set up QEP_TABs and the outer join info
  SetUpQEPTabs(query_block, /*num_tables=*/3, tables);
  query_block->join->qep_tab[2].set_first_inner(2);
  query_block->join->qep_tab[2].set_last_inner(2);
  query_block->join->qep_tab[2].set_first_upper(1);

  Item *where_cond = query_block->join->where_cond;

  JOIN *join = query_block->join;
  // Attach conditions to tables
  join->qep_tab[1].set_condition(where_cond);
  join->qep_tab[2].set_condition(query_block->join->join_tab[2].condition());

  // Add is_not_null_compl condition for the outer join condition attached
  // to t3
  Item *outer_join_cond = new (m_thd->mem_root) Item_func_trig_cond(
      join->qep_tab[2].condition(), /* f - trigger variable=*/nullptr,
      query_block->join, join->qep_tab[2].first_inner(),
      Item_func_trig_cond::IS_NOT_NULL_COMPL);
  outer_join_cond->quick_fix_field();
  join->qep_tab[2].set_condition(outer_join_cond);

  join->qep_tab[2].firstmatch_return = 0;

  // Finally create access paths
  qep_tab_map unhandled_duplicates = 0;
  qep_tab_map conditions_depend_on_outer_tables = 0;
  vector<PendingInvalidator> pending_invalidators;

  AccessPath *root = ConnectJoins(
      /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
      /*last_idx=*/3, join->qep_tab, m_thd,
      /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
      &pending_invalidators, /*pending_join_conditions=*/nullptr,
      &unhandled_duplicates, &conditions_depend_on_outer_tables);

  // Verify if everything is as expected
  SCOPED_TRACE(PrintQueryPlan(0, root, join,
                              /*is_root_of_join=*/true));

  ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
  EXPECT_EQ(RelationalExpression::SEMIJOIN,
            root->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &semijoin_conditions =
      root->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, semijoin_conditions.size());
  EXPECT_EQ("(t1.x = t2.x)", ItemToString(semijoin_conditions[0]));

  AccessPath *semi_outer = root->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, semi_outer->type);
  EXPECT_EQ(m_fake_tables["t1"], semi_outer->table_scan().table);

  AccessPath *semi_inner = root->hash_join().inner;
  ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
  EXPECT_EQ(RelationalExpression::LEFT_JOIN,
            semi_inner->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &outer_join_conditions =
      semi_inner->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, outer_join_conditions.size());
  EXPECT_EQ("(t2.x = t3.x)", ItemToString(outer_join_conditions[0]));

  AccessPath *first_table_outer = semi_inner->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_outer->type);
  EXPECT_EQ(m_fake_tables["t2"], first_table_outer->table_scan().table);

  AccessPath *second_table_outer = semi_inner->hash_join().inner;
  ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_outer->type);
  EXPECT_EQ(m_fake_tables["t3"], second_table_outer->table_scan().table);
}

// Test semijoin within outer join.
TEST_F(ConnectJoinTest, SemiJoinInOuterJoin) {
  Query_block *query_block = ParseAndResolve(
      "SELECT 1 FROM t1 LEFT JOIN t2 ON t1.x = t2.x WHERE "
      "t2.x IN (SELECT t3.x FROM t3)",
      /*nullable=*/true);

  query_block->join->const_tables = 0;
  query_block->join->primary_tables = query_block->join->tables = 3;

  // Set up the plan for three table join of the above query.
  // Plan would be t1 LEFT JOIN t2 SEMI JOIN t3.
  vector<Table> tables;
  tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
  tables.push_back(Table("t2", /*plan_idx=*/2, /*prefix_tables=*/0b111));
  tables.push_back(Table("t3", /*plan_idx=*/1, /*prefix_tables=*/0b101));
  SetUpJoinTabs(query_block, /*num_tables=*/3, tables);

  tables.clear();
  tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
  tables.push_back(Table("t3", /*plan_idx=*/1, /*prefix_tables=*/0b011));
  tables.push_back(Table("t2", /*plan_idx=*/2, /*prefix_tables=*/0b111));
  SetUpQEPTabs(query_block, /*num_tables=*/3, tables);

  // Setup outer join info
  query_block->join->qep_tab[1].set_first_inner(1);
  query_block->join->qep_tab[1].set_last_inner(2);
  query_block->join->qep_tab[1].set_first_upper(0);

  COND_EQUAL *cond_equal = nullptr;
  // Generate multi-equalities
  EXPECT_FALSE(optimize_cond(m_thd, query_block->where_cond_ref(), &cond_equal,
                             &query_block->m_table_nest,
                             &query_block->cond_value));
  JOIN_TAB *map2table[3];

  // Using the multi-equalities and based on the table order picked, create
  // join conditions for each of the joins.
  for (unsigned int i = 0; i < query_block->join->primary_tables; i++)
    map2table[i] = &query_block->join->join_tab[i];
  Item **where_cond = &query_block->join->where_cond;
  *where_cond =
      substitute_for_best_equal_field(m_thd, query_block->join->where_cond,
                                      query_block->join->cond_equal, map2table);
  EXPECT_EQ("((t3.x = t1.x) and (t2.x = t1.x))", ItemToString(*where_cond));

  JOIN *join = query_block->join;
  Item *cond = nullptr;
  for (unsigned int i = 0; i < query_block->join->primary_tables; i++) {
    JOIN_TAB *join_tab = &query_block->join->join_tab[i];
    table_map used_tables = join_tab->prefix_tables();
    table_map current_map = 1ULL << i;
    cond = make_cond_for_table(m_thd, *where_cond, used_tables, current_map,
                               /*exclude_expensive_cond=*/false);
    if (cond) join->qep_tab[join_tab->idx()].set_condition(cond);
  }

  // Add is_not_null_compl condition for the outer join condition attached
  // to t3
  Item *first_outer_join_cond = new (m_thd->mem_root) Item_func_trig_cond(
      join->qep_tab[1].condition(), nullptr, query_block->join,
      join->qep_tab[1].first_inner(), Item_func_trig_cond::IS_NOT_NULL_COMPL);
  first_outer_join_cond->quick_fix_field();
  join->qep_tab[1].set_condition(first_outer_join_cond);

  Item *second_outer_join_cond = new (m_thd->mem_root) Item_func_trig_cond(
      join->qep_tab[2].condition(), nullptr, query_block->join,
      join->qep_tab[2].first_inner(), Item_func_trig_cond::IS_NOT_NULL_COMPL);
  second_outer_join_cond->quick_fix_field();
  join->qep_tab[2].set_condition(second_outer_join_cond);

  join->qep_tab[2].firstmatch_return = 1;

  // Create access paths.
  qep_tab_map unhandled_duplicates = 0;
  qep_tab_map conditions_depend_on_outer_tables = 0;
  vector<PendingInvalidator> pending_invalidators;

  AccessPath *root = ConnectJoins(
      /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
      /*last_idx=*/3, join->qep_tab, m_thd,
      /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
      &pending_invalidators, /*pending_join_conditions=*/nullptr,
      &unhandled_duplicates, &conditions_depend_on_outer_tables);

  SCOPED_TRACE(PrintQueryPlan(0, root, join,
                              /*is_root_of_join=*/true));

  ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
  EXPECT_EQ(RelationalExpression::LEFT_JOIN,
            root->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &leftjoin_conditions =
      root->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, leftjoin_conditions.size());
  EXPECT_EQ("(t3.x = t1.x)", ItemToString(leftjoin_conditions[0]));

  AccessPath *left_outer = root->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, left_outer->type);
  EXPECT_EQ(m_fake_tables["t1"], left_outer->table_scan().table);

  AccessPath *left_inner = root->hash_join().inner;
  ASSERT_EQ(AccessPath::HASH_JOIN, left_inner->type);
  EXPECT_EQ(RelationalExpression::SEMIJOIN,
            left_inner->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &semi_join_conditions =
      left_inner->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, semi_join_conditions.size());
  EXPECT_EQ("(t2.x = t3.x)", ItemToString(semi_join_conditions[0]));

  AccessPath *first_table_semi = left_inner->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_semi->type);
  EXPECT_EQ(m_fake_tables["t3"], first_table_semi->table_scan().table);

  AccessPath *second_table_semi = left_inner->hash_join().inner;
  ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_semi->type);
  EXPECT_EQ(m_fake_tables["t2"], second_table_semi->table_scan().table);
}

// We test a semijoin having multiple equalites and a non-equal function
TEST_F(ConnectJoinTest, SemiJoinWithNotEqual) {
  Query_block *query_block = ParseAndResolve(
      "SELECT 1 FROM t1 WHERE t1.x IN (SELECT t2.x FROM t2 JOIN t3 "
      "ON t2.x=t3.x JOIN t4 ON t3.x = t4.x where t3.y != t4.y)",
      /*nullable=*/true);

  query_block->join->const_tables = 0;
  query_block->join->primary_tables = query_block->join->tables = 4;

  // Set up the plan for four table join of the above query.
  // Plan would be t1 SEMIJOIN (t2 JOIN t3 JOIN t4). As the optimizer generates
  // plan for an NLJ, the table order would be t1->t4->t3->t2
  // JOIN_TAB indexing will be based on the position in the table list which is
  // t1,t2,t3,t4. However the planner would pick - t1,t4,t3,t2
  vector<Table> tables;
  tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b0001));
  tables.push_back(Table("t2", /*plan_idx=*/3, /*prefix_tables=*/0b1111));
  tables.push_back(Table("t3", /*plan_idx=*/2, /*prefix_tables=*/0b1101));
  tables.push_back(Table("t4", /*plan_idx=*/1, /*prefix_tables=*/0b1001));
  SetUpJoinTabs(query_block, /*num_tables=*/4, tables);

  // As mentioned before, the planner at the end would pick - t1,t4,t3,t2
  // QEP_TAB indexing is based on the final plan and not the pos_in_table_list.
  tables.clear();
  tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b0001));
  tables.push_back(Table("t4", /*plan_idx=*/1, /*prefix_tables=*/0b0011));
  tables.push_back(Table("t3", /*plan_idx=*/2, /*prefix_tables=*/0b0111));
  tables.push_back(Table("t2", /*plan_idx=*/3, /*prefix_tables=*/0b1111));
  SetUpQEPTabs(query_block, /*num_tables*/ 4, tables);

  COND_EQUAL *cond_equal = nullptr;
  // Generate multi-equalities.
  EXPECT_FALSE(optimize_cond(m_thd, query_block->where_cond_ref(), &cond_equal,
                             &query_block->m_table_nest,
                             &query_block->cond_value));
  JOIN_TAB *map2table[4];

  // Using the multi-equalities and based on the table order picked, create
  // join conditions.
  for (unsigned int i = 0; i < query_block->join->primary_tables; i++)
    map2table[i] = &query_block->join->join_tab[i];
  Item **where_cond = &query_block->join->where_cond;
  *where_cond =
      substitute_for_best_equal_field(m_thd, query_block->join->where_cond,
                                      query_block->join->cond_equal, map2table);
  EXPECT_EQ(
      "((t4.x = t1.x) and (t3.x = t1.x) and (t2.x = t1.x) and (t3.y <> t4.y))",
      ItemToString(*where_cond));

  JOIN *join = query_block->join;
  // Attach conditions to tables.
  Item *cond = nullptr;
  for (unsigned int i = 0; i < query_block->join->primary_tables; i++) {
    JOIN_TAB *join_tab = &query_block->join->join_tab[i];
    table_map used_tables = join_tab->prefix_tables();
    table_map current_map = 1ULL << i;
    cond = make_cond_for_table(m_thd, *where_cond, used_tables, current_map,
                               /*exclude_expensive_cond=*/false);
    if (cond) join->qep_tab[join_tab->idx()].set_condition(cond);
  }

  // Setup semijoin path.
  join->qep_tab[3].firstmatch_return = 0;

  // Finally create access paths.
  qep_tab_map unhandled_duplicates = 0;
  qep_tab_map conditions_depend_on_outer_tables = 0;
  vector<PendingInvalidator> pending_invalidators;

  AccessPath *root = ConnectJoins(
      /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
      /*last_idx=*/4, join->qep_tab, m_thd,
      /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
      &pending_invalidators, /*pending_join_conditions=*/nullptr,
      &unhandled_duplicates, &conditions_depend_on_outer_tables);

  SCOPED_TRACE(PrintQueryPlan(0, root, join,
                              /*is_root_of_join=*/true));

  // Verify that we have t1 hash-semijoin (t2 hash join (t3 hash join t4
  // on t3.x = t4.x (with filter on t3.y != t4.y)) on t2.x = t4.x)
  // on t1.x = t4.x)
  // Earlier filter would not be placed with the inner join between
  // t3 and t4
  ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
  EXPECT_EQ(RelationalExpression::SEMIJOIN,
            root->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &semijoin_conditions =
      root->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, semijoin_conditions.size());
  EXPECT_EQ("(t4.x = t1.x)", ItemToString(semijoin_conditions[0]));

  AccessPath *semi_outer = root->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, semi_outer->type);
  EXPECT_EQ(m_fake_tables["t1"], semi_outer->table_scan().table);

  AccessPath *semi_inner = root->hash_join().inner;
  ASSERT_EQ(AccessPath::HASH_JOIN, semi_inner->type);
  EXPECT_EQ(RelationalExpression::INNER_JOIN,
            semi_inner->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &inner_join_conditions =
      semi_inner->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, inner_join_conditions.size());
  EXPECT_EQ("(t2.x = t4.x)", ItemToString(inner_join_conditions[0]));

  AccessPath *first_table_inner = semi_inner->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_inner->type);
  EXPECT_EQ(m_fake_tables["t2"], first_table_inner->table_scan().table);

  AccessPath *second_table_inner = semi_inner->hash_join().inner;
  ASSERT_EQ(AccessPath::FILTER, second_table_inner->type);
  EXPECT_EQ("(t3.y <> t4.y)",
            ItemToString(second_table_inner->filter().condition));

  AccessPath *filter_child = second_table_inner->filter().child;
  ASSERT_EQ(AccessPath::HASH_JOIN, filter_child->type);
  EXPECT_EQ(RelationalExpression::INNER_JOIN,
            filter_child->hash_join().join_predicate->expr->type);

  const Mem_root_array<Item_eq_base *> &below_filter_inner_join_conditions =
      filter_child->hash_join().join_predicate->expr->equijoin_conditions;
  ASSERT_EQ(1, below_filter_inner_join_conditions.size());
  EXPECT_EQ("(t3.x = t4.x)",
            ItemToString(below_filter_inner_join_conditions[0]));

  AccessPath *first_table_inner_to_filter = filter_child->hash_join().outer;
  ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_inner_to_filter->type);
  EXPECT_EQ(m_fake_tables["t3"],
            first_table_inner_to_filter->table_scan().table);

  AccessPath *second_table_inner_to_filter = filter_child->hash_join().inner;
  ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_inner_to_filter->type);
  EXPECT_EQ(m_fake_tables["t4"],
            second_table_inner_to_filter->table_scan().table);
}