| 1 | /* Copyright (c) 2011, 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 <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <limits.h> |
| 27 | #include <stddef.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | #include <array> |
| 31 | #include <memory> |
| 32 | #include <new> |
| 33 | #include <utility> |
| 34 | |
| 35 | #include "decimal.h" |
| 36 | #include "field_types.h" |
| 37 | #include "lex_string.h" |
| 38 | #include "m_ctype.h" |
| 39 | #include "m_string.h" |
| 40 | #include "my_alloc.h" |
| 41 | #include "my_inttypes.h" |
| 42 | #include "my_macros.h" |
| 43 | #include "my_table_map.h" |
| 44 | #include "mysql_time.h" |
| 45 | #include "mysys_err.h" |
| 46 | #include "sql-common/json_dom.h" |
| 47 | #include "sql/item.h" |
| 48 | #include "sql/item_cmpfunc.h" |
| 49 | #include "sql/item_create.h" |
| 50 | #include "sql/item_strfunc.h" |
| 51 | #include "sql/item_sum.h" |
| 52 | #include "sql/item_timefunc.h" |
| 53 | #include "sql/my_decimal.h" |
| 54 | #include "sql/sql_class.h" |
| 55 | #include "sql/sql_lex.h" |
| 56 | #include "sql/tztime.h" |
| 57 | #include "sql_string.h" |
| 58 | #include "unittest/gunit/fake_table.h" |
| 59 | #include "unittest/gunit/mock_field_long.h" |
| 60 | #include "unittest/gunit/mock_field_timestamp.h" |
| 61 | #include "unittest/gunit/mysys_util.h" |
| 62 | #include "unittest/gunit/test_utils.h" |
| 63 | |
| 64 | namespace item_unittest { |
| 65 | |
| 66 | using my_testing::Mock_error_handler; |
| 67 | using my_testing::Server_initializer; |
| 68 | using std::array; |
| 69 | using std::make_pair; |
| 70 | using std::pair; |
| 71 | using ::testing::Return; |
| 72 | |
| 73 | class ItemTest : public ::testing::Test { |
| 74 | protected: |
| 75 | void SetUp() override { initializer.SetUp(); } |
| 76 | void TearDown() override { initializer.TearDown(); } |
| 77 | |
| 78 | THD *thd() { return initializer.thd(); } |
| 79 | |
| 80 | Server_initializer initializer; |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | This is a simple mock Field class, illustrating how to set expectations on |
| 85 | type_conversion_status Field_long::store(longlong nr, bool unsigned_val); |
| 86 | */ |
| 87 | class Mock_field_long : public Field_long { |
| 88 | public: |
| 89 | Mock_field_long(uint32 length) |
| 90 | : Field_long(nullptr, // ptr_arg |
| 91 | length, // len_arg |
| 92 | nullptr, // null_ptr_arg |
| 93 | 0, // null_bit_arg |
| 94 | Field::NONE, // auto_flags_arg |
| 95 | nullptr, // field_name_arg |
| 96 | false, // zero_arg |
| 97 | false) // unsigned_arg |
| 98 | { |
| 99 | table = new Fake_TABLE(this); |
| 100 | ptr = table->record[0]; |
| 101 | |
| 102 | // Make it possible to write into this field |
| 103 | bitmap_set_bit(table->write_set, 0); |
| 104 | } |
| 105 | |
| 106 | ~Mock_field_long() override { |
| 107 | delete static_cast<Fake_TABLE *>(table); |
| 108 | table = nullptr; |
| 109 | } |
| 110 | |
| 111 | // Avoid warning about hiding other overloaded versions of store(). |
| 112 | using Field_long::store; |
| 113 | |
| 114 | /* |
| 115 | This is the only member function we need to override. |
| 116 | Note: Sun Studio needs a little help in resolving longlong. |
| 117 | */ |
| 118 | MOCK_METHOD2(store, type_conversion_status(::longlong nr, bool unsigned_val)); |
| 119 | }; |
| 120 | |
| 121 | /** |
| 122 | Mock class for CHAR field. |
| 123 | */ |
| 124 | |
| 125 | class Mock_field_string : public Field_string { |
| 126 | private: |
| 127 | Fake_TABLE *m_fake_tbl; |
| 128 | |
| 129 | public: |
| 130 | Mock_field_string(uint32 length, const CHARSET_INFO *cs = &my_charset_latin1) |
| 131 | : Field_string(nullptr, // ptr_arg |
| 132 | length, // len_arg |
| 133 | nullptr, // null_ptr_arg |
| 134 | 0, // null_bit_arg |
| 135 | Field::NONE, // auto_flags_arg |
| 136 | nullptr, // field_name_arg |
| 137 | cs) // char set |
| 138 | { |
| 139 | m_fake_tbl = new Fake_TABLE(this); |
| 140 | |
| 141 | // Allocate place for storing the field value |
| 142 | ptr = m_fake_tbl->record[0]; |
| 143 | |
| 144 | // Make it possible to write into this field |
| 145 | bitmap_set_bit(m_fake_tbl->write_set, 0); |
| 146 | |
| 147 | /* |
| 148 | check_for_truncated_fields must be set in order for producing |
| 149 | warning/error for Item_string::save_in_field(). |
| 150 | */ |
| 151 | m_fake_tbl->in_use->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 152 | } |
| 153 | |
| 154 | ~Mock_field_string() { |
| 155 | delete m_fake_tbl; |
| 156 | m_fake_tbl = nullptr; |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | /** |
| 161 | Mock class for VARCHAR field. |
| 162 | */ |
| 163 | |
| 164 | class Mock_field_varstring : public Field_varstring { |
| 165 | private: |
| 166 | Fake_TABLE *m_fake_tbl; |
| 167 | |
| 168 | public: |
| 169 | Mock_field_varstring(uint32 length, TABLE_SHARE *share, |
| 170 | const CHARSET_INFO *cs = &my_charset_latin1) |
| 171 | : Field_varstring(length, // len_arg |
| 172 | false, // maybe_null_arg |
| 173 | nullptr, // field_name_arg |
| 174 | share, // share |
| 175 | cs) // char set |
| 176 | { |
| 177 | m_fake_tbl = new Fake_TABLE(this); |
| 178 | |
| 179 | // Allocate place for storing the field value |
| 180 | ptr = m_fake_tbl->record[0]; |
| 181 | |
| 182 | // Make it possible to write into this field |
| 183 | bitmap_set_bit(m_fake_tbl->write_set, 0); |
| 184 | |
| 185 | /* |
| 186 | check_for_truncated_fields must be set in order for producing |
| 187 | warning/error for Item_string::save_in_field(). |
| 188 | */ |
| 189 | m_fake_tbl->in_use->check_for_truncated_fields = CHECK_FIELD_WARN; |
| 190 | } |
| 191 | |
| 192 | ~Mock_field_varstring() { |
| 193 | delete m_fake_tbl; |
| 194 | m_fake_tbl = nullptr; |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | TEST_F(ItemTest, ItemInt) { |
| 199 | const int32 val = 42; |
| 200 | char stringbuf[10]; |
| 201 | (void)snprintf(stringbuf, sizeof(stringbuf), "%d", val); |
| 202 | |
| 203 | // An Item expects to be owned by current_thd->free_list, |
| 204 | // so allocate with new, and do not delete it. |
| 205 | Item_int *item_int = new Item_int(val); |
| 206 | |
| 207 | EXPECT_EQ(Item::INT_ITEM, item_int->type()); |
| 208 | EXPECT_EQ(INT_RESULT, item_int->result_type()); |
| 209 | EXPECT_EQ(MYSQL_TYPE_LONGLONG, item_int->data_type()); |
| 210 | EXPECT_EQ(val, item_int->val_int()); |
| 211 | EXPECT_DOUBLE_EQ((double)val, item_int->val_real()); |
| 212 | EXPECT_TRUE(item_int->basic_const_item()); |
| 213 | |
| 214 | my_decimal decimal_val; |
| 215 | EXPECT_EQ(&decimal_val, item_int->val_decimal(&decimal_val)); |
| 216 | |
| 217 | String string_val; |
| 218 | EXPECT_EQ(&string_val, item_int->val_str(&string_val)); |
| 219 | EXPECT_STREQ(stringbuf, string_val.c_ptr_safe()); |
| 220 | |
| 221 | Mock_field_long field_val(item_int->max_length); |
| 222 | // We expect to be called with arguments(nr == val, unsigned_val == false) |
| 223 | EXPECT_CALL(field_val, store(val, false)) |
| 224 | .Times(1) |
| 225 | .WillRepeatedly(Return(TYPE_OK)); |
| 226 | EXPECT_EQ(TYPE_OK, item_int->save_in_field(&field_val, true)); |
| 227 | |
| 228 | Item *clone = item_int->clone_item(); |
| 229 | EXPECT_TRUE(item_int->eq(clone, true)); |
| 230 | EXPECT_TRUE(item_int->eq(item_int, true)); |
| 231 | |
| 232 | String print_val; |
| 233 | item_int->print(thd(), &print_val, QT_ORDINARY); |
| 234 | EXPECT_STREQ(stringbuf, print_val.c_ptr_safe()); |
| 235 | |
| 236 | const uint precision = item_int->decimal_precision(); |
| 237 | EXPECT_EQ(MY_INT32_NUM_DECIMAL_DIGITS, precision); |
| 238 | |
| 239 | item_int->neg(); |
| 240 | EXPECT_EQ(-val, item_int->val_int()); |
| 241 | EXPECT_EQ(precision, item_int->decimal_precision()); |
| 242 | |
| 243 | // Functions inherited from parent class(es). |
| 244 | const table_map tmap = 0; |
| 245 | EXPECT_EQ(tmap, item_int->used_tables()); |
| 246 | |
| 247 | /* |
| 248 | TODO: There are about 100 member functions in Item. |
| 249 | Figure out which ones are relevant for unit testing here. |
| 250 | */ |
| 251 | } |
| 252 | |
| 253 | TEST_F(ItemTest, ItemString) { |
| 254 | const char short_str[] = "abc"; |
| 255 | const char long_str[] = "abcd"; |
| 256 | const char space_str[] = "abc "; |
| 257 | const char bad_char[] = "𝌆abc"; |
| 258 | const char bad_char_end[] = "abc𝌆"; |
| 259 | |
| 260 | Item_string *item_short_string = |
| 261 | new Item_string(STRING_WITH_LEN(short_str), &my_charset_latin1); |
| 262 | Item_string *item_long_string = |
| 263 | new Item_string(STRING_WITH_LEN(long_str), &my_charset_latin1); |
| 264 | Item_string *item_space_string = |
| 265 | new Item_string(STRING_WITH_LEN(space_str), &my_charset_latin1); |
| 266 | Item_string *item_bad_char = |
| 267 | new Item_string(STRING_WITH_LEN(bad_char), &my_charset_bin); |
| 268 | Item_string *item_bad_char_end = |
| 269 | new Item_string(STRING_WITH_LEN(bad_char_end), &my_charset_bin); |
| 270 | |
| 271 | /* |
| 272 | Bug 16407965 ITEM::SAVE_IN_FIELD_NO_WARNING() DOES NOT RETURN CORRECT |
| 273 | CONVERSION STATUS |
| 274 | */ |
| 275 | |
| 276 | // Create a CHAR field that can store short_str but not long_str |
| 277 | Mock_field_string field_string(3); |
| 278 | EXPECT_EQ(MYSQL_TYPE_STRING, field_string.type()); |
| 279 | |
| 280 | // CHAR field for testing strings with illegal values |
| 281 | Mock_field_string field_string_utf8(20, &my_charset_utf8mb3_general_ci); |
| 282 | EXPECT_EQ(MYSQL_TYPE_STRING, field_string_utf8.type()); |
| 283 | |
| 284 | /* |
| 285 | Tests of Item_string::save_in_field() when storing into a CHAR field. |
| 286 | */ |
| 287 | EXPECT_EQ(TYPE_OK, item_short_string->save_in_field(&field_string, true)); |
| 288 | EXPECT_EQ(TYPE_WARN_TRUNCATED, |
| 289 | item_long_string->save_in_field(&field_string, true)); |
| 290 | // Field_string does not consider trailing spaces when truncating a string |
| 291 | EXPECT_EQ(TYPE_OK, item_space_string->save_in_field(&field_string, true)); |
| 292 | // When the first character invalid, the whole string is truncated. |
| 293 | EXPECT_EQ(TYPE_WARN_INVALID_STRING, |
| 294 | item_bad_char->save_in_field(&field_string_utf8, true)); |
| 295 | // If the string contains an invalid character, the entire string is invalid |
| 296 | EXPECT_EQ(TYPE_WARN_INVALID_STRING, |
| 297 | item_bad_char_end->save_in_field(&field_string_utf8, true)); |
| 298 | |
| 299 | /* |
| 300 | Tests of Item_string::save_in_field_no_warnings() when storing into |
| 301 | a CHAR field. |
| 302 | */ |
| 303 | EXPECT_EQ(TYPE_OK, |
| 304 | item_short_string->save_in_field_no_warnings(&field_string, true)); |
| 305 | EXPECT_EQ(TYPE_WARN_TRUNCATED, |
| 306 | item_long_string->save_in_field_no_warnings(&field_string, true)); |
| 307 | // Field_string does not consider trailing spaces when truncating a string |
| 308 | EXPECT_EQ(TYPE_OK, |
| 309 | item_space_string->save_in_field_no_warnings(&field_string, true)); |
| 310 | EXPECT_EQ(TYPE_WARN_INVALID_STRING, |
| 311 | item_bad_char->save_in_field_no_warnings(&field_string_utf8, true)); |
| 312 | // If the string contains an invalid character, the entire string is invalid |
| 313 | EXPECT_EQ( |
| 314 | TYPE_WARN_INVALID_STRING, |
| 315 | item_bad_char_end->save_in_field_no_warnings(&field_string_utf8, true)); |
| 316 | |
| 317 | /* |
| 318 | Create a VARCHAR field that can store short_str but not long_str. |
| 319 | Need a table share object since the constructor for Field_varstring |
| 320 | updates its table share. |
| 321 | */ |
| 322 | TABLE_SHARE table_share; |
| 323 | Mock_field_varstring field_varstring(3, &table_share); |
| 324 | EXPECT_EQ(MYSQL_TYPE_VARCHAR, field_varstring.type()); |
| 325 | |
| 326 | // VARCHAR field for testing strings with illegal values |
| 327 | Mock_field_varstring field_varstring_utf8(20, &table_share, |
| 328 | &my_charset_utf8mb3_general_ci); |
| 329 | EXPECT_EQ(MYSQL_TYPE_VARCHAR, field_varstring_utf8.type()); |
| 330 | |
| 331 | /* |
| 332 | Tests of Item_string::save_in_field() when storing into a VARCHAR field. |
| 333 | */ |
| 334 | EXPECT_EQ(TYPE_OK, item_short_string->save_in_field(&field_varstring, true)); |
| 335 | EXPECT_EQ(TYPE_WARN_TRUNCATED, |
| 336 | item_long_string->save_in_field(&field_varstring, true)); |
| 337 | // Field_varstring produces a note when truncating a string with |
| 338 | // trailing spaces |
| 339 | EXPECT_EQ(TYPE_NOTE_TRUNCATED, |
| 340 | item_space_string->save_in_field(&field_varstring, true)); |
| 341 | // When the first character invalid, the whole string is truncated. |
| 342 | EXPECT_EQ(TYPE_WARN_INVALID_STRING, |
| 343 | item_bad_char->save_in_field(&field_varstring_utf8, true)); |
| 344 | // If the string contains an invalid character, the entire string is invalid |
| 345 | EXPECT_EQ(TYPE_WARN_INVALID_STRING, |
| 346 | item_bad_char_end->save_in_field(&field_varstring_utf8, true)); |
| 347 | |
| 348 | /* |
| 349 | Tests of Item_string::save_in_field_no_warnings() when storing into |
| 350 | a VARCHAR field. |
| 351 | */ |
| 352 | EXPECT_EQ(TYPE_OK, item_short_string->save_in_field_no_warnings( |
| 353 | &field_varstring, true)); |
| 354 | EXPECT_EQ(TYPE_WARN_TRUNCATED, item_long_string->save_in_field_no_warnings( |
| 355 | &field_varstring, true)); |
| 356 | // Field_varstring produces a note when truncating a string with |
| 357 | // trailing spaces |
| 358 | EXPECT_EQ(TYPE_NOTE_TRUNCATED, item_space_string->save_in_field_no_warnings( |
| 359 | &field_varstring, true)); |
| 360 | EXPECT_EQ(TYPE_WARN_INVALID_STRING, item_bad_char->save_in_field_no_warnings( |
| 361 | &field_varstring_utf8, true)); |
| 362 | // If the string contains an invalid character, the entire string is invalid |
| 363 | EXPECT_EQ(TYPE_WARN_INVALID_STRING, |
| 364 | item_bad_char_end->save_in_field_no_warnings(&field_varstring_utf8, |
| 365 | true)); |
| 366 | } |
| 367 | |
| 368 | TEST_F(ItemTest, ItemEqual) { |
| 369 | // Bug#13720201 VALGRIND: VARIOUS BLOCKS OF BYTES DEFINITELY LOST |
| 370 | Mock_field_timestamp mft; |
| 371 | mft.table->const_table = true; |
| 372 | mft.make_readable(); |
| 373 | // foo is longer than STRING_BUFFER_USUAL_SIZE used by cmp_item_sort_string. |
| 374 | const char foo[] = |
| 375 | "0123456789012345678901234567890123456789" |
| 376 | "0123456789012345678901234567890123456789" |
| 377 | "0123456789012345678901234567890123456789"; |
| 378 | Item_equal *item_equal = |
| 379 | new Item_equal(new Item_string(STRING_WITH_LEN(foo), &my_charset_bin), |
| 380 | new Item_field(&mft)); |
| 381 | |
| 382 | EXPECT_FALSE(item_equal->fix_fields(thd(), nullptr)); |
| 383 | EXPECT_EQ(1, item_equal->val_int()); |
| 384 | } |
| 385 | |
| 386 | TEST_F(ItemTest, ItemViewRef) { |
| 387 | Mock_field_long f_field1(true); |
| 388 | Mock_field_long f_field2(true); |
| 389 | Item_field *field1 = new Item_field(&f_field1); |
| 390 | Item_field *field2 = new Item_field(&f_field2); |
| 391 | Item *field3 = new Item_int(123); |
| 392 | |
| 393 | // Create a view reference over a constant expression from an inner |
| 394 | // table of an outer join. |
| 395 | Table_ref table; |
| 396 | table.outer_join = true; |
| 397 | table.set_tableno(2); |
| 398 | Item_view_ref *view_ref = |
| 399 | new Item_view_ref(&thd()->lex->current_query_block()->context, &field3, |
| 400 | nullptr, nullptr, "t1", "f1", &table); |
| 401 | |
| 402 | Item_func_eq *eq1 = new Item_func_eq(field1, field2); |
| 403 | Item_func_eq *eq2 = new Item_func_eq(field1, field3); |
| 404 | Item_func_eq *eq3 = new Item_func_eq(field1, view_ref); |
| 405 | |
| 406 | // True because both arguments are fields. |
| 407 | EXPECT_TRUE(eq1->contains_only_equi_join_condition()); |
| 408 | // False because the right side argument is a constant and |
| 409 | // therefore concluded as a filter. |
| 410 | EXPECT_FALSE(eq2->contains_only_equi_join_condition()); |
| 411 | // Even though the used table information for the right side |
| 412 | // argument says it is not a constant expression, it examines |
| 413 | // the underlying expression in the view reference and concludes |
| 414 | // it as constant expression there by making it a filter and |
| 415 | // not a equi-join condition. |
| 416 | EXPECT_FALSE(eq3->contains_only_equi_join_condition()); |
| 417 | } |
| 418 | |
| 419 | TEST_F(ItemTest, ItemRollupSwitcher) { |
| 420 | Mock_field_long f_field1(true); |
| 421 | Item_field *field1 = new Item_field(&f_field1); |
| 422 | POS p; |
| 423 | Item_sum_sum *agg1 = new Item_sum_sum(p, field1, false, nullptr); |
| 424 | Item_sum_sum *agg2 = new Item_sum_sum(p, field1, false, nullptr); |
| 425 | |
| 426 | List<Item> li1; |
| 427 | li1.push_back(agg1); |
| 428 | Item_rollup_sum_switcher *sw1 = new Item_rollup_sum_switcher(&li1); |
| 429 | List<Item> li2; |
| 430 | li2.push_back(agg2); |
| 431 | Item_rollup_sum_switcher *sw2 = new Item_rollup_sum_switcher(&li2); |
| 432 | EXPECT_TRUE(agg1->eq(agg2, false)); |
| 433 | EXPECT_TRUE(agg1->eq(sw2, false)); |
| 434 | EXPECT_TRUE(sw1->eq(sw2, false)); |
| 435 | EXPECT_TRUE(sw1->eq(agg2, false)); |
| 436 | |
| 437 | EXPECT_FALSE(agg1->is_rollup_sum_wrapper()); |
| 438 | EXPECT_TRUE(sw1->is_rollup_sum_wrapper()); |
| 439 | } |
| 440 | |
| 441 | TEST_F(ItemTest, ItemEqualEq) { |
| 442 | Mock_field_timestamp field1; |
| 443 | field1.field_name = "field1"; |
| 444 | Mock_field_timestamp field2; |
| 445 | field2.field_name = "field2"; |
| 446 | |
| 447 | Item_equal *item_equal1 = |
| 448 | new Item_equal(new Item_field(&field1), new Item_field(&field2)); |
| 449 | Item_equal *item_equal2 = |
| 450 | new Item_equal(new Item_field(&field2), new Item_field(&field1)); |
| 451 | Item_equal *item_equal3 = |
| 452 | new Item_equal(new Item_int(123), new Item_field(&field1)); |
| 453 | Item_equal *item_equal4 = |
| 454 | new Item_equal(new Item_int(123), new Item_field(&field1)); |
| 455 | item_equal4->add(new Item_field(&field2)); |
| 456 | |
| 457 | Item_func_equal *spaceship = |
| 458 | new Item_func_equal(new Item_field(&field1), new Item_field(&field2)); |
| 459 | |
| 460 | // Create an array of <int, Item *> pairs. If the int members of two pairs are |
| 461 | // the same, the Item * members should be considered equal by Item::eq(). (The |
| 462 | // first two objects are equivalent. The rest of them are different.) |
| 463 | array<pair<int, Item *>, 5> items{ |
| 464 | make_pair(1, item_equal1), make_pair(1, item_equal2), |
| 465 | make_pair(2, item_equal3), make_pair(3, item_equal4), |
| 466 | make_pair(4, spaceship), |
| 467 | }; |
| 468 | |
| 469 | // Now compare all items with each other. |
| 470 | for (bool binary_cmp : {true, false}) { |
| 471 | for (pair<int, Item *> it1 : items) { |
| 472 | for (pair<int, Item *> it2 : items) { |
| 473 | EXPECT_EQ(it1.first == it2.first, |
| 474 | it1.second->eq(it2.second, binary_cmp)) |
| 475 | << "Item 1: " << ItemToString(it1.second) |
| 476 | << ", Item 2: " << ItemToString(it2.second) |
| 477 | << ", binary_cmp = " << binary_cmp; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | TEST_F(ItemTest, ItemFuncExportSet) { |
| 484 | String str; |
| 485 | Item *on_string = new Item_string(STRING_WITH_LEN("on"), &my_charset_bin); |
| 486 | Item *off_string = new Item_string(STRING_WITH_LEN("off"), &my_charset_bin); |
| 487 | Item *sep_string = new Item_string(STRING_WITH_LEN(","), &my_charset_bin); |
| 488 | { |
| 489 | // Testing basic functionality. |
| 490 | Item *export_set = |
| 491 | new Item_func_export_set(POS(), new Item_int(2), on_string, off_string, |
| 492 | sep_string, new Item_int(4)); |
| 493 | Parse_context pc(thd(), thd()->lex->current_query_block()); |
| 494 | EXPECT_FALSE(export_set->itemize(&pc, &export_set)); |
| 495 | EXPECT_FALSE(export_set->fix_fields(thd(), nullptr)); |
| 496 | EXPECT_EQ(&str, export_set->val_str(&str)); |
| 497 | EXPECT_STREQ("off,on,off,off", str.c_ptr_safe()); |
| 498 | } |
| 499 | { |
| 500 | // Testing corner case: number_of_bits == zero. |
| 501 | Item *export_set = |
| 502 | new Item_func_export_set(POS(), new Item_int(2), on_string, off_string, |
| 503 | sep_string, new Item_int(0)); |
| 504 | Parse_context pc(thd(), thd()->lex->current_query_block()); |
| 505 | EXPECT_FALSE(export_set->itemize(&pc, &export_set)); |
| 506 | EXPECT_FALSE(export_set->fix_fields(thd(), nullptr)); |
| 507 | EXPECT_EQ(&str, export_set->val_str(&str)); |
| 508 | EXPECT_STREQ("", str.c_ptr_safe()); |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | Bug#11765562 58545: |
| 513 | EXPORT_SET() CAN BE USED TO MAKE ENTIRE SERVER COMPLETELY UNRESPONSIVE |
| 514 | */ |
| 515 | const ulong max_packet_size = 1024; |
| 516 | const ulonglong repeat = max_packet_size / 2; |
| 517 | Item *item_int_repeat = new Item_int(repeat); |
| 518 | Item *string_x = new Item_string(STRING_WITH_LEN("x"), &my_charset_bin); |
| 519 | String *const null_string = nullptr; |
| 520 | thd()->variables.max_allowed_packet = max_packet_size; |
| 521 | { |
| 522 | // Testing overflow caused by 'on-string'. |
| 523 | Mock_error_handler error_handler(thd(), ER_WARN_ALLOWED_PACKET_OVERFLOWED); |
| 524 | Item *export_set = new Item_func_export_set( |
| 525 | POS(), new Item_int(0xff), |
| 526 | new Item_func_repeat(POS(), string_x, item_int_repeat), string_x, |
| 527 | sep_string); |
| 528 | Parse_context pc(thd(), thd()->lex->current_query_block()); |
| 529 | SCOPED_TRACE(""); |
| 530 | EXPECT_FALSE(export_set->itemize(&pc, &export_set)); |
| 531 | EXPECT_FALSE(export_set->fix_fields(thd(), nullptr)); |
| 532 | EXPECT_EQ(null_string, export_set->val_str(&str)); |
| 533 | EXPECT_STREQ("", str.c_ptr_safe()); |
| 534 | EXPECT_EQ(1, error_handler.handle_called()); |
| 535 | } |
| 536 | { |
| 537 | // Testing overflow caused by 'off-string'. |
| 538 | Mock_error_handler error_handler(thd(), ER_WARN_ALLOWED_PACKET_OVERFLOWED); |
| 539 | Item *export_set = new Item_func_export_set( |
| 540 | POS(), new Item_int(0xff), string_x, |
| 541 | new Item_func_repeat(POS(), string_x, item_int_repeat), sep_string); |
| 542 | Parse_context pc(thd(), thd()->lex->current_query_block()); |
| 543 | SCOPED_TRACE(""); |
| 544 | EXPECT_FALSE(export_set->itemize(&pc, &export_set)); |
| 545 | EXPECT_FALSE(export_set->fix_fields(thd(), nullptr)); |
| 546 | EXPECT_EQ(null_string, export_set->val_str(&str)); |
| 547 | EXPECT_STREQ("", str.c_ptr_safe()); |
| 548 | EXPECT_EQ(1, error_handler.handle_called()); |
| 549 | } |
| 550 | { |
| 551 | // Testing overflow caused by 'separator-string'. |
| 552 | Mock_error_handler error_handler(thd(), ER_WARN_ALLOWED_PACKET_OVERFLOWED); |
| 553 | Item *export_set = new Item_func_export_set( |
| 554 | POS(), new Item_int(0xff), string_x, string_x, |
| 555 | new Item_func_repeat(POS(), string_x, item_int_repeat)); |
| 556 | Parse_context pc(thd(), thd()->lex->current_query_block()); |
| 557 | SCOPED_TRACE(""); |
| 558 | EXPECT_FALSE(export_set->itemize(&pc, &export_set)); |
| 559 | EXPECT_FALSE(export_set->fix_fields(thd(), nullptr)); |
| 560 | EXPECT_EQ(null_string, export_set->val_str(&str)); |
| 561 | EXPECT_STREQ("", str.c_ptr_safe()); |
| 562 | EXPECT_EQ(1, error_handler.handle_called()); |
| 563 | } |
| 564 | { |
| 565 | // Testing overflow caused by 'on-string'. |
| 566 | longlong max_size = 1024LL; |
| 567 | thd()->variables.max_allowed_packet = static_cast<ulong>(max_size); |
| 568 | Mock_error_handler error_handler(thd(), ER_WARN_ALLOWED_PACKET_OVERFLOWED); |
| 569 | Item *lpad = new Item_func_lpad( |
| 570 | POS(), new Item_string(STRING_WITH_LEN("a"), &my_charset_bin), |
| 571 | new Item_int(max_size), |
| 572 | new Item_string(STRING_WITH_LEN("pppppppppppppppp" |
| 573 | "pppppppppppppppp"), |
| 574 | &my_charset_bin)); |
| 575 | Item *export_set = new Item_func_export_set( |
| 576 | POS(), new Item_string(STRING_WITH_LEN("1111111"), &my_charset_bin), |
| 577 | lpad, new Item_int(1)); |
| 578 | Parse_context pc(thd(), thd()->lex->current_query_block()); |
| 579 | SCOPED_TRACE(""); |
| 580 | EXPECT_FALSE(export_set->itemize(&pc, &export_set)); |
| 581 | EXPECT_FALSE(export_set->fix_fields(thd(), nullptr)); |
| 582 | EXPECT_EQ(null_string, export_set->val_str(&str)); |
| 583 | EXPECT_STREQ("", str.c_ptr_safe()); |
| 584 | EXPECT_EQ(1, error_handler.handle_called()); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | TEST_F(ItemTest, ItemFuncIntDivOverflow) { |
| 589 | const char dividend_str[] = |
| 590 | "99999999999999999999999999999999999999999" |
| 591 | "99999999999999999999999999999999999999999"; |
| 592 | const char divisor_str[] = "0.5"; |
| 593 | Item_float *dividend = new Item_float(dividend_str, sizeof(dividend_str)); |
| 594 | Item_float *divisor = new Item_float(divisor_str, sizeof(divisor_str)); |
| 595 | Item_func_div_int *quotient = new Item_func_div_int(dividend, divisor); |
| 596 | |
| 597 | Mock_error_handler error_handler(thd(), ER_TRUNCATED_WRONG_VALUE); |
| 598 | EXPECT_FALSE(quotient->fix_fields(thd(), nullptr)); |
| 599 | initializer.set_expected_error(ER_DATA_OUT_OF_RANGE); |
| 600 | quotient->val_int(); |
| 601 | } |
| 602 | |
| 603 | TEST_F(ItemTest, ItemFuncIntDivUnderflow) { |
| 604 | // Bug #11792200 - DIVIDING LARGE NUMBERS CAUSES STACK CORRUPTIONS |
| 605 | const char dividend_str[] = "1.175494351E-37"; |
| 606 | const char divisor_str[] = "1.7976931348623157E+308"; |
| 607 | Item_float *dividend = new Item_float(dividend_str, sizeof(dividend_str)); |
| 608 | Item_float *divisor = new Item_float(divisor_str, sizeof(divisor_str)); |
| 609 | Item_func_div_int *quotient = new Item_func_div_int(dividend, divisor); |
| 610 | |
| 611 | Mock_error_handler error_handler(thd(), ER_TRUNCATED_WRONG_VALUE); |
| 612 | EXPECT_FALSE(quotient->fix_fields(thd(), nullptr)); |
| 613 | EXPECT_EQ(0, quotient->val_int()); |
| 614 | } |
| 615 | |
| 616 | TEST_F(ItemTest, ItemFuncNegLongLongMin) { |
| 617 | // Bug#14314156 MAIN.FUNC_MATH TEST FAILS ON MYSQL-TRUNK ON PB2 |
| 618 | const longlong longlong_min = LLONG_MIN; |
| 619 | Item_func_neg *item_neg = new Item_func_neg(new Item_int(longlong_min)); |
| 620 | |
| 621 | EXPECT_FALSE(item_neg->fix_fields(thd(), nullptr)); |
| 622 | initializer.set_expected_error(ER_DATA_OUT_OF_RANGE); |
| 623 | EXPECT_EQ(0, item_neg->int_op()); |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | This is not an exhaustive test. It simply demonstrates that more of the |
| 628 | initializations in mysqld.cc are needed for testing Item_xxx classes. |
| 629 | */ |
| 630 | TEST_F(ItemTest, ItemFuncSetUserVar) { |
| 631 | const longlong val1 = 1; |
| 632 | Item_decimal *item_dec = new Item_decimal(val1, false); |
| 633 | Item_string *item_str = new Item_string("1", 1, &my_charset_latin1); |
| 634 | |
| 635 | LEX_CSTRING var_name = {STRING_WITH_LEN("a")}; |
| 636 | Item_func_set_user_var *user_var = |
| 637 | new Item_func_set_user_var(var_name, item_str); |
| 638 | EXPECT_FALSE(user_var->set_entry(thd(), true)); |
| 639 | EXPECT_FALSE(user_var->fix_fields(thd(), nullptr)); |
| 640 | EXPECT_EQ(val1, user_var->val_int()); |
| 641 | |
| 642 | my_decimal decimal; |
| 643 | my_decimal *decval_1 = user_var->val_decimal(&decimal); |
| 644 | user_var->save_item_result(item_str); |
| 645 | my_decimal *decval_2 = user_var->val_decimal(&decimal); |
| 646 | user_var->save_item_result(item_dec); |
| 647 | |
| 648 | EXPECT_EQ(decval_1, decval_2); |
| 649 | EXPECT_EQ(decval_1, &decimal); |
| 650 | } |
| 651 | |
| 652 | // Test of Item::operator new() when we simulate out-of-memory. |
| 653 | TEST_F(ItemTest, OutOfMemory) { |
| 654 | Item_int *item = new Item_int(42); |
| 655 | EXPECT_NE(nullptr, item); |
| 656 | |
| 657 | #if !defined(NDEBUG) |
| 658 | // Setting debug flags triggers enter/exit trace, so redirect to /dev/null. |
| 659 | DBUG_SET("o," IF_WIN("NUL", "/dev/null")); |
| 660 | |
| 661 | DBUG_SET("+d,simulate_out_of_memory"); |
| 662 | initializer.set_expected_error(EE_OUTOFMEMORY); |
| 663 | item = new Item_int(42); |
| 664 | EXPECT_EQ(nullptr, item); |
| 665 | |
| 666 | DBUG_SET("+d,simulate_out_of_memory"); |
| 667 | item = new (thd()->mem_root) Item_int(42); |
| 668 | EXPECT_EQ(nullptr, item); |
| 669 | #endif |
| 670 | } |
| 671 | |
| 672 | // We never use dynamic_cast, but we expect it to work. |
| 673 | TEST_F(ItemTest, DynamicCast) { |
| 674 | Item *item = new Item_int(42); |
| 675 | const Item_int *null_item = nullptr; |
| 676 | EXPECT_NE(null_item, dynamic_cast<Item_int *>(item)); |
| 677 | } |
| 678 | |
| 679 | TEST_F(ItemTest, ItemFuncXor) { |
| 680 | const uint length = 1U; |
| 681 | Item_int *item_zero = new Item_int(0, length); |
| 682 | Item_int *item_one_a = new Item_int(1, length); |
| 683 | |
| 684 | Item_func_xor *item_xor = new Item_func_xor(item_zero, item_one_a); |
| 685 | |
| 686 | EXPECT_FALSE(item_xor->fix_fields(thd(), nullptr)); |
| 687 | EXPECT_EQ(1, item_xor->val_int()); |
| 688 | EXPECT_EQ(1U, item_xor->decimal_precision()); |
| 689 | |
| 690 | Item_int *item_one_b = new Item_int(1, length); |
| 691 | |
| 692 | Item_func_xor *item_xor_same = new Item_func_xor(item_one_a, item_one_b); |
| 693 | |
| 694 | EXPECT_FALSE(item_xor_same->fix_fields(thd(), nullptr)); |
| 695 | EXPECT_EQ(0, item_xor_same->val_int()); |
| 696 | EXPECT_FALSE(item_xor_same->val_bool()); |
| 697 | EXPECT_FALSE(item_xor_same->is_null()); |
| 698 | |
| 699 | String print_buffer; |
| 700 | item_xor->print(thd(), &print_buffer, QT_ORDINARY); |
| 701 | EXPECT_STREQ("(0 xor 1)", print_buffer.c_ptr_safe()); |
| 702 | |
| 703 | Item *neg_xor = item_xor->truth_transformer(thd(), Item::BOOL_NEGATED); |
| 704 | EXPECT_FALSE(neg_xor->fix_fields(thd(), nullptr)); |
| 705 | EXPECT_EQ(0, neg_xor->val_int()); |
| 706 | EXPECT_DOUBLE_EQ(0.0, neg_xor->val_real()); |
| 707 | EXPECT_FALSE(neg_xor->val_bool()); |
| 708 | EXPECT_FALSE(neg_xor->is_null()); |
| 709 | |
| 710 | print_buffer = String(); |
| 711 | neg_xor->print(thd(), &print_buffer, QT_ORDINARY); |
| 712 | EXPECT_STREQ("((not(0)) xor 1)", print_buffer.c_ptr_safe()); |
| 713 | |
| 714 | Item_func_xor *item_xor_null = new Item_func_xor(item_zero, new Item_null()); |
| 715 | EXPECT_FALSE(item_xor_null->fix_fields(thd(), nullptr)); |
| 716 | |
| 717 | EXPECT_EQ(0, item_xor_null->val_int()); |
| 718 | EXPECT_TRUE(item_xor_null->is_null()); |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | Testing MYSQL_TIME_cache. |
| 723 | */ |
| 724 | TEST_F(ItemTest, MysqlTimeCache) { |
| 725 | String str_buff, *str; |
| 726 | MysqlTime datetime6(2011, 11, 7, 10, 20, 30, 123456, false, |
| 727 | MYSQL_TIMESTAMP_DATETIME); |
| 728 | MysqlTime time6(0, 0, 0, 10, 20, 30, 123456, false, MYSQL_TIMESTAMP_TIME); |
| 729 | my_timeval tv6 = {1320661230, 123456}; |
| 730 | const MYSQL_TIME *ltime; |
| 731 | MYSQL_TIME_cache cache; |
| 732 | |
| 733 | /* |
| 734 | Testing DATETIME(6). |
| 735 | Initializing from MYSQL_TIME. |
| 736 | */ |
| 737 | cache.set_datetime(&datetime6, 6); |
| 738 | EXPECT_EQ(1840440237558456896LL, cache.val_packed()); |
| 739 | EXPECT_EQ(6, cache.decimals()); |
| 740 | // Call val_str() then cptr() |
| 741 | str = cache.val_str(&str_buff); |
| 742 | EXPECT_STREQ("2011-11-07 10:20:30.123456", str->c_ptr_safe()); |
| 743 | EXPECT_STREQ("2011-11-07 10:20:30.123456", cache.cptr()); |
| 744 | cache.set_datetime(&datetime6, 6); |
| 745 | // Now call the other way around: cptr() then val_str() |
| 746 | EXPECT_STREQ("2011-11-07 10:20:30.123456", cache.cptr()); |
| 747 | EXPECT_STREQ("2011-11-07 10:20:30.123456", str->c_ptr_safe()); |
| 748 | // Testing get_TIME_ptr() |
| 749 | ltime = cache.get_TIME_ptr(); |
| 750 | EXPECT_EQ(ltime->year, datetime6.year); |
| 751 | EXPECT_EQ(ltime->month, datetime6.month); |
| 752 | EXPECT_EQ(ltime->day, datetime6.day); |
| 753 | EXPECT_EQ(ltime->hour, datetime6.hour); |
| 754 | EXPECT_EQ(ltime->minute, datetime6.minute); |
| 755 | EXPECT_EQ(ltime->second, datetime6.second); |
| 756 | EXPECT_EQ(ltime->second_part, datetime6.second_part); |
| 757 | EXPECT_EQ(ltime->neg, datetime6.neg); |
| 758 | EXPECT_EQ(ltime->time_type, datetime6.time_type); |
| 759 | // Testing eq() |
| 760 | { |
| 761 | MYSQL_TIME datetime6_2 = datetime6; |
| 762 | MYSQL_TIME_cache cache2; |
| 763 | datetime6_2.second_part += 1; |
| 764 | cache2.set_datetime(&datetime6_2, 6); |
| 765 | EXPECT_EQ(cache.eq(cache), true); |
| 766 | EXPECT_EQ(cache.eq(cache2), false); |
| 767 | EXPECT_EQ(cache2.eq(cache2), true); |
| 768 | EXPECT_EQ(cache2.eq(cache), false); |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | Testing DATETIME(6). |
| 773 | Initializing from "struct timeval". |
| 774 | */ |
| 775 | cache.set_datetime(tv6, 6, my_tz_UTC); |
| 776 | EXPECT_EQ(1840440237558456896LL, cache.val_packed()); |
| 777 | EXPECT_EQ(6, cache.decimals()); |
| 778 | str = cache.val_str(&str_buff); |
| 779 | EXPECT_STREQ("2011-11-07 10:20:30.123456", str->c_ptr_safe()); |
| 780 | EXPECT_STREQ("2011-11-07 10:20:30.123456", cache.cptr()); |
| 781 | |
| 782 | /* |
| 783 | Testing TIME(6). |
| 784 | Initializing from MYSQL_TIME. |
| 785 | */ |
| 786 | cache.set_time(&time6, 6); |
| 787 | EXPECT_EQ(709173043776LL, cache.val_packed()); |
| 788 | EXPECT_EQ(6, cache.decimals()); |
| 789 | // Call val_str() then cptr() |
| 790 | str = cache.val_str(&str_buff); |
| 791 | EXPECT_STREQ("10:20:30.123456", str->c_ptr_safe()); |
| 792 | EXPECT_STREQ("10:20:30.123456", cache.cptr()); |
| 793 | |
| 794 | /* |
| 795 | Testing TIME(6). |
| 796 | Initializing from "struct timeval". |
| 797 | */ |
| 798 | cache.set_time(tv6, 6, my_tz_UTC); |
| 799 | EXPECT_EQ(709173043776LL, cache.val_packed()); |
| 800 | EXPECT_EQ(6, cache.decimals()); |
| 801 | str = cache.val_str(&str_buff); |
| 802 | EXPECT_STREQ("10:20:30.123456", str->c_ptr_safe()); |
| 803 | EXPECT_STREQ("10:20:30.123456", cache.cptr()); |
| 804 | |
| 805 | /* |
| 806 | Testing DATETIME(5) |
| 807 | */ |
| 808 | MysqlTime datetime5 = { |
| 809 | 2011, 11, 7, 10, 20, 30, 123450, false, MYSQL_TIMESTAMP_DATETIME}; |
| 810 | cache.set_datetime(&datetime5, 5); |
| 811 | EXPECT_EQ(1840440237558456890LL, cache.val_packed()); |
| 812 | EXPECT_EQ(5, cache.decimals()); |
| 813 | /* Call val_str() then cptr() */ |
| 814 | str = cache.val_str(&str_buff); |
| 815 | EXPECT_STREQ("2011-11-07 10:20:30.12345", str->c_ptr_safe()); |
| 816 | EXPECT_STREQ("2011-11-07 10:20:30.12345", cache.cptr()); |
| 817 | cache.set_datetime(&datetime5, 5); |
| 818 | /* Now call the other way around: cptr() then val_str() */ |
| 819 | EXPECT_STREQ("2011-11-07 10:20:30.12345", cache.cptr()); |
| 820 | EXPECT_STREQ("2011-11-07 10:20:30.12345", str->c_ptr_safe()); |
| 821 | |
| 822 | /* |
| 823 | Testing DATE. |
| 824 | Initializing from MYSQL_TIME. |
| 825 | */ |
| 826 | MysqlTime date(2011, 11, 7, 0, 0, 0, 0, false, MYSQL_TIMESTAMP_DATE); |
| 827 | cache.set_date(&date); |
| 828 | EXPECT_EQ(1840439528385413120LL, cache.val_packed()); |
| 829 | EXPECT_EQ(0, cache.decimals()); |
| 830 | str = cache.val_str(&str_buff); |
| 831 | EXPECT_STREQ("2011-11-07", str->c_ptr_safe()); |
| 832 | EXPECT_STREQ("2011-11-07", cache.cptr()); |
| 833 | |
| 834 | /* |
| 835 | Testing DATE. |
| 836 | Initializing from "struct tm". |
| 837 | */ |
| 838 | cache.set_date(tv6, my_tz_UTC); |
| 839 | EXPECT_EQ(1840439528385413120LL, cache.val_packed()); |
| 840 | EXPECT_EQ(0, cache.decimals()); |
| 841 | str = cache.val_str(&str_buff); |
| 842 | EXPECT_STREQ("2011-11-07", str->c_ptr_safe()); |
| 843 | EXPECT_STREQ("2011-11-07", cache.cptr()); |
| 844 | } |
| 845 | |
| 846 | extern "C" { |
| 847 | // Verifies that Item_func_conv::val_str does not call my_strntoll() |
| 848 | longlong fail_strntoll(const CHARSET_INFO *, const char *, size_t, int, |
| 849 | const char **, int *) { |
| 850 | ADD_FAILURE() << "Unexpected call"; |
| 851 | return 0; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | class Mock_charset : public CHARSET_INFO { |
| 856 | public: |
| 857 | Mock_charset(const CHARSET_INFO &csi) { |
| 858 | CHARSET_INFO *this_as_cset = this; |
| 859 | *this_as_cset = csi; |
| 860 | |
| 861 | number = 666; |
| 862 | m_cset_handler = *(csi.cset); |
| 863 | m_cset_handler.strntoll = fail_strntoll; |
| 864 | cset = &m_cset_handler; |
| 865 | } |
| 866 | |
| 867 | private: |
| 868 | MY_CHARSET_HANDLER m_cset_handler; |
| 869 | }; |
| 870 | |
| 871 | TEST_F(ItemTest, ItemFuncConvIntMin) { |
| 872 | Mock_charset charset(*system_charset_info); |
| 873 | SCOPED_TRACE(""); |
| 874 | Item *item_conv = new Item_func_conv(POS(), new Item_string("5", 1, &charset), |
| 875 | new Item_int(INT_MIN), // from_base |
| 876 | new Item_int(INT_MIN)); // to_base |
| 877 | Parse_context pc(thd(), thd()->lex->current_query_block()); |
| 878 | EXPECT_FALSE(item_conv->itemize(&pc, &item_conv)); |
| 879 | EXPECT_FALSE(item_conv->fix_fields(thd(), nullptr)); |
| 880 | const String *null_string = nullptr; |
| 881 | String str; |
| 882 | EXPECT_EQ(null_string, item_conv->val_str(&str)); |
| 883 | } |
| 884 | |
| 885 | TEST_F(ItemTest, ItemDecimalTypecast) { |
| 886 | const char msg[] = ""; |
| 887 | POS pos; |
| 888 | pos.cpp.start = pos.cpp.end = pos.raw.start = pos.raw.end = msg; |
| 889 | |
| 890 | Cast_type type; |
| 891 | type.target = ITEM_CAST_DECIMAL; |
| 892 | type.length = "123456789012345678901234567890"; |
| 893 | type.dec = nullptr; |
| 894 | |
| 895 | { |
| 896 | initializer.set_expected_error(ER_TOO_BIG_PRECISION); |
| 897 | EXPECT_EQ(nullptr, create_func_cast(thd(), pos, nullptr, type, false)); |
| 898 | } |
| 899 | |
| 900 | { |
| 901 | char buff[20]; |
| 902 | snprintf(buff, sizeof(buff) - 1, "%d", DECIMAL_MAX_PRECISION + 1); |
| 903 | type.length = buff; |
| 904 | type.dec = nullptr; |
| 905 | initializer.set_expected_error(ER_TOO_BIG_PRECISION); |
| 906 | EXPECT_EQ(nullptr, create_func_cast(thd(), pos, nullptr, type, false)); |
| 907 | } |
| 908 | |
| 909 | { |
| 910 | type.length = nullptr; |
| 911 | type.dec = "123456789012345678901234567890"; |
| 912 | initializer.set_expected_error(ER_TOO_BIG_SCALE); |
| 913 | EXPECT_EQ(nullptr, create_func_cast(thd(), pos, nullptr, type, false)); |
| 914 | } |
| 915 | |
| 916 | { |
| 917 | char buff[20]; |
| 918 | snprintf(buff, sizeof(buff) - 1, "%d", DECIMAL_MAX_SCALE + 1); |
| 919 | type.length = buff; |
| 920 | type.dec = buff; |
| 921 | initializer.set_expected_error(ER_TOO_BIG_SCALE); |
| 922 | EXPECT_EQ(nullptr, create_func_cast(thd(), pos, nullptr, type, false)); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | TEST_F(ItemTest, NormalizedPrint) { |
| 927 | Item_null *item_null = new Item_null; |
| 928 | { |
| 929 | String s; |
| 930 | item_null->print(thd(), &s, QT_ORDINARY); |
| 931 | EXPECT_STREQ("NULL", s.c_ptr()); |
| 932 | } |
| 933 | { |
| 934 | String s; |
| 935 | item_null->print(thd(), &s, QT_NORMALIZED_FORMAT); |
| 936 | EXPECT_STREQ("?", s.c_ptr()); |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | TEST_F(ItemTest, CompareEmptyStrings) { |
| 941 | Item *item1 = new Item_string(nullptr, 0, &my_charset_bin); |
| 942 | Item *item2 = new Item_string(nullptr, 0, &my_charset_bin); |
| 943 | Item_result_field *owner = new Item_func_le(item1, item2); |
| 944 | EXPECT_FALSE(item1->fix_fields(thd(), nullptr)); |
| 945 | EXPECT_FALSE(item2->fix_fields(thd(), nullptr)); |
| 946 | |
| 947 | Arg_comparator comparator(&item1, &item2); |
| 948 | comparator.set_cmp_func(owner, &item1, &item2, false); |
| 949 | |
| 950 | EXPECT_EQ(0, comparator.compare_binary_string()); |
| 951 | } |
| 952 | |
| 953 | TEST_F(ItemTest, ItemJson) { |
| 954 | MEM_ROOT *const mem_root = initializer.thd()->mem_root; |
| 955 | |
| 956 | const Item_name_string name(Name_string(STRING_WITH_LEN("json"))); |
| 957 | |
| 958 | Json_string jstr("123"); |
| 959 | Item_json *item = new Item_json( |
| 960 | make_unique_destroy_only<Json_wrapper>(mem_root, &jstr, true), name); |
| 961 | |
| 962 | Json_wrapper wr; |
| 963 | EXPECT_FALSE(item->val_json(&wr)); |
| 964 | EXPECT_EQ(&jstr, wr.get_dom()); |
| 965 | |
| 966 | String string_buffer; |
| 967 | const String *str = item->val_str(&string_buffer); |
| 968 | EXPECT_EQ("\"123\"", to_string(*str)); |
| 969 | EXPECT_EQ(item->collation.collation, str->charset()); |
| 970 | |
| 971 | EXPECT_EQ(123.0, item->val_real()); |
| 972 | EXPECT_EQ(123, item->val_int()); |
| 973 | |
| 974 | my_decimal decimal_buffer; |
| 975 | const my_decimal *decimal = item->val_decimal(&decimal_buffer); |
| 976 | double dbl = 0; |
| 977 | EXPECT_EQ(E_DEC_OK, decimal2double(decimal, &dbl)); |
| 978 | EXPECT_EQ(123.0, dbl); |
| 979 | |
| 980 | Item *clone = item->clone_item(); |
| 981 | EXPECT_NE(item, clone); |
| 982 | EXPECT_TRUE(item->eq(clone, true)); |
| 983 | EXPECT_FALSE(clone->val_json(&wr)); |
| 984 | EXPECT_NE(&jstr, wr.get_dom()); |
| 985 | EXPECT_EQ(0, wr.compare(Json_wrapper(&jstr, true))); |
| 986 | EXPECT_EQ(123, clone->val_int()); |
| 987 | |
| 988 | const MysqlTime date(2020, 1, 2); |
| 989 | EXPECT_EQ(MYSQL_TIMESTAMP_DATE, date.time_type); |
| 990 | item = new Item_json( |
| 991 | make_unique_destroy_only<Json_wrapper>( |
| 992 | mem_root, std::unique_ptr<Json_dom>(new (std::nothrow) Json_datetime( |
| 993 | date, MYSQL_TYPE_DATE))), |
| 994 | name); |
| 995 | MYSQL_TIME time_result; |
| 996 | EXPECT_FALSE(item->get_date(&time_result, 0)); |
| 997 | EXPECT_EQ(date.time_type, time_result.time_type); |
| 998 | EXPECT_EQ(date.year, time_result.year); |
| 999 | EXPECT_EQ(date.month, time_result.month); |
| 1000 | EXPECT_EQ(date.day, time_result.day); |
| 1001 | |
| 1002 | const MysqlTime time(0, 0, 0, 10, 20, 30, 40, false, MYSQL_TIMESTAMP_TIME); |
| 1003 | item = new Item_json( |
| 1004 | make_unique_destroy_only<Json_wrapper>( |
| 1005 | mem_root, std::unique_ptr<Json_dom>(new (std::nothrow) Json_datetime( |
| 1006 | time, MYSQL_TYPE_TIME))), |
| 1007 | name); |
| 1008 | EXPECT_FALSE(item->get_time(&time_result)); |
| 1009 | EXPECT_EQ(time.time_type, time_result.time_type); |
| 1010 | EXPECT_EQ(time.hour, time_result.hour); |
| 1011 | EXPECT_EQ(time.minute, time_result.minute); |
| 1012 | EXPECT_EQ(time.second, time_result.second); |
| 1013 | EXPECT_EQ(time.second_part, time_result.second_part); |
| 1014 | EXPECT_EQ(time.neg, time_result.neg); |
| 1015 | } |
| 1016 | |
| 1017 | } // namespace item_unittest |