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

yiguolei 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 a86134cb39 [fix](executor) Fixed an error with cast as time. #20144
a86134cb39 is described below

commit a86134cb39a58358dd59d3df7f8b673b54107ec8
Author: Mryange <[email protected]>
AuthorDate: Mon May 29 12:17:21 2023 +0800

    [fix](executor) Fixed an error with cast as time. #20144
    
    before
    
    mysql [(none)]>select cast("10:10:10" as time);
    +-------------------------------+
    | CAST('10:10:10' AS TIMEV2(0)) |
    +-------------------------------+
    | 00:00:00                      |
    +-------------------------------+
    after
    
    mysql [(none)]>select cast("10:10:10" as time);
    +-------------------------------+
    | CAST('10:10:10' AS TIMEV2(0)) |
    +-------------------------------+
    | 10:10:10                      |
    +-------------------------------+
    In the past, we supported this syntax.
    
    mysql [(none)]>select cast("2023:05:01 13:14:15" as time);
    +------------------------------------------+
    | CAST('2023:05:01 13:14:15' AS TIMEV2(0)) |
    +------------------------------------------+
    | 13:14:15                                 |
    +------------------------------------------+
    However, "10:10:10" is also a valid datetime.
    
    mysql [(none)]>select cast("10:10:10" as datetime);
    +-----------------------------------+
    | CAST('10:10:10' AS DATETIMEV2(0)) |
    +-----------------------------------+
    | 2010-10-10 00:00:00               |
    +-----------------------------------+
    So here, the order of parsing has been adjusted.
---
 be/src/vec/functions/function_cast.h                     | 16 ++++++++++++----
 regression-test/data/correctness/test_cast_as_time.out   | 13 +++++++++++--
 .../suites/correctness/test_cast_as_time.groovy          |  6 ++++++
 3 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/be/src/vec/functions/function_cast.h 
b/be/src/vec/functions/function_cast.h
index 5c27f33bfd..90bafadf96 100644
--- a/be/src/vec/functions/function_cast.h
+++ b/be/src/vec/functions/function_cast.h
@@ -121,11 +121,20 @@ struct TimeCast {
     template <typename T>
     static bool try_parse_time(char* s, size_t len, T& x) {
         /// TODO: Maybe we can move Timecast to the io_helper.
-        if (VecDateTimeValue dv {}; dv.from_date_str(s, len)) {
-            // can be parse as a datetime
-            x = dv.hour() * 3600 + dv.minute() * 60 + dv.second();
+        if (try_as_time(s, len, x)) {
             return true;
+        } else {
+            if (VecDateTimeValue dv {}; dv.from_date_str(s, len)) {
+                // can be parse as a datetime
+                x = dv.hour() * 3600 + dv.minute() * 60 + dv.second();
+                return true;
+            }
+            return false;
         }
+    }
+
+    template <typename T>
+    static bool try_as_time(char* s, size_t len, T& x) {
         char* first_char = s;
         char* end_char = s + len;
         int hour = 0, minute = 0, second = 0;
@@ -185,7 +194,6 @@ struct TimeCast {
         x = hour * 3600 + minute * 60 + second;
         return true;
     }
-
     // Cast from number
     template <typename T, typename S>
     static bool try_parse_time(T from, S& x) {
diff --git a/regression-test/data/correctness/test_cast_as_time.out 
b/regression-test/data/correctness/test_cast_as_time.out
index 9665ad225f..50a6af259e 100644
--- a/regression-test/data/correctness/test_cast_as_time.out
+++ b/regression-test/data/correctness/test_cast_as_time.out
@@ -1,11 +1,20 @@
 -- This file is automatically generated. You should know what you did if you 
want to edit this
 -- !select1 --
 00:03:00
-\\N
+\N
 20:20:20
+
 -- !select2 --
 19:18:17
 30:20:00
 00:04:00
+
 -- !select3 --
-19:19:19
\ No newline at end of file
+19:19:19
+
+-- !select4 --
+10:10:10
+
+-- !select5 --
+2010-10-10T00:00
+
diff --git a/regression-test/suites/correctness/test_cast_as_time.groovy 
b/regression-test/suites/correctness/test_cast_as_time.groovy
index 5b6c06b97b..b15440f791 100644
--- a/regression-test/suites/correctness/test_cast_as_time.groovy
+++ b/regression-test/suites/correctness/test_cast_as_time.groovy
@@ -48,4 +48,10 @@ suite("test_cast_as_time") {
     qt_select3 """
         select cast('2023-02-21 19:19:19' as time)
     """    
+    qt_select4 """
+       select cast("10:10:10" as time)
+    """    
+    qt_select5 """
+       select cast("10:10:10" as datetimev2)
+    """   
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to