Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](Nereids) fix double literal to string literal cast problem #49416

Merged
merged 5 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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)")
}
Loading