Skip to content

Commit 58393b6

Browse files
author
LiBinfeng
authored
[fix](Nereids) fix double literal to string literal cast problem (#49416)
When cast double like 1.000 to string, it should strip .000 in the trailing of double
1 parent 79a6dd1 commit 58393b6

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,20 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti
261261
return new CharLiteral(desc, ((CharType) targetType).getLen());
262262
}
263263
} else if (targetType.isVarcharType()) {
264+
if (this.dataType.isDoubleType() || this.dataType.isFloatType()) {
265+
int pointZeroIndex = findPointZeroIndex(desc);
266+
if (pointZeroIndex > -1) {
267+
return new VarcharLiteral(desc.substring(0, pointZeroIndex), ((VarcharType) targetType).getLen());
268+
}
269+
}
264270
return new VarcharLiteral(desc, ((VarcharType) targetType).getLen());
265271
} else if (targetType instanceof StringType) {
272+
if (this.dataType.isDoubleType() || this.dataType.isFloatType()) {
273+
int pointZeroIndex = findPointZeroIndex(desc);
274+
if (pointZeroIndex > -1) {
275+
return new StringLiteral(desc.substring(0, pointZeroIndex));
276+
}
277+
}
266278
return new StringLiteral(desc);
267279
} else if (targetType.isDateType()) {
268280
return new DateLiteral(desc);
@@ -286,6 +298,19 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti
286298
throw new AnalysisException("cannot cast " + desc + " from type " + this.dataType + " to type " + targetType);
287299
}
288300

301+
private static int findPointZeroIndex(String str) {
302+
int pointIndex = -1;
303+
for (int i = 0; i < str.length(); ++i) {
304+
char c = str.charAt(i);
305+
if (pointIndex > 0 && c != '0') {
306+
return -1;
307+
} else if (pointIndex == -1 && c == '.') {
308+
pointIndex = i;
309+
}
310+
}
311+
return pointIndex;
312+
}
313+
289314
/** fromLegacyLiteral */
290315
public static Literal fromLegacyLiteral(LiteralExpr literalExpr, Type type) {
291316
DataType dataType = DataType.fromCatalogType(type);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("fold_constant_cast") {
19+
sql "set enable_nereids_planner=true"
20+
sql "set enable_fallback_to_original_planner=false"
21+
sql "set enable_fold_constant_by_be=false"
22+
// bug_fix
23+
testFoldConst("select concat(substr('2025-03-20',1,4)-1,'-01-01')")
24+
testFoldConst("select concat(substr('2025-03-20',1,4)-1.0,'-01-01')")
25+
testFoldConst("select concat(substr('2025-03-20',1,4)+1.0,'-01-01')")
26+
testFoldConst("select concat(substr('2025-03-20',1,4)-0.5,'-01-01')")
27+
testFoldConst("select cast(cast(2025.00 as double) as string)")
28+
testFoldConst("select cast(cast(2025.00 as float) as string)")
29+
testFoldConst("SELECT CAST(CAST(123 AS DOUBLE) AS STRING)")
30+
testFoldConst("SELECT CAST(CAST(123.456 AS DOUBLE) AS STRING)")
31+
testFoldConst("SELECT CAST(CAST(-123.456 AS DOUBLE) AS STRING)")
32+
testFoldConst("SELECT CAST(CAST(0.001 AS DOUBLE) AS STRING)")
33+
testFoldConst("SELECT CAST(CAST(-0.001 AS DOUBLE) AS STRING)")
34+
testFoldConst("SELECT CAST(CAST(1e+10 AS DOUBLE) AS STRING)")
35+
testFoldConst("SELECT CAST(CAST(1e-10 AS DOUBLE) AS STRING)")
36+
testFoldConst("SELECT CAST(CAST(-1e+10 AS DOUBLE) AS STRING)")
37+
testFoldConst("SELECT CAST(CAST(-1e-10 AS DOUBLE) AS STRING)")
38+
testFoldConst("SELECT CAST(CAST(123456789.123456789 AS DOUBLE) AS STRING)")
39+
testFoldConst("SELECT CAST(CAST(-123456789.123456789 AS DOUBLE) AS STRING)")
40+
testFoldConst("SELECT CAST(CAST(0 AS DOUBLE) AS STRING)")
41+
testFoldConst("SELECT CAST(CAST(0.1 AS DOUBLE) AS STRING)")
42+
testFoldConst("SELECT CAST(CAST(-0.1 AS DOUBLE) AS STRING)")
43+
testFoldConst("SELECT CAST(CAST(123 AS FLOAT) AS STRING)")
44+
testFoldConst("SELECT CAST(CAST(123.456 AS FLOAT) AS STRING)")
45+
testFoldConst("SELECT CAST(CAST(-123.456 AS FLOAT) AS STRING)")
46+
testFoldConst("SELECT CAST(CAST(0.001 AS FLOAT) AS STRING)")
47+
testFoldConst("SELECT CAST(CAST(-0.001 AS FLOAT) AS STRING)")
48+
testFoldConst("SELECT CAST(CAST(1e+10 AS FLOAT) AS STRING)")
49+
}

0 commit comments

Comments
 (0)