xref: /sqlite-3.40.0/test/where6.test (revision 5e767c57)
142165be1Sdrh# 2007 June 8
242165be1Sdrh#
342165be1Sdrh# The author disclaims copyright to this source code.  In place of
442165be1Sdrh# a legal notice, here is a blessing:
542165be1Sdrh#
642165be1Sdrh#    May you do good and not evil.
742165be1Sdrh#    May you find forgiveness for yourself and forgive others.
842165be1Sdrh#    May you share freely, never taking more than you give.
942165be1Sdrh#
1042165be1Sdrh#***********************************************************************
1142165be1Sdrh# This file implements regression tests for SQLite library.  The
1242165be1Sdrh# focus of this file is testing that terms in the ON clause of
1342165be1Sdrh# a LEFT OUTER JOIN are not used with indices.  See ticket #3015.
1442165be1Sdrh#
15dafc0ce8Sdrh# $Id: where6.test,v 1.2 2008/04/17 19:14:02 drh Exp $
1642165be1Sdrh
1742165be1Sdrhset testdir [file dirname $argv0]
1842165be1Sdrhsource $testdir/tester.tcl
1942165be1Sdrh
2042165be1Sdrh# Build some test data
2142165be1Sdrh#
22dafc0ce8Sdrhdo_test where6-1.1 {
2342165be1Sdrh  execsql {
2442165be1Sdrh    CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c);
2542165be1Sdrh    INSERT INTO t1 VALUES(1,3,1);
2642165be1Sdrh    INSERT INTO t1 VALUES(2,4,2);
2742165be1Sdrh    CREATE TABLE t2(x INTEGER PRIMARY KEY);
2842165be1Sdrh    INSERT INTO t2 VALUES(3);
2942165be1Sdrh
3042165be1Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1;
3142165be1Sdrh  }
3242165be1Sdrh} {1 3 1 3 2 4 2 {}}
33dafc0ce8Sdrhdo_test where6-1.2 {
34dafc0ce8Sdrh  execsql {
35dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON x=b AND c=1;
36dafc0ce8Sdrh  }
37dafc0ce8Sdrh} {1 3 1 3 2 4 2 {}}
38dafc0ce8Sdrhdo_test where6-1.3 {
39dafc0ce8Sdrh  execsql {
40dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c;
41dafc0ce8Sdrh  }
42dafc0ce8Sdrh} {1 3 1 3 2 4 2 {}}
43dafc0ce8Sdrhdo_test where6-1.4 {
44dafc0ce8Sdrh  execsql {
45dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON b=x AND 1=c;
46dafc0ce8Sdrh  }
47dafc0ce8Sdrh} {1 3 1 3 2 4 2 {}}
48dafc0ce8Sdrh
49dafc0ce8Sdrhifcapable explain {
50dafc0ce8Sdrh  do_test where6-1.5 {
51dafc0ce8Sdrh     explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c}
52dafc0ce8Sdrh  } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1}]
53dafc0ce8Sdrh  do_test where6-1.6 {
54dafc0ce8Sdrh     explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE 1=c}
55dafc0ce8Sdrh  } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1}]
56dafc0ce8Sdrh}
57dafc0ce8Sdrh
58dafc0ce8Sdrhdo_test where6-1.11 {
5942165be1Sdrh  execsql {
6042165be1Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1;
6142165be1Sdrh  }
6242165be1Sdrh} {1 3 1 3}
63dafc0ce8Sdrhdo_test where6-1.12 {
64dafc0ce8Sdrh  execsql {
65dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE c=1;
66dafc0ce8Sdrh  }
67dafc0ce8Sdrh} {1 3 1 3}
68dafc0ce8Sdrhdo_test where6-1.13 {
69dafc0ce8Sdrh  execsql {
70dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE 1=c;
71dafc0ce8Sdrh  }
72dafc0ce8Sdrh} {1 3 1 3}
73dafc0ce8Sdrh
74dafc0ce8Sdrh
75dafc0ce8Sdrh
76dafc0ce8Sdrhdo_test where6-2.1 {
7742165be1Sdrh  execsql {
7842165be1Sdrh    CREATE INDEX i1 ON t1(c);
7942165be1Sdrh
8042165be1Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1;
8142165be1Sdrh  }
8242165be1Sdrh} {1 3 1 3 2 4 2 {}}
83dafc0ce8Sdrhdo_test where6-2.2 {
84dafc0ce8Sdrh  execsql {
85dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON x=b AND c=1;
86dafc0ce8Sdrh  }
87dafc0ce8Sdrh} {1 3 1 3 2 4 2 {}}
88dafc0ce8Sdrhdo_test where6-2.3 {
89dafc0ce8Sdrh  execsql {
90dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c;
91dafc0ce8Sdrh  }
92dafc0ce8Sdrh} {1 3 1 3 2 4 2 {}}
93dafc0ce8Sdrhdo_test where6-2.4 {
94dafc0ce8Sdrh  execsql {
95dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON b=x AND 1=c;
96dafc0ce8Sdrh  }
97dafc0ce8Sdrh} {1 3 1 3 2 4 2 {}}
98dafc0ce8Sdrh
99dafc0ce8Sdrhifcapable explain {
100dafc0ce8Sdrh  do_test where6-2.5 {
101dafc0ce8Sdrh     explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c}
102dafc0ce8Sdrh  } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1}]
103dafc0ce8Sdrh  do_test where6-2.6 {
104dafc0ce8Sdrh     explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE 1=c}
105dafc0ce8Sdrh  } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1}]
106dafc0ce8Sdrh}
107dafc0ce8Sdrh
108dafc0ce8Sdrh
109dafc0ce8Sdrhdo_test where6-2.11 {
11042165be1Sdrh  execsql {
11142165be1Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1;
11242165be1Sdrh  }
11342165be1Sdrh} {1 3 1 3}
114dafc0ce8Sdrhdo_test where6-2.12 {
115dafc0ce8Sdrh  execsql {
116dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE c=1;
117dafc0ce8Sdrh  }
118dafc0ce8Sdrh} {1 3 1 3}
119dafc0ce8Sdrhdo_test where6-2.13 {
120dafc0ce8Sdrh  execsql {
121dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE 1=c;
122dafc0ce8Sdrh  }
123dafc0ce8Sdrh} {1 3 1 3}
124dafc0ce8Sdrhdo_test where6-2.14 {
125dafc0ce8Sdrh  execsql {
126dafc0ce8Sdrh    SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE 1=c;
127dafc0ce8Sdrh  }
128dafc0ce8Sdrh} {1 3 1 3}
12942165be1Sdrh
130*5e767c57Sdrh# Ticket [ebdbadade5b]:
131*5e767c57Sdrh# If the ON close on a LEFT JOIN is of the form x=y where both x and y
132*5e767c57Sdrh# are indexed columns on tables to left of the join, then do not use that
133*5e767c57Sdrh# term with indices to either table.
134*5e767c57Sdrh#
135*5e767c57Sdrhdo_test where6-3.1 {
136*5e767c57Sdrh  db eval {
137*5e767c57Sdrh    CREATE TABLE t4(x UNIQUE);
138*5e767c57Sdrh    INSERT INTO t4 VALUES('abc');
139*5e767c57Sdrh    INSERT INTO t4 VALUES('def');
140*5e767c57Sdrh    INSERT INTO t4 VALUES('ghi');
141*5e767c57Sdrh    CREATE TABLE t5(a, b, c, PRIMARY KEY(a,b));
142*5e767c57Sdrh    INSERT INTO t5 VALUES('abc','def',123);
143*5e767c57Sdrh    INSERT INTO t5 VALUES('def','ghi',456);
144*5e767c57Sdrh
145*5e767c57Sdrh    SELECT t4a.x, t4b.x, t5.c, t6.v
146*5e767c57Sdrh      FROM t4 AS t4a
147*5e767c57Sdrh           INNER JOIN t4 AS t4b
148*5e767c57Sdrh           LEFT JOIN t5 ON t5.a=t4a.x AND t5.b=t4b.x
149*5e767c57Sdrh           LEFT JOIN (SELECT 1 AS v) AS t6 ON t4a.x=t4b.x
150*5e767c57Sdrh     ORDER BY 1, 2, 3;
151*5e767c57Sdrh  }
152*5e767c57Sdrh} {abc abc {} 1 abc def 123 {} abc ghi {} {} def abc {} {} def def {} 1 def ghi 456 {} ghi abc {} {} ghi def {} {} ghi ghi {} 1}
153*5e767c57Sdrh
15442165be1Sdrhfinish_test
155