[MDEV-6417] Wrong results when using join and group Created: 2014-07-03  Updated: 2015-02-18  Resolved: 2015-02-18

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.0.10, 10.0.12
Fix Version/s: 10.0.16

Type: Bug Priority: Major
Reporter: Tomasz Assignee: Sergei Petrunia
Resolution: Duplicate Votes: 0
Labels: None
Environment:

Ubuntu 14.04
10.0.12-MariaDB-1~trusty-log - mariadb.org binary distribution


Attachments: Text File bug_interesting.txt     Text File bugsql.txt     Text File explain.txt     File my.cnf    
Issue Links:
Relates
relates to MDEV-5719 Wrong result with GROUP BY and LEFT O... Closed

 Description   

I am using two tables joined together. If I use DISTINCT everything is OK, but I need unique rows. Using GROUP BY returns NULL. In old mysql 5.5 everything works fine.

Query 1 bad results:

SELECT items_6 . * , cm1.brand AS brand1, cm2.model AS model1
FROM items_6
LEFT JOIN cars_models AS cm1 ON cm1.brandid = items_6.brand
LEFT JOIN cars_models AS cm2 ON cm2.id = items_6.model
GROUP BY items_6.id

Query 2 - good results, but not unique by items_6.id

SELECT DISTINCT items_6 . * , cm1.brand AS brand1, cm2.model AS model1
FROM items_6
LEFT JOIN cars_models AS cm1 ON cm1.brandid = items_6.brand
LEFT JOIN cars_models AS cm2 ON cm2.id = items_6.model
GROUP BY items_6.id

Export and results provided in attachment.



 Comments   
Comment by Tomasz [ 2014-07-03 ]

I find that this query works OK:

SELECT * 
FROM (
SELECT DISTINCT items_6. * , cm1.brand AS brand1, cm2.model AS model1
FROM items_6
LEFT JOIN cars_models AS cm1 ON cm1.brandid = items_6.brand
LEFT JOIN cars_models AS cm2 ON cm2.id = items_6.model
) t
GROUP BY t.id

Comment by Elena Stepanova [ 2014-07-03 ]

Hi,

Thanks for the report.

The wrong result from query 1 seems to be very similar to bug MDEV-5719, reproducible on the current 10.0 tree.

Query 1 - bad results:
SELECT items_6 . * , cm1.brand AS brand1, cm2.model AS model1
FROM items_6
LEFT JOIN cars_models AS cm1 ON cm1.brandid = items_6.brand
LEFT JOIN cars_models AS cm2 ON cm2.id = items_6.model
GROUP BY items_6.id
+----+-------+-------+----------+--------+--------+
| id | brand | model | imported | brand1 | model1 |
+----+-------+-------+----------+--------+--------+
| 28 |    54 |   618 |        0 | NULL   | NULL   |
| 29 |    65 |  1133 |        0 | NULL   | NULL   |
| 30 |   123 |     0 |        0 | NULL   | NULL   |
+----+-------+-------+----------+--------+--------+

However, I cannot reproduce the non-unique result from query 2. Could you please confirm that you are really getting this (copy-paste from the attachment):

Query 2 - good results, but not unique by items_6.id:
SELECT DISTINCT items_6 . * , cm1.brand AS brand1, cm2.model AS model1
FROM items_6
LEFT JOIN cars_models AS cm1 ON cm1.brandid = items_6.brand
LEFT JOIN cars_models AS cm2 ON cm2.id = items_6.model
GROUP BY items_6.id
+----+-------+-------+----------+------------+--------+
| id | brand | model | imported | brand1     | model1 |
+----+-------+-------+----------+------------+--------+
| 28 |    54 |   618 |        0 | NULL       | NULL   |
| 29 |    65 |  1133 |        0 | Volkswagen | Touran |
| 29 |    65 |  1133 |        0 | VW         | Touran |
| 30 |   123 |     0 |        0 | NULL       | NULL   |
+----+-------+-------+----------+------------+--------+

The result looks like something you would get without GROUP BY clause.

If you are really indeed getting it, could you please also paste the output of

EXPLAIN EXTENDED
SELECT DISTINCT items_6 . * , cm1.brand AS brand1, cm2.model AS model1
FROM items_6
LEFT JOIN cars_models AS cm1 ON cm1.brandid = items_6.brand
LEFT JOIN cars_models AS cm2 ON cm2.id = items_6.model
GROUP BY items_6.id;
SHOW WARNINGS;

and attach your cnf file .

Thanks.

Comment by Tomasz [ 2014-07-03 ]

Sorry, I made an mistake when copying queries from database. Updated bugsql.txt, added my.cnf

Comment by Tomasz [ 2014-07-03 ]

explain.txt - requested query, but changed after mistake. Warning is from time, when I tested the query on more complex table.

Comment by Elena Stepanova [ 2014-07-04 ]

Okay, thanks for clarification.
Without GROUP BY, the result of the second query looks okay, no issues there.
The problem with query 1 looks the same as or similar to MDEV-5719, I will assign it to psergey to confirm.
Here is EXPLAIN from the 1st query:

EXPLAIN EXTENDED SELECT items_6 . * , cm1.brand AS brand1, cm2.model AS model1 FROM items_6 LEFT JOIN cars_models AS cm1 ON cm1.brandid = items_6.brand LEFT JOIN cars_models AS cm2 ON cm2.id = items_6.model GROUP BY items_6.id;
+------+-------------+---------+--------+---------------+---------+---------+--------------------+------+----------+-------------+
| id   | select_type | table   | type   | possible_keys | key     | key_len | ref                | rows | filtered | Extra       |
+------+-------------+---------+--------+---------------+---------+---------+--------------------+------+----------+-------------+
|    1 | SIMPLE      | items_6 | index  | NULL          | PRIMARY | 4       | NULL               |    3 |   100.00 |             |
|    1 | SIMPLE      | cm1     | ref    | brandid       | brandid | 2       | test.items_6.brand |   11 |   100.00 | Using where |
|    1 | SIMPLE      | cm2     | eq_ref | PRIMARY       | PRIMARY | 2       | test.items_6.model |    1 |   100.00 | Using where |
+------+-------------+---------+--------+---------------+---------+---------+--------------------+------+----------+-------------+
 
| Note  | 1003 | select `test`.`items_6`.`id` AS `id`,`test`.`items_6`.`brand` AS `brand`,`test`.`items_6`.`model` AS `model`,`test`.`items_6`.`imported` AS `imported`,`test`.`cm1`.`brand` AS `brand1`,`test`.`cm2`.`model` AS `model1` from `test`.`items_6` left join `test`.`cars_models` `cm1` on(((`test`.`cm1`.`brandid` = `test`.`items_6`.`brand`) and (`test`.`items_6`.`brand` is not null))) left join `test`.`cars_models` `cm2` on(((`test`.`cm2`.`id` = `test`.`items_6`.`model`) and (`test`.`items_6`.`model` is not null))) where 1 group by `test`.`items_6`.`id` |

Comment by Tomasz [ 2014-07-04 ]

This is very interesting, when I delete line ID=30 everything works fine. (see attachment bug_interesting.txt)

Comment by Sergei Petrunia [ 2015-02-18 ]

Elena's guess was correct - this issue is fixed by the fix for MDEV-5719.

Comment by Sergei Petrunia [ 2015-02-18 ]

Marking as duplicate of MDEV-5719

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