pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://www.pingcap.com/tidb-serverless/
https://pingcap.com
Apache License 2.0
36.96k stars 5.82k forks source link

Queries with wrong column name lists can execute #29549

Open xuyifangreeneyes opened 2 years ago

xuyifangreeneyes commented 2 years ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

use test;
drop table if exists t;
create table t (a int, b int, c int);
set @@tidb_enable_extended_stats = on;

/* first sql */
select * from t as t1 join t as t2 using (t1.a);

/* second sql */
analyze table t update histogram on t.a;

/* third sql */
analyze table t drop histogram on t.a;

/* fourth sql */
analyze table t columns t1.a;

/* fifth sql */
alter table t add stats_extended s1 correlation(t1.a, b);

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

MySQL reports syntax error for the first three sqls.

mysql> select * from t as t1 join t as t2 using (t1.a);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.a)' at line 1
mysql> analyze table t update histogram on t.a;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.a' at line 1
mysql> analyze table t drop histogram on t.a;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.a' at line 1

Both the fourth sql and the fifth sql are wrong and TiDB should report error(analyze columns and extended stats are TiDB's own syntax).

3. What did you see instead (Required)

All the sqls run successfully.

mysql> select * from t as t1 join t as t2 using (t1.a);
Empty set (0.00 sec)

mysql> analyze table t update histogram on t.a;
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> analyze table t drop histogram on t.a;
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> analyze table t columns t1.a;
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> alter table t add stats_extended s1 correlation(t1.a, b);
Query OK, 0 rows affected (0.01 sec)

4. What is your TiDB version? (Required)

mysql> SELECT tidb_version();
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                                                                                                             |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v5.3.0-alpha-1352-g4df2c21a9
Edition: Community
Git Commit Hash: 4df2c21a94da3c211a129bb190b37d1649a61ed9
Git Branch: master
UTC Build Time: 2021-11-08 02:23:21
GoVersion: go1.16.4
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
xuyifangreeneyes commented 2 years ago

Some other similar errors also occur in create table partition by and alter table add column

xuyifangreeneyes commented 2 years ago

The root cause is that in parser.y, we use ColumnName for all the places. ColumnName can parser db.tbl.col, tbl.col and col. However, in some places only col is allowed while db.tbl.col and tbl.col are not allowed.

xuyifangreeneyes commented 2 years ago
create table t (a int, b int);

MySQL:

mysql> alter table t add column t.c int;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.c int' at line 1

TiDB:

mysql> alter table t add column t.c int;
Query OK, 0 rows affected (0.02 sec)
xuyifangreeneyes commented 2 years ago

MySQL:

mysql> create table t (a int, t.b int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.b int)' at line 1

TiDB:

mysql> create table t (a int, t.b int);
Query OK, 0 rows affected (0.01 sec)