xref: /sqlite-3.40.0/test/in3.test (revision 8a29dfde)
1# 2007 November 29
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 tests the optimisations made in November 2007 of expressions
12# of the following form:
13#
14#     <value> IN (SELECT <column> FROM <table>)
15#
16# $Id: in3.test,v 1.4 2008/03/12 10:39:00 danielk1977 Exp $
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21ifcapable !subquery {
22  finish_test
23  return
24}
25
26# Return the number of OpenEphemeral instructions used in the
27# implementation of the sql statement passed as a an argument.
28#
29proc nEphemeral {sql} {
30  set nEph 0
31  foreach op [execsql "EXPLAIN $sql"] {
32    if {$op eq "OpenEphemeral"} {incr nEph}
33  }
34  set nEph
35}
36
37# This proc works the same way as execsql, except that the number
38# of OpenEphemeral instructions used in the implementation of the
39# statement is inserted into the start of the returned list.
40#
41proc exec_neph {sql} {
42  return [concat [nEphemeral $sql] [execsql $sql]]
43}
44
45do_test in3-1.1 {
46  execsql {
47    CREATE TABLE t1(a PRIMARY KEY, b);
48    INSERT INTO t1 VALUES(1, 2);
49    INSERT INTO t1 VALUES(3, 4);
50    INSERT INTO t1 VALUES(5, 6);
51  }
52} {}
53
54# All of these queries should avoid using a temp-table:
55#
56do_test in3-1.2 {
57  exec_neph { SELECT rowid FROM t1 WHERE rowid IN (SELECT rowid FROM t1); }
58} {0 1 2 3}
59do_test in3-1.3 {
60  exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1); }
61} {0 1 3 5}
62do_test in3-1.4 {
63  exec_neph { SELECT rowid FROM t1 WHERE rowid+0 IN (SELECT rowid FROM t1); }
64} {0 1 2 3}
65do_test in3-1.5 {
66  exec_neph { SELECT a FROM t1 WHERE a+0 IN (SELECT a FROM t1); }
67} {0 1 3 5}
68
69# Because none of the sub-select queries in the following statements
70# match the pattern ("SELECT <column> FROM <table>"), the following do
71# require a temp table.
72#
73do_test in3-1.6 {
74  exec_neph { SELECT rowid FROM t1 WHERE rowid IN (SELECT rowid+0 FROM t1); }
75} {1 1 2 3}
76do_test in3-1.7 {
77  exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a+0 FROM t1); }
78} {1 1 3 5}
79do_test in3-1.8 {
80  exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 WHERE 1); }
81} {1 1 3 5}
82do_test in3-1.9 {
83  exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 GROUP BY a); }
84} {1 1 3 5}
85
86# This should not use a temp-table. Even though the sub-select does
87# not exactly match the pattern "SELECT <column> FROM <table>", in
88# this case the ORDER BY is a no-op and can be ignored.
89do_test in3-1.10 {
90  exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a); }
91} {0 1 3 5}
92
93# These do use the temp-table. Adding the LIMIT clause means the
94# ORDER BY cannot be ignored.
95do_test in3-1.11 {
96  exec_neph {SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a LIMIT 1)}
97} {1 1}
98do_test in3-1.12 {
99  exec_neph {
100    SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a LIMIT 1 OFFSET 1)
101  }
102} {1 3}
103
104# Has to use a temp-table because of the compound sub-select.
105#
106do_test in3-1.13 {
107  exec_neph {
108    SELECT a FROM t1 WHERE a IN (
109      SELECT a FROM t1 UNION ALL SELECT a FROM t1
110    )
111  }
112} {1 1 3 5}
113
114# The first of these queries has to use the temp-table, because the
115# collation sequence used for the index on "t1.a" does not match the
116# collation sequence used by the "IN" comparison. The second does not
117# require a temp-table, because the collation sequences match.
118#
119do_test in3-1.14 {
120  exec_neph { SELECT a FROM t1 WHERE a COLLATE nocase IN (SELECT a FROM t1) }
121} {1 1 3 5}
122do_test in3-1.15 {
123  exec_neph { SELECT a FROM t1 WHERE a COLLATE binary IN (SELECT a FROM t1) }
124} {0 1 3 5}
125
126# Neither of these queries require a temp-table. The collation sequence
127# makes no difference when using a rowid.
128#
129do_test in3-1.16 {
130  exec_neph {SELECT a FROM t1 WHERE a COLLATE nocase IN (SELECT rowid FROM t1)}
131} {0 1 3}
132do_test in3-1.17 {
133  exec_neph {SELECT a FROM t1 WHERE a COLLATE binary IN (SELECT rowid FROM t1)}
134} {0 1 3}
135
136# The following tests - in3.2.* - test a bug that was difficult to track
137# down during development. They are not particularly well focused.
138#
139do_test in3-2.1 {
140  execsql {
141    DROP TABLE IF EXISTS t1;
142    CREATE TABLE t1(w int, x int, y int);
143    CREATE TABLE t2(p int, q int, r int, s int);
144  }
145  for {set i 1} {$i<=100} {incr i} {
146    set w $i
147    set x [expr {int(log($i)/log(2))}]
148    set y [expr {$i*$i + 2*$i + 1}]
149    execsql "INSERT INTO t1 VALUES($w,$x,$y)"
150  }
151  set maxy [execsql {select max(y) from t1}]
152  db eval { INSERT INTO t2 SELECT 101-w, x, $maxy+1-y, y FROM t1 }
153} {}
154do_test in3-2.2 {
155  execsql {
156    SELECT rowid
157    FROM t1
158    WHERE rowid IN (SELECT rowid FROM t1 WHERE rowid IN (1, 2));
159  }
160} {1 2}
161do_test in3-2.3 {
162  execsql {
163    select rowid from t1 where rowid IN (-1,2,4)
164  }
165} {2 4}
166do_test in3-2.4 {
167  execsql {
168    SELECT rowid FROM t1 WHERE rowid IN
169       (select rowid from t1 where rowid IN (-1,2,4))
170  }
171} {2 4}
172
173#-------------------------------------------------------------------------
174# This next block of tests - in3-3.* - verify that column affinity is
175# correctly handled in cases where an index might be used to optimise
176# an IN (SELECT) expression.
177#
178do_test in3-3.1 {
179  catch {execsql {
180    DROP TABLE t1;
181    DROP TABLE t2;
182  }}
183
184  execsql {
185
186    CREATE TABLE t1(a BLOB, b NUMBER ,c TEXT);
187    CREATE UNIQUE INDEX t1_i1 ON t1(a);        /* no affinity */
188    CREATE UNIQUE INDEX t1_i2 ON t1(b);        /* numeric affinity */
189    CREATE UNIQUE INDEX t1_i3 ON t1(c);        /* text affinity */
190
191    CREATE TABLE t2(x BLOB, y NUMBER, z TEXT);
192    CREATE UNIQUE INDEX t2_i1 ON t2(x);        /* no affinity */
193    CREATE UNIQUE INDEX t2_i2 ON t2(y);        /* numeric affinity */
194    CREATE UNIQUE INDEX t2_i3 ON t2(z);        /* text affinity */
195
196    INSERT INTO t1 VALUES(1, 1, 1);
197    INSERT INTO t2 VALUES('1', '1', '1');
198  }
199} {}
200
201do_test in3-3.2 {
202  # No affinity is applied before comparing "x" and "a". Therefore
203  # the index can be used (the comparison is false, text!=number).
204  exec_neph { SELECT x IN (SELECT a FROM t1) FROM t2 }
205} {0 0}
206do_test in3-3.3 {
207  # Logically, numeric affinity is applied to both sides before
208  # the comparison.  Therefore it is possible to use index t1_i2.
209  exec_neph { SELECT x IN (SELECT b FROM t1) FROM t2 }
210} {0 1}
211do_test in3-3.4 {
212  # No affinity is applied before the comparison takes place. Making
213  # it possible to use index t1_i3.
214  exec_neph { SELECT x IN (SELECT c FROM t1) FROM t2 }
215} {0 1}
216
217do_test in3-3.5 {
218  # Numeric affinity should be applied to each side before the comparison
219  # takes place. Therefore we cannot use index t1_i1, which has no affinity.
220  exec_neph { SELECT y IN (SELECT a FROM t1) FROM t2 }
221} {1 1}
222do_test in3-3.6 {
223  # Numeric affinity is applied to both sides before
224  # the comparison.  Therefore it is possible to use index t1_i2.
225  exec_neph { SELECT y IN (SELECT b FROM t1) FROM t2 }
226} {0 1}
227do_test in3-3.7 {
228  # Numeric affinity is applied before the comparison takes place.
229  # Making it impossible to use index t1_i3.
230  exec_neph { SELECT y IN (SELECT c FROM t1) FROM t2 }
231} {1 1}
232
233#---------------------------------------------------------------------
234#
235# Test using a multi-column index.
236#
237do_test in3-4.1 {
238  execsql {
239    CREATE TABLE t3(a, b, c);
240    CREATE UNIQUE INDEX t3_i ON t3(b, a);
241  }
242
243  execsql {
244    INSERT INTO t3 VALUES(1, 'numeric', 2);
245    INSERT INTO t3 VALUES(2, 'text', 2);
246    INSERT INTO t3 VALUES(3, 'real', 2);
247    INSERT INTO t3 VALUES(4, 'none', 2);
248  }
249} {}
250do_test in3-4.2 {
251  exec_neph { SELECT 'text' IN (SELECT b FROM t3) }
252} {0 1}
253do_test in3-4.3 {
254  exec_neph { SELECT 'TEXT' COLLATE nocase IN (SELECT b FROM t3) }
255} {1 1}
256do_test in3-4.4 {
257  # A temp table must be used because t3_i.b is not guaranteed to be unique.
258  exec_neph { SELECT b FROM t3 WHERE b IN (SELECT b FROM t3) }
259} {1 none numeric real text}
260do_test in3-4.5 {
261  execsql { CREATE UNIQUE INDEX t3_i2 ON t3(b) }
262  exec_neph { SELECT b FROM t3 WHERE b IN (SELECT b FROM t3) }
263} {0 none numeric real text}
264do_test in3-4.6 {
265  execsql { DROP INDEX t3_i2 }
266} {}
267
268# The following two test cases verify that ticket #2991 has been fixed.
269#
270do_test in3-5.1 {
271  execsql {
272    CREATE TABLE Folders(
273      folderid INTEGER PRIMARY KEY,
274      parentid INTEGER,
275      rootid INTEGER,
276      path VARCHAR(255)
277    );
278  }
279} {}
280do_test in3-5.2 {
281  catchsql {
282    DELETE FROM Folders WHERE folderid IN
283    (SELECT folderid FROM Folder WHERE path LIKE 'C:\MP3\Albums\' || '%');
284  }
285} {1 {no such table: Folder}}
286
287finish_test
288