| 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 "unittest/gunit/handler-t.h" |
| 25 | |
| 26 | #include <stddef.h> |
| 27 | #include <sys/types.h> |
| 28 | |
| 29 | #include "sql/sql_executor.h" |
| 30 | #include "unittest/gunit/fake_table.h" |
| 31 | #include "unittest/gunit/mock_field_datetime.h" |
| 32 | #include "unittest/gunit/test_utils.h" |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | using my_testing::Mock_error_handler; |
| 37 | using my_testing::Server_initializer; |
| 38 | |
| 39 | using ::testing::StrictMock; |
| 40 | |
| 41 | class HandlerTest : public ::testing::Test { |
| 42 | protected: |
| 43 | void SetUp() override { initializer.SetUp(); } |
| 44 | void TearDown() override { initializer.TearDown(); } |
| 45 | |
| 46 | THD *thd() { return initializer.thd(); } |
| 47 | |
| 48 | Server_initializer initializer; |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | Some handler error returns are passed on to report_handler_error() |
| 53 | which will: |
| 54 | - ignore errors like END_OF_FILE |
| 55 | - print most errors to the error log |
| 56 | - pass the error code back to handler::print_error() |
| 57 | */ |
| 58 | TEST_F(HandlerTest, ReportErrorHandler) { |
| 59 | Mock_field_datetime field_datetime; |
| 60 | Fake_TABLE *table = static_cast<Fake_TABLE *>(field_datetime.table); |
| 61 | handlerton *hton = nullptr; |
| 62 | StrictMock<Mock_HANDLER> mock_handler(hton, table->get_share()); |
| 63 | table->set_handler(&mock_handler); |
| 64 | |
| 65 | // This error should be ignored. |
| 66 | EXPECT_EQ(-1, report_handler_error(table, HA_ERR_END_OF_FILE)); |
| 67 | |
| 68 | // This one should not be printed to stderr, but passed on to the handler. |
| 69 | EXPECT_CALL(mock_handler, print_error(HA_ERR_TABLE_DEF_CHANGED, 0)).Times(1); |
| 70 | EXPECT_EQ(1, report_handler_error(table, HA_ERR_TABLE_DEF_CHANGED)); |
| 71 | } |
| 72 | |
| 73 | TEST_F(HandlerTest, TableInMemoryEstimate) { |
| 74 | Mock_field_datetime field_datetime; |
| 75 | Fake_TABLE *table = static_cast<Fake_TABLE *>(field_datetime.table); |
| 76 | handlerton *hton = nullptr; |
| 77 | StrictMock<Mock_HANDLER> mock_handler(hton, table->get_share()); |
| 78 | table->set_handler(&mock_handler); |
| 79 | |
| 80 | // Verify that the handler does not know the buffer size |
| 81 | EXPECT_EQ(mock_handler.get_memory_buffer_size(), -1); |
| 82 | /* |
| 83 | The implementation of table_in_memory_estimate() assumes that the |
| 84 | memory buffer is 100 MB if the storage engine does not report the |
| 85 | size of its memory buffer. |
| 86 | */ |
| 87 | const uint mem_buf_size = 100 * 1024 * 1024; |
| 88 | |
| 89 | /* |
| 90 | Define representative table sizes to use in tests. |
| 91 | */ |
| 92 | // Table that is less than 20% of memory buffer |
| 93 | const uint table_size_small = static_cast<uint>(mem_buf_size * 0.19); |
| 94 | // Table that is larger than 20% but less than 100% of memory buffer |
| 95 | const uint table_size_medium = mem_buf_size / 2; |
| 96 | // Table that is larger than memory buffer |
| 97 | const uint table_size_large = mem_buf_size * 2; |
| 98 | |
| 99 | /* |
| 100 | Verify that the default table in memory estimate for a handler has been |
| 101 | correctly initialized. |
| 102 | */ |
| 103 | EXPECT_EQ(mock_handler.stats.table_in_mem_estimate, |
| 104 | IN_MEMORY_ESTIMATE_UNKNOWN); |
| 105 | |
| 106 | /* |
| 107 | Test with a table that is less than 20% of memory buffer. This should |
| 108 | be entirely in the memory buffer. |
| 109 | */ |
| 110 | mock_handler.stats.data_file_length = table_size_small; |
| 111 | EXPECT_EQ(mock_handler.table_in_memory_estimate(), 1.0); |
| 112 | |
| 113 | /* |
| 114 | Test with a medium sized table that is more than 20% but less than |
| 115 | 100% of the memory buffer size. |
| 116 | */ |
| 117 | mock_handler.stats.data_file_length = table_size_medium; |
| 118 | EXPECT_GT(mock_handler.table_in_memory_estimate(), 0.0); |
| 119 | EXPECT_LT(mock_handler.table_in_memory_estimate(), 1.0); |
| 120 | |
| 121 | /* |
| 122 | Test with a huge table. This should not be in memory at all. |
| 123 | */ |
| 124 | mock_handler.stats.data_file_length = table_size_large; |
| 125 | EXPECT_EQ(mock_handler.table_in_memory_estimate(), 0.0); |
| 126 | |
| 127 | /* |
| 128 | Simulate that the storage engine has reported that 50 percent of the |
| 129 | table is in a memory buffer. |
| 130 | */ |
| 131 | mock_handler.stats.table_in_mem_estimate = 0.5; |
| 132 | |
| 133 | /* |
| 134 | Set the table size to be less than 20 percent but larger than 10K. |
| 135 | */ |
| 136 | mock_handler.stats.data_file_length = table_size_small; |
| 137 | EXPECT_DOUBLE_EQ(mock_handler.table_in_memory_estimate(), 0.5); |
| 138 | |
| 139 | /* |
| 140 | Set the table size to be larger than 20 percent but less than 100 percent. |
| 141 | */ |
| 142 | mock_handler.stats.data_file_length = table_size_medium; |
| 143 | EXPECT_DOUBLE_EQ(mock_handler.table_in_memory_estimate(), 0.5); |
| 144 | |
| 145 | /* |
| 146 | Set the table size to be larger than the memory buffer. |
| 147 | */ |
| 148 | mock_handler.stats.data_file_length = table_size_large; |
| 149 | EXPECT_DOUBLE_EQ(mock_handler.table_in_memory_estimate(), 0.5); |
| 150 | } |
| 151 | |
| 152 | TEST_F(HandlerTest, IndexInMemoryEstimate) { |
| 153 | Mock_field_datetime field_datetime; |
| 154 | Fake_TABLE *table = static_cast<Fake_TABLE *>(field_datetime.table); |
| 155 | handlerton *hton = nullptr; |
| 156 | NiceMock<Mock_HANDLER> mock_handler(hton, table->get_share()); |
| 157 | table->set_handler(&mock_handler); |
| 158 | mock_handler.change_table_ptr(table, table->get_share()); |
| 159 | const uint key_no = 0; |
| 160 | |
| 161 | // Verify that the handler does not know the buffer size |
| 162 | EXPECT_EQ(mock_handler.get_memory_buffer_size(), -1); |
| 163 | /* |
| 164 | The implementation of index_in_memory_estimate() assumes that the |
| 165 | memory buffer is 100 MB if the storage engine does not report the |
| 166 | size of its memory buffer. |
| 167 | */ |
| 168 | const uint mem_buf_size = 100 * 1024 * 1024; |
| 169 | |
| 170 | /* |
| 171 | Define representative table and index sizes to use in tests. |
| 172 | */ |
| 173 | // Index that is less than 20% of memory buffer |
| 174 | const uint index_size_small = static_cast<uint>(mem_buf_size * 0.19); |
| 175 | // Index that is larger than 20% but less than 100% of memory buffer |
| 176 | const uint index_size_medium = mem_buf_size / 2; |
| 177 | // Index that is larger than memory buffer |
| 178 | const uint index_size_large = mem_buf_size * 2; |
| 179 | |
| 180 | // Initialize the estimate for how much of the index that is in memory |
| 181 | table->key_info[key_no].set_in_memory_estimate(IN_MEMORY_ESTIMATE_UNKNOWN); |
| 182 | |
| 183 | /* |
| 184 | Test with an index that is less than 20% of memory buffer. This should |
| 185 | be entirely in the memory buffer. |
| 186 | */ |
| 187 | mock_handler.stats.index_file_length = index_size_small; |
| 188 | EXPECT_EQ(mock_handler.index_in_memory_estimate(key_no), 1.0); |
| 189 | |
| 190 | /* |
| 191 | Test with a medium sized index that is more than 20% but less than |
| 192 | 100% of the memory buffer size. |
| 193 | */ |
| 194 | mock_handler.stats.index_file_length = index_size_medium; |
| 195 | EXPECT_GT(mock_handler.index_in_memory_estimate(key_no), 0.0); |
| 196 | EXPECT_LT(mock_handler.index_in_memory_estimate(key_no), 1.0); |
| 197 | |
| 198 | /* |
| 199 | Test with a huge index. This should not be in memory at all. |
| 200 | */ |
| 201 | mock_handler.stats.index_file_length = index_size_large; |
| 202 | EXPECT_EQ(mock_handler.index_in_memory_estimate(key_no), 0.0); |
| 203 | |
| 204 | /* |
| 205 | Simulate that the storage engine has reported that 50 percent of the |
| 206 | index is in a memory buffer. |
| 207 | */ |
| 208 | table->key_info[key_no].set_in_memory_estimate(0.5); |
| 209 | |
| 210 | /* |
| 211 | Set the index size to be less than 20 percent but larger than 10K. |
| 212 | */ |
| 213 | mock_handler.stats.index_file_length = index_size_small; |
| 214 | EXPECT_DOUBLE_EQ(mock_handler.index_in_memory_estimate(key_no), 0.5); |
| 215 | |
| 216 | /* |
| 217 | Set the index size to be larger than 20 percent but less than 100 percent. |
| 218 | */ |
| 219 | mock_handler.stats.index_file_length = index_size_medium; |
| 220 | EXPECT_DOUBLE_EQ(mock_handler.index_in_memory_estimate(key_no), 0.5); |
| 221 | |
| 222 | /* |
| 223 | Set the index size to be larger than the memory buffer. |
| 224 | */ |
| 225 | mock_handler.stats.index_file_length = index_size_large; |
| 226 | EXPECT_DOUBLE_EQ(mock_handler.index_in_memory_estimate(key_no), 0.5); |
| 227 | } |
| 228 | |
| 229 | TEST_F(HandlerTest, SamplingInterfaceAllRows) { |
| 230 | Mock_field_datetime field_datetime; |
| 231 | Fake_TABLE *table = static_cast<Fake_TABLE *>(field_datetime.table); |
| 232 | StrictMock<Mock_SAMPLING_HANDLER> mock_handler(nullptr, table, |
| 233 | table->get_share()); |
| 234 | table->set_handler(&mock_handler); |
| 235 | |
| 236 | uchar buffer[8]; |
| 237 | void *scan_ctx = nullptr; |
| 238 | |
| 239 | // rnd_init should be called exactly one time by ha_sample_init. |
| 240 | EXPECT_CALL(mock_handler, rnd_init(true)).Times(1); |
| 241 | EXPECT_EQ(mock_handler.ha_sample_init(scan_ctx, 100.0, 0, |
| 242 | enum_sampling_method::SYSTEM, false), |
| 243 | 0); |
| 244 | EXPECT_EQ(mock_handler.inited, handler::SAMPLING); |
| 245 | |
| 246 | /* |
| 247 | Since we have set the sampling rate to 100%, all rows should be returned. |
| 248 | Thus, rnd_next should be called exactly as many times as ha_sample_next(). |
| 249 | */ |
| 250 | const int num_iterations = 100; |
| 251 | EXPECT_CALL(mock_handler, rnd_next(buffer)).Times(num_iterations); |
| 252 | |
| 253 | for (int i = 0; i < num_iterations; ++i) |
| 254 | mock_handler.ha_sample_next(scan_ctx, buffer); |
| 255 | |
| 256 | // rnd_end should be called exactly one time by ha_sample_end. |
| 257 | EXPECT_CALL(mock_handler, rnd_end()).Times(1); |
| 258 | EXPECT_EQ(mock_handler.ha_sample_end(scan_ctx), 0); |
| 259 | EXPECT_EQ(mock_handler.inited, handler::NONE); |
| 260 | } |
| 261 | |
| 262 | TEST_F(HandlerTest, SamplingInterfaceNoRows) { |
| 263 | Mock_field_datetime field_datetime; |
| 264 | Fake_TABLE *table = reinterpret_cast<Fake_TABLE *>(field_datetime.table); |
| 265 | StrictMock<Mock_SAMPLING_HANDLER> mock_handler(nullptr, table, |
| 266 | table->get_share()); |
| 267 | table->set_handler(&mock_handler); |
| 268 | |
| 269 | uchar buffer[8]; |
| 270 | void *scan_ctx = nullptr; |
| 271 | |
| 272 | // rnd_init should be called exactly one time by ha_sample_init. |
| 273 | EXPECT_CALL(mock_handler, rnd_init(true)).Times(1); |
| 274 | EXPECT_EQ(mock_handler.ha_sample_init(scan_ctx, 0.0, 0, |
| 275 | enum_sampling_method::SYSTEM, false), |
| 276 | 0); |
| 277 | EXPECT_EQ(mock_handler.inited, handler::SAMPLING); |
| 278 | |
| 279 | /* |
| 280 | Since we have set the sampling rate to 0%, no rows should be returned. Thus, |
| 281 | rnd_next should never be called. |
| 282 | */ |
| 283 | EXPECT_CALL(mock_handler, rnd_next(buffer)).Times(0); |
| 284 | |
| 285 | for (int i = 0; i < 100; ++i) mock_handler.ha_sample_next(scan_ctx, buffer); |
| 286 | |
| 287 | // rnd_end should be called exactly one time by ha_sample_end. |
| 288 | EXPECT_CALL(mock_handler, rnd_end()).Times(1); |
| 289 | EXPECT_EQ(mock_handler.ha_sample_end(scan_ctx), 0); |
| 290 | EXPECT_EQ(mock_handler.inited, handler::NONE); |
| 291 | } |
| 292 | |
| 293 | } // namespace |