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

Date function is not compatible with Mysql #22530

Closed
hequn8128 opened this issue Jan 26, 2021 · 4 comments · Fixed by #22790
Closed

Date function is not compatible with Mysql #22530

hequn8128 opened this issue Jan 26, 2021 · 4 comments · Fixed by #22790
Labels
severity/major sig/execution SIG execution type/bug The issue is confirmed as a bug.

Comments

@hequn8128
Copy link

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

2. What did you expect to see? (Required)

mysql: 8.0.23

mysql> SELECT DATE_FORMAT("0000-02-28",'%W %d %M %Y') as valid_date;
+--------------------------+
| valid_date               |
+--------------------------+
| Tuesday 28 February 0000 |
+--------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATE_FORMAT("0000-01-01",'%W %d %M %Y') as valid_date;
+------------------------+
| valid_date             |
+------------------------+
| Sunday 01 January 0000 |
+------------------------+
1 row in set (0.00 sec)

mysql> select str_to_date("2003-01-02 10:11:12.0012", "%Y-%m-%d %H:%i:%S.%f") as f1,
    -> str_to_date("2003-01-02 10:11:12.0012", "%Y-%m-%d %H:%i:%S") as f2,
    -> str_to_date("2003-01-02", "%Y-%m-%d") as f3,
    -> str_to_date("02 10:11:12", "%d %H:%i:%S.%f") as f4,
    -> str_to_date("02 10:11:12", "%d %H:%i:%S") as f5,
    -> str_to_date("02 10", "%d %f") as f6;
+----------------------------+---------------------+------------+-----------------+----------+-----------------+
| f1                         | f2                  | f3         | f4              | f5       | f6              |
+----------------------------+---------------------+------------+-----------------+----------+-----------------+
| 2003-01-02 10:11:12.001200 | 2003-01-02 10:11:12 | 2003-01-02 | 58:11:12.000000 | 58:11:12 | 48:00:00.100000 |
+----------------------------+---------------------+------------+-----------------+----------+-----------------+
1 row in set, 1 warning (0.02 sec)

mysql> select str_to_date("2003-....01ABCD-02 10:11:12.0012", "%Y-%.%m%@-%d %H:%i:%S.%f") as a;
+----------------------------+
| a                          |
+----------------------------+
| 2003-01-02 10:11:12.001200 |
+----------------------------+
1 row in set (0.00 sec)

3. What did you see instead (Required)

mysql> SELECT DATE_FORMAT("0000-02-28",'%W %d %M %Y') as valid_date;
+-------------------------+
| valid_date              |
+-------------------------+
| Monday 28 February 0000 |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATE_FORMAT("0000-01-01",'%W %d %M %Y') as valid_date;
+--------------------------+
| valid_date               |
+--------------------------+
| Saturday 01 January 0000 |
+--------------------------+
1 row in set (0.00 sec)

mysql> select str_to_date("2003-01-02 10:11:12.0012", "%Y-%m-%d %H:%i:%S.%f") as f1,
    -> str_to_date("2003-01-02 10:11:12.0012", "%Y-%m-%d %H:%i:%S") as f2,
    -> str_to_date("2003-01-02", "%Y-%m-%d") as f3,
    -> str_to_date("02 10:11:12", "%d %H:%i:%S.%f") as f4,
    -> str_to_date("02 10:11:12", "%d %H:%i:%S") as f5,
    -> str_to_date("02 10", "%d %f") as f6;
+----------------------------+---------------------+------------+-----------------+----------+-----------------+
| f1                         | f2                  | f3         | f4              | f5       | f6              |
+----------------------------+---------------------+------------+-----------------+----------+-----------------+
| 2003-01-02 10:11:12.001200 | 2003-01-02 10:11:12 | 2003-01-02 | 58:11:12.000000 | 58:11:12 | 48:00:00.100000 |
+----------------------------+---------------------+------------+-----------------+----------+-----------------+
1 row in set, 1 warning (0.02 sec)

mysql> select str_to_date("2003-....01ABCD-02 10:11:12.0012", "%Y-%.%m%@-%d %H:%i:%S.%f") as a;
+---+
| a |
+---+
| NULL |
+---+
1 row in set, 1 warning (0.00 sec)

4. What is your TiDB version? (Required)

mysql> SELECT tidb_version();
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                                                                                                              |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v4.0.0-beta.2-2047-g0aa476034
Edition: Community
Git Commit Hash: 0aa476034ae8a69040ab2b4fe455114ba0e21695
Git Branch: master
UTC Build Time: 2021-01-26 02:15:59
GoVersion: go1.15.6
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
@hequn8128 hequn8128 added the type/bug The issue is confirmed as a bug. label Jan 26, 2021
@guo-shaoge
Copy link
Collaborator

guo-shaoge commented Feb 5, 2021

Looks like it's a bug of MySQL.

result of PostgreSQl-8.4 is same with TiDB. 6 means Saturday. doc:

test=# select to_char(to_date('0000-01-01', 'YYYY-MM-DD'), '%ID');
 to_char
---------
 %6
(1 row)

We can also write a Go program to verify. The result is Saturday.

package main

import (
  "fmt"
  "time"
)

func main() {
  // means 0000-01-01 00:00:00.000000
  year, month, day, hour, minute, second, microsecond := 0, 1, 1, 0, 0, 0, 0
  t := time.Date(year, time.Month(month), day, hour, minute, second, microsecond * 1000, time.Local)
  fmt.Println(t.Weekday())
}

MySQL and PostgreSQL both use proleptic Gregorian calendar, which is required by standard SQL.

Not sure for now why MySQL's result is Sunday.

@guo-shaoge
Copy link
Collaborator

guo-shaoge commented Feb 18, 2021

According to wikipedia, 1 BC starts on Saturday.

Year zero exists in ISO8601 where it coincides with the Gregorian year 1 BC(here)

So i believe it's MySQL's bug. bug report.

PS: str_to_date doesn't support %./%#/%@, need to fix.

@ti-srebot
Copy link
Contributor

ti-srebot commented Feb 19, 2021

Please edit this comment or add a new comment to complete the following information

Not a bug

  1. Remove the 'type/bug' label
  2. Add notes to indicate why it is not a bug

Duplicate bug

  1. Add the 'type/duplicate' label
  2. Add the link to the original bug

Bug

Note: Make Sure that 'component', and 'severity' labels are added
Example for how to fill out the template: #20100

1. Root Cause Analysis (RCA) (optional)

2. Symptom (optional)

3. All Trigger Conditions (optional)

4. Workaround (optional)

5. Affected versions

[v2.0.0:v2.0.11], [v2.1.0:v2.1.19],[v3.0.0:v3.0.20],[v4.0.0:v4.0.10],[v5.0.0-rc]

6. Fixed versions

master

@ti-srebot
Copy link
Contributor

( AffectedVersions ) fields are empty.
The values in ( AffectedVersions ) fields are incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity/major sig/execution SIG execution type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants