List:Commits« Previous MessageNext Message »
From:Tor Didriksen Date:January 13 2012 9:35am
Subject:bzr push into mysql-trunk branch (tor.didriksen:3451 to 3454)
View as plain text  
 3454 Tor Didriksen	2012-01-13 [merge]
      Automerge opt-backporting => opt-team

    modified:
      mysql-test/include/subquery_mat.inc
      mysql-test/include/subquery_sj.inc
      mysql-test/r/archive_gis.result
      mysql-test/r/case.result
      mysql-test/r/func_if.result
      mysql-test/r/gis.result
      mysql-test/r/group_by.result
      mysql-test/r/subquery_mat.result
      mysql-test/r/subquery_mat_all.result
      mysql-test/r/subquery_mat_none.result
      mysql-test/r/subquery_sj_all.result
      mysql-test/r/subquery_sj_all_bka.result
      mysql-test/r/subquery_sj_all_bka_nixbnl.result
      mysql-test/r/subquery_sj_all_bkaunique.result
      mysql-test/r/subquery_sj_dupsweed.result
      mysql-test/r/subquery_sj_dupsweed_bka.result
      mysql-test/r/subquery_sj_dupsweed_bka_nixbnl.result
      mysql-test/r/subquery_sj_dupsweed_bkaunique.result
      mysql-test/r/subquery_sj_firstmatch.result
      mysql-test/r/subquery_sj_firstmatch_bka.result
      mysql-test/r/subquery_sj_firstmatch_bka_nixbnl.result
      mysql-test/r/subquery_sj_firstmatch_bkaunique.result
      mysql-test/r/subquery_sj_loosescan.result
      mysql-test/r/subquery_sj_loosescan_bka.result
      mysql-test/r/subquery_sj_loosescan_bka_nixbnl.result
      mysql-test/r/subquery_sj_loosescan_bkaunique.result
      mysql-test/r/subquery_sj_mat.result
      mysql-test/r/subquery_sj_mat_bka.result
      mysql-test/r/subquery_sj_mat_bka_nixbnl.result
      mysql-test/r/subquery_sj_mat_bkaunique.result
      mysql-test/r/subquery_sj_mat_nosj.result
      mysql-test/r/subquery_sj_none.result
      mysql-test/r/subquery_sj_none_bka.result
      mysql-test/r/subquery_sj_none_bka_nixbnl.result
      mysql-test/r/subquery_sj_none_bkaunique.result
      mysql-test/r/view.result
      mysql-test/r/view_alias.result
      mysql-test/suite/innodb/r/innodb_gis.result
      mysql-test/suite/opt_trace/r/filesort_pq.result
      mysql-test/suite/opt_trace/r/general2_no_prot.result
      mysql-test/suite/opt_trace/r/general2_ps_prot.result
      mysql-test/suite/opt_trace/r/subquery_no_prot.result
      mysql-test/suite/opt_trace/r/subquery_ps_prot.result
      mysql-test/t/group_by.test
      sql/filesort.cc
      sql/item.cc
      sql/item.h
      sql/sql_executor.cc
      sql/sql_lex.cc
      sql/sql_optimizer.cc
      sql/sql_parse.cc
      sql/sql_planner.cc
      sql/sql_resolver.cc
      sql/sql_select.cc
      sql/sql_select.h
      sql/sql_tmp_table.cc
      sql/table.h
 3453 Tor Didriksen	2012-01-13 [merge]
      NULL merge opt-backporting => opt-team

 3452 Tor Didriksen	2012-01-13 [merge]
      empty merge opt-backporting => opt-team

 3451 Tor Didriksen	2012-01-05 [merge]
      empty merge trunk => opt-team

=== modified file 'mysql-test/include/subquery_mat.inc'
--- a/mysql-test/include/subquery_mat.inc	2011-11-25 10:39:07 +0000
+++ b/mysql-test/include/subquery_mat.inc	2012-01-09 14:19:49 +0000
@@ -677,12 +677,12 @@ INSERT INTO t2 VALUES (5);
 CREATE TABLE t3 (k INTEGER);
 
 EXPLAIN
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
 
 EXPLAIN
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
 
 DROP TABLE t1, t2, t3;
 
@@ -929,3 +929,35 @@ SELECT * FROM t3;
 # prove that subquery materialization was used:
 SHOW STATUS LIKE "CREATED_TMP_TABLES";
 DROP TABLE t1,t2,t3;
+
+--echo #
+--echo # Bug#13552968: Extra row with materialization on join + subquery in
+--echo #
+
+CREATE TABLE t1 (
+  col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+
+INSERT INTO t1 VALUES ('b');
+
+CREATE TABLE t2 (
+  col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+
+INSERT INTO t2 VALUES ('k');
+
+CREATE TABLE t3 (
+  col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+
+let $query=
+SELECT STRAIGHT_JOIN *
+FROM t1 LEFT JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
+                                                 FROM t3);
+
+eval explain $query;
+eval $query;
+
+DROP TABLE t1, t2, t3;
+
+--echo # End of test for bug#13552968

=== modified file 'mysql-test/include/subquery_sj.inc'
--- a/mysql-test/include/subquery_sj.inc	2011-12-13 08:49:11 +0000
+++ b/mysql-test/include/subquery_sj.inc	2012-01-12 14:53:51 +0000
@@ -3910,9 +3910,6 @@ WHERE (grandparent1.col_varchar_key) IN
 eval EXPLAIN $query;
 eval $query;
 
---echo -- Notice that Materialize-scan algorithm reports wrong result for this query.
---echo -- This problem will be filed as a separate bug and dealt with in WL#5561.
-
 DROP TABLE t1, t2;
 
 --echo # End of test for bug#12603183.
@@ -4575,4 +4572,204 @@ DROP TABLE t1, t2;
 
 --echo # End of test for bug#13414014.
 
+--echo #
+--echo # Bug#13545215: Missing rows on nested in-subquery with materialization
+--echo #
+
+CREATE TABLE t1 (
+  col_int_key int,
+  col_varchar_key varchar(1),
+  col_varchar_nokey varchar(1),
+  KEY col_int_key (col_int_key),
+  KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+
+INSERT INTO t1 VALUES
+ (8,'x','x'), (0,'p','p'), (8,'c','c');
+
+CREATE TABLE t2 (
+  pk int NOT NULL,
+  col_varchar_key varchar(1),
+  col_varchar_nokey varchar(1),
+  PRIMARY KEY (pk),
+  KEY col_varchar_key (col_varchar_key)
+);
+
+INSERT INTO t2 VALUES
+ (1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+ (5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+
+CREATE TABLE t3 (
+ col_int_nokey int
+);
+
+INSERT INTO t3 VALUES (7);
+
+let $query=
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+    SELECT col_varchar_nokey
+    FROM t2 AS parent1
+    WHERE col_varchar_key IN (
+        SELECT child1.col_varchar_nokey
+        FROM t2 AS child1 LEFT JOIN t3 AS child2
+             ON child1.pk < child2.col_int_nokey
+        )
+    );
+
+eval explain $query;
+--sorted_result
+eval $query;
+
+DROP TABLE t1, t2, t3;
+
+--echo # End of test for bug#13545215.
+
+--echo #
+--echo # BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+--echo # MATERIALIZATION + SEMIJOIN ON
+--echo #
+CREATE TABLE t1 (
+  col_int_key int(11) DEFAULT NULL,
+  col_varchar_key varchar(1) DEFAULT NULL,
+  col_varchar_nokey varchar(1) DEFAULT NULL,
+  KEY col_int_key (col_int_key),
+  KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+
+let $query=
+SELECT
+  alias1.col_varchar_nokey AS a1_nokey,
+  alias1.col_varchar_key AS a1_key,
+  alias2.col_varchar_nokey AS a2_nokey
+FROM
+  t1 AS alias1, t1 AS alias2
+WHERE
+  (alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+  IN
+  (
+    SELECT
+      SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+    FROM
+      t1 AS SQ2_alias1, t1 AS SQ2_alias2
+  )
+;
+
+eval EXPLAIN $query;
+--sorted_result
+eval $query;
+
+DROP TABLE t1;
+
+--echo #
+--echo # Bug#13541406: Wrong result with loosescan on select .. where .. in
+--echo #
+
+CREATE TABLE t1 (
+  col_int_key INT NOT NULL,
+  col_varchar_nokey VARCHAR(1) NOT NULL,
+  KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+
+INSERT INTO t1 VALUES
+ (7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+ (1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+ (9,'e'), (5,'i'), (0,'y'), (3,'w');
+
+CREATE TABLE t2 (
+  pk INT NOT NULL,
+  col_int_key INT NOT NULL,
+  col_varchar_key VARCHAR(1) NOT NULL,
+  col_varchar_nokey VARCHAR(1) NOT NULL,
+  PRIMARY KEY (pk),
+  KEY col_int_key (col_int_key),
+  KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+
+INSERT INTO t2 VALUES
+ (1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+ (5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+ (9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+ (13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+ (17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+
+let $query=
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+    SELECT it2.col_varchar_nokey, it1.col_varchar_key
+    FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+
+--echo # This query should never use a LooseScan strategy
+
+eval explain $query;
+--sorted_result
+eval $query;
+
+DROP TABLE t1, t2;
+
+--echo # End of test for bug#13541406.
+
+--echo #
+--echo # Bug#13576391: Missing rows on select with in-subquery and
+--echo # batched-key-access=on and semijoin
+--echo #
+
+CREATE TABLE t1 (
+ col_int_nokey int NOT NULL,
+ col_varchar_key varchar(1) NOT NULL,
+ KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+
+INSERT INTO t1 VALUES
+ (1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+ (0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+ (6,'e'), (3,'i'), (6,'y'), (6,'w');
+
+CREATE TABLE t2 (
+ col_int_nokey int NOT NULL,
+ col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+
+INSERT INTO t2 VALUES
+ (4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+ (3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+ (1,'q'), (6,'w'), (2,'d'), (9,'e');
+
+let $query=
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+  SELECT col_varchar_key
+  FROM t1 AS it
+  WHERE it.col_int_nokey <= it.col_int_nokey
+    AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+
+eval explain $query;
+eval $query;
+
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+
+eval explain $query;
+eval $query;
+
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+
+eval explain $query;
+eval $query;
+
+DROP TABLE t1, t2;
+
+--echo # End of test for bug#13576391.
+
 --echo # End of 5.6 tests

=== modified file 'mysql-test/r/archive_gis.result'
--- a/mysql-test/r/archive_gis.result	2011-08-03 11:29:20 +0000
+++ b/mysql-test/r/archive_gis.result	2012-01-11 09:22:26 +0000
@@ -405,7 +405,7 @@ id	select_type	table	type	possible_keys	
 1	SIMPLE	g1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary; Using filesort
 1	SIMPLE	g2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using join buffer (Block Nested Loop)
 Warnings:
-Note	1003	/* select#1 */ select `test`.`g1`.`fid` AS `first`,`test`.`g2`.`fid` AS `second`,mbrwithin(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `w`,mbrcontains(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `c`,mbroverlaps(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `o`,mbrequals(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `e`,mbrdisjoint(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `d`,st_touches(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `t`,mbrintersects(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `i`,st_crosses(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `r` from `test`.`gis_geometrycollection` `g1` join `test`.`gis_geometrycollection` `g2` order by `test`.`g1`.`fid`,`test`.`g2`.`fid`
+Note	1003	/* select#1 */ select `test`.`g1`.`fid` AS `first`,`test`.`g2`.`fid` AS `second`,mbrwithin(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `w`,mbrcontains(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `c`,mbroverlaps(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `o`,mbrequals(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `e`,mbrdisjoint(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `d`,st_touches(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `t`,mbrintersects(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `i`,st_crosses(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `r` from `test`.`gis_geometrycollection` `g1` join `test`.`gis_geometrycollection` `g2` order by `first`,`second`
 DROP TABLE gis_point, gis_line, gis_polygon, gis_multi_point, gis_multi_line, gis_multi_polygon, gis_geometrycollection, gis_geometry;
 CREATE TABLE t1 (
 a INTEGER PRIMARY KEY AUTO_INCREMENT,

=== modified file 'mysql-test/r/case.result'
--- a/mysql-test/r/case.result	2011-11-03 10:15:06 +0000
+++ b/mysql-test/r/case.result	2012-01-10 18:58:10 +0000
@@ -66,7 +66,7 @@ explain extended select case a when 1 th
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	4	100.00	Using temporary; Using filesort
 Warnings:
-Note	1003	/* select#1 */ select (case `test`.`t1`.`a` when 1 then 2 when 2 then 3 else 0 end) AS `fcase`,count(0) AS `count(*)` from `test`.`t1` group by (case `test`.`t1`.`a` when 1 then 2 when 2 then 3 else 0 end)
+Note	1003	/* select#1 */ select (case `test`.`t1`.`a` when 1 then 2 when 2 then 3 else 0 end) AS `fcase`,count(0) AS `count(*)` from `test`.`t1` group by `fcase`
 select case a when 1 then "one" when 2 then "two" else "nothing" end as fcase, count(*) from t1 group by fcase;
 fcase	count(*)
 nothing	2

=== modified file 'mysql-test/r/func_if.result'
--- a/mysql-test/r/func_if.result	2011-08-19 07:32:38 +0000
+++ b/mysql-test/r/func_if.result	2012-01-10 18:58:10 +0000
@@ -43,7 +43,7 @@ explain extended select if(u=1,st,binary
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where; Using filesort
 Warnings:
-Note	1003	/* select#1 */ select if((`test`.`t1`.`u` = 1),`test`.`t1`.`st`,cast(`test`.`t1`.`st` as char charset binary)) AS `s` from `test`.`t1` where (`test`.`t1`.`st` like '%a%') order by if((`test`.`t1`.`u` = 1),`test`.`t1`.`st`,cast(`test`.`t1`.`st` as char charset binary))
+Note	1003	/* select#1 */ select if((`test`.`t1`.`u` = 1),`test`.`t1`.`st`,cast(`test`.`t1`.`st` as char charset binary)) AS `s` from `test`.`t1` where (`test`.`t1`.`st` like '%a%') order by `s`
 select nullif(u, 1) from t1;
 nullif(u, 1)
 NULL

=== modified file 'mysql-test/r/gis.result'
--- a/mysql-test/r/gis.result	2011-12-07 15:02:50 +0000
+++ b/mysql-test/r/gis.result	2012-01-10 18:58:10 +0000
@@ -397,7 +397,7 @@ id	select_type	table	type	possible_keys	
 1	SIMPLE	g1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary; Using filesort
 1	SIMPLE	g2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using join buffer (Block Nested Loop)
 Warnings:
-Note	1003	/* select#1 */ select `test`.`g1`.`fid` AS `first`,`test`.`g2`.`fid` AS `second`,mbrwithin(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `w`,mbrcontains(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `c`,mbroverlaps(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `o`,mbrequals(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `e`,mbrdisjoint(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `d`,st_touches(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `t`,mbrintersects(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `i`,st_crosses(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `r` from `test`.`gis_geometrycollection` `g1` join `test`.`gis_geometrycollection` `g2` order by `test`.`g1`.`fid`,`test`.`g2`.`fid`
+Note	1003	/* select#1 */ select `test`.`g1`.`fid` AS `first`,`test`.`g2`.`fid` AS `second`,mbrwithin(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `w`,mbrcontains(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `c`,mbroverlaps(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `o`,mbrequals(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `e`,mbrdisjoint(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `d`,st_touches(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `t`,mbrintersects(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `i`,st_crosses(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `r` from `test`.`gis_geometrycollection` `g1` join `test`.`gis_geometrycollection` `g2` order by `first`,`second`
 DROP TABLE gis_point, gis_line, gis_polygon, gis_multi_point, gis_multi_line, gis_multi_polygon, gis_geometrycollection, gis_geometry;
 CREATE TABLE t1 (
 gp  point,

=== modified file 'mysql-test/r/group_by.result'
--- a/mysql-test/r/group_by.result	2012-01-05 10:17:03 +0000
+++ b/mysql-test/r/group_by.result	2012-01-13 09:33:13 +0000
@@ -2266,3 +2266,139 @@ MAX
 NULL
 1
 DROP TABLE t1,t2;
+# Bug 11923239 - ERROR WITH CORRELATED SUBQUERY IN VIEW WITH
+# ONLY_FULL_GROUP_BY SQL MODE
+CREATE TABLE t1 (
+pk INT,
+col_int_key INT,
+col_int_nokey INT,
+col_varchar_key VARCHAR(10),
+col_varchar_nokey VARCHAR(10),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t1 VALUES (), ();
+# In GROUP BY, aliases are printed as aliases.
+EXPLAIN EXTENDED SELECT alias1.col_int_nokey AS field1,
+(SELECT alias2.col_int_key
+FROM t1 AS alias2
+WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey
+) AS field2
+FROM t1 AS alias1
+GROUP BY field1, field2;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary; Using filesort
+2	DEPENDENT SUBQUERY	alias2	index	NULL	col_int_key	5	NULL	2	100.00	Using where; Using index
+Warnings:
+Note	1276	Field or reference 'test.alias1.col_varchar_key' of SELECT #2 was resolved in SELECT #1
+Note	1276	Field or reference 'test.alias1.col_varchar_nokey' of SELECT #2 was resolved in SELECT #1
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_int_nokey` AS `field1`,(/* select#2 */ select `test`.`alias2`.`col_int_key` from `test`.`t1` `alias2` where (`test`.`alias1`.`col_varchar_key` <= `test`.`alias1`.`col_varchar_nokey`)) AS `field2` from `test`.`t1` `alias1` group by `field1`,`field2`
+# In GROUP BY, expressions are printed as expressions.
+EXPLAIN EXTENDED SELECT alias1.col_int_nokey AS field1,
+(SELECT alias2.col_int_key
+FROM t1 AS alias2
+WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey
+) AS field2
+FROM t1 AS alias1
+GROUP BY field1,
+(SELECT alias2.col_int_key
+FROM t1 AS alias2
+WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary; Using filesort
+3	DEPENDENT SUBQUERY	alias2	index	NULL	col_int_key	5	NULL	2	100.00	Using where; Using index
+2	DEPENDENT SUBQUERY	alias2	index	NULL	col_int_key	5	NULL	2	100.00	Using where; Using index
+Warnings:
+Note	1276	Field or reference 'test.alias1.col_varchar_key' of SELECT #2 was resolved in SELECT #1
+Note	1276	Field or reference 'test.alias1.col_varchar_nokey' of SELECT #2 was resolved in SELECT #1
+Note	1276	Field or reference 'test.alias1.col_varchar_key' of SELECT #3 was resolved in SELECT #1
+Note	1276	Field or reference 'test.alias1.col_varchar_nokey' of SELECT #3 was resolved in SELECT #1
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_int_nokey` AS `field1`,(/* select#2 */ select `test`.`alias2`.`col_int_key` from `test`.`t1` `alias2` where (`test`.`alias1`.`col_varchar_key` <= `test`.`alias1`.`col_varchar_nokey`)) AS `field2` from `test`.`t1` `alias1` group by `field1`,(/* select#3 */ select `test`.`alias2`.`col_int_key` from `test`.`t1` `alias2` where (`test`.`alias1`.`col_varchar_key` <= `test`.`alias1`.`col_varchar_nokey`))
+# Aliased expression in GROUP BY in a view.
+CREATE VIEW v1 AS SELECT alias1.col_int_nokey AS field1,
+(SELECT alias2.col_int_key
+FROM t1 AS alias2
+WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey
+) AS field2
+FROM t1 AS alias1
+GROUP BY field1, field2;
+# In GROUP BY, aliases are printed as aliases.
+SHOW CREATE VIEW v1;
+View	Create View	character_set_client	collation_connection
+v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `alias1`.`col_int_nokey` AS `field1`,(select `alias2`.`col_int_key` from `t1` `alias2` where (`alias1`.`col_varchar_key` <= `alias1`.`col_varchar_nokey`)) AS `field2` from `t1` `alias1` group by `field1`,`field2`	latin1	latin1_swedish_ci
+SET @old_sql_mode = @@sql_mode;
+SET @@sql_mode='ONLY_FULL_GROUP_BY';
+SELECT alias1.col_int_nokey AS field1,
+(SELECT alias2.col_int_key
+FROM t1 AS alias2
+WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey
+) AS field2
+FROM t1 AS alias1
+GROUP BY field1, field2;
+field1	field2
+NULL	NULL
+# The SELECT above has been accepted, and v1 was created
+# using the same SELECT as above, so SELECTing from v1
+# should be accepted.
+SELECT * FROM v1;
+field1	field2
+NULL	NULL
+# Here is why in GROUP BY we print aliases of subqueries as
+# aliases: below, "GROUP BY (subquery)" confuses
+# ONLY_FULL_GROUP_BY, it causes an error though the subquery of
+# GROUP BY and of SELECT list are the same. Fixing this would
+# require implementing Item_subselect::eq(). It's not worth
+# the effort because:
+# a) GROUP BY (subquery) is non-SQL-standard so is likely of
+# very little interest to users of ONLY_FULL_GROUP_BY
+# b) as the user uses ONLY_FULL_GROUP_BY, he wants to have the
+# same subquery in GROUP BY and SELECT list, so can give the
+# subquery an alias in the SELECT list and use this alias in
+# GROUP BY, thus avoiding the problem.
+SELECT alias1.col_int_nokey AS field1,
+(SELECT alias2.col_int_key
+FROM t1 AS alias2
+WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey
+) AS field2
+FROM t1 AS alias1
+GROUP BY field1,
+(SELECT alias2.col_int_key
+FROM t1 AS alias2
+WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey
+);
+ERROR 42000: 'test.alias1.col_varchar_key' isn't in GROUP BY
+DROP VIEW v1;
+SET @@sql_mode = @old_sql_mode;
+# Verify that if an alias is used in GROUP BY/ORDER BY it
+# is printed as an alias, not as the expression.
+CREATE TABLE t2(a INT);
+INSERT INTO t2 VALUES(3),(4);
+EXPLAIN EXTENDED SELECT
+pk AS foo, col_int_key AS bar, (SELECT a FROM t2 WHERE a=t1.pk) AS baz
+FROM t1
+GROUP BY foo, col_int_key, baz ORDER BY pk, bar, (SELECT a FROM t2 WHERE a=t1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary; Using filesort
+3	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
+2	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
+Warnings:
+Note	1276	Field or reference 'test.t1.pk' of SELECT #2 was resolved in SELECT #1
+Note	1276	Field or reference 'test.t1.pk' of SELECT #3 was resolved in SELECT #1
+Note	1003	/* select#1 */ select `t1`.`pk` AS `foo`,`t1`.`col_int_key` AS `bar`,(/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where (`test`.`t2`.`a` = `t1`.`pk`)) AS `baz` from `test`.`t1` group by `foo`,`t1`.`col_int_key`,`baz` order by `t1`.`pk`,`bar`,(/* select#3 */ select `test`.`t2`.`a` from `test`.`t2` where (`test`.`t2`.`a` = `t1`.`pk`))
+EXPLAIN EXTENDED SELECT
+pk AS foo, col_int_key AS foo, (SELECT a FROM t2 WHERE a=t1.pk) AS foo
+FROM t1
+GROUP BY pk, col_int_key, (SELECT a FROM t2 WHERE a=t1.pk)
+ORDER BY pk, col_int_key, (SELECT a FROM t2 WHERE a=t1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary; Using filesort
+4	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
+3	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
+2	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
+Warnings:
+Note	1276	Field or reference 'test.t1.pk' of SELECT #2 was resolved in SELECT #1
+Note	1276	Field or reference 'test.t1.pk' of SELECT #4 was resolved in SELECT #1
+Note	1276	Field or reference 'test.t1.pk' of SELECT #3 was resolved in SELECT #1
+Note	1003	/* select#1 */ select `t1`.`pk` AS `foo`,`t1`.`col_int_key` AS `foo`,(/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where (`test`.`t2`.`a` = `t1`.`pk`)) AS `foo` from `test`.`t1` group by `t1`.`pk`,`t1`.`col_int_key`,(/* select#3 */ select `test`.`t2`.`a` from `test`.`t2` where (`test`.`t2`.`a` = `t1`.`pk`)) order by `t1`.`pk`,`t1`.`col_int_key`,(/* select#4 */ select `test`.`t2`.`a` from `test`.`t2` where (`test`.`t2`.`a` = `t1`.`pk`))
+DROP TABLE t1,t2;

=== modified file 'mysql-test/r/subquery_mat.result'
--- a/mysql-test/r/subquery_mat.result	2011-11-25 10:39:07 +0000
+++ b/mysql-test/r/subquery_mat.result	2012-01-09 14:19:49 +0000
@@ -968,23 +968,23 @@ CREATE TABLE t2 (j INTEGER);
 INSERT INTO t2 VALUES (5);
 CREATE TABLE t3 (k INTEGER);
 EXPLAIN
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where
 2	SUBQUERY	t3	system	NULL	NULL	NULL	NULL	0	const row not found
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
-i
-10
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
+i	j
+10	NULL
 EXPLAIN
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where
 2	SUBQUERY	t3	system	NULL	NULL	NULL	NULL	0	const row not found
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
-i
-10
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
+i	j
+10	NULL
 DROP TABLE t1, t2, t3;
 # End BUG#52344
 CREATE TABLE t1 (
@@ -1088,7 +1088,7 @@ explain SELECT a FROM (
 SELECT t1.* FROM t1 LEFT JOIN t2 ON t1.a > 3 OR t2.b IN (SELECT a FROM t1)
 ) table1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	const row not found
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2	
 2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
 3	SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	const row not found
 SELECT a FROM (
@@ -1204,4 +1204,32 @@ SHOW STATUS LIKE "CREATED_TMP_TABLES";
 Variable_name	Value
 Created_tmp_tables	2
 DROP TABLE t1,t2,t3;
+#
+# Bug#13552968: Extra row with materialization on join + subquery in
+#
+CREATE TABLE t1 (
+col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+INSERT INTO t1 VALUES ('b');
+CREATE TABLE t2 (
+col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+INSERT INTO t2 VALUES ('k');
+CREATE TABLE t3 (
+col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+explain SELECT STRAIGHT_JOIN *
+FROM t1 LEFT JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
+FROM t3);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where
+2	SUBQUERY	t3	system	NULL	NULL	NULL	NULL	0	const row not found
+SELECT STRAIGHT_JOIN *
+FROM t1 LEFT JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
+FROM t3);
+col_varchar_nokey	col_varchar_nokey
+b	NULL
+DROP TABLE t1, t2, t3;
+# End of test for bug#13552968
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_mat_all.result'
--- a/mysql-test/r/subquery_mat_all.result	2011-11-25 10:39:07 +0000
+++ b/mysql-test/r/subquery_mat_all.result	2012-01-09 14:19:49 +0000
@@ -970,23 +970,23 @@ CREATE TABLE t2 (j INTEGER);
 INSERT INTO t2 VALUES (5);
 CREATE TABLE t3 (k INTEGER);
 EXPLAIN
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
 2	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
-i
-10
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
+i	j
+10	NULL
 EXPLAIN
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where
 2	SUBQUERY	t3	system	NULL	NULL	NULL	NULL	0	const row not found
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
-i
-10
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
+i	j
+10	NULL
 DROP TABLE t1, t2, t3;
 # End BUG#52344
 CREATE TABLE t1 (
@@ -1090,7 +1090,7 @@ explain SELECT a FROM (
 SELECT t1.* FROM t1 LEFT JOIN t2 ON t1.a > 3 OR t2.b IN (SELECT a FROM t1)
 ) table1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	const row not found
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2	
 2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
 3	SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	const row not found
 SELECT a FROM (
@@ -1205,4 +1205,32 @@ SHOW STATUS LIKE "CREATED_TMP_TABLES";
 Variable_name	Value
 Created_tmp_tables	0
 DROP TABLE t1,t2,t3;
+#
+# Bug#13552968: Extra row with materialization on join + subquery in
+#
+CREATE TABLE t1 (
+col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+INSERT INTO t1 VALUES ('b');
+CREATE TABLE t2 (
+col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+INSERT INTO t2 VALUES ('k');
+CREATE TABLE t3 (
+col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+explain SELECT STRAIGHT_JOIN *
+FROM t1 LEFT JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
+FROM t3);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where
+2	SUBQUERY	t3	system	NULL	NULL	NULL	NULL	0	const row not found
+SELECT STRAIGHT_JOIN *
+FROM t1 LEFT JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
+FROM t3);
+col_varchar_nokey	col_varchar_nokey
+b	NULL
+DROP TABLE t1, t2, t3;
+# End of test for bug#13552968
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_mat_none.result'
--- a/mysql-test/r/subquery_mat_none.result	2011-11-25 10:39:07 +0000
+++ b/mysql-test/r/subquery_mat_none.result	2012-01-09 14:19:49 +0000
@@ -967,23 +967,23 @@ CREATE TABLE t2 (j INTEGER);
 INSERT INTO t2 VALUES (5);
 CREATE TABLE t3 (k INTEGER);
 EXPLAIN
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
 2	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
-i
-10
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT k FROM t3);
+i	j
+10	NULL
 EXPLAIN
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
 2	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
-SELECT i FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
-i
-10
+SELECT i, j FROM t1 LEFT JOIN t2 ON (j) IN (SELECT max(k) FROM t3);
+i	j
+10	NULL
 DROP TABLE t1, t2, t3;
 # End BUG#52344
 CREATE TABLE t1 (
@@ -1202,4 +1202,32 @@ SHOW STATUS LIKE "CREATED_TMP_TABLES";
 Variable_name	Value
 Created_tmp_tables	0
 DROP TABLE t1,t2,t3;
+#
+# Bug#13552968: Extra row with materialization on join + subquery in
+#
+CREATE TABLE t1 (
+col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+INSERT INTO t1 VALUES ('b');
+CREATE TABLE t2 (
+col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+INSERT INTO t2 VALUES ('k');
+CREATE TABLE t3 (
+col_varchar_nokey varchar(1) NOT NULL
+) ENGINE=MyISAM;
+explain SELECT STRAIGHT_JOIN *
+FROM t1 LEFT JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
+FROM t3);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
+2	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT STRAIGHT_JOIN *
+FROM t1 LEFT JOIN t2 ON t1.col_varchar_nokey IN (SELECT col_varchar_nokey
+FROM t3);
+col_varchar_nokey	col_varchar_nokey
+b	NULL
+DROP TABLE t1, t2, t3;
+# End of test for bug#13552968
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_all.result'
--- a/mysql-test/r/subquery_sj_all.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_all.result	2012-01-12 14:53:51 +0000
@@ -6595,6 +6595,7 @@ col_varchar_nokey
 i
 h
 q
+a
 v
 u
 s
@@ -6606,8 +6607,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7270,5 +7269,306 @@ x
 q
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start materialize; Scan
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using index; Start materialize
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	End materialize; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index; Start temporary
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_all_bka.result'
--- a/mysql-test/r/subquery_sj_all_bka.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_all_bka.result	2012-01-12 14:53:51 +0000
@@ -6596,6 +6596,7 @@ col_varchar_nokey
 i
 h
 q
+a
 v
 u
 s
@@ -6607,8 +6608,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7271,6 +7270,307 @@ x
 q
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start materialize; Scan
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using index; Start materialize
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	End materialize; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	Using join buffer (Batched Key Access)
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	FirstMatch(ot); Using join buffer (Batched Key Access)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot); Using join buffer (Batched Key Access)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_all_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_all_bka_nixbnl.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_all_bka_nixbnl.result	2012-01-12 14:53:51 +0000
@@ -6596,6 +6596,7 @@ col_varchar_nokey
 i
 h
 q
+a
 v
 u
 s
@@ -6607,8 +6608,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7271,6 +7270,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	ref	col_int_key	col_int_key	5	test.grandparent1.col_int_key	2	Using index
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Using where; Start materialize; Scan
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+2	DEPENDENT SUBQUERY	parent1	ref	col_varchar_key	col_varchar_key	4	test.child1.col_varchar_nokey	2	Using where; Using join buffer (Batched Key Access)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using index; Start materialize
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	End materialize
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	Using join buffer (Batched Key Access)
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	FirstMatch(ot); Using join buffer (Batched Key Access)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot); Using join buffer (Batched Key Access)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_all_bkaunique.result'
--- a/mysql-test/r/subquery_sj_all_bkaunique.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_all_bkaunique.result	2012-01-12 14:53:51 +0000
@@ -6597,6 +6597,7 @@ col_varchar_nokey
 i
 h
 q
+a
 v
 u
 s
@@ -6608,8 +6609,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7272,6 +7271,307 @@ x
 q
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start materialize; Scan
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using index; Start materialize
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	End materialize; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	Using join buffer (Batched Key Access (unique))
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	FirstMatch(ot); Using join buffer (Batched Key Access (unique))
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using temporary; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot); Using join buffer (Batched Key Access (unique))
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed.result'
--- a/mysql-test/r/subquery_sj_dupsweed.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed.result	2012-01-12 14:53:51 +0000
@@ -6606,8 +6606,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7270,5 +7268,306 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	Start temporary
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using where; Using index; Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index; Start temporary
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	Start temporary; End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	Start temporary; End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	Start temporary; End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed_bka.result'
--- a/mysql-test/r/subquery_sj_dupsweed_bka.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_bka.result	2012-01-12 14:53:51 +0000
@@ -6607,8 +6607,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7271,6 +7269,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	Start temporary
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using where; Using index; Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_dupsweed_bka_nixbnl.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_bka_nixbnl.result	2012-01-12 14:53:51 +0000
@@ -6607,8 +6607,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7271,6 +7269,307 @@ x
 q
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	ref	col_int_key	col_int_key	5	test.grandparent1.col_int_key	2	Using index
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Using where; Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where
+2	DEPENDENT SUBQUERY	parent1	ref	col_varchar_key	col_varchar_key	4	test.child1.col_varchar_nokey	2	Using where; End temporary
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	Start temporary
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where
+1	PRIMARY	SQ2_alias1	ref	col_varchar_key	col_varchar_key	4	test.alias2.col_varchar_nokey	1	Using index
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; End temporary
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed_bkaunique.result'
--- a/mysql-test/r/subquery_sj_dupsweed_bkaunique.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_bkaunique.result	2012-01-12 14:53:51 +0000
@@ -6608,8 +6608,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7272,6 +7270,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	Start temporary
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using where; Using index; Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_firstmatch.result'
--- a/mysql-test/r/subquery_sj_firstmatch.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch.result	2012-01-12 14:53:51 +0000
@@ -6607,8 +6607,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7271,6 +7269,307 @@ x
 q
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	ref	col_varchar_key	col_varchar_key	4	test.alias2.col_varchar_nokey	1	Using index
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; FirstMatch(alias2)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index; Start temporary
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 #
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for

=== modified file 'mysql-test/r/subquery_sj_firstmatch_bka.result'
--- a/mysql-test/r/subquery_sj_firstmatch_bka.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_bka.result	2012-01-12 14:53:51 +0000
@@ -6608,8 +6608,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7272,6 +7270,307 @@ x
 q
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	ref	col_varchar_key	col_varchar_key	4	test.alias2.col_varchar_nokey	1	Using index
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; FirstMatch(alias2)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 #
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for

=== modified file 'mysql-test/r/subquery_sj_firstmatch_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_firstmatch_bka_nixbnl.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_bka_nixbnl.result	2012-01-12 14:53:51 +0000
@@ -6608,8 +6608,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7272,6 +7270,307 @@ x
 q
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	ref	col_int_key	col_int_key	5	test.grandparent1.col_int_key	2	Using index
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Using where; Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where
+2	DEPENDENT SUBQUERY	parent1	ref	col_varchar_key	col_varchar_key	4	test.child1.col_varchar_nokey	2	Using where; End temporary
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where
+1	PRIMARY	SQ2_alias1	ref	col_varchar_key	col_varchar_key	4	test.alias2.col_varchar_nokey	1	Using index
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; FirstMatch(alias2)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 #
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for

=== modified file 'mysql-test/r/subquery_sj_firstmatch_bkaunique.result'
--- a/mysql-test/r/subquery_sj_firstmatch_bkaunique.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_bkaunique.result	2012-01-12 14:53:51 +0000
@@ -6609,8 +6609,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7273,6 +7271,307 @@ x
 q
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	ref	col_varchar_key	col_varchar_key	4	test.alias2.col_varchar_nokey	1	Using index
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; FirstMatch(alias2)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	FirstMatch(ot)
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 #
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for

=== modified file 'mysql-test/r/subquery_sj_loosescan.result'
--- a/mysql-test/r/subquery_sj_loosescan.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_loosescan.result	2012-01-12 14:53:51 +0000
@@ -6607,8 +6607,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7271,5 +7269,306 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	Start temporary
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using where; Using index; Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index; Start temporary
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	Start temporary; End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	Start temporary; End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	Start temporary; End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_loosescan_bka.result'
--- a/mysql-test/r/subquery_sj_loosescan_bka.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_bka.result	2012-01-12 14:53:51 +0000
@@ -6608,8 +6608,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7272,6 +7270,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	Start temporary
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using where; Using index; Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_loosescan_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_loosescan_bka_nixbnl.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_bka_nixbnl.result	2012-01-12 14:53:51 +0000
@@ -6608,8 +6608,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7272,6 +7270,307 @@ x
 q
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	ref	col_int_key	col_int_key	5	test.grandparent1.col_int_key	2	Using index
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Using where; Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where
+2	DEPENDENT SUBQUERY	parent1	ref	col_varchar_key	col_varchar_key	4	test.child1.col_varchar_nokey	2	Using where; End temporary
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	Start temporary
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where
+1	PRIMARY	SQ2_alias1	ref	col_varchar_key	col_varchar_key	4	test.alias2.col_varchar_nokey	1	Using index
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; End temporary
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_loosescan_bkaunique.result'
--- a/mysql-test/r/subquery_sj_loosescan_bkaunique.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_bkaunique.result	2012-01-12 14:53:51 +0000
@@ -6609,8 +6609,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7273,6 +7271,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start temporary
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	Start temporary
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using where; Using index; Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; End temporary; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat.result'
--- a/mysql-test/r/subquery_sj_mat.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_mat.result	2012-01-12 14:53:51 +0000
@@ -6595,6 +6595,7 @@ col_varchar_nokey
 i
 h
 q
+a
 v
 u
 s
@@ -6606,8 +6607,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7270,5 +7269,306 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start materialize; Scan
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using index; Start materialize
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	End materialize; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index; Start temporary
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	Start temporary; End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	Start temporary; End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	Start temporary; End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat_bka.result'
--- a/mysql-test/r/subquery_sj_mat_bka.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_mat_bka.result	2012-01-12 14:53:51 +0000
@@ -6596,6 +6596,7 @@ col_varchar_nokey
 i
 h
 q
+a
 v
 u
 s
@@ -6607,8 +6608,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7271,6 +7270,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start materialize; Scan
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using index; Start materialize
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	End materialize; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_mat_bka_nixbnl.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_mat_bka_nixbnl.result	2012-01-12 14:53:51 +0000
@@ -6596,6 +6596,7 @@ col_varchar_nokey
 i
 h
 q
+a
 v
 u
 s
@@ -6607,8 +6608,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7271,6 +7270,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	ref	col_int_key	col_int_key	5	test.grandparent1.col_int_key	2	Using index
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Using where; Start materialize; Scan
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+2	DEPENDENT SUBQUERY	parent1	ref	col_varchar_key	col_varchar_key	4	test.child1.col_varchar_nokey	2	Using where
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using index; Start materialize
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	End materialize
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat_bkaunique.result'
--- a/mysql-test/r/subquery_sj_mat_bkaunique.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_mat_bkaunique.result	2012-01-12 14:53:51 +0000
@@ -6597,6 +6597,7 @@ col_varchar_nokey
 i
 h
 q
+a
 v
 u
 s
@@ -6608,8 +6609,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7272,6 +7271,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Start materialize; Scan
+2	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	col_varchar_key	NULL	NULL	NULL	8	Using where; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+1	PRIMARY	SQ2_alias1	index	col_varchar_key	col_varchar_key	9	NULL	4	Using index; Start materialize
+1	PRIMARY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	End materialize; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	test.ot2.col_varchar_nokey	1	Using index
+1	PRIMARY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+1	PRIMARY	ot1	ref	col_varchar_key	col_varchar_key	3	test.it2.col_varchar_nokey	1	Using index; End temporary
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	1	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort; Start temporary
+1	PRIMARY	it	ref	col_varchar_key	col_varchar_key	3	test.ot.col_varchar_nokey	2	End temporary
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat_nosj.result'
--- a/mysql-test/r/subquery_sj_mat_nosj.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_mat_nosj.result	2012-01-12 14:53:51 +0000
@@ -6683,8 +6683,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7347,5 +7345,306 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	SUBQUERY	parent1	ALL	NULL	NULL	NULL	NULL	8	Using where
+3	SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	
+3	SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; Using join buffer (Block Nested Loop)
+2	SUBQUERY	SQ2_alias1	index	NULL	col_varchar_key	9	NULL	4	Using index
+2	SUBQUERY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot1	index	NULL	col_varchar_key	7	NULL	20	Using index
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (Block Nested Loop)
+2	SUBQUERY	it1	index	PRIMARY	col_varchar_key	7	NULL	20	Using index
+2	SUBQUERY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	1	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	2	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	2	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_none.result'
--- a/mysql-test/r/subquery_sj_none.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_none.result	2012-01-12 14:53:51 +0000
@@ -6618,8 +6618,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7282,5 +7280,306 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	NULL	NULL	NULL	NULL	8	Using where
+3	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Using where
+3	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	SQ2_alias1	ref	col_varchar_key	col_varchar_key	4	func	1	Using index
+2	DEPENDENT SUBQUERY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot1	index	NULL	col_varchar_key	7	NULL	20	Using index
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	func	1	Using index
+2	DEPENDENT SUBQUERY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	Using where
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	1	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	2	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	2	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_none_bka.result'
--- a/mysql-test/r/subquery_sj_none_bka.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_none_bka.result	2012-01-12 14:53:51 +0000
@@ -6619,8 +6619,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7283,6 +7281,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	NULL	NULL	NULL	NULL	8	Using where
+3	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Using where
+3	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	SQ2_alias1	ref	col_varchar_key	col_varchar_key	4	func	1	Using index
+2	DEPENDENT SUBQUERY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot1	index	NULL	col_varchar_key	7	NULL	20	Using index
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	func	1	Using index
+2	DEPENDENT SUBQUERY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	Using where
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	1	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	2	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	2	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_none_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_none_bka_nixbnl.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_none_bka_nixbnl.result	2012-01-12 14:53:51 +0000
@@ -6619,8 +6619,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7283,6 +7281,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	ref	col_int_key	col_int_key	5	test.grandparent1.col_int_key	2	Using index
+2	DEPENDENT SUBQUERY	parent1	ALL	NULL	NULL	NULL	NULL	8	Using where
+3	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Using where
+3	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where
+2	DEPENDENT SUBQUERY	SQ2_alias1	ref	col_varchar_key	col_varchar_key	4	func	1	Using index
+2	DEPENDENT SUBQUERY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot1	index	NULL	col_varchar_key	7	NULL	20	Using index
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Using where
+2	DEPENDENT SUBQUERY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	func	1	Using index
+2	DEPENDENT SUBQUERY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	Using where
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	1	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	2	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	2	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_none_bkaunique.result'
--- a/mysql-test/r/subquery_sj_none_bkaunique.result	2011-12-13 08:49:11 +0000
+++ b/mysql-test/r/subquery_sj_none_bkaunique.result	2012-01-12 14:53:51 +0000
@@ -6620,8 +6620,6 @@ e
 i
 y
 w
--- Notice that Materialize-scan algorithm reports wrong result for this query.
--- This problem will be filed as a separate bug and dealt with in WL#5561.
 DROP TABLE t1, t2;
 # End of test for bug#12603183.
 #
@@ -7284,6 +7282,307 @@ q
 x
 DROP TABLE t1, t2;
 # End of test for bug#13414014.
+#
+# Bug#13545215: Missing rows on nested in-subquery with materialization
+#
+CREATE TABLE t1 (
+col_int_key int,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ;
+INSERT INTO t1 VALUES
+(8,'x','x'), (0,'p','p'), (8,'c','c');
+CREATE TABLE t2 (
+pk int NOT NULL,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t2 VALUES
+(1,'v','v'), (2,'v','v'), (3,'c','c'), (4,NULL,NULL),
+(5,'x','x'), (6,'i','i'), (7,'e','e'), (8,'p','p');
+CREATE TABLE t3 (
+col_int_nokey int
+);
+INSERT INTO t3 VALUES (7);
+explain SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	grandparent1	ALL	col_int_key	NULL	NULL	NULL	3	Using where
+1	PRIMARY	grandparent2	index	col_int_key	col_int_key	5	NULL	3	Using where; Using index; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	parent1	ALL	NULL	NULL	NULL	NULL	8	Using where
+3	DEPENDENT SUBQUERY	child1	ALL	NULL	NULL	NULL	NULL	8	Using where
+3	DEPENDENT SUBQUERY	child2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (Block Nested Loop)
+SELECT grandparent1.col_varchar_nokey
+FROM t1 AS grandparent1 JOIN t1 AS grandparent2 USING (col_int_key)
+WHERE grandparent1.col_varchar_key IN (
+SELECT col_varchar_nokey
+FROM t2 AS parent1
+WHERE col_varchar_key IN (
+SELECT child1.col_varchar_nokey
+FROM t2 AS child1 LEFT JOIN t3 AS child2
+ON child1.pk < child2.col_int_nokey
+)
+);
+col_varchar_nokey
+c
+c
+p
+x
+x
+DROP TABLE t1, t2, t3;
+# End of test for bug#13545215.
+#
+# BUG#13553211 - MISSING ROWS ON SELECT WITH IN-SUBQUERY AND
+# MATERIALIZATION + SEMIJOIN ON
+#
+CREATE TABLE t1 (
+col_int_key int(11) DEFAULT NULL,
+col_varchar_key varchar(1) DEFAULT NULL,
+col_varchar_nokey varchar(1) DEFAULT NULL,
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+);
+INSERT INTO t1 VALUES (4,'v','v');
+INSERT INTO t1 VALUES (62,'v','v');
+INSERT INTO t1 VALUES (7,'c','c');
+INSERT INTO t1 VALUES (1,NULL,NULL);
+EXPLAIN SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	4	
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	SQ2_alias1	ref	col_varchar_key	col_varchar_key	4	func	1	Using index
+2	DEPENDENT SUBQUERY	SQ2_alias2	ALL	NULL	NULL	NULL	NULL	4	Using where; Using join buffer (Block Nested Loop)
+SELECT
+alias1.col_varchar_nokey AS a1_nokey,
+alias1.col_varchar_key AS a1_key,
+alias2.col_varchar_nokey AS a2_nokey
+FROM
+t1 AS alias1, t1 AS alias2
+WHERE
+(alias1.col_varchar_nokey,alias2.col_varchar_nokey)
+IN
+(
+SELECT
+SQ2_alias2.col_varchar_nokey, SQ2_alias1.col_varchar_key
+FROM
+t1 AS SQ2_alias1, t1 AS SQ2_alias2
+)
+;
+a1_nokey	a1_key	a2_nokey
+c	c	c
+c	c	v
+c	c	v
+v	v	c
+v	v	c
+v	v	v
+v	v	v
+v	v	v
+v	v	v
+DROP TABLE t1;
+#
+# Bug#13541406: Wrong result with loosescan on select .. where .. in
+#
+CREATE TABLE t1 (
+col_int_key INT NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+KEY col_int_key (col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t1 VALUES
+(7,'v'), (0,'s'), (9,'l'), (3,'y'), (4,'c'), (2,'i'), (5,'h'), (3,'q'),
+(1,'a'), (3,'v'), (6,'u'), (7,'s'), (5,'y'), (1,'z'), (204,'h'), (224,'p'),
+(9,'e'), (5,'i'), (0,'y'), (3,'w');
+CREATE TABLE t2 (
+pk INT NOT NULL,
+col_int_key INT NOT NULL,
+col_varchar_key VARCHAR(1) NOT NULL,
+col_varchar_nokey VARCHAR(1) NOT NULL,
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) ENGINE=InnoDB;
+INSERT INTO t2 VALUES
+(1,0,'j','j'), (2,8,'v','v'), (3,1,'c','c'), (4,8,'m','m'),
+(5,9,'d','d'), (6,24,'d','d'), (7,6,'y','y'), (8,1,'t','t'),
+(9,6,'d','d'), (10,2,'s','s'), (11,4,'r','r'), (12,8,'m','m'),
+(13,4,'b','b'), (14,4,'x','x'), (15,7,'g','g'), (16,4,'p','p'),
+(17,1,'q','q'), (18,9,'w','w'), (19,4,'d','d'), (20,8,'e','e');
+# This query should never use a LooseScan strategy
+explain SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot1	index	NULL	col_varchar_key	7	NULL	20	Using index
+1	PRIMARY	ot2	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (Block Nested Loop)
+2	DEPENDENT SUBQUERY	it1	ref	PRIMARY,col_varchar_key	col_varchar_key	3	func	1	Using index
+2	DEPENDENT SUBQUERY	it2	ref	col_int_key	col_int_key	4	test.it1.pk	1	Using where
+SELECT ot1.col_int_key AS field1
+FROM t2 AS ot1, t2 AS ot2
+WHERE (ot1.col_varchar_key, ot2.col_varchar_nokey) IN (
+SELECT it2.col_varchar_nokey, it1.col_varchar_key
+FROM t2 AS it1 JOIN t1 AS it2 ON it2.col_int_key = it1.pk);
+field1
+1
+1
+1
+2
+6
+6
+6
+6
+6
+8
+8
+8
+8
+8
+8
+9
+DROP TABLE t1, t2;
+# End of test for bug#13541406.
+#
+# Bug#13576391: Missing rows on select with in-subquery and
+# batched-key-access=on and semijoin
+#
+CREATE TABLE t1 (
+col_int_nokey int NOT NULL,
+col_varchar_key varchar(1) NOT NULL,
+KEY col_varchar_key (col_varchar_key)
+) engine=InnoDB;
+INSERT INTO t1 VALUES
+(1,'v'), (7,'s'), (4,'l'), (7,'y'), (0,'c'), (2,'i'), (9,'h'), (4,'q'),
+(0,'a'), (9,'v'), (1,'u'), (3,'s'), (8,'y'), (8,'z'), (18,'h'), (84,'p'),
+(6,'e'), (3,'i'), (6,'y'), (6,'w');
+CREATE TABLE t2 (
+col_int_nokey int NOT NULL,
+col_varchar_nokey varchar(1) NOT NULL
+) engine=InnoDB;
+INSERT INTO t2 VALUES
+(4,'j'), (6,'v'), (3,'c'), (5,'m'), (3,'d'), (246,'d'), (2,'y'), (9,'t'),
+(3,'d'), (8,'s'), (1,'r'), (8,'m'), (8,'b'), (5,'x'), (7,'g'), (5,'p'),
+(1,'q'), (6,'w'), (2,'d'), (9,'e');
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	1	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=MyISAM;
+ALTER TABLE t2 ENGINE=MyISAM;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	2	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+ALTER TABLE t1 ENGINE=Memory;
+ALTER TABLE t2 ENGINE=Memory;
+explain SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	20	Using where; Using filesort
+2	DEPENDENT SUBQUERY	it	index_subquery	col_varchar_key	col_varchar_key	3	func	2	Using where
+SELECT col_varchar_nokey
+FROM t2 AS ot
+WHERE col_varchar_nokey IN (
+SELECT col_varchar_key
+FROM t1 AS it
+WHERE it.col_int_nokey <= it.col_int_nokey
+AND NOT ot.col_int_nokey < 2
+)
+ORDER BY col_varchar_nokey;
+col_varchar_nokey
+c
+e
+p
+s
+v
+w
+y
+DROP TABLE t1, t2;
+# End of test for bug#13576391.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/view.result'
--- a/mysql-test/r/view.result	2011-12-09 08:59:22 +0000
+++ b/mysql-test/r/view.result	2012-01-10 18:58:10 +0000
@@ -2686,7 +2686,7 @@ SELECT id, date(d) + INTERVAL TIME_TO_SE
 FROM t1 GROUP BY id, t;
 SHOW CREATE VIEW v1;
 View	Create View	character_set_client	collation_connection
-v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`id` AS `id`,(cast(`t1`.`d` as date) + interval time_to_sec(`t1`.`d`) second) AS `t`,count(0) AS `COUNT(*)` from `t1` group by `t1`.`id`,(cast(`t1`.`d` as date) + interval time_to_sec(`t1`.`d`) second)	latin1	latin1_swedish_ci
+v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`id` AS `id`,(cast(`t1`.`d` as date) + interval time_to_sec(`t1`.`d`) second) AS `t`,count(0) AS `COUNT(*)` from `t1` group by `t1`.`id`,`t`	latin1	latin1_swedish_ci
 SELECT * FROM v1;
 id	t	COUNT(*)
 DROP VIEW v1;

=== modified file 'mysql-test/r/view_alias.result'
--- a/mysql-test/r/view_alias.result	2010-03-09 10:36:26 +0000
+++ b/mysql-test/r/view_alias.result	2012-01-10 18:58:10 +0000
@@ -96,16 +96,16 @@ DROP VIEW v1;
 CREATE VIEW v1 AS SELECT a FROM t1 WHERE EXISTS (SELECT a AS alias FROM t1 GROUP BY alias);
 SHOW CREATE VIEW v1;
 View	Create View	character_set_client	collation_connection
-v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where exists(select `t1`.`a` AS `alias` from `t1` group by `t1`.`a`)	latin1	latin1_swedish_ci
+v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where exists(select `t1`.`a` AS `alias` from `t1` group by `alias`)	latin1	latin1_swedish_ci
 DROP VIEW v1;
-CREATE VIEW v1 AS select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select `test`.`t1`.`a` AS `alias` from `test`.`t1` group by `test`.`t1`.`a`);
+CREATE VIEW v1 AS select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select `test`.`t1`.`a` AS `alias` from `test`.`t1` group by `alias`);
 DROP VIEW v1;
 # Alias as the expression column name.
 CREATE VIEW v1 AS SELECT a FROM t1 WHERE EXISTS (SELECT '  a   ' AS alias FROM t1 GROUP BY alias);
 SHOW CREATE VIEW v1;
 View	Create View	character_set_client	collation_connection
-v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where exists(select '  a   ' AS `alias` from `t1` group by '  a   ')	latin1	latin1_swedish_ci
+v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where exists(select '  a   ' AS `alias` from `t1` group by `alias`)	latin1	latin1_swedish_ci
 DROP VIEW v1;
-CREATE VIEW v1 AS select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select '  a   ' AS `alias` from `test`.`t1` group by '  a   ');
+CREATE VIEW v1 AS select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select '  a   ' AS `alias` from `test`.`t1` group by `alias`);
 DROP VIEW v1;
 DROP TABLE t1, t2;

=== modified file 'mysql-test/suite/innodb/r/innodb_gis.result'
--- a/mysql-test/suite/innodb/r/innodb_gis.result	2011-08-03 11:29:20 +0000
+++ b/mysql-test/suite/innodb/r/innodb_gis.result	2012-01-10 18:58:10 +0000
@@ -405,7 +405,7 @@ id	select_type	table	type	possible_keys	
 1	SIMPLE	g1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary; Using filesort
 1	SIMPLE	g2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using join buffer (Block Nested Loop)
 Warnings:
-Note	1003	/* select#1 */ select `test`.`g1`.`fid` AS `first`,`test`.`g2`.`fid` AS `second`,mbrwithin(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `w`,mbrcontains(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `c`,mbroverlaps(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `o`,mbrequals(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `e`,mbrdisjoint(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `d`,st_touches(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `t`,mbrintersects(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `i`,st_crosses(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `r` from `test`.`gis_geometrycollection` `g1` join `test`.`gis_geometrycollection` `g2` order by `test`.`g1`.`fid`,`test`.`g2`.`fid`
+Note	1003	/* select#1 */ select `test`.`g1`.`fid` AS `first`,`test`.`g2`.`fid` AS `second`,mbrwithin(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `w`,mbrcontains(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `c`,mbroverlaps(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `o`,mbrequals(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `e`,mbrdisjoint(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `d`,st_touches(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `t`,mbrintersects(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `i`,st_crosses(`test`.`g1`.`g`,`test`.`g2`.`g`) AS `r` from `test`.`gis_geometrycollection` `g1` join `test`.`gis_geometrycollection` `g2` order by `first`,`second`
 DROP TABLE gis_point, gis_line, gis_polygon, gis_multi_point, gis_multi_line, gis_multi_polygon, gis_geometrycollection, gis_geometry;
 CREATE TABLE t1 (
 a INTEGER PRIMARY KEY AUTO_INCREMENT,

=== modified file 'mysql-test/suite/opt_trace/r/filesort_pq.result'
--- a/mysql-test/suite/opt_trace/r/filesort_pq.result	2011-11-21 08:51:57 +0000
+++ b/mysql-test/suite/opt_trace/r/filesort_pq.result	2012-01-10 18:58:10 +0000
@@ -1318,7 +1318,7 @@ SELECT CONCAT("hello ", f2) AS foo FROM 
         "select#": 1,
         "steps": [
           {
-            "expanded_query": "/* select#1 */ select concat('hello ',`test`.`t1`.`f2`) AS `foo` from `test`.`t1` order by concat('hello ',`test`.`t1`.`f2`) limit 2"
+            "expanded_query": "/* select#1 */ select concat('hello ',`test`.`t1`.`f2`) AS `foo` from `test`.`t1` order by `foo` limit 2"
           }
         ]
       }
@@ -1389,14 +1389,14 @@ SELECT CONCAT("hello ", f2) AS foo FROM 
           {
             "clause_processing": {
               "clause": "ORDER BY",
-              "original_clause": "concat('hello ',`test`.`t1`.`f2`)",
+              "original_clause": "`foo`",
               "items": [
                 {
                   "item": "concat('hello ',`test`.`t1`.`f2`)"
                 }
               ],
               "resulting_clause_is_simple": true,
-              "resulting_clause": "concat('hello ',`test`.`t1`.`f2`)"
+              "resulting_clause": "`foo`"
             }
           },
           {

=== modified file 'mysql-test/suite/opt_trace/r/general2_no_prot.result'
--- a/mysql-test/suite/opt_trace/r/general2_no_prot.result	2012-01-03 11:04:14 +0000
+++ b/mysql-test/suite/opt_trace/r/general2_no_prot.result	2012-01-10 18:58:10 +0000
@@ -3340,7 +3340,7 @@ select 497, tmp.ID, NULL from (select 49
         "select#": 2,
         "steps": [
           {
-            "expanded_query": "/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from ((`test`.`t1` join `test`.`t2` on((`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`))) join `test`.`t3` on((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`))) group by `test`.`t2`.`PARID` order by max(`test`.`t3`.`DATA`) desc"
+            "expanded_query": "/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from ((`test`.`t1` join `test`.`t2` on((`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`))) join `test`.`t3` on((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`))) group by `test`.`t2`.`PARID` order by `DATA` desc"
           }
         ] /* steps */
       } /* join_preparation */
@@ -3350,7 +3350,7 @@ select 497, tmp.ID, NULL from (select 49
         "select#": 1,
         "steps": [
           {
-            "expanded_query": "/* select#1 */ select 497 AS `497`,`tmp`.`ID` AS `ID`,NULL AS `NULL` from (/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from ((`test`.`t1` join `test`.`t2` on((`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`))) join `test`.`t3` on((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`))) group by `test`.`t2`.`PARID` order by max(`test`.`t3`.`DATA`) desc) `tmp`"
+            "expanded_query": "/* select#1 */ select 497 AS `497`,`tmp`.`ID` AS `ID`,NULL AS `NULL` from (/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from ((`test`.`t1` join `test`.`t2` on((`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`))) join `test`.`t3` on((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`))) group by `test`.`t2`.`PARID` order by `DATA` desc) `tmp`"
           }
         ] /* steps */
       } /* join_preparation */
@@ -3369,7 +3369,7 @@ select 497, tmp.ID, NULL from (select 49
                       "JOIN_condition_to_WHERE",
                       "parenthesis_removal"
                     ] /* transformations */,
-                    "expanded_query": "/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from `test`.`t1` join `test`.`t2` join `test`.`t3` where ((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`) and (`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`)) group by `test`.`t2`.`PARID` order by max(`test`.`t3`.`DATA`) desc"
+                    "expanded_query": "/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from `test`.`t1` join `test`.`t2` join `test`.`t3` where ((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`) and (`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`)) group by `test`.`t2`.`PARID` order by `DATA` desc"
                   } /* transformations_to_nested_joins */
                 },
                 {
@@ -4685,7 +4685,7 @@ GROUP BY field2 ORDER BY alias1.col_int_
         "select#": 1,
         "steps": [
           {
-            "expanded_query": "/* select#1 */ select sum(`test`.`alias2`.`col_varchar_nokey`) AS `SUM(alias2.col_varchar_nokey)`,`test`.`alias2`.`pk` AS `field2` from (`test`.`t1` `alias1` straight_join `test`.`t2` `alias2` on((`test`.`alias2`.`pk` = `test`.`alias1`.`col_int_key`))) where `test`.`alias1`.`pk` group by `test`.`alias2`.`pk` order by `test`.`alias1`.`col_int_key`,`test`.`alias2`.`pk`"
+            "expanded_query": "/* select#1 */ select sum(`test`.`alias2`.`col_varchar_nokey`) AS `SUM(alias2.col_varchar_nokey)`,`test`.`alias2`.`pk` AS `field2` from (`test`.`t1` `alias1` straight_join `test`.`t2` `alias2` on((`test`.`alias2`.`pk` = `test`.`alias1`.`col_int_key`))) where `test`.`alias1`.`pk` group by `field2` order by `test`.`alias1`.`col_int_key`,`test`.`alias2`.`pk`"
           }
         ] /* steps */
       } /* join_preparation */
@@ -4700,7 +4700,7 @@ GROUP BY field2 ORDER BY alias1.col_int_
                 "JOIN_condition_to_WHERE",
                 "parenthesis_removal"
               ] /* transformations */,
-              "expanded_query": "/* select#1 */ select sum(`test`.`alias2`.`col_varchar_nokey`) AS `SUM(alias2.col_varchar_nokey)`,`test`.`alias2`.`pk` AS `field2` from `test`.`t1` `alias1` straight_join `test`.`t2` `alias2` where (`test`.`alias1`.`pk` and (`test`.`alias2`.`pk` = `test`.`alias1`.`col_int_key`)) group by `test`.`alias2`.`pk` order by `test`.`alias1`.`col_int_key`,`test`.`alias2`.`pk`"
+              "expanded_query": "/* select#1 */ select sum(`test`.`alias2`.`col_varchar_nokey`) AS `SUM(alias2.col_varchar_nokey)`,`test`.`alias2`.`pk` AS `field2` from `test`.`t1` `alias1` straight_join `test`.`t2` `alias2` where (`test`.`alias1`.`pk` and (`test`.`alias2`.`pk` = `test`.`alias1`.`col_int_key`)) group by `field2` order by `test`.`alias1`.`col_int_key`,`test`.`alias2`.`pk`"
             } /* transformations_to_nested_joins */
           },
           {
@@ -4891,14 +4891,14 @@ GROUP BY field2 ORDER BY alias1.col_int_
           {
             "clause_processing": {
               "clause": "GROUP BY",
-              "original_clause": "`test`.`alias2`.`pk`",
+              "original_clause": "`field2`",
               "items": [
                 {
                   "item": "`test`.`alias2`.`pk`"
                 }
               ] /* items */,
               "resulting_clause_is_simple": false,
-              "resulting_clause": "`test`.`alias2`.`pk`"
+              "resulting_clause": "`field2`"
             } /* clause_processing */
           },
           {

=== modified file 'mysql-test/suite/opt_trace/r/general2_ps_prot.result'
--- a/mysql-test/suite/opt_trace/r/general2_ps_prot.result	2012-01-03 11:04:14 +0000
+++ b/mysql-test/suite/opt_trace/r/general2_ps_prot.result	2012-01-10 18:58:10 +0000
@@ -3393,7 +3393,7 @@ select 497, tmp.ID, NULL from (select 49
         "select#": 2,
         "steps": [
           {
-            "expanded_query": "/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from ((`test`.`t1` join `test`.`t2` on((`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`))) join `test`.`t3` on((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`))) group by `test`.`t2`.`PARID` order by max(`test`.`t3`.`DATA`) desc"
+            "expanded_query": "/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from ((`test`.`t1` join `test`.`t2` on((`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`))) join `test`.`t3` on((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`))) group by `test`.`t2`.`PARID` order by `DATA` desc"
           }
         ] /* steps */
       } /* join_preparation */
@@ -3403,7 +3403,7 @@ select 497, tmp.ID, NULL from (select 49
         "select#": 1,
         "steps": [
           {
-            "expanded_query": "/* select#1 */ select 497 AS `497`,`tmp`.`ID` AS `ID`,NULL AS `NULL` from (/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from ((`test`.`t1` join `test`.`t2` on((`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`))) join `test`.`t3` on((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`))) group by `test`.`t2`.`PARID` order by max(`test`.`t3`.`DATA`) desc) `tmp`"
+            "expanded_query": "/* select#1 */ select 497 AS `497`,`tmp`.`ID` AS `ID`,NULL AS `NULL` from (/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from ((`test`.`t1` join `test`.`t2` on((`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`))) join `test`.`t3` on((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`))) group by `test`.`t2`.`PARID` order by `DATA` desc) `tmp`"
           }
         ] /* steps */
       } /* join_preparation */
@@ -3422,7 +3422,7 @@ select 497, tmp.ID, NULL from (select 49
                       "JOIN_condition_to_WHERE",
                       "parenthesis_removal"
                     ] /* transformations */,
-                    "expanded_query": "/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from `test`.`t1` join `test`.`t2` join `test`.`t3` where ((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`) and (`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`)) group by `test`.`t2`.`PARID` order by max(`test`.`t3`.`DATA`) desc"
+                    "expanded_query": "/* select#2 */ select 497 AS `ID`,max(`test`.`t3`.`DATA`) AS `DATA` from `test`.`t1` join `test`.`t2` join `test`.`t3` where ((`test`.`t1`.`OBJECTID` = `test`.`t3`.`ID`) and (`test`.`t1`.`OBJECTID` = `test`.`t2`.`ID`)) group by `test`.`t2`.`PARID` order by `DATA` desc"
                   } /* transformations_to_nested_joins */
                 },
                 {
@@ -4738,7 +4738,7 @@ GROUP BY field2 ORDER BY alias1.col_int_
         "select#": 1,
         "steps": [
           {
-            "expanded_query": "/* select#1 */ select sum(`test`.`alias2`.`col_varchar_nokey`) AS `SUM(alias2.col_varchar_nokey)`,`test`.`alias2`.`pk` AS `field2` from (`test`.`t1` `alias1` straight_join `test`.`t2` `alias2` on((`test`.`alias2`.`pk` = `test`.`alias1`.`col_int_key`))) where `test`.`alias1`.`pk` group by `test`.`alias2`.`pk` order by `test`.`alias1`.`col_int_key`,`test`.`alias2`.`pk`"
+            "expanded_query": "/* select#1 */ select sum(`test`.`alias2`.`col_varchar_nokey`) AS `SUM(alias2.col_varchar_nokey)`,`test`.`alias2`.`pk` AS `field2` from (`test`.`t1` `alias1` straight_join `test`.`t2` `alias2` on((`test`.`alias2`.`pk` = `test`.`alias1`.`col_int_key`))) where `test`.`alias1`.`pk` group by `field2` order by `test`.`alias1`.`col_int_key`,`test`.`alias2`.`pk`"
           }
         ] /* steps */
       } /* join_preparation */
@@ -4753,7 +4753,7 @@ GROUP BY field2 ORDER BY alias1.col_int_
                 "JOIN_condition_to_WHERE",
                 "parenthesis_removal"
               ] /* transformations */,
-              "expanded_query": "/* select#1 */ select sum(`test`.`alias2`.`col_varchar_nokey`) AS `SUM(alias2.col_varchar_nokey)`,`test`.`alias2`.`pk` AS `field2` from `test`.`t1` `alias1` straight_join `test`.`t2` `alias2` where (`test`.`alias1`.`pk` and (`test`.`alias2`.`pk` = `test`.`alias1`.`col_int_key`)) group by `test`.`alias2`.`pk` order by `test`.`alias1`.`col_int_key`,`test`.`alias2`.`pk`"
+              "expanded_query": "/* select#1 */ select sum(`test`.`alias2`.`col_varchar_nokey`) AS `SUM(alias2.col_varchar_nokey)`,`test`.`alias2`.`pk` AS `field2` from `test`.`t1` `alias1` straight_join `test`.`t2` `alias2` where (`test`.`alias1`.`pk` and (`test`.`alias2`.`pk` = `test`.`alias1`.`col_int_key`)) group by `field2` order by `test`.`alias1`.`col_int_key`,`test`.`alias2`.`pk`"
             } /* transformations_to_nested_joins */
           },
           {
@@ -4944,14 +4944,14 @@ GROUP BY field2 ORDER BY alias1.col_int_
           {
             "clause_processing": {
               "clause": "GROUP BY",
-              "original_clause": "`test`.`alias2`.`pk`",
+              "original_clause": "`field2`",
               "items": [
                 {
                   "item": "`test`.`alias2`.`pk`"
                 }
               ] /* items */,
               "resulting_clause_is_simple": false,
-              "resulting_clause": "`test`.`alias2`.`pk`"
+              "resulting_clause": "`field2`"
             } /* clause_processing */
           },
           {

=== modified file 'mysql-test/suite/opt_trace/r/subquery_no_prot.result'
--- a/mysql-test/suite/opt_trace/r/subquery_no_prot.result	2011-11-21 11:58:25 +0000
+++ b/mysql-test/suite/opt_trace/r/subquery_no_prot.result	2012-01-10 18:58:10 +0000
@@ -1013,7 +1013,7 @@ field4,field5,field6	{
             } /* join_preparation */
           },
           {
-            "expanded_query": "/* select#1 */ select distinct `test`.`alias1`.`col_varchar_key` AS `field1`,`test`.`alias1`.`col_date_key` AS `field2`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))) AS `field3`,(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`) AS `field4`,`alias2`.`col_varchar_nokey` AS!
  `field5`,`alias2`.`col_varchar_nokey` AS `field6` from ((`test`.`t4` `alias3` left join ((/* select#5 */ select `test`.`sq3_alias2`.`pk` AS `pk`,`test`.`sq3_alias2`.`col_int_nokey` AS `col_int_nokey`,`test`.`sq3_alias2`.`col_int_key` AS `col_int_key`,`test`.`sq3_alias2`.`col_date_key` AS `col_date_key`,`test`.`sq3_alias2`.`col_date_nokey` AS `col_date_nokey`,`test`.`sq3_alias2`.`col_time_key` AS `col_time_key`,`test`.`sq3_alias2`.`col_time_nokey` AS `col_time_nokey`,`test`.`sq3_alias2`.`col_datetime_key` AS `col_datetime_key`,`test`.`sq3_alias2`.`col_datetime_nokey` AS `col_datetime_nokey`,`test`.`sq3_alias2`.`col_varchar_key` AS `col_varchar_key`,`test`.`sq3_alias2`.`col_varchar_nokey` AS `col_varchar_nokey` from (`test`.`t5` `sq3_alias1` join `test`.`t4` `sq3_alias2`))) `alias2` on((`test`.`alias3`.`col_varchar_key` = `alias2`.`col_varchar_key`))) left join `test`.`t5` `alias1` on((`test`.`alias3`.`col_int_key` = `alias2`.`pk`))) where (<in_optimizer>(`test`.`alias1`.`co!
 l_varchar_nokey`,<exists>(/* select#6 */ select 1 from (`test`.`t3` `s
q4_alias1` join (`test`.`t3` `sq4_alias3` left join `test`.`t2` `sq4_alias2` on((`test`.`sq4_alias3`.`pk` = `test`.`sq4_alias2`.`col_int_key`))) on((`test`.`sq4_alias3`.`col_varchar_nokey` = `test`.`sq4_alias2`.`col_varchar_key`))) where ((`test`.`sq4_alias2`.`col_int_key` < `test`.`alias1`.`col_int_nokey`) and (`test`.`sq4_alias3`.`col_varchar_nokey` <> `test`.`alias1`.`col_varchar_key`) and (<cache>(`test`.`alias1`.`col_varchar_nokey`) = `test`.`sq4_alias1`.`col_varchar_key`)))) and (`test`.`alias1`.`col_int_key` <> 214)) group by `test`.`alias1`.`col_varchar_key`,`test`.`alias1`.`col_date_key`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ se!
 lect `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))),(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`),`alias2`.`col_varchar_nokey`,`alias2`.`col_varchar_nokey`"
+            "expanded_query": "/* select#1 */ select distinct `test`.`alias1`.`col_varchar_key` AS `field1`,`test`.`alias1`.`col_date_key` AS `field2`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))) AS `field3`,(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`) AS `field4`,`alias2`.`col_varchar_nokey` AS!
  `field5`,`alias2`.`col_varchar_nokey` AS `field6` from ((`test`.`t4` `alias3` left join ((/* select#5 */ select `test`.`sq3_alias2`.`pk` AS `pk`,`test`.`sq3_alias2`.`col_int_nokey` AS `col_int_nokey`,`test`.`sq3_alias2`.`col_int_key` AS `col_int_key`,`test`.`sq3_alias2`.`col_date_key` AS `col_date_key`,`test`.`sq3_alias2`.`col_date_nokey` AS `col_date_nokey`,`test`.`sq3_alias2`.`col_time_key` AS `col_time_key`,`test`.`sq3_alias2`.`col_time_nokey` AS `col_time_nokey`,`test`.`sq3_alias2`.`col_datetime_key` AS `col_datetime_key`,`test`.`sq3_alias2`.`col_datetime_nokey` AS `col_datetime_nokey`,`test`.`sq3_alias2`.`col_varchar_key` AS `col_varchar_key`,`test`.`sq3_alias2`.`col_varchar_nokey` AS `col_varchar_nokey` from (`test`.`t5` `sq3_alias1` join `test`.`t4` `sq3_alias2`))) `alias2` on((`test`.`alias3`.`col_varchar_key` = `alias2`.`col_varchar_key`))) left join `test`.`t5` `alias1` on((`test`.`alias3`.`col_int_key` = `alias2`.`pk`))) where (<in_optimizer>(`test`.`alias1`.`co!
 l_varchar_nokey`,<exists>(/* select#6 */ select 1 from (`test`.`t3` `s
q4_alias1` join (`test`.`t3` `sq4_alias3` left join `test`.`t2` `sq4_alias2` on((`test`.`sq4_alias3`.`pk` = `test`.`sq4_alias2`.`col_int_key`))) on((`test`.`sq4_alias3`.`col_varchar_nokey` = `test`.`sq4_alias2`.`col_varchar_key`))) where ((`test`.`sq4_alias2`.`col_int_key` < `test`.`alias1`.`col_int_nokey`) and (`test`.`sq4_alias3`.`col_varchar_nokey` <> `test`.`alias1`.`col_varchar_key`) and (<cache>(`test`.`alias1`.`col_varchar_nokey`) = `test`.`sq4_alias1`.`col_varchar_key`)))) and (`test`.`alias1`.`col_int_key` <> 214)) group by `field1`,`field2`,`field3`,`field4`,`field5`,`field6`"
           }
         ] /* steps */
       } /* join_preparation */
@@ -1127,7 +1127,7 @@ field4,field5,field6	{
                 "JOIN_condition_to_WHERE",
                 "parenthesis_removal"
               ] /* transformations */,
-              "expanded_query": "/* select#1 */ select distinct `test`.`alias1`.`col_varchar_key` AS `field1`,`test`.`alias1`.`col_date_key` AS `field2`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))) AS `field3`,(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`) AS `field4`,`alias2`.`col_varchar_nokey` !
 AS `field5`,`alias2`.`col_varchar_nokey` AS `field6` from `test`.`t4` `alias3` join ((/* select#5 */ select '0' AS `pk`,'7' AS `col_int_nokey`,'8' AS `col_int_key`,'2008-10-02' AS `col_date_key`,'2008-10-02' AS `col_date_nokey`,'04:07:22' AS `col_time_key`,'04:07:22' AS `col_time_nokey`,'2001-10-08 00:00:00' AS `col_datetime_key`,'2001-10-08 00:00:00' AS `col_datetime_nokey`,'g' AS `col_varchar_key`,'g' AS `col_varchar_nokey` from `test`.`t5` `sq3_alias1`)) `alias2` join `test`.`t5` `alias1` where (<in_optimizer>(`test`.`alias1`.`col_varchar_nokey`,<exists>(/* select#6 */ select 1 from (`test`.`t3` `sq4_alias1` join (`test`.`t3` `sq4_alias3` left join `test`.`t2` `sq4_alias2` on((`test`.`sq4_alias3`.`pk` = `test`.`sq4_alias2`.`col_int_key`))) on((`test`.`sq4_alias3`.`col_varchar_nokey` = `test`.`sq4_alias2`.`col_varchar_key`))) where ((`test`.`sq4_alias2`.`col_int_key` < `test`.`alias1`.`col_int_nokey`) and (`test`.`sq4_alias3`.`col_varchar_nokey` <> `test`.`alias1`.`col_va!
 rchar_key`) and (<cache>(`test`.`alias1`.`col_varchar_nokey`) = `test`
.`sq4_alias1`.`col_varchar_key`)))) and (`test`.`alias1`.`col_int_key` <> 214) and (`test`.`alias3`.`col_int_key` = `alias2`.`pk`) and (`test`.`alias3`.`col_varchar_key` = `alias2`.`col_varchar_key`)) group by `test`.`alias1`.`col_varchar_key`,`test`.`alias1`.`col_date_key`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))),(/* select#4 */ select max(`test!
 `.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`),`alias2`.`col_varchar_nokey`,`alias2`.`col_varchar_nokey`"
+              "expanded_query": "/* select#1 */ select distinct `test`.`alias1`.`col_varchar_key` AS `field1`,`test`.`alias1`.`col_date_key` AS `field2`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))) AS `field3`,(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`) AS `field4`,`alias2`.`col_varchar_nokey` !
 AS `field5`,`alias2`.`col_varchar_nokey` AS `field6` from `test`.`t4` `alias3` join ((/* select#5 */ select '0' AS `pk`,'7' AS `col_int_nokey`,'8' AS `col_int_key`,'2008-10-02' AS `col_date_key`,'2008-10-02' AS `col_date_nokey`,'04:07:22' AS `col_time_key`,'04:07:22' AS `col_time_nokey`,'2001-10-08 00:00:00' AS `col_datetime_key`,'2001-10-08 00:00:00' AS `col_datetime_nokey`,'g' AS `col_varchar_key`,'g' AS `col_varchar_nokey` from `test`.`t5` `sq3_alias1`)) `alias2` join `test`.`t5` `alias1` where (<in_optimizer>(`test`.`alias1`.`col_varchar_nokey`,<exists>(/* select#6 */ select 1 from (`test`.`t3` `sq4_alias1` join (`test`.`t3` `sq4_alias3` left join `test`.`t2` `sq4_alias2` on((`test`.`sq4_alias3`.`pk` = `test`.`sq4_alias2`.`col_int_key`))) on((`test`.`sq4_alias3`.`col_varchar_nokey` = `test`.`sq4_alias2`.`col_varchar_key`))) where ((`test`.`sq4_alias2`.`col_int_key` < `test`.`alias1`.`col_int_nokey`) and (`test`.`sq4_alias3`.`col_varchar_nokey` <> `test`.`alias1`.`col_va!
 rchar_key`) and (<cache>(`test`.`alias1`.`col_varchar_nokey`) = `test`
.`sq4_alias1`.`col_varchar_key`)))) and (`test`.`alias1`.`col_int_key` <> 214) and (`test`.`alias3`.`col_int_key` = `alias2`.`pk`) and (`test`.`alias3`.`col_varchar_key` = `alias2`.`col_varchar_key`)) group by `field1`,`field2`,`field3`,`field4`,`field5`,`field6`"
             } /* transformations_to_nested_joins */
           },
           {
@@ -1428,7 +1428,7 @@ field4,field5,field6	{
           {
             "clause_processing": {
               "clause": "GROUP BY",
-              "original_clause": "`test`.`alias1`.`col_varchar_key`,`test`.`alias1`.`col_date_key`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))),(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`),`alias2`.`col_varchar_nokey`,`alias2`.`col_varchar_nokey`",
+              "original_clause": "`field1`,`field2`,`field3`,`field4`,`field5`,`field6`",
               "items": [
                 {
                   "item": "`test`.`alias1`.`col_varchar_key`"
@@ -1696,7 +1696,7 @@ field4,field5,field6	{
                 }
               ] /* items */,
               "resulting_clause_is_simple": false,
-              "resulting_clause": "`test`.`alias1`.`col_varchar_key`,`test`.`alias1`.`col_date_key`,`alias2`.`col_varchar_nokey`"
+              "resulting_clause": "`field1`,`field2`,`field5`"
             } /* clause_processing */
           },
           {
@@ -1868,7 +1868,7 @@ table2.`col_int_nokey` <> any ( select 5
             } /* join_preparation */
           },
           {
-            "expanded_query": "/* select#1 */ select (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) AS `field1` from (`test`.`t1` `table1` straight_join `test`.`t1` `table2` on((`test`.`table2`.`col_varchar_key` = `test`.`table1`.`col_varchar_key`))) where (<nop>(<in_optimizer>(`test`.`table2`.`col_int_nokey`,<exists>(/* select#3 */ select 1 from `test`.`t2` where trigcond_if(outer_field_is_not_null, (<cache>(`test`.`table2`.`col_int_nokey`) <> 5), true)))) and (`test`.`table1`.`pk` in (192,18))) order by (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) desc"
+            "expanded_query": "/* select#1 */ select (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) AS `field1` from (`test`.`t1` `table1` straight_join `test`.`t1` `table2` on((`test`.`table2`.`col_varchar_key` = `test`.`table1`.`col_varchar_key`))) where (<nop>(<in_optimizer>(`test`.`table2`.`col_int_nokey`,<exists>(/* select#3 */ select 1 from `test`.`t2` where trigcond_if(outer_field_is_not_null, (<cache>(`test`.`table2`.`col_int_nokey`) <> 5), true)))) and (`test`.`table1`.`pk` in (192,18))) order by `field1` desc"
           }
         ] /* steps */
       } /* join_preparation */
@@ -1883,7 +1883,7 @@ table2.`col_int_nokey` <> any ( select 5
                 "JOIN_condition_to_WHERE",
                 "parenthesis_removal"
               ] /* transformations */,
-              "expanded_query": "/* select#1 */ select (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) AS `field1` from `test`.`t1` `table1` straight_join `test`.`t1` `table2` where (<nop>(<in_optimizer>(`test`.`table2`.`col_int_nokey`,<exists>(/* select#3 */ select 1 from `test`.`t2` where trigcond_if(outer_field_is_not_null, (<cache>(`test`.`table2`.`col_int_nokey`) <> 5), true)))) and (`test`.`table1`.`pk` in (192,18)) and (`test`.`table2`.`col_varchar_key` = `test`.`table1`.`col_varchar_key`)) order by (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) desc"
+              "expanded_query": "/* select#1 */ select (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) AS `field1` from `test`.`t1` `table1` straight_join `test`.`t1` `table2` where (<nop>(<in_optimizer>(`test`.`table2`.`col_int_nokey`,<exists>(/* select#3 */ select 1 from `test`.`t2` where trigcond_if(outer_field_is_not_null, (<cache>(`test`.`table2`.`col_int_nokey`) <> 5), true)))) and (`test`.`table1`.`pk` in (192,18)) and (`test`.`table2`.`col_varchar_key` = `test`.`table1`.`col_varchar_key`)) order by `field1` desc"
             } /* transformations_to_nested_joins */
           },
           {
@@ -2102,7 +2102,7 @@ table2.`col_int_nokey` <> any ( select 5
           {
             "clause_processing": {
               "clause": "ORDER BY",
-              "original_clause": "(/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) desc",
+              "original_clause": "`field1` desc",
               "items": [
                 {
                   "item": "(/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`)",

=== modified file 'mysql-test/suite/opt_trace/r/subquery_ps_prot.result'
--- a/mysql-test/suite/opt_trace/r/subquery_ps_prot.result	2011-11-23 18:28:18 +0000
+++ b/mysql-test/suite/opt_trace/r/subquery_ps_prot.result	2012-01-10 18:58:10 +0000
@@ -991,7 +991,7 @@ field4,field5,field6	{
             } /* join_preparation */
           },
           {
-            "expanded_query": "/* select#1 */ select distinct `test`.`alias1`.`col_varchar_key` AS `field1`,`test`.`alias1`.`col_date_key` AS `field2`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))) AS `field3`,(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`) AS `field4`,`alias2`.`col_varchar_nokey` AS!
  `field5`,`alias2`.`col_varchar_nokey` AS `field6` from ((`test`.`t4` `alias3` left join ((/* select#5 */ select `test`.`sq3_alias2`.`pk` AS `pk`,`test`.`sq3_alias2`.`col_int_nokey` AS `col_int_nokey`,`test`.`sq3_alias2`.`col_int_key` AS `col_int_key`,`test`.`sq3_alias2`.`col_date_key` AS `col_date_key`,`test`.`sq3_alias2`.`col_date_nokey` AS `col_date_nokey`,`test`.`sq3_alias2`.`col_time_key` AS `col_time_key`,`test`.`sq3_alias2`.`col_time_nokey` AS `col_time_nokey`,`test`.`sq3_alias2`.`col_datetime_key` AS `col_datetime_key`,`test`.`sq3_alias2`.`col_datetime_nokey` AS `col_datetime_nokey`,`test`.`sq3_alias2`.`col_varchar_key` AS `col_varchar_key`,`test`.`sq3_alias2`.`col_varchar_nokey` AS `col_varchar_nokey` from (`test`.`t5` `sq3_alias1` join `test`.`t4` `sq3_alias2`))) `alias2` on((`test`.`alias3`.`col_varchar_key` = `alias2`.`col_varchar_key`))) left join `test`.`t5` `alias1` on((`test`.`alias3`.`col_int_key` = `alias2`.`pk`))) where (<in_optimizer>(`test`.`alias1`.`co!
 l_varchar_nokey`,<exists>(/* select#6 */ select 1 from (`test`.`t3` `s
q4_alias1` join (`test`.`t3` `sq4_alias3` left join `test`.`t2` `sq4_alias2` on((`test`.`sq4_alias3`.`pk` = `test`.`sq4_alias2`.`col_int_key`))) on((`test`.`sq4_alias3`.`col_varchar_nokey` = `test`.`sq4_alias2`.`col_varchar_key`))) where ((`test`.`sq4_alias2`.`col_int_key` < `test`.`alias1`.`col_int_nokey`) and (`test`.`sq4_alias3`.`col_varchar_nokey` <> `test`.`alias1`.`col_varchar_key`) and (<cache>(`test`.`alias1`.`col_varchar_nokey`) = `test`.`sq4_alias1`.`col_varchar_key`)))) and (`test`.`alias1`.`col_int_key` <> 214)) group by `test`.`alias1`.`col_varchar_key`,`test`.`alias1`.`col_date_key`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ se!
 lect `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))),(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`),`alias2`.`col_varchar_nokey`,`alias2`.`col_varchar_nokey`"
+            "expanded_query": "/* select#1 */ select distinct `test`.`alias1`.`col_varchar_key` AS `field1`,`test`.`alias1`.`col_date_key` AS `field2`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))) AS `field3`,(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`) AS `field4`,`alias2`.`col_varchar_nokey` AS!
  `field5`,`alias2`.`col_varchar_nokey` AS `field6` from ((`test`.`t4` `alias3` left join ((/* select#5 */ select `test`.`sq3_alias2`.`pk` AS `pk`,`test`.`sq3_alias2`.`col_int_nokey` AS `col_int_nokey`,`test`.`sq3_alias2`.`col_int_key` AS `col_int_key`,`test`.`sq3_alias2`.`col_date_key` AS `col_date_key`,`test`.`sq3_alias2`.`col_date_nokey` AS `col_date_nokey`,`test`.`sq3_alias2`.`col_time_key` AS `col_time_key`,`test`.`sq3_alias2`.`col_time_nokey` AS `col_time_nokey`,`test`.`sq3_alias2`.`col_datetime_key` AS `col_datetime_key`,`test`.`sq3_alias2`.`col_datetime_nokey` AS `col_datetime_nokey`,`test`.`sq3_alias2`.`col_varchar_key` AS `col_varchar_key`,`test`.`sq3_alias2`.`col_varchar_nokey` AS `col_varchar_nokey` from (`test`.`t5` `sq3_alias1` join `test`.`t4` `sq3_alias2`))) `alias2` on((`test`.`alias3`.`col_varchar_key` = `alias2`.`col_varchar_key`))) left join `test`.`t5` `alias1` on((`test`.`alias3`.`col_int_key` = `alias2`.`pk`))) where (<in_optimizer>(`test`.`alias1`.`co!
 l_varchar_nokey`,<exists>(/* select#6 */ select 1 from (`test`.`t3` `s
q4_alias1` join (`test`.`t3` `sq4_alias3` left join `test`.`t2` `sq4_alias2` on((`test`.`sq4_alias3`.`pk` = `test`.`sq4_alias2`.`col_int_key`))) on((`test`.`sq4_alias3`.`col_varchar_nokey` = `test`.`sq4_alias2`.`col_varchar_key`))) where ((`test`.`sq4_alias2`.`col_int_key` < `test`.`alias1`.`col_int_nokey`) and (`test`.`sq4_alias3`.`col_varchar_nokey` <> `test`.`alias1`.`col_varchar_key`) and (<cache>(`test`.`alias1`.`col_varchar_nokey`) = `test`.`sq4_alias1`.`col_varchar_key`)))) and (`test`.`alias1`.`col_int_key` <> 214)) group by `field1`,`field2`,`field3`,`field4`,`field5`,`field6`"
           }
         ] /* steps */
       } /* join_preparation */
@@ -1105,7 +1105,7 @@ field4,field5,field6	{
                 "JOIN_condition_to_WHERE",
                 "parenthesis_removal"
               ] /* transformations */,
-              "expanded_query": "/* select#1 */ select distinct `test`.`alias1`.`col_varchar_key` AS `field1`,`test`.`alias1`.`col_date_key` AS `field2`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))) AS `field3`,(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`) AS `field4`,`alias2`.`col_varchar_nokey` !
 AS `field5`,`alias2`.`col_varchar_nokey` AS `field6` from `test`.`t4` `alias3` join ((/* select#5 */ select '0' AS `pk`,'7' AS `col_int_nokey`,'8' AS `col_int_key`,'2008-10-02' AS `col_date_key`,'2008-10-02' AS `col_date_nokey`,'04:07:22' AS `col_time_key`,'04:07:22' AS `col_time_nokey`,'2001-10-08 00:00:00' AS `col_datetime_key`,'2001-10-08 00:00:00' AS `col_datetime_nokey`,'g' AS `col_varchar_key`,'g' AS `col_varchar_nokey` from `test`.`t5` `sq3_alias1`)) `alias2` join `test`.`t5` `alias1` where (<in_optimizer>(`test`.`alias1`.`col_varchar_nokey`,<exists>(/* select#6 */ select 1 from (`test`.`t3` `sq4_alias1` join (`test`.`t3` `sq4_alias3` left join `test`.`t2` `sq4_alias2` on((`test`.`sq4_alias3`.`pk` = `test`.`sq4_alias2`.`col_int_key`))) on((`test`.`sq4_alias3`.`col_varchar_nokey` = `test`.`sq4_alias2`.`col_varchar_key`))) where ((`test`.`sq4_alias2`.`col_int_key` < `test`.`alias1`.`col_int_nokey`) and (`test`.`sq4_alias3`.`col_varchar_nokey` <> `test`.`alias1`.`col_va!
 rchar_key`) and (<cache>(`test`.`alias1`.`col_varchar_nokey`) = `test`
.`sq4_alias1`.`col_varchar_key`)))) and (`test`.`alias1`.`col_int_key` <> 214) and (`test`.`alias3`.`col_int_key` = `alias2`.`pk`) and (`test`.`alias3`.`col_varchar_key` = `alias2`.`col_varchar_key`)) group by `test`.`alias1`.`col_varchar_key`,`test`.`alias1`.`col_date_key`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))),(/* select#4 */ select max(`test!
 `.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`),`alias2`.`col_varchar_nokey`,`alias2`.`col_varchar_nokey`"
+              "expanded_query": "/* select#1 */ select distinct `test`.`alias1`.`col_varchar_key` AS `field1`,`test`.`alias1`.`col_date_key` AS `field2`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))) AS `field3`,(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`) AS `field4`,`alias2`.`col_varchar_nokey` !
 AS `field5`,`alias2`.`col_varchar_nokey` AS `field6` from `test`.`t4` `alias3` join ((/* select#5 */ select '0' AS `pk`,'7' AS `col_int_nokey`,'8' AS `col_int_key`,'2008-10-02' AS `col_date_key`,'2008-10-02' AS `col_date_nokey`,'04:07:22' AS `col_time_key`,'04:07:22' AS `col_time_nokey`,'2001-10-08 00:00:00' AS `col_datetime_key`,'2001-10-08 00:00:00' AS `col_datetime_nokey`,'g' AS `col_varchar_key`,'g' AS `col_varchar_nokey` from `test`.`t5` `sq3_alias1`)) `alias2` join `test`.`t5` `alias1` where (<in_optimizer>(`test`.`alias1`.`col_varchar_nokey`,<exists>(/* select#6 */ select 1 from (`test`.`t3` `sq4_alias1` join (`test`.`t3` `sq4_alias3` left join `test`.`t2` `sq4_alias2` on((`test`.`sq4_alias3`.`pk` = `test`.`sq4_alias2`.`col_int_key`))) on((`test`.`sq4_alias3`.`col_varchar_nokey` = `test`.`sq4_alias2`.`col_varchar_key`))) where ((`test`.`sq4_alias2`.`col_int_key` < `test`.`alias1`.`col_int_nokey`) and (`test`.`sq4_alias3`.`col_varchar_nokey` <> `test`.`alias1`.`col_va!
 rchar_key`) and (<cache>(`test`.`alias1`.`col_varchar_nokey`) = `test`
.`sq4_alias1`.`col_varchar_key`)))) and (`test`.`alias1`.`col_int_key` <> 214) and (`test`.`alias3`.`col_int_key` = `alias2`.`pk`) and (`test`.`alias3`.`col_varchar_key` = `alias2`.`col_varchar_key`)) group by `field1`,`field2`,`field3`,`field4`,`field5`,`field6`"
             } /* transformations_to_nested_joins */
           },
           {
@@ -1406,7 +1406,7 @@ field4,field5,field6	{
           {
             "clause_processing": {
               "clause": "GROUP BY",
-              "original_clause": "`test`.`alias1`.`col_varchar_key`,`test`.`alias1`.`col_date_key`,(/* select#2 */ select min(`test`.`sq1_alias1`.`col_varchar_nokey`) AS `sq1_field1` from (`test`.`t1` `sq1_alias1` join (`test`.`t5` `sq1_alias2` left join `test`.`t5` `sq1_alias3` on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) on((`test`.`sq1_alias3`.`col_varchar_nokey` = `test`.`sq1_alias2`.`col_varchar_key`))) where exists(/* select#3 */ select `test`.`c_sq1_alias2`.`col_int_nokey` AS `c_sq1_field1` from (`test`.`t4` `c_sq1_alias2` left join `test`.`t3` `c_sq1_alias1` on((`test`.`c_sq1_alias2`.`col_int_nokey` = `test`.`c_sq1_alias1`.`pk`))) where (`test`.`c_sq1_alias2`.`col_varchar_key` = `test`.`sq1_alias2`.`col_varchar_nokey`))),(/* select#4 */ select max(`test`.`sq2_alias1`.`pk`) AS `sq2_field1` from `test`.`t5` `sq2_alias1`),`alias2`.`col_varchar_nokey`,`alias2`.`col_varchar_nokey`",
+              "original_clause": "`field1`,`field2`,`field3`,`field4`,`field5`,`field6`",
               "items": [
                 {
                   "item": "`test`.`alias1`.`col_varchar_key`"
@@ -1674,7 +1674,7 @@ field4,field5,field6	{
                 }
               ] /* items */,
               "resulting_clause_is_simple": false,
-              "resulting_clause": "`test`.`alias1`.`col_varchar_key`,`test`.`alias1`.`col_date_key`,`alias2`.`col_varchar_nokey`"
+              "resulting_clause": "`field1`,`field2`,`field5`"
             } /* clause_processing */
           },
           {
@@ -1836,7 +1836,7 @@ table2.`col_int_nokey` <> any ( select 5
             } /* join_preparation */
           },
           {
-            "expanded_query": "/* select#1 */ select (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) AS `field1` from (`test`.`t1` `table1` straight_join `test`.`t1` `table2` on((`test`.`table2`.`col_varchar_key` = `test`.`table1`.`col_varchar_key`))) where (<nop>(<in_optimizer>(`test`.`table2`.`col_int_nokey`,<exists>(/* select#3 */ select 1 from `test`.`t2` where trigcond_if(outer_field_is_not_null, (<cache>(`test`.`table2`.`col_int_nokey`) <> 5), true)))) and (`test`.`table1`.`pk` in (192,18))) order by (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) desc"
+            "expanded_query": "/* select#1 */ select (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) AS `field1` from (`test`.`t1` `table1` straight_join `test`.`t1` `table2` on((`test`.`table2`.`col_varchar_key` = `test`.`table1`.`col_varchar_key`))) where (<nop>(<in_optimizer>(`test`.`table2`.`col_int_nokey`,<exists>(/* select#3 */ select 1 from `test`.`t2` where trigcond_if(outer_field_is_not_null, (<cache>(`test`.`table2`.`col_int_nokey`) <> 5), true)))) and (`test`.`table1`.`pk` in (192,18))) order by `field1` desc"
           }
         ] /* steps */
       } /* join_preparation */
@@ -1851,7 +1851,7 @@ table2.`col_int_nokey` <> any ( select 5
                 "JOIN_condition_to_WHERE",
                 "parenthesis_removal"
               ] /* transformations */,
-              "expanded_query": "/* select#1 */ select (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) AS `field1` from `test`.`t1` `table1` straight_join `test`.`t1` `table2` where (<nop>(<in_optimizer>(`test`.`table2`.`col_int_nokey`,<exists>(/* select#3 */ select 1 from `test`.`t2` where trigcond_if(outer_field_is_not_null, (<cache>(`test`.`table2`.`col_int_nokey`) <> 5), true)))) and (`test`.`table1`.`pk` in (192,18)) and (`test`.`table2`.`col_varchar_key` = `test`.`table1`.`col_varchar_key`)) order by (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) desc"
+              "expanded_query": "/* select#1 */ select (/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) AS `field1` from `test`.`t1` `table1` straight_join `test`.`t1` `table2` where (<nop>(<in_optimizer>(`test`.`table2`.`col_int_nokey`,<exists>(/* select#3 */ select 1 from `test`.`t2` where trigcond_if(outer_field_is_not_null, (<cache>(`test`.`table2`.`col_int_nokey`) <> 5), true)))) and (`test`.`table1`.`pk` in (192,18)) and (`test`.`table2`.`col_varchar_key` = `test`.`table1`.`col_varchar_key`)) order by `field1` desc"
             } /* transformations_to_nested_joins */
           },
           {
@@ -2070,7 +2070,7 @@ table2.`col_int_nokey` <> any ( select 5
           {
             "clause_processing": {
               "clause": "ORDER BY",
-              "original_clause": "(/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`) desc",
+              "original_clause": "`field1` desc",
               "items": [
                 {
                   "item": "(/* select#2 */ select sum(`test`.`subquery1_t1`.`col_int_nokey`) AS `subquery1_field1` from `test`.`t1` `subquery1_t1`)",

=== modified file 'mysql-test/t/group_by.test'
--- a/mysql-test/t/group_by.test	2012-01-04 13:53:33 +0000
+++ b/mysql-test/t/group_by.test	2012-01-10 18:58:10 +0000
@@ -1609,3 +1609,102 @@ eval $query;
 
 DROP TABLE t1,t2;
 
+--echo # Bug 11923239 - ERROR WITH CORRELATED SUBQUERY IN VIEW WITH
+--echo # ONLY_FULL_GROUP_BY SQL MODE
+CREATE TABLE t1 (
+  pk INT,
+  col_int_key INT,
+  col_int_nokey INT,
+  col_varchar_key VARCHAR(10),
+  col_varchar_nokey VARCHAR(10),
+  KEY col_int_key (col_int_key),
+  KEY col_varchar_key (col_varchar_key)
+);
+INSERT INTO t1 VALUES (), ();
+
+let $query_with_alias_in_group_by=
+SELECT alias1.col_int_nokey AS field1,
+  (SELECT alias2.col_int_key
+   FROM t1 AS alias2
+   WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey
+  ) AS field2
+FROM t1 AS alias1
+GROUP BY field1, field2;
+
+let $query_with_no_alias_in_group_by=
+SELECT alias1.col_int_nokey AS field1,
+  (SELECT alias2.col_int_key
+   FROM t1 AS alias2
+   WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey
+  ) AS field2
+FROM t1 AS alias1
+GROUP BY field1,
+  (SELECT alias2.col_int_key
+   FROM t1 AS alias2
+   WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey
+  );
+
+--echo # In GROUP BY, aliases are printed as aliases.
+
+eval EXPLAIN EXTENDED $query_with_alias_in_group_by;
+
+--echo # In GROUP BY, expressions are printed as expressions.
+
+eval EXPLAIN EXTENDED $query_with_no_alias_in_group_by;
+
+--echo # Aliased expression in GROUP BY in a view.
+
+eval CREATE VIEW v1 AS $query_with_alias_in_group_by;
+
+--echo # In GROUP BY, aliases are printed as aliases.
+
+SHOW CREATE VIEW v1;
+
+SET @old_sql_mode = @@sql_mode;
+SET @@sql_mode='ONLY_FULL_GROUP_BY';
+
+eval $query_with_alias_in_group_by;
+
+--echo # The SELECT above has been accepted, and v1 was created
+--echo # using the same SELECT as above, so SELECTing from v1
+--echo # should be accepted.
+SELECT * FROM v1;
+
+--echo # Here is why in GROUP BY we print aliases of subqueries as
+--echo # aliases: below, "GROUP BY (subquery)" confuses
+--echo # ONLY_FULL_GROUP_BY, it causes an error though the subquery of
+--echo # GROUP BY and of SELECT list are the same. Fixing this would
+--echo # require implementing Item_subselect::eq(). It's not worth
+--echo # the effort because:
+--echo # a) GROUP BY (subquery) is non-SQL-standard so is likely of
+--echo # very little interest to users of ONLY_FULL_GROUP_BY
+--echo # b) as the user uses ONLY_FULL_GROUP_BY, he wants to have the
+--echo # same subquery in GROUP BY and SELECT list, so can give the
+--echo # subquery an alias in the SELECT list and use this alias in
+--echo # GROUP BY, thus avoiding the problem.
+
+--error ER_WRONG_FIELD_WITH_GROUP
+eval $query_with_no_alias_in_group_by;
+
+DROP VIEW v1;
+SET @@sql_mode = @old_sql_mode;
+
+--echo # Verify that if an alias is used in GROUP BY/ORDER BY it
+--echo # is printed as an alias, not as the expression.
+
+CREATE TABLE t2(a INT);
+INSERT INTO t2 VALUES(3),(4);
+
+EXPLAIN EXTENDED SELECT
+pk AS foo, col_int_key AS bar, (SELECT a FROM t2 WHERE a=t1.pk) AS baz
+FROM t1
+GROUP BY foo, col_int_key, baz ORDER BY pk, bar, (SELECT a FROM t2 WHERE a=t1.pk);
+
+# Printing the alias in GROUP/ORDER BY would introduce an ambiguity.
+EXPLAIN EXTENDED SELECT
+pk AS foo, col_int_key AS foo, (SELECT a FROM t2 WHERE a=t1.pk) AS foo
+FROM t1
+GROUP BY pk, col_int_key, (SELECT a FROM t2 WHERE a=t1.pk)
+ORDER BY pk, col_int_key, (SELECT a FROM t2 WHERE a=t1.pk);
+
+DROP TABLE t1,t2;

=== modified file 'sql/filesort.cc'
--- a/sql/filesort.cc	2011-11-21 11:58:25 +0000
+++ b/sql/filesort.cc	2012-01-12 14:53:51 +0000
@@ -154,7 +154,8 @@ static void trace_filesort_information(O
   @param      select         Condition to apply to the rows
   @param      max_rows       Return only this many rows
   @param      sort_positions Set to TRUE if we want to force sorting by position
-                             (Needed by UPDATE/INSERT or ALTER TABLE)
+                             (Needed by UPDATE/INSERT or ALTER TABLE or
+                              when rowids are required by executor)
   @param[out] examined_rows  Store number of examined rows here
   @param[out] found_rows     Store the number of found rows here.
 

=== modified file 'sql/item.cc'
--- a/sql/item.cc	2012-01-05 10:14:20 +0000
+++ b/sql/item.cc	2012-01-10 18:58:10 +0000
@@ -670,6 +670,32 @@ void Item::print_item_w_name(String *str
 }
 
 
+/**
+   @details
+   "SELECT (subq) GROUP BY (same_subq)" confuses ONLY_FULL_GROUP_BY (it does
+   not see that both subqueries are the same, raises an error).
+   To avoid hitting this problem, if the original query was:
+   "SELECT expression AS x GROUP BY x", we print "GROUP BY x", not
+   "GROUP BY expression". Same for ORDER BY.
+   This has practical importance for views created as
+   "CREATE VIEW v SELECT (subq) AS x GROUP BY x"
+   (print_order() is used to write the view's definition in the frm file).
+*/
+void Item::print_for_order(String *str,
+                           enum_query_type query_type,
+                           bool used_alias)
+{
+  if (used_alias)
+  {
+    DBUG_ASSERT(name && !is_autogenerated_name);
+    // In the clause, user has referenced expression using an alias; we use it
+    append_identifier(current_thd, str, name, (uint) strlen(name));
+  }
+  else
+    print(str,query_type);
+}
+
+
 void Item::cleanup()
 {
   DBUG_ENTER("Item::cleanup");

=== modified file 'sql/item.h'
--- a/sql/item.h	2011-12-13 18:18:20 +0000
+++ b/sql/item.h	2012-01-10 18:58:10 +0000
@@ -1071,6 +1071,15 @@ public:
   }
 
   void print_item_w_name(String *, enum_query_type query_type);
+  /**
+     Prints the item when it's part of ORDER BY and GROUP BY.
+     @param  str            String to print to
+     @param  query_type     How to format the item
+     @param  used_alias     Whether item was referenced with alias.
+  */
+  void print_for_order(String *str, enum_query_type query_type,
+                       bool used_alias);
+
   virtual void update_used_tables() {}
   virtual void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
                               List<Item> &fields) {}

=== modified file 'sql/sql_executor.cc'
--- a/sql/sql_executor.cc	2012-01-05 14:20:38 +0000
+++ b/sql/sql_executor.cc	2012-01-13 09:33:13 +0000
@@ -1751,6 +1751,7 @@ sub_select_sjm(JOIN *join, JOIN_TAB *joi
     // Save contents of join tab for possible repeated materializations:
     const READ_RECORD saved_access= last_tab->read_record;
     const READ_RECORD::Setup_func saved_rfr= last_tab->read_first_record;
+    st_join_table *const saved_last_inner= last_tab->last_inner;
 
     // Initialize full scan
     if (init_read_record(&last_tab->read_record, join->thd,
@@ -1777,6 +1778,7 @@ sub_select_sjm(JOIN *join, JOIN_TAB *joi
     last_tab->set_condition(save_cond, __LINE__);
     last_tab->read_record= saved_access;
     last_tab->read_first_record= saved_rfr;
+    last_tab->last_inner= saved_last_inner;
   }
   else
   {
@@ -1784,7 +1786,7 @@ sub_select_sjm(JOIN *join, JOIN_TAB *joi
     if ((res= join_read_key2(join_tab, sjm->table, sjm->tab_ref)) == 1)
       DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */
     if (res || !sjm->in_equality->val_int())
-      DBUG_RETURN(NESTED_LOOP_NO_MORE_ROWS);
+      DBUG_RETURN(NESTED_LOOP_OK);
     rc= (*last_tab->next_select)
       (join, join_tab + sjm->table_count, end_of_records);
   }
@@ -1876,6 +1878,8 @@ sub_select_cache(JOIN *join, JOIN_TAB *j
   /*
      TODO: Check whether we really need the call below and we can't do
            without it. If it's not the case remove it.
+     @note This branch is currently dead because setup_join_buffering()
+     disables join buffering if QS_DYNAMIC_RANGE is enabled.
   */
   rc= cache->join_records(TRUE);
   if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
@@ -2637,15 +2641,10 @@ join_read_const_table(JOIN_TAB *tab, POS
       }
     }
   }
-  /* We will evaluate on-expressions here only if it is not considered
-     expensive.  This also prevents executing materialized subqueries
-     in optimization phase.  This is necessary since proper setup for
-     such execution has not been done at this stage.  
-     (See comment in internal_remove_eq_conds() tagged 
-     DontEvaluateMaterializedSubqueryTooEarly).
-  */
-  if (*tab->on_expr_ref && !table->null_row && 
-      !(*tab->on_expr_ref)->is_expensive())
+
+  // We cannot handle outer-joined tables with expensive join conditions here:
+  DBUG_ASSERT(!(*tab->on_expr_ref && (*tab->on_expr_ref)->is_expensive()));
+  if (*tab->on_expr_ref && !table->null_row)
   {
     if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
       mark_as_null_row(table);  
@@ -3917,7 +3916,7 @@ create_sort_index(THD *thd, JOIN *join, 
   if (table->s->tmp_table)
     table->file->info(HA_STATUS_VARIABLE);	// Get record count
   filesort_retval= filesort(thd, table, join->sortorder, length,
-                            select, filesort_limit, 0,
+                            select, filesort_limit, tab->keep_current_rowid,
                             &examined_rows, &found_rows);
   table->sort.found_records= filesort_retval;
   tab->records= found_rows;                     // For SQL_CALC_ROWS

=== modified file 'sql/sql_lex.cc'
--- a/sql/sql_lex.cc	2012-01-05 10:17:03 +0000
+++ b/sql/sql_lex.cc	2012-01-10 18:58:10 +0000
@@ -2253,7 +2253,7 @@ void st_select_lex::print_order(String *
       str->append(buffer, (uint) length);
     }
     else
-      (*order->item)->print(str, query_type);
+      (*order->item)->print_for_order(str, query_type, order->used_alias);
     if (order->direction == ORDER::ORDER_DESC)
       str->append(STRING_WITH_LEN(" desc"));
     if (order->next)

=== modified file 'sql/sql_optimizer.cc'
--- a/sql/sql_optimizer.cc	2012-01-05 10:17:03 +0000
+++ b/sql/sql_optimizer.cc	2012-01-09 14:19:49 +0000
@@ -3012,9 +3012,20 @@ const_table_extraction_done:
 	// All dep. must be constants
         if (s->dependent & ~(join->const_table_map))
 	  continue;
-	if (table->file->stats.records <= 1L &&
-	    (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
-            !tl->in_outer_join_nest())
+        /*
+          Mark a dependent table as constant if
+           1. it has exactly zero or one rows (it is a system table), and
+           2. it is not within a nested outer join, and
+           3. it does not have an expensive join condition.
+              This is because we have to determine whether an outer-joined table
+              has a real row or a null-extended row in the optimizer phase.
+              We have no possibility to evaluate its join condition at
+              execution time, when it is marked as a system table.
+        */
+	if (table->file->stats.records <= 1L &&                            // 1
+            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && // 1
+            !tl->in_outer_join_nest() &&                                   // 2
+            !(*s->on_expr_ref && (*s->on_expr_ref)->is_expensive()))       // 3
 	{					// system table
 	  int tmp= 0;
 	  s->type=JT_SYSTEM;

=== modified file 'sql/sql_parse.cc'
--- a/sql/sql_parse.cc	2012-01-05 14:20:38 +0000
+++ b/sql/sql_parse.cc	2012-01-13 09:33:13 +0000
@@ -6085,7 +6085,7 @@ add_proc_to_list(THD* thd, Item *item)
   item_ptr = (Item**) (order+1);
   *item_ptr= item;
   order->item=item_ptr;
-  order->free_me=0;
+  order->used_alias= false;
   thd->lex->proc_list.link_in_list(order, &order->next);
   return 0;
 }
@@ -6104,7 +6104,7 @@ bool add_to_list(THD *thd, SQL_I_List<OR
   order->item_ptr= item;
   order->item= &order->item_ptr;
   order->direction= (asc ? ORDER::ORDER_ASC : ORDER::ORDER_DESC);
-  order->free_me=0;
+  order->used_alias= false;
   order->used=0;
   order->counter_used= 0;
   list.link_in_list(order, &order->next);

=== modified file 'sql/sql_planner.cc'
--- a/sql/sql_planner.cc	2011-12-16 14:39:02 +0000
+++ b/sql/sql_planner.cc	2012-01-11 13:29:29 +0000
@@ -138,47 +138,6 @@ join_tab_cmp_embedded_first(const void *
 
 
 /*
-  Given a semi-join nest, find out which of the IN-equalities are bound
-
-  SYNOPSIS
-    get_bound_sj_equalities()
-      sj_nest           Semi-join nest
-      remaining_tables  Tables that are not yet bound
-
-  DESCRIPTION
-    Given a semi-join nest, find out which of the IN-equalities have their
-    left part expression bound (i.e. the said expression doesn't refer to
-    any of remaining_tables and can be evaluated).
-
-  RETURN
-    Bitmap of bound IN-equalities.
-*/
-
-static ulonglong get_bound_sj_equalities(TABLE_LIST *sj_nest, 
-                                         table_map remaining_tables)
-{
-  List_iterator<Item> li(sj_nest->nested_join->sj_outer_exprs);
-  Item *item;
-  uint i= 0;
-  ulonglong res= 0;
-  while ((item= li++))
-  {
-    /*
-      Q: should this take into account equality propagation and how?
-      A: If e->outer_side is an Item_field, walk over the equality
-         class and see if there is an element that is bound?
-      (this is an optional feature)
-    */
-    if (!(item->used_tables() & remaining_tables))
-    {
-      res |= 1ULL << i;
-    }
-  }
-  return res;
-}
-
-
-/*
   This is a class for considering possible loose index scan optimizations.
   It's usage pattern is as follows:
     best_access_path()
@@ -265,36 +224,35 @@ public:
             table_map cur_sj_inner_tables, bool is_sjm_nest)
   {
     /*
-      Discover the bound equalities. We need to do this if
+      We may consider the LooseScan strategy if
         1. The next table is an SJ-inner table, and
-        2. It is the first table from that semijoin, and
-        3. We're not within a semi-join range (i.e. all semi-joins either have
+        2, We have no more than 64 IN expressions (must fit in bitmap), and
+        3. It is the first table from that semijoin, and
+        4. We're not within a semi-join range (i.e. all semi-joins either have
            all or none of their tables in join_table_map), except
-           s->emb_sj_nest (which we've just entered, see #2).
-        4. All non-IN-equality correlation references from this sj-nest are 
-           bound
-        5. But some of the IN-equalities aren't (so this can't be handled by 
-           FirstMatch strategy)
-        6. Not a derived table/view. (a temporary restriction)
+           s->emb_sj_nest (which we've just entered, see #2), and
+        5. All non-IN-equality correlation references from this sj-nest are 
+           bound, and
+        6. But some of the IN-equalities aren't (so this can't be handled by 
+           FirstMatch strategy), and
+        7. LooseScan is not disabled, and
+        8. Not a derived table/view. (a temporary restriction)
     */
     best_loose_scan_cost= DBL_MAX;
     if (s->emb_sj_nest && !is_sjm_nest &&                               // (1)
-        s->emb_sj_nest->nested_join->sj_inner_exprs.elements < 64 && 
-        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==        // (2)
-         s->emb_sj_nest->sj_inner_tables) &&                            // (2)
-        cur_sj_inner_tables == 0 &&                                     // (3)
+        s->emb_sj_nest->nested_join->sj_inner_exprs.elements <= 64 &&   // (2)
+        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==        // (3)
+         s->emb_sj_nest->sj_inner_tables) &&                            // (3)
+        cur_sj_inner_tables == 0 &&                                     // (4)
         !(remaining_tables & 
-          s->emb_sj_nest->nested_join->sj_corr_tables) &&               // (4)
-        (remaining_tables & s->emb_sj_nest->nested_join->sj_depends_on) &&// (5)
-        s->join->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_LOOSE_SCAN) &&
-        !s->table->pos_in_table_list->uses_materialization())
-    {
-      /* This table is an LooseScan scan candidate */
-      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
-                                                   remaining_tables);
-      try_loosescan= TRUE;
-      DBUG_PRINT("info", ("Will try LooseScan scan, bound_map=%llx",
-                          (longlong)bound_sj_equalities));
+          s->emb_sj_nest->nested_join->sj_corr_tables) &&               // (5)
+        (remaining_tables & s->emb_sj_nest->nested_join->sj_depends_on) && //(6)
+        s->join->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_LOOSE_SCAN) &&//(7)
+        !s->table->pos_in_table_list->uses_materialization())           // (8)
+    {
+      try_loosescan= true;      // This table is a LooseScan scan candidate
+      bound_sj_equalities= 0;   // These equalities are populated later
+      DBUG_PRINT("info", ("Will try LooseScan scan"));
     }
   }
 

=== modified file 'sql/sql_resolver.cc'
--- a/sql/sql_resolver.cc	2011-12-15 09:00:42 +0000
+++ b/sql/sql_resolver.cc	2012-01-10 18:58:10 +0000
@@ -1036,6 +1036,8 @@ find_order_in_list(THD *thd, Ref_ptr_arr
       */
       order->item= &ref_pointer_array[counter];
       order->in_field_list=1;
+      if (resolution == RESOLVED_AGAINST_ALIAS)
+        order->used_alias= true;
       return FALSE;
     }
     else

=== modified file 'sql/sql_select.cc'
--- a/sql/sql_select.cc	2012-01-05 14:20:38 +0000
+++ b/sql/sql_select.cc	2012-01-13 09:33:13 +0000
@@ -328,10 +328,15 @@ static bool might_do_join_buffering(uint
             that the correlation is not just through the IN-equality).
       
       (2) - Inner table for which the LooseScan scan is performed.
+            Notice that special requirements for existence of certain indexes
+            apply to this table, @see class Loose_scan_opt.
 
       (3) - The remainder of the duplicate-generating range. It is served by 
-            application of FirstMatch strategy, with the exception that
-            outer IN-correlated tables are considered to be non-correlated.
+            application of FirstMatch strategy. Outer IN-correlated tables
+            must be correlated to the LooseScan table but not to the inner
+            tables in this range. (Currently, there can be no outer tables
+            in this range because of implementation restrictions,
+            @see Optimize_table_order::advance_sj_state()).
 
       (4) - The suffix of outer correlated and non-correlated tables.
 

=== modified file 'sql/sql_select.h'
--- a/sql/sql_select.h	2011-12-15 15:15:37 +0000
+++ b/sql/sql_select.h	2012-01-11 13:29:29 +0000
@@ -101,7 +101,7 @@ public:
   */
   bool *cond_guard;
   /**
-     0..64    <=> This was created from semi-join IN-equality # sj_pred_no.
+     0..63    <=> This was created from semi-join IN-equality # sj_pred_no.
      UINT_MAX  Otherwise
 
      Not used if the index is fulltext (such index cannot be used for

=== modified file 'sql/sql_tmp_table.cc'
--- a/sql/sql_tmp_table.cc	2012-01-04 09:51:39 +0000
+++ b/sql/sql_tmp_table.cc	2012-01-12 10:21:25 +0000
@@ -1194,12 +1194,11 @@ TABLE *create_duplicate_weedout_tmp_tabl
   uchar *bitmaps;
   uint *blob_field;
   MI_COLUMNDEF *recinfo, *start_recinfo;
-  bool using_unique_constraint=FALSE;
-  bool use_packed_rows= FALSE;
+  bool using_unique_constraint=false;
   Field *field, *key_field;
   uint null_pack_length, null_count;
   uchar *null_flags;
-  uchar *pos;
+
   DBUG_ENTER("create_duplicate_weedout_tmp_table");
   DBUG_ASSERT(!sjtbl->is_confluent);
   /*
@@ -1222,7 +1221,7 @@ TABLE *create_duplicate_weedout_tmp_tabl
 
   /* STEP 2: Figure if we'll be using a key or blob+constraint */
   if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
-    using_unique_constraint= TRUE;
+    using_unique_constraint= true;
 
   /* STEP 3: Allocate memory for temptable description */
   init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
@@ -1341,12 +1340,12 @@ TABLE *create_duplicate_weedout_tmp_tabl
 
   recinfo= start_recinfo;
   null_flags=(uchar*) table->record[0];
-  pos=table->record[0]+ null_pack_length;
-  if (null_pack_length)
+
   {
+    /* Table description for the NULL bits */
     memset(recinfo, 0, sizeof(*recinfo));
-    recinfo->type=FIELD_NORMAL;
-    recinfo->length=null_pack_length;
+    recinfo->type= FIELD_NORMAL;
+    recinfo->length= null_pack_length;
     recinfo++;
     memset(null_flags, 255, null_pack_length);	// Set null fields
 
@@ -1357,39 +1356,21 @@ TABLE *create_duplicate_weedout_tmp_tabl
   null_count=1;
 
   {
-    //Field *field= *reg_field;
-    uint length;
+    /* Table description for the concatenated rowid column */
     memset(recinfo, 0, sizeof(*recinfo));
-    field->move_field(pos,(uchar*) 0,0);
-
-    field->reset();
-    /*
-      Test if there is a default field value. The test for ->ptr is to skip
-      'offset' fields generated by initalize_tables
+    /* 
+       Don't care about packing the VARCHAR since it's only a
+       concatenation of rowids. @see create_tmp_table() for how
+       packed VARCHARs can be achieved
     */
-    // Initialize the table field:
-    memset(field->ptr, 0, field->pack_length());
-
-    length=field->pack_length();
-    pos+= length;
-
-    /* Make entry for create table */
-    recinfo->length=length;
-    if (field->flags & BLOB_FLAG)
-      recinfo->type= (int) FIELD_BLOB;
-    else if (use_packed_rows &&
-             field->real_type() == MYSQL_TYPE_STRING &&
-	     length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
-      recinfo->type=FIELD_SKIP_ENDSPACE;
-    else
-      recinfo->type=FIELD_NORMAL;
+    recinfo->type= FIELD_NORMAL;
+    recinfo->length= field->pack_length();
 
+    field->move_field(table->record[0] + null_pack_length, 0, 0);
+    field->reset();
     field->table_name= &table->alias;
   }
 
-  //param->recinfo=recinfo;
-  //store_record(table,s->default_values);        // Make empty default record
-
   if (thd->variables.tmp_table_size == ~ (ulonglong) 0)		// No limit
     share->max_rows= ~(ha_rows) 0;
   else
@@ -1401,7 +1382,6 @@ TABLE *create_duplicate_weedout_tmp_tabl
   set_if_bigger(share->max_rows,1);		// For dummy start options
 
 
-  //// keyinfo= param->keyinfo;
   if (TRUE)
   {
     DBUG_PRINT("info",("Creating group key in temporary table"));

=== modified file 'sql/table.h'
--- a/sql/table.h	2012-01-05 14:20:38 +0000
+++ b/sql/table.h	2012-01-13 09:33:13 +0000
@@ -188,7 +188,7 @@ private:
 
 /*************************************************************************/
 
-/* Order clause list element */
+/** Order clause list element */
 
 typedef struct st_order {
   struct st_order *next;
@@ -203,9 +203,15 @@ typedef struct st_order {
   };
 
   enum_order direction;                 /* Requested direction of ordering */
-  bool   free_me;                       /* true if item isn't shared  */
   bool   in_field_list;                 /* true if in select field list */
   bool   counter_used;                  /* parameter was counter of columns */
+  /**
+     Tells whether this ORDER element was referenced with an alias or with an
+     expression, in the query:
+     SELECT a AS foo GROUP BY foo: true.
+     SELECT a AS foo GROUP BY a: false.
+  */
+  bool   used_alias;
   Field  *field;                        /* If tmp-table group */
   char   *buff;                         /* If tmp-table group */
   table_map used, depend_map;

No bundle (reason: useless for push emails).
Thread
bzr push into mysql-trunk branch (tor.didriksen:3451 to 3454) Tor Didriksen13 Jan