You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2023/06/12 10:47:13 UTC

[doris] branch branch-1.2-lts updated: [fix](decimalv3) fix result error when cast a round decimalv3 to double (#20688)

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

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


The following commit(s) were added to refs/heads/branch-1.2-lts by this push:
     new 9ddb080ed6 [fix](decimalv3) fix result error when cast a round decimalv3 to double (#20688)
9ddb080ed6 is described below

commit 9ddb080ed6d074dcdebc356eb5ee4c65da343a84
Author: dujl <du...@bytedance.com>
AuthorDate: Mon Jun 12 18:47:05 2023 +0800

    [fix](decimalv3) fix result error when cast a round decimalv3 to double (#20688)
    
    in round function, when the src data is decimalv3 and the scale parameter is large than src data's scale, the round result scale will be set to source data's scale, but it use scale parameter to create the result column. This will result in the round result is wrong.
    
    CREATE TABLE `tb_round_decimal` (
              `id` int NOT NULL COMMENT '',
              `d1` decimalv3(9, 4) NULL COMMENT '',
              `d2` decimalv3(27, 4)  NULL DEFAULT "0" ,
              `d3` decimalv3(38, 4)  NULL
            ) ENGINE=OLAP
            UNIQUE KEY(`id`)
            DISTRIBUTED BY HASH(`id`) BUCKETS 10
            PROPERTIES (
            "replication_allocation" = "tag.location.default: 1"
            );
    
    
    insert into tb_round_decimal values (1, 123.56789, 234.67895, 345.78956);
    insert into tb_round_decimal values (2, 123.56789, 234.67895, 345.78956);
    
    select cast(round(sum(d1), 6) as double), cast(round(sum(d2), 6) as double), cast(roun
---
 be/src/vec/functions/round.h                       |  6 ++++-
 .../sql_functions/math_functions/test_round.out    | 23 ++++++++++++++++++
 .../sql_functions/math_functions/test_round.groovy | 28 ++++++++++++++++++++++
 3 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/be/src/vec/functions/round.h b/be/src/vec/functions/round.h
index 6d24166736..298d956c12 100644
--- a/be/src/vec/functions/round.h
+++ b/be/src/vec/functions/round.h
@@ -25,6 +25,7 @@
 #else
 #include <fenv.h>
 #endif
+#include <algorithm>
 
 #include "vec/columns/column.h"
 #include "vec/columns/column_decimal.h"
@@ -457,7 +458,10 @@ struct Dispatcher {
             const auto* const decimal_col = check_and_get_column<ColumnDecimal<T>>(col_general);
             const auto& vec_src = decimal_col->get_data();
 
-            auto col_res = ColumnDecimal<T>::create(vec_src.size(), scale_arg);
+            UInt32 result_scale =
+                    std::min(static_cast<UInt32>(std::max(scale_arg, static_cast<Int16>(0))),
+                             decimal_col->get_scale());
+            auto col_res = ColumnDecimal<T>::create(vec_src.size(), result_scale);
             auto& vec_res = col_res->get_data();
 
             if (!vec_res.empty()) {
diff --git a/regression-test/data/query_p0/sql_functions/math_functions/test_round.out b/regression-test/data/query_p0/sql_functions/math_functions/test_round.out
index d82966882d..c3dfffa26c 100644
--- a/regression-test/data/query_p0/sql_functions/math_functions/test_round.out
+++ b/regression-test/data/query_p0/sql_functions/math_functions/test_round.out
@@ -46,3 +46,26 @@
 -- !query --
 111	001	15.0700	0.2300
 
+-- !query --
+247.1356	469.3578	691.579
+
+-- !query --
+247.14	469.36	691.58
+
+-- !query --
+200	500	700
+
+-- !query --
+0	0	0
+
+-- !query --
+247.135	469.357	691.579
+
+-- !query --
+247.140	469.360	691.580
+
+-- !query --
+200.000	500.000	700.000
+
+-- !query --
+0.000	0.000	0.000
\ No newline at end of file
diff --git a/regression-test/suites/query_p0/sql_functions/math_functions/test_round.groovy b/regression-test/suites/query_p0/sql_functions/math_functions/test_round.groovy
index e8db7cc4c6..86fc8ea6ad 100644
--- a/regression-test/suites/query_p0/sql_functions/math_functions/test_round.groovy
+++ b/regression-test/suites/query_p0/sql_functions/math_functions/test_round.groovy
@@ -88,4 +88,32 @@ suite("test_round") {
     sql """ insert into ${tableName1} values ('111', 1.2432, '001', 0.2341, 12.1234123); """
     sql """ insert into ${tableName2} select  TENANT_ID,PRODENTP_CODE,ROUND((MAX(PURC_CNT)*MAX(PUBONLN_PRC)),2) delv_amt,ROUND(SUM(ORD_SUMAMT),2) from ${tableName1} GROUP BY TENANT_ID,PRODENTP_CODE; """
     qt_query """ select * from ${tableName2} """
+
+
+    def tableName3 = "test_round_decimal"
+    sql """ CREATE TABLE `${tableName3}` (
+          `id` int NOT NULL COMMENT 'id',
+          `d1` decimalv3(9, 4) NULL COMMENT '',
+          `d2` decimalv3(27, 4)  NULL DEFAULT "0" ,
+          `d3` decimalv3(38, 4)  NULL
+        ) ENGINE=OLAP
+        UNIQUE KEY(`id`)
+        DISTRIBUTED BY HASH(`id`) BUCKETS 10
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1"
+        );                """
+
+    sql """ insert into ${tableName3} values (1, 123.56789, 234.67895, 345.78956); """
+    sql """ insert into ${tableName3} values (2, 123.56789, 234.67895, 345.78956); """
+
+    qt_query """ select cast(round(sum(d1), 6) as double), cast(round(sum(d2), 6) as double), cast(round(sum(d3), 6) as double) from ${tableName3} """
+    qt_query """ select cast(round(sum(d1), 2) as double), cast(round(sum(d2), 2) as double), cast(round(sum(d3),2) as double) from ${tableName3} """
+    qt_query """ select cast(round(sum(d1), -2) as double), cast(round(sum(d2), -2) as double), cast(round(sum(d3), -2) as double) from ${tableName3} """
+    qt_query """ select cast(round(sum(d1), -4) as double), cast(round(sum(d2), -4) as double), cast(round(sum(d3), -4) as double) from ${tableName3} """
+
+    qt_query """ select cast(round(sum(d1), 6) as decimalv3(27, 3)), cast(round(sum(d2), 6) as decimalv3(27, 3)), cast(round(sum(d3), 6) as decimalv3(27, 3)) from ${tableName3} """
+    qt_query """ select cast(round(sum(d1), 2) as decimalv3(27, 3)), cast(round(sum(d2), 2) as decimalv3(27, 3)), cast(round(sum(d3),2) as decimalv3(27, 3)) from ${tableName3} """
+    qt_query """ select cast(round(sum(d1), -2) as decimalv3(27, 3)), cast(round(sum(d2), -2) as decimalv3(27, 3)), cast(round(sum(d3), -2) as decimalv3(27, 3)) from ${tableName3} """
+    qt_query """ select cast(round(sum(d1), -4) as decimalv3(27, 3)), cast(round(sum(d2), -4) as decimalv3(27, 3)), cast(round(sum(d3), -4) as decimalv3(27, 3)) from ${tableName3} """
+
 }


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