| 1 | /* Copyright (c) 2021, 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 <stdint.h> // uint64_t |
| 25 | #include <stdlib.h> // atoi |
| 26 | |
| 27 | #include <string> // std::string |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | #include "m_ctype.h" // my_charset_utf8mb4_0900_ai_ci |
| 32 | #include "my_alloc.h" // MEM_ROOT |
| 33 | #include "my_time.h" // MYSQL_TIME |
| 34 | #include "sql/field.h" // my_charset_numeric |
| 35 | #include "sql/histograms/equi_height.h" // Equi_height |
| 36 | #include "sql/histograms/histogram.h" // Histogram, Histogram_comparator |
| 37 | #include "sql/histograms/value_map.h" // Value_map<T> |
| 38 | #include "sql/my_decimal.h" // my_decimal |
| 39 | #include "sql_string.h" // String |
| 40 | |
| 41 | namespace histogram_selectivity_test { |
| 42 | |
| 43 | using namespace histograms; |
| 44 | |
| 45 | class HistogramSelectivityTest : public ::testing::Test { |
| 46 | protected: |
| 47 | MEM_ROOT m_mem_root; |
| 48 | |
| 49 | public: |
| 50 | HistogramSelectivityTest() : m_mem_root(PSI_NOT_INSTRUMENTED, 256) {} |
| 51 | }; |
| 52 | |
| 53 | template <class T> |
| 54 | void set_default(T *key) { |
| 55 | *key = 1; |
| 56 | } |
| 57 | |
| 58 | template <class T> |
| 59 | void increment(T *key) { |
| 60 | *key += 1; |
| 61 | } |
| 62 | |
| 63 | template <class T> |
| 64 | std::string key_to_string(const T &key) { |
| 65 | using std::to_string; |
| 66 | return to_string(key); |
| 67 | } |
| 68 | |
| 69 | void set_default(my_decimal *key) { |
| 70 | int2my_decimal(E_DEC_FATAL_ERROR, 0LL, false, key); |
| 71 | } |
| 72 | |
| 73 | void increment(my_decimal *key) { |
| 74 | longlong value; |
| 75 | my_decimal2int(E_DEC_FATAL_ERROR, key, false, &value); |
| 76 | int2my_decimal(E_DEC_FATAL_ERROR, value + 1, false, key); |
| 77 | } |
| 78 | |
| 79 | std::string key_to_string(const my_decimal &decimal) { |
| 80 | longlong value; |
| 81 | my_decimal2int(E_DEC_FATAL_ERROR, &decimal, false, &value); |
| 82 | return std::to_string(value); |
| 83 | } |
| 84 | |
| 85 | void set_default(MYSQL_TIME *datetime) { |
| 86 | set_zero_time(datetime, MYSQL_TIMESTAMP_DATETIME); |
| 87 | } |
| 88 | |
| 89 | void increment(MYSQL_TIME *datetime) { |
| 90 | datetime->year = (datetime->year + 1) % 10000; |
| 91 | datetime->month = (datetime->month + 1) % 12; |
| 92 | datetime->day = (datetime->day + 1) % 28; |
| 93 | datetime->hour = (datetime->hour + 1) % 12; |
| 94 | datetime->minute = (datetime->minute + 1) % 60; |
| 95 | datetime->second = (datetime->second + 1) % 60; |
| 96 | } |
| 97 | |
| 98 | std::string key_to_string(const MYSQL_TIME &datetime) { |
| 99 | char datetime_characters[MAX_DATE_STRING_REP_LENGTH]; |
| 100 | my_datetime_to_str(datetime, datetime_characters, 0); |
| 101 | return std::string(datetime_characters); |
| 102 | } |
| 103 | |
| 104 | void set_default(String *key) { |
| 105 | key->set_int(0, false, &my_charset_utf8mb4_0900_ai_ci); |
| 106 | } |
| 107 | |
| 108 | void increment(String *key) { |
| 109 | int value = atoi(key->c_ptr_safe()); |
| 110 | value += 1; |
| 111 | key->set_int(value, false, &my_charset_utf8mb4_0900_ai_ci); |
| 112 | } |
| 113 | |
| 114 | enum class FrequencyDistribution { |
| 115 | Uniform, |
| 116 | Linear, |
| 117 | Quadratic, |
| 118 | Cubic, |
| 119 | LinearModulo100, |
| 120 | LinearDecreasing, |
| 121 | QuadraticDecreasing, |
| 122 | ExponentiallyDecreasing, |
| 123 | Pseudorandom, |
| 124 | SingleHeavyValue, |
| 125 | ExponentialTail, |
| 126 | }; |
| 127 | |
| 128 | template <class T> |
| 129 | void fill_value_map(Value_map<T> *map, int number_of_keys, |
| 130 | FrequencyDistribution dist) { |
| 131 | T key; |
| 132 | set_default(&key); |
| 133 | switch (dist) { |
| 134 | case FrequencyDistribution::Uniform: { |
| 135 | for (int i = 1; i <= number_of_keys; ++i) { |
| 136 | map->add_values(key, 1); |
| 137 | increment(&key); |
| 138 | } |
| 139 | break; |
| 140 | } |
| 141 | case FrequencyDistribution::Linear: { |
| 142 | for (int i = 1; i <= number_of_keys; ++i) { |
| 143 | map->add_values(key, i); |
| 144 | increment(&key); |
| 145 | } |
| 146 | break; |
| 147 | } |
| 148 | case FrequencyDistribution::Quadratic: { |
| 149 | for (int i = 1; i <= number_of_keys; ++i) { |
| 150 | map->add_values(key, i * i); |
| 151 | increment(&key); |
| 152 | } |
| 153 | break; |
| 154 | } |
| 155 | case FrequencyDistribution::Cubic: { |
| 156 | for (int i = 1; i <= number_of_keys; ++i) { |
| 157 | map->add_values(key, i * i * i); |
| 158 | increment(&key); |
| 159 | } |
| 160 | break; |
| 161 | } |
| 162 | case FrequencyDistribution::LinearModulo100: { |
| 163 | for (int i = 1; i <= number_of_keys; ++i) { |
| 164 | map->add_values(key, (i % 100) + 1); |
| 165 | increment(&key); |
| 166 | } |
| 167 | break; |
| 168 | } |
| 169 | case FrequencyDistribution::LinearDecreasing: { |
| 170 | for (int i = 1; i <= number_of_keys; ++i) { |
| 171 | map->add_values(key, number_of_keys - i + 1); |
| 172 | increment(&key); |
| 173 | } |
| 174 | break; |
| 175 | } |
| 176 | case FrequencyDistribution::QuadraticDecreasing: { |
| 177 | for (int i = 1; i <= number_of_keys; ++i) { |
| 178 | map->add_values(key, number_of_keys * number_of_keys - i * i + 1); |
| 179 | increment(&key); |
| 180 | } |
| 181 | break; |
| 182 | } |
| 183 | case FrequencyDistribution::ExponentiallyDecreasing: { |
| 184 | size_t frequency = number_of_keys * number_of_keys; |
| 185 | size_t one = 1; |
| 186 | for (int i = 1; i <= number_of_keys; ++i) { |
| 187 | map->add_values(key, std::max(frequency, one)); |
| 188 | frequency = frequency / 2; |
| 189 | increment(&key); |
| 190 | } |
| 191 | break; |
| 192 | } |
| 193 | case FrequencyDistribution::Pseudorandom: { |
| 194 | // We use a random polynomial in a prime field (p = 2^17 - 1) to generate |
| 195 | // a fixed pseudorandom sequence (also known as universal hashing). |
| 196 | const uint64_t max_frequency = 10000; |
| 197 | const uint64_t p = 131071; |
| 198 | for (int i = 1; i <= number_of_keys; ++i) { |
| 199 | uint64_t x = static_cast<uint64_t>(i); |
| 200 | uint64_t frequency = |
| 201 | 1 + (((39618 + 107019 * x + 78986 * x * x) % p) % max_frequency); |
| 202 | map->add_values(key, frequency); |
| 203 | increment(&key); |
| 204 | } |
| 205 | break; |
| 206 | } |
| 207 | case FrequencyDistribution::SingleHeavyValue: { |
| 208 | for (int i = 1; i <= number_of_keys; ++i) { |
| 209 | if (i == number_of_keys / 2) { |
| 210 | map->add_values(key, number_of_keys); |
| 211 | } else { |
| 212 | map->add_values(key, 1); |
| 213 | } |
| 214 | increment(&key); |
| 215 | } |
| 216 | break; |
| 217 | } |
| 218 | case FrequencyDistribution::ExponentialTail: { |
| 219 | // Add an exponentially increasing tail to the otherwise uniform data. |
| 220 | for (int i = 1; i <= number_of_keys; ++i) { |
| 221 | int remaining_keys = number_of_keys - i + 1; |
| 222 | int scale = 1; |
| 223 | if (remaining_keys <= 5) { |
| 224 | map->add_values(key, scale * number_of_keys); |
| 225 | scale = 2 * scale; |
| 226 | } else { |
| 227 | map->add_values(key, 1); |
| 228 | } |
| 229 | increment(&key); |
| 230 | } |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | std::string ValueMapTypeToString(Value_map_type type) { |
| 237 | switch (type) { |
| 238 | case Value_map_type::INVALID: |
| 239 | return "INVALID"; |
| 240 | case Value_map_type::STRING: |
| 241 | return "STRING"; |
| 242 | case Value_map_type::INT: |
| 243 | return "INT"; |
| 244 | case Value_map_type::UINT: |
| 245 | return "UINT"; |
| 246 | case Value_map_type::DOUBLE: |
| 247 | return "DOUBLE"; |
| 248 | case Value_map_type::DECIMAL: |
| 249 | return "DECIMAL"; |
| 250 | case Value_map_type::DATE: |
| 251 | return "DATE"; |
| 252 | case Value_map_type::TIME: |
| 253 | return "TIME"; |
| 254 | case Value_map_type::DATETIME: |
| 255 | return "DATETIME"; |
| 256 | case Value_map_type::ENUM: |
| 257 | return "ENUM"; |
| 258 | case Value_map_type::SET: |
| 259 | return "SET"; |
| 260 | } |
| 261 | return "Error"; |
| 262 | } |
| 263 | |
| 264 | std::string FrequencyDistributionToString(FrequencyDistribution distribution) { |
| 265 | switch (distribution) { |
| 266 | case FrequencyDistribution::Uniform: |
| 267 | return "Uniform"; |
| 268 | case FrequencyDistribution::Linear: |
| 269 | return "Linear"; |
| 270 | case FrequencyDistribution::Quadratic: |
| 271 | return "Quadratic"; |
| 272 | case FrequencyDistribution::Cubic: |
| 273 | return "Cubic"; |
| 274 | case FrequencyDistribution::LinearModulo100: |
| 275 | return "LinearModulo100"; |
| 276 | case FrequencyDistribution::LinearDecreasing: |
| 277 | return "LinearDecreasing"; |
| 278 | case FrequencyDistribution::QuadraticDecreasing: |
| 279 | return "QuadraticDecreasing"; |
| 280 | case FrequencyDistribution::ExponentiallyDecreasing: |
| 281 | return "ExponentiallyDecreasing"; |
| 282 | case FrequencyDistribution::Pseudorandom: |
| 283 | return "Pseudorandom"; |
| 284 | case FrequencyDistribution::SingleHeavyValue: |
| 285 | return "SingleHeavyValue"; |
| 286 | case FrequencyDistribution::ExponentialTail: |
| 287 | return "ExponentialTail"; |
| 288 | } |
| 289 | return "Error"; |
| 290 | } |
| 291 | |
| 292 | std::string SelectivityErrorInfo(Value_map_type type, |
| 293 | FrequencyDistribution distribution, |
| 294 | size_t number_of_buckets) { |
| 295 | return std::string("Histogram type: ") + ValueMapTypeToString(type) + |
| 296 | ", Frequency distribution: " + |
| 297 | FrequencyDistributionToString(distribution) + |
| 298 | ", Buckets: " + std::to_string(number_of_buckets); |
| 299 | } |
| 300 | |
| 301 | // Fill a value map according to a given distribution, build a histogram, and |
| 302 | // verify that histogram selectivity estimates do not deviate from the true |
| 303 | // selectivities by too much. |
| 304 | // With the right construction algorithm it is possible to guarantee an absolute |
| 305 | // error of at most 2.0/#buckets. While the the current equi-height construction |
| 306 | // offers no such guarantee, it still passes the test. |
| 307 | template <class T> |
| 308 | void VerifySelectivityEstimates(MEM_ROOT *mem_root, CHARSET_INFO *charset, |
| 309 | Value_map_type type, |
| 310 | FrequencyDistribution distribution, |
| 311 | size_t number_of_buckets) { |
| 312 | // The number_of_keys cubed should fit into an int, otherwise the Cubic |
| 313 | // distribution will overflow. |
| 314 | const int number_of_keys = 1000; |
| 315 | |
| 316 | Value_map<T> key_frequencies(charset, type); |
| 317 | fill_value_map<T>(&key_frequencies, number_of_keys, distribution); |
| 318 | Equi_height<T> *histogram = |
| 319 | Equi_height<T>::create(mem_root, "db1", "tbl1", "col1", type); |
| 320 | EXPECT_FALSE(histogram->build_histogram(key_frequencies, number_of_buckets)); |
| 321 | |
| 322 | ha_rows total_frequency = 0; |
| 323 | for (const auto &[key, frequency] : key_frequencies) |
| 324 | total_frequency += frequency; |
| 325 | |
| 326 | const double max_abs_error = |
| 327 | 2.0 / static_cast<double>(number_of_buckets) + 0.00000001; |
| 328 | |
| 329 | std::string error_info = |
| 330 | SelectivityErrorInfo(type, distribution, number_of_buckets); |
| 331 | |
| 332 | ha_rows cumulative_frequency = 0; |
| 333 | for (const auto &[key, frequency] : key_frequencies) { |
| 334 | double less_than_selectivity = |
| 335 | static_cast<double>(cumulative_frequency) / total_frequency; |
| 336 | EXPECT_NEAR(less_than_selectivity, |
| 337 | histogram->get_less_than_selectivity(key), max_abs_error) |
| 338 | << "less than " << key_to_string(key) << "\n" |
| 339 | << error_info; |
| 340 | |
| 341 | double equal_to_selectivity = |
| 342 | static_cast<double>(frequency) / total_frequency; |
| 343 | EXPECT_NEAR(equal_to_selectivity, histogram->get_equal_to_selectivity(key), |
| 344 | max_abs_error) |
| 345 | << "equal to " << key_to_string(key) << "\n" |
| 346 | << error_info; |
| 347 | |
| 348 | double greater_than_selectivity = |
| 349 | 1.0 - (less_than_selectivity + equal_to_selectivity); |
| 350 | EXPECT_NEAR(greater_than_selectivity, |
| 351 | histogram->get_greater_than_selectivity(key), max_abs_error) |
| 352 | << "greater than " << key_to_string(key) << "\n" |
| 353 | << error_info; |
| 354 | |
| 355 | cumulative_frequency += frequency; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | TEST_F(HistogramSelectivityTest, EquiHeightSelectivity) { |
| 360 | std::vector<Value_map_type> histogram_types = { |
| 361 | Value_map_type::STRING, Value_map_type::INT, Value_map_type::UINT, |
| 362 | Value_map_type::DOUBLE, Value_map_type::DECIMAL, |
| 363 | // Value_map_type::DATE, |
| 364 | // Value_map_type::TIME, |
| 365 | Value_map_type::DATETIME, |
| 366 | // Value_map_type::ENUM, |
| 367 | // Value_map_type::SET, |
| 368 | }; |
| 369 | std::vector<FrequencyDistribution> distributions = { |
| 370 | FrequencyDistribution::Uniform, |
| 371 | FrequencyDistribution::Linear, |
| 372 | FrequencyDistribution::Quadratic, |
| 373 | FrequencyDistribution::Cubic, |
| 374 | FrequencyDistribution::LinearModulo100, |
| 375 | FrequencyDistribution::LinearDecreasing, |
| 376 | FrequencyDistribution::QuadraticDecreasing, |
| 377 | FrequencyDistribution::ExponentiallyDecreasing, |
| 378 | FrequencyDistribution::Pseudorandom, |
| 379 | FrequencyDistribution::SingleHeavyValue, |
| 380 | FrequencyDistribution::ExponentialTail}; |
| 381 | std::vector<size_t> numbers_of_buckets = {2, 4, 8, 16, 32, 64, 128, 256, 512}; |
| 382 | for (const auto &histogram_type : histogram_types) { |
| 383 | for (const auto &distribution : distributions) { |
| 384 | for (const auto &number_of_buckets : numbers_of_buckets) { |
| 385 | switch (histogram_type) { |
| 386 | case Value_map_type::INT: { |
| 387 | VerifySelectivityEstimates<longlong>( |
| 388 | &m_mem_root, &my_charset_numeric, histogram_type, distribution, |
| 389 | number_of_buckets); |
| 390 | break; |
| 391 | } |
| 392 | case Value_map_type::STRING: |
| 393 | VerifySelectivityEstimates<String>( |
| 394 | &m_mem_root, &my_charset_utf8mb4_0900_ai_ci, histogram_type, |
| 395 | distribution, number_of_buckets); |
| 396 | break; |
| 397 | case Value_map_type::UINT: { |
| 398 | VerifySelectivityEstimates<ulonglong>( |
| 399 | &m_mem_root, &my_charset_numeric, histogram_type, distribution, |
| 400 | number_of_buckets); |
| 401 | break; |
| 402 | } |
| 403 | case Value_map_type::DOUBLE: { |
| 404 | VerifySelectivityEstimates<double>(&m_mem_root, &my_charset_numeric, |
| 405 | histogram_type, distribution, |
| 406 | number_of_buckets); |
| 407 | break; |
| 408 | } |
| 409 | case Value_map_type::DECIMAL: { |
| 410 | VerifySelectivityEstimates<my_decimal>( |
| 411 | &m_mem_root, &my_charset_numeric, histogram_type, distribution, |
| 412 | number_of_buckets); |
| 413 | break; |
| 414 | } |
| 415 | case Value_map_type::DATE: |
| 416 | case Value_map_type::TIME: |
| 417 | case Value_map_type::DATETIME: { |
| 418 | VerifySelectivityEstimates<MYSQL_TIME>( |
| 419 | &m_mem_root, &my_charset_numeric, histogram_type, distribution, |
| 420 | number_of_buckets); |
| 421 | break; |
| 422 | } |
| 423 | case Value_map_type::ENUM: |
| 424 | case Value_map_type::SET: |
| 425 | EXPECT_TRUE(false) |
| 426 | << "Test for Value_map_type::" |
| 427 | << ValueMapTypeToString(histogram_type) << " not implemented."; |
| 428 | break; |
| 429 | case Value_map_type::INVALID: |
| 430 | EXPECT_TRUE(false) << "Value_map_type::INVALID."; |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | } // namespace histogram_selectivity_test |