[MDEV-3914] Wrong result (NULLs instead of real values) with INNER and RIGHT JOIN in a FROM subquery, derived_merge=on Created: 2012-12-04  Updated: 2012-12-06  Resolved: 2012-12-06

Status: Closed
Project: MariaDB Server
Component/s: None
Affects Version/s: 10.0.0, 5.5.28, 5.3.10
Fix Version/s: 10.0.1, 5.5.29, 5.3.12

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Oleksandr Byelkin
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-486 LP:1010116 - Incorrect query results ... Closed

 Description   

The following test case

SET optimizer_switch = 'derived_merge=on';
 
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1),(2);
 
CREATE TABLE t2 (b INT) ENGINE=MyISAM;
INSERT INTO t2 VALUES (3),(4);
 
CREATE TABLE t3 (c INT) ENGINE=MyISAM;
INSERT INTO t3 VALUES (5),(6);
 
SELECT * FROM ( SELECT c FROM ( t1 INNER JOIN t2 ) RIGHT JOIN t3 ON a = c ) AS alias;

returns NULLs:

c
------
NULL
NULL

If the subquery is executed separately, it returns not null values, which is the correct result:

c
------
5
6

The problem was either introduced or made visible with the revision http://bazaar.launchpad.net/~maria-captains/maria/5.3/revision/3574:

revno: 3574
committer: sanja@montyprogram.com
branch nick: work-maria-5.3-MDEV-486
timestamp: Wed 2012-09-05 23:23:58 +0300
message:
  MDEV-486 LP BUG#1010116 fix.
  
  Link view/derived table fields to a real table to check turning the table record to null row.
  
  Item_direct_view_ref wrapper now checks if table is turned to null row.

Reproducible on current maria/5.3, maria/5.5, maria/10.0.
MySQL 5.6 and MariaDB 5.2 return the correct result.

Minimal optimizer_switch: derived_merge=on.
Also reproducible with the default optimizer_switch:

index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on

EXPLAIN with the default optimizer_switch (also wrong result):

id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using join buffer (flat, BNL join)
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using join buffer (incremental, BNL join)
Warnings:
Note	1003	select `test`.`t3`.`c` AS `c` from `test`.`t3` left join (`test`.`t1` join `test`.`t2`) on((`test`.`t1`.`a` = `test`.`t3`.`c`)) where 1

EXPLAIN with the minimal optimizer_switch (also wrong result):

id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	
Warnings:
Note	1003	select `test`.`t3`.`c` AS `c` from `test`.`t3` left join (`test`.`t1` join `test`.`t2`) on((`test`.`t1`.`a` = `test`.`t3`.`c`)) where 1



 Comments   
Comment by Elena Stepanova [ 2012-12-04 ]

Assigned to Sanja because it relates to his commit, please reassign if necessary.

Comment by Oleksandr Byelkin [ 2012-12-04 ]

The problem is that TABLE_LIST::get_real_join_table incorrectly finds first table - t1 in "( t1 INNER JOIN t2 ) RIGHT JOIN t3". Correct first table is t3.

Comment by Oleksandr Byelkin [ 2012-12-05 ]

Fix committed for review.

Comment by Oleksandr Byelkin [ 2012-12-05 ]

Pushed to 5.3

Generated at Thu Feb 08 06:52:14 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.