[MDEV-11225] SELECT from view, which was replaced in prepared statement inside cursor, gives wrong result Created: 2016-11-02  Updated: 2022-11-29

Status: Stalled
Project: MariaDB Server
Component/s: Prepared Statements, Views
Affects Version/s: 5.5, 10.0, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9
Fix Version/s: 10.3

Type: Bug Priority: Major
Reporter: Wolfgang Walther Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: upstream-fixed

Issue Links:
Blocks
is blocked by MDEV-5816 MySQL WL#4179 - Stored programs: vali... Closed
Sprint: 10.1.22

 Description   

Affects earlier versions as well.

The following creates and replaces a view pointing to different tables within prepared statements inside a cursor. While still in the cursor a SELECT from the replaced view yields results from the table referenced previously, the view doesn't seem to be updated. SHOW CREATE VIEW gives correct results, though.

Test case:

DROP PROCEDURE IF EXISTS test.sp_testcase;
DROP TABLE IF EXISTS test.tmp_list;
DROP TABLE IF EXISTS test.a;
DROP TABLE IF EXISTS test.b;
 
CREATE TABLE test.tmp_list AS
SELECT 'a' AS col
UNION
SELECT 'b' AS col
;
 
CREATE TABLE test.a AS
SELECT 'AAA';
 
CREATE TABLE test.b AS
SELECT 'BBB';
 
DELIMITER #
 
CREATE PROCEDURE test.sp_testcase ()
BEGIN
 
  DECLARE done BOOLEAN DEFAULT FALSE;
  DECLARE var VARCHAR(255);
  DECLARE cur CURSOR FOR
  SELECT col FROM test.tmp_list;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
  
  OPEN cur;
  testLoop: LOOP
    FETCH cur INTO var;
    IF done THEN
      LEAVE testLoop;
    END IF;
    
    SELECT var;
    
    SET @stmt = CONCAT('CREATE OR REPLACE VIEW test.v AS SELECT * FROM ', var);
    SELECT @stmt;
    PREPARE _sql FROM @stmt;
    EXECUTE _sql;
    DROP PREPARE _sql;
    
    -- should return 'AAA' on the first iteration and 'BBB' on the second
    -- returns 'AAA' on both iterations
    SELECT * FROM test.v;
 
    SHOW CREATE VIEW test.v;
    
  END LOOP testLoop;
  CLOSE cur;
 
  -- returns 'BBB' as expected
  SELECT * FROM test.v;
  SHOW CREATE VIEW test.v;
    
END#
 
DELIMITER ;
 
CALL test.sp_testcase();
 
DROP PROCEDURE test.sp_testcase;
DROP TABLE test.tmp_list;
DROP TABLE test.a;
DROP TABLE test.b;



 Comments   
Comment by Elena Stepanova [ 2016-11-03 ]

Thanks for the report. Reproducible as described on all of 5.5-10.2.
Also reproducible on MySQL 5.5, but not 5.6/5.7.

Comment by Oleksandr Byelkin [ 2017-03-05 ]

Problem is that view detect prepare on previous execution and do not try to process it second time. It is in mysql_make_view "if (table->view)" branch.

Comment by Oleksandr Byelkin [ 2017-03-05 ]

need to talk with somebody about memory allocation in this scenario (there is solution but this in general should allocate memory for each view definition).

Comment by Oleksandr Byelkin [ 2017-03-06 ]

in mysql we have new TABLE_LIST object on each SELECT execution.

Comment by Oleksandr Byelkin [ 2017-03-07 ]

It looks like we lack this https://dev.mysql.com/worklog/task/?id=4179

Generated at Thu Feb 08 07:48:16 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.