You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by kx...@apache.org on 2023/06/20 16:02:57 UTC

[doris] branch master updated: [fix](decimalv3) fix result error when cast a round decimalv3 to double (#20678)

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

kxiao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 0cf9de8cef [fix](decimalv3) fix result error when cast a round decimalv3 to double (#20678)
0cf9de8cef is described below

commit 0cf9de8cefdf74c5fdcfa0693553778a1d7bf4f8
Author: dujl <du...@bytedance.com>
AuthorDate: Wed Jun 21 00:02:48 2023 +0800

    [fix](decimalv3) fix result error when cast a round decimalv3 to double (#20678)
---
 be/src/vec/functions/round.h                       |  6 ++++-
 .../sql_functions/math_functions/test_round.out    | 27 +++++++++++++++++++
 .../sql_functions/math_functions/test_round.groovy | 30 ++++++++++++++++++++++
 3 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/be/src/vec/functions/round.h b/be/src/vec/functions/round.h
index 15565c87c7..1460fc12ec 100644
--- a/be/src/vec/functions/round.h
+++ b/be/src/vec/functions/round.h
@@ -28,6 +28,7 @@
 #else
 #include <fenv.h>
 #endif
+#include <algorithm>
 
 #include "vec/columns/column.h"
 #include "vec/columns/column_decimal.h"
@@ -461,7 +462,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 82d286a5d2..155e438c9e 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
@@ -88,3 +88,30 @@
 -- !query --
 111	001	15.0700	0.2300
 
+-- !query --
+123.5679	234.6790	345.7896
+123.5679	234.6790	345.7896
+
+-- !query --
+247.1358	469.358	691.5792
+
+-- !query --
+247.14	469.36	691.58
+
+-- !query --
+200	500	700
+
+-- !query --
+0	0	0
+
+-- !query --
+247.136	469.358	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 115e8a5b1d..96564e1400 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
@@ -108,4 +108,34 @@
     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 d1, d2, d3 from ${tableName3} order by d1 """
+
+    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