xref: /illumos-gate/usr/src/lib/libsqlite/test/view.test (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
1#
2# 2002 February 26
3#
4# The author disclaims copyright to this source code.  In place of
5# a legal notice, here is a blessing:
6#
7#    May you do good and not evil.
8#    May you find forgiveness for yourself and forgive others.
9#    May you share freely, never taking more than you give.
10#
11#***********************************************************************
12# This file implements regression tests for SQLite library.  The
13# focus of this file is testing VIEW statements.
14#
15# $Id: view.test,v 1.16.2.1 2004/07/20 00:20:47 drh Exp $
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19do_test view-1.0 {
20  execsql {
21    CREATE TABLE t1(a,b,c);
22    INSERT INTO t1 VALUES(1,2,3);
23    INSERT INTO t1 VALUES(4,5,6);
24    INSERT INTO t1 VALUES(7,8,9);
25    SELECT * FROM t1;
26  }
27} {1 2 3 4 5 6 7 8 9}
28
29do_test view-1.1 {
30  execsql {
31    BEGIN;
32    CREATE VIEW v1 AS SELECT a,b FROM t1;
33    SELECT * FROM v1 ORDER BY a;
34  }
35} {1 2 4 5 7 8}
36do_test view-1.2 {
37  catchsql {
38    ROLLBACK;
39    SELECT * FROM v1 ORDER BY a;
40  }
41} {1 {no such table: v1}}
42do_test view-1.3 {
43  execsql {
44    CREATE VIEW v1 AS SELECT a,b FROM t1;
45    SELECT * FROM v1 ORDER BY a;
46  }
47} {1 2 4 5 7 8}
48do_test view-1.3.1 {
49  db close
50  sqlite db test.db
51  execsql {
52    SELECT * FROM v1 ORDER BY a;
53  }
54} {1 2 4 5 7 8}
55do_test view-1.4 {
56  catchsql {
57    DROP VIEW v1;
58    SELECT * FROM v1 ORDER BY a;
59  }
60} {1 {no such table: v1}}
61do_test view-1.5 {
62  execsql {
63    CREATE VIEW v1 AS SELECT a,b FROM t1;
64    SELECT * FROM v1 ORDER BY a;
65  }
66} {1 2 4 5 7 8}
67do_test view-1.6 {
68  catchsql {
69    DROP TABLE t1;
70    SELECT * FROM v1 ORDER BY a;
71  }
72} {1 {no such table: main.t1}}
73do_test view-1.7 {
74  execsql {
75    CREATE TABLE t1(x,a,b,c);
76    INSERT INTO t1 VALUES(1,2,3,4);
77    INSERT INTO t1 VALUES(4,5,6,7);
78    INSERT INTO t1 VALUES(7,8,9,10);
79    SELECT * FROM v1 ORDER BY a;
80  }
81} {2 3 5 6 8 9}
82do_test view-1.8 {
83  db close
84  sqlite db test.db
85  execsql {
86    SELECT * FROM v1 ORDER BY a;
87  }
88} {2 3 5 6 8 9}
89
90do_test view-2.1 {
91  execsql {
92    CREATE VIEW v2 AS SELECT * FROM t1 WHERE a>5
93  };  # No semicolon
94  execsql2 {
95    SELECT * FROM v2;
96  }
97} {x 7 a 8 b 9 c 10}
98do_test view-2.2 {
99  catchsql {
100    INSERT INTO v2 VALUES(1,2,3,4);
101  }
102} {1 {cannot modify v2 because it is a view}}
103do_test view-2.3 {
104  catchsql {
105    UPDATE v2 SET a=10 WHERE a=5;
106  }
107} {1 {cannot modify v2 because it is a view}}
108do_test view-2.4 {
109  catchsql {
110    DELETE FROM v2;
111  }
112} {1 {cannot modify v2 because it is a view}}
113do_test view-2.5 {
114  execsql {
115    INSERT INTO t1 VALUES(11,12,13,14);
116    SELECT * FROM v2 ORDER BY x;
117  }
118} {7 8 9 10 11 12 13 14}
119do_test view-2.6 {
120  execsql {
121    SELECT x FROM v2 WHERE a>10
122  }
123} {11}
124
125# Test that column name of views are generated correctly.
126#
127do_test view-3.1 {
128  execsql2 {
129    SELECT * FROM v1 LIMIT 1
130  }
131} {a 2 b 3}
132do_test view-3.2 {
133  execsql2 {
134    SELECT * FROM v2 LIMIT 1
135  }
136} {x 7 a 8 b 9 c 10}
137do_test view-3.3 {
138  execsql2 {
139    DROP VIEW v1;
140    CREATE VIEW v1 AS SELECT a AS 'xyz', b+c AS 'pqr', c-b FROM t1;
141    SELECT * FROM v1 LIMIT 1
142  }
143} {xyz 2 pqr 7 c-b 1}
144do_test  view-3.4 {
145  execsql2 {
146    CREATE VIEW v3 AS SELECT a FROM t1 UNION SELECT b FROM t1 ORDER BY b;
147    SELECT * FROM v3 LIMIT 4;
148  }
149} {b 2 b 3 b 5 b 6}
150do_test  view-3.5 {
151  execsql2 {
152    CREATE VIEW v4 AS
153      SELECT a, b FROM t1
154      UNION
155      SELECT b AS 'x', a AS 'y' FROM t1
156      ORDER BY x, y;
157    SELECT y FROM v4 ORDER BY y LIMIT 4;
158  }
159} {y 2 y 3 y 5 y 6}
160
161
162do_test view-4.1 {
163  catchsql {
164    DROP VIEW t1;
165  }
166} {1 {use DROP TABLE to delete table t1}}
167do_test view-4.2 {
168  execsql {
169    SELECT 1 FROM t1 LIMIT 1;
170  }
171} 1
172do_test view-4.3 {
173  catchsql {
174    DROP TABLE v1;
175  }
176} {1 {use DROP VIEW to delete view v1}}
177do_test view-4.4 {
178  execsql {
179     SELECT 1 FROM v1 LIMIT 1;
180  }
181} {1}
182do_test view-4.5 {
183  catchsql {
184    CREATE INDEX i1v1 ON v1(xyz);
185  }
186} {1 {views may not be indexed}}
187
188do_test view-5.1 {
189  execsql {
190    CREATE TABLE t2(y,a);
191    INSERT INTO t2 VALUES(22,2);
192    INSERT INTO t2 VALUES(33,3);
193    INSERT INTO t2 VALUES(44,4);
194    INSERT INTO t2 VALUES(55,5);
195    SELECT * FROM t2;
196  }
197} {22 2 33 3 44 4 55 5}
198do_test view-5.2 {
199  execsql {
200    CREATE VIEW v5 AS
201      SELECT t1.x AS v, t2.y AS w FROM t1 JOIN t2 USING(a);
202    SELECT * FROM v5;
203  }
204} {1 22 4 55}
205
206# Verify that the view v5 gets flattened.  see sqliteFlattenSubquery().
207# Ticket #272
208do_test view-5.3 {
209  lsearch [execsql {
210    EXPLAIN SELECT * FROM v5;
211  }] OpenTemp
212} {-1}
213do_test view-5.4 {
214  execsql {
215    SELECT * FROM v5 AS a, t2 AS b WHERE a.w=b.y;
216  }
217} {1 22 22 2 4 55 55 5}
218do_test view-5.5 {
219  lsearch [execsql {
220    EXPLAIN SELECT * FROM v5 AS a, t2 AS b WHERE a.w=b.y;
221  }] OpenTemp
222} {-1}
223do_test view-5.6 {
224  execsql {
225    SELECT * FROM t2 AS b, v5 AS a WHERE a.w=b.y;
226  }
227} {22 2 1 22 55 5 4 55}
228do_test view-5.7 {
229  lsearch [execsql {
230    EXPLAIN SELECT * FROM t2 AS b, v5 AS a WHERE a.w=b.y;
231  }] OpenTemp
232} {-1}
233do_test view-5.8 {
234  execsql {
235    SELECT * FROM t1 AS a, v5 AS b, t2 AS c WHERE a.x=b.v AND b.w=c.y;
236  }
237} {1 2 3 4 1 22 22 2 4 5 6 7 4 55 55 5}
238do_test view-5.9 {
239  lsearch [execsql {
240    EXPLAIN SELECT * FROM t1 AS a, v5 AS b, t2 AS c WHERE a.x=b.v AND b.w=c.y;
241  }] OpenTemp
242} {-1}
243
244do_test view-6.1 {
245  execsql {
246    SELECT min(x), min(a), min(b), min(c), min(a+b+c) FROM v2;
247  }
248} {7 8 9 10 27}
249do_test view-6.2 {
250  execsql {
251    SELECT max(x), max(a), max(b), max(c), max(a+b+c) FROM v2;
252  }
253} {11 12 13 14 39}
254
255do_test view-7.1 {
256  execsql {
257    CREATE TABLE test1(id integer primary key, a);
258    CREATE TABLE test2(id integer, b);
259    INSERT INTO test1 VALUES(1,2);
260    INSERT INTO test2 VALUES(1,3);
261    CREATE VIEW test AS
262      SELECT test1.id, a, b
263      FROM test1 JOIN test2 ON test2.id=test1.id;
264    SELECT * FROM test;
265  }
266} {1 2 3}
267do_test view-7.2 {
268  db close
269  sqlite db test.db
270  execsql {
271    SELECT * FROM test;
272  }
273} {1 2 3}
274do_test view-7.3 {
275  execsql {
276    DROP VIEW test;
277    CREATE VIEW test AS
278      SELECT test1.id, a, b
279      FROM test1 JOIN test2 USING(id);
280    SELECT * FROM test;
281  }
282} {1 2 3}
283do_test view-7.4 {
284  db close
285  sqlite db test.db
286  execsql {
287    SELECT * FROM test;
288  }
289} {1 2 3}
290do_test view-7.5 {
291  execsql {
292    DROP VIEW test;
293    CREATE VIEW test AS
294      SELECT test1.id, a, b
295      FROM test1 NATURAL JOIN test2;
296    SELECT * FROM test;
297  }
298} {1 2 3}
299do_test view-7.6 {
300  db close
301  sqlite db test.db
302  execsql {
303    SELECT * FROM test;
304  }
305} {1 2 3}
306
307do_test view-8.1 {
308  execsql {
309    CREATE VIEW v6 AS SELECT pqr, xyz FROM v1;
310    SELECT * FROM v6 ORDER BY xyz;
311  }
312} {7 2 13 5 19 8 27 12}
313do_test view-8.2 {
314  db close
315  sqlite db test.db
316  execsql {
317    SELECT * FROM v6 ORDER BY xyz;
318  }
319} {7 2 13 5 19 8 27 12}
320do_test view-8.3 {
321  execsql {
322    CREATE VIEW v7 AS SELECT pqr+xyz AS a FROM v6;
323    SELECT * FROM v7 ORDER BY a;
324  }
325} {9 18 27 39}
326do_test view-8.4 {
327  execsql {
328    CREATE VIEW v8 AS SELECT max(cnt) AS mx FROM
329      (SELECT a%2 AS eo, count(*) AS cnt FROM t1 GROUP BY eo);
330    SELECT * FROM v8;
331  }
332} 3
333do_test view-8.5 {
334  execsql {
335    SELECT mx+10, mx*2 FROM v8;
336  }
337} {13 6}
338do_test view-8.6 {
339  execsql {
340    SELECT mx+10, pqr FROM v6, v8 WHERE xyz=2;
341  }
342} {13 7}
343do_test view-8.7 {
344  execsql {
345    SELECT mx+10, pqr FROM v6, v8 WHERE xyz>2;
346  }
347} {13 13 13 19 13 27}
348
349# Tests for a bug found by Michiel de Wit involving ORDER BY in a VIEW.
350#
351do_test view-9.1 {
352  execsql {
353    INSERT INTO t2 SELECT * FROM t2 WHERE a<5;
354    INSERT INTO t2 SELECT * FROM t2 WHERE a<4;
355    INSERT INTO t2 SELECT * FROM t2 WHERE a<3;
356    SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1;
357  }
358} {1 2 4 8}
359do_test view-9.2 {
360  execsql {
361    SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1 LIMIT 3;
362  }
363} {1 2 4}
364do_test view-9.3 {
365  execsql {
366    CREATE VIEW v9 AS
367       SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1 LIMIT 3;
368    SELECT * FROM v9;
369  }
370} {1 2 4}
371do_test view-9.4 {
372  execsql {
373    SELECT * FROM v9 ORDER BY 1 DESC;
374  }
375} {4 2 1}
376do_test view-9.5 {
377  execsql {
378    CREATE VIEW v10 AS
379       SELECT DISTINCT a, count(*) FROM t2 GROUP BY a ORDER BY 2 LIMIT 3;
380    SELECT * FROM v10;
381  }
382} {5 1 4 2 3 4}
383do_test view-9.6 {
384  execsql {
385    SELECT * FROM v10 ORDER BY 1;
386  }
387} {3 4 4 2 5 1}
388
389# Tables with columns having peculiar quoted names used in views
390# Ticket #756.
391#
392do_test view-10.1 {
393  execsql {
394    CREATE TABLE t3("9" integer, [4] text);
395    INSERT INTO t3 VALUES(1,2);
396    CREATE VIEW v_t3_a AS SELECT a.[9] FROM t3 AS a;
397    CREATE VIEW v_t3_b AS SELECT "4" FROM t3;
398    SELECT * FROM v_t3_a;
399  }
400} {1}
401do_test view-10.2 {
402  execsql {
403    SELECT * FROM v_t3_b;
404  }
405} {2}
406
407
408finish_test
409