| 1 | /* Copyright (c) 2016, 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 <limits> // std::numeric_limits |
| 26 | #include <map> // std::map |
| 27 | #include <string> // std::string |
| 28 | |
| 29 | #include "lex_string.h" |
| 30 | #include "m_ctype.h" // my_charset_latin1, my_charset_bin |
| 31 | #include "my_inttypes.h" |
| 32 | #include "my_systime.h" // my_micro_time() |
| 33 | #include "my_time.h" // MYSQL_TIME |
| 34 | #include "sql-common/json_dom.h" // Json_object |
| 35 | #include "sql/field.h" // my_charset_numeric |
| 36 | #include "sql/histograms/equi_height.h" // Equi_height |
| 37 | #include "sql/histograms/histogram.h" // Histogram, Histogram_comparator |
| 38 | #include "sql/histograms/singleton.h" // Singleton |
| 39 | #include "sql/histograms/value_map.h" // Value_map<T> |
| 40 | #include "sql/my_decimal.h" // my_decimal |
| 41 | #include "sql/sql_time.h" // my_time_compare |
| 42 | #include "sql/tztime.h" // my_tz_UTC |
| 43 | #include "template_utils.h" // down_cast |
| 44 | |
| 45 | namespace histograms_unittest { |
| 46 | |
| 47 | using namespace histograms; |
| 48 | |
| 49 | class HistogramsTest : public ::testing::Test { |
| 50 | protected: |
| 51 | MEM_ROOT m_mem_root; |
| 52 | |
| 53 | Value_map<double> double_values; |
| 54 | Value_map<String> string_values; |
| 55 | Value_map<ulonglong> uint_values; |
| 56 | Value_map<longlong> int_values; |
| 57 | Value_map<my_decimal> decimal_values; |
| 58 | Value_map<MYSQL_TIME> datetime_values; |
| 59 | Value_map<MYSQL_TIME> date_values; |
| 60 | Value_map<MYSQL_TIME> time_values; |
| 61 | Value_map<String> blob_values; |
| 62 | |
| 63 | /* |
| 64 | Declare these arrays here, so that they survive the lifetime of the unit |
| 65 | tests. |
| 66 | |
| 67 | Do not use negative char values, since these will be cast to an uchar |
| 68 | pointer in the function sortcmp. |
| 69 | */ |
| 70 | const char blob_buf1[4] = {0, 0, 0, 0}; |
| 71 | const char blob_buf2[4] = {127, 127, 127, 127}; |
| 72 | |
| 73 | public: |
| 74 | HistogramsTest() |
| 75 | : m_mem_root(PSI_NOT_INSTRUMENTED, 256), |
| 76 | double_values(&my_charset_numeric, Value_map_type::DOUBLE), |
| 77 | string_values(&my_charset_latin1, Value_map_type::STRING), |
| 78 | uint_values(&my_charset_numeric, Value_map_type::UINT), |
| 79 | int_values(&my_charset_numeric, Value_map_type::INT), |
| 80 | decimal_values(&my_charset_numeric, Value_map_type::DECIMAL), |
| 81 | datetime_values(&my_charset_numeric, Value_map_type::DATETIME), |
| 82 | date_values(&my_charset_numeric, Value_map_type::DATE), |
| 83 | time_values(&my_charset_numeric, Value_map_type::TIME), |
| 84 | blob_values(&my_charset_bin, Value_map_type::STRING) { |
| 85 | // Double values. |
| 86 | double_values.add_values(std::numeric_limits<double>::lowest(), 10); |
| 87 | double_values.add_values(std::numeric_limits<double>::max(), 10); |
| 88 | double_values.add_values(std::numeric_limits<double>::epsilon(), 10); |
| 89 | double_values.add_values(0.0, 10); |
| 90 | double_values.add_values(42.0, 10); |
| 91 | double_values.add_values(43.0, 10); |
| 92 | |
| 93 | // String values. |
| 94 | string_values.add_values(String("", &my_charset_latin1), 10); |
| 95 | string_values.add_values(String("string4", &my_charset_latin1), 10); |
| 96 | string_values.add_values(String("string3", &my_charset_latin1), 10); |
| 97 | string_values.add_values(String("string1", &my_charset_latin1), 10); |
| 98 | string_values.add_values(String("string2", &my_charset_latin1), 10); |
| 99 | |
| 100 | // Unsigned integer values (ulonglong). |
| 101 | uint_values.add_values(std::numeric_limits<ulonglong>::lowest(), 10); |
| 102 | uint_values.add_values(std::numeric_limits<ulonglong>::max(), 10); |
| 103 | uint_values.add_values(42ULL, 10); |
| 104 | uint_values.add_values(43ULL, 10); |
| 105 | uint_values.add_values(10000ULL, 10); |
| 106 | |
| 107 | // Signed integer values (longlong). |
| 108 | int_values.add_values(std::numeric_limits<longlong>::lowest(), 10); |
| 109 | int_values.add_values(std::numeric_limits<longlong>::max(), 10); |
| 110 | int_values.add_values(0LL, 10); |
| 111 | int_values.add_values(-1LL, 10); |
| 112 | int_values.add_values(1LL, 10); |
| 113 | int_values.add_values(42LL, 10); |
| 114 | int_values.add_values(10000LL, 10); |
| 115 | |
| 116 | // Decimal values (my_decimal). |
| 117 | my_decimal decimal1; |
| 118 | int2my_decimal(E_DEC_FATAL_ERROR, 0LL, false, &decimal1); |
| 119 | decimal_values.add_values(decimal1, 10); |
| 120 | |
| 121 | my_decimal decimal2; |
| 122 | int2my_decimal(E_DEC_FATAL_ERROR, -1000LL, false, &decimal2); |
| 123 | decimal_values.add_values(decimal2, 10); |
| 124 | |
| 125 | my_decimal decimal3; |
| 126 | int2my_decimal(E_DEC_FATAL_ERROR, 1000LL, false, &decimal3); |
| 127 | decimal_values.add_values(decimal3, 10); |
| 128 | |
| 129 | my_decimal decimal4; |
| 130 | int2my_decimal(E_DEC_FATAL_ERROR, 42LL, false, &decimal4); |
| 131 | decimal_values.add_values(decimal4, 10); |
| 132 | |
| 133 | my_decimal decimal5; |
| 134 | int2my_decimal(E_DEC_FATAL_ERROR, 1LL, false, &decimal5); |
| 135 | decimal_values.add_values(decimal5, 10); |
| 136 | |
| 137 | /* |
| 138 | Datetime values (MYSQL_TIME). |
| 139 | |
| 140 | We are using these packed values for testing: |
| 141 | |
| 142 | 914866242077065216 => 1000-01-01 00:00:00.000000 |
| 143 | 914866242077065217 => 1000-01-01 00:00:00.000001 |
| 144 | 1845541820734373888 => 2017-05-23 08:08:03.000000 |
| 145 | 9147936188962652735 => 9999-12-31 23:59:59.999999 |
| 146 | 9147936188962652734 => 9999-12-31 23:59:59.999998 |
| 147 | */ |
| 148 | MYSQL_TIME datetime1; |
| 149 | TIME_from_longlong_datetime_packed(&datetime1, 9147936188962652734); |
| 150 | datetime_values.add_values(datetime1, 10); |
| 151 | |
| 152 | MYSQL_TIME datetime2; |
| 153 | TIME_from_longlong_datetime_packed(&datetime2, 914866242077065217); |
| 154 | datetime_values.add_values(datetime2, 10); |
| 155 | |
| 156 | MYSQL_TIME datetime3; |
| 157 | TIME_from_longlong_datetime_packed(&datetime3, 914866242077065216); |
| 158 | datetime_values.add_values(datetime3, 10); |
| 159 | |
| 160 | MYSQL_TIME datetime4; |
| 161 | TIME_from_longlong_datetime_packed(&datetime4, 1845541820734373888); |
| 162 | datetime_values.add_values(datetime4, 10); |
| 163 | |
| 164 | MYSQL_TIME datetime5; |
| 165 | TIME_from_longlong_datetime_packed(&datetime5, 9147936188962652735); |
| 166 | datetime_values.add_values(datetime5, 10); |
| 167 | |
| 168 | /* |
| 169 | Date values (MYSQL_TIME). |
| 170 | |
| 171 | Do not test negative values, since negative DATETIME is not supported by |
| 172 | MySQL. We also call "set_zero_time", to initialize the entire MYSQL_TIME |
| 173 | structure. If we don't, valgrind will complain on uninitialised values. |
| 174 | */ |
| 175 | MYSQL_TIME date1; |
| 176 | set_zero_time(&date1, MYSQL_TIMESTAMP_DATE); |
| 177 | set_max_hhmmss(&date1); |
| 178 | date_values.add_values(date1, 10); |
| 179 | |
| 180 | MYSQL_TIME date2; |
| 181 | set_zero_time(&date2, MYSQL_TIMESTAMP_DATE); |
| 182 | TIME_from_longlong_date_packed(&date2, 10000); |
| 183 | date_values.add_values(date2, 10); |
| 184 | |
| 185 | MYSQL_TIME date3; |
| 186 | set_zero_time(&date3, MYSQL_TIMESTAMP_DATE); |
| 187 | TIME_from_longlong_date_packed(&date3, 0); |
| 188 | date_values.add_values(date3, 10); |
| 189 | |
| 190 | MYSQL_TIME date4; |
| 191 | set_zero_time(&date4, MYSQL_TIMESTAMP_DATE); |
| 192 | TIME_from_longlong_date_packed(&date4, 100); |
| 193 | date_values.add_values(date4, 10); |
| 194 | |
| 195 | MYSQL_TIME date5; |
| 196 | set_zero_time(&date5, MYSQL_TIMESTAMP_DATE); |
| 197 | TIME_from_longlong_date_packed(&date5, 100000); |
| 198 | date_values.add_values(date5, 10); |
| 199 | |
| 200 | /* |
| 201 | Time values (MYSQL_TIME). |
| 202 | |
| 203 | Do not test negative values, since negative DATETIME is not supported by |
| 204 | MySQL. |
| 205 | */ |
| 206 | MYSQL_TIME time1; |
| 207 | set_zero_time(&time1, MYSQL_TIMESTAMP_TIME); |
| 208 | set_max_time(&time1, false); |
| 209 | time_values.add_values(time1, 10); |
| 210 | |
| 211 | MYSQL_TIME time2; |
| 212 | set_zero_time(&time2, MYSQL_TIMESTAMP_TIME); |
| 213 | TIME_from_longlong_time_packed(&time2, 12); |
| 214 | time_values.add_values(time2, 10); |
| 215 | |
| 216 | MYSQL_TIME time3; |
| 217 | set_zero_time(&time3, MYSQL_TIMESTAMP_TIME); |
| 218 | TIME_from_longlong_time_packed(&time3, 0); |
| 219 | time_values.add_values(time3, 10); |
| 220 | |
| 221 | MYSQL_TIME time4; |
| 222 | set_zero_time(&time4, MYSQL_TIMESTAMP_TIME); |
| 223 | TIME_from_longlong_time_packed(&time4, 42); |
| 224 | time_values.add_values(time4, 10); |
| 225 | |
| 226 | MYSQL_TIME time5; |
| 227 | set_zero_time(&time5, MYSQL_TIMESTAMP_TIME); |
| 228 | TIME_from_longlong_time_packed(&time5, 100000); |
| 229 | time_values.add_values(time5, 10); |
| 230 | |
| 231 | // Blob values. |
| 232 | blob_values.add_values(String(blob_buf1, 4, &my_charset_bin), 10); |
| 233 | blob_values.add_values(String(blob_buf2, 4, &my_charset_bin), 10); |
| 234 | blob_values.add_values(String("foo", &my_charset_bin), 10); |
| 235 | blob_values.add_values(String("bar", &my_charset_bin), 10); |
| 236 | blob_values.add_values(String("foobar", &my_charset_bin), 10); |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | /* |
| 241 | Utility function that verify the following properties for a histogram that is |
| 242 | converted to JSON: |
| 243 | - All histogram types must have the field "last-updated" of type J_DATETIME. |
| 244 | - All histogram types must have the field "histogram-type" of type J_STRING. |
| 245 | * Check that the printed histogram type actually is the correct one. |
| 246 | - All histogram types must have the field "buckets" of type J_ARRAY. |
| 247 | * Check that the number of buckets in the JSON array is the same as the |
| 248 | amount of buckets in the original histogram. |
| 249 | - All histogram types must have the field "null-values" of type J_DOUBLE. |
| 250 | - All histogram types must have the field "collation-id" of type J_UINT. |
| 251 | */ |
| 252 | void VerifyCommonJSONFields(Json_object *json_histogram, |
| 253 | const Histogram *histogram) { |
| 254 | ASSERT_TRUE(histogram != nullptr); |
| 255 | |
| 256 | // Last updated field. |
| 257 | Json_dom *last_updated_dom = json_histogram->get("last-updated"); |
| 258 | EXPECT_NE(last_updated_dom, nullptr); |
| 259 | EXPECT_EQ(last_updated_dom->json_type(), enum_json_type::J_DATETIME); |
| 260 | |
| 261 | // Histogram type field. |
| 262 | Json_dom *histogram_type_dom = json_histogram->get("histogram-type"); |
| 263 | EXPECT_NE(histogram_type_dom, nullptr); |
| 264 | EXPECT_EQ(histogram_type_dom->json_type(), enum_json_type::J_STRING); |
| 265 | |
| 266 | Json_string *json_histogram_type = |
| 267 | static_cast<Json_string *>(histogram_type_dom); |
| 268 | |
| 269 | switch (histogram->get_histogram_type()) { |
| 270 | case Histogram::enum_histogram_type::EQUI_HEIGHT: |
| 271 | EXPECT_STREQ(json_histogram_type->value().c_str(), "equi-height"); |
| 272 | break; |
| 273 | case Histogram::enum_histogram_type::SINGLETON: |
| 274 | EXPECT_STREQ(json_histogram_type->value().c_str(), "singleton"); |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | // Buckets field. |
| 279 | Json_dom *buckets_dom = json_histogram->get("buckets"); |
| 280 | EXPECT_NE(buckets_dom, nullptr); |
| 281 | EXPECT_EQ(buckets_dom->json_type(), enum_json_type::J_ARRAY); |
| 282 | |
| 283 | // Fraction of null values. |
| 284 | Json_dom *null_values_dom = json_histogram->get("null-values"); |
| 285 | EXPECT_NE(null_values_dom, nullptr); |
| 286 | EXPECT_EQ(null_values_dom->json_type(), enum_json_type::J_DOUBLE); |
| 287 | |
| 288 | // Collation ID |
| 289 | Json_dom *collation_id_dom = json_histogram->get("collation-id"); |
| 290 | EXPECT_NE(collation_id_dom, nullptr); |
| 291 | EXPECT_EQ(collation_id_dom->json_type(), enum_json_type::J_UINT); |
| 292 | |
| 293 | Json_array *buckets = static_cast<Json_array *>(buckets_dom); |
| 294 | EXPECT_EQ(buckets->size(), histogram->get_num_buckets()); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | Utility function that verifies the following constraints for a singleton |
| 299 | histogram that is converted to JSON: |
| 300 | - The value in a singleton bucket is greater than or equal to the value in |
| 301 | the previous bucket. |
| 302 | - The cumulative frequency is in the range (0.0, 1.0] (lower exclusive, |
| 303 | upper inclusive). |
| 304 | - The cumulative frequency is greater than the cumulative frequency in the |
| 305 | previous bucket. |
| 306 | */ |
| 307 | void VerifySingletonBucketConstraintsDouble(const Histogram *histogram) { |
| 308 | ASSERT_TRUE(histogram != nullptr); |
| 309 | |
| 310 | Json_object json_object; |
| 311 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 312 | |
| 313 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 314 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 315 | |
| 316 | double previous_value = 0.0; |
| 317 | double previous_cumulative_frequency = 0.0; |
| 318 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 319 | Json_dom *bucket_dom = (*buckets)[i]; |
| 320 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 321 | |
| 322 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[1]); |
| 323 | double current_cumulative_frequency = json_frequency->value(); |
| 324 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 325 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 326 | |
| 327 | Json_double *json_double = down_cast<Json_double *>((*bucket)[0]); |
| 328 | double current_value = json_double->value(); |
| 329 | if (i > 0) { |
| 330 | EXPECT_TRUE(Histogram_comparator()(previous_value, current_value)); |
| 331 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 332 | } |
| 333 | previous_value = current_value; |
| 334 | previous_cumulative_frequency = current_cumulative_frequency; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void VerifySingletonBucketConstraintsInt(Histogram *histogram) { |
| 339 | ASSERT_TRUE(histogram != nullptr); |
| 340 | |
| 341 | Json_object json_object; |
| 342 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 343 | |
| 344 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 345 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 346 | |
| 347 | longlong previous_value = 0; |
| 348 | double previous_cumulative_frequency = 0.0; |
| 349 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 350 | Json_dom *bucket_dom = (*buckets)[i]; |
| 351 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 352 | |
| 353 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[1]); |
| 354 | double current_cumulative_frequency = json_frequency->value(); |
| 355 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 356 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 357 | |
| 358 | Json_int *json_int = down_cast<Json_int *>((*bucket)[0]); |
| 359 | longlong current_value = json_int->value(); |
| 360 | if (i > 0) { |
| 361 | EXPECT_TRUE(Histogram_comparator()(previous_value, current_value)); |
| 362 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 363 | } |
| 364 | previous_value = current_value; |
| 365 | previous_cumulative_frequency = current_cumulative_frequency; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | void VerifySingletonBucketConstraintsUInt(Histogram *histogram) { |
| 370 | ASSERT_TRUE(histogram != nullptr); |
| 371 | |
| 372 | Json_object json_object; |
| 373 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 374 | |
| 375 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 376 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 377 | |
| 378 | ulonglong previous_value = 0; |
| 379 | double previous_cumulative_frequency = 0.0; |
| 380 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 381 | Json_dom *bucket_dom = (*buckets)[i]; |
| 382 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 383 | |
| 384 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[1]); |
| 385 | double current_cumulative_frequency = json_frequency->value(); |
| 386 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 387 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 388 | |
| 389 | Json_uint *json_uint = down_cast<Json_uint *>((*bucket)[0]); |
| 390 | ulonglong current_value = json_uint->value(); |
| 391 | if (i > 0) { |
| 392 | EXPECT_TRUE(Histogram_comparator()(previous_value, current_value)); |
| 393 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 394 | } |
| 395 | previous_value = current_value; |
| 396 | previous_cumulative_frequency = current_cumulative_frequency; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void VerifySingletonBucketConstraintsString(Histogram *histogram, |
| 401 | const CHARSET_INFO *charset) { |
| 402 | ASSERT_TRUE(histogram != nullptr); |
| 403 | |
| 404 | Json_object json_object; |
| 405 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 406 | |
| 407 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 408 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 409 | |
| 410 | String previous_value; |
| 411 | double previous_cumulative_frequency = 0.0; |
| 412 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 413 | Json_dom *bucket_dom = (*buckets)[i]; |
| 414 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 415 | |
| 416 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[1]); |
| 417 | double current_cumulative_frequency = json_frequency->value(); |
| 418 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 419 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 420 | |
| 421 | Json_opaque *json_opaque = down_cast<Json_opaque *>((*bucket)[0]); |
| 422 | String current_value(json_opaque->value(), json_opaque->size(), charset); |
| 423 | if (i > 0) { |
| 424 | EXPECT_TRUE(Histogram_comparator()(previous_value, current_value)); |
| 425 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 426 | } |
| 427 | previous_value = current_value; |
| 428 | previous_cumulative_frequency = current_cumulative_frequency; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void VerifySingletonBucketConstraintsDecimal(Histogram *histogram) { |
| 433 | ASSERT_TRUE(histogram != nullptr); |
| 434 | |
| 435 | Json_object json_object; |
| 436 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 437 | |
| 438 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 439 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 440 | |
| 441 | const my_decimal *previous_value = nullptr; |
| 442 | double previous_cumulative_frequency = 0.0; |
| 443 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 444 | Json_dom *bucket_dom = (*buckets)[i]; |
| 445 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 446 | |
| 447 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[1]); |
| 448 | double current_cumulative_frequency = json_frequency->value(); |
| 449 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 450 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 451 | |
| 452 | Json_decimal *json_decimal = down_cast<Json_decimal *>((*bucket)[0]); |
| 453 | const my_decimal *current_value = json_decimal->value(); |
| 454 | if (i > 0) { |
| 455 | EXPECT_TRUE(Histogram_comparator()(*previous_value, *current_value)); |
| 456 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 457 | } |
| 458 | previous_value = current_value; |
| 459 | previous_cumulative_frequency = current_cumulative_frequency; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | void VerifySingletonBucketConstraintsTemporal(Histogram *histogram) { |
| 464 | ASSERT_TRUE(histogram != nullptr); |
| 465 | |
| 466 | Json_object json_object; |
| 467 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 468 | |
| 469 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 470 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 471 | |
| 472 | const MYSQL_TIME *previous_value = nullptr; |
| 473 | double previous_cumulative_frequency = 0.0; |
| 474 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 475 | Json_dom *bucket_dom = (*buckets)[i]; |
| 476 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 477 | |
| 478 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[1]); |
| 479 | double current_cumulative_frequency = json_frequency->value(); |
| 480 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 481 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 482 | |
| 483 | Json_datetime *json_datetime = down_cast<Json_datetime *>((*bucket)[0]); |
| 484 | const MYSQL_TIME *current_value = json_datetime->value(); |
| 485 | if (i > 0) { |
| 486 | EXPECT_TRUE(Histogram_comparator()(*previous_value, *current_value)); |
| 487 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 488 | } |
| 489 | previous_value = current_value; |
| 490 | previous_cumulative_frequency = current_cumulative_frequency; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | Utility function that verifies the following constraints for an equi-height |
| 496 | histogram that is converted to JSON: |
| 497 | - The lower inclusive value in an equi-height bucket is less than or equal |
| 498 | to the upper inclusive value. |
| 499 | - The lower inclusive value in an equi-height bucket is greater than the |
| 500 | upper inclusive value of the previous bucket. |
| 501 | - The cumulative frequency is in the range (0.0, 1.0] (lower exclusive, |
| 502 | upper inclusive). |
| 503 | - The cumulative frequency is greater than the cumulative frequency in the |
| 504 | previous bucket. |
| 505 | - The number of distinct values in a bucket is equal to or greater than 1. |
| 506 | */ |
| 507 | void VerifyEquiHeightBucketConstraintsDouble(Histogram *histogram) { |
| 508 | ASSERT_TRUE(histogram != nullptr); |
| 509 | |
| 510 | Json_object json_object; |
| 511 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 512 | |
| 513 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 514 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 515 | |
| 516 | double previous_upper_value = 0.0; |
| 517 | double previous_cumulative_frequency = 0.0; |
| 518 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 519 | Json_dom *bucket_dom = (*buckets)[i]; |
| 520 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 521 | |
| 522 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[2]); |
| 523 | double current_cumulative_frequency = json_frequency->value(); |
| 524 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 525 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 526 | |
| 527 | Json_uint *json_num_distinct = down_cast<Json_uint *>((*bucket)[3]); |
| 528 | EXPECT_GE(json_num_distinct->value(), 1ULL); |
| 529 | |
| 530 | /* |
| 531 | Index 1 should be lower inclusive value, and index 2 should be upper |
| 532 | inclusive value. |
| 533 | */ |
| 534 | Json_double *json_double_lower = down_cast<Json_double *>((*bucket)[0]); |
| 535 | Json_double *json_double_upper = down_cast<Json_double *>((*bucket)[1]); |
| 536 | |
| 537 | double current_lower_value = json_double_lower->value(); |
| 538 | double current_upper_value = json_double_upper->value(); |
| 539 | if (i > 0) { |
| 540 | EXPECT_TRUE( |
| 541 | Histogram_comparator()(previous_upper_value, current_lower_value)); |
| 542 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 543 | } |
| 544 | |
| 545 | EXPECT_FALSE( |
| 546 | Histogram_comparator()(current_upper_value, current_lower_value)); |
| 547 | |
| 548 | previous_upper_value = current_upper_value; |
| 549 | previous_cumulative_frequency = current_cumulative_frequency; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | void VerifyEquiHeightBucketConstraintsInt(Histogram *histogram) { |
| 554 | ASSERT_TRUE(histogram != nullptr); |
| 555 | |
| 556 | Json_object json_object; |
| 557 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 558 | |
| 559 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 560 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 561 | |
| 562 | longlong previous_upper_value = 0; |
| 563 | double previous_cumulative_frequency = 0.0; |
| 564 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 565 | Json_dom *bucket_dom = (*buckets)[i]; |
| 566 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 567 | |
| 568 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[2]); |
| 569 | double current_cumulative_frequency = json_frequency->value(); |
| 570 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 571 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 572 | |
| 573 | Json_uint *json_num_distinct = down_cast<Json_uint *>((*bucket)[3]); |
| 574 | EXPECT_GE(json_num_distinct->value(), 1ULL); |
| 575 | |
| 576 | /* |
| 577 | Index 1 should be lower inclusive value, and index 2 should be upper |
| 578 | inclusive value. |
| 579 | */ |
| 580 | Json_int *json_int_lower = down_cast<Json_int *>((*bucket)[0]); |
| 581 | Json_int *json_int_upper = down_cast<Json_int *>((*bucket)[1]); |
| 582 | |
| 583 | longlong current_lower_value = json_int_lower->value(); |
| 584 | longlong current_upper_value = json_int_upper->value(); |
| 585 | if (i > 0) { |
| 586 | EXPECT_TRUE( |
| 587 | Histogram_comparator()(previous_upper_value, current_lower_value)); |
| 588 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 589 | } |
| 590 | |
| 591 | EXPECT_FALSE( |
| 592 | Histogram_comparator()(current_upper_value, current_lower_value)); |
| 593 | |
| 594 | previous_upper_value = current_upper_value; |
| 595 | previous_cumulative_frequency = current_cumulative_frequency; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | void VerifyEquiHeightBucketConstraintsUInt(Histogram *histogram) { |
| 600 | ASSERT_TRUE(histogram != nullptr); |
| 601 | |
| 602 | Json_object json_object; |
| 603 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 604 | |
| 605 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 606 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 607 | |
| 608 | ulonglong previous_upper_value = 0; |
| 609 | double previous_cumulative_frequency = 0.0; |
| 610 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 611 | Json_dom *bucket_dom = (*buckets)[i]; |
| 612 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 613 | |
| 614 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[2]); |
| 615 | double current_cumulative_frequency = json_frequency->value(); |
| 616 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 617 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 618 | |
| 619 | Json_uint *json_num_distinct = down_cast<Json_uint *>((*bucket)[3]); |
| 620 | EXPECT_GE(json_num_distinct->value(), 1ULL); |
| 621 | |
| 622 | /* |
| 623 | Index 1 should be lower inclusive value, and index 2 should be upper |
| 624 | inclusive value. |
| 625 | */ |
| 626 | Json_uint *json_uint_lower = down_cast<Json_uint *>((*bucket)[0]); |
| 627 | Json_uint *json_uint_upper = down_cast<Json_uint *>((*bucket)[1]); |
| 628 | |
| 629 | ulonglong current_lower_value = json_uint_lower->value(); |
| 630 | ulonglong current_upper_value = json_uint_upper->value(); |
| 631 | if (i > 0) { |
| 632 | EXPECT_TRUE( |
| 633 | Histogram_comparator()(previous_upper_value, current_lower_value)); |
| 634 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 635 | } |
| 636 | |
| 637 | EXPECT_FALSE( |
| 638 | Histogram_comparator()(current_upper_value, current_lower_value)); |
| 639 | |
| 640 | previous_upper_value = current_upper_value; |
| 641 | previous_cumulative_frequency = current_cumulative_frequency; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | void VerifyEquiHeightBucketConstraintsString(Histogram *histogram, |
| 646 | const CHARSET_INFO *charset) { |
| 647 | ASSERT_TRUE(histogram != nullptr); |
| 648 | |
| 649 | Json_object json_object; |
| 650 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 651 | |
| 652 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 653 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 654 | |
| 655 | String previous_upper_value; |
| 656 | double previous_cumulative_frequency = 0.0; |
| 657 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 658 | Json_dom *bucket_dom = (*buckets)[i]; |
| 659 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 660 | |
| 661 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[2]); |
| 662 | double current_cumulative_frequency = json_frequency->value(); |
| 663 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 664 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 665 | |
| 666 | Json_uint *json_num_distinct = down_cast<Json_uint *>((*bucket)[3]); |
| 667 | EXPECT_GE(json_num_distinct->value(), 1ULL); |
| 668 | |
| 669 | /* |
| 670 | Index 1 should be lower inclusive value, and index 2 should be upper |
| 671 | inclusive value. |
| 672 | */ |
| 673 | Json_opaque *json_opaque_lower = down_cast<Json_opaque *>((*bucket)[0]); |
| 674 | Json_opaque *json_opaque_upper = down_cast<Json_opaque *>((*bucket)[1]); |
| 675 | |
| 676 | String current_lower_value(json_opaque_lower->value(), |
| 677 | json_opaque_lower->size(), charset); |
| 678 | String current_upper_value(json_opaque_upper->value(), |
| 679 | json_opaque_upper->size(), charset); |
| 680 | |
| 681 | if (i > 0) { |
| 682 | EXPECT_TRUE( |
| 683 | Histogram_comparator()(previous_upper_value, current_lower_value)); |
| 684 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 685 | } |
| 686 | |
| 687 | EXPECT_FALSE( |
| 688 | Histogram_comparator()(current_upper_value, current_lower_value)); |
| 689 | |
| 690 | previous_upper_value = current_upper_value; |
| 691 | previous_cumulative_frequency = current_cumulative_frequency; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | void VerifyEquiHeightBucketConstraintsDecimal(Histogram *histogram) { |
| 696 | ASSERT_TRUE(histogram != nullptr); |
| 697 | |
| 698 | Json_object json_object; |
| 699 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 700 | |
| 701 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 702 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 703 | |
| 704 | const my_decimal *previous_upper_value = nullptr; |
| 705 | double previous_cumulative_frequency = 0.0; |
| 706 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 707 | Json_dom *bucket_dom = (*buckets)[i]; |
| 708 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 709 | |
| 710 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[2]); |
| 711 | double current_cumulative_frequency = json_frequency->value(); |
| 712 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 713 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 714 | |
| 715 | Json_uint *json_num_distinct = down_cast<Json_uint *>((*bucket)[3]); |
| 716 | EXPECT_GE(json_num_distinct->value(), 1ULL); |
| 717 | |
| 718 | /* |
| 719 | Index 1 should be lower inclusive value, and index 2 should be upper |
| 720 | inclusive value. |
| 721 | */ |
| 722 | Json_decimal *json_decimal_lower = down_cast<Json_decimal *>((*bucket)[0]); |
| 723 | Json_decimal *json_decimal_upper = down_cast<Json_decimal *>((*bucket)[1]); |
| 724 | |
| 725 | const my_decimal *current_lower_value(json_decimal_lower->value()); |
| 726 | const my_decimal *current_upper_value(json_decimal_upper->value()); |
| 727 | |
| 728 | if (i > 0) { |
| 729 | EXPECT_TRUE( |
| 730 | Histogram_comparator()(*previous_upper_value, *current_lower_value)); |
| 731 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 732 | } |
| 733 | |
| 734 | EXPECT_FALSE( |
| 735 | Histogram_comparator()(*current_upper_value, *current_lower_value)); |
| 736 | |
| 737 | previous_upper_value = current_upper_value; |
| 738 | previous_cumulative_frequency = current_cumulative_frequency; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | void VerifyEquiHeightBucketConstraintsTemporal(Histogram *histogram) { |
| 743 | ASSERT_TRUE(histogram != nullptr); |
| 744 | |
| 745 | Json_object json_object; |
| 746 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 747 | |
| 748 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 749 | Json_array *buckets = down_cast<Json_array *>(buckets_dom); |
| 750 | |
| 751 | const MYSQL_TIME *previous_upper_value = nullptr; |
| 752 | double previous_cumulative_frequency = 0.0; |
| 753 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 754 | Json_dom *bucket_dom = (*buckets)[i]; |
| 755 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 756 | |
| 757 | Json_double *json_frequency = down_cast<Json_double *>((*bucket)[2]); |
| 758 | double current_cumulative_frequency = json_frequency->value(); |
| 759 | EXPECT_GT(current_cumulative_frequency, 0.0); |
| 760 | EXPECT_LE(current_cumulative_frequency, 1.0); |
| 761 | |
| 762 | Json_uint *json_num_distinct = down_cast<Json_uint *>((*bucket)[3]); |
| 763 | EXPECT_GE(json_num_distinct->value(), 1ULL); |
| 764 | |
| 765 | /* |
| 766 | Index 1 should be lower inclusive value, and index 2 should be upper |
| 767 | inclusive value. |
| 768 | */ |
| 769 | Json_datetime *json_datetime_lower = |
| 770 | down_cast<Json_datetime *>((*bucket)[0]); |
| 771 | Json_datetime *json_datetime_upper = |
| 772 | down_cast<Json_datetime *>((*bucket)[1]); |
| 773 | |
| 774 | const MYSQL_TIME *current_lower_value(json_datetime_lower->value()); |
| 775 | const MYSQL_TIME *current_upper_value(json_datetime_upper->value()); |
| 776 | |
| 777 | if (i > 0) { |
| 778 | EXPECT_TRUE( |
| 779 | Histogram_comparator()(*previous_upper_value, *current_lower_value)); |
| 780 | EXPECT_LT(previous_cumulative_frequency, current_cumulative_frequency); |
| 781 | } |
| 782 | |
| 783 | EXPECT_FALSE( |
| 784 | Histogram_comparator()(*current_upper_value, *current_lower_value)); |
| 785 | |
| 786 | previous_upper_value = current_upper_value; |
| 787 | previous_cumulative_frequency = current_cumulative_frequency; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | Utility function that verify the following properties for an equi-height |
| 793 | histogram that is converted to JSON: |
| 794 | - The histogram has all the "common" JSON fields |
| 795 | (see VerifyCommonJSONFields). |
| 796 | - All equi-height buckets have the following types in each index: |
| 797 | 0: J_DOUBLE |
| 798 | 1: Depends on the data type stored in the histogram |
| 799 | 2: Depends on the data type stored in the histogram |
| 800 | 3: J_UINT |
| 801 | |
| 802 | The function does not check that the values are correct, but rather that they |
| 803 | are present with the expected type. |
| 804 | */ |
| 805 | void VerifyEquiHeightJSONStructure(Histogram *histogram, |
| 806 | enum_json_type expected_json_type) { |
| 807 | ASSERT_TRUE(histogram != nullptr); |
| 808 | |
| 809 | Json_object json_object; |
| 810 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 811 | VerifyCommonJSONFields(&json_object, histogram); |
| 812 | |
| 813 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 814 | Json_array *buckets = static_cast<Json_array *>(buckets_dom); |
| 815 | |
| 816 | // Verify that all the buckets have the expected structure. |
| 817 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 818 | Json_dom *bucket_dom = (*buckets)[i]; |
| 819 | EXPECT_EQ(bucket_dom->json_type(), enum_json_type::J_ARRAY); |
| 820 | |
| 821 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 822 | EXPECT_EQ(bucket->size(), 4U); |
| 823 | |
| 824 | // Index 0 should be lower inclusive value. |
| 825 | EXPECT_EQ((*bucket)[0]->json_type(), expected_json_type); |
| 826 | |
| 827 | // Index 1 should be upper inclusive value. |
| 828 | EXPECT_EQ((*bucket)[1]->json_type(), expected_json_type); |
| 829 | |
| 830 | // Index 2 should be cumulative frequency. |
| 831 | EXPECT_EQ((*bucket)[2]->json_type(), enum_json_type::J_DOUBLE); |
| 832 | |
| 833 | // Index 3 should be numer of distinct values. |
| 834 | EXPECT_EQ((*bucket)[3]->json_type(), enum_json_type::J_UINT); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | Utility function that verify the following properties for a singleton |
| 840 | histogram that is converted to JSON: |
| 841 | - The histogram has all the "common" JSON fields |
| 842 | (see VerifyCommonJSONFields). |
| 843 | - All equi-height buckets have the following types in each index: |
| 844 | 0: J_DOUBLE |
| 845 | 1: Depends on the data type stored in the histogram |
| 846 | |
| 847 | The function does not check that the values are correct, but rather that they |
| 848 | are present with the expected type. |
| 849 | */ |
| 850 | void VerifySingletonJSONStructure(Histogram *histogram, |
| 851 | enum_json_type expected_json_type) { |
| 852 | ASSERT_TRUE(histogram != nullptr); |
| 853 | |
| 854 | Json_object json_object; |
| 855 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 856 | VerifyCommonJSONFields(&json_object, histogram); |
| 857 | |
| 858 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 859 | Json_array *buckets = static_cast<Json_array *>(buckets_dom); |
| 860 | |
| 861 | // Verify that all the buckets have the expected structure. |
| 862 | for (size_t i = 0; i < buckets->size(); ++i) { |
| 863 | Json_dom *bucket_dom = (*buckets)[i]; |
| 864 | EXPECT_EQ(bucket_dom->json_type(), enum_json_type::J_ARRAY); |
| 865 | |
| 866 | Json_array *bucket = static_cast<Json_array *>(bucket_dom); |
| 867 | EXPECT_EQ(bucket->size(), 2U); |
| 868 | |
| 869 | // Index 0 should be the value. |
| 870 | EXPECT_EQ((*bucket)[0]->json_type(), expected_json_type); |
| 871 | |
| 872 | // Index 1 should be cumulative frequency. |
| 873 | EXPECT_EQ((*bucket)[1]->json_type(), enum_json_type::J_DOUBLE); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | Check that a singleton histogram can be built and converted to JSON for all |
| 879 | supported data types: |
| 880 | |
| 881 | - Double |
| 882 | - String |
| 883 | - Uint |
| 884 | - Int |
| 885 | - Decimal |
| 886 | - Datetime (MYSQL_TIME) |
| 887 | - Date (MYSQL_TIME) |
| 888 | - Time (MYSQL_TIME) |
| 889 | - Blob/binary |
| 890 | */ |
| 891 | TEST_F(HistogramsTest, DoubleSingletonToJSON) { |
| 892 | Singleton<double> *histogram = Singleton<double>::create( |
| 893 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DOUBLE); |
| 894 | ASSERT_TRUE(histogram != nullptr); |
| 895 | |
| 896 | EXPECT_FALSE(histogram->build_histogram(double_values, double_values.size())); |
| 897 | EXPECT_EQ(double_values.size(), histogram->get_num_buckets()); |
| 898 | EXPECT_EQ(double_values.size(), histogram->get_num_distinct_values()); |
| 899 | |
| 900 | VerifySingletonJSONStructure(histogram, enum_json_type::J_DOUBLE); |
| 901 | VerifySingletonBucketConstraintsDouble(histogram); |
| 902 | } |
| 903 | |
| 904 | TEST_F(HistogramsTest, StringSingletonToJSON) { |
| 905 | Singleton<String> *histogram = Singleton<String>::create( |
| 906 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::STRING); |
| 907 | ASSERT_TRUE(histogram != nullptr); |
| 908 | |
| 909 | EXPECT_FALSE(histogram->build_histogram(string_values, string_values.size())); |
| 910 | EXPECT_EQ(string_values.size(), histogram->get_num_buckets()); |
| 911 | EXPECT_EQ(string_values.size(), histogram->get_num_distinct_values()); |
| 912 | |
| 913 | VerifySingletonJSONStructure(histogram, enum_json_type::J_OPAQUE); |
| 914 | VerifySingletonBucketConstraintsString(histogram, &my_charset_latin1); |
| 915 | } |
| 916 | |
| 917 | TEST_F(HistogramsTest, UintSingletonToJSON) { |
| 918 | Singleton<ulonglong> *histogram = Singleton<ulonglong>::create( |
| 919 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::UINT); |
| 920 | ASSERT_TRUE(histogram != nullptr); |
| 921 | |
| 922 | EXPECT_FALSE(histogram->build_histogram(uint_values, uint_values.size())); |
| 923 | EXPECT_EQ(uint_values.size(), histogram->get_num_buckets()); |
| 924 | EXPECT_EQ(uint_values.size(), histogram->get_num_distinct_values()); |
| 925 | |
| 926 | VerifySingletonJSONStructure(histogram, enum_json_type::J_UINT); |
| 927 | VerifySingletonBucketConstraintsUInt(histogram); |
| 928 | } |
| 929 | |
| 930 | TEST_F(HistogramsTest, IntSingletonToJSON) { |
| 931 | Singleton<longlong> *histogram = Singleton<longlong>::create( |
| 932 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 933 | ASSERT_TRUE(histogram != nullptr); |
| 934 | |
| 935 | EXPECT_FALSE(histogram->build_histogram(int_values, int_values.size())); |
| 936 | EXPECT_EQ(int_values.size(), histogram->get_num_buckets()); |
| 937 | EXPECT_EQ(int_values.size(), histogram->get_num_distinct_values()); |
| 938 | |
| 939 | VerifySingletonJSONStructure(histogram, enum_json_type::J_INT); |
| 940 | VerifySingletonBucketConstraintsInt(histogram); |
| 941 | } |
| 942 | |
| 943 | TEST_F(HistogramsTest, DecimalSingletonToJSON) { |
| 944 | Singleton<my_decimal> *histogram = Singleton<my_decimal>::create( |
| 945 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DECIMAL); |
| 946 | ASSERT_TRUE(histogram != nullptr); |
| 947 | |
| 948 | EXPECT_FALSE( |
| 949 | histogram->build_histogram(decimal_values, decimal_values.size())); |
| 950 | EXPECT_EQ(decimal_values.size(), histogram->get_num_buckets()); |
| 951 | EXPECT_EQ(decimal_values.size(), histogram->get_num_distinct_values()); |
| 952 | |
| 953 | VerifySingletonJSONStructure(histogram, enum_json_type::J_DECIMAL); |
| 954 | VerifySingletonBucketConstraintsDecimal(histogram); |
| 955 | } |
| 956 | |
| 957 | TEST_F(HistogramsTest, DatetimeSingletonToJSON) { |
| 958 | Singleton<MYSQL_TIME> *histogram = Singleton<MYSQL_TIME>::create( |
| 959 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DATETIME); |
| 960 | ASSERT_TRUE(histogram != nullptr); |
| 961 | |
| 962 | EXPECT_FALSE( |
| 963 | histogram->build_histogram(datetime_values, datetime_values.size())); |
| 964 | EXPECT_EQ(datetime_values.size(), histogram->get_num_buckets()); |
| 965 | EXPECT_EQ(datetime_values.size(), histogram->get_num_distinct_values()); |
| 966 | |
| 967 | VerifySingletonJSONStructure(histogram, enum_json_type::J_DATETIME); |
| 968 | VerifySingletonBucketConstraintsTemporal(histogram); |
| 969 | } |
| 970 | |
| 971 | TEST_F(HistogramsTest, DateSingletonToJSON) { |
| 972 | Singleton<MYSQL_TIME> *histogram = Singleton<MYSQL_TIME>::create( |
| 973 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DATE); |
| 974 | ASSERT_TRUE(histogram != nullptr); |
| 975 | |
| 976 | EXPECT_FALSE(histogram->build_histogram(date_values, date_values.size())); |
| 977 | EXPECT_EQ(date_values.size(), histogram->get_num_buckets()); |
| 978 | EXPECT_EQ(date_values.size(), histogram->get_num_distinct_values()); |
| 979 | |
| 980 | VerifySingletonJSONStructure(histogram, enum_json_type::J_DATE); |
| 981 | VerifySingletonBucketConstraintsTemporal(histogram); |
| 982 | } |
| 983 | |
| 984 | TEST_F(HistogramsTest, TimeSingletonToJSON) { |
| 985 | Singleton<MYSQL_TIME> *histogram = Singleton<MYSQL_TIME>::create( |
| 986 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::TIME); |
| 987 | ASSERT_TRUE(histogram != nullptr); |
| 988 | |
| 989 | EXPECT_FALSE(histogram->build_histogram(time_values, time_values.size())); |
| 990 | EXPECT_EQ(time_values.size(), histogram->get_num_buckets()); |
| 991 | EXPECT_EQ(time_values.size(), histogram->get_num_distinct_values()); |
| 992 | |
| 993 | VerifySingletonJSONStructure(histogram, enum_json_type::J_TIME); |
| 994 | VerifySingletonBucketConstraintsTemporal(histogram); |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | Check that an equi-height histogram can be built and converted to JSON for all |
| 999 | supported data types: |
| 1000 | |
| 1001 | - Double |
| 1002 | - String |
| 1003 | - Uint |
| 1004 | - Int |
| 1005 | - Decimal |
| 1006 | - Datetime (MYSQL_TIME) |
| 1007 | - Date (MYSQL_TIME) |
| 1008 | - Time (MYSQL_TIME) |
| 1009 | - Blob/binary |
| 1010 | |
| 1011 | Create equi-height histograms with the same number of buckets as the number |
| 1012 | of distinct values in the data set. This will lead to every histogram bucket |
| 1013 | having lower_inclusive_value == upper_inclusive value. |
| 1014 | |
| 1015 | We check that the resulting JSON has the expected structure, as well as every |
| 1016 | bucket having lower_inclusive_value <= upper_inclusive. |
| 1017 | */ |
| 1018 | TEST_F(HistogramsTest, DoubleEquiHeightToJSON) { |
| 1019 | Equi_height<double> *histogram = Equi_height<double>::create( |
| 1020 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DOUBLE); |
| 1021 | ASSERT_TRUE(histogram != nullptr); |
| 1022 | |
| 1023 | EXPECT_FALSE(histogram->build_histogram(double_values, double_values.size())); |
| 1024 | EXPECT_EQ(double_values.size(), histogram->get_num_buckets()); |
| 1025 | EXPECT_EQ(double_values.size(), histogram->get_num_distinct_values()); |
| 1026 | |
| 1027 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_DOUBLE); |
| 1028 | VerifyEquiHeightBucketConstraintsDouble(histogram); |
| 1029 | } |
| 1030 | |
| 1031 | TEST_F(HistogramsTest, StringEquiHeightToJSON) { |
| 1032 | Equi_height<String> *histogram = Equi_height<String>::create( |
| 1033 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::STRING); |
| 1034 | ASSERT_TRUE(histogram != nullptr); |
| 1035 | |
| 1036 | EXPECT_FALSE(histogram->build_histogram(string_values, string_values.size())); |
| 1037 | EXPECT_EQ(string_values.size(), histogram->get_num_buckets()); |
| 1038 | EXPECT_EQ(string_values.size(), histogram->get_num_distinct_values()); |
| 1039 | |
| 1040 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_OPAQUE); |
| 1041 | VerifyEquiHeightBucketConstraintsString(histogram, &my_charset_latin1); |
| 1042 | } |
| 1043 | |
| 1044 | TEST_F(HistogramsTest, UintEquiHeightToJSON) { |
| 1045 | Equi_height<ulonglong> *histogram = Equi_height<ulonglong>::create( |
| 1046 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::UINT); |
| 1047 | ASSERT_TRUE(histogram != nullptr); |
| 1048 | |
| 1049 | EXPECT_FALSE(histogram->build_histogram(uint_values, uint_values.size())); |
| 1050 | EXPECT_EQ(uint_values.size(), histogram->get_num_buckets()); |
| 1051 | EXPECT_EQ(uint_values.size(), histogram->get_num_distinct_values()); |
| 1052 | |
| 1053 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_UINT); |
| 1054 | VerifyEquiHeightBucketConstraintsUInt(histogram); |
| 1055 | } |
| 1056 | |
| 1057 | TEST_F(HistogramsTest, IntEquiHeightToJSON) { |
| 1058 | Equi_height<longlong> *histogram = Equi_height<longlong>::create( |
| 1059 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 1060 | ASSERT_TRUE(histogram != nullptr); |
| 1061 | |
| 1062 | EXPECT_FALSE(histogram->build_histogram(int_values, int_values.size())); |
| 1063 | EXPECT_EQ(int_values.size(), histogram->get_num_buckets()); |
| 1064 | EXPECT_EQ(int_values.size(), histogram->get_num_distinct_values()); |
| 1065 | |
| 1066 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_INT); |
| 1067 | VerifyEquiHeightBucketConstraintsInt(histogram); |
| 1068 | } |
| 1069 | |
| 1070 | TEST_F(HistogramsTest, DecimalEquiHeightToJSON) { |
| 1071 | Equi_height<my_decimal> *histogram = Equi_height<my_decimal>::create( |
| 1072 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DECIMAL); |
| 1073 | ASSERT_TRUE(histogram != nullptr); |
| 1074 | |
| 1075 | EXPECT_FALSE( |
| 1076 | histogram->build_histogram(decimal_values, decimal_values.size())); |
| 1077 | EXPECT_EQ(decimal_values.size(), histogram->get_num_buckets()); |
| 1078 | EXPECT_EQ(decimal_values.size(), histogram->get_num_distinct_values()); |
| 1079 | |
| 1080 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_DECIMAL); |
| 1081 | VerifyEquiHeightBucketConstraintsDecimal(histogram); |
| 1082 | } |
| 1083 | |
| 1084 | TEST_F(HistogramsTest, DatetimeEquiHeightToJSON) { |
| 1085 | Equi_height<MYSQL_TIME> *histogram = Equi_height<MYSQL_TIME>::create( |
| 1086 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DATETIME); |
| 1087 | ASSERT_TRUE(histogram != nullptr); |
| 1088 | |
| 1089 | EXPECT_FALSE( |
| 1090 | histogram->build_histogram(datetime_values, datetime_values.size())); |
| 1091 | EXPECT_EQ(datetime_values.size(), histogram->get_num_buckets()); |
| 1092 | EXPECT_EQ(datetime_values.size(), histogram->get_num_distinct_values()); |
| 1093 | |
| 1094 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_DATETIME); |
| 1095 | VerifyEquiHeightBucketConstraintsTemporal(histogram); |
| 1096 | } |
| 1097 | |
| 1098 | TEST_F(HistogramsTest, DateEquiHeightToJSON) { |
| 1099 | Equi_height<MYSQL_TIME> *histogram = Equi_height<MYSQL_TIME>::create( |
| 1100 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DATE); |
| 1101 | ASSERT_TRUE(histogram != nullptr); |
| 1102 | |
| 1103 | EXPECT_FALSE(histogram->build_histogram(date_values, date_values.size())); |
| 1104 | EXPECT_EQ(date_values.size(), histogram->get_num_buckets()); |
| 1105 | EXPECT_EQ(date_values.size(), histogram->get_num_distinct_values()); |
| 1106 | |
| 1107 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_DATE); |
| 1108 | VerifyEquiHeightBucketConstraintsTemporal(histogram); |
| 1109 | } |
| 1110 | |
| 1111 | TEST_F(HistogramsTest, TimeEquiHeightToJSON) { |
| 1112 | Equi_height<MYSQL_TIME> *histogram = Equi_height<MYSQL_TIME>::create( |
| 1113 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::TIME); |
| 1114 | ASSERT_TRUE(histogram != nullptr); |
| 1115 | |
| 1116 | EXPECT_FALSE(histogram->build_histogram(time_values, time_values.size())); |
| 1117 | EXPECT_EQ(time_values.size(), histogram->get_num_buckets()); |
| 1118 | EXPECT_EQ(time_values.size(), histogram->get_num_distinct_values()); |
| 1119 | |
| 1120 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_TIME); |
| 1121 | VerifyEquiHeightBucketConstraintsTemporal(histogram); |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | Create Equi-height histograms with fewer buckets than the distinct number |
| 1126 | of values. This will force at least one of the buckets to have |
| 1127 | lower_inclusive_value != upper_inclusive_value. |
| 1128 | |
| 1129 | We check that the resulting JSON has the expected structure, as well as every |
| 1130 | bucket having lower_inclusive_value <= upper_inclusive. |
| 1131 | */ |
| 1132 | TEST_F(HistogramsTest, DoubleEquiHeightFewBuckets) { |
| 1133 | Equi_height<double> *histogram = Equi_height<double>::create( |
| 1134 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DOUBLE); |
| 1135 | ASSERT_TRUE(histogram != nullptr); |
| 1136 | |
| 1137 | EXPECT_FALSE(histogram->build_histogram(double_values, 2U)); |
| 1138 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_DOUBLE); |
| 1139 | VerifyEquiHeightBucketConstraintsDouble(histogram); |
| 1140 | } |
| 1141 | |
| 1142 | TEST_F(HistogramsTest, StringEquiHeightFewBuckets) { |
| 1143 | Equi_height<String> *histogram = Equi_height<String>::create( |
| 1144 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::STRING); |
| 1145 | ASSERT_TRUE(histogram != nullptr); |
| 1146 | |
| 1147 | EXPECT_FALSE(histogram->build_histogram(string_values, 2U)); |
| 1148 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_OPAQUE); |
| 1149 | VerifyEquiHeightBucketConstraintsString(histogram, &my_charset_latin1); |
| 1150 | } |
| 1151 | |
| 1152 | TEST_F(HistogramsTest, UintEquiHeightFewBuckets) { |
| 1153 | Equi_height<ulonglong> *histogram = Equi_height<ulonglong>::create( |
| 1154 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::UINT); |
| 1155 | ASSERT_TRUE(histogram != nullptr); |
| 1156 | |
| 1157 | EXPECT_FALSE(histogram->build_histogram(uint_values, 2U)); |
| 1158 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_UINT); |
| 1159 | VerifyEquiHeightBucketConstraintsUInt(histogram); |
| 1160 | } |
| 1161 | |
| 1162 | TEST_F(HistogramsTest, IntEquiHeightFewBuckets) { |
| 1163 | Equi_height<longlong> *histogram = Equi_height<longlong>::create( |
| 1164 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 1165 | ASSERT_TRUE(histogram != nullptr); |
| 1166 | |
| 1167 | EXPECT_FALSE(histogram->build_histogram(int_values, 2U)); |
| 1168 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_INT); |
| 1169 | VerifyEquiHeightBucketConstraintsInt(histogram); |
| 1170 | } |
| 1171 | |
| 1172 | TEST_F(HistogramsTest, DecimalEquiHeightFewBuckets) { |
| 1173 | Equi_height<my_decimal> *histogram = Equi_height<my_decimal>::create( |
| 1174 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DECIMAL); |
| 1175 | ASSERT_TRUE(histogram != nullptr); |
| 1176 | |
| 1177 | EXPECT_FALSE(histogram->build_histogram(decimal_values, 2U)); |
| 1178 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_DECIMAL); |
| 1179 | VerifyEquiHeightBucketConstraintsDecimal(histogram); |
| 1180 | } |
| 1181 | |
| 1182 | TEST_F(HistogramsTest, DatetimeEquiHeightFewBuckets) { |
| 1183 | Equi_height<MYSQL_TIME> *histogram = Equi_height<MYSQL_TIME>::create( |
| 1184 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DATETIME); |
| 1185 | ASSERT_TRUE(histogram != nullptr); |
| 1186 | |
| 1187 | EXPECT_FALSE(histogram->build_histogram(datetime_values, 2U)); |
| 1188 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_DATETIME); |
| 1189 | VerifyEquiHeightBucketConstraintsTemporal(histogram); |
| 1190 | } |
| 1191 | |
| 1192 | TEST_F(HistogramsTest, DateEquiHeightFewBuckets) { |
| 1193 | Equi_height<MYSQL_TIME> *histogram = Equi_height<MYSQL_TIME>::create( |
| 1194 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DATE); |
| 1195 | ASSERT_TRUE(histogram != nullptr); |
| 1196 | |
| 1197 | EXPECT_FALSE(histogram->build_histogram(date_values, 2U)); |
| 1198 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_DATE); |
| 1199 | VerifyEquiHeightBucketConstraintsTemporal(histogram); |
| 1200 | } |
| 1201 | |
| 1202 | TEST_F(HistogramsTest, TimeEquiHeightFewBuckets) { |
| 1203 | Equi_height<MYSQL_TIME> *histogram = Equi_height<MYSQL_TIME>::create( |
| 1204 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::TIME); |
| 1205 | ASSERT_TRUE(histogram != nullptr); |
| 1206 | |
| 1207 | EXPECT_FALSE(histogram->build_histogram(time_values, 2U)); |
| 1208 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_TIME); |
| 1209 | VerifyEquiHeightBucketConstraintsTemporal(histogram); |
| 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | Verify that the "auto-select histogram"-mechanism works as expected. That is, |
| 1214 | it should select a singleton histogram when we have less or equal amount of |
| 1215 | distinct values as the specified amount of buckets. In all other cases it |
| 1216 | should create an equi-height histogram. |
| 1217 | */ |
| 1218 | TEST_F(HistogramsTest, AutoSelectHistogramType) { |
| 1219 | /* |
| 1220 | Case 1: Less buckets than the number of distinct values. We should end up |
| 1221 | with an equi-height histogram. |
| 1222 | */ |
| 1223 | size_t num_buckets = double_values.size() - 1; |
| 1224 | Histogram *histogram1 = build_histogram(&m_mem_root, double_values, |
| 1225 | num_buckets, "db1", "tbl1", "col1"); |
| 1226 | |
| 1227 | EXPECT_EQ(Histogram::enum_histogram_type::EQUI_HEIGHT, |
| 1228 | histogram1->get_histogram_type()); |
| 1229 | EXPECT_LE(histogram1->get_num_buckets(), num_buckets); |
| 1230 | EXPECT_EQ(histogram1->get_num_distinct_values(), double_values.size()); |
| 1231 | |
| 1232 | /* |
| 1233 | Case 2: Same number of buckets as the number of distinct values. We should |
| 1234 | end up with a singleton histogram. |
| 1235 | */ |
| 1236 | num_buckets = double_values.size(); |
| 1237 | Histogram *histogram2 = build_histogram(&m_mem_root, double_values, |
| 1238 | num_buckets, "db1", "tbl1", "col1"); |
| 1239 | |
| 1240 | EXPECT_EQ(Histogram::enum_histogram_type::SINGLETON, |
| 1241 | histogram2->get_histogram_type()); |
| 1242 | EXPECT_EQ(histogram2->get_num_buckets(), double_values.size()); |
| 1243 | EXPECT_EQ(histogram2->get_num_distinct_values(), double_values.size()); |
| 1244 | |
| 1245 | /* |
| 1246 | Case 3: More buckets than the number of distinct values. We should end up |
| 1247 | with a singleton histogram. |
| 1248 | */ |
| 1249 | num_buckets = std::numeric_limits<std::size_t>::max(); |
| 1250 | Histogram *histogram3 = build_histogram(&m_mem_root, double_values, |
| 1251 | num_buckets, "db1", "tbl1", "col1"); |
| 1252 | |
| 1253 | EXPECT_EQ(Histogram::enum_histogram_type::SINGLETON, |
| 1254 | histogram3->get_histogram_type()); |
| 1255 | EXPECT_LE(histogram3->get_num_buckets(), double_values.size()); |
| 1256 | EXPECT_EQ(histogram3->get_num_distinct_values(), double_values.size()); |
| 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | An utility function that verifies the actual values in the singleton JSON |
| 1261 | bucket. |
| 1262 | */ |
| 1263 | void VerifySingletonBucketContentsInt(Json_array *singleton_buckets, |
| 1264 | int bucket_index, |
| 1265 | double cumulative_frequency, |
| 1266 | longlong value) { |
| 1267 | Json_array *json_bucket = |
| 1268 | down_cast<Json_array *>((*singleton_buckets)[bucket_index]); |
| 1269 | |
| 1270 | Json_int *json_value = down_cast<Json_int *>((*json_bucket)[0]); |
| 1271 | Json_double *json_cumulative_frequency = |
| 1272 | down_cast<Json_double *>((*json_bucket)[1]); |
| 1273 | |
| 1274 | EXPECT_DOUBLE_EQ(cumulative_frequency, json_cumulative_frequency->value()); |
| 1275 | EXPECT_EQ(value, json_value->value()); |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | An utility function that verifies the actual values in the singleton JSON |
| 1280 | bucket. |
| 1281 | */ |
| 1282 | void VerifySingletonBucketContentsUInt(Json_array *singleton_buckets, |
| 1283 | int bucket_index, |
| 1284 | double cumulative_frequency, |
| 1285 | ulonglong value) { |
| 1286 | Json_array *json_bucket = |
| 1287 | down_cast<Json_array *>((*singleton_buckets)[bucket_index]); |
| 1288 | |
| 1289 | Json_uint *json_value = down_cast<Json_uint *>((*json_bucket)[0]); |
| 1290 | Json_double *json_cumulative_frequency = |
| 1291 | down_cast<Json_double *>((*json_bucket)[1]); |
| 1292 | |
| 1293 | EXPECT_DOUBLE_EQ(cumulative_frequency, json_cumulative_frequency->value()); |
| 1294 | EXPECT_EQ(value, json_value->value()); |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | An utility function that verifies the actual values in the singleton JSON |
| 1299 | bucket. |
| 1300 | */ |
| 1301 | void VerifySingletonBucketContentsString(Json_array *singleton_buckets, |
| 1302 | int bucket_index, |
| 1303 | double cumulative_frequency, |
| 1304 | String value, |
| 1305 | const CHARSET_INFO *charset) { |
| 1306 | Json_array *json_bucket = |
| 1307 | down_cast<Json_array *>((*singleton_buckets)[bucket_index]); |
| 1308 | |
| 1309 | Json_opaque *json_value_dom = down_cast<Json_opaque *>((*json_bucket)[0]); |
| 1310 | Json_double *json_cumulative_frequency = |
| 1311 | down_cast<Json_double *>((*json_bucket)[1]); |
| 1312 | |
| 1313 | String json_value(json_value_dom->value(), json_value_dom->size(), charset); |
| 1314 | |
| 1315 | EXPECT_EQ(json_value.charset()->number, value.charset()->number); |
| 1316 | EXPECT_DOUBLE_EQ(cumulative_frequency, json_cumulative_frequency->value()); |
| 1317 | EXPECT_EQ(sortcmp(&value, &json_value, charset), 0); |
| 1318 | } |
| 1319 | |
| 1320 | /* |
| 1321 | An utility function that verifies the actual values in the singleton JSON |
| 1322 | bucket. |
| 1323 | */ |
| 1324 | void VerifySingletonBucketContentsDouble(Json_array *singleton_buckets, |
| 1325 | int bucket_index, |
| 1326 | double cumulative_frequency, |
| 1327 | double value) { |
| 1328 | Json_array *json_bucket = |
| 1329 | down_cast<Json_array *>((*singleton_buckets)[bucket_index]); |
| 1330 | |
| 1331 | Json_double *json_value = down_cast<Json_double *>((*json_bucket)[0]); |
| 1332 | Json_double *json_cumulative_frequency = |
| 1333 | down_cast<Json_double *>((*json_bucket)[1]); |
| 1334 | |
| 1335 | EXPECT_DOUBLE_EQ(cumulative_frequency, json_cumulative_frequency->value()); |
| 1336 | EXPECT_EQ(value, json_value->value()); |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | An utility function that verifies the actual values in the singleton JSON |
| 1341 | bucket. |
| 1342 | */ |
| 1343 | void VerifySingletonBucketContentsDecimal(Json_array *singleton_buckets, |
| 1344 | int bucket_index, |
| 1345 | double cumulative_frequency, |
| 1346 | my_decimal value) { |
| 1347 | Json_array *json_bucket = |
| 1348 | down_cast<Json_array *>((*singleton_buckets)[bucket_index]); |
| 1349 | |
| 1350 | Json_decimal *json_value = down_cast<Json_decimal *>((*json_bucket)[0]); |
| 1351 | Json_double *json_cumulative_frequency = |
| 1352 | down_cast<Json_double *>((*json_bucket)[1]); |
| 1353 | |
| 1354 | EXPECT_DOUBLE_EQ(cumulative_frequency, json_cumulative_frequency->value()); |
| 1355 | EXPECT_EQ(my_decimal_cmp(json_value->value(), &value), 0); |
| 1356 | } |
| 1357 | |
| 1358 | /* |
| 1359 | An utility function that verifies the actual values in the singleton JSON |
| 1360 | bucket. |
| 1361 | */ |
| 1362 | void VerifySingletonBucketContentsTemporal(Json_array *singleton_buckets, |
| 1363 | int bucket_index, |
| 1364 | double cumulative_frequency, |
| 1365 | MYSQL_TIME value) { |
| 1366 | Json_array *json_bucket = |
| 1367 | down_cast<Json_array *>((*singleton_buckets)[bucket_index]); |
| 1368 | |
| 1369 | Json_datetime *json_value = down_cast<Json_datetime *>((*json_bucket)[0]); |
| 1370 | Json_double *json_cumulative_frequency = |
| 1371 | down_cast<Json_double *>((*json_bucket)[1]); |
| 1372 | |
| 1373 | EXPECT_DOUBLE_EQ(cumulative_frequency, json_cumulative_frequency->value()); |
| 1374 | EXPECT_EQ(my_time_compare(*json_value->value(), value), 0); |
| 1375 | } |
| 1376 | |
| 1377 | template <typename T> |
| 1378 | Equi_height<T> *BuildEquiHeightAndVerifyBasicProperties( |
| 1379 | MEM_ROOT *mem_root, const Value_map<T> &value_map, size_t num_buckets) { |
| 1380 | Equi_height<T> *histogram = Equi_height<T>::create( |
| 1381 | mem_root, "db1", "tbl1", "col1", value_map.get_data_type()); |
| 1382 | EXPECT_TRUE(histogram != nullptr); |
| 1383 | |
| 1384 | EXPECT_EQ(histogram->get_num_buckets(), 0U); |
| 1385 | EXPECT_EQ(histogram->get_num_buckets_specified(), 0U); |
| 1386 | EXPECT_EQ(histogram->get_num_distinct_values(), 0U); |
| 1387 | EXPECT_EQ(histogram->get_data_type(), value_map.get_data_type()); |
| 1388 | |
| 1389 | EXPECT_STREQ(histogram->get_database_name().str, "db1"); |
| 1390 | EXPECT_STREQ(histogram->get_table_name().str, "tbl1"); |
| 1391 | EXPECT_STREQ(histogram->get_column_name().str, "col1"); |
| 1392 | |
| 1393 | EXPECT_FALSE(histogram->build_histogram(value_map, num_buckets)); |
| 1394 | EXPECT_EQ(histogram->get_num_buckets(), num_buckets); |
| 1395 | EXPECT_EQ(histogram->get_num_buckets_specified(), num_buckets); |
| 1396 | EXPECT_EQ(histogram->get_num_distinct_values(), value_map.size()); |
| 1397 | EXPECT_EQ(histogram->get_character_set()->number, |
| 1398 | value_map.get_character_set()->number); |
| 1399 | |
| 1400 | return histogram; |
| 1401 | } |
| 1402 | |
| 1403 | template <typename T> |
| 1404 | void EquiHeightBucketsAreEqual(const equi_height::Bucket<T> &b1, |
| 1405 | const equi_height::Bucket<T> &b2) { |
| 1406 | EXPECT_EQ(b1.get_cumulative_frequency(), b2.get_cumulative_frequency()); |
| 1407 | EXPECT_EQ(b1.get_num_distinct(), b2.get_num_distinct()); |
| 1408 | |
| 1409 | // Equality check: Neither value is less than the other. |
| 1410 | EXPECT_FALSE(Histogram_comparator()(b1.get_lower_inclusive(), |
| 1411 | b2.get_lower_inclusive())); |
| 1412 | EXPECT_FALSE(Histogram_comparator()(b2.get_lower_inclusive(), |
| 1413 | b1.get_lower_inclusive())); |
| 1414 | |
| 1415 | EXPECT_FALSE(Histogram_comparator()(b1.get_upper_inclusive(), |
| 1416 | b2.get_upper_inclusive())); |
| 1417 | EXPECT_FALSE(Histogram_comparator()(b2.get_upper_inclusive(), |
| 1418 | b1.get_upper_inclusive())); |
| 1419 | } |
| 1420 | |
| 1421 | template <typename T> |
| 1422 | void EquiHeightHistogramsAreEqual(const Equi_height<T> *h1, |
| 1423 | const Equi_height<T> *h2) { |
| 1424 | EXPECT_STREQ(h1->get_database_name().str, h2->get_database_name().str); |
| 1425 | EXPECT_STREQ(h1->get_table_name().str, h2->get_table_name().str); |
| 1426 | EXPECT_STREQ(h1->get_column_name().str, h2->get_column_name().str); |
| 1427 | |
| 1428 | EXPECT_EQ(h1->get_histogram_type(), h2->get_histogram_type()); |
| 1429 | EXPECT_EQ(h1->get_data_type(), h2->get_data_type()); |
| 1430 | EXPECT_EQ(h1->get_num_buckets(), h2->get_num_buckets()); |
| 1431 | EXPECT_EQ(h1->get_num_buckets_specified(), h2->get_num_buckets_specified()); |
| 1432 | EXPECT_EQ(h1->get_num_distinct_values(), h2->get_num_distinct_values()); |
| 1433 | EXPECT_EQ(h1->get_non_null_values_fraction(), |
| 1434 | h2->get_non_null_values_fraction()); |
| 1435 | EXPECT_EQ(h1->get_null_values_fraction(), h2->get_null_values_fraction()); |
| 1436 | EXPECT_EQ(h1->get_character_set()->number, h2->get_character_set()->number); |
| 1437 | EXPECT_EQ(h1->get_sampling_rate(), h2->get_sampling_rate()); |
| 1438 | |
| 1439 | ASSERT_EQ(h1->get_num_buckets(), h2->get_num_buckets()); |
| 1440 | for (size_t i = 0; i < h1->get_num_buckets(); ++i) { |
| 1441 | EquiHeightBucketsAreEqual(h1->get_buckets()[i], h2->get_buckets()[i]); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | Serialize and deserialize the given histogram. |
| 1447 | Verify that the deserialized histogram matches the original. |
| 1448 | */ |
| 1449 | template <typename T> |
| 1450 | void VerifyEquiHeightSerialization(MEM_ROOT *mem_root, |
| 1451 | const Equi_height<T> *histogram) { |
| 1452 | // Serialization. |
| 1453 | Json_object json_object; |
| 1454 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 1455 | |
| 1456 | // Deserialization. |
| 1457 | Error_context ctx; |
| 1458 | Histogram *deserialized_histogram = Histogram::json_to_histogram( |
| 1459 | mem_root, "db1", "tbl1", "col1", json_object, &ctx); |
| 1460 | ASSERT_TRUE(deserialized_histogram != nullptr); |
| 1461 | Equi_height<T> *deserialized_equi_height = |
| 1462 | dynamic_cast<Equi_height<T> *>(deserialized_histogram); |
| 1463 | ASSERT_TRUE(deserialized_equi_height != nullptr); |
| 1464 | |
| 1465 | EquiHeightHistogramsAreEqual(histogram, deserialized_equi_height); |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | Verify that histogram selectivity estimates for the values in the value_map |
| 1470 | are within max_abs_error of the actual selectivities. |
| 1471 | */ |
| 1472 | template <typename T> |
| 1473 | void VerifyEquiHeightSelectivities(const Value_map<T> &value_map, |
| 1474 | const Equi_height<T> *histogram, |
| 1475 | double max_error_factor) { |
| 1476 | double max_abs_error = |
| 1477 | max_error_factor / |
| 1478 | static_cast<double>(histogram->get_num_buckets_specified()); |
| 1479 | |
| 1480 | ha_rows non_null_values = 0; |
| 1481 | for (const auto &[value, count] : value_map) non_null_values += count; |
| 1482 | ha_rows total_values = non_null_values + value_map.get_num_null_values(); |
| 1483 | |
| 1484 | ha_rows cumulative_values = 0; |
| 1485 | for (const auto &[value, count] : value_map) { |
| 1486 | double less_than_selectivity = |
| 1487 | static_cast<double>(cumulative_values) / total_values; |
| 1488 | EXPECT_NEAR(less_than_selectivity, |
| 1489 | histogram->get_less_than_selectivity(value), max_abs_error); |
| 1490 | |
| 1491 | double equal_to_selectivity = static_cast<double>(count) / total_values; |
| 1492 | EXPECT_NEAR(equal_to_selectivity, |
| 1493 | histogram->get_equal_to_selectivity(value), max_abs_error); |
| 1494 | |
| 1495 | double greater_than_selectivity = |
| 1496 | 1.0 - (less_than_selectivity + equal_to_selectivity); |
| 1497 | EXPECT_NEAR(greater_than_selectivity, |
| 1498 | histogram->get_greater_than_selectivity(value), max_abs_error); |
| 1499 | |
| 1500 | cumulative_values += count; |
| 1501 | } |
| 1502 | |
| 1503 | double null_fraction = |
| 1504 | static_cast<double>(value_map.get_num_null_values()) / total_values; |
| 1505 | double non_null_fraction = |
| 1506 | static_cast<double>(non_null_values) / total_values; |
| 1507 | |
| 1508 | const double null_fraction_max_error = 1.0e-9; |
| 1509 | EXPECT_NEAR(histogram->get_null_values_fraction(), null_fraction, |
| 1510 | null_fraction_max_error); |
| 1511 | EXPECT_NEAR(histogram->get_non_null_values_fraction(), non_null_fraction, |
| 1512 | null_fraction_max_error); |
| 1513 | } |
| 1514 | |
| 1515 | /* |
| 1516 | The json type that we expect histogram values (bucket endpoints) of a given |
| 1517 | type to be serialized into. |
| 1518 | */ |
| 1519 | enum_json_type ValueMapTypeToJsonType(Value_map_type value_type) { |
| 1520 | switch (value_type) { |
| 1521 | case Value_map_type::INVALID: |
| 1522 | return enum_json_type::J_ERROR; |
| 1523 | case Value_map_type::STRING: |
| 1524 | return enum_json_type::J_OPAQUE; |
| 1525 | case Value_map_type::INT: |
| 1526 | return enum_json_type::J_INT; |
| 1527 | case Value_map_type::UINT: |
| 1528 | return enum_json_type::J_UINT; |
| 1529 | case Value_map_type::DOUBLE: |
| 1530 | return enum_json_type::J_DOUBLE; |
| 1531 | case Value_map_type::DECIMAL: |
| 1532 | return enum_json_type::J_DECIMAL; |
| 1533 | case Value_map_type::DATE: |
| 1534 | return enum_json_type::J_DATE; |
| 1535 | case Value_map_type::TIME: |
| 1536 | return enum_json_type::J_TIME; |
| 1537 | case Value_map_type::DATETIME: |
| 1538 | return enum_json_type::J_DATETIME; |
| 1539 | case Value_map_type::ENUM: |
| 1540 | return enum_json_type::J_UINT; |
| 1541 | case Value_map_type::SET: |
| 1542 | return enum_json_type::J_UINT; |
| 1543 | } |
| 1544 | return enum_json_type::J_ERROR; |
| 1545 | } |
| 1546 | |
| 1547 | template <typename T> |
| 1548 | void VerifyEquiHeight(MEM_ROOT *mem_root, const Value_map<T> &value_map, |
| 1549 | size_t num_buckets, double max_error_factor = 1.0) { |
| 1550 | Equi_height<T> *histogram = |
| 1551 | BuildEquiHeightAndVerifyBasicProperties(mem_root, value_map, num_buckets); |
| 1552 | enum_json_type expected_json_value_type = |
| 1553 | ValueMapTypeToJsonType(histogram->get_data_type()); |
| 1554 | VerifyEquiHeightJSONStructure(histogram, expected_json_value_type); |
| 1555 | VerifyEquiHeightSerialization(mem_root, histogram); |
| 1556 | VerifyEquiHeightSelectivities(value_map, histogram, max_error_factor); |
| 1557 | } |
| 1558 | |
| 1559 | TEST_F(HistogramsTest, VerifyEquiHeightContentsInt1) { |
| 1560 | size_t num_buckets = 3; |
| 1561 | double max_error_factor = 1.0; |
| 1562 | VerifyEquiHeight(&m_mem_root, int_values, num_buckets, max_error_factor); |
| 1563 | } |
| 1564 | |
| 1565 | TEST_F(HistogramsTest, VerifyEquiHeightContentsInt2) { |
| 1566 | /* |
| 1567 | Create a value map with the following key/value pairs; |
| 1568 | [NULL, 10000] |
| 1569 | [0, 10000] |
| 1570 | [1, 9999] |
| 1571 | [2, 9998] |
| 1572 | ... |
| 1573 | [9998, 2] |
| 1574 | [9999, 1] |
| 1575 | */ |
| 1576 | Value_map<longlong> values(&my_charset_numeric, Value_map_type::INT); |
| 1577 | values.add_null_values(10000); |
| 1578 | for (longlong i = 0; i < 10000; i++) { |
| 1579 | size_t frequency = static_cast<size_t>(10000 - i); |
| 1580 | values.add_values(i, frequency); |
| 1581 | } |
| 1582 | |
| 1583 | size_t num_buckets = 10; |
| 1584 | double max_error_factor = 1.0; |
| 1585 | VerifyEquiHeight(&m_mem_root, values, num_buckets, max_error_factor); |
| 1586 | } |
| 1587 | |
| 1588 | TEST_F(HistogramsTest, VerifyEquiHeightContentsDouble) { |
| 1589 | size_t num_buckets = 3; |
| 1590 | double max_error_factor = 1.0; |
| 1591 | VerifyEquiHeight(&m_mem_root, double_values, num_buckets, max_error_factor); |
| 1592 | } |
| 1593 | |
| 1594 | TEST_F(HistogramsTest, VerifyEquiHeightContentsString) { |
| 1595 | size_t num_buckets = 3; |
| 1596 | double max_error_factor = 1.0; |
| 1597 | VerifyEquiHeight(&m_mem_root, string_values, num_buckets, max_error_factor); |
| 1598 | } |
| 1599 | |
| 1600 | TEST_F(HistogramsTest, VerifyEquiHeightContentsUint) { |
| 1601 | size_t num_buckets = 3; |
| 1602 | double max_error_factor = 1.0; |
| 1603 | VerifyEquiHeight(&m_mem_root, uint_values, num_buckets, max_error_factor); |
| 1604 | } |
| 1605 | |
| 1606 | TEST_F(HistogramsTest, VerifyEquiHeightContentsDecimal) { |
| 1607 | size_t num_buckets = 3; |
| 1608 | double max_error_factor = 1.0; |
| 1609 | VerifyEquiHeight(&m_mem_root, decimal_values, num_buckets, max_error_factor); |
| 1610 | } |
| 1611 | |
| 1612 | TEST_F(HistogramsTest, VerifyEquiHeightContentsDatetime) { |
| 1613 | size_t num_buckets = 3; |
| 1614 | double max_error_factor = 1.0; |
| 1615 | VerifyEquiHeight(&m_mem_root, datetime_values, num_buckets, max_error_factor); |
| 1616 | } |
| 1617 | |
| 1618 | TEST_F(HistogramsTest, VerifyEquiHeightContentsBlob) { |
| 1619 | size_t num_buckets = 3; |
| 1620 | double max_error_factor = 1.0; |
| 1621 | VerifyEquiHeight(&m_mem_root, blob_values, num_buckets, max_error_factor); |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | Create a singleton histogram, where we manually verify the value for every |
| 1626 | property in every bucket. |
| 1627 | */ |
| 1628 | TEST_F(HistogramsTest, VerifySingletonContentsDouble) { |
| 1629 | Singleton<double> *histogram = Singleton<double>::create( |
| 1630 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DOUBLE); |
| 1631 | ASSERT_TRUE(histogram != nullptr); |
| 1632 | |
| 1633 | EXPECT_STREQ(histogram->get_database_name().str, "db1"); |
| 1634 | EXPECT_STREQ(histogram->get_table_name().str, "tbl1"); |
| 1635 | EXPECT_STREQ(histogram->get_column_name().str, "col1"); |
| 1636 | |
| 1637 | Value_map<double> value_map(&my_charset_numeric, Value_map_type::DOUBLE); |
| 1638 | value_map.add_null_values(10); |
| 1639 | value_map.insert(double_values.begin(), double_values.end()); |
| 1640 | |
| 1641 | EXPECT_FALSE(histogram->build_histogram(value_map, value_map.size())); |
| 1642 | EXPECT_EQ(histogram->get_num_buckets(), value_map.size()); |
| 1643 | EXPECT_EQ(histogram->get_num_distinct_values(), value_map.size()); |
| 1644 | |
| 1645 | Json_object json_object; |
| 1646 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 1647 | |
| 1648 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 1649 | Json_array *json_buckets = static_cast<Json_array *>(buckets_dom); |
| 1650 | |
| 1651 | VerifySingletonBucketContentsDouble(json_buckets, 0, (10.0 / 70.0), |
| 1652 | std::numeric_limits<double>::lowest()); |
| 1653 | |
| 1654 | VerifySingletonBucketContentsDouble(json_buckets, 1, (20.0 / 70.0), 0.0); |
| 1655 | |
| 1656 | VerifySingletonBucketContentsDouble(json_buckets, 2, (30.0 / 70.0), |
| 1657 | std::numeric_limits<double>::epsilon()); |
| 1658 | |
| 1659 | VerifySingletonBucketContentsDouble(json_buckets, 3, (40.0 / 70.0), 42.0); |
| 1660 | |
| 1661 | VerifySingletonBucketContentsDouble(json_buckets, 4, (50.0 / 70.0), 43.0); |
| 1662 | |
| 1663 | VerifySingletonBucketContentsDouble(json_buckets, 5, (60.0 / 70.0), |
| 1664 | std::numeric_limits<double>::max()); |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | Create a singleton histogram, where we manually verify the value for every |
| 1669 | property in every bucket. |
| 1670 | */ |
| 1671 | TEST_F(HistogramsTest, VerifySingletonContentsInt) { |
| 1672 | Singleton<longlong> *histogram = Singleton<longlong>::create( |
| 1673 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 1674 | ASSERT_TRUE(histogram != nullptr); |
| 1675 | |
| 1676 | EXPECT_STREQ(histogram->get_database_name().str, "db1"); |
| 1677 | EXPECT_STREQ(histogram->get_table_name().str, "tbl1"); |
| 1678 | EXPECT_STREQ(histogram->get_column_name().str, "col1"); |
| 1679 | |
| 1680 | Value_map<longlong> value_map(&my_charset_numeric, Value_map_type::INT); |
| 1681 | value_map.add_null_values(10); |
| 1682 | value_map.insert(int_values.begin(), int_values.end()); |
| 1683 | |
| 1684 | EXPECT_FALSE(histogram->build_histogram(value_map, value_map.size())); |
| 1685 | EXPECT_EQ(histogram->get_num_buckets(), value_map.size()); |
| 1686 | EXPECT_EQ(histogram->get_num_distinct_values(), value_map.size()); |
| 1687 | |
| 1688 | Json_object json_object; |
| 1689 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 1690 | |
| 1691 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 1692 | Json_array *json_buckets = static_cast<Json_array *>(buckets_dom); |
| 1693 | |
| 1694 | VerifySingletonBucketContentsInt(json_buckets, 0, (10.0 / 80.0), |
| 1695 | std::numeric_limits<longlong>::lowest()); |
| 1696 | |
| 1697 | VerifySingletonBucketContentsInt(json_buckets, 1, (20.0 / 80.0), -1LL); |
| 1698 | |
| 1699 | VerifySingletonBucketContentsInt(json_buckets, 2, (30.0 / 80.0), 0LL); |
| 1700 | |
| 1701 | VerifySingletonBucketContentsInt(json_buckets, 3, (40.0 / 80.0), 1LL); |
| 1702 | |
| 1703 | VerifySingletonBucketContentsInt(json_buckets, 4, (50.0 / 80.0), 42LL); |
| 1704 | |
| 1705 | VerifySingletonBucketContentsInt(json_buckets, 5, (60.0 / 80.0), 10000LL); |
| 1706 | |
| 1707 | VerifySingletonBucketContentsInt(json_buckets, 6, (70.0 / 80.0), |
| 1708 | std::numeric_limits<longlong>::max()); |
| 1709 | } |
| 1710 | |
| 1711 | /* |
| 1712 | Create a singleton histogram, where we manually verify the value for every |
| 1713 | property in every bucket. |
| 1714 | */ |
| 1715 | TEST_F(HistogramsTest, VerifySingletonContentsUInt) { |
| 1716 | Singleton<ulonglong> *histogram = Singleton<ulonglong>::create( |
| 1717 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::UINT); |
| 1718 | ASSERT_TRUE(histogram != nullptr); |
| 1719 | |
| 1720 | EXPECT_STREQ(histogram->get_database_name().str, "db1"); |
| 1721 | EXPECT_STREQ(histogram->get_table_name().str, "tbl1"); |
| 1722 | EXPECT_STREQ(histogram->get_column_name().str, "col1"); |
| 1723 | |
| 1724 | Value_map<ulonglong> value_map(&my_charset_numeric, Value_map_type::UINT); |
| 1725 | value_map.add_null_values(10); |
| 1726 | value_map.insert(uint_values.begin(), uint_values.end()); |
| 1727 | |
| 1728 | EXPECT_FALSE(histogram->build_histogram(value_map, value_map.size())); |
| 1729 | EXPECT_EQ(histogram->get_num_buckets(), value_map.size()); |
| 1730 | EXPECT_EQ(histogram->get_num_distinct_values(), value_map.size()); |
| 1731 | |
| 1732 | Json_object json_object; |
| 1733 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 1734 | |
| 1735 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 1736 | Json_array *json_buckets = static_cast<Json_array *>(buckets_dom); |
| 1737 | |
| 1738 | VerifySingletonBucketContentsUInt(json_buckets, 0, (10.0 / 60.0), |
| 1739 | std::numeric_limits<ulonglong>::lowest()); |
| 1740 | |
| 1741 | VerifySingletonBucketContentsUInt(json_buckets, 1, (20.0 / 60.0), 42ULL); |
| 1742 | |
| 1743 | VerifySingletonBucketContentsUInt(json_buckets, 2, (30.0 / 60.0), 43ULL); |
| 1744 | |
| 1745 | VerifySingletonBucketContentsUInt(json_buckets, 3, (40.0 / 60.0), 10000ULL); |
| 1746 | |
| 1747 | VerifySingletonBucketContentsUInt(json_buckets, 4, (50.0 / 60.0), |
| 1748 | std::numeric_limits<ulonglong>::max()); |
| 1749 | } |
| 1750 | |
| 1751 | /* |
| 1752 | Create a singleton histogram, where we manually verify the value for every |
| 1753 | property in every bucket. |
| 1754 | */ |
| 1755 | TEST_F(HistogramsTest, VerifySingletonContentsString) { |
| 1756 | Singleton<String> *histogram = Singleton<String>::create( |
| 1757 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::STRING); |
| 1758 | ASSERT_TRUE(histogram != nullptr); |
| 1759 | |
| 1760 | EXPECT_STREQ(histogram->get_database_name().str, "db1"); |
| 1761 | EXPECT_STREQ(histogram->get_table_name().str, "tbl1"); |
| 1762 | EXPECT_STREQ(histogram->get_column_name().str, "col1"); |
| 1763 | |
| 1764 | Value_map<String> value_map(&my_charset_latin1, Value_map_type::STRING); |
| 1765 | value_map.add_null_values(10); |
| 1766 | value_map.insert(string_values.begin(), string_values.end()); |
| 1767 | |
| 1768 | EXPECT_FALSE(histogram->build_histogram(value_map, value_map.size())); |
| 1769 | EXPECT_EQ(histogram->get_num_buckets(), value_map.size()); |
| 1770 | EXPECT_EQ(histogram->get_num_distinct_values(), value_map.size()); |
| 1771 | |
| 1772 | Json_object json_object; |
| 1773 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 1774 | |
| 1775 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 1776 | Json_array *json_buckets = static_cast<Json_array *>(buckets_dom); |
| 1777 | |
| 1778 | String string1("", &my_charset_latin1); |
| 1779 | String string2("string1", &my_charset_latin1); |
| 1780 | String string3("string2", &my_charset_latin1); |
| 1781 | String string4("string3", &my_charset_latin1); |
| 1782 | String string5("string4", &my_charset_latin1); |
| 1783 | |
| 1784 | VerifySingletonBucketContentsString(json_buckets, 0, (10.0 / 60.0), string1, |
| 1785 | &my_charset_latin1); |
| 1786 | VerifySingletonBucketContentsString(json_buckets, 1, (20.0 / 60.0), string2, |
| 1787 | &my_charset_latin1); |
| 1788 | VerifySingletonBucketContentsString(json_buckets, 2, (30.0 / 60.0), string3, |
| 1789 | &my_charset_latin1); |
| 1790 | VerifySingletonBucketContentsString(json_buckets, 3, (40.0 / 60.0), string4, |
| 1791 | &my_charset_latin1); |
| 1792 | VerifySingletonBucketContentsString(json_buckets, 4, (50.0 / 60.0), string5, |
| 1793 | &my_charset_latin1); |
| 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | Create a singleton histogram, where we manually verify the value for every |
| 1798 | property in every bucket. |
| 1799 | */ |
| 1800 | TEST_F(HistogramsTest, VerifySingletonContentsDecimal) { |
| 1801 | Singleton<my_decimal> *histogram = Singleton<my_decimal>::create( |
| 1802 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DECIMAL); |
| 1803 | ASSERT_TRUE(histogram != nullptr); |
| 1804 | |
| 1805 | EXPECT_STREQ(histogram->get_database_name().str, "db1"); |
| 1806 | EXPECT_STREQ(histogram->get_table_name().str, "tbl1"); |
| 1807 | EXPECT_STREQ(histogram->get_column_name().str, "col1"); |
| 1808 | |
| 1809 | Value_map<my_decimal> value_map(&my_charset_latin1, Value_map_type::DECIMAL); |
| 1810 | value_map.add_null_values(10); |
| 1811 | value_map.insert(decimal_values.begin(), decimal_values.end()); |
| 1812 | |
| 1813 | EXPECT_FALSE(histogram->build_histogram(value_map, value_map.size())); |
| 1814 | EXPECT_EQ(histogram->get_num_buckets(), value_map.size()); |
| 1815 | EXPECT_EQ(histogram->get_num_distinct_values(), value_map.size()); |
| 1816 | |
| 1817 | Json_object json_object; |
| 1818 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 1819 | |
| 1820 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 1821 | Json_array *json_buckets = static_cast<Json_array *>(buckets_dom); |
| 1822 | |
| 1823 | my_decimal decimal1; |
| 1824 | int2my_decimal(E_DEC_FATAL_ERROR, -1000LL, false, &decimal1); |
| 1825 | |
| 1826 | my_decimal decimal2; |
| 1827 | int2my_decimal(E_DEC_FATAL_ERROR, 0LL, false, &decimal2); |
| 1828 | |
| 1829 | my_decimal decimal3; |
| 1830 | int2my_decimal(E_DEC_FATAL_ERROR, 1LL, false, &decimal3); |
| 1831 | |
| 1832 | my_decimal decimal4; |
| 1833 | int2my_decimal(E_DEC_FATAL_ERROR, 42LL, false, &decimal4); |
| 1834 | |
| 1835 | my_decimal decimal5; |
| 1836 | int2my_decimal(E_DEC_FATAL_ERROR, 1000LL, false, &decimal5); |
| 1837 | |
| 1838 | VerifySingletonBucketContentsDecimal(json_buckets, 0, (10.0 / 60.0), |
| 1839 | decimal1); |
| 1840 | VerifySingletonBucketContentsDecimal(json_buckets, 1, (20.0 / 60.0), |
| 1841 | decimal2); |
| 1842 | VerifySingletonBucketContentsDecimal(json_buckets, 2, (30.0 / 60.0), |
| 1843 | decimal3); |
| 1844 | VerifySingletonBucketContentsDecimal(json_buckets, 3, (40.0 / 60.0), |
| 1845 | decimal4); |
| 1846 | VerifySingletonBucketContentsDecimal(json_buckets, 4, (50.0 / 60.0), |
| 1847 | decimal5); |
| 1848 | } |
| 1849 | |
| 1850 | /* |
| 1851 | Create a singleton histogram, where we manually verify the value for every |
| 1852 | property in every bucket. |
| 1853 | */ |
| 1854 | TEST_F(HistogramsTest, VerifySingletonContentsDateTime) { |
| 1855 | Singleton<MYSQL_TIME> *histogram = Singleton<MYSQL_TIME>::create( |
| 1856 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::DATETIME); |
| 1857 | ASSERT_TRUE(histogram != nullptr); |
| 1858 | |
| 1859 | EXPECT_STREQ(histogram->get_database_name().str, "db1"); |
| 1860 | EXPECT_STREQ(histogram->get_table_name().str, "tbl1"); |
| 1861 | EXPECT_STREQ(histogram->get_column_name().str, "col1"); |
| 1862 | |
| 1863 | Value_map<MYSQL_TIME> value_map(&my_charset_latin1, Value_map_type::DATETIME); |
| 1864 | value_map.add_null_values(10); |
| 1865 | value_map.insert(datetime_values.begin(), datetime_values.end()); |
| 1866 | |
| 1867 | EXPECT_FALSE(histogram->build_histogram(value_map, value_map.size())); |
| 1868 | EXPECT_EQ(histogram->get_num_buckets(), value_map.size()); |
| 1869 | EXPECT_EQ(histogram->get_num_distinct_values(), value_map.size()); |
| 1870 | |
| 1871 | Json_object json_object; |
| 1872 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 1873 | |
| 1874 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 1875 | Json_array *json_buckets = static_cast<Json_array *>(buckets_dom); |
| 1876 | |
| 1877 | MYSQL_TIME time1; |
| 1878 | TIME_from_longlong_datetime_packed(&time1, 914866242077065216); |
| 1879 | |
| 1880 | MYSQL_TIME time2; |
| 1881 | TIME_from_longlong_datetime_packed(&time2, 914866242077065217); |
| 1882 | |
| 1883 | MYSQL_TIME time3; |
| 1884 | TIME_from_longlong_datetime_packed(&time3, 1845541820734373888); |
| 1885 | |
| 1886 | MYSQL_TIME time4; |
| 1887 | TIME_from_longlong_datetime_packed(&time4, 9147936188962652734); |
| 1888 | |
| 1889 | MYSQL_TIME time5; |
| 1890 | TIME_from_longlong_datetime_packed(&time5, 9147936188962652735); |
| 1891 | |
| 1892 | VerifySingletonBucketContentsTemporal(json_buckets, 0, (10.0 / 60.0), time1); |
| 1893 | VerifySingletonBucketContentsTemporal(json_buckets, 1, (20.0 / 60.0), time2); |
| 1894 | VerifySingletonBucketContentsTemporal(json_buckets, 2, (30.0 / 60.0), time3); |
| 1895 | VerifySingletonBucketContentsTemporal(json_buckets, 3, (40.0 / 60.0), time4); |
| 1896 | VerifySingletonBucketContentsTemporal(json_buckets, 4, (50.0 / 60.0), time5); |
| 1897 | } |
| 1898 | |
| 1899 | /* |
| 1900 | Create a singleton histogram, where we manually verify the value for every |
| 1901 | property in every bucket. |
| 1902 | */ |
| 1903 | TEST_F(HistogramsTest, VerifySingletonContentsBlob) { |
| 1904 | Singleton<String> *histogram = Singleton<String>::create( |
| 1905 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::STRING); |
| 1906 | ASSERT_TRUE(histogram != nullptr); |
| 1907 | |
| 1908 | EXPECT_STREQ(histogram->get_database_name().str, "db1"); |
| 1909 | EXPECT_STREQ(histogram->get_table_name().str, "tbl1"); |
| 1910 | EXPECT_STREQ(histogram->get_column_name().str, "col1"); |
| 1911 | |
| 1912 | Value_map<String> value_map(&my_charset_bin, Value_map_type::STRING); |
| 1913 | value_map.add_null_values(10); |
| 1914 | value_map.insert(blob_values.begin(), blob_values.end()); |
| 1915 | |
| 1916 | EXPECT_FALSE(histogram->build_histogram(value_map, value_map.size())); |
| 1917 | EXPECT_EQ(histogram->get_num_buckets(), value_map.size()); |
| 1918 | EXPECT_EQ(histogram->get_num_distinct_values(), value_map.size()); |
| 1919 | |
| 1920 | Json_object json_object; |
| 1921 | EXPECT_FALSE(histogram->histogram_to_json(&json_object)); |
| 1922 | |
| 1923 | Json_dom *buckets_dom = json_object.get("buckets"); |
| 1924 | Json_array *json_buckets = static_cast<Json_array *>(buckets_dom); |
| 1925 | |
| 1926 | String blob1(blob_buf1, 4, &my_charset_bin); |
| 1927 | String blob2("bar", &my_charset_bin); |
| 1928 | String blob3("foo", &my_charset_bin); |
| 1929 | String blob4("foobar", &my_charset_bin); |
| 1930 | String blob5(blob_buf2, 4, &my_charset_bin); |
| 1931 | |
| 1932 | VerifySingletonBucketContentsString(json_buckets, 0, (10.0 / 60.0), blob1, |
| 1933 | &my_charset_bin); |
| 1934 | VerifySingletonBucketContentsString(json_buckets, 1, (20.0 / 60.0), blob2, |
| 1935 | &my_charset_bin); |
| 1936 | VerifySingletonBucketContentsString(json_buckets, 2, (30.0 / 60.0), blob3, |
| 1937 | &my_charset_bin); |
| 1938 | VerifySingletonBucketContentsString(json_buckets, 3, (40.0 / 60.0), blob4, |
| 1939 | &my_charset_bin); |
| 1940 | VerifySingletonBucketContentsString(json_buckets, 4, (50.0 / 60.0), blob5, |
| 1941 | &my_charset_bin); |
| 1942 | } |
| 1943 | |
| 1944 | /* |
| 1945 | Create an equi-height histogram with zero buckets sepcified. Ensure that the |
| 1946 | resulting histogram actually have zero buckets. |
| 1947 | */ |
| 1948 | TEST_F(HistogramsTest, EmptyEquiHeightHistogram) { |
| 1949 | Equi_height<longlong> *histogram = Equi_height<longlong>::create( |
| 1950 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 1951 | ASSERT_TRUE(histogram != nullptr); |
| 1952 | |
| 1953 | Value_map<longlong> empty_value_map(&my_charset_numeric, Value_map_type::INT); |
| 1954 | |
| 1955 | // Empty map, no null values, but several buckets specified. |
| 1956 | EXPECT_FALSE(histogram->build_histogram(empty_value_map, 10U)); |
| 1957 | EXPECT_EQ(histogram->get_num_buckets(), 0U); |
| 1958 | EXPECT_EQ(histogram->get_num_distinct_values(), 0U); |
| 1959 | |
| 1960 | // Empty map, multiple null values and several buckets specified. |
| 1961 | empty_value_map.add_null_values(500); |
| 1962 | EXPECT_FALSE(histogram->build_histogram(empty_value_map, 10U)); |
| 1963 | EXPECT_EQ(histogram->get_num_buckets(), 0U); |
| 1964 | EXPECT_EQ(histogram->get_num_distinct_values(), 0U); |
| 1965 | } |
| 1966 | |
| 1967 | /* |
| 1968 | Create a singleton histogram from an empty value map. Ensure that the |
| 1969 | resulting histogram actually have zero buckets. |
| 1970 | */ |
| 1971 | TEST_F(HistogramsTest, EmptySingletonHistogram) { |
| 1972 | Singleton<longlong> *histogram = Singleton<longlong>::create( |
| 1973 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 1974 | ASSERT_TRUE(histogram != nullptr); |
| 1975 | |
| 1976 | Value_map<longlong> empty_value_map(&my_charset_numeric, Value_map_type::INT); |
| 1977 | |
| 1978 | // Empty map, no null values, |
| 1979 | EXPECT_FALSE(histogram->build_histogram(empty_value_map, 10U)); |
| 1980 | EXPECT_EQ(histogram->get_num_buckets(), 0U); |
| 1981 | EXPECT_EQ(histogram->get_num_distinct_values(), 0U); |
| 1982 | } |
| 1983 | |
| 1984 | /* |
| 1985 | Create an equi-height histogram from an empty value map, but with several NULL |
| 1986 | values. Check that the resulting histogram has a fraction of NULL values equal |
| 1987 | to 1.0. |
| 1988 | */ |
| 1989 | TEST_F(HistogramsTest, EquiHeightNullValues) { |
| 1990 | Equi_height<longlong> *histogram = Equi_height<longlong>::create( |
| 1991 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 1992 | ASSERT_TRUE(histogram != nullptr); |
| 1993 | |
| 1994 | Value_map<longlong> empty_value_map(&my_charset_numeric, Value_map_type::INT); |
| 1995 | empty_value_map.add_null_values(10); |
| 1996 | |
| 1997 | EXPECT_FALSE(histogram->build_histogram(empty_value_map, 1U)); |
| 1998 | EXPECT_DOUBLE_EQ(histogram->get_null_values_fraction(), 1.0); |
| 1999 | } |
| 2000 | |
| 2001 | /* |
| 2002 | Create a singleton histogram from an empty value map, but with several NULL |
| 2003 | values. Check that the resulting histogram has a fraction of NULL values equal |
| 2004 | to 1.0. |
| 2005 | */ |
| 2006 | TEST_F(HistogramsTest, SingletonNullValues) { |
| 2007 | Singleton<longlong> *histogram = Singleton<longlong>::create( |
| 2008 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 2009 | ASSERT_TRUE(histogram != nullptr); |
| 2010 | |
| 2011 | Value_map<longlong> empty_value_map(&my_charset_numeric, Value_map_type::INT); |
| 2012 | empty_value_map.add_null_values(10); |
| 2013 | |
| 2014 | EXPECT_FALSE(histogram->build_histogram(empty_value_map, 10U)); |
| 2015 | EXPECT_DOUBLE_EQ(histogram->get_null_values_fraction(), 1.0); |
| 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | Check that the histogram comparator only checks the 42 first characters of |
| 2020 | long string values. If the strings differ at any character after the 42nd |
| 2021 | character, the strings should be considered equal. |
| 2022 | |
| 2023 | This does not test any histogram per se, but the histogram comparator. |
| 2024 | */ |
| 2025 | TEST_F(HistogramsTest, LongStringValues) { |
| 2026 | /* |
| 2027 | Ensure that HISTOGRAM_MAX_COMPARE_LENGTH is set to the value we have assumed |
| 2028 | throughout this test. |
| 2029 | */ |
| 2030 | EXPECT_EQ(42U, HISTOGRAM_MAX_COMPARE_LENGTH); |
| 2031 | |
| 2032 | Value_map<String> long_strings(&my_charset_latin1, Value_map_type::STRING); |
| 2033 | |
| 2034 | /* |
| 2035 | The following three strings should be considered equal, since the 42 first |
| 2036 | characters are equal. |
| 2037 | */ |
| 2038 | String string1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnop0000", |
| 2039 | &my_charset_latin1); |
| 2040 | |
| 2041 | String string2("abcdefghijklmnopqrstuvwxyzabcdefghijklmnop2222", |
| 2042 | &my_charset_latin1); |
| 2043 | |
| 2044 | String string3("abcdefghijklmnopqrstuvwxyzabcdefghijklmnop1111", |
| 2045 | &my_charset_latin1); |
| 2046 | |
| 2047 | /* |
| 2048 | The following three strings should be considered different, since they |
| 2049 | differ at the 42nd character |
| 2050 | */ |
| 2051 | String string4("abcdefghijklmnopqrstuvwxyzabcdefghijklmno2222", |
| 2052 | &my_charset_latin1); |
| 2053 | |
| 2054 | String string5("abcdefghijklmnopqrstuvwxyzabcdefghijklmno1111", |
| 2055 | &my_charset_latin1); |
| 2056 | |
| 2057 | String string6("abcdefghijklmnopqrstuvwxyzabcdefghijklmno0000", |
| 2058 | &my_charset_latin1); |
| 2059 | |
| 2060 | long_strings.add_values(string1, 10); |
| 2061 | long_strings.add_values(string2, 10); |
| 2062 | long_strings.add_values(string3, 10); |
| 2063 | long_strings.add_values(string4, 10); |
| 2064 | long_strings.add_values(string5, 10); |
| 2065 | long_strings.add_values(string6, 10); |
| 2066 | |
| 2067 | EXPECT_EQ(4U, long_strings.size()); |
| 2068 | } |
| 2069 | |
| 2070 | /* |
| 2071 | Check that the histogram comparator only checks the 42 first bytes of long |
| 2072 | binary values. If the values differ at any byte after the 42nd byte, the |
| 2073 | binary values should be considered equal. |
| 2074 | |
| 2075 | This does not test any histogram per se, but the histogram comparator. |
| 2076 | */ |
| 2077 | TEST_F(HistogramsTest, LongBlobValues) { |
| 2078 | /* |
| 2079 | Ensure that HISTOGRAM_MAX_COMPARE_LENGTH is set to the value we have assume |
| 2080 | throughout this test. |
| 2081 | */ |
| 2082 | EXPECT_EQ(42U, HISTOGRAM_MAX_COMPARE_LENGTH); |
| 2083 | |
| 2084 | Value_map<String> long_blobs(&my_charset_bin, Value_map_type::STRING); |
| 2085 | |
| 2086 | /* |
| 2087 | The following three blobs should be considered equal, since the 42 first |
| 2088 | bytes are equal. |
| 2089 | */ |
| 2090 | const char buf1[46] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, |
| 2091 | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2092 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 2093 | 37, 38, 39, 40, 41, 42, 2, 2, 2, 2}; |
| 2094 | |
| 2095 | const char buf2[46] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, |
| 2096 | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2097 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 2098 | 37, 38, 39, 40, 41, 42, 1, 1, 1, 1}; |
| 2099 | |
| 2100 | const char buf3[46] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, |
| 2101 | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2102 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 2103 | 37, 38, 39, 40, 41, 42, 0, 0, 0, 0}; |
| 2104 | |
| 2105 | /* |
| 2106 | The following three blobs should be considered different, since they differ |
| 2107 | at the 42nd byte |
| 2108 | */ |
| 2109 | const char buf4[46] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, |
| 2110 | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2111 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 2112 | 37, 38, 39, 40, 41, 2, 2, 2, 2, 2}; |
| 2113 | |
| 2114 | const char buf5[46] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, |
| 2115 | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2116 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 2117 | 37, 38, 39, 40, 41, 1, 1, 1, 1, 1}; |
| 2118 | |
| 2119 | const char buf6[46] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, |
| 2120 | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 2121 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 2122 | 37, 38, 39, 40, 41, 0, 0, 0, 0, 0}; |
| 2123 | |
| 2124 | long_blobs.add_values(String(buf1, 46, &my_charset_bin), 10); |
| 2125 | long_blobs.add_values(String(buf2, 46, &my_charset_bin), 10); |
| 2126 | long_blobs.add_values(String(buf3, 46, &my_charset_bin), 10); |
| 2127 | long_blobs.add_values(String(buf4, 46, &my_charset_bin), 10); |
| 2128 | long_blobs.add_values(String(buf5, 46, &my_charset_bin), 10); |
| 2129 | long_blobs.add_values(String(buf6, 46, &my_charset_bin), 10); |
| 2130 | |
| 2131 | EXPECT_EQ(4U, long_blobs.size()); |
| 2132 | } |
| 2133 | |
| 2134 | /* |
| 2135 | Check that the histogram comparator only checks the 42 first characters of |
| 2136 | long string values, where the strings are multi-byte strings. If the strings |
| 2137 | differ at any character after the 42nd character, the strings should be |
| 2138 | considered equal. |
| 2139 | |
| 2140 | This does not test any histogram per se, but the histogram comparator. |
| 2141 | */ |
| 2142 | TEST_F(HistogramsTest, MultiByteStrings) { |
| 2143 | /* |
| 2144 | Ensure that HISTOGRAM_MAX_COMPARE_LENGTH is set to the value we have assumed |
| 2145 | throughout this test. |
| 2146 | */ |
| 2147 | EXPECT_EQ(42U, HISTOGRAM_MAX_COMPARE_LENGTH); |
| 2148 | |
| 2149 | /* |
| 2150 | Declare the strings to have UCS2 character set, which is fixed 2 byte per |
| 2151 | character. |
| 2152 | */ |
| 2153 | MY_CHARSET_LOADER loader; |
| 2154 | CHARSET_INFO *cs = |
| 2155 | my_collation_get_by_name(&loader, "ucs2_general_ci", MYF(0)); |
| 2156 | |
| 2157 | Value_map<String> long_strings(cs, Value_map_type::STRING); |
| 2158 | |
| 2159 | String string1("", cs); |
| 2160 | String string2("", cs); |
| 2161 | String string3("", cs); |
| 2162 | String string4("", cs); |
| 2163 | String string5("", cs); |
| 2164 | String string6("", cs); |
| 2165 | |
| 2166 | /* |
| 2167 | The following three strings should be considered equal, since the 42 first |
| 2168 | characters are equal. |
| 2169 | */ |
| 2170 | string1.append("abcdefghijklmnopqrstuvwxyzabcdefghijklmnop2222", 46, |
| 2171 | &my_charset_latin1); |
| 2172 | string2.append("abcdefghijklmnopqrstuvwxyzabcdefghijklmnop1111", 46, |
| 2173 | &my_charset_latin1); |
| 2174 | string3.append("abcdefghijklmnopqrstuvwxyzabcdefghijklmnop0000", 46, |
| 2175 | &my_charset_latin1); |
| 2176 | /* |
| 2177 | The following three strings should be considered different, since they |
| 2178 | differ at the 42nd character |
| 2179 | */ |
| 2180 | string4.append("abcdefghijklmnopqrstuvwxyzabcdefghijklmno22222", 46, |
| 2181 | &my_charset_latin1); |
| 2182 | string5.append("abcdefghijklmnopqrstuvwxyzabcdefghijklmno11111", 46, |
| 2183 | &my_charset_latin1); |
| 2184 | string6.append("abcdefghijklmnopqrstuvwxyzabcdefghijklmno00000", 46, |
| 2185 | &my_charset_latin1); |
| 2186 | |
| 2187 | /* |
| 2188 | Since we are using UCS-2, we should have twice the amount of bytes as we |
| 2189 | have characters. |
| 2190 | */ |
| 2191 | EXPECT_EQ(string6.numchars(), 46U); |
| 2192 | EXPECT_EQ(string6.length(), 92U); |
| 2193 | |
| 2194 | long_strings.add_values(string1, 10); |
| 2195 | long_strings.add_values(string2, 10); |
| 2196 | long_strings.add_values(string3, 10); |
| 2197 | long_strings.add_values(string4, 10); |
| 2198 | long_strings.add_values(string5, 10); |
| 2199 | long_strings.add_values(string6, 10); |
| 2200 | |
| 2201 | EXPECT_EQ(4U, long_strings.size()); |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | Build an equi-height histogram with a significant amount of distinct values. |
| 2206 | */ |
| 2207 | TEST_F(HistogramsTest, BigEquiHeight) { |
| 2208 | Value_map<longlong> values(&my_charset_numeric, Value_map_type::INT); |
| 2209 | values.add_null_values(514); |
| 2210 | for (longlong i = 0; i < 100000; i++) { |
| 2211 | size_t frequency = static_cast<size_t>((rand() % 10000) + 1); |
| 2212 | values.add_values(i, frequency); |
| 2213 | } |
| 2214 | |
| 2215 | Equi_height<longlong> *histogram = Equi_height<longlong>::create( |
| 2216 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 2217 | ASSERT_TRUE(histogram != nullptr); |
| 2218 | |
| 2219 | EXPECT_EQ(0U, histogram->get_num_buckets()); |
| 2220 | EXPECT_EQ(0U, histogram->get_num_distinct_values()); |
| 2221 | |
| 2222 | // Build a histogram with 200 buckets. |
| 2223 | size_t num_buckets = 200; |
| 2224 | EXPECT_FALSE(histogram->build_histogram(values, num_buckets)); |
| 2225 | EXPECT_LE(histogram->get_num_buckets(), num_buckets); |
| 2226 | EXPECT_EQ(100000U, histogram->get_num_distinct_values()); |
| 2227 | |
| 2228 | VerifyEquiHeightJSONStructure(histogram, enum_json_type::J_INT); |
| 2229 | VerifyEquiHeightBucketConstraintsInt(histogram); |
| 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | Build a singleton histogram, and check if the printed time is within a few |
| 2234 | seconds of the current time. |
| 2235 | |
| 2236 | We do not add any values to the histogram, since we want it to be built as |
| 2237 | fast as possible. |
| 2238 | */ |
| 2239 | TEST_F(HistogramsTest, HistogramTimeCreated) { |
| 2240 | Value_map<longlong> values(&my_charset_numeric, Value_map_type::INT); |
| 2241 | |
| 2242 | Singleton<longlong> *histogram = Singleton<longlong>::create( |
| 2243 | &m_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 2244 | ASSERT_TRUE(histogram != nullptr); |
| 2245 | |
| 2246 | EXPECT_EQ(0U, histogram->get_num_buckets()); |
| 2247 | EXPECT_EQ(0U, histogram->get_num_distinct_values()); |
| 2248 | |
| 2249 | EXPECT_FALSE(histogram->build_histogram(values, 10U)); |
| 2250 | |
| 2251 | // Get the current time in GMT timezone. |
| 2252 | MYSQL_TIME current_time; |
| 2253 | ulonglong micro_time = my_micro_time(); |
| 2254 | my_tz_UTC->gmt_sec_to_TIME(¤t_time, |
| 2255 | static_cast<my_time_t>(micro_time / 1000000)); |
| 2256 | |
| 2257 | Json_object json_histogram; |
| 2258 | EXPECT_FALSE(histogram->histogram_to_json(&json_histogram)); |
| 2259 | |
| 2260 | Json_dom *last_updated_dom = json_histogram.get("last-updated"); |
| 2261 | Json_datetime *last_updated = down_cast<Json_datetime *>(last_updated_dom); |
| 2262 | |
| 2263 | longlong seconds_diff = 0; |
| 2264 | long microseconds_diff = 0; |
| 2265 | calc_time_diff(*last_updated->value(), current_time, 1, &seconds_diff, |
| 2266 | µseconds_diff); |
| 2267 | |
| 2268 | EXPECT_LE(seconds_diff, 2LL); |
| 2269 | } |
| 2270 | |
| 2271 | /* |
| 2272 | Check that an out-of-memory situation doesn't crash brutally, but fails |
| 2273 | gracefully. |
| 2274 | */ |
| 2275 | TEST_F(HistogramsTest, HistogramOOM) { |
| 2276 | Value_map<longlong> values(&my_charset_numeric, Value_map_type::INT); |
| 2277 | values.add_values(1, 10); |
| 2278 | values.add_values(2, 10); |
| 2279 | values.add_values(3, 10); |
| 2280 | values.add_values(4, 10); |
| 2281 | |
| 2282 | MEM_ROOT oom_mem_root(PSI_NOT_INSTRUMENTED, 32); |
| 2283 | |
| 2284 | /* |
| 2285 | Restrict the maximum capacity of the MEM_ROOT so it cannot grow anymore. But |
| 2286 | don't set it to 0, as this means "unlimited". |
| 2287 | */ |
| 2288 | oom_mem_root.set_max_capacity(4); |
| 2289 | |
| 2290 | Histogram *histogram = nullptr; |
| 2291 | |
| 2292 | // Force an equi-height (num_buckets < num_distinct_values) |
| 2293 | histogram = build_histogram(&oom_mem_root, values, 1U, "db1", "tbl1", "col1"); |
| 2294 | EXPECT_EQ(histogram, nullptr); |
| 2295 | |
| 2296 | // Force a singleton (num_buckets >= num_distinct_values) |
| 2297 | histogram = |
| 2298 | build_histogram(&oom_mem_root, values, 10U, "db1", "tbl1", "col1"); |
| 2299 | EXPECT_EQ(histogram, nullptr); |
| 2300 | } |
| 2301 | |
| 2302 | /* |
| 2303 | Check that an out-of-memory situation doesn't crash brutally, but fails |
| 2304 | gracefully. |
| 2305 | */ |
| 2306 | TEST_F(HistogramsTest, EquiHeightOOM) { |
| 2307 | Value_map<longlong> values(&my_charset_numeric, Value_map_type::INT); |
| 2308 | values.add_values(1, 10); |
| 2309 | values.add_values(2, 10); |
| 2310 | values.add_values(3, 10); |
| 2311 | values.add_values(4, 10); |
| 2312 | |
| 2313 | MEM_ROOT oom_mem_root(PSI_NOT_INSTRUMENTED, 128); |
| 2314 | |
| 2315 | { |
| 2316 | /* |
| 2317 | Create the histogram in a new scope so that the underlying structures |
| 2318 | are freed before the MEM_ROOT. |
| 2319 | */ |
| 2320 | Equi_height<longlong> *histogram = Equi_height<longlong>::create( |
| 2321 | &oom_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 2322 | ASSERT_TRUE(histogram != nullptr); |
| 2323 | |
| 2324 | // Restrict the maximum capacity of the MEM_ROOT so it cannot grow anymore. |
| 2325 | oom_mem_root.set_max_capacity(oom_mem_root.allocated_size()); |
| 2326 | EXPECT_TRUE(histogram->build_histogram(values, 10U)); |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | /* |
| 2331 | Check that the Equi_height factory method returns a nullptr if it runs out of |
| 2332 | memory during construction. |
| 2333 | */ |
| 2334 | TEST_F(HistogramsTest, EquiHeightCreationOOM) { |
| 2335 | // Successfully allocate a histogram on a MEM_ROOT. |
| 2336 | MEM_ROOT not_oom_mem_root(PSI_NOT_INSTRUMENTED, 128); |
| 2337 | Equi_height<longlong> *not_oom_histogram = Equi_height<longlong>::create( |
| 2338 | ¬_oom_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 2339 | EXPECT_TRUE(not_oom_histogram != nullptr); |
| 2340 | |
| 2341 | // Create a new MEM_ROOT and fix its capacity. |
| 2342 | MEM_ROOT fixed_capacity_mem_root(PSI_NOT_INSTRUMENTED, 128); |
| 2343 | fixed_capacity_mem_root.set_max_capacity(not_oom_mem_root.allocated_size()); |
| 2344 | |
| 2345 | // Verify that the same allocation does not fail when we fix the capacity. |
| 2346 | Equi_height<longlong> *not_oom_histogram2 = Equi_height<longlong>::create( |
| 2347 | &fixed_capacity_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 2348 | EXPECT_TRUE(not_oom_histogram2 != nullptr); |
| 2349 | |
| 2350 | // Allocate a histogram with long strings leading to an OOM error during |
| 2351 | // construction (not when allocating space for the histogram itself). |
| 2352 | MEM_ROOT fixed_capacity_mem_root2(PSI_NOT_INSTRUMENTED, 128); |
| 2353 | fixed_capacity_mem_root2.set_max_capacity(not_oom_mem_root.allocated_size()); |
| 2354 | std::string long_string(1000, 'x'); // A string of length 1000. |
| 2355 | Equi_height<longlong> *oom_histogram = Equi_height<longlong>::create( |
| 2356 | &fixed_capacity_mem_root2, long_string, long_string, long_string, |
| 2357 | Value_map_type::INT); |
| 2358 | EXPECT_TRUE(oom_histogram == nullptr); |
| 2359 | } |
| 2360 | |
| 2361 | /* |
| 2362 | Check that an out-of-memory situation doesn't crash brutally, but fails |
| 2363 | gracefully. We need to add more than a few buckets to the default-initialized |
| 2364 | vector holding the buckets in order to trigger an allocation. |
| 2365 | */ |
| 2366 | TEST_F(HistogramsTest, SingletonOOM) { |
| 2367 | Value_map<longlong> values(&my_charset_numeric, Value_map_type::INT); |
| 2368 | size_t num_buckets = 100; |
| 2369 | for (longlong i = 0; i < static_cast<longlong>(num_buckets); ++i) { |
| 2370 | values.add_values(i, 10); |
| 2371 | } |
| 2372 | MEM_ROOT oom_mem_root(PSI_NOT_INSTRUMENTED, 128); |
| 2373 | |
| 2374 | { |
| 2375 | /* |
| 2376 | Create the histogram in a new scope so that the underlying structures |
| 2377 | are freed before the MEM_ROOT. |
| 2378 | */ |
| 2379 | Singleton<longlong> *histogram = Singleton<longlong>::create( |
| 2380 | &oom_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 2381 | ASSERT_TRUE(histogram != nullptr); |
| 2382 | |
| 2383 | // Restrict the maximum capacity of the MEM_ROOT so it cannot grow anymore. |
| 2384 | oom_mem_root.set_max_capacity(oom_mem_root.allocated_size()); |
| 2385 | EXPECT_TRUE(histogram->build_histogram(values, num_buckets)); |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | /* |
| 2390 | Check that the Singleton histogram factory method returns a nullptr if it runs |
| 2391 | out of memory during construction. |
| 2392 | */ |
| 2393 | TEST_F(HistogramsTest, SingletonCreationOOM) { |
| 2394 | // Successfully allocate a histogram on a MEM_ROOT. |
| 2395 | MEM_ROOT not_oom_mem_root(PSI_NOT_INSTRUMENTED, 128); |
| 2396 | Singleton<longlong> *not_oom_histogram = Singleton<longlong>::create( |
| 2397 | ¬_oom_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 2398 | EXPECT_TRUE(not_oom_histogram != nullptr); |
| 2399 | |
| 2400 | // Create a new MEM_ROOT and fix its capacity. |
| 2401 | MEM_ROOT fixed_capacity_mem_root(PSI_NOT_INSTRUMENTED, 128); |
| 2402 | fixed_capacity_mem_root.set_max_capacity(not_oom_mem_root.allocated_size()); |
| 2403 | |
| 2404 | // Verify that the same allocation does not fail when we fix the capacity. |
| 2405 | Singleton<longlong> *not_oom_histogram2 = Singleton<longlong>::create( |
| 2406 | &fixed_capacity_mem_root, "db1", "tbl1", "col1", Value_map_type::INT); |
| 2407 | EXPECT_TRUE(not_oom_histogram2 != nullptr); |
| 2408 | |
| 2409 | // Allocate a histogram with long strings leading to an OOM error during |
| 2410 | // construction (not when allocating space for the histogram itself). |
| 2411 | MEM_ROOT fixed_capacity_mem_root2(PSI_NOT_INSTRUMENTED, 128); |
| 2412 | fixed_capacity_mem_root2.set_max_capacity(not_oom_mem_root.allocated_size()); |
| 2413 | std::string long_string(1000, 'x'); // A string of length 1000. |
| 2414 | Singleton<longlong> *oom_histogram = Singleton<longlong>::create( |
| 2415 | &fixed_capacity_mem_root2, long_string, long_string, long_string, |
| 2416 | Value_map_type::INT); |
| 2417 | EXPECT_TRUE(oom_histogram == nullptr); |
| 2418 | } |
| 2419 | |
| 2420 | } // namespace histograms_unittest |