1# 2009 February 23 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 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. 9# 10#*********************************************************************** 11# This file implements regression tests for SQLite library. The 12# focus of this file is testing the reverse_select_order pragma. 13# 14# $Id: whereA.test,v 1.3 2009/06/10 19:33:29 drh Exp $ 15 16set testdir [file dirname $argv0] 17source $testdir/tester.tcl 18 19do_test whereA-1.1 { 20 db eval { 21 CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE, c); 22 INSERT INTO t1 VALUES(1,2,3); 23 INSERT INTO t1 values(2,'hello','world'); 24 INSERT INTO t1 VALUES(3,4.53,NULL); 25 SELECT * FROM t1 26 } 27} {1 2 3 2 hello world 3 4.53 {}} 28do_test whereA-1.2 { 29 db eval { 30 PRAGMA reverse_unordered_selects=1; 31 SELECT * FROM t1; 32 } 33} {3 4.53 {} 2 hello world 1 2 3} 34 35do_test whereA-1.3 { 36 db close 37 sqlite3 db test.db 38 db eval { 39 PRAGMA reverse_unordered_selects=1; 40 SELECT * FROM t1; 41 } 42} {3 4.53 {} 2 hello world 1 2 3} 43do_test whereA-1.4 { 44 db close 45 sqlite3 db test.db 46 db eval { 47 PRAGMA reverse_unordered_selects=1; 48 SELECT * FROM t1 ORDER BY rowid; 49 } 50} {1 2 3 2 hello world 3 4.53 {}} 51do_test whereA-1.5 { 52 db eval { 53 VACUUM; 54 SELECT * FROM t1 ORDER BY rowid; 55 } 56} {1 2 3 2 hello world 3 4.53 {}} 57do_test whereA-1.6 { 58 db eval { 59 PRAGMA reverse_unordered_selects; 60 } 61} {1} 62do_test whereA-1.7 { 63 db close 64 sqlite3 db test.db 65 db eval { 66 PRAGMA reverse_unordered_selects=1; 67 VACUUM; 68 SELECT * FROM t1; 69 } 70} {3 4.53 {} 2 hello world 1 2 3} 71do_execsql_test whereA-1.8 { 72 SELECT * FROM t1 WHERE b=2 AND a IS NULL; 73} {} 74do_execsql_test whereA-1.9 { 75 SELECT * FROM t1 WHERE b=2 AND a IS NOT NULL; 76} {1 2 3} 77 78do_test whereA-2.1 { 79 db eval { 80 PRAGMA reverse_unordered_selects=0; 81 SELECT * FROM t1 WHERE a>0; 82 } 83} {1 2 3 2 hello world 3 4.53 {}} 84do_test whereA-2.2 { 85 db eval { 86 PRAGMA reverse_unordered_selects=1; 87 SELECT * FROM t1 WHERE a>0; 88 } 89} {3 4.53 {} 2 hello world 1 2 3} 90 91do_test whereA-2.3 { 92 db eval { 93 PRAGMA reverse_unordered_selects=1; 94 SELECT * FROM t1 WHERE a>0 ORDER BY rowid; 95 } 96} {1 2 3 2 hello world 3 4.53 {}} 97 98do_test whereA-3.1 { 99 db eval { 100 PRAGMA reverse_unordered_selects=0; 101 SELECT * FROM t1 WHERE b>0; 102 } 103} {1 2 3 3 4.53 {} 2 hello world} 104do_test whereA-3.2 { 105 db eval { 106 PRAGMA reverse_unordered_selects=1; 107 SELECT * FROM t1 WHERE b>0; 108 } 109} {2 hello world 3 4.53 {} 1 2 3} 110do_test whereA-3.3 { 111 db eval { 112 PRAGMA reverse_unordered_selects=1; 113 SELECT * FROM t1 WHERE b>0 ORDER BY b; 114 } 115} {1 2 3 3 4.53 {} 2 hello world} 116 117do_test whereA-4.1 { 118 db eval { 119 CREATE TABLE t2(x); 120 INSERT INTO t2 VALUES(1); 121 INSERT INTO t2 VALUES(2); 122 SELECT x FROM t2; 123 } 124} {2 1} 125# Do an SQL statement. Append the search count to the end of the result. 126# 127proc count sql { 128 set ::sqlite_sort_count 0 129 return [concat [execsql $sql] $::sqlite_sort_count] 130} 131do_test whereA-4.2 { ;# Ticket #3904 132 db eval { 133 CREATE INDEX t2x ON t2(x); 134 } 135 count { 136 SELECT x FROM t2; 137 } 138} {2 1 0} 139do_test whereA-4.3 { 140 count { 141 SELECT x FROM t2 ORDER BY x; 142 } 143} {1 2 0} 144do_test whereA-4.4 { 145 count { 146 SELECT x FROM t2 ORDER BY x DESC; 147 } 148} {2 1 0} 149do_test whereA-4.5 { 150 db eval {DROP INDEX t2x;} 151 count { 152 SELECT x FROM t2 ORDER BY x; 153 } 154} {1 2 1} 155do_test whereA-4.6 { 156 count { 157 SELECT x FROM t2 ORDER BY x DESC; 158 } 159} {2 1 1} 160 161# Ticket https://sqlite.org/src/tktview/cb91bf4290c211 2017-08-01 162# Assertion fault following PRAGMA reverse_unordered_selects=ON. 163# 164do_execsql_test whereA-5.1 { 165 PRAGMA reverse_unordered_selects=on; 166 DROP TABLE IF EXISTS t1; 167 CREATE TABLE t1(a,b); 168 INSERT INTO t1 VALUES(1,2); 169 CREATE INDEX t1b ON t1(b); 170 SELECT a FROM t1 WHERE b=-99 OR b>1; 171} {1} 172 173# 2020-10-02 OSSFuzz find for an issue introduced by a check-in 174# on the previous day. 175# 176reset_db 177do_execsql_test whereA-6.1 { 178 CREATE TABLE t1(a, b); 179 CREATE INDEX t1aa ON t1(a,a); 180 INSERT INTO t1 VALUES(1,2); 181 ANALYZE; 182 UPDATE sqlite_stat1 SET stat='27 3 3' WHERE idx='t1aa'; 183 ANALYZE sqlite_schema; 184 PRAGMA reverse_unordered_selects (1) ; 185 SELECT a FROM t1 WHERE a=1 OR a=2; 186} {1} 187 188 189finish_test 190