tidb icon indicating copy to clipboard operation
tidb copied to clipboard

incorrect result of cast string to datetime

Open zz-jason opened this issue 9 years ago • 13 comments

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

zz-jason avatar Sep 12 '17 05:09 zz-jason

#dibs

I would like to take a look on this issue.

gaocegege avatar Sep 20 '17 09:09 gaocegege

@gaocegege Thank you, you may like to take a look at #4273 before you start to work on this.

XuHuaiyu avatar Sep 20 '17 09:09 XuHuaiyu

@XuHuaiyu Thanks for your guidance.

gaocegege avatar Sep 20 '17 09:09 gaocegege

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.

mccxj avatar Sep 21 '17 10:09 mccxj

Maybe i could work on this issue?

laidahe avatar Aug 20 '18 12:08 laidahe

@laidahe Great!

zz-jason avatar Aug 20 '18 12:08 zz-jason

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 avatar Aug 30 '18 03:08 laidahe

@laidahe Looks like you find the root cause. It will be awesome if you can spend some time in fixing this issue.

zhexuany avatar Aug 30 '18 07:08 zhexuany

I am working on it.It's my pleasure.

laidahe avatar Aug 30 '18 10:08 laidahe

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)

wwar avatar Apr 10 '20 02:04 wwar

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)

wjhuang2016 avatar Dec 22 '20 03:12 wjhuang2016

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

ti-srebot avatar Dec 22 '20 03:12 ti-srebot

( severity(label) AffectedVersions ) fields are empty.

ti-srebot avatar Jan 15 '21 10:01 ti-srebot