Skip to content
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 accurateCastOrNull for out-of-range DateTime #58139

Merged
merged 3 commits into from Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/Functions/FunctionsConversion.h
Expand Up @@ -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;
}

Expand Down Expand Up @@ -1697,7 +1697,6 @@ struct ConvertThroughParsing
break;
}
}

parseImpl<ToDataType>(vec_to[i], read_buffer, local_time_zone, precise_float_parsing);
} while (false);
}
Expand Down Expand Up @@ -3291,7 +3290,6 @@ class FunctionCast final : public FunctionCastBase
{
/// 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);
}
Expand Down
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tests/queries/0_stateless/01556_accurate_cast_or_null.sql
Expand Up @@ -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');
Expand Down