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 VIEW statements.
14 # $Id: view.test,v 1.39 2008/12/14 14:45:21 danielk1977 Exp $
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 # Omit this entire file if the library is not configured with views enabled.
26 CREATE TABLE t1(a,b,c);
27 INSERT INTO t1 VALUES(1,2,3);
28 INSERT INTO t1 VALUES(4,5,6);
29 INSERT INTO t1 VALUES(7,8,9);
37 CREATE VIEW IF NOT EXISTS v1 AS SELECT a,b FROM t1;
38 SELECT * FROM v1 ORDER BY a;
44 SELECT * FROM v1 ORDER BY a;
46 } {1 {no such table: v1}}
49 CREATE VIEW v1 AS SELECT a,b FROM t1;
50 SELECT * FROM v1 ORDER BY a;
57 SELECT * FROM v1 ORDER BY a;
62 DROP VIEW IF EXISTS v1;
63 SELECT * FROM v1 ORDER BY a;
65 } {1 {no such table: v1}}
68 CREATE VIEW v1 AS SELECT a,b FROM t1;
69 SELECT * FROM v1 ORDER BY a;
75 SELECT * FROM v1 ORDER BY a;
77 } {1 {no such table: main.t1}}
80 CREATE TABLE t1(x,a,b,c);
81 INSERT INTO t1 VALUES(1,2,3,4);
82 INSERT INTO t1 VALUES(4,5,6,7);
83 INSERT INTO t1 VALUES(7,8,9,10);
84 SELECT * FROM v1 ORDER BY a;
91 SELECT * FROM v1 ORDER BY a;
97 CREATE VIEW v2 AS SELECT * FROM t1 WHERE a>5
105 INSERT INTO v2 VALUES(1,2,3,4);
107 } {1 {cannot modify v2 because it is a view}}
110 UPDATE v2 SET a=10 WHERE a=5;
112 } {1 {cannot modify v2 because it is a view}}
117 } {1 {cannot modify v2 because it is a view}}
120 INSERT INTO t1 VALUES(11,12,13,14);
121 SELECT * FROM v2 ORDER BY x;
123 } {7 8 9 10 11 12 13 14}
126 SELECT x FROM v2 WHERE a>10
130 # Test that column name of views are generated correctly.
134 SELECT * FROM v1 LIMIT 1
139 SELECT * FROM v2 LIMIT 1
145 CREATE VIEW v1 AS SELECT a AS 'xyz', b+c AS 'pqr', c-b FROM t1;
146 SELECT * FROM v1 LIMIT 1
148 } {xyz 2 pqr 7 c-b 1}
151 CREATE VIEW v1b AS SELECT t1.a, b+c, t1.c FROM t1;
152 SELECT * FROM v1b LIMIT 1
159 CREATE VIEW v3 AS SELECT a FROM t1 UNION SELECT b FROM t1 ORDER BY b;
160 SELECT * FROM v3 LIMIT 4;
168 SELECT b AS 'x', a AS 'y' FROM t1
170 SELECT b FROM v4 ORDER BY b LIMIT 4;
173 } ;# ifcapable compound
180 } {1 {use DROP TABLE to delete table t1}}
183 SELECT 1 FROM t1 LIMIT 1;
190 } {1 {use DROP VIEW to delete view v1}}
193 SELECT 1 FROM v1 LIMIT 1;
198 CREATE INDEX i1v1 ON v1(xyz);
200 } {1 {views may not be indexed}}
204 CREATE TABLE t2(y,a);
205 INSERT INTO t2 VALUES(22,2);
206 INSERT INTO t2 VALUES(33,3);
207 INSERT INTO t2 VALUES(44,4);
208 INSERT INTO t2 VALUES(55,5);
211 } {22 2 33 3 44 4 55 5}
215 SELECT t1.x AS v, t2.y AS w FROM t1 JOIN t2 USING(a);
220 # Verify that the view v5 gets flattened. see sqliteFlattenSubquery().
221 # This will only work if EXPLAIN is enabled.
224 ifcapable {explain} {
227 EXPLAIN SELECT * FROM v5;
232 SELECT * FROM v5 AS a, t2 AS b WHERE a.w=b.y;
234 } {1 22 22 2 4 55 55 5}
237 EXPLAIN SELECT * FROM v5 AS a, t2 AS b WHERE a.w=b.y;
242 SELECT * FROM t2 AS b, v5 AS a WHERE a.w=b.y;
244 } {22 2 1 22 55 5 4 55}
247 EXPLAIN SELECT * FROM t2 AS b, v5 AS a WHERE a.w=b.y;
252 SELECT * FROM t1 AS a, v5 AS b, t2 AS c WHERE a.x=b.v AND b.w=c.y;
254 } {1 2 3 4 1 22 22 2 4 5 6 7 4 55 55 5}
257 EXPLAIN SELECT * FROM t1 AS a, v5 AS b, t2 AS c WHERE a.x=b.v AND b.w=c.y;
264 SELECT min(x), min(a), min(b), min(c), min(a+b+c) FROM v2;
269 SELECT max(x), max(a), max(b), max(c), max(a+b+c) FROM v2;
275 CREATE TABLE test1(id integer primary key, a);
276 CREATE TABLE test2(id integer, b);
277 INSERT INTO test1 VALUES(1,2);
278 INSERT INTO test2 VALUES(1,3);
280 SELECT test1.id, a, b
281 FROM test1 JOIN test2 ON test2.id=test1.id;
296 SELECT test1.id, a, b
297 FROM test1 JOIN test2 USING(id);
312 SELECT test1.id, a, b
313 FROM test1 NATURAL JOIN test2;
327 CREATE VIEW v6 AS SELECT pqr, xyz FROM v1;
328 SELECT * FROM v6 ORDER BY xyz;
330 } {7 2 13 5 19 8 27 12}
335 SELECT * FROM v6 ORDER BY xyz;
337 } {7 2 13 5 19 8 27 12}
340 CREATE VIEW v7 AS SELECT pqr+xyz AS a FROM v6;
341 SELECT * FROM v7 ORDER BY a;
348 CREATE VIEW v8 AS SELECT max(cnt) AS mx FROM
349 (SELECT a%2 AS eo, count(*) AS cnt FROM t1 GROUP BY eo);
355 SELECT mx+10, mx*2 FROM v8;
360 SELECT mx+10, pqr FROM v6, v8 WHERE xyz=2;
365 SELECT mx+10, pqr FROM v6, v8 WHERE xyz>2;
367 } {13 13 13 19 13 27}
368 } ;# ifcapable subquery
370 # Tests for a bug found by Michiel de Wit involving ORDER BY in a VIEW.
374 INSERT INTO t2 SELECT * FROM t2 WHERE a<5;
375 INSERT INTO t2 SELECT * FROM t2 WHERE a<4;
376 INSERT INTO t2 SELECT * FROM t2 WHERE a<3;
377 SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1;
382 SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1 LIMIT 3;
388 SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1 LIMIT 3;
394 SELECT * FROM v9 ORDER BY 1 DESC;
400 SELECT DISTINCT a, count(*) FROM t2 GROUP BY a ORDER BY 2 LIMIT 3;
406 SELECT * FROM v10 ORDER BY 1;
410 # Tables with columns having peculiar quoted names used in views
415 CREATE TABLE t3("9" integer, [4] text);
416 INSERT INTO t3 VALUES(1,2);
417 CREATE VIEW v_t3_a AS SELECT a.[9] FROM t3 AS a;
418 CREATE VIEW v_t3_b AS SELECT "4" FROM t3;
419 SELECT * FROM v_t3_a;
424 SELECT * FROM v_t3_b;
430 CREATE TABLE t4(a COLLATE NOCASE);
431 INSERT INTO t4 VALUES('This');
432 INSERT INTO t4 VALUES('this');
433 INSERT INTO t4 VALUES('THIS');
434 SELECT * FROM t4 WHERE a = 'THIS';
440 SELECT * FROM (SELECT * FROM t4) WHERE a = 'THIS';
446 CREATE VIEW v11 AS SELECT * FROM t4;
447 SELECT * FROM v11 WHERE a = 'THIS';
451 # Ticket #1270: Do not allow parameters in view definitions.
455 CREATE VIEW v12 AS SELECT a FROM t1 WHERE b=?
457 } {1 {parameters are not allowed in views}}
463 ATTACH 'test2.db' AS two;
464 CREATE TABLE two.t2(x,y);
465 CREATE VIEW v13 AS SELECT y FROM two.t2;
467 } {1 {view v13 cannot reference objects in database two}}
474 CREATE TEMP VIEW t1 AS SELECT a,b FROM t1;
475 SELECT * FROM temp.t1;
477 } {1 {view t1 is circularly defined}}
479 # Tickets #1688, #1709
483 CREATE VIEW v15 AS SELECT a AS x, b AS y FROM t1;
484 SELECT * FROM v15 LIMIT 1;
489 SELECT x, y FROM v15 LIMIT 1
495 CREATE VIEW IF NOT EXISTS v1 AS SELECT * FROM t1;
500 SELECT sql FROM sqlite_master WHERE name='v1'
502 } {{CREATE VIEW v1 AS SELECT a AS 'xyz', b+c AS 'pqr', c-b FROM t1}}
505 DROP VIEW IF EXISTS nosuchview
509 # correct error message when attempting to drop a view that does not
516 } {1 {no such view: nosuchview}}
519 DROP VIEW main.nosuchview
521 } {1 {no such view: main.nosuchview}}
527 CREATE TABLE t1(a, b, c);
528 INSERT INTO t1 VALUES(1, 2, 3);
529 INSERT INTO t1 VALUES(4, 5, 6);
531 CREATE VIEW vv1 AS SELECT * FROM t1;
532 CREATE VIEW vv2 AS SELECT * FROM vv1;
533 CREATE VIEW vv3 AS SELECT * FROM vv2;
534 CREATE VIEW vv4 AS SELECT * FROM vv3;
535 CREATE VIEW vv5 AS SELECT * FROM vv4;
542 # Make sure "rowid" columns in a view are named correctly.
546 CREATE VIEW v3308a AS SELECT rowid, * FROM t1;
551 } {rowid 1 a 1 b 2 c 3 rowid 2 a 4 b 5 c 6}
554 CREATE VIEW v3308b AS SELECT t1.rowid, t1.a, t1.b+t1.c FROM t1;
559 } {rowid 1 a 1 t1.b+t1.c 5 rowid 2 a 4 t1.b+t1.c 11}
562 CREATE VIEW v3308c AS SELECT t1.oid, A, t1.b+t1.c AS x FROM t1;
567 } {rowid 1 a 1 x 5 rowid 2 a 4 x 11}
569 # Ticket #3539 had this crashing (see commit [5940]).
572 DROP TABLE IF EXISTS t1;
573 DROP VIEW IF EXISTS v1;
575 CREATE VIEW v1 AS SELECT c1 FROM (SELECT t1.c1 FROM t1);
579 # Ticket [d58ccbb3f1b]: Prevent Table.nRef overflow.
585 INSERT INTO t1 VALUES(5);
586 CREATE VIEW v1 AS SELECT x*2 FROM t1;
587 CREATE VIEW v2 AS SELECT * FROM v1 UNION SELECT * FROM v1;
588 CREATE VIEW v4 AS SELECT * FROM v2 UNION SELECT * FROM v2;
589 CREATE VIEW v8 AS SELECT * FROM v4 UNION SELECT * FROM v4;
590 CREATE VIEW v16 AS SELECT * FROM v8 UNION SELECT * FROM v8;
591 CREATE VIEW v32 AS SELECT * FROM v16 UNION SELECT * FROM v16;
592 CREATE VIEW v64 AS SELECT * FROM v32 UNION SELECT * FROM v32;
593 CREATE VIEW v128 AS SELECT * FROM v64 UNION SELECT * FROM v64;
594 CREATE VIEW v256 AS SELECT * FROM v128 UNION SELECT * FROM v128;
595 CREATE VIEW v512 AS SELECT * FROM v256 UNION SELECT * FROM v256;
596 CREATE VIEW v1024 AS SELECT * FROM v512 UNION SELECT * FROM v512;
597 CREATE VIEW v2048 AS SELECT * FROM v1024 UNION SELECT * FROM v1024;
598 CREATE VIEW v4096 AS SELECT * FROM v2048 UNION SELECT * FROM v2048;
599 CREATE VIEW v8192 AS SELECT * FROM v4096 UNION SELECT * FROM v4096;
600 CREATE VIEW v16384 AS SELECT * FROM v8192 UNION SELECT * FROM v8192;
601 CREATE VIEW v32768 AS SELECT * FROM v16384 UNION SELECT * FROM v16384;
602 CREATE VIEW vx AS SELECT * FROM v32768 UNION SELECT * FROM v32768;
604 } {1 {too many references to "v1": max 65535}}
607 db progress 1000 {expr 1}
609 SELECT * FROM v32768;
616 do_execsql_test view-22.1 {
617 CREATE VIEW x1 AS SELECT 123 AS '', 234 AS '', 345 AS '';
622 db eval {SELECT * FROM x1} x break
623 lsort [array names x]