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.4 2008/04/17 19:14:02 drh Exp $ 16 17set testdir [file dirname $argv0] 18source $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# 26do_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 48ifcapable explain { 49 do_test where3-1.1.1 { 50 explain_no_trace {SELECT * FROM t1, t2 LEFT JOIN t3 ON q=x 51 WHERE p=2 AND a=q} 52 } [explain_no_trace {SELECT * FROM t1, t2 LEFT JOIN t3 ON x=q 53 WHERE p=2 AND a=q}] 54} 55 56# Ticket #1830 57# 58# This is similar to the above but with the LEFT JOIN on the 59# other side. 60# 61do_test where3-1.2 { 62 execsql { 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 ); 67 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' ); 72 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' ); 76 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' ); 81 82 SELECT parent1.parent1key, child1.value, child2.value 83 FROM parent1 84 LEFT OUTER JOIN child1 ON child1.child1key = parent1.child1key 85 INNER JOIN child2 ON child2.child2key = parent1.child2key; 86 } 87} {1 {Value for C1.1} {Value for C2.1} 2 {} {Value for C2.2} 3 {Value for C1.3} {Value for C2.3}} 88 89ifcapable explain { 90 do_test where3-1.2.1 { 91 explain_no_trace { 92 SELECT parent1.parent1key, child1.value, child2.value 93 FROM parent1 94 LEFT OUTER JOIN child1 ON child1.child1key = parent1.child1key 95 INNER JOIN child2 ON child2.child2key = parent1.child2key; 96 } 97 } [explain_no_trace { 98 SELECT parent1.parent1key, child1.value, child2.value 99 FROM parent1 100 LEFT OUTER JOIN child1 ON parent1.child1key = child1.child1key 101 INNER JOIN child2 ON child2.child2key = parent1.child2key; 102 }] 103} 104 105# This procedure executes the SQL. Then it appends 106# the names of the table and index used 107# 108proc queryplan {sql} { 109 set ::sqlite_sort_count 0 110 set data [execsql $sql] 111 set eqp [execsql "EXPLAIN QUERY PLAN $sql"] 112 # puts eqp=$eqp 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]} { 118 lappend data $tab * 119 } 120 } 121 return $data 122} 123 124 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. 128# 129# Following the fix to ticket #1652, there was a time when 130# the C table would not reorder. So the following reorderings 131# were possible: 132# 133# A B C left join D 134# B A C left join D 135# 136# But these reorders were not allowed 137# 138# C A B left join D 139# A C B left join D 140# C B A left join D 141# B C A left join D 142# 143# The following tests are here to verify that the latter four 144# reorderings are allowed again. 145# 146do_test where3-2.1 { 147 execsql { 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); 152 } 153 queryplan { 154 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx 155 WHERE cpk=bx AND bpk=ax 156 } 157} {tA * tB * tC * tD *} 158do_test where3-2.1.1 { 159 queryplan { 160 SELECT * FROM tA, tB, tC LEFT JOIN tD ON cx=dpk 161 WHERE cpk=bx AND bpk=ax 162 } 163} {tA * tB * tC * tD *} 164do_test where3-2.1.2 { 165 queryplan { 166 SELECT * FROM tA, tB, tC LEFT JOIN tD ON cx=dpk 167 WHERE bx=cpk AND bpk=ax 168 } 169} {tA * tB * tC * tD *} 170do_test where3-2.1.3 { 171 queryplan { 172 SELECT * FROM tA, tB, tC LEFT JOIN tD ON cx=dpk 173 WHERE bx=cpk AND ax=bpk 174 } 175} {tA * tB * tC * tD *} 176do_test where3-2.1.4 { 177 queryplan { 178 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx 179 WHERE bx=cpk AND ax=bpk 180 } 181} {tA * tB * tC * tD *} 182do_test where3-2.1.5 { 183 queryplan { 184 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx 185 WHERE cpk=bx AND ax=bpk 186 } 187} {tA * tB * tC * tD *} 188do_test where3-2.2 { 189 queryplan { 190 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx 191 WHERE cpk=bx AND apk=bx 192 } 193} {tB * tA * tC * tD *} 194do_test where3-2.3 { 195 queryplan { 196 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx 197 WHERE cpk=bx AND apk=bx 198 } 199} {tB * tA * tC * tD *} 200do_test where3-2.4 { 201 queryplan { 202 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx 203 WHERE apk=cx AND bpk=ax 204 } 205} {tC * tA * tB * tD *} 206do_test where3-2.5 { 207 queryplan { 208 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx 209 WHERE cpk=ax AND bpk=cx 210 } 211} {tA * tC * tB * tD *} 212do_test where3-2.6 { 213 queryplan { 214 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx 215 WHERE bpk=cx AND apk=bx 216 } 217} {tC * tB * tA * tD *} 218do_test where3-2.7 { 219 queryplan { 220 SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx 221 WHERE cpk=bx AND apk=cx 222 } 223} {tB * tC * tA * tD *} 224 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. 229# 230do_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 CREATE TABLE t302(x, y); 235 INSERT INTO t302 VALUES(4,5); 236 ANALYZE; 237 explain query plan SELECT * FROM t302, t301 WHERE t302.x=5 AND t301.a=t302.y; 238} { 239 0 0 0 {SCAN TABLE t302} 240 0 1 1 {SEARCH TABLE t301 USING INTEGER PRIMARY KEY (rowid=?)} 241} 242do_execsql_test where3-3.1 { 243 explain query plan 244 SELECT * FROM t301, t302 WHERE t302.x=5 AND t301.a=t302.y; 245} { 246 0 0 1 {SCAN TABLE t302} 247 0 1 0 {SEARCH TABLE t301 USING INTEGER PRIMARY KEY (rowid=?)} 248} 249 250# Verify that when there are multiple tables in a join which must be 251# full table scans that the query planner attempts put the table with 252# the fewest number of output rows as the outer loop. 253# 254do_execsql_test where3-4.0 { 255 CREATE TABLE t400(a INTEGER PRIMARY KEY, b, c); 256 CREATE TABLE t401(p INTEGER PRIMARY KEY, q, r); 257 CREATE TABLE t402(x INTEGER PRIMARY KEY, y, z); 258 EXPLAIN QUERY PLAN 259 SELECT * FROM t400, t401, t402 WHERE t402.z GLOB 'abc*'; 260} { 261 0 0 2 {SCAN TABLE t402} 262 0 1 0 {SCAN TABLE t400} 263 0 2 1 {SCAN TABLE t401} 264} 265do_execsql_test where3-4.1 { 266 EXPLAIN QUERY PLAN 267 SELECT * FROM t400, t401, t402 WHERE t401.r GLOB 'abc*'; 268} { 269 0 0 1 {SCAN TABLE t401} 270 0 1 0 {SCAN TABLE t400} 271 0 2 2 {SCAN TABLE t402} 272} 273do_execsql_test where3-4.2 { 274 EXPLAIN QUERY PLAN 275 SELECT * FROM t400, t401, t402 WHERE t400.c GLOB 'abc*'; 276} { 277 0 0 0 {SCAN TABLE t400} 278 0 1 1 {SCAN TABLE t401} 279 0 2 2 {SCAN TABLE t402} 280} 281 282# Verify that a performance regression encountered by firefox 283# has been fixed. 284# 285do_execsql_test where3-5.0 { 286 CREATE TABLE aaa (id INTEGER PRIMARY KEY, type INTEGER, 287 fk INTEGER DEFAULT NULL, parent INTEGER, 288 position INTEGER, title LONGVARCHAR, 289 keyword_id INTEGER, folder_type TEXT, 290 dateAdded INTEGER, lastModified INTEGER); 291 CREATE INDEX aaa_111 ON aaa (fk, type); 292 CREATE INDEX aaa_222 ON aaa (parent, position); 293 CREATE INDEX aaa_333 ON aaa (fk, lastModified); 294 CREATE TABLE bbb (id INTEGER PRIMARY KEY, type INTEGER, 295 fk INTEGER DEFAULT NULL, parent INTEGER, 296 position INTEGER, title LONGVARCHAR, 297 keyword_id INTEGER, folder_type TEXT, 298 dateAdded INTEGER, lastModified INTEGER); 299 CREATE INDEX bbb_111 ON bbb (fk, type); 300 CREATE INDEX bbb_222 ON bbb (parent, position); 301 CREATE INDEX bbb_333 ON bbb (fk, lastModified); 302 303 EXPLAIN QUERY PLAN 304 SELECT bbb.title AS tag_title 305 FROM aaa JOIN bbb ON bbb.id = aaa.parent 306 WHERE aaa.fk = 'constant' 307 AND LENGTH(bbb.title) > 0 308 AND bbb.parent = 4 309 ORDER BY bbb.title COLLATE NOCASE ASC; 310} { 311 0 0 0 {SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)} 312 0 1 1 {SEARCH TABLE bbb USING INTEGER PRIMARY KEY (rowid=?)} 313 0 0 0 {USE TEMP B-TREE FOR ORDER BY} 314} 315do_execsql_test where3-5.1 { 316 EXPLAIN QUERY PLAN 317 SELECT bbb.title AS tag_title 318 FROM aaa JOIN aaa AS bbb ON bbb.id = aaa.parent 319 WHERE aaa.fk = 'constant' 320 AND LENGTH(bbb.title) > 0 321 AND bbb.parent = 4 322 ORDER BY bbb.title COLLATE NOCASE ASC; 323} { 324 0 0 0 {SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)} 325 0 1 1 {SEARCH TABLE aaa AS bbb USING INTEGER PRIMARY KEY (rowid=?)} 326 0 0 0 {USE TEMP B-TREE FOR ORDER BY} 327} 328do_execsql_test where3-5.2 { 329 EXPLAIN QUERY PLAN 330 SELECT bbb.title AS tag_title 331 FROM bbb JOIN aaa ON bbb.id = aaa.parent 332 WHERE aaa.fk = 'constant' 333 AND LENGTH(bbb.title) > 0 334 AND bbb.parent = 4 335 ORDER BY bbb.title COLLATE NOCASE ASC; 336} { 337 0 0 1 {SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)} 338 0 1 0 {SEARCH TABLE bbb USING INTEGER PRIMARY KEY (rowid=?)} 339 0 0 0 {USE TEMP B-TREE FOR ORDER BY} 340} 341do_execsql_test where3-5.3 { 342 EXPLAIN QUERY PLAN 343 SELECT bbb.title AS tag_title 344 FROM aaa AS bbb JOIN aaa ON bbb.id = aaa.parent 345 WHERE aaa.fk = 'constant' 346 AND LENGTH(bbb.title) > 0 347 AND bbb.parent = 4 348 ORDER BY bbb.title COLLATE NOCASE ASC; 349} { 350 0 0 1 {SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)} 351 0 1 0 {SEARCH TABLE aaa AS bbb USING INTEGER PRIMARY KEY (rowid=?)} 352 0 0 0 {USE TEMP B-TREE FOR ORDER BY} 353} 354 355# Name resolution with NATURAL JOIN and USING 356# 357do_test where3-6.setup { 358 db eval { 359 CREATE TABLE t6w(a, w); 360 INSERT INTO t6w VALUES(1, 'w-one'); 361 INSERT INTO t6w VALUES(2, 'w-two'); 362 INSERT INTO t6w VALUES(9, 'w-nine'); 363 CREATE TABLE t6x(a, x); 364 INSERT INTO t6x VALUES(1, 'x-one'); 365 INSERT INTO t6x VALUES(3, 'x-three'); 366 INSERT INTO t6x VALUES(9, 'x-nine'); 367 CREATE TABLE t6y(a, y); 368 INSERT INTO t6y VALUES(1, 'y-one'); 369 INSERT INTO t6y VALUES(4, 'y-four'); 370 INSERT INTO t6y VALUES(9, 'y-nine'); 371 CREATE TABLE t6z(a, z); 372 INSERT INTO t6z VALUES(1, 'z-one'); 373 INSERT INTO t6z VALUES(5, 'z-five'); 374 INSERT INTO t6z VALUES(9, 'z-nine'); 375 } 376} {} 377set cnt 0 378foreach predicate { 379 {} 380 {ORDER BY a} 381 {ORDER BY t6w.a} 382 {WHERE a>0} 383 {WHERE t6y.a>0} 384 {WHERE a>0 ORDER BY a} 385} { 386 incr cnt 387 do_test where3-6.$cnt.1 { 388 set sql "SELECT * FROM t6w NATURAL JOIN t6x NATURAL JOIN t6y" 389 append sql " NATURAL JOIN t6z " 390 append sql $::predicate 391 db eval $sql 392 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine} 393 do_test where3-6.$cnt.2 { 394 set sql "SELECT * FROM t6w JOIN t6x USING(a) JOIN t6y USING(a)" 395 append sql " JOIN t6z USING(a) " 396 append sql $::predicate 397 db eval $sql 398 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine} 399 do_test where3-6.$cnt.3 { 400 set sql "SELECT * FROM t6w NATURAL JOIN t6x JOIN t6y USING(a)" 401 append sql " JOIN t6z USING(a) " 402 append sql $::predicate 403 db eval $sql 404 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine} 405 do_test where3-6.$cnt.4 { 406 set sql "SELECT * FROM t6w JOIN t6x USING(a) NATURAL JOIN t6y" 407 append sql " JOIN t6z USING(a) " 408 append sql $::predicate 409 db eval $sql 410 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine} 411 do_test where3-6.$cnt.5 { 412 set sql "SELECT * FROM t6w JOIN t6x USING(a) JOIN t6y USING(a)" 413 append sql " NATURAL JOIN t6z " 414 append sql $::predicate 415 db eval $sql 416 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine} 417 do_test where3-6.$cnt.6 { 418 set sql "SELECT * FROM t6w JOIN t6x USING(a) NATURAL JOIN t6y" 419 append sql " NATURAL JOIN t6z " 420 append sql $::predicate 421 db eval $sql 422 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine} 423 do_test where3-6.$cnt.7 { 424 set sql "SELECT * FROM t6w NATURAL JOIN t6x JOIN t6y USING(a)" 425 append sql " NATURAL JOIN t6z " 426 append sql $::predicate 427 db eval $sql 428 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine} 429 do_test where3-6.$cnt.8 { 430 set sql "SELECT * FROM t6w NATURAL JOIN t6x NATURAL JOIN t6y" 431 append sql " JOIN t6z USING(a) " 432 append sql $::predicate 433 db eval $sql 434 } {1 w-one x-one y-one z-one 9 w-nine x-nine y-nine z-nine} 435} 436 437 438finish_test 439