Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Formats/SchemaInferenceUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ namespace

ReadBufferFromString buf(field);
DayNum tmp;
return tryReadDateText(tmp, buf, DateLUT::instance(), /*allowed_delimiters=*/"-/:") && buf.eof();
return tryReadDateText(tmp, buf, DateLUT::instance(), /*allowed_delimiters=*/"-/:", /*saturate_on_overflow=*/false) && buf.eof();
}

DataTypePtr tryInferDateTimeOrDateTime64(std::string_view field, const FormatSettings & settings)
Expand Down Expand Up @@ -827,7 +827,7 @@ namespace
switch (settings.date_time_input_format)
{
case FormatSettings::DateTimeInputFormat::Basic:
if (tryReadDateTimeText(tmp, buf, DateLUT::instance(), /*allowed_date_delimiters=*/"-/:", /*allowed_time_delimiters=*/":") && buf.eof())
if (tryReadDateTimeText(tmp, buf, DateLUT::instance(), /*allowed_date_delimiters=*/"-/:", /*allowed_time_delimiters=*/":", /*saturate_on_overflow=*/false) && buf.eof())
return std::make_shared<DataTypeDateTime>();
break;
case FormatSettings::DateTimeInputFormat::BestEffort:
Expand All @@ -846,7 +846,7 @@ namespace
switch (settings.date_time_input_format)
{
case FormatSettings::DateTimeInputFormat::Basic:
if (tryReadDateTime64Text(tmp, 9, buf, DateLUT::instance(), /*allowed_date_delimiters=*/"-/:", /*allowed_time_delimiters=*/":") && buf.eof())
if (tryReadDateTime64Text(tmp, 9, buf, DateLUT::instance(), /*allowed_date_delimiters=*/"-/:", /*allowed_time_delimiters=*/":", /*saturate_on_overflow=*/false) && buf.eof())
return std::make_shared<DataTypeDateTime64>(9);
break;
case FormatSettings::DateTimeInputFormat::BestEffort:
Expand Down
44 changes: 32 additions & 12 deletions src/IO/ReadHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,13 @@ template bool readDateTextFallback<bool>(LocalDate &, ReadBuffer &, const char *


template <typename ReturnType, bool dt64_mode>
ReturnType readDateTimeTextFallback(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_date_delimiters, const char * allowed_time_delimiters)
ReturnType readDateTimeTextFallback(
time_t & datetime,
ReadBuffer & buf,
const DateLUTImpl & date_lut,
const char * allowed_date_delimiters,
const char * allowed_time_delimiters,
bool saturate_on_overflow)
{
static constexpr bool throw_exception = std::is_same_v<ReturnType, void>;

Expand Down Expand Up @@ -1576,15 +1582,29 @@ ReturnType readDateTimeTextFallback(time_t & datetime, ReadBuffer & buf, const D
}
else
{
auto datetime_maybe = tryToMakeDateTime(date_lut, year, month, day, hour, minute, second);
if (!datetime_maybe)
return false;
if (saturate_on_overflow)
{
/// Use saturating version - makeDateTime saturates out-of-range years
if (unlikely(year == 0))
datetime = 0;
else
datetime = makeDateTime(date_lut, year, month, day, hour, minute, second);
}
else
{
/// Use non-saturating version - return false for out-of-range values
auto datetime_maybe = tryToMakeDateTime(date_lut, year, month, day, hour, minute, second);
if (!datetime_maybe)
return false;

/// For usual DateTime check if value is within supported range
if (!dt64_mode && (*datetime_maybe < 0 || *datetime_maybe > UINT32_MAX))
return false;
if constexpr (!dt64_mode)
{
if (*datetime_maybe < 0 || *datetime_maybe > static_cast<Int64>(UINT32_MAX))
return false;
}

datetime = *datetime_maybe;
datetime = *datetime_maybe;
}
}
}
else
Expand Down Expand Up @@ -1620,10 +1640,10 @@ ReturnType readDateTimeTextFallback(time_t & datetime, ReadBuffer & buf, const D
return ReturnType(true);
}

template void readDateTimeTextFallback<void, false>(time_t &, ReadBuffer &, const DateLUTImpl &, const char *, const char *);
template void readDateTimeTextFallback<void, true>(time_t &, ReadBuffer &, const DateLUTImpl &, const char *, const char *);
template bool readDateTimeTextFallback<bool, false>(time_t &, ReadBuffer &, const DateLUTImpl &, const char *, const char *);
template bool readDateTimeTextFallback<bool, true>(time_t &, ReadBuffer &, const DateLUTImpl &, const char *, const char *);
template void readDateTimeTextFallback<void, false>(time_t &, ReadBuffer &, const DateLUTImpl &, const char *, const char *, bool);
template void readDateTimeTextFallback<void, true>(time_t &, ReadBuffer &, const DateLUTImpl &, const char *, const char *, bool);
template bool readDateTimeTextFallback<bool, false>(time_t &, ReadBuffer &, const DateLUTImpl &, const char *, const char *, bool);
template bool readDateTimeTextFallback<bool, true>(time_t &, ReadBuffer &, const DateLUTImpl &, const char *, const char *, bool);

template <typename ReturnType, bool t64_mode>
ReturnType readTimeTextFallback(time_t & time, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_date_delimiters, const char * allowed_time_delimiters)
Expand Down
79 changes: 52 additions & 27 deletions src/IO/ReadHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ inline bool tryToConvertToDayNum(DayNum & date, ExtendedDayNum & from)
}

template <typename ReturnType = void>
inline ReturnType readDateTextImpl(DayNum & date, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_delimiters = nullptr)
inline ReturnType readDateTextImpl(DayNum & date, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_delimiters = nullptr, bool saturate_on_overflow = true)
{
static constexpr bool throw_exception = std::is_same_v<ReturnType, void>;

Expand All @@ -636,12 +636,22 @@ inline ReturnType readDateTextImpl(DayNum & date, ReadBuffer & buf, const DateLU
if (!readDateTextImpl<ReturnType>(local_date, buf, allowed_delimiters))
return false;

auto ret = tryToMakeDayNum(date_lut, local_date.year(), local_date.month(), local_date.day());
if (!ret)
return false;
if (saturate_on_overflow)
{
/// Use saturating versions - makeDayNum saturates out-of-range years, convertToDayNum saturates to 0 or 0xFFFF
ExtendedDayNum ret = makeDayNum(date_lut, local_date.year(), local_date.month(), local_date.day());
convertToDayNum(date, ret);
}
else
{
/// Use non-saturating versions - return false for out-of-range values
auto ret = tryToMakeDayNum(date_lut, local_date.year(), local_date.month(), local_date.day());
if (!ret)
return false;

if (!tryToConvertToDayNum(date, *ret))
return false;
if (!tryToConvertToDayNum(date, *ret))
return false;
}

return true;
}
Expand Down Expand Up @@ -685,9 +695,9 @@ inline bool tryReadDateText(LocalDate & date, ReadBuffer & buf, const char * all
return readDateTextImpl<bool>(date, buf, allowed_delimiters);
}

inline bool tryReadDateText(DayNum & date, ReadBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance(), const char * allowed_delimiters = nullptr)
inline bool tryReadDateText(DayNum & date, ReadBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance(), const char * allowed_delimiters = nullptr, bool saturate_on_overflow = true)
{
return readDateTextImpl<bool>(date, buf, time_zone, allowed_delimiters);
return readDateTextImpl<bool>(date, buf, time_zone, allowed_delimiters, saturate_on_overflow);
}

inline bool tryReadDateText(ExtendedDayNum & date, ReadBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance(), const char * allowed_delimiters = nullptr)
Expand Down Expand Up @@ -815,7 +825,7 @@ inline T parseFromStringWithoutAssertEOF(std::string_view str)
}

template <typename ReturnType = void, bool dt64_mode = false>
ReturnType readDateTimeTextFallback(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr);
ReturnType readDateTimeTextFallback(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr, bool saturate_on_overflow = true);

template <typename ReturnType = void, bool t64_mode = false>
ReturnType readTimeTextFallback(time_t & time, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr);
Expand All @@ -824,7 +834,7 @@ ReturnType readTimeTextFallback(time_t & time, ReadBuffer & buf, const DateLUTIm
* As an exception, also supported parsing of unix timestamp in form of decimal number.
*/
template <typename ReturnType = void, bool dt64_mode = false>
inline ReturnType readDateTimeTextImpl(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr)
inline ReturnType readDateTimeTextImpl(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr, bool saturate_on_overflow = true)
{
static constexpr bool throw_exception = std::is_same_v<ReturnType, void>;

Expand Down Expand Up @@ -898,15 +908,30 @@ inline ReturnType readDateTimeTextImpl(time_t & datetime, ReadBuffer & buf, cons
}
else
{
auto datetime_maybe = tryToMakeDateTime(date_lut, year, month, day, hour, minute, second);
if (!datetime_maybe)
return false;

/// For usual DateTime check if value is within supported range
if (!dt64_mode && (*datetime_maybe < 0 || *datetime_maybe > UINT32_MAX))
return false;

datetime = *datetime_maybe;
if (saturate_on_overflow)
{
/// Use saturating version - makeDateTime saturates out-of-range years
if (unlikely(year == 0))
datetime = 0;
else
datetime = makeDateTime(date_lut, year, month, day, hour, minute, second);
}
else
{
/// Use non-saturating version - return false for out-of-range values
auto datetime_maybe = tryToMakeDateTime(date_lut, year, month, day, hour, minute, second);
if (!datetime_maybe)
return false;

/// For usual DateTime check if value is within supported range
if constexpr (!dt64_mode)
{
if (*datetime_maybe < 0 || *datetime_maybe > static_cast<Int64>(UINT32_MAX))
return false;
}

datetime = *datetime_maybe;
}
}

if (dt_long)
Expand All @@ -919,7 +944,7 @@ inline ReturnType readDateTimeTextImpl(time_t & datetime, ReadBuffer & buf, cons
/// Why not readIntTextUnsafe? Because for needs of AdFox, parsing of unix timestamp with leading zeros is supported: 000...NNNN.
return readIntTextImpl<time_t, ReturnType, ReadIntTextCheckOverflow::CHECK_OVERFLOW>(datetime, buf);
}
return readDateTimeTextFallback<ReturnType, dt64_mode>(datetime, buf, date_lut, allowed_date_delimiters, allowed_time_delimiters);
return readDateTimeTextFallback<ReturnType, dt64_mode>(datetime, buf, date_lut, allowed_date_delimiters, allowed_time_delimiters, saturate_on_overflow);
}

/** In hhh:mm:ss format, according to specified time zone.
Expand Down Expand Up @@ -1107,7 +1132,7 @@ inline ReturnType readTimeTextImpl(time_t & time, ReadBuffer & buf, const DateLU
}

template <typename ReturnType>
inline ReturnType readDateTimeTextImpl(DateTime64 & datetime64, UInt32 scale, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr)
inline ReturnType readDateTimeTextImpl(DateTime64 & datetime64, UInt32 scale, ReadBuffer & buf, const DateLUTImpl & date_lut, const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr, bool saturate_on_overflow = true)
{
static constexpr bool throw_exception = std::is_same_v<ReturnType, void>;

Expand All @@ -1121,7 +1146,7 @@ inline ReturnType readDateTimeTextImpl(DateTime64 & datetime64, UInt32 scale, Re
{
try
{
readDateTimeTextImpl<ReturnType, true>(whole, buf, date_lut, allowed_date_delimiters, allowed_time_delimiters);
readDateTimeTextImpl<ReturnType, true>(whole, buf, date_lut, allowed_date_delimiters, allowed_time_delimiters, saturate_on_overflow);
}
catch (const DB::Exception &)
{
Expand All @@ -1131,7 +1156,7 @@ inline ReturnType readDateTimeTextImpl(DateTime64 & datetime64, UInt32 scale, Re
}
else
{
auto ok = readDateTimeTextImpl<ReturnType, true>(whole, buf, date_lut, allowed_date_delimiters, allowed_time_delimiters);
auto ok = readDateTimeTextImpl<ReturnType, true>(whole, buf, date_lut, allowed_date_delimiters, allowed_time_delimiters, saturate_on_overflow);
if (!ok && (buf.eof() || *buf.position() != '.'))
return ReturnType(false);
}
Expand Down Expand Up @@ -1367,14 +1392,14 @@ inline bool tryReadTimeText(time_t & time, ReadBuffer & buf, const DateLUTImpl &
return readTimeTextImpl<bool>(time, buf, time_zone, allowed_date_delimiters, allowed_time_delimiters);
}

inline bool tryReadDateTimeText(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance(), const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr)
inline bool tryReadDateTimeText(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance(), const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr, bool saturate_on_overflow = true)
{
return readDateTimeTextImpl<bool>(datetime, buf, time_zone, allowed_date_delimiters, allowed_time_delimiters);
return readDateTimeTextImpl<bool>(datetime, buf, time_zone, allowed_date_delimiters, allowed_time_delimiters, saturate_on_overflow);
}

inline bool tryReadDateTime64Text(DateTime64 & datetime64, UInt32 scale, ReadBuffer & buf, const DateLUTImpl & date_lut = DateLUT::instance(), const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr)
inline bool tryReadDateTime64Text(DateTime64 & datetime64, UInt32 scale, ReadBuffer & buf, const DateLUTImpl & date_lut = DateLUT::instance(), const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr, bool saturate_on_overflow = true)
{
return readDateTimeTextImpl<bool>(datetime64, scale, buf, date_lut, allowed_date_delimiters, allowed_time_delimiters);
return readDateTimeTextImpl<bool>(datetime64, scale, buf, date_lut, allowed_date_delimiters, allowed_time_delimiters, saturate_on_overflow);
}

inline bool tryReadTime64Text(Time64 & time64, UInt32 scale, ReadBuffer & buf, const DateLUTImpl & date_lut = DateLUT::instance(), const char * allowed_date_delimiters = nullptr, const char * allowed_time_delimiters = nullptr)
Expand Down
26 changes: 16 additions & 10 deletions src/IO/parseDateTimeBestEffort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace DB

namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int CANNOT_PARSE_DATETIME;
extern const int LOGICAL_ERROR;
extern const int CANNOT_PARSE_DATETIME;
}


Expand Down Expand Up @@ -753,7 +753,7 @@ ReturnType parseDateTimeBestEffortImpl(
}
};

if constexpr (std::is_same_v<ReturnType, void>)
if constexpr (!strict || std::is_same_v<ReturnType, void>)
{
if (has_time_zone_offset)
{
Expand All @@ -764,20 +764,24 @@ ReturnType parseDateTimeBestEffortImpl(
{
res = local_time_zone.makeDateTime(year, month, day_of_month, hour, minute, second);
}

if constexpr (std::is_same_v<ReturnType, bool>)
return true;
}
else
{

if (has_time_zone_offset)
{
auto res_maybe = utc_time_zone.tryToMakeDateTime(year, month, day_of_month, hour, minute, second);
if (!res_maybe)
return false;

/// For usual DateTime check if value is within supported range
if (!is_64 && (*res_maybe < 0 || *res_maybe > UINT32_MAX))
return false;

if constexpr (!is_64)
{
if (*res_maybe < 0 || *res_maybe > UINT32_MAX)
return false;
}
res = *res_maybe;
adjust_time_zone();
}
Expand All @@ -788,9 +792,11 @@ ReturnType parseDateTimeBestEffortImpl(
return false;

/// For usual DateTime check if value is within supported range
if (!is_64 && (*res_maybe < 0 || *res_maybe > UINT32_MAX))
return false;

if constexpr (!is_64)
{
if (*res_maybe < 0 || *res_maybe > UINT32_MAX)
return false;
}
res = *res_maybe;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
256
2020-12-24
\N
1970-01-01
\N
\N
\N
2149-06-06
2020-12-24 01:02:03
\N
\N
1970-01-01 02:00:00
\N
2020-12-24 01:02:03.00
\N
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
2023-05-30 14:38:20
1970-01-01 00:00:19
1970-01-01 19:26:40
\N
\N
1970-01-01 00:00:00
2106-02-07 06:28:15
\N
\N
\N
2023-05-30
\N
2149-06-06
1970-01-20
\N
\N
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
2023-03-21 19:00:00 3
2023-03-21 20:00:00 3
2023-03-21 21:00:00 3
2106-02-07 06:28:15 9
2023-03-21 13:00:00 0
2023-03-21 14:00:00 1
2023-03-21 15:00:00 2
Expand All @@ -16,6 +17,7 @@
2023-03-21 19:00:00 3
2023-03-21 20:00:00 3
2023-03-21 21:00:00 3
2106-02-07 06:28:15 9
2023-03-21 12:00:00 \N
2023-03-21 13:00:00 0
2023-03-21 14:00:00 1
Expand All @@ -26,7 +28,7 @@
2023-03-21 19:00:00 3
2023-03-21 20:00:00 3
2023-03-21 21:00:00 3
\N \N
2106-02-07 06:28:15 9
\N \N
2023-03-21 12:00:00 0
2023-03-21 13:00:00 0
Expand All @@ -38,7 +40,7 @@
2023-03-21 19:00:00 3
2023-03-21 20:00:00 3
2023-03-21 21:00:00 3
\N 0
2106-02-07 06:28:15 9
\N 0
2023-03-21 12:00:00 \N
2023-03-21 13:00:00 \N
Expand All @@ -50,5 +52,5 @@
2023-03-21 19:00:00 \N
2023-03-21 20:00:00 \N
2023-03-21 21:00:00 \N
\N \N
2106-02-07 06:28:15 \N
\N \N
Loading
Loading