[MDEV-6972] Left joined subquery gives wrong result Created: 2014-10-29 Updated: 2015-02-15 Resolved: 2015-02-15 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Views |
| Affects Version/s: | 5.3.12, 5.5.40, 10.0.14 |
| Fix Version/s: | N/A |
| Type: | Bug | Priority: | Critical |
| Reporter: | Dennis Minderhoud | Assignee: | Oleksandr Byelkin |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | optimizer | ||
| Environment: |
Windows Server 2012 |
||
| Issue Links: |
|
||||||||||||
| Description |
|
The result of the query stated below gives always an amount of 1.
When this query was run on MySQL 5.6, the result was as expected;
Result MySQL:
|
| Comments |
| Comment by Elena Stepanova [ 2014-10-29 ] | ||||||||||||||||||||||||||||
|
Thanks for the report. It is likely to be a duplicate of | ||||||||||||||||||||||||||||
| Comment by Oleksandr Byelkin [ 2014-11-17 ] | ||||||||||||||||||||||||||||
|
The problem is in table elimination. With table_elimination=off we have correct result also derived table is absent in the EXPLAIN output. | ||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2014-11-19 ] | ||||||||||||||||||||||||||||
|
EXPLAIN shows that table elimination removed t2:
This happened, because ON expression is
AND pk is:
However, the query's select list is :
it has t2.amount . This means, t2 could not be eliminated. Need to investigate why table elimination still eliminated the table. | ||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2014-11-19 ] | ||||||||||||||||||||||||||||
|
Debugging in eliminate_tables:
For some reason, "t2.amount" was replaced with constant "1". This is why Table Elimination thinks it can eliminate table t2. | ||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2014-11-19 ] | ||||||||||||||||||||||||||||
|
The reason to replace t2.amount with "1" was that "amount" is not a real field. It is a constant:
| ||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2014-11-19 ] | ||||||||||||||||||||||||||||
|
I wonder, if the select list has "coalesce(1,0)", how could it be that it has different values when table_elimination=off ? The answer is:
Apparently, Item_direct_view_ref that points to a constant will still check if certain table is NULL, and return different values depending on that:
Based on this, I think that Item_func_direct_view_ref() should not return 0 from item->used_tables(). It should return null_ref_table, if it has one. | ||||||||||||||||||||||||||||
| Comment by Oleksandr Byelkin [ 2015-02-14 ] | ||||||||||||||||||||||||||||
|
I feel Déjà vu here. I definetely alredy was adding null_ref_table dependency to used_tables(). (The patch probably lost somewhere) | ||||||||||||||||||||||||||||
| Comment by Oleksandr Byelkin [ 2015-02-14 ] | ||||||||||||||||||||||||||||
|
The usage of null_ref_table have to be limited somehow in LEFT JOIN... | ||||||||||||||||||||||||||||
| Comment by Oleksandr Byelkin [ 2015-02-15 ] | ||||||||||||||||||||||||||||
|
It is duplicate of |