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

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


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new 1547d635bbb [fix](decimal256) fix casting decimal256 to float (#54178)
1547d635bbb is described below

commit 1547d635bbb706d0160a0cdc71ab905b06a21e4a
Author: TengJianPing <[email protected]>
AuthorDate: Fri Aug 1 19:37:26 2025 +0800

    [fix](decimal256) fix casting decimal256 to float (#54178)
---
 be/src/vec/data_types/data_type_decimal.h          |   8 +-
 .../datatype_p0/decimalv3/test_decimal256_cast.out | Bin 1216 -> 1433 bytes
 .../datatype_p0/decimalv3/test_decimalv3_cast4.out | Bin 380 -> 368 bytes
 .../decimalv3/test_decimal256_cast.groovy          |  84 +++++++++++++++------
 4 files changed, 70 insertions(+), 22 deletions(-)

diff --git a/be/src/vec/data_types/data_type_decimal.h 
b/be/src/vec/data_types/data_type_decimal.h
index 4880b2ceb86..12c7975e057 100644
--- a/be/src/vec/data_types/data_type_decimal.h
+++ b/be/src/vec/data_types/data_type_decimal.h
@@ -645,7 +645,13 @@ void convert_from_decimal(typename ToDataType::FieldType* 
dst,
         } else {
             auto multiplier = FromDataType::get_scale_multiplier(scale);
             for (size_t i = 0; i < size; ++i) {
-                dst[i] = static_cast<ToFieldType>(src[i].value) / 
multiplier.value;
+                if constexpr (IsDecimal256<FromFieldType>) {
+                    dst[i] = static_cast<ToFieldType>(static_cast<long 
double>(src[i].value) /
+                                                      static_cast<long 
double>(multiplier.value));
+                } else {
+                    dst[i] = 
static_cast<ToFieldType>(static_cast<double>(src[i].value) /
+                                                      
static_cast<double>(multiplier.value));
+                }
             }
         }
         if constexpr (narrow_integral) {
diff --git 
a/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out 
b/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out
index ac1f3da16dd..3ba2863a6fd 100644
Binary files 
a/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out and 
b/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out differ
diff --git 
a/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast4.out 
b/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast4.out
index 657ca307204..49e526cd663 100644
Binary files 
a/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast4.out and 
b/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast4.out differ
diff --git 
a/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy 
b/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy
index 3ddfbec3975..64c5d87cac0 100644
--- a/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy
+++ b/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy
@@ -18,9 +18,9 @@
 suite("test_decimal256_cast") {
     sql "set enable_nereids_planner = true;"
     sql "set enable_decimal256 = true;"
-    // sql """
-    //     set debug_skip_fold_constant=true;
-    // """
+    sql """
+        set debug_skip_fold_constant=true;
+    """
 
     qt_decimal256_cast0 """SELECT /*+ SET_VAR(enable_fold_constant_by_be = 
false) */
         
cast(999999999999999999999999999999999999999999999999999999999999999999.9999999999
 as decimalv3(76,10));"""
@@ -74,18 +74,12 @@ suite("test_decimal256_cast") {
         select k1, cast(v1 as decimalv3(76, 0)) from cast_to_dec256 order by 
k1, v1;
     """
 
-    // test {
-    //     sql """
-    //         select /*+SET_VAR(enable_fold_constant_by_be = true) 
*/cast(cast("12345678.000000000000000000000000000000001" as decimalv3(76, 60)) 
as float);
-    //     """
-    //     exception "Arithmetic overflow"
-    // }
-    // test {
-    //     sql """
-    //         select /*+SET_VAR(enable_fold_constant_by_be = false) 
*/cast(cast("12345678.000000000000000000000000000000001" as decimalv3(76, 60)) 
as float);
-    //     """
-    //     exception "Arithmetic overflow"
-    // }
+     qt_decimal256_cast_to_float1 """
+         select /*+SET_VAR(enable_fold_constant_by_be = true) 
*/cast(cast("12345678.000000000000000000000000000000001" as decimalv3(76, 60)) 
as float);
+     """
+     qt_decimal256_cast_to_float2 """
+         select /*+SET_VAR(enable_fold_constant_by_be = false) 
*/cast(cast("12345678.000000000000000000000000000000001" as decimalv3(76, 60)) 
as float);
+     """
 
     sql """
         drop table  if exists dec256cast_to_float;
@@ -100,16 +94,64 @@ suite("test_decimal256_cast") {
     );
     """
     sql """
-        insert into dec256cast_to_float values  (1, 
"12345678.000000000000000000000000000000001");
+        insert into dec256cast_to_float values 
+            (1, "12345678.000000000000000000000000000000001"),
+            (2, 
"9999999999999999.999999999999999999999999999999999999999999999999999999999999"),
+            (3, 
"0.000000250794773934844880991039000000000000000000000000000000"),
+            (4, 
"0.999999999999999999999999999999999999999999999999999999999999");
+    """
+    
+    qt_decimal256_cast_to_float3 """
+        select k1, cast(v1 as float) from dec256cast_to_float order by 1;
+    """
+    qt_decimal256_cast_to_double_1 """
+        select k1, cast(v1 as double) from dec256cast_to_float order by 1;
     """
+
     test {
-        sql """
-            select cast(v1 as float) from dec256cast_to_float;
-        """
+        sql """select /*+SET_VAR(debug_skip_fold_constant = false) 
*/cast(cast("1000000000000000000000000000000000000000000000000000000000000000000000.111111"
 as decimalv3(76, 6)) as float);"""
         exception "Arithmetic overflow"
     }
-    qt_decimal256_cast_to_double_1 """
-        select cast(v1 as double) from dec256cast_to_float;
+    test {
+        sql """select /*+SET_VAR(debug_skip_fold_constant = true) 
*/cast(cast("1000000000000000000000000000000000000000000000000000000000000000000000.111111"
 as decimalv3(76, 6)) as float);"""
+        exception "Arithmetic overflow"
+    }
+
+    test {
+        sql """select /*+SET_VAR(debug_skip_fold_constant = false) 
*/cast(cast("9999999999999999999999999999999999999999999999999999999999999999999999.999999"
 as decimalv3(76, 6)) as float);"""
+        exception "Arithmetic overflow"
+    }
+    test {
+        sql """select /*+SET_VAR(debug_skip_fold_constant = true) 
*/cast(cast("9999999999999999999999999999999999999999999999999999999999999999999999.999999"
 as decimalv3(76, 6)) as float);"""
+        exception "Arithmetic overflow"
+    }
+
+    sql "drop table if exists dec256cast_to_float_overflow"
+    sql """
+    create table dec256cast_to_float_overflow (
+        k1 int,
+        v1 decimalv3(76, 6)
+    ) distributed by hash(k1)
+    properties (
+        'replication_num' = '1'
+    );
+    """
+    sql """
+        insert into dec256cast_to_float_overflow values  (1, 
"1000000000000000000000000000000000000000000000000000000000000000000000.111111");
+    """
+    test {
+        sql "select cast(v1 as float) from dec256cast_to_float_overflow;"
+        exception "Arithmetic overflow"
+    }
+    sql """
+        truncate table dec256cast_to_float_overflow;
     """
+    sql """
+        insert into dec256cast_to_float_overflow values  (1, 
"9999999999999999999999999999999999999999999999999999999999999999999999.999999");
+    """
+    test {
+        sql "select cast(v1 as float) from dec256cast_to_float_overflow;"
+        exception "Arithmetic overflow"
+    }
 
 }
\ No newline at end of file


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

Reply via email to