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

morrysnow pushed a commit to branch branch-3.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-3.1 by this push:
     new 6a029c91425 branch-3.1: [fix](decimal) fix wrong result of decimal 
divide (#54538)
6a029c91425 is described below

commit 6a029c9142588d9b67238502255a9410bbd7372c
Author: TengJianPing <[email protected]>
AuthorDate: Wed Aug 13 14:27:30 2025 +0800

    branch-3.1: [fix](decimal) fix wrong result of decimal divide (#54538)
    
    ### What problem does this PR solve?
    
    This problem does not exists in master branch, maybe already fixed by
    recent refactoring.
    Fro branch 2.1 #54537
    Fro branch 3.0 #54453
---
 be/src/vec/functions/function_binary_arithmetic.h   |   8 +++++---
 be/src/vec/functions/minus.cpp                      |   2 +-
 be/src/vec/functions/multiply.cpp                   |   2 +-
 be/src/vec/functions/plus.cpp                       |   2 +-
 .../decimalv3/test_arithmetic_expressions.out       | Bin 10770 -> 10834 bytes
 .../decimalv3/test_arithmetic_expressions.groovy    |  10 ++++++++++
 6 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/be/src/vec/functions/function_binary_arithmetic.h 
b/be/src/vec/functions/function_binary_arithmetic.h
index 9f2af326f71..c20d27767cd 100644
--- a/be/src/vec/functions/function_binary_arithmetic.h
+++ b/be/src/vec/functions/function_binary_arithmetic.h
@@ -238,7 +238,9 @@ struct DecimalBinaryOperation {
     using OpTraits = OperationTraits<Operation, A, B>;
 
     using NativeResultType = typename NativeType<ResultType>::Type;
-    using Op = Operation<NativeResultType, NativeResultType>;
+    using NativeLeftType = typename NativeType<A>::Type;
+    using NativeRightType = typename NativeType<B>::Type;
+    using Op = Operation<NativeLeftType, NativeRightType>;
 
     using Traits = NumberTraits::BinaryOperatorTraits<A, B>;
     using ArrayC = typename ColumnDecimal<ResultType>::Container;
@@ -654,11 +656,11 @@ private:
     }
 
     /// null_map for divide and mod
-    static ALWAYS_INLINE NativeResultType apply(NativeResultType a, 
NativeResultType b,
+    static ALWAYS_INLINE NativeResultType apply(const NativeLeftType& a, const 
NativeRightType& b,
                                                 UInt8& is_null,
                                                 const ResultType& 
max_result_number) {
         static_assert(OpTraits::is_division || OpTraits::is_mod);
-        if constexpr (IsDecimalV2<B> || IsDecimalV2<A>) {
+        if constexpr (IsDecimalV2<B> && IsDecimalV2<A>) {
             DecimalV2Value l(a);
             DecimalV2Value r(b);
             auto ans = Op::template apply(l, r, is_null);
diff --git a/be/src/vec/functions/minus.cpp b/be/src/vec/functions/minus.cpp
index 122842b2d9d..b666fffd673 100644
--- a/be/src/vec/functions/minus.cpp
+++ b/be/src/vec/functions/minus.cpp
@@ -46,7 +46,7 @@ struct MinusImpl {
     /// Apply operation and check overflow. It's used for Decimal operations. 
@returns true if overflowed, false otherwise.
     template <typename Result = ResultType>
     static inline bool apply(A a, B b, Result& c) {
-        return common::sub_overflow(static_cast<Result>(a), b, c);
+        return common::sub_overflow(static_cast<Result>(a), 
static_cast<Result>(b), c);
     }
 };
 
diff --git a/be/src/vec/functions/multiply.cpp 
b/be/src/vec/functions/multiply.cpp
index f871a3a742c..458df6ae8d0 100644
--- a/be/src/vec/functions/multiply.cpp
+++ b/be/src/vec/functions/multiply.cpp
@@ -99,7 +99,7 @@ struct MultiplyImpl {
     /// Apply operation and check overflow. It's used for Decimal operations. 
@returns true if overflowed, false otherwise.
     template <typename Result = ResultType>
     static inline bool apply(A a, B b, Result& c) {
-        return common::mul_overflow(static_cast<Result>(a), b, c);
+        return common::mul_overflow(static_cast<Result>(a), 
static_cast<Result>(b), c);
     }
 };
 
diff --git a/be/src/vec/functions/plus.cpp b/be/src/vec/functions/plus.cpp
index 480db82a581..6aeffacb025 100644
--- a/be/src/vec/functions/plus.cpp
+++ b/be/src/vec/functions/plus.cpp
@@ -47,7 +47,7 @@ struct PlusImpl {
     /// Apply operation and check overflow. It's used for Decimal operations. 
@returns true if overflowed, false otherwise.
     template <typename Result = ResultType>
     static inline bool apply(A a, B b, Result& c) {
-        return common::add_overflow(static_cast<Result>(a), b, c);
+        return common::add_overflow(static_cast<Result>(a), 
static_cast<Result>(b), c);
     }
 };
 
diff --git 
a/regression-test/data/datatype_p0/decimalv3/test_arithmetic_expressions.out 
b/regression-test/data/datatype_p0/decimalv3/test_arithmetic_expressions.out
index 7b74bec40d4..3d72fcc9eac 100644
Binary files 
a/regression-test/data/datatype_p0/decimalv3/test_arithmetic_expressions.out 
and 
b/regression-test/data/datatype_p0/decimalv3/test_arithmetic_expressions.out 
differ
diff --git 
a/regression-test/suites/datatype_p0/decimalv3/test_arithmetic_expressions.groovy
 
b/regression-test/suites/datatype_p0/decimalv3/test_arithmetic_expressions.groovy
index 5936cf29b45..cbe52a6b6e9 100644
--- 
a/regression-test/suites/datatype_p0/decimalv3/test_arithmetic_expressions.groovy
+++ 
b/regression-test/suites/datatype_p0/decimalv3/test_arithmetic_expressions.groovy
@@ -455,4 +455,14 @@ mysql [test]>select k3, CAST(k3 AS DECIMALV3(38, 10)) from 
test_arithmetic_expre
 
     qt_decimal256_mod """ select v1, v2, v1 % v2, v1 % v3 from 
test_arithmetic_expressions_256_5 ORDER BY id; """
 
+    // bugfix, divide
+    sql "DROP TABLE IF EXISTS `fix_decimal_divide`"
+    sql """
+        create table fix_decimal_divide(f1 decimalv3(5,2), f2 decimalv3(20,0)) 
properties("replication_num"="1");
+    """
+    sql """
+        insert into fix_decimal_divide values(100.02, 9223372036854775807);
+    """
+    qt_fix_decimal_divide_0 "select f1, f2, f1 / f2 from fix_decimal_divide 
order by 1,2,3;"
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to