tidb-challenge-program / bug-hunting-issue

Bug hunting issues.
3 stars 0 forks source link

P0-[4.0 bug hunting]-[SQL Plan Management]-Incorrect result for LEFT JOIN AND NULLIF #45

Open mrigger opened 4 years ago

mrigger commented 4 years ago

Consider the following statements:

CREATE TABLE t0(c0 INTEGER);
CREATE TABLE t1(c0 INTEGER);
INSERT INTO t1 VALUES (0);
INSERT INTO t0 VALUES (0);
SELECT * FROM t1 LEFT JOIN t0 ON t0.c0=t1.c0 WHERE NOT NULLIF(t1.c0, t0.c0); --expected: {}, actual: {0, NULL}

Unexpectedly, the SELECT fetches a row:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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> CREATE TABLE t0(c0 INTEGER);
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE t1(c0 INTEGER);
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO t1 VALUES (0);
Query OK, 1 row affected (0.00 sec)

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

mysql> SELECT * FROM t1 LEFT JOIN t0 ON t0.c0=t1.c0 WHERE NOT NULLIF(t1.c0, t0.c0);
+------+------+
| c0   | c0   |
+------+------+
|    0 | NULL |
+------+------+
1 row in set (0.01 sec)

Small changes that should result in the same result set fetch an empty result set, as expected:

SELECT * FROM t1 LEFT JOIN t0 ON t0.c0=t1.c0 WHERE NOT NULLIF(0, t0.c0); -- {}
SELECT * FROM t1 LEFT JOIN t0 ON t0.c0=0 WHERE NOT NULLIF(t1.c0, t0.c0); -- {}
SELECT * FROM t1 LEFT JOIN t0 ON TRUE WHERE NOT NULLIF(t1.c0, t0.c0); -- {}

I found this based on the latest master commit (a1dc18da4b712067beecf1032885869040010d62), and can also reproduce this on the 4.0 RC.

When using MySQL 8.0.19, this works as expected:

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 INTEGER);
Query OK, 0 rows affected (0.06 sec)

mysql> CREATE TABLE t1(c0 INTEGER);
Query OK, 0 rows affected (0.06 sec)

mysql> INSERT INTO t1 VALUES (0);
Query OK, 1 row affected (0.00 sec)

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

mysql> SELECT * FROM t1 LEFT JOIN t0 ON t0.c0=t1.c0 WHERE NOT NULLIF(t1.c0, t0.c0);
Empty set (0.00 sec)
shuke987 commented 4 years ago

/bug P1