3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the reverse_select_order pragma.
14 # $Id: whereA.test,v 1.3 2009/06/10 19:33:29 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
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);
27 } {1 2 3 2 hello world 3 4.53 {}}
30 PRAGMA reverse_unordered_selects=1;
33 } {3 4.53 {} 2 hello world 1 2 3}
39 PRAGMA reverse_unordered_selects=1;
42 } {3 4.53 {} 2 hello world 1 2 3}
47 PRAGMA reverse_unordered_selects=1;
48 SELECT * FROM t1 ORDER BY rowid;
50 } {1 2 3 2 hello world 3 4.53 {}}
54 SELECT * FROM t1 ORDER BY rowid;
56 } {1 2 3 2 hello world 3 4.53 {}}
59 PRAGMA reverse_unordered_selects;
66 PRAGMA reverse_unordered_selects=1;
70 } {3 4.53 {} 2 hello world 1 2 3}
71 do_execsql_test whereA-1.8 {
72 SELECT * FROM t1 WHERE b=2 AND a IS NULL;
74 do_execsql_test whereA-1.9 {
75 SELECT * FROM t1 WHERE b=2 AND a IS NOT NULL;
80 PRAGMA reverse_unordered_selects=0;
81 SELECT * FROM t1 WHERE a>0;
83 } {1 2 3 2 hello world 3 4.53 {}}
86 PRAGMA reverse_unordered_selects=1;
87 SELECT * FROM t1 WHERE a>0;
89 } {3 4.53 {} 2 hello world 1 2 3}
93 PRAGMA reverse_unordered_selects=1;
94 SELECT * FROM t1 WHERE a>0 ORDER BY rowid;
96 } {1 2 3 2 hello world 3 4.53 {}}
100 PRAGMA reverse_unordered_selects=0;
101 SELECT * FROM t1 WHERE b>0;
103 } {1 2 3 3 4.53 {} 2 hello world}
106 PRAGMA reverse_unordered_selects=1;
107 SELECT * FROM t1 WHERE b>0;
109 } {2 hello world 3 4.53 {} 1 2 3}
112 PRAGMA reverse_unordered_selects=1;
113 SELECT * FROM t1 WHERE b>0 ORDER BY b;
115 } {1 2 3 3 4.53 {} 2 hello world}
120 INSERT INTO t2 VALUES(1);
121 INSERT INTO t2 VALUES(2);
125 # Do an SQL statement. Append the search count to the end of the result.
128 set ::sqlite_sort_count 0
129 return [concat [execsql $sql] $::sqlite_sort_count]
131 do_test whereA-4.2 { ;# Ticket #3904
133 CREATE INDEX t2x ON t2(x);
141 SELECT x FROM t2 ORDER BY x;
146 SELECT x FROM t2 ORDER BY x DESC;
150 db eval {DROP INDEX t2x;}
152 SELECT x FROM t2 ORDER BY x;
157 SELECT x FROM t2 ORDER BY x DESC;
161 # Ticket https://sqlite.org/src/tktview/cb91bf4290c211 2017-08-01
162 # Assertion fault following PRAGMA reverse_unordered_selects=ON.
164 do_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;
173 # 2020-10-02 OSSFuzz find for an issue introduced by a check-in
174 # on the previous day.
177 do_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);
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;