1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-27 18:02:13 +03:00

MDEV-32275 getting error 'Illegal parameter data types row and bigint for operation '+' ' when using ITERATE in a FOR..DO

An "ITERATE innerLoop" did not work properly inside
a WHILE loop, which itself is inside an outer FOR loop:

outerLoop:
  FOR
   ...
   innerLoop:
    WHILE
      ...
      ITERATE innerLoop;
      ...
    END WHILE;
    ...
  END FOR;

It erroneously generated an integer increment code for the outer FOR loop.
There were two problems:
1. "ITERATE innerLoop" worked like "ITERATE outerLoop"
2. It was always integer increment, even in case of FOR cursor loops.

Background:
- A FOR loop automatically creates a dedicated sp_pcontext stack entry,
  to put the iteration and bound variables on it.

- Other loop types (LOOP, WHILE, REPEAT), do not generate a dedicated
  slack entry.

  The old code erroneously assumed that sp_pcontext::m_for_loop
  either describes the most inner loop (in case the inner loop is FOR),
  or is empty (in case the inner loop is not FOR).

  But in fact, sp_pcontext::m_for_loop is never empty inside a FOR loop:
  it describes the closest FOR loop, even if this FOR loop has nested
  non-FOR loops inside.

  So when we're near the ITERATE statement in the above script,
  sp_pcontext::m_for_loop is not empty - it stores information about
  the FOR loop labeled as "outrLoop:".

Fix:
- Adding a new member sp_pcontext::Lex_for_loop::m_start_label,
  to remember the explicit or the auto-generated label correspoding
  to the start of the FOR body. It's used during generation
  of "ITERATE loop_label" code to check if "loop_label" belongs
  to the current FOR loop pointed by sp_pcontext::m_for_loop,
  or belongs to a non-FOR nested loop.

- Adding LEX methods sp_for_loop_intrange_iterate() and
  sp_for_loop_cursor_iterate() to reuse the code between
  methods handling:
  * ITERATE
  * END FOR

- Adding a test for Lex_for_loop::is_for_loop_cursor()
  and generate a code either a cursor fetch, or for an integer increment.
  Before this change, it always erroneously generated an integer increment
  version.

- Cleanup: Initialize Lex_for_loop_st::m_cursor_offset inside
  Lex_for_loop_st::init(), to avoid not initialized members.

- Cleanup: Removing a redundant method:
    Lex_for_loop_st::init(const Lex_for_loop_st &other)
  Using Lex_for_loop_st::operator(const Lex_for_loop_st &other) instead.
This commit is contained in:
Alexander Barkov
2023-10-04 14:22:02 +04:00
parent e2da748c29
commit 534a2bf1c6
8 changed files with 672 additions and 24 deletions

View File

@ -975,3 +975,227 @@ drop function f1;
--echo #
--echo # End of 10.3 tests
--echo #
--echo #
--echo # Start of 10.4 tests
--echo #
--echo #
--echo # MDEV-32275 getting error 'Illegal parameter data types row and bigint for operation '+' ' when using ITERATE in a FOR..DO
--echo #
--echo #
--echo # Unlabeled FOR/cursor with a nested labeled LOOP inside
--echo #
DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
DECLARE loopDone TINYINT DEFAULT FALSE;
FOR _row IN (SELECT '' AS a) DO
SELECT 'start of outerLoop';
innerLoop: LOOP
SELECT 'start of innerLoop';
IF loopDone THEN
LEAVE innerLoop;
END IF;
SET loopDone = TRUE;
IF _row.a = 'v1'
THEN
SELECT 'start of THEN block';
ITERATE innerLoop;
END IF;
SELECT 'end of innerLoop';
END LOOP;
SELECT 'end of outerLoop';
END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;
--echo #
--echo # Labeled FOR/cursor with a nested labeled LOOP
--echo #
DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
DECLARE loopDone TINYINT DEFAULT FALSE;
outerLoop:
FOR _row IN (SELECT '' AS a) DO
SELECT 'start of outerLoop';
innerLoop: LOOP
SELECT 'start of innerLoop';
IF loopDone THEN
LEAVE innerLoop;
END IF;
SET loopDone = TRUE;
IF _row.a = 'v1'
THEN
SELECT 'start of IF/v1/THEN block';
ITERATE innerLoop;
END IF;
IF _row.a = 'v2'
THEN
SELECT 'start of IF/v2/THEN block';
ITERATE outerLoop;
END IF;
SELECT 'end of innerLoop';
END LOOP;
SELECT 'end of outerLoop';
END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;
--echo #
--echo # Unlabeled FOR/integer with a labeled LOOP inside
--echo #
DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
DECLARE loopDone TINYINT DEFAULT FALSE;
FOR _index IN 1..10 DO
SELECT 'start of outerLoop';
innerLoop: LOOP
SELECT 'start of innerLoop';
IF loopDone THEN
LEAVE innerLoop;
END IF;
SET loopDone = TRUE;
IF _index = 1
THEN
SELECT 'start of THEN block';
ITERATE innerLoop;
END IF;
SELECT 'end of innerLoop';
END LOOP;
SELECT 'end of outerLoop';
END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;
--echo #
--echo # Labeled FOR/integer with a labeled LOOP inside
--echo #
DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
DECLARE loopDone TINYINT DEFAULT FALSE;
outerLoop:
FOR _index IN 1..10 DO
SELECT 'start of outerLoop';
innerLoop: LOOP
SELECT 'start of innerLoop';
IF loopDone THEN
LEAVE innerLoop;
END IF;
SET loopDone = TRUE;
IF _index = 1
THEN
SELECT 'start of IF/1/THEN block';
ITERATE innerLoop;
END IF;
IF _index = 2
THEN
SELECT 'start of IF/2/THEN block';
ITERATE outerLoop;
END IF;
SELECT 'end of innerLoop';
END LOOP;
SELECT 'end of outerLoop';
END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;
--echo #
--echo # Unlabeled FOR/integer with a labeled FOR inside
--echo #
DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
DECLARE loopDone TINYINT DEFAULT FALSE;
FOR _index_outer IN 1..10 DO
SELECT 'start of outerLoop';
innerLoop:
FOR _index_inner IN 1..10 DO
SELECT 'start of innerLoop';
IF loopDone THEN
LEAVE innerLoop;
END IF;
SET loopDone = TRUE;
IF _index_inner = 1
THEN
SELECT 'start of IF/1/THEN block';
ITERATE innerLoop;
END IF;
SELECT 'end of innerLoop';
END FOR;
SELECT 'end of outerLoop';
END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;
--echo #
--echo # Labeled FOR/integer with a labeled FOR inside
--echo #
DELIMITER $$;
CREATE OR REPLACE PROCEDURE p1()
BEGIN
DECLARE loopDone TINYINT DEFAULT FALSE;
outerLoop:
FOR _index_outer IN 1..10 DO
SELECT 'start of outerLoop';
innerLoop:
FOR _index_inner IN 1..10 DO
SELECT 'start of innerLoop';
IF loopDone THEN
LEAVE innerLoop;
END IF;
SET loopDone = TRUE;
IF _index_inner = 1
THEN
SELECT 'start of IF/1/THEN block';
ITERATE innerLoop;
END IF;
IF _index_inner = 2
THEN
SELECT 'start of IF/2/THEN block';
ITERATE outerLoop;
END IF;
SELECT 'end of innerLoop';
END FOR;
SELECT 'end of outerLoop';
END FOR;
END
$$
DELIMITER ;$$
SHOW PROCEDURE CODE p1;
DROP PROCEDURE p1;
--echo #
--echo # End of 10.4 tests
--echo #