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;
41 do_test view-1.1.100 {
42 db config enable_view off
44 SELECT * FROM v1 ORDER BY a;
46 } {1 {access to view "v1" prohibited}}
47 do_execsql_test view-1.1.101 {
48 CREATE TEMP VIEW v1temp AS SELECT a, b FROM t1;
49 SELECT * FROM v1temp ORDER BY a;
51 do_test view-1.1.110 {
52 db config enable_view on
54 SELECT * FROM v1 ORDER BY a;
55 SELECT * FROM v1temp ORDER BY a;
57 } {0 {1 2 4 5 7 8 1 2 4 5 7 8}}
59 do_execsql_test view-1.1.120 {
60 SELECT name, type FROM pragma_table_list('v1');
66 SELECT * FROM v1 ORDER BY a;
68 } {1 {no such table: v1}}
71 CREATE VIEW v1 AS SELECT a,b FROM t1;
72 SELECT * FROM v1 ORDER BY a;
79 SELECT * FROM v1 ORDER BY a;
84 DROP VIEW IF EXISTS v1;
85 SELECT * FROM v1 ORDER BY a;
87 } {1 {no such table: v1}}
90 CREATE VIEW v1 AS SELECT a,b FROM t1;
91 SELECT * FROM v1 ORDER BY a;
97 SELECT * FROM v1 ORDER BY a;
99 } {1 {no such table: main.t1}}
102 CREATE TABLE t1(x,a,b,c);
103 INSERT INTO t1 VALUES(1,2,3,4);
104 INSERT INTO t1 VALUES(4,5,6,7);
105 INSERT INTO t1 VALUES(7,8,9,10);
106 SELECT * FROM v1 ORDER BY a;
113 SELECT * FROM v1 ORDER BY a;
117 do_execsql_test view-1.10 {
118 CREATE TABLE t9(x INTEGER);
119 CREATE VIEW v9a AS SELECT x FROM t9;
120 CREATE VIEW v9b AS SELECT * FROM t9;
121 CREATE VIEW v9c(x) AS SELECT x FROM t9;
122 CREATE VIEW v9d(x) AS SELECT * FROM t9;
124 do_execsql_test view-1.11 {
125 PRAGMA table_info(v9a);
126 } {0 x INTEGER 0 {} 0}
127 do_execsql_test view-1.12 {
128 PRAGMA table_info(v9b);
129 } {0 x INTEGER 0 {} 0}
130 do_execsql_test view-1.13 {
131 PRAGMA table_info(v9c);
132 } {0 x INTEGER 0 {} 0}
133 do_execsql_test view-1.14 {
134 PRAGMA table_info(v9d);
135 } {0 x INTEGER 0 {} 0}
139 CREATE VIEW v2 AS SELECT * FROM t1 WHERE a>5
147 INSERT INTO v2 VALUES(1,2,3,4);
149 } {1 {cannot modify v2 because it is a view}}
152 UPDATE v2 SET a=10 WHERE a=5;
154 } {1 {cannot modify v2 because it is a view}}
159 } {1 {cannot modify v2 because it is a view}}
162 INSERT INTO t1 VALUES(11,12,13,14);
163 SELECT * FROM v2 ORDER BY x;
165 } {7 8 9 10 11 12 13 14}
168 SELECT x FROM v2 WHERE a>10
172 # Test that column name of views are generated correctly.
176 SELECT * FROM v1 LIMIT 1
181 SELECT * FROM v2 LIMIT 1
187 CREATE VIEW v1 AS SELECT a AS 'xyz', b+c AS 'pqr', c-b FROM t1;
188 SELECT * FROM v1 LIMIT 1
190 } {xyz 2 pqr 7 c-b 1}
193 CREATE VIEW v1b AS SELECT t1.a, b+c, t1.c FROM t1;
194 SELECT * FROM v1b LIMIT 1
199 CREATE VIEW v1c(x,y,z) AS SELECT a, b+c, c-b FROM t1;
200 SELECT * FROM v1c LIMIT 1;
203 do_catchsql_test view-3.3.4 {
204 CREATE VIEW v1err(x,y DESC,z) AS SELECT a, b+c, c-b FROM t1;
205 } {1 {syntax error after column name "y"}}
206 do_catchsql_test view-3.3.5 {
207 DROP VIEW IF EXISTS v1err;
208 CREATE VIEW v1err(x,y) AS SELECT a, b+c, c-b FROM t1;
210 } {1 {expected 2 columns for 'v1err' but got 3}}
211 do_catchsql_test view-3.3.6 {
212 DROP VIEW IF EXISTS v1err;
213 CREATE VIEW v1err(w,x,y,z) AS SELECT a, b+c, c-b FROM t1;
215 } {1 {expected 4 columns for 'v1err' but got 3}}
220 CREATE VIEW v3 AS SELECT a FROM t1 UNION SELECT b FROM t1 ORDER BY b;
221 SELECT * FROM v3 LIMIT 4;
229 SELECT b AS 'x', a AS 'y' FROM t1
231 SELECT b FROM v4 ORDER BY b LIMIT 4;
234 } ;# ifcapable compound
241 } {1 {use DROP TABLE to delete table t1}}
244 SELECT 1 FROM t1 LIMIT 1;
251 } {1 {use DROP VIEW to delete view v1}}
254 SELECT 1 FROM v1 LIMIT 1;
259 CREATE INDEX i1v1 ON v1(xyz);
261 } {1 {views may not be indexed}}
265 CREATE TABLE t2(y,a);
266 INSERT INTO t2 VALUES(22,2);
267 INSERT INTO t2 VALUES(33,3);
268 INSERT INTO t2 VALUES(44,4);
269 INSERT INTO t2 VALUES(55,5);
272 } {22 2 33 3 44 4 55 5}
276 SELECT t1.x AS v, t2.y AS w FROM t1 JOIN t2 USING(a);
281 # Verify that the view v5 gets flattened. see sqliteFlattenSubquery().
282 # This will only work if EXPLAIN is enabled.
285 ifcapable {explain} {
288 EXPLAIN SELECT * FROM v5;
293 SELECT * FROM v5 AS a, t2 AS b WHERE a.w=b.y;
295 } {1 22 22 2 4 55 55 5}
298 EXPLAIN SELECT * FROM v5 AS a, t2 AS b WHERE a.w=b.y;
303 SELECT * FROM t2 AS b, v5 AS a WHERE a.w=b.y;
305 } {22 2 1 22 55 5 4 55}
308 EXPLAIN SELECT * FROM t2 AS b, v5 AS a WHERE a.w=b.y;
313 SELECT * FROM t1 AS a, v5 AS b, t2 AS c WHERE a.x=b.v AND b.w=c.y;
315 } {1 2 3 4 1 22 22 2 4 5 6 7 4 55 55 5}
318 EXPLAIN SELECT * FROM t1 AS a, v5 AS b, t2 AS c WHERE a.x=b.v AND b.w=c.y;
325 SELECT min(x), min(a), min(b), min(c), min(a+b+c) FROM v2;
330 SELECT max(x), max(a), max(b), max(c), max(a+b+c) FROM v2;
336 CREATE TABLE test1(id integer primary key, a);
337 CREATE TABLE test2(id integer, b);
338 INSERT INTO test1 VALUES(1,2);
339 INSERT INTO test2 VALUES(1,3);
341 SELECT test1.id, a, b
342 FROM test1 JOIN test2 ON test2.id=test1.id;
357 SELECT test1.id, a, b
358 FROM test1 JOIN test2 USING(id);
373 SELECT test1.id, a, b
374 FROM test1 NATURAL JOIN test2;
388 CREATE VIEW v6 AS SELECT pqr, xyz FROM v1;
389 SELECT * FROM v6 ORDER BY xyz;
391 } {7 2 13 5 19 8 27 12}
396 SELECT * FROM v6 ORDER BY xyz;
398 } {7 2 13 5 19 8 27 12}
401 CREATE VIEW v7(a) AS SELECT pqr+xyz FROM v6;
402 SELECT * FROM v7 ORDER BY a;
409 CREATE VIEW v8 AS SELECT max(cnt) AS mx FROM
410 (SELECT a%2 AS eo, count(*) AS cnt FROM t1 GROUP BY eo);
416 SELECT mx+10, mx*2 FROM v8;
421 SELECT mx+10, pqr FROM v6, v8 WHERE xyz=2;
426 SELECT mx+10, pqr FROM v6, v8 WHERE xyz>2;
428 } {13 13 13 19 13 27}
429 } ;# ifcapable subquery
431 # Tests for a bug found by Michiel de Wit involving ORDER BY in a VIEW.
435 INSERT INTO t2 SELECT * FROM t2 WHERE a<5;
436 INSERT INTO t2 SELECT * FROM t2 WHERE a<4;
437 INSERT INTO t2 SELECT * FROM t2 WHERE a<3;
438 SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1;
443 SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1 LIMIT 3;
449 SELECT DISTINCT count(*) FROM t2 GROUP BY a ORDER BY 1 LIMIT 3;
455 SELECT * FROM v9 ORDER BY 1 DESC;
461 SELECT DISTINCT a, count(*) FROM t2 GROUP BY a ORDER BY 2 LIMIT 3;
467 SELECT * FROM v10 ORDER BY 1;
471 # Tables with columns having peculiar quoted names used in views
476 CREATE TABLE t3("9" integer, [4] text);
477 INSERT INTO t3 VALUES(1,2);
478 CREATE VIEW v_t3_a AS SELECT a.[9] FROM t3 AS a;
479 CREATE VIEW v_t3_b AS SELECT "4" FROM t3;
480 SELECT * FROM v_t3_a;
485 SELECT * FROM v_t3_b;
491 CREATE TABLE t4(a COLLATE NOCASE);
492 INSERT INTO t4 VALUES('This');
493 INSERT INTO t4 VALUES('this');
494 INSERT INTO t4 VALUES('THIS');
495 SELECT * FROM t4 WHERE a = 'THIS';
501 SELECT * FROM (SELECT * FROM t4) WHERE a = 'THIS';
507 CREATE VIEW v11 AS SELECT * FROM t4;
508 SELECT * FROM v11 WHERE a = 'THIS';
512 # Ticket #1270: Do not allow parameters in view definitions.
516 CREATE VIEW v12 AS SELECT a FROM t1 WHERE b=?
518 } {1 {parameters are not allowed in views}}
521 CREATE VIEW v12(x) AS SELECT a FROM t1 WHERE b=?
523 } {1 {parameters are not allowed in views}}
529 ATTACH 'test2.db' AS two;
530 CREATE TABLE two.t2(x,y);
531 CREATE VIEW v13 AS SELECT y FROM two.t2;
533 } {1 {view v13 cannot reference objects in database two}}
540 CREATE TEMP VIEW t1 AS SELECT a,b FROM t1;
541 SELECT * FROM temp.t1;
543 } {1 {view t1 is circularly defined}}
546 DROP VIEW IF EXISTS temp.t1;
547 CREATE TEMP VIEW t1(a,b) AS SELECT a,b FROM t1;
548 SELECT * FROM temp.t1;
550 } {1 {view t1 is circularly defined}}
552 # Tickets #1688, #1709
556 CREATE VIEW v15 AS SELECT a AS x, b AS y FROM t1;
557 SELECT * FROM v15 LIMIT 1;
562 SELECT x, y FROM v15 LIMIT 1
568 CREATE VIEW IF NOT EXISTS v1 AS SELECT * FROM t1;
573 SELECT sql FROM sqlite_master WHERE name='v1'
575 } {{CREATE VIEW v1 AS SELECT a AS 'xyz', b+c AS 'pqr', c-b FROM t1}}
578 DROP VIEW IF EXISTS nosuchview
582 # correct error message when attempting to drop a view that does not
589 } {1 {no such view: nosuchview}}
592 DROP VIEW main.nosuchview
594 } {1 {no such view: main.nosuchview}}
600 CREATE TABLE t1(a, b, c);
601 INSERT INTO t1 VALUES(1, 2, 3);
602 INSERT INTO t1 VALUES(4, 5, 6);
604 CREATE VIEW vv1 AS SELECT * FROM t1;
605 CREATE VIEW vv2 AS SELECT * FROM vv1;
606 CREATE VIEW vv3 AS SELECT * FROM vv2;
607 CREATE VIEW vv4 AS SELECT * FROM vv3;
608 CREATE VIEW vv5 AS SELECT * FROM vv4;
615 # Make sure "rowid" columns in a view are named correctly.
619 CREATE VIEW v3308a AS SELECT rowid, * FROM t1;
624 } {rowid 1 a 1 b 2 c 3 rowid 2 a 4 b 5 c 6}
627 CREATE VIEW v3308b AS SELECT t1.rowid, t1.a, t1.b+t1.c FROM t1;
632 } {rowid 1 a 1 t1.b+t1.c 5 rowid 2 a 4 t1.b+t1.c 11}
635 CREATE VIEW v3308c AS SELECT t1.oid, A, t1.b+t1.c AS x FROM t1;
640 } {rowid 1 a 1 x 5 rowid 2 a 4 x 11}
642 # Ticket #3539 had this crashing (see commit [5940]).
645 DROP TABLE IF EXISTS t1;
646 DROP VIEW IF EXISTS v1;
648 CREATE VIEW v1 AS SELECT c1 FROM (SELECT t1.c1 FROM t1);
654 do_execsql_test view-22.1 {
655 CREATE VIEW x1 AS SELECT 123 AS '', 234 AS '', 345 AS '';
660 db eval {SELECT * FROM x1} x break
661 lsort [array names x]
666 CREATE TABLE t25 (x);
667 INSERT INTO t25 (x) VALUES (1);
670 proc authLogDelete {code arg1 arg2 arg3 arg4 args} {
671 if {$code=="SQLITE_DELETE" && [string match sqlite_stat* $arg1]} {
672 # lappend ::log [list $code $arg1 $arg2 $arg3 $arg4 $args]
673 lappend ::log [list $code $arg1 $arg2 $arg3 $arg4]
678 db authorizer ::authLogDelete
679 db eval {DROP VIEW x1;}
683 set res [list {SQLITE_DELETE sqlite_stat1 {} main {}}]
684 ifcapable stat4 { lappend res {SQLITE_DELETE sqlite_stat4 {} main {}} }
687 db eval {DROP TABLE t25;}
691 #-------------------------------------------------------------------------
692 do_execsql_test view-26.0 {
693 CREATE TABLE t16(a, b, c UNIQUE);
694 INSERT INTO t16 VALUES(1, 1, 1);
695 INSERT INTO t16 VALUES(2, 2, 2);
696 INSERT INTO t16 VALUES(3, 3, 3);
697 CREATE VIEW v16 AS SELECT max(a) AS mx, min(b) AS mn FROM t16 GROUP BY c;
699 SELECT * FROM v16 AS one, v16 AS two WHERE one.mx=1;
705 do_execsql_test view-26.1 {
706 WITH v17(x,y) AS (SELECT max(a), min(b) FROM t16 GROUP BY c)
707 SELECT * FROM v17 AS one, v17 AS two WHERE one.x=1;
714 #-------------------------------------------------------------------------
716 do_execsql_test view-27.0 {
717 CREATE TABLE t0(c0 TEXT, c1);
718 INSERT INTO t0(c0, c1) VALUES (-1, 0);
719 CREATE VIEW v0(c0, c1) AS SELECT t0.c0, AVG(t0.c1) FROM t0;
722 do_execsql_test view-27.1 {
723 SELECT c0, typeof(c0), c1, typeof(c1) FROM v0;
729 do_execsql_test view-27.2 { SELECT c0<c1 FROM v0 } 1
730 do_execsql_test view-27.3 { SELECT c1<c0 FROM v0 } 0
731 do_execsql_test view-27.4 {
732 SELECT 1 FROM v0 WHERE c1<c0
734 do_execsql_test view-27.5 {
735 SELECT 1 FROM v0 WHERE c0<c1
738 do_execsql_test view-27.6 {
739 SELECT c0<c1 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0)
741 do_execsql_test view-27.7 {
742 SELECT c1<c0 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0)
744 do_execsql_test view-27.8 {
745 SELECT 1 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0) WHERE c1<c0
747 do_execsql_test view-27.9 {
748 SELECT 1 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0) WHERE c0<c1
751 #-------------------------------------------------------------------------
753 do_execsql_test view-28.0 {
754 CREATE TABLE t0(c0 TEXT);
755 CREATE VIEW v0(c0) AS SELECT t0.c0 FROM t0;
756 INSERT INTO t0(c0) VALUES ('0');
758 do_execsql_test view-28.1 {
759 SELECT 0 IN (c0) FROM t0;
761 do_execsql_test view-28.2 {
762 SELECT 0 IN (c0) FROM (SELECT c0 FROM t0);
765 #-------------------------------------------------------------------------
766 # 2020-10-26. https://sqlite.org/forum/forumpost/daa2c728cc
769 do_catchsql_test view-29.0 {
770 CREATE TABLE t1(a,b,c);
771 CREATE VIEW IF NOT EXISTS IF AS SELECT null;
772 } {1 {malformed database schema (IF) - near "AS": syntax error}}
773 do_catchsql_test view-29.1 {
774 CREATE TABLE t2(c,d,e);
775 SELECT name FROM sqlite_schema ORDER BY name;
778 #-------------------------------------------------------------------------
779 # 2022-12-11. https://sqlite.org/src/info/679ed6a2
781 # 2022-12-14 change: If the AS SELECT of a VIEW is a compound where
782 # the datatypes on each arm of the compound are different, then the
783 # datatype of the overall column is BLOB (ANY).
786 do_execsql_test view-30.0 {
787 CREATE TABLE t0(a INT, b TEXT);
789 INSERT INTO t0 VALUES(1,'one');
791 CREATE VIEW t1 AS SELECT a, b FROM t0 UNION ALL SELECT 2, 2;
792 CREATE VIEW t2(a,b) AS SELECT a, b FROM t0 UNION ALL SELECT 2, 2;
795 ifcapable schema_pragmas {
796 do_execsql_test view-30.1 {
797 PRAGMA table_info = t1;
798 } { 0 a INT 0 {} 0 1 b BLOB 0 {} 0 }
799 do_execsql_test view-30.2 {
800 PRAGMA table_info = t2;
801 } { 0 a INT 0 {} 0 1 b BLOB 0 {} 0 }
804 #-----------------------------------------------------------------------
805 # 2024-04-25 Trying to make type information on compound subqueries
806 # more predictable and rational.
809 do_execsql_test view-31.1 {
810 CREATE TABLE x2(b TEXT);
811 CREATE TABLE x1(a TEXT);
812 INSERT INTO x1 VALUES('123');
813 -- Two queries get the same result even though the order of terms
814 -- in the CTE is reversed
815 WITH c(x) AS ( SELECT b FROM x2 UNION SELECT 123 )
816 SELECT count(*) FROM x1 WHERE a IN c;
817 WITH c(x) AS ( SELECT 123 UNION SELECT b FROM x2 )
818 SELECT count(*) FROM x1 WHERE a IN c;
820 do_execsql_test view-31.2 {
821 CREATE TABLE t3(a INTEGER, b TEXT);
822 INSERT INTO t3 VALUES(123, 123);
823 WITH s AS ( VALUES(123), (456) ) SELECT * FROM t3 WHERE b IN s;