Comment 2 for bug 884184

Revision history for this message
Igor Babaev (igorb-seattle) wrote :

This bug can be reproduced as well with:
 - LEFT JOIN instead of RIGHT JOIN
 - view instead of derived table.

The bug is reproducible in 5.2 (and most probably in 5.1):

igor@sophia:~/maria/maria-5.2/mysql-test> ../client/mysql test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

MariaDB [test]> DROP TABLE IF EXISTS t1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [test]> CREATE TABLE t1 ( a int(11), b varchar(1)) ;
Query OK, 0 rows affected (0.01 sec)

MariaDB [test]> INSERT IGNORE INTO t1 VALUES (0,'g');
Query OK, 1 row affected (0.00 sec)

MariaDB [test]>
MariaDB [test]> DROP TABLE IF EXISTS t3;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [test]> CREATE TABLE t3 ( a varchar(1)) ;
Query OK, 0 rows affected (0.01 sec)

MariaDB [test]> INSERT IGNORE INTO t3 VALUES ('g');
Query OK, 1 row affected (0.00 sec)

MariaDB [test]>
MariaDB [test]> DROP TABLE IF EXISTS t2;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [test]> CREATE TABLE t2 ( a int(11) NOT NULL, PRIMARY KEY (a)) ;
Query OK, 0 rows affected (0.01 sec)

MariaDB [test]> create view v1 as SELECT t1.* FROM t1 LEFT JOIN t2 ON ( t1.a = t2.a ) WHERE t2.a <> 0;
Query OK, 0 rows affected (0.01 sec)

MariaDB [test]> SELECT alias1.* FROM t3 LEFT JOIN v1 as alias1 ON ( t3.a = alias1.b );
+------+------+
| a | b |
+------+------+
| 0 | g |
+------+------+
1 row in set (0.00 sec)

MariaDB [test]> EXPLAIN SELECT alias1.* FROM t3 LEFT JOIN v1 as alias1 ON ( t3.a = alias1.b );
+----+-------------+-------+--------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | t3 | system | NULL | NULL | NULL | NULL | 1 | |
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
+----+-------------+-------+--------+---------------+------+---------+------+------+-------------+
2 rows in set (0.00 sec)

This is a bug of table elimination:

MariaDB [test]> set optimizer_switch='table_elimination=off';
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> EXPLAIN SELECT alias1.* FROM t3 LEFT JOIN v1 as alias1 ON ( t3.a = alias1.b );
+----+-------------+-------+--------+---------------+---------+---------+-----------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+-----------+------+--------------------------+
| 1 | SIMPLE | t3 | system | NULL | NULL | NULL | NULL | 1 | |
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
| 1 | SIMPLE | t2 | eq_ref | PRIMARY | PRIMARY | 4 | test.t1.a | 1 | Using where; Using index |
+----+-------------+-------+--------+---------------+---------+---------+-----------+------+--------------------------+
3 rows in set (0.00 sec)

MariaDB [test]> SELECT alias1.* FROM t3 LEFT JOIN v1 as alias1 ON ( t3.a = alias1.b );
+------+------+
| a | b |
+------+------+
| NULL | NULL |
+------+------+
1 row in set (0.00 sec)