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 that terms in the ON clause of
13 # a LEFT OUTER JOIN are not used with indices. See ticket #3015.
15 # $Id: where6.test,v 1.2 2008/04/17 19:14:02 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Build some test data
24 CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c);
25 INSERT INTO t1 VALUES(1,3,1);
26 INSERT INTO t1 VALUES(2,4,2);
27 CREATE TABLE t2(x INTEGER PRIMARY KEY);
28 INSERT INTO t2 VALUES(3);
30 SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1;
35 SELECT * FROM t1 LEFT JOIN t2 ON x=b AND c=1;
40 SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c;
45 SELECT * FROM t1 LEFT JOIN t2 ON b=x AND 1=c;
51 explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c}
52 } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1}]
54 explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE 1=c}
55 } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1}]
60 SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1;
65 SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE c=1;
70 SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE 1=c;
78 CREATE INDEX i1 ON t1(c);
80 SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1;
85 SELECT * FROM t1 LEFT JOIN t2 ON x=b AND c=1;
90 SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c;
95 SELECT * FROM t1 LEFT JOIN t2 ON b=x AND 1=c;
101 explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c}
102 } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1}]
104 explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE 1=c}
105 } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1}]
109 do_test where6-2.11 {
111 SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1;
114 do_test where6-2.12 {
116 SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE c=1;
119 do_test where6-2.13 {
121 SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE 1=c;
124 do_test where6-2.14 {
126 SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE 1=c;
130 # Ticket [ebdbadade5b]:
131 # If the ON close on a LEFT JOIN is of the form x=y where both x and y
132 # are indexed columns on tables to left of the join, then do not use that
133 # term with indices to either table.
137 CREATE TABLE t4(x UNIQUE);
138 INSERT INTO t4 VALUES('abc');
139 INSERT INTO t4 VALUES('def');
140 INSERT INTO t4 VALUES('ghi');
141 CREATE TABLE t5(a, b, c, PRIMARY KEY(a,b));
142 INSERT INTO t5 VALUES('abc','def',123);
143 INSERT INTO t5 VALUES('def','ghi',456);
145 SELECT t4a.x, t4b.x, t5.c, t6.v
148 LEFT JOIN t5 ON t5.a=t4a.x AND t5.b=t4b.x
149 LEFT JOIN (SELECT 1 AS v) AS t6 ON t4a.x=t4b.x
152 } {abc abc {} 1 abc def 123 {} abc ghi {} {} def abc {} {} def def {} 1 def ghi 456 {} ghi abc {} {} ghi def {} {} ghi ghi {} 1}