mirror of
https://github.com/MariaDB/server.git
synced 2025-07-30 16:24:05 +03:00
MDEV-12441 Variables declared after cursors with parameters lose values
Parse context frames (sp_pcontext) can have holes in variable run-time offsets, the missing offsets reside on the children contexts in such cases. Example: CREATE PROCEDURE p1() AS x0 INT:=100; -- context 0, position 0, run-time 0 CURSOR cur( p0 INT, -- context 1, position 0, run-time 1 p1 INT -- context 1, position 1, run-time 2 ) IS SELECT p0, p1; x1 INT:=101; -- context 0, position 1, run-time 3 BEGIN ... END; Fixing a few methods to take this into account: - sp_pcontext::find_variable() - sp_pcontext::retrieve_field_definitions() - LEX::sp_variable_declarations_init() - LEX::sp_variable_declarations_finalize() - LEX::sp_variable_declarations_rowtype_finalize() - LEX::sp_variable_declarations_with_ref_finalize() Adding a convenience method: sp_pcontext::get_last_context_variable(uint offset_from_the_end); to access variables from the end, rather than from the beginning. This helps to loop through the context variable array (m_vars) on the fragment that does not have any holes. Additionally, renaming sp_pcontext::find_context_variable() to sp_pcontext::get_context_variable(). This method simply returns the variable by its index. So let's rename to avoid assumptions that some heavy lookup is going on inside.
This commit is contained in:
@ -971,3 +971,87 @@ DELIMITER ;$$
|
||||
SHOW PROCEDURE CODE p1;
|
||||
CALL p1();
|
||||
DROP PROCEDURE p1;
|
||||
|
||||
|
||||
--echo #
|
||||
--echo # MDEV-12441 Variables declared after cursors with parameters lose values
|
||||
--echo #
|
||||
|
||||
DELIMITER $$;
|
||||
CREATE PROCEDURE p1() AS
|
||||
x0 INT:=100;
|
||||
CURSOR cur(cp1 INT, cp2 INT) IS SELECT cp1+cp2;
|
||||
x1 INT:=101;
|
||||
BEGIN
|
||||
OPEN cur(10,11);
|
||||
CLOSE cur;
|
||||
SELECT x0, x1;
|
||||
END;
|
||||
$$
|
||||
DELIMITER ;$$
|
||||
SHOW PROCEDURE CODE p1;
|
||||
CALL p1();
|
||||
DROP PROCEDURE p1;
|
||||
|
||||
|
||||
DELIMITER $$;
|
||||
CREATE PROCEDURE p1() AS
|
||||
x0 INT:=100;
|
||||
CURSOR cur0(cp1 INT, cp2 INT) IS SELECT cp1+cp2;
|
||||
x1 INT:=101;
|
||||
CURSOR cur1(cp1 INT, cp2 INT) IS SELECT cp1+cp2;
|
||||
x2 INT:=102;
|
||||
CURSOR cur2(cp1 INT, cp2 INT) IS SELECT cp1+cp2;
|
||||
x3 INT:=103;
|
||||
BEGIN
|
||||
OPEN cur0(0,1);
|
||||
CLOSE cur0;
|
||||
SELECT x0, x1, x2, x3;
|
||||
OPEN cur1(10,11);
|
||||
CLOSE cur1;
|
||||
SELECT x0, x1, x2, x3;
|
||||
OPEN cur2(20,21);
|
||||
CLOSE cur2;
|
||||
SELECT x0, x1, x2, x3;
|
||||
END;
|
||||
$$
|
||||
DELIMITER ;$$
|
||||
SHOW PROCEDURE CODE p1;
|
||||
CALL p1();
|
||||
DROP PROCEDURE p1;
|
||||
|
||||
|
||||
CREATE TABLE t1 (a INT);
|
||||
DELIMITER $$;
|
||||
CREATE PROCEDURE p1() AS
|
||||
x0 INT:=100;
|
||||
CURSOR cur(cp1 INT, cp2 INT) IS SELECT cp1+cp2;
|
||||
x1 t1.a%TYPE:=101;
|
||||
BEGIN
|
||||
OPEN cur(10,11);
|
||||
CLOSE cur;
|
||||
SELECT x0, x1;
|
||||
END;
|
||||
$$
|
||||
DELIMITER ;$$
|
||||
SHOW PROCEDURE CODE p1;
|
||||
CALL p1();
|
||||
DROP PROCEDURE p1;
|
||||
DROP TABLE t1;
|
||||
|
||||
|
||||
DELIMITER $$;
|
||||
CREATE PROCEDURE p1() AS
|
||||
x0 INT:=100;
|
||||
CURSOR cur(cp1 INT, cp2 INT) IS SELECT cp1+cp2;
|
||||
x1 ROW(a INT,b INT):=ROW(101,102);
|
||||
BEGIN
|
||||
OPEN cur(10,11);
|
||||
CLOSE cur;
|
||||
SELECT x0, x1.a, x1.b;
|
||||
END;
|
||||
$$
|
||||
DELIMITER ;$$
|
||||
SHOW PROCEDURE CODE p1;
|
||||
CALL p1();
|
||||
DROP PROCEDURE p1;
|
||||
|
Reference in New Issue
Block a user