Debian Codegraph @

unittest/gunit/interesting_orders-t.cc

raw symbols SCIP
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 <gtest/gtest.h>
25
26 #include <array>
27
28 #include "sql/item.h"
29 #include "sql/join_optimizer/interesting_orders.h"
30 #include "sql/key_spec.h"
31 #include "sql/sql_array.h"
32 #include "sql/thd_raii.h"
33 #include "unittest/gunit/benchmark.h"
34 #include "unittest/gunit/fake_table.h"
35 #include "unittest/gunit/test_utils.h"
36
37 using std::array;
38 using std::unique_ptr;
39
40 template <class T, size_t Size>
41 static int AddOrdering(THD *thd, std::array<T, Size> &terms, bool interesting,
42 LogicalOrderings *orderings) {
43 const Ordering::Kind kind = terms[0].direction == ORDER_NOT_RELEVANT
44 ? Ordering::Kind::kGroup
45 : Ordering::Kind::kOrder;
46
47 const Ordering::Elements elements{terms.data(), terms.size()};
48
49 return orderings->AddOrdering(thd, Ordering{elements, kind}, interesting,
50 /*used_at_end=*/true, /*homogenize_tables=*/0);
51 }
52
53 TEST(InterestingOrderingTest, DeduplicateHandles) {
54 my_testing::Server_initializer m_initializer;
55 m_initializer.SetUp();
56 LogicalOrderings orderings(m_initializer.thd());
57
58 EXPECT_EQ(1, orderings.GetHandle(new Item_int(1)));
59 EXPECT_EQ(2, orderings.GetHandle(new Item_int(2)));
60 EXPECT_EQ(1, orderings.GetHandle(new Item_int(1)));
61 EXPECT_EQ(3, orderings.GetHandle(new Item_int(10)));
62 }
63
64 TEST(InterestingOrderingTest, DeduplicateOrderings) {
65 my_testing::Server_initializer m_initializer;
66 m_initializer.SetUp();
67 THD *thd = m_initializer.thd();
68
69 LogicalOrderings orderings(thd);
70 ItemHandle i1 = orderings.GetHandle(new Item_int(1));
71 ItemHandle i2 = orderings.GetHandle(new Item_int(2));
72 ItemHandle i3 = orderings.GetHandle(new Item_int(3));
73
74 array<OrderElement, 2> order_a{OrderElement{i1, ORDER_ASC},
75 OrderElement{i2, ORDER_ASC}};
76 EXPECT_EQ(1, AddOrdering(thd, order_a, /*interesting=*/false, &orderings));
77 EXPECT_FALSE(orderings.ordering_is_relevant_for_sortahead(1));
78
79 array<OrderElement, 2> order_b{OrderElement{i1, ORDER_ASC},
80 OrderElement{i3, ORDER_ASC}};
81 EXPECT_EQ(2, AddOrdering(thd, order_b, /*interesting=*/true, &orderings));
82 EXPECT_TRUE(orderings.ordering_is_relevant_for_sortahead(2));
83 EXPECT_EQ(1, AddOrdering(thd, order_a, /*interesting=*/true, &orderings));
84 EXPECT_TRUE(orderings.ordering_is_relevant_for_sortahead(1));
85
86 array<OrderElement, 2> order_equiv_a{OrderElement{i1, ORDER_ASC},
87 OrderElement{i2, ORDER_ASC}};
88 EXPECT_EQ(1,
89 AddOrdering(thd, order_equiv_a, /*interesting=*/true, &orderings));
90
91 array<OrderElement, 2> grouping_a{OrderElement{i1, ORDER_NOT_RELEVANT},
92 OrderElement{i2, ORDER_NOT_RELEVANT}};
93 EXPECT_EQ(3, AddOrdering(thd, grouping_a, /*interesting=*/true, &orderings));
94 }
95
96 TEST(InterestingOrderingTest, DeduplicateFunctionalDependencies) {
97 my_testing::Server_initializer m_initializer;
98 m_initializer.SetUp();
99 THD *thd = m_initializer.thd();
100
101 LogicalOrderings orderings(thd);
102 ItemHandle i1 = orderings.GetHandle(new Item_int(1));
103 ItemHandle i2 = orderings.GetHandle(new Item_int(2));
104
105 // Add i1 = i2.
106 array<ItemHandle, 1> head_i1{i1};
107 FunctionalDependency fd_equiv;
108 fd_equiv.type = FunctionalDependency::EQUIVALENCE;
109 fd_equiv.head = Bounds_checked_array<ItemHandle>(head_i1);
110 fd_equiv.tail = i2;
111 EXPECT_EQ(1, orderings.AddFunctionalDependency(thd, fd_equiv));
112
113 // Invert the equivalence; it should still be deduplicated away.
114 array<ItemHandle, 1> head_i2{i2};
115 fd_equiv.head = Bounds_checked_array<ItemHandle>(head_i2);
116 fd_equiv.tail = i1;
117 EXPECT_EQ(1, orderings.AddFunctionalDependency(thd, fd_equiv));
118
119 // Add i1 → i2.
120 FunctionalDependency fd_12;
121 fd_12.type = FunctionalDependency::FD;
122 fd_12.head = Bounds_checked_array<ItemHandle>(head_i1);
123 fd_12.tail = i2;
124 EXPECT_EQ(2, orderings.AddFunctionalDependency(thd, fd_12));
125 EXPECT_EQ(2, orderings.AddFunctionalDependency(thd, fd_12));
126
127 EXPECT_EQ(1, orderings.AddFunctionalDependency(thd, fd_equiv));
128
129 // Add i2 → i1. It is different from i1 → i2.
130 fd_12.head = Bounds_checked_array<ItemHandle>(head_i2);
131 fd_12.tail = i1;
132 EXPECT_EQ(3, orderings.AddFunctionalDependency(thd, fd_12));
133 }
134
135 TEST(InterestingOrderingTest, PruneFunctionalDependencies) {
136 my_testing::Server_initializer m_initializer;
137 m_initializer.SetUp();
138 THD *thd = m_initializer.thd();
139
140 LogicalOrderings orderings(thd);
141 ItemHandle i1 = orderings.GetHandle(new Item_int(1));
142 ItemHandle i2 = orderings.GetHandle(new Item_int(2));
143 ItemHandle i3 = orderings.GetHandle(new Item_int(3));
144 ItemHandle i4 = orderings.GetHandle(new Item_int(4));
145
146 // i1 and i2 are part of an interesting order.
147 array<OrderElement, 2> order_a{OrderElement{i1, ORDER_ASC},
148 OrderElement{i2, ORDER_ASC}};
149 EXPECT_EQ(1, AddOrdering(thd, order_a, /*interesting=*/true, &orderings));
150
151 // Add i1 -> i3. It should be pruned, since i3 is not part of
152 // an interesting order.
153 array<ItemHandle, 1> head_i1{i1};
154 FunctionalDependency fd_13;
155 fd_13.type = FunctionalDependency::FD;
156 fd_13.head = Bounds_checked_array<ItemHandle>(head_i1);
157 fd_13.tail = i3;
158 int fd_13_idx = orderings.AddFunctionalDependency(thd, fd_13);
159
160 // Add {} -> i1. It should be kept, since i1 is part of an interesting order.
161 FunctionalDependency fd_create_1;
162 fd_create_1.type = FunctionalDependency::FD;
163 fd_create_1.head = Bounds_checked_array<ItemHandle>();
164 fd_create_1.tail = i1;
165 int fd_create_1_idx = orderings.AddFunctionalDependency(thd, fd_create_1);
166
167 // Add {} → i4 and i2 = i4. These should both be kept, since i2 is part of
168 // and interesting order (and i2 = i4 counts as i4 → i2).
169 FunctionalDependency fd_create_4;
170 fd_create_4.type = FunctionalDependency::FD;
171 fd_create_4.head = Bounds_checked_array<ItemHandle>();
172 fd_create_4.tail = i4;
173 int fd_create_4_idx = orderings.AddFunctionalDependency(thd, fd_create_4);
174
175 array<ItemHandle, 1> head_i2{i2};
176 FunctionalDependency fd_24;
177 fd_24.type = FunctionalDependency::EQUIVALENCE;
178 fd_24.head = Bounds_checked_array<ItemHandle>(head_i2);
179 fd_24.tail = i4;
180 int fd_24_idx = orderings.AddFunctionalDependency(thd, fd_24);
181
182 string trace;
183 orderings.Build(thd, &trace);
184 SCOPED_TRACE(trace); // Prints out the trace on failure.
185
186 EXPECT_TRUE(orderings.GetFDSet(fd_13_idx).none());
187 EXPECT_FALSE(orderings.GetFDSet(fd_create_1_idx).none());
188 EXPECT_FALSE(orderings.GetFDSet(fd_create_4_idx).none());
189 EXPECT_FALSE(orderings.GetFDSet(fd_24_idx).none());
190 }
191
192 class InterestingOrderingTableTest : public ::testing::Test {
193 public:
194 InterestingOrderingTableTest() {
195 m_initializer.SetUp();
196 m_orderings.reset(new LogicalOrderings(m_initializer.thd()));
197
198 m_table.reset(new Fake_TABLE(/*num_columns=*/6, /*nullable=*/true));
199 m_table->field[0]->field_name = "a";
200 m_table->field[1]->field_name = "b";
201 m_table->field[2]->field_name = "c";
202 m_table->field[3]->field_name = "d";
203 m_table->field[4]->field_name = "e";
204 m_table->field[5]->field_name = "f";
205
206 a = m_orderings->GetHandle(new Item_field(m_table->field[0]));
207 b = m_orderings->GetHandle(new Item_field(m_table->field[1]));
208 c = m_orderings->GetHandle(new Item_field(m_table->field[2]));
209 d = m_orderings->GetHandle(new Item_field(m_table->field[3]));
210 e = m_orderings->GetHandle(new Item_field(m_table->field[4]));
211 // Don't add f; the tests can use it to get a higher handle
212 // than the others.
213 }
214
215 protected:
216 my_testing::Server_initializer m_initializer;
217 unique_ptr<LogicalOrderings> m_orderings;
218 unique_ptr<Fake_TABLE> m_table;
219 ItemHandle a, b, c, d, e;
220 };
221
222 TEST_F(InterestingOrderingTableTest, HomogenizeOrderings) {
223 THD *thd = m_initializer.thd();
224
225 // Add two tables, with some columns.
226 unique_ptr_destroy_only<Fake_TABLE> t1(
227 new (thd->mem_root) Fake_TABLE(/*num_columns=*/3, /*nullable=*/true));
228 t1->field[0]->field_name = "a";
229 t1->field[1]->field_name = "b";
230 t1->field[2]->field_name = "c";
231 ItemHandle t1_a = m_orderings->GetHandle(new Item_field(t1->field[0]));
232 ItemHandle t1_b = m_orderings->GetHandle(new Item_field(t1->field[1]));
233 ItemHandle t1_c = m_orderings->GetHandle(new Item_field(t1->field[2]));
234
235 unique_ptr_destroy_only<Fake_TABLE> t2(
236 new (thd->mem_root) Fake_TABLE(/*num_columns=*/3, /*nullable=*/true));
237 t2->field[0]->field_name = "a";
238 t2->field[1]->field_name = "b";
239 t2->field[2]->field_name = "c";
240 ItemHandle t2_a = m_orderings->GetHandle(new Item_field(t2->field[0]));
241 // t2_b is unused.
242 ItemHandle t2_c = m_orderings->GetHandle(new Item_field(t2->field[2]));
243
244 // Add t1.a = t2.a.
245 array<ItemHandle, 1> head_t1_a{t1_a};
246 FunctionalDependency fd_equiv;
247 fd_equiv.type = FunctionalDependency::EQUIVALENCE;
248 fd_equiv.head = Bounds_checked_array<ItemHandle>(head_t1_a);
249 fd_equiv.tail = t2_a;
250 m_orderings->AddFunctionalDependency(thd, fd_equiv);
251
252 // Add t1.a → t1.b.
253 FunctionalDependency fd_ab;
254 fd_ab.type = FunctionalDependency::FD;
255 fd_ab.head = Bounds_checked_array<ItemHandle>(head_t1_a);
256 fd_ab.tail = t1_b;
257 m_orderings->AddFunctionalDependency(thd, fd_ab);
258
259 // Set up the ordering (t1.a, t2.a). It should be homogenized into (t1.a)
260 // and (t2.a) due to the equivalence.
261 array<OrderElement, 2> order_aa{OrderElement{t1_a, ORDER_ASC},
262 OrderElement{t2_a, ORDER_ASC}};
263 EXPECT_EQ(
264 1, AddOrdering(thd, order_aa, /*interesting=*/true, m_orderings.get()));
265
266 // Add the ordering (t2.a, t1.b, t1.c↓). It should be homogenized into
267 // (t1.a, t1.c↓); the t1.b is optimized away due to the FD.
268 array<OrderElement, 3> order_abc{OrderElement{t2_a, ORDER_ASC},
269 OrderElement{t1_b, ORDER_ASC},
270 OrderElement{t1_c, ORDER_DESC}};
271 EXPECT_EQ(
272 2, AddOrdering(thd, order_abc, /*interesting=*/true, m_orderings.get()));
273
274 // And finally, (t1.a, t1.c, t2.a, t2.c), which cannot be homogenized
275 // onto a single table.
276 array<OrderElement, 4> order_acac{
277 OrderElement{t1_a, ORDER_ASC}, OrderElement{t1_c, ORDER_ASC},
278 OrderElement{t2_a, ORDER_ASC}, OrderElement{t2_c, ORDER_ASC}};
279 EXPECT_EQ(
280 3, AddOrdering(thd, order_acac, /*interesting=*/true, m_orderings.get()));
281
282 string trace;
283 m_orderings->Build(thd, &trace);
284 SCOPED_TRACE(trace); // Prints out the trace on failure.
285
286 ASSERT_EQ(7, m_orderings->num_orderings());
287
288 // (t1.a).
289 ASSERT_THAT(m_orderings->ordering(4).GetElements(),
290 testing::ElementsAre(OrderElement{t1_a, ORDER_ASC}));
291
292 // (t2.a).
293 ASSERT_THAT(m_orderings->ordering(5).GetElements(),
294 testing::ElementsAre(OrderElement{t2_a, ORDER_ASC}));
295
296 // (t1.a, t1.c↓).
297 ASSERT_THAT(m_orderings->ordering(6).GetElements(),
298 testing::ElementsAre(OrderElement{t1_a, ORDER_ASC},
299 OrderElement{t1_c, ORDER_DESC}));
300 }
301
302 TEST_F(InterestingOrderingTableTest, SetOrder) {
303 THD *thd = m_initializer.thd();
304
305 unique_ptr_destroy_only<Fake_TABLE> table(
306 new (thd->mem_root) Fake_TABLE(/*num_columns=*/3, /*nullable=*/true));
307 table->field[0]->field_name = "a";
308 table->field[1]->field_name = "b";
309 table->field[2]->field_name = "c";
310
311 ItemHandle a = m_orderings->GetHandle(new Item_field(table->field[0]));
312 ItemHandle b = m_orderings->GetHandle(new Item_field(table->field[1]));
313 ItemHandle c = m_orderings->GetHandle(new Item_field(table->field[2]));
314
315 // Interesting orders are a, a↓, b and bc.
316 array<OrderElement, 1> order_a{OrderElement{a, ORDER_ASC}};
317 array<OrderElement, 1> order_a_desc{OrderElement{a, ORDER_DESC}};
318 array<OrderElement, 1> order_b{OrderElement{b, ORDER_ASC}};
319 array<OrderElement, 2> order_bc{OrderElement{b, ORDER_ASC},
320 OrderElement{c, ORDER_ASC}};
321 int a_idx =
322 AddOrdering(thd, order_a, /*interesting=*/true, m_orderings.get());
323 int a_desc_idx =
324 AddOrdering(thd, order_a_desc, /*interesting=*/true, m_orderings.get());
325 int b_idx =
326 AddOrdering(thd, order_b, /*interesting=*/true, m_orderings.get());
327 int bc_idx =
328 AddOrdering(thd, order_bc, /*interesting=*/true, m_orderings.get());
329
330 string trace;
331 m_orderings->Build(thd, &trace);
332 SCOPED_TRACE(trace); // Prints out the trace on failure.
333
334 a_idx = m_orderings->RemapOrderingIndex(a_idx);
335 a_desc_idx = m_orderings->RemapOrderingIndex(a_desc_idx);
336 b_idx = m_orderings->RemapOrderingIndex(b_idx);
337 bc_idx = m_orderings->RemapOrderingIndex(bc_idx);
338
339 LogicalOrderings::StateIndex idx;
340
341 idx = m_orderings->SetOrder(a_idx);
342 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, a_idx));
343 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, a_desc_idx));
344 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, b_idx));
345 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, bc_idx));
346
347 idx = m_orderings->SetOrder(a_desc_idx);
348 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, bc_idx));
349 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, a_desc_idx));
350 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, b_idx));
351 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, a_idx));
352
353 idx = m_orderings->SetOrder(b_idx);
354 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, a_desc_idx));
355 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, a_idx));
356 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, b_idx));
357 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, bc_idx));
358
359 idx = m_orderings->SetOrder(bc_idx);
360 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, a_desc_idx));
361 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, a_idx));
362 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, b_idx));
363 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, bc_idx));
364 }
365
366 TEST_F(InterestingOrderingTableTest, BasicTest) {
367 THD *thd = m_initializer.thd();
368
369 // Interesting orders are ab, abc, de, abed.
370 array<OrderElement, 2> order_ab{OrderElement{a, ORDER_ASC},
371 OrderElement{b, ORDER_ASC}};
372 array<OrderElement, 3> order_abc{OrderElement{a, ORDER_ASC},
373 OrderElement{b, ORDER_ASC},
374 OrderElement{c, ORDER_ASC}};
375 array<OrderElement, 2> order_de{OrderElement{d, ORDER_ASC},
376 OrderElement{e, ORDER_ASC}};
377 array<OrderElement, 4> order_abed{
378 OrderElement{a, ORDER_ASC}, OrderElement{b, ORDER_ASC},
379 OrderElement{e, ORDER_ASC}, OrderElement{d, ORDER_ASC}};
380 int ab_idx =
381 AddOrdering(thd, order_ab, /*interesting=*/true, m_orderings.get());
382 int abc_idx =
383 AddOrdering(thd, order_abc, /*interesting=*/true, m_orderings.get());
384 int de_idx =
385 AddOrdering(thd, order_de, /*interesting=*/true, m_orderings.get());
386 int abed_idx =
387 AddOrdering(thd, order_abed, /*interesting=*/true, m_orderings.get());
388
389 // Add b=d.
390 array<ItemHandle, 1> head_b{b};
391 FunctionalDependency fd_equiv;
392 fd_equiv.type = FunctionalDependency::EQUIVALENCE;
393 fd_equiv.head = Bounds_checked_array<ItemHandle>(head_b);
394 fd_equiv.tail = d;
395 int fd_equiv_idx = m_orderings->AddFunctionalDependency(thd, fd_equiv);
396
397 // Add {a, b} → e.
398 array<ItemHandle, 2> head_ab{a, b};
399 FunctionalDependency fd_complex;
400 fd_complex.type = FunctionalDependency::FD;
401 fd_complex.head = Bounds_checked_array<ItemHandle>(head_ab);
402 fd_complex.tail = e;
403 int fd_complex_idx = m_orderings->AddFunctionalDependency(thd, fd_complex);
404
405 // Finally, add {} → a and {} → d.
406 array<ItemHandle, 0> head_empty{};
407
408 FunctionalDependency fd_empty_a;
409 fd_empty_a.type = FunctionalDependency::FD;
410 fd_empty_a.head = Bounds_checked_array<ItemHandle>(head_empty);
411 fd_empty_a.tail = a;
412 int fd_empty_a_idx = m_orderings->AddFunctionalDependency(thd, fd_empty_a);
413
414 FunctionalDependency fd_empty_d;
415 fd_empty_d.type = FunctionalDependency::FD;
416 fd_empty_d.head = Bounds_checked_array<ItemHandle>(head_empty);
417 fd_empty_d.tail = d;
418 int fd_empty_d_idx = m_orderings->AddFunctionalDependency(thd, fd_empty_d);
419
420 string trace;
421 m_orderings->Build(thd, &trace);
422 SCOPED_TRACE(trace); // Prints out the trace on failure.
423
424 ab_idx = m_orderings->RemapOrderingIndex(ab_idx);
425 abc_idx = m_orderings->RemapOrderingIndex(abc_idx);
426 de_idx = m_orderings->RemapOrderingIndex(de_idx);
427 abed_idx = m_orderings->RemapOrderingIndex(abed_idx);
428
429 LogicalOrderings::StateIndex idx;
430 FunctionalDependencySet fds{0};
431
432 // Start with the empty ordering.
433 idx = m_orderings->SetOrder(0);
434
435 // Apply {} → a and {} → d.
436 fds |= m_orderings->GetFDSet(fd_empty_a_idx);
437 fds |= m_orderings->GetFDSet(fd_empty_d_idx);
438 idx = m_orderings->ApplyFDs(idx, fds);
439
440 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, ab_idx));
441 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, abc_idx));
442 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, de_idx));
443 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, abed_idx));
444
445 // Apply b = d. Now we should follow ab.
446 FunctionalDependencySet backup_fds = fds;
447 fds |= m_orderings->GetFDSet(fd_equiv_idx);
448 LogicalOrderings::StateIndex idx2 = m_orderings->ApplyFDs(idx, fds);
449 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx2, ab_idx));
450 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx2, abc_idx));
451 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx2, de_idx));
452 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx2, abed_idx));
453
454 // Go back and instead apply {a, b} → e. It shouldn't matter much;
455 // no orders should match.
456 fds = backup_fds;
457 fds |= m_orderings->GetFDSet(fd_complex_idx);
458 idx = m_orderings->ApplyFDs(idx, fds);
459 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, ab_idx));
460 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, abc_idx));
461 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, de_idx));
462 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, abed_idx));
463
464 // Finally, apply b = d again. This should give us ab _and_ abed
465 // (since we now have {a, b}, we also follow e).
466 fds |= m_orderings->GetFDSet(fd_equiv_idx);
467 idx = m_orderings->ApplyFDs(idx, fds);
468 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, ab_idx));
469 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, abc_idx));
470 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, de_idx));
471 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, abed_idx));
472 }
473
474 TEST_F(InterestingOrderingTableTest, AddReverseElement) {
475 THD *thd = m_initializer.thd();
476
477 // Interesting orders are a, ab↓.
478 array<OrderElement, 1> order_a{OrderElement{a, ORDER_ASC}};
479 array<OrderElement, 2> order_ab{OrderElement{a, ORDER_ASC},
480 OrderElement{b, ORDER_DESC}};
481 int a_idx =
482 AddOrdering(thd, order_a, /*interesting=*/true, m_orderings.get());
483 int ab_idx =
484 AddOrdering(thd, order_ab, /*interesting=*/true, m_orderings.get());
485
486 // Add {a} → b.
487 array<ItemHandle, 1> head_a{a};
488 FunctionalDependency fd_ab;
489 fd_ab.type = FunctionalDependency::FD;
490 fd_ab.head = Bounds_checked_array<ItemHandle>(head_a);
491 fd_ab.tail = b;
492 int fd_ab_idx = m_orderings->AddFunctionalDependency(thd, fd_ab);
493
494 string trace;
495 m_orderings->Build(thd, &trace);
496 SCOPED_TRACE(trace); // Prints out the trace on failure.
497
498 a_idx = m_orderings->RemapOrderingIndex(a_idx);
499 ab_idx = m_orderings->RemapOrderingIndex(ab_idx);
500
501 LogicalOrderings::StateIndex idx;
502 FunctionalDependencySet fds{0};
503
504 // Start with a.
505 idx = m_orderings->SetOrder(a_idx);
506
507 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, a_idx));
508 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, ab_idx));
509
510 // Apply {a} → b, which should make us follow ab↓, too.
511 fds |= m_orderings->GetFDSet(fd_ab_idx);
512 idx = m_orderings->ApplyFDs(idx, fds);
513
514 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, a_idx));
515 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, ab_idx));
516 }
517
518 TEST_F(InterestingOrderingTableTest, AddReverseElementThroughEquivalences) {
519 THD *thd = m_initializer.thd();
520
521 // Interesting orders are a, ac↓.
522 array<OrderElement, 1> order_a{OrderElement{a, ORDER_ASC}};
523 array<OrderElement, 2> order_ac{OrderElement{a, ORDER_ASC},
524 OrderElement{c, ORDER_DESC}};
525 int a_idx =
526 AddOrdering(thd, order_a, /*interesting=*/true, m_orderings.get());
527 int ac_idx =
528 AddOrdering(thd, order_ac, /*interesting=*/true, m_orderings.get());
529
530 // Add {a} → b.
531 array<ItemHandle, 1> head_a{a};
532 FunctionalDependency fd_ab;
533 fd_ab.type = FunctionalDependency::FD;
534 fd_ab.head = Bounds_checked_array<ItemHandle>(head_a);
535 fd_ab.tail = b;
536 int fd_ab_idx = m_orderings->AddFunctionalDependency(thd, fd_ab);
537
538 // Add b = c.
539 array<ItemHandle, 1> head_b{b};
540 FunctionalDependency fd_equiv;
541 fd_equiv.type = FunctionalDependency::EQUIVALENCE;
542 fd_equiv.head = Bounds_checked_array<ItemHandle>(head_b);
543 fd_equiv.tail = c;
544 int fd_equiv_idx = m_orderings->AddFunctionalDependency(thd, fd_equiv);
545
546 string trace;
547 m_orderings->Build(thd, &trace);
548 SCOPED_TRACE(trace); // Prints out the trace on failure.
549
550 a_idx = m_orderings->RemapOrderingIndex(a_idx);
551 ac_idx = m_orderings->RemapOrderingIndex(ac_idx);
552
553 LogicalOrderings::StateIndex idx;
554 FunctionalDependencySet fds{0};
555
556 // Start with a, then add both FDs. We should get ac↓ by means of adding ab↓
557 // and then converting b to c; note that b↓ should be added even though it
558 // was never in an ordering.
559 idx = m_orderings->SetOrder(a_idx);
560 fds |= m_orderings->GetFDSet(fd_ab_idx);
561 fds |= m_orderings->GetFDSet(fd_equiv_idx);
562 idx = m_orderings->ApplyFDs(idx, fds);
563
564 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, a_idx));
565 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, ac_idx));
566 }
567
568 // Demonstrates that the prefix test should not prune away non-strict prefixes
569 // if it wants to avoid following additional edges.
570 TEST_F(InterestingOrderingTableTest, DoesNotStrictlyPruneOnPrefixes) {
571 THD *thd = m_initializer.thd();
572
573 // Interesting orders are abcd, dc.
574 array<OrderElement, 4> order_abcd{
575 OrderElement{a, ORDER_ASC}, OrderElement{b, ORDER_ASC},
576 OrderElement{c, ORDER_ASC}, OrderElement{d, ORDER_ASC}};
577 array<OrderElement, 2> order_dc{OrderElement{d, ORDER_ASC},
578 OrderElement{c, ORDER_ASC}};
579 int abcd_idx =
580 AddOrdering(thd, order_abcd, /*interesting=*/true, m_orderings.get());
581 int dc_idx =
582 AddOrdering(thd, order_dc, /*interesting=*/true, m_orderings.get());
583
584 // Add b=d.
585 array<ItemHandle, 1> head_b{b};
586 FunctionalDependency fd_equiv;
587 fd_equiv.type = FunctionalDependency::EQUIVALENCE;
588 fd_equiv.head = Bounds_checked_array<ItemHandle>(head_b);
589 fd_equiv.tail = d;
590 int fd_equiv_idx = m_orderings->AddFunctionalDependency(thd, fd_equiv);
591
592 // Add {} → a.
593 array<ItemHandle, 0> head_empty{};
594
595 FunctionalDependency fd_empty_a;
596 fd_empty_a.type = FunctionalDependency::FD;
597 fd_empty_a.head = Bounds_checked_array<ItemHandle>(head_empty);
598 fd_empty_a.tail = a;
599 int fd_empty_a_idx = m_orderings->AddFunctionalDependency(thd, fd_empty_a);
600
601 string trace;
602 m_orderings->Build(thd, &trace);
603 SCOPED_TRACE(trace); // Prints out the trace on failure.
604
605 dc_idx = m_orderings->RemapOrderingIndex(dc_idx);
606 abcd_idx = m_orderings->RemapOrderingIndex(abcd_idx);
607
608 LogicalOrderings::StateIndex idx;
609 FunctionalDependencySet fds{0};
610
611 // Start at dc, then apply b=d. This generates, among others, the order
612 // (bcd). It is not a prefix of the interesting order abcd, but still,
613 // we don't want to prune it out.
614 idx = m_orderings->SetOrder(dc_idx);
615
616 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, dc_idx));
617 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, abcd_idx));
618
619 fds |= m_orderings->GetFDSet(fd_equiv_idx);
620 idx = m_orderings->ApplyFDs(idx, fds);
621
622 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, dc_idx));
623 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, abcd_idx));
624
625 // Now apply {} → a. Note that we break the contract here and don't
626 // include b=d in the set of functional dependencies; this is to verify that
627 // the state machine didn't actually need to follow b=d again, which it would
628 // if the order (bcd) was pruned out earlier. (Then, we'd find it through
629 // generating (abc) first in this step, which _is_ a prefix, so this is not
630 // about correctness, only performance.)
631 fds.reset();
632 fds |= m_orderings->GetFDSet(fd_empty_a_idx);
633 idx = m_orderings->ApplyFDs(idx, fds);
634
635 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, dc_idx));
636 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, abcd_idx));
637 }
638
639 // Demonstrates that pruning must take equivalences into account.
640 TEST_F(InterestingOrderingTableTest, TwoEquivalences) {
641 THD *thd = m_initializer.thd();
642
643 // Interesting orders are abc, dec.
644 array<OrderElement, 3> order_abc{OrderElement{a, ORDER_ASC},
645 OrderElement{b, ORDER_ASC},
646 OrderElement{c, ORDER_ASC}};
647 array<OrderElement, 3> order_dec{OrderElement{d, ORDER_ASC},
648 OrderElement{e, ORDER_ASC},
649 OrderElement{c, ORDER_ASC}};
650 int abc_idx =
651 AddOrdering(thd, order_abc, /*interesting=*/true, m_orderings.get());
652 int dec_idx =
653 AddOrdering(thd, order_dec, /*interesting=*/true, m_orderings.get());
654
655 // Add a=d and b=e.
656 array<ItemHandle, 1> head_a{a};
657 FunctionalDependency fd_ad;
658 fd_ad.type = FunctionalDependency::EQUIVALENCE;
659 fd_ad.head = Bounds_checked_array<ItemHandle>(head_a);
660 fd_ad.tail = d;
661 int fd_ad_idx = m_orderings->AddFunctionalDependency(thd, fd_ad);
662
663 array<ItemHandle, 1> head_b{b};
664 FunctionalDependency fd_be;
665 fd_be.type = FunctionalDependency::EQUIVALENCE;
666 fd_be.head = Bounds_checked_array<ItemHandle>(head_b);
667 fd_be.tail = e;
668 int fd_be_idx = m_orderings->AddFunctionalDependency(thd, fd_be);
669
670 string trace;
671 m_orderings->Build(thd, &trace);
672 SCOPED_TRACE(trace); // Prints out the trace on failure.
673
674 abc_idx = m_orderings->RemapOrderingIndex(abc_idx);
675 dec_idx = m_orderings->RemapOrderingIndex(dec_idx);
676
677 LogicalOrderings::StateIndex idx;
678 FunctionalDependencySet fds{0};
679
680 // Start at abc, then apply both a=d and b=e. Now we should have dec.
681 // Note that if we did not take equivalences into account when pruning,
682 // we could prune away the intermediate dbc ordering and never reach dec.
683 idx = m_orderings->SetOrder(abc_idx);
684
685 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, abc_idx));
686 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, dec_idx));
687
688 fds |= m_orderings->GetFDSet(fd_ad_idx);
689 fds |= m_orderings->GetFDSet(fd_be_idx);
690 idx = m_orderings->ApplyFDs(idx, fds);
691
692 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, abc_idx));
693 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, dec_idx));
694 }
695
696 TEST_F(InterestingOrderingTableTest, SortByConst) {
697 THD *thd = m_initializer.thd();
698
699 // The only interesting order is ab.
700 array<OrderElement, 2> order_ab{OrderElement{a, ORDER_ASC},
701 OrderElement{b, ORDER_ASC}};
702 int ab_idx =
703 AddOrdering(thd, order_ab, /*interesting=*/true, m_orderings.get());
704
705 // Add b=c.
706 array<ItemHandle, 1> head_b{b};
707 FunctionalDependency fd_equiv;
708 fd_equiv.type = FunctionalDependency::EQUIVALENCE;
709 fd_equiv.head = Bounds_checked_array<ItemHandle>(head_b);
710 fd_equiv.tail = c;
711 int fd_equiv_idx = m_orderings->AddFunctionalDependency(thd, fd_equiv);
712
713 // Finally, add {} → a and {} → c.
714 array<ItemHandle, 0> head_empty{};
715
716 FunctionalDependency fd_empty_a;
717 fd_empty_a.type = FunctionalDependency::FD;
718 fd_empty_a.head = Bounds_checked_array<ItemHandle>(head_empty);
719 fd_empty_a.tail = a;
720 int fd_empty_a_idx = m_orderings->AddFunctionalDependency(thd, fd_empty_a);
721
722 FunctionalDependency fd_empty_c;
723 fd_empty_c.type = FunctionalDependency::FD;
724 fd_empty_c.head = Bounds_checked_array<ItemHandle>(head_empty);
725 fd_empty_c.tail = c;
726 int fd_empty_c_idx = m_orderings->AddFunctionalDependency(thd, fd_empty_c);
727
728 string trace;
729 m_orderings->Build(thd, &trace);
730 SCOPED_TRACE(trace); // Prints out the trace on failure.
731
732 ab_idx = m_orderings->RemapOrderingIndex(ab_idx);
733
734 // Start with the empty ordering.
735 LogicalOrderings::StateIndex idx = m_orderings->SetOrder(0);
736 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, ab_idx));
737
738 // If we do WHERE b=c AND a=<const> AND c=<const>, we should get (ab).
739 FunctionalDependencySet fds{0};
740 fds |= m_orderings->GetFDSet(fd_equiv_idx);
741 fds |= m_orderings->GetFDSet(fd_empty_a_idx);
742 fds |= m_orderings->GetFDSet(fd_empty_c_idx);
743 idx = m_orderings->ApplyFDs(idx, fds);
744
745 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, ab_idx));
746 }
747
748 TEST_F(InterestingOrderingTableTest, AlwaysActiveFD) {
749 THD *thd = m_initializer.thd();
750
751 // Interesting orderings are ab and b.
752 array<OrderElement, 2> order_ab{OrderElement{a, ORDER_ASC},
753 OrderElement{b, ORDER_ASC}};
754 array<OrderElement, 1> order_b{OrderElement{a, ORDER_ASC}};
755 int ab_idx =
756 AddOrdering(thd, order_ab, /*interesting=*/true, m_orderings.get());
757 int b_idx =
758 AddOrdering(thd, order_b, /*interesting=*/true, m_orderings.get());
759
760 // Add {} → a and {} → b, but the former is always active.
761 array<ItemHandle, 0> head_empty{};
762
763 FunctionalDependency fd_empty_a;
764 fd_empty_a.type = FunctionalDependency::FD;
765 fd_empty_a.head = Bounds_checked_array<ItemHandle>(head_empty);
766 fd_empty_a.tail = a;
767 fd_empty_a.always_active = true;
768 int fd_empty_a_idx = m_orderings->AddFunctionalDependency(thd, fd_empty_a);
769
770 FunctionalDependency fd_empty_b;
771 fd_empty_b.type = FunctionalDependency::FD;
772 fd_empty_b.head = Bounds_checked_array<ItemHandle>(head_empty);
773 fd_empty_b.tail = b;
774 int fd_empty_b_idx = m_orderings->AddFunctionalDependency(thd, fd_empty_b);
775
776 array<ItemHandle, 1> head_a{a};
777 FunctionalDependency fd_equiv;
778 fd_equiv.type = FunctionalDependency::EQUIVALENCE;
779 fd_equiv.head = Bounds_checked_array<ItemHandle>(head_a);
780 fd_equiv.tail = b;
781 int fd_equiv_idx = m_orderings->AddFunctionalDependency(thd, fd_equiv);
782
783 string trace;
784 m_orderings->Build(thd, &trace);
785 SCOPED_TRACE(trace); // Prints out the trace on failure.
786
787 ab_idx = m_orderings->RemapOrderingIndex(ab_idx);
788 b_idx = m_orderings->RemapOrderingIndex(b_idx);
789
790 // Start with the empty ordering.
791 LogicalOrderings::StateIndex idx = m_orderings->SetOrder(0);
792 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, ab_idx));
793
794 // Now we should get ab simply by means of {} → b, since a is always-active.
795 // Note that in a sense, the code here can cheat, because it can reduce ab to
796 // b ahead of time if it wants. However, this does not hold for the next test.
797 FunctionalDependencySet fds = m_orderings->GetFDSet(fd_empty_b_idx);
798 idx = m_orderings->ApplyFDs(idx, fds);
799 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, ab_idx));
800
801 // Restart, then apply a = b. This should give us b.
802 idx = m_orderings->SetOrder(0);
803 fds = m_orderings->GetFDSet(fd_equiv_idx);
804 idx = m_orderings->ApplyFDs(idx, fds);
805 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, b_idx));
806
807 // The always-on FD should have no bitmap, so that we don't waste time trying
808 // to follow it at runtime.
809 EXPECT_TRUE(m_orderings->GetFDSet(fd_empty_a_idx).none());
810 EXPECT_FALSE(m_orderings->GetFDSet(fd_empty_b_idx).none());
811 }
812
813 TEST_F(InterestingOrderingTableTest, FDsFromComputedItems) {
814 THD *thd = m_initializer.thd();
815
816 // Add a new item for b + 1.
817 Item *bplus1_item =
818 new Item_func_plus(new Item_field(m_table->field[1]), new Item_int(1));
819 bplus1_item->update_used_tables();
820 ItemHandle bplus1 = m_orderings->GetHandle(bplus1_item);
821
822 // The interesting orders are a and a, b + 1.
823 array<OrderElement, 1> order_a{OrderElement{a, ORDER_ASC}};
824 array<OrderElement, 2> order_ab{OrderElement{a, ORDER_ASC},
825 OrderElement{bplus1, ORDER_ASC}};
826 int a_idx =
827 AddOrdering(thd, order_a, /*interesting=*/true, m_orderings.get());
828 int ab_idx =
829 AddOrdering(thd, order_ab, /*interesting=*/true, m_orderings.get());
830
831 // Add a → b, which is always active.
832 array<ItemHandle, 1> head_a{a};
833 FunctionalDependency fd_ab;
834 fd_ab.type = FunctionalDependency::FD;
835 fd_ab.head = Bounds_checked_array<ItemHandle>(head_a);
836 fd_ab.tail = b;
837 fd_ab.always_active = true;
838 int fd_ab_idx = m_orderings->AddFunctionalDependency(thd, fd_ab);
839
840 string trace;
841 m_orderings->Build(thd, &trace);
842 SCOPED_TRACE(trace); // Prints out the trace on failure.
843
844 a_idx = m_orderings->RemapOrderingIndex(a_idx);
845 ab_idx = m_orderings->RemapOrderingIndex(ab_idx);
846
847 // Start with a. Now we should also have a, b + 1 (there should be
848 // an implict b → b + 1 FD), even though b is not in the ordering.
849 LogicalOrderings::StateIndex idx = m_orderings->SetOrder(a_idx);
850 idx = m_orderings->ApplyFDs(idx, m_orderings->GetFDSet(fd_ab_idx));
851 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, a_idx));
852 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, ab_idx));
853 }
854
855 TEST_F(InterestingOrderingTableTest, MoreOrderedThan) {
856 THD *thd = m_initializer.thd();
857
858 // Interesting orders a, ab, c.
859 array<OrderElement, 1> order_a{OrderElement{a, ORDER_ASC}};
860 array<OrderElement, 2> order_ab{OrderElement{a, ORDER_ASC},
861 OrderElement{b, ORDER_ASC}};
862 array<OrderElement, 1> order_c{OrderElement{c, ORDER_ASC}};
863 int a_order_idx =
864 AddOrdering(thd, order_a, /*interesting=*/true, m_orderings.get());
865 int ab_order_idx =
866 AddOrdering(thd, order_ab, /*interesting=*/true, m_orderings.get());
867 int c_order_idx =
868 AddOrdering(thd, order_c, /*interesting=*/true, m_orderings.get());
869
870 // Add a=c.
871 array<ItemHandle, 1> head_a{a};
872 FunctionalDependency fd_equiv;
873 fd_equiv.type = FunctionalDependency::EQUIVALENCE;
874 fd_equiv.head = Bounds_checked_array<ItemHandle>(head_a);
875 fd_equiv.tail = c;
876 int fd_equiv_idx = m_orderings->AddFunctionalDependency(thd, fd_equiv);
877
878 string trace;
879 m_orderings->Build(thd, &trace);
880 SCOPED_TRACE(trace); // Prints out the trace on failure.
881
882 a_order_idx = m_orderings->RemapOrderingIndex(a_order_idx);
883 ab_order_idx = m_orderings->RemapOrderingIndex(ab_order_idx);
884 c_order_idx = m_orderings->RemapOrderingIndex(c_order_idx);
885
886 // Start at a and apply a = c, which should give us a and c.
887 LogicalOrderings::StateIndex ac_idx = m_orderings->SetOrder(a_order_idx);
888 FunctionalDependencySet fds{0};
889 fds |= m_orderings->GetFDSet(fd_equiv_idx);
890 ac_idx = m_orderings->ApplyFDs(ac_idx, fds);
891
892 LogicalOrderings::StateIndex empty_idx = m_orderings->SetOrder(0);
893 LogicalOrderings::StateIndex a_idx = m_orderings->SetOrder(a_order_idx);
894 LogicalOrderings::StateIndex ab_idx = m_orderings->SetOrder(ab_order_idx);
895 LogicalOrderings::StateIndex c_idx = m_orderings->SetOrder(c_order_idx);
896
897 EXPECT_FALSE(m_orderings->MoreOrderedThan(empty_idx, empty_idx, 0));
898 EXPECT_FALSE(m_orderings->MoreOrderedThan(empty_idx, a_idx, 0));
899 EXPECT_FALSE(m_orderings->MoreOrderedThan(empty_idx, ab_idx, 0));
900 EXPECT_FALSE(m_orderings->MoreOrderedThan(empty_idx, c_idx, 0));
901 EXPECT_FALSE(m_orderings->MoreOrderedThan(empty_idx, ac_idx, 0));
902
903 EXPECT_TRUE(m_orderings->MoreOrderedThan(a_idx, empty_idx, 0));
904 EXPECT_FALSE(m_orderings->MoreOrderedThan(a_idx, a_idx, 0));
905 EXPECT_FALSE(m_orderings->MoreOrderedThan(a_idx, ab_idx, 0));
906 EXPECT_TRUE(m_orderings->MoreOrderedThan(a_idx, c_idx, 0));
907 EXPECT_FALSE(m_orderings->MoreOrderedThan(a_idx, ac_idx, 0));
908
909 EXPECT_TRUE(m_orderings->MoreOrderedThan(ab_idx, empty_idx, 0));
910 EXPECT_TRUE(m_orderings->MoreOrderedThan(ab_idx, a_idx, 0));
911 EXPECT_FALSE(m_orderings->MoreOrderedThan(ab_idx, ab_idx, 0));
912 EXPECT_TRUE(m_orderings->MoreOrderedThan(ab_idx, c_idx, 0));
913 EXPECT_TRUE(m_orderings->MoreOrderedThan(ab_idx, ac_idx, 0));
914
915 EXPECT_TRUE(m_orderings->MoreOrderedThan(c_idx, empty_idx, 0));
916 EXPECT_TRUE(m_orderings->MoreOrderedThan(c_idx, a_idx, 0));
917 EXPECT_TRUE(m_orderings->MoreOrderedThan(c_idx, ab_idx, 0));
918 EXPECT_FALSE(m_orderings->MoreOrderedThan(c_idx, c_idx, 0));
919 EXPECT_FALSE(m_orderings->MoreOrderedThan(c_idx, ac_idx, 0));
920
921 EXPECT_TRUE(m_orderings->MoreOrderedThan(ac_idx, empty_idx, 0));
922 EXPECT_TRUE(m_orderings->MoreOrderedThan(ac_idx, a_idx, 0));
923 EXPECT_TRUE(m_orderings->MoreOrderedThan(ac_idx, ab_idx, 0));
924 EXPECT_TRUE(m_orderings->MoreOrderedThan(ac_idx, c_idx, 0));
925 EXPECT_FALSE(m_orderings->MoreOrderedThan(ac_idx, ac_idx, 0));
926
927 // If we don't care about (a) anymore (e.g. because it was a merge join
928 // that has since passed), it should be ignored in comparisons.
929 std::bitset<kMaxSupportedOrderings> ignored;
930 ignored.set(a_order_idx);
931
932 // Still true, because it could become c.
933 EXPECT_TRUE(m_orderings->MoreOrderedThan(a_idx, empty_idx, ignored));
934
935 EXPECT_FALSE(m_orderings->MoreOrderedThan(a_idx, a_idx, ignored));
936 EXPECT_FALSE(m_orderings->MoreOrderedThan(a_idx, ab_idx, ignored));
937 EXPECT_FALSE(m_orderings->MoreOrderedThan(a_idx, c_idx, ignored));
938 EXPECT_FALSE(m_orderings->MoreOrderedThan(a_idx, ac_idx, ignored));
939
940 EXPECT_TRUE(m_orderings->MoreOrderedThan(ab_idx, a_idx, ignored));
941 }
942
943 TEST_F(InterestingOrderingTableTest, HomogenizedOrderingsAreEquallyGood) {
944 THD *thd = m_initializer.thd();
945
946 // Add three tables, with one column each.
947 unique_ptr_destroy_only<Fake_TABLE> t1(
948 new (thd->mem_root) Fake_TABLE(/*num_columns=*/1, /*nullable=*/true));
949 t1->field[0]->field_name = "t1.a";
950 ItemHandle t1_a = m_orderings->GetHandle(new Item_field(t1->field[0]));
951
952 unique_ptr_destroy_only<Fake_TABLE> t2(
953 new (thd->mem_root) Fake_TABLE(/*num_columns=*/1, /*nullable=*/true));
954 t2->field[0]->field_name = "t2.a";
955 ItemHandle t2_a = m_orderings->GetHandle(new Item_field(t2->field[0]));
956
957 unique_ptr_destroy_only<Fake_TABLE> t3(
958 new (thd->mem_root) Fake_TABLE(/*num_columns=*/1, /*nullable=*/true));
959 t3->field[0]->field_name = "t3.a";
960 ItemHandle t3_a = m_orderings->GetHandle(new Item_field(t3->field[0]));
961
962 // And t1.a = t2.a.
963 array<ItemHandle, 1> head_t1_a{t1_a};
964 FunctionalDependency fd_12;
965 fd_12.type = FunctionalDependency::EQUIVALENCE;
966 fd_12.head = Bounds_checked_array<ItemHandle>(head_t1_a);
967 fd_12.tail = t2_a;
968 m_orderings->AddFunctionalDependency(thd, fd_12);
969
970 // And t1.a = t3.a.
971 FunctionalDependency fd_13;
972 fd_13.type = FunctionalDependency::EQUIVALENCE;
973 fd_13.head = Bounds_checked_array<ItemHandle>(head_t1_a);
974 fd_13.tail = t3_a;
975 m_orderings->AddFunctionalDependency(thd, fd_13);
976
977 // Set up the ordering (t1.a). It should be homogenized into (t2.a)
978 // and (t3.a) due to the equivalence.
979 array<OrderElement, 1> order_a{OrderElement{t1_a, ORDER_ASC}};
980 EXPECT_EQ(1, AddOrdering(thd, order_a,
981 /*interesting=*/true, m_orderings.get()));
982
983 string trace;
984 m_orderings->Build(thd, &trace);
985 SCOPED_TRACE(trace); // Prints out the trace on failure.
986
987 // Just make sure we have the right indexes.
988 ASSERT_EQ(4, m_orderings->num_orderings());
989 ASSERT_THAT(m_orderings->ordering(1).GetElements(),
990 testing::ElementsAre(OrderElement{t1_a, ORDER_ASC}));
991 ASSERT_THAT(m_orderings->ordering(2).GetElements(),
992 testing::ElementsAre(OrderElement{t2_a, ORDER_ASC}));
993 ASSERT_THAT(m_orderings->ordering(3).GetElements(),
994 testing::ElementsAre(OrderElement{t3_a, ORDER_ASC}));
995 LogicalOrderings::StateIndex empty_idx = m_orderings->SetOrder(0);
996 LogicalOrderings::StateIndex t1a_idx = m_orderings->SetOrder(1);
997 LogicalOrderings::StateIndex t2a_idx = m_orderings->SetOrder(2);
998 LogicalOrderings::StateIndex t3a_idx = m_orderings->SetOrder(3);
999
1000 // (t1.a) is better than both (t2.a) and (t3.a), but the two are,
1001 // crucially, equivalent to each other.
1002 EXPECT_TRUE(m_orderings->MoreOrderedThan(t1a_idx, t2a_idx, 0));
1003 EXPECT_TRUE(m_orderings->MoreOrderedThan(t1a_idx, t3a_idx, 0));
1004
1005 EXPECT_FALSE(m_orderings->MoreOrderedThan(t2a_idx, t3a_idx, 0));
1006 EXPECT_FALSE(m_orderings->MoreOrderedThan(t3a_idx, t2a_idx, 0));
1007
1008 // However, both of them should be more interesting than nothing.
1009 EXPECT_TRUE(m_orderings->MoreOrderedThan(t2a_idx, empty_idx, 0));
1010 EXPECT_TRUE(m_orderings->MoreOrderedThan(t3a_idx, empty_idx, 0));
1011 }
1012
1013 TEST_F(InterestingOrderingTableTest, PruneUninterestingOrders) {
1014 THD *thd = m_initializer.thd();
1015
1016 unique_ptr_destroy_only<Fake_TABLE> table(
1017 new (thd->mem_root) Fake_TABLE(/*num_columns=*/3, /*nullable=*/true));
1018 table->field[0]->field_name = "a";
1019 table->field[1]->field_name = "b";
1020 table->field[2]->field_name = "c";
1021
1022 ItemHandle a = m_orderings->GetHandle(new Item_field(table->field[0]));
1023 ItemHandle b = m_orderings->GetHandle(new Item_field(table->field[1]));
1024 ItemHandle c = m_orderings->GetHandle(new Item_field(table->field[2]));
1025
1026 // Interesting orders are a and bc.
1027 array<OrderElement, 1> order_a{OrderElement{a, ORDER_ASC}};
1028 array<OrderElement, 2> order_bc{OrderElement{b, ORDER_ASC},
1029 OrderElement{c, ORDER_ASC}};
1030 int a_idx =
1031 AddOrdering(thd, order_a, /*interesting=*/true, m_orderings.get());
1032 int bc_idx =
1033 AddOrdering(thd, order_bc, /*interesting=*/true, m_orderings.get());
1034
1035 // Add b → c.
1036 array<ItemHandle, 1> head_b{b};
1037 FunctionalDependency fd_bc;
1038 fd_bc.type = FunctionalDependency::FD;
1039 fd_bc.head = Bounds_checked_array<ItemHandle>(head_b);
1040 fd_bc.tail = c;
1041 m_orderings->AddFunctionalDependency(thd, fd_bc);
1042
1043 // Uninteresting orders are b, c and abc. c should be pruned away,
1044 // since there's no way we can reach anything interesting,
1045 // but b should be kept, since it could become bc. abc should be
1046 // shortened and deduplicated into a.
1047 array<OrderElement, 1> order_b{OrderElement{b, ORDER_ASC}};
1048 array<OrderElement, 1> order_c{OrderElement{c, ORDER_ASC}};
1049 array<OrderElement, 3> order_abc{OrderElement{a, ORDER_ASC},
1050 OrderElement{b, ORDER_ASC},
1051 OrderElement{c, ORDER_ASC}};
1052 int b_idx =
1053 AddOrdering(thd, order_b, /*interesting=*/false, m_orderings.get());
1054 int c_idx =
1055 AddOrdering(thd, order_c, /*interesting=*/false, m_orderings.get());
1056 int abc_idx =
1057 AddOrdering(thd, order_abc, /*interesting=*/false, m_orderings.get());
1058
1059 string trace;
1060 m_orderings->Build(thd, &trace);
1061 SCOPED_TRACE(trace); // Prints out the trace on failure.
1062
1063 EXPECT_EQ(0, m_orderings->RemapOrderingIndex(c_idx));
1064 EXPECT_NE(0, m_orderings->RemapOrderingIndex(b_idx));
1065 EXPECT_NE(m_orderings->RemapOrderingIndex(bc_idx),
1066 m_orderings->RemapOrderingIndex(b_idx));
1067 EXPECT_EQ(m_orderings->RemapOrderingIndex(a_idx),
1068 m_orderings->RemapOrderingIndex(abc_idx));
1069 }
1070
1071 TEST_F(InterestingOrderingTableTest, Groupings) {
1072 THD *thd = m_initializer.thd();
1073
1074 // Interesting orders are ab, {a} and {abc} ({} means grouping).
1075 array<OrderElement, 2> order_ab{OrderElement{a, ORDER_ASC},
1076 OrderElement{b, ORDER_ASC}};
1077 array<OrderElement, 1> group_a{OrderElement{a, ORDER_NOT_RELEVANT}};
1078 array<OrderElement, 3> group_abc{OrderElement{a, ORDER_NOT_RELEVANT},
1079 OrderElement{b, ORDER_NOT_RELEVANT},
1080 OrderElement{c, ORDER_NOT_RELEVANT}};
1081 int ab_idx =
1082 AddOrdering(thd, order_ab, /*interesting=*/true, m_orderings.get());
1083 int group_a_idx =
1084 AddOrdering(thd, group_a, /*interesting=*/true, m_orderings.get());
1085 int group_abc_idx =
1086 AddOrdering(thd, group_abc, /*interesting=*/true, m_orderings.get());
1087
1088 // Add b → c.
1089 array<ItemHandle, 1> head_b{b};
1090
1091 FunctionalDependency fd_bc;
1092 fd_bc.type = FunctionalDependency::FD;
1093 fd_bc.head = Bounds_checked_array<ItemHandle>(head_b);
1094 fd_bc.tail = c;
1095 int fd_bc_idx = m_orderings->AddFunctionalDependency(thd, fd_bc);
1096
1097 string trace;
1098 m_orderings->Build(thd, &trace);
1099 SCOPED_TRACE(trace); // Prints out the trace on failure.
1100
1101 ab_idx = m_orderings->RemapOrderingIndex(ab_idx);
1102 group_a_idx = m_orderings->RemapOrderingIndex(group_a_idx);
1103 group_abc_idx = m_orderings->RemapOrderingIndex(group_abc_idx);
1104
1105 // Start at ab.
1106 LogicalOrderings::StateIndex idx = m_orderings->SetOrder(ab_idx);
1107 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, ab_idx));
1108 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, group_a_idx));
1109 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, group_abc_idx));
1110
1111 // Apply b → c.
1112 idx = m_orderings->ApplyFDs(idx, m_orderings->GetFDSet(fd_bc_idx));
1113 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, ab_idx));
1114 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, group_a_idx));
1115 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, group_abc_idx));
1116 }
1117
1118 TEST_F(InterestingOrderingTableTest, UninterestingOrderingsCanBecomeGroupings) {
1119 THD *thd = m_initializer.thd();
1120
1121 // {ac} is interesting, cba is uninteresting. We should be able to
1122 // build the former from the latter with c → a FD (see below).
1123 array<OrderElement, 3> order_cba{OrderElement{c, ORDER_ASC},
1124 OrderElement{b, ORDER_ASC},
1125 OrderElement{a, ORDER_ASC}};
1126 array<OrderElement, 2> group_ac{OrderElement{a, ORDER_NOT_RELEVANT},
1127 OrderElement{c, ORDER_NOT_RELEVANT}};
1128 int cba_idx =
1129 AddOrdering(thd, order_cba, /*interesting=*/false, m_orderings.get());
1130 int group_ac_idx =
1131 AddOrdering(thd, group_ac, /*interesting=*/true, m_orderings.get());
1132
1133 // Add c → a.
1134 array<ItemHandle, 1> head_c{c};
1135
1136 FunctionalDependency fd_ca;
1137 fd_ca.type = FunctionalDependency::FD;
1138 fd_ca.head = Bounds_checked_array<ItemHandle>(head_c);
1139 fd_ca.tail = a;
1140 int fd_ca_idx = m_orderings->AddFunctionalDependency(thd, fd_ca);
1141
1142 string trace;
1143 m_orderings->Build(thd, &trace);
1144 SCOPED_TRACE(trace); // Prints out the trace on failure.
1145
1146 // cba should not be pruned away entirely, since we can use (c) to convert
1147 // into {c} and then continue on to {ac} later.
1148 cba_idx = m_orderings->RemapOrderingIndex(cba_idx);
1149 group_ac_idx = m_orderings->RemapOrderingIndex(group_ac_idx);
1150
1151 EXPECT_NE(0, cba_idx);
1152 EXPECT_NE(0, group_ac_idx);
1153
1154 // Start at cba.
1155 LogicalOrderings::StateIndex idx = m_orderings->SetOrder(cba_idx);
1156 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, group_ac_idx));
1157
1158 // Apply c → a.
1159 idx = m_orderings->ApplyFDs(idx, m_orderings->GetFDSet(fd_ca_idx));
1160 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, group_ac_idx));
1161 }
1162
1163 TEST_F(InterestingOrderingTableTest, GroupCover) {
1164 THD *thd = m_initializer.thd();
1165
1166 // Interesting orders are {abc}, {d} and (b↓a).
1167 array<OrderElement, 3> group_abc{OrderElement{a, ORDER_NOT_RELEVANT},
1168 OrderElement{b, ORDER_NOT_RELEVANT},
1169 OrderElement{c, ORDER_NOT_RELEVANT}};
1170 array<OrderElement, 1> group_d{OrderElement{d, ORDER_NOT_RELEVANT}};
1171 array<OrderElement, 2> order_ba{OrderElement{b, ORDER_DESC},
1172 OrderElement{a, ORDER_ASC}};
1173
1174 AddOrdering(thd, group_abc, /*interesting=*/true, m_orderings.get());
1175 AddOrdering(thd, group_d, /*interesting=*/true, m_orderings.get());
1176 AddOrdering(thd, order_ba, /*interesting=*/true, m_orderings.get());
1177
1178 string trace;
1179 m_orderings->Build(thd, &trace);
1180 SCOPED_TRACE(trace); // Prints out the trace on failure.
1181
1182 // We should have two new orderings: (b↓ac) and (d).
1183 ASSERT_EQ(6, m_orderings->num_orderings());
1184
1185 // (b↓ac).
1186 EXPECT_THAT(m_orderings->ordering(4).GetElements(),
1187 testing::ElementsAre(OrderElement{b, ORDER_DESC},
1188 OrderElement{a, ORDER_ASC},
1189 OrderElement{c, ORDER_ASC}));
1190
1191 // (d).
1192 EXPECT_THAT(m_orderings->ordering(5).GetElements(),
1193 testing::ElementsAre(OrderElement{d, ORDER_ASC}));
1194 }
1195
1196 TEST_F(InterestingOrderingTableTest, NoGroupCoverWithNondeterminism) {
1197 THD *thd = m_initializer.thd();
1198
1199 Item_func *r_item =
1200 new Item_func_plus(new Item_int(2),
1201 new Item_int(2)); // Guaranteed random.
1202 r_item->set_used_tables(RAND_TABLE_BIT); // Chosen by fair die roll.
1203 ItemHandle r = m_orderings->GetHandle(r_item);
1204
1205 // Get a new field that's higher than r, so that the grouping below
1206 // is valid.
1207 ItemHandle f = m_orderings->GetHandle(new Item_field(m_table->field[5]));
1208
1209 // Interesting orders are {rf} and (f).
1210 array<OrderElement, 2> group_rf{OrderElement{r, ORDER_NOT_RELEVANT},
1211 OrderElement{f, ORDER_NOT_RELEVANT}};
1212 array<OrderElement, 1> order_f{OrderElement{f, ORDER_ASC}};
1213
1214 int group_rf_idx =
1215 AddOrdering(thd, group_rf, /*interesting=*/true, m_orderings.get());
1216 int f_idx =
1217 AddOrdering(thd, order_f, /*interesting=*/true, m_orderings.get());
1218
1219 string trace;
1220 m_orderings->Build(thd, &trace);
1221 SCOPED_TRACE(trace); // Prints out the trace on failure.
1222
1223 // We will have covered {rf} with (fr), but that ordering should
1224 // _not_ be used to satisfy (f). In this case, (rf) would also be
1225 // an acceptable cover, but we don't constrain the cover logic;
1226 // there's not really any need.
1227 ASSERT_EQ(4, m_orderings->num_orderings());
1228 EXPECT_THAT(m_orderings->ordering(3).GetElements(),
1229 testing::ElementsAre(OrderElement{f, ORDER_ASC},
1230 OrderElement{r, ORDER_ASC}));
1231
1232 int idx = m_orderings->SetOrder(3);
1233 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, group_rf_idx));
1234 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, f_idx));
1235 }
1236
1237 TEST_F(InterestingOrderingTableTest, GroupReordering) {
1238 THD *thd = m_initializer.thd();
1239
1240 // Interesting orders are (b) and {bc}.
1241 array<OrderElement, 1> order_b{OrderElement{b, ORDER_ASC}};
1242 array<OrderElement, 2> group_bc{OrderElement{b, ORDER_NOT_RELEVANT},
1243 OrderElement{c, ORDER_NOT_RELEVANT}};
1244
1245 int b_idx =
1246 AddOrdering(thd, order_b, /*interesting=*/true, m_orderings.get());
1247 int bc_idx =
1248 AddOrdering(thd, group_bc, /*interesting=*/true, m_orderings.get());
1249
1250 // Add a = c.
1251 FunctionalDependency fd_equiv;
1252 fd_equiv.type = FunctionalDependency::EQUIVALENCE;
1253 fd_equiv.head = Bounds_checked_array<ItemHandle>(&a, 1);
1254 fd_equiv.tail = c;
1255 int fd_equiv_idx = m_orderings->AddFunctionalDependency(thd, fd_equiv);
1256
1257 // Add b → a.
1258 FunctionalDependency fd_ba;
1259 fd_ba.type = FunctionalDependency::FD;
1260 fd_ba.head = Bounds_checked_array<ItemHandle>(&b, 1);
1261 fd_ba.tail = a;
1262 int fd_ba_idx = m_orderings->AddFunctionalDependency(thd, fd_ba);
1263
1264 string trace;
1265 m_orderings->Build(thd, &trace);
1266 SCOPED_TRACE(trace); // Prints out the trace on failure.
1267
1268 b_idx = m_orderings->RemapOrderingIndex(b_idx);
1269 bc_idx = m_orderings->RemapOrderingIndex(bc_idx);
1270
1271 // Start with (b).
1272 int idx = m_orderings->SetOrder(b_idx);
1273 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, b_idx));
1274 EXPECT_FALSE(m_orderings->DoesFollowOrder(idx, bc_idx));
1275
1276 // Apply both FDs.
1277 FunctionalDependencySet fds{0};
1278 fds |= m_orderings->GetFDSet(fd_equiv_idx);
1279 fds |= m_orderings->GetFDSet(fd_ba_idx);
1280 idx = m_orderings->ApplyFDs(idx, fds);
1281
1282 // Now we should also follow {b,c}. Note that this requires us
1283 // either to create {b,a}, which follows a counterintuitive group
1284 // canonicalization (the intuitive would be {a,b}), or internally
1285 // represent {b,c} as {c,b}. Otherwise, we would be pruning away
1286 // the the {a,b} (or {b,a}) grouping before reaching {b,c}.
1287 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, b_idx));
1288 EXPECT_TRUE(m_orderings->DoesFollowOrder(idx, bc_idx));
1289 }
1290
1291 // Measures the time to build the interesting orders for this query:
1292 //
1293 // SELECT col1, col2, ... , colN, COUNT(*)
1294 // FROM t1 JOIN t2 USING (col1, col2, ... , colN)
1295 // GROUP BY col1, col2, ... , colN
1296 // ORDER BY col1, col2, ... , colN
1297 //
1298 // It used to spend a lot of time in LogicalOrderings::PruneNFSM() when N was
1299 // high and the generated NFSM was large. The number of NFSM states generated
1300 // for this query is 2^(N+2)-3. Let's consider the case when N=2:
1301 //
1302 // There is one state for the empty ordering. There is one interesting order
1303 // given by the ORDER BY clause (col1, col2), but due to the functional
1304 // dependencies given by the join predicate, colN could expand to either t1.colN
1305 // or t2.colN, so we get four states (t1.col1, t1.col2), (t1.col1, t2.col2),
1306 // (t2.col1, t1.col2), (t2.col1, t2.col2). Additionally, each of these states
1307 // have decay edges to shorter orderings by removing columns at the end, so we
1308 // have states for (t1.col1) and (t2.col1). Giving a total of 6 non-empty
1309 // orderings. And each of those orderings will also have a decay edge to a
1310 // grouping on the same columns, thanks to the GROUP BY clause, adding another 6
1311 // states for the groupings. So in total 1 + 6 + 6 = 13 states.
1312 //
1313 // There is a cut-off at 200 states when building the NFSM, but this is not a
1314 // hard limit, and the NFSM could grow considerably bigger. At the time of
1315 // adding this benchmark, the test case for N=32 builds an NFSM with 5017
1316 // states. Which is much smaller than the 17 179 869 181 states it would have
1317 // had without the cut-off, but still much bigger than the 200 states it was
1318 // supposed to stop at.
1319 template <size_t N>
1320 static void BM_BuildInterestingOrders(size_t num_iterations) {
1321 StopBenchmarkTiming();
1322
1323 my_testing::Server_initializer initializer;
1324 initializer.SetUp();
1325 THD *thd = initializer.thd();
1326
1327 Fake_TABLE table1(N, /*cols_nullable=*/true);
1328 Fake_TABLE table2(N, /*cols_nullable=*/true);
1329
1330 array<Item_field *, N * 2> items;
1331 for (size_t i = 0; i < N; ++i) {
1332 items[i] = new Item_field(table1.field[i]);
1333 items[i + N] = new Item_field(table2.field[i]);
1334 }
1335
1336 array<ItemHandle, items.size()> handles;
1337 array<OrderElement, N> ordering;
1338 array<OrderElement, N> grouping;
1339
1340 MEM_ROOT mem_root;
1341 Swap_mem_root_guard mem_root_guard(thd, &mem_root);
1342
1343 StartBenchmarkTiming();
1344
1345 for (size_t iteration = 0; iteration < num_iterations; ++iteration) {
1346 mem_root.ClearForReuse();
1347
1348 LogicalOrderings orderings(thd);
1349
1350 // Create handles for all items involved.
1351 for (size_t i = 0; i < handles.size(); ++i) {
1352 handles[i] = orderings.GetHandle(items[i]);
1353 }
1354
1355 // ORDER BY col1, col2, ...
1356 for (size_t i = 0; i < ordering.size(); ++i) {
1357 ordering[i] = OrderElement{handles[i], ORDER_ASC};
1358 }
1359 AddOrdering(thd, ordering, /*interesting=*/true, &orderings);
1360
1361 // GROUP BY col1, col2, ...
1362 for (size_t i = 0; i < grouping.size(); ++i) {
1363 grouping[i] = OrderElement{handles[i], ORDER_NOT_RELEVANT};
1364 }
1365 AddOrdering(thd, grouping, /*interesting=*/true, &orderings);
1366
1367 // Functional dependencies from USING (col1, col2, ...).
1368 for (size_t i = 0; i < N; ++i) {
1369 FunctionalDependency fd_equiv;
1370 fd_equiv.type = FunctionalDependency::EQUIVALENCE;
1371 fd_equiv.head = make_array(&handles[i], 1);
1372 fd_equiv.tail = handles[i + N];
1373 orderings.AddFunctionalDependency(thd, fd_equiv);
1374 }
1375
1376 // Build the state machines.
1377 orderings.Build(thd, /*trace=*/nullptr);
1378 }
1379
1380 StopBenchmarkTiming();
1381 }
1382
1383 static void BM_BuildInterestingOrders1(size_t num_iterations) {
1384 BM_BuildInterestingOrders<1>(num_iterations);
1385 }
1386 BENCHMARK(BM_BuildInterestingOrders1)
1387
1388 static void BM_BuildInterestingOrders2(size_t num_iterations) {
1389 BM_BuildInterestingOrders<2>(num_iterations);
1390 }
1391 BENCHMARK(BM_BuildInterestingOrders2)
1392
1393 static void BM_BuildInterestingOrders4(size_t num_iterations) {
1394 BM_BuildInterestingOrders<4>(num_iterations);
1395 }
1396 BENCHMARK(BM_BuildInterestingOrders4)
1397
1398 static void BM_BuildInterestingOrders8(size_t num_iterations) {
1399 BM_BuildInterestingOrders<8>(num_iterations);
1400 }
1401 BENCHMARK(BM_BuildInterestingOrders8)
1402
1403 static void BM_BuildInterestingOrders16(size_t num_iterations) {
1404 BM_BuildInterestingOrders<16>(num_iterations);
1405 }
1406 BENCHMARK(BM_BuildInterestingOrders16)
1407
1408 static void BM_BuildInterestingOrders32(size_t num_iterations) {
1409 BM_BuildInterestingOrders<32>(num_iterations);
1410 }
1411 BENCHMARK(BM_BuildInterestingOrders32)