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 join reordering optimization
13 # in cases that include a LEFT JOIN.
15 # $Id: where3.test,v 1.4 2008/04/17 19:14:02 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # The following is from ticket #1652.
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.
28 CREATE TABLE t1(a, b);
29 CREATE TABLE t2(p, q);
30 CREATE TABLE t3(x, y);
32 INSERT INTO t1 VALUES(111,'one');
33 INSERT INTO t1 VALUES(222,'two');
34 INSERT INTO t1 VALUES(333,'three');
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);
41 INSERT INTO t3 VALUES(999,'nine');
42 CREATE INDEX t3i1 ON t3(x);
44 SELECT * FROM t1, t2 LEFT JOIN t3 ON q=x WHERE p=2 AND a=q;
46 } {222 two 2 222 {} {}}
48 ifcapable explain&&!cursorhints {
49 do_test where3-1.1.1 {
50 explain_no_trace {SELECT * FROM t1, t2 LEFT JOIN t3 ON q=x
52 } [explain_no_trace {SELECT * FROM t1, t2 LEFT JOIN t3 ON x=q
58 # This is similar to the above but with the LEFT JOIN on the
63 CREATE TABLE parent1(parent1key, child1key, Child2key, child3key);
64 CREATE TABLE child1 ( child1key NVARCHAR, value NVARCHAR );
65 CREATE UNIQUE INDEX PKIDXChild1 ON child1 ( child1key );
66 CREATE TABLE child2 ( child2key NVARCHAR, value NVARCHAR );
68 INSERT INTO parent1(parent1key,child1key,child2key)
69 VALUES ( 1, 'C1.1', 'C2.1' );
70 INSERT INTO child1 ( child1key, value ) VALUES ( 'C1.1', 'Value for C1.1' );
71 INSERT INTO child2 ( child2key, value ) VALUES ( 'C2.1', 'Value for C2.1' );
73 INSERT INTO parent1 ( parent1key, child1key, child2key )
74 VALUES ( 2, 'C1.2', 'C2.2' );
75 INSERT INTO child2 ( child2key, value ) VALUES ( 'C2.2', 'Value for C2.2' );
77 INSERT INTO parent1 ( parent1key, child1key, child2key )
78 VALUES ( 3, 'C1.3', 'C2.3' );
79 INSERT INTO child1 ( child1key, value ) VALUES ( 'C1.3', 'Value for C1.3' );
80 INSERT INTO child2 ( child2key, value ) VALUES ( 'C2.3', 'Value for C2.3' );
82 SELECT parent1.parent1key, child1.value, child2.value
84 LEFT OUTER JOIN child1 ON child1.child1key = parent1.child1key
85 INNER JOIN child2 ON child2.child2key = parent1.child2key;
87 } {1 {Value for C1.1} {Value for C2.1} 2 {} {Value for C2.2} 3 {Value for C1.3} {Value for C2.3}}
89 ifcapable explain&&!cursorhints {
90 do_test where3-1.2.1 {
92 SELECT parent1.parent1key, child1.value, child2.value
94 LEFT OUTER JOIN child1 ON child1.child1key = parent1.child1key
95 INNER JOIN child2 ON child2.child2key = parent1.child2key;
98 SELECT parent1.parent1key, child1.value, child2.value
100 LEFT OUTER JOIN child1 ON parent1.child1key = child1.child1key
101 INNER JOIN child2 ON child2.child2key = parent1.child2key;
105 # This procedure executes the SQL. Then it appends
106 # the names of the table and index used
108 proc queryplan {sql} {
109 set ::sqlite_sort_count 0
110 set data [execsql $sql]
111 set eqp [execsql "EXPLAIN QUERY PLAN $sql"]
113 foreach {a b c x} $eqp {
114 if {[regexp { TABLE (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \
115 $x all as tab idx]} {
116 lappend data $tab $idx
117 } elseif {[regexp { TABLE (\w+ AS )?(\w+)\y} $x all as tab]} {
125 # If you have a from clause of the form: A B C left join D
126 # then make sure the query optimizer is able to reorder the
127 # A B C part anyway it wants.
129 # Following the fix to ticket #1652, there was a time when
130 # the C table would not reorder. So the following reorderings
136 # But these reorders were not allowed
143 # The following tests are here to verify that the latter four
144 # reorderings are allowed again.
148 CREATE TABLE tA(apk integer primary key, ax);
149 CREATE TABLE tB(bpk integer primary key, bx);
150 CREATE TABLE tC(cpk integer primary key, cx);
151 CREATE TABLE tD(dpk integer primary key, dx);
154 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
155 WHERE cpk=bx AND bpk=ax
157 } {tA * tB * tC * tD *}
158 do_test where3-2.1.1 {
160 SELECT * FROM tA, tB, tC LEFT JOIN tD ON cx=dpk
161 WHERE cpk=bx AND bpk=ax
163 } {tA * tB * tC * tD *}
164 do_test where3-2.1.2 {
166 SELECT * FROM tA, tB, tC LEFT JOIN tD ON cx=dpk
167 WHERE bx=cpk AND bpk=ax
169 } {tA * tB * tC * tD *}
170 do_test where3-2.1.3 {
172 SELECT * FROM tA, tB, tC LEFT JOIN tD ON cx=dpk
173 WHERE bx=cpk AND ax=bpk
175 } {tA * tB * tC * tD *}
176 do_test where3-2.1.4 {
178 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
179 WHERE bx=cpk AND ax=bpk
181 } {tA * tB * tC * tD *}
182 do_test where3-2.1.5 {
184 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
185 WHERE cpk=bx AND ax=bpk
187 } {tA * tB * tC * tD *}
190 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
191 WHERE cpk=bx AND apk=bx
193 } {tB * tA * tC * tD *}
196 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
197 WHERE cpk=bx AND apk=bx
199 } {tB * tA * tC * tD *}
202 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
203 WHERE apk=cx AND bpk=ax
205 } {tC * tA * tB * tD *}
208 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
209 WHERE cpk=ax AND bpk=cx
211 } {tA * tC * tB * tD *}
214 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
215 WHERE bpk=cx AND apk=bx
217 } {tC * tB * tA * tD *}
220 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
221 WHERE cpk=bx AND apk=cx
223 } {tB * tC * tA * tD *}
225 # Ticket [13f033c865f878953]
226 # If the outer loop must be a full table scan, do not let ANALYZE trick
227 # the planner into use a table for the outer loop that might be indexable
228 # if held until an inner loop.
230 do_execsql_test where3-3.0 {
231 CREATE TABLE t301(a INTEGER PRIMARY KEY,b,c);
232 CREATE INDEX t301c ON t301(c);
233 INSERT INTO t301 VALUES(1,2,3);
234 INSERT INTO t301 VALUES(2,2,3);
235 CREATE TABLE t302(x, y);
236 INSERT INTO t302 VALUES(4,5);
239 do_eqp_test where3-3.0a {
240 SELECT * FROM t302, t301 WHERE t302.x=5 AND t301.a=t302.y;
244 `--SEARCH TABLE t301 USING INTEGER PRIMARY KEY (rowid=?)
246 do_eqp_test where3-3.1 {
247 SELECT * FROM t301, t302 WHERE t302.x=5 AND t301.a=t302.y;
251 `--SEARCH TABLE t301 USING INTEGER PRIMARY KEY (rowid=?)
253 do_execsql_test where3-3.2 {
254 SELECT * FROM t301 WHERE c=3 AND a IS NULL;
256 do_execsql_test where3-3.3 {
257 SELECT * FROM t301 WHERE c=3 AND a IS NOT NULL;
260 if 0 { # Query planner no longer does this
261 # Verify that when there are multiple tables in a join which must be
262 # full table scans that the query planner attempts put the table with
263 # the fewest number of output rows as the outer loop.
265 do_execsql_test where3-4.0 {
266 CREATE TABLE t400(a INTEGER PRIMARY KEY, b, c);
267 CREATE TABLE t401(p INTEGER PRIMARY KEY, q, r);
268 CREATE TABLE t402(x INTEGER PRIMARY KEY, y, z);
270 SELECT * FROM t400, t401, t402 WHERE t402.z GLOB 'abc*';
272 0 0 2 {SCAN TABLE t402}
273 0 1 0 {SCAN TABLE t400}
274 0 2 1 {SCAN TABLE t401}
276 do_execsql_test where3-4.1 {
278 SELECT * FROM t400, t401, t402 WHERE t401.r GLOB 'abc*';
280 0 0 1 {SCAN TABLE t401}
281 0 1 0 {SCAN TABLE t400}
282 0 2 2 {SCAN TABLE t402}
284 do_execsql_test where3-4.2 {
286 SELECT * FROM t400, t401, t402 WHERE t400.c GLOB 'abc*';
288 0 0 0 {SCAN TABLE t400}
289 0 1 1 {SCAN TABLE t401}
290 0 2 2 {SCAN TABLE t402}
294 # Verify that a performance regression encountered by firefox
297 do_execsql_test where3-5.0 {
298 CREATE TABLE aaa (id INTEGER PRIMARY KEY, type INTEGER,
299 fk INTEGER DEFAULT NULL, parent INTEGER,
300 position INTEGER, title LONGVARCHAR,
301 keyword_id INTEGER, folder_type TEXT,
302 dateAdded INTEGER, lastModified INTEGER);
303 CREATE INDEX aaa_111 ON aaa (fk, type);
304 CREATE INDEX aaa_222 ON aaa (parent, position);
305 CREATE INDEX aaa_333 ON aaa (fk, lastModified);
306 CREATE TABLE bbb (id INTEGER PRIMARY KEY, type INTEGER,
307 fk INTEGER DEFAULT NULL, parent INTEGER,
308 position INTEGER, title LONGVARCHAR,
309 keyword_id INTEGER, folder_type TEXT,
310 dateAdded INTEGER, lastModified INTEGER);
311 CREATE INDEX bbb_111 ON bbb (fk, type);
312 CREATE INDEX bbb_222 ON bbb (parent, position);
313 CREATE INDEX bbb_333 ON bbb (fk, lastModified);
315 do_eqp_test where3-5.0a {
316 SELECT bbb.title AS tag_title
317 FROM aaa JOIN bbb ON bbb.id = aaa.parent
318 WHERE aaa.fk = 'constant'
319 AND LENGTH(bbb.title) > 0
321 ORDER BY bbb.title COLLATE NOCASE ASC;
324 |--SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)
325 |--SEARCH TABLE bbb USING INTEGER PRIMARY KEY (rowid=?)
326 `--USE TEMP B-TREE FOR ORDER BY
328 do_eqp_test where3-5.1 {
329 SELECT bbb.title AS tag_title
330 FROM aaa JOIN aaa AS bbb ON bbb.id = aaa.parent
331 WHERE aaa.fk = 'constant'
332 AND LENGTH(bbb.title) > 0
334 ORDER BY bbb.title COLLATE NOCASE ASC;
337 |--SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)
338 |--SEARCH TABLE aaa AS bbb USING INTEGER PRIMARY KEY (rowid=?)
339 `--USE TEMP B-TREE FOR ORDER BY
341 do_eqp_test where3-5.2 {
342 SELECT bbb.title AS tag_title
343 FROM bbb JOIN aaa ON bbb.id = aaa.parent
344 WHERE aaa.fk = 'constant'
345 AND LENGTH(bbb.title) > 0
347 ORDER BY bbb.title COLLATE NOCASE ASC;
350 |--SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)
351 |--SEARCH TABLE bbb USING INTEGER PRIMARY KEY (rowid=?)
352 `--USE TEMP B-TREE FOR ORDER BY
354 do_eqp_test where3-5.3 {
355 SELECT bbb.title AS tag_title
356 FROM aaa AS bbb JOIN aaa ON bbb.id = aaa.parent
357 WHERE aaa.fk = 'constant'
358 AND LENGTH(bbb.title) > 0
360 ORDER BY bbb.title COLLATE NOCASE ASC;
363 |--SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)
364 |--SEARCH TABLE aaa AS bbb USING INTEGER PRIMARY KEY (rowid=?)
365 `--USE TEMP B-TREE FOR ORDER BY
368 # Name resolution with NATURAL JOIN and USING
370 do_test where3-6.setup {
372 CREATE TABLE t6w(a, w);
373 INSERT INTO t6w VALUES(1, 'w-one');
374 INSERT INTO t6w VALUES(2, 'w-two');
375 INSERT INTO t6w VALUES(9, 'w-nine');
376 CREATE TABLE t6x(a, x);
377 INSERT INTO t6x VALUES(1, 'x-one');
378 INSERT INTO t6x VALUES(3, 'x-three');
379 INSERT INTO t6x VALUES(9, 'x-nine');
380 CREATE TABLE t6y(a, y);
381 INSERT INTO t6y VALUES(1, 'y-one');
382 INSERT INTO t6y VALUES(4, 'y-four');
383 INSERT INTO t6y VALUES(9, 'y-nine');
384 CREATE TABLE t6z(a, z);
385 INSERT INTO t6z VALUES(1, 'z-one');
386 INSERT INTO t6z VALUES(5, 'z-five');
387 INSERT INTO t6z VALUES(9, 'z-nine');
397 {WHERE a>0 ORDER BY a}
400 do_test where3-6.$cnt.1 {
401 set sql "SELECT * FROM t6w NATURAL JOIN t6x NATURAL JOIN t6y"
402 append sql " NATURAL JOIN t6z "
403 append sql $::predicate
405 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine}
406 do_test where3-6.$cnt.2 {
407 set sql "SELECT * FROM t6w JOIN t6x USING(a) JOIN t6y USING(a)"
408 append sql " JOIN t6z USING(a) "
409 append sql $::predicate
411 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine}
412 do_test where3-6.$cnt.3 {
413 set sql "SELECT * FROM t6w NATURAL JOIN t6x JOIN t6y USING(a)"
414 append sql " JOIN t6z USING(a) "
415 append sql $::predicate
417 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine}
418 do_test where3-6.$cnt.4 {
419 set sql "SELECT * FROM t6w JOIN t6x USING(a) NATURAL JOIN t6y"
420 append sql " JOIN t6z USING(a) "
421 append sql $::predicate
423 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine}
424 do_test where3-6.$cnt.5 {
425 set sql "SELECT * FROM t6w JOIN t6x USING(a) JOIN t6y USING(a)"
426 append sql " NATURAL JOIN t6z "
427 append sql $::predicate
429 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine}
430 do_test where3-6.$cnt.6 {
431 set sql "SELECT * FROM t6w JOIN t6x USING(a) NATURAL JOIN t6y"
432 append sql " NATURAL JOIN t6z "
433 append sql $::predicate
435 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine}
436 do_test where3-6.$cnt.7 {
437 set sql "SELECT * FROM t6w NATURAL JOIN t6x JOIN t6y USING(a)"
438 append sql " NATURAL JOIN t6z "
439 append sql $::predicate
441 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine}
442 do_test where3-6.$cnt.8 {
443 set sql "SELECT * FROM t6w NATURAL JOIN t6x NATURAL JOIN t6y"
444 append sql " JOIN t6z USING(a) "
445 append sql $::predicate
447 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine}
450 do_execsql_test where3-7-setup {
451 CREATE TABLE t71(x1 INTEGER PRIMARY KEY, y1);
452 CREATE TABLE t72(x2 INTEGER PRIMARY KEY, y2);
453 CREATE TABLE t73(x3, y3);
454 CREATE TABLE t74(x4, y4);
455 INSERT INTO t71 VALUES(123,234);
456 INSERT INTO t72 VALUES(234,345);
457 INSERT INTO t73 VALUES(123,234);
458 INSERT INTO t74 VALUES(234,345);
459 INSERT INTO t74 VALUES(234,678);
461 foreach disabled_opt {none omit-noop-join all} {
462 optimization_control db all 1
463 optimization_control db $disabled_opt 0
464 do_execsql_test where3-7.$disabled_opt.1 {
465 SELECT x1 FROM t71 LEFT JOIN t72 ON x2=y1;
467 do_execsql_test where3-7.$disabled_opt.2 {
468 SELECT x1 FROM t71 LEFT JOIN t72 ON x2=y1 WHERE y2 IS NULL;
470 do_execsql_test where3-7.$disabled_opt.3 {
471 SELECT x1 FROM t71 LEFT JOIN t72 ON x2=y1 WHERE y2 IS NOT NULL;
473 do_execsql_test where3-7.$disabled_opt.4 {
474 SELECT x1 FROM t71 LEFT JOIN t72 ON x2=y1 AND y2 IS NULL;
476 do_execsql_test where3-7.$disabled_opt.5 {
477 SELECT x1 FROM t71 LEFT JOIN t72 ON x2=y1 AND y2 IS NOT NULL;
479 do_execsql_test where3-7.$disabled_opt.6 {
480 SELECT x3 FROM t73 LEFT JOIN t72 ON x2=y3;
482 do_execsql_test where3-7.$disabled_opt.7 {
483 SELECT DISTINCT x3 FROM t73 LEFT JOIN t72 ON x2=y3;
485 do_execsql_test where3-7.$disabled_opt.8 {
486 SELECT x3 FROM t73 LEFT JOIN t74 ON x4=y3;
488 do_execsql_test where3-7.$disabled_opt.9 {
489 SELECT DISTINCT x3 FROM t73 LEFT JOIN t74 ON x4=y3;