Merge trunk into this branch.
[sqlite.git] / test / countofview.test
blobcea6fa4274035a7270eb93700f8c4bbec7ae1a86
1 # 2018-08-04
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
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 source $testdir/malloc_common.tcl
17 set testprefix countofview
19 do_execsql_test 1.0 {
20   CREATE TABLE t2(c);
21   CREATE TABLE t3(f);
23   INSERT INTO t2 VALUES(1), (2);
24   INSERT INTO t3 VALUES(3);
27 do_execsql_test 1.1 {
28   select c from t2 union all select f from t3 limit 1 offset 1
29 } {2}
31 do_execsql_test 1.2 {
32   select count(*) from (
33     select c from t2 union all select f from t3 limit 1 offset 1
34   )
35 } {1}
37 do_execsql_test 1.3 {
38   select count(*) from (
39     select c from t2 union all select f from t3
40   )
41 } {3}
43 # 2019-05-15
44 do_execsql_test 2.0 {
45   CREATE TABLE t1(x);
46   INSERT INTO t1 VALUES(1),(99),('abc');
47   CREATE VIEW v1(x,y) AS SELECT x,1 FROM t1 UNION ALL SELECT x,2 FROM t1;
48   SELECT count(*) FROM v1 WHERE x<>1;
49 } {4}
50 do_execsql_test 2.1 {
51   SELECT count(*) FROM v1 GROUP BY y;
52 } {3 3}
54 # 2023-03-01 dbsqlfuzz ef8623915d843b150c159166ee4548c78cc6895a
55 # count-of-view should not apply to CTEs.
57 ifcapable progress {
58   proc progress_stop args {return 1}
59   db progress 1000 progress_stop
60   do_catchsql_test 3.1 {
61     WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c)
62     SELECT count(*) FROM c;
63   } {1 interrupted}
66 # 2023-03-07 dbsqlfuzz 23d782160b71c3f8f535ccb2da313dfc8eb8c631
68 do_execsql_test 4.1 {
69   DROP TABLE t1;
70   DROP TABLE t2;
71   DROP TABLE t3;
72   CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT);
73   INSERT INTO t1 VALUES(4,'four');
74   CREATE TABLE t2(c INTEGER PRIMARY KEY, d TEXT);
75   CREATE VIEW t3 AS SELECT a, b FROM t1 UNION ALL SELECT c, d FROM t2;
76   SELECT count(*) FROM t3 ORDER BY sum(a);
77 } 1
79 # 2023-03-31 dbsqlfuzz 6a107e3055bd22afab31cfddabc2d9d54fcbaf69
80 # Having clauses should disqualify count-of-view
82 reset_db
83 do_execsql_test 5.1 {
84   CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT);
85   INSERT INTO t1 VALUES(1,'one'),(4,'four');
86   CREATE TABLE t2(c INTEGER PRIMARY KEY, d TEXT);
87   INSERT INTO t2 VALUES(2,'two'),(5,'five');
88   CREATE VIEW t3 AS SELECT a, b FROM t1 UNION ALL SELECT c, d FROM t2;
89   SELECT count(*) FROM t3 HAVING count(*)>0;
90 } 4
91 do_execsql_test 5.2 {
92   SELECT count(*) FROM t3 HAVING count(*)>5;
93 } {}
94 do_execsql_test 5.3 {
95   SELECT count(*) FROM t3 HAVING max(b)>'mmm';
96 } 4
97 do_execsql_test 5.4 {
98   SELECT count(*) FROM t3 HAVING min(b)>'mmm';
99 } {}
100 do_execsql_test 5.5 {
101   SELECT count(*) FROM (
102      SELECT a, max(b) FROM t1 HAVING a<100 UNION ALL SELECT c, d FROM t2
103   )
104 } 3
107 finish_test