tidb icon indicating copy to clipboard operation
tidb copied to clipboard

Output is different from mysql where clause used in with dual table

Open Alkaagr81 opened this issue 3 years ago • 1 comments

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

CREATE TABLE t1 (id int NOT NULL,c1 int NOT NULL,c2 int,
PRIMARY KEY(id),INDEX c1_c2_idx(c1, c2));
INSERT INTO t1 (id, c1, c2) VALUES (1,1,1), (2,2,2), (10,10,1), (11,10,8),
(12,10,1), (13,10,2); 
SELECT DISTINCT c1 FROM t1 WHERE 1 IN (2, (SELECT 1 FROM DUAL WHERE (c2 = 2)), 3);
SELECT DISTINCT c1 FROM t1 IGNORE INDEX (c1_c2_idx) WHERE EXISTS (SELECT * FROM DUAL WHERE (c2 = 2));

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

mysql> CREATE TABLE t1 (id int NOT NULL,c1 int NOT NULL,c2 int,
    -> PRIMARY KEY(id),INDEX c1_c2_idx(c1, c2));
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO t1 (id, c1, c2) VALUES (1,1,1), (2,2,2), (10,10,1), (11,10,8),
    -> (12,10,1), (13,10,2); 
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> SELECT DISTINCT c1 FROM t1 WHERE 1 IN (2, (SELECT 1 FROM DUAL WHERE (c2 = 2)), 3);
+----+
| c1 |
+----+
|  2 |
| 10 |
+----+
2 rows in set (0.00 sec)

mysql> SELECT DISTINCT c1 FROM t1 IGNORE INDEX (c1_c2_idx) WHERE EXISTS (SELECT * FROM DUAL WHERE (c2 = 2));
+----+
| c1 |
+----+
|  2 |
| 10 |
+----+
2 rows in set (0.00 sec)

3. What did you see instead (Required)

mysql> CREATE TABLE t1 (id int NOT NULL,c1 int NOT NULL,c2 int,
    -> PRIMARY KEY(id),INDEX c1_c2_idx(c1, c2));
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO t1 (id, c1, c2) VALUES (1,1,1), (2,2,2), (10,10,1), (11,10,8),
    -> (12,10,1), (13,10,2); 
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> SELECT DISTINCT c1 FROM t1 WHERE 1 IN (2, (SELECT 1 FROM DUAL WHERE (c2 = 2)), 3);
+----+
| c1 |
+----+
|  2 |
|  1 |
| 10 |
+----+
3 rows in set (0.01 sec)

mysql> SELECT DISTINCT c1 FROM t1 IGNORE INDEX (c1_c2_idx) WHERE EXISTS (SELECT * FROM DUAL WHERE (c2 = 2));
ERROR 1051 (42S02): Unknown table ''

4. What is your TiDB version? (Required)


| Release Version: v5.4.0-alpha-184-g51f53f337-dirty
Edition: Community
Git Commit Hash: 51f53f3376bd548b302f654d6c11096f89119145
Git Branch: master
UTC Build Time: 2021-11-23 02:51:51
GoVersion: go1.16.9
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |

Alkaagr81 avatar Jan 03 '22 20:01 Alkaagr81

I can't reproduce the first case:

+----+
| c1 |
+----+
|  2 |
| 10 |
+----+

Maybe it's been fixed by some other PR targeting some other bug.

However case 2 still exists. I'd change the label to route it to planner team.

zanmato1984 avatar May 15 '22 08:05 zanmato1984

A minimum reproducible case:

mysql> select * from dual;
ERROR 1051 (42S02): Unknown table ''

The reason is that dual is a virtual table that has no column, MySQL returns a similar error when executing this query:

mysql> select * from dual;
ERROR 1096 (HY000): No tables used

This error seems acceptable in this case, degrade this issue from Major to Minor.

qw4990 avatar May 16 '24 07:05 qw4990