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

commit 3b85e3de1ba3dc9ed164c1c8158665464060fe08
Author: starocean999 <[email protected]>
AuthorDate: Mon Jan 29 15:26:19 2024 +0800

    [fix](planner)avg function may use wrong decimal precision and scale 
(#30364)
---
 .../java/org/apache/doris/analysis/FunctionCallExpr.java | 16 ++++++++++++++--
 regression-test/data/correctness_p0/test_avg.out         |  3 +++
 regression-test/suites/correctness_p0/test_avg.groovy    |  3 +++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
index cb638482f8c..6d20717f63f 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
@@ -971,8 +971,20 @@ public class FunctionCallExpr extends Expr {
         // DecimalV3 scale lower than DEFAULT_MIN_AVG_DECIMAL128_SCALE should 
do cast
         if (fnName.getFunction().equalsIgnoreCase("avg") && 
arg.type.isDecimalV3()
                 && arg.type.getDecimalDigits() < 
ScalarType.DEFAULT_MIN_AVG_DECIMAL128_SCALE) {
-            Type t = ScalarType.createDecimalType(arg.type.getPrimitiveType(), 
arg.type.getPrecision(),
-                    ScalarType.DEFAULT_MIN_AVG_DECIMAL128_SCALE);
+            int precision = arg.type.getPrecision();
+            int scale = arg.type.getDecimalDigits();
+            precision = precision - scale + 
ScalarType.DEFAULT_MIN_AVG_DECIMAL128_SCALE;
+            scale = ScalarType.DEFAULT_MIN_AVG_DECIMAL128_SCALE;
+            if (SessionVariable.getEnableDecimal256()) {
+                if (precision > ScalarType.MAX_DECIMAL256_PRECISION) {
+                    precision = ScalarType.MAX_DECIMAL256_PRECISION;
+                }
+            } else {
+                if (precision > ScalarType.MAX_DECIMAL128_PRECISION) {
+                    precision = ScalarType.MAX_DECIMAL128_PRECISION;
+                }
+            }
+            Type t = ScalarType.createDecimalType(arg.type.getPrimitiveType(), 
precision, scale);
             Expr e = getChild(0).castTo(t);
             setChild(0, e);
         }
diff --git a/regression-test/data/correctness_p0/test_avg.out 
b/regression-test/data/correctness_p0/test_avg.out
index da5b605234b..baf9d5b4630 100644
--- a/regression-test/data/correctness_p0/test_avg.out
+++ b/regression-test/data/correctness_p0/test_avg.out
@@ -115,3 +115,6 @@
 -- !select3 --
 \N     \N
 
+-- !select4 --
+0.0100
+
diff --git a/regression-test/suites/correctness_p0/test_avg.groovy 
b/regression-test/suites/correctness_p0/test_avg.groovy
index 3fd8e5e9570..bddacb68698 100644
--- a/regression-test/suites/correctness_p0/test_avg.groovy
+++ b/regression-test/suites/correctness_p0/test_avg.groovy
@@ -74,4 +74,7 @@ suite("test_avg") {
     qt_select3 """select avg(distinct k2), avg(distinct cast(k4 as largeint)) 
from avg_test;"""
 
     sql """ drop table if exists avg_test; """
+
+    sql """set enable_nereids_planner=false;"""
+    qt_select4 """SELECT avg(col) from ( SELECT 0.01 col  union all  select 
0.01 col ) t;"""
 }


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

Reply via email to