incorrect result of cast string to datetime
1. What did you do?
select cast("2019-12-09 12" as datetime);
2. What did you expect to see?
MySQL > select cast("2019-12-09 12" as datetime);
+-----------------------------------+
| cast("2019-12-09 12" as datetime) |
+-----------------------------------+
| 2019-12-09 12:00:00 |
+-----------------------------------+
1 row in set (0.00 sec)
3. What did you see instead?
TiDB > select cast("2019-12-09 12" as datetime);
ERROR 1105 (HY000): invalid time format
4. What version of TiDB are you using (tidb-server -V)?
$./bin/tidb-server -V
Release Version: 0.9.0
Git Commit Hash: d11591fc16da7468ca51d34d055fa715586eedeb
Git Commit Branch: master
UTC Build Time: 2017-09-12 05:03:54
#dibs
I would like to take a look on this issue.
@gaocegege Thank you, you may like to take a look at #4273 before you start to work on this.
@XuHuaiyu Thanks for your guidance.
1.mysql's time cast function is very dirty. eg. str_to_datetime/number_to_datetime is different. the function of parseTime(or parseDatetime) is not fully compatible with MySQL's str_to_datetime/str_to_time. I'm migrating this code.
2.MySQL's sql_mode also affects the entire parse result. error like "ERROR 1105 (HY000): invalid time format" was usually a simple waring.
Maybe i could work on this issue?
@laidahe Great!
i find something intresting. see below 3 examples in mysql:
select cast("2019-12-09 12:.#$!56" as datetime); select cast("2019-12@#$%^&09 12:.#$!56" as datetime); select cast("2019-12@#$%^&Y09 12:.#$!56" as datetime);(notice the 'Y' char)
mysql> select cast("2019-12-09 12:.#$!56" as datetime);
+------------------------------------------+
| cast("2019-12-09 12:.#$!56" as datetime) |
+------------------------------------------+
| 2019-12-09 12:56:00 |
+------------------------------------------+
mysql> select cast("2019-12@#$%^&09 12:.#$!56" as datetime);
+-----------------------------------------------+
| cast("2019-12@#$%^&09 12:.#$!56" as datetime) |
+-----------------------------------------------+
| 2019-12-09 12:56:00 |
+-----------------------------------------------+
mysql> select cast("2019-12@#$%^&Y09 12:.#$!56" as datetime);
+------------------------------------------------+
| cast("2019-12@#$%^&Y09 12:.#$!56" as datetime) |
+------------------------------------------------+
| NULL |
+------------------------------------------------+
The result in tidb :
mysql> select cast("2019-12-09 12:.#$!56" as datetime);
ERROR 1292 (22007): invalid time format: '2019-12-09 12:.#$!56'
mysql> select cast("2019-12@#$%^&09 12:.#$!56" as datetime);
ERROR 1292 (22007): invalid time format: '2019-12@#$%^&09 12:.#$!56'
mysql> select cast("2019-12@#$%^&Y09 12:.#$!56" as datetime);
ERROR 1292 (22007): invalid time format: '2019-12@#$%^&Y09 12:.#$!56'
I think mysql scan the string for digital number,ignoring special characters. The problem in tidb mybe the following code in ParseDateFormat function:
.........
// Separator is a single none-number char.
if !unicode.IsNumber(rune(format[i])) {
if !unicode.IsNumber(rune(format[i-1])) {
return nil
}
seps = append(seps, format[start:i])
start = i + 1
}
The code show that tidb split datetime with just one single none-number char!That is the problem!
@laidahe Looks like you find the root cause. It will be awesome if you can spend some time in fixing this issue.
I am working on it.It's my pleasure.
Looping back on this issue. The original issue reported has been fixed, but the following test case still differs from MySQL:
select cast("2019-12-09 12:.#$!56" as datetime);
select cast("2019-12@#$%^&09 12:.#$!56" as datetime);
Output:
mysql [localhost:8019] {msandbox} ((none)) > select cast("2019-12-09 12:.#$!56" as datetime);
+------------------------------------------+
| cast("2019-12-09 12:.#$!56" as datetime) |
+------------------------------------------+
| 2019-12-09 12:56:00 |
+------------------------------------------+
1 row in set (0.00 sec)
mysql [localhost:8019] {msandbox} ((none)) > select cast("2019-12@#$%^&09 12:.#$!56" as datetime);
+-----------------------------------------------+
| cast("2019-12@#$%^&09 12:.#$!56" as datetime) |
+-----------------------------------------------+
| 2019-12-09 12:56:00 |
+-----------------------------------------------+
1 row in set (0.00 sec)
tidb> select cast("2019-12-09 12:.#$!56" as datetime);
+------------------------------------------+
| cast("2019-12-09 12:.#$!56" as datetime) |
+------------------------------------------+
| NULL |
+------------------------------------------+
1 row in set, 1 warning (0.00 sec)
tidb> select cast("2019-12@#$%^&09 12:.#$!56" as datetime);
+-----------------------------------------------+
| cast("2019-12@#$%^&09 12:.#$!56" as datetime) |
+-----------------------------------------------+
| NULL |
+-----------------------------------------------+
1 row in set, 1 warning (0.00 sec)
tidb> SELECT tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v4.0.0-beta.2-183-g57db6cec7
Git Commit Hash: 57db6cec7ffad78e74b6ac6c67a2bfe9a6718d17
Git Branch: master
UTC Build Time: 2020-04-04 08:06:18
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
1 row in set (0.01 sec)
Confirm that these bugs had been fixed in master.
mysql> select cast("2019-12-09 12:.#$!56" as datetime);
+------------------------------------------+
| cast("2019-12-09 12:.#$!56" as datetime) |
+------------------------------------------+
| 2019-12-09 12:56:00 |
+------------------------------------------+
1 row in set (0.00 sec)
mysql> select cast("2019-12@#$%^&09 12:.#$!56" as datetime);
+-----------------------------------------------+
| cast("2019-12@#$%^&09 12:.#$!56" as datetime) |
+-----------------------------------------------+
| 2019-12-09 12:56:00 |
+-----------------------------------------------+
1 row in set (0.00 sec)
Please edit this comment or add a new comment to complete the following information
Not a bug
- Remove the 'type/bug' label
- Add notes to indicate why it is not a bug
Duplicate bug
- Add the 'type/duplicate' label
- 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: https://github.com/pingcap/tidb/issues/20100
1. Root Cause Analysis (RCA) (optional)
2. Symptom (optional)
3. All Trigger Conditions (optional)
4. Workaround (optional)
5. Affected versions
6. Fixed versions
master
( severity(label) AffectedVersions ) fields are empty.