Commit a5780a48 authored by pem@mysql.comhem.se's avatar pem@mysql.comhem.se

Fixed BUG#336: Subselects with tables does not work as values for local SP variables

 and BUG#1654: Stored Procedure Crash if contains subquery and set function

Disallowed subselects in RETURN (for FUNCTIONs) and SET of local variables.
The latter should work, but turned out to be difficult to fix, so we just
disallow it for the time being.
parent 2c16ccdb
...@@ -333,4 +333,5 @@ ...@@ -333,4 +333,5 @@
#define ER_SP_DUP_COND 1314 #define ER_SP_DUP_COND 1314
#define ER_SP_DUP_CURS 1315 #define ER_SP_DUP_CURS 1315
#define ER_SP_CANT_ALTER 1316 #define ER_SP_CANT_ALTER 1316
#define ER_ERROR_MESSAGES 317 #define ER_SP_SUBSELECT_NYI 1317
#define ER_ERROR_MESSAGES 318
...@@ -194,3 +194,4 @@ ER_SP_DUP_VAR, "42000", "", ...@@ -194,3 +194,4 @@ ER_SP_DUP_VAR, "42000", "",
ER_SP_DUP_COND, "42000", "", ER_SP_DUP_COND, "42000", "",
ER_SP_DUP_CURS, "42000", "", ER_SP_DUP_CURS, "42000", "",
/*ER_SP_CANT_ALTER*/ /*ER_SP_CANT_ALTER*/
ER_SP_SUBSELECT_NYI, "0A000", "",
...@@ -270,4 +270,14 @@ ERROR 42S22: Unknown column 'valname' in 'order clause' ...@@ -270,4 +270,14 @@ ERROR 42S22: Unknown column 'valname' in 'order clause'
drop procedure bug1965; drop procedure bug1965;
select 1 into a; select 1 into a;
ERROR 42000: Undeclared variable: a ERROR 42000: Undeclared variable: a
create procedure bug336(id char(16))
begin
declare x int;
set x = (select sum(t.data) from test.t2 t);
end;
ERROR 0A000: Subselect value not supported
create function bug1654()
returns int
return (select sum(t.data) from test.t2 t);
ERROR 0A000: Statements like SELECT, INSERT, UPDATE (and others) are not allowed in a FUNCTION
drop table t1; drop table t1;
...@@ -361,6 +361,23 @@ drop procedure bug1965| ...@@ -361,6 +361,23 @@ drop procedure bug1965|
--error 1309 --error 1309
select 1 into a| select 1 into a|
#
# BUG#336
#
--error 1317
create procedure bug336(id char(16))
begin
declare x int;
set x = (select sum(t.data) from test.t2 t);
end|
#
# BUG#1654
#
--error 1296
create function bug1654()
returns int
return (select sum(t.data) from test.t2 t)|
drop table t1| drop table t1|
......
...@@ -240,6 +240,17 @@ Item_splocal::this_const_item() const ...@@ -240,6 +240,17 @@ Item_splocal::this_const_item() const
return thd->spcont->get_item(m_offset); return thd->spcont->get_item(m_offset);
} }
Item::Type
Item_splocal::type() const
{
THD *thd= current_thd;
if (thd->spcont)
return thd->spcont->get_item(m_offset)->type();
return NULL_ITEM; // Anything but SUBSELECT_ITEM
}
bool DTCollation::aggregate(DTCollation &dt) bool DTCollation::aggregate(DTCollation &dt)
{ {
if (!my_charset_same(collation, dt.collation)) if (!my_charset_same(collation, dt.collation))
......
...@@ -260,10 +260,7 @@ class Item_splocal : public Item ...@@ -260,10 +260,7 @@ class Item_splocal : public Item
// Abstract methods inherited from Item. Just defer the call to // Abstract methods inherited from Item. Just defer the call to
// the item in the frame // the item in the frame
inline enum Type type() const enum Type type() const;
{
return this_const_item()->type();
}
inline double val() inline double val()
{ {
......
...@@ -329,3 +329,4 @@ character-set=latin2 ...@@ -329,3 +329,4 @@ character-set=latin2
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -323,3 +323,4 @@ character-set=latin1 ...@@ -323,3 +323,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -331,3 +331,4 @@ character-set=latin1 ...@@ -331,3 +331,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -320,3 +320,4 @@ character-set=latin1 ...@@ -320,3 +320,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -325,3 +325,4 @@ character-set=latin7 ...@@ -325,3 +325,4 @@ character-set=latin7
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -320,3 +320,4 @@ character-set=latin1 ...@@ -320,3 +320,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -332,3 +332,4 @@ character-set=latin1 ...@@ -332,3 +332,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -320,3 +320,4 @@ character-set=greek ...@@ -320,3 +320,4 @@ character-set=greek
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -322,3 +322,4 @@ character-set=latin2 ...@@ -322,3 +322,4 @@ character-set=latin2
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -320,3 +320,4 @@ character-set=latin1 ...@@ -320,3 +320,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -322,3 +322,4 @@ character-set=ujis ...@@ -322,3 +322,4 @@ character-set=ujis
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -320,3 +320,4 @@ character-set=euckr ...@@ -320,3 +320,4 @@ character-set=euckr
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -322,3 +322,4 @@ character-set=latin1 ...@@ -322,3 +322,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -322,3 +322,4 @@ character-set=latin1 ...@@ -322,3 +322,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -324,3 +324,4 @@ character-set=latin2 ...@@ -324,3 +324,4 @@ character-set=latin2
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -321,3 +321,4 @@ character-set=latin1 ...@@ -321,3 +321,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -324,3 +324,4 @@ character-set=latin2 ...@@ -324,3 +324,4 @@ character-set=latin2
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -322,3 +322,4 @@ character-set=koi8r ...@@ -322,3 +322,4 @@ character-set=koi8r
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -315,3 +315,4 @@ character-set=cp1250 ...@@ -315,3 +315,4 @@ character-set=cp1250
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -328,3 +328,4 @@ character-set=latin2 ...@@ -328,3 +328,4 @@ character-set=latin2
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -322,3 +322,4 @@ character-set=latin1 ...@@ -322,3 +322,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -320,3 +320,4 @@ character-set=latin1 ...@@ -320,3 +320,4 @@ character-set=latin1
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -325,3 +325,4 @@ character-set=koi8u ...@@ -325,3 +325,4 @@ character-set=koi8u
"Duplicate condition: %s" "Duplicate condition: %s"
"Duplicate cursor: %s" "Duplicate cursor: %s"
"Failed to ALTER %s %s" "Failed to ALTER %s %s"
"Subselect value not supported"
...@@ -714,6 +714,7 @@ sp_instr_stmt::exec_stmt(THD *thd, LEX *lex) ...@@ -714,6 +714,7 @@ sp_instr_stmt::exec_stmt(THD *thd, LEX *lex)
{ {
LEX *olex; // The other lex LEX *olex; // The other lex
Item *freelist; Item *freelist;
SELECT_LEX *sl;
int res; int res;
olex= thd->lex; // Save the other lex olex= thd->lex; // Save the other lex
...@@ -726,7 +727,7 @@ sp_instr_stmt::exec_stmt(THD *thd, LEX *lex) ...@@ -726,7 +727,7 @@ sp_instr_stmt::exec_stmt(THD *thd, LEX *lex)
// Copy WHERE clause pointers to avoid damaging by optimisation // Copy WHERE clause pointers to avoid damaging by optimisation
// Also clear ref_pointer_arrays. // Also clear ref_pointer_arrays.
for (SELECT_LEX *sl= lex->all_selects_list ; for (sl= lex->all_selects_list ;
sl ; sl ;
sl= sl->next_select_in_list()) sl= sl->next_select_in_list())
{ {
...@@ -772,7 +773,7 @@ sp_instr_stmt::exec_stmt(THD *thd, LEX *lex) ...@@ -772,7 +773,7 @@ sp_instr_stmt::exec_stmt(THD *thd, LEX *lex)
close_thread_tables(thd); /* Free tables */ close_thread_tables(thd); /* Free tables */
} }
for (SELECT_LEX *sl= lex->all_selects_list ; for (sl= lex->all_selects_list ;
sl ; sl ;
sl= sl->next_select_in_list()) sl= sl->next_select_in_list())
{ {
......
...@@ -1548,10 +1548,15 @@ sp_proc_stmt: ...@@ -1548,10 +1548,15 @@ sp_proc_stmt:
} }
else else
{ {
sp_instr_freturn *i= sp_instr_freturn *i;
new sp_instr_freturn(lex->sphead->instructions(),
$2, lex->sphead->m_returns);
if ($2->type() == Item::SUBSELECT_ITEM)
{ /* QQ For now, just disallow subselects as values */
send_error(lex->thd, ER_SP_BADSTATEMENT);
YYABORT;
}
i= new sp_instr_freturn(lex->sphead->instructions(),
$2, lex->sphead->m_returns);
lex->sphead->add_instr(i); lex->sphead->add_instr(i);
lex->sphead->m_has_return= TRUE; lex->sphead->m_has_return= TRUE;
} }
...@@ -5933,6 +5938,11 @@ option_value: ...@@ -5933,6 +5938,11 @@ option_value:
} }
else else
{ /* An SP local variable */ { /* An SP local variable */
if ($3 && $3->type() == Item::SUBSELECT_ITEM)
{ /* QQ For now, just disallow subselects as values */
send_error(lex->thd, ER_SP_SUBSELECT_NYI);
YYABORT;
}
sp_pvar_t *spv= lex->spcont->find_pvar(&$1.base_name); sp_pvar_t *spv= lex->spcont->find_pvar(&$1.base_name);
sp_instr_set *i= new sp_instr_set(lex->sphead->instructions(), sp_instr_set *i= new sp_instr_set(lex->sphead->instructions(),
spv->offset, $3, spv->type); spv->offset, $3, spv->type);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment