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

"Table Does Not Exist" Error from Recursive CTE Query Inside Function

Details

    Description

      I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

      Here is the exact error if that is helpful:

      ERROR 1146 (42S02) at line 33: Table 'ck2.Characters' doesn't exist

      I've attached the full SQL, but this is a isolated example that has the same problem:

      create or replace database ck2;
      use ck2;
       
      create table Characters (
          id int unsigned key,
       
          mother_id int unsigned check (mother_id != id),
          foreign key (mother_id)
              references Characters(id)
              on delete cascade,
       
          real_father_id int unsigned check(real_father_id != id),
          foreign key (real_father_id)
              references Characters(id)
              on delete cascade
      );
       
      insert into Characters values
          (0, null, null),
          (1, 0, null),
          (2, 1, null)
      ;
       
      create or replace function count_descendants(cid int unsigned) returns int unsigned return (
          with recursive Descendants as (
              select * from Characters where id = cid
              union
              select c.* from Characters as c, Descendants as d        
                  where d.id = c.mother_id or d.id = c.real_father_id
          ) select count(distinct(id)) from Descendants
          );
       
      select count_descendants(0);
      

      Attachments

        Issue Links

          Activity

            iguessthislldo Fred Hornsey created issue -
            iguessthislldo Fred Hornsey made changes -
            Field Original Value New Value
            Description I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

            Here is the exact error if that is helpful:

            {{ERROR 1146 (42S02) at line 12: Table 'ck2.Characters' doesn't exist}}

            I've attached the sql file I use to create the database and a separate sql file that contains the function and a attempt at using it.
            I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

            Here is the exact error if that is helpful:

            {{ERROR 1146 (42S02) at line 12: Table 'ck2.Characters' doesn't exist}}

            I've attached the sql file I use to create the database and a separate sql file that contains the function and a attempt at using it.

            {{use ck2;

            create or replace function count_descendants(cid int unsigned) returns int unsigned return (
                with recursive Descendants as (
                    select * from Characters where id = cid
                    union
                    
                        where d.id = c.mother_id or d.id = c.real_father_id
                ) select count(distinct(id)) from Descendants
                );

            select count_descendants(91402);

            drop function count_descendants;
            }}
            iguessthislldo Fred Hornsey made changes -
            Description I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

            Here is the exact error if that is helpful:

            {{ERROR 1146 (42S02) at line 12: Table 'ck2.Characters' doesn't exist}}

            I've attached the sql file I use to create the database and a separate sql file that contains the function and a attempt at using it.

            {{use ck2;

            create or replace function count_descendants(cid int unsigned) returns int unsigned return (
                with recursive Descendants as (
                    select * from Characters where id = cid
                    union
                    
                        where d.id = c.mother_id or d.id = c.real_father_id
                ) select count(distinct(id)) from Descendants
                );

            select count_descendants(91402);

            drop function count_descendants;
            }}
            I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

            Here is the exact error if that is helpful:

            {{ERROR 1146 (42S02) at line 12: Table 'ck2.Characters' doesn't exist}}

            I've attached the sql file I use to create the database and a separate sql file that contains the function and a attempt at using it.
            iguessthislldo Fred Hornsey made changes -
            Description I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

            Here is the exact error if that is helpful:

            {{ERROR 1146 (42S02) at line 12: Table 'ck2.Characters' doesn't exist}}

            I've attached the sql file I use to create the database and a separate sql file that contains the function and a attempt at using it.
            I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

            Here is the exact error if that is helpful:

            {{ERROR 1146 (42S02) at line 33: Table 'ck2.Characters' doesn't exist}}

            I've attached the full SQL, but this is a isolated example that has the same problem:
            {{
            create or replace database ck2;
            use ck2;

            create table Characters (
                id int unsigned key,

                mother_id int unsigned check (mother_id != id),
                foreign key (mother_id)
                    references Characters(id)
                    on delete cascade,

                real_father_id int unsigned check(real_father_id != id),
                foreign key (real_father_id)
                    references Characters(id)
                    on delete cascade
            );

            insert into Characters values
                (0, null, null),
                (1, 0, null),
                (2, 1, null)
            ;

            create or replace function count_descendants(cid int unsigned) returns int unsigned return (
                with recursive Descendants as (
                    select * from Characters where id = cid
                    union
                    select c.* from Characters as c, Descendants as d
                        where d.id = c.mother_id or d.id = c.real_father_id
                ) select count(distinct(id)) from Descendants
                );

            select count_descendants(0);
            }}
            iguessthislldo Fred Hornsey made changes -
            Description I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

            Here is the exact error if that is helpful:

            {{ERROR 1146 (42S02) at line 33: Table 'ck2.Characters' doesn't exist}}

            I've attached the full SQL, but this is a isolated example that has the same problem:
            {{
            create or replace database ck2;
            use ck2;

            create table Characters (
                id int unsigned key,

                mother_id int unsigned check (mother_id != id),
                foreign key (mother_id)
                    references Characters(id)
                    on delete cascade,

                real_father_id int unsigned check(real_father_id != id),
                foreign key (real_father_id)
                    references Characters(id)
                    on delete cascade
            );

            insert into Characters values
                (0, null, null),
                (1, 0, null),
                (2, 1, null)
            ;

            create or replace function count_descendants(cid int unsigned) returns int unsigned return (
                with recursive Descendants as (
                    select * from Characters where id = cid
                    union
                    select c.* from Characters as c, Descendants as d
                        where d.id = c.mother_id or d.id = c.real_father_id
                ) select count(distinct(id)) from Descendants
                );

            select count_descendants(0);
            }}
            I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

            Here is the exact error if that is helpful:

            {{ERROR 1146 (42S02) at line 33: Table 'ck2.Characters' doesn't exist}}

            I've attached the full SQL, but this is a isolated example that has the same problem:
            {{create or replace database ck2;
            use ck2;

            create table Characters (
                id int unsigned key,

                mother_id int unsigned check (mother_id != id),
                foreign key (mother_id)
                    references Characters(id)
                    on delete cascade,

                real_father_id int unsigned check(real_father_id != id),
                foreign key (real_father_id)
                    references Characters(id)
                    on delete cascade
            );

            insert into Characters values
                (0, null, null),
                (1, 0, null),
                (2, 1, null)
            ;

            create or replace function count_descendants(cid int unsigned) returns int unsigned return (
                with recursive Descendants as (
                    select * from Characters where id = cid
                    union
                    select c.* from Characters as c, Descendants as d
                        where d.id = c.mother_id or d.id = c.real_father_id
                ) select count(distinct(id)) from Descendants
                );

            select count_descendants(0);}}
            iguessthislldo Fred Hornsey made changes -
            Description I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

            Here is the exact error if that is helpful:

            {{ERROR 1146 (42S02) at line 33: Table 'ck2.Characters' doesn't exist}}

            I've attached the full SQL, but this is a isolated example that has the same problem:
            {{create or replace database ck2;
            use ck2;

            create table Characters (
                id int unsigned key,

                mother_id int unsigned check (mother_id != id),
                foreign key (mother_id)
                    references Characters(id)
                    on delete cascade,

                real_father_id int unsigned check(real_father_id != id),
                foreign key (real_father_id)
                    references Characters(id)
                    on delete cascade
            );

            insert into Characters values
                (0, null, null),
                (1, 0, null),
                (2, 1, null)
            ;

            create or replace function count_descendants(cid int unsigned) returns int unsigned return (
                with recursive Descendants as (
                    select * from Characters where id = cid
                    union
                    select c.* from Characters as c, Descendants as d
                        where d.id = c.mother_id or d.id = c.real_father_id
                ) select count(distinct(id)) from Descendants
                );

            select count_descendants(0);}}
            I'm trying to wrap a recursive cte query inside a function. The query works outside the function and the function statement itself is accepted without error. When the function is used however it reports that the table I'm trying to query inside the function does not exist. A "use" statement is used before the function definition and specifying the database.table combination does not change the result.

            Here is the exact error if that is helpful:

            {{ERROR 1146 (42S02) at line 33: Table 'ck2.Characters' doesn't exist}}

            I've attached the full SQL, but this is a isolated example that has the same problem:

            {code:sql}
            create or replace database ck2;
            use ck2;

            create table Characters (
                id int unsigned key,

                mother_id int unsigned check (mother_id != id),
                foreign key (mother_id)
                    references Characters(id)
                    on delete cascade,

                real_father_id int unsigned check(real_father_id != id),
                foreign key (real_father_id)
                    references Characters(id)
                    on delete cascade
            );

            insert into Characters values
                (0, null, null),
                (1, 0, null),
                (2, 1, null)
            ;

            create or replace function count_descendants(cid int unsigned) returns int unsigned return (
                with recursive Descendants as (
                    select * from Characters where id = cid
                    union
                    select c.* from Characters as c, Descendants as d
                        where d.id = c.mother_id or d.id = c.real_father_id
                ) select count(distinct(id)) from Descendants
                );

            select count_descendants(0);
            {code}
            alice Alice Sherepa made changes -
            Assignee Alice Sherepa [ alice ]
            alice Alice Sherepa added a comment -

            Thanks for the report! Reproduced on 10.2 commit 8639e288086247ce39917f4cb55191c8bb5b5a8c, 10.3

            CREATE or replace TABLE t1 (id int);
            INSERT INTO t1 VALUES (0), (1),(2);
             
            WITH recursive cte AS (SELECT id from t1 union select 3 from cte) SELECT count(id) FROM cte;
             
            CREATE OR REPLACE FUNCTION func() RETURNS int RETURN (
            WITH recursive cte AS (SELECT id from t1 union select 3 from cte) SELECT count(id) FROM cte);
             
            SELECT func();
            

            CREATE or replace TABLE t1 (id int);
            INSERT INTO t1 VALUES (0), (1),(2);
            WITH recursive cte AS (SELECT id from t1 union select 3 from cte) SELECT count(id) FROM cte;
            count(id)
            4
            CREATE OR REPLACE FUNCTION func() RETURNS int RETURN (
            WITH recursive cte AS (SELECT id from t1 union select 3 from cte) SELECT count(id) FROM cte);
            SELECT func();
            main.1_my                                [ fail ]
                    Test ended at 2018-07-02 14:51:10
             
            CURRENT_TEST: main.1_my
            mysqltest: At line 9: query 'SELECT func()' failed: 1146: Table 'test.t1' doesn't exist
            
            

            alice Alice Sherepa added a comment - Thanks for the report! Reproduced on 10.2 commit 8639e288086247ce39917f4cb55191c8bb5b5a8c, 10.3 CREATE or replace TABLE t1 (id int ); INSERT INTO t1 VALUES (0), (1),(2);   WITH recursive cte AS ( SELECT id from t1 union select 3 from cte) SELECT count (id) FROM cte;   CREATE OR REPLACE FUNCTION func() RETURNS int RETURN ( WITH recursive cte AS ( SELECT id from t1 union select 3 from cte) SELECT count (id) FROM cte); SELECT func(); CREATE or replace TABLE t1 (id int); INSERT INTO t1 VALUES (0), (1),(2); WITH recursive cte AS (SELECT id from t1 union select 3 from cte) SELECT count(id) FROM cte; count(id) 4 CREATE OR REPLACE FUNCTION func() RETURNS int RETURN ( WITH recursive cte AS (SELECT id from t1 union select 3 from cte) SELECT count(id) FROM cte); SELECT func(); main.1_my [ fail ] Test ended at 2018-07-02 14:51:10   CURRENT_TEST: main.1_my mysqltest: At line 9: query 'SELECT func()' failed: 1146: Table 'test.t1' doesn't exist
            alice Alice Sherepa made changes -
            alice Alice Sherepa made changes -
            Fix Version/s 10.2 [ 14601 ]
            alice Alice Sherepa made changes -
            Status Open [ 1 ] Confirmed [ 10101 ]
            alice Alice Sherepa made changes -
            Assignee Alice Sherepa [ alice ] Igor Babaev [ igor ]
            igor Igor Babaev (Inactive) made changes -
            Status Confirmed [ 10101 ] In Progress [ 3 ]

            A fix for this bug was pushed into 10.2

            igor Igor Babaev (Inactive) added a comment - A fix for this bug was pushed into 10.2
            igor Igor Babaev (Inactive) made changes -
            Fix Version/s 10.2.17 [ 23111 ]
            Fix Version/s 10.2 [ 14601 ]
            Resolution Fixed [ 1 ]
            Status In Progress [ 3 ] Closed [ 6 ]
            Jon Kilgallon Jon Kilgallon made changes -
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 88145 ] MariaDB v4 [ 154594 ]

            People

              igor Igor Babaev (Inactive)
              iguessthislldo Fred Hornsey
              Votes:
              1 Vote for this issue
              Watchers:
              5 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.