| 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 <initializer_list> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <gmock/gmock.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | #include "my_alloc.h" // MEM_ROOT |
| 31 | #include "sql/join_optimizer/access_path.h" |
| 32 | #include "sql/join_optimizer/walk_access_paths.h" |
| 33 | #include "sql/mem_root_array.h" |
| 34 | #include "sql/table.h" |
| 35 | |
| 36 | class JOIN; |
| 37 | |
| 38 | using std::vector; |
| 39 | using testing::ElementsAre; |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | AccessPath MakeNestedLoopJoin(AccessPath *outer, AccessPath *inner) { |
| 44 | AccessPath join; |
| 45 | join.type = AccessPath::NESTED_LOOP_JOIN; |
| 46 | join.nested_loop_join().outer = outer; |
| 47 | join.nested_loop_join().inner = inner; |
| 48 | return join; |
| 49 | } |
| 50 | |
| 51 | AccessPath MakeHashJoin(AccessPath *outer, AccessPath *inner) { |
| 52 | AccessPath join; |
| 53 | join.type = AccessPath::HASH_JOIN; |
| 54 | join.hash_join().outer = outer; |
| 55 | join.hash_join().inner = inner; |
| 56 | return join; |
| 57 | } |
| 58 | |
| 59 | AccessPath MakeTableScan(TABLE *table = nullptr) { |
| 60 | AccessPath path; |
| 61 | path.type = AccessPath::TABLE_SCAN; |
| 62 | path.table_scan().table = table; |
| 63 | return path; |
| 64 | } |
| 65 | |
| 66 | AccessPath MakeZeroRows(AccessPath *child) { |
| 67 | AccessPath path; |
| 68 | path.type = AccessPath::ZERO_ROWS; |
| 69 | path.zero_rows().child = child; |
| 70 | return path; |
| 71 | } |
| 72 | |
| 73 | AccessPath MakeAppend(MEM_ROOT *mem_root, AccessPath *c1, AccessPath *c2) { |
| 74 | AccessPath path; |
| 75 | path.type = AccessPath::APPEND; |
| 76 | |
| 77 | Mem_root_array<AppendPathParameters> *params = |
| 78 | new (mem_root) Mem_root_array<AppendPathParameters>(mem_root); |
| 79 | AppendPathParameters param1, param2; |
| 80 | param1.path = c1; |
| 81 | param2.path = c2; |
| 82 | params->push_back(param1); |
| 83 | params->push_back(param2); |
| 84 | |
| 85 | path.append().children = params; |
| 86 | return path; |
| 87 | } |
| 88 | |
| 89 | } // namespace |
| 90 | |
| 91 | namespace walk_access_paths_test { |
| 92 | |
| 93 | TEST(WalkAccessPathsTest, PreOrderTraversal) { |
| 94 | /* |
| 95 | * Set up this access path tree: |
| 96 | * |
| 97 | * NLJ1 |
| 98 | * / \ |
| 99 | * NLJ2 NLJ3 |
| 100 | * / \ / \ |
| 101 | * HJ1 TS3 TS4 HJ2 |
| 102 | * / \ / \ |
| 103 | * TS1 TS2 TS5 TS6 |
| 104 | */ |
| 105 | |
| 106 | AccessPath ts1 = MakeTableScan(); |
| 107 | AccessPath ts2 = MakeTableScan(); |
| 108 | AccessPath ts3 = MakeTableScan(); |
| 109 | AccessPath ts4 = MakeTableScan(); |
| 110 | AccessPath ts5 = MakeTableScan(); |
| 111 | AccessPath ts6 = MakeTableScan(); |
| 112 | |
| 113 | AccessPath hj1 = MakeHashJoin(&ts1, &ts2); |
| 114 | AccessPath hj2 = MakeHashJoin(&ts5, &ts6); |
| 115 | |
| 116 | AccessPath nlj2 = MakeNestedLoopJoin(&hj1, &ts3); |
| 117 | AccessPath nlj3 = MakeNestedLoopJoin(&ts4, &hj2); |
| 118 | AccessPath nlj1 = MakeNestedLoopJoin(&nlj2, &nlj3); |
| 119 | |
| 120 | vector<AccessPath *> paths; |
| 121 | WalkAccessPaths(&nlj1, nullptr, WalkAccessPathPolicy::STOP_AT_MATERIALIZATION, |
| 122 | [&paths](AccessPath *path, const JOIN *) { |
| 123 | paths.push_back(path); |
| 124 | // Skip subtrees under a hash join. |
| 125 | return path->type == AccessPath::HASH_JOIN; |
| 126 | }); |
| 127 | |
| 128 | // Expect the tree to have been walked depth-first and pre-order, and |
| 129 | // everything below a hash join path was skipped. |
| 130 | EXPECT_THAT(paths, ElementsAre(&nlj1, &nlj2, &hj1, &ts3, &nlj3, &ts4, &hj2)); |
| 131 | } |
| 132 | |
| 133 | TEST(WalkAccessPathsTest, PostOrderTraversal) { |
| 134 | /* |
| 135 | * Set up this access path tree: |
| 136 | * |
| 137 | * NLJ1 |
| 138 | * / \ |
| 139 | * NLJ2 NLJ3 |
| 140 | * / \ / \ |
| 141 | * HJ1 TS3 TS4 HJ2 |
| 142 | * / \ / \ |
| 143 | * TS1 TS2 TS5 TS6 |
| 144 | */ |
| 145 | |
| 146 | AccessPath ts1 = MakeTableScan(); |
| 147 | AccessPath ts2 = MakeTableScan(); |
| 148 | AccessPath ts3 = MakeTableScan(); |
| 149 | AccessPath ts4 = MakeTableScan(); |
| 150 | AccessPath ts5 = MakeTableScan(); |
| 151 | AccessPath ts6 = MakeTableScan(); |
| 152 | |
| 153 | AccessPath hj1 = MakeHashJoin(&ts1, &ts2); |
| 154 | AccessPath hj2 = MakeHashJoin(&ts5, &ts6); |
| 155 | |
| 156 | AccessPath nlj2 = MakeNestedLoopJoin(&hj1, &ts3); |
| 157 | AccessPath nlj3 = MakeNestedLoopJoin(&ts4, &hj2); |
| 158 | AccessPath nlj1 = MakeNestedLoopJoin(&nlj2, &nlj3); |
| 159 | |
| 160 | vector<AccessPath *> paths; |
| 161 | WalkAccessPaths( |
| 162 | &nlj1, /*join=*/nullptr, WalkAccessPathPolicy::STOP_AT_MATERIALIZATION, |
| 163 | [&paths](AccessPath *path, const JOIN *) { |
| 164 | paths.push_back(path); |
| 165 | // The return value is ignored when doing post-order traversal. |
| 166 | return path->type == AccessPath::HASH_JOIN; |
| 167 | }, |
| 168 | /*post_order_traversal=*/true); |
| 169 | |
| 170 | // Expect the tree to have been walked depth-first and post-order. No subtrees |
| 171 | // are cut off for post-order traversal, so we see all paths. (Because we've |
| 172 | // already finished processing the subtree when the functor is called, its |
| 173 | // returning true does not prevent us recursing into the subtree.) |
| 174 | EXPECT_THAT(paths, ElementsAre(&ts1, &ts2, &hj1, &ts3, &nlj2, &ts4, &ts5, |
| 175 | &ts6, &hj2, &nlj3, &nlj1)); |
| 176 | } |
| 177 | |
| 178 | TEST(WalkAccessPathsTest, ZeroRows) { |
| 179 | /* |
| 180 | * Set up this access path tree: |
| 181 | * |
| 182 | * NLJ1 |
| 183 | * / \ |
| 184 | * NLJ2 NLJ3 |
| 185 | * / \ / \ |
| 186 | * TS1 ZERO TS2 TS3 |
| 187 | * | |
| 188 | * NLJ4 |
| 189 | * / \ |
| 190 | * TS4 TS5 |
| 191 | */ |
| 192 | |
| 193 | TABLE t1; |
| 194 | TABLE t2; |
| 195 | TABLE t3; |
| 196 | TABLE t4; |
| 197 | TABLE t5; |
| 198 | |
| 199 | AccessPath ts1 = MakeTableScan(&t1); |
| 200 | AccessPath ts2 = MakeTableScan(&t2); |
| 201 | AccessPath ts3 = MakeTableScan(&t3); |
| 202 | AccessPath ts4 = MakeTableScan(&t4); |
| 203 | AccessPath ts5 = MakeTableScan(&t5); |
| 204 | |
| 205 | AccessPath nlj4 = MakeNestedLoopJoin(&ts4, &ts5); |
| 206 | AccessPath zero = MakeZeroRows(&nlj4); |
| 207 | AccessPath nlj2 = MakeNestedLoopJoin(&ts1, &zero); |
| 208 | AccessPath nlj3 = MakeNestedLoopJoin(&ts2, &ts3); |
| 209 | AccessPath nlj1 = MakeNestedLoopJoin(&nlj2, &nlj3); |
| 210 | |
| 211 | // WalkAccessPaths() should not see the paths below the ZERO_ROWS access path. |
| 212 | { |
| 213 | vector<AccessPath *> paths; |
| 214 | WalkAccessPaths( |
| 215 | &nlj1, /*join=*/nullptr, WalkAccessPathPolicy::STOP_AT_MATERIALIZATION, |
| 216 | [&paths](AccessPath *path, const JOIN *) { |
| 217 | paths.push_back(path); |
| 218 | // The return value is ignored when doing post-order traversal. |
| 219 | return path->type == AccessPath::HASH_JOIN; |
| 220 | }, |
| 221 | /*post_order_traversal=*/false); |
| 222 | EXPECT_THAT(paths, |
| 223 | ElementsAre(&nlj1, &nlj2, &ts1, &zero, &nlj3, &ts2, &ts3)); |
| 224 | } |
| 225 | |
| 226 | // WalkTablesUnderAccessPath() should see all tables when called with |
| 227 | // include_pruned_tables = true. |
| 228 | { |
| 229 | vector<TABLE *> tables; |
| 230 | WalkTablesUnderAccessPath( |
| 231 | &nlj1, |
| 232 | [&tables](TABLE *table) { |
| 233 | tables.push_back(table); |
| 234 | return false; |
| 235 | }, |
| 236 | /*include_pruned_tables=*/true); |
| 237 | EXPECT_THAT(tables, ElementsAre(&t1, &t4, &t5, &t2, &t3)); |
| 238 | } |
| 239 | |
| 240 | // WalkTablesUnderAccessPath() should not see tables under ZERO_ROWS when |
| 241 | // called with include_pruned_tables = false. |
| 242 | { |
| 243 | vector<TABLE *> tables; |
| 244 | WalkTablesUnderAccessPath( |
| 245 | &nlj1, |
| 246 | [&tables](TABLE *table) { |
| 247 | tables.push_back(table); |
| 248 | return false; |
| 249 | }, |
| 250 | /*include_pruned_tables=*/false); |
| 251 | EXPECT_THAT(tables, ElementsAre(&t1, &t2, &t3)); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | TEST(WalkAccessPathsTest, ZeroRowsNoChild) { |
| 256 | AccessPath zero_path = MakeZeroRows(/*child=*/nullptr); |
| 257 | |
| 258 | vector<AccessPath *> paths; |
| 259 | WalkAccessPaths(&zero_path, /*join=*/nullptr, |
| 260 | WalkAccessPathPolicy::ENTIRE_TREE, |
| 261 | [&paths](AccessPath *path, const JOIN *) { |
| 262 | paths.push_back(path); |
| 263 | return false; |
| 264 | }); |
| 265 | EXPECT_THAT(paths, ElementsAre(&zero_path)); |
| 266 | |
| 267 | for (bool include_pruned_tables : {true, false}) { |
| 268 | WalkTablesUnderAccessPath( |
| 269 | &zero_path, |
| 270 | [](TABLE *) { |
| 271 | EXPECT_TRUE(false); |
| 272 | return true; |
| 273 | }, |
| 274 | include_pruned_tables); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | TEST(WalkAccessPathsTest, Append) { |
| 279 | /* |
| 280 | * Set up this access path tree: |
| 281 | * |
| 282 | * APPEND |
| 283 | * / \ |
| 284 | * TS1 TS2 |
| 285 | */ |
| 286 | |
| 287 | TABLE t1; |
| 288 | TABLE t2; |
| 289 | |
| 290 | AccessPath ts1 = MakeTableScan(&t1); |
| 291 | AccessPath ts2 = MakeTableScan(&t2); |
| 292 | |
| 293 | MEM_ROOT mem_root(PSI_NOT_INSTRUMENTED, 1024); |
| 294 | AccessPath append = MakeAppend(&mem_root, &ts1, &ts2); |
| 295 | |
| 296 | vector<AccessPath *> paths; |
| 297 | WalkAccessPaths(&append, /*join=*/nullptr, WalkAccessPathPolicy::ENTIRE_TREE, |
| 298 | [&paths](AccessPath *path, const JOIN *) { |
| 299 | paths.push_back(path); |
| 300 | return false; |
| 301 | }); |
| 302 | EXPECT_THAT(paths, ElementsAre(&append, &ts1, &ts2)); |
| 303 | paths.clear(); |
| 304 | |
| 305 | WalkAccessPaths(&append, /*join=*/nullptr, |
| 306 | WalkAccessPathPolicy::STOP_AT_MATERIALIZATION, |
| 307 | [&paths](AccessPath *path, const JOIN *) { |
| 308 | paths.push_back(path); |
| 309 | return false; |
| 310 | }); |
| 311 | EXPECT_THAT(paths, ElementsAre(&append)); |
| 312 | } |
| 313 | |
| 314 | TEST(WalkAccessPathsTest, TemptableAggregate) { |
| 315 | AccessPath ts1 = MakeTableScan(); |
| 316 | AccessPath ts2 = MakeTableScan(); |
| 317 | AccessPath temptable_aggregate; |
| 318 | temptable_aggregate.type = AccessPath::TEMPTABLE_AGGREGATE; |
| 319 | temptable_aggregate.temptable_aggregate().subquery_path = &ts1; |
| 320 | temptable_aggregate.temptable_aggregate().table_path = &ts2; |
| 321 | |
| 322 | vector<AccessPath *> paths; |
| 323 | WalkAccessPaths(&temptable_aggregate, /*join=*/nullptr, |
| 324 | WalkAccessPathPolicy::ENTIRE_TREE, |
| 325 | [&paths](AccessPath *path, const JOIN *) { |
| 326 | paths.push_back(path); |
| 327 | return false; |
| 328 | }); |
| 329 | EXPECT_THAT(paths, ElementsAre(&temptable_aggregate, &ts1, &ts2)); |
| 330 | } |
| 331 | |
| 332 | TEST(WalkAccessPathsTest, PushedJoinRef) { |
| 333 | TABLE t1; |
| 334 | AccessPath pushed_join_ref; |
| 335 | pushed_join_ref.type = AccessPath::PUSHED_JOIN_REF; |
| 336 | pushed_join_ref.pushed_join_ref().table = &t1; |
| 337 | for (bool include_pruned_tables : {true, false}) { |
| 338 | vector<TABLE *> tables; |
| 339 | WalkTablesUnderAccessPath( |
| 340 | &pushed_join_ref, |
| 341 | [&tables](TABLE *table) { |
| 342 | tables.push_back(table); |
| 343 | return false; |
| 344 | }, |
| 345 | include_pruned_tables); |
| 346 | EXPECT_THAT(tables, ElementsAre(&t1)); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | } // namespace walk_access_paths_test |