[MDEV-6919] wrong result from subquery - too much optimization? Created: 2014-10-23 Updated: 2015-08-04 Resolved: 2015-08-04 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Data Manipulation - Subquery, Optimizer |
| Affects Version/s: | 5.3.12, 5.5.33a, 5.5.40, 10.0.14 |
| Fix Version/s: | 5.5.43, 10.0.18 |
| Type: | Bug | Priority: | Major |
| Reporter: | Christian Boltz | Assignee: | Oleksandr Byelkin |
| Resolution: | Fixed | Votes: | 1 |
| Labels: | None | ||
| Environment: |
openSUSE 13.1, mariadb-5.5.33-2.2.x86_64 (from official distribution repo) |
||
| Issue Links: |
|
||||||||||||
| Sprint: | 5.5.45 | ||||||||||||
| Description |
|
MariaDB gives me a wrong result when using a subquery. The affected query is (scroll down for full reproducer with table structure):
It looks like MariaDB optimizes the subquery to "always 1" if the other fields from the subquery are unused or only used in the JOIN condition. The workaround is to add __mailbox_username to the outer SELECT. This adds a superfluous column to the result, but gives the correct result for is_mailbox. This broke alias deletion in PostfixAdmin, see https://sourceforge.net/p/postfixadmin/bugs/325/ The query works fine in MySQL (I tested on mysql-community-server-5.6.17-3.1.x86_64 on current openSUSE Factory), so this bug is specific to MariaDB. Full reproducer: a) create tables and insert some test rows:
b) now run the query:
Needless to say that the result for alias@example.com is wrong. EXPLAIN clearly shows that the subquery is optimized away: EXPLAIN SELECT address,coalesce(__is_mailbox,0) as is_mailbox, __is_mailbox FROM alias
Now add __mailbox_username to the outer SELECT: SELECT address,coalesce(__is_mailbox,0) as is_mailbox, __is_mailbox, __mailbox_username FROM alias
2 rows in set (0.01 sec) The modified query contains the correct result (and a superfluous column in the resultset). For completeness, here's the EXPLAIN for the working query: EXPLAIN SELECT address,coalesce(__is_mailbox,0) as is_mailbox, __is_mailbox, __mailbox_username FROM alias LEFT JOIN ( SELECT 1 as __is_mailbox, username as __mailbox_username FROM mailbox WHERE username IS NOT NULL ) AS __mailbox ON __mailbox_username = address;
|
| Comments |
| Comment by Elena Stepanova [ 2014-10-23 ] | ||||||||||||||||||||||||||||||||||
|
Thanks for the report. As a workaround, please try to set optimizer_switch='derived_merge=off'. It is likely to be a duplicate of | ||||||||||||||||||||||||||||||||||
| Comment by Oleksandr Byelkin [ 2015-08-02 ] | ||||||||||||||||||||||||||||||||||
|
It looks like the bug is already fixed: CREATE TABLE `alias` ( | ||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2015-08-04 ] | ||||||||||||||||||||||||||||||||||
|
On revision 20109712aeb3d23e5e975780897ad236cbcd2ddc "(
On 8cbaafd22b145512cc91f7b512290320849e77bd (the revision right before 20109712aeb3d23e5e975780897ad236cbcd2ddc "(
So, I think we can be reasonably sure the bug was fixed along with |