Merge pull request #56286 from bigo-sg/ch_56285

Fix inconsistency of "cast('0' as DateTime64(3))" and "cast('0' as Nullable(DateTime64(3)))"
This commit is contained in:
Raúl Marín 2023-11-06 10:30:51 +01:00 committed by GitHub
commit 653a51a862
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 6 deletions

View File

@ -984,20 +984,31 @@ inline ReturnType readDateTimeTextImpl(time_t & datetime, ReadBuffer & buf, cons
template <typename ReturnType>
inline ReturnType readDateTimeTextImpl(DateTime64 & datetime64, UInt32 scale, ReadBuffer & buf, const DateLUTImpl & date_lut)
{
static constexpr bool throw_exception = std::is_same_v<ReturnType, void>;
time_t whole = 0;
bool is_negative_timestamp = (!buf.eof() && *buf.position() == '-');
bool is_empty = buf.eof();
if (!is_empty)
{
try
if constexpr (throw_exception)
{
readDateTimeTextImpl<ReturnType, true>(whole, buf, date_lut);
try
{
readDateTimeTextImpl<ReturnType, true>(whole, buf, date_lut);
}
catch (const DB::ParsingException &)
{
if (buf.eof() || *buf.position() != '.')
throw;
}
}
catch (const DB::ParsingException & exception)
else
{
if (buf.eof() || *buf.position() != '.')
throw exception;
auto ok = readDateTimeTextImpl<ReturnType, true>(whole, buf, date_lut);
if (!ok && (buf.eof() || *buf.position() != '.'))
return ReturnType(false);
}
}

View File

@ -1,3 +1,5 @@
1969-12-31 23:57:57.000
1970-01-01 00:00:23.900
1969-12-31 23:59:36.100
\N
\N

View File

@ -2,4 +2,9 @@ SELECT toDateTime64('-123', 3, 'UTC'); -- Allowed: no year starts with '-'
SELECT toDateTime64('23.9', 3, 'UTC'); -- Allowed: no year has a dot in notation
SELECT toDateTime64('-23.9', 3, 'UTC'); -- Allowed
SELECT toDateTime64('1234', 3, 'UTC'); -- { serverError CANNOT_PARSE_DATETIME }
SELECT toDateTime64OrNull('0', 3, 'UTC');
SELECT cast('0' as Nullable(DateTime64(3, 'UTC')));
SELECT toDateTime64('1234', 3, 'UTC'); -- { serverError CANNOT_PARSE_DATETIME }
SELECT toDateTime64('0', 3, 'UTC'); -- { serverError CANNOT_PARSE_DATETIME }
SELECT cast('0' as DateTime64(3, 'UTC')); -- { serverError CANNOT_PARSE_DATETIME }