yiguolei commented on code in PR #27787:
URL: https://github.com/apache/doris/pull/27787#discussion_r1410083831


##########
be/src/vec/data_types/data_type_decimal.h:
##########
@@ -420,34 +427,63 @@ ToDataType::FieldType convert_decimals(const typename 
FromDataType::FieldType& v
                                                   FromFieldType, ToFieldType>>;
 
     MaxFieldType converted_value;
+    // from integer to decimal
     if (scale_to > scale_from) {
+        LOG(WARNING) << "multiply wider scale";
         converted_value =
                 DataTypeDecimal<MaxFieldType>::get_scale_multiplier(scale_to - 
scale_from);
-        if (common::mul_overflow(static_cast<MaxFieldType>(value).value, 
converted_value.value,
-                                 converted_value.value)) {
-            if (overflow_flag) {
-                *overflow_flag = 1;
+        if constexpr (multiply_may_overflow) {
+            LOG(WARNING) << "multiply may overflow";
+            if (common::mul_overflow(static_cast<MaxFieldType>(value).value, 
converted_value.value,
+                                     converted_value.value)) {
+                LOG(WARNING) << "multiply may overflow, multiply overflowed"; 
// ok
+                THROW_ARITHMETIC_OVERFLOW_ERRROR;
+            } else {
+                if constexpr (narrow_integral) {
+                    LOG(WARNING) << "multiply may overflow, narrow integer"; 
// ok
+                    if (UNLIKELY(converted_value.value > max_result.value ||
+                                 converted_value.value < min_result.value)) {
+                        LOG(WARNING) << "multiply may overflow, res overflow"; 
// ok
+                        THROW_ARITHMETIC_OVERFLOW_ERRROR;
+                    }
+                }
             }
-            return converted_value < MaxFieldType() ? 
type_limit<ToFieldType>::min()
-                                                    : 
type_limit<ToFieldType>::max();
+        } else {
+            LOG(WARNING) << "multiply CAN'T overflow"; // ok
+            converted_value *= static_cast<MaxFieldType>(value).value;
+            if constexpr (narrow_integral) {
+                LOG(WARNING) << "multiply CAN'T overflow, narrow integer"; // 
ok
+                if (UNLIKELY(converted_value.value > max_result.value ||
+                             converted_value.value < min_result.value)) {
+                    LOG(WARNING) << "multiply CAN'T overflow, narrow 
integral"; // ok
+                    THROW_ARITHMETIC_OVERFLOW_ERRROR;
+                } // ok
+            }     // ok
         }
     } else {
+        // from decimal to integer
+        LOG(WARNING) << "multiply narrow scale"; // ok
         converted_value =
                 static_cast<MaxFieldType>(value) /
                 DataTypeDecimal<MaxFieldType>::get_scale_multiplier(scale_from 
- scale_to);
-    }
-
-    if constexpr (sizeof(FromFieldType) > sizeof(ToFieldType)) {
-        if (converted_value < FromFieldType(type_limit<ToFieldType>::min())) {
-            if (overflow_flag) {
-                *overflow_flag = 1;
+        if (value >= FromFieldType(0)) {
+            if constexpr (narrow_integral) {
+                LOG(WARNING) << "multiply narrow scale, positive, narrow 
integral"; // ok
+                if (UNLIKELY(converted_value.value > max_result.value)) {
+                    THROW_ARITHMETIC_OVERFLOW_ERRROR; // ok
+                }                                     // ok
+            } else {
+                LOG(WARNING) << "multiply narrow scale, positive, NOT narrow 
integral";
             }
-            return type_limit<ToFieldType>::min();
-        } else if (converted_value > 
FromFieldType(type_limit<ToFieldType>::max())) {
-            if (overflow_flag) {
-                *overflow_flag = 1;
+        } else {
+            if constexpr (narrow_integral) {
+                LOG(WARNING) << "multiply narrow scale, negative, narrow 
integral";
+                if (UNLIKELY(converted_value.value < min_result.value)) {
+                    THROW_ARITHMETIC_OVERFLOW_ERRROR;

Review Comment:
   throw exception  is better than  macro



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