You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by pa...@apache.org on 2022/10/04 08:07:29 UTC

[doris] branch master updated: [Enhancement](optimize) optimize for function multiply on decimalv2 (#13049)

This is an automated email from the ASF dual-hosted git repository.

panxiaolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new db89b0b703 [Enhancement](optimize) optimize for function multiply on decimalv2 (#13049)
db89b0b703 is described below

commit db89b0b7033d8224ee3f3dc37da02a9fc84020a5
Author: Pxl <px...@qq.com>
AuthorDate: Tue Oct 4 16:07:18 2022 +0800

    [Enhancement](optimize) optimize for function multiply on decimalv2 (#13049)
    
    optimize for function multiply on decimalv2
---
 be/src/vec/functions/function_binary_arithmetic.h | 13 +++++++++---
 be/src/vec/functions/multiply.cpp                 | 24 ++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/be/src/vec/functions/function_binary_arithmetic.h b/be/src/vec/functions/function_binary_arithmetic.h
index 6f07fa9f73..34b7851403 100644
--- a/be/src/vec/functions/function_binary_arithmetic.h
+++ b/be/src/vec/functions/function_binary_arithmetic.h
@@ -20,6 +20,9 @@
 
 #pragma once
 
+#include <type_traits>
+
+#include "runtime/decimalv2_value.h"
 #include "vec/columns/column_const.h"
 #include "vec/columns/column_decimal.h"
 #include "vec/columns/column_nullable.h"
@@ -241,9 +244,13 @@ struct DecimalBinaryOperation {
             }
         }
 
-        /// default: use it if no return before
-        for (size_t i = 0; i < size; ++i) {
-            c[i] = apply(a[i], b[i]);
+        if constexpr (OpTraits::is_multiply && std::is_same_v<A, Decimal128> &&
+                      std::is_same_v<B, Decimal128>) {
+            Op::vector_vector(a, b, c);
+        } else {
+            for (size_t i = 0; i < size; i++) {
+                c[i] = apply(a[i], b[i]);
+            }
         }
     }
 
diff --git a/be/src/vec/functions/multiply.cpp b/be/src/vec/functions/multiply.cpp
index 95475ed847..da20805861 100644
--- a/be/src/vec/functions/multiply.cpp
+++ b/be/src/vec/functions/multiply.cpp
@@ -18,6 +18,8 @@
 // https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/Multiply.cpp
 // and modified by Doris
 
+#include "runtime/decimalv2_value.h"
+#include "vec/columns/column_decimal.h"
 #include "vec/common/arithmetic_overflow.h"
 #include "vec/functions/function_binary_arithmetic.h"
 #include "vec/functions/simple_function_factory.h"
@@ -35,9 +37,29 @@ struct MultiplyImpl {
     }
 
     template <typename Result = DecimalV2Value>
-    static inline DecimalV2Value apply(DecimalV2Value a, DecimalV2Value b) {
+    static inline DecimalV2Value apply(const DecimalV2Value& a, const DecimalV2Value& b) {
         return a * b;
     }
+
+    static void vector_vector(const ColumnDecimal128::Container& a,
+                              const ColumnDecimal128::Container& b,
+                              ColumnDecimal128::Container& c) {
+        size_t size = c.size();
+        int8 sgn[size];
+
+        for (int i = 0; i < size; i++) {
+            sgn[i] = ((DecimalV2Value(a[i]).value() >= 0) == (DecimalV2Value(b[i]).value() >= 0))
+                             ? 1
+                             : -1;
+        }
+
+        for (int i = 0; i < size; i++) {
+            c[i] = (DecimalV2Value(a[i]).value() * DecimalV2Value(b[i]).value() - sgn[i]) /
+                           DecimalV2Value::ONE_BILLION +
+                   sgn[i];
+        }
+    }
+
     /// Apply operation and check overflow. It's used for Deciamal operations. @returns true if overflowed, false otherwise.
     template <typename Result = ResultType>
     static inline bool apply(A a, B b, Result& c) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org