xref: /illumos-gate/usr/src/lib/libsqlite/test/minmax.test (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
1#
2# 2001 September 15
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 SELECT statements that contain
14# aggregate min() and max() functions and which are handled as
15# as a special case.
16#
17# $Id: minmax.test,v 1.9.2.2 2004/07/18 21:14:05 drh Exp $
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22do_test minmax-1.0 {
23  execsql {
24    BEGIN;
25    CREATE TABLE t1(x, y);
26    INSERT INTO t1 VALUES(1,1);
27    INSERT INTO t1 VALUES(2,2);
28    INSERT INTO t1 VALUES(3,2);
29    INSERT INTO t1 VALUES(4,3);
30    INSERT INTO t1 VALUES(5,3);
31    INSERT INTO t1 VALUES(6,3);
32    INSERT INTO t1 VALUES(7,3);
33    INSERT INTO t1 VALUES(8,4);
34    INSERT INTO t1 VALUES(9,4);
35    INSERT INTO t1 VALUES(10,4);
36    INSERT INTO t1 VALUES(11,4);
37    INSERT INTO t1 VALUES(12,4);
38    INSERT INTO t1 VALUES(13,4);
39    INSERT INTO t1 VALUES(14,4);
40    INSERT INTO t1 VALUES(15,4);
41    INSERT INTO t1 VALUES(16,5);
42    INSERT INTO t1 VALUES(17,5);
43    INSERT INTO t1 VALUES(18,5);
44    INSERT INTO t1 VALUES(19,5);
45    INSERT INTO t1 VALUES(20,5);
46    COMMIT;
47    SELECT DISTINCT y FROM t1 ORDER BY y;
48  }
49} {1 2 3 4 5}
50
51do_test minmax-1.1 {
52  set sqlite_search_count 0
53  execsql {SELECT min(x) FROM t1}
54} {1}
55do_test minmax-1.2 {
56  set sqlite_search_count
57} {19}
58do_test minmax-1.3 {
59  set sqlite_search_count 0
60  execsql {SELECT max(x) FROM t1}
61} {20}
62do_test minmax-1.4 {
63  set sqlite_search_count
64} {19}
65do_test minmax-1.5 {
66  execsql {CREATE INDEX t1i1 ON t1(x)}
67  set sqlite_search_count 0
68  execsql {SELECT min(x) FROM t1}
69} {1}
70do_test minmax-1.6 {
71  set sqlite_search_count
72} {2}
73do_test minmax-1.7 {
74  set sqlite_search_count 0
75  execsql {SELECT max(x) FROM t1}
76} {20}
77do_test minmax-1.8 {
78  set sqlite_search_count
79} {1}
80do_test minmax-1.9 {
81  set sqlite_search_count 0
82  execsql {SELECT max(y) FROM t1}
83} {5}
84do_test minmax-1.10 {
85  set sqlite_search_count
86} {19}
87
88do_test minmax-2.0 {
89  execsql {
90    CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
91    INSERT INTO t2 SELECT * FROM t1;
92  }
93  set sqlite_search_count 0
94  execsql {SELECT min(a) FROM t2}
95} {1}
96do_test minmax-2.1 {
97  set sqlite_search_count
98} {0}
99do_test minmax-2.2 {
100  set sqlite_search_count 0
101  execsql {SELECT max(a) FROM t2}
102} {20}
103do_test minmax-2.3 {
104  set sqlite_search_count
105} {0}
106
107do_test minmax-3.0 {
108  execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
109  set sqlite_search_count 0
110  execsql {SELECT max(a) FROM t2}
111} {21}
112do_test minmax-3.1 {
113  set sqlite_search_count
114} {0}
115do_test minmax-3.2 {
116  execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
117  set sqlite_search_count 0
118  execsql {
119    SELECT b FROM t2 WHERE a=(SELECT max(a) FROM t2)
120  }
121} {999}
122do_test minmax-3.3 {
123  set sqlite_search_count
124} {0}
125
126do_test minmax-4.1 {
127  execsql {
128    SELECT coalesce(min(x+0),-1), coalesce(max(x+0),-1) FROM
129      (SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y')
130  }
131} {1 20}
132do_test minmax-4.2 {
133  execsql {
134    SELECT y, sum(x) FROM
135      (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1)
136    GROUP BY y ORDER BY y;
137  }
138} {1 1 2 5 3 22 4 92 5 90 6 0}
139do_test minmax-4.3 {
140  execsql {
141    SELECT y, count(x), count(*) FROM
142      (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1)
143    GROUP BY y ORDER BY y;
144  }
145} {1 1 1 2 2 3 3 4 5 4 8 9 5 5 6 6 0 1}
146
147# Make sure the min(x) and max(x) optimizations work on empty tables
148# including empty tables with indices. Ticket #296.
149#
150do_test minmax-5.1 {
151  execsql {
152    CREATE TABLE t3(x INTEGER UNIQUE NOT NULL);
153    SELECT coalesce(min(x),999) FROM t3;
154  }
155} {999}
156do_test minmax-5.2 {
157  execsql {
158    SELECT coalesce(min(rowid),999) FROM t3;
159  }
160} {999}
161do_test minmax-5.3 {
162  execsql {
163    SELECT coalesce(max(x),999) FROM t3;
164  }
165} {999}
166do_test minmax-5.4 {
167  execsql {
168    SELECT coalesce(max(rowid),999) FROM t3;
169  }
170} {999}
171do_test minmax-5.5 {
172  execsql {
173    SELECT coalesce(max(rowid),999) FROM t3 WHERE rowid<25;
174  }
175} {999}
176
177# Make sure the min(x) and max(x) optimizations work when there
178# is a LIMIT clause.  Ticket #396.
179#
180do_test minmax-6.1 {
181  execsql {
182    SELECT min(a) FROM t2 LIMIT 1
183  }
184} {1}
185do_test minmax-6.2 {
186  execsql {
187    SELECT max(a) FROM t2 LIMIT 3
188  }
189} {22}
190do_test minmax-6.3 {
191  execsql {
192    SELECT min(a) FROM t2 LIMIT 0,100
193  }
194} {1}
195do_test minmax-6.4 {
196  execsql {
197    SELECT max(a) FROM t2 LIMIT 1,100
198  }
199} {}
200do_test minmax-6.5 {
201  execsql {
202    SELECT min(x) FROM t3 LIMIT 1
203  }
204} {{}}
205do_test minmax-6.6 {
206  execsql {
207    SELECT max(x) FROM t3 LIMIT 0
208  }
209} {}
210do_test minmax-6.7 {
211  execsql {
212    SELECT max(a) FROM t2 LIMIT 0
213  }
214} {}
215
216# Make sure the max(x) and min(x) optimizations work for nested
217# queries.  Ticket #587.
218#
219do_test minmax-7.1 {
220  execsql {
221    SELECT max(x) FROM t1;
222  }
223} 20
224do_test minmax-7.2 {
225  execsql {
226    SELECT * FROM (SELECT max(x) FROM t1);
227  }
228} 20
229do_test minmax-7.3 {
230  execsql {
231    SELECT min(x) FROM t1;
232  }
233} 1
234do_test minmax-7.4 {
235  execsql {
236    SELECT * FROM (SELECT min(x) FROM t1);
237  }
238} 1
239
240# Make sure min(x) and max(x) work correctly when the datatype is
241# TEXT instead of NUMERIC.  Ticket #623.
242#
243do_test minmax-8.1 {
244  execsql {
245    CREATE TABLE t4(a TEXT);
246    INSERT INTO t4 VALUES('1234');
247    INSERT INTO t4 VALUES('234');
248    INSERT INTO t4 VALUES('34');
249    SELECT min(a), max(a) FROM t4;
250  }
251} {1234 34}
252do_test minmax-8.2 {
253  execsql {
254    CREATE TABLE t5(a INTEGER);
255    INSERT INTO t5 VALUES('1234');
256    INSERT INTO t5 VALUES('234');
257    INSERT INTO t5 VALUES('34');
258    SELECT min(a), max(a) FROM t5;
259  }
260} {34 1234}
261
262# Ticket #658:  Test the min()/max() optimization when the FROM clause
263# is a subquery.
264#
265do_test minmax-9.1 {
266  execsql {
267    SELECT max(rowid) FROM (
268      SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
269    )
270  }
271} {1}
272do_test minmax-9.2 {
273  execsql {
274    SELECT max(rowid) FROM (
275      SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5
276    )
277  }
278} {{}}
279
280# If there is a NULL in an aggregate max() or min(), ignore it.  An
281# aggregate min() or max() will only return NULL if all values are NULL.
282#
283do_test minmax-10.1 {
284  execsql {
285    CREATE TABLE t6(x);
286    INSERT INTO t6 VALUES(1);
287    INSERT INTO t6 VALUES(2);
288    INSERT INTO t6 VALUES(NULL);
289    SELECT coalesce(min(x),-1) FROM t6;
290  }
291} {1}
292do_test minmax-10.2 {
293  execsql {
294    SELECT max(x) FROM t6;
295  }
296} {2}
297do_test minmax-10.3 {
298  execsql {
299    CREATE INDEX i6 ON t6(x);
300    SELECT coalesce(min(x),-1) FROM t6;
301  }
302} {1}
303do_test minmax-10.4 {
304  execsql {
305    SELECT max(x) FROM t6;
306  }
307} {2}
308do_test minmax-10.5 {
309  execsql {
310    DELETE FROM t6 WHERE x NOT NULL;
311    SELECT count(*) FROM t6;
312  }
313} 1
314do_test minmax-10.6 {
315  execsql {
316    SELECT count(x) FROM t6;
317  }
318} 0
319do_test minmax-10.7 {
320  execsql {
321    SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
322  }
323} {{} {}}
324do_test minmax-10.8 {
325  execsql {
326    SELECT min(x), max(x) FROM t6;
327  }
328} {{} {}}
329do_test minmax-10.9 {
330  execsql {
331    INSERT INTO t6 SELECT * FROM t6;
332    INSERT INTO t6 SELECT * FROM t6;
333    INSERT INTO t6 SELECT * FROM t6;
334    INSERT INTO t6 SELECT * FROM t6;
335    INSERT INTO t6 SELECT * FROM t6;
336    INSERT INTO t6 SELECT * FROM t6;
337    INSERT INTO t6 SELECT * FROM t6;
338    INSERT INTO t6 SELECT * FROM t6;
339    INSERT INTO t6 SELECT * FROM t6;
340    INSERT INTO t6 SELECT * FROM t6;
341    SELECT count(*) FROM t6;
342  }
343} 1024
344do_test minmax-10.10 {
345  execsql {
346    SELECT count(x) FROM t6;
347  }
348} 0
349do_test minmax-10.11 {
350  execsql {
351    SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
352  }
353} {{} {}}
354do_test minmax-10.12 {
355  execsql {
356    SELECT min(x), max(x) FROM t6;
357  }
358} {{} {}}
359
360finish_test
361