github-actions[bot] commented on code in PR #28249:
URL: https://github.com/apache/doris/pull/28249#discussion_r1429668471


##########
be/src/vec/functions/function_cast.h:
##########
@@ -256,9 +256,8 @@ struct ConvertImpl {
 
     template <typename Additions = void*>
     static Status execute(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,

Review Comment:
   warning: function 'execute' has cognitive complexity of 96 (threshold 50) 
[readability-function-cognitive-complexity]
   ```cpp
       static Status execute(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
                     ^
   ```
   <details>
   <summary>Additional context</summary>
   
   **be/src/vec/functions/function_cast.h:268:** +1, including nesting penalty 
of 0, nesting level increased to 1
   ```cpp
           if constexpr (IsDataTypeDecimal<FromDataType> || 
IsDataTypeDecimal<ToDataType>) {
           ^
   ```
   **be/src/vec/functions/function_cast.h:268:** +1
   ```cpp
           if constexpr (IsDataTypeDecimal<FromDataType> || 
IsDataTypeDecimal<ToDataType>) {
                                                         ^
   ```
   **be/src/vec/functions/function_cast.h:269:** +2, including nesting penalty 
of 1, nesting level increased to 2
   ```cpp
               if constexpr (!(IsDataTypeDecimalOrNumber<FromDataType> || 
IsTimeType<FromDataType> ||
               ^
   ```
   **be/src/vec/functions/function_cast.h:270:** +1
   ```cpp
                               IsTimeV2Type<FromDataType>) ||
                                                           ^
   ```
   **be/src/vec/functions/function_cast.h:276:** +1, including nesting penalty 
of 0, nesting level increased to 1
   ```cpp
           if (const ColVecFrom* col_from =
           ^
   ```
   **be/src/vec/functions/function_cast.h:282:** +2, including nesting penalty 
of 1, nesting level increased to 2
   ```cpp
               if constexpr (IsDataTypeDecimal<FromDataType>) {
               ^
   ```
   **be/src/vec/functions/function_cast.h:294:** +2, including nesting penalty 
of 1, nesting level increased to 2
   ```cpp
               if constexpr (IsDataTypeDecimal<ToDataType>) {
               ^
   ```
   **be/src/vec/functions/function_cast.h:306:** +1, nesting level increased to 
2
   ```cpp
               } else {
                 ^
   ```
   **be/src/vec/functions/function_cast.h:309:** +2, including nesting penalty 
of 1, nesting level increased to 2
   ```cpp
               if constexpr (IsDataTypeNumber<ToDataType>) {
               ^
   ```
   **be/src/vec/functions/function_cast.h:313:** +2, including nesting penalty 
of 1, nesting level increased to 2
   ```cpp
               if constexpr (std::is_integral_v<ToFieldType>) {
               ^
   ```
   **be/src/vec/functions/function_cast.h:323:** +2, including nesting penalty 
of 1, nesting level increased to 2
   ```cpp
               if constexpr (IsDataTypeDecimal<FromDataType> || 
IsDataTypeDecimal<ToDataType>) {
               ^
   ```
   **be/src/vec/functions/function_cast.h:323:** +1
   ```cpp
               if constexpr (IsDataTypeDecimal<FromDataType> || 
IsDataTypeDecimal<ToDataType>) {
                                                             ^
   ```
   **be/src/vec/functions/function_cast.h:324:** +1
   ```cpp
                   bool narrow_integral = context->check_overflow_for_decimal() 
&&
                                                                                
^
   ```
   **be/src/vec/functions/function_cast.h:328:** +3, including nesting penalty 
of 2, nesting level increased to 3
   ```cpp
                   if (to_scale > from_scale) {
                   ^
   ```
   **be/src/vec/functions/function_cast.h:359:** +1, nesting level increased to 
2
   ```cpp
               } else if constexpr (IsTimeType<FromDataType>) {
                      ^
   ```
   **be/src/vec/functions/function_cast.h:360:** +3, including nesting penalty 
of 2, nesting level increased to 3
   ```cpp
                   for (size_t i = 0; i < size; ++i) {
                   ^
   ```
   **be/src/vec/functions/function_cast.h:361:** +4, including nesting penalty 
of 3, nesting level increased to 4
   ```cpp
                       if constexpr (IsTimeType<ToDataType>) {
                       ^
   ```
   **be/src/vec/functions/function_cast.h:363:** +5, including nesting penalty 
of 4, nesting level increased to 5
   ```cpp
                           if constexpr (IsDateTimeType<ToDataType>) {
                           ^
   ```
   **be/src/vec/functions/function_cast.h:365:** +1, nesting level increased to 
5
   ```cpp
                           } else {
                             ^
   ```
   **be/src/vec/functions/function_cast.h:368:** +1, nesting level increased to 
4
   ```cpp
                       } else if constexpr (IsDateV2Type<ToDataType>) {
                              ^
   ```
   **be/src/vec/functions/function_cast.h:370:** +1, nesting level increased to 
4
   ```cpp
                       } else if constexpr (IsDateTimeV2Type<ToDataType>) {
                              ^
   ```
   **be/src/vec/functions/function_cast.h:372:** +1, nesting level increased to 
4
   ```cpp
                       } else {
                         ^
   ```
   **be/src/vec/functions/function_cast.h:377:** +1, nesting level increased to 
2
   ```cpp
               } else if constexpr (IsTimeV2Type<FromDataType>) {
                      ^
   ```
   **be/src/vec/functions/function_cast.h:378:** +3, including nesting penalty 
of 2, nesting level increased to 3
   ```cpp
                   for (size_t i = 0; i < size; ++i) {
                   ^
   ```
   **be/src/vec/functions/function_cast.h:379:** +4, including nesting penalty 
of 3, nesting level increased to 4
   ```cpp
                       if constexpr (IsTimeV2Type<ToDataType>) {
                       ^
   ```
   **be/src/vec/functions/function_cast.h:380:** +5, including nesting penalty 
of 4, nesting level increased to 5
   ```cpp
                           if constexpr (IsDateTimeV2Type<ToDataType> && 
IsDateV2Type<FromDataType>) {
                           ^
   ```
   **be/src/vec/functions/function_cast.h:380:** +1
   ```cpp
                           if constexpr (IsDateTimeV2Type<ToDataType> && 
IsDateV2Type<FromDataType>) {
                                                                      ^
   ```
   **be/src/vec/functions/function_cast.h:382:** +1, nesting level increased to 
5
   ```cpp
                           } else if constexpr (IsDateTimeV2Type<FromDataType> 
&&
                                  ^
   ```
   **be/src/vec/functions/function_cast.h:382:** +1
   ```cpp
                           } else if constexpr (IsDateTimeV2Type<FromDataType> 
&&
                                                                               ^
   ```
   **be/src/vec/functions/function_cast.h:385:** +1, nesting level increased to 
5
   ```cpp
                           } else {
                             ^
   ```
   **be/src/vec/functions/function_cast.h:389:** +1, nesting level increased to 
4
   ```cpp
                       } else if constexpr (IsTimeType<ToDataType>) {
                              ^
   ```
   **be/src/vec/functions/function_cast.h:390:** +5, including nesting penalty 
of 4, nesting level increased to 5
   ```cpp
                           if constexpr (IsDateTimeType<ToDataType> && 
IsDateV2Type<FromDataType>) {
                           ^
   ```
   **be/src/vec/functions/function_cast.h:390:** +1
   ```cpp
                           if constexpr (IsDateTimeType<ToDataType> && 
IsDateV2Type<FromDataType>) {
                                                                    ^
   ```
   **be/src/vec/functions/function_cast.h:392:** +1, nesting level increased to 
5
   ```cpp
                           } else if constexpr (IsDateType<ToDataType> && 
IsDateV2Type<FromDataType>) {
                                  ^
   ```
   **be/src/vec/functions/function_cast.h:392:** +1
   ```cpp
                           } else if constexpr (IsDateType<ToDataType> && 
IsDateV2Type<FromDataType>) {
                                                                       ^
   ```
   **be/src/vec/functions/function_cast.h:394:** +1, nesting level increased to 
5
   ```cpp
                           } else if constexpr (IsDateTimeType<ToDataType> &&
                                  ^
   ```
   **be/src/vec/functions/function_cast.h:394:** +1
   ```cpp
                           } else if constexpr (IsDateTimeType<ToDataType> &&
                                                                           ^
   ```
   **be/src/vec/functions/function_cast.h:397:** +1, nesting level increased to 
5
   ```cpp
                           } else if constexpr (IsDateType<ToDataType> &&
                                  ^
   ```
   **be/src/vec/functions/function_cast.h:397:** +1
   ```cpp
                           } else if constexpr (IsDateType<ToDataType> &&
                                                                       ^
   ```
   **be/src/vec/functions/function_cast.h:400:** +1, nesting level increased to 
5
   ```cpp
                           } else {
                             ^
   ```
   **be/src/vec/functions/function_cast.h:403:** +1, nesting level increased to 
4
   ```cpp
                       } else {
                         ^
   ```
   **be/src/vec/functions/function_cast.h:404:** +5, including nesting penalty 
of 4, nesting level increased to 5
   ```cpp
                           if constexpr (IsDateTimeV2Type<FromDataType>) {
                           ^
   ```
   **be/src/vec/functions/function_cast.h:408:** +1, nesting level increased to 
5
   ```cpp
                           } else {
                             ^
   ```
   **be/src/vec/functions/function_cast.h:415:** +1, nesting level increased to 
2
   ```cpp
               } else {
                 ^
   ```
   **be/src/vec/functions/function_cast.h:416:** +3, including nesting penalty 
of 2, nesting level increased to 3
   ```cpp
                   if constexpr (IsDataTypeNumber<FromDataType> &&
                   ^
   ```
   **be/src/vec/functions/function_cast.h:423:** +4, including nesting penalty 
of 3, nesting level increased to 4
   ```cpp
                       for (size_t i = 0; i < size; ++i) {
                       ^
   ```
   **be/src/vec/functions/function_cast.h:431:** +1, nesting level increased to 
3
   ```cpp
                   } else {
                     ^
   ```
   **be/src/vec/functions/function_cast.h:432:** +4, including nesting penalty 
of 3, nesting level increased to 4
   ```cpp
                       for (size_t i = 0; i < size; ++i) {
                       ^
   ```
   **be/src/vec/functions/function_cast.h:438:** +2, including nesting penalty 
of 1, nesting level increased to 2
   ```cpp
               if constexpr (std::is_same_v<uint8_t, ToFieldType>) {
               ^
   ```
   **be/src/vec/functions/function_cast.h:439:** +3, including nesting penalty 
of 2, nesting level increased to 3
   ```cpp
                   for (int i = 0; i < size; ++i) {
                   ^
   ```
   **be/src/vec/functions/function_cast.h:445:** +1, nesting level increased to 
1
   ```cpp
           } else {
             ^
   ```
   
   </details>
   



##########
be/src/vec/data_types/data_type_decimal.h:
##########
@@ -410,73 +412,83 @@ constexpr bool IsDataTypeDecimalOrNumber =
 
 // only for casting between other integral types and decimals
 template <typename FromDataType, typename ToDataType, bool 
multiply_may_overflow,
-          bool narrow_integral>
+          bool narrow_integral, typename RealFrom, typename RealTo>
     requires IsDataTypeDecimal<FromDataType> && IsDataTypeDecimal<ToDataType>
-ToDataType::FieldType convert_decimals(const typename FromDataType::FieldType& 
value,
-                                       UInt32 scale_from, UInt32 scale_to,
-                                       const typename ToDataType::FieldType& 
min_result,
-                                       const typename ToDataType::FieldType& 
max_result) {
+void convert_to_decimals(RealTo* dst, const RealFrom* src, UInt32 scale_from, 
UInt32 scale_to,
+                         const typename ToDataType::FieldType& min_result,
+                         const typename ToDataType::FieldType& max_result, 
size_t size) {
     using FromFieldType = typename FromDataType::FieldType;
     using ToFieldType = typename ToDataType::FieldType;
-    using MaxFieldType =
-            std::conditional_t<(sizeof(FromFieldType) == sizeof(ToFieldType)) 
&&
-                                       (std::is_same_v<ToFieldType, 
Decimal128I> ||
-                                        std::is_same_v<FromFieldType, 
Decimal128I>),
-                               Decimal128I,
-                               std::conditional_t<(sizeof(FromFieldType) > 
sizeof(ToFieldType)),
-                                                  FromFieldType, ToFieldType>>;
+    using MaxFieldType = std::conditional_t<(sizeof(FromFieldType) > 
sizeof(ToFieldType)),
+                                            FromFieldType, ToFieldType>;
 
-    MaxFieldType converted_value;
+    DCHECK_GE(scale_to, scale_from);
     // from integer to decimal
-    if (scale_to > scale_from) {
-        converted_value =
-                DataTypeDecimal<MaxFieldType>::get_scale_multiplier(scale_to - 
scale_from);
+    MaxFieldType multiplier =
+            DataTypeDecimal<MaxFieldType>::get_scale_multiplier(scale_to - 
scale_from);
+    MaxFieldType tmp;
+    for (size_t i = 0; i < size; i++) {
         if constexpr (multiply_may_overflow) {
-            if (common::mul_overflow(static_cast<MaxFieldType>(value).value, 
converted_value.value,
-                                     converted_value.value)) {
+            if (common::mul_overflow(static_cast<MaxFieldType>(src[i]).value, 
multiplier.value,
+                                     tmp.value)) {
                 throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR, 
"Arithmetic overflow");
-            } else {
-                if constexpr (narrow_integral) {
-                    if (UNLIKELY(converted_value.value > max_result.value ||
-                                 converted_value.value < min_result.value)) {
-                        throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR,
-                                        "Arithmetic overflow");
-                    }
-                }
-            }
-        } else {
-            converted_value *= static_cast<MaxFieldType>(value).value;
-            if constexpr (narrow_integral) {
-                if (UNLIKELY(converted_value.value > max_result.value ||
-                             converted_value.value < min_result.value)) {
-                    throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR, 
"Arithmetic overflow");
-                }
             }
-        }
-    } else {
-        // from decimal to integer
-        converted_value =
-                static_cast<MaxFieldType>(value) /
-                DataTypeDecimal<MaxFieldType>::get_scale_multiplier(scale_from 
- scale_to);
-        if (value >= FromFieldType(0)) {
             if constexpr (narrow_integral) {
-                if (UNLIKELY(converted_value.value > max_result.value)) {
-                    throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR, 
"Arithmetic overflow");
+                if (tmp.value < min_result.value || tmp.value > 
max_result.value) {
+                    throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR,
+                                    "Arithmetic overflow, convert failed from 
{}, "
+                                    "expected data is [{}, {}]",
+                                    tmp.value, min_result.value, 
max_result.value);
                 }
             }
+            dst[i].value = tmp.value;
         } else {
-            if constexpr (narrow_integral) {
-                if (UNLIKELY(converted_value.value < min_result.value)) {
-                    throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR, 
"Arithmetic overflow");
-                }
+            dst[i].value = multiplier.value * 
static_cast<MaxFieldType>(src[i]).value;
+        }
+    }
+
+    if constexpr (!multiply_may_overflow && narrow_integral) {
+        for (size_t i = 0; i < size; i++) {
+            if (dst[i].value < min_result.value || dst[i].value > 
max_result.value) {
+                throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR,
+                                "Arithmetic overflow, convert failed from {}, "
+                                "expected data is [{}, {}]",
+                                dst[i].value, min_result.value, 
max_result.value);
             }
         }
     }
+}
 
-    return converted_value;
+// only for casting between other integral types and decimals
+template <typename FromDataType, typename ToDataType, bool narrow_integral, 
typename RealFrom,
+          typename RealTo>
+    requires IsDataTypeDecimal<FromDataType> && IsDataTypeDecimal<ToDataType>
+void convert_from_decimals(RealTo* dst, const RealFrom* src, UInt32 scale_from,
+                           const typename ToDataType::FieldType& min_result,
+                           const typename ToDataType::FieldType& max_result, 
size_t size) {
+    using FromFieldType = typename FromDataType::FieldType;
+    using ToFieldType = typename ToDataType::FieldType;
+    using MaxFieldType = std::conditional_t<(sizeof(FromFieldType) > 
sizeof(ToFieldType)),
+                                            FromFieldType, ToFieldType>;
+
+    // from decimal to integer
+    MaxFieldType multiplier = 
DataTypeDecimal<MaxFieldType>::get_scale_multiplier(scale_from);
+    for (size_t i = 0; i < size; i++) {
+        auto tmp = static_cast<MaxFieldType>(src[i]).value / multiplier.value;
+        if constexpr (narrow_integral) {
+            if (tmp < min_result.value || tmp > max_result.value) {
+                throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR,
+                                "Arithmetic overflow, convert failed from {}, "
+                                "expected data is [{}, {}]",
+                                tmp, min_result.value, max_result.value);
+            }
+        }
+        dst[i] = tmp;
+    }
 }
 
-template <typename FromDataType, typename ToDataType>
+template <typename FromDataType, typename ToDataType, bool 
multiply_may_overflow,
+          bool narrow_integral>
 void convert_decimal_cols(

Review Comment:
   warning: function 'convert_decimal_cols' has cognitive complexity of 66 
(threshold 50) [readability-function-cognitive-complexity]
   ```cpp
   void convert_decimal_cols(
        ^
   ```
   <details>
   <summary>Additional context</summary>
   
   **be/src/vec/data_types/data_type_decimal.h:509:** +1, including nesting 
penalty of 0, nesting level increased to 1
   ```cpp
       if (scale_to > scale_from) {
       ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:513:** +2, including nesting 
penalty of 1, nesting level increased to 2
   ```cpp
           for (size_t i = 0; i < sz; i++) {
           ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:514:** +3, including nesting 
penalty of 2, nesting level increased to 3
   ```cpp
               if constexpr (multiply_may_overflow) {
               ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:515:** +4, including nesting 
penalty of 3, nesting level increased to 4
   ```cpp
                   if 
(common::mul_overflow(static_cast<MaxNativeType>(vec_from[i].value), multiplier,
                   ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:518:** +1, nesting level 
increased to 4
   ```cpp
                   } else {
                     ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:519:** +5, including nesting 
penalty of 4, nesting level increased to 5
   ```cpp
                       if (UNLIKELY(res > max_result.value || res < 
-max_result.value)) {
                       ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:519:** +1
   ```cpp
                       if (UNLIKELY(res > max_result.value || res < 
-max_result.value)) {
                                                           ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:524:** +1, nesting level 
increased to 5
   ```cpp
                       } else {
                         ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:528:** +1, nesting level 
increased to 3
   ```cpp
               } else {
                 ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:530:** +4, including nesting 
penalty of 3, nesting level increased to 4
   ```cpp
                   if constexpr (narrow_integral) {
                   ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:531:** +5, including nesting 
penalty of 4, nesting level increased to 5
   ```cpp
                       if (UNLIKELY(res > max_result.value || res < 
-max_result.value)) {
                       ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:531:** +1
   ```cpp
                       if (UNLIKELY(res > max_result.value || res < 
-max_result.value)) {
                                                           ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:541:** +1, nesting level 
increased to 1
   ```cpp
       } else if (scale_to == scale_from) {
              ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:542:** +2, including nesting 
penalty of 1, nesting level increased to 2
   ```cpp
           for (size_t i = 0; i < sz; i++) {
           ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:543:** +3, including nesting 
penalty of 2, nesting level increased to 3
   ```cpp
               if constexpr (narrow_integral) {
               ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:544:** +4, including nesting 
penalty of 3, nesting level increased to 4
   ```cpp
                   if (UNLIKELY(vec_from[i].value > max_result.value ||
                   ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:554:** +1, nesting level 
increased to 1
   ```cpp
       } else {
         ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:558:** +2, including nesting 
penalty of 1, nesting level increased to 2
   ```cpp
           for (size_t i = 0; i < sz; i++) {
           ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:559:** +3, including nesting 
penalty of 2, nesting level increased to 3
   ```cpp
               if (vec_from[i] >= FromFieldType(0)) {
               ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:560:** +4, including nesting 
penalty of 3, nesting level increased to 4
   ```cpp
                   if constexpr (narrow_integral) {
                   ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:562:** +5, including nesting 
penalty of 4, nesting level increased to 5
   ```cpp
                       if (UNLIKELY(res > max_result.value)) {
                       ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:569:** +1, nesting level 
increased to 4
   ```cpp
                   } else {
                     ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:572:** +1, nesting level 
increased to 3
   ```cpp
               } else {
                 ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:573:** +4, including nesting 
penalty of 3, nesting level increased to 4
   ```cpp
                   if constexpr (narrow_integral) {
                   ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:575:** +5, including nesting 
penalty of 4, nesting level increased to 5
   ```cpp
                       if (UNLIKELY(res < -max_result.value)) {
                       ^
   ```
   **be/src/vec/data_types/data_type_decimal.h:582:** +1, nesting level 
increased to 4
   ```cpp
                   } else {
                     ^
   ```
   
   </details>
   



-- 
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

Reply via email to