[MDEV-9028] Incorrect query result when combining DISTINCT, LEFT JOIN and subquery with constant column Created: 2015-10-28  Updated: 2017-02-20  Resolved: 2017-02-20

Status: Closed
Project: MariaDB Server
Component/s: Data Manipulation - Subquery
Affects Version/s: 5.5.43, 5.5, 10.0, 10.1
Fix Version/s: 5.5.55

Type: Bug Priority: Major
Reporter: Nathan Lewis Assignee: Igor Babaev
Resolution: Fixed Votes: 1
Labels: regression, wrong_result
Environment:

Ubuntu 12


Sprint: 5.5.55

 Description   

There appears to be a problem with the results returned from a query when DISTINCT, LEFT JOIN and a subquery which contains a constant value are combined.

I've boiled down the problem into a simple example of how it fails:

SELECT DISTINCT realtable.id AS realid, virtualtable.id AS virtualid, virtualtable.recordexists
FROM realtable
LEFT JOIN (SELECT realtable.id, "yes" AS recordexists FROM realtable) virtualtable
ON realtable.id = virtualtable.id

If you create a table called "realtable" with columns int "id" and string "data", insert two records (1, "yes") and (2, "yes") and run the query then you'll get:

realid, virtualid, recordexists
1, 1, null
2, 2, null

which is incorrect. The value "yes" should be returned in the recordexists column. Obviously the "virtual" records are being joined correctly to the "real" records, because the virtualid returns the correct value, but the constant "yes" is missing. If you remove DISTINCT, change LEFT JOIN to just JOIN, or change "yes" to realtable.data then you get the correct results:

realid, virtualid, recordexists
1, 1, yes
2, 2, yes

I also confirmed that this is NOT caused by using the same table in both the main and sub queries.

I've got a query structured like this which is working in Postgress and MySQL, and when delivered to a client who tried it in MariaDB it started failing. This could probably be considered a data loss bug.



 Comments   
Comment by Elena Stepanova [ 2015-10-29 ]

nathanl, thanks for the report.


Test case

create table realtable (id int, data varchar(255));
insert into realtable values (1,'yes'),(2,'yes');
 
SELECT DISTINCT realtable.id AS realid, virtualtable.id AS virtualid, virtualtable.recordexists
FROM realtable
LEFT JOIN (SELECT realtable.id, 'yes' AS recordexists FROM realtable) virtualtable
ON realtable.id = virtualtable.id;
 
drop table realtable;

The problem was introduced by the following revision in 5.5.43:

commit 20109712aeb3d23e5e975780897ad236cbcd2ddc
Author: Oleksandr Byelkin <sanja@mariadb.com>
Date:   Tue Apr 14 23:18:54 2015 +0200
 
    MDEV-6892: WHERE does not apply
    
    Taking into account implicit dependence of constant view field from nullable table of left join added.
    
    Fixed finding real table to check if it turned to NULL (materialized view & derived taken into account)
    
    Removed incorrect uninitialization.

Comment by Oleksandr Byelkin [ 2017-02-17 ]

OK to push! Thank you a lot for the fix!

Comment by Igor Babaev [ 2017-02-20 ]

The fix for this bug was pushed into the 5.5 tree.

Generated at Thu Feb 08 07:31:35 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.