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

CREATE PROCEDURE returns no error when using an unknown variable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Fixed
    • 5.5(EOL), 10.0(EOL), 10.1(EOL), 10.2(EOL), 10.3(EOL), 10.4(EOL)
    • 10.5.4
    • Stored routines
    • None

    Description

      DELIMITER $$
      CREATE OR REPLACE PROCEDURE p1(a INT)
      BEGIN
        DECLARE res INT DEFAULT 0;
        IF (a < 0) THEN
          SET res=x;
        END IF;
      END;
      $$
      DELIMITER ;
      

      Now if I call the procedure as follows:

      CALL p1(0);
      

      it still returns no errors.

      It only returns an error if the condition in IF evaluates to true:

      call p1(-1);
      

      ERROR 1054 (42S22): Unknown column 'x' in 'field list'
      

      This behavior makes it very difficult to design stored procedures.
      The error should happen during CREATE PROCEDURE time.

      In this context, x can only be a variable. It cannot be a column because no tables are involved.

      Attachments

        Issue Links

          Activity

            bar Alexander Barkov created issue -
            bar Alexander Barkov made changes -
            Field Original Value New Value
            Rank Ranked higher
            bar Alexander Barkov made changes -
            Description {code:sql}
            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1(a INT)
            BEGIN
              DECLARE res INT DEFAULT 0;
              IF (a < 0) THEN
                SET res=x;
              END IF;
            END;
            $$
            DELIMITER ;
            {code}
            Now if I call the procedure as follows:
            {code:sql}
            CALL p1(0);
            {code}
            it still returns no errors.

            It only returns an error if the condition in {{IF}} evaluates to {{true}}:
            {code:sql
            call p(-1);
            {code}
            {noformat}
            ERROR 1054 (42S22): Unknown column 'x' in 'field list'
            {noformat}

            This behavior makes it very difficult to design stored procedures.
            The error should happen during {{CREATE PROCEDURE}} time.

            In this context, {{x}} can only be a variable. It cannot be a column because no tables are involved.
            {code:sql}
            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1(a INT)
            BEGIN
              DECLARE res INT DEFAULT 0;
              IF (a < 0) THEN
                SET res=x;
              END IF;
            END;
            $$
            DELIMITER ;
            {code}
            Now if I call the procedure as follows:
            {code:sql}
            CALL p1(0);
            {code}
            it still returns no errors.

            It only returns an error if the condition in {{IF}} evaluates to {{true}}:
            {code:sql}
            call p(-1);
            {code}
            {noformat}
            ERROR 1054 (42S22): Unknown column 'x' in 'field list'
            {noformat}

            This behavior makes it very difficult to design stored procedures.
            The error should happen during {{CREATE PROCEDURE}} time.

            In this context, {{x}} can only be a variable. It cannot be a column because no tables are involved.
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            Status Open [ 1 ] In Progress [ 3 ]
            bar Alexander Barkov made changes -
            Fix Version/s 10.4 [ 22408 ]
            Fix Version/s 10.3 [ 22126 ]
            bar Alexander Barkov made changes -
            Description {code:sql}
            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1(a INT)
            BEGIN
              DECLARE res INT DEFAULT 0;
              IF (a < 0) THEN
                SET res=x;
              END IF;
            END;
            $$
            DELIMITER ;
            {code}
            Now if I call the procedure as follows:
            {code:sql}
            CALL p1(0);
            {code}
            it still returns no errors.

            It only returns an error if the condition in {{IF}} evaluates to {{true}}:
            {code:sql}
            call p(-1);
            {code}
            {noformat}
            ERROR 1054 (42S22): Unknown column 'x' in 'field list'
            {noformat}

            This behavior makes it very difficult to design stored procedures.
            The error should happen during {{CREATE PROCEDURE}} time.

            In this context, {{x}} can only be a variable. It cannot be a column because no tables are involved.
            {code:sql}
            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1(a INT)
            BEGIN
              DECLARE res INT DEFAULT 0;
              IF (a < 0) THEN
                SET res=x;
              END IF;
            END;
            $$
            DELIMITER ;
            {code}
            Now if I call the procedure as follows:
            {code:sql}
            CALL p1(0);
            {code}
            it still returns no errors.

            It only returns an error if the condition in {{IF}} evaluates to {{true}}:
            {code:sql}
            call p1(-1);
            {code}
            {noformat}
            ERROR 1054 (42S22): Unknown column 'x' in 'field list'
            {noformat}

            This behavior makes it very difficult to design stored procedures.
            The error should happen during {{CREATE PROCEDURE}} time.

            In this context, {{x}} can only be a variable. It cannot be a column because no tables are involved.
            bar Alexander Barkov made changes -
            Fix Version/s 10.5 [ 23123 ]
            Fix Version/s 10.4 [ 22408 ]
            bar Alexander Barkov made changes -
            Affects Version/s 10.4 [ 22408 ]
            bar Alexander Barkov made changes -
            Status In Progress [ 3 ] Stalled [ 10000 ]
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            Comment [ The same problem is repeatable with aggregate functions:
            {code:sql}
            CREATE OR REPLACE FUNCTION f1() RETURNS INT RETURN SUM(1);
            SELECT f1();
            {code}
            {noformat}
            ERROR 1111 (HY000): Invalid use of group function
            {noformat}

            And window functions:
            {code:sql}
            CREATE OR REPLACE FUNCTION f1() RETURNS INT RETURN ROW_NUMBER() OVER();
            SELECT f1();
            {code}
            {noformat}
            ERROR 4015 (HY000): Window function is allowed only in SELECT list and ORDER BY clause
            {noformat}

            It should report the error at creation rather than execution time.
            ]
            bar Alexander Barkov made changes -
            Assignee Alexander Barkov [ bar ] Oleksandr Byelkin [ sanja ]
            bar Alexander Barkov made changes -
            Assignee Oleksandr Byelkin [ sanja ] Alexander Barkov [ bar ]
            bar Alexander Barkov added a comment - - edited

            More examples when an error is not returned:

            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1(a INT)
            BEGIN
              DECLARE res INT DEFAULT 0;
              SET res=(SELECT 1 WHERE unknown);
            END;
            $$
            DELIMITER ;
            

            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1(a INT)
            BEGIN
              DECLARE res INT DEFAULT 0;
              SET res=(SELECT 1 HAVING unknown);
            END;
            $$
            DELIMITER ;
            

            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1(a INT)
            BEGIN
              DECLARE res INT DEFAULT 0;
              SET res=(SELECT 1 GROUP BY unknown);
            END;
            $$
            DELIMITER ;
            

            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1(a INT)
            BEGIN
              DECLARE res INT DEFAULT 0;
              SET res=(SELECT 1 ORDER BY unknown);
            END;
            $$
            DELIMITER ;
            

            bar Alexander Barkov added a comment - - edited More examples when an error is not returned: DELIMITER $$ CREATE OR REPLACE PROCEDURE p1(a INT ) BEGIN DECLARE res INT DEFAULT 0; SET res=( SELECT 1 WHERE unknown); END ; $$ DELIMITER ; DELIMITER $$ CREATE OR REPLACE PROCEDURE p1(a INT ) BEGIN DECLARE res INT DEFAULT 0; SET res=( SELECT 1 HAVING unknown); END ; $$ DELIMITER ; DELIMITER $$ CREATE OR REPLACE PROCEDURE p1(a INT ) BEGIN DECLARE res INT DEFAULT 0; SET res=( SELECT 1 GROUP BY unknown); END ; $$ DELIMITER ; DELIMITER $$ CREATE OR REPLACE PROCEDURE p1(a INT ) BEGIN DECLARE res INT DEFAULT 0; SET res=( SELECT 1 ORDER BY unknown); END ; $$ DELIMITER ;
            bar Alexander Barkov added a comment - - edited

            More examples with TVC when no error is returned:

            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1(a INT)
            BEGIN
              DECLARE res INT DEFAULT 0;
              SET res=(VALUES(xxx));
            END;
            $$
            DELIMITER ;
            

            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1(a INT)
            BEGIN
              DECLARE res INT DEFAULT 0;
              SET res=(VALUES(1) ORDER BY yyy);
              SELECT res;
            END;
            $$
            DELIMITER ;
            

            bar Alexander Barkov added a comment - - edited More examples with TVC when no error is returned: DELIMITER $$ CREATE OR REPLACE PROCEDURE p1(a INT ) BEGIN DECLARE res INT DEFAULT 0; SET res=( VALUES (xxx)); END ; $$ DELIMITER ; DELIMITER $$ CREATE OR REPLACE PROCEDURE p1(a INT ) BEGIN DECLARE res INT DEFAULT 0; SET res=( VALUES (1) ORDER BY yyy); SELECT res; END ; $$ DELIMITER ;
            bar Alexander Barkov added a comment - - edited

            More examples with DEFAULT and VALUE

            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1()
            BEGIN
              DECLARE res INT DEFAULT 0;
              SET res=DEFAULT(unknown);
              SELECT res;
            END;
            $$
            DELIMITER ;
            

            DELIMITER $$
            CREATE OR REPLACE PROCEDURE p1()
            BEGIN
              DECLARE res INT DEFAULT 0;
              SET res=VALUE(unknown);
              SELECT res;
            END;
            $$
            DELIMITER ;
            

            bar Alexander Barkov added a comment - - edited More examples with DEFAULT and VALUE DELIMITER $$ CREATE OR REPLACE PROCEDURE p1() BEGIN DECLARE res INT DEFAULT 0; SET res= DEFAULT (unknown); SELECT res; END ; $$ DELIMITER ; DELIMITER $$ CREATE OR REPLACE PROCEDURE p1() BEGIN DECLARE res INT DEFAULT 0; SET res=VALUE(unknown); SELECT res; END ; $$ DELIMITER ;
            bar Alexander Barkov added a comment - sanja , can you please review a patch? https://github.com/MariaDB/server/commit/06942363ebc847c5cd16f4b8e610451d3c8eeb3f Thanks.
            bar Alexander Barkov made changes -
            Status Stalled [ 10000 ] In Progress [ 3 ]
            bar Alexander Barkov made changes -
            Assignee Alexander Barkov [ bar ] Oleksandr Byelkin [ sanja ]
            Status In Progress [ 3 ] In Review [ 10002 ]

            OK, to push

            sanja Oleksandr Byelkin added a comment - OK, to push
            sanja Oleksandr Byelkin made changes -
            Status In Review [ 10002 ] Stalled [ 10000 ]

            igor it looks like Item::walk do not check TVC now even if asked to go inside subqueries (see the patch)

            sanja Oleksandr Byelkin added a comment - igor it looks like Item::walk do not check TVC now even if asked to go inside subqueries (see the patch)
            sanja Oleksandr Byelkin made changes -
            Assignee Oleksandr Byelkin [ sanja ] Alexander Barkov [ bar ]
            bar Alexander Barkov made changes -
            issue.field.resolutiondate 2020-06-10 14:39:30.0 2020-06-10 14:39:30.07
            bar Alexander Barkov made changes -
            Fix Version/s 10.5.4 [ 24264 ]
            Fix Version/s 10.5 [ 23123 ]
            Resolution Fixed [ 1 ]
            Status Stalled [ 10000 ] Closed [ 6 ]
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 83736 ] MariaDB v4 [ 153158 ]

            People

              bar Alexander Barkov
              bar Alexander Barkov
              Votes:
              0 Vote for this issue
              Watchers:
              3 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.