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

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


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new 54d4aca38f7 [fix](decimal256) fix casting decimal256 to float (#54140)
54d4aca38f7 is described below

commit 54d4aca38f728cbfb03b4c94540fda48642eca68
Author: TengJianPing <[email protected]>
AuthorDate: Fri Aug 1 11:43:47 2025 +0800

    [fix](decimal256) fix casting decimal256 to float (#54140)
---
 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          |  76 ++++++++++++++++-----
 4 files changed, 66 insertions(+), 18 deletions(-)

diff --git a/be/src/vec/data_types/data_type_decimal.h 
b/be/src/vec/data_types/data_type_decimal.h
index 43557084e52..4050ba699f5 100644
--- a/be/src/vec/data_types/data_type_decimal.h
+++ b/be/src/vec/data_types/data_type_decimal.h
@@ -597,7 +597,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 0132e74010d..64c5d87cac0 100644
--- a/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy
+++ b/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy
@@ -74,42 +74,84 @@ suite("test_decimal256_cast") {
         select k1, cast(v1 as decimalv3(76, 0)) from cast_to_dec256 order by 
k1, v1;
     """
 
+     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;
+    """
+    sql """
+    create table dec256cast_to_float (
+        k1 int,
+        v1 decimalv3(76, 60)
+    ) distributed by hash(k1)
+    properties (
+        'replication_num' = '1'
+    );
+    """
+    sql """
+        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 /*+SET_VAR(enable_fold_constant_by_be = true) 
*/cast(cast("12345678.000000000000000000000000000000001" as decimalv3(76, 60)) 
as float);
-        """
+        sql """select /*+SET_VAR(debug_skip_fold_constant = false) 
*/cast(cast("1000000000000000000000000000000000000000000000000000000000000000000000.111111"
 as decimalv3(76, 6)) 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);
-        """
+        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 """
-        drop table  if exists dec256cast_to_float;
-    """
-    sql """
-    create table dec256cast_to_float (
+    create table dec256cast_to_float_overflow (
         k1 int,
-        v1 decimalv3(76, 60)
+        v1 decimalv3(76, 6)
     ) distributed by hash(k1)
     properties (
         'replication_num' = '1'
     );
     """
     sql """
-        insert into dec256cast_to_float values  (1, 
"12345678.000000000000000000000000000000001");
+        insert into dec256cast_to_float_overflow values  (1, 
"1000000000000000000000000000000000000000000000000000000000000000000000.111111");
     """
     test {
-        sql """
-            select cast(v1 as float) from dec256cast_to_float;
-        """
+        sql "select cast(v1 as float) from dec256cast_to_float_overflow;"
         exception "Arithmetic overflow"
     }
-    qt_decimal256_cast_to_double_1 """
-        select cast(v1 as double) from dec256cast_to_float;
+    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