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 #***********************************************************************
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 source $testdir/malloc_common.tcl
17 set testprefix countofview
23 INSERT INTO t2 VALUES(1), (2);
24 INSERT INTO t3 VALUES(3);
28 select c from t2 union all select f from t3 limit 1 offset 1
32 select count(*) from (
33 select c from t2 union all select f from t3 limit 1 offset 1
38 select count(*) from (
39 select c from t2 union all select f from t3
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;
51 SELECT count(*) FROM v1 GROUP BY y;
54 # 2023-03-01 dbsqlfuzz ef8623915d843b150c159166ee4548c78cc6895a
55 # count-of-view should not apply to CTEs.
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;
66 # 2023-03-07 dbsqlfuzz 23d782160b71c3f8f535ccb2da313dfc8eb8c631
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);
79 # 2023-03-31 dbsqlfuzz 6a107e3055bd22afab31cfddabc2d9d54fcbaf69
80 # Having clauses should disqualify count-of-view
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;
92 SELECT count(*) FROM t3 HAVING count(*)>5;
95 SELECT count(*) FROM t3 HAVING max(b)>'mmm';
98 SELECT count(*) FROM t3 HAVING min(b)>'mmm';
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