-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix cast int as time #1788
Fix cast int as time #1788
Changes from 1 commit
ce251ca
c888585
f43e0e1
8364c32
02aab1a
8528e37
9da3d32
44eeec8
a4db0eb
0e88aa1
6ff0fe5
361d1b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: leiysky <leiysky@outlook.com>
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -930,147 +930,69 @@ size_t maxFormattedDateTimeStringLength(const String & format) | |
return std::max<size_t>(result, 1); | ||
} | ||
|
||
void checkDate(const UInt64 & year, const UInt64 & month, const UInt64 & day) | ||
void MyTimeBase::check(bool allow_zero_in_date, bool allow_invalid_date) const | ||
{ | ||
if (year == 0 && month == 0 && day == 0) | ||
if (!(year == 0 && month == 0 && day == 0)) | ||
{ | ||
return; | ||
} | ||
|
||
if (month == 0 || day == 0) | ||
{ | ||
throw TiFlashException("Invalid Datetime value", Errors::Types::WrongValue); | ||
if (!allow_zero_in_date && (month == 0 || day == 0)) | ||
{ | ||
char buff[] = "0000-00-00"; | ||
std::sprintf(buff, "%04d-%02d-%02d", year, month, day); | ||
throw TiFlashException("Incorrect datetime value: " + String(buff), Errors::Types::WrongValue); | ||
} | ||
} | ||
|
||
if (year >= 9999 || month > 12) | ||
{ | ||
throw TiFlashException("Invalid Datetime value", Errors::Types::WrongValue); | ||
throw TiFlashException("Incorrect time value", Errors::Types::WrongValue); | ||
} | ||
|
||
UInt8 max_day = 31; | ||
if (!allow_invalid_date) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
constexpr static UInt8 max_days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | ||
UInt8 max_day = max_days_in_month[month - 1]; | ||
static auto is_leap_year = [](UInt16 year) { return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); }; | ||
static auto is_leap_year = [](UInt16 _year) { return ((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0); }; | ||
max_day = max_days_in_month[month - 1]; | ||
if (month == 2 && is_leap_year(year)) | ||
{ | ||
max_day = 29; | ||
} | ||
|
||
if (day > max_day) | ||
{ | ||
throw TiFlashException("Invalid Datetime value", Errors::Types::WrongValue); | ||
} | ||
} | ||
return; | ||
} | ||
|
||
MyDateTime checkedDateTime(const UInt64 & year, const UInt64 & month, const UInt64 & day, const UInt64 & hour, const UInt64 & minute, | ||
const UInt64 & second, const UInt64 & microsecond) | ||
{ | ||
if (year >= (1 << MyTimeBase::YEAR_BIT_FIELD_WIDTH) || month >= (1 << MyTimeBase::MONTH_BIT_FIELD_WIDTH) | ||
|| day >= (1 << MyTimeBase::DAY_BIT_FIELD_WIDTH) || hour >= (1 << MyTimeBase::HOUR_BIT_FIELD_WIDTH) | ||
|| minute >= (1 << MyTimeBase::MINUTE_BIT_FIELD_WIDTH) || second >= (1 << MyTimeBase::SECOND_BIT_FIELD_WIDTH) | ||
|| microsecond >= (1 << MyTimeBase::MICROSECOND_BIT_FIELD_WIDTH)) | ||
{ | ||
throw TiFlashException("Datetime value overflow", Errors::Types::WrongValue); | ||
} | ||
checkDate(year, month, day); | ||
if (hour >= 24 || minute >= 60 || second >= 60) | ||
{ | ||
throw TiFlashException("Invalid Datetime value", Errors::Types::WrongValue); | ||
} | ||
return MyDateTime(year, month, day, hour, minute, second, microsecond); | ||
} | ||
|
||
MyDateTime numberToDateTime(Int64 number) | ||
{ | ||
MyDateTime datetime(0); | ||
|
||
auto get_datetime = [](const Int64 & num) { | ||
UInt64 ymd = num / 1000000; | ||
UInt64 hms = num - ymd * 1000000; | ||
|
||
UInt64 year = ymd / 10000; | ||
ymd %= 10000; | ||
UInt64 month = ymd / 100; | ||
UInt64 day = ymd % 100; | ||
|
||
UInt64 hour = hms / 10000; | ||
hms %= 10000; | ||
UInt64 minute = hms / 100; | ||
UInt64 second = hms % 100; | ||
|
||
return checkedDateTime(year, month, day, hour, minute, second, 0); | ||
}; | ||
|
||
if (number == 0) | ||
return datetime; | ||
|
||
// datetime type | ||
if (number >= 10000101000000) | ||
{ | ||
return get_datetime(number); | ||
} | ||
|
||
// check MMDD | ||
if (number < 101) | ||
if (day > max_day) | ||
{ | ||
throw TiFlashException("Cannot convert " + std::to_string(number) + " to Datetime", Errors::Types::WrongValue); | ||
char buff[] = "0000-00-00"; | ||
std::sprintf(buff, "%04d-%02d-%02d", year, month, day); | ||
throw TiFlashException("Incorrect datetime value: " + String(buff), Errors::Types::WrongValue); | ||
} | ||
|
||
// check YYMMDD: 2000-2069 | ||
if (number <= 69 * 10000 + 1231) | ||
if (hour < 0 || hour >= 24) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Theoretically speaking, for a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
number = (number + 20000000) * 1000000; | ||
return get_datetime(number); | ||
throw TiFlashException("Incorrect datetime value", Errors::Types::WrongValue); | ||
} | ||
|
||
if (number < 70 * 10000 + 101) | ||
if (minute >= 60) | ||
{ | ||
throw TiFlashException("Cannot convert " + std::to_string(number) + " to Datetime", Errors::Types::WrongValue); | ||
throw TiFlashException("Incorrect datetime value", Errors::Types::WrongValue); | ||
} | ||
|
||
// check YYMMDD | ||
if (number <= 991231) | ||
if (second >= 60) | ||
{ | ||
number = (number + 19000000) * 1000000; | ||
return get_datetime(number); | ||
} | ||
|
||
// check hour/min/second | ||
if (number <= 99991231) | ||
{ | ||
number *= 1000000; | ||
return get_datetime(number); | ||
} | ||
|
||
// check MMDDHHMMSS | ||
if (number < 101000000) | ||
{ | ||
throw TiFlashException("Cannot convert " + std::to_string(number) + " to Datetime", Errors::Types::WrongValue); | ||
} | ||
|
||
// check YYMMDDhhmmss: 2000-2069 | ||
if (number <= 69 * 10000000000 + 1231235959) | ||
{ | ||
number += 20000000000000; | ||
return get_datetime(number); | ||
} | ||
|
||
// check YYYYMMDDhhmmss | ||
if (number < 70 * 10000000000 + 101000000) | ||
{ | ||
throw TiFlashException("Cannot convert " + std::to_string(number) + " to Datetime", Errors::Types::WrongValue); | ||
throw TiFlashException("Incorrect datetime value", Errors::Types::WrongValue); | ||
} | ||
return; | ||
} | ||
|
||
// check YYMMDDHHMMSS | ||
if (number <= 991231235959) | ||
bool checkedDateTime(const UInt64 & year, const UInt64 & month, const UInt64 & day, const UInt64 & hour, const UInt64 & minute, | ||
const UInt64 & second, const UInt64 & microsecond, MyDateTime & result) | ||
{ | ||
if (year >= (1 << MyTimeBase::YEAR_BIT_FIELD_WIDTH) || month >= (1 << MyTimeBase::MONTH_BIT_FIELD_WIDTH) | ||
|| day >= (1 << MyTimeBase::DAY_BIT_FIELD_WIDTH) || hour >= (1 << MyTimeBase::HOUR_BIT_FIELD_WIDTH) | ||
|| minute >= (1 << MyTimeBase::MINUTE_BIT_FIELD_WIDTH) || second >= (1 << MyTimeBase::SECOND_BIT_FIELD_WIDTH) | ||
|| microsecond >= (1 << MyTimeBase::MICROSECOND_BIT_FIELD_WIDTH)) | ||
{ | ||
number += 19000000000000; | ||
return get_datetime(number); | ||
result = MyDateTime(0, 0, 0, 0, 0, 0, 0); | ||
return true; | ||
} | ||
|
||
return get_datetime(number); | ||
result = MyDateTime(year, month, day, hour, minute, second, microsecond); | ||
return false; | ||
} | ||
|
||
MyDateTimeFormatter::MyDateTimeFormatter(const String & layout) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,10 @@ struct MyTimeBase | |
int week(UInt32 mode) const; | ||
|
||
std::tuple<int, int> calcWeek(UInt32 mode) const; | ||
|
||
// Check validity of time under specified SQL_MODE. | ||
// May throw exception. | ||
void check(bool allow_zero_in_date, bool allow_invalid_date) const; | ||
}; | ||
|
||
struct MyDateTime : public MyTimeBase | ||
|
@@ -139,11 +143,14 @@ int calcDayNum(int year, int month, int day); | |
|
||
size_t maxFormattedDateTimeStringLength(const String & format); | ||
|
||
MyDateTime numberToDateTime(Int64 number); | ||
|
||
|
||
bool isPunctuation(char c); | ||
|
||
bool isValidSeperator(char c, int previous_parts); | ||
|
||
// Build MyDateTime value with checking overflow of internal fields, return true if input is invalid. | ||
bool checkedDateTime(const UInt64 & year, const UInt64 & month, const UInt64 & day, const UInt64 & hour, const UInt64 & minute, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function name is misleading, better to use a more appropriate name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
const UInt64 & second, const UInt64 & microsecond, MyDateTime & result); | ||
|
||
} // namespace DB |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why
year == 0
is allowed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks weird but MySQL does allow this.
Reference from https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_zero_in_date: