diff options
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/test/where3.test')
-rw-r--r-- | libraries/sqlite/unix/sqlite-3.5.1/test/where3.test | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/test/where3.test b/libraries/sqlite/unix/sqlite-3.5.1/test/where3.test new file mode 100644 index 0000000..30e0976 --- /dev/null +++ b/libraries/sqlite/unix/sqlite-3.5.1/test/where3.test | |||
@@ -0,0 +1,162 @@ | |||
1 | # 2006 January 31 | ||
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 join reordering optimization | ||
13 | # in cases that include a LEFT JOIN. | ||
14 | # | ||
15 | # $Id: where3.test,v 1.3 2006/12/16 16:25:17 drh Exp $ | ||
16 | |||
17 | set testdir [file dirname $argv0] | ||
18 | source $testdir/tester.tcl | ||
19 | |||
20 | # The following is from ticket #1652. | ||
21 | # | ||
22 | # A comma join then a left outer join: A,B left join C. | ||
23 | # Arrange indices so that the B table is chosen to go first. | ||
24 | # Also put an index on C, but make sure that A is chosen before C. | ||
25 | # | ||
26 | do_test where3-1.1 { | ||
27 | execsql { | ||
28 | CREATE TABLE t1(a, b); | ||
29 | CREATE TABLE t2(p, q); | ||
30 | CREATE TABLE t3(x, y); | ||
31 | |||
32 | INSERT INTO t1 VALUES(111,'one'); | ||
33 | INSERT INTO t1 VALUES(222,'two'); | ||
34 | INSERT INTO t1 VALUES(333,'three'); | ||
35 | |||
36 | INSERT INTO t2 VALUES(1,111); | ||
37 | INSERT INTO t2 VALUES(2,222); | ||
38 | INSERT INTO t2 VALUES(4,444); | ||
39 | CREATE INDEX t2i1 ON t2(p); | ||
40 | |||
41 | INSERT INTO t3 VALUES(999,'nine'); | ||
42 | CREATE INDEX t3i1 ON t3(x); | ||
43 | |||
44 | SELECT * FROM t1, t2 LEFT JOIN t3 ON q=x WHERE p=2 AND a=q; | ||
45 | } | ||
46 | } {222 two 2 222 {} {}} | ||
47 | |||
48 | # Ticket #1830 | ||
49 | # | ||
50 | # This is similar to the above but with the LEFT JOIN on the | ||
51 | # other side. | ||
52 | # | ||
53 | do_test where3-1.2 { | ||
54 | execsql { | ||
55 | CREATE TABLE parent1(parent1key, child1key, Child2key, child3key); | ||
56 | CREATE TABLE child1 ( child1key NVARCHAR, value NVARCHAR ); | ||
57 | CREATE UNIQUE INDEX PKIDXChild1 ON child1 ( child1key ); | ||
58 | CREATE TABLE child2 ( child2key NVARCHAR, value NVARCHAR ); | ||
59 | |||
60 | INSERT INTO parent1(parent1key,child1key,child2key) | ||
61 | VALUES ( 1, 'C1.1', 'C2.1' ); | ||
62 | INSERT INTO child1 ( child1key, value ) VALUES ( 'C1.1', 'Value for C1.1' ); | ||
63 | INSERT INTO child2 ( child2key, value ) VALUES ( 'C2.1', 'Value for C2.1' ); | ||
64 | |||
65 | INSERT INTO parent1 ( parent1key, child1key, child2key ) | ||
66 | VALUES ( 2, 'C1.2', 'C2.2' ); | ||
67 | INSERT INTO child2 ( child2key, value ) VALUES ( 'C2.2', 'Value for C2.2' ); | ||
68 | |||
69 | INSERT INTO parent1 ( parent1key, child1key, child2key ) | ||
70 | VALUES ( 3, 'C1.3', 'C2.3' ); | ||
71 | INSERT INTO child1 ( child1key, value ) VALUES ( 'C1.3', 'Value for C1.3' ); | ||
72 | INSERT INTO child2 ( child2key, value ) VALUES ( 'C2.3', 'Value for C2.3' ); | ||
73 | |||
74 | SELECT parent1.parent1key, child1.value, child2.value | ||
75 | FROM parent1 | ||
76 | LEFT OUTER JOIN child1 ON child1.child1key = parent1.child1key | ||
77 | INNER JOIN child2 ON child2.child2key = parent1.child2key; | ||
78 | } | ||
79 | } {1 {Value for C1.1} {Value for C2.1} 2 {} {Value for C2.2} 3 {Value for C1.3} {Value for C2.3}} | ||
80 | |||
81 | # This procedure executes the SQL. Then it appends | ||
82 | # the ::sqlite_query_plan variable. | ||
83 | # | ||
84 | proc queryplan {sql} { | ||
85 | set ::sqlite_sort_count 0 | ||
86 | set data [execsql $sql] | ||
87 | return [concat $data $::sqlite_query_plan] | ||
88 | } | ||
89 | |||
90 | |||
91 | # If you have a from clause of the form: A B C left join D | ||
92 | # then make sure the query optimizer is able to reorder the | ||
93 | # A B C part anyway it wants. | ||
94 | # | ||
95 | # Following the fix to ticket #1652, there was a time when | ||
96 | # the C table would not reorder. So the following reorderings | ||
97 | # were possible: | ||
98 | # | ||
99 | # A B C left join D | ||
100 | # B A C left join D | ||
101 | # | ||
102 | # But these reorders were not allowed | ||
103 | # | ||
104 | # C A B left join D | ||
105 | # A C B left join D | ||
106 | # C B A left join D | ||
107 | # B C A left join D | ||
108 | # | ||
109 | # The following tests are here to verify that the latter four | ||
110 | # reorderings are allowed again. | ||
111 | # | ||
112 | do_test where3-2.1 { | ||
113 | execsql { | ||
114 | CREATE TABLE tA(apk integer primary key, ax); | ||
115 | CREATE TABLE tB(bpk integer primary key, bx); | ||
116 | CREATE TABLE tC(cpk integer primary key, cx); | ||
117 | CREATE TABLE tD(dpk integer primary key, dx); | ||
118 | } | ||
119 | queryplan { | ||
120 | SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx | ||
121 | WHERE cpk=bx AND bpk=ax | ||
122 | } | ||
123 | } {tA {} tB * tC * tD *} | ||
124 | do_test where3-2.2 { | ||
125 | queryplan { | ||
126 | SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx | ||
127 | WHERE cpk=bx AND apk=bx | ||
128 | } | ||
129 | } {tB {} tA * tC * tD *} | ||
130 | do_test where3-2.3 { | ||
131 | queryplan { | ||
132 | SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx | ||
133 | WHERE cpk=bx AND apk=bx | ||
134 | } | ||
135 | } {tB {} tA * tC * tD *} | ||
136 | do_test where3-2.4 { | ||
137 | queryplan { | ||
138 | SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx | ||
139 | WHERE apk=cx AND bpk=ax | ||
140 | } | ||
141 | } {tC {} tA * tB * tD *} | ||
142 | do_test where3-2.5 { | ||
143 | queryplan { | ||
144 | SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx | ||
145 | WHERE cpk=ax AND bpk=cx | ||
146 | } | ||
147 | } {tA {} tC * tB * tD *} | ||
148 | do_test where3-2.5 { | ||
149 | queryplan { | ||
150 | SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx | ||
151 | WHERE bpk=cx AND apk=bx | ||
152 | } | ||
153 | } {tC {} tB * tA * tD *} | ||
154 | do_test where3-2.6 { | ||
155 | queryplan { | ||
156 | SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx | ||
157 | WHERE cpk=bx AND apk=cx | ||
158 | } | ||
159 | } {tB {} tC * tA * tD *} | ||
160 | |||
161 | |||
162 | finish_test | ||