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);
|
{"report":{"fcp":1229.7000002861023,"ttfb":366,"pageVisibility":"visible","entityId":68435,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"ceaa0377-83a5-4013-bf77-1f30fa0dab4b","navigationType":0,"readyForUser":1329,"redirectCount":0,"resourceLoadedEnd":1411,"resourceLoadedStart":373.1000003814697,"resourceTiming":[{"duration":346.8999996185303,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":373.1000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":373.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":720,"responseStart":0,"secureConnectionStart":0},{"duration":348.59999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/2bf333562ca6724060a9d5f1535471f6/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":373.30000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":373.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":721.9000000953674,"responseStart":0,"secureConnectionStart":0},{"duration":357.7000002861023,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":373.5,"connectEnd":373.5,"connectStart":373.5,"domainLookupEnd":373.5,"domainLookupStart":373.5,"fetchStart":373.5,"redirectEnd":0,"redirectStart":0,"requestStart":373.5,"responseEnd":731.2000002861023,"responseStart":731.2000002861023,"secureConnectionStart":373.5},{"duration":423.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/2bf333562ca6724060a9d5f1535471f6/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":373.6000003814697,"connectEnd":373.6000003814697,"connectStart":373.6000003814697,"domainLookupEnd":373.6000003814697,"domainLookupStart":373.6000003814697,"fetchStart":373.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":373.6000003814697,"responseEnd":796.7000002861023,"responseStart":796.7000002861023,"secureConnectionStart":373.6000003814697},{"duration":426.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":373.90000009536743,"connectEnd":373.90000009536743,"connectStart":373.90000009536743,"domainLookupEnd":373.90000009536743,"domainLookupStart":373.90000009536743,"fetchStart":373.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":373.90000009536743,"responseEnd":800.8000001907349,"responseStart":800.8000001907349,"secureConnectionStart":373.90000009536743},{"duration":428.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":374.1000003814697,"connectEnd":374.1000003814697,"connectStart":374.1000003814697,"domainLookupEnd":374.1000003814697,"domainLookupStart":374.1000003814697,"fetchStart":374.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":374.1000003814697,"responseEnd":802.3000001907349,"responseStart":802.3000001907349,"secureConnectionStart":374.1000003814697},{"duration":428.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":374.30000019073486,"connectEnd":374.30000019073486,"connectStart":374.30000019073486,"domainLookupEnd":374.30000019073486,"domainLookupStart":374.30000019073486,"fetchStart":374.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":374.30000019073486,"responseEnd":803.1000003814697,"responseStart":803.1000003814697,"secureConnectionStart":374.30000019073486},{"duration":505.09999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":374.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":374.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":879.5,"responseStart":0,"secureConnectionStart":0},{"duration":429.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":374.6000003814697,"connectEnd":374.6000003814697,"connectStart":374.6000003814697,"domainLookupEnd":374.6000003814697,"domainLookupStart":374.6000003814697,"fetchStart":374.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":374.6000003814697,"responseEnd":804.2000002861023,"responseStart":804.2000002861023,"secureConnectionStart":374.6000003814697},{"duration":504.90000009536743,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":374.7000002861023,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":374.7000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":879.6000003814697,"responseStart":0,"secureConnectionStart":0},{"duration":431.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":374.90000009536743,"connectEnd":374.90000009536743,"connectStart":374.90000009536743,"domainLookupEnd":374.90000009536743,"domainLookupStart":374.90000009536743,"fetchStart":374.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":374.90000009536743,"responseEnd":806,"responseStart":806,"secureConnectionStart":374.90000009536743},{"duration":698.9000000953674,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":375.90000009536743,"connectEnd":375.90000009536743,"connectStart":375.90000009536743,"domainLookupEnd":375.90000009536743,"domainLookupStart":375.90000009536743,"fetchStart":375.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":375.90000009536743,"responseEnd":1074.8000001907349,"responseStart":1074.8000001907349,"secureConnectionStart":375.90000009536743},{"duration":934.8000001907349,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":375.90000009536743,"connectEnd":375.90000009536743,"connectStart":375.90000009536743,"domainLookupEnd":375.90000009536743,"domainLookupStart":375.90000009536743,"fetchStart":375.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":375.90000009536743,"responseEnd":1310.7000002861023,"responseStart":1310.7000002861023,"secureConnectionStart":375.90000009536743},{"duration":191.59999990463257,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":893.1000003814697,"connectEnd":893.1000003814697,"connectStart":893.1000003814697,"domainLookupEnd":893.1000003814697,"domainLookupStart":893.1000003814697,"fetchStart":893.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":893.1000003814697,"responseEnd":1084.7000002861023,"responseStart":1084.7000002861023,"secureConnectionStart":893.1000003814697},{"duration":233.40000009536743,"initiatorType":"link","name":"https://jira.mariadb.org/s/50bc9be5bfead1a25e72c1a9338c94f6-CDN/lu2cib/820016/12ta74/e108c7645258ccb43280ed3404e3e949/_/download/contextbatch/css/com.atlassian.jira.plugins.jira-development-integration-plugin:0,-_super,-jira.view.issue,-jira.global,-jira.general,-jira.browse.project,-project.issue.navigator,-atl.general/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":1177.4000000953674,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1177.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1410.8000001907349,"responseStart":0,"secureConnectionStart":0},{"duration":233,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2cib/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/css/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":1178,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1178,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1411,"responseStart":0,"secureConnectionStart":0},{"duration":206.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/e65b778d185daf5aee24936755b43da6/_/download/contextbatch/js/browser-metrics-plugin.contrib,-_super,-project.issue.navigator,-jira.view.issue,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":1178.9000000953674,"connectEnd":1178.9000000953674,"connectStart":1178.9000000953674,"domainLookupEnd":1178.9000000953674,"domainLookupStart":1178.9000000953674,"fetchStart":1178.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":1178.9000000953674,"responseEnd":1385,"responseStart":1385,"secureConnectionStart":1178.9000000953674},{"duration":208.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/e0bf5781d46ea69fb123572974cf39de-CDN/lu2cib/820016/12ta74/e108c7645258ccb43280ed3404e3e949/_/download/contextbatch/js/com.atlassian.jira.plugins.jira-development-integration-plugin:0,-_super,-jira.view.issue,-jira.global,-jira.general,-jira.browse.project,-project.issue.navigator,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":1179.3000001907349,"connectEnd":1179.3000001907349,"connectStart":1179.3000001907349,"domainLookupEnd":1179.3000001907349,"domainLookupStart":1179.3000001907349,"fetchStart":1179.3000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":1179.3000001907349,"responseEnd":1387.5,"responseStart":1387.5,"secureConnectionStart":1179.3000001907349},{"duration":211.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/097ae97cb8fbec7d6ea4bbb1f26955b9-CDN/lu2cib/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/js/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":1179.7000002861023,"connectEnd":1179.7000002861023,"connectStart":1179.7000002861023,"domainLookupEnd":1179.7000002861023,"domainLookupStart":1179.7000002861023,"fetchStart":1179.7000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":1179.7000002861023,"responseEnd":1391.6000003814697,"responseStart":1391.6000003814697,"secureConnectionStart":1179.7000002861023}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":193,"responseStart":366,"responseEnd":371,"domLoading":370,"domInteractive":1444,"domContentLoadedEventStart":1444,"domContentLoadedEventEnd":1495,"domComplete":1673,"loadEventStart":1673,"loadEventEnd":1674,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1418.3000001907349},{"name":"bigPipe.sidebar-id.end","time":1419.1000003814697},{"name":"bigPipe.activity-panel-pipe-id.start","time":1419.3000001907349},{"name":"bigPipe.activity-panel-pipe-id.end","time":1421.3000001907349},{"name":"activityTabFullyLoaded","time":1514.4000000953674}],"measures":[],"correlationId":"e178c9d9aa2990","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":107,"dbReadsTimeInMs":13,"dbConnsTimeInMs":21,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
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