From 6a31899edb3b5d77a3ee179ac315e67d97da3d70 Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Mon, 24 Mar 2025 18:10:21 +0800 Subject: [PATCH 1/5] [fix](Nereids) fix double literal to string literal cast problem --- .../doris/nereids/trees/expressions/literal/Literal.java | 6 ++++++ .../fold_constant/fold_constant_string_arithmatic.groovy | 4 ++++ 2 files changed, 10 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 d367ccfcf9ccfa..465d7d1692ce82 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,14 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti return new CharLiteral(desc, ((CharType) targetType).getLen()); } } else if (targetType.isVarcharType()) { + if (this.dataType.isDoubleType()) { + return new VarcharLiteral(desc.replaceAll("\\.0+$", ""), ((VarcharType) targetType).getLen()); + } return new VarcharLiteral(desc, ((VarcharType) targetType).getLen()); } else if (targetType instanceof StringType) { + if (this.dataType.isDoubleType()) { + return new StringLiteral(desc.replaceAll("\\.0+$", "")); + } return new StringLiteral(desc); } else if (targetType.isDateType()) { return new DateLiteral(desc); diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy index 76de337c7a9c86..cd0b09c4e7e314 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy @@ -1723,5 +1723,9 @@ suite("fold_constant_string_arithmatic") { testFoldConst("select split_by_string('a😁a😁a', '')") testFoldConst("select character_length('a😁a😁a')") testFoldConst("select replace_empty('a😁a😁a', '', '2')") + + // bug_fix + testFoldConst("select concat(substr('2025-03-20',1,4)-1,'-01-01')") + testFoldConst("select cast(2025.00 as string)") } From 3c19f9ca6db047c4b6735268d7df6a00e198de8a Mon Sep 17 00:00:00 2001 From: libinfeng Date: Mon, 24 Mar 2025 20:28:55 +0800 Subject: [PATCH 2/5] fix p0 --- .../doris/nereids/trees/expressions/literal/Literal.java | 4 ++-- .../fold_constant/fold_constant_string_arithmatic.groovy | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) 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 465d7d1692ce82..22412b823463c0 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,12 +261,12 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti return new CharLiteral(desc, ((CharType) targetType).getLen()); } } else if (targetType.isVarcharType()) { - if (this.dataType.isDoubleType()) { + if (this.dataType.isDoubleType() || this.dataType.isFloatType()) { return new VarcharLiteral(desc.replaceAll("\\.0+$", ""), ((VarcharType) targetType).getLen()); } return new VarcharLiteral(desc, ((VarcharType) targetType).getLen()); } else if (targetType instanceof StringType) { - if (this.dataType.isDoubleType()) { + if (this.dataType.isDoubleType() || this.dataType.isFloatType()) { return new StringLiteral(desc.replaceAll("\\.0+$", "")); } return new StringLiteral(desc); diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy index cd0b09c4e7e314..334bffb07d18cb 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy @@ -1726,6 +1726,7 @@ suite("fold_constant_string_arithmatic") { // bug_fix testFoldConst("select concat(substr('2025-03-20',1,4)-1,'-01-01')") - testFoldConst("select cast(2025.00 as string)") + testFoldConst("select cast(cast(2025.00 as double) as string)") + testFoldConst("select cast(cast(2025.00 as float) as string)") } From 3edb2a3acea79f7893bd35ca0bc18c917f4c9e53 Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Tue, 25 Mar 2025 11:42:51 +0800 Subject: [PATCH 3/5] add more cases --- .../fold_constant/fold_constant_string_arithmatic.groovy | 3 +++ 1 file changed, 3 insertions(+) diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy index 334bffb07d18cb..2980ba1518b58d 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy @@ -1726,6 +1726,9 @@ suite("fold_constant_string_arithmatic") { // 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)") } From f59b25acca32499a78330d5c90fdc8d456ff889b Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Tue, 25 Mar 2025 14:34:42 +0800 Subject: [PATCH 4/5] move cast cases to new file --- .../fold_constant/fold_constant_cast.groovy | 49 +++++++++++++++++++ .../fold_constant_string_arithmatic.groovy | 8 --- 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_cast.groovy 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)") +} diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy index 2980ba1518b58d..76de337c7a9c86 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy @@ -1723,13 +1723,5 @@ suite("fold_constant_string_arithmatic") { testFoldConst("select split_by_string('a😁a😁a', '')") testFoldConst("select character_length('a😁a😁a')") testFoldConst("select replace_empty('a😁a😁a', '', '2')") - - // 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)") } From 4fe6c2d00d2422b2baf84df6a32288b95641a32b Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Wed, 26 Mar 2025 11:45:30 +0800 Subject: [PATCH 5/5] change regex to normal code --- .../trees/expressions/literal/Literal.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) 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 22412b823463c0..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 @@ -262,12 +262,18 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti } } else if (targetType.isVarcharType()) { if (this.dataType.isDoubleType() || this.dataType.isFloatType()) { - return new VarcharLiteral(desc.replaceAll("\\.0+$", ""), ((VarcharType) targetType).getLen()); + 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()) { - return new StringLiteral(desc.replaceAll("\\.0+$", "")); + int pointZeroIndex = findPointZeroIndex(desc); + if (pointZeroIndex > -1) { + return new StringLiteral(desc.substring(0, pointZeroIndex)); + } } return new StringLiteral(desc); } else if (targetType.isDateType()) { @@ -292,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);