| 1 | /* Copyright (c) 2020, 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 <assert.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <bitset> |
| 29 | #include <unordered_set> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include <gmock/gmock.h> |
| 33 | #include "my_compiler.h" |
| 34 | #include "sql/join_optimizer/subgraph_enumeration.h" |
| 35 | #include "unittest/gunit/benchmark.h" |
| 36 | |
| 37 | using ::testing::_; |
| 38 | using ::testing::AnyNumber; |
| 39 | using ::testing::Expectation; |
| 40 | using ::testing::Return; |
| 41 | using ::testing::StrictMock; |
| 42 | |
| 43 | using hypergraph::Hypergraph; |
| 44 | using hypergraph::NodeMap; |
| 45 | using hypergraph::PrintSet; |
| 46 | |
| 47 | class MockReceiver { |
| 48 | public: |
| 49 | MOCK_METHOD1(HasSeen, bool(NodeMap)); |
| 50 | MOCK_METHOD1(FoundSingleNode, bool(int)); |
| 51 | MOCK_METHOD3(FoundSubgraphPair, bool(NodeMap, NodeMap, int)); |
| 52 | }; |
| 53 | |
| 54 | TEST(DPhypTest, ExampleHypergraph) { |
| 55 | MEM_ROOT mem_root; |
| 56 | /* |
| 57 | The example graph from the DPhyp paper. One large |
| 58 | hyperedge and four simple edges. |
| 59 | |
| 60 | R1-. ,-R4 |
| 61 | | \ / | |
| 62 | R2---x---R5 |
| 63 | | / \ | |
| 64 | R3-' `-R6 |
| 65 | */ |
| 66 | Hypergraph g(&mem_root); |
| 67 | g.AddNode(); // R1 |
| 68 | g.AddNode(); // R2 |
| 69 | g.AddNode(); // R3 |
| 70 | g.AddNode(); // R4 |
| 71 | g.AddNode(); // R5 |
| 72 | g.AddNode(); // R6 |
| 73 | g.AddEdge(0b000001, 0b000010); // R1-R2 |
| 74 | g.AddEdge(0b000010, 0b000100); // R2-R3 |
| 75 | g.AddEdge(0b001000, 0b010000); // R4-R5 |
| 76 | g.AddEdge(0b010000, 0b100000); // R5-R6 |
| 77 | g.AddEdge(0b000111, 0b111000); // {R1,R2,R3}-{R4,R5,R6} |
| 78 | |
| 79 | StrictMock<MockReceiver> mr; |
| 80 | EXPECT_CALL(mr, FoundSingleNode(0)); |
| 81 | EXPECT_CALL(mr, FoundSingleNode(1)); |
| 82 | EXPECT_CALL(mr, FoundSingleNode(2)); |
| 83 | EXPECT_CALL(mr, FoundSingleNode(3)); |
| 84 | EXPECT_CALL(mr, FoundSingleNode(4)); |
| 85 | EXPECT_CALL(mr, FoundSingleNode(5)); |
| 86 | |
| 87 | // Fallback matcher. |
| 88 | EXPECT_CALL(mr, HasSeen(_)).WillRepeatedly(Return(false)); |
| 89 | |
| 90 | // Right side of the graph: |
| 91 | |
| 92 | // Found link between R5 and R6. |
| 93 | Expectation seen_r5_r6 = |
| 94 | EXPECT_CALL(mr, FoundSubgraphPair(0b010000, 0b100000, 3)); |
| 95 | EXPECT_CALL(mr, HasSeen(0b110000)) |
| 96 | .After(seen_r5_r6) |
| 97 | .WillRepeatedly(Return(true)); |
| 98 | |
| 99 | // Found link between R4 and R5. |
| 100 | Expectation seen_r4_r5 = |
| 101 | EXPECT_CALL(mr, FoundSubgraphPair(0b001000, 0b010000, 2)); |
| 102 | EXPECT_CALL(mr, HasSeen(0b011000)) |
| 103 | .After(seen_r4_r5) |
| 104 | .WillRepeatedly(Return(true)); |
| 105 | |
| 106 | // Found link between R4 and {R5,R6}, through the R4-R5 edge. |
| 107 | Expectation seen_r4_r5r6 = |
| 108 | EXPECT_CALL(mr, FoundSubgraphPair(0b001000, 0b110000, 2)); |
| 109 | |
| 110 | // Found like between {R4,R5} and {R6}, through the R5-R6 edge. |
| 111 | Expectation seen_r4r5_r6 = |
| 112 | EXPECT_CALL(mr, FoundSubgraphPair(0b011000, 0b100000, 3)); |
| 113 | |
| 114 | // {R4,R5,R6} is connected (called only after we've seen its components). |
| 115 | EXPECT_CALL(mr, HasSeen(0b111000)) |
| 116 | .After(seen_r4_r5r6) |
| 117 | .After(seen_r4r5_r6) |
| 118 | .WillRepeatedly(Return(true)); |
| 119 | |
| 120 | // Very similar, left side of the graph: |
| 121 | |
| 122 | // Found link between R2 and R3. |
| 123 | Expectation seen_r2_r3 = |
| 124 | EXPECT_CALL(mr, FoundSubgraphPair(0b000010, 0b000100, 1)); |
| 125 | EXPECT_CALL(mr, HasSeen(0b000110)) |
| 126 | .After(seen_r2_r3) |
| 127 | .WillRepeatedly(Return(true)); |
| 128 | |
| 129 | // Found link between R1 and R2. |
| 130 | Expectation seen_r1_r2 = |
| 131 | EXPECT_CALL(mr, FoundSubgraphPair(0b000001, 0b000010, 0)); |
| 132 | EXPECT_CALL(mr, HasSeen(0b000011)) |
| 133 | .After(seen_r1_r2) |
| 134 | .WillRepeatedly(Return(true)); |
| 135 | |
| 136 | // Found link between R1 and {R2,R3}, through the R1-R2 edge. |
| 137 | Expectation seen_r1_r2r3 = |
| 138 | EXPECT_CALL(mr, FoundSubgraphPair(0b000001, 0b000110, 0)); |
| 139 | |
| 140 | // Found like between {R1,R2} and {R3}, through the R2-R3 edge. |
| 141 | Expectation seen_r1r2_r3 = |
| 142 | EXPECT_CALL(mr, FoundSubgraphPair(0b000011, 0b000100, 1)); |
| 143 | |
| 144 | // {R1,R2,R3} is connected (called only after we've seen its components). |
| 145 | EXPECT_CALL(mr, HasSeen(0b000111)) |
| 146 | .After(seen_r1_r2r3) |
| 147 | .After(seen_r1r2_r3) |
| 148 | .WillRepeatedly(Return(true)); |
| 149 | |
| 150 | // Found link between {R1,R2,R3} and {R4,R5,R6}. |
| 151 | EXPECT_CALL(mr, FoundSubgraphPair(0b000111, 0b111000, 4)) |
| 152 | .After(seen_r1_r2r3) |
| 153 | .After(seen_r1r2_r3) |
| 154 | .After(seen_r4_r5r6) |
| 155 | .After(seen_r4r5_r6); |
| 156 | |
| 157 | EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &mr)); |
| 158 | } |
| 159 | |
| 160 | TEST(DPhypTest, Loop) { |
| 161 | MEM_ROOT mem_root; |
| 162 | /* |
| 163 | Shows that we can go around a loop and connect R1 to {R2,R3,R4,R5} |
| 164 | graph through {R2,R5}, even though R5 was not part of R1's |
| 165 | neighborhood (ie., R2 was chosen as the representative node). |
| 166 | This requires that we remember that R5 was a part of R1's full |
| 167 | neighborhood. |
| 168 | |
| 169 | R2----R3 |
| 170 | / | |
| 171 | / | |
| 172 | R1-- | |
| 173 | \ | |
| 174 | \ | |
| 175 | R5----R4 |
| 176 | */ |
| 177 | Hypergraph g(&mem_root); |
| 178 | g.AddNode(); // R1 |
| 179 | g.AddNode(); // R2 |
| 180 | g.AddNode(); // R3 |
| 181 | g.AddNode(); // R4 |
| 182 | g.AddNode(); // R5 |
| 183 | g.AddEdge(0b00001, 0b10010); // R1-{R2,R5} |
| 184 | g.AddEdge(0b00010, 0b00100); // R2-R3 |
| 185 | g.AddEdge(0b00100, 0b01000); // R3-R4 |
| 186 | g.AddEdge(0b01000, 0b10000); // R4-R5 |
| 187 | |
| 188 | StrictMock<MockReceiver> mr; |
| 189 | EXPECT_CALL(mr, FoundSingleNode(0)); |
| 190 | EXPECT_CALL(mr, FoundSingleNode(1)); |
| 191 | EXPECT_CALL(mr, FoundSingleNode(2)); |
| 192 | EXPECT_CALL(mr, FoundSingleNode(3)); |
| 193 | EXPECT_CALL(mr, FoundSingleNode(4)); |
| 194 | |
| 195 | // Fallback matcher. |
| 196 | EXPECT_CALL(mr, HasSeen(_)).WillRepeatedly(Return(false)); |
| 197 | |
| 198 | // Found link between R4 and R5. |
| 199 | Expectation seen_r4_r5 = |
| 200 | EXPECT_CALL(mr, FoundSubgraphPair(0b01000, 0b10000, 3)); |
| 201 | EXPECT_CALL(mr, HasSeen(0b11000)) |
| 202 | .After(seen_r4_r5) |
| 203 | .WillRepeatedly(Return(true)); |
| 204 | |
| 205 | // Found link between R3 and R4. |
| 206 | Expectation seen_r3_r4 = |
| 207 | EXPECT_CALL(mr, FoundSubgraphPair(0b00100, 0b01000, 2)); |
| 208 | EXPECT_CALL(mr, HasSeen(0b01100)) |
| 209 | .After(seen_r3_r4) |
| 210 | .WillRepeatedly(Return(true)); |
| 211 | |
| 212 | // Found link between R3 and {R4,R5}, through the R3-R4 edge. |
| 213 | Expectation seen_r3_r4r5 = |
| 214 | EXPECT_CALL(mr, FoundSubgraphPair(0b00100, 0b11000, 2)).After(seen_r4_r5); |
| 215 | |
| 216 | // Found link between {R3,R4} and R5, through the R4-R5 edge. |
| 217 | Expectation seen_r3r4_r5 = |
| 218 | EXPECT_CALL(mr, FoundSubgraphPair(0b01100, 0b10000, 3)).After(seen_r3_r4); |
| 219 | |
| 220 | // {R3,R4,R5} is connected (called only after we've seen its components). |
| 221 | EXPECT_CALL(mr, HasSeen(0b11100)) |
| 222 | .After(seen_r3_r4r5) |
| 223 | .After(seen_r3r4_r5) |
| 224 | .WillRepeatedly(Return(true)); |
| 225 | |
| 226 | // Found link between R2 and R3. |
| 227 | Expectation seen_r2_r3 = |
| 228 | EXPECT_CALL(mr, FoundSubgraphPair(0b00010, 0b00100, 1)); |
| 229 | EXPECT_CALL(mr, HasSeen(0b00110)).WillRepeatedly(Return(true)); |
| 230 | |
| 231 | // Found link between R2 and {R3,R4}, through the R2-R3 edge. |
| 232 | Expectation seen_r2_r3r4 = |
| 233 | EXPECT_CALL(mr, FoundSubgraphPair(0b00010, 0b01100, 1)).After(seen_r3_r4); |
| 234 | |
| 235 | // Found link between {R2,R3} and R4, through the R3-R4 edge. |
| 236 | Expectation seen_r2r3_r4 = |
| 237 | EXPECT_CALL(mr, FoundSubgraphPair(0b00110, 0b01000, 2)).After(seen_r2_r3); |
| 238 | |
| 239 | // {R2,R3,R4} is connected (called only after we've seen its components). |
| 240 | EXPECT_CALL(mr, HasSeen(0b01110)) |
| 241 | .After(seen_r2_r3r4) |
| 242 | .After(seen_r2r3_r4) |
| 243 | .WillRepeatedly(Return(true)); |
| 244 | |
| 245 | // Found link between R2 and {R3,R4,R5}, through the R2-R3 edge. |
| 246 | Expectation seen_r2_r3r4r5 = |
| 247 | EXPECT_CALL(mr, FoundSubgraphPair(0b00010, 0b11100, 1)) |
| 248 | .After(seen_r3_r4r5) |
| 249 | .After(seen_r3r4_r5); |
| 250 | |
| 251 | // Found link between {R2,R3} and {R4,R5}, through the R3-R4 edge. |
| 252 | Expectation seen_r2r3_r4r5 = |
| 253 | EXPECT_CALL(mr, FoundSubgraphPair(0b00110, 0b11000, 2)) |
| 254 | .After(seen_r2_r3) |
| 255 | .After(seen_r4_r5); |
| 256 | |
| 257 | // Found link between {R2,R3,R4} and R5, through the R4-R5 edge. |
| 258 | Expectation seen_r2r3r4_r5 = |
| 259 | EXPECT_CALL(mr, FoundSubgraphPair(0b01110, 0b10000, 3)) |
| 260 | .After(seen_r2r3_r4) |
| 261 | .After(seen_r2_r3r4); |
| 262 | |
| 263 | // {R2,R3,R4,R5} is connected (called only after we've seen its components). |
| 264 | EXPECT_CALL(mr, HasSeen(0b11110)) |
| 265 | .After(seen_r2_r3r4r5) |
| 266 | .After(seen_r2r3_r4r5) |
| 267 | .After(seen_r2r3r4_r5) |
| 268 | .WillRepeatedly(Return(true)); |
| 269 | |
| 270 | // Finally, found link between R1 and {R2,R3,R4,R5}, through the R1-{R2,R5} |
| 271 | // edge. |
| 272 | EXPECT_CALL(mr, FoundSubgraphPair(0b00001, 0b11110, 0)) |
| 273 | .After(seen_r2_r3r4r5) |
| 274 | .After(seen_r2r3_r4r5) |
| 275 | .After(seen_r2r3r4_r5); |
| 276 | |
| 277 | EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &mr)); |
| 278 | } |
| 279 | |
| 280 | TEST(DPhypTest, AbortWithError) { |
| 281 | MEM_ROOT mem_root; |
| 282 | /* |
| 283 | A simple chain. |
| 284 | |
| 285 | R1--R2--R3 |
| 286 | */ |
| 287 | Hypergraph g(&mem_root); |
| 288 | g.AddNode(); // R1 |
| 289 | g.AddNode(); // R2 |
| 290 | g.AddNode(); // R3 |
| 291 | g.AddEdge(0b000001, 0b000010); // R1-R2 |
| 292 | g.AddEdge(0b000010, 0b000100); // R2-R3 |
| 293 | |
| 294 | StrictMock<MockReceiver> mr; |
| 295 | EXPECT_CALL(mr, FoundSingleNode(1)); |
| 296 | EXPECT_CALL(mr, FoundSingleNode(2)); |
| 297 | |
| 298 | // Fallback matcher. |
| 299 | EXPECT_CALL(mr, HasSeen(_)).WillRepeatedly(Return(false)); |
| 300 | |
| 301 | // Found link between R2 and R3. We return true (error) here, |
| 302 | // so the algorithm should abort without ever seeing R1 |
| 303 | // or any of the links to it. |
| 304 | EXPECT_CALL(mr, FoundSubgraphPair(0b000010, 0b000100, 1)) |
| 305 | .WillOnce(Return(true)); |
| 306 | |
| 307 | EXPECT_TRUE(EnumerateAllConnectedPartitions(g, &mr)); |
| 308 | } |
| 309 | |
| 310 | // A Receiver used for unit tests. It records all subgraph pairs we see, |
| 311 | // allowing us to check afterwards that the correct ones were discovered |
| 312 | // (and no others). It also verifies correct ordering of HasSeen() calls. |
| 313 | struct AccumulatingReceiver { |
| 314 | struct Subplan { |
| 315 | NodeMap left, right; |
| 316 | int edge_idx; |
| 317 | }; |
| 318 | |
| 319 | bool HasSeen(NodeMap subgraph) { |
| 320 | if (seen_subplans.find(subgraph) == seen_subplans.end()) { |
| 321 | has_returned_nonconnected.insert(subgraph); |
| 322 | return false; |
| 323 | } else { |
| 324 | assert(has_returned_nonconnected.count(subgraph) == 0); |
| 325 | return true; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | bool FoundSingleNode(int node_idx) { |
| 330 | NodeMap map = TableBitmap(node_idx); |
| 331 | |
| 332 | // We must always see all enumerations for a subset before we can |
| 333 | // use that subset. |
| 334 | assert(used_in_larger_subset.count(map) == 0); |
| 335 | |
| 336 | // Should be called only once. |
| 337 | assert(seen_subplans.count(map) == 0); |
| 338 | |
| 339 | seen_subplans.emplace(map, Subplan{0, 0, -1}); |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | bool FoundSubgraphPair(NodeMap left, NodeMap right, int edge_idx) { |
| 344 | printf("Found connection between %s and %s along edge %d\n", |
| 345 | PrintSet(left).c_str(), PrintSet(right).c_str(), edge_idx); |
| 346 | |
| 347 | // We must always see all enumerations for a subset before we can |
| 348 | // use that subset. |
| 349 | assert(used_in_larger_subset.count(left | right) == 0); |
| 350 | used_in_larger_subset.insert(left); |
| 351 | used_in_larger_subset.insert(right); |
| 352 | |
| 353 | // Additional test that in practice tests the same thing. |
| 354 | assert(has_returned_nonconnected.count(left | right) == 0); |
| 355 | |
| 356 | // We should only get a given subgraph pair once. |
| 357 | EXPECT_FALSE(SeenSubgraphPair(left, right, edge_idx)) |
| 358 | << "Duplicate connection between " << PrintSet(left) << " and " |
| 359 | << PrintSet(right) << " along edge " << edge_idx; |
| 360 | |
| 361 | seen_subplans.emplace(left | right, Subplan{left, right, edge_idx}); |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | // Checks whether FoundSubgraphPair() was called with the given arguments. |
| 366 | // Fairly slow for large graphs. |
| 367 | bool SeenSubgraphPair(NodeMap left, NodeMap right, int edge_idx) { |
| 368 | const auto subset_subplans = seen_subplans.equal_range(left | right); |
| 369 | for (auto it = subset_subplans.first; it != subset_subplans.second; ++it) { |
| 370 | if (it->second.left == left && it->second.right == right && |
| 371 | it->second.edge_idx == edge_idx) { |
| 372 | return true; |
| 373 | } |
| 374 | } |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | std::unordered_set<NodeMap> has_returned_nonconnected; |
| 379 | std::unordered_set<NodeMap> used_in_larger_subset; |
| 380 | std::multimap<NodeMap, Subplan> seen_subplans; |
| 381 | }; |
| 382 | |
| 383 | // A very simple receiver used during benchmarking only, used to isolate |
| 384 | // away receiver performance from the algorithm itself. Probably the fastest |
| 385 | // imaginable implementation; does nothing useful except remember which |
| 386 | // subgraphs are connected, as required for HasSeen(). |
| 387 | template <int Size> |
| 388 | struct BenchmarkReceiver { |
| 389 | bool HasSeen(NodeMap subgraph) { return seen_subplans[subgraph]; } |
| 390 | |
| 391 | bool FoundSingleNode(int node_idx) { |
| 392 | NodeMap map = TableBitmap(node_idx); |
| 393 | seen_subplans.set(map); |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | bool FoundSubgraphPair(NodeMap left, NodeMap right, int) { |
| 398 | seen_subplans.set(left | right); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | static constexpr int num_elements = 1 << Size; |
| 403 | std::bitset<num_elements> seen_subplans; |
| 404 | }; |
| 405 | |
| 406 | // Creates a simple chain A-B-C-D-..., and verifies that we get all possible |
| 407 | // permutations. |
| 408 | TEST(DPhypTest, Chain) { |
| 409 | constexpr int num_elements = 20; |
| 410 | |
| 411 | MEM_ROOT mem_root; |
| 412 | Hypergraph g(&mem_root); |
| 413 | for (int i = 0; i < num_elements; ++i) { |
| 414 | g.AddNode(); |
| 415 | if (i != 0) { |
| 416 | g.AddEdge(TableBitmap(i - 1), TableBitmap(i)); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | AccumulatingReceiver receiver; |
| 421 | EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &receiver)); |
| 422 | |
| 423 | // Look at all possible subchains of the chain. |
| 424 | int expected_subplans = 0; |
| 425 | for (int start_idx = 0; start_idx < num_elements; ++start_idx) { |
| 426 | for (int end_idx = start_idx + 1; end_idx <= num_elements; ++end_idx) { |
| 427 | NodeMap subset = TablesBetween(start_idx, end_idx); |
| 428 | |
| 429 | if (end_idx == start_idx + 1) { |
| 430 | // Single node, so should have a single single-node subplan. |
| 431 | ASSERT_EQ(1, receiver.seen_subplans.count(subset)); |
| 432 | EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.left); |
| 433 | EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.right); |
| 434 | EXPECT_EQ(-1, receiver.seen_subplans.find(subset)->second.edge_idx); |
| 435 | ++expected_subplans; |
| 436 | continue; |
| 437 | } |
| 438 | |
| 439 | // This subchain should be splittable along all possible midpoints. |
| 440 | for (int split_after_idx = start_idx; split_after_idx < end_idx - 1; |
| 441 | ++split_after_idx) { |
| 442 | NodeMap left = TablesBetween(start_idx, split_after_idx + 1); |
| 443 | NodeMap right = TablesBetween(split_after_idx + 1, end_idx); |
| 444 | int edge_idx = split_after_idx; |
| 445 | |
| 446 | EXPECT_TRUE(receiver.SeenSubgraphPair(left, right, edge_idx)) |
| 447 | << "Subset " << PrintSet(subset) << " should be splittable into " |
| 448 | << PrintSet(left) << " and " << PrintSet(right) << " along edge " |
| 449 | << edge_idx; |
| 450 | ++expected_subplans; |
| 451 | } |
| 452 | |
| 453 | EXPECT_TRUE(receiver.seen_subplans.count(subset)); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // We should have no other subplans than the ones we checked for earlier. |
| 458 | EXPECT_EQ(expected_subplans, receiver.seen_subplans.size()); |
| 459 | } |
| 460 | |
| 461 | // Demonstrates that we need to grow neighborhoods carefully when looking for |
| 462 | // complement seeds. Specifically, when starting with {R1} (which has |
| 463 | // neighborhood {R2,R3,R4}) and growing it with R2, we'd normally only consider |
| 464 | // the neighborhood of R2, since R3 and R4 are now in the forbidden set. |
| 465 | // However, when looking for seeds for the complement of {R1,R2}, we need to |
| 466 | // take R3 and R4 back into account, since they are not forbidden in this |
| 467 | // context. |
| 468 | // |
| 469 | // This test doesn't test precise call ordering, only that we get all the |
| 470 | // expected sets. |
| 471 | TEST(DPhypTest, SmallStar) { |
| 472 | MEM_ROOT mem_root; |
| 473 | /* |
| 474 | R2 |
| 475 | | |
| 476 | | |
| 477 | R1---R3 |
| 478 | | |
| 479 | | |
| 480 | R4 |
| 481 | */ |
| 482 | Hypergraph g(&mem_root); |
| 483 | g.AddNode(); // R1 |
| 484 | g.AddNode(); // R2 |
| 485 | g.AddNode(); // R3 |
| 486 | g.AddNode(); // R4 |
| 487 | g.AddEdge(0b00001, 0b00010); // R1-R2 |
| 488 | g.AddEdge(0b00001, 0b00100); // R1-R3 |
| 489 | g.AddEdge(0b00001, 0b01000); // R1-R4 |
| 490 | |
| 491 | StrictMock<MockReceiver> mr; |
| 492 | EXPECT_CALL(mr, FoundSingleNode(0)); |
| 493 | EXPECT_CALL(mr, FoundSingleNode(1)); |
| 494 | EXPECT_CALL(mr, FoundSingleNode(2)); |
| 495 | EXPECT_CALL(mr, FoundSingleNode(3)); |
| 496 | |
| 497 | for (int i = 1; i < 16; ++i) { |
| 498 | if (IsSingleBitSet(i)) { |
| 499 | EXPECT_CALL(mr, HasSeen(i)) |
| 500 | .Times(AnyNumber()) |
| 501 | .WillRepeatedly(Return(true)); |
| 502 | } else { |
| 503 | // Anything containing R1 is connected, anything else is not. |
| 504 | EXPECT_CALL(mr, HasSeen(i)) |
| 505 | .Times(AnyNumber()) |
| 506 | .WillRepeatedly(Return(i & 1)); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | EXPECT_CALL(mr, FoundSubgraphPair(0b0001, 0b0010, 0)); // R1-R2. |
| 511 | EXPECT_CALL(mr, FoundSubgraphPair(0b0001, 0b0100, 1)); // R1-R3. |
| 512 | EXPECT_CALL(mr, FoundSubgraphPair(0b0001, 0b1000, 2)); // R1-R4. |
| 513 | |
| 514 | EXPECT_CALL(mr, |
| 515 | FoundSubgraphPair(0b0011, 0b0100, 1)); // {R1,R2}-R3 along R1-R3. |
| 516 | EXPECT_CALL(mr, |
| 517 | FoundSubgraphPair(0b0011, 0b1000, 2)); // {R1,R2}-R4 along R1-R4. |
| 518 | |
| 519 | EXPECT_CALL(mr, |
| 520 | FoundSubgraphPair(0b0101, 0b0010, 0)); // {R1,R3}-R2 along R1-R2. |
| 521 | EXPECT_CALL(mr, |
| 522 | FoundSubgraphPair(0b0101, 0b1000, 2)); // {R1,R3}-R4 along R1-R4. |
| 523 | |
| 524 | EXPECT_CALL(mr, |
| 525 | FoundSubgraphPair(0b1001, 0b0010, 0)); // {R1,R4}-R2 along R1-R2. |
| 526 | EXPECT_CALL(mr, |
| 527 | FoundSubgraphPair(0b1001, 0b0100, 1)); // {R1,R4}-R3 along R1-R3. |
| 528 | |
| 529 | EXPECT_CALL( |
| 530 | mr, FoundSubgraphPair(0b0111, 0b1000, 2)); // {R1,R2,R3}-R4 along R1-R4. |
| 531 | EXPECT_CALL( |
| 532 | mr, FoundSubgraphPair(0b1011, 0b0100, 1)); // {R1,R2,R4}-R3 along R1-R3. |
| 533 | EXPECT_CALL( |
| 534 | mr, FoundSubgraphPair(0b1101, 0b0010, 0)); // {R1,R2,R4}-R3 along R1-R2. |
| 535 | |
| 536 | EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &mr)); |
| 537 | } |
| 538 | |
| 539 | // Creates a clique (everything connected to everything, with simple edges) |
| 540 | // and checks that we get every possible permutation, along every relevant edge. |
| 541 | TEST(DPhypTest, Clique) { |
| 542 | constexpr int num_elements = 6; |
| 543 | |
| 544 | int edge_indexes[num_elements][num_elements]; |
| 545 | |
| 546 | MEM_ROOT mem_root; |
| 547 | Hypergraph g(&mem_root); |
| 548 | for (int i = 0; i < num_elements; ++i) { |
| 549 | g.AddNode(); |
| 550 | for (int j = 0; j < i; ++j) { |
| 551 | g.AddEdge(TableBitmap(i), TableBitmap(j)); |
| 552 | edge_indexes[i][j] = edge_indexes[j][i] = (g.edges.size() - 1) / 2; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | AccumulatingReceiver receiver; |
| 557 | EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &receiver)); |
| 558 | |
| 559 | int expected_subplans = 0; |
| 560 | |
| 561 | // Look at all possible non-zero subsets of the clique. |
| 562 | for (NodeMap subset = 1; subset < (NodeMap{1} << num_elements); ++subset) { |
| 563 | if (IsSingleBitSet(subset)) { |
| 564 | // Single node, so should have a single single-node subplan. |
| 565 | ASSERT_EQ(1, receiver.seen_subplans.count(subset)); |
| 566 | EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.left); |
| 567 | EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.right); |
| 568 | EXPECT_EQ(-1, receiver.seen_subplans.find(subset)->second.edge_idx); |
| 569 | ++expected_subplans; |
| 570 | continue; |
| 571 | } |
| 572 | |
| 573 | // Find all possible two-way partitions of this subset. |
| 574 | for (NodeMap left : NonzeroSubsetsOf(subset)) { |
| 575 | if (left == subset) continue; |
| 576 | NodeMap right = subset & ~left; |
| 577 | if (IsolateLowestBit(left) > IsolateLowestBit(right)) continue; |
| 578 | |
| 579 | for (size_t left_idx : BitsSetIn(left)) { |
| 580 | for (size_t right_idx : BitsSetIn(right)) { |
| 581 | int edge_idx = edge_indexes[left_idx][right_idx]; |
| 582 | EXPECT_TRUE(receiver.SeenSubgraphPair(left, right, edge_idx)) |
| 583 | << "Subset " << PrintSet(subset) << " should be splittable into " |
| 584 | << PrintSet(left) << " and " << PrintSet(right) << " along edge " |
| 585 | << edge_idx; |
| 586 | ++expected_subplans; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // We should have no other subplans than the ones we checked for earlier. |
| 593 | EXPECT_EQ(expected_subplans, receiver.seen_subplans.size()); |
| 594 | } |
| 595 | |
| 596 | // Constructs a hypergraph of A LEFT JOIN (B LEFT JOIN (C LEFT JOIN ...)), |
| 597 | // for null-tolerant joins; ie., no reordering is possible and only one |
| 598 | // possible plan should exist. |
| 599 | TEST(DPhypTest, OuterJoinChain) { |
| 600 | constexpr int num_nodes = 5; |
| 601 | |
| 602 | MEM_ROOT mem_root; |
| 603 | Hypergraph g(&mem_root); |
| 604 | g.AddNode(); // R1 |
| 605 | g.AddNode(); // R2 |
| 606 | g.AddNode(); // R3 |
| 607 | g.AddNode(); // R4 |
| 608 | g.AddNode(); // R5 |
| 609 | g.AddEdge(0b11110, 0b00001); // R1-{R2,R3,R4,R5} |
| 610 | g.AddEdge(0b11100, 0b00010); // R2-{R3,R4,R5} |
| 611 | g.AddEdge(0b11000, 0b00100); // R3-{R4,R5} |
| 612 | g.AddEdge(0b10000, 0b01000); // R4-R5 |
| 613 | |
| 614 | AccumulatingReceiver receiver; |
| 615 | EXPECT_FALSE(EnumerateAllConnectedPartitions(g, &receiver)); |
| 616 | |
| 617 | int expected_subplans = 0; |
| 618 | |
| 619 | for (size_t node_idx = 0; node_idx < num_nodes; ++node_idx) { |
| 620 | NodeMap subset = TableBitmap(node_idx); |
| 621 | ASSERT_EQ(1, receiver.seen_subplans.count(subset)); |
| 622 | EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.left); |
| 623 | EXPECT_EQ(0, receiver.seen_subplans.find(subset)->second.right); |
| 624 | EXPECT_EQ(-1, receiver.seen_subplans.find(subset)->second.edge_idx); |
| 625 | ++expected_subplans; |
| 626 | } |
| 627 | |
| 628 | for (size_t edge_idx = 0; edge_idx < num_nodes - 1; ++edge_idx) { |
| 629 | NodeMap subset = g.edges[edge_idx * 2].left | g.edges[edge_idx * 2].right; |
| 630 | ASSERT_EQ(1, receiver.seen_subplans.count(subset)); |
| 631 | |
| 632 | // NOTE: The edges come out flipped compared to the order we added them, |
| 633 | // due to the ordering properties. |
| 634 | EXPECT_EQ(g.edges[edge_idx * 2].right, |
| 635 | receiver.seen_subplans.find(subset)->second.left); |
| 636 | EXPECT_EQ(g.edges[edge_idx * 2].left, |
| 637 | receiver.seen_subplans.find(subset)->second.right); |
| 638 | |
| 639 | EXPECT_EQ(edge_idx, receiver.seen_subplans.find(subset)->second.edge_idx); |
| 640 | ++expected_subplans; |
| 641 | } |
| 642 | |
| 643 | // We should have no other subplans than the ones we checked for earlier. |
| 644 | EXPECT_EQ(expected_subplans, receiver.seen_subplans.size()); |
| 645 | } |
| 646 | |
| 647 | static void BM_Chain20(size_t num_iterations) { |
| 648 | StopBenchmarkTiming(); |
| 649 | constexpr int num_nodes = 20; |
| 650 | |
| 651 | MEM_ROOT mem_root; |
| 652 | Hypergraph g(&mem_root); |
| 653 | for (int i = 0; i < num_nodes; ++i) { |
| 654 | g.AddNode(); |
| 655 | if (i != 0) { |
| 656 | g.AddEdge(TableBitmap(i - 1), TableBitmap(i)); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | for (size_t i = 0; i < num_iterations; ++i) { |
| 661 | BenchmarkReceiver<num_nodes> receiver; |
| 662 | |
| 663 | StartBenchmarkTiming(); |
| 664 | EnumerateAllConnectedPartitions(g, &receiver); |
| 665 | StopBenchmarkTiming(); |
| 666 | } |
| 667 | } |
| 668 | BENCHMARK(BM_Chain20) |
| 669 | |
| 670 | // Like the OuterJoinChain test, just as a benchmark. |
| 671 | // |
| 672 | // Note that even though we only emit one possible plan, this test is not |
| 673 | // that much faster then BM_Chain20. The reason is that even though the |
| 674 | // number of subsets go down from O(n³) to O(n²), each node also is touched |
| 675 | // by more hyperedges (on the order of O(n)), so neighborhood finding has to |
| 676 | // sift through more edges. It would be nice if we had some way of culling |
| 677 | // these “obviously wrong” edges without a linear search (e.g., it is |
| 678 | // meaningless for R5 to traverse a hyperedge to R1 in the neighborhood |
| 679 | // calculation when expanding subgraphs, since it goes “backwards”), but in the |
| 680 | // presence of cycles, there does not seem to be an obvious way of codifying |
| 681 | // this. |
| 682 | static void BM_NestedOuterJoin20(size_t num_iterations) { |
| 683 | StopBenchmarkTiming(); |
| 684 | constexpr int num_nodes = 20; |
| 685 | |
| 686 | MEM_ROOT mem_root; |
| 687 | Hypergraph g(&mem_root); |
| 688 | for (int i = 0; i < num_nodes; ++i) { |
| 689 | g.AddNode(); |
| 690 | } |
| 691 | for (int i = 0; i < num_nodes - 1; ++i) { |
| 692 | g.AddEdge(TableBitmap(i), TablesBetween(i + 1, num_nodes)); |
| 693 | } |
| 694 | |
| 695 | for (size_t i = 0; i < num_iterations; ++i) { |
| 696 | BenchmarkReceiver<num_nodes> receiver; |
| 697 | |
| 698 | StartBenchmarkTiming(); |
| 699 | EnumerateAllConnectedPartitions(g, &receiver); |
| 700 | StopBenchmarkTiming(); |
| 701 | } |
| 702 | } |
| 703 | BENCHMARK(BM_NestedOuterJoin20) |
| 704 | |
| 705 | // Benchmark from the DPhyp paper. We only implement the version |
| 706 | // with hyperedges split into cardinality-2 hypernodes. |
| 707 | static void BM_HyperCycle16(size_t num_iterations) { |
| 708 | StopBenchmarkTiming(); |
| 709 | constexpr int num_nodes = 16; // A multiple of four. |
| 710 | |
| 711 | MEM_ROOT mem_root; |
| 712 | Hypergraph g(&mem_root); |
| 713 | for (int i = 0; i < num_nodes; ++i) { |
| 714 | g.AddNode(); |
| 715 | } |
| 716 | |
| 717 | // Add the simple edges that create the cycle. |
| 718 | for (int i = 0; i < num_nodes; ++i) { |
| 719 | g.AddEdge(TableBitmap(i), TableBitmap((i + 1) % num_nodes)); |
| 720 | } |
| 721 | |
| 722 | // Add some hyperedges. |
| 723 | for (int i = 0; i < num_nodes; i += 4) { |
| 724 | g.AddEdge(TablesBetween(i, i + 2), TablesBetween(i + 2, i + 4)); |
| 725 | } |
| 726 | |
| 727 | for (size_t i = 0; i < num_iterations; ++i) { |
| 728 | BenchmarkReceiver<num_nodes> receiver; |
| 729 | |
| 730 | StartBenchmarkTiming(); |
| 731 | EnumerateAllConnectedPartitions(g, &receiver); |
| 732 | StopBenchmarkTiming(); |
| 733 | } |
| 734 | } |
| 735 | BENCHMARK(BM_HyperCycle16) |
| 736 | |
| 737 | static void BM_Star17(size_t num_iterations) { |
| 738 | StopBenchmarkTiming(); |
| 739 | constexpr int num_nodes = 17; |
| 740 | |
| 741 | MEM_ROOT mem_root; |
| 742 | Hypergraph g(&mem_root); |
| 743 | g.AddNode(); // The central node. |
| 744 | for (int i = 1; i < num_nodes; ++i) { |
| 745 | g.AddNode(); |
| 746 | g.AddEdge(TableBitmap(0), TableBitmap(i)); |
| 747 | } |
| 748 | |
| 749 | for (size_t i = 0; i < num_iterations; ++i) { |
| 750 | BenchmarkReceiver<num_nodes> receiver; |
| 751 | |
| 752 | StartBenchmarkTiming(); |
| 753 | EnumerateAllConnectedPartitions(g, &receiver); |
| 754 | StopBenchmarkTiming(); |
| 755 | } |
| 756 | } |
| 757 | BENCHMARK(BM_Star17) |
| 758 | |
| 759 | // Benchmark from the DPhyp paper. This is the version with hyperedges split |
| 760 | // into cardinality-2 hypernodes. |
| 761 | static void BM_HyperStar17_ManyHyperedges(size_t num_iterations) { |
| 762 | StopBenchmarkTiming(); |
| 763 | constexpr int num_nodes = 17; // A multiple of four, plus one. |
| 764 | |
| 765 | MEM_ROOT mem_root; |
| 766 | Hypergraph g(&mem_root); |
| 767 | g.AddNode(); // The central node. |
| 768 | for (int i = 1; i < num_nodes; ++i) { |
| 769 | g.AddNode(); |
| 770 | g.AddEdge(TableBitmap(0), TableBitmap(i)); |
| 771 | } |
| 772 | |
| 773 | // Add some hyperedges. |
| 774 | constexpr int half = (num_nodes - 1) / 2; |
| 775 | for (int i = 0; i < half; i += 2) { |
| 776 | g.AddEdge(TablesBetween(i + 1, i + 3), |
| 777 | TablesBetween(i + half + 1, i + half + 3)); |
| 778 | } |
| 779 | |
| 780 | for (size_t i = 0; i < num_iterations; ++i) { |
| 781 | BenchmarkReceiver<num_nodes> receiver; |
| 782 | |
| 783 | StartBenchmarkTiming(); |
| 784 | EnumerateAllConnectedPartitions(g, &receiver); |
| 785 | StopBenchmarkTiming(); |
| 786 | } |
| 787 | } |
| 788 | BENCHMARK(BM_HyperStar17_ManyHyperedges) |
| 789 | |
| 790 | // Benchmark from the DPhyp paper. This is the version with no hyperedge |
| 791 | // split (only one large hyperedge). |
| 792 | static void BM_HyperStar17_SingleLargeHyperedge(size_t num_iterations) { |
| 793 | StopBenchmarkTiming(); |
| 794 | constexpr int num_nodes = 17; // A multiple of two, plus one. |
| 795 | |
| 796 | MEM_ROOT mem_root; |
| 797 | Hypergraph g(&mem_root); |
| 798 | g.AddNode(); // The central node. |
| 799 | for (int i = 1; i < num_nodes; ++i) { |
| 800 | g.AddNode(); |
| 801 | g.AddEdge(TableBitmap(0), TableBitmap(i)); |
| 802 | } |
| 803 | |
| 804 | // Add a single large hyperedge. |
| 805 | constexpr int half = (num_nodes - 1) / 2; |
| 806 | g.AddEdge(TablesBetween(1, half + 1), TablesBetween(half + 1, num_nodes)); |
| 807 | |
| 808 | for (size_t i = 0; i < num_iterations; ++i) { |
| 809 | BenchmarkReceiver<num_nodes> receiver; |
| 810 | |
| 811 | StartBenchmarkTiming(); |
| 812 | EnumerateAllConnectedPartitions(g, &receiver); |
| 813 | StopBenchmarkTiming(); |
| 814 | } |
| 815 | } |
| 816 | BENCHMARK(BM_HyperStar17_SingleLargeHyperedge) |