pingcap / parser

A MySQL Compatible SQL Parser
Apache License 2.0
1.41k stars 490 forks source link

Failed to parse WHERE / GROUP BY clauses in a SELECT statement without a FROM table #115

Open kennytm opened 5 years ago

kennytm commented 5 years ago

These SQLs:

select 1 where true;
select 1 group by 1;
select 1 having true;

All return one row with the value 1 on MySQL 8.0 (also tested on PostgreSQL 9.6 and SQLite 3.24 with the same behavior), but failed to parse on TiDB 2.1.1 and master:

mysql> select 1 where true;
ERROR 1105 (HY000): line 1 column 14 near " true" (total length 19)
mysql> select 1 group by 1;
ERROR 1105 (HY000): line 1 column 14 near " by 1" (total length 19)
mysql> select 1 having true;
ERROR 1105 (HY000): line 1 column 15 near " true" (total length 20)

Table-less queries which are correctly accepted by TiDB:

select 1 order by 1;
select 1 limit 1;
zhenghaoz commented 4 years ago

Let me fix it please :smile:

wangggong commented 4 years ago

Hey, I wanna fix this issue. I read the discussion, and wondering:

  1. What is the conclusion of these SQL shown in pr #587 :
SELECT @@autocommit limit 1; -- @@autocommit:1 or `@@autocommit limit 1`:1?
select 1 where true group by 1 having 1>0; -- Throw an error? Or just return 1:1?
  1. If the case is going to be fixed, is it ok to just fix the SQL shown in this pr? Or maybe the overwrite of SELECT statement is needed?

If there is a clear conclusion, I'll be happy to fix it if I can. Otherwise maybe I can go out and see other cases ^_^.

PS: I also read doc in mysql (https://dev.mysql.com/doc/refman/8.0/en/select.html), no clear conclusion found.

kennytm commented 4 years ago

Follow MySQL's behavior on doubt.

mysql> SELECT @@autocommit limit 1;
+--------------+
| @@autocommit |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)

mysql> select 1 where true group by 1 having 1>0;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.01 sec)