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

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


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new 3f1634c2f44 branch-3.0: [fix](Nereids) fix double literal to string 
literal cast problem #49416 (#49522)
3f1634c2f44 is described below

commit 3f1634c2f448d021c5777e774e547fa3b42c89a9
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Thu Mar 27 19:07:48 2025 +0800

    branch-3.0: [fix](Nereids) fix double literal to string literal cast 
problem #49416 (#49522)
    
    Cherry-picked from #49416
    
    Co-authored-by: LiBinfeng <[email protected]>
---
 .../nereids/trees/expressions/literal/Literal.java | 25 +++++++++++
 .../fold_constant/fold_constant_cast.groovy        | 49 ++++++++++++++++++++++
 2 files changed, 74 insertions(+)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
index bb6bbe62a6c..932bf6ba83d 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
@@ -267,8 +267,20 @@ public abstract class Literal extends Expression 
implements LeafExpression, Comp
                 return new CharLiteral(desc, ((CharType) targetType).getLen());
             }
         } else if (targetType.isVarcharType()) {
+            if (this.dataType.isDoubleType() || this.dataType.isFloatType()) {
+                int pointZeroIndex = findPointZeroIndex(desc);
+                if (pointZeroIndex > -1) {
+                    return new VarcharLiteral(desc.substring(0, 
pointZeroIndex), ((VarcharType) targetType).getLen());
+                }
+            }
             return new VarcharLiteral(desc, ((VarcharType) 
targetType).getLen());
         } else if (targetType instanceof StringType) {
+            if (this.dataType.isDoubleType() || this.dataType.isFloatType()) {
+                int pointZeroIndex = findPointZeroIndex(desc);
+                if (pointZeroIndex > -1) {
+                    return new StringLiteral(desc.substring(0, 
pointZeroIndex));
+                }
+            }
             return new StringLiteral(desc);
         } else if (targetType.isDateType()) {
             return new DateLiteral(desc);
@@ -292,6 +304,19 @@ public abstract class Literal extends Expression 
implements LeafExpression, Comp
         throw new AnalysisException("cannot cast " + desc + " from type " + 
this.dataType + " to type " + targetType);
     }
 
+    private static int findPointZeroIndex(String str) {
+        int pointIndex = -1;
+        for (int i = 0; i < str.length(); ++i) {
+            char c = str.charAt(i);
+            if (pointIndex > 0 && c != '0') {
+                return -1;
+            } else if (pointIndex == -1 && c == '.') {
+                pointIndex = i;
+            }
+        }
+        return pointIndex;
+    }
+
     /** fromLegacyLiteral */
     public static Literal fromLegacyLiteral(LiteralExpr literalExpr, Type 
type) {
         DataType dataType = DataType.fromCatalogType(type);
diff --git 
a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_cast.groovy
 
b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_cast.groovy
new file mode 100644
index 00000000000..d7a0ed6be92
--- /dev/null
+++ 
b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_cast.groovy
@@ -0,0 +1,49 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("fold_constant_cast") {
+    sql "set enable_nereids_planner=true"
+    sql "set enable_fallback_to_original_planner=false"
+    sql "set enable_fold_constant_by_be=false"
+    // bug_fix
+    testFoldConst("select concat(substr('2025-03-20',1,4)-1,'-01-01')")
+    testFoldConst("select concat(substr('2025-03-20',1,4)-1.0,'-01-01')")
+    testFoldConst("select concat(substr('2025-03-20',1,4)+1.0,'-01-01')")
+    testFoldConst("select concat(substr('2025-03-20',1,4)-0.5,'-01-01')")
+    testFoldConst("select cast(cast(2025.00 as double) as string)")
+    testFoldConst("select cast(cast(2025.00 as float) as string)")
+    testFoldConst("SELECT CAST(CAST(123 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(123.456 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(-123.456 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(0.001 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(-0.001 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(1e+10 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(1e-10 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(-1e+10 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(-1e-10 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(123456789.123456789 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(-123456789.123456789 AS DOUBLE) AS 
STRING)")
+    testFoldConst("SELECT CAST(CAST(0 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(0.1 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(-0.1 AS DOUBLE) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(123 AS FLOAT) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(123.456 AS FLOAT) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(-123.456 AS FLOAT) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(0.001 AS FLOAT) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(-0.001 AS FLOAT) AS STRING)")
+    testFoldConst("SELECT CAST(CAST(1e+10 AS FLOAT) AS STRING)")
+}


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

Reply via email to