Details
-
Bug
-
Status: Closed (View Workflow)
-
Major
-
Resolution: Fixed
-
10.3.7
-
Ubuntu 18.04 x86_64, MariaDB was downloaded through MariaDB apt repo
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
- relates to
-
MDEV-16661 Query with recursive cte inside stored function hangs
-
- Closed
-
-
MDEV-25464 Joining CTEs inside a function fails with error code 1146 despite the tables existing
-
- Closed
-
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;
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