Skip to content

Commit

Permalink
[bugfix](VecDateTimeValue) eat the value of microsecond in function f…
Browse files Browse the repository at this point in the history
…rom_date_format_str (apache#13446)

* [bugfix](VecDateTimeValue) eat the value of microsecond in function from_date_format_str

* add sql based regression test

Co-authored-by: xiaojunjie <[email protected]>
  • Loading branch information
2 people authored and chenjie committed Oct 20, 2022
1 parent 563dc29 commit 2335902
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions be/src/vec/runtime/vdatetime_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,12 @@ bool VecDateTimeValue::from_date_format_str(const char* format, int format_len,
break;
// Micro second
case 'f':
// _microsecond is removed, but need to eat this val
tmp = val + min(6, val_end - val);
if (!str_to_int64(val, &tmp, &int_value)) {
return false;
}
val = tmp;
break;
// AM/PM
case 'p':
Expand Down
21 changes: 21 additions & 0 deletions be/test/vec/runtime/vdatetime_value_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,27 @@ TEST(VDateTimeValueTest, date_diff_test) {
EXPECT_TRUE(datetime_diff<TimeUnit::MINUTE>(date_v2_1, date_v2_2) == 31 * 24 * 60);
EXPECT_TRUE(datetime_diff<TimeUnit::SECOND>(date_v2_1, date_v2_2) == 31 * 24 * 60 * 60);
}

{
VecDateTimeValue date_v2_1;
std::string origin_date1 = "2022-05-24 06:00:00";
std::string date_format1 = "%Y-%m-%d %H:%i:%s";
EXPECT_TRUE(date_v2_1.from_date_format_str(date_format1.data(), date_format1.size(),
origin_date1.data(), origin_date1.size()));

VecDateTimeValue date_v2_2;
std::string origin_date2 = "2022-06-24 06:00:00.123 AM";
std::string date_format2 = "%Y-%m-%d %h:%i:%s.%f %p";
EXPECT_TRUE(date_v2_2.from_date_format_str(date_format2.data(), date_format2.size(),
origin_date2.data(), origin_date2.size()));

EXPECT_TRUE(datetime_diff<TimeUnit::DAY>(date_v2_1, date_v2_2) == 31);
EXPECT_TRUE(datetime_diff<TimeUnit::YEAR>(date_v2_1, date_v2_2) == 0);
EXPECT_TRUE(datetime_diff<TimeUnit::MONTH>(date_v2_1, date_v2_2) == 1);
EXPECT_TRUE(datetime_diff<TimeUnit::HOUR>(date_v2_1, date_v2_2) == 31 * 24);
EXPECT_TRUE(datetime_diff<TimeUnit::MINUTE>(date_v2_1, date_v2_2) == 31 * 24 * 60);
EXPECT_TRUE(datetime_diff<TimeUnit::SECOND>(date_v2_1, date_v2_2) == 31 * 24 * 60 * 60);
}
}

TEST(VDateTimeValueTest, date_v2_to_string_test) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ February
-- !sql --
2014-12-21T12:34:56

-- !sql --
2014-12-21T12:34:56

-- !sql --
2004-10-18

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ suite("test_date_function") {
sql """ insert into ${tableName} values ("2014-12-21 12:34:56") """
qt_sql """ select str_to_date(test_datetime, '%Y-%m-%d %H:%i:%s') from ${tableName}; """
qt_sql """ select str_to_date("2014-12-21 12:34%3A56", '%Y-%m-%d %H:%i%%3A%s'); """
qt_sql """ select str_to_date("2014-12-21 12:34:56.789 PM", '%Y-%m-%d %h:%i:%s.%f %p'); """
qt_sql """ select str_to_date('200442 Monday', '%X%V %W') """
sql """ truncate table ${tableName} """
sql """ insert into ${tableName} values ("2020-09-01") """
Expand Down

0 comments on commit 2335902

Please sign in to comment.