sl@0: # 2005 December 21 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: # This file implements regression tests for SQLite library. The sl@0: # focus of this script is descending indices. sl@0: # sl@0: # $Id: descidx1.test,v 1.10 2008/03/19 00:21:31 drh Exp $ sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: db eval {PRAGMA legacy_file_format=OFF} 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: # This procedure returns the value of the file-format in file 'test.db'. sl@0: # sl@0: proc get_file_format {{fname test.db}} { sl@0: return [hexio_get_int [hexio_read $fname 44 4]] sl@0: } sl@0: sl@0: sl@0: # Verify that the file format starts as 4. sl@0: # sl@0: do_test descidx1-1.1 { sl@0: execsql { sl@0: CREATE TABLE t1(a,b); sl@0: CREATE INDEX i1 ON t1(b ASC); sl@0: } sl@0: get_file_format sl@0: } {4} sl@0: do_test descidx1-1.2 { sl@0: execsql { sl@0: CREATE INDEX i2 ON t1(a DESC); sl@0: } sl@0: get_file_format sl@0: } {4} sl@0: sl@0: # Put some information in the table and verify that the descending sl@0: # index actually works. sl@0: # sl@0: do_test descidx1-2.1 { sl@0: execsql { sl@0: INSERT INTO t1 VALUES(1,1); sl@0: INSERT INTO t1 VALUES(2,2); sl@0: INSERT INTO t1 SELECT a+2, a+2 FROM t1; sl@0: INSERT INTO t1 SELECT a+4, a+4 FROM t1; sl@0: SELECT b FROM t1 WHERE a>3 AND a<7; sl@0: } sl@0: } {6 5 4} sl@0: do_test descidx1-2.2 { sl@0: execsql { sl@0: SELECT a FROM t1 WHERE b>3 AND b<7; sl@0: } sl@0: } {4 5 6} sl@0: do_test descidx1-2.3 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a>=3 AND a<7; sl@0: } sl@0: } {6 5 4 3} sl@0: do_test descidx1-2.4 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a>3 AND a<=7; sl@0: } sl@0: } {7 6 5 4} sl@0: do_test descidx1-2.5 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a>=3 AND a<=7; sl@0: } sl@0: } {7 6 5 4 3} sl@0: do_test descidx1-2.6 { sl@0: execsql { sl@0: SELECT a FROM t1 WHERE b>=3 AND b<=7; sl@0: } sl@0: } {3 4 5 6 7} sl@0: sl@0: # This procedure executes the SQL. Then it checks to see if the OP_Sort sl@0: # opcode was executed. If an OP_Sort did occur, then "sort" is appended sl@0: # to the result. If no OP_Sort happened, then "nosort" is appended. sl@0: # sl@0: # This procedure is used to check to make sure sorting is or is not sl@0: # occurring as expected. sl@0: # sl@0: proc cksort {sql} { sl@0: set ::sqlite_sort_count 0 sl@0: set data [execsql $sql] sl@0: if {$::sqlite_sort_count} {set x sort} {set x nosort} sl@0: lappend data $x sl@0: return $data sl@0: } sl@0: sl@0: # Test sorting using a descending index. sl@0: # sl@0: do_test descidx1-3.1 { sl@0: cksort {SELECT a FROM t1 ORDER BY a} sl@0: } {1 2 3 4 5 6 7 8 nosort} sl@0: do_test descidx1-3.2 { sl@0: cksort {SELECT a FROM t1 ORDER BY a ASC} sl@0: } {1 2 3 4 5 6 7 8 nosort} sl@0: do_test descidx1-3.3 { sl@0: cksort {SELECT a FROM t1 ORDER BY a DESC} sl@0: } {8 7 6 5 4 3 2 1 nosort} sl@0: do_test descidx1-3.4 { sl@0: cksort {SELECT b FROM t1 ORDER BY a} sl@0: } {1 2 3 4 5 6 7 8 nosort} sl@0: do_test descidx1-3.5 { sl@0: cksort {SELECT b FROM t1 ORDER BY a ASC} sl@0: } {1 2 3 4 5 6 7 8 nosort} sl@0: do_test descidx1-3.6 { sl@0: cksort {SELECT b FROM t1 ORDER BY a DESC} sl@0: } {8 7 6 5 4 3 2 1 nosort} sl@0: do_test descidx1-3.7 { sl@0: cksort {SELECT a FROM t1 ORDER BY b} sl@0: } {1 2 3 4 5 6 7 8 nosort} sl@0: do_test descidx1-3.8 { sl@0: cksort {SELECT a FROM t1 ORDER BY b ASC} sl@0: } {1 2 3 4 5 6 7 8 nosort} sl@0: do_test descidx1-3.9 { sl@0: cksort {SELECT a FROM t1 ORDER BY b DESC} sl@0: } {8 7 6 5 4 3 2 1 nosort} sl@0: do_test descidx1-3.10 { sl@0: cksort {SELECT b FROM t1 ORDER BY b} sl@0: } {1 2 3 4 5 6 7 8 nosort} sl@0: do_test descidx1-3.11 { sl@0: cksort {SELECT b FROM t1 ORDER BY b ASC} sl@0: } {1 2 3 4 5 6 7 8 nosort} sl@0: do_test descidx1-3.12 { sl@0: cksort {SELECT b FROM t1 ORDER BY b DESC} sl@0: } {8 7 6 5 4 3 2 1 nosort} sl@0: sl@0: do_test descidx1-3.21 { sl@0: cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a} sl@0: } {4 5 6 7 nosort} sl@0: do_test descidx1-3.22 { sl@0: cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC} sl@0: } {4 5 6 7 nosort} sl@0: do_test descidx1-3.23 { sl@0: cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC} sl@0: } {7 6 5 4 nosort} sl@0: do_test descidx1-3.24 { sl@0: cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a} sl@0: } {4 5 6 7 nosort} sl@0: do_test descidx1-3.25 { sl@0: cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC} sl@0: } {4 5 6 7 nosort} sl@0: do_test descidx1-3.26 { sl@0: cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC} sl@0: } {7 6 5 4 nosort} sl@0: sl@0: # Create a table with indices that are descending on some terms and sl@0: # ascending on others. sl@0: # sl@0: ifcapable bloblit { sl@0: do_test descidx1-4.1 { sl@0: execsql { sl@0: CREATE TABLE t2(a INT, b TEXT, c BLOB, d REAL); sl@0: CREATE INDEX i3 ON t2(a ASC, b DESC, c ASC); sl@0: CREATE INDEX i4 ON t2(b DESC, a ASC, d DESC); sl@0: INSERT INTO t2 VALUES(1,'one',x'31',1.0); sl@0: INSERT INTO t2 VALUES(2,'two',x'3232',2.0); sl@0: INSERT INTO t2 VALUES(3,'three',x'333333',3.0); sl@0: INSERT INTO t2 VALUES(4,'four',x'34343434',4.0); sl@0: INSERT INTO t2 VALUES(5,'five',x'3535353535',5.0); sl@0: INSERT INTO t2 VALUES(6,'six',x'363636363636',6.0); sl@0: INSERT INTO t2 VALUES(2,'two',x'323232',2.1); sl@0: INSERT INTO t2 VALUES(2,'zwei',x'3232',2.2); sl@0: INSERT INTO t2 VALUES(2,NULL,NULL,2.3); sl@0: SELECT count(*) FROM t2; sl@0: } sl@0: } {9} sl@0: do_test descidx1-4.2 { sl@0: execsql { sl@0: SELECT d FROM t2 ORDER BY a; sl@0: } sl@0: } {1.0 2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0} sl@0: do_test descidx1-4.3 { sl@0: execsql { sl@0: SELECT d FROM t2 WHERE a>=2; sl@0: } sl@0: } {2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0} sl@0: do_test descidx1-4.4 { sl@0: execsql { sl@0: SELECT d FROM t2 WHERE a>2; sl@0: } sl@0: } {3.0 4.0 5.0 6.0} sl@0: do_test descidx1-4.5 { sl@0: execsql { sl@0: SELECT d FROM t2 WHERE a=2 AND b>'two'; sl@0: } sl@0: } {2.2} sl@0: do_test descidx1-4.6 { sl@0: execsql { sl@0: SELECT d FROM t2 WHERE a=2 AND b>='two'; sl@0: } sl@0: } {2.2 2.0 2.1} sl@0: do_test descidx1-4.7 { sl@0: execsql { sl@0: SELECT d FROM t2 WHERE a=2 AND b<'two'; sl@0: } sl@0: } {} sl@0: do_test descidx1-4.8 { sl@0: execsql { sl@0: SELECT d FROM t2 WHERE a=2 AND b<='two'; sl@0: } sl@0: } {2.0 2.1} sl@0: } sl@0: sl@0: do_test descidx1-5.1 { sl@0: execsql { sl@0: CREATE TABLE t3(a,b,c,d); sl@0: CREATE INDEX t3i1 ON t3(a DESC, b ASC, c DESC, d ASC); sl@0: INSERT INTO t3 VALUES(0,0,0,0); sl@0: INSERT INTO t3 VALUES(0,0,0,1); sl@0: INSERT INTO t3 VALUES(0,0,1,0); sl@0: INSERT INTO t3 VALUES(0,0,1,1); sl@0: INSERT INTO t3 VALUES(0,1,0,0); sl@0: INSERT INTO t3 VALUES(0,1,0,1); sl@0: INSERT INTO t3 VALUES(0,1,1,0); sl@0: INSERT INTO t3 VALUES(0,1,1,1); sl@0: INSERT INTO t3 VALUES(1,0,0,0); sl@0: INSERT INTO t3 VALUES(1,0,0,1); sl@0: INSERT INTO t3 VALUES(1,0,1,0); sl@0: INSERT INTO t3 VALUES(1,0,1,1); sl@0: INSERT INTO t3 VALUES(1,1,0,0); sl@0: INSERT INTO t3 VALUES(1,1,0,1); sl@0: INSERT INTO t3 VALUES(1,1,1,0); sl@0: INSERT INTO t3 VALUES(1,1,1,1); sl@0: SELECT count(*) FROM t3; sl@0: } sl@0: } {16} sl@0: do_test descidx1-5.2 { sl@0: cksort { sl@0: SELECT a||b||c||d FROM t3 ORDER BY a,b,c,d; sl@0: } sl@0: } {0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 sort} sl@0: do_test descidx1-5.3 { sl@0: cksort { sl@0: SELECT a||b||c||d FROM t3 ORDER BY a DESC, b ASC, c DESC, d ASC; sl@0: } sl@0: } {1010 1011 1000 1001 1110 1111 1100 1101 0010 0011 0000 0001 0110 0111 0100 0101 nosort} sl@0: do_test descidx1-5.4 { sl@0: cksort { sl@0: SELECT a||b||c||d FROM t3 ORDER BY a ASC, b DESC, c ASC, d DESC; sl@0: } sl@0: } {0101 0100 0111 0110 0001 0000 0011 0010 1101 1100 1111 1110 1001 1000 1011 1010 nosort} sl@0: do_test descidx1-5.5 { sl@0: cksort { sl@0: SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a DESC, b ASC, c DESC sl@0: } sl@0: } {101 100 111 110 001 000 011 010 nosort} sl@0: do_test descidx1-5.6 { sl@0: cksort { sl@0: SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b DESC, c ASC sl@0: } sl@0: } {010 011 000 001 110 111 100 101 nosort} sl@0: do_test descidx1-5.7 { sl@0: cksort { sl@0: SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b DESC, c DESC sl@0: } sl@0: } {011 010 001 000 111 110 101 100 sort} sl@0: do_test descidx1-5.8 { sl@0: cksort { sl@0: SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b ASC, c ASC sl@0: } sl@0: } {000 001 010 011 100 101 110 111 sort} sl@0: do_test descidx1-5.9 { sl@0: cksort { sl@0: SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a DESC, b DESC, c ASC sl@0: } sl@0: } {110 111 100 101 010 011 000 001 sort} sl@0: sl@0: # Test the legacy_file_format pragma here because we have access to sl@0: # the get_file_format command. sl@0: # sl@0: ifcapable legacyformat { sl@0: do_test descidx1-6.1 { sl@0: db close sl@0: file delete -force test.db test.db-journal sl@0: sqlite3 db test.db sl@0: execsql {PRAGMA legacy_file_format} sl@0: } {1} sl@0: } else { sl@0: do_test descidx1-6.1 { sl@0: db close sl@0: file delete -force test.db test.db-journal sl@0: sqlite3 db test.db sl@0: execsql {PRAGMA legacy_file_format} sl@0: } {0} sl@0: } sl@0: do_test descidx1-6.2 { sl@0: execsql {PRAGMA legacy_file_format=YES} sl@0: execsql {PRAGMA legacy_file_format} sl@0: } {1} sl@0: do_test descidx1-6.3 { sl@0: execsql { sl@0: CREATE TABLE t1(a,b,c); sl@0: } sl@0: get_file_format sl@0: } {1} sl@0: ifcapable vacuum { sl@0: # Verify that the file format is preserved across a vacuum. sl@0: do_test descidx1-6.3.1 { sl@0: execsql {VACUUM} sl@0: get_file_format sl@0: } {1} sl@0: } sl@0: do_test descidx1-6.4 { sl@0: db close sl@0: file delete -force test.db test.db-journal sl@0: sqlite3 db test.db sl@0: execsql {PRAGMA legacy_file_format=NO} sl@0: execsql {PRAGMA legacy_file_format} sl@0: } {0} sl@0: do_test descidx1-6.5 { sl@0: execsql { sl@0: CREATE TABLE t1(a,b,c); sl@0: CREATE INDEX i1 ON t1(a ASC, b DESC, c ASC); sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: INSERT INTO t1 VALUES(1,1,0); sl@0: INSERT INTO t1 VALUES(1,2,1); sl@0: INSERT INTO t1 VALUES(1,3,4); sl@0: } sl@0: get_file_format sl@0: } {4} sl@0: ifcapable vacuum { sl@0: # Verify that the file format is preserved across a vacuum. sl@0: do_test descidx1-6.6 { sl@0: execsql {VACUUM} sl@0: get_file_format sl@0: } {4} sl@0: do_test descidx1-6.7 { sl@0: execsql { sl@0: PRAGMA legacy_file_format=ON; sl@0: VACUUM; sl@0: } sl@0: get_file_format sl@0: } {4} sl@0: } sl@0: sl@0: sl@0: sl@0: finish_test