HappenLee commented on code in PR #49792: URL: https://github.com/apache/doris/pull/49792#discussion_r2033065984
########## be/src/vec/runtime/time_value.h: ########## @@ -49,48 +51,187 @@ class TimeValue { return roundedValue; } - // refer to https://dev.mysql.com/doc/refman/5.7/en/time.html - // the time value between '-838:59:59' and '838:59:59' - /// TODO: Why is the time type stored as double? Can we directly use int64 and remove the time limit? - static int64_t check_over_max_time(double time) { - const static int64_t max_time = 3020399LL * 1000 * 1000; - // cast(-4562632 as time) - // -456:26:32 - // hour(cast(-4562632 as time)) - // 456 - // second(cast(-4562632 as time)) - // 32 - if (time > max_time || time < -max_time) { - return max_time; - } - return static_cast<int64_t>(time); - } - + // Construct time based on hour/minute/second/microsecond, ignoring the sign + /// TODO: Maybe we need to ensure that this function always receives positive numbers static TimeType make_time(int64_t hour, int64_t minute, int64_t second, int64_t microsecond = 0) { - int64_t value = hour * ONE_HOUR_MICROSECONDS + minute * ONE_MINUTE_MICROSECONDS + - second * ONE_SECOND_MICROSECONDS + microsecond; + int64_t value = (hour * ONE_HOUR_MICROSECONDS) + (minute * ONE_MINUTE_MICROSECONDS) + + (second * ONE_SECOND_MICROSECONDS) + microsecond; return static_cast<TimeType>(value); } static std::string to_string(TimeType time, int scale) { return timev2_to_buffer_from_double(time, scale); } + + // Return the hour/minute/second part of the time, ignoring the sign + /* + select cast(-121314 as time); -> -12:13:14 + select hour(cast(-121314 as time)),minute(cast(-121314 as time)),second(cast(-121314 as time)); -> 12 13 14 + */ static int32_t hour(TimeType time) { - return static_cast<int32_t>(check_over_max_time(time) / ONE_HOUR_MICROSECONDS); + return std::abs(static_cast<int32_t>(check_over_max_time(time) / ONE_HOUR_MICROSECONDS)); } static int32_t minute(TimeType time) { - return (check_over_max_time(time) % ONE_HOUR_MICROSECONDS) / ONE_MINUTE_MICROSECONDS; + return std::abs(static_cast<int32_t>((check_over_max_time(time) % ONE_HOUR_MICROSECONDS) / + ONE_MINUTE_MICROSECONDS)); } static int32_t second(TimeType time) { - return (check_over_max_time(time) / ONE_SECOND_MICROSECONDS) % ONE_MINUTE_SECONDS; + return std::abs(static_cast<int32_t>((check_over_max_time(time) / ONE_SECOND_MICROSECONDS) % + ONE_MINUTE_SECONDS)); } + // Construct time based on seconds static TimeType from_second(int64_t sec) { return static_cast<TimeType>(sec * ONE_SECOND_MICROSECONDS); } + + // Cast from string + // Some examples of conversions. + // '300' -> 00:03:00 '20:23' -> 20:23:00 '20:23:24' -> 20:23:24 + template <typename T> + static bool try_parse_time(char* s, size_t len, T& x) { + if (try_as_time(s, len, x)) { + return true; + } else { + // For example, "2013-01-01 01:02:03" can be parsed as datetime + if (DateV2Value<doris::DateTimeV2ValueType> dv {}; dv.from_date_str(s, (int)len)) { Review Comment: add timezone back -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org