List:Commits« Previous MessageNext Message »
From:Roy Lyseng Date:May 30 2012 2:27pm
Subject:bzr push into mysql-trunk branch (roy.lyseng:3917 to 3918) Bug#14064201
View as plain text  
 3918 Roy Lyseng	2012-05-30
      Bug#14064201: Missing data on join of derived table + WHERE .. IN with two ops
      
      This is another fix for propagation of items in materialized semi-joins
      combined with outer-joined tables. Here we have a MaterializeScan plan
      with a multiple equality added to the outer join condition. The multiple
      equality refers two columns from the materialized table, say "a" and "b",
      but as they are equal, the code in setup_sj_materialization() sets up
      copy-back code for column "a" and "a" (as column "b" is found to be
      equal to "a"). I am unable to find a simple solution for how to eliminate
      the generated equality on "b" in the join condition, thus the simplest
      solution seems to be to add the column "b" when copying back.
      
      mysql-test/include/subquery_sj.inc
        Added test case for bug#14064201.
      
      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
        Added test case results for bug#14064201.
      
      sql/sql_class.h
        Added number of copied fields to class Semijoin_mat_exec.
      
      sql/sql_executor.cc
        In sub_select_sjm(), take number of fields to be copied from
        copy_field_count.
      
      sql/sql_select.cc
        In setup_sj_materialization(), make sure that both original and
        substituted are copied back into field buffers when reading the
        materialized table.

    modified:
      mysql-test/include/subquery_sj.inc
      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
      sql/sql_class.h
      sql/sql_executor.cc
      sql/sql_select.cc
 3917 Roy Lyseng	2012-05-29
      Bug#14064201: Missing data on join of derived table + WHERE .. IN with two ops
      
      The fix for bug#13414014 and the first fix for bug#14064201 improved
      the situation for these kinds of queries, but there were still problems.
      
      The second test case for bug#14064201 generates an equality for an outer
      join condition for the two columns from the materialized semi-joined
      tables, but only the first of these are available when processing
      the condition.
      
      We have this multiple equality:
       =(gp1.col_int_nokey, t2.col_int_nokey, t2.col_int_key, t1.col_int_nokey)
      
      gp1 is the first table of the join plan, t2 is the second and t1 is the
      third. t2 is also the materialized semi-join table.
      
      When trying to generate an equality for t2.col_int_key, which is
      found to match t2.col_int_nokey, only the first column of the multiple
      equality coming from the inner table (t2) is available during processing
      of the outer join condition. Thus, we have to eliminate all columns
      following the first. We do that by adding the check
      
        item_equal->get_subst_item(item_field) == item_field
      
      when testing for fields to be included in eliminate_item_equal().
       
      mysql-test/include/subquery_sj.inc
        Added test case for bug#14064201.
      
      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
        Added test case results for bug#14064201.
      
      sql/sql_optimizer.cc
        In eliminate_item_equal(), an if test was enhanced with a check
        for the column in question being the first coming from the
        materialized semi-join tables.

    modified:
      mysql-test/include/subquery_sj.inc
      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
      sql/sql_optimizer.cc
=== modified file 'mysql-test/include/subquery_sj.inc'
--- a/mysql-test/include/subquery_sj.inc	2012-05-29 13:24:49 +0000
+++ b/mysql-test/include/subquery_sj.inc	2012-05-30 14:26:55 +0000
@@ -6263,6 +6263,87 @@ eval $query;
 
 DROP TABLE t1, t2;
 
+CREATE TABLE t1 (  
+  pk int,  
+  col_int_key int,  
+  col_datetime_key datetime,  
+  col_varchar_key varchar(1),  
+  col_varchar_nokey varchar(1),  
+  PRIMARY KEY (pk),  
+  KEY col_int_key (col_int_key),  
+  KEY col_datetime_key (col_datetime_key),  
+  KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+ 
+INSERT INTO t1 VALUES
+ (10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+ (12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+ (14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+ (16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+ (18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+ (20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+ (22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+ (24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+ (26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+ (28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+ 
+CREATE TABLE t2 (
+  pk int,
+  col_int_key int,
+  col_datetime_key datetime,
+  col_varchar_key varchar(1),
+  col_varchar_nokey varchar(1),
+  PRIMARY KEY (pk),
+  KEY col_int_key (col_int_key),
+  KEY col_datetime_key (col_datetime_key),
+  KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+ 
+INSERT INTO t2 VALUES
+ (1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+ (3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+ (5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+ (7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+ (9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+ (11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+ (13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+ (15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+ (17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+ (19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+ 
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+     RIGHT JOIN t2 AS alias2
+       JOIN t2 AS alias3
+       ON alias3.pk = alias2.pk
+     ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+        alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+       (SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+        FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+        WHERE sq2_alias2.col_int_key < 2);
+ 
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+
+let $query=
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+     RIGHT JOIN t2 AS alias2
+       JOIN t2 AS alias3
+       ON alias3.pk = alias2.pk
+     ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+        alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+       (SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+        FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+        WHERE sq2_alias2.col_int_key < 2);
+
+eval explain format=json $query;
+eval $query;
+
+DROP TABLE t1, t2;
+
 --echo # End of test for bug#14064201.
 
 --echo # End of 5.6 tests

=== modified file 'mysql-test/r/subquery_sj_all.result'
--- a/mysql-test/r/subquery_sj_all.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_all.result	2012-05-30 14:26:55 +0000
@@ -10030,6 +10030,247 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "using_temporary_table": true,
+          "access_type": "ALL",
+          "materialized_from_subquery": {
+            "query_block": {
+              "nested_loop": [
+                {
+                  "table": {
+                    "table_name": "sq2_alias1",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100
+                  }
+                },
+                {
+                  "table": {
+                    "table_name": "sq2_alias2",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100,
+                    "using_join_buffer": "Block Nested Loop",
+                    "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+                  }
+                }
+              ]
+            }
+          }
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_all_bka.result	2012-05-30 14:26:55 +0000
@@ -10035,6 +10035,248 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "using_temporary_table": true,
+          "access_type": "ALL",
+          "materialized_from_subquery": {
+            "query_block": {
+              "nested_loop": [
+                {
+                  "table": {
+                    "table_name": "sq2_alias1",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100
+                  }
+                },
+                {
+                  "table": {
+                    "table_name": "sq2_alias2",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100,
+                    "using_join_buffer": "Block Nested Loop",
+                    "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+                  }
+                }
+              ]
+            }
+          }
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100,
+          "using_join_buffer": "Batched Key Access"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_all_bka_nixbnl.result	2012-05-30 14:26:55 +0000
@@ -10033,6 +10033,245 @@ col_int_nokey	col_int_key
 8	1
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "using_temporary_table": true,
+          "access_type": "ALL",
+          "materialized_from_subquery": {
+            "query_block": {
+              "nested_loop": [
+                {
+                  "table": {
+                    "table_name": "sq2_alias1",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100
+                  }
+                },
+                {
+                  "table": {
+                    "table_name": "sq2_alias2",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100,
+                    "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+                  }
+                }
+              ]
+            }
+          }
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100,
+          "using_join_buffer": "Batched Key Access"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_all_bkaunique.result'
--- a/mysql-test/r/subquery_sj_all_bkaunique.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_all_bkaunique.result	2012-05-30 14:26:55 +0000
@@ -10036,6 +10036,248 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "using_temporary_table": true,
+          "access_type": "ALL",
+          "materialized_from_subquery": {
+            "query_block": {
+              "nested_loop": [
+                {
+                  "table": {
+                    "table_name": "sq2_alias1",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100
+                  }
+                },
+                {
+                  "table": {
+                    "table_name": "sq2_alias2",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100,
+                    "using_join_buffer": "Block Nested Loop",
+                    "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+                  }
+                }
+              ]
+            }
+          }
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100,
+          "using_join_buffer": "Batched Key Access (unique)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed.result'
--- a/mysql-test/r/subquery_sj_dupsweed.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed.result	2012-05-30 14:26:55 +0000
@@ -10010,6 +10010,242 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "duplicates_removal": {
+          "using_temporary_table": true,
+          "nested_loop": [
+            {
+              "table": {
+                "table_name": "sq2_alias1",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100
+              }
+            },
+            {
+              "table": {
+                "table_name": "sq2_alias2",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+              }
+            },
+            {
+              "table": {
+                "table_name": "alias2",
+                "access_type": "ALL",
+                "possible_keys": [
+                  "PRIMARY"
+                ],
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_bka.result	2012-05-30 14:26:55 +0000
@@ -10015,6 +10015,242 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "duplicates_removal": {
+          "using_temporary_table": true,
+          "nested_loop": [
+            {
+              "table": {
+                "table_name": "sq2_alias1",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100
+              }
+            },
+            {
+              "table": {
+                "table_name": "sq2_alias2",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+              }
+            },
+            {
+              "table": {
+                "table_name": "alias2",
+                "access_type": "ALL",
+                "possible_keys": [
+                  "PRIMARY"
+                ],
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_bka_nixbnl.result	2012-05-30 14:26:55 +0000
@@ -10015,6 +10015,239 @@ col_int_nokey	col_int_key
 8	1
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "duplicates_removal": {
+          "using_temporary_table": true,
+          "nested_loop": [
+            {
+              "table": {
+                "table_name": "sq2_alias1",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100,
+                "attached_condition": "(`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)"
+              }
+            },
+            {
+              "table": {
+                "table_name": "sq2_alias2",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100,
+                "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "((<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true)) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed_bkaunique.result'
--- a/mysql-test/r/subquery_sj_dupsweed_bkaunique.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_bkaunique.result	2012-05-30 14:26:55 +0000
@@ -10016,6 +10016,242 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "duplicates_removal": {
+          "using_temporary_table": true,
+          "nested_loop": [
+            {
+              "table": {
+                "table_name": "sq2_alias1",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100
+              }
+            },
+            {
+              "table": {
+                "table_name": "sq2_alias2",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+              }
+            },
+            {
+              "table": {
+                "table_name": "alias2",
+                "access_type": "ALL",
+                "possible_keys": [
+                  "PRIMARY"
+                ],
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_firstmatch.result'
--- a/mysql-test/r/subquery_sj_firstmatch.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch.result	2012-05-30 14:26:55 +0000
@@ -10001,6 +10001,234 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "sq2_alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "(`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "sq2_alias2",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "first_match": "alias3",
+          "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "((<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true)) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 #

=== modified file 'mysql-test/r/subquery_sj_firstmatch_bka.result'
--- a/mysql-test/r/subquery_sj_firstmatch_bka.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_bka.result	2012-05-30 14:26:55 +0000
@@ -10002,6 +10002,234 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "sq2_alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "(`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "sq2_alias2",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "first_match": "alias3",
+          "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "((<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true)) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 #

=== modified file 'mysql-test/r/subquery_sj_firstmatch_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_firstmatch_bka_nixbnl.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_bka_nixbnl.result	2012-05-30 14:26:55 +0000
@@ -10006,6 +10006,233 @@ col_int_nokey	col_int_key
 8	1
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "sq2_alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "(`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "sq2_alias2",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "first_match": "alias3",
+          "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "((<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true)) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 #

=== modified file 'mysql-test/r/subquery_sj_firstmatch_bkaunique.result'
--- a/mysql-test/r/subquery_sj_firstmatch_bkaunique.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_bkaunique.result	2012-05-30 14:26:55 +0000
@@ -10003,6 +10003,234 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "sq2_alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "(`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "sq2_alias2",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "first_match": "alias3",
+          "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "((<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true)) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 #

=== modified file 'mysql-test/r/subquery_sj_loosescan.result'
--- a/mysql-test/r/subquery_sj_loosescan.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_loosescan.result	2012-05-30 14:26:55 +0000
@@ -10011,6 +10011,242 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "duplicates_removal": {
+          "using_temporary_table": true,
+          "nested_loop": [
+            {
+              "table": {
+                "table_name": "sq2_alias1",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100
+              }
+            },
+            {
+              "table": {
+                "table_name": "sq2_alias2",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+              }
+            },
+            {
+              "table": {
+                "table_name": "alias2",
+                "access_type": "ALL",
+                "possible_keys": [
+                  "PRIMARY"
+                ],
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_bka.result	2012-05-30 14:26:55 +0000
@@ -10016,6 +10016,242 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "duplicates_removal": {
+          "using_temporary_table": true,
+          "nested_loop": [
+            {
+              "table": {
+                "table_name": "sq2_alias1",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100
+              }
+            },
+            {
+              "table": {
+                "table_name": "sq2_alias2",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+              }
+            },
+            {
+              "table": {
+                "table_name": "alias2",
+                "access_type": "ALL",
+                "possible_keys": [
+                  "PRIMARY"
+                ],
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_bka_nixbnl.result	2012-05-30 14:26:55 +0000
@@ -10016,6 +10016,239 @@ col_int_nokey	col_int_key
 8	1
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "duplicates_removal": {
+          "using_temporary_table": true,
+          "nested_loop": [
+            {
+              "table": {
+                "table_name": "sq2_alias1",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100,
+                "attached_condition": "(`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)"
+              }
+            },
+            {
+              "table": {
+                "table_name": "sq2_alias2",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100,
+                "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "((<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true)) and <if>(is_not_null_compl(alias1), (((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias1`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`alias2`.`col_varchar_key`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_loosescan_bkaunique.result'
--- a/mysql-test/r/subquery_sj_loosescan_bkaunique.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_bkaunique.result	2012-05-30 14:26:55 +0000
@@ -10017,6 +10017,242 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "duplicates_removal": {
+          "using_temporary_table": true,
+          "nested_loop": [
+            {
+              "table": {
+                "table_name": "sq2_alias1",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100
+              }
+            },
+            {
+              "table": {
+                "table_name": "sq2_alias2",
+                "access_type": "ALL",
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+              }
+            },
+            {
+              "table": {
+                "table_name": "alias2",
+                "access_type": "ALL",
+                "possible_keys": [
+                  "PRIMARY"
+                ],
+                "rows": 20,
+                "filtered": 100,
+                "using_join_buffer": "Block Nested Loop",
+                "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat.result'
--- a/mysql-test/r/subquery_sj_mat.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_mat.result	2012-05-30 14:26:55 +0000
@@ -10028,6 +10028,247 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "using_temporary_table": true,
+          "access_type": "ALL",
+          "materialized_from_subquery": {
+            "query_block": {
+              "nested_loop": [
+                {
+                  "table": {
+                    "table_name": "sq2_alias1",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100
+                  }
+                },
+                {
+                  "table": {
+                    "table_name": "sq2_alias2",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100,
+                    "using_join_buffer": "Block Nested Loop",
+                    "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+                  }
+                }
+              ]
+            }
+          }
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_mat_bka.result	2012-05-30 14:26:55 +0000
@@ -10029,6 +10029,247 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "using_temporary_table": true,
+          "access_type": "ALL",
+          "materialized_from_subquery": {
+            "query_block": {
+              "nested_loop": [
+                {
+                  "table": {
+                    "table_name": "sq2_alias1",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100
+                  }
+                },
+                {
+                  "table": {
+                    "table_name": "sq2_alias2",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100,
+                    "using_join_buffer": "Block Nested Loop",
+                    "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+                  }
+                }
+              ]
+            }
+          }
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_mat_bka_nixbnl.result	2012-05-30 14:26:55 +0000
@@ -10027,6 +10027,244 @@ col_int_nokey	col_int_key
 8	1
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "using_temporary_table": true,
+          "access_type": "ALL",
+          "materialized_from_subquery": {
+            "query_block": {
+              "nested_loop": [
+                {
+                  "table": {
+                    "table_name": "sq2_alias1",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100
+                  }
+                },
+                {
+                  "table": {
+                    "table_name": "sq2_alias2",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100,
+                    "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+                  }
+                }
+              ]
+            }
+          }
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat_bkaunique.result'
--- a/mysql-test/r/subquery_sj_mat_bkaunique.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_mat_bkaunique.result	2012-05-30 14:26:55 +0000
@@ -10030,6 +10030,247 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "using_temporary_table": true,
+          "access_type": "ALL",
+          "materialized_from_subquery": {
+            "query_block": {
+              "nested_loop": [
+                {
+                  "table": {
+                    "table_name": "sq2_alias1",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100
+                  }
+                },
+                {
+                  "table": {
+                    "table_name": "sq2_alias2",
+                    "access_type": "ALL",
+                    "rows": 20,
+                    "filtered": 100,
+                    "using_join_buffer": "Block Nested Loop",
+                    "attached_condition": "((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))"
+                  }
+                }
+              ]
+            }
+          }
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "(<if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true) and <if>(is_not_null_compl(alias1), (((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`), true))"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` semi join (`test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2`) join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on((((`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias3`.`col_varchar_nokey` = `test`.`sq2_alias1`.`col_varchar_nokey`)) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and (`test`.`sq2_alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`alias2`.`col_varchar_key` = `test`.`sq2_alias1`.`col_varchar_nokey`) and (`test`.`sq2_alias2`.`col_int_key` < 2))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat_nosj.result'
--- a/mysql-test/r/subquery_sj_mat_nosj.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_mat_nosj.result	2012-05-30 14:26:55 +0000
@@ -10098,6 +10098,251 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "<in_optimizer>((`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`),(`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`) in ( <materialize> (select `test`.`sq2_alias2`.`col_varchar_key`,`test`.`sq2_alias1`.`col_varchar_nokey` from `test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2` where (`test`.`sq2_alias2`.`col_int_key` < 2) ), <primary_index_lookup>(`test`.`alias2`.`col_varchar_key` in <temporary table> on distinct_key where ((`test`.`alias2`.`col_varchar_key` = `materialized subselect`.`col_varchar_key`) and (`test`.`alias2`.`col_varchar_key` = `materialized subselect`.`col_varchar_nokey`)))))",
+          "attached_subqueries": [
+            {
+              "using_temporary_table": true,
+              "dependent": false,
+              "cacheable": true,
+              "table": {
+                "access_type": "eq_ref",
+                "key": "<auto_key>",
+                "rows": 1
+              },
+              "query_block": {
+                "select_id": 2,
+                "nested_loop": [
+                  {
+                    "table": {
+                      "table_name": "sq2_alias1",
+                      "access_type": "ALL",
+                      "rows": 20,
+                      "filtered": 100
+                    }
+                  },
+                  {
+                    "table": {
+                      "table_name": "sq2_alias2",
+                      "access_type": "ALL",
+                      "rows": 20,
+                      "filtered": 100,
+                      "using_join_buffer": "Block Nested Loop",
+                      "attached_condition": "(`test`.`sq2_alias2`.`col_int_key` < 2)"
+                    }
+                  }
+                ]
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true)"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on(((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and <in_optimizer>((`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`),(`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`) in ( <materialize> (/* select#2 */ select `test`.`sq2_alias2`.`col_varchar_key`,`test`.`sq2_alias1`.`col_varchar_nokey` from `test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2` where (`test`.`sq2_alias2`.`col_int_key` < 2) ), <primary_index_lookup>(`test`.`alias2`.`col_varchar_key` in <temporary table> on distinct_key where ((`test`.`alias2`.`col_varchar_key` = `materialized subselect`.`col_varchar_key`) and (`test`.`alias2`.`col_varchar_key` = `materialized subselect`.`col_var!
 char_nokey`))))))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_none.result	2012-05-30 14:26:55 +0000
@@ -10014,6 +10014,246 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "<in_optimizer>((`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`),<exists>(select `test`.`sq2_alias2`.`col_varchar_key`,`test`.`sq2_alias1`.`col_varchar_nokey` from `test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2` where ((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`))))",
+          "attached_subqueries": [
+            {
+              "dependent": true,
+              "cacheable": false,
+              "query_block": {
+                "select_id": 2,
+                "nested_loop": [
+                  {
+                    "table": {
+                      "table_name": "sq2_alias1",
+                      "access_type": "ALL",
+                      "rows": 20,
+                      "filtered": 100,
+                      "attached_condition": "(<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+                    }
+                  },
+                  {
+                    "table": {
+                      "table_name": "sq2_alias2",
+                      "access_type": "ALL",
+                      "rows": 20,
+                      "filtered": 100,
+                      "using_join_buffer": "Block Nested Loop",
+                      "attached_condition": "((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`))"
+                    }
+                  }
+                ]
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true)"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on(((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and <in_optimizer>((`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`),<exists>(/* select#2 */ select `test`.`sq2_alias2`.`col_varchar_key`,`test`.`sq2_alias1`.`col_varchar_nokey` from `test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2` where ((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`)))))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_none_bka.result	2012-05-30 14:26:55 +0000
@@ -10015,6 +10015,246 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "<in_optimizer>((`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`),<exists>(select `test`.`sq2_alias2`.`col_varchar_key`,`test`.`sq2_alias1`.`col_varchar_nokey` from `test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2` where ((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`))))",
+          "attached_subqueries": [
+            {
+              "dependent": true,
+              "cacheable": false,
+              "query_block": {
+                "select_id": 2,
+                "nested_loop": [
+                  {
+                    "table": {
+                      "table_name": "sq2_alias1",
+                      "access_type": "ALL",
+                      "rows": 20,
+                      "filtered": 100,
+                      "attached_condition": "(<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+                    }
+                  },
+                  {
+                    "table": {
+                      "table_name": "sq2_alias2",
+                      "access_type": "ALL",
+                      "rows": 20,
+                      "filtered": 100,
+                      "using_join_buffer": "Block Nested Loop",
+                      "attached_condition": "((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`))"
+                    }
+                  }
+                ]
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true)"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on(((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and <in_optimizer>((`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`),<exists>(/* select#2 */ select `test`.`sq2_alias2`.`col_varchar_key`,`test`.`sq2_alias1`.`col_varchar_nokey` from `test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2` where ((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`)))))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 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	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_none_bka_nixbnl.result	2012-05-30 14:26:55 +0000
@@ -10012,6 +10012,244 @@ col_int_nokey	col_int_key
 8	1
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "<in_optimizer>((`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`),<exists>(select `test`.`sq2_alias2`.`col_varchar_key`,`test`.`sq2_alias1`.`col_varchar_nokey` from `test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2` where ((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`))))",
+          "attached_subqueries": [
+            {
+              "dependent": true,
+              "cacheable": false,
+              "query_block": {
+                "select_id": 2,
+                "nested_loop": [
+                  {
+                    "table": {
+                      "table_name": "sq2_alias1",
+                      "access_type": "ALL",
+                      "rows": 20,
+                      "filtered": 100,
+                      "attached_condition": "(<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+                    }
+                  },
+                  {
+                    "table": {
+                      "table_name": "sq2_alias2",
+                      "access_type": "ALL",
+                      "rows": 20,
+                      "filtered": 100,
+                      "attached_condition": "((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`))"
+                    }
+                  }
+                ]
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true)"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on(((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and <in_optimizer>((`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`),<exists>(/* select#2 */ select `test`.`sq2_alias2`.`col_varchar_key`,`test`.`sq2_alias1`.`col_varchar_nokey` from `test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2` where ((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`)))))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_none_bkaunique.result'
--- a/mysql-test/r/subquery_sj_none_bkaunique.result	2012-05-29 13:24:49 +0000
+++ b/mysql-test/r/subquery_sj_none_bkaunique.result	2012-05-30 14:26:55 +0000
@@ -10016,6 +10016,246 @@ col_int_nokey	col_int_key
 8	5
 8	1
 DROP TABLE t1, t2;
+CREATE TABLE t1 (  
+pk int,  
+col_int_key int,  
+col_datetime_key datetime,  
+col_varchar_key varchar(1),  
+col_varchar_nokey varchar(1),  
+PRIMARY KEY (pk),  
+KEY col_int_key (col_int_key),  
+KEY col_datetime_key (col_datetime_key),  
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t1 VALUES
+(10,7,'2004-06-06 04:22:12','v','v'), (11,0,'2005-11-13 01:12:31','s','s'),
+(12,9,'2002-05-04 01:50:00','l','l'), (13,3,'2004-10-27 10:28:45','y','y'),
+(14,4,'2006-07-22 05:24:23','c','c'), (15,2,'2002-05-16 21:34:03','i','i'),
+(16,5,'2008-04-17 10:45:30','h','h'), (17,3,'2009-04-21 02:58:02','q','q'),
+(18,1,'2008-01-11 11:01:51','a','a'), (19,3,'1900-01-01 00:00:00','v','v'),
+(20,6,'2007-05-17 18:24:57','u','u'), (21,7,'2007-08-07 00:00:00','s','s'),
+(22,5,'2001-08-28 00:00:00','y','y'), (23,1,'2004-04-16 00:27:28','z','z'),
+(24,204,'2005-05-03 07:06:22','h','h'), (25,224,'2009-03-11 17:09:50','p','p'),
+(26,9,'2007-12-08 01:54:28','e','e'), (27,5,'2009-07-28 18:19:54','i','i'),
+(28,0,'2008-06-08 00:00:00','y','y'), (29,3,'2005-02-09 09:20:26','w','w');
+CREATE TABLE t2 (
+pk int,
+col_int_key int,
+col_datetime_key datetime,
+col_varchar_key varchar(1),
+col_varchar_nokey varchar(1),
+PRIMARY KEY (pk),
+KEY col_int_key (col_int_key),
+KEY col_datetime_key (col_datetime_key),
+KEY col_varchar_key (col_varchar_key,col_int_key)
+) engine=MyISAM;
+INSERT INTO t2 VALUES
+(1,0,'2002-02-13 17:30:06','j','j'), (2,8,'2008-09-27 00:34:58','v','v'),
+(3,1,'2007-05-28 00:00:00','c','c'), (4,8,'2009-07-25 09:21:20','m','m'),
+(5,9,'2002-01-16 00:00:00','d','d'), (6,24,'2006-10-12 04:32:53','d','d'),
+(7,6,'2001-02-15 03:08:38','y','y'), (8,1,'2004-10-02 20:31:15','t','t'),
+(9,6,'2002-08-20 22:48:00','d','d'), (10,2,'1900-01-01 00:00:00','s','s'),
+(11,4,'2005-08-15 00:00:00','r','r'), (12,8,'1900-01-01 00:00:00','m','m'),
+(13,4,'2008-05-16 08:09:06','b','b'), (14,4,'2001-01-20 12:47:23','x','x'),
+(15,7,'2008-07-02 00:00:00','g','g'), (16,4,'1900-01-01 00:00:00','p','p'),
+(17,1,'2002-12-08 11:34:58','q','q'), (18,9,'1900-01-01 00:00:00','w','w'),
+(19,4,'1900-01-01 00:00:00','d','d'), (20,8,'2002-08-25 20:35:06','e','e');
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+ALTER TABLE t1 DISABLE KEYS;
+ALTER TABLE t2 DISABLE KEYS;
+explain format=json SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+EXPLAIN
+{
+  "query_block": {
+    "select_id": 1,
+    "nested_loop": [
+      {
+        "table": {
+          "table_name": "alias2",
+          "access_type": "ALL",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "rows": 20,
+          "filtered": 100,
+          "attached_condition": "<in_optimizer>((`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`),<exists>(select `test`.`sq2_alias2`.`col_varchar_key`,`test`.`sq2_alias1`.`col_varchar_nokey` from `test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2` where ((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`))))",
+          "attached_subqueries": [
+            {
+              "dependent": true,
+              "cacheable": false,
+              "query_block": {
+                "select_id": 2,
+                "nested_loop": [
+                  {
+                    "table": {
+                      "table_name": "sq2_alias1",
+                      "access_type": "ALL",
+                      "rows": 20,
+                      "filtered": 100,
+                      "attached_condition": "(<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`)"
+                    }
+                  },
+                  {
+                    "table": {
+                      "table_name": "sq2_alias2",
+                      "access_type": "ALL",
+                      "rows": 20,
+                      "filtered": 100,
+                      "using_join_buffer": "Block Nested Loop",
+                      "attached_condition": "((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`))"
+                    }
+                  }
+                ]
+              }
+            }
+          ]
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias3",
+          "access_type": "eq_ref",
+          "possible_keys": [
+            "PRIMARY"
+          ],
+          "key": "PRIMARY",
+          "key_length": "4",
+          "ref": [
+            "test.alias2.pk"
+          ],
+          "rows": 1,
+          "filtered": 100
+        }
+      },
+      {
+        "table": {
+          "table_name": "alias1",
+          "access_type": "ALL",
+          "rows": 20,
+          "filtered": 100,
+          "using_join_buffer": "Block Nested Loop",
+          "attached_condition": "<if>(is_not_null_compl(alias1), ((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`), true)"
+        }
+      }
+    ]
+  }
+}
+Warnings:
+Note	1003	/* select#1 */ select `test`.`alias1`.`col_datetime_key` AS `col_datetime_key` from `test`.`t2` `alias2` join `test`.`t2` `alias3` left join `test`.`t2` `alias1` on(((`test`.`alias3`.`col_varchar_nokey` = `test`.`alias2`.`col_varchar_key`) or `test`.`alias2`.`col_varchar_nokey`)) where ((`test`.`alias3`.`pk` = `test`.`alias2`.`pk`) and <in_optimizer>((`test`.`alias2`.`col_varchar_key`,`test`.`alias2`.`col_varchar_key`),<exists>(/* select#2 */ select `test`.`sq2_alias2`.`col_varchar_key`,`test`.`sq2_alias1`.`col_varchar_nokey` from `test`.`t1` `sq2_alias1` join `test`.`t1` `sq2_alias2` where ((`test`.`sq2_alias2`.`col_int_key` < 2) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias2`.`col_varchar_key`) and (<cache>(`test`.`alias2`.`col_varchar_key`) = `test`.`sq2_alias1`.`col_varchar_nokey`)))))
+SELECT alias1.col_datetime_key
+FROM t2 AS alias1
+RIGHT JOIN t2 AS alias2
+JOIN t2 AS alias3
+ON alias3.pk = alias2.pk
+ON alias3.col_varchar_nokey = alias2.col_varchar_key OR
+alias2.col_varchar_nokey
+WHERE (alias2.col_varchar_key, alias2.col_varchar_key) IN
+(SELECT sq2_alias2.col_varchar_key, sq2_alias1.col_varchar_nokey
+FROM t1 AS sq2_alias1, t1 AS sq2_alias2
+WHERE sq2_alias2.col_int_key < 2);
+col_datetime_key
+2002-02-13 17:30:06
+2002-02-13 17:30:06
+2008-09-27 00:34:58
+2008-09-27 00:34:58
+2007-05-28 00:00:00
+2007-05-28 00:00:00
+2009-07-25 09:21:20
+2009-07-25 09:21:20
+2002-01-16 00:00:00
+2002-01-16 00:00:00
+2006-10-12 04:32:53
+2006-10-12 04:32:53
+2001-02-15 03:08:38
+2001-02-15 03:08:38
+2004-10-02 20:31:15
+2004-10-02 20:31:15
+2002-08-20 22:48:00
+2002-08-20 22:48:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2005-08-15 00:00:00
+2005-08-15 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2008-05-16 08:09:06
+2008-05-16 08:09:06
+2001-01-20 12:47:23
+2001-01-20 12:47:23
+2008-07-02 00:00:00
+2008-07-02 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-12-08 11:34:58
+2002-12-08 11:34:58
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+1900-01-01 00:00:00
+2002-08-25 20:35:06
+2002-08-25 20:35:06
+DROP TABLE t1, t2;
 # End of test for bug#14064201.
 # End of 5.6 tests
 set optimizer_switch=default;

=== modified file 'sql/sql_class.h'
--- a/sql/sql_class.h	2012-05-18 14:39:25 +0000
+++ b/sql/sql_class.h	2012-05-30 14:26:55 +0000
@@ -4256,7 +4256,7 @@ public:
     :table_count(table_count), is_scan(is_scan),
     materialized(FALSE), table_param(), table_cols(),
     table(NULL), tab_ref(NULL), in_equality(NULL),
-    join_cond(NULL), copy_field(NULL)
+    join_cond(NULL), copy_field(NULL), copy_field_count(0)
   {}
 private:
   // Nobody deletes me apparently ...
@@ -4275,6 +4275,7 @@ public:
   Item *in_equality;            // See create_subquery_equalities()
   Item *join_cond;              // See comments in make_join_select()
   Copy_field *copy_field;       // Needed for materialization scan
+  uint copy_field_count;        // Number of columns to copy back
 };
 
 

=== modified file 'sql/sql_executor.cc'
--- a/sql/sql_executor.cc	2012-05-29 09:30:56 +0000
+++ b/sql/sql_executor.cc	2012-05-30 14:26:55 +0000
@@ -1834,7 +1834,7 @@ sub_select_sjm(JOIN *join, JOIN_TAB *joi
     last_tab->read_first_record= join_read_record_no_init;
     last_tab->read_record.copy_field= sjm->copy_field;
     last_tab->read_record.copy_field_end= sjm->copy_field +
-                                          sjm->table_cols.elements;
+                                          sjm->copy_field_count;
     last_tab->read_record.read_record= rr_sequential_and_unpack;
     DBUG_ASSERT(last_tab->read_record.unlock_row == rr_unlock_row);
 

=== modified file 'sql/sql_select.cc'
--- a/sql/sql_select.cc	2012-05-24 11:55:37 +0000
+++ b/sql/sql_select.cc	2012-05-30 14:26:55 +0000
@@ -2572,7 +2572,7 @@ bool setup_sj_materialization(JOIN_TAB *
       temptable record, we copy its columns to their corresponding columns
       in the record buffers for the source tables. 
     */
-    sjm->copy_field= new Copy_field[sjm->table_cols.elements];
+    sjm->copy_field= new Copy_field[sjm->table_cols.elements*2];
     it.rewind();
     for (uint i=0; i < sjm->table_cols.elements; i++)
     {
@@ -2602,12 +2602,22 @@ bool setup_sj_materialization(JOIN_TAB *
          equality propagation, then we need to unpack it to the first
          element equality propagation member that refers to table that is
          within the subquery.
+         Because we do not have complete control over which fields are needed
+         in all condition generation, we also copy back the original items.
       */
-      Field *copy_to= get_best_field(static_cast<Item_field *>(item),
-                                     tab->join->cond_equal)->field;
-      sjm->copy_field[i].set(copy_to, sjm->table->field[i], FALSE);
+      Item_field *const item_field= static_cast<Item_field *>(item);
+      Field *copy_to= get_best_field(item_field, tab->join->cond_equal)->field;
+      sjm->copy_field[sjm->copy_field_count++].
+        set(copy_to, sjm->table->field[i], false);
       /* The write_set for source tables must be set up to allow the copying */
       bitmap_set_bit(copy_to->table->write_set, copy_to->field_index);
+      if (copy_to != item_field->field)
+      {
+        copy_to= item_field->field;
+        sjm->copy_field[sjm->copy_field_count++].
+          set(copy_to, sjm->table->field[i], false);
+        bitmap_set_bit(copy_to->table->write_set, copy_to->field_index);
+      }
     }
   }
 

No bundle (reason: useless for push emails).
Thread
bzr push into mysql-trunk branch (roy.lyseng:3917 to 3918) Bug#14064201Roy Lyseng30 May