This is an automated email from the ASF dual-hosted git repository.
morningman pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.0 by this push:
new 83409fc104b [Bug](function) Fix function for cast string as
date/datetime (#35637) (#35931)
83409fc104b is described below
commit 83409fc104b9d9f50c9ea7114a595b3cf20745d7
Author: yongjinhou <[email protected]>
AuthorDate: Thu Jun 6 10:35:37 2024 +0800
[Bug](function) Fix function for cast string as date/datetime (#35637)
(#35931)
cherry-pick #35637 from master to branch-2.0
---
.../trees/expressions/literal/DateLiteral.java | 18 ++++++++++++++++++
.../data/correctness_p0/test_cast_date_decimal.out | 14 +++++++++++++-
.../correctness_p0/test_cast_date_decimal.groovy | 20 ++++++++++++++++----
3 files changed, 47 insertions(+), 5 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java
index 9693858fb7e..7c40c6eae36 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java
@@ -181,6 +181,10 @@ public class DateLiteral extends Literal {
}
// normalize leading 0 for date and time
+ // The first part is 1-digit x: 000x-month-day
+ // The first part is 2-digit xy: 20xy-month-day / 19xy-month-day
+ // The first part is 3-digit xyz: 20xy-0z-day / 19xy-0z-day
+ // The first part is 4-digit xyzw: xyzw-month-day
while (i < s.length() && partNumber < 6) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
@@ -192,6 +196,20 @@ public class DateLiteral extends Literal {
int len = j - i;
if (len == 4 || len == 2) {
sb.append(s, i, j);
+ } else if (len == 3) {
+ if (partNumber == 0) {
+ String yy = s.substring(i, i + 2);
+ int year = Integer.parseInt(yy);
+ if (year >= 0 && year <= 69) {
+ sb.append("20");
+ } else if (year >= 70 && year <= 99) {
+ sb.append("19");
+ }
+ sb.append(yy).append('-');
+ } else {
+ sb.append(s, i, i + 2).append(' ');
+ }
+ j = j - 1;
} else if (len == 1) {
if (partNumber == 0) {
sb.append("000").append(c);
diff --git a/regression-test/data/correctness_p0/test_cast_date_decimal.out
b/regression-test/data/correctness_p0/test_cast_date_decimal.out
index 5b10e282f28..91b9ceb8ebc 100644
--- a/regression-test/data/correctness_p0/test_cast_date_decimal.out
+++ b/regression-test/data/correctness_p0/test_cast_date_decimal.out
@@ -1,4 +1,16 @@
-- This file is automatically generated. You should know what you did if you
want to edit this
--- !sql --
+-- !sql1 --
true
+-- !sql2 --
+2024-12-12
+
+-- !sql3 --
+2024-12-12
+
+-- !sql4 --
+2024-12-12
+
+-- !sql5 --
+2012-03-12
+
diff --git
a/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy
b/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy
index a2d00c05efe..03a970d2a99 100644
--- a/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy
+++ b/regression-test/suites/correctness_p0/test_cast_date_decimal.groovy
@@ -16,11 +16,23 @@
// under the License.
suite("test_cast_date_decimal") {
- sql """
- set enable_nereids_planner=false;
+ qt_sql1 """
+ select cast('2020-02-02' as date ) between cast('2020-02-02' as date )
and cast('2020-02-02' as date ) + 1.0;
"""
- qt_sql """
- select cast('2020-02-02' as date ) between cast('2020-02-02' as date )
and cast('2020-02-02' as date ) + 1.0;
+ qt_sql2 """
+ select cast('2024-12-12' as date);
+ """
+
+ qt_sql3 """
+ select cast('2024.12.12' as date);
+ """
+
+ qt_sql4 """
+ select cast('24.12.12' as date);
+ """
+
+ qt_sql5 """
+ select cast('123.123' as date);
"""
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]