Debian Codegraph @

unittest/gunit/connect_joins-t.cc

raw symbols SCIP
1 /* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gtest/gtest.h>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28
29 #include "my_table_map.h"
30 #include "sql/item.h"
31 #include "sql/item_cmpfunc.h"
32 #include "sql/join_optimizer/access_path.h"
33 #include "sql/join_optimizer/explain_access_path.h"
34 #include "sql/join_optimizer/relational_expression.h"
35 #include "sql/mem_root_array.h"
36 #include "sql/sql_class.h"
37 #include "sql/sql_executor.h"
38 #include "sql/sql_lex.h"
39 #include "sql/sql_opt_exec_shared.h"
40 #include "sql/sql_optimizer.h"
41 #include "sql/sql_select.h"
42 #include "unittest/gunit/fake_table.h"
43 #include "unittest/gunit/optimizer_test.h"
44
45 using optimizer_test::Table;
46 using std::vector;
47 using ConnectJoinTest = OptimizerTestBase;
48
49 // Tests a semijoin access path with two tables.
50 TEST_F(ConnectJoinTest, SemiJoin) {
51 Query_block *query_block =
52 ParseAndResolve("SELECT 1 FROM t1 WHERE t1.x IN (SELECT t2.x FROM t2)",
53 /*nullable=*/true);
54
55 query_block->join->const_tables = 0;
56 query_block->join->primary_tables = query_block->join->tables = 2;
57
58 // Set up plan for two table join - t1 semijoin t2. prefix_tables
59 // is unused for this query plan.
60 vector<Table> tables;
61 tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b01));
62 tables.push_back(Table("t2", /*plan_idx=*/1, /*prefix_tables=*/0b11));
63
64 SetUpQEPTabs(query_block, /*num_tables=*/2, tables);
65
66 // Set up the semijoin path, by setting "firstmatch_return" to the table
67 // where the semijoin iterator will be created. Also attach the join
68 // condition.
69 JOIN *join = query_block->join;
70 join->qep_tab[1].firstmatch_return = 0;
71 join->qep_tab[1].set_condition(query_block->join->where_cond);
72
73 qep_tab_map unhandled_duplicates = 0;
74 qep_tab_map conditions_depend_on_outer_tables = 0;
75 vector<PendingInvalidator> pending_invalidators;
76
77 // This will set up the access paths.
78 AccessPath *root = ConnectJoins(
79 /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
80 /*last_idx=*/2, join->qep_tab, m_thd,
81 /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
82 &pending_invalidators, /*pending_join_conditions=*/nullptr,
83 &unhandled_duplicates, &conditions_depend_on_outer_tables);
84
85 // Prints out the query plan on failure.
86 SCOPED_TRACE(PrintQueryPlan(0, root, join,
87 /*is_root_of_join=*/true));
88
89 // Verify that we have t1 hash-semijoin t2 on t1.x = t2.x.
90 ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
91 EXPECT_EQ(RelationalExpression::SEMIJOIN,
92 root->hash_join().join_predicate->expr->type);
93
94 const Mem_root_array<Item_eq_base *> &join_conditions =
95 root->hash_join().join_predicate->expr->equijoin_conditions;
96 ASSERT_EQ(1, join_conditions.size());
97 EXPECT_EQ("(t1.x = t2.x)", ItemToString(join_conditions[0]));
98
99 AccessPath *outer = root->hash_join().outer;
100 ASSERT_EQ(AccessPath::TABLE_SCAN, outer->type);
101 EXPECT_EQ(m_fake_tables["t1"], outer->table_scan().table);
102
103 AccessPath *inner = root->hash_join().inner;
104 ASSERT_EQ(AccessPath::TABLE_SCAN, inner->type);
105 EXPECT_EQ(m_fake_tables["t2"], inner->table_scan().table);
106 }
107
108 // We test a semijoin with two tables on its inner side (no multiple
109 // equalities).
110 TEST_F(ConnectJoinTest, SemiJoinWithInnerJoin) {
111 Query_block *query_block = ParseAndResolve(
112 "SELECT 1 FROM t1 WHERE t1.x IN (SELECT t2.x FROM t2 JOIN t3 "
113 "ON t2.y=t3.y)",
114 /*nullable=*/true);
115
116 query_block->join->const_tables = 0;
117 query_block->join->primary_tables = query_block->join->tables = 3;
118
119 // Set up the plan for three table join of the above query.
120 // Plan would be t1 SEMIJOIN (t2 JOIN t3). As the optimizer generates plan
121 // for an NLJ, the table order would be t1->t3->t2.
122 vector<Table> tables;
123 tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
124 tables.push_back(Table("t3", /*plan_idx=*/1, /*prefix_tables=*/0b011));
125 tables.push_back(Table("t2", /*plan_idx=*/2, /*prefix_tables=*/0b111));
126 SetUpQEPTabs(query_block, /*num_tables=*/3, tables);
127
128 // Setup the semijoin.
129 JOIN *join = query_block->join;
130 join->qep_tab[2].firstmatch_return = 0;
131 join->qep_tab[2].set_condition(query_block->join->where_cond);
132
133 qep_tab_map unhandled_duplicates = 0;
134 qep_tab_map conditions_depend_on_outer_tables = 0;
135 vector<PendingInvalidator> pending_invalidators;
136
137 // Create access paths now.
138 AccessPath *root = ConnectJoins(
139 /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
140 /*last_idx=*/3, join->qep_tab, m_thd,
141 /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
142 &pending_invalidators, /*pending_join_conditions=*/nullptr,
143 &unhandled_duplicates, &conditions_depend_on_outer_tables);
144
145 SCOPED_TRACE(PrintQueryPlan(0, root, join,
146 /*is_root_of_join=*/true));
147
148 // Verify that we have t1 hash-semijoin (t2 hash join t3 on t2.y = t3.y)
149 // on t1.x = t2.x
150 ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
151 EXPECT_EQ(RelationalExpression::SEMIJOIN,
152 root->hash_join().join_predicate->expr->type);
153
154 const Mem_root_array<Item_eq_base *> &semijoin_conditions =
155 root->hash_join().join_predicate->expr->equijoin_conditions;
156 ASSERT_EQ(1, semijoin_conditions.size());
157 EXPECT_EQ("(t1.x = t2.x)", ItemToString(semijoin_conditions[0]));
158
159 AccessPath *semi_outer = root->hash_join().outer;
160 ASSERT_EQ(AccessPath::TABLE_SCAN, semi_outer->type);
161 EXPECT_EQ(m_fake_tables["t1"], semi_outer->table_scan().table);
162
163 AccessPath *semi_inner = root->hash_join().inner;
164 ASSERT_EQ(AccessPath::HASH_JOIN, semi_inner->type);
165 EXPECT_EQ(RelationalExpression::INNER_JOIN,
166 semi_inner->hash_join().join_predicate->expr->type);
167
168 const Mem_root_array<Item_eq_base *> &inner_join_conditions =
169 semi_inner->hash_join().join_predicate->expr->equijoin_conditions;
170 ASSERT_EQ(1, inner_join_conditions.size());
171 EXPECT_EQ("(t2.y = t3.y)", ItemToString(inner_join_conditions[0]));
172
173 AccessPath *first_table_inner = semi_inner->hash_join().outer;
174 ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_inner->type);
175 EXPECT_EQ(m_fake_tables["t2"], first_table_inner->table_scan().table);
176
177 AccessPath *second_table_inner = semi_inner->hash_join().inner;
178 ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_inner->type);
179 EXPECT_EQ(m_fake_tables["t3"], second_table_inner->table_scan().table);
180 }
181
182 // We test a semijoin with two tables on its inner side with multiple
183 // equalities.
184 TEST_F(ConnectJoinTest, SemiJoinWithMultiEqual) {
185 Query_block *query_block = ParseAndResolve(
186 "SELECT 1 FROM t1 WHERE t1.x IN (SELECT t2.x FROM t2 JOIN t3 "
187 "ON t2.x=t3.x)",
188 /*nullable=*/true);
189
190 query_block->join->const_tables = 0;
191 query_block->join->primary_tables = query_block->join->tables = 3;
192
193 // Set up the plan for three table join of the above query.
194 // Plan would be t1 SEMIJOIN (t2 JOIN t3). As the optimizer generates plan
195 // for an NLJ, the table order would be t1->t3->t2.
196 // JOIN_TAB indexing will be based on the position in the table list which is
197 // t1,t2,t3. However the planner would pick - t1,t3,t2
198 vector<Table> tables;
199 tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
200 tables.push_back(Table("t2", /*plan_idx=*/2, /*prefix_tables=*/0b111));
201 tables.push_back(Table("t3", /*plan_idx=*/1, /*prefix_tables=*/0b101));
202 SetUpJoinTabs(query_block, /*num_tables=*/3, tables);
203
204 // As mentioned before, the planner at the end would pick - t1,t3,t2
205 // QEP_TAB indexing is based on the final plan and not the pos_in_table_list.
206 tables.clear();
207 tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
208 tables.push_back(Table("t3", /*plan_idx=*/1, /*prefix_tables=*/0b011));
209 tables.push_back(Table("t2", /*plan_idx=*/2, /*prefix_tables=*/0b111));
210 SetUpQEPTabs(query_block, /*num_tables*/ 3, tables);
211
212 COND_EQUAL *cond_equal = nullptr;
213 // Generate multi-equalities.
214 EXPECT_FALSE(optimize_cond(m_thd, query_block->where_cond_ref(), &cond_equal,
215 &query_block->m_table_nest,
216 &query_block->cond_value));
217 JOIN_TAB *map2table[3];
218
219 // Using the multi-equalities and based on the table order picked, create
220 // join conditions.
221 for (unsigned int i = 0; i < query_block->join->primary_tables; i++)
222 map2table[i] = &query_block->join->join_tab[i];
223 Item **where_cond = &query_block->join->where_cond;
224 *where_cond =
225 substitute_for_best_equal_field(m_thd, query_block->join->where_cond,
226 query_block->join->cond_equal, map2table);
227 EXPECT_EQ("((t3.x = t1.x) and (t2.x = t1.x))", ItemToString(*where_cond));
228
229 JOIN *join = query_block->join;
230 // Attach conditions to tables.
231 Item *cond = nullptr;
232 for (unsigned int i = 0; i < query_block->join->primary_tables; i++) {
233 JOIN_TAB *join_tab = &query_block->join->join_tab[i];
234 table_map used_tables = join_tab->prefix_tables();
235 table_map current_map = 1ULL << i;
236 cond = make_cond_for_table(m_thd, *where_cond, used_tables, current_map,
237 /*exclude_expensive_cond=*/false);
238 if (cond) join->qep_tab[join_tab->idx()].set_condition(cond);
239 }
240
241 // Setup semijoin path.
242 join->qep_tab[2].firstmatch_return = 0;
243
244 // Finally create access paths.
245 qep_tab_map unhandled_duplicates = 0;
246 qep_tab_map conditions_depend_on_outer_tables = 0;
247 vector<PendingInvalidator> pending_invalidators;
248
249 AccessPath *root = ConnectJoins(
250 /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
251 /*last_idx=*/3, join->qep_tab, m_thd,
252 /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
253 &pending_invalidators, /*pending_join_conditions=*/nullptr,
254 &unhandled_duplicates, &conditions_depend_on_outer_tables);
255
256 SCOPED_TRACE(PrintQueryPlan(0, root, join,
257 /*is_root_of_join=*/true));
258
259 // Verify that we have t1 hash-semijoin (t2 hash join t3 on t2.x = t3.x)
260 // on t1.x = t3.x
261 ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
262 EXPECT_EQ(RelationalExpression::SEMIJOIN,
263 root->hash_join().join_predicate->expr->type);
264
265 const Mem_root_array<Item_eq_base *> &semijoin_conditions =
266 root->hash_join().join_predicate->expr->equijoin_conditions;
267 ASSERT_EQ(1, semijoin_conditions.size());
268 EXPECT_EQ("(t3.x = t1.x)", ItemToString(semijoin_conditions[0]));
269
270 AccessPath *semi_outer = root->hash_join().outer;
271 ASSERT_EQ(AccessPath::TABLE_SCAN, semi_outer->type);
272 EXPECT_EQ(m_fake_tables["t1"], semi_outer->table_scan().table);
273
274 AccessPath *semi_inner = root->hash_join().inner;
275 ASSERT_EQ(AccessPath::HASH_JOIN, semi_inner->type);
276 EXPECT_EQ(RelationalExpression::INNER_JOIN,
277 semi_inner->hash_join().join_predicate->expr->type);
278
279 const Mem_root_array<Item_eq_base *> &inner_join_conditions =
280 semi_inner->hash_join().join_predicate->expr->equijoin_conditions;
281 ASSERT_EQ(1, inner_join_conditions.size());
282 EXPECT_EQ("(t2.x = t3.x)", ItemToString(inner_join_conditions[0]));
283
284 AccessPath *first_table_inner = semi_inner->hash_join().outer;
285 ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_inner->type);
286 EXPECT_EQ(m_fake_tables["t2"], first_table_inner->table_scan().table);
287
288 AccessPath *second_table_inner = semi_inner->hash_join().inner;
289 ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_inner->type);
290 EXPECT_EQ(m_fake_tables["t3"], second_table_inner->table_scan().table);
291 }
292
293 // Test outer join
294 TEST_F(ConnectJoinTest, OuterJoin) {
295 Query_block *query_block = ParseAndResolve(
296 "SELECT 1 FROM t1 JOIN t2 ON t1.x = t2.x LEFT JOIN t3 "
297 "ON t2.x=t3.x",
298 /*nullable=*/true);
299
300 query_block->join->const_tables = 0;
301 query_block->join->primary_tables = query_block->join->tables = 3;
302
303 // Set up the plan for three table join of the above query.
304 // Plan would be t1 JOIN t2 LEFT JOIN t3.
305 vector<Table> tables;
306 tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
307 tables.push_back(Table("t2", /*plan_idx=*/1, /*prefix_tables=*/0b011));
308 tables.push_back(Table("t3", /*plan_idx=*/2, /*prefix_tables=*/0b111));
309 SetUpJoinTabs(query_block, /*num_tables=*/3, tables);
310 // Setup outer join info
311 // first inner table for including outer join
312 query_block->join->join_tab[2].set_first_inner(2);
313 // last table for embedding outer join
314 query_block->join->join_tab[2].set_last_inner(2);
315 // first inner table for embedding outer join
316 query_block->join->join_tab[2].set_first_upper(1);
317
318 // Set up QEP_TABs now
319 SetUpQEPTabs(query_block, /*num_tables=*/3, tables);
320 query_block->join->qep_tab[2].set_first_inner(2);
321 query_block->join->qep_tab[2].set_last_inner(2);
322 query_block->join->qep_tab[2].set_first_upper(1);
323
324 COND_EQUAL *cond_equal = nullptr;
325 // Generate multi-equalities
326 EXPECT_FALSE(optimize_cond(m_thd, query_block->where_cond_ref(), &cond_equal,
327 &query_block->m_table_nest,
328 &query_block->cond_value));
329 JOIN_TAB *map2table[3];
330
331 // Using the multi-equalities and based on the table order picked, create
332 // join conditions for each of the joins.
333 for (unsigned int i = 0; i < query_block->join->primary_tables; i++)
334 map2table[i] = &query_block->join->join_tab[i];
335 Item **where_cond = &query_block->join->where_cond;
336 *where_cond =
337 substitute_for_best_equal_field(m_thd, query_block->join->where_cond,
338 query_block->join->cond_equal, map2table);
339 EXPECT_EQ("(t1.x = t2.x)", ItemToString(*where_cond));
340
341 JOIN *join = query_block->join;
342 // Attach conditions to tables
343 Item *cond = nullptr;
344 for (unsigned int i = 0; i < query_block->join->primary_tables; i++) {
345 JOIN_TAB *join_tab = &query_block->join->join_tab[i];
346 table_map used_tables = join_tab->prefix_tables();
347 table_map current_map = 1ULL << i;
348 cond = make_cond_for_table(m_thd, *where_cond, used_tables, current_map,
349 /*exclude_expensive_cond=*/false);
350 if (cond)
351 join->qep_tab[join_tab->idx()].set_condition(cond);
352 else if (join->qep_tab[join_tab->idx()].condition() !=
353 join_tab->condition())
354 join->qep_tab[join_tab->idx()].set_condition(join_tab->condition());
355 }
356
357 // Add is_not_null_compl condition for the outer join condition attached
358 // to t3
359 Item *outer_join_cond = new (m_thd->mem_root) Item_func_trig_cond(
360 join->qep_tab[2].condition(), /*f - trigger variable=*/nullptr,
361 query_block->join, join->qep_tab[2].first_inner(),
362 Item_func_trig_cond::IS_NOT_NULL_COMPL);
363 outer_join_cond->quick_fix_field();
364 join->qep_tab[2].set_condition(outer_join_cond);
365
366 // Finally create access paths.
367 qep_tab_map unhandled_duplicates = 0;
368 qep_tab_map conditions_depend_on_outer_tables = 0;
369 vector<PendingInvalidator> pending_invalidators;
370
371 AccessPath *root = ConnectJoins(
372 /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
373 /*last_idx=*/3, join->qep_tab, m_thd,
374 /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
375 &pending_invalidators, /*pending_join_conditions=*/nullptr,
376 &unhandled_duplicates, &conditions_depend_on_outer_tables);
377
378 SCOPED_TRACE(PrintQueryPlan(0, root, join,
379 /*is_root_of_join=*/true));
380
381 ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
382 EXPECT_EQ(RelationalExpression::LEFT_JOIN,
383 root->hash_join().join_predicate->expr->type);
384
385 const Mem_root_array<Item_eq_base *> &leftjoin_conditions =
386 root->hash_join().join_predicate->expr->equijoin_conditions;
387 ASSERT_EQ(1, leftjoin_conditions.size());
388 EXPECT_EQ("(t2.x = t3.x)", ItemToString(leftjoin_conditions[0]));
389
390 AccessPath *left_outer = root->hash_join().outer;
391 ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
392 EXPECT_EQ(RelationalExpression::INNER_JOIN,
393 left_outer->hash_join().join_predicate->expr->type);
394
395 const Mem_root_array<Item_eq_base *> &inner_join_conditions =
396 left_outer->hash_join().join_predicate->expr->equijoin_conditions;
397 ASSERT_EQ(1, inner_join_conditions.size());
398 EXPECT_EQ("(t1.x = t2.x)", ItemToString(inner_join_conditions[0]));
399
400 AccessPath *first_table_outer = left_outer->hash_join().outer;
401 ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_outer->type);
402 EXPECT_EQ(m_fake_tables["t2"], first_table_outer->table_scan().table);
403
404 AccessPath *second_table_outer = left_outer->hash_join().inner;
405 ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_outer->type);
406 EXPECT_EQ(m_fake_tables["t1"], second_table_outer->table_scan().table);
407
408 AccessPath *left_inner = root->hash_join().inner;
409 ASSERT_EQ(AccessPath::TABLE_SCAN, left_inner->type);
410 EXPECT_EQ(m_fake_tables["t3"], left_inner->table_scan().table);
411 }
412
413 // Test semijoin with outer join on its inner side
414 TEST_F(ConnectJoinTest, OuterJoinInSemiJoin) {
415 Query_block *query_block = ParseAndResolve(
416 "SELECT 1 FROM t1 WHERE t1.x IN (SELECT t2.x FROM t2 LEFT "
417 "JOIN t3"
418 " ON t2.x=t3.x)",
419 /*nullable=*/true);
420
421 query_block->join->const_tables = 0;
422 query_block->join->primary_tables = query_block->join->tables = 3;
423
424 // Set up the plan for three table join of the above query.
425 // Plan would be t1 SEMIJOIN t2 LEFT JOIN t3.
426 vector<Table> tables;
427 tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
428 tables.push_back(Table("t2", /*plan_idx=*/1, /*prefix_tables=*/0b011));
429 tables.push_back(Table("t3", /*plan_idx=*/2, /*prefix_tables=*/0b111));
430 SetUpJoinTabs(query_block, /*num_tables=*/3, tables);
431
432 // Set up QEP_TABs and the outer join info
433 SetUpQEPTabs(query_block, /*num_tables=*/3, tables);
434 query_block->join->qep_tab[2].set_first_inner(2);
435 query_block->join->qep_tab[2].set_last_inner(2);
436 query_block->join->qep_tab[2].set_first_upper(1);
437
438 Item *where_cond = query_block->join->where_cond;
439
440 JOIN *join = query_block->join;
441 // Attach conditions to tables
442 join->qep_tab[1].set_condition(where_cond);
443 join->qep_tab[2].set_condition(query_block->join->join_tab[2].condition());
444
445 // Add is_not_null_compl condition for the outer join condition attached
446 // to t3
447 Item *outer_join_cond = new (m_thd->mem_root) Item_func_trig_cond(
448 join->qep_tab[2].condition(), /* f - trigger variable=*/nullptr,
449 query_block->join, join->qep_tab[2].first_inner(),
450 Item_func_trig_cond::IS_NOT_NULL_COMPL);
451 outer_join_cond->quick_fix_field();
452 join->qep_tab[2].set_condition(outer_join_cond);
453
454 join->qep_tab[2].firstmatch_return = 0;
455
456 // Finally create access paths
457 qep_tab_map unhandled_duplicates = 0;
458 qep_tab_map conditions_depend_on_outer_tables = 0;
459 vector<PendingInvalidator> pending_invalidators;
460
461 AccessPath *root = ConnectJoins(
462 /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
463 /*last_idx=*/3, join->qep_tab, m_thd,
464 /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
465 &pending_invalidators, /*pending_join_conditions=*/nullptr,
466 &unhandled_duplicates, &conditions_depend_on_outer_tables);
467
468 // Verify if everything is as expected
469 SCOPED_TRACE(PrintQueryPlan(0, root, join,
470 /*is_root_of_join=*/true));
471
472 ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
473 EXPECT_EQ(RelationalExpression::SEMIJOIN,
474 root->hash_join().join_predicate->expr->type);
475
476 const Mem_root_array<Item_eq_base *> &semijoin_conditions =
477 root->hash_join().join_predicate->expr->equijoin_conditions;
478 ASSERT_EQ(1, semijoin_conditions.size());
479 EXPECT_EQ("(t1.x = t2.x)", ItemToString(semijoin_conditions[0]));
480
481 AccessPath *semi_outer = root->hash_join().outer;
482 ASSERT_EQ(AccessPath::TABLE_SCAN, semi_outer->type);
483 EXPECT_EQ(m_fake_tables["t1"], semi_outer->table_scan().table);
484
485 AccessPath *semi_inner = root->hash_join().inner;
486 ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
487 EXPECT_EQ(RelationalExpression::LEFT_JOIN,
488 semi_inner->hash_join().join_predicate->expr->type);
489
490 const Mem_root_array<Item_eq_base *> &outer_join_conditions =
491 semi_inner->hash_join().join_predicate->expr->equijoin_conditions;
492 ASSERT_EQ(1, outer_join_conditions.size());
493 EXPECT_EQ("(t2.x = t3.x)", ItemToString(outer_join_conditions[0]));
494
495 AccessPath *first_table_outer = semi_inner->hash_join().outer;
496 ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_outer->type);
497 EXPECT_EQ(m_fake_tables["t2"], first_table_outer->table_scan().table);
498
499 AccessPath *second_table_outer = semi_inner->hash_join().inner;
500 ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_outer->type);
501 EXPECT_EQ(m_fake_tables["t3"], second_table_outer->table_scan().table);
502 }
503
504 // Test semijoin within outer join.
505 TEST_F(ConnectJoinTest, SemiJoinInOuterJoin) {
506 Query_block *query_block = ParseAndResolve(
507 "SELECT 1 FROM t1 LEFT JOIN t2 ON t1.x = t2.x WHERE "
508 "t2.x IN (SELECT t3.x FROM t3)",
509 /*nullable=*/true);
510
511 query_block->join->const_tables = 0;
512 query_block->join->primary_tables = query_block->join->tables = 3;
513
514 // Set up the plan for three table join of the above query.
515 // Plan would be t1 LEFT JOIN t2 SEMI JOIN t3.
516 vector<Table> tables;
517 tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
518 tables.push_back(Table("t2", /*plan_idx=*/2, /*prefix_tables=*/0b111));
519 tables.push_back(Table("t3", /*plan_idx=*/1, /*prefix_tables=*/0b101));
520 SetUpJoinTabs(query_block, /*num_tables=*/3, tables);
521
522 tables.clear();
523 tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b001));
524 tables.push_back(Table("t3", /*plan_idx=*/1, /*prefix_tables=*/0b011));
525 tables.push_back(Table("t2", /*plan_idx=*/2, /*prefix_tables=*/0b111));
526 SetUpQEPTabs(query_block, /*num_tables=*/3, tables);
527
528 // Setup outer join info
529 query_block->join->qep_tab[1].set_first_inner(1);
530 query_block->join->qep_tab[1].set_last_inner(2);
531 query_block->join->qep_tab[1].set_first_upper(0);
532
533 COND_EQUAL *cond_equal = nullptr;
534 // Generate multi-equalities
535 EXPECT_FALSE(optimize_cond(m_thd, query_block->where_cond_ref(), &cond_equal,
536 &query_block->m_table_nest,
537 &query_block->cond_value));
538 JOIN_TAB *map2table[3];
539
540 // Using the multi-equalities and based on the table order picked, create
541 // join conditions for each of the joins.
542 for (unsigned int i = 0; i < query_block->join->primary_tables; i++)
543 map2table[i] = &query_block->join->join_tab[i];
544 Item **where_cond = &query_block->join->where_cond;
545 *where_cond =
546 substitute_for_best_equal_field(m_thd, query_block->join->where_cond,
547 query_block->join->cond_equal, map2table);
548 EXPECT_EQ("((t3.x = t1.x) and (t2.x = t1.x))", ItemToString(*where_cond));
549
550 JOIN *join = query_block->join;
551 Item *cond = nullptr;
552 for (unsigned int i = 0; i < query_block->join->primary_tables; i++) {
553 JOIN_TAB *join_tab = &query_block->join->join_tab[i];
554 table_map used_tables = join_tab->prefix_tables();
555 table_map current_map = 1ULL << i;
556 cond = make_cond_for_table(m_thd, *where_cond, used_tables, current_map,
557 /*exclude_expensive_cond=*/false);
558 if (cond) join->qep_tab[join_tab->idx()].set_condition(cond);
559 }
560
561 // Add is_not_null_compl condition for the outer join condition attached
562 // to t3
563 Item *first_outer_join_cond = new (m_thd->mem_root) Item_func_trig_cond(
564 join->qep_tab[1].condition(), nullptr, query_block->join,
565 join->qep_tab[1].first_inner(), Item_func_trig_cond::IS_NOT_NULL_COMPL);
566 first_outer_join_cond->quick_fix_field();
567 join->qep_tab[1].set_condition(first_outer_join_cond);
568
569 Item *second_outer_join_cond = new (m_thd->mem_root) Item_func_trig_cond(
570 join->qep_tab[2].condition(), nullptr, query_block->join,
571 join->qep_tab[2].first_inner(), Item_func_trig_cond::IS_NOT_NULL_COMPL);
572 second_outer_join_cond->quick_fix_field();
573 join->qep_tab[2].set_condition(second_outer_join_cond);
574
575 join->qep_tab[2].firstmatch_return = 1;
576
577 // Create access paths.
578 qep_tab_map unhandled_duplicates = 0;
579 qep_tab_map conditions_depend_on_outer_tables = 0;
580 vector<PendingInvalidator> pending_invalidators;
581
582 AccessPath *root = ConnectJoins(
583 /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
584 /*last_idx=*/3, join->qep_tab, m_thd,
585 /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
586 &pending_invalidators, /*pending_join_conditions=*/nullptr,
587 &unhandled_duplicates, &conditions_depend_on_outer_tables);
588
589 SCOPED_TRACE(PrintQueryPlan(0, root, join,
590 /*is_root_of_join=*/true));
591
592 ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
593 EXPECT_EQ(RelationalExpression::LEFT_JOIN,
594 root->hash_join().join_predicate->expr->type);
595
596 const Mem_root_array<Item_eq_base *> &leftjoin_conditions =
597 root->hash_join().join_predicate->expr->equijoin_conditions;
598 ASSERT_EQ(1, leftjoin_conditions.size());
599 EXPECT_EQ("(t3.x = t1.x)", ItemToString(leftjoin_conditions[0]));
600
601 AccessPath *left_outer = root->hash_join().outer;
602 ASSERT_EQ(AccessPath::TABLE_SCAN, left_outer->type);
603 EXPECT_EQ(m_fake_tables["t1"], left_outer->table_scan().table);
604
605 AccessPath *left_inner = root->hash_join().inner;
606 ASSERT_EQ(AccessPath::HASH_JOIN, left_inner->type);
607 EXPECT_EQ(RelationalExpression::SEMIJOIN,
608 left_inner->hash_join().join_predicate->expr->type);
609
610 const Mem_root_array<Item_eq_base *> &semi_join_conditions =
611 left_inner->hash_join().join_predicate->expr->equijoin_conditions;
612 ASSERT_EQ(1, semi_join_conditions.size());
613 EXPECT_EQ("(t2.x = t3.x)", ItemToString(semi_join_conditions[0]));
614
615 AccessPath *first_table_semi = left_inner->hash_join().outer;
616 ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_semi->type);
617 EXPECT_EQ(m_fake_tables["t3"], first_table_semi->table_scan().table);
618
619 AccessPath *second_table_semi = left_inner->hash_join().inner;
620 ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_semi->type);
621 EXPECT_EQ(m_fake_tables["t2"], second_table_semi->table_scan().table);
622 }
623
624 // We test a semijoin having multiple equalites and a non-equal function
625 TEST_F(ConnectJoinTest, SemiJoinWithNotEqual) {
626 Query_block *query_block = ParseAndResolve(
627 "SELECT 1 FROM t1 WHERE t1.x IN (SELECT t2.x FROM t2 JOIN t3 "
628 "ON t2.x=t3.x JOIN t4 ON t3.x = t4.x where t3.y != t4.y)",
629 /*nullable=*/true);
630
631 query_block->join->const_tables = 0;
632 query_block->join->primary_tables = query_block->join->tables = 4;
633
634 // Set up the plan for four table join of the above query.
635 // Plan would be t1 SEMIJOIN (t2 JOIN t3 JOIN t4). As the optimizer generates
636 // plan for an NLJ, the table order would be t1->t4->t3->t2
637 // JOIN_TAB indexing will be based on the position in the table list which is
638 // t1,t2,t3,t4. However the planner would pick - t1,t4,t3,t2
639 vector<Table> tables;
640 tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b0001));
641 tables.push_back(Table("t2", /*plan_idx=*/3, /*prefix_tables=*/0b1111));
642 tables.push_back(Table("t3", /*plan_idx=*/2, /*prefix_tables=*/0b1101));
643 tables.push_back(Table("t4", /*plan_idx=*/1, /*prefix_tables=*/0b1001));
644 SetUpJoinTabs(query_block, /*num_tables=*/4, tables);
645
646 // As mentioned before, the planner at the end would pick - t1,t4,t3,t2
647 // QEP_TAB indexing is based on the final plan and not the pos_in_table_list.
648 tables.clear();
649 tables.push_back(Table("t1", /*plan_idx=*/0, /*prefix_tables=*/0b0001));
650 tables.push_back(Table("t4", /*plan_idx=*/1, /*prefix_tables=*/0b0011));
651 tables.push_back(Table("t3", /*plan_idx=*/2, /*prefix_tables=*/0b0111));
652 tables.push_back(Table("t2", /*plan_idx=*/3, /*prefix_tables=*/0b1111));
653 SetUpQEPTabs(query_block, /*num_tables*/ 4, tables);
654
655 COND_EQUAL *cond_equal = nullptr;
656 // Generate multi-equalities.
657 EXPECT_FALSE(optimize_cond(m_thd, query_block->where_cond_ref(), &cond_equal,
658 &query_block->m_table_nest,
659 &query_block->cond_value));
660 JOIN_TAB *map2table[4];
661
662 // Using the multi-equalities and based on the table order picked, create
663 // join conditions.
664 for (unsigned int i = 0; i < query_block->join->primary_tables; i++)
665 map2table[i] = &query_block->join->join_tab[i];
666 Item **where_cond = &query_block->join->where_cond;
667 *where_cond =
668 substitute_for_best_equal_field(m_thd, query_block->join->where_cond,
669 query_block->join->cond_equal, map2table);
670 EXPECT_EQ(
671 "((t4.x = t1.x) and (t3.x = t1.x) and (t2.x = t1.x) and (t3.y <> t4.y))",
672 ItemToString(*where_cond));
673
674 JOIN *join = query_block->join;
675 // Attach conditions to tables.
676 Item *cond = nullptr;
677 for (unsigned int i = 0; i < query_block->join->primary_tables; i++) {
678 JOIN_TAB *join_tab = &query_block->join->join_tab[i];
679 table_map used_tables = join_tab->prefix_tables();
680 table_map current_map = 1ULL << i;
681 cond = make_cond_for_table(m_thd, *where_cond, used_tables, current_map,
682 /*exclude_expensive_cond=*/false);
683 if (cond) join->qep_tab[join_tab->idx()].set_condition(cond);
684 }
685
686 // Setup semijoin path.
687 join->qep_tab[3].firstmatch_return = 0;
688
689 // Finally create access paths.
690 qep_tab_map unhandled_duplicates = 0;
691 qep_tab_map conditions_depend_on_outer_tables = 0;
692 vector<PendingInvalidator> pending_invalidators;
693
694 AccessPath *root = ConnectJoins(
695 /*upper_first_idx=*/NO_PLAN_IDX, /*first_idx=*/0,
696 /*last_idx=*/4, join->qep_tab, m_thd,
697 /*calling_context=*/TOP_LEVEL, /*pending_conditions=*/nullptr,
698 &pending_invalidators, /*pending_join_conditions=*/nullptr,
699 &unhandled_duplicates, &conditions_depend_on_outer_tables);
700
701 SCOPED_TRACE(PrintQueryPlan(0, root, join,
702 /*is_root_of_join=*/true));
703
704 // Verify that we have t1 hash-semijoin (t2 hash join (t3 hash join t4
705 // on t3.x = t4.x (with filter on t3.y != t4.y)) on t2.x = t4.x)
706 // on t1.x = t4.x)
707 // Earlier filter would not be placed with the inner join between
708 // t3 and t4
709 ASSERT_EQ(AccessPath::HASH_JOIN, root->type);
710 EXPECT_EQ(RelationalExpression::SEMIJOIN,
711 root->hash_join().join_predicate->expr->type);
712
713 const Mem_root_array<Item_eq_base *> &semijoin_conditions =
714 root->hash_join().join_predicate->expr->equijoin_conditions;
715 ASSERT_EQ(1, semijoin_conditions.size());
716 EXPECT_EQ("(t4.x = t1.x)", ItemToString(semijoin_conditions[0]));
717
718 AccessPath *semi_outer = root->hash_join().outer;
719 ASSERT_EQ(AccessPath::TABLE_SCAN, semi_outer->type);
720 EXPECT_EQ(m_fake_tables["t1"], semi_outer->table_scan().table);
721
722 AccessPath *semi_inner = root->hash_join().inner;
723 ASSERT_EQ(AccessPath::HASH_JOIN, semi_inner->type);
724 EXPECT_EQ(RelationalExpression::INNER_JOIN,
725 semi_inner->hash_join().join_predicate->expr->type);
726
727 const Mem_root_array<Item_eq_base *> &inner_join_conditions =
728 semi_inner->hash_join().join_predicate->expr->equijoin_conditions;
729 ASSERT_EQ(1, inner_join_conditions.size());
730 EXPECT_EQ("(t2.x = t4.x)", ItemToString(inner_join_conditions[0]));
731
732 AccessPath *first_table_inner = semi_inner->hash_join().outer;
733 ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_inner->type);
734 EXPECT_EQ(m_fake_tables["t2"], first_table_inner->table_scan().table);
735
736 AccessPath *second_table_inner = semi_inner->hash_join().inner;
737 ASSERT_EQ(AccessPath::FILTER, second_table_inner->type);
738 EXPECT_EQ("(t3.y <> t4.y)",
739 ItemToString(second_table_inner->filter().condition));
740
741 AccessPath *filter_child = second_table_inner->filter().child;
742 ASSERT_EQ(AccessPath::HASH_JOIN, filter_child->type);
743 EXPECT_EQ(RelationalExpression::INNER_JOIN,
744 filter_child->hash_join().join_predicate->expr->type);
745
746 const Mem_root_array<Item_eq_base *> &below_filter_inner_join_conditions =
747 filter_child->hash_join().join_predicate->expr->equijoin_conditions;
748 ASSERT_EQ(1, below_filter_inner_join_conditions.size());
749 EXPECT_EQ("(t3.x = t4.x)",
750 ItemToString(below_filter_inner_join_conditions[0]));
751
752 AccessPath *first_table_inner_to_filter = filter_child->hash_join().outer;
753 ASSERT_EQ(AccessPath::TABLE_SCAN, first_table_inner_to_filter->type);
754 EXPECT_EQ(m_fake_tables["t3"],
755 first_table_inner_to_filter->table_scan().table);
756
757 AccessPath *second_table_inner_to_filter = filter_child->hash_join().inner;
758 ASSERT_EQ(AccessPath::TABLE_SCAN, second_table_inner_to_filter->type);
759 EXPECT_EQ(m_fake_tables["t4"],
760 second_table_inner_to_filter->table_scan().table);
761 }