Loading mysql-test/r/subselect.result +9 −6 Original line number Diff line number Diff line Loading @@ -55,7 +55,7 @@ SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1; 1 1 SELECT (SELECT 1), a; ERROR 42S22: Unknown column 'a' in 'checking transformed subquery' ERROR 42S22: Unknown column 'a' in 'field list' SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1; a 1 Loading Loading @@ -999,7 +999,9 @@ drop table t1, t2; CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1; INSERT INTO t1 VALUES (1); UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); ERROR HY000: Invalid use of group function select * from t1; i 1 drop table t1; CREATE TABLE t1 (a int(1)); EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1; Loading Loading @@ -1084,7 +1086,7 @@ SHOW CREATE TABLE t1; Table Create Table t1 CREATE TABLE `t1` ( `a` bigint(20) NOT NULL default '0', `(SELECT a)` bigint(20) default NULL `(SELECT a)` bigint(20) NOT NULL default '0' ) ENGINE=MyISAM DEFAULT CHARSET=latin1 drop table t1; CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a; Loading @@ -1102,7 +1104,7 @@ a SHOW CREATE TABLE t1; Table Create Table t1 CREATE TABLE `t1` ( `a` bigint(20) default NULL `a` bigint(20) NOT NULL default '0' ) ENGINE=MyISAM DEFAULT CHARSET=latin1 drop table t1; create table t1 (a int); Loading Loading @@ -1186,11 +1188,12 @@ PRIMARY KEY (`i`) ) ENGINE=MyISAM CHARSET=latin1; INSERT INTO t1 VALUES (1); UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i)); ERROR HY000: Invalid use of group function UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); ERROR HY000: Invalid use of group function UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); ERROR 42S02: Unknown table 't' in field list select * from t1; i 1 drop table t1; CREATE TABLE t1 ( id int(11) default NULL Loading mysql-test/t/subselect.test +2 −3 Original line number Diff line number Diff line Loading @@ -543,8 +543,8 @@ drop table t1, t2; CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1; INSERT INTO t1 VALUES (1); -- error 1111 UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); select * from t1; drop table t1; #test of uncacheable subqueries Loading Loading @@ -689,12 +689,11 @@ CREATE TABLE `t1` ( ) ENGINE=MyISAM CHARSET=latin1; INSERT INTO t1 VALUES (1); -- error 1111 UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i)); -- error 1111 UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); -- error 1109 UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); select * from t1; drop table t1; # Loading sql/item_subselect.cc +76 −15 Original line number Diff line number Diff line Loading @@ -164,12 +164,7 @@ bool Item_subselect::fix_fields(THD *thd_param, TABLE_LIST *tables, Item **ref) thd->where= "checking transformed subquery"; if (!(*ref)->fixed) ret= (*ref)->fix_fields(thd, tables, ref); // We can't substitute aggregate functions like "SELECT (max(i))" if (substype() == SINGLEROW_SUBS && (*ref)->with_sum_func) { my_error(ER_INVALID_GROUP_FUNC_USE, MYF(0)); return 1; } thd->where= save_where; return ret; } // Is it one field subselect? Loading Loading @@ -322,6 +317,7 @@ Item_singlerow_subselect::select_transformer(JOIN *join) if (!select_lex->master_unit()->first_select()->next_select() && !select_lex->table_list.elements && select_lex->item_list.elements == 1 && !select_lex->item_list.head()->with_sum_func && /* We cant change name of Item_field or Item_ref, because it will prevent it's correct resolving, but we should save name of Loading @@ -330,7 +326,13 @@ Item_singlerow_subselect::select_transformer(JOIN *join) TODO: solve above problem */ !(select_lex->item_list.head()->type() == FIELD_ITEM || select_lex->item_list.head()->type() == REF_ITEM) select_lex->item_list.head()->type() == REF_ITEM) && /* switch off this optimisation for prepare statement, because we do not rollback this changes TODO: make rollback for it, or special name resolving mode in 5.0. */ !arena->is_stmt_prepare() ) { Loading @@ -352,9 +354,6 @@ Item_singlerow_subselect::select_transformer(JOIN *join) if (join->conds || join->having) { Item *cond; if (arena->is_stmt_prepare()) thd->set_n_backup_item_arena(arena, &backup); if (!join->having) cond= join->conds; else if (!join->conds) Loading @@ -365,16 +364,12 @@ Item_singlerow_subselect::select_transformer(JOIN *join) if (!(substitution= new Item_func_if(cond, substitution, new Item_null()))) goto err; if (arena->is_stmt_prepare()) thd->restore_backup_item_arena(arena, &backup); } return RES_REDUCE; } return RES_OK; err: if (arena->is_stmt_prepare()) thd->restore_backup_item_arena(arena, &backup); return RES_ERROR; } Loading @@ -401,6 +396,13 @@ void Item_singlerow_subselect::fix_length_and_dec() engine->fix_length_and_dec(row); value= *row; } /* If there are not tables in subquery then ability to have NULL value depends on SELECT list (if single row subquery have tables then it always can be NULL if there are not records fetched). */ if (engine->no_tables()) maybe_null= engine->may_be_null(); } uint Item_singlerow_subselect::cols() Loading Loading @@ -644,6 +646,7 @@ Item_subselect::trans_res Item_in_subselect::single_value_transformer(JOIN *join, Comp_creator *func) { const char *save_where= thd->where; DBUG_ENTER("Item_in_subselect::single_value_transformer"); if (changed) Loading Loading @@ -899,6 +902,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, ok: if (arena->is_stmt_prepare()) thd->restore_backup_item_arena(arena, &backup); thd->where= save_where; DBUG_RETURN(RES_OK); err: Loading @@ -911,6 +915,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, Item_subselect::trans_res Item_in_subselect::row_value_transformer(JOIN *join) { const char *save_where= thd->where; DBUG_ENTER("Item_in_subselect::row_value_transformer"); if (changed) Loading Loading @@ -1003,6 +1008,7 @@ Item_in_subselect::row_value_transformer(JOIN *join) } if (arena->is_stmt_prepare()) thd->restore_backup_item_arena(arena, &backup); thd->where= save_where; DBUG_RETURN(RES_OK); err: Loading Loading @@ -1562,3 +1568,58 @@ int subselect_uniquesubquery_engine::change_item(Item_subselect *si, DBUG_ASSERT(0); return -1; } /* Report about presence of tables in subquery SINOPSYS subselect_single_select_engine::no_tables() RETURN TRUE there are not tables used in subquery FALSE there are some tables in subquery */ bool subselect_single_select_engine::no_tables() { return(select_lex->table_list.elements == 0); } /* Report about presence of tables in subquery SINOPSYS subselect_union_engine::no_tables() RETURN TRUE there are not tables used in subquery FALSE there are some tables in subquery */ bool subselect_union_engine::no_tables() { for (SELECT_LEX *sl= unit->first_select(); sl; sl= sl->next_select()) { if (sl->table_list.elements) return FALSE; } return TRUE; } /* Report about presence of tables in subquery SINOPSYS subselect_uniquesubquery_engine::no_tables() RETURN TRUE there are not tables used in subquery FALSE there are some tables in subquery */ bool subselect_uniquesubquery_engine::no_tables() { /* returning value is correct, but this method should never be called */ return 0; } sql/item_subselect.h +4 −0 Original line number Diff line number Diff line Loading @@ -291,6 +291,7 @@ class subselect_engine: public Sql_alloc static table_map calc_const_tables(TABLE_LIST *); virtual void print(String *str)= 0; virtual int change_item(Item_subselect *si, select_subselect *result)= 0; virtual bool no_tables()= 0; }; Loading @@ -315,6 +316,7 @@ class subselect_single_select_engine: public subselect_engine table_map upper_select_const_tables(); void print (String *str); int change_item(Item_subselect *si, select_subselect *result); bool no_tables(); }; Loading @@ -335,6 +337,7 @@ class subselect_union_engine: public subselect_engine table_map upper_select_const_tables(); void print (String *str); int change_item(Item_subselect *si, select_subselect *result); bool no_tables(); }; Loading Loading @@ -364,6 +367,7 @@ class subselect_uniquesubquery_engine: public subselect_engine table_map upper_select_const_tables() { return 0; } void print (String *str); int change_item(Item_subselect *si, select_subselect *result); bool no_tables(); }; Loading Loading
mysql-test/r/subselect.result +9 −6 Original line number Diff line number Diff line Loading @@ -55,7 +55,7 @@ SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1; 1 1 SELECT (SELECT 1), a; ERROR 42S22: Unknown column 'a' in 'checking transformed subquery' ERROR 42S22: Unknown column 'a' in 'field list' SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1; a 1 Loading Loading @@ -999,7 +999,9 @@ drop table t1, t2; CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1; INSERT INTO t1 VALUES (1); UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); ERROR HY000: Invalid use of group function select * from t1; i 1 drop table t1; CREATE TABLE t1 (a int(1)); EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1; Loading Loading @@ -1084,7 +1086,7 @@ SHOW CREATE TABLE t1; Table Create Table t1 CREATE TABLE `t1` ( `a` bigint(20) NOT NULL default '0', `(SELECT a)` bigint(20) default NULL `(SELECT a)` bigint(20) NOT NULL default '0' ) ENGINE=MyISAM DEFAULT CHARSET=latin1 drop table t1; CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a; Loading @@ -1102,7 +1104,7 @@ a SHOW CREATE TABLE t1; Table Create Table t1 CREATE TABLE `t1` ( `a` bigint(20) default NULL `a` bigint(20) NOT NULL default '0' ) ENGINE=MyISAM DEFAULT CHARSET=latin1 drop table t1; create table t1 (a int); Loading Loading @@ -1186,11 +1188,12 @@ PRIMARY KEY (`i`) ) ENGINE=MyISAM CHARSET=latin1; INSERT INTO t1 VALUES (1); UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i)); ERROR HY000: Invalid use of group function UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); ERROR HY000: Invalid use of group function UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); ERROR 42S02: Unknown table 't' in field list select * from t1; i 1 drop table t1; CREATE TABLE t1 ( id int(11) default NULL Loading
mysql-test/t/subselect.test +2 −3 Original line number Diff line number Diff line Loading @@ -543,8 +543,8 @@ drop table t1, t2; CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1; INSERT INTO t1 VALUES (1); -- error 1111 UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); select * from t1; drop table t1; #test of uncacheable subqueries Loading Loading @@ -689,12 +689,11 @@ CREATE TABLE `t1` ( ) ENGINE=MyISAM CHARSET=latin1; INSERT INTO t1 VALUES (1); -- error 1111 UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i)); -- error 1111 UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); -- error 1109 UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); select * from t1; drop table t1; # Loading
sql/item_subselect.cc +76 −15 Original line number Diff line number Diff line Loading @@ -164,12 +164,7 @@ bool Item_subselect::fix_fields(THD *thd_param, TABLE_LIST *tables, Item **ref) thd->where= "checking transformed subquery"; if (!(*ref)->fixed) ret= (*ref)->fix_fields(thd, tables, ref); // We can't substitute aggregate functions like "SELECT (max(i))" if (substype() == SINGLEROW_SUBS && (*ref)->with_sum_func) { my_error(ER_INVALID_GROUP_FUNC_USE, MYF(0)); return 1; } thd->where= save_where; return ret; } // Is it one field subselect? Loading Loading @@ -322,6 +317,7 @@ Item_singlerow_subselect::select_transformer(JOIN *join) if (!select_lex->master_unit()->first_select()->next_select() && !select_lex->table_list.elements && select_lex->item_list.elements == 1 && !select_lex->item_list.head()->with_sum_func && /* We cant change name of Item_field or Item_ref, because it will prevent it's correct resolving, but we should save name of Loading @@ -330,7 +326,13 @@ Item_singlerow_subselect::select_transformer(JOIN *join) TODO: solve above problem */ !(select_lex->item_list.head()->type() == FIELD_ITEM || select_lex->item_list.head()->type() == REF_ITEM) select_lex->item_list.head()->type() == REF_ITEM) && /* switch off this optimisation for prepare statement, because we do not rollback this changes TODO: make rollback for it, or special name resolving mode in 5.0. */ !arena->is_stmt_prepare() ) { Loading @@ -352,9 +354,6 @@ Item_singlerow_subselect::select_transformer(JOIN *join) if (join->conds || join->having) { Item *cond; if (arena->is_stmt_prepare()) thd->set_n_backup_item_arena(arena, &backup); if (!join->having) cond= join->conds; else if (!join->conds) Loading @@ -365,16 +364,12 @@ Item_singlerow_subselect::select_transformer(JOIN *join) if (!(substitution= new Item_func_if(cond, substitution, new Item_null()))) goto err; if (arena->is_stmt_prepare()) thd->restore_backup_item_arena(arena, &backup); } return RES_REDUCE; } return RES_OK; err: if (arena->is_stmt_prepare()) thd->restore_backup_item_arena(arena, &backup); return RES_ERROR; } Loading @@ -401,6 +396,13 @@ void Item_singlerow_subselect::fix_length_and_dec() engine->fix_length_and_dec(row); value= *row; } /* If there are not tables in subquery then ability to have NULL value depends on SELECT list (if single row subquery have tables then it always can be NULL if there are not records fetched). */ if (engine->no_tables()) maybe_null= engine->may_be_null(); } uint Item_singlerow_subselect::cols() Loading Loading @@ -644,6 +646,7 @@ Item_subselect::trans_res Item_in_subselect::single_value_transformer(JOIN *join, Comp_creator *func) { const char *save_where= thd->where; DBUG_ENTER("Item_in_subselect::single_value_transformer"); if (changed) Loading Loading @@ -899,6 +902,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, ok: if (arena->is_stmt_prepare()) thd->restore_backup_item_arena(arena, &backup); thd->where= save_where; DBUG_RETURN(RES_OK); err: Loading @@ -911,6 +915,7 @@ Item_in_subselect::single_value_transformer(JOIN *join, Item_subselect::trans_res Item_in_subselect::row_value_transformer(JOIN *join) { const char *save_where= thd->where; DBUG_ENTER("Item_in_subselect::row_value_transformer"); if (changed) Loading Loading @@ -1003,6 +1008,7 @@ Item_in_subselect::row_value_transformer(JOIN *join) } if (arena->is_stmt_prepare()) thd->restore_backup_item_arena(arena, &backup); thd->where= save_where; DBUG_RETURN(RES_OK); err: Loading Loading @@ -1562,3 +1568,58 @@ int subselect_uniquesubquery_engine::change_item(Item_subselect *si, DBUG_ASSERT(0); return -1; } /* Report about presence of tables in subquery SINOPSYS subselect_single_select_engine::no_tables() RETURN TRUE there are not tables used in subquery FALSE there are some tables in subquery */ bool subselect_single_select_engine::no_tables() { return(select_lex->table_list.elements == 0); } /* Report about presence of tables in subquery SINOPSYS subselect_union_engine::no_tables() RETURN TRUE there are not tables used in subquery FALSE there are some tables in subquery */ bool subselect_union_engine::no_tables() { for (SELECT_LEX *sl= unit->first_select(); sl; sl= sl->next_select()) { if (sl->table_list.elements) return FALSE; } return TRUE; } /* Report about presence of tables in subquery SINOPSYS subselect_uniquesubquery_engine::no_tables() RETURN TRUE there are not tables used in subquery FALSE there are some tables in subquery */ bool subselect_uniquesubquery_engine::no_tables() { /* returning value is correct, but this method should never be called */ return 0; }
sql/item_subselect.h +4 −0 Original line number Diff line number Diff line Loading @@ -291,6 +291,7 @@ class subselect_engine: public Sql_alloc static table_map calc_const_tables(TABLE_LIST *); virtual void print(String *str)= 0; virtual int change_item(Item_subselect *si, select_subselect *result)= 0; virtual bool no_tables()= 0; }; Loading @@ -315,6 +316,7 @@ class subselect_single_select_engine: public subselect_engine table_map upper_select_const_tables(); void print (String *str); int change_item(Item_subselect *si, select_subselect *result); bool no_tables(); }; Loading @@ -335,6 +337,7 @@ class subselect_union_engine: public subselect_engine table_map upper_select_const_tables(); void print (String *str); int change_item(Item_subselect *si, select_subselect *result); bool no_tables(); }; Loading Loading @@ -364,6 +367,7 @@ class subselect_uniquesubquery_engine: public subselect_engine table_map upper_select_const_tables() { return 0; } void print (String *str); int change_item(Item_subselect *si, select_subselect *result); bool no_tables(); }; Loading