2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing VIEW statements.
17 # $Id: view.test,v 1.16.2.1 2004/07/20 00:20:47 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
23 CREATE TABLE t1(a,b,c);
24 INSERT INTO t1 VALUES(1,2,3);
25 INSERT INTO t1 VALUES(4,5,6);
26 INSERT INTO t1 VALUES(7,8,9);
34 CREATE VIEW v1 AS SELECT a,b FROM t1;
35 SELECT * FROM v1 ORDER BY a;
41 SELECT * FROM v1 ORDER BY a;
43 } {1 {no such table: v1}}
46 CREATE VIEW v1 AS SELECT a,b FROM t1;
47 SELECT * FROM v1 ORDER BY a;
54 SELECT * FROM v1 ORDER BY a;
60 SELECT * FROM v1 ORDER BY a;
62 } {1 {no such table: v1}}
65 CREATE VIEW v1 AS SELECT a,b FROM t1;
66 SELECT * FROM v1 ORDER BY a;
72 SELECT * FROM v1 ORDER BY a;
74 } {1 {no such table: main.t1}}
77 CREATE TABLE t1(x,a,b,c);
78 INSERT INTO t1 VALUES(1,2,3,4);
79 INSERT INTO t1 VALUES(4,5,6,7);
80 INSERT INTO t1 VALUES(7,8,9,10);
81 SELECT * FROM v1 ORDER BY a;
88 SELECT * FROM v1 ORDER BY a;
94 CREATE VIEW v2 AS SELECT * FROM t1 WHERE a>5
102 INSERT INTO v2 VALUES(1,2,3,4);
104 } {1 {cannot modify v2 because it is a view}}
107 UPDATE v2 SET a=10 WHERE a=5;
109 } {1 {cannot modify v2 because it is a view}}
114 } {1 {cannot modify v2 because it is a view}}
117 INSERT INTO t1 VALUES(11,12,13,14);
118 SELECT * FROM v2 ORDER BY x;
120 } {7 8 9 10 11 12 13 14}
123 SELECT x FROM v2 WHERE a>10
127 # Test that column name of views are generated correctly.
131 SELECT * FROM v1 LIMIT 1
136 SELECT * FROM v2 LIMIT 1
142 CREATE VIEW v1 AS SELECT a AS 'xyz', b+c AS 'pqr', c-b FROM t1;
143 SELECT * FROM v1 LIMIT 1
145 } {xyz 2 pqr 7 c-b 1}
148 CREATE VIEW v3 AS SELECT a FROM t1 UNION SELECT b FROM t1 ORDER BY b;
149 SELECT * FROM v3 LIMIT 4;
157 SELECT b AS 'x', a AS 'y' FROM t1
159 SELECT y FROM v4 ORDER BY y LIMIT 4;
168 } {1 {use DROP TABLE to delete table t1}}
171 SELECT 1 FROM t1 LIMIT 1;
178 } {1 {use DROP VIEW to delete view v1}}
181 SELECT 1 FROM v1 LIMIT 1;
186 CREATE INDEX i1v1 ON v1(xyz);
188 } {1 {views may not be indexed}}
192 CREATE TABLE t2(y,a);
193 INSERT INTO t2 VALUES(22,2);
194 INSERT INTO t2 VALUES(33,3);
195 INSERT INTO t2 VALUES(44,4);
196 INSERT INTO t2 VALUES(55,5);
199 } {22 2 33 3 44 4 55 5}
203 SELECT t1.x AS v, t2.y AS w FROM t1 JOIN t2 USING(a);
208 # Verify that the view v5 gets flattened. see sqliteFlattenSubquery().
212 EXPLAIN SELECT * FROM v5;
217 SELECT * FROM v5 AS a, t2 AS b WHERE a.w=b.y;
219 } {1 22 22 2 4 55 55 5}
222 EXPLAIN SELECT * FROM v5 AS a, t2 AS b WHERE a.w=b.y;
227 SELECT * FROM t2 AS b, v5 AS a WHERE a.w=b.y;
229 } {22 2 1 22 55 5 4 55}
232 EXPLAIN SELECT * FROM t2 AS b, v5 AS a WHERE a.w=b.y;
237 SELECT * FROM t1 AS a, v5 AS b, t2 AS c WHERE a.x=b.v AND b.w=c.y;
239 } {1 2 3 4 1 22 22 2 4 5 6 7 4 55 55 5}
242 EXPLAIN SELECT * FROM t1 AS a, v5 AS b, t2 AS c WHERE a.x=b.v AND b.w=c.y;
248 SELECT min(x), min(a), min(b), min(c), min(a+b+c) FROM v2;
253 SELECT max(x), max(a), max(b), max(c), max(a+b+c) FROM v2;
259 CREATE TABLE test1(id integer primary key, a);
260 CREATE TABLE test2(id integer, b);
261 INSERT INTO test1 VALUES(1,2);
262 INSERT INTO test2 VALUES(1,3);
264 SELECT test1.id, a, b
265 FROM test1 JOIN test2 ON test2.id=test1.id;
280 SELECT test1.id, a, b
281 FROM test1 JOIN test2 USING(id);
296 SELECT test1.id, a, b
297 FROM test1 NATURAL JOIN test2;
311 CREATE VIEW v6 AS SELECT pqr, xyz FROM v1;
312 SELECT * FROM v6 ORDER BY xyz;
314 } {7 2 13 5 19 8 27 12}
319 SELECT * FROM v6 ORDER BY xyz;
321 } {7 2 13 5 19 8 27 12}
324 CREATE VIEW v7 AS SELECT pqr+xyz AS a FROM v6;
325 SELECT * FROM v7 ORDER BY a;
330 CREATE VIEW v8 AS SELECT max(cnt) AS mx FROM
331 (SELECT a%2 AS eo, count(*) AS cnt FROM t1 GROUP BY eo);
337 SELECT mx+10, mx*2 FROM v8;
342 SELECT mx+10, pqr FROM v6, v8 WHERE xyz=2;
347 SELECT mx+10, pqr FROM v6, v8 WHERE xyz>2;
349 } {13 13 13 19 13 27}
351 # Tests for a bug found by Michiel de Wit involving ORDER BY in a VIEW.
355 INSERT INTO t2 SELECT * FROM t2 WHERE a<5;
356 INSERT INTO t2 SELECT * FROM t2 WHERE a<4;
357 INSERT INTO t2 SELECT * FROM t2 WHERE a<3;
358 SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1;
363 SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1 LIMIT 3;
369 SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1 LIMIT 3;
375 SELECT * FROM v9 ORDER BY 1 DESC;
381 SELECT DISTINCT a, count(*) FROM t2 GROUP BY a ORDER BY 2 LIMIT 3;
387 SELECT * FROM v10 ORDER BY 1;
391 # Tables with columns having peculiar quoted names used in views
396 CREATE TABLE t3("9" integer, [4] text);
397 INSERT INTO t3 VALUES(1,2);
398 CREATE VIEW v_t3_a AS SELECT a.[9] FROM t3 AS a;
399 CREATE VIEW v_t3_b AS SELECT "4" FROM t3;
400 SELECT * FROM v_t3_a;
405 SELECT * FROM v_t3_b;