3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is recursive common table expressions with
13 # multiple recursive terms in the compound select.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 set ::testprefix with5
26 CREATE TABLE link(aa INT, bb INT);
27 CREATE INDEX link_f ON link(aa,bb);
28 CREATE INDEX link_t ON link(bb,aa);
29 INSERT INTO link(aa,bb) VALUES
42 WITH RECURSIVE closure(x) AS (
45 SELECT aa FROM closure, link WHERE link.bb=closure.x
47 SELECT bb FROM closure, link WHERE link.aa=closure.x
49 SELECT x FROM closure ORDER BY x;
52 WITH RECURSIVE closure(x) AS (
55 SELECT aa FROM link, closure WHERE link.bb=closure.x
57 SELECT bb FROM closure, link WHERE link.aa=closure.x
59 SELECT x FROM closure ORDER BY x;
62 WITH RECURSIVE closure(x) AS (
65 SELECT bb FROM closure, link WHERE link.aa=closure.x
67 SELECT aa FROM link, closure WHERE link.bb=closure.x
69 SELECT x FROM closure ORDER BY x;
72 WITH RECURSIVE closure(x) AS (
73 VALUES(1),(200),(300),(400)
77 SELECT bb FROM closure, link WHERE link.aa=closure.x
79 SELECT aa FROM link, closure WHERE link.bb=closure.x
81 SELECT x FROM closure ORDER BY x;
84 WITH RECURSIVE closure(x) AS (
85 VALUES(1),(200),(300),(400)
89 SELECT bb FROM closure, link WHERE link.aa=closure.x
91 SELECT aa FROM link, closure WHERE link.bb=closure.x
93 SELECT x FROM closure ORDER BY x;
94 } {1 2 3 4 5 6 7 8 9 11 200 300 400}
96 do_catchsql_test 120 {
97 WITH RECURSIVE closure(x) AS (
98 VALUES(1),(200),(300),(400)
102 SELECT bb FROM closure, link WHERE link.aa=closure.x
104 SELECT aa FROM link, closure WHERE link.bb=closure.x
106 SELECT x FROM closure ORDER BY x;
107 } {1 {circular reference: closure}}
108 do_catchsql_test 121 {
109 WITH RECURSIVE closure(x) AS (
110 VALUES(1),(200),(300),(400)
114 SELECT bb FROM closure, link WHERE link.aa=closure.x
116 SELECT aa FROM link, closure WHERE link.bb=closure.x
118 SELECT x FROM closure ORDER BY x;
119 } {1 {circular reference: closure}}
121 do_execsql_test 130 {
122 WITH RECURSIVE closure(x) AS (
125 SELECT aa FROM link JOIN closure ON bb=x
127 SELECT bb FROM link JOIN closure on aa=x
130 SELECT * FROM closure;
132 do_execsql_test 131 {
133 WITH RECURSIVE closure(x) AS (
138 SELECT aa FROM link JOIN closure ON bb=x
140 SELECT bb FROM link JOIN closure on aa=x
143 SELECT * FROM closure;
146 do_execsql_test 200 {
147 CREATE TABLE linkA(aa1,aa2);
148 INSERT INTO linkA(aa1,aa2) VALUES(1,3),(5,7),(9,11);
149 CREATE TABLE linkB(bb1,bb2);
150 INSERT INTO linkB(bb1,bb2) VALUES(7,9),(11,13),(3,5);
151 CREATE TABLE linkC(cc1,cc2);
152 INSERT INTO linkC(cc1,cc2) VALUES(1,2),(2,4),(6,8);
153 CREATE TABLE linkD(dd1,dd2);
154 INSERT INTO linkD(dd1,dd2) VALUES(4,6),(100,110);
156 do_execsql_test 210 {
157 WITH RECURSIVE closure(x) AS (
160 SELECT aa2 FROM linkA JOIN closure ON x=aa1
162 SELECT bb2 FROM linkB JOIN closure ON x=bb1
164 SELECT cc2 FROM linkC JOIN closure ON x=cc1
166 SELECT dd2 FROM linkD JOIN closure ON x=dd1
168 SELECT x FROM closure ORDER BY +x;
169 } {1 2 3 4 5 6 7 8 9 11 13}
170 do_execsql_test 220 {
171 CREATE TABLE linkA_ipk(aa1 INTEGER PRIMARY KEY,aa2);
172 INSERT INTO linkA_ipk(aa1,aa2) SELECT aa1, aa2 FROM linkA;
173 CREATE TABLE linkB_ipk(bb1 INTEGER PRIMARY KEY,bb2);
174 INSERT INTO linkB_ipk(bb1,bb2) SELECT bb1, bb2 FROM linkB;
175 CREATE TABLE linkC_ipk(cc1 INTEGER PRIMARY KEY,cc2);
176 INSERT INTO linkC_ipk(cc1,cc2) SELECT cc1, cc2 FROM linkC;
177 CREATE TABLE linkD_ipk(dd1 INTEGER PRIMARY KEY,dd2);
178 INSERT INTO linkD_ipk(dd1,dd2) SELECT dd1, dd2 FROM linkD;
179 WITH RECURSIVE closure(x) AS (
182 SELECT aa2 FROM linkA_ipk JOIN closure ON x=aa1
184 SELECT bb2 FROM linkB_ipk JOIN closure ON x=bb1
186 SELECT cc2 FROM linkC_ipk JOIN closure ON x=cc1
188 SELECT dd2 FROM linkD_ipk JOIN closure ON x=dd1
190 SELECT x FROM closure ORDER BY +x;
191 } {1 2 3 4 5 6 7 8 9 11 13}