Bug #48046 | Server incorrectly processing JOINs on NULL values | ||
---|---|---|---|
Submitted: | 14 Oct 2009 16:27 | Modified: | 20 Dec 2010 13:21 |
Reporter: | Patrick Crews | Email Updates: | |
Status: | Duplicate | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S3 (Non-critical) |
Version: | 5.1 | OS: | Any |
Assigned to: | Assigned Account | CPU Architecture: | Any |
Tags: | null, OUTER JOIN |
[14 Oct 2009 16:27]
Patrick Crews
[15 Oct 2009 13:46]
Øystein Grøvlen
Assigning this to me since it looks very similar to Bug#47217.
[19 Oct 2009 15:39]
Patrick Crews
EXPLAIN output: Query 0: 6.0: EXPLAIN SELECT table1 .`time_key` field1 , table2 .`pk` FROM BB table1 LEFT JOIN BB table2 ON table2 .`varchar_nokey` = table1 .`varchar_key` HAVING field1 ; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE table1 system NULL NULL NULL NULL 1 1 SIMPLE table2 system NULL NULL NULL NULL 1 5.1: EXPLAIN SELECT table1 .`time_key` field1 , table2 .`pk` FROM BB table1 LEFT JOIN BB table2 ON table2 .`varchar_nokey` = table1 .`varchar_key` HAVING field1 ; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE table1 system NULL NULL NULL NULL 1 1 SIMPLE table2 system NULL NULL NULL NULL 1 Query 1: 6.0: EXPLAIN SELECT table1 . `time_key` AS field1 , table2 . `pk` AS field2 , table2 . `int_nokey` AS field3 , table1 . `varchar_nokey` AS field4 , table2 . `datetime_key` AS field5 FROM ( BB AS table1 LEFT JOIN BB AS table2 ON (table2 . `varchar_nokey` = table1 . `varchar_key` ) ) GROUP BY field1, field2, field3, field4, field5 HAVING field1 <= 'y' ORDER BY table1 . `time_key` , field2 DESC; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE table1 system NULL NULL NULL NULL 1 1 SIMPLE table2 system NULL NULL NULL NULL 1 Warnings: Warning 1292 Incorrect time value: 'y' for column 'time_key' at row 1 5.1: EXPLAIN SELECT table1 . `time_key` AS field1 , table2 . `pk` AS field2 , table2 . `int_nokey` AS field3 , table1 . `varchar_nokey` AS field4 , table2 . `datetime_key` AS field5 FROM ( BB AS table1 LEFT JOIN BB AS table2 ON (table2 . `varchar_nokey` = table1 . `varchar_key` ) ) GROUP BY field1, field2, field3, field4, field5 HAVING field1 <= 'y' ORDER BY table1 . `time_key` , field2 DESC; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE table1 system NULL NULL NULL NULL 1 1 SIMPLE table2 system NULL NULL NULL NULL 1 Warnings: Warning 1292 Incorrect time value: 'y' for column 'time_key' at row 1
[19 Nov 2009 11:54]
Øystein Grøvlen
6.0 shows the correct behavior. The query is a left outer join between tow tables. Wikipedia says the following about left outer joins: "The result of a left outer join (or simply left join) for table A and B always contains all records of the "left" table (A), even if the join-condition does not find any matching record in the "right" table (B). This means that if the ON clause matches 0 (zero) records in B, the join will still return a row in the result—but with NULL in each column from B. This means that a left outer join returns all the values from the left table, plus matched values from the right table (or NULL in case of no matching join predicate)." In other words, when the join column of the left is NULL, it will not find any mathcing rows, and the output columns of the right hand table should all be NULL. This means that the 5.1 behavior is wrong. This issue is probably related to use of an index. I notice that if I switch to join a non-indexed column of the left table with an indexed column of the right table, one get the right result (i.e., NULL for table2.pk).
[6 Dec 2010 22:55]
Ole John Aske
This seems to be a duplicate of bug#57034 which I have recently commited a fix for. Has tested the testcases in this report with my fix and the reported problems seems to have been solved.
[20 Dec 2010 13:21]
Ole John Aske
Closed as duplicate of bug#57034