1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
# 2012 September 18
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix in5
do_test in5-1.1 {
execsql {
CREATE TABLE t1x(x INTEGER PRIMARY KEY);
INSERT INTO t1x VALUES(1),(3),(5),(7),(9);
CREATE TABLE t1y(y INTEGER UNIQUE);
INSERT INTO t1y VALUES(2),(4),(6),(8);
CREATE TABLE t1z(z TEXT UNIQUE);
INSERT INTO t1z VALUES('a'),('c'),('e'),('g');
CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT, d TEXT);
INSERT INTO t2 VALUES(1,2,'a','12a'),(1,2,'b','12b'),
(2,3,'g','23g'),(3,5,'c','35c'),
(4,6,'h','46h'),(5,6,'e','56e');
CREATE TABLE t3x AS SELECT x FROM t1x;
CREATE TABLE t3y AS SELECT y FROM t1y;
CREATE TABLE t3z AS SELECT z FROM t1z;
SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY c;
}
} {12a 56e}
do_test in5-1.2 {
execsql {
SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
}
} {23g}
do_test in5-1.3 {
execsql {
SELECT d FROM t2 WHERE a IN t3x AND b IN t3y AND c IN t3z ORDER BY d;
}
} {12a 56e}
do_test in5-2.1 {
execsql {
CREATE INDEX t2abc ON t2(a,b,c);
SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
}
} {12a 56e}
do_test in5-2.2 {
execsql {
SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
}
} {23g}
do_test in5-2.3 {
regexp {OpenEphemeral} [db eval {
EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
}]
} {0}
do_test in5-2.4 {
execsql {
SELECT d FROM t2 WHERE a IN t3x AND b IN t3y AND c IN t3z ORDER BY d;
}
} {12a 56e}
do_test in5-2.5.1 {
regexp {OpenEphemeral} [db eval {
EXPLAIN SELECT d FROM t2 WHERE a IN t3x AND b IN t1y AND c IN t1z
}]
} {1}
do_test in5-2.5.2 {
regexp {OpenEphemeral} [db eval {
EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t3y AND c IN t1z
}]
} {1}
do_test in5-2.5.3 {
regexp {OpenEphemeral} [db eval {
EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t3z
}]
} {1}
do_test in5-3.1 {
execsql {
DROP INDEX t2abc;
CREATE INDEX t2ab ON t2(a,b);
SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
}
} {12a 56e}
do_test in5-3.2 {
execsql {
SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
}
} {23g}
do_test in5-3.3 {
regexp {OpenEphemeral} [db eval {
EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
}]
} {0}
do_test in5-4.1 {
execsql {
DROP INDEX t2ab;
CREATE INDEX t2abcd ON t2(a,b,c,d);
SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
}
} {12a 56e}
do_test in5-4.2 {
execsql {
SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
}
} {23g}
do_test in5-4.3 {
regexp {OpenEphemeral} [db eval {
EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
}]
} {0}
do_test in5-5.1 {
execsql {
DROP INDEX t2abcd;
CREATE INDEX t2cbad ON t2(c,b,a,d);
SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
}
} {12a 56e}
do_test in5-5.2 {
execsql {
SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
}
} {23g}
do_test in5-5.3 {
regexp {OpenEphemeral} [db eval {
EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
}]
} {0}
#-------------------------------------------------------------------------
# At one point SQLite was removing the DISTINCT keyword from expressions
# similar to:
#
# <expr1> IN (SELECT DISTINCT <expr2> FROM...)
#
# However, there are a few obscure cases where this is incorrect. For
# example, if the SELECT features a LIMIT clause, or if the collation
# sequence or affinity used by the DISTINCT does not match the one used
# by the IN(...) expression.
#
do_execsql_test 6.1.1 {
CREATE TABLE t1(a COLLATE nocase);
INSERT INTO t1 VALUES('one');
INSERT INTO t1 VALUES('ONE');
}
do_execsql_test 6.1.2 {
SELECT count(*) FROM t1 WHERE a COLLATE BINARY IN (SELECT DISTINCT a FROM t1)
} {1}
do_execsql_test 6.2.1 {
CREATE TABLE t3(a, b);
INSERT INTO t3 VALUES(1, 1);
INSERT INTO t3 VALUES(1, 2);
INSERT INTO t3 VALUES(1, 3);
INSERT INTO t3 VALUES(2, 4);
INSERT INTO t3 VALUES(2, 5);
INSERT INTO t3 VALUES(2, 6);
INSERT INTO t3 VALUES(3, 7);
INSERT INTO t3 VALUES(3, 8);
INSERT INTO t3 VALUES(3, 9);
}
do_execsql_test 6.2.2 {
SELECT count(*) FROM t3 WHERE b IN (SELECT DISTINCT a FROM t3 LIMIT 5);
} {3}
do_execsql_test 6.2.3 {
SELECT count(*) FROM t3 WHERE b IN (SELECT a FROM t3 LIMIT 5);
} {2}
do_execsql_test 6.3.1 {
CREATE TABLE x1(a);
CREATE TABLE x2(b);
INSERT INTO x1 VALUES(1), (1), (2);
INSERT INTO x2 VALUES(1), (2);
SELECT count(*) FROM x2 WHERE b IN (SELECT DISTINCT a FROM x1 LIMIT 2);
} {2}
#-------------------------------------------------------------------------
# Test to confirm that bug [5e3c886796e5] is fixed.
#
do_execsql_test 7.1 {
CREATE TABLE y1(a, b);
CREATE TABLE y2(c);
INSERT INTO y1 VALUES(1, 'one');
INSERT INTO y1 VALUES('two', 'two');
INSERT INTO y1 VALUES(3, 'three');
INSERT INTO y2 VALUES('one');
INSERT INTO y2 VALUES('two');
INSERT INTO y2 VALUES('three');
} {}
do_execsql_test 7.2.1 {
SELECT a FROM y1 WHERE b NOT IN (SELECT a FROM y2);
} {1 3}
do_execsql_test 7.2.2 {
SELECT a FROM y1 WHERE b IN (SELECT a FROM y2);
} {two}
do_execsql_test 7.3.1 {
CREATE INDEX y2c ON y2(c);
SELECT a FROM y1 WHERE b NOT IN (SELECT a FROM y2);
} {1 3}
do_execsql_test 7.3.2 {
SELECT a FROM y1 WHERE b IN (SELECT a FROM y2);
} {two}
#-------------------------------------------------------------------------
# Tests to confirm that indexes on the rowid column do not confuse
# the query planner. See ticket [0eab1ac7591f511d].
#
do_execsql_test 8.0 {
CREATE TABLE n1(a INTEGER PRIMARY KEY, b VARCHAR(500));
CREATE UNIQUE INDEX n1a ON n1(a);
}
do_execsql_test 8.1 {
SELECT count(*) FROM n1 WHERE a IN (1, 2, 3)
} 0
do_execsql_test 8.2 {
SELECT count(*) FROM n1 WHERE a IN (SELECT +a FROM n1)
} 0
do_execsql_test 8.3 {
INSERT INTO n1 VALUES(1, NULL), (2, NULL), (3, NULL);
SELECT count(*) FROM n1 WHERE a IN (1, 2, 3)
} 3
do_execsql_test 8.4 {
SELECT count(*) FROM n1 WHERE a IN (SELECT +a FROM n1)
} 3
#-------------------------------------------------------------------------
# Test that ticket 61fe97454c is fixed.
#
do_execsql_test 9.0 {
CREATE TABLE t9(a INTEGER PRIMARY KEY);
INSERT INTO t9 VALUES (44), (45);
}
do_execsql_test 9.1 {
SELECT * FROM t9 WHERE a IN (44, 45, 44, 45)
} {44 45}
#-------------------------------------------------------------------------
# Test that ticket c7a117190 is fixed.
#
reset_db
do_execsql_test 9.0 {
CREATE TABLE t0(c0);
CREATE VIEW v0(c0) AS SELECT LOWER(CAST('1e500' AS TEXT)) FROM t0;
INSERT INTO t0(c0) VALUES (NULL);
}
do_execsql_test 9.1 {
SELECT lower('1e500') FROM t0 WHERE rowid NOT IN (0, 0, lower('1e500'));
} {1e500}
do_execsql_test 9.2 {
SELECT lower('1e500') FROM t0 WHERE rowid != lower('1e500');
} {1e500}
#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 10.0 {
CREATE TABLE t1(a, b TEXT COLLATE NOCASE);
INSERT INTO t1 VALUES('abc', 'def');
INSERT INTO t1 VALUES('ghi', 'jkl');
}
do_execsql_test 10.1 {
SELECT rowid FROM t1 WHERE (a, b) IN ( VALUES('abc', 'def'), ('ghi', 'JKL') );
} {1 2}
do_execsql_test 10.2 {
CREATE INDEX i1 ON t1(a, b COLLATE BINARY);
}
do_execsql_test 10.3 {
SELECT rowid FROM t1 WHERE (a, b) IN ( VALUES('abc', 'def'), ('ghi', 'JKL') );
} {1 2}
finish_test
|