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]] {1 {too many columns in index}}
255 #---------------------------------------------------------------------------
256 # Check that adding a WITH clause to an INSERT disables the xfer
259 proc do_xfer_test {tn bXfer sql {res {}}} {
260 set ::sqlite3_xferopt_count 0
261 uplevel [list do_test $tn [subst -nocommands {
262 set dres [db eval {$sql}]
263 list [set ::sqlite3_xferopt_count] [set dres]
264 }] [list $bXfer $res]]
267 do_execsql_test 5.1 {
268 DROP TABLE IF EXISTS t1;
269 DROP TABLE IF EXISTS t2;
270 CREATE TABLE t1(a, b);
271 CREATE TABLE t2(a, b);
274 do_xfer_test 5.2 1 { INSERT INTO t1 SELECT * FROM t2 }
275 do_xfer_test 5.3 0 { INSERT INTO t1 SELECT a, b FROM t2 }
276 do_xfer_test 5.4 0 { INSERT INTO t1 SELECT b, a FROM t2 }
278 WITH x AS (SELECT a, b FROM t2) INSERT INTO t1 SELECT * FROM x
281 WITH x AS (SELECT a, b FROM t2) INSERT INTO t1 SELECT * FROM t2
284 INSERT INTO t1 WITH x AS ( SELECT * FROM t2 ) SELECT * FROM x
287 INSERT INTO t1 WITH x(a,b) AS ( SELECT * FROM t2 ) SELECT * FROM x
290 #---------------------------------------------------------------------------
291 # Check that syntax (and other) errors in statements with WITH clauses
292 # attached to them do not cause problems (e.g. memory leaks).
294 do_execsql_test 6.1 {
295 DROP TABLE IF EXISTS t1;
296 DROP TABLE IF EXISTS t2;
297 CREATE TABLE t1(a, b);
298 CREATE TABLE t2(a, b);
301 do_catchsql_test 6.2 {
302 WITH x AS (SELECT * FROM t1)
303 INSERT INTO t2 VALUES(1, 2,);
304 } {1 {near ")": syntax error}}
306 do_catchsql_test 6.3 {
307 WITH x AS (SELECT * FROM t1)
308 INSERT INTO t2 SELECT a, b, FROM t1;
309 } {1 {near "FROM": syntax error}}
311 do_catchsql_test 6.3 {
312 WITH x AS (SELECT * FROM t1)
313 INSERT INTO t2 SELECT a, b FROM abc;
314 } {1 {no such table: abc}}
316 do_catchsql_test 6.4 {
317 WITH x AS (SELECT * FROM t1)
318 INSERT INTO t2 SELECT a, b, FROM t1 a a a;
319 } {1 {near "FROM": syntax error}}
321 do_catchsql_test 6.5 {
322 WITH x AS (SELECT * FROM t1)
323 DELETE FROM t2 WHERE;
324 } {1 {near ";": syntax error}}
326 do_catchsql_test 6.6 {
327 WITH x AS (SELECT * FROM t1) DELETE FROM t2 WHERE
328 } {/1 {near .* syntax error}/}
330 do_catchsql_test 6.7 {
331 WITH x AS (SELECT * FROM t1) DELETE FROM t2 WHRE 1;
332 } {/1 {near .* syntax error}/}
334 do_catchsql_test 6.8 {
335 WITH x AS (SELECT * FROM t1) UPDATE t2 SET a = 10, b = ;
336 } {/1 {near .* syntax error}/}
338 do_catchsql_test 6.9 {
339 WITH x AS (SELECT * FROM t1) UPDATE t2 SET a = 10, b = 1 WHERE a===b;
340 } {/1 {near .* syntax error}/}
342 do_catchsql_test 6.10 {
346 SELECT a*b,a+b FROM x WHERE c=2
349 } {1 {no such column: c}}
351 #-------------------------------------------------------------------------
352 # Recursive queries in IN(...) expressions.
354 do_execsql_test 7.1 {
355 CREATE TABLE t5(x INTEGER);
356 CREATE TABLE t6(y INTEGER);
358 WITH s(x) AS ( VALUES(7) UNION ALL SELECT x+7 FROM s WHERE x<49 )
363 WITH s(x) AS ( VALUES(2) UNION ALL SELECT x+2 FROM s WHERE x<49 )
367 do_execsql_test 7.2 {
368 SELECT * FROM t6 WHERE y IN (SELECT x FROM t5)
371 do_execsql_test 7.3 {
372 WITH ss AS (SELECT x FROM t5)
373 SELECT * FROM t6 WHERE y IN (SELECT x FROM ss)
376 do_execsql_test 7.4 {
377 WITH ss(x) AS ( VALUES(7) UNION ALL SELECT x+7 FROM ss WHERE x<49 )
378 SELECT * FROM t6 WHERE y IN (SELECT x FROM ss)
381 do_execsql_test 7.5 {
382 SELECT * FROM t6 WHERE y IN (
383 WITH ss(x) AS ( VALUES(7) UNION ALL SELECT x+7 FROM ss WHERE x<49 )
388 #-------------------------------------------------------------------------
389 # At one point the following was causing an assertion failure and a
392 do_execsql_test 8.1 {
394 INSERT INTO t7 VALUES(NULL);
395 CREATE VIEW v AS SELECT * FROM t7 ORDER BY y;
398 do_execsql_test 8.2 {
402 SELECT a+1 FROM q, v WHERE a<5
407 do_execsql_test 8.3 {
411 SELECT a+1 FROM q, v WHERE a<5