Skip to content

Commit de68c1e

Browse files
LiBinfengYour Name
authored andcommitted
[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 c089721 commit de68c1e

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
@@ -267,8 +267,20 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti
267267
return new CharLiteral(desc, ((CharType) targetType).getLen());
268268
}
269269
} else if (targetType.isVarcharType()) {
270+
if (this.dataType.isDoubleType() || this.dataType.isFloatType()) {
271+
int pointZeroIndex = findPointZeroIndex(desc);
272+
if (pointZeroIndex > -1) {
273+
return new VarcharLiteral(desc.substring(0, pointZeroIndex), ((VarcharType) targetType).getLen());
274+
}
275+
}
270276
return new VarcharLiteral(desc, ((VarcharType) targetType).getLen());
271277
} else if (targetType instanceof StringType) {
278+
if (this.dataType.isDoubleType() || this.dataType.isFloatType()) {
279+
int pointZeroIndex = findPointZeroIndex(desc);
280+
if (pointZeroIndex > -1) {
281+
return new StringLiteral(desc.substring(0, pointZeroIndex));
282+
}
283+
}
272284
return new StringLiteral(desc);
273285
} else if (targetType.isDateType()) {
274286
return new DateLiteral(desc);
@@ -292,6 +304,19 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti
292304
throw new AnalysisException("cannot cast " + desc + " from type " + this.dataType + " to type " + targetType);
293305
}
294306

307+
private static int findPointZeroIndex(String str) {
308+
int pointIndex = -1;
309+
for (int i = 0; i < str.length(); ++i) {
310+
char c = str.charAt(i);
311+
if (pointIndex > 0 && c != '0') {
312+
return -1;
313+
} else if (pointIndex == -1 && c == '.') {
314+
pointIndex = i;
315+
}
316+
}
317+
return pointIndex;
318+
}
319+
295320
/** fromLegacyLiteral */
296321
public static Literal fromLegacyLiteral(LiteralExpr literalExpr, Type type) {
297322
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)