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 testing the WITH clause.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set ::testprefix with1
25 CREATE TABLE t1(x INTEGER, y INTEGER);
26 WITH x(a) AS ( SELECT * FROM t1) SELECT 10
30 SELECT * FROM ( WITH x AS ( SELECT * FROM t1) SELECT 10 );
34 WITH x(a) AS ( SELECT * FROM t1) INSERT INTO t1 VALUES(1,2);
38 WITH x(a) AS ( SELECT * FROM t1) DELETE FROM t1;
42 WITH x(a) AS ( SELECT * FROM t1) UPDATE t1 SET x = y;
45 #--------------------------------------------------------------------------
48 DROP TABLE IF EXISTS t1;
50 INSERT INTO t1 VALUES(1);
51 INSERT INTO t1 VALUES(2);
52 WITH tmp AS ( SELECT * FROM t1 ) SELECT x FROM tmp;
56 WITH tmp(a) AS ( SELECT * FROM t1 ) SELECT a FROM tmp;
61 WITH tmp(a) AS ( SELECT * FROM t1 ) SELECT a FROM tmp
66 WITH tmp1(a) AS ( SELECT * FROM t1 ),
67 tmp2(x) AS ( SELECT * FROM tmp1)
72 WITH tmp2(x) AS ( SELECT * FROM tmp1),
73 tmp1(a) AS ( SELECT * FROM t1 )
77 #-------------------------------------------------------------------------
78 do_catchsql_test 3.1 {
79 WITH tmp2(x) AS ( SELECT * FROM tmp1 ),
80 tmp1(a) AS ( SELECT * FROM tmp2 )
82 } {1 {circular reference: tmp1}}
84 do_catchsql_test 3.2 {
85 CREATE TABLE t2(x INTEGER);
86 WITH tmp(a) AS (SELECT * FROM t1),
87 tmp(a) AS (SELECT * FROM t1)
89 } {1 {duplicate WITH table name: tmp}}
95 INSERT INTO t3 VALUES('T3');
96 INSERT INTO t4 VALUES('T4');
98 WITH t3(a) AS (SELECT * FROM t4)
102 do_execsql_test 3.4 {
103 WITH tmp AS ( SELECT * FROM t3 ),
104 tmp2 AS ( WITH tmp AS ( SELECT * FROM t4 ) SELECT * FROM tmp )
108 do_execsql_test 3.5 {
109 WITH tmp AS ( SELECT * FROM t3 ),
110 tmp2 AS ( WITH xxxx AS ( SELECT * FROM t4 ) SELECT * FROM tmp )
114 do_catchsql_test 3.6 {
115 WITH tmp AS ( SELECT * FROM t3 ),
117 } {1 {near "SELECT": syntax error}}
119 #-------------------------------------------------------------------------
120 do_execsql_test 4.1 {
121 DROP TABLE IF EXISTS t1;
123 INSERT INTO t1 VALUES(1);
124 INSERT INTO t1 VALUES(2);
125 INSERT INTO t1 VALUES(3);
126 INSERT INTO t1 VALUES(4);
128 WITH dset AS ( SELECT 2 UNION ALL SELECT 4 )
129 DELETE FROM t1 WHERE x IN dset;
133 do_execsql_test 4.2 {
134 WITH iset AS ( SELECT 2 UNION ALL SELECT 4 )
135 INSERT INTO t1 SELECT * FROM iset;
139 do_execsql_test 4.3 {
140 WITH uset(a, b) AS ( SELECT 2, 8 UNION ALL SELECT 4, 9 )
141 UPDATE t1 SET x = COALESCE( (SELECT b FROM uset WHERE a=x), x );
145 #-------------------------------------------------------------------------
147 do_execsql_test 5.1 {
148 WITH i(x) AS ( VALUES(1) UNION ALL SELECT x+1 FROM i)
149 SELECT x FROM i LIMIT 10;
150 } {1 2 3 4 5 6 7 8 9 10}
152 do_catchsql_test 5.2 {
153 WITH i(x) AS ( VALUES(1) UNION ALL SELECT x+1 FROM i ORDER BY 1)
154 SELECT x FROM i LIMIT 10;
155 } {0 {1 2 3 4 5 6 7 8 9 10}}
157 do_execsql_test 5.2.1 {
158 CREATE TABLE edge(xfrom, xto, seq, PRIMARY KEY(xfrom, xto)) WITHOUT ROWID;
159 INSERT INTO edge VALUES(0, 1, 10);
160 INSERT INTO edge VALUES(1, 2, 20);
161 INSERT INTO edge VALUES(0, 3, 30);
162 INSERT INTO edge VALUES(2, 4, 40);
163 INSERT INTO edge VALUES(3, 4, 40);
164 INSERT INTO edge VALUES(2, 5, 50);
165 INSERT INTO edge VALUES(3, 6, 60);
166 INSERT INTO edge VALUES(5, 7, 70);
167 INSERT INTO edge VALUES(3, 7, 70);
168 INSERT INTO edge VALUES(4, 8, 80);
169 INSERT INTO edge VALUES(7, 8, 80);
170 INSERT INTO edge VALUES(8, 9, 90);
176 SELECT edge.xto, edge.seq FROM edge, ancest
177 WHERE edge.xfrom=ancest.id
180 SELECT * FROM ancest;
181 } {0 0 1 10 2 20 3 30 4 40 5 50 6 60 7 70 8 80 9 90}
182 do_execsql_test 5.2.2 {
187 SELECT edge.xto, edge.seq FROM edge, ancest
188 WHERE edge.xfrom=ancest.id
191 SELECT * FROM ancest;
192 } {0 0 1 10 2 20 3 30 4 40 4 40 5 50 6 60 7 70 7 70 8 80 8 80 8 80 8 80 9 90 9 90 9 90 9 90}
193 do_execsql_test 5.2.3 {
198 SELECT edge.xto, edge.seq FROM edge, ancest
199 WHERE edge.xfrom=ancest.id
200 ORDER BY 2 LIMIT 4 OFFSET 2
202 SELECT * FROM ancest;
203 } {2 20 3 30 4 40 4 40}
205 do_catchsql_test 5.3 {
206 WITH i(x) AS ( VALUES(1) UNION ALL SELECT x+1 FROM i LIMIT 5)
210 do_execsql_test 5.4 {
211 WITH i(x) AS ( VALUES(1) UNION ALL SELECT (x+1)%10 FROM i)
212 SELECT x FROM i LIMIT 20;
213 } {1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0}
215 do_execsql_test 5.5 {
216 WITH i(x) AS ( VALUES(1) UNION SELECT (x+1)%10 FROM i)
217 SELECT x FROM i LIMIT 20;
218 } {1 2 3 4 5 6 7 8 9 0}
220 do_catchsql_test 5.6.1 {
221 WITH i(x, y) AS ( VALUES(1) )
223 } {1 {table i has 1 values for 2 columns}}
225 do_catchsql_test 5.6.2 {
226 WITH i(x) AS ( VALUES(1,2) )
228 } {1 {table i has 2 values for 1 columns}}
230 do_catchsql_test 5.6.3 {
231 CREATE TABLE t5(a, b);
232 WITH i(x) AS ( SELECT * FROM t5 )
234 } {1 {table i has 2 values for 1 columns}}
236 do_catchsql_test 5.6.4 {
237 WITH i(x) AS ( SELECT 1, 2 UNION ALL SELECT 1 )
239 } {1 {table i has 2 values for 1 columns}}
241 do_catchsql_test 5.6.5 {
242 WITH i(x) AS ( SELECT 1 UNION ALL SELECT 1, 2 )
244 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
246 do_catchsql_test 5.6.6 {
247 WITH i(x) AS ( SELECT 1 UNION ALL SELECT x+1, x*2 FROM i )
249 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
251 do_catchsql_test 5.6.7 {
252 WITH i(x) AS ( SELECT 1, 2 UNION SELECT x+1 FROM i )
254 } {1 {table i has 2 values for 1 columns}}
256 #-------------------------------------------------------------------------
258 do_execsql_test 6.1 {
260 id INTEGER PRIMARY KEY, parentid REFERENCES f, name TEXT
263 INSERT INTO f VALUES(0, NULL, '');
264 INSERT INTO f VALUES(1, 0, 'bin');
265 INSERT INTO f VALUES(2, 1, 'true');
266 INSERT INTO f VALUES(3, 1, 'false');
267 INSERT INTO f VALUES(4, 1, 'ls');
268 INSERT INTO f VALUES(5, 1, 'grep');
269 INSERT INTO f VALUES(6, 0, 'etc');
270 INSERT INTO f VALUES(7, 6, 'rc.d');
271 INSERT INTO f VALUES(8, 7, 'rc.apache');
272 INSERT INTO f VALUES(9, 7, 'rc.samba');
273 INSERT INTO f VALUES(10, 0, 'home');
274 INSERT INTO f VALUES(11, 10, 'dan');
275 INSERT INTO f VALUES(12, 11, 'public_html');
276 INSERT INTO f VALUES(13, 12, 'index.html');
277 INSERT INTO f VALUES(14, 13, 'logo.gif');
280 do_execsql_test 6.2 {
281 WITH flat(fid, fpath) AS (
282 SELECT id, '' FROM f WHERE parentid IS NULL
284 SELECT id, fpath || '/' || name FROM f, flat WHERE parentid=fid
286 SELECT fpath FROM flat WHERE fpath!='' ORDER BY 1;
289 /bin/false /bin/grep /bin/ls /bin/true
292 /etc/rc.d/rc.apache /etc/rc.d/rc.samba
295 /home/dan/public_html
296 /home/dan/public_html/index.html
297 /home/dan/public_html/index.html/logo.gif
300 do_execsql_test 6.3 {
301 WITH flat(fid, fpath) AS (
302 SELECT id, '' FROM f WHERE parentid IS NULL
304 SELECT id, fpath || '/' || name FROM f, flat WHERE parentid=fid
306 SELECT count(*) FROM flat;
309 do_execsql_test 6.4 {
313 SELECT i+1 FROM x WHERE i<10
315 SELECT count(*) FROM x
319 #-------------------------------------------------------------------------
321 do_execsql_test 7.1 {
322 CREATE TABLE tree(i, p);
323 INSERT INTO tree VALUES(1, NULL);
324 INSERT INTO tree VALUES(2, 1);
325 INSERT INTO tree VALUES(3, 1);
326 INSERT INTO tree VALUES(4, 2);
327 INSERT INTO tree VALUES(5, 4);
330 do_execsql_test 7.2 {
331 WITH t(id, path) AS (
332 SELECT i, '' FROM tree WHERE p IS NULL
334 SELECT i, path || '/' || i FROM tree, t WHERE p = id
337 } {{} /2 /3 /2/4 /2/4/5}
339 do_execsql_test 7.3 {
343 SELECT i FROM tree, t WHERE p = id
348 do_catchsql_test 7.4 {
352 SELECT i FROM tree WHERE p IN (SELECT id FROM t)
355 } {1 {circular reference: t}}
357 do_catchsql_test 7.5 {
361 SELECT i FROM tree, t WHERE p = id AND p IN (SELECT id FROM t)
364 } {1 {multiple recursive references: t}}
366 do_catchsql_test 7.6 {
368 SELECT i FROM tree WHERE 2 IN (SELECT id FROM t)
370 SELECT i FROM tree, t WHERE p = id
373 } {1 {circular reference: t}}
375 # Compute the mandelbrot set using a recursive query
377 do_execsql_test 8.1-mandelbrot {
379 xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+0.05 FROM xaxis WHERE x<1.2),
380 yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+0.1 FROM yaxis WHERE y<1.0),
381 m(iter, cx, cy, x, y) AS (
382 SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis
384 SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m
385 WHERE (x*x + y*y) < 4.0 AND iter<28
387 m2(iter, cx, cy) AS (
388 SELECT max(iter), cx, cy FROM m GROUP BY cx, cy
391 SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '')
394 SELECT group_concat(rtrim(t),x'0a') FROM a;
399 ..##+*##########+.++++
400 .+.##################+.
401 .............+###################+.+
402 ..++..#.....*#####################+.
403 ...+#######++#######################.
404 ....+*################################.
405 #############################################...
406 ....+*################################.
407 ...+#######++#######################.
408 ..++..#.....*#####################+.
409 .............+###################+.+
410 .+.##################+.
411 ..##+*##########+.++++
418 # Solve a sudoku puzzle using a recursive query
420 do_execsql_test 8.2-soduko {
423 VALUES('53..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79')
426 /* A table filled with digits 1..9, inclusive. */
430 CAST(lp+1 AS TEXT), lp+1 FROM digits WHERE lp<9
433 /* The tricky bit. */
435 SELECT sud, instr(sud, '.') FROM input
438 substr(s, 1, ind-1) || z || substr(s, ind+1),
439 instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )
445 WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)
446 OR z.z = substr(s, ((ind-1)%9) + (lp-1)*9 + 1, 1)
447 OR z.z = substr(s, (((ind-1)/3) % 3) * 3
448 + ((ind-1)/27) * 27 + lp
449 + ((lp-1) / 3) * 6, 1)
452 SELECT s FROM x WHERE ind=0;
453 } {534678912672195348198342567859761423426853791713924856961537284287419635345286179}
455 #--------------------------------------------------------------------------
456 # Some tests that use LIMIT and OFFSET in the definition of recursive CTEs.
458 set I [list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
459 proc limit_test {tn iLimit iOffset} {
460 if {$iOffset < 0} { set iOffset 0 }
462 set result [lrange $::I $iOffset end]
464 set result [lrange $::I $iOffset [expr $iLimit+$iOffset-1]]
466 uplevel [list do_execsql_test $tn [subst -nocommands {
470 SELECT a+1 FROM ii WHERE a<20
471 LIMIT $iLimit OFFSET $iOffset
487 #--------------------------------------------------------------------------
488 # Test the ORDER BY clause on recursive tables.
491 do_execsql_test 10.1 {
492 DROP TABLE IF EXISTS tree;
493 CREATE TABLE tree(id INTEGER PRIMARY KEY, parentid, payload);
496 proc insert_into_tree {L} {
497 db eval { DELETE FROM tree }
499 unset -nocomplain parentid
500 foreach seg [split $key /] {
501 if {$seg==""} continue
503 SELECT id FROM tree WHERE parentid IS $parentid AND payload=$seg
506 db eval { INSERT INTO tree VALUES(NULL, $parentid, $seg) }
507 set parentid [db last_insert_rowid]
521 do_execsql_test 10.2 {
522 WITH flat(fid, p) AS (
523 SELECT id, '/' || payload FROM tree WHERE parentid IS NULL
525 SELECT id, p || '/' || payload FROM flat, tree WHERE parentid=fid
527 SELECT p FROM flat ORDER BY p;
534 # Scan the tree-structure currently stored in table tree. Return a list
537 proc scan_tree {bDepthFirst bReverse} {
539 set order "ORDER BY "
540 if {$bDepthFirst==0} { append order "2 ASC," }
542 append order " 3 ASC"
544 append order " 3 DESC"
548 WITH flat(fid, depth, p) AS (
549 SELECT id, 1, '/' || payload FROM tree WHERE parentid IS NULL
551 SELECT id, depth+1, p||'/'||payload FROM flat, tree WHERE parentid=fid
567 # Breadth first, siblings in ascending order.
577 # Depth first, siblings in ascending order.
588 # Breadth first, siblings in descending order.
598 # Depth first, siblings in ascending order.
610 # Test name resolution in ORDER BY clauses.
612 do_catchsql_test 10.7.1 {
614 SELECT 1 AS b UNION ALL SELECT a+1 AS c FROM t WHERE a<5 ORDER BY a
617 } {1 {1st ORDER BY term does not match any column in the result set}}
618 do_execsql_test 10.7.2 {
620 SELECT 1 AS b UNION ALL SELECT a+1 AS c FROM t WHERE a<5 ORDER BY b
624 do_execsql_test 10.7.3 {
626 SELECT 1 AS b UNION ALL SELECT a+1 AS c FROM t WHERE a<5 ORDER BY c
631 # Test COLLATE clauses attached to ORDER BY.
645 do_execsql_test 10.8.1 {
646 WITH flat(fid, depth, p) AS (
647 SELECT id, 1, '/' || payload FROM tree WHERE parentid IS NULL
649 SELECT id, depth+1, p||'/'||payload FROM flat, tree WHERE parentid=fid
650 ORDER BY 2, 3 COLLATE nocase
655 /a/b /a/C /a/d /B/e /B/F /B/g /c/h /c/I /c/j
657 do_execsql_test 10.8.2 {
658 WITH flat(fid, depth, p) AS (
659 SELECT id, 1, ('/' || payload) COLLATE nocase
660 FROM tree WHERE parentid IS NULL
662 SELECT id, depth+1, (p||'/'||payload)
663 FROM flat, tree WHERE parentid=fid
669 /a/b /a/C /a/d /B/e /B/F /B/g /c/h /c/I /c/j
672 do_execsql_test 10.8.3 {
673 WITH flat(fid, depth, p) AS (
674 SELECT id, 1, ('/' || payload)
675 FROM tree WHERE parentid IS NULL
677 SELECT id, depth+1, (p||'/'||payload) COLLATE nocase
678 FROM flat, tree WHERE parentid=fid
684 /a/b /a/C /a/d /B/e /B/F /B/g /c/h /c/I /c/j
687 do_execsql_test 10.8.4.1 {
688 CREATE TABLE tst(a,b);
689 INSERT INTO tst VALUES('a', 'A');
690 INSERT INTO tst VALUES('b', 'B');
691 INSERT INTO tst VALUES('c', 'C');
692 SELECT a COLLATE nocase FROM tst UNION ALL SELECT b FROM tst ORDER BY 1;
694 do_execsql_test 10.8.4.2 {
695 SELECT a FROM tst UNION ALL SELECT b COLLATE nocase FROM tst ORDER BY 1;
697 do_execsql_test 10.8.4.3 {
698 SELECT a||'' FROM tst UNION ALL SELECT b COLLATE nocase FROM tst ORDER BY 1;
701 # Test cases to illustrate on the ORDER BY clause on a recursive query can be
702 # used to control depth-first versus breath-first search in a tree.
704 do_execsql_test 11.1 {
706 name TEXT PRIMARY KEY,
707 boss TEXT REFERENCES org
709 INSERT INTO org VALUES('Alice',NULL);
710 INSERT INTO org VALUES('Bob','Alice');
711 INSERT INTO org VALUES('Cindy','Alice');
712 INSERT INTO org VALUES('Dave','Bob');
713 INSERT INTO org VALUES('Emma','Bob');
714 INSERT INTO org VALUES('Fred','Cindy');
715 INSERT INTO org VALUES('Gail','Cindy');
716 INSERT INTO org VALUES('Harry','Dave');
717 INSERT INTO org VALUES('Ingrid','Dave');
718 INSERT INTO org VALUES('Jim','Emma');
719 INSERT INTO org VALUES('Kate','Emma');
720 INSERT INTO org VALUES('Lanny','Fred');
721 INSERT INTO org VALUES('Mary','Fred');
722 INSERT INTO org VALUES('Noland','Gail');
723 INSERT INTO org VALUES('Olivia','Gail');
724 -- The above are all under Alice. Add a few more records for people
725 -- not in Alice's group, just to prove that they won't be selected.
726 INSERT INTO org VALUES('Xaviar',NULL);
727 INSERT INTO org VALUES('Xia','Xaviar');
728 INSERT INTO org VALUES('Xerxes','Xaviar');
729 INSERT INTO org VALUES('Xena','Xia');
730 -- Find all members of Alice's group, breath-first order
732 under_alice(name,level) AS (
735 SELECT org.name, under_alice.level+1
736 FROM org, under_alice
737 WHERE org.boss=under_alice.name
740 SELECT group_concat(substr('...............',1,level*3) || name,x'0a')
758 # The previous query used "ORDER BY level" to yield a breath-first search.
759 # Change that to "ORDER BY level DESC" for a depth-first search.
761 do_execsql_test 11.2 {
763 under_alice(name,level) AS (
766 SELECT org.name, under_alice.level+1
767 FROM org, under_alice
768 WHERE org.boss=under_alice.name
771 SELECT group_concat(substr('...............',1,level*3) || name,x'0a')
789 # Without an ORDER BY clause, the recursive query should use a FIFO,
790 # resulting in a breath-first search.
792 do_execsql_test 11.3 {
794 under_alice(name,level) AS (
797 SELECT org.name, under_alice.level+1
798 FROM org, under_alice
799 WHERE org.boss=under_alice.name
801 SELECT group_concat(substr('...............',1,level*3) || name,x'0a')
819 #--------------------------------------------------------------------------
820 # Ticket [31a19d11b97088296ac104aaff113a9790394927] (2014-02-09)
821 # Name resolution issue with compound SELECTs and Common Table Expressions
823 do_execsql_test 12.1 {
825 t1(x) AS (VALUES(2) UNION ALL SELECT x+2 FROM t1 WHERE x<20),
826 t2(y) AS (VALUES(3) UNION ALL SELECT y+3 FROM t2 WHERE y<20)
827 SELECT x FROM t1 EXCEPT SELECT y FROM t2 ORDER BY 1;
828 } {2 4 8 10 14 16 20}
831 # Column wildcards on the LHS of a recursive table expression
833 do_catchsql_test 13.1 {
834 WITH RECURSIVE c(i) AS (SELECT * UNION ALL SELECT i+1 FROM c WHERE i<10)
836 } {1 {no tables specified}}
837 do_catchsql_test 13.2 {
838 WITH RECURSIVE c(i) AS (SELECT 5,* UNION ALL SELECT i+1 FROM c WHERE i<10)
840 } {1 {no tables specified}}
841 do_catchsql_test 13.3 {
842 WITH RECURSIVE c(i,j) AS (SELECT 5,* UNION ALL SELECT i+1,11 FROM c WHERE i<10)
844 } {1 {table c has 1 values for 2 columns}}
848 do_execsql_test 14.1 {
849 WITH x AS (SELECT * FROM t) SELECT 0 EXCEPT SELECT 0 ORDER BY 1 COLLATE binary;
852 # 2015-05-27: Do not allow rowid usage within a CTE
854 do_catchsql_test 15.1 {
856 d(x) AS (VALUES(1) UNION ALL SELECT rowid+1 FROM d WHERE rowid<10)
858 } {1 {no such column: rowid}}
860 # 2015-07-05: Do not allow aggregate recursive queries
862 do_catchsql_test 16.1 {
864 i(x) AS (VALUES(1) UNION SELECT count(*) FROM i)
866 } {1 {recursive aggregate queries not supported}}
868 # Or window-function recursive queries. Ticket e8275b41.
870 ifcapable windowfunc {
871 do_catchsql_test 16.2 {
873 i(x) AS (VALUES(1) UNION SELECT count(*) OVER () FROM i)
875 } {1 {cannot use window functions in recursive queries}}
876 do_catchsql_test 16.3 {
878 t(id, parent) AS (VALUES(1,2)),
879 q(id, parent, rn) AS (
882 SELECT t.*, ROW_NUMBER() OVER (ORDER BY t.id) AS rn
883 FROM q JOIN t ON t.parent = q.id
886 } {1 {cannot use window functions in recursive queries}}
889 #-------------------------------------------------------------------------
890 do_execsql_test 17.1 {
892 WITH y(b) AS (SELECT 10)
893 SELECT 9 UNION ALL SELECT * FROM y
898 do_execsql_test 17.2 {
900 WITH y(b) AS (SELECT 10)
901 SELECT * FROM y UNION ALL SELECT * FROM y
909 WITH y(b) AS (SELECT 10)
910 SELECT * FROM y UNION ALL SELECT * FROM y
919 do_catchsql_test 17.3 {
922 SELECT 5 FROM i UNION SELECT 8 FROM i
925 } {1 {circular reference: i}}
927 do_catchsql_test 17.4 {
930 SELECT 5 FROM t1 UNION SELECT 8 FROM t11
933 } {1 {no such table: t11}}
935 do_execsql_test 17.5 {
938 x2 AS (SELECT * FROM x1),
940 WITH x1 AS (SELECT 11)
941 SELECT * FROM x2 UNION ALL SELECT * FROM x2
946 do_execsql_test 17.6 {
949 x2 AS (SELECT * FROM x1),
951 WITH x1 AS (SELECT 11)
952 SELECT * FROM x2 UNION ALL SELECT * FROM x1
957 do_execsql_test 17.7 {
960 x2 AS (SELECT * FROM x1),
964 x4 AS ( SELECT * FROM x2 )
965 SELECT * FROM x4 UNION ALL SELECT * FROM x1
970 do_execsql_test 17.8 {
973 x2 AS (SELECT * FROM x1),
977 x4 AS ( SELECT * FROM x2 )
978 SELECT * FROM x4 UNION ALL SELECT * FROM x1
983 do_execsql_test 17.9 {
988 SELECT * FROM x1 UNION ALL SELECT * FROM x2
999 # Added to test a fix to a faulty assert() discovered by libFuzzer.
1001 do_execsql_test 18.1 {
1002 WITH xyz(x) AS (VALUES(NULL) UNION SELECT round(1<x) FROM xyz ORDER BY 1)
1003 SELECT quote(x) FROM xyz;
1005 do_execsql_test 18.2 {
1007 SELECT printf('%d', 5) * NULL
1008 UNION SELECT round(1<1+x)
1014 # EXPLAIN QUERY PLAN on a self-join of a CTE
1016 do_execsql_test 19.1a {
1017 DROP TABLE IF EXISTS t1;
1022 x1(a) AS (values(100))
1024 SELECT * FROM (WITH x2(y) AS (SELECT * FROM x1) SELECT y+a FROM x1, x2);
1029 | `--SCAN CONSTANT ROW
1035 # See check-in https://sqlite.org/src/info/0926df095faf72c2
1036 # Tried to optimize co-routine processing by changing a Copy opcode
1037 # into SCopy. But OSSFuzz found two (similar) cases where that optimization
1040 do_execsql_test 20.1 {
1041 WITH c(i)AS(VALUES(9)UNION SELECT~i FROM c)SELECT max(5)>i fROM c;
1043 do_execsql_test 20.2 {
1044 WITH c(i)AS(VALUES(5)UNIoN SELECT 0)SELECT min(1)-i fROM c;
1048 # Two different CTE tables with the same name appear in within a single FROM
1049 # clause due to the query-flattener optimization. make sure this does not cause
1050 # problems. This problem was discovered by Matt Denton.
1052 do_execsql_test 21.1 {
1053 WITH RECURSIVE t21(a,b) AS (
1054 WITH t21(x) AS (VALUES(1))
1055 SELECT x, x FROM t21 ORDER BY 1
1057 SELECT * FROM t21 AS tA, t21 AS tB
1059 do_execsql_test 21.1b {
1060 /* This variant from chromium bug 922312 on 2019-01-16 */
1061 WITH RECURSIVE t21(a,b) AS (
1062 WITH t21(x) AS (VALUES(1))
1063 SELECT x, x FROM t21 ORDER BY 1 LIMIT 5
1065 SELECT * FROM t21 AS tA, t21 AS tB
1067 do_execsql_test 21.2 {
1069 EXISTS (WITH RECURSIVE Table0 AS (WITH Table0 AS (SELECT DISTINCT 1)
1070 SELECT *, * FROM Table0 ORDER BY 1 DESC)
1071 SELECT * FROM Table0 NATURAL JOIN Table0));
1075 # Make sure crazy nexted CTE joins terminate with an error quickly.
1077 do_catchsql_test 22.1 {
1078 WITH RECURSIVE c AS (
1079 WITH RECURSIVE c AS (
1080 WITH RECURSIVE c AS (
1081 WITH RECURSIVE c AS (
1082 WITH c AS (VALUES(0))
1083 SELECT 1 FROM c LEFT JOIN c ON ltrim(1)
1085 SELECT 1 FROM c,c,c,c,c,c,c,c,c
1087 SELECT 2 FROM c,c,c,c,c,c,c,c,c
1089 SELECT 3 FROM c,c,c,c,c,c,c,c,c
1091 SELECT 4 FROM c,c,c,c,c,c,c,c,c;
1092 } {1 {too many FROM clause terms, max: 200}}
1095 # ticket https://www.sqlite.org/src/tktview/ce823231949d3abf42453c8f20
1098 do_execsql_test 23.1 {
1099 CREATE TABLE t1(id INTEGER NULL PRIMARY KEY, name Text);
1100 INSERT INTO t1 VALUES (1, 'john');
1101 INSERT INTO t1 VALUES (2, 'james');
1102 INSERT INTO t1 VALUES (3, 'jingle');
1103 INSERT INTO t1 VALUES (4, 'himer');
1104 INSERT INTO t1 VALUES (5, 'smith');
1106 WITH t4(Name) AS (VALUES ('A'), ('B'))
1107 SELECT Name Name FROM t4;
1109 WITH t4(Att, Val, Act) AS (VALUES
1113 SELECT D.Id Id, P.Name Protocol, T.Att Att, T.Val Val, T.Act Act
1118 } {1 A C D E 1 A F G H 1 B C D E 1 B F G H 2 A C D E 2 A F G H 2 B C D E 2 B F G H 3 A C D E 3 A F G H 3 B C D E 3 B F G H 4 A C D E 4 A F G H 4 B C D E 4 B F G H 5 A C D E 5 A F G H 5 B C D E 5 B F G H}
1120 #-------------------------------------------------------------------------
1122 do_execsql_test 24.1 {
1123 CREATE TABLE t1(a, b, c);
1124 CREATE VIEW v1 AS SELECT max(a), min(b) FROM t1 GROUP BY c;
1127 set program [db eval {EXPLAIN SELECT 1 FROM v1,v1,v1}]
1128 expr [lsearch $program OpenDup]>0
1130 do_execsql_test 24.2 {
1132 CREATE VIEW aux.v3 AS VALUES(1);
1133 CREATE VIEW main.v3 AS VALUES(3);
1135 CREATE VIEW aux.v2 AS SELECT * FROM v3;
1136 CREATE VIEW main.v2 AS SELECT * FROM v3;
1138 SELECT * FROM main.v2 AS a, aux.v2 AS b, aux.v2 AS c, main.v2 AS d;
1143 # 2020-01-02 chromium ticket 1033461
1144 # Do not allow the generated name of a CTE be "true" or "false" as
1145 # such a label might be later confused for the boolean literals of
1146 # the same name, causing inconsistencies in the abstract syntax
1147 # tree. This problem first arose in version 3.23.0 when SQLite
1148 # began recognizing "true" and "false" as boolean literals, but also
1149 # had to continue to recognize "true" and "false" as identifiers for
1150 # backwards compatibility.
1153 1 {CREATE TABLE dual AS SELECT 'X' AS dummy}
1154 2 {CREATE TEMP TABLE dual AS SELECT 'X' AS dummy}
1155 3 {CREATE VIEW dual(dummy) AS VALUES('X')}
1156 4 {CREATE TEMP VIEW dual(dummy) AS VALUES('X')}
1160 do_execsql_test 25.$id {
1163 WITH cte2 AS (SELECT avg(DISTINCT TRUE) FROM dual)
1164 SELECT 2571 FROM cte2
1169 SELECT (SELECT 1324 FROM cte1) FROM cte1;
1173 do_catchsql_test 26.0 {
1175 VALUES(1) UNION ALL SELECT x+1 FRO, a.b,O. * ,I¬i O, a.b,O. * ORDER BY 1
1177 SELECT x,O. * O FROM i ¬I,I? 10;
1178 } {1 {near "O": syntax error}}
1180 # 2020-09-17 ticket c51489c3b8f919c5
1181 # DISTINCT cannot be ignored in a UNION ALL recursive CTE
1184 do_execsql_test 26.1 {
1185 CREATE TABLE t (label VARCHAR(10), step INTEGER);
1186 INSERT INTO T VALUES('a', 1);
1187 INSERT INTO T VALUES('a', 1);
1188 INSERT INTO T VALUES('b', 1);
1189 WITH RECURSIVE cte(label, step) AS (
1190 SELECT DISTINCT * FROM t
1192 SELECT label, step + 1 FROM cte WHERE step < 3
1194 SELECT * FROM cte ORDER BY +label, +step;
1195 } {a 1 a 2 a 3 b 1 b 2 b 3}
1196 do_execsql_test 26.2 {
1197 WITH RECURSIVE cte(label, step) AS (
1200 SELECT label, step + 1 FROM cte WHERE step < 3
1202 SELECT * FROM cte ORDER BY +label, +step;
1203 } {a 1 a 2 a 3 b 1 b 2 b 3}
1204 do_execsql_test 26.3 {
1205 CREATE TABLE tworow(x);
1206 INSERT INTO tworow(x) VALUES(1),(2);
1207 DELETE FROM t WHERE rowid=2;
1208 WITH RECURSIVE cte(label, step) AS (
1211 SELECT DISTINCT label, step + 1 FROM cte, tworow WHERE step < 3
1213 SELECT * FROM cte ORDER BY +label, +step;
1214 } {a 1 a 2 a 3 b 1 b 2 b 3}
1217 # forum post https://sqlite.org/forum/forumpost/8590e3f6dc
1220 do_execsql_test 27.1 {
1222 CREATE TABLE log(k, cte_map, main_map);
1223 CREATE TABLE map(k, v);
1224 INSERT INTO map VALUES(1, 'main1'), (2, 'main2');
1226 CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
1228 WITH map(k,v) AS (VALUES(1,'cte1'),(2,'cte2'))
1231 (SELECT v FROM map WHERE k=new.k),
1232 (SELECT v FROM main.map WHERE k=new.k);
1235 INSERT INTO t1 VALUES(1);
1236 INSERT INTO t1 VALUES(2);
1237 SELECT k, cte_map, main_map, '|' FROM log ORDER BY k;
1238 } {1 cte1 main1 | 2 cte2 main2 |}