1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/minmax3.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,217 @@
1.4 +# 2008 January 5
1.5 +#
1.6 +# The author disclaims copyright to this source code. In place of
1.7 +# a legal notice, here is a blessing:
1.8 +#
1.9 +# May you do good and not evil.
1.10 +# May you find forgiveness for yourself and forgive others.
1.11 +# May you share freely, never taking more than you give.
1.12 +#
1.13 +#***********************************************************************
1.14 +# $Id: minmax3.test,v 1.5 2008/07/12 14:52:20 drh Exp $
1.15 +
1.16 +set testdir [file dirname $argv0]
1.17 +source $testdir/tester.tcl
1.18 +
1.19 +# Do an SQL statement. Append the search count to the end of the result.
1.20 +#
1.21 +proc count sql {
1.22 + set ::sqlite_search_count 0
1.23 + return [concat [execsql $sql] $::sqlite_search_count]
1.24 +}
1.25 +
1.26 +# This procedure sets the value of the file-format in file 'test.db'
1.27 +# to $newval. Also, the schema cookie is incremented.
1.28 +#
1.29 +proc set_file_format {newval} {
1.30 + hexio_write test.db 44 [hexio_render_int32 $newval]
1.31 + set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
1.32 + incr schemacookie
1.33 + hexio_write test.db 40 [hexio_render_int32 $schemacookie]
1.34 + return {}
1.35 +}
1.36 +
1.37 +do_test minmax3-1.0 {
1.38 + execsql {
1.39 + CREATE TABLE t1(x, y, z);
1.40 + }
1.41 + db close
1.42 + set_file_format 4
1.43 + sqlite3 db test.db
1.44 + execsql {
1.45 + BEGIN;
1.46 + INSERT INTO t1 VALUES('1', 'I', 'one');
1.47 + INSERT INTO t1 VALUES('2', 'IV', 'four');
1.48 + INSERT INTO t1 VALUES('2', NULL, 'three');
1.49 + INSERT INTO t1 VALUES('2', 'II', 'two');
1.50 + INSERT INTO t1 VALUES('2', 'V', 'five');
1.51 + INSERT INTO t1 VALUES('3', 'VI', 'six');
1.52 + COMMIT;
1.53 + }
1.54 +} {}
1.55 +do_test minmax3-1.1.1 {
1.56 + # Linear scan.
1.57 + count { SELECT max(y) FROM t1 WHERE x = '2'; }
1.58 +} {V 5}
1.59 +do_test minmax3-1.1.2 {
1.60 + # Index optimizes the WHERE x='2' constraint.
1.61 + execsql { CREATE INDEX i1 ON t1(x) }
1.62 + count { SELECT max(y) FROM t1 WHERE x = '2'; }
1.63 +} {V 9}
1.64 +do_test minmax3-1.1.3 {
1.65 + # Index optimizes the WHERE x='2' constraint and the MAX(y).
1.66 + execsql { CREATE INDEX i2 ON t1(x,y) }
1.67 + count { SELECT max(y) FROM t1 WHERE x = '2'; }
1.68 +} {V 1}
1.69 +do_test minmax3-1.1.4 {
1.70 + # Index optimizes the WHERE x='2' constraint and the MAX(y).
1.71 + execsql { DROP INDEX i2 ; CREATE INDEX i2 ON t1(x, y DESC) }
1.72 + count { SELECT max(y) FROM t1 WHERE x = '2'; }
1.73 +} {V 1}
1.74 +do_test minmax3-1.1.5 {
1.75 + count { SELECT max(y) FROM t1 WHERE x = '2' AND y != 'V'; }
1.76 +} {IV 2}
1.77 +do_test minmax3-1.1.6 {
1.78 + count { SELECT max(y) FROM t1 WHERE x = '2' AND y < 'V'; }
1.79 +} {IV 1}
1.80 +do_test minmax3-1.1.6 {
1.81 + count { SELECT max(y) FROM t1 WHERE x = '2' AND z != 'five'; }
1.82 +} {IV 4}
1.83 +
1.84 +do_test minmax3-1.2.1 {
1.85 + # Linear scan of t1.
1.86 + execsql { DROP INDEX i1 ; DROP INDEX i2 }
1.87 + count { SELECT min(y) FROM t1 WHERE x = '2'; }
1.88 +} {II 5}
1.89 +do_test minmax3-1.2.2 {
1.90 + # Index i1 optimizes the WHERE x='2' constraint.
1.91 + execsql { CREATE INDEX i1 ON t1(x) }
1.92 + count { SELECT min(y) FROM t1 WHERE x = '2'; }
1.93 +} {II 9}
1.94 +do_test minmax3-1.2.3 {
1.95 + # Index i2 optimizes the WHERE x='2' constraint and the min(y).
1.96 + execsql { CREATE INDEX i2 ON t1(x,y) }
1.97 + count { SELECT min(y) FROM t1 WHERE x = '2'; }
1.98 +} {II 1}
1.99 +do_test minmax3-1.2.4 {
1.100 + # Index optimizes the WHERE x='2' constraint and the MAX(y).
1.101 + execsql { DROP INDEX i2 ; CREATE INDEX i2 ON t1(x, y DESC) }
1.102 + count { SELECT min(y) FROM t1 WHERE x = '2'; }
1.103 +} {II 1}
1.104 +
1.105 +do_test minmax3-1.3.1 {
1.106 + # Linear scan
1.107 + execsql { DROP INDEX i1 ; DROP INDEX i2 }
1.108 + count { SELECT min(y) FROM t1; }
1.109 +} {I 5}
1.110 +do_test minmax3-1.3.2 {
1.111 + # Index i1 optimizes the min(y)
1.112 + execsql { CREATE INDEX i1 ON t1(y) }
1.113 + count { SELECT min(y) FROM t1; }
1.114 +} {I 1}
1.115 +do_test minmax3-1.3.3 {
1.116 + # Index i1 optimizes the min(y)
1.117 + execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(y DESC) }
1.118 + count { SELECT min(y) FROM t1; }
1.119 +} {I 1}
1.120 +
1.121 +do_test minmax3-1.4.1 {
1.122 + # Linear scan
1.123 + execsql { DROP INDEX i1 }
1.124 + count { SELECT max(y) FROM t1; }
1.125 +} {VI 5}
1.126 +do_test minmax3-1.4.2 {
1.127 + # Index i1 optimizes the max(y)
1.128 + execsql { CREATE INDEX i1 ON t1(y) }
1.129 + count { SELECT max(y) FROM t1; }
1.130 +} {VI 0}
1.131 +do_test minmax3-1.4.3 {
1.132 + # Index i1 optimizes the max(y)
1.133 + execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(y DESC) }
1.134 + execsql { SELECT y from t1}
1.135 + count { SELECT max(y) FROM t1; }
1.136 +} {VI 0}
1.137 +do_test minmax3-1.4.4 {
1.138 + execsql { DROP INDEX i1 }
1.139 +} {}
1.140 +
1.141 +do_test minmax3-2.1 {
1.142 + execsql {
1.143 + CREATE TABLE t2(a, b);
1.144 + CREATE INDEX i3 ON t2(a, b);
1.145 + INSERT INTO t2 VALUES(1, NULL);
1.146 + INSERT INTO t2 VALUES(1, 1);
1.147 + INSERT INTO t2 VALUES(1, 2);
1.148 + INSERT INTO t2 VALUES(1, 3);
1.149 + INSERT INTO t2 VALUES(2, NULL);
1.150 + INSERT INTO t2 VALUES(2, 1);
1.151 + INSERT INTO t2 VALUES(2, 2);
1.152 + INSERT INTO t2 VALUES(2, 3);
1.153 + INSERT INTO t2 VALUES(3, 1);
1.154 + INSERT INTO t2 VALUES(3, 2);
1.155 + INSERT INTO t2 VALUES(3, 3);
1.156 + }
1.157 +} {}
1.158 +do_test minmax3-2.2 {
1.159 + execsql { SELECT min(b) FROM t2 WHERE a = 1; }
1.160 +} {1}
1.161 +do_test minmax3-2.3 {
1.162 + execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>1; }
1.163 +} {2}
1.164 +do_test minmax3-2.4 {
1.165 + execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>-1; }
1.166 +} {1}
1.167 +do_test minmax3-2.5 {
1.168 + execsql { SELECT min(b) FROM t2 WHERE a = 1; }
1.169 +} {1}
1.170 +do_test minmax3-2.6 {
1.171 + execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<2; }
1.172 +} {1}
1.173 +do_test minmax3-2.7 {
1.174 + execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<1; }
1.175 +} {{}}
1.176 +do_test minmax3-2.8 {
1.177 + execsql { SELECT min(b) FROM t2 WHERE a = 3 AND b<1; }
1.178 +} {{}}
1.179 +
1.180 +do_test minmax3-2.1 {
1.181 + execsql {
1.182 + DROP TABLE t2;
1.183 + CREATE TABLE t2(a, b);
1.184 + CREATE INDEX i3 ON t2(a, b DESC);
1.185 + INSERT INTO t2 VALUES(1, NULL);
1.186 + INSERT INTO t2 VALUES(1, 1);
1.187 + INSERT INTO t2 VALUES(1, 2);
1.188 + INSERT INTO t2 VALUES(1, 3);
1.189 + INSERT INTO t2 VALUES(2, NULL);
1.190 + INSERT INTO t2 VALUES(2, 1);
1.191 + INSERT INTO t2 VALUES(2, 2);
1.192 + INSERT INTO t2 VALUES(2, 3);
1.193 + INSERT INTO t2 VALUES(3, 1);
1.194 + INSERT INTO t2 VALUES(3, 2);
1.195 + INSERT INTO t2 VALUES(3, 3);
1.196 + }
1.197 +} {}
1.198 +do_test minmax3-2.2 {
1.199 + execsql { SELECT min(b) FROM t2 WHERE a = 1; }
1.200 +} {1}
1.201 +do_test minmax3-2.3 {
1.202 + execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>1; }
1.203 +} {2}
1.204 +do_test minmax3-2.4 {
1.205 + execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>-1; }
1.206 +} {1}
1.207 +do_test minmax3-2.5 {
1.208 + execsql { SELECT min(b) FROM t2 WHERE a = 1; }
1.209 +} {1}
1.210 +do_test minmax3-2.6 {
1.211 + execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<2; }
1.212 +} {1}
1.213 +do_test minmax3-2.7 {
1.214 + execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<1; }
1.215 +} {{}}
1.216 +do_test minmax3-2.8 {
1.217 + execsql { SELECT min(b) FROM t2 WHERE a = 3 AND b<1; }
1.218 +} {{}}
1.219 +
1.220 +finish_test