Debian Codegraph @

unittest/gunit/join_tab_sort-t.cc

raw symbols SCIP
1 /* Copyright (c) 2012, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gtest/gtest.h>
25 #include <sys/types.h>
26 #include <random>
27 #include <vector>
28
29 #include "my_inttypes.h"
30 #include "sql/merge_sort.h"
31 #include "sql/sql_planner.h"
32 #include "sql/sql_select.h"
33 #include "unittest/gunit/test_utils.h"
34
35 namespace join_tab_sort_unittest {
36
37 using my_testing::Mock_error_handler;
38 using my_testing::Server_initializer;
39
40 class JTSortTest : public ::testing::Test {
41 public:
42 JTSortTest() : urng(std::random_device().operator()()) {}
43
44 protected:
45 void SetUp() override { initializer.SetUp(); }
46 void TearDown() override { initializer.TearDown(); }
47
48 Server_initializer initializer;
49 std::mt19937 urng;
50 };
51
52 class MOCK_JOIN_TAB : public JOIN_TAB {
53 public:
54 MOCK_JOIN_TAB(uint recs, uint table_no) : JOIN_TAB() {
55 found_records = recs;
56 set_qs(&m_shared);
57 this->set_table(&m_table);
58 m_table_list.set_tableno(table_no);
59 this->table_ref = &m_table_list;
60 }
61
62 TABLE m_table;
63 Table_ref m_table_list;
64 QEP_shared m_shared;
65 };
66
67 std::ostream &operator<<(std::ostream &s, const MOCK_JOIN_TAB &jt) {
68 return s << "{" << jt.found_records << ", " << jt.m_table_list.map() << "}";
69 }
70
71 TEST_F(JTSortTest, SimpleSortTest) {
72 MOCK_JOIN_TAB jt1(UINT_MAX, 0);
73 MOCK_JOIN_TAB jt2(2, 0);
74 MOCK_JOIN_TAB jt3(1, 0);
75 MOCK_JOIN_TAB jt4(10, 0);
76 MOCK_JOIN_TAB jt5(5, 0);
77
78 MOCK_JOIN_TAB *arr[5];
79 arr[0] = &jt1;
80 arr[1] = &jt2;
81 arr[2] = &jt3;
82 arr[3] = &jt4;
83 arr[4] = &jt5;
84
85 insert_sort(arr, arr + 5, Join_tab_compare_default());
86
87 EXPECT_EQ(1U, arr[0]->found_records);
88 EXPECT_EQ(2U, arr[1]->found_records);
89 EXPECT_EQ(5U, arr[2]->found_records);
90 EXPECT_EQ(10U, arr[3]->found_records);
91 EXPECT_EQ(UINT_MAX, arr[4]->found_records);
92 }
93
94 TEST_F(JTSortTest, SortFoundRecordsTest) {
95 const int num_tables = 50;
96 MOCK_JOIN_TAB *arr[num_tables];
97
98 for (int i = 0; i < num_tables; i++)
99 arr[i] = new (*THR_MALLOC) MOCK_JOIN_TAB(i, 0);
100
101 // MERGE SORT
102 std::shuffle(arr, arr + 50, urng);
103 merge_sort(arr, arr + num_tables, Join_tab_compare_default());
104 for (int i = 1; i < num_tables; i++)
105 EXPECT_TRUE(arr[i]->found_records > arr[i - 1]->found_records);
106
107 // INSERT SORT
108 std::shuffle(arr, arr + 50, urng);
109 insert_sort(arr, arr + num_tables, Join_tab_compare_default());
110 for (int i = 1; i < num_tables; i++)
111 EXPECT_TRUE(arr[i]->found_records > arr[i - 1]->found_records);
112
113 for (int i = 0; i < num_tables; i++) {
114 destroy(arr[i]);
115 }
116 }
117
118 TEST_F(JTSortTest, SortDependsTest) {
119 const int num_tables = 50;
120 MOCK_JOIN_TAB *arr[num_tables];
121
122 /*
123 dependency has higher precedence than found_records, so the tables
124 shall be ordered with decreasing number of records in this test
125 */
126 for (int i = 0; i < num_tables; i++) {
127 arr[i] = new (*THR_MALLOC) MOCK_JOIN_TAB(i, i);
128 for (int j = i + 1; j < num_tables; j++) arr[i]->dependent |= 1ULL << j;
129 }
130
131 // MERGE SORT
132 std::shuffle(arr, arr + num_tables, urng);
133 merge_sort(arr, arr + num_tables, Join_tab_compare_default());
134 for (int i = 1; i < num_tables; i++)
135 EXPECT_TRUE(arr[i]->found_records < arr[i - 1]->found_records)
136 << "i: " << *(arr[i]) << " "
137 << "i-1: " << *(arr[i - 1]);
138
139 // INSERT SORT
140 std::shuffle(arr, arr + num_tables, urng);
141 insert_sort(arr, arr + num_tables, Join_tab_compare_default());
142 for (int i = 1; i < num_tables; i++)
143 EXPECT_TRUE(arr[i]->found_records < arr[i - 1]->found_records);
144
145 for (int i = 0; i < num_tables; i++) {
146 destroy(arr[i]);
147 }
148 }
149
150 TEST_F(JTSortTest, SortKeyDependsTest) {
151 const int num_tables = 50;
152 MOCK_JOIN_TAB *arr[num_tables];
153
154 /*
155 key_dependency has higher precedence than found_records, so the
156 tables shall be ordered with decreasing number of records in this
157 test
158 */
159 for (int i = 0; i < num_tables; i++) {
160 arr[i] = new (*THR_MALLOC) MOCK_JOIN_TAB(i, i);
161 for (int j = i + 1; j < num_tables; j++) arr[i]->key_dependent |= 1ULL << j;
162 }
163
164 // MERGE SORT
165 std::shuffle(arr, arr + num_tables, urng);
166 merge_sort(arr, arr + num_tables, Join_tab_compare_default());
167 for (int i = 1; i < num_tables; i++)
168 EXPECT_TRUE(arr[i]->found_records < arr[i - 1]->found_records);
169
170 // INSERT SORT
171 std::shuffle(arr, arr + num_tables, urng);
172 insert_sort(arr, arr + num_tables, Join_tab_compare_default());
173 for (int i = 1; i < num_tables; i++)
174 EXPECT_TRUE(arr[i]->found_records < arr[i - 1]->found_records);
175
176 for (int i = 0; i < num_tables; i++) destroy(arr[i]);
177 }
178
179 /*
180 Above, sorting for JOIN_TABs were tested. Below we check that the
181 sorting works for ints types as well.
182 */
183
184 class Int_compare_ptr {
185 public:
186 bool operator()(const int *i1, const int *i2) const { return *i1 < *i2; }
187 };
188
189 TEST_F(JTSortTest, SortIntTest) {
190 const uint ints_to_sort = 1000;
191
192 std::vector<int> arr;
193 std::vector<int *> arr_ptr;
194
195 arr.reserve(ints_to_sort);
196 arr_ptr.reserve(ints_to_sort);
197
198 for (uint i = 0; i < ints_to_sort; i++) {
199 arr.push_back(i);
200 arr_ptr.push_back(&arr[i]);
201 }
202
203 EXPECT_TRUE(arr.size() == ints_to_sort);
204 EXPECT_TRUE(arr_ptr.size() == ints_to_sort);
205
206 // MERGE SORT
207 std::shuffle(&arr_ptr.front(), &arr_ptr.back() + 1, urng);
208 merge_sort(&arr_ptr.front(), &arr_ptr.back() + 1, Int_compare_ptr());
209 for (uint i = 0; i < arr_ptr.size(); i++) EXPECT_TRUE(*arr_ptr[i] == (int)i);
210
211 // INSERT SORT
212 std::shuffle(&arr_ptr.front(), &arr_ptr.back() + 1, urng);
213 insert_sort(&arr_ptr.front(), &arr_ptr.back() + 1, Int_compare_ptr());
214 for (uint i = 0; i < arr_ptr.size(); i++) EXPECT_TRUE(*arr_ptr[i] == (int)i);
215 }
216
217 TEST_F(JTSortTest, SortInt2Test) {
218 const uint ints_to_sort = 1000;
219
220 std::vector<int> arr;
221 std::vector<int *> arr_ptr;
222
223 arr.reserve(ints_to_sort);
224 arr_ptr.reserve(ints_to_sort);
225
226 for (uint i = 0; i < (ints_to_sort - 2); i++) {
227 arr.push_back((i % 2) ? i : (i * -1));
228 arr_ptr.push_back(&arr[i]);
229 }
230
231 arr.push_back(INT_MAX32);
232 arr_ptr.push_back(&arr.back());
233
234 arr.push_back(INT_MIN32);
235 arr_ptr.push_back(&arr.back());
236
237 EXPECT_TRUE(arr.size() == ints_to_sort);
238 EXPECT_TRUE(arr_ptr.size() == ints_to_sort);
239
240 // MERGE SORT
241 std::shuffle(&arr_ptr.front(), &arr_ptr.back() + 1, urng);
242 merge_sort(&arr_ptr.front(), &arr_ptr.back() + 1, Int_compare_ptr());
243 for (uint i = 1; i < arr_ptr.size(); i++)
244 EXPECT_TRUE(*arr_ptr[i - 1] < *arr_ptr[i]);
245
246 // INSERT SORT
247 std::shuffle(&arr_ptr.front(), &arr_ptr.back() + 1, urng);
248 insert_sort(&arr_ptr.front(), &arr_ptr.back() + 1, Int_compare_ptr());
249 for (uint i = 1; i < arr_ptr.size(); i++)
250 EXPECT_TRUE(*arr_ptr[i - 1] < *arr_ptr[i]);
251 }
252
253 } // namespace join_tab_sort_unittest