Bug #65702 parentheses around table with union
Submitted: 21 Jun 2012 21:01 Modified: 20 Aug 2013 20:11
Reporter: Richard Ejem Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Parser Severity:S3 (Non-critical)
Version:5.5.9, 5.5.25, 5.5.26 OS:Any (win 7 x64)
Assigned to: CPU Architecture:Any
Tags: parentheses, regression, UNION

[21 Jun 2012 21:01] Richard Ejem
Description:
Closing a source in the FROM clause to parentheses in combination with an UNION causes a parse error.

Because parentheses work without using a union, I think it is a bug or at least strange behavior.

My friend tested it on 5.1.63 and the bug did not appeared there - all queries listed below ran successfully.
After few minutes of googling I`ve found mentions about this bug in versions prior to 5.0, so it seems that the bug re-appeared again (discovered on 5.5.9, then upgraded to last 5.5.25 where bug is still present).

How to repeat:
Both these queries ran on MySQL database in versions presented above cause a parse error:
SELECT * FROM ((select 1) x) UNION SELECT * FROM ((select 2) y)
SELECT * FROM (test) UNION SELECT * FROM (test)

These work fine:
SELECT * FROM (select 1) x UNION SELECT * FROM (select 2) y
SELECT * FROM ((select 1) x)
SELECT * FROM test UNION SELECT * FROM test
SELECT * FROM (test)

Suggested fix:
Syntax with parentheses should be correctly parsed.
[22 Jun 2012 4:17] Valeriy Kravchuk
Thank you for the bug report. Indeed, it works in 5.1.x:

macbook-pro:5.5 openxs$ bin/mysql -uroot test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.64-debug Source distribution

Copyright (c) 2000, 2011, 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 * FROM ((select 1) x) UNION SELECT * FROM ((select 2) y);
+---+
| 1 |
+---+
| 1 |
| 2 |
+---+
2 rows in set (0.07 sec)

but NOT in 5.5.x:

macbook-pro:5.5 openxs$ bin/mysql -uroot test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.26-debug Source distribution

Copyright (c) 2000, 2011, 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 * FROM ((select 1) x) UNION SELECT * FROM ((select 2) y);
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 '' at line 1

See bug #65381 also. Root case may be the same.
[20 Aug 2013 14:16] Gleb Shchepa
The duplicate of this bug has been fixed in 5.5.32.