Bug #53161 | Left join optimized into join leads to incorrect results | ||
---|---|---|---|
Submitted: | 26 Apr 2010 13:12 | Modified: | 18 Jan 2013 1:51 |
Reporter: | Adam Trotter | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S1 (Critical) |
Version: | 5.1.37, 5.1.46, 5.1.47 | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | GROUP BY, join, left join, Optimizer |
[26 Apr 2010 13:12]
Adam Trotter
[26 Apr 2010 13:30]
Valeriy Kravchuk
Thank you for the bug report. Verified just as described: 77-52-4-109:5.1 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 33 Server version: 5.1.47-debug Source distribution Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> drop temporary table if exists t1; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> create temporary table t1 -> ( -> id char(50) not null, -> type tinyint(1) not null, -> primary key(id) -> ); Query OK, 0 rows affected (0.01 sec) mysql> drop temporary table if exists t2; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> drop temporary table if exists t3; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> create temporary table t2 like t1; Query OK, 0 rows affected (0.01 sec) mysql> create temporary table t3 like t1; Query OK, 0 rows affected (0.00 sec) mysql> insert into t1 set id='001', type=1; Query OK, 1 row affected (0.00 sec) mysql> insert into t2 set id='001:201', type=2; Query OK, 1 row affected (0.00 sec) mysql> insert into t3 set id='001:201:301', type=3; Query OK, 1 row affected (0.00 sec) mysql> drop temporary table if exists t4; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> create temporary table t4 -> ( -> id char(50) not null, -> primary key(id) -> ); Query OK, 0 rows affected (0.00 sec) mysql> select t2.id, -> (select t3.id -> from t3 -> left join t4 on t4.id=t3.id -> where t3.type=3 -> and t3.id rlike concat(t2.id, ':[[:alnum:]]{3}$') -> limit 1 -> ) as bug -> from t1 -> join t2 on t2.type = 2 -> and t2.id rlike concat('^', t1.id, ':[[:alnum:]]{3}$') -> where t1.id='001' -> ; +---------+-------------+ | id | bug | +---------+-------------+ | 001:201 | 001:201:301 | +---------+-------------+ 1 row in set (0.04 sec) mysql> select t2.id, -> (select t3.id -> from t3 -> left join t4 on t4.id=t3.id -> where t3.type=3 -> and t3.id rlike concat(t2.id, ':[[:alnum:]]{3}$') -> limit 1 -> ) as bug -> from t1 -> join t2 on t2.type = 2 -> and t2.id rlike concat('^', t1.id, ':[[:alnum:]]{3}$') -> where t1.id='001' -> group by t2.id; +---------+------+ | id | bug | +---------+------+ | 001:201 | NULL | +---------+------+ 1 row in set (0.00 sec) mysql> explain select t2.id, (select t3.id from t3 left join t4 on t4.id=t3.id where t3.type=3 and t3.id rlike concat(t2.id, ':[[:alnum:]]{3}$') limit 1 ) as bug from t1 join t2 on t2.type = 2 and t2.id rlike concat('^', t1.id, ':[[:alnum:]]{3}$') where t1.id='001' group by t2.id; +----+--------------------+-------+--------+---------------+------+---------+------+------+-----------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+-------+--------+---------------+------+---------+------+------+-----------------------------------------------------+ | 1 | PRIMARY | t1 | system | PRIMARY | NULL | NULL | NULL | 1 | | | 1 | PRIMARY | t2 | system | NULL | NULL | NULL | NULL | 1 | | | 2 | DEPENDENT SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables | +----+--------------------+-------+--------+---------------+------+---------+------+------+-----------------------------------------------------+ 3 rows in set (0.03 sec) mysql> explain select t2.id, (select t3.id from t3 left join t4 on t4.id=t3.id where t3.type=3 and t3.id rlike concat(t2.id, ':[[:alnum:]]{3}$') limit 1 ) as bug from t1 join t2 on t2.type = 2 and t2.id rlike concat('^', t1.id, ':[[:alnum:]]{3}$') where t1.id='001'; +----+--------------------+-------+--------+---------------+------+---------+------+------+---------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+-------+--------+---------------+------+---------+------+------+---------------------+ | 1 | PRIMARY | t1 | system | PRIMARY | NULL | NULL | NULL | 1 | | | 1 | PRIMARY | t2 | system | NULL | NULL | NULL | NULL | 1 | | | 2 | DEPENDENT SUBQUERY | t3 | system | NULL | NULL | NULL | NULL | 1 | | | 2 | DEPENDENT SUBQUERY | t4 | system | PRIMARY | NULL | NULL | NULL | 0 | const row not found | +----+--------------------+-------+--------+---------------+------+---------+------+------+---------------------+ 4 rows in set (0.00 sec)
[26 Apr 2010 13:34]
Valeriy Kravchuk
Same results with 5.0.91 and 5.1.37, so this is not a recent regression.
[18 Jan 2013 1:51]
Paul DuBois
Noted in 5.1.68, 5.5.31 changelogs. Subqueries with OUTER JOIN could return incorrect results if the subquery referred to a column from another SELECT.