sl@0: # 2008 January 5 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # $Id: minmax3.test,v 1.5 2008/07/12 14:52:20 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # Do an SQL statement. Append the search count to the end of the result. sl@0: # sl@0: proc count sql { sl@0: set ::sqlite_search_count 0 sl@0: return [concat [execsql $sql] $::sqlite_search_count] sl@0: } sl@0: sl@0: # This procedure sets the value of the file-format in file 'test.db' sl@0: # to $newval. Also, the schema cookie is incremented. sl@0: # sl@0: proc set_file_format {newval} { sl@0: hexio_write test.db 44 [hexio_render_int32 $newval] sl@0: set schemacookie [hexio_get_int [hexio_read test.db 40 4]] sl@0: incr schemacookie sl@0: hexio_write test.db 40 [hexio_render_int32 $schemacookie] sl@0: return {} sl@0: } sl@0: sl@0: do_test minmax3-1.0 { sl@0: execsql { sl@0: CREATE TABLE t1(x, y, z); sl@0: } sl@0: db close sl@0: set_file_format 4 sl@0: sqlite3 db test.db sl@0: execsql { sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES('1', 'I', 'one'); sl@0: INSERT INTO t1 VALUES('2', 'IV', 'four'); sl@0: INSERT INTO t1 VALUES('2', NULL, 'three'); sl@0: INSERT INTO t1 VALUES('2', 'II', 'two'); sl@0: INSERT INTO t1 VALUES('2', 'V', 'five'); sl@0: INSERT INTO t1 VALUES('3', 'VI', 'six'); sl@0: COMMIT; sl@0: } sl@0: } {} sl@0: do_test minmax3-1.1.1 { sl@0: # Linear scan. sl@0: count { SELECT max(y) FROM t1 WHERE x = '2'; } sl@0: } {V 5} sl@0: do_test minmax3-1.1.2 { sl@0: # Index optimizes the WHERE x='2' constraint. sl@0: execsql { CREATE INDEX i1 ON t1(x) } sl@0: count { SELECT max(y) FROM t1 WHERE x = '2'; } sl@0: } {V 9} sl@0: do_test minmax3-1.1.3 { sl@0: # Index optimizes the WHERE x='2' constraint and the MAX(y). sl@0: execsql { CREATE INDEX i2 ON t1(x,y) } sl@0: count { SELECT max(y) FROM t1 WHERE x = '2'; } sl@0: } {V 1} sl@0: do_test minmax3-1.1.4 { sl@0: # Index optimizes the WHERE x='2' constraint and the MAX(y). sl@0: execsql { DROP INDEX i2 ; CREATE INDEX i2 ON t1(x, y DESC) } sl@0: count { SELECT max(y) FROM t1 WHERE x = '2'; } sl@0: } {V 1} sl@0: do_test minmax3-1.1.5 { sl@0: count { SELECT max(y) FROM t1 WHERE x = '2' AND y != 'V'; } sl@0: } {IV 2} sl@0: do_test minmax3-1.1.6 { sl@0: count { SELECT max(y) FROM t1 WHERE x = '2' AND y < 'V'; } sl@0: } {IV 1} sl@0: do_test minmax3-1.1.6 { sl@0: count { SELECT max(y) FROM t1 WHERE x = '2' AND z != 'five'; } sl@0: } {IV 4} sl@0: sl@0: do_test minmax3-1.2.1 { sl@0: # Linear scan of t1. sl@0: execsql { DROP INDEX i1 ; DROP INDEX i2 } sl@0: count { SELECT min(y) FROM t1 WHERE x = '2'; } sl@0: } {II 5} sl@0: do_test minmax3-1.2.2 { sl@0: # Index i1 optimizes the WHERE x='2' constraint. sl@0: execsql { CREATE INDEX i1 ON t1(x) } sl@0: count { SELECT min(y) FROM t1 WHERE x = '2'; } sl@0: } {II 9} sl@0: do_test minmax3-1.2.3 { sl@0: # Index i2 optimizes the WHERE x='2' constraint and the min(y). sl@0: execsql { CREATE INDEX i2 ON t1(x,y) } sl@0: count { SELECT min(y) FROM t1 WHERE x = '2'; } sl@0: } {II 1} sl@0: do_test minmax3-1.2.4 { sl@0: # Index optimizes the WHERE x='2' constraint and the MAX(y). sl@0: execsql { DROP INDEX i2 ; CREATE INDEX i2 ON t1(x, y DESC) } sl@0: count { SELECT min(y) FROM t1 WHERE x = '2'; } sl@0: } {II 1} sl@0: sl@0: do_test minmax3-1.3.1 { sl@0: # Linear scan sl@0: execsql { DROP INDEX i1 ; DROP INDEX i2 } sl@0: count { SELECT min(y) FROM t1; } sl@0: } {I 5} sl@0: do_test minmax3-1.3.2 { sl@0: # Index i1 optimizes the min(y) sl@0: execsql { CREATE INDEX i1 ON t1(y) } sl@0: count { SELECT min(y) FROM t1; } sl@0: } {I 1} sl@0: do_test minmax3-1.3.3 { sl@0: # Index i1 optimizes the min(y) sl@0: execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(y DESC) } sl@0: count { SELECT min(y) FROM t1; } sl@0: } {I 1} sl@0: sl@0: do_test minmax3-1.4.1 { sl@0: # Linear scan sl@0: execsql { DROP INDEX i1 } sl@0: count { SELECT max(y) FROM t1; } sl@0: } {VI 5} sl@0: do_test minmax3-1.4.2 { sl@0: # Index i1 optimizes the max(y) sl@0: execsql { CREATE INDEX i1 ON t1(y) } sl@0: count { SELECT max(y) FROM t1; } sl@0: } {VI 0} sl@0: do_test minmax3-1.4.3 { sl@0: # Index i1 optimizes the max(y) sl@0: execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(y DESC) } sl@0: execsql { SELECT y from t1} sl@0: count { SELECT max(y) FROM t1; } sl@0: } {VI 0} sl@0: do_test minmax3-1.4.4 { sl@0: execsql { DROP INDEX i1 } sl@0: } {} sl@0: sl@0: do_test minmax3-2.1 { sl@0: execsql { sl@0: CREATE TABLE t2(a, b); sl@0: CREATE INDEX i3 ON t2(a, b); sl@0: INSERT INTO t2 VALUES(1, NULL); sl@0: INSERT INTO t2 VALUES(1, 1); sl@0: INSERT INTO t2 VALUES(1, 2); sl@0: INSERT INTO t2 VALUES(1, 3); sl@0: INSERT INTO t2 VALUES(2, NULL); sl@0: INSERT INTO t2 VALUES(2, 1); sl@0: INSERT INTO t2 VALUES(2, 2); sl@0: INSERT INTO t2 VALUES(2, 3); sl@0: INSERT INTO t2 VALUES(3, 1); sl@0: INSERT INTO t2 VALUES(3, 2); sl@0: INSERT INTO t2 VALUES(3, 3); sl@0: } sl@0: } {} sl@0: do_test minmax3-2.2 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1; } sl@0: } {1} sl@0: do_test minmax3-2.3 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>1; } sl@0: } {2} sl@0: do_test minmax3-2.4 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>-1; } sl@0: } {1} sl@0: do_test minmax3-2.5 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1; } sl@0: } {1} sl@0: do_test minmax3-2.6 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<2; } sl@0: } {1} sl@0: do_test minmax3-2.7 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<1; } sl@0: } {{}} sl@0: do_test minmax3-2.8 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 3 AND b<1; } sl@0: } {{}} sl@0: sl@0: do_test minmax3-2.1 { sl@0: execsql { sl@0: DROP TABLE t2; sl@0: CREATE TABLE t2(a, b); sl@0: CREATE INDEX i3 ON t2(a, b DESC); sl@0: INSERT INTO t2 VALUES(1, NULL); sl@0: INSERT INTO t2 VALUES(1, 1); sl@0: INSERT INTO t2 VALUES(1, 2); sl@0: INSERT INTO t2 VALUES(1, 3); sl@0: INSERT INTO t2 VALUES(2, NULL); sl@0: INSERT INTO t2 VALUES(2, 1); sl@0: INSERT INTO t2 VALUES(2, 2); sl@0: INSERT INTO t2 VALUES(2, 3); sl@0: INSERT INTO t2 VALUES(3, 1); sl@0: INSERT INTO t2 VALUES(3, 2); sl@0: INSERT INTO t2 VALUES(3, 3); sl@0: } sl@0: } {} sl@0: do_test minmax3-2.2 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1; } sl@0: } {1} sl@0: do_test minmax3-2.3 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>1; } sl@0: } {2} sl@0: do_test minmax3-2.4 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>-1; } sl@0: } {1} sl@0: do_test minmax3-2.5 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1; } sl@0: } {1} sl@0: do_test minmax3-2.6 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<2; } sl@0: } {1} sl@0: do_test minmax3-2.7 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<1; } sl@0: } {{}} sl@0: do_test minmax3-2.8 { sl@0: execsql { SELECT min(b) FROM t2 WHERE a = 3 AND b<1; } sl@0: } {{}} sl@0: sl@0: finish_test