diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 9c8a237468a..2d686285050 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -1991,3 +1991,15 @@ ac NULL drop tables t1,t2; set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ; +create table t1 (a int, b int); +create table t2 (a int, b int); +insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5); +insert into t2 values (1,3),(2,1); +select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b; +a b (select max(b) from t2 where t1.b=t2.a) +1 1 3 +1 2 1 +1 3 NULL +2 4 NULL +2 5 NULL +drop table t1, t2; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index 19803f6e3ba..976ce7bf6b0 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -1289,3 +1289,13 @@ drop tables t1,t2; connect (root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK); connection root; set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ; + +# +# primary query with temporary table and subquery with groupping +# +create table t1 (a int, b int); +create table t2 (a int, b int); +insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5); +insert into t2 values (1,3),(2,1); +select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b; +drop table t1, t2; diff --git a/sql/sql_class.h b/sql/sql_class.h index 312d9de9794..6d77b75d70f 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -1305,7 +1305,7 @@ public: if (copy_field) /* Fix for Intel compiler */ { delete [] copy_field; - copy_field=0; + save_copy_field= copy_field= 0; } } }; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 9162cd30d63..06731d26073 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -936,7 +936,7 @@ JOIN::optimize() } } - if (select_lex->master_unit()->uncacheable) + if (select_lex->uncacheable) { if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN)))) DBUG_RETURN(-1); @@ -3833,7 +3833,8 @@ JOIN::join_free(bool full) JOIN_TAB *tab,*end; DBUG_ENTER("JOIN::join_free"); - full= full || !select_lex->uncacheable; + full= full || (!select_lex->uncacheable && + !thd->lex->describe); if (table) { @@ -3862,6 +3863,7 @@ JOIN::join_free(bool full) for (tab= join_tab, end= tab+tables; tab != end; tab++) tab->cleanup(); table= 0; + tables= 0; } else {