This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.1 by this push:
new 03e21dddff6 [cherry-pick](branch-21) fix cast string to int return
wrong result (#36788) (#37803)
03e21dddff6 is described below
commit 03e21dddff68fd092d71ba5e6b55693ac3c3fd83
Author: zhangstar333 <[email protected]>
AuthorDate: Mon Jul 15 18:48:49 2024 +0800
[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.-->
---
be/src/util/string_parser.hpp | 5 +++++
.../functions/executable/DateTimeExtractAndTransform.java | 4 ++--
.../nereids/trees/expressions/functions/scalar/FromDays.java | 4 ++--
gensrc/script/doris_builtins_functions.py | 1 +
regression-test/data/datatype_p0/json/json_cast.out | 12 ++++++++++++
regression-test/suites/datatype_p0/json/json_cast.groovy | 5 +++++
6 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/be/src/util/string_parser.hpp b/be/src/util/string_parser.hpp
index 34bd678c947..8bc6ecae914 100644
--- a/be/src/util/string_parser.hpp
+++ b/be/src/util/string_parser.hpp
@@ -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.
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java
index 0639e96a9c9..a5cffe3dabd 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java
@@ -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")
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/FromDays.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/FromDays.java
index 7adf680c0ef..a2b5a420c34 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/FromDays.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/FromDays.java
@@ -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)
);
/**
diff --git a/gensrc/script/doris_builtins_functions.py
b/gensrc/script/doris_builtins_functions.py
index df50d392f20..5cb06853565 100644
--- a/gensrc/script/doris_builtins_functions.py
+++ b/gensrc/script/doris_builtins_functions.py
@@ -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'],
diff --git a/regression-test/data/datatype_p0/json/json_cast.out
b/regression-test/data/datatype_p0/json/json_cast.out
index 73809a89024..8f58f45ce74 100644
--- a/regression-test/data/datatype_p0/json/json_cast.out
+++ b/regression-test/data/datatype_p0/json/json_cast.out
@@ -41,3 +41,15 @@ true
-- !sql14 --
1.1111
+-- !sql15 --
+\N
+
+-- !sql16 --
+\N
+
+-- !sql17 --
+\N
+
+-- !sql18 --
+\N
+
diff --git a/regression-test/suites/datatype_p0/json/json_cast.groovy
b/regression-test/suites/datatype_p0/json/json_cast.groovy
index 7646b4f7ccb..458c60992d9 100644
--- a/regression-test/suites/datatype_p0/json/json_cast.groovy
+++ b/regression-test/suites/datatype_p0/json/json_cast.groovy
@@ -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);"""
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]