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 d367ccfcf9ccfa..63564abdc9db72 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 @@ -261,8 +261,20 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti 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); @@ -286,6 +298,19 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti 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 00000000000000..d7a0ed6be92079 --- /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)") +}