xref: /sqlite-3.40.0/test/orderby2.test (revision d663b5bd)
160441af4Sdrh# 2012 Sept 27
260441af4Sdrh#
360441af4Sdrh# The author disclaims copyright to this source code.  In place of
460441af4Sdrh# a legal notice, here is a blessing:
560441af4Sdrh#
660441af4Sdrh#    May you do good and not evil.
760441af4Sdrh#    May you find forgiveness for yourself and forgive others.
860441af4Sdrh#    May you share freely, never taking more than you give.
960441af4Sdrh#
1060441af4Sdrh#***********************************************************************
1160441af4Sdrh# This file implements regression tests for SQLite library.  The
1260441af4Sdrh# focus of this file is testing that the optimizations that disable
1360441af4Sdrh# ORDER BY clauses when the natural order of a query is correct.
1460441af4Sdrh#
1560441af4Sdrh
1660441af4Sdrh
1760441af4Sdrhset testdir [file dirname $argv0]
1860441af4Sdrhsource $testdir/tester.tcl
1960441af4Sdrhset ::testprefix orderby2
2060441af4Sdrh
2160441af4Sdrh# Generate test data for a join.  Verify that the join gets the
2260441af4Sdrh# correct answer.
2360441af4Sdrh#
2460441af4Sdrhdo_test 1.0 {
2560441af4Sdrh  db eval {
2660441af4Sdrh    CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
2760441af4Sdrh    INSERT INTO t1 VALUES(1,11), (2,22);
2860441af4Sdrh    CREATE TABLE t2(d, e, UNIQUE(d,e));
2960441af4Sdrh    INSERT INTO t2 VALUES(10, 'ten'), (11,'eleven'), (12,'twelve'),
3060441af4Sdrh                         (11, 'oneteen');
3160441af4Sdrh  }
3260441af4Sdrh} {}
3360441af4Sdrh
3460441af4Sdrhdo_test 1.1a {
3560441af4Sdrh  db eval {
3660441af4Sdrh    SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY d, e;
3760441af4Sdrh  }
3860441af4Sdrh} {eleven oneteen}
3960441af4Sdrhdo_test 1.1b {
4060441af4Sdrh  db eval {
4160441af4Sdrh    EXPLAIN QUERY PLAN
4260441af4Sdrh    SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY d, e;
4360441af4Sdrh  }
4460441af4Sdrh} {~/ORDER BY/}
4560441af4Sdrh
4660441af4Sdrhdo_test 1.2a {
4760441af4Sdrh  db eval {
4860441af4Sdrh    SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY e;
4960441af4Sdrh  }
5060441af4Sdrh} {eleven oneteen}
5160441af4Sdrhdo_test 1.2b {
5260441af4Sdrh  db eval {
5360441af4Sdrh    EXPLAIN QUERY PLAN
5460441af4Sdrh    SELECT e FROM t1, t2 WHERE a=1 AND d=b ORDER BY e;
5560441af4Sdrh  }
5660441af4Sdrh} {~/ORDER BY/}
5760441af4Sdrh
5860441af4Sdrhdo_test 1.3a {
5960441af4Sdrh  db eval {
6060441af4Sdrh    SELECT e, b FROM t1, t2 WHERE a=1 ORDER BY d, e;
6160441af4Sdrh  }
6260441af4Sdrh} {ten 11 eleven 11 oneteen 11 twelve 11}
6360441af4Sdrhdo_test 1.3b {
6460441af4Sdrh  db eval {
6560441af4Sdrh    EXPLAIN QUERY PLAN
6660441af4Sdrh    SELECT e, b FROM t1, t2 WHERE a=1 ORDER BY d, e;
6760441af4Sdrh  }
6860441af4Sdrh} {~/ORDER BY/}
6960441af4Sdrh
70*178eb61cSdrh# The following tests derived from TH3 test module cov1/where34.test
71*178eb61cSdrh#
72*178eb61cSdrhdo_test 2.0 {
73*178eb61cSdrh  db eval {
74*178eb61cSdrh    CREATE TABLE t31(a,b); CREATE INDEX t31ab ON t31(a,b);
75*178eb61cSdrh    CREATE TABLE t32(c,d); CREATE INDEX t32cd ON t32(c,d);
76*178eb61cSdrh    CREATE TABLE t33(e,f); CREATE INDEX t33ef ON t33(e,f);
77*178eb61cSdrh    CREATE TABLE t34(g,h); CREATE INDEX t34gh ON t34(g,h);
78*178eb61cSdrh
79*178eb61cSdrh    INSERT INTO t31 VALUES(1,4), (2,3), (1,3);
80*178eb61cSdrh    INSERT INTO t32 VALUES(4,5), (3,6), (3,7), (4,8);
81*178eb61cSdrh    INSERT INTO t33 VALUES(5,9), (7,10), (6,11), (8,12), (8,13), (7,14);
82*178eb61cSdrh    INSERT INTO t34 VALUES(11,20), (10,21), (12,22), (9,23), (13,24),
83*178eb61cSdrh                          (14,25), (12,26);
84*178eb61cSdrh    SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
85*178eb61cSdrh     WHERE c=b AND e=d AND g=f
86*178eb61cSdrh     ORDER BY a ASC, c ASC, e DESC, g ASC;
87*178eb61cSdrh  }
88*178eb61cSdrh} {1,3,7,10 1,3,7,14 1,3,6,11 1,4,8,12 1,4,8,12 1,4,8,13 1,4,5,9 2,3,7,10 2,3,7,14 2,3,6,11}
89*178eb61cSdrhdo_test 2.1 {
90*178eb61cSdrh  db eval {
91*178eb61cSdrh    SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
92*178eb61cSdrh     WHERE c=b AND e=d AND g=f
93*178eb61cSdrh     ORDER BY +a ASC, +c ASC, +e DESC, +g ASC;
94*178eb61cSdrh  }
95*178eb61cSdrh} {1,3,7,10 1,3,7,14 1,3,6,11 1,4,8,12 1,4,8,12 1,4,8,13 1,4,5,9 2,3,7,10 2,3,7,14 2,3,6,11}
9660441af4Sdrhdo_test 2.2 {
9760441af4Sdrh  db eval {
98    SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
99     WHERE c=b AND e=d AND g=f
100     ORDER BY a ASC, c ASC, e ASC, g ASC;
101  }
102} {1,3,6,11 1,3,7,10 1,3,7,14 1,4,5,9 1,4,8,12 1,4,8,12 1,4,8,13 2,3,6,11 2,3,7,10 2,3,7,14}
103do_test 2.3 {
104  optimization_control db cover-idx-scan off
105  db cache flush
106  db eval {
107    SELECT a||','||c||','||e||','||g FROM t31, t32, t33, t34
108     WHERE c=b AND e=d AND g=f
109     ORDER BY a ASC, c ASC, e ASC, g ASC;
110  }
111} {1,3,6,11 1,3,7,10 1,3,7,14 1,4,5,9 1,4,8,12 1,4,8,12 1,4,8,13 2,3,6,11 2,3,7,10 2,3,7,14}
112optimization_control db all on
113db cache flush
114
115
116
117finish_test
118