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

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


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 33b017c331f [fix](nereids)window expression's window frame may lost in 
NormalizeToSlot (#30482)
33b017c331f is described below

commit 33b017c331f477985b5ced35a7b0255e08d0efc2
Author: starocean999 <[email protected]>
AuthorDate: Tue Jan 30 17:33:28 2024 +0800

    [fix](nereids)window expression's window frame may lost in NormalizeToSlot 
(#30482)
    
    * [fix](nereids)window expression's window frame may lost in NormalizeToSlot
    
    * [fix](planner)avg function may use wrong decimal precision and scale
---
 .../main/java/org/apache/doris/analysis/FunctionCallExpr.java  | 10 ++++++++--
 .../apache/doris/nereids/rules/rewrite/NormalizeToSlot.java    |  3 +++
 regression-test/data/correctness_p0/test_avg.out               |  3 +++
 regression-test/suites/correctness_p0/test_avg.groovy          |  3 +++
 .../suites/nereids_p0/aggregate/agg_window_project.groovy      |  5 +++++
 5 files changed, 22 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 68de4c7d6d6..b547fb5e308 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
@@ -960,8 +960,14 @@ 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 (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/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/NormalizeToSlot.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/NormalizeToSlot.java
index 41f384ac776..9b32ff42cea 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/NormalizeToSlot.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/NormalizeToSlot.java
@@ -182,6 +182,9 @@ public interface NormalizeToSlot {
                 }
                 newChildren.add(newChild);
             }
+            if (windowExpression.getWindowFrame().isPresent()) {
+                newChildren.add(windowExpression.getWindowFrame().get());
+            }
             return hasNewChildren ? windowExpression.withChildren(newChildren) 
: windowExpression;
         }
     }
diff --git a/regression-test/data/correctness_p0/test_avg.out 
b/regression-test/data/correctness_p0/test_avg.out
index 4cfab69a28b..225ed688e85 100644
--- a/regression-test/data/correctness_p0/test_avg.out
+++ b/regression-test/data/correctness_p0/test_avg.out
@@ -14,3 +14,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 20e53486340..8772ed591e0 100644
--- a/regression-test/suites/correctness_p0/test_avg.groovy
+++ b/regression-test/suites/correctness_p0/test_avg.groovy
@@ -73,4 +73,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;"""
 }
diff --git 
a/regression-test/suites/nereids_p0/aggregate/agg_window_project.groovy 
b/regression-test/suites/nereids_p0/aggregate/agg_window_project.groovy
index e9e0659e220..b75f46b1a06 100644
--- a/regression-test/suites/nereids_p0/aggregate/agg_window_project.groovy
+++ b/regression-test/suites/nereids_p0/aggregate/agg_window_project.groovy
@@ -96,5 +96,10 @@ suite("agg_window_project") {
 
     order_qt_select4 """select a, c, sum(sum(b)) over(partition by c order by 
c rows between unbounded preceding and current row) from test_window_table2 
group by a, c having a > 1;"""
 
+    explain {
+        sql("select a, c, sum(sum(b)) over(partition by c order by c rows 
between unbounded preceding and current row) from test_window_table2 group by 
a, c having a > 1;")
+        contains "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW"
+    }
+
     sql "DROP TABLE IF EXISTS test_window_table2;"
 }


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

Reply via email to