github-actions[bot] commented on code in PR #39422:
URL: https://github.com/apache/doris/pull/39422#discussion_r1718129496
##########
be/src/vec/runtime/time_value.h:
##########
@@ -17,22 +17,165 @@
#pragma once
+#include <cmath>
#include <string>
#include "runtime/define_primitive_type.h"
#include "runtime/primitive_type.h"
#include "util/date_func.h"
+#include "util/string_parser.hpp"
+#include "vec/runtime/vdatetime_value.h"
namespace doris {
/// TODO: Due to the "Time type is not supported for OLAP table" issue, a lot
of basic content is missing.It will be supplemented later.
class TimeValue {
using TimeType = typename PrimitiveTypeTraits<TYPE_TIMEV2>::CppType;
+ constexpr static int64_t microsecond = 1000 * 1000;
public:
+ static double to_time(int64_t hour, int64_t minute, int64_t second,
int64_t micro = 0) {
+ return hour * 3600 * microsecond + minute * 60 * microsecond + second
* microsecond + micro;
+ }
+
static std::string to_string(TimeType time, int scale) {
return timev2_to_buffer_from_double(time, scale);
}
+
+ // Cast from string
+ // Some examples of conversions.
+ // '300' -> 00:03:00 '20:23' -> 20:23:00 '20:23:24' -> 20:23:24
+ static bool try_parse_time_from_string(char* s, size_t len, TimeType& x,
+ const cctz::time_zone&
local_time_zone) {
+ if (_try_as_time_from_string(s, len, x)) {
+ return true;
+ } else {
+ if (DateV2Value<DateTimeV2ValueType> dv {}; dv.from_date_str(s,
len, local_time_zone)) {
+ // can be parse as a datetime
+ x = to_time(dv.hour(), dv.minute(), dv.second(),
dv.microsecond());
+ return true;
+ }
+ return false;
+ }
+ }
+ // Cast from number
+ template <typename T>
+ static bool try_parse_time_from_number(T from, TimeType& x) {
+ int64 time = static_cast<int64>(from);
+ int64 seconds = int64(time / 100);
+ int64 hour = 0, minute = 0, second = 0;
+ second = int64(time - 100 * seconds);
+ time /= 100;
+ seconds = int64(time / 100);
+ minute = int64(time - 100 * seconds);
+ hour = seconds;
+ if (minute >= 60 || second >= 60) {
+ return false;
+ }
+ x = to_time(hour, minute, second);
+ return true;
+ }
+
+private:
+ template <typename T>
+ static bool _try_as_time_from_string(char* s, size_t len, T& x) {
Review Comment:
warning: function '_try_as_time_from_string' has cognitive complexity of 51
(threshold 50) [readability-function-cognitive-complexity]
```cpp
static bool _try_as_time_from_string(char* s, size_t len, T& x) {
^
```
<details>
<summary>Additional context</summary>
**be/src/vec/runtime/time_value.h:84:** nesting level increased to 1
```cpp
auto parse_from_str_to_int = [](char* begin, size_t len, auto& num) {
^
```
**be/src/vec/runtime/time_value.h:88:** +2, including nesting penalty of 1,
nesting level increased to 2
```cpp
if (UNLIKELY(parse_result != StringParser::PARSE_SUCCESS)) {
^
```
**be/src/vec/runtime/time_value.h:94:** nesting level increased to 1
```cpp
auto parse_micro = [&](char* begin, int64_t len, int64_t& num) {
^
```
**be/src/vec/runtime/time_value.h:98:** +2, including nesting penalty of 1,
nesting level increased to 2
```cpp
if (!parse_from_str_to_int(begin, len, micro_num)) {
^
```
**be/src/vec/runtime/time_value.h:107:** +1, including nesting penalty of 0,
nesting level increased to 1
```cpp
if (char* first_colon {nullptr};
^
```
**be/src/vec/runtime/time_value.h:109:** +2, including nesting penalty of 1,
nesting level increased to 2
```cpp
if (char* second_colon {nullptr};
^
```
**be/src/vec/runtime/time_value.h:112:** +3, including nesting penalty of 2,
nesting level increased to 3
```cpp
if (char* third_colon {nullptr};
^
```
**be/src/vec/runtime/time_value.h:117:** +4, including nesting penalty of 3,
nesting level increased to 4
```cpp
if (!parse_from_str_to_int(first_char, first_colon -
first_char, hour)) {
^
```
**be/src/vec/runtime/time_value.h:121:** +4, including nesting penalty of 3,
nesting level increased to 4
```cpp
if (!parse_from_str_to_int(first_colon + 1, second_colon
- first_colon - 1,
^
```
**be/src/vec/runtime/time_value.h:126:** +4, including nesting penalty of 3,
nesting level increased to 4
```cpp
if (!parse_from_str_to_int(second_colon + 1, third_colon
- second_colon - 1,
^
```
**be/src/vec/runtime/time_value.h:131:** +4, including nesting penalty of 3,
nesting level increased to 4
```cpp
if (!parse_micro(third_colon + 1, end_char - third_colon
- 1, micro)) {
^
```
**be/src/vec/runtime/time_value.h:134:** +1, nesting level increased to 3
```cpp
} else {
^
```
**be/src/vec/runtime/time_value.h:137:** +4, including nesting penalty of 3,
nesting level increased to 4
```cpp
if (!parse_from_str_to_int(first_char, first_colon -
first_char, hour)) {
^
```
**be/src/vec/runtime/time_value.h:141:** +4, including nesting penalty of 3,
nesting level increased to 4
```cpp
if (!parse_from_str_to_int(first_colon + 1, second_colon
- first_colon - 1,
^
```
**be/src/vec/runtime/time_value.h:146:** +4, including nesting penalty of 3,
nesting level increased to 4
```cpp
if (!parse_from_str_to_int(second_colon + 1, end_char -
second_colon - 1,
^
```
**be/src/vec/runtime/time_value.h:151:** +1, nesting level increased to 2
```cpp
} else {
^
```
**be/src/vec/runtime/time_value.h:154:** +3, including nesting penalty of 2,
nesting level increased to 3
```cpp
if (!parse_from_str_to_int(first_char, first_colon -
first_char, hour)) {
^
```
**be/src/vec/runtime/time_value.h:158:** +3, including nesting penalty of 2,
nesting level increased to 3
```cpp
if (!parse_from_str_to_int(first_colon + 1, end_char -
first_colon - 1, minute)) {
^
```
**be/src/vec/runtime/time_value.h:162:** +1, nesting level increased to 1
```cpp
} else {
^
```
**be/src/vec/runtime/time_value.h:166:** +2, including nesting penalty of 1,
nesting level increased to 2
```cpp
if (!parse_from_str_to_int(first_char, len, from)) {
^
```
**be/src/vec/runtime/time_value.h:172:** +1, including nesting penalty of 0,
nesting level increased to 1
```cpp
if (minute >= 60 || second >= 60) {
^
```
**be/src/vec/runtime/time_value.h:172:** +1
```cpp
if (minute >= 60 || second >= 60) {
^
```
</details>
##########
be/src/vec/runtime/time_value.h:
##########
@@ -17,22 +17,165 @@
#pragma once
+#include <cmath>
#include <string>
#include "runtime/define_primitive_type.h"
#include "runtime/primitive_type.h"
#include "util/date_func.h"
+#include "util/string_parser.hpp"
+#include "vec/runtime/vdatetime_value.h"
namespace doris {
/// TODO: Due to the "Time type is not supported for OLAP table" issue, a lot
of basic content is missing.It will be supplemented later.
class TimeValue {
using TimeType = typename PrimitiveTypeTraits<TYPE_TIMEV2>::CppType;
+ constexpr static int64_t microsecond = 1000 * 1000;
public:
+ static double to_time(int64_t hour, int64_t minute, int64_t second,
int64_t micro = 0) {
+ return hour * 3600 * microsecond + minute * 60 * microsecond + second
* microsecond + micro;
+ }
+
static std::string to_string(TimeType time, int scale) {
return timev2_to_buffer_from_double(time, scale);
}
+
+ // Cast from string
+ // Some examples of conversions.
+ // '300' -> 00:03:00 '20:23' -> 20:23:00 '20:23:24' -> 20:23:24
+ static bool try_parse_time_from_string(char* s, size_t len, TimeType& x,
+ const cctz::time_zone&
local_time_zone) {
+ if (_try_as_time_from_string(s, len, x)) {
+ return true;
+ } else {
+ if (DateV2Value<DateTimeV2ValueType> dv {}; dv.from_date_str(s,
len, local_time_zone)) {
+ // can be parse as a datetime
+ x = to_time(dv.hour(), dv.minute(), dv.second(),
dv.microsecond());
+ return true;
+ }
+ return false;
+ }
+ }
+ // Cast from number
+ template <typename T>
+ static bool try_parse_time_from_number(T from, TimeType& x) {
+ int64 time = static_cast<int64>(from);
+ int64 seconds = int64(time / 100);
Review Comment:
warning: use auto when initializing with a cast to avoid duplicating the
type name [modernize-use-auto]
```suggestion
auto seconds = int64(time / 100);
```
##########
be/src/vec/runtime/time_value.h:
##########
@@ -17,22 +17,165 @@
#pragma once
+#include <cmath>
#include <string>
#include "runtime/define_primitive_type.h"
#include "runtime/primitive_type.h"
#include "util/date_func.h"
+#include "util/string_parser.hpp"
+#include "vec/runtime/vdatetime_value.h"
namespace doris {
/// TODO: Due to the "Time type is not supported for OLAP table" issue, a lot
of basic content is missing.It will be supplemented later.
class TimeValue {
using TimeType = typename PrimitiveTypeTraits<TYPE_TIMEV2>::CppType;
+ constexpr static int64_t microsecond = 1000 * 1000;
public:
+ static double to_time(int64_t hour, int64_t minute, int64_t second,
int64_t micro = 0) {
+ return hour * 3600 * microsecond + minute * 60 * microsecond + second
* microsecond + micro;
+ }
+
static std::string to_string(TimeType time, int scale) {
return timev2_to_buffer_from_double(time, scale);
}
+
+ // Cast from string
+ // Some examples of conversions.
+ // '300' -> 00:03:00 '20:23' -> 20:23:00 '20:23:24' -> 20:23:24
+ static bool try_parse_time_from_string(char* s, size_t len, TimeType& x,
+ const cctz::time_zone&
local_time_zone) {
+ if (_try_as_time_from_string(s, len, x)) {
+ return true;
+ } else {
+ if (DateV2Value<DateTimeV2ValueType> dv {}; dv.from_date_str(s,
len, local_time_zone)) {
+ // can be parse as a datetime
+ x = to_time(dv.hour(), dv.minute(), dv.second(),
dv.microsecond());
+ return true;
+ }
+ return false;
+ }
+ }
+ // Cast from number
+ template <typename T>
+ static bool try_parse_time_from_number(T from, TimeType& x) {
+ int64 time = static_cast<int64>(from);
+ int64 seconds = int64(time / 100);
+ int64 hour = 0, minute = 0, second = 0;
+ second = int64(time - 100 * seconds);
+ time /= 100;
+ seconds = int64(time / 100);
+ minute = int64(time - 100 * seconds);
+ hour = seconds;
+ if (minute >= 60 || second >= 60) {
+ return false;
+ }
+ x = to_time(hour, minute, second);
+ return true;
+ }
+
+private:
+ template <typename T>
+ static bool _try_as_time_from_string(char* s, size_t len, T& x) {
Review Comment:
warning: function '_try_as_time_from_string' exceeds recommended
size/complexity thresholds [readability-function-size]
```cpp
static bool _try_as_time_from_string(char* s, size_t len, T& x) {
^
```
<details>
<summary>Additional context</summary>
**be/src/vec/runtime/time_value.h:80:** 97 lines including whitespace and
comments (threshold 80)
```cpp
static bool _try_as_time_from_string(char* s, size_t len, T& x) {
^
```
</details>
##########
be/src/vec/runtime/time_value.h:
##########
@@ -17,22 +17,165 @@
#pragma once
+#include <cmath>
#include <string>
#include "runtime/define_primitive_type.h"
#include "runtime/primitive_type.h"
#include "util/date_func.h"
+#include "util/string_parser.hpp"
+#include "vec/runtime/vdatetime_value.h"
namespace doris {
/// TODO: Due to the "Time type is not supported for OLAP table" issue, a lot
of basic content is missing.It will be supplemented later.
class TimeValue {
using TimeType = typename PrimitiveTypeTraits<TYPE_TIMEV2>::CppType;
+ constexpr static int64_t microsecond = 1000 * 1000;
public:
+ static double to_time(int64_t hour, int64_t minute, int64_t second,
int64_t micro = 0) {
+ return hour * 3600 * microsecond + minute * 60 * microsecond + second
* microsecond + micro;
+ }
+
static std::string to_string(TimeType time, int scale) {
return timev2_to_buffer_from_double(time, scale);
}
+
+ // Cast from string
+ // Some examples of conversions.
+ // '300' -> 00:03:00 '20:23' -> 20:23:00 '20:23:24' -> 20:23:24
+ static bool try_parse_time_from_string(char* s, size_t len, TimeType& x,
+ const cctz::time_zone&
local_time_zone) {
+ if (_try_as_time_from_string(s, len, x)) {
+ return true;
+ } else {
+ if (DateV2Value<DateTimeV2ValueType> dv {}; dv.from_date_str(s,
len, local_time_zone)) {
+ // can be parse as a datetime
+ x = to_time(dv.hour(), dv.minute(), dv.second(),
dv.microsecond());
+ return true;
+ }
+ return false;
+ }
+ }
+ // Cast from number
+ template <typename T>
+ static bool try_parse_time_from_number(T from, TimeType& x) {
+ int64 time = static_cast<int64>(from);
+ int64 seconds = int64(time / 100);
+ int64 hour = 0, minute = 0, second = 0;
+ second = int64(time - 100 * seconds);
+ time /= 100;
+ seconds = int64(time / 100);
+ minute = int64(time - 100 * seconds);
+ hour = seconds;
+ if (minute >= 60 || second >= 60) {
+ return false;
+ }
+ x = to_time(hour, minute, second);
+ return true;
+ }
+
+private:
+ template <typename T>
+ static bool _try_as_time_from_string(char* s, size_t len, T& x) {
+ char* first_char = s;
+ char* end_char = s + len;
+ int64_t hour = 0, minute = 0, second = 0, micro = 0;
+ auto parse_from_str_to_int = [](char* begin, size_t len, auto& num) {
Review Comment:
warning: pointer parameter 'begin' can be pointer to const
[readability-non-const-parameter]
```suggestion
auto parse_from_str_to_int = [](const char* begin, size_t len, auto&
num) {
```
##########
be/src/vec/runtime/time_value.h:
##########
@@ -17,22 +17,165 @@
#pragma once
+#include <cmath>
#include <string>
#include "runtime/define_primitive_type.h"
#include "runtime/primitive_type.h"
#include "util/date_func.h"
+#include "util/string_parser.hpp"
+#include "vec/runtime/vdatetime_value.h"
namespace doris {
/// TODO: Due to the "Time type is not supported for OLAP table" issue, a lot
of basic content is missing.It will be supplemented later.
class TimeValue {
using TimeType = typename PrimitiveTypeTraits<TYPE_TIMEV2>::CppType;
+ constexpr static int64_t microsecond = 1000 * 1000;
public:
+ static double to_time(int64_t hour, int64_t minute, int64_t second,
int64_t micro = 0) {
+ return hour * 3600 * microsecond + minute * 60 * microsecond + second
* microsecond + micro;
+ }
+
static std::string to_string(TimeType time, int scale) {
return timev2_to_buffer_from_double(time, scale);
}
+
+ // Cast from string
+ // Some examples of conversions.
+ // '300' -> 00:03:00 '20:23' -> 20:23:00 '20:23:24' -> 20:23:24
+ static bool try_parse_time_from_string(char* s, size_t len, TimeType& x,
+ const cctz::time_zone&
local_time_zone) {
+ if (_try_as_time_from_string(s, len, x)) {
+ return true;
+ } else {
+ if (DateV2Value<DateTimeV2ValueType> dv {}; dv.from_date_str(s,
len, local_time_zone)) {
+ // can be parse as a datetime
+ x = to_time(dv.hour(), dv.minute(), dv.second(),
dv.microsecond());
+ return true;
+ }
+ return false;
+ }
+ }
+ // Cast from number
+ template <typename T>
+ static bool try_parse_time_from_number(T from, TimeType& x) {
+ int64 time = static_cast<int64>(from);
Review Comment:
warning: use auto when initializing with a cast to avoid duplicating the
type name [modernize-use-auto]
```suggestion
auto time = static_cast<int64>(from);
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]