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

ROW variables do not get assigned from subselects

Details

    Description

      This script using the SELECT..INTO syntax correctly initializes a ROW variable:

      CREATE OR REPLACE TABLE t1 (a INT, b TEXT);
      INSERT INTO t1 VALUES (1,'b1');
      DELIMITER $$
      BEGIN NOT ATOMIC
        DECLARE r ROW TYPE OF t1;
        SELECT * INTO r FROM t1 WHERE a=1;
        SELECT r.a, r.b;
      END;
      $$
      DELIMITER ;
      

      +------+------+
      | r.a  | r.b  |
      +------+------+
      |    1 | b1   |
      +------+------+
      

      so far so good.

      However if I modify the script slightly and replace SELECT..INTO to an equivalent SET, it does not work:

      CREATE OR REPLACE TABLE t1 (a INT, b TEXT);
      INSERT INTO t1 VALUES (1,'b1');
      DELIMITER $$
      BEGIN NOT ATOMIC
        DECLARE r ROW TYPE OF t1;
        SET r=(SELECT * FROM t1 WHERE a=1);
        SELECT r.a, r.b;
      END;
      $$
      DELIMITER ;
      

      +------+------+
      | r.a  | r.b  |
      +------+------+
      | NULL | NULL |
      +------+------+
      

      Another equivalent script with a subselect in the DEFAULT clause of a variable declaration also does not work:

      CREATE OR REPLACE TABLE t1 (a INT, b TEXT);
      INSERT INTO t1 VALUES (1,'b1');
      DELIMITER $$
      BEGIN NOT ATOMIC
        DECLARE r ROW TYPE OF t1 DEFAULT (SELECT * FROM t1 WHERE a=1);
        SELECT r.a, r.b;
      END;
      $$
      DELIMITER ;
      

      +------+------+
      | r.a  | r.b  |
      +------+------+
      | NULL | NULL |
      +------+------+
      

      It should be fixed to have the secord and the third script produce equal results to the first script.

      Attachments

        Issue Links

          Activity

            bar Alexander Barkov created issue -
            bar Alexander Barkov made changes -
            Field Original Value New Value
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            Description This script using the SELECT..INTO syntax correctly initializes a ROW variable:
            {code:sql}
            CREATE OR REPLACE TABLE t1 (a INT, b TEXT);
            INSERT INTO t1 VALUES (1,'b1');
            DELIMITER $$
            BEGIN NOT ATOMIC
              DECLARE r ROW TYPE OF t1;
              SELECT * INTO r FROM t1 WHERE a=1;
              SELECT r.a, r.b;
            END;
            $$
            DELIMITER ;
            {code}
            {noformat}
            +------+------+
            | r.a | r.b |
            +------+------+
            | 1 | b1 |
            +------+------+
            {noformat}
            so far so good.


            However if I modify the script slightly and replace SELECT..INTO to an equivalent SET, it does not work:
            {code:sql}
            CREATE OR REPLACE TABLE t1 (a INT, b TEXT);
            INSERT INTO t1 VALUES (1,'b1');
            DELIMITER $$
            BEGIN NOT ATOMIC
              DECLARE r ROW TYPE OF t1;
              SET r=(SELECT * FROM t1 WHERE a=1);
              SELECT r.a, r.b;
            END;
            $$
            DELIMITER ;
            {code}
            {noformat}
            +------+------+
            | r.a | r.b |
            +------+------+
            | NULL | NULL |
            +------+------+
            {noformat}


            This equivalent script with a subselect in the DEFAULT clause of a variable declaration also does not work:
            {code:sql}
            CREATE OR REPLACE TABLE t1 (a INT, b TEXT);
            INSERT INTO t1 VALUES (1,'b1');
            DELIMITER $$
            BEGIN NOT ATOMIC
              DECLARE r ROW TYPE OF t1 DEFAULT (SELECT * FROM t1 WHERE a=1);
              SELECT r.a, r.b;
            END;
            $$
            DELIMITER ;
            {code}
            {noformat}
            +------+------+
            | r.a | r.b |
            +------+------+
            | NULL | NULL |
            +------+------+
            {noformat}

            It should be fixed to have the secord and the third script produce equal results to the first script.
            This script using the SELECT..INTO syntax correctly initializes a ROW variable:
            {code:sql}
            CREATE OR REPLACE TABLE t1 (a INT, b TEXT);
            INSERT INTO t1 VALUES (1,'b1');
            DELIMITER $$
            BEGIN NOT ATOMIC
              DECLARE r ROW TYPE OF t1;
              SELECT * INTO r FROM t1 WHERE a=1;
              SELECT r.a, r.b;
            END;
            $$
            DELIMITER ;
            {code}
            {noformat}
            +------+------+
            | r.a | r.b |
            +------+------+
            | 1 | b1 |
            +------+------+
            {noformat}
            so far so good.


            However if I modify the script slightly and replace SELECT..INTO to an equivalent SET, it does not work:
            {code:sql}
            CREATE OR REPLACE TABLE t1 (a INT, b TEXT);
            INSERT INTO t1 VALUES (1,'b1');
            DELIMITER $$
            BEGIN NOT ATOMIC
              DECLARE r ROW TYPE OF t1;
              SET r=(SELECT * FROM t1 WHERE a=1);
              SELECT r.a, r.b;
            END;
            $$
            DELIMITER ;
            {code}
            {noformat}
            +------+------+
            | r.a | r.b |
            +------+------+
            | NULL | NULL |
            +------+------+
            {noformat}

            Another equivalent script with a subselect in the DEFAULT clause of a variable declaration also does not work:
            {code:sql}
            CREATE OR REPLACE TABLE t1 (a INT, b TEXT);
            INSERT INTO t1 VALUES (1,'b1');
            DELIMITER $$
            BEGIN NOT ATOMIC
              DECLARE r ROW TYPE OF t1 DEFAULT (SELECT * FROM t1 WHERE a=1);
              SELECT r.a, r.b;
            END;
            $$
            DELIMITER ;
            {code}
            {noformat}
            +------+------+
            | r.a | r.b |
            +------+------+
            | NULL | NULL |
            +------+------+
            {noformat}

            It should be fixed to have the secord and the third script produce equal results to the first script.
            bar Alexander Barkov made changes -
            Priority Major [ 3 ] Critical [ 2 ]
            bar Alexander Barkov made changes -
            issue.field.resolutiondate 2023-05-12 08:52:37.0 2023-05-12 08:52:37.471
            bar Alexander Barkov made changes -
            Fix Version/s 11.0.2 [ 28706 ]
            Fix Version/s 11.1.1 [ 28704 ]
            Fix Version/s 10.4.30 [ 28912 ]
            Fix Version/s 10.5.21 [ 28913 ]
            Fix Version/s 10.6.14 [ 28914 ]
            Fix Version/s 10.8.9 [ 28915 ]
            Fix Version/s 10.9.7 [ 28916 ]
            Fix Version/s 10.10.5 [ 28917 ]
            Fix Version/s 10.11.4 [ 28918 ]
            Fix Version/s 10.4 [ 22408 ]
            Resolution Fixed [ 1 ]
            Status Open [ 1 ] Closed [ 6 ]
            julien.fritsch Julien Fritsch made changes -
            Fix Version/s 10.8.9 [ 28915 ]
            julien.fritsch Julien Fritsch made changes -
            Assignee Alexander Barkov [ bar ]
            dbart Daniel Bartholomew made changes -
            Fix Version/s 10.4.31 [ 29010 ]
            Fix Version/s 10.5.22 [ 29011 ]
            Fix Version/s 10.6.15 [ 29013 ]
            Fix Version/s 10.9.8 [ 29015 ]
            Fix Version/s 10.10.6 [ 29017 ]
            Fix Version/s 10.11.5 [ 29019 ]
            Fix Version/s 11.0.3 [ 28920 ]
            Fix Version/s 11.1.2 [ 28921 ]
            Fix Version/s 11.1.1 [ 28704 ]
            Fix Version/s 11.0.2 [ 28706 ]
            Fix Version/s 10.4.30 [ 28912 ]
            Fix Version/s 10.5.21 [ 28913 ]
            Fix Version/s 10.6.14 [ 28914 ]
            Fix Version/s 10.9.7 [ 28916 ]
            Fix Version/s 10.10.5 [ 28917 ]
            Fix Version/s 10.11.4 [ 28918 ]
            bar Alexander Barkov made changes -

            People

              bar Alexander Barkov
              bar Alexander Barkov
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

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