Merge pull request #58139 from zvonand/zvonand-issue-53190

Fix `accurateCastOrNull` for out-of-range DateTime
This commit is contained in:
Nikolay Degterinsky 2023-12-23 14:52:59 +01:00 committed by GitHub
commit a30980c930
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 6 deletions

View File

@ -1413,10 +1413,10 @@ inline bool tryParseImpl<DataTypeDate32>(DataTypeDate32::FieldType & x, ReadBuff
template <>
inline bool tryParseImpl<DataTypeDateTime>(DataTypeDateTime::FieldType & x, ReadBuffer & rb, const DateLUTImpl * time_zone, bool)
{
time_t tmp = 0;
if (!tryReadDateTimeText(tmp, rb, *time_zone))
time_t time = 0;
if (!tryReadDateTimeText(time, rb, *time_zone))
return false;
x = static_cast<UInt32>(tmp);
convertFromTime<DataTypeDateTime>(x, time);
return true;
}
@ -1697,7 +1697,6 @@ struct ConvertThroughParsing
break;
}
}
parseImpl<ToDataType>(vec_to[i], read_buffer, local_time_zone, precise_float_parsing);
} while (false);
}
@ -3291,7 +3290,6 @@ private:
{
/// In case when converting to Nullable type, we apply different parsing rule,
/// that will not throw an exception but return NULL in case of malformed input.
FunctionPtr function = FunctionConvertFromString<ToDataType, FunctionName, ConvertFromStringExceptionMode::Null>::create();
return createFunctionAdaptor(function, from_type);
}

View File

@ -36,6 +36,8 @@
2023-05-30 14:38:20
1970-01-01 00:00:19
1970-01-01 19:26:40
1970-01-01 00:00:00
2106-02-07 06:28:15
\N
\N
\N

View File

@ -39,9 +39,12 @@ SELECT accurateCastOrNull(number + 127, 'Int8') AS x FROM numbers (2) ORDER BY x
SELECT accurateCastOrNull(-1, 'DateTime');
SELECT accurateCastOrNull(5000000000, 'DateTime');
SELECT accurateCastOrNull('1xxx', 'DateTime');
select toString(accurateCastOrNull('2023-05-30 14:38:20', 'DateTime'), timezone());
SELECT toString(accurateCastOrNull('2023-05-30 14:38:20', 'DateTime'), timezone());
SELECT toString(accurateCastOrNull(19, 'DateTime'), 'UTC');
SELECT toString(accurateCastOrNull(70000, 'DateTime'), 'UTC');
-- need fixed timezone in these two lines
SELECT toString(accurateCastOrNull('1965-05-30 14:38:20', 'DateTime'), timezone()) SETTINGS session_timezone = 'UTC';
SELECT toString(accurateCastOrNull('2223-05-30 14:38:20', 'DateTime'), timezone()) SETTINGS session_timezone = 'UTC';
SELECT accurateCastOrNull(-1, 'Date');
SELECT accurateCastOrNull(5000000000, 'Date');