Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ktat opened this issue Sep 18, 2024 · 2 comments
Closed

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

ktat opened this issue Sep 18, 2024 · 2 comments

Comments

@ktat
Copy link
Contributor

ktat commented Sep 18, 2024

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
Copy link
Contributor Author

ktat commented Sep 18, 2024

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                            |
+----------------------------------+----------------------------------+---------------------------------+

ktat added a commit to ktat/go-mysql-server that referenced this issue Sep 18, 2024
- Bug fix: cannot parse "%Y%m%d" format dolthub#2666
@jycor
Copy link
Contributor

jycor commented Sep 23, 2024

Issue is resolved by: #2666

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants