| 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 <gtest/gtest.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <initializer_list> |
| 29 | #include <limits> |
| 30 | #include <sstream> |
| 31 | #include <string> |
| 32 | |
| 33 | #include "decimal.h" |
| 34 | #include "m_ctype.h" |
| 35 | #include "m_string.h" |
| 36 | #include "my_time.h" |
| 37 | #include "mysql_time.h" |
| 38 | #include "sql/item.h" |
| 39 | #include "sql/item_timefunc.h" |
| 40 | #include "sql/my_decimal.h" |
| 41 | #include "sql/parse_location.h" |
| 42 | #include "sql/parse_tree_node_base.h" |
| 43 | #include "sql/sql_class.h" |
| 44 | #include "sql/sql_const.h" |
| 45 | #include "sql/sql_lex.h" |
| 46 | #include "sql/system_variables.h" |
| 47 | #include "sql_string.h" |
| 48 | #include "unittest/gunit/test_utils.h" |
| 49 | |
| 50 | namespace item_timefunc_unittest { |
| 51 | |
| 52 | using my_testing::Server_initializer; |
| 53 | |
| 54 | class ItemTimeFuncTest : public ::testing::Test { |
| 55 | protected: |
| 56 | void SetUp() override { initializer.SetUp(); } |
| 57 | |
| 58 | void TearDown() override { initializer.TearDown(); } |
| 59 | |
| 60 | THD *thd() { return initializer.thd(); } |
| 61 | |
| 62 | Server_initializer initializer; |
| 63 | }; |
| 64 | |
| 65 | TEST_F(ItemTimeFuncTest, dateAddInterval) { |
| 66 | Item_int *arg0 = new Item_int(20130122145221LL); // 2013-01-22 14:52:21 |
| 67 | Item_decimal *arg1 = new Item_decimal(0.1234567); |
| 68 | Item *item = new Item_date_add_interval(POS(), arg0, arg1, |
| 69 | INTERVAL_SECOND_MICROSECOND, false); |
| 70 | Parse_context pc(thd(), thd()->lex->current_query_block()); |
| 71 | EXPECT_FALSE(item->itemize(&pc, &item)); |
| 72 | EXPECT_FALSE(item->fix_fields(thd(), nullptr)); |
| 73 | |
| 74 | // The below result is not correct, see Bug#16198372 |
| 75 | EXPECT_DOUBLE_EQ(20130122145222.234567, item->val_real()); |
| 76 | } |
| 77 | |
| 78 | // Checks that the metadata and result are consistent for a time function that |
| 79 | // returns an integer. |
| 80 | static void CheckMetadataConsistency(THD *thd, Item *item) { |
| 81 | SCOPED_TRACE(ItemToString(item)); |
| 82 | |
| 83 | Item *ref[] = {item}; |
| 84 | ASSERT_FALSE(item->fix_fields(thd, ref)); |
| 85 | ASSERT_EQ(item, ref[0]); |
| 86 | |
| 87 | // Expect a signed integer return type. |
| 88 | EXPECT_FALSE(item->unsigned_flag); |
| 89 | EXPECT_EQ(0, item->decimals); |
| 90 | |
| 91 | const int64_t int_result = item->val_int(); |
| 92 | if (item->null_value) { |
| 93 | EXPECT_TRUE(item->is_nullable()); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | // int result and string result should match. |
| 98 | const std::string expected_string_result = std::to_string(int_result); |
| 99 | StringBuffer<STRING_BUFFER_USUAL_SIZE> buffer; |
| 100 | EXPECT_EQ(expected_string_result, to_string(*item->val_str(&buffer))); |
| 101 | |
| 102 | // Check that the metadata matches what was actually returned. |
| 103 | const size_t result_char_length = expected_string_result.size(); |
| 104 | const size_t digits = result_char_length - (int_result < 0 ? 1 : 0); |
| 105 | EXPECT_LE(result_char_length, item->max_char_length()); |
| 106 | EXPECT_LE(digits, item->decimal_precision()); |
| 107 | EXPECT_LE(digits, item->decimal_int_part()); |
| 108 | } |
| 109 | |
| 110 | // Checks that the metadata and result are consistent for a time function that |
| 111 | // returns an integer. Additionally, check that the expected result is returned. |
| 112 | static void CheckMetadataAndResult(THD *thd, Item *item, |
| 113 | int64_t expected_result) { |
| 114 | SCOPED_TRACE(ItemToString(item)); |
| 115 | CheckMetadataConsistency(thd, item); |
| 116 | EXPECT_EQ(expected_result, item->val_int()); |
| 117 | EXPECT_FALSE(item->null_value); |
| 118 | } |
| 119 | |
| 120 | // Verifies that the results returned by the PERIOD_ADD function are consistent |
| 121 | // with the metadata. |
| 122 | TEST_F(ItemTimeFuncTest, PeriodAddMetadata) { |
| 123 | // PERIOD_ADD returns values on the form YYYYMM, but it's not limited to |
| 124 | // four-digit year. |
| 125 | CheckMetadataAndResult( |
| 126 | thd(), |
| 127 | new Item_func_period_add(POS(), new Item_int(999912), new Item_int(1)), |
| 128 | 1000001); |
| 129 | |
| 130 | // Maximum return value. |
| 131 | CheckMetadataAndResult( |
| 132 | thd(), |
| 133 | new Item_func_period_add(POS(), new Item_int(9223372036854775806LL), |
| 134 | new Item_int(1)), |
| 135 | std::numeric_limits<int64_t>::max()); |
| 136 | |
| 137 | // Overflow makes the result wrap around. |
| 138 | CheckMetadataAndResult( |
| 139 | thd(), |
| 140 | new Item_func_period_add(POS(), new Item_int(9223372036854775806LL), |
| 141 | new Item_int(2)), |
| 142 | std::numeric_limits<int64_t>::min()); |
| 143 | } |
| 144 | |
| 145 | // Verifies that the results returned by the PERIOD_DIFF function are consistent |
| 146 | // with the metadata. |
| 147 | TEST_F(ItemTimeFuncTest, PeriodDiffMetadata) { |
| 148 | CheckMetadataAndResult(thd(), |
| 149 | new Item_func_period_diff(POS(), new Item_int(202101), |
| 150 | new Item_int(202101)), |
| 151 | 0); |
| 152 | |
| 153 | CheckMetadataAndResult(thd(), |
| 154 | new Item_func_period_diff(POS(), new Item_int(202101), |
| 155 | new Item_int(201912)), |
| 156 | 13); |
| 157 | |
| 158 | CheckMetadataAndResult(thd(), |
| 159 | new Item_func_period_diff(POS(), new Item_int(201912), |
| 160 | new Item_int(202101)), |
| 161 | -13); |
| 162 | |
| 163 | CheckMetadataAndResult( |
| 164 | thd(), |
| 165 | new Item_func_period_diff(POS(), new Item_int(9223372036854775807LL), |
| 166 | new Item_int(1)), |
| 167 | 1106804644422549102LL); |
| 168 | |
| 169 | CheckMetadataAndResult( |
| 170 | thd(), |
| 171 | new Item_func_period_diff(POS(), new Item_int(1), |
| 172 | new Item_int(9223372036854775807LL)), |
| 173 | -1106804644422549102LL); |
| 174 | } |
| 175 | |
| 176 | // Verifies that the results returned by the TO_DAYS function are consistent |
| 177 | // with the metadata. |
| 178 | TEST_F(ItemTimeFuncTest, ToDaysMetadata) { |
| 179 | auto arg = new Item_string(STRING_WITH_LEN("9999-12-31"), |
| 180 | &my_charset_utf8mb4_0900_ai_ci); |
| 181 | auto to_days = new Item_func_to_days(POS(), arg); |
| 182 | CheckMetadataAndResult(thd(), to_days, 3652424); |
| 183 | } |
| 184 | |
| 185 | // Verifies that the results returned by the TO_SECONDS function are consistent |
| 186 | // with the metadata. |
| 187 | TEST_F(ItemTimeFuncTest, ToSecondsMetadata) { |
| 188 | auto arg = new Item_string(STRING_WITH_LEN("9999-12-31 23:59:59"), |
| 189 | &my_charset_utf8mb4_0900_ai_ci); |
| 190 | auto to_seconds = new Item_func_to_seconds(POS(), arg); |
| 191 | CheckMetadataAndResult(thd(), to_seconds, 315569519999); |
| 192 | } |
| 193 | |
| 194 | // Verifies that the results returned by the DAYOFMONTH function are consistent |
| 195 | // with the metadata. |
| 196 | TEST_F(ItemTimeFuncTest, DayOfMonthMetadata) { |
| 197 | for (int i = 20210100; i <= 20210131; ++i) { |
| 198 | CheckMetadataAndResult( |
| 199 | thd(), new Item_func_dayofmonth(POS(), new Item_int(i)), i % 100); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Verifies that the results returned by the MONTH function are consistent with |
| 204 | // the metadata. |
| 205 | TEST_F(ItemTimeFuncTest, MonthMetadata) { |
| 206 | for (int month = 1; month <= 12; ++month) { |
| 207 | CheckMetadataAndResult( |
| 208 | thd(), new Item_func_month(POS(), new Item_int(20210001 + month * 100)), |
| 209 | month); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Verifies that the results returned by the DAYOFYEAR function are consistent |
| 214 | // with the metadata. |
| 215 | TEST_F(ItemTimeFuncTest, DayOfYearMetadata) { |
| 216 | CheckMetadataAndResult( |
| 217 | thd(), new Item_func_dayofyear(POS(), new Item_int(20200101)), 1); |
| 218 | |
| 219 | CheckMetadataAndResult( |
| 220 | thd(), new Item_func_dayofyear(POS(), new Item_int(20201231)), 366); |
| 221 | } |
| 222 | |
| 223 | // Verifies that the results returned by the HOUR function are consistent |
| 224 | // with the metadata. |
| 225 | TEST_F(ItemTimeFuncTest, HourMetadata) { |
| 226 | static_assert(TIME_MAX_HOUR == 838, |
| 227 | "TIME_MAX_HOUR has changed. Update the test case to test the " |
| 228 | "new maximum value."); |
| 229 | CheckMetadataAndResult(thd(), |
| 230 | new Item_func_hour(POS(), new Item_int(8380000)), 838); |
| 231 | |
| 232 | CheckMetadataAndResult( |
| 233 | thd(), new Item_func_hour(POS(), new Item_int(-8380000)), 838); |
| 234 | } |
| 235 | |
| 236 | // Verifies that the results returned by the MINUTE function are consistent |
| 237 | // with the metadata. |
| 238 | TEST_F(ItemTimeFuncTest, MinuteMetadata) { |
| 239 | for (int i = 0; i < 60; ++i) { |
| 240 | CheckMetadataAndResult( |
| 241 | thd(), new Item_func_minute(POS(), new Item_int(i * 100)), i); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Verifies that the results returned by the QUARTER function are consistent |
| 246 | // with the metadata. |
| 247 | TEST_F(ItemTimeFuncTest, QuarterMetadata) { |
| 248 | for (int month = 1; month <= 12; ++month) { |
| 249 | CheckMetadataAndResult( |
| 250 | thd(), |
| 251 | new Item_func_quarter(POS(), new Item_int(20000001 + month * 100)), |
| 252 | (month + 2) / 3); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // Verifies that the results returned by the SECOND function are consistent |
| 257 | // with the metadata. |
| 258 | TEST_F(ItemTimeFuncTest, SecondMetadata) { |
| 259 | for (int i = 0; i < 60; ++i) { |
| 260 | CheckMetadataAndResult(thd(), new Item_func_second(POS(), new Item_int(i)), |
| 261 | i); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Verifies that the results returned by the WEEK function are consistent |
| 266 | // with the metadata. |
| 267 | TEST_F(ItemTimeFuncTest, WeekMetadata) { |
| 268 | auto mode = new Item_int(int{WEEK_MONDAY_FIRST | WEEK_YEAR}); |
| 269 | |
| 270 | CheckMetadataAndResult( |
| 271 | thd(), new Item_func_week(POS(), new Item_int(20200101), mode), 1); |
| 272 | |
| 273 | CheckMetadataAndResult( |
| 274 | thd(), new Item_func_week(POS(), new Item_int(20210101), mode), 53); |
| 275 | } |
| 276 | |
| 277 | // Verifies that the results returned by the YEARWEAK function are consistent |
| 278 | // with the metadata. |
| 279 | TEST_F(ItemTimeFuncTest, YearWeekMetadata) { |
| 280 | auto mode = new Item_int(int{WEEK_MONDAY_FIRST | WEEK_YEAR}); |
| 281 | |
| 282 | CheckMetadataAndResult( |
| 283 | thd(), new Item_func_yearweek(POS(), new Item_int(20200101), mode), |
| 284 | 202001); |
| 285 | |
| 286 | CheckMetadataAndResult( |
| 287 | thd(), new Item_func_yearweek(POS(), new Item_int(20210101), mode), |
| 288 | 202053); |
| 289 | } |
| 290 | |
| 291 | // Verifies that the results returned by the YEAR function are consistent |
| 292 | // with the metadata. |
| 293 | TEST_F(ItemTimeFuncTest, YearMetadata) { |
| 294 | CheckMetadataAndResult( |
| 295 | thd(), new Item_func_year(POS(), new Item_int(20201231)), 2020); |
| 296 | |
| 297 | CheckMetadataAndResult( |
| 298 | thd(), new Item_func_year(POS(), new Item_int(20210101)), 2021); |
| 299 | } |
| 300 | |
| 301 | // Verifies that the results returned by the CAST(... AS YEAR) are consistent |
| 302 | // with the metadata. |
| 303 | TEST_F(ItemTimeFuncTest, CastAsYearMetadata) { |
| 304 | CheckMetadataAndResult( |
| 305 | thd(), new Item_typecast_year(POS(), new Item_int(2021)), 2021); |
| 306 | |
| 307 | CheckMetadataAndResult( |
| 308 | thd(), new Item_typecast_year(POS(), new Item_int(2155)), 2155); |
| 309 | } |
| 310 | |
| 311 | // Verifies that the results returned by the WEEKDAY function are consistent |
| 312 | // with the metadata. |
| 313 | TEST_F(ItemTimeFuncTest, WeekdayMetadata) { |
| 314 | for (int i = 0; i < 7; i++) { |
| 315 | // WEEKDAY returns 0 for Monday and 6 for Sunday. |
| 316 | CheckMetadataAndResult( |
| 317 | thd(), new Item_func_weekday(POS(), new Item_int(20210104 + i), false), |
| 318 | i); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // Verifies that the results returned by the DAYOFWEEK function are consistent |
| 323 | // with the metadata. |
| 324 | TEST_F(ItemTimeFuncTest, DayOfWeekMetadata) { |
| 325 | for (int i = 0; i < 7; i++) { |
| 326 | // DAYOFWEEK returns 1 for Sunday and 7 for Saturday. |
| 327 | CheckMetadataAndResult( |
| 328 | thd(), new Item_func_weekday(POS(), new Item_int(20210103 + i), true), |
| 329 | i + 1); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // Verifies that the results returned by the TIME_TO_SEC function are consistent |
| 334 | // with the metadata. |
| 335 | TEST_F(ItemTimeFuncTest, TimeToSecMetadata) { |
| 336 | static_assert(TIME_MAX_HOUR == 838, |
| 337 | "TIME_MAX_HOUR has changed. Update the test case to test the " |
| 338 | "new maximum value."); |
| 339 | |
| 340 | { |
| 341 | auto arg = new Item_string(STRING_WITH_LEN("838:59:59"), |
| 342 | &my_charset_utf8mb4_0900_ai_ci); |
| 343 | auto time_to_sec = new Item_func_time_to_sec(POS(), arg); |
| 344 | CheckMetadataAndResult(thd(), time_to_sec, 3020399); |
| 345 | } |
| 346 | |
| 347 | { |
| 348 | auto arg = new Item_string(STRING_WITH_LEN("-838:59:59"), |
| 349 | &my_charset_utf8mb4_0900_ai_ci); |
| 350 | auto time_to_sec = new Item_func_time_to_sec(POS(), arg); |
| 351 | CheckMetadataAndResult(thd(), time_to_sec, -3020399); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // Verifies that the results returned by the MICROSECOND function are consistent |
| 356 | // with the metadata. |
| 357 | TEST_F(ItemTimeFuncTest, MicrosecondMetadata) { |
| 358 | auto arg = new Item_string(STRING_WITH_LEN("10:11:12.123456789"), |
| 359 | &my_charset_utf8mb4_0900_ai_ci); |
| 360 | auto microsecond = new Item_func_microsecond(POS(), arg); |
| 361 | // The result gets rounded to six digits. |
| 362 | CheckMetadataAndResult(thd(), microsecond, 123457); |
| 363 | } |
| 364 | |
| 365 | struct test_data { |
| 366 | const char *secs; |
| 367 | unsigned int hour; |
| 368 | unsigned int minute; |
| 369 | unsigned int second; |
| 370 | unsigned long second_part; |
| 371 | }; |
| 372 | |
| 373 | ::std::ostream &operator<<(::std::ostream &os, const struct test_data &data) { |
| 374 | return os << data.secs; |
| 375 | } |
| 376 | |
| 377 | class ItemTimeFuncTestP : public ::testing::TestWithParam<test_data> { |
| 378 | protected: |
| 379 | void SetUp() override { |
| 380 | initializer.SetUp(); |
| 381 | m_t = GetParam(); |
| 382 | } |
| 383 | |
| 384 | void TearDown() override { initializer.TearDown(); } |
| 385 | |
| 386 | THD *thd() { return initializer.thd(); } |
| 387 | |
| 388 | Server_initializer initializer; |
| 389 | test_data m_t; |
| 390 | }; |
| 391 | |
| 392 | const test_data test_values[] = {{"0.1234564", 0, 0, 0, 123456}, |
| 393 | {"0.1234567", 0, 0, 0, 123457}, |
| 394 | {"0.1234", 0, 0, 0, 123400}, |
| 395 | {"12.1234567", 0, 0, 12, 123457}, |
| 396 | {"123", 0, 2, 3, 0}, |
| 397 | {"2378.3422349", 0, 39, 38, 342235}, |
| 398 | {"3020398.999999999", 838, 59, 59, 0}, |
| 399 | {"3020399", 838, 59, 59, 0}, |
| 400 | {"99999999.99999999", 838, 59, 59, 0}}; |
| 401 | |
| 402 | INSTANTIATE_TEST_SUITE_P(a, ItemTimeFuncTestP, |
| 403 | ::testing::ValuesIn(test_values)); |
| 404 | |
| 405 | /** |
| 406 | Test member function of @c Item_time_func |
| 407 | |
| 408 | @param item item of a sub-class of @c Item_time_func |
| 409 | @param ltime time structure that contains the expected result |
| 410 | @param decimals number of significant decimals in the expected result |
| 411 | */ |
| 412 | void testItemTimeFunctions(Item_time_func *item, MYSQL_TIME *ltime, |
| 413 | int decimals) { |
| 414 | long long int mysql_time = |
| 415 | 10000 * ltime->hour + 100 * ltime->minute + ltime->second; |
| 416 | EXPECT_EQ(mysql_time, item->val_int()); |
| 417 | |
| 418 | long long int packed = TIME_to_longlong_packed(*ltime); |
| 419 | EXPECT_EQ(packed, item->val_time_temporal()); |
| 420 | |
| 421 | double d = mysql_time + ltime->second_part / 1000000.0; |
| 422 | EXPECT_DOUBLE_EQ(d, item->val_real()); |
| 423 | |
| 424 | my_decimal decval1, decval2; |
| 425 | my_decimal *dec = item->val_decimal(&decval1); |
| 426 | double2decimal(d, &decval2); |
| 427 | EXPECT_EQ(0, my_decimal_cmp(dec, &decval2)); |
| 428 | |
| 429 | char s[20]; |
| 430 | sprintf(s, "%02d:%02d:%02d", ltime->hour, ltime->minute, ltime->second); |
| 431 | if (ltime->second_part > 0) { // Avoid trailing zeroes |
| 432 | int decs = ltime->second_part; |
| 433 | while (decs % 10 == 0) decs /= 10; |
| 434 | sprintf(s + strlen(s), ".%d", decs); |
| 435 | } else if (decimals > 0) |
| 436 | // There were decimals, but they have disappeared due to overflow |
| 437 | sprintf(s + strlen(s), ".000000"); |
| 438 | String timeStr(20); |
| 439 | EXPECT_STREQ(s, item->val_str(&timeStr)->c_ptr()); |
| 440 | |
| 441 | MYSQL_TIME ldate; |
| 442 | //> Second argument of Item_func_time::get_date is not used for anything |
| 443 | item->get_date(&ldate, 0); |
| 444 | // Todo: Should check that year, month, and day is relative to current date |
| 445 | EXPECT_EQ(ltime->hour % 24, ldate.hour); |
| 446 | EXPECT_EQ(ltime->minute, ldate.minute); |
| 447 | EXPECT_EQ(ltime->second, ldate.second); |
| 448 | EXPECT_EQ(ltime->second_part, ldate.second_part); |
| 449 | |
| 450 | // Todo: Item_time_func::save_in_field is not tested |
| 451 | } |
| 452 | |
| 453 | TEST_P(ItemTimeFuncTestP, secToTime) { |
| 454 | Item_decimal *sec = new Item_decimal(POS(), m_t.secs, strlen(m_t.secs), |
| 455 | &my_charset_latin1_bin); |
| 456 | Item_func_sec_to_time *time = new Item_func_sec_to_time(POS(), sec); |
| 457 | |
| 458 | Parse_context pc(thd(), thd()->lex->current_query_block()); |
| 459 | Item *item; |
| 460 | EXPECT_FALSE(time->itemize(&pc, &item)); |
| 461 | EXPECT_EQ(time, item); |
| 462 | EXPECT_FALSE(time->fix_fields(thd(), nullptr)); |
| 463 | |
| 464 | MYSQL_TIME ltime; |
| 465 | time->get_time(<ime); |
| 466 | EXPECT_EQ(0U, ltime.year); |
| 467 | EXPECT_EQ(0U, ltime.month); |
| 468 | EXPECT_EQ(0U, ltime.day); |
| 469 | EXPECT_EQ(m_t.hour, ltime.hour); |
| 470 | EXPECT_EQ(m_t.minute, ltime.minute); |
| 471 | EXPECT_EQ(m_t.second, ltime.second); |
| 472 | EXPECT_EQ(m_t.second_part, ltime.second_part); |
| 473 | |
| 474 | testItemTimeFunctions(time, <ime, sec->decimals); |
| 475 | } |
| 476 | |
| 477 | // Test for MODE_TIME_TRUNCATE_FRACTIONAL. |
| 478 | class ItemTimeFuncTruncFracTestP : public ::testing::TestWithParam<test_data> { |
| 479 | protected: |
| 480 | void SetUp() override { |
| 481 | initializer.SetUp(); |
| 482 | m_t = GetParam(); |
| 483 | save_mode = thd()->variables.sql_mode; |
| 484 | thd()->variables.sql_mode |= MODE_TIME_TRUNCATE_FRACTIONAL; |
| 485 | } |
| 486 | |
| 487 | void TearDown() override { |
| 488 | thd()->variables.sql_mode = save_mode; |
| 489 | initializer.TearDown(); |
| 490 | } |
| 491 | |
| 492 | THD *thd() { return initializer.thd(); } |
| 493 | |
| 494 | Server_initializer initializer; |
| 495 | test_data m_t; |
| 496 | sql_mode_t save_mode; |
| 497 | }; |
| 498 | |
| 499 | const test_data test_values_trunc_frac[] = { |
| 500 | {"0.1234564", 0, 0, 0, 123456}, |
| 501 | {"0.1234567", 0, 0, 0, 123456}, |
| 502 | {"0.1234", 0, 0, 0, 123400}, |
| 503 | {"12.1234567", 0, 0, 12, 123456}, |
| 504 | {"123", 0, 2, 3, 0}, |
| 505 | {"2378.3422349", 0, 39, 38, 342234}, |
| 506 | {"3020398.999999999", 838, 59, 58, 999999}, |
| 507 | {"3020399", 838, 59, 59, 0}, |
| 508 | {"99999999.99999999", 838, 59, 59, 0}}; |
| 509 | |
| 510 | INSTANTIATE_TEST_SUITE_P(a, ItemTimeFuncTruncFracTestP, |
| 511 | ::testing::ValuesIn(test_values_trunc_frac)); |
| 512 | |
| 513 | TEST_P(ItemTimeFuncTruncFracTestP, secToTime) { |
| 514 | Item_decimal *sec = new Item_decimal(POS(), m_t.secs, strlen(m_t.secs), |
| 515 | &my_charset_latin1_bin); |
| 516 | Item_func_sec_to_time *time = new Item_func_sec_to_time(POS(), sec); |
| 517 | |
| 518 | Parse_context pc(thd(), thd()->lex->current_query_block()); |
| 519 | Item *item; |
| 520 | EXPECT_FALSE(time->itemize(&pc, &item)); |
| 521 | EXPECT_EQ(time, item); |
| 522 | EXPECT_FALSE(time->fix_fields(thd(), nullptr)); |
| 523 | |
| 524 | MYSQL_TIME ltime; |
| 525 | time->get_time(<ime); |
| 526 | EXPECT_EQ(0U, ltime.year); |
| 527 | EXPECT_EQ(0U, ltime.month); |
| 528 | EXPECT_EQ(0U, ltime.day); |
| 529 | EXPECT_EQ(m_t.hour, ltime.hour); |
| 530 | EXPECT_EQ(m_t.minute, ltime.minute); |
| 531 | EXPECT_EQ(m_t.second, ltime.second); |
| 532 | EXPECT_EQ(m_t.second_part, ltime.second_part); |
| 533 | |
| 534 | testItemTimeFunctions(time, <ime, sec->decimals); |
| 535 | } |
| 536 | |
| 537 | struct TimestampDiffParam { |
| 538 | std::string first_arg; |
| 539 | std::string second_arg; |
| 540 | interval_type interval; |
| 541 | int64_t expected_result; |
| 542 | }; |
| 543 | |
| 544 | class ItemFuncTimestampDiffTest |
| 545 | : public ::testing::TestWithParam<TimestampDiffParam> { |
| 546 | protected: |
| 547 | void SetUp() override { initializer.SetUp(); } |
| 548 | void TearDown() override { initializer.TearDown(); } |
| 549 | Server_initializer initializer; |
| 550 | }; |
| 551 | |
| 552 | // Verifies that the results returned by the TIMESTAMPDIFF function are |
| 553 | // consistent with the metadata. |
| 554 | TEST_P(ItemFuncTimestampDiffTest, CheckMetadataAndResult) { |
| 555 | const TimestampDiffParam ¶m = GetParam(); |
| 556 | auto arg1 = new Item_string(param.first_arg.data(), param.first_arg.size(), |
| 557 | &my_charset_utf8mb4_0900_ai_ci); |
| 558 | auto arg2 = new Item_string(param.second_arg.data(), param.second_arg.size(), |
| 559 | &my_charset_utf8mb4_0900_ai_ci); |
| 560 | auto diff = new Item_func_timestamp_diff(POS(), arg1, arg2, param.interval); |
| 561 | CheckMetadataAndResult(initializer.thd(), diff, param.expected_result); |
| 562 | } |
| 563 | |
| 564 | // TIMESTAMPDIFF(arg1, arg2) == -TIMESTAMPDIFF(arg2, arg1) |
| 565 | TEST_P(ItemFuncTimestampDiffTest, CheckMetadataAndResultArgsSwapped) { |
| 566 | const TimestampDiffParam ¶m = GetParam(); |
| 567 | auto arg1 = new Item_string(param.second_arg.data(), param.second_arg.size(), |
| 568 | &my_charset_utf8mb4_0900_ai_ci); |
| 569 | auto arg2 = new Item_string(param.first_arg.data(), param.first_arg.size(), |
| 570 | &my_charset_utf8mb4_0900_ai_ci); |
| 571 | auto diff = new Item_func_timestamp_diff(POS(), arg1, arg2, param.interval); |
| 572 | CheckMetadataAndResult(initializer.thd(), diff, -param.expected_result); |
| 573 | } |
| 574 | |
| 575 | INSTANTIATE_TEST_SUITE_P( |
| 576 | a, ItemFuncTimestampDiffTest, |
| 577 | ::testing::ValuesIn(std::initializer_list<TimestampDiffParam>{ |
| 578 | {"0000-01-01 00:00:00", "9999-12-31 23:59:59.999999", |
| 579 | INTERVAL_MICROSECOND, 315569433599999999}, |
| 580 | {"0000-01-01 00:00:00", "9999-12-31 23:59:59.999999", INTERVAL_SECOND, |
| 581 | 315569433599}, |
| 582 | {"0000-01-01 00:00:00", "9999-12-31 23:59:59.999999", INTERVAL_MINUTE, |
| 583 | 5259490559}, |
| 584 | {"0000-01-01 00:00:00", "9999-12-31 23:59:59.999999", INTERVAL_HOUR, |
| 585 | 87658175}, |
| 586 | {"0000-01-01 00:00:00", "9999-12-31 23:59:59.999999", INTERVAL_DAY, |
| 587 | 3652423}, |
| 588 | {"0000-01-01 00:00:00", "9999-12-31 23:59:59.999999", INTERVAL_WEEK, |
| 589 | 521774}, |
| 590 | {"0000-01-01 00:00:00", "9999-12-31 23:59:59.999999", INTERVAL_MONTH, |
| 591 | 12 * 10000 - 1}, |
| 592 | {"0000-01-01 00:00:00", "9999-12-31 23:59:59.999999", INTERVAL_QUARTER, |
| 593 | 4 * 10000 - 1}, |
| 594 | })); |
| 595 | |
| 596 | // Verifies that the results returned by the EXTRACT function are consistent |
| 597 | // with the metadata. |
| 598 | class ItemExtractMetadataTest : public ::testing::TestWithParam<interval_type> { |
| 599 | protected: |
| 600 | void SetUp() override { initializer.SetUp(); } |
| 601 | void TearDown() override { initializer.TearDown(); } |
| 602 | Server_initializer initializer; |
| 603 | }; |
| 604 | |
| 605 | TEST_P(ItemExtractMetadataTest, ExtractFromHighestTimestamp) { |
| 606 | auto arg = new Item_string(STRING_WITH_LEN("9999-12-31 23:59:59.999999"), |
| 607 | &my_charset_utf8mb4_0900_ai_ci); |
| 608 | CheckMetadataConsistency(initializer.thd(), |
| 609 | new Item_extract(POS(), GetParam(), arg)); |
| 610 | } |
| 611 | |
| 612 | TEST_P(ItemExtractMetadataTest, ExtractFromHighestTime) { |
| 613 | static_assert(TIME_MAX_HOUR == 838, |
| 614 | "TIME_MAX_HOUR has changed. Update the test case to test the " |
| 615 | "new maximum value."); |
| 616 | auto arg = new Item_string(STRING_WITH_LEN("838:59:59.000000"), |
| 617 | &my_charset_utf8mb4_0900_ai_ci); |
| 618 | CheckMetadataConsistency(initializer.thd(), |
| 619 | new Item_extract(POS(), GetParam(), arg)); |
| 620 | } |
| 621 | |
| 622 | TEST_P(ItemExtractMetadataTest, ExtractFromLowestTime) { |
| 623 | static_assert(TIME_MAX_HOUR == 838, |
| 624 | "TIME_MAX_HOUR has changed. Update the test case to test the " |
| 625 | "new maximum value."); |
| 626 | auto arg = new Item_string(STRING_WITH_LEN("-838:59:59.000000"), |
| 627 | &my_charset_utf8mb4_0900_ai_ci); |
| 628 | CheckMetadataConsistency(initializer.thd(), |
| 629 | new Item_extract(POS(), GetParam(), arg)); |
| 630 | } |
| 631 | |
| 632 | // Tests the EXTRACT function has correct metadata for all possible interval |
| 633 | // types. |
| 634 | INSTANTIATE_TEST_SUITE_P(IntervalTypes, ItemExtractMetadataTest, |
| 635 | testing::Range(static_cast<interval_type>(0), |
| 636 | INTERVAL_LAST)); |
| 637 | |
| 638 | } // namespace item_timefunc_unittest |