[cherry-pick](branch-21) fix cast string to int return wrong result (#36788) (#37803)

## Proposed changes
cherry-pick from master:
https://github.com/apache/doris/pull/36788
https://github.com/apache/doris/pull/36505

<!--Describe your changes.-->
This commit is contained in:
zhangstar333
2024-07-15 18:48:49 +08:00
committed by GitHub
parent e5219467dd
commit 03e21dddff
6 changed files with 27 additions and 4 deletions

View File

@ -279,6 +279,11 @@ T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseR
[[fallthrough]];
case '+':
++i;
// only one '+'/'-' char, so could return failure directly
if (UNLIKELY(len == 1)) {
*result = PARSE_FAILURE;
return 0;
}
}
// This is the fast path where the string cannot overflow.

View File

@ -393,7 +393,7 @@ public class DateTimeExtractAndTransform {
/**
* from_days.
*/
@ExecFunction(name = "from_days", argTypes = {"INT"}, returnType = "DATE")
@ExecFunction(name = "from_days", argTypes = {"INT"}, returnType = "DATEV2")
public static Expression fromDays(IntegerLiteral n) {
// doris treat 0000AD as ordinary year but java LocalDateTime treat it as lunar year.
LocalDateTime res = LocalDateTime.of(0, 1, 1, 0, 0, 0)
@ -401,7 +401,7 @@ public class DateTimeExtractAndTransform {
if (res.isBefore(LocalDateTime.of(0, 3, 1, 0, 0, 0))) {
res = res.plusDays(-1);
}
return DateLiteral.fromJavaDateType(res);
return DateV2Literal.fromJavaDateType(res);
}
@ExecFunction(name = "last_day", argTypes = {"DATE"}, returnType = "DATE")

View File

@ -23,7 +23,7 @@ import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DateType;
import org.apache.doris.nereids.types.DateV2Type;
import org.apache.doris.nereids.types.IntegerType;
import com.google.common.base.Preconditions;
@ -38,7 +38,7 @@ public class FromDays extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DateType.INSTANCE).args(IntegerType.INSTANCE)
FunctionSignature.ret(DateV2Type.INSTANCE).args(IntegerType.INSTANCE)
);
/**

View File

@ -885,6 +885,7 @@ visible_functions = {
[['utc_timestamp'], 'DATETIME', [], 'ALWAYS_NOT_NULLABLE'],
[['timestamp'], 'DATETIME', ['DATETIME'], 'ALWAYS_NULLABLE'],
[['from_days'], 'DATEV2', ['INT'], 'ALWAYS_NULLABLE'],
[['from_days'], 'DATE', ['INT'], 'ALWAYS_NULLABLE'],
[['last_day'], 'DATE', ['DATETIME'], 'ALWAYS_NULLABLE'],
[['last_day'], 'DATE', ['DATE'], 'ALWAYS_NULLABLE'],

View File

@ -41,3 +41,15 @@ true
-- !sql14 --
1.1111
-- !sql15 --
\N
-- !sql16 --
\N
-- !sql17 --
\N
-- !sql18 --
\N

View File

@ -31,4 +31,9 @@ suite("test_json_type_cast", "p0") {
qt_sql12 """select cast("111111" as json)"""
qt_sql13 """select cast(111111 as json)"""
qt_sql14 """select cast(1.1111 as json)"""
qt_sql15 """select cast("+" as int);"""
qt_sql16 """select cast("-" as int);"""
qt_sql17 """select cast("a" as int);"""
qt_sql18 """select cast("/" as int);"""
}