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

Table functions (aka SQL functions returning tables)

Details

    Description

      This is a part of MDEV-5199 (Table functions) which addresses table functions that are defined in SQL (as opposed to table functions that are implemented in C++ and loaded as .so/.dlls).

      This task is a GSOC '15 project of dj not anymore

      Proposed syntax

      (note: the following differs from description in MDEV-5199, and this is the latest text).

      We will use SQL Server's variant of syntax, where CREATE FUNCTION explicitly mentions function name. To define a table function one will use:

      CREATE FUNCTION func_name (args)  RETURNS TABLE table_name (column type [, column type ...])

      Inside function body, table table_name is visible as a temporary table. The table is initially empty. The code of the function is expected to insert rows into table_name. When the function finishes, the table will be returned to outside as the function's result.

      Implementation overview

      Table function will create and populate a temporary table (either HEAP or Aria/MyISAM, just like it is done for other kinds of temporary tables). Once the table function has finished populating the temporary table, it will be returned to the statement that invoked the table function. The caller statement will then read the table.

      In other words, we will not support any kind of "streaming" for results of table function.

      A possible optimization

      Trivial functions (with the body of a single INSERT ... SELECT) can be treated as a VIEW. It is not clear whether this should be implemented in the scope of this WL entry.

      Other details

      Constant parameters

      Table functions must have constant parameters. One can't use things like

      select * from tbl1,  table_func(tbl1.column) where ...

      This is because our implementation will pre-populate table function tables once per query execution. MySQL/MariaDB optimizer doesn't support tables that may change their contents during query execution.

      Parser

      The parser should support all proposed syntax.
      note: the table can only be addressed through an alias. This syntax is not
      allowed:

      SELECT table_func(1,2).column FROM table_func(1,2)

      one must use

      SELECT T_ALIAS.column FROM table_func(1,2) as T_ALIAS

      Name resolution/preparation in the upper query

      When we find a table reference in the FROM clause, we should make
      a lookup in mysql.proc, make appropriate checks, then open and parse
      the function.
      We should be able to take the "RETURNS ...." statement and produce a TABLE
      object from it. This is needed for doing name resolution on the other parts
      of the "caller" query .

      Name resolution/preparation in the Stored Function

      When one defines a stored function:

      CREATE FUNCTION func_name (args)  RETURNS TABLE table_name (column type [, column type ...])
      BEGIN
         stmt1;
         stmt2;
      END

      All statements inside the body of the function (i.e. stmt1, stmt2, etc) must use a modified name resolution process where table table_name resolves to the table that will be returned by the function. This may shadow the real table with name table_name.

      mysql.proc contents

      How does table functions should be stored in mysql.proc table. Are they a special kind of

      Execution

      • "SELECT sp_func()" should return error for table functions

      Attachments

        Issue Links

          Activity

            Updated the description

            psergei Sergei Petrunia added a comment - Updated the description

            The tree for this project is here: https://github.com/djdij123/server.git

            psergei Sergei Petrunia added a comment - The tree for this project is here: https://github.com/djdij123/server.git
            psergei Sergei Petrunia added a comment - - edited

            Closed the subtasks as nobody is working on the patch currently. This task itself remains open in case somebody wants to pick it up.

            psergei Sergei Petrunia added a comment - - edited Closed the subtasks as nobody is working on the patch currently. This task itself remains open in case somebody wants to pick it up.

            Please consider implementing tables as a new data type, so one can declare variables of the new TABLE data type and pass those variables as arguments to stored routines. This is needed for the Oracle compatibility project.

            Something like this would be nice:

            DELIMITER $$
            CREATE PROCEDURE p1(t1 TABLE(a INT, b VARCHAR(32))
            BEGIN
              SELECT * FROM t1;
              INSERT INTO t2 SELECT * FROM t1;
            END;
            $$
            CREATE PROCEDURE p2
            BEGIN
              DECLARE t1 TABLE(a INT, b VARCHAR(32));
              INSERT INTO t1 VALUES (1,'b1');
              CALL p1(t1);
            END;
            $$
            CALL p2();
            

            bar Alexander Barkov added a comment - Please consider implementing tables as a new data type, so one can declare variables of the new TABLE data type and pass those variables as arguments to stored routines. This is needed for the Oracle compatibility project. Something like this would be nice: DELIMITER $$ CREATE PROCEDURE p1(t1 TABLE (a INT , b VARCHAR (32)) BEGIN SELECT * FROM t1; INSERT INTO t2 SELECT * FROM t1; END ; $$ CREATE PROCEDURE p2 BEGIN DECLARE t1 TABLE (a INT , b VARCHAR (32)); INSERT INTO t1 VALUES (1, 'b1' ); CALL p1(t1); END ; $$ CALL p2();

            Note, the bb-10.2-compatibility tree has a support for the ROW data type. The ROW data type can be used for local variables and parameter.

            See "MDEV-10914 ROW data type for stored routine variables" for details, as well as tests in mysql-test/t/sp-row.test.

            Adding the TABLE data type can reuse ideas from the patch implementing MDEV-10914.

            Note, returning ROW variables from functions is not implemented yet. But this is mostly because of the parser related challenges, as as soon as we return ROW from a function, we need to support this:

            SELECT f1().x FROM DUAL;
            

            Otherwise adding ROW as a function return data type is easy. So there should not be any problems to implement TABLE as a function return value as soon as you have TABLE as a variable/parameter data type.

            bar Alexander Barkov added a comment - Note, the bb-10.2-compatibility tree has a support for the ROW data type. The ROW data type can be used for local variables and parameter. See " MDEV-10914 ROW data type for stored routine variables " for details, as well as tests in mysql-test/t/sp-row.test . Adding the TABLE data type can reuse ideas from the patch implementing MDEV-10914 . Note, returning ROW variables from functions is not implemented yet. But this is mostly because of the parser related challenges, as as soon as we return ROW from a function, we need to support this: SELECT f1().x FROM DUAL; Otherwise adding ROW as a function return data type is easy. So there should not be any problems to implement TABLE as a function return value as soon as you have TABLE as a variable/parameter data type.

            People

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