Skip to content

Commit

Permalink
add overflow test
Browse files Browse the repository at this point in the history
Signed-off-by: birdstorm <[email protected]>
  • Loading branch information
birdstorm committed Nov 16, 2022
1 parent 0a5e346 commit f9cadfa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
78 changes: 43 additions & 35 deletions dbms/src/Common/MyTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,39 @@ bool noNeedCheckTime(Int32, Int32, Int32, Int32, Int32, Int32)
return true;
}

UInt64 addSeconds(UInt64 t, Int64 delta)
{
// todo support zero date
if (t == 0)
{
return t;
}
MyDateTime my_time(t);
Int64 current_second = my_time.hour * MyTimeBase::SECOND_IN_ONE_HOUR + my_time.minute * MyTimeBase::SECOND_IN_ONE_MINUTE + my_time.second;
current_second += delta;
if (current_second >= 0)
{
Int64 days = current_second / MyTimeBase::SECOND_IN_ONE_DAY;
current_second = current_second % MyTimeBase::SECOND_IN_ONE_DAY;
if (days != 0)
addDays(my_time, days);
}
else
{
Int64 days = (-current_second) / MyTimeBase::SECOND_IN_ONE_DAY;
if ((-current_second) % MyTimeBase::SECOND_IN_ONE_DAY != 0)
{
days++;
}
current_second += days * MyTimeBase::SECOND_IN_ONE_DAY;
addDays(my_time, -days);
}
my_time.hour = current_second / MyTimeBase::SECOND_IN_ONE_HOUR;
my_time.minute = (current_second % MyTimeBase::SECOND_IN_ONE_HOUR) / MyTimeBase::SECOND_IN_ONE_MINUTE;
my_time.second = current_second % MyTimeBase::SECOND_IN_ONE_MINUTE;
return my_time.toPackedUInt();
}

// Return true if the time is invalid.
inline bool getDatetime(const Int64 & num, MyDateTime & result)
{
Expand Down Expand Up @@ -995,11 +1028,12 @@ std::pair<Field, bool> parseMyDateTimeAndJudgeIsDate(const String & str, int8_t
}

UInt32 micro_second = 0;
if (hhmmss && !frac_str.empty())
bool overflow;
if (hhmmss)
{
// If input string is "20170118.999", without hhmmss, fsp is meaningless.
// TODO: this case is not only meaningless, but erroneous, please confirm.
bool overflow, matched;
bool matched;
std::tie(micro_second, overflow, matched) = parseFrac(frac_str, fsp);
if (!matched)
{
Expand All @@ -1014,6 +1048,13 @@ std::pair<Field, bool> parseMyDateTimeAndJudgeIsDate(const String & str, int8_t

MyDateTime result(year, month, day, hour, minute, second, micro_second);

if (overflow)
{
// fraction part overflow, add one second to result
MyDateTime tmp(addSeconds(result.toPackedUInt(), 1));
result = tmp;
}

if (has_tz)
{
if (!hhmmss)
Expand Down Expand Up @@ -1596,39 +1637,6 @@ bool toCoreTimeChecked(const UInt64 & year, const UInt64 & month, const UInt64 &
return false;
}

UInt64 addSeconds(UInt64 t, Int64 delta)
{
// todo support zero date
if (t == 0)
{
return t;
}
MyDateTime my_time(t);
Int64 current_second = my_time.hour * MyTimeBase::SECOND_IN_ONE_HOUR + my_time.minute * MyTimeBase::SECOND_IN_ONE_MINUTE + my_time.second;
current_second += delta;
if (current_second >= 0)
{
Int64 days = current_second / MyTimeBase::SECOND_IN_ONE_DAY;
current_second = current_second % MyTimeBase::SECOND_IN_ONE_DAY;
if (days != 0)
addDays(my_time, days);
}
else
{
Int64 days = (-current_second) / MyTimeBase::SECOND_IN_ONE_DAY;
if ((-current_second) % MyTimeBase::SECOND_IN_ONE_DAY != 0)
{
days++;
}
current_second += days * MyTimeBase::SECOND_IN_ONE_DAY;
addDays(my_time, -days);
}
my_time.hour = current_second / MyTimeBase::SECOND_IN_ONE_HOUR;
my_time.minute = (current_second % MyTimeBase::SECOND_IN_ONE_HOUR) / MyTimeBase::SECOND_IN_ONE_MINUTE;
my_time.second = current_second % MyTimeBase::SECOND_IN_ONE_MINUTE;
return my_time.toPackedUInt();
}

void fillMonthAndDay(int day_num, int & month, int & day, const int * accumulated_days_per_month)
{
month = day_num / 31;
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Common/StringUtils/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,4 @@ inline std::tuple<int, int, bool> number(const std::string & str)
{
return {0, 0, false};
}
}
}
2 changes: 2 additions & 0 deletions dbms/src/Common/tests/gtest_mytime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ try
{
checkParseMyDateTime(str, expected, type_with_fraction);
}
DataTypeMyDateTime tp(2);
checkParseMyDateTime("2010-12-31 23:59:59.99999", "2011-01-01 00:00:00.00", tp);
}
catch (Exception & e)
{
Expand Down

0 comments on commit f9cadfa

Please sign in to comment.