Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-11225

SELECT from view, which was replaced in prepared statement inside cursor, gives wrong result

Details

    • 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;
      

      Attachments

        Issue Links

          Activity

            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.

            elenst Elena Stepanova added a comment - 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.

            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.

            sanja Oleksandr Byelkin added a comment - 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.

            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).

            sanja Oleksandr Byelkin added a comment - 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).

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

            sanja Oleksandr Byelkin added a comment - in mysql we have new TABLE_LIST object on each SELECT execution.
            sanja Oleksandr Byelkin added a comment - It looks like we lack this https://dev.mysql.com/worklog/task/?id=4179

            People

              sanja Oleksandr Byelkin
              wollewalda Wolfgang Walther
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.