zhangstar333 commented on code in PR #32746:
URL: https://github.com/apache/doris/pull/32746#discussion_r1543990644


##########
be/src/vec/functions/round.h:
##########
@@ -446,6 +479,191 @@ struct Dispatcher {
             return nullptr;
         }
     }
+
+    // NOTE: This function is only tested for truncate
+    // DO NOT USE THIS METHOD FOR OTHER ROUNDING BASED FUNCTION UNTIL YOU KNOW 
EXACTLY WHAT YOU ARE DOING !!!
+    static ColumnPtr apply_vec_vec(const IColumn* col_general, const IColumn* 
col_scale) {
+        if constexpr (rounding_mode != RoundingMode::Trunc) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Using column as scale is only supported 
for function truncate");
+        }
+
+        const ColumnInt32& col_scale_i32 = assert_cast<const 
ColumnInt32&>(*col_scale);
+
+        if constexpr (IsNumber<T>) {
+            const auto* col = assert_cast<const ColumnVector<T>*>(col_general);
+            const size_t input_row_count = col->size();
+            auto col_res = ColumnVector<T>::create();
+
+            typename ColumnVector<T>::Container& vec_res = col_res->get_data();
+            vec_res.resize(input_row_count);
+
+            for (size_t i = 0; i < input_row_count; ++i) {
+                const Int32 scale_arg = col_scale_i32.get_data()[i];
+                if (scale_arg > std::numeric_limits<Int16>::max() ||
+                    scale_arg < std::numeric_limits<Int16>::min()) {
+                    throw doris::Exception(ErrorCode::OUT_OF_BOUND,
+                                           "Scale argument for function is out 
of bound: {}",
+                                           scale_arg);
+                }
+
+                if (scale_arg == 0) {
+                    size_t scale = 1;
+                    
FunctionRoundingImpl<ScaleMode::Zero>::apply(col->get_data()[i], scale,
+                                                                 vec_res[i]);
+                } else if (scale_arg > 0) {
+                    size_t scale = int_exp10(col_scale_i32.get_data()[i]);

Review Comment:
   col_scale_i32.get_data()[i] seems no need get again, could use scale_arg 
directly?



##########
be/src/vec/functions/round.h:
##########
@@ -446,6 +479,191 @@ struct Dispatcher {
             return nullptr;
         }
     }
+
+    // NOTE: This function is only tested for truncate
+    // DO NOT USE THIS METHOD FOR OTHER ROUNDING BASED FUNCTION UNTIL YOU KNOW 
EXACTLY WHAT YOU ARE DOING !!!
+    static ColumnPtr apply_vec_vec(const IColumn* col_general, const IColumn* 
col_scale) {
+        if constexpr (rounding_mode != RoundingMode::Trunc) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Using column as scale is only supported 
for function truncate");
+        }
+
+        const ColumnInt32& col_scale_i32 = assert_cast<const 
ColumnInt32&>(*col_scale);
+
+        if constexpr (IsNumber<T>) {
+            const auto* col = assert_cast<const ColumnVector<T>*>(col_general);
+            const size_t input_row_count = col->size();
+            auto col_res = ColumnVector<T>::create();
+
+            typename ColumnVector<T>::Container& vec_res = col_res->get_data();
+            vec_res.resize(input_row_count);
+
+            for (size_t i = 0; i < input_row_count; ++i) {
+                const Int32 scale_arg = col_scale_i32.get_data()[i];
+                if (scale_arg > std::numeric_limits<Int16>::max() ||
+                    scale_arg < std::numeric_limits<Int16>::min()) {
+                    throw doris::Exception(ErrorCode::OUT_OF_BOUND,
+                                           "Scale argument for function is out 
of bound: {}",
+                                           scale_arg);
+                }
+
+                if (scale_arg == 0) {
+                    size_t scale = 1;
+                    
FunctionRoundingImpl<ScaleMode::Zero>::apply(col->get_data()[i], scale,
+                                                                 vec_res[i]);
+                } else if (scale_arg > 0) {
+                    size_t scale = int_exp10(col_scale_i32.get_data()[i]);
+                    
FunctionRoundingImpl<ScaleMode::Positive>::apply(col->get_data()[i], scale,
+                                                                     
vec_res[i]);
+                } else {
+                    size_t scale = int_exp10(-col_scale_i32.get_data()[i]);
+                    
FunctionRoundingImpl<ScaleMode::Negative>::apply(col->get_data()[i], scale,
+                                                                     
vec_res[i]);
+                }
+            }
+            return col_res;
+        } else if constexpr (IsDecimalNumber<T>) {
+            const auto* decimal_col = assert_cast<const 
ColumnDecimal<T>*>(col_general);
+            const auto& vec_src = decimal_col->get_data();
+            const size_t input_row_count = vec_src.size();
+
+            for (size_t i = 0; i < input_row_count; ++i) {
+                const Int32 scale_arg = col_scale_i32.get_data()[i];
+
+                if (scale_arg > std::numeric_limits<Int16>::max() ||
+                    scale_arg < std::numeric_limits<Int16>::min()) {
+                    throw doris::Exception(ErrorCode::OUT_OF_BOUND,
+                                           "Scale argument for function is out 
of bound: {}",
+                                           scale_arg);
+                }
+            }
+
+            // For truncate, ALWAYS use SAME scale with source Decimal column
+            const Int32 input_scale = decimal_col->get_scale();
+            auto col_res = ColumnDecimal<T>::create(input_row_count, 
input_scale);
+
+            for (size_t i = 0; i < input_row_count; ++i) {
+                DecimalRoundingImpl<T, rounding_mode, 
tie_breaking_mode>::apply(
+                        decimal_col->get_element(i).value, input_scale,
+                        col_res->get_element(i).value, 
col_scale_i32.get_data()[i]);
+            }
+
+            for (size_t i = 0; i < input_row_count; ++i) {
+                // For truncate(ColumnDecimal, ColumnInt32), we should always 
have same scale with source Decimal column
+                // So we need this check to make sure the result have correct 
digits count
+                //
+                // Case 0: scale_arg <= -(integer part digits count)
+                //      do nothing, because result is 0
+                // Case 1: scale_arg <= 0 && scale_arg > -(integer part digits 
count)
+                //      decimal parts has been erased, so add them back by 
multiply 10^(scale_arg)
+                // Case 2: scale_arg > 0 && scale_arg < decimal part digits 
count
+                //      decimal part now has scale_arg digits, so multiply 
10^(input_scale - scal_arg)
+                // Case 3: scale_arg >= input_scale
+                //      do nothing
+                const Int32 scale_arg = col_scale_i32.get_data()[i];
+                if (scale_arg <= 0) {
+                    col_res->get_element(i).value *= int_exp10(input_scale);
+                } else if (scale_arg > 0 && scale_arg < input_scale) {
+                    col_res->get_element(i).value *= int_exp10(input_scale - 
scale_arg);
+                }
+            }
+
+            return col_res;
+        } else {
+            LOG(FATAL) << "__builtin_unreachable";
+            __builtin_unreachable();
+            return nullptr;
+        }
+    }
+
+    // NOTE: This function is only tested for truncate
+    // DO NOT USE THIS METHOD FOR OTHER ROUNDING BASED FUNCTION UNTIL YOU KNOW 
EXACTLY WHAT YOU ARE DOING !!! only test for truncate
+    static ColumnPtr apply_const_vec(const ColumnConst* const_col_general,
+                                     const IColumn* col_scale) {
+        if constexpr (rounding_mode != RoundingMode::Trunc) {
+            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
+                                   "Using column as scale is only supported 
for function truncate");
+        }
+
+        const ColumnInt32& col_scale_i32 = assert_cast<const 
ColumnInt32&>(*col_scale);
+
+        if constexpr (IsDecimalNumber<T>) {
+            const ColumnDecimal<T>& data_col_general =
+                    assert_cast<const 
ColumnDecimal<T>&>(const_col_general->get_data_column());
+            const T& general_val = data_col_general.get_data()[0];
+            Int32 input_scale = data_col_general.get_scale();
+            const size_t input_rows_count = col_scale->size();
+            auto col_res = ColumnDecimal<T>::create(input_rows_count, 
input_scale);
+
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                const Int32 scale_arg = col_scale_i32.get_data()[i];
+
+                if (scale_arg > std::numeric_limits<Int16>::max() ||
+                    scale_arg < std::numeric_limits<Int16>::min()) {
+                    throw doris::Exception(ErrorCode::OUT_OF_BOUND,
+                                           "Scale argument for function is out 
of bound: {}",
+                                           scale_arg);
+                }
+            }
+
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                DecimalRoundingImpl<T, rounding_mode, 
tie_breaking_mode>::apply(
+                        general_val, input_scale, 
col_res->get_element(i).value,
+                        col_scale_i32.get_data()[i]);
+            }
+
+            for (size_t i = 0; i < input_rows_count; ++i) {
+                // For truncate(ColumnDecimal, ColumnInt32), we should always 
have same scale with source Decimal column
+                // So we need this check to make sure the result have correct 
digits count
+                //
+                // Case 0: scale_arg <= -(integer part digits count)
+                //      do nothing, because result is 0
+                // Case 1: scale_arg <= 0 && scale_arg > -(integer part digits 
count)
+                //      decimal parts has been erased, so add them back by 
multiply 10^(scale_arg)
+                // Case 2: scale_arg > 0 && scale_arg < decimal part digits 
count
+                //      decimal part now has scale_arg digits, so multiply 
10^(input_scale - scal_arg)
+                // Case 3: scale_arg >= input_scale
+                //      do nothing
+                const Int32 scale_arg = col_scale_i32.get_data()[i];
+                if (scale_arg <= 0) {
+                    col_res->get_element(i).value *= int_exp10(input_scale);
+                } else if (scale_arg > 0 && scale_arg < input_scale) {
+                    col_res->get_element(i).value *= int_exp10(input_scale - 
scale_arg);
+                }
+            }
+
+            return col_res;
+        } else if constexpr (IsNumber<T>) {
+            const ColumnVector<T>& data_col_general =
+                    assert_cast<const 
ColumnVector<T>&>(const_col_general->get_data_column());
+            T general_val = data_col_general.get_data()[0];
+            const size_t intput_rows_cound = col_scale->size();
+            auto col_res = ColumnVector<T>::create(intput_rows_cound);
+            typename ColumnVector<T>::Container& vec_res = col_res->get_data();
+
+            for (size_t i = 0; i < intput_rows_cound; ++i) {
+                const Int16 scale_arg = col_scale_i32.get_data()[i];

Review Comment:
   in apply_vec_vec function, have check all data of scale_arg in [min, max]
   here seems not have this check, it's no need or in other function?



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

Reply via email to