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

morrysnow 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 7e4cf07678f [fix](Nereids) fold const return type does not matched 
with type coercion (#44022)
7e4cf07678f is described below

commit 7e4cf07678f1d70e5a788676a70a10dabd76a8ca
Author: LiBinfeng <libinf...@selectdb.com>
AuthorDate: Mon Nov 18 11:45:16 2024 +0800

    [fix](Nereids) fold const return type does not matched with type coercion 
(#44022)
    
    Related PR: #40744
    
    when executing floor(1) it would castTo decimalV3(3,0) because it need
    (3,0) to contain it's message.
    But after fold const, it lost precision(3) because decimalV3 literal
    class does not have mechanism to save precision
    Solved: after folding constant, we need to change result type to the
    type we wanted
---
 .../functions/executable/NumericArithmetic.java     | 21 +++++++++++++--------
 .../fold_constant_numeric_arithmatic.groovy         |  9 +++++++++
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java
index a9acfeb2d60..d739c830df2 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java
@@ -704,7 +704,7 @@ public class NumericArithmetic {
      */
     @ExecFunction(name = "round")
     public static Expression round(DecimalV3Literal first) {
-        return castDecimalV3Literal(first.round(0), 
first.getValue().precision());
+        return castDecimalV3Literal(first.round(0), ((DecimalV3Type) 
first.getDataType()).getPrecision());
     }
 
     /**
@@ -712,7 +712,8 @@ public class NumericArithmetic {
      */
     @ExecFunction(name = "round")
     public static Expression round(DecimalV3Literal first, IntegerLiteral 
second) {
-        return castDecimalV3Literal(first.round(second.getValue()), 
first.getValue().precision());
+        return castDecimalV3Literal(first.round(second.getValue()),
+                ((DecimalV3Type) first.getDataType()).getPrecision());
     }
 
     /**
@@ -738,7 +739,7 @@ public class NumericArithmetic {
      */
     @ExecFunction(name = "ceil")
     public static Expression ceil(DecimalV3Literal first) {
-        return castDecimalV3Literal(first.roundCeiling(0), 
first.getValue().precision());
+        return castDecimalV3Literal(first.roundCeiling(0), ((DecimalV3Type) 
first.getDataType()).getPrecision());
     }
 
     /**
@@ -746,7 +747,8 @@ public class NumericArithmetic {
      */
     @ExecFunction(name = "ceil")
     public static Expression ceil(DecimalV3Literal first, IntegerLiteral 
second) {
-        return castDecimalV3Literal(first.roundCeiling(second.getValue()), 
first.getValue().precision());
+        return castDecimalV3Literal(first.roundCeiling(second.getValue()),
+                ((DecimalV3Type) first.getDataType()).getPrecision());
     }
 
     /**
@@ -772,7 +774,7 @@ public class NumericArithmetic {
      */
     @ExecFunction(name = "floor")
     public static Expression floor(DecimalV3Literal first) {
-        return castDecimalV3Literal(first.roundFloor(0), 
first.getValue().precision());
+        return castDecimalV3Literal(first.roundFloor(0), ((DecimalV3Type) 
first.getDataType()).getPrecision());
     }
 
     /**
@@ -780,7 +782,8 @@ public class NumericArithmetic {
      */
     @ExecFunction(name = "floor")
     public static Expression floor(DecimalV3Literal first, IntegerLiteral 
second) {
-        return castDecimalV3Literal(first.roundFloor(second.getValue()), 
first.getValue().precision());
+        return castDecimalV3Literal(first.roundFloor(second.getValue()),
+                ((DecimalV3Type) first.getDataType()).getPrecision());
     }
 
     /**
@@ -1142,9 +1145,11 @@ public class NumericArithmetic {
         if (first.getValue().compareTo(BigDecimal.ZERO) == 0) {
             return first;
         } else if (first.getValue().compareTo(BigDecimal.ZERO) < 0) {
-            return castDecimalV3Literal(first.roundCeiling(second.getValue()), 
first.getValue().precision());
+            return castDecimalV3Literal(first.roundCeiling(second.getValue()),
+                    ((DecimalV3Type) first.getDataType()).getPrecision());
         } else {
-            return castDecimalV3Literal(first.roundFloor(second.getValue()), 
first.getValue().precision());
+            return castDecimalV3Literal(first.roundFloor(second.getValue()),
+                    ((DecimalV3Type) first.getDataType()).getPrecision());
         }
     }
 
diff --git 
a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_numeric_arithmatic.groovy
 
b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_numeric_arithmatic.groovy
index dbfd3fad7bf..14fecc91b35 100644
--- 
a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_numeric_arithmatic.groovy
+++ 
b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_numeric_arithmatic.groovy
@@ -434,4 +434,13 @@ test {
     testFoldConst("with cte as (select round(300.343, -4) order by 1 limit 1) 
select * from cte")
     testFoldConst("with cte as (select ceil(300.343, -4) order by 1 limit 1) 
select * from cte")
     testFoldConst("with cte as (select truncate(300.343, -4) order by 1 limit 
1) select * from cte")
+
+    testFoldConst("with cte as (select floor(3) order by 1 limit 1) select * 
from cte")
+    testFoldConst("with cte as (select round(3) order by 1 limit 1) select * 
from cte")
+    testFoldConst("with cte as (select ceil(3) order by 1 limit 1) select * 
from cte")
+
+    testFoldConst("with cte as (select floor(3, 2) order by 1 limit 1) select 
* from cte")
+    testFoldConst("with cte as (select round(3, 2) order by 1 limit 1) select 
* from cte")
+    testFoldConst("with cte as (select ceil(3, 2) order by 1 limit 1) select * 
from cte")
+    testFoldConst("with cte as (select truncate(3, 2) order by 1 limit 1) 
select * from cte")
 }


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

Reply via email to