dolthub/go-mysql-server

STR_TO_DATE cannot parse "%Y%m%d" format

Closed this issue · 2 comments

ktat commented

MySQL STR_TO_DATE can parse "%Y%m%d" format. But go-mysql-server cannot parse it.

MySQL:

mysql> select STR_TO_DATE('20240801', '%Y%m%d');
+-----------------------------------+
| STR_TO_DATE('20240801', '%Y%m%d') |
+-----------------------------------+
| 2024-08-01                        |
+-----------------------------------+

When you added the following test case, it fails.

diff --git a/sql/expression/function/str_to_date_test.go b/sql/expression/function/str_to_date_test.go
index 19e96dac5..32fd0277e 100644
--- a/sql/expression/function/str_to_date_test.go
+++ b/sql/expression/function/str_to_date_test.go
@@ -21,6 +21,7 @@ func TestStrToDate(t *testing.T) {
                expected string
        }{
                {"standard", "Dec 26, 2000 2:13:15", "%b %e, %Y %T", "2000-12-26 02:13:15"},
+               {"ymd", "20240101", "%Y%m%d", "2024-01-01 00:00:00"},
        }
 
        for _, tt := range testCases {

Test Result:

--- FAIL: TestStrToDate (0.00s)
    --- FAIL: TestStrToDate/ymd (0.00s)
        str_to_date_test.go:37: 
                Error Trace:    /home/ktat/git/github/go-mysql-server/sql/expression/function/str_to_date_test.go:37
                Error:          Not equal: 
                                expected: string("2024-01-01 00:00:00")
                                actual  : <nil>(<nil>)
                Test:           TestStrToDate/ymd
FAIL
FAIL    command-line-arguments  0.010s
FAIL

I checked the part of parsing year, parseYear4DigitNumeric for `%Y' is usef in the following code.

year, rest, err := takeNumber(chars)

It takes "20240101" as year and rest is empty.

Shoud use takeNumberAtMostNChars instead of takeNumber ?

ktat commented

additional information. MySQL STR_TO_DATE behavior

mysql> select STR_TO_DATE('2024121', '%Y%m%d'), STR_TO_DATE('2024810', '%Y%m%d'), STR_TO_DATE('202411', '%Y%m%d');
+----------------------------------+----------------------------------+---------------------------------+
| STR_TO_DATE('2024121', '%Y%m%d') | STR_TO_DATE('2024810', '%Y%m%d') | STR_TO_DATE('202411', '%Y%m%d') |
+----------------------------------+----------------------------------+---------------------------------+
| 2024-12-01                       | NULL                             | NULL                            |
+----------------------------------+----------------------------------+---------------------------------+

Issue is resolved by: #2666

Thanks!