xref: /sqlite-3.40.0/test/orderby5.test (revision 77c9b3cc)
1b8916be9Sdrh# 2013-06-14
2b8916be9Sdrh#
3b8916be9Sdrh# The author disclaims copyright to this source code.  In place of
4b8916be9Sdrh# a legal notice, here is a blessing:
5b8916be9Sdrh#
6b8916be9Sdrh#    May you do good and not evil.
7b8916be9Sdrh#    May you find forgiveness for yourself and forgive others.
8b8916be9Sdrh#    May you share freely, never taking more than you give.
9b8916be9Sdrh#
10b8916be9Sdrh#***********************************************************************
11b8916be9Sdrh# This file implements regression tests for SQLite library.  The
12b8916be9Sdrh# focus of this file is testing that the optimizations that disable
13b8916be9Sdrh# ORDER BY clauses work correctly
14b8916be9Sdrh#
15b8916be9Sdrh
16b8916be9Sdrh
17b8916be9Sdrhset testdir [file dirname $argv0]
18b8916be9Sdrhsource $testdir/tester.tcl
19b8916be9Sdrhset ::testprefix orderby5
20b8916be9Sdrh
21b8916be9Sdrh# Generate test data for a join.  Verify that the join gets the
22b8916be9Sdrh# correct answer.
23b8916be9Sdrh#
24b8916be9Sdrhdo_execsql_test 1.1 {
25b8916be9Sdrh  CREATE TABLE t1(a,b,c);
26b8916be9Sdrh  CREATE INDEX t1bc ON t1(b,c);
27b8916be9Sdrh
28b8916be9Sdrh  EXPLAIN QUERY PLAN
29b8916be9Sdrh  SELECT DISTINCT a, b, c FROM t1 WHERE a=0;
30b8916be9Sdrh} {~/B-TREE/}
31b8916be9Sdrhdo_execsql_test 1.2.1 {
32b8916be9Sdrh  EXPLAIN QUERY PLAN
33b8916be9Sdrh  SELECT DISTINCT a, c, b FROM t1 WHERE a=0;
34b8916be9Sdrh} {~/B-TREE/}
35b8916be9Sdrhdo_execsql_test 1.2.2 {
36b8916be9Sdrh  EXPLAIN QUERY PLAN
37b8916be9Sdrh  SELECT DISTINCT a, c, b FROM t1 WHERE a='xyz' COLLATE nocase;
38b8916be9Sdrh} {/B-TREE/}
39b8916be9Sdrhdo_execsql_test 1.2.3 {
40b8916be9Sdrh  EXPLAIN QUERY PLAN
41b8916be9Sdrh  SELECT DISTINCT a COLLATE nocase, c, b FROM t1 WHERE a='xyz';
42b8916be9Sdrh} {/B-TREE/}
43b8916be9Sdrhdo_execsql_test 1.2.4 {
44b8916be9Sdrh  EXPLAIN QUERY PLAN
45b8916be9Sdrh  SELECT DISTINCT a COLLATE nocase, c, b FROM t1 WHERE a='xyz' COLLATE nocase;
46b8916be9Sdrh} {~/B-TREE/}
47b8916be9Sdrhdo_execsql_test 1.3 {
48b8916be9Sdrh  EXPLAIN QUERY PLAN
49b8916be9Sdrh  SELECT DISTINCT b, a, c FROM t1 WHERE a=0;
50b8916be9Sdrh} {~/B-TREE/}
51b8916be9Sdrhdo_execsql_test 1.4 {
52b8916be9Sdrh  EXPLAIN QUERY PLAN
53b8916be9Sdrh  SELECT DISTINCT b, c, a FROM t1 WHERE a=0;
54b8916be9Sdrh} {~/B-TREE/}
55b8916be9Sdrhdo_execsql_test 1.5 {
56b8916be9Sdrh  EXPLAIN QUERY PLAN
57b8916be9Sdrh  SELECT DISTINCT c, a, b FROM t1 WHERE a=0;
58b8916be9Sdrh} {~/B-TREE/}
59b8916be9Sdrhdo_execsql_test 1.6 {
60b8916be9Sdrh  EXPLAIN QUERY PLAN
61b8916be9Sdrh  SELECT DISTINCT c, b, a FROM t1 WHERE a=0;
62b8916be9Sdrh} {~/B-TREE/}
63b8916be9Sdrhdo_execsql_test 1.7 {
64b8916be9Sdrh  EXPLAIN QUERY PLAN
65b8916be9Sdrh  SELECT DISTINCT c, b, a FROM t1 WHERE +a=0;
66b8916be9Sdrh} {/B-TREE/}
676284db90Sdrh
686284db90Sdrh# In some cases, it is faster to do repeated index lookups than it is to
696284db90Sdrh# sort.  But in other cases, it is faster to sort than to do repeated index
706284db90Sdrh# lookups.
716284db90Sdrh#
726284db90Sdrhdo_execsql_test 2.1a {
736284db90Sdrh  CREATE TABLE t2(a,b,c);
746284db90Sdrh  CREATE INDEX t2bc ON t2(b,c);
756284db90Sdrh  ANALYZE;
766284db90Sdrh  INSERT INTO sqlite_stat1 VALUES('t1','t1bc','1000000 10 9');
776284db90Sdrh  INSERT INTO sqlite_stat1 VALUES('t2','t2bc','100 10 5');
786284db90Sdrh  ANALYZE sqlite_master;
796284db90Sdrh
806284db90Sdrh  EXPLAIN QUERY PLAN
816284db90Sdrh  SELECT * FROM t2 WHERE a=0 ORDER BY a, b, c;
826284db90Sdrh} {~/B-TREE/}
832dd3cdcfSdan
846284db90Sdrhdo_execsql_test 2.1b {
85b8916be9Sdrh  EXPLAIN QUERY PLAN
86f559ed34Sdrh  SELECT * FROM t1 WHERE likelihood(a=0, 0.03) ORDER BY a, b, c;
876284db90Sdrh} {/B-TREE/}
886284db90Sdrh
89b8916be9Sdrhdo_execsql_test 2.2 {
90b8916be9Sdrh  EXPLAIN QUERY PLAN
91b8916be9Sdrh  SELECT * FROM t1 WHERE +a=0 ORDER BY a, b, c;
92b8916be9Sdrh} {/B-TREE/}
93b8916be9Sdrhdo_execsql_test 2.3 {
94b8916be9Sdrh  EXPLAIN QUERY PLAN
95b8916be9Sdrh  SELECT * FROM t1 WHERE a=0 ORDER BY b, a, c;
96b8916be9Sdrh} {~/B-TREE/}
97b8916be9Sdrhdo_execsql_test 2.4 {
98b8916be9Sdrh  EXPLAIN QUERY PLAN
99b8916be9Sdrh  SELECT * FROM t1 WHERE a=0 ORDER BY b, c, a;
100b8916be9Sdrh} {~/B-TREE/}
101b8916be9Sdrhdo_execsql_test 2.5 {
102b8916be9Sdrh  EXPLAIN QUERY PLAN
103b8916be9Sdrh  SELECT * FROM t1 WHERE a=0 ORDER BY a, c, b;
104b8916be9Sdrh} {/B-TREE/}
105b8916be9Sdrhdo_execsql_test 2.6 {
106b8916be9Sdrh  EXPLAIN QUERY PLAN
107b8916be9Sdrh  SELECT * FROM t1 WHERE a=0 ORDER BY c, a, b;
108b8916be9Sdrh} {/B-TREE/}
109b8916be9Sdrhdo_execsql_test 2.7 {
110b8916be9Sdrh  EXPLAIN QUERY PLAN
111b8916be9Sdrh  SELECT * FROM t1 WHERE a=0 ORDER BY c, b, a;
112b8916be9Sdrh} {/B-TREE/}
113b8916be9Sdrh
114b8916be9Sdrh
115416846a3Sdrhdo_execsql_test 3.0 {
116416846a3Sdrh  CREATE TABLE t3(a INTEGER PRIMARY KEY, b, c, d, e, f);
117416846a3Sdrh  CREATE INDEX t3bcde ON t3(b, c, d, e);
118416846a3Sdrh  EXPLAIN QUERY PLAN
119416846a3Sdrh  SELECT a FROM t3 WHERE b=2 AND c=3 ORDER BY d DESC, e DESC, b, c, a DESC;
120416846a3Sdrh} {~/B-TREE/}
121416846a3Sdrhdo_execsql_test 3.1 {
122416846a3Sdrh  DROP TABLE t3;
123416846a3Sdrh  CREATE TABLE t3(a INTEGER PRIMARY KEY, b, c, d, e, f) WITHOUT rowid;
124416846a3Sdrh  CREATE INDEX t3bcde ON t3(b, c, d, e);
125416846a3Sdrh  EXPLAIN QUERY PLAN
126416846a3Sdrh  SELECT a FROM t3 WHERE b=2 AND c=3 ORDER BY d DESC, e DESC, b, c, a DESC;
127416846a3Sdrh} {~/B-TREE/}
128416846a3Sdrh
12941aa442cSdan#-------------------------------------------------------------------------
13041aa442cSdando_execsql_test 4.1.0 {
13141aa442cSdan  CREATE TABLE t4(b COLLATE nocase);
13241aa442cSdan  INSERT INTO t4 VALUES('abc');
13341aa442cSdan  INSERT INTO t4 VALUES('ABC');
13441aa442cSdan  INSERT INTO t4 VALUES('aBC');
13541aa442cSdan}
13641aa442cSdando_execsql_test 4.1.1 {
13741aa442cSdan  SELECT * FROM t4 ORDER BY b COLLATE binary
13841aa442cSdan} {ABC aBC abc}
13941aa442cSdando_execsql_test 4.1.2 {
14041aa442cSdan  SELECT * FROM t4 WHERE b='abc' ORDER BY b COLLATE binary
14141aa442cSdan} {ABC aBC abc}
14241aa442cSdan
14341aa442cSdando_execsql_test 4.2.1 {
14441aa442cSdan  CREATE TABLE Records(typeID INTEGER, key TEXT COLLATE nocase, value TEXT);
14541aa442cSdan  CREATE INDEX RecordsIndex ON Records(typeID, key, value);
14641aa442cSdan}
14741aa442cSdando_execsql_test 4.2.2 {
14841aa442cSdan  explain query plan
14941aa442cSdan  SELECT typeID, key, value FROM Records
15041aa442cSdan  WHERE typeID = 2 AND key = 'x'
15141aa442cSdan  ORDER BY key, value;
15241aa442cSdan} {~/TEMP B-TREE/}
15341aa442cSdando_execsql_test 4.2.3 {
15441aa442cSdan  explain query plan
15541aa442cSdan  SELECT typeID, key, value FROM Records
15641aa442cSdan  WHERE typeID = 2 AND (key = 'x' COLLATE binary)
15741aa442cSdan  ORDER BY key, value;
15841aa442cSdan} {~/TEMP B-TREE/}
15941aa442cSdando_execsql_test 4.2.4 {
16041aa442cSdan  explain query plan
16141aa442cSdan  SELECT typeID, key, value FROM Records
16241aa442cSdan  WHERE typeID = 2
16341aa442cSdan  ORDER BY key, value;
16441aa442cSdan} {~/TEMP B-TREE/}
16541aa442cSdan
16641aa442cSdandb collate hello [list string match]
16741aa442cSdando_execsql_test 4.3.1 {
16841aa442cSdan  CREATE TABLE t5(a INTEGER PRIMARY KEY, b COLLATE hello, c, d);
16941aa442cSdan}
17041aa442cSdandb close
17141aa442cSdansqlite3 db test.db
17241aa442cSdando_catchsql_test 4.3.2 {
17341aa442cSdan  SELECT a FROM t5 WHERE b='def' ORDER BY b;
17441aa442cSdan} {1 {no such collation sequence: hello}}
175416846a3Sdrh
176*77c9b3ccSdrh# 2020-02-13 ticket 41c1456a6e61c0e7
177*77c9b3ccSdrhdo_execsql_test 4.4.0 {
178*77c9b3ccSdrh  DROP TABLE t1;
179*77c9b3ccSdrh  CREATE TABLE t1(a);
180*77c9b3ccSdrh  DROP TABLE t2;
181*77c9b3ccSdrh  CREATE TABLE t2(b INTEGER PRIMARY KEY, c INT);
182*77c9b3ccSdrh  SELECT DISTINCT *
183*77c9b3ccSdrh    FROM t1 LEFT JOIN t2 ON b=c AND b=(SELECT a FROM t1)
184*77c9b3ccSdrh   WHERE c>10;
185*77c9b3ccSdrh} {}
186*77c9b3ccSdrh
187b8916be9Sdrhfinish_test
188