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.
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 set testprefix filter1
18 ifcapable !windowfunc {
25 CREATE INDEX i1 ON t1(a);
26 INSERT INTO t1 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9);
29 do_execsql_test 1.1 { SELECT sum(a) FROM t1; } 45
30 do_execsql_test 1.2 { SELECT sum(a) FILTER( WHERE a<5 ) FROM t1; } 10
33 SELECT sum(a) FILTER( WHERE a>9 ),
34 sum(a) FILTER( WHERE a>8 ),
35 sum(a) FILTER( WHERE a>7 ),
36 sum(a) FILTER( WHERE a>6 ),
37 sum(a) FILTER( WHERE a>5 ),
38 sum(a) FILTER( WHERE a>4 ),
39 sum(a) FILTER( WHERE a>3 ),
40 sum(a) FILTER( WHERE a>2 ),
41 sum(a) FILTER( WHERE a>1 ),
42 sum(a) FILTER( WHERE a>0 )
44 } {{} 9 17 24 30 35 39 42 44 45}
47 SELECT max(a) FILTER (WHERE (a % 2)==0) FROM t1
51 SELECT min(a) FILTER (WHERE a>4) FROM t1
55 SELECT count(*) FILTER (WHERE a!=5) FROM t1
59 SELECT min(a) FILTER (WHERE a>3) FROM t1 GROUP BY (a%2) ORDER BY 1;
64 SELECT sum(a) FILTER( WHERE a>9 ),
65 sum(a) FILTER( WHERE a>8 ),
66 sum(a) FILTER( WHERE a>7 ),
67 sum(a) FILTER( WHERE a>6 ),
68 sum(a) FILTER( WHERE a>5 ),
69 sum(a) FILTER( WHERE a>4 ),
70 sum(a) FILTER( WHERE a>3 ),
71 sum(a) FILTER( WHERE a>2 ),
72 sum(a) FILTER( WHERE a>1 ),
73 sum(a) FILTER( WHERE a>0 )
76 } {{} 9 17 24 30 35 39 42 44 45}
79 #-------------------------------------------------------------------------
82 # .1 FILTER on a non-aggregate function,
83 # .2 Window function in FILTER clause,
84 # .3 Aggregate function in FILTER clause,
89 INSERT INTO t1 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9);
92 do_catchsql_test 2.1 {
93 SELECT upper(a) FILTER (WHERE a=1) FROM t1
94 } {1 {FILTER may not be used with non-aggregate upper()}}
96 do_catchsql_test 2.2 {
97 SELECT sum(a) FILTER (WHERE 1 - max(a) OVER () > 0) FROM t1
98 } {1 {misuse of window function max()}}
100 do_catchsql_test 2.3 {
101 SELECT sum(a) FILTER (WHERE 1 - count(a)) FROM t1
102 } {1 {misuse of aggregate function count()}}
104 #-------------------------------------------------------------------------
106 do_execsql_test 3.0 {
107 CREATE TABLE t1(a,b);
108 INSERT INTO t1 VALUES(1, 1);
110 do_execsql_test 3.1 {
111 SELECT b, max(a) FILTER (WHERE b='x') FROM t1;
114 do_execsql_test 3.2 {
115 CREATE TABLE t2(a, b, c);
116 INSERT INTO t2 VALUES(1, 2, 3);
117 INSERT INTO t2 VALUES(1, 3, 4);
118 INSERT INTO t2 VALUES(2, 5, 6);
119 INSERT INTO t2 VALUES(2, 7, 8);
121 do_execsql_test 3.3 {
122 SELECT a, c, max(b) FILTER (WHERE c='x') FROM t2 GROUP BY a;
125 do_execsql_test 3.4 {
127 INSERT INTO t2 VALUES(1, 5, 'x');
128 INSERT INTO t2 VALUES(1, 2, 3);
129 INSERT INTO t2 VALUES(1, 4, 'x');
130 INSERT INTO t2 VALUES(2, 5, 6);
131 INSERT INTO t2 VALUES(2, 7, 8);
133 do_execsql_test 3.5 {
134 SELECT a, c, max(b) FILTER (WHERE c='x') FROM t2 GROUP BY a;
137 #-------------------------------------------------------------------------
139 do_execsql_test 4.0 {
140 CREATE TABLE t1(a, b, c);
141 INSERT INTO t1 VALUES('a', 0, 5);
142 INSERT INTO t1 VALUES('a', 1, 10);
143 INSERT INTO t1 VALUES('a', 0, 15);
145 INSERT INTO t1 VALUES('b', 0, 5);
146 INSERT INTO t1 VALUES('b', 1, 1000);
147 INSERT INTO t1 VALUES('b', 0, 5);
149 INSERT INTO t1 VALUES('c', 0, 1);
150 INSERT INTO t1 VALUES('c', 1, 2);
151 INSERT INTO t1 VALUES('c', 0, 3);
154 do_execsql_test 4.1 {
155 SELECT avg(c) FILTER (WHERE b!=1) AS h FROM t1 GROUP BY a ORDER BY h;
157 do_execsql_test 4.2 {
158 SELECT avg(c) FILTER (WHERE b!=1) AS h FROM t1 GROUP BY a ORDER BY (h+1.0);
160 do_execsql_test 4.3 {
161 SELECT a, avg(c) FILTER (WHERE b!=1) AS h FROM t1 GROUP BY a ORDER BY avg(c);
162 } {c 2.0 a 10.0 b 5.0}
163 do_execsql_test 4.4 {
164 SELECT a, avg(c) FILTER (WHERE b!=1) FROM t1 GROUP BY a ORDER BY 2
165 } {c 2.0 b 5.0 a 10.0}
167 #-------------------------------------------------------------------------
169 do_execsql_test 5.0 {
170 CREATE TABLE t1(a, b);
171 INSERT INTO t1 VALUES(1, 2);
172 INSERT INTO t1 VALUES(1, 3);
175 do_execsql_test 5.1 {
176 SELECT count(*) FILTER (WHERE b>2) FROM (SELECT * FROM t1)
179 do_execsql_test 5.2 {
180 SELECT count(*) FILTER (WHERE b>2) OVER () FROM (SELECT * FROM t1)
183 do_execsql_test 5.3 {
184 SELECT count(*) FILTER (WHERE b>2) OVER (ORDER BY b) FROM (SELECT * FROM t1)
187 #-------------------------------------------------------------------------
189 do_execsql_test 6.0 {
190 CREATE TABLE t1(a,b);
191 INSERT INTO t1 VALUES(1,1);
192 INSERT INTO t1 VALUES(2,2);
193 CREATE TABLE t2(x,y);
194 INSERT INTO t2 VALUES(1,1);
197 do_execsql_test 6.1 {
198 SELECT (SELECT COUNT(a) FILTER(WHERE x) FROM t2) FROM t1;
200 do_execsql_test 6.2 {
201 SELECT (SELECT COUNT(a+x) FROM t2) FROM t1;
203 do_execsql_test 6.3 {
204 SELECT (SELECT COUNT(a) FROM t2) FROM t1;
207 #-------------------------------------------------------------------------
209 do_execsql_test 7.0 {
210 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
211 INSERT INTO t1 VALUES(321, 100000);
212 INSERT INTO t1 VALUES(111, 110000);
213 INSERT INTO t1 VALUES(444, 120000);
214 INSERT INTO t1 VALUES(222, 130000);
217 do_execsql_test 7.1 {
218 SELECT max(a), max(a) FILTER (WHERE b<12345), b FROM t1;
223 # 2023-02-17 dbsqlfuzz 4f8e0de6e272bbbb3e1b41cb5aea31e0b47297e3
224 # count() with FILTER clause using the COUNTOFVIEW optimization.
227 do_execsql_test 8.0 {
228 CREATE TABLE t0(c0 INT);
229 CREATE TABLE t1a(a INTEGER PRIMARY KEY, b TEXT);
230 INSERT INTO t1a VALUES(1,'one'),(2,NULL),(3,'three');
231 CREATE TABLE t1b(c INTEGER PRIMARY KEY, d TEXT);
232 INSERT INTO t1b VALUES(4,'four'),(5,NULL),(6,'six');
233 CREATE VIEW t1 AS SELECT a, b FROM t1a UNION ALL SELECT c, d FROM t1b;
234 SELECT count()FILTER(WHERE b IS NULL) FROM t1;