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 that the optimizations that disable
13 # ORDER BY clauses work correctly
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set ::testprefix orderby5
21 # Generate test data for a join. Verify that the join gets the
25 CREATE TABLE t1(a,b,c);
26 CREATE INDEX t1bc ON t1(b,c);
29 SELECT DISTINCT a, b, c FROM t1 WHERE a=0;
31 do_execsql_test 1.2.1 {
33 SELECT DISTINCT a, c, b FROM t1 WHERE a=0;
35 do_execsql_test 1.2.2 {
37 SELECT DISTINCT a, c, b FROM t1 WHERE a='xyz' COLLATE nocase;
39 do_execsql_test 1.2.3 {
41 SELECT DISTINCT a COLLATE nocase, c, b FROM t1 WHERE a='xyz';
43 do_execsql_test 1.2.4 {
45 SELECT DISTINCT a COLLATE nocase, c, b FROM t1 WHERE a='xyz' COLLATE nocase;
49 SELECT DISTINCT b, a, c FROM t1 WHERE a=0;
53 SELECT DISTINCT b, c, a FROM t1 WHERE a=0;
57 SELECT DISTINCT c, a, b FROM t1 WHERE a=0;
61 SELECT DISTINCT c, b, a FROM t1 WHERE a=0;
65 SELECT DISTINCT c, b, a FROM t1 WHERE +a=0;
68 # In some cases, it is faster to do repeated index lookups than it is to
69 # sort. But in other cases, it is faster to sort than to do repeated index
72 do_execsql_test 2.1a {
73 CREATE TABLE t2(a,b,c);
74 CREATE INDEX t2bc ON t2(b,c);
76 INSERT INTO sqlite_stat1 VALUES('t1','t1bc','1000000 10 9');
77 INSERT INTO sqlite_stat1 VALUES('t2','t2bc','100 10 5');
78 ANALYZE sqlite_master;
81 SELECT * FROM t2 WHERE a=0 ORDER BY a, b, c;
84 do_execsql_test 2.1b {
86 SELECT * FROM t1 WHERE likelihood(a=0, 0.03) ORDER BY a, b, c;
91 SELECT * FROM t1 WHERE +a=0 ORDER BY a, b, c;
95 SELECT * FROM t1 WHERE a=0 ORDER BY b, a, c;
99 SELECT * FROM t1 WHERE a=0 ORDER BY b, c, a;
101 do_execsql_test 2.5 {
103 SELECT * FROM t1 WHERE a=0 ORDER BY a, c, b;
105 do_execsql_test 2.6 {
107 SELECT * FROM t1 WHERE a=0 ORDER BY c, a, b;
109 do_execsql_test 2.7 {
111 SELECT * FROM t1 WHERE a=0 ORDER BY c, b, a;
115 do_execsql_test 3.0 {
116 CREATE TABLE t3(a INTEGER PRIMARY KEY, b, c, d, e, f);
117 CREATE INDEX t3bcde ON t3(b, c, d, e);
119 SELECT a FROM t3 WHERE b=2 AND c=3 ORDER BY d DESC, e DESC, b, c, a DESC;
121 do_execsql_test 3.1 {
123 CREATE TABLE t3(a INTEGER PRIMARY KEY, b, c, d, e, f) WITHOUT rowid;
124 CREATE INDEX t3bcde ON t3(b, c, d, e);
126 SELECT a FROM t3 WHERE b=2 AND c=3 ORDER BY d DESC, e DESC, b, c, a DESC;
129 #-------------------------------------------------------------------------
130 do_execsql_test 4.1.0 {
131 CREATE TABLE t4(b COLLATE nocase);
132 INSERT INTO t4 VALUES('abc');
133 INSERT INTO t4 VALUES('ABC');
134 INSERT INTO t4 VALUES('aBC');
136 do_execsql_test 4.1.1 {
137 SELECT * FROM t4 ORDER BY b COLLATE binary
139 do_execsql_test 4.1.2 {
140 SELECT * FROM t4 WHERE b='abc' ORDER BY b COLLATE binary
143 do_execsql_test 4.2.1 {
144 CREATE TABLE Records(typeID INTEGER, key TEXT COLLATE nocase, value TEXT);
145 CREATE INDEX RecordsIndex ON Records(typeID, key, value);
147 do_execsql_test 4.2.2 {
149 SELECT typeID, key, value FROM Records
150 WHERE typeID = 2 AND key = 'x'
153 do_execsql_test 4.2.3 {
155 SELECT typeID, key, value FROM Records
156 WHERE typeID = 2 AND (key = 'x' COLLATE binary)
159 do_execsql_test 4.2.4 {
161 SELECT typeID, key, value FROM Records
166 db collate hello [list string match]
167 do_execsql_test 4.3.1 {
168 CREATE TABLE t5(a INTEGER PRIMARY KEY, b COLLATE hello, c, d);
172 do_catchsql_test 4.3.2 {
173 SELECT a FROM t5 WHERE b='def' ORDER BY b;
174 } {1 {no such collation sequence: hello}}
176 # 2020-02-13 ticket 41c1456a6e61c0e7
177 do_execsql_test 4.4.0 {
181 CREATE TABLE t2(b INTEGER PRIMARY KEY, c INT);
183 FROM t1 LEFT JOIN t2 ON b=c AND b=(SELECT a FROM t1)