List:Commits« Previous MessageNext Message »
From:Roy Lyseng Date:April 23 2012 11:30am
Subject:bzr push into mysql-trunk branch (roy.lyseng:3898 to 3899) Bug#13956813
View as plain text  
 3899 Roy Lyseng	2012-04-23
      Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
      
      This bug requires outer join processing, duplicate weedout semi-join
      strategy, derived tables and join buffering for multiple tables in
      order to hit.
      
      It is related to bug#13383857, which added materialization for derived
      tables when join buffering was used. But this was not enough: In some
      cases, we may call evaluate_null_complemented_join_record() without
      all inner tables of the outer join having been materialized. This may
      e.g happen if there are no qualifying rows in the first inner table
      of the outer join. Then, the first and all subsequent inner tables
      will be null-complemented. This by itself does not mandate
      materialization, but materialization is also a prerequisite for setting
      up the rowid buffer for duplicate weedout. A rowid copy operation has
      been set up for this table, but it crashes if the rowid buffer has not
      also been set up.
      
      The fix is to add initializer code (ie table materialization) for
      all tables that have not yet been initialized, even for null-extended
      inner tables of an outer join.
      
      mysql-test/include/subquery_sj.inc
        Added test case for bug#13956813.
      
      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#13956813.
      
      sql/sql_executor.cc
        In evaluate_null_complemented_join_record(), add materialization
        and rowid buffer setup for tables that have not already been
        materialized.
        Materialization is encapsulated into a static inline function.

    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_executor.cc
 3898 Roy Lyseng	2012-04-19
      Bug#13955713: Assert 'JOIN->best_read < ...' on second execution
      
      Two problems caused this second execution crash:
      First, semi-join transformation is conditional, but is assumed
      to be unconditional. Why? Because if the WHERE condition for a query
      block is always false, semi-join transformations are not completed.
      The problem is that the field sj_inner_tables is set in
      fix_after_pullout() only for the first execution, and the call to
      make_join_statistics() which is supposed to call
      pull_out_semijoin_tables(), is omitted if the WHERE condition
      evaluates to false.
      Fixed by setting sj_inner_tables in record_semijoin_nests() which is
      always called after a semi-join transformation.
      
      Second, why is the WHERE condition always false on first execution,
      but not on the second? After semi-join transformation, the WHERE
      condition looks like this:
      
        exists(select sq2_alias1.pk AS sq2_field1
               from t1 as sq2_alias1
               where sq2_alias1.col_varchar_key < alias1.col_varchar_nokey) AND
        alias3.col_varchar_nokey = alias2.col_varchar_key AND
        <in_optimizer>(9,<exists>
              (select sum(t1_sq1_alias1.pk) AS t1_sq1_field2 from t1 t1_sq1_alias1
               having (<cache>(9) = <ref_null_helper>(sum(t1_sq1_alias1.pk))))) AND
        8 = sq1_alias1.pk
      
      The third predicate above represents the IN predicate that was not
      semi-join transformed. It turned out that the Item_in_subselect
      object representing this IN predicate had originally const_item_cache
      = false, but ::fix_after_pullout() errnoneously set it to true.
      Thus, the condition is deemed constant and the condition is evaluated,
      and because the IN predicate is always false, the whole condition is false.
      On second execution, const_item_cache is set to false, the IN
      predicate is not evaluated, the WHERE condition is not false, and
      make_join_statistics() is called, but fails to call
      pull_out_semijoin_tables() because first_optimization is false.
      
      Fixed by not modifying const_item_cache in ::fix_after_pullout().
      Notice that the const-ness of the subquery predicate may also be changed
      in Item_ident::fix_after_pullout(), but it can only change it from
      true to false, which I think is harmless.
      
      mysql-test/include/subquery_sj.inc
        Added test case for bug#13955713.
      
      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#13955713.
      
      sql/item_subselect.cc
        In Item_subselect::fix_after_pullout() and
        Item_in_subselect::fix_after_pullout(), keep const_item_cache unchanged.
      
      sql/sql_optimizer.cc
        In record_semijoin_nests(), set sj_inner_tables for all semi-join
        nests. Notice that the field is redundant and should be removed.
        Bug#13969141 is created to remove it.

    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/item_subselect.cc
      sql/sql_optimizer.cc
=== modified file 'mysql-test/include/subquery_sj.inc'
--- a/mysql-test/include/subquery_sj.inc	2012-04-19 12:34:50 +0000
+++ b/mysql-test/include/subquery_sj.inc	2012-04-23 11:29:25 +0000
@@ -5884,4 +5884,71 @@ DROP TABLE t1;
 
 --echo # End of test for bug#13955713.
 
+--echo #
+--echo # Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+--echo #
+
+CREATE TABLE t1 (
+  pk INT,
+  col_varchar_key VARCHAR(1),
+  col_varchar_nokey VARCHAR(1)
+);
+
+CREATE TABLE t2 (
+  pk INT,
+  col_varchar_key VARCHAR(1),
+  col_varchar_nokey VARCHAR(1)
+);
+
+INSERT INTO t2 VALUES
+ (10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+ (14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+ (18,'v','v'), (19,NULL,NULL), (20,'r','r');
+
+CREATE TABLE t3 (
+  pk INT,
+  col_int_key INT,
+  col_varchar_key VARCHAR(1),
+  KEY col_int_key (col_int_key)
+);
+
+INSERT INTO t3 VALUES
+ (15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+ (19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+ (23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+ (27,0,'p'), (28,NULL,'j'), (29,8,'c');
+
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+     ON t1.pk = t3.pk;
+
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+       sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+     ON sq2_alias1.pk = sq2_alias2.col_int_key;
+
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+
+let $query=
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+     LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+     ON alias2.col_varchar_key = alias1.col_varchar_key AND
+        (alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+        (SELECT * FROM view_inline_1
+        )
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+   (SELECT * FROM view_inline_2
+   );
+
+eval explain $query;
+eval $query;
+
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+
+--echo # End of test for bug#13956813.
 --echo # End of 5.6 tests

=== modified file 'mysql-test/r/subquery_sj_all.result'
--- a/mysql-test/r/subquery_sj_all.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_all.result	2012-04-23 11:29:25 +0000
@@ -9414,5 +9414,79 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	NULL
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where; FirstMatch(alias2)
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Materialize
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # 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-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_all_bka.result	2012-04-23 11:29:25 +0000
@@ -9419,6 +9419,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	NULL
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where; FirstMatch(alias2)
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Materialize
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_all_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_all_bka_nixbnl.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_all_bka_nixbnl.result	2012-04-23 11:29:25 +0000
@@ -9420,6 +9420,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	NULL
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where; FirstMatch(alias2)
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Materialize
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_all_bkaunique.result'
--- a/mysql-test/r/subquery_sj_all_bkaunique.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_all_bkaunique.result	2012-04-23 11:29:25 +0000
@@ -9420,6 +9420,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	NULL
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where; FirstMatch(alias2)
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Materialize
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed.result'
--- a/mysql-test/r/subquery_sj_dupsweed.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed.result	2012-04-23 11:29:25 +0000
@@ -9398,5 +9398,79 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (Block Nested Loop)
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # 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-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_bka.result	2012-04-23 11:29:25 +0000
@@ -9399,6 +9399,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (Block Nested Loop)
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_dupsweed_bka_nixbnl.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_bka_nixbnl.result	2012-04-23 11:29:25 +0000
@@ -9407,6 +9407,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ref	auto_key0	auto_key0	6	alias1.col_varchar_key,alias1.col_varchar_nokey	2	Using index; End temporary
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed_bkaunique.result'
--- a/mysql-test/r/subquery_sj_dupsweed_bkaunique.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_bkaunique.result	2012-04-23 11:29:25 +0000
@@ -9400,6 +9400,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (Block Nested Loop)
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_firstmatch.result'
--- a/mysql-test/r/subquery_sj_firstmatch.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch.result	2012-04-23 11:29:25 +0000
@@ -9397,6 +9397,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	NULL
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where; FirstMatch(alias2)
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Using where; FirstMatch(<derived2>); Using join buffer (Block Nested Loop)
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 #
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for

=== modified file 'mysql-test/r/subquery_sj_firstmatch_bka.result'
--- a/mysql-test/r/subquery_sj_firstmatch_bka.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_bka.result	2012-04-23 11:29:25 +0000
@@ -9398,6 +9398,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	NULL
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where; FirstMatch(alias2)
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Using where; FirstMatch(<derived2>); Using join buffer (Block Nested Loop)
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 #
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for

=== modified file 'mysql-test/r/subquery_sj_firstmatch_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_firstmatch_bka_nixbnl.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_bka_nixbnl.result	2012-04-23 11:29:25 +0000
@@ -9406,6 +9406,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	NULL
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where; FirstMatch(alias2)
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ref	auto_key0	auto_key0	6	alias1.col_varchar_key,alias1.col_varchar_nokey	2	Using index; FirstMatch(<derived2>)
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 #
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for

=== modified file 'mysql-test/r/subquery_sj_firstmatch_bkaunique.result'
--- a/mysql-test/r/subquery_sj_firstmatch_bkaunique.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_bkaunique.result	2012-04-23 11:29:25 +0000
@@ -9399,6 +9399,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	NULL
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where; FirstMatch(alias2)
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Using where; FirstMatch(<derived2>); Using join buffer (Block Nested Loop)
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 #
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for

=== modified file 'mysql-test/r/subquery_sj_loosescan.result'
--- a/mysql-test/r/subquery_sj_loosescan.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_loosescan.result	2012-04-23 11:29:25 +0000
@@ -9399,5 +9399,79 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (Block Nested Loop)
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # 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-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_bka.result	2012-04-23 11:29:25 +0000
@@ -9400,6 +9400,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (Block Nested Loop)
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_loosescan_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_loosescan_bka_nixbnl.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_bka_nixbnl.result	2012-04-23 11:29:25 +0000
@@ -9408,6 +9408,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ref	auto_key0	auto_key0	6	alias1.col_varchar_key,alias1.col_varchar_nokey	2	Using index; End temporary
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_loosescan_bkaunique.result'
--- a/mysql-test/r/subquery_sj_loosescan_bkaunique.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_bkaunique.result	2012-04-23 11:29:25 +0000
@@ -9401,6 +9401,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (Block Nested Loop)
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat.result'
--- a/mysql-test/r/subquery_sj_mat.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_mat.result	2012-04-23 11:29:25 +0000
@@ -9412,5 +9412,79 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where; End temporary
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Materialize
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # 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-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_mat_bka.result	2012-04-23 11:29:25 +0000
@@ -9413,6 +9413,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where; End temporary
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Materialize
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_mat_bka_nixbnl.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_mat_bka_nixbnl.result	2012-04-23 11:29:25 +0000
@@ -9414,6 +9414,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where; End temporary
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Materialize
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat_bkaunique.result'
--- a/mysql-test/r/subquery_sj_mat_bkaunique.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_mat_bkaunique.result	2012-04-23 11:29:25 +0000
@@ -9414,6 +9414,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Start temporary
+1	PRIMARY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+1	PRIMARY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	15	Using where; End temporary
+1	PRIMARY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	Materialize
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat_nosj.result'
--- a/mysql-test/r/subquery_sj_mat_nosj.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_mat_nosj.result	2012-04-23 11:29:25 +0000
@@ -9484,5 +9484,79 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Using where
+1	PRIMARY	<derived2>	ref	auto_key0	auto_key0	4	test.alias2.col_varchar_key	2	Using where
+4	SUBQUERY	<derived7>	ALL	NULL	NULL	NULL	NULL	2	NULL
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+3	SUBQUERY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	NULL
+3	SUBQUERY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	NULL
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # 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-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_none.result	2012-04-23 11:29:25 +0000
@@ -9399,5 +9399,79 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Using where
+1	PRIMARY	<derived2>	ref	auto_key0	auto_key0	4	test.alias2.col_varchar_key	2	Using where
+4	DEPENDENT SUBQUERY	<derived7>	index_subquery	auto_key0	auto_key0	6	func,func	2	Using index; Using where
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+3	DEPENDENT SUBQUERY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+3	DEPENDENT SUBQUERY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # 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-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_none_bka.result	2012-04-23 11:29:25 +0000
@@ -9400,6 +9400,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Using where
+1	PRIMARY	<derived2>	ref	auto_key0	auto_key0	4	test.alias2.col_varchar_key	2	Using where
+4	DEPENDENT SUBQUERY	<derived7>	index_subquery	auto_key0	auto_key0	6	func,func	2	Using index; Using where
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+3	DEPENDENT SUBQUERY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+3	DEPENDENT SUBQUERY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_none_bka_nixbnl.result'
--- a/mysql-test/r/subquery_sj_none_bka_nixbnl.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_none_bka_nixbnl.result	2012-04-23 11:29:25 +0000
@@ -9400,6 +9400,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Using where
+1	PRIMARY	<derived2>	ref	auto_key0	auto_key0	4	test.alias2.col_varchar_key	2	Using where
+4	DEPENDENT SUBQUERY	<derived7>	index_subquery	auto_key0	auto_key0	6	func,func	2	Using index; Using where
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+3	DEPENDENT SUBQUERY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+3	DEPENDENT SUBQUERY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_none_bkaunique.result'
--- a/mysql-test/r/subquery_sj_none_bkaunique.result	2012-04-19 12:34:50 +0000
+++ b/mysql-test/r/subquery_sj_none_bkaunique.result	2012-04-23 11:29:25 +0000
@@ -9401,6 +9401,80 @@ NULL
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1;
 # End of test for bug#13955713.
+#
+# Bug#13956813: Segfault in memcpy from Join_cache::write_record_data()
+#
+CREATE TABLE t1 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+CREATE TABLE t2 (
+pk INT,
+col_varchar_key VARCHAR(1),
+col_varchar_nokey VARCHAR(1)
+);
+INSERT INTO t2 VALUES
+(10,'j','j'), (11,'z','z'), (12,'c','c'), (13,'a','a'),
+(14,'q','q'), (15,'y','y'), (16,NULL,NULL), (17,'r','r'),
+(18,'v','v'), (19,NULL,NULL), (20,'r','r');
+CREATE TABLE t3 (
+pk INT,
+col_int_key INT,
+col_varchar_key VARCHAR(1),
+KEY col_int_key (col_int_key)
+);
+INSERT INTO t3 VALUES
+(15,NULL,'u'), (16,1,'m'), (17,9,NULL), (18,2,'o'),
+(19,9,'w'), (20,2,'m'), (21,4,'q'), (22,0,NULL),
+(23,4,'d'), (24,8,'g'), (25,NULL,'x'), (26,NULL,'f'),
+(27,0,'p'), (28,NULL,'j'), (29,8,'c');
+CREATE VIEW view_inline_0 AS
+SELECT t1.*
+FROM t1 INNER JOIN t3
+ON t1.pk = t3.pk;
+CREATE VIEW view_inline_1 AS
+SELECT sq2_alias2.col_varchar_key AS sq2_field1,
+sq2_alias1.col_varchar_key AS sq2_field2
+FROM t3 AS sq2_alias1 LEFT OUTER JOIN t3 AS sq2_alias2
+ON sq2_alias1.pk = sq2_alias2.col_int_key;
+CREATE VIEW view_inline_2 AS
+SELECT 'p', 'p' UNION SELECT 'k', 's';
+explain SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	11	Using where
+1	PRIMARY	<derived2>	ref	auto_key0	auto_key0	4	test.alias2.col_varchar_key	2	Using where
+4	DEPENDENT SUBQUERY	<derived7>	index_subquery	auto_key0	auto_key0	6	func,func	2	Using index; Using where
+7	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+8	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
+NULL	UNION RESULT	<union7,8>	ALL	NULL	NULL	NULL	NULL	NULL	Using temporary
+3	DEPENDENT SUBQUERY	sq2_alias1	ALL	NULL	NULL	NULL	NULL	15	Using where
+3	DEPENDENT SUBQUERY	sq2_alias2	ref	col_int_key	col_int_key	5	test.sq2_alias1.pk	2	Using where
+2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
+SELECT SUM(alias1.col_varchar_nokey) AS field2
+FROM t2 AS alias2
+LEFT JOIN (SELECT * FROM view_inline_0) AS alias1
+ON alias2.col_varchar_key = alias1.col_varchar_key AND
+(alias2.col_varchar_nokey, alias2.col_varchar_key) IN
+(SELECT * FROM view_inline_1
+)
+WHERE (alias1.col_varchar_key, alias1.col_varchar_nokey) IN
+(SELECT * FROM view_inline_2
+);
+field2
+NULL
+DROP VIEW view_inline_0, view_inline_1, view_inline_2;
+DROP TABLE t1, t2, t3;
+# End of test for bug#13956813.
 # End of 5.6 tests
 set optimizer_switch=default;
 set optimizer_switch=default;

=== modified file 'sql/sql_executor.cc'
--- a/sql/sql_executor.cc	2012-04-11 12:12:00 +0000
+++ b/sql/sql_executor.cc	2012-04-23 11:29:25 +0000
@@ -1879,6 +1879,37 @@ sub_select_sjm(JOIN *join, JOIN_TAB *joi
 }
 
 
+/**
+  Perform one-time initializations for a join_tab object.
+
+  Includes derived table materialization and binding of rowid buffers,
+  if needed.
+  (Currently, semi-join materialization and subquery materialization are
+  handled by other means).
+
+  @param tab   join_tab object to perform materialize input data for.
+
+  @return      False if success, True if error
+*/
+
+static inline bool materialize_join_table(JOIN_TAB *tab)
+{
+  // Check whether materialization is required.
+  if (!tab->materialize_table ||
+      tab->table->pos_in_table_list->materialized)
+    return false;
+
+  // Materialize table prior to reading it
+  if ((*tab->materialize_table)(tab))
+    return true;
+
+  // Bind to the rowid buffer managed by the TABLE object.
+  if (tab->copy_current_rowid)
+    tab->copy_current_rowid->bind_buffer(tab->table->file->ref);
+
+  return false;
+}
+
 /*
   Fill the join buffer with partial records, retrieve all full  matches for them
 
@@ -1938,16 +1969,9 @@ sub_select_cache(JOIN *join, JOIN_TAB *j
     join->thd->send_kill_message();
     DBUG_RETURN(NESTED_LOOP_KILLED);
   }
-  /* Materialize table prior to reading it */
-  if (join_tab->materialize_table &&
-      !join_tab->table->pos_in_table_list->materialized)
-  {
-    if ((*join_tab->materialize_table)(join_tab))
-      DBUG_RETURN(NESTED_LOOP_ERROR);
-    // Bind to the rowid buffer managed by the TABLE object.
-    if (join_tab->copy_current_rowid)
-      join_tab->copy_current_rowid->bind_buffer(join_tab->table->file->ref);
-  }
+  if (materialize_join_table(join_tab))
+    DBUG_RETURN(NESTED_LOOP_ERROR);
+
   if (!test_if_use_dynamic_range_scan(join_tab))
   {
     if (!cache->put_record())
@@ -2146,15 +2170,8 @@ sub_select(JOIN *join,JOIN_TAB *join_tab
 
   join->thd->get_stmt_da()->reset_current_row_for_warning();
 
-  /* Materialize table prior reading it */
-  if (join_tab->materialize_table &&
-      !join_tab->table->pos_in_table_list->materialized)
-  {
-    error= (*join_tab->materialize_table)(join_tab);
-    // Bind to the rowid buffer managed by the TABLE object.
-    if (join_tab->copy_current_rowid)
-      join_tab->copy_current_rowid->bind_buffer(join_tab->table->file->ref);
-  }
+  if (materialize_join_table(join_tab))
+    error= NESTED_LOOP_ERROR;
 
   if (!error)
     error= (*join_tab->read_first_record)(join_tab);
@@ -2561,6 +2578,14 @@ evaluate_null_complemented_join_record(J
 
   for ( ; join_tab <= last_inner_tab ; join_tab++)
   {
+    /*
+      Make sure that materialization is done, so that the rowid buffer is bound.
+      The materialized table itself is actually redundant, unless it is used
+      in a later table scan that needs to read rows.
+    */
+    if (materialize_join_table(join_tab))
+      DBUG_RETURN(NESTED_LOOP_ERROR);
+
     /* Change the the values of guard predicate variables. */
     join_tab->found= 1;
     join_tab->not_null_compl= 0;

No bundle (reason: useless for push emails).
Thread
bzr push into mysql-trunk branch (roy.lyseng:3898 to 3899) Bug#13956813Roy Lyseng23 Apr