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 with2
26 INSERT INTO t1 VALUES(1);
27 INSERT INTO t1 VALUES(2);
31 WITH x1 AS (SELECT * FROM t1)
32 SELECT sum(a) FROM x1;
36 WITH x1 AS (SELECT * FROM t1)
37 SELECT (SELECT sum(a) FROM x1);
41 WITH x1 AS (SELECT * FROM t1)
42 SELECT (SELECT sum(a) FROM x1);
47 INSERT INTO t2 VALUES(2);
48 INSERT INTO t2 VALUES(3);
49 INSERT INTO t2 VALUES(5);
51 WITH x1 AS (SELECT i FROM t2),
53 SELECT min(i)-1 FROM x1 UNION SELECT a+1 FROM i WHERE a<10
55 SELECT a FROM i WHERE a NOT IN x1
59 WITH x1 AS (SELECT a FROM t1),
60 x2 AS (SELECT i FROM t2),
61 x3 AS (SELECT * FROM x1, x2 WHERE x1.a IN x2 AND x2.i IN x1)
66 CREATE TABLE t3 AS SELECT 3 AS x;
67 CREATE TABLE t4 AS SELECT 4 AS x;
69 WITH x1 AS (SELECT * FROM t3),
71 WITH t3 AS (SELECT * FROM t4)
79 WITH t3 AS (SELECT * FROM t4)
87 WITH t3 AS (SELECT * FROM t4)
94 WITH x1 AS (SELECT * FROM t1)
95 SELECT (SELECT sum(a) FROM x1), (SELECT max(a) FROM x1);
98 do_execsql_test 1.10 {
99 WITH x1 AS (SELECT * FROM t1)
100 SELECT (SELECT sum(a) FROM x1), (SELECT max(a) FROM x1), a FROM x1;
103 do_execsql_test 1.11 {
107 j(x) AS ( SELECT * FROM i ),
108 i(x) AS ( SELECT * FROM t1 )
114 do_execsql_test 1.12 {
118 SELECT i || '.' FROM r, (
119 SELECT x FROM x INTERSECT SELECT y FROM y
120 ) WHERE length(i) < 10
122 x(x) AS ( VALUES(1) UNION ALL VALUES(2) UNION ALL VALUES(3) ),
123 y(y) AS ( VALUES(2) UNION ALL VALUES(4) UNION ALL VALUES(6) )
126 } {. .. ... .... ..... ...... ....... ........ ......... ..........}
128 do_execsql_test 1.13 {
132 SELECT i || '.' FROM r, ( SELECT x FROM x WHERE x=2 ) WHERE length(i) < 10
134 x(x) AS ( VALUES(1) UNION ALL VALUES(2) UNION ALL VALUES(3) )
136 SELECT * FROM r ORDER BY length(i) DESC;
137 } {.......... ......... ........ ....... ...... ..... .... ... .. .}
139 do_execsql_test 1.14 {
144 SELECT x+1 FROM t4 WHERE x<10
149 do_execsql_test 1.15 {
154 SELECT x+1 FROM main.t4 WHERE x<10
159 do_catchsql_test 1.16 {
164 SELECT x+1 FROM t4, main.t4, t4 WHERE x<10
167 } {1 {multiple references to recursive table: t4}}
170 #---------------------------------------------------------------------------
171 # Check that variables can be used in CTEs.
175 do_execsql_test 2.1 {
177 VALUES($min) UNION ALL SELECT x+1 FROM i WHERE x < $max
182 do_execsql_test 2.2 {
184 VALUES($min) UNION ALL SELECT x+1 FROM i WHERE x < $max
186 SELECT x FROM i JOIN i AS j USING (x);
189 #---------------------------------------------------------------------------
190 # Check that circular references are rejected.
192 do_catchsql_test 3.1 {
193 WITH i(x, y) AS ( VALUES(1, (SELECT x FROM i)) )
195 } {1 {circular reference: i}}
197 do_catchsql_test 3.2 {
199 i(x) AS ( SELECT * FROM j ),
200 j(x) AS ( SELECT * FROM k ),
201 k(x) AS ( SELECT * FROM i )
203 } {1 {circular reference: i}}
205 do_catchsql_test 3.3 {
207 i(x) AS ( SELECT * FROM (SELECT * FROM j) ),
208 j(x) AS ( SELECT * FROM (SELECT * FROM i) )
210 } {1 {circular reference: i}}
212 do_catchsql_test 3.4 {
214 i(x) AS ( SELECT * FROM (SELECT * FROM j) ),
215 j(x) AS ( SELECT * FROM (SELECT * FROM i) )
217 } {1 {circular reference: j}}
219 do_catchsql_test 3.5 {
222 WITH j(x) AS ( SELECT * FROM i )
226 } {1 {circular reference: i}}
228 #---------------------------------------------------------------------------
229 # Try empty and very long column lists.
231 do_catchsql_test 4.1 {
232 WITH x() AS ( SELECT 1,2,3 )
234 } {1 {near ")": syntax error}}
237 for {set i 1} {$i<=$n} {incr i} {
242 WITH x([join $cols ,]) AS (SELECT [join $vals ,])
243 SELECT (c$n == $n) FROM x
247 do_execsql_test 4.2 [genstmt 10] 1
248 do_execsql_test 4.3 [genstmt 100] 1
249 do_execsql_test 4.4 [genstmt 255] 1
250 set nLimit [sqlite3_limit db SQLITE_LIMIT_COLUMN -1]
251 do_execsql_test 4.5 [genstmt [expr $nLimit-1]] 1
252 do_execsql_test 4.6 [genstmt $nLimit] 1
253 do_catchsql_test 4.7 [genstmt [expr $nLimit+1]] \
254 {1 {too many columns in result set}}
256 #---------------------------------------------------------------------------
257 # Check that adding a WITH clause to an INSERT disables the xfer
260 proc do_xfer_test {tn bXfer sql {res {}}} {
261 set ::sqlite3_xferopt_count 0
262 uplevel [list do_test $tn [subst -nocommands {
263 set dres [db eval {$sql}]
264 list [set ::sqlite3_xferopt_count] [set dres]
265 }] [list $bXfer $res]]
268 do_execsql_test 5.1 {
269 DROP TABLE IF EXISTS t1;
270 DROP TABLE IF EXISTS t2;
271 CREATE TABLE t1(a, b);
272 CREATE TABLE t2(a, b);
275 do_xfer_test 5.2 1 { INSERT INTO t1 SELECT * FROM t2 }
276 do_xfer_test 5.3 0 { INSERT INTO t1 SELECT a, b FROM t2 }
277 do_xfer_test 5.4 0 { INSERT INTO t1 SELECT b, a FROM t2 }
279 WITH x AS (SELECT a, b FROM t2) INSERT INTO t1 SELECT * FROM x
282 WITH x AS (SELECT a, b FROM t2) INSERT INTO t1 SELECT * FROM t2
285 INSERT INTO t1 WITH x AS ( SELECT * FROM t2 ) SELECT * FROM x
288 INSERT INTO t1 WITH x(a,b) AS ( SELECT * FROM t2 ) SELECT * FROM x
291 #---------------------------------------------------------------------------
292 # Check that syntax (and other) errors in statements with WITH clauses
293 # attached to them do not cause problems (e.g. memory leaks).
295 do_execsql_test 6.1 {
296 DROP TABLE IF EXISTS t1;
297 DROP TABLE IF EXISTS t2;
298 CREATE TABLE t1(a, b);
299 CREATE TABLE t2(a, b);
302 do_catchsql_test 6.2 {
303 WITH x AS (SELECT * FROM t1)
304 INSERT INTO t2 VALUES(1, 2,);
305 } {1 {near ")": syntax error}}
307 do_catchsql_test 6.3 {
308 WITH x AS (SELECT * FROM t1)
309 INSERT INTO t2 SELECT a, b, FROM t1;
310 } {1 {near "FROM": syntax error}}
312 do_catchsql_test 6.3 {
313 WITH x AS (SELECT * FROM t1)
314 INSERT INTO t2 SELECT a, b FROM abc;
315 } {1 {no such table: abc}}
317 do_catchsql_test 6.4 {
318 WITH x AS (SELECT * FROM t1)
319 INSERT INTO t2 SELECT a, b, FROM t1 a a a;
320 } {1 {near "FROM": syntax error}}
322 do_catchsql_test 6.5 {
323 WITH x AS (SELECT * FROM t1)
324 DELETE FROM t2 WHERE;
325 } {1 {near ";": syntax error}}
327 do_catchsql_test 6.6 {
328 WITH x AS (SELECT * FROM t1) DELETE FROM t2 WHERE
329 } {1 {incomplete input}}
331 do_catchsql_test 6.7 {
332 WITH x AS (SELECT * FROM t1) DELETE FROM t2 WHRE 1;
333 } {/1 {near .* syntax error}/}
335 do_catchsql_test 6.8 {
336 WITH x AS (SELECT * FROM t1) UPDATE t2 SET a = 10, b = ;
337 } {/1 {near .* syntax error}/}
339 do_catchsql_test 6.9 {
340 WITH x AS (SELECT * FROM t1) UPDATE t2 SET a = 10, b = 1 WHERE a===b;
341 } {/1 {near .* syntax error}/}
343 do_catchsql_test 6.10 {
347 SELECT a*b,a+b FROM x WHERE c=2
350 } {1 {no such column: c}}
352 #-------------------------------------------------------------------------
353 # Recursive queries in IN(...) expressions.
355 do_execsql_test 7.1 {
356 CREATE TABLE t5(x INTEGER);
357 CREATE TABLE t6(y INTEGER);
359 WITH s(x) AS ( VALUES(7) UNION ALL SELECT x+7 FROM s WHERE x<49 )
364 WITH s(x) AS ( VALUES(2) UNION ALL SELECT x+2 FROM s WHERE x<49 )
368 do_execsql_test 7.2 {
369 SELECT * FROM t6 WHERE y IN (SELECT x FROM t5)
372 do_execsql_test 7.3 {
373 WITH ss AS (SELECT x FROM t5)
374 SELECT * FROM t6 WHERE y IN (SELECT x FROM ss)
377 do_execsql_test 7.4 {
378 WITH ss(x) AS ( VALUES(7) UNION ALL SELECT x+7 FROM ss WHERE x<49 )
379 SELECT * FROM t6 WHERE y IN (SELECT x FROM ss)
382 do_execsql_test 7.5 {
383 SELECT * FROM t6 WHERE y IN (
384 WITH ss(x) AS ( VALUES(7) UNION ALL SELECT x+7 FROM ss WHERE x<49 )
389 #-------------------------------------------------------------------------
390 # At one point the following was causing an assertion failure and a
393 do_execsql_test 8.1 {
395 INSERT INTO t7 VALUES(NULL);
396 CREATE VIEW v AS SELECT * FROM t7 ORDER BY y;
399 do_execsql_test 8.2 {
403 SELECT a+1 FROM q, v WHERE a<5
408 do_execsql_test 8.3 {
412 SELECT a+1 FROM q, v WHERE a<5
418 # Ticket bb8a9fd4a9b7fce5
420 do_execsql_test 9.1 {
422 WITH abc AS ( SELECT 1234 ) SELECT * FROM abc
424 SELECT * FROM xyz AS one, xyz AS two, (
425 SELECT * FROM xyz UNION ALL SELECT * FROM xyz
427 } {1234 1234 1234 1234 1234 1234}
429 load_static_extension db series
430 do_execsql_test 9.2 {
435 cst2(minx, maxx, stepx, miny, maxy, stepy, qualitativex, qualitativey) AS (
436 SELECT NULL, NULL, NULL, NULL, NULL, NULL, 0, 0
438 ds0(m, n, x, y, x2, y2, title, size, mark, label, markmode) AS (
439 SELECT 1, 2, 3, 4, 5, 6, 7 , 8, 9, 10, 11
441 ds(m, n, x, y, x2, y2, title, size, mark, label, markmode) AS (
445 title, size, mark, label, markmode
447 WINDOW w AS (PARTITION BY m, x ORDER BY n)
449 d(m, n, x, y, x2, y2, labelx,labely,title,size,mark,label,markmode) AS (
450 SELECT m, n, x, y, x2, y2, x, y, title, size, mark, label, markmode
453 ylabels(y, label) AS (
454 SELECT y, MIN(labely) FROM d GROUP BY y
456 yaxis(maxy, miny, stepy , minstepy) AS (
459 SELECT coalesce(miny, min(min(y2),
460 min(y))), coalesce(maxy, max(max(y2),
461 max(y))) + qualitativey
464 xt1(mx, mn) AS (SELECT maxx, minx FROM xt0),
465 xt2(mx, mn, step) AS (SELECT mx, mn, (mx-mn) FROM xt1),
468 SELECT mx, mn, first_value(rs) OVER (order by x desc) AS ms
469 FROM (SELECT mx, mn, step, f,(mx-mn) as rng,
470 1.0*step/f as rs, 1.0*(mx-mn)/(step/f) AS x
471 FROM xt2, (SELECT 1 AS f UNION ALL SELECT 2
473 UNION ALL SELECT 5)) AS src
474 WHERE x < 10 limit 1),
476 SELECT MIN(abs(y2-y)) FROM d WHERE y2 != y
478 SELECT (mx/ms)*ms, (mn/ms)*ms, coalesce(stepy, ms),
479 coalesce(minstepy, ms, stepy) FROM xt3, cst2,xt4
481 distinct_mark_n_m(mark, ze, zem, title) AS (
482 SELECT DISTINCT mark, n AS ze, m AS zem, title FROM ds0
484 facet0(m, mi, title, radial) AS (
485 SELECT md, row_number() OVER () - 1, title, 'radial'
486 IN (SELECT mark FROM distinct_mark_n_m WHERE zem = md)
487 FROM (SELECT DISTINCT zem AS md, title AS title
488 FROM distinct_mark_n_m ORDER BY 2, 1)
490 facet(m, mi, xorigin, yorigin, title, radial) AS (
492 rsx * 1.2 * IFNULL(CASE WHEN (
499 rsy * 1.2 * IFNULL(CASE WHEN (
506 title, radial FROM facet0, cst
508 radygrid(m, mi, tty, wty, ttx, ttx2, xorigin, yorigin) AS (
509 SELECT m, mi, rsy / 2 / ((maxy-miny)/stepy) * (value-1) AS tty,
510 coalesce(NULL, miny + stepy * (value-1)) AS wty,
511 xorigin, xorigin+rsx, xorigin + rsx / 2,
513 FROM generate_series(1), yaxis, cst,
514 facet LEFT JOIN ylabels ON ylabels.y = (miny + (value-1) * stepy)
515 WHERE radial AND stop = 1+1.0*(maxy-miny)/stepy
517 ypos(m, mi, pcx, pcy, radial) AS (
518 SELECT m, mi, xorigin, yorigin + CASE
519 WHEN 0 BETWEEN miny AND maxy THEN
520 rsy - (0 - miny) * rsy / (maxy-miny)
521 WHEN 0 >= maxy THEN 0
523 END, radial FROM yaxis, cst, facet WHERE NOT radial
525 SELECT m, mi, xorigin + rsx / 2, yorigin + (CASE
526 WHEN 0 BETWEEN miny AND maxy THEN
527 rsy - (0 - miny) * rsy / 2 / (maxy-miny)
528 WHEN 0 >= maxy THEN 0
530 END ) / 2, radial FROM yaxis, cst, facet WHERE radial
532 SELECT * FROM radygrid , ypos;
534 } ;# end ifcapable vtab
537 # dbsqlfuzz 01b8355086998f0a452cb31208e80b9d29ca739a
539 # Correlated CTEs should not be materialized.
542 do_execsql_test 10.1 {
543 SELECT 1 AS c WHERE (
545 WITH t1(a) AS (VALUES( c ))
546 SELECT ( SELECT t1a.a FROM t1 AS t1a, t1 AS t1x )
547 FROM t1 AS xyz GROUP BY 1
553 # Forum post https://sqlite.org/forum/forumpost/aa4a7a3980
555 ifcapable altertable {
557 do_execsql_test 11.1 {
562 WITH z AS(SELECT * FROM t1)
566 ALTER TABLE t1 RENAME COLUMN a TO b;
567 SELECT sql FROM sqlite_schema WHERE name='t1';
568 } {{CREATE TABLE t1(b)}}
569 do_catchsql_test 11.2 {
570 INSERT INTO t1 VALUES(55);
573 do_catchsql_test 11.3 {
578 WITH z AS(SELECT * FROM t1)
581 ) SELECT * from t1, x;
583 } {1 {no such column: a}}
584 do_catchsql_test 11.4 {
589 WITH z AS(SELECT * FROM t1)
592 ) SELECT * from t1, x;
594 } {1 {no tables specified}}
595 do_catchsql_test 11.5 {
598 WITH z AS(SELECT * FROM t1)
599 SELECT * FROM no_such_table
605 # 2021-05-23 dbsqlfuzz 6b7a144674e215f06ddfeb9042c873d9ee956ac0 */
607 ifcapable altertable {
608 do_execsql_test 12.1 {
610 INSERT INTO t1 VALUES(1),('hello'),(4.25),(NULL),(x'3c626c6f623e');
611 CREATE VIEW v2(c) AS WITH x AS (WITH y AS (WITH z AS(SELECT * FROM t1) SELECT * FROM v2) SELECT a) SELECT * from t1;
612 CREATE VIEW v3(c) AS WITH x AS (WITH y AS (WITH z AS(SELECT * FROM v2) SELECT * FROM v3) SELECT a) SELECT * from t1;
613 ALTER TABLE t1 RENAME TO t1x;
614 SELECT quote(c) FROM v3;
615 } {1 'hello' 4.25 NULL X'3C626C6F623E'}
618 # 2021-08-11 https://sqlite.org/forum/forumpost/d496c3d29bc93736
620 do_execsql_test 13.1 {
622 t1(x) AS (SELECT 111),
623 t2(y) AS (SELECT 222),
624 t3(z) AS (SELECT * FROM t2 WHERE false UNION ALL SELECT * FROM t2)
625 SELECT * FROM t1, t3;