Debian Codegraph @

unittest/gunit/opt_guessrecperkey-t.cc

raw symbols SCIP
1 /*
2 Copyright (c) 2014, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25 #include <gtest/gtest.h>
26 #include <sys/types.h>
27
28 #include "sql/key.h" // KEY
29 #include "sql/opt_statistics.h" // guess_rec_per_key
30 #include "unittest/gunit/fake_key.h" // Fake_KEY
31 #include "unittest/gunit/fake_table.h" // Fake_TABLE
32 #include "unittest/gunit/test_utils.h"
33
34 namespace guessrecperkey_unittest {
35
36 using my_testing::Server_initializer;
37
38 class GuessRecPerKeyTest : public ::testing::Test {
39 void SetUp() override { initializer.SetUp(); }
40
41 void TearDown() override { initializer.TearDown(); }
42
43 private:
44 Server_initializer initializer;
45 };
46
47 TEST_F(GuessRecPerKeyTest, GuessRecPerKeyMultiColumn) {
48 const uint key_parts = 3;
49
50 Fake_TABLE table(key_parts, false);
51 Fake_KEY key(key_parts, false);
52 Fake_KEY unique_key(key_parts, true);
53
54 // Table is empty, records per key estimate should be 1
55 EXPECT_EQ(guess_rec_per_key(&table, &key, 1), 1.0f);
56 EXPECT_EQ(guess_rec_per_key(&table, &key, 2), 1.0f);
57 EXPECT_EQ(guess_rec_per_key(&table, &key, 3), 1.0f);
58 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 1), 1.0f);
59 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 2), 1.0f);
60 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 3), 1.0f);
61
62 /*
63 Test with a large table and a non-unique key.
64 */
65
66 // Increase the size of the table
67 const ha_rows large_table_size = 10000;
68 table.file->stats.records = large_table_size;
69
70 // Rec per key for first key part should be 1 percent of table size
71 EXPECT_FLOAT_EQ(guess_rec_per_key(&table, &key, 1),
72 rec_per_key_t(large_table_size / 100));
73
74 // Rec per key for last key part should be 10 for a non-unique key
75 EXPECT_EQ(guess_rec_per_key(&table, &key, 3), 10.0f);
76
77 // Rec per key for the second key part should be somewhere between first
78 // and last key part
79 rec_per_key_t rec_per_key = guess_rec_per_key(&table, &key, 2);
80 EXPECT_LT(rec_per_key, rec_per_key_t(large_table_size / 100));
81 EXPECT_GT(rec_per_key, 10.0f);
82
83 /*
84 Test with a large table and a unique key.
85 */
86
87 // Rec per key for first key part should be 1 percent of table size
88 EXPECT_FLOAT_EQ(guess_rec_per_key(&table, &unique_key, 1),
89 rec_per_key_t(large_table_size / 100));
90
91 // Rec per key for last key part should be 1 for a unique key
92 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 3), 1.0f);
93
94 // Rec per key for the second key part should be somewhere between first
95 // and last key part
96 rec_per_key = guess_rec_per_key(&table, &unique_key, 2);
97 EXPECT_LT(rec_per_key, rec_per_key_t(large_table_size / 100));
98 EXPECT_GT(rec_per_key, 1.0f);
99
100 /*
101 Test with a small table and a non-unique key.
102 */
103
104 // Set the size of the table
105 const ha_rows small_table_size = 150;
106 table.file->stats.records = small_table_size;
107
108 // Rec per key for first key part should be 1 percent of table size, but
109 // not smaller than rec per key for the last key
110 EXPECT_EQ(guess_rec_per_key(&table, &key, 1), 10.0f);
111
112 // Rec per key for last key part should be 10 for a non-unique key
113 EXPECT_EQ(guess_rec_per_key(&table, &key, 3), 10.0f);
114
115 // Rec per key for the second key part should be somewhere between first
116 // and last key part, but in this case they are identical
117 EXPECT_EQ(guess_rec_per_key(&table, &key, 2), 10.0f);
118
119 /*
120 Test with a small table and a unique key.
121 */
122
123 // Rec per key for first key part should be 1 percent of table size
124 EXPECT_FLOAT_EQ(guess_rec_per_key(&table, &unique_key, 1),
125 rec_per_key_t(small_table_size) / 100);
126
127 // Rec per key for last key part should be 1 for a unique key
128 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 3), 1.0f);
129
130 // Rec per key for the second key part should be somewhere between first
131 // and last key part
132 rec_per_key = guess_rec_per_key(&table, &unique_key, 2);
133 EXPECT_LT(rec_per_key, rec_per_key_t(small_table_size) / 100);
134 EXPECT_GT(rec_per_key, 1.0f);
135
136 /*
137 Test with a tiny table and a non-unique key.
138 */
139
140 // Set the size of the table
141 const ha_rows tiny_table_size = 30;
142 table.file->stats.records = tiny_table_size;
143
144 // Rec per key for first key part should be 1 percent of table size, but
145 // not smaller than rec per key for the last key
146 EXPECT_FLOAT_EQ(guess_rec_per_key(&table, &key, 1),
147 rec_per_key_t(tiny_table_size) / 10);
148
149 // Rec per key for last key part should be 10 for a non-unique key, but
150 // there should be at least then unique values in the table
151 EXPECT_FLOAT_EQ(guess_rec_per_key(&table, &key, 3),
152 rec_per_key_t(tiny_table_size) / 10);
153
154 // Rec per key for the second key part should be somewhere between first
155 // and last key part, but in this case they are identical
156 EXPECT_FLOAT_EQ(guess_rec_per_key(&table, &key, 2),
157 rec_per_key_t(tiny_table_size) / 10);
158
159 /*
160 Test with a tiny table and a unique key.
161 */
162
163 // Rec per key for first key part should be 1 percent of table size, but
164 // not smaller than rec per key for the last key
165 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 1), 1.0f);
166
167 // Rec per key for last key part should be 1 for a unique key
168 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 3), 1.0f);
169
170 // Rec per key for the second key part should be somewhere between first
171 // and last key part, but in this case they are indentical
172 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 2), 1.0f);
173
174 /*
175 Test that setting rec per key value for the last key part will be
176 taken into account.
177 */
178 key.rec_per_key[key_parts - 1] = 2;
179
180 // This is a tiny table so all rec per keys estimates should be
181 // identical to the estimate for the last key
182 EXPECT_EQ(guess_rec_per_key(&table, &key, 1), 2.0f);
183 EXPECT_EQ(guess_rec_per_key(&table, &key, 2), 2.0f);
184
185 // This even is the case for a unique key
186 unique_key.rec_per_key[key_parts - 1] = 2;
187 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 1), 2.0f);
188 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 2), 2.0f);
189 }
190
191 TEST_F(GuessRecPerKeyTest, GuessRecPerKeySingleColumn) {
192 const uint key_parts = 1;
193
194 Fake_TABLE table(key_parts, false);
195 Fake_KEY key(key_parts, false);
196 Fake_KEY unique_key(key_parts, true);
197
198 // Table is empty, records per key estimate should be 1
199 EXPECT_EQ(guess_rec_per_key(&table, &key, 1), 1.0f);
200 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 1), 1.0f);
201
202 /*
203 Test with a large table and a non-unique key.
204 */
205
206 // Increase the size of the table
207 const ha_rows large_table_size = 10000;
208 table.file->stats.records = large_table_size;
209
210 // Rec per key for first key part should be 1 percent of table size
211 EXPECT_FLOAT_EQ(guess_rec_per_key(&table, &key, 1),
212 rec_per_key_t(large_table_size / 100));
213 /*
214 Test with a large table and a unique key.
215 */
216
217 // Rec per key for a unique index should always be 1
218 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 1), 1.0f);
219
220 /*
221 Test with a small table and a non-unique key.
222 */
223
224 // Set the size of the table
225 const ha_rows small_table_size = 150;
226 table.file->stats.records = small_table_size;
227
228 // Rec per key for a non-unique table should be 1 percent of table size
229 // but not lower than 10.
230 EXPECT_EQ(guess_rec_per_key(&table, &key, 1), 10.0f);
231
232 /*
233 Test with a small table and a unique key.
234 */
235
236 // Rec per key for a unique index should always be 1
237 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 1), 1.0f);
238
239 /*
240 Test with a tiny table and a non-unique key.
241 */
242
243 // Set the size of the table
244 const ha_rows tiny_table_size = 30;
245 table.file->stats.records = tiny_table_size;
246
247 // Rec per key for first key part should be 1 percent of table size, but
248 // not smaller than 10% of the table
249 EXPECT_FLOAT_EQ(guess_rec_per_key(&table, &key, 1),
250 rec_per_key_t(tiny_table_size) / 10);
251
252 /*
253 Test with a tiny table and a unique key.
254 */
255
256 // Rec per key for a unique index should always be 1
257 EXPECT_EQ(guess_rec_per_key(&table, &unique_key, 1), 1.0f);
258 }
259
260 TEST_F(GuessRecPerKeyTest, GuessRecPerKeyIndexExtension) {
261 const uint columns = 5;
262 const uint pk_parts = 2;
263 const uint key_parts = 2;
264
265 Fake_TABLE table(columns, false);
266 Fake_KEY key(key_parts, key_parts + pk_parts, false);
267
268 // Table is empty, records per key estimate should be 1
269 EXPECT_EQ(guess_rec_per_key(&table, &key, 1), 1.0f);
270 EXPECT_EQ(guess_rec_per_key(&table, &key, 2), 1.0f);
271 EXPECT_EQ(guess_rec_per_key(&table, &key, 3), 1.0f);
272 }
273
274 } // namespace guessrecperkey_unittest