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

P1-[4.0 bug hunting]-[SQL Plan Management]-Incorrect result for CAST to DATETIME #17809

Closed
sre-bot opened this issue Jun 5, 2020 · 3 comments · Fixed by #19485
Closed

P1-[4.0 bug hunting]-[SQL Plan Management]-Incorrect result for CAST to DATETIME #17809

sre-bot opened this issue Jun 5, 2020 · 3 comments · Fixed by #19485
Assignees
Labels
component/expression priority/P2 The issue has P2 priority. severity/critical sig/execution SIG execution type/bug The issue is confirmed as a bug.

Comments

@sre-bot
Copy link
Contributor

sre-bot commented Jun 5, 2020

Bug Hunter issue tidb-challenge-program/bug-hunting-issue#13


Consider the following statements:

CREATE TABLE t0(c0 DOUBLE);
INSERT INTO t0(c0) VALUES (0);
SELECT CAST(t0.c0 AS DATETIME) FROM t0; -- expected: {0000-00-00 00:00:00}, actual: {NULL}

Unexpectedly, the CAST computes NULL and prints a warning:

Server version: 5.7.25-TiDB- TiDB Server (Apache License 2.0), MySQL 5.7 compatible

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT CAST(t0.c0 AS DATETIME) FROM t0; -- expected: {0000-00-00 00:00:00}, actual: {NULL}
+-------------------------+
| CAST(t0.c0 AS DATETIME) |
+-------------------------+
| NULL                    |
+-------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+---------------------------+
| Level   | Code | Message                   |
+---------+------+---------------------------+
| Warning | 1292 | Incorrect time value: '0' |
+---------+------+---------------------------+
1 row in set (0.00 sec)

This is unexpected, which can be also seen when directly using the constant, which computes the expected value:

SELECT CAST(0.0 AS DATETIME) FROM t0; -- {0000-00-00 00:00:00}

This is problematic, since it can can result in an incorrect result for queries, which is demonstrated by the following query, which I used to find this bug:

CREATE TABLE t0(c0 DOUBLE);
CREATE TABLE t1(c0 INT);
INSERT INTO t1(c0) VALUES (0);
INSERT INTO t0(c0) VALUES (0);
SELECT * FROM t1 LEFT JOIN t0 ON TRUE WHERE CAST(t0.c0 AS DATETIME) IS NULL; -- expected: {}, actual: {0|0}

I found this based on the 4.0 RC, and also found that this can be still reproduced on the latest master (7e71069):

+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: 
Git Commit Hash: 
Git Branch: 
UTC Build Time: 2020-04-15 06:06:10
GoVersion: go1.13.4
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MySQL 8.0.19 computes the result I would expect:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE TABLE t0(c0 DOUBLE);
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO t0(c0) VALUES (0);
Query OK, 1 row affected (0.01 sec)

mysql> SELECT CAST(t0.c0 AS DATETIME) FROM t0; -- expected: {0000-00-00 00:00:00}, actual: {NULL}
+-------------------------+
| CAST(t0.c0 AS DATETIME) |
+-------------------------+
| 0000-00-00 00:00:00     |
+-------------------------+
1 row in set (0.00 sec)

Since this bug causes an incorrect result bug, your classification seems to classify this bug as P0.

@sre-bot sre-bot added the type/bug The issue is confirmed as a bug. label Jun 5, 2020
@djshow832 djshow832 added the sig/planner SIG: Planner label Jun 5, 2020
@winoros winoros added sig/execution SIG execution component/expression and removed sig/planner SIG: Planner labels Jun 8, 2020
@fzhedu fzhedu self-assigned this Aug 25, 2020
@lzmhhh123
Copy link
Contributor

@dyzsr PTAL.

@lzmhhh123
Copy link
Contributor

/assign @dyzsr

@dyzsr
Copy link
Contributor

dyzsr commented Aug 27, 2020

/assign @dyzsr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression priority/P2 The issue has P2 priority. severity/critical 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.

7 participants