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 6539c8fd35d [fix](decimal) throw overflow exception if result is NaN 
of Infinit when converting from decimal to float (#40290) (#41007)
6539c8fd35d is described below

commit 6539c8fd35da4647e5461417198ce45f8201387c
Author: TengJianPing <18241664+jackte...@users.noreply.github.com>
AuthorDate: Fri Sep 20 14:05:51 2024 +0800

    [fix](decimal) throw overflow exception if result is NaN of Infinit when 
converting from decimal to float (#40290) (#41007)
    
    ## Proposed changes
    
    Issue Number: close #xxx
    
    BP #40290
---
 be/src/vec/data_types/data_type_decimal.h          |  5 +-
 .../datatype_p0/decimalv3/test_decimal256_cast.out | 10 +++
 .../decimalv3/test_decimal256_cast.groovy          | 71 ++++++++++++++++++++++
 3 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/be/src/vec/data_types/data_type_decimal.h 
b/be/src/vec/data_types/data_type_decimal.h
index 8b12010e159..2acae5800d0 100644
--- a/be/src/vec/data_types/data_type_decimal.h
+++ b/be/src/vec/data_types/data_type_decimal.h
@@ -642,10 +642,11 @@ void convert_from_decimal(typename ToDataType::FieldType* 
dst,
                 dst[i] = static_cast<ToFieldType>(src[i].value) / 
multiplier.value;
             }
         }
-        FromDataType from_data_type(precision, scale);
         if constexpr (narrow_integral) {
+            FromDataType from_data_type(precision, scale);
             for (size_t i = 0; i < size; i++) {
-                if (dst[i] < min_result || dst[i] > max_result) {
+                if (std::isnan(dst[i]) || std::isinf(dst[i]) || dst[i] < 
min_result ||
+                    dst[i] > max_result) {
                     
THROW_DECIMAL_CONVERT_OVERFLOW_EXCEPTION(from_data_type.to_string(src[i]),
                                                              
from_data_type.get_name(),
                                                              ToDataType 
{}.get_name());
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 0d79c060690..ac1f3da16dd 100644
--- a/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out
+++ b/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out
@@ -26,3 +26,13 @@
 -- !decimal256_cast8 --
 0
 
+-- !decimal256_cast9 --
+-9     
-999999999999999999999999999999999999999999999999999999999999999999.9999999999
+9      
999999999999999999999999999999999999999999999999999999999999999999.9999999999
+
+-- !decimal256_cast10 --
+10     0
+
+-- !decimal256_cast_to_double_1 --
+1.2345678E7
+
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 ea3001232f1..3ddfbec3975 100644
--- a/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy
+++ b/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy
@@ -18,6 +18,9 @@
 suite("test_decimal256_cast") {
     sql "set enable_nereids_planner = true;"
     sql "set enable_decimal256 = 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));"""
@@ -41,4 +44,72 @@ suite("test_decimal256_cast") {
         select 
cast('0.000000000000000000000000000000000000000000000000000000000000000000000012345678901'
 as decimalv3(76,0));
     """
 
+    sql """
+        drop table  if exists cast_to_dec256;
+    """
+    sql """
+    create table cast_to_dec256 (
+        k1 int,
+        v1 varchar(128)
+    ) distributed by hash(k1)
+    properties (
+        'replication_num' = '1'
+    );
+    """
+    sql """
+        insert into cast_to_dec256  values(9, 
"999999999999999999999999999999999999999999999999999999999999999999.9999999999"),
+            (-9, 
"-999999999999999999999999999999999999999999999999999999999999999999.9999999999");
+    """
+    qt_decimal256_cast9 """
+        select k1, cast(v1 as decimalv3(76,10)) from cast_to_dec256 order by 
k1, v1;
+    """
+
+    sql """
+        truncate table cast_to_dec256;
+    """
+    sql """
+        insert into cast_to_dec256  values(10, 
"0.000000000000000000000000000000000000000000000000000000000000000000000012345678901");
+    """
+    qt_decimal256_cast10 """
+        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"
+    // }
+
+    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");
+    """
+    test {
+        sql """
+            select cast(v1 as float) from dec256cast_to_float;
+        """
+        exception "Arithmetic overflow"
+    }
+    qt_decimal256_cast_to_double_1 """
+        select cast(v1 as double) from dec256cast_to_float;
+    """
+
 }
\ No newline at end of file


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

Reply via email to