diff --git a/mysql-test/main/sp_validation.result b/mysql-test/main/sp_validation.result index c9771f8f862..b95e84aad87 100644 --- a/mysql-test/main/sp_validation.result +++ b/mysql-test/main/sp_validation.result @@ -1965,4 +1965,32 @@ b 2 3 DROP TABLE t1, t2; +# +# MDEV-33525: Recreate/reuse temporary table +# +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1); +CREATE FUNCTION f1() +RETURNS INT +BEGIN +DECLARE res INT; +DECLARE t1_cur CURSOR FOR SELECT 100 FROM t1, t1_tmp; +CREATE TEMPORARY TABLE t1_tmp SELECT 1 a; +OPEN t1_cur; +CLOSE t1_cur; +DROP TEMPORARY TABLE t1_tmp; +RETURN 0; +END +| +SELECT f1(); +f1() +0 +# Without the patch, the second call of f1 would result in error: +# ER_NO_SUCH_TABLE (1146): Table 'test.t1' doesn't exist +SELECT f1(); +f1() +0 +# Clean up +DROP FUNCTION f1; +DROP TABLE t1; SET sql_mode = default; diff --git a/mysql-test/main/sp_validation.test b/mysql-test/main/sp_validation.test index 990095f1a7f..6f095710e2c 100644 --- a/mysql-test/main/sp_validation.test +++ b/mysql-test/main/sp_validation.test @@ -2760,5 +2760,40 @@ SELECT * FROM t2; DROP TABLE t1, t2; +--echo # +--echo # MDEV-33525: Recreate/reuse temporary table +--echo # +CREATE TABLE t1 (a INT); +INSERT INTO t1 VALUES (1); + +--delimiter | +CREATE FUNCTION f1() +RETURNS INT +BEGIN + DECLARE res INT; + + DECLARE t1_cur CURSOR FOR SELECT 100 FROM t1, t1_tmp; + + CREATE TEMPORARY TABLE t1_tmp SELECT 1 a; + + OPEN t1_cur; + CLOSE t1_cur; + + DROP TEMPORARY TABLE t1_tmp; + + RETURN 0; +END +| + +--delimiter ; +SELECT f1(); +--echo # Without the patch, the second call of f1 would result in error: +--echo # ER_NO_SUCH_TABLE (1146): Table 'test.t1' doesn't exist +SELECT f1(); + +--echo # Clean up +DROP FUNCTION f1; +DROP TABLE t1; + SET sql_mode = default; --enable_ps2_protocol diff --git a/sql/sp_instr.cc b/sql/sp_instr.cc index 2e5a77d0d36..650d068d71f 100644 --- a/sql/sp_instr.cc +++ b/sql/sp_instr.cc @@ -754,6 +754,7 @@ LEX* sp_lex_instr::parse_expr(THD *thd, sp_head *sp, LEX *sp_instr_lex) cleanup_items(cursor_lex->free_list); cursor_free_list= &cursor_lex->free_list; DBUG_ASSERT(thd->lex == sp_instr_lex); + lex_start(thd); } thd->lex->sphead= sp;