This is an automated email from the ASF dual-hosted git repository.

linzhongcheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new c8985dc9ba3 [Bug](function) Fix function for cast string as 
date/datetime (#35637)
c8985dc9ba3 is described below

commit c8985dc9ba36f1bb8204f81591545de4e4dae8fc
Author: yongjinhou <109586248+yongjin...@users.noreply.github.com>
AuthorDate: Wed Jun 5 17:34:19 2024 +0800

    [Bug](function) Fix function for cast string as date/datetime (#35637)
    
    ## Proposed changes
    
    Issue Number: close #35635
    
    <!--Describe your changes.-->
    Cast rules:Consistent with mysql.
    String:Date
    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
---
 .../nereids/trees/expressions/literal/DateLiteral.java | 18 ++++++++++++++++++
 .../data/correctness_p0/test_cast_date_decimal.out     | 14 +++++++++++++-
 .../correctness_p0/test_cast_date_decimal.groovy       | 18 +++++++++++++++++-
 3 files changed, 48 insertions(+), 2 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 93127933ea7..04a0f0120e2 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
@@ -183,6 +183,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)) {
@@ -194,6 +198,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 0272c0be822..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,7 +16,23 @@
 // under the License.
 
 suite("test_cast_date_decimal") {
-    qt_sql """
+    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_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: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to