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
|
/* 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 <initializer_list>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "my_alloc.h" // MEM_ROOT
#include "sql/join_optimizer/access_path.h"
#include "sql/join_optimizer/walk_access_paths.h"
#include "sql/mem_root_array.h"
#include "sql/table.h"
class JOIN;
using std::vector;
using testing::ElementsAre;
namespace {
AccessPath MakeNestedLoopJoin(AccessPath *outer, AccessPath *inner) {
AccessPath join;
join.type = AccessPath::NESTED_LOOP_JOIN;
join.nested_loop_join().outer = outer;
join.nested_loop_join().inner = inner;
return join;
}
AccessPath MakeHashJoin(AccessPath *outer, AccessPath *inner) {
AccessPath join;
join.type = AccessPath::HASH_JOIN;
join.hash_join().outer = outer;
join.hash_join().inner = inner;
return join;
}
AccessPath MakeTableScan(TABLE *table = nullptr) {
AccessPath path;
path.type = AccessPath::TABLE_SCAN;
path.table_scan().table = table;
return path;
}
AccessPath MakeZeroRows(AccessPath *child) {
AccessPath path;
path.type = AccessPath::ZERO_ROWS;
path.zero_rows().child = child;
return path;
}
AccessPath MakeAppend(MEM_ROOT *mem_root, AccessPath *c1, AccessPath *c2) {
AccessPath path;
path.type = AccessPath::APPEND;
Mem_root_array<AppendPathParameters> *params =
new (mem_root) Mem_root_array<AppendPathParameters>(mem_root);
AppendPathParameters param1, param2;
param1.path = c1;
param2.path = c2;
params->push_back(param1);
params->push_back(param2);
path.append().children = params;
return path;
}
} // namespace
namespace walk_access_paths_test {
TEST(WalkAccessPathsTest, PreOrderTraversal) {
/*
* Set up this access path tree:
*
* NLJ1
* / \
* NLJ2 NLJ3
* / \ / \
* HJ1 TS3 TS4 HJ2
* / \ / \
* TS1 TS2 TS5 TS6
*/
AccessPath ts1 = MakeTableScan();
AccessPath ts2 = MakeTableScan();
AccessPath ts3 = MakeTableScan();
AccessPath ts4 = MakeTableScan();
AccessPath ts5 = MakeTableScan();
AccessPath ts6 = MakeTableScan();
AccessPath hj1 = MakeHashJoin(&ts1, &ts2);
AccessPath hj2 = MakeHashJoin(&ts5, &ts6);
AccessPath nlj2 = MakeNestedLoopJoin(&hj1, &ts3);
AccessPath nlj3 = MakeNestedLoopJoin(&ts4, &hj2);
AccessPath nlj1 = MakeNestedLoopJoin(&nlj2, &nlj3);
vector<AccessPath *> paths;
WalkAccessPaths(&nlj1, nullptr, WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
[&paths](AccessPath *path, const JOIN *) {
paths.push_back(path);
// Skip subtrees under a hash join.
return path->type == AccessPath::HASH_JOIN;
});
// Expect the tree to have been walked depth-first and pre-order, and
// everything below a hash join path was skipped.
EXPECT_THAT(paths, ElementsAre(&nlj1, &nlj2, &hj1, &ts3, &nlj3, &ts4, &hj2));
}
TEST(WalkAccessPathsTest, PostOrderTraversal) {
/*
* Set up this access path tree:
*
* NLJ1
* / \
* NLJ2 NLJ3
* / \ / \
* HJ1 TS3 TS4 HJ2
* / \ / \
* TS1 TS2 TS5 TS6
*/
AccessPath ts1 = MakeTableScan();
AccessPath ts2 = MakeTableScan();
AccessPath ts3 = MakeTableScan();
AccessPath ts4 = MakeTableScan();
AccessPath ts5 = MakeTableScan();
AccessPath ts6 = MakeTableScan();
AccessPath hj1 = MakeHashJoin(&ts1, &ts2);
AccessPath hj2 = MakeHashJoin(&ts5, &ts6);
AccessPath nlj2 = MakeNestedLoopJoin(&hj1, &ts3);
AccessPath nlj3 = MakeNestedLoopJoin(&ts4, &hj2);
AccessPath nlj1 = MakeNestedLoopJoin(&nlj2, &nlj3);
vector<AccessPath *> paths;
WalkAccessPaths(
&nlj1, /*join=*/nullptr, WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
[&paths](AccessPath *path, const JOIN *) {
paths.push_back(path);
// The return value is ignored when doing post-order traversal.
return path->type == AccessPath::HASH_JOIN;
},
/*post_order_traversal=*/true);
// Expect the tree to have been walked depth-first and post-order. No subtrees
// are cut off for post-order traversal, so we see all paths. (Because we've
// already finished processing the subtree when the functor is called, its
// returning true does not prevent us recursing into the subtree.)
EXPECT_THAT(paths, ElementsAre(&ts1, &ts2, &hj1, &ts3, &nlj2, &ts4, &ts5,
&ts6, &hj2, &nlj3, &nlj1));
}
TEST(WalkAccessPathsTest, ZeroRows) {
/*
* Set up this access path tree:
*
* NLJ1
* / \
* NLJ2 NLJ3
* / \ / \
* TS1 ZERO TS2 TS3
* |
* NLJ4
* / \
* TS4 TS5
*/
TABLE t1;
TABLE t2;
TABLE t3;
TABLE t4;
TABLE t5;
AccessPath ts1 = MakeTableScan(&t1);
AccessPath ts2 = MakeTableScan(&t2);
AccessPath ts3 = MakeTableScan(&t3);
AccessPath ts4 = MakeTableScan(&t4);
AccessPath ts5 = MakeTableScan(&t5);
AccessPath nlj4 = MakeNestedLoopJoin(&ts4, &ts5);
AccessPath zero = MakeZeroRows(&nlj4);
AccessPath nlj2 = MakeNestedLoopJoin(&ts1, &zero);
AccessPath nlj3 = MakeNestedLoopJoin(&ts2, &ts3);
AccessPath nlj1 = MakeNestedLoopJoin(&nlj2, &nlj3);
// WalkAccessPaths() should not see the paths below the ZERO_ROWS access path.
{
vector<AccessPath *> paths;
WalkAccessPaths(
&nlj1, /*join=*/nullptr, WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
[&paths](AccessPath *path, const JOIN *) {
paths.push_back(path);
// The return value is ignored when doing post-order traversal.
return path->type == AccessPath::HASH_JOIN;
},
/*post_order_traversal=*/false);
EXPECT_THAT(paths,
ElementsAre(&nlj1, &nlj2, &ts1, &zero, &nlj3, &ts2, &ts3));
}
// WalkTablesUnderAccessPath() should see all tables when called with
// include_pruned_tables = true.
{
vector<TABLE *> tables;
WalkTablesUnderAccessPath(
&nlj1,
[&tables](TABLE *table) {
tables.push_back(table);
return false;
},
/*include_pruned_tables=*/true);
EXPECT_THAT(tables, ElementsAre(&t1, &t4, &t5, &t2, &t3));
}
// WalkTablesUnderAccessPath() should not see tables under ZERO_ROWS when
// called with include_pruned_tables = false.
{
vector<TABLE *> tables;
WalkTablesUnderAccessPath(
&nlj1,
[&tables](TABLE *table) {
tables.push_back(table);
return false;
},
/*include_pruned_tables=*/false);
EXPECT_THAT(tables, ElementsAre(&t1, &t2, &t3));
}
}
TEST(WalkAccessPathsTest, ZeroRowsNoChild) {
AccessPath zero_path = MakeZeroRows(/*child=*/nullptr);
vector<AccessPath *> paths;
WalkAccessPaths(&zero_path, /*join=*/nullptr,
WalkAccessPathPolicy::ENTIRE_TREE,
[&paths](AccessPath *path, const JOIN *) {
paths.push_back(path);
return false;
});
EXPECT_THAT(paths, ElementsAre(&zero_path));
for (bool include_pruned_tables : {true, false}) {
WalkTablesUnderAccessPath(
&zero_path,
[](TABLE *) {
EXPECT_TRUE(false);
return true;
},
include_pruned_tables);
}
}
TEST(WalkAccessPathsTest, Append) {
/*
* Set up this access path tree:
*
* APPEND
* / \
* TS1 TS2
*/
TABLE t1;
TABLE t2;
AccessPath ts1 = MakeTableScan(&t1);
AccessPath ts2 = MakeTableScan(&t2);
MEM_ROOT mem_root(PSI_NOT_INSTRUMENTED, 1024);
AccessPath append = MakeAppend(&mem_root, &ts1, &ts2);
vector<AccessPath *> paths;
WalkAccessPaths(&append, /*join=*/nullptr, WalkAccessPathPolicy::ENTIRE_TREE,
[&paths](AccessPath *path, const JOIN *) {
paths.push_back(path);
return false;
});
EXPECT_THAT(paths, ElementsAre(&append, &ts1, &ts2));
paths.clear();
WalkAccessPaths(&append, /*join=*/nullptr,
WalkAccessPathPolicy::STOP_AT_MATERIALIZATION,
[&paths](AccessPath *path, const JOIN *) {
paths.push_back(path);
return false;
});
EXPECT_THAT(paths, ElementsAre(&append));
}
TEST(WalkAccessPathsTest, TemptableAggregate) {
AccessPath ts1 = MakeTableScan();
AccessPath ts2 = MakeTableScan();
AccessPath temptable_aggregate;
temptable_aggregate.type = AccessPath::TEMPTABLE_AGGREGATE;
temptable_aggregate.temptable_aggregate().subquery_path = &ts1;
temptable_aggregate.temptable_aggregate().table_path = &ts2;
vector<AccessPath *> paths;
WalkAccessPaths(&temptable_aggregate, /*join=*/nullptr,
WalkAccessPathPolicy::ENTIRE_TREE,
[&paths](AccessPath *path, const JOIN *) {
paths.push_back(path);
return false;
});
EXPECT_THAT(paths, ElementsAre(&temptable_aggregate, &ts1, &ts2));
}
TEST(WalkAccessPathsTest, PushedJoinRef) {
TABLE t1;
AccessPath pushed_join_ref;
pushed_join_ref.type = AccessPath::PUSHED_JOIN_REF;
pushed_join_ref.pushed_join_ref().table = &t1;
for (bool include_pruned_tables : {true, false}) {
vector<TABLE *> tables;
WalkTablesUnderAccessPath(
&pushed_join_ref,
[&tables](TABLE *table) {
tables.push_back(table);
return false;
},
include_pruned_tables);
EXPECT_THAT(tables, ElementsAre(&t1));
}
}
} // namespace walk_access_paths_test
|