interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / where2.test

918 lines · 28039 bytes Raw Blame History
1# 2005 July 28
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 use of indices in WHERE clauses
13# based on recent changes to the optimizer.
14#
15# $Id: where2.test,v 1.15 2009/02/02 01:50:40 drh Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Build some test data
21#
22do_test where2-1.0 {
23 execsql {
24 BEGIN;
25 CREATE TABLE t1(w int, x int, y int, z int);
26 }
27 for {set i 1} {$i<=100} {incr i} {
28 set w $i
29 set x [expr {int(log($i)/log(2))}]
30 set y [expr {$i*$i + 2*$i + 1}]
31 set z [expr {$x+$y}]
32 ifcapable tclvar {
33 execsql {INSERT INTO t1 VALUES($::w,$::x,$::y,$::z)}
34 } else {
35 execsql {INSERT INTO t1 VALUES(:w,:x,:y,:z)}
36 }
37 }
38 execsql {
39 CREATE UNIQUE INDEX i1w ON t1(w);
40 CREATE INDEX i1xy ON t1(x,y);
41 CREATE INDEX i1zyx ON t1(z,y,x);
42 COMMIT;
43 }
44} {}
45
46# Do an SQL statement. Append the search count to the end of the result.
47#
48proc count sql {
49 set ::sqlite_search_count 0
50 return [concat [execsql $sql] $::sqlite_search_count]
51}
52
53# This procedure executes the SQL. Then it checks to see if the OP_Sort
54# opcode was executed. If an OP_Sort did occur, then "sort" is appended
55# to the result. If no OP_Sort happened, then "nosort" is appended.
56#
57# This procedure is used to check to make sure sorting is or is not
58# occurring as expected.
59#
60proc cksort {sql} {
61 set data [execsql $sql]
62 if {[db status sort]} {set x sort} {set x nosort}
63 lappend data $x
64 return $data
65}
66
67# This procedure executes the SQL. Then it appends to the result the
68# "sort" or "nosort" keyword (as in the cksort procedure above) then
69# it appends the name of the table and index used.
70#
71proc queryplan {sql} {
72 set ::sqlite_sort_count 0
73 set data [execsql $sql]
74 if {$::sqlite_sort_count} {set x sort} {set x nosort}
75 lappend data $x
76 set eqp [execsql "EXPLAIN QUERY PLAN $sql"]
77 # puts eqp=$eqp
78 foreach {a b c x} $eqp {
79 if {[regexp {SCAN CONSTANT} $x]} {
80 # noop
81 } elseif {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \
82 $x all ss as tab idx]} {
83 lappend data $tab $idx
84 } elseif {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+)\y} $x all ss as tab]} {
85 lappend data $tab *
86 }
87 }
88 return $data
89}
90
91
92# Prefer a UNIQUE index over another index.
93#
94do_test where2-1.1 {
95 queryplan {
96 SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396
97 }
98} {85 6 7396 7402 nosort t1 i1w}
99
100# Always prefer a rowid== constraint over any other index.
101#
102do_test where2-1.3 {
103 queryplan {
104 SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396 AND rowid=85
105 }
106} {85 6 7396 7402 nosort t1 *}
107
108# When constrained by a UNIQUE index, the ORDER BY clause is always ignored.
109#
110do_test where2-2.1 {
111 queryplan {
112 SELECT * FROM t1 WHERE w=85 ORDER BY random();
113 }
114} {85 6 7396 7402 nosort t1 i1w}
115do_test where2-2.2 {
116 queryplan {
117 SELECT * FROM t1 WHERE x=6 AND y=7396 ORDER BY random();
118 }
119} {85 6 7396 7402 sort t1 i1xy}
120do_test where2-2.3 {
121 queryplan {
122 SELECT * FROM t1 WHERE rowid=85 AND x=6 AND y=7396 ORDER BY random();
123 }
124} {85 6 7396 7402 nosort t1 *}
125
126# Ticket [65bdeb9739605cc22966f49208452996ff29a640] 2014-02-26
127# Make sure "ORDER BY random" does not gets optimized out.
128#
129do_test where2-2.4 {
130 db eval {
131 CREATE TABLE x1(a INTEGER PRIMARY KEY, b DEFAULT 1);
132 WITH RECURSIVE
133 cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<50)
134 INSERT INTO x1 SELECT x, 1 FROM cnt;
135 CREATE TABLE x2(x INTEGER PRIMARY KEY);
136 INSERT INTO x2 VALUES(1);
137 }
138 set sql {SELECT * FROM x1, x2 WHERE x=1 ORDER BY random()}
139 set out1 [db eval $sql]
140 set out2 [db eval $sql]
141 set out3 [db eval $sql]
142 expr {$out1!=$out2 && $out2!=$out3}
143} {1}
144do_execsql_test where2-2.5 {
145 -- random() is not optimized out
146 EXPLAIN SELECT * FROM x1, x2 WHERE x=1 ORDER BY random();
147} {/ random/}
148do_execsql_test where2-2.5b {
149 -- random() is not optimized out
150 EXPLAIN SELECT * FROM x1, x2 WHERE x=1 ORDER BY random();
151} {/ SorterOpen /}
152do_execsql_test where2-2.6 {
153 -- other constant functions are optimized out
154 EXPLAIN SELECT * FROM x1, x2 WHERE x=1 ORDER BY abs(5);
155} {~/ abs/}
156do_execsql_test where2-2.6b {
157 -- other constant functions are optimized out
158 EXPLAIN SELECT * FROM x1, x2 WHERE x=1 ORDER BY abs(5);
159} {~/ SorterOpen /}
160
161
162
163# Efficient handling of forward and reverse table scans.
164#
165do_test where2-3.1 {
166 queryplan {
167 SELECT * FROM t1 ORDER BY rowid LIMIT 2
168 }
169} {1 0 4 4 2 1 9 10 nosort t1 *}
170do_test where2-3.2 {
171 queryplan {
172 SELECT * FROM t1 ORDER BY rowid DESC LIMIT 2
173 }
174} {100 6 10201 10207 99 6 10000 10006 nosort t1 *}
175
176# The IN operator can be used by indices at multiple layers
177#
178ifcapable subquery {
179 do_test where2-4.1 {
180 queryplan {
181 SELECT * FROM t1 WHERE z IN (10207,10006) AND y IN (10000,10201)
182 AND x>0 AND x<10
183 ORDER BY w
184 }
185 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
186 do_test where2-4.2 {
187 queryplan {
188 SELECT * FROM t1 WHERE z IN (10207,10006) AND y=10000
189 AND x>0 AND x<10
190 ORDER BY w
191 }
192 } {99 6 10000 10006 sort t1 i1zyx}
193 do_test where2-4.3 {
194 queryplan {
195 SELECT * FROM t1 WHERE z=10006 AND y IN (10000,10201)
196 AND x>0 AND x<10
197 ORDER BY w
198 }
199 } {99 6 10000 10006 sort t1 i1zyx}
200 ifcapable compound {
201 do_test where2-4.4 {
202 queryplan {
203 SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006)
204 AND y IN (10000,10201)
205 AND x>0 AND x<10
206 ORDER BY w
207 }
208 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
209 do_test where2-4.5 {
210 queryplan {
211 SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006)
212 AND y IN (SELECT 10000 UNION SELECT 10201)
213 AND x>0 AND x<10
214 ORDER BY w
215 }
216 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
217 }
218 do_test where2-4.6a {
219 queryplan {
220 SELECT * FROM t1
221 WHERE x IN (1,2,3,4,5,6,7,8)
222 AND y IN (10000,10001,10002,10003,10004,10005)
223 ORDER BY x
224 }
225 } {99 6 10000 10006 nosort t1 i1xy}
226 do_test where2-4.6b {
227 queryplan {
228 SELECT * FROM t1
229 WHERE x IN (1,2,3,4,5,6,7,8)
230 AND y IN (10000,10001,10002,10003,10004,10005)
231 ORDER BY x DESC
232 }
233 } {99 6 10000 10006 nosort t1 i1xy}
234 do_test where2-4.6c {
235 queryplan {
236 SELECT * FROM t1
237 WHERE x IN (1,2,3,4,5,6,7,8)
238 AND y IN (10000,10001,10002,10003,10004,10005)
239 ORDER BY x, y
240 }
241 } {99 6 10000 10006 nosort t1 i1xy}
242 do_test where2-4.6d {
243 queryplan {
244 SELECT * FROM t1
245 WHERE x IN (1,2,3,4,5,6,7,8)
246 AND y IN (10000,10001,10002,10003,10004,10005)
247 ORDER BY x, y DESC
248 }
249 } {99 6 10000 10006 sort t1 i1xy}
250
251 # Duplicate entires on the RHS of an IN operator do not cause duplicate
252 # output rows.
253 #
254 do_test where2-4.6x {
255 queryplan {
256 SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207)
257 ORDER BY w
258 }
259 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
260 do_test where2-4.6y {
261 queryplan {
262 SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207)
263 ORDER BY w DESC
264 }
265 } {100 6 10201 10207 99 6 10000 10006 sort t1 i1zyx}
266 ifcapable compound {
267 do_test where2-4.7 {
268 queryplan {
269 SELECT * FROM t1 WHERE z IN (
270 SELECT 10207 UNION ALL SELECT 10006
271 UNION ALL SELECT 10006 UNION ALL SELECT 10207)
272 ORDER BY w
273 }
274 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
275 }
276
277} ;# ifcapable subquery
278
279# The use of an IN operator disables the index as a sorter.
280#
281do_test where2-5.1 {
282 queryplan {
283 SELECT * FROM t1 WHERE w=99 ORDER BY w
284 }
285} {99 6 10000 10006 nosort t1 i1w}
286
287ifcapable subquery {
288 do_test where2-5.2a {
289 queryplan {
290 SELECT * FROM t1 WHERE w IN (99) ORDER BY w
291 }
292 } {99 6 10000 10006 nosort t1 i1w}
293 do_test where2-5.2b {
294 queryplan {
295 SELECT * FROM t1 WHERE w IN (99) ORDER BY w DESC
296 }
297 } {99 6 10000 10006 nosort t1 i1w}
298}
299
300# Verify that OR clauses get translated into IN operators.
301#
302set ::idx {}
303ifcapable subquery {set ::idx i1w}
304do_test where2-6.1.1 {
305 queryplan {
306 SELECT * FROM t1 WHERE w=99 OR w=100 ORDER BY +w
307 }
308} [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
309do_test where2-6.1.2 {
310 queryplan {
311 SELECT * FROM t1 WHERE 99=w OR 100=w ORDER BY +w
312 }
313} [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
314do_test where2-6.2 {
315 queryplan {
316 SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=w ORDER BY +w
317 }
318} [list 6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
319
320do_test where2-6.3 {
321 queryplan {
322 SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=+w ORDER BY +w
323 }
324} {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 *}
325do_test where2-6.4 {
326 queryplan {
327 SELECT *, '|' FROM t1 WHERE w=99 OR +w=100 OR 6=w ORDER BY +w
328 }
329} {6 2 49 51 | 99 6 10000 10006 | 100 6 10201 10207 | sort t1 *}
330do_test where2-6.5 {
331 queryplan {
332 SELECT *, '|' FROM t1 WHERE w=99 OR y=10201 OR 6=w ORDER BY +w
333 }
334} {6 2 49 51 | 99 6 10000 10006 | 100 6 10201 10207 | sort t1 *}
335
336set ::idx {}
337ifcapable subquery {set ::idx i1zyx}
338do_test where2-6.5 {
339 queryplan {
340 SELECT b.* FROM t1 a, t1 b
341 WHERE a.w=1 AND (a.y=b.z OR b.z=10)
342 ORDER BY +b.w
343 }
344} [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx]
345do_test where2-6.6 {
346 queryplan {
347 SELECT b.* FROM t1 a, t1 b
348 WHERE a.w=1 AND (b.z=10 OR a.y=b.z OR b.z=10)
349 ORDER BY +b.w
350 }
351} [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx]
352
353if {[permutation] != "no_optimization"} {
354
355# Ticket #2249. Make sure the OR optimization is not attempted if
356# comparisons between columns of different affinities are needed.
357#
358do_test where2-6.7 {
359 execsql {
360 CREATE TABLE t2249a(a TEXT UNIQUE, x CHAR(100));
361 CREATE TABLE t2249b(b INTEGER);
362 INSERT INTO t2249a(a) VALUES('0123');
363 INSERT INTO t2249b VALUES(123);
364 }
365 queryplan {
366 -- Because a is type TEXT and b is type INTEGER, both a and b
367 -- will attempt to convert to NUMERIC before the comparison.
368 -- They will thus compare equal.
369 --
370 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a=b;
371 }
372} {123 0123 nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
373do_test where2-6.9 {
374 queryplan {
375 -- The + operator removes affinity from the rhs. No conversions
376 -- occur and the comparison is false. The result is an empty set.
377 --
378 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a=+b;
379 }
380} {nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
381do_test where2-6.9.2 {
382 # The same thing but with the expression flipped around.
383 queryplan {
384 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE +b=a
385 }
386} {nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
387do_test where2-6.10 {
388 queryplan {
389 -- Use + on both sides of the comparison to disable indices
390 -- completely. Make sure we get the same result.
391 --
392 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE +a=+b;
393 }
394} {nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
395do_test where2-6.11 {
396 # This will not attempt the OR optimization because of the a=b
397 # comparison.
398 queryplan {
399 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a=b OR a='hello';
400 }
401} {123 0123 nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
402do_test where2-6.11.2 {
403 # Permutations of the expression terms.
404 queryplan {
405 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE b=a OR a='hello';
406 }
407} {123 0123 nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
408do_test where2-6.11.3 {
409 # Permutations of the expression terms.
410 queryplan {
411 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE 'hello'=a OR b=a;
412 }
413} {123 0123 nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
414do_test where2-6.11.4 {
415 # Permutations of the expression terms.
416 queryplan {
417 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR b=a;
418 }
419} {123 0123 nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
420ifcapable explain&&subquery {
421 # These tests are not run if subquery support is not included in the
422 # build. This is because these tests test the "a = 1 OR a = 2" to
423 # "a IN (1, 2)" optimisation transformation, which is not enabled if
424 # subqueries and the IN operator is not available.
425 #
426 do_test where2-6.12 {
427 # In this case, the +b disables the affinity conflict and allows
428 # the OR optimization to be used again. The result is now an empty
429 # set, the same as in where2-6.9.
430 queryplan {
431 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a=+b OR a='hello';
432 }
433 } {nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
434 do_test where2-6.12.2 {
435 # In this case, the +b disables the affinity conflict and allows
436 # the OR optimization to be used again. The result is now an empty
437 # set, the same as in where2-6.9.
438 queryplan {
439 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR +b=a;
440 }
441 } {nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
442 do_test where2-6.12.3 {
443 # In this case, the +b disables the affinity conflict and allows
444 # the OR optimization to be used again. The result is now an empty
445 # set, the same as in where2-6.9.
446 queryplan {
447 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE +b=a OR a='hello';
448 }
449 } {nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
450 do_test where2-6.13 {
451 # The addition of +a on the second term disabled the OR optimization.
452 # But we should still get the same empty-set result as in where2-6.9.
453 queryplan {
454 SELECT b,a FROM t2249b CROSS JOIN t2249a WHERE a=+b OR +a='hello';
455 }
456 } {nosort t2249b * t2249a sqlite_autoindex_t2249a_1}
457}
458
459# Bug 2026-06-17T05:04:48Z
460#
461do_execsql_test where2-6.14.1 {
462 CREATE TABLE t614a(a TEXT COLLATE NOCASE, b TEXT COLLATE NOCASE);
463 INSERT INTO t614a VALUES('AAA','BBB');
464 CREATE TABLE t614b(x,y,c TEXT);
465 INSERT INTO t614b(c) VALUES('aaa'),('bbb');
466 CREATE INDEX t614b_c ON t614b(c);
467 SELECT c FROM t614a, t614b WHERE a=c OR b=c;
468} {aaa bbb}
469do_execsql_test where2-6.14.2 {
470 SELECT c FROM t614a, t614b WHERE c=a OR c=b;
471} {}
472do_execsql_test where2-6.14.3 {
473 SELECT c FROM t614a, t614b WHERE c=a OR b=c;
474} {bbb}
475do_execsql_test where2-6.14.4 {
476 SELECT c FROM t614a, t614b WHERE a=c OR c=b;
477} {aaa}
478do_execsql_test where2-6.15.1 {
479 CREATE TABLE t615a(a TEXT, b TEXT);
480 INSERT INTO t615a VALUES('AAA','BBB');
481 CREATE TABLE t615b(x,y,c TEXT COLLATE NOCASE);
482 INSERT INTO t615b(c) VALUES('aaa'),('bbb');
483 CREATE INDEX t615b_c ON t615b(c);
484 SELECT c FROM t615a, t615b WHERE c=a OR c=b;
485} {aaa bbb}
486do_execsql_test where2-6.15.2 {
487 SELECT c FROM t615a, t615b WHERE a=c OR b=c;
488} {}
489do_execsql_test where2-6.15.3 {
490 SELECT c FROM t615a, t615b WHERE c=a OR b=c;
491} {aaa}
492do_execsql_test where2-6.15.4 {
493 SELECT c FROM t615a, t615b WHERE a=c OR c=b;
494} {bbb}
495do_execsql_test where2-6.16.1 {
496 CREATE TABLE t616a(a TEXT COLLATE NOCASE, b TEXT COLLATE NOCASE);
497 INSERT INTO t616a VALUES('AAA','BBB');
498 CREATE TABLE t616b(x,y,c TEXT COLLATE NOCASE);
499 INSERT INTO t616b(c) VALUES('aaa'),('bbb');
500 CREATE INDEX t616b_c ON t616b(c);
501 SELECT c FROM t616a, t616b WHERE c=a OR c=b;
502} {aaa bbb}
503do_execsql_test where2-6.16.2 {
504 SELECT c FROM t616a, t616b WHERE a=c OR b=c;
505} {aaa bbb}
506do_execsql_test where2-6.16.3 {
507 SELECT c FROM t616a, t616b WHERE c=a OR b=c;
508} {aaa bbb}
509do_execsql_test where2-6.16.4 {
510 SELECT c FROM t616a, t616b WHERE a=c OR c=b;
511} {aaa bbb}
512
513# Assertion faults added by check-in 2026-06-17T13:31:01.199Z and
514# subsequently discovered by dbsqlfuzz.
515#
516do_execsql_test where2-6.17.1 {
517 CREATE TABLE t617_a(a INTEGER PRIMARY KEY, b TEXT);
518 CREATE TABLE t617_b(c INTEGER PRIMARY KEY, d TEXT);
519 CREATE VIEW t617 AS SELECT a, b FROM t617_a UNION ALL SELECT c, c FROM t617_b;
520}
521do_execsql_test where2-6.17.2 {
522 SELECT EXISTS(
523 SELECT 1
524 FROM (SELECT 1 FROM t617 WHERE a='two' OR b='i')
525 WHERE a='ex' OR b='iv'
526 ) FROM t617;
527} {}
528do_execsql_test where2-6.17.3 {
529 SELECT * FROM t617 JOIN t617 USING(a,a,b) WHERE (b=0) OR a=10;
530} {}
531
532
533# Variations on the order of terms in a WHERE clause in order
534# to make sure the OR optimizer can recognize them all.
535do_test where2-6.20 {
536 queryplan {
537 SELECT x.a, y.a FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a
538 }
539} {0123 0123 nosort x sqlite_autoindex_t2249a_1 y sqlite_autoindex_t2249a_1}
540ifcapable explain&&subquery {
541 # These tests are not run if subquery support is not included in the
542 # build. This is because these tests test the "a = 1 OR a = 2" to
543 # "a IN (1, 2)" optimisation transformation, which is not enabled if
544 # subqueries and the IN operator is not available.
545 #
546 do_test where2-6.21 {
547 queryplan {
548 SELECT x.a,y.a FROM t2249a x CROSS JOIN t2249a y
549 WHERE x.a=y.a OR y.a='hello'
550 }
551 } {0123 0123 nosort x sqlite_autoindex_t2249a_1 y sqlite_autoindex_t2249a_1}
552 do_test where2-6.22 {
553 queryplan {
554 SELECT x.a,y.a FROM t2249a x CROSS JOIN t2249a y
555 WHERE y.a=x.a OR y.a='hello'
556 }
557 } {0123 0123 nosort x sqlite_autoindex_t2249a_1 y sqlite_autoindex_t2249a_1}
558 do_test where2-6.23 {
559 queryplan {
560 SELECT x.a,y.a FROM t2249a x CROSS JOIN t2249a y
561 WHERE y.a='hello' OR x.a=y.a
562 }
563 } {0123 0123 nosort x sqlite_autoindex_t2249a_1 y sqlite_autoindex_t2249a_1}
564}
565
566# Unique queries (queries that are guaranteed to return only a single
567# row of result) do not call the sorter. But all tables must give
568# a unique result. If any one table in the join does not give a unique
569# result then sorting is necessary.
570#
571do_test where2-7.1 {
572 cksort {
573 create table t8(a unique, b, c);
574 insert into t8 values(1,2,3);
575 insert into t8 values(2,3,4);
576 create table t9(x,y);
577 insert into t9 values(2,4);
578 insert into t9 values(2,3);
579 select y from t8, t9 where a=1 order by a, y;
580 }
581} {3 4 sort}
582do_test where2-7.2 {
583 cksort {
584 select * from t8 where a=1 order by b, c
585 }
586} {1 2 3 nosort}
587do_test where2-7.3 {
588 cksort {
589 select * from t8, t9 where a=1 and y=3 order by b, x
590 }
591} {1 2 3 2 3 sort}
592do_test where2-7.4 {
593 cksort {
594 create unique index i9y on t9(y);
595 select * from t8, t9 where a=1 and y=3 order by b, x
596 }
597} {1 2 3 2 3 nosort}
598
599} ;# if {[permutation] != "no_optimization"}
600
601# Ticket #1807. Using IN constrains on multiple columns of
602# a multi-column index.
603#
604ifcapable subquery {
605 do_test where2-8.1 {
606 execsql {
607 SELECT * FROM t1 WHERE x IN (20,21) AND y IN (1,2)
608 }
609 } {}
610 do_test where2-8.2 {
611 execsql {
612 SELECT * FROM t1 WHERE x IN (1,2) AND y IN (-5,-6)
613 }
614 } {}
615 execsql {CREATE TABLE tx AS SELECT * FROM t1}
616 do_test where2-8.3 {
617 execsql {
618 SELECT w FROM t1
619 WHERE x IN (SELECT x FROM tx WHERE rowid<0)
620 AND +y IN (SELECT y FROM tx WHERE rowid=1)
621 }
622 } {}
623 do_test where2-8.4 {
624 execsql {
625 SELECT w FROM t1
626 WHERE x IN (SELECT x FROM tx WHERE rowid=1)
627 AND y IN (SELECT y FROM tx WHERE rowid<0)
628 }
629 } {}
630 #set sqlite_where_trace 1
631 do_test where2-8.5 {
632 execsql {
633 CREATE INDEX tx_xyz ON tx(x, y, z, w);
634 SELECT w FROM tx
635 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
636 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
637 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 12 AND 14)
638 }
639 } {12 13 14}
640 do_test where2-8.6 {
641 execsql {
642 SELECT w FROM tx
643 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
644 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 12 AND 14)
645 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
646 }
647 } {12 13 14}
648 do_test where2-8.7 {
649 execsql {
650 SELECT w FROM tx
651 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 12 AND 14)
652 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
653 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
654 }
655 } {10 11 12 13 14 15}
656 do_test where2-8.8 {
657 execsql {
658 SELECT w FROM tx
659 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
660 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
661 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
662 }
663 } {10 11 12 13 14 15 16 17 18 19 20}
664 do_test where2-8.9 {
665 execsql {
666 SELECT w FROM tx
667 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
668 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
669 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 2 AND 4)
670 }
671 } {}
672 do_test where2-8.10 {
673 execsql {
674 SELECT w FROM tx
675 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
676 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 2 AND 4)
677 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
678 }
679 } {}
680 do_test where2-8.11 {
681 execsql {
682 SELECT w FROM tx
683 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 2 AND 4)
684 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
685 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
686 }
687 } {}
688 do_test where2-8.12 {
689 execsql {
690 SELECT w FROM tx
691 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
692 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
693 AND z IN (SELECT z FROM t1 WHERE w BETWEEN -4 AND -2)
694 }
695 } {}
696 do_test where2-8.13 {
697 execsql {
698 SELECT w FROM tx
699 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
700 AND y IN (SELECT y FROM t1 WHERE w BETWEEN -4 AND -2)
701 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
702 }
703 } {}
704 do_test where2-8.14 {
705 execsql {
706 SELECT w FROM tx
707 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN -4 AND -2)
708 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
709 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
710 }
711 } {}
712 do_test where2-8.15 {
713 execsql {
714 SELECT w FROM tx
715 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
716 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
717 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 200 AND 300)
718 }
719 } {}
720 do_test where2-8.16 {
721 execsql {
722 SELECT w FROM tx
723 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
724 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 200 AND 300)
725 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
726 }
727 } {}
728 do_test where2-8.17 {
729 execsql {
730 SELECT w FROM tx
731 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 200 AND 300)
732 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
733 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
734 }
735 } {}
736 do_test where2-8.18 {
737 execsql {
738 SELECT w FROM tx
739 WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20)
740 AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20)
741 AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 200 AND 300)
742 }
743 } {}
744 do_test where2-8.19 {
745 execsql {
746 SELECT w FROM tx
747 WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20)
748 AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 200 AND 300)
749 AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20)
750 }
751 } {}
752 do_test where2-8.20 {
753 execsql {
754 SELECT w FROM tx
755 WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 200 AND 300)
756 AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20)
757 AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20)
758 }
759 } {}
760}
761
762# Make sure WHERE clauses of the form A=1 AND (B=2 OR B=3) are optimized
763# when we have an index on A and B.
764#
765ifcapable or_opt&&tclvar {
766 do_test where2-9.1 {
767 execsql {
768 BEGIN;
769 CREATE TABLE t10(a,b,c);
770 INSERT INTO t10 VALUES(1,1,1);
771 INSERT INTO t10 VALUES(1,2,2);
772 INSERT INTO t10 VALUES(1,3,3);
773 }
774 for {set i 4} {$i<=1000} {incr i} {
775 execsql {INSERT INTO t10 VALUES(1,$i,$i)}
776 }
777 execsql {
778 CREATE INDEX i10 ON t10(a,b);
779 COMMIT;
780 SELECT count(*) FROM t10;
781 }
782 } 1000
783 ifcapable subquery {
784 do_test where2-9.2 {
785 count {
786 SELECT * FROM t10 WHERE a=1 AND (b=2 OR b=3)
787 }
788 } {1 2 2 1 3 3 7}
789 }
790}
791
792# Indices with redundant columns
793#
794do_test where2-11.1 {
795 execsql {
796 CREATE TABLE t11(a,b,c,d);
797 CREATE INDEX i11aba ON t11(a,b,a,c); -- column A occurs twice.
798 INSERT INTO t11 VALUES(1,2,3,4);
799 INSERT INTO t11 VALUES(5,6,7,8);
800 INSERT INTO t11 VALUES(1,2,9,10);
801 INSERT INTO t11 VALUES(5,11,12,13);
802 SELECT c FROM t11 WHERE a=1 AND b=2 ORDER BY c;
803 }
804} {3 9}
805do_test where2-11.2 {
806 execsql {
807 CREATE INDEX i11cccccccc ON t11(c,c,c,c,c,c,c,c); -- repeated column
808 SELECT d FROM t11 WHERE c=9;
809 }
810} {10}
811do_test where2-11.3 {
812 execsql {
813 SELECT d FROM t11 WHERE c IN (1,2,3,4,5);
814 }
815} {4}
816do_test where2-11.4 {
817 execsql {
818 SELECT d FROM t11 WHERE c=7 OR (a=1 AND b=2) ORDER BY d;
819 }
820} {4 8 10}
821
822# Verify that the OR clause is used in an outer loop even when
823# the OR clause scores slightly better on an inner loop.
824if {[permutation] != "no_optimization"} {
825do_execsql_test where2-12.1 {
826 CREATE TABLE t12(x INTEGER PRIMARY KEY, y INT, z CHAR(100));
827 CREATE INDEX t12y ON t12(y);
828 EXPLAIN QUERY PLAN
829 SELECT a.x, b.x
830 FROM t12 AS a JOIN t12 AS b ON a.y=b.x
831 WHERE (b.x=$abc OR b.y=$abc);
832} {/SEARCH b .*SEARCH b /}
833}
834
835# Verify that all necessary OP_OpenRead opcodes occur in the OR optimization.
836#
837do_execsql_test where2-13.1 {
838 CREATE TABLE t13(a,b);
839 CREATE INDEX t13a ON t13(a);
840 INSERT INTO t13 VALUES(4,5);
841 SELECT * FROM t13 WHERE (1=2 AND a=3) OR a=4;
842} {4 5}
843
844# https://sqlite.org/src/info/5e3c886796e5512e (2016-03-09)
845# Correlated subquery on the RHS of an IN operator
846#
847do_execsql_test where2-14.1 {
848 CREATE TABLE t14a(x INTEGER PRIMARY KEY);
849 INSERT INTO t14a(x) VALUES(1),(2),(3),(4);
850 CREATE TABLE t14b(y INTEGER PRIMARY KEY);
851 INSERT INTO t14b(y) VALUES(1);
852 SELECT x FROM t14a WHERE x NOT IN (SELECT x FROM t14b);
853} {}
854
855# Demonstrate that the code removed by
856# https://sqlite.org/src/info/2025-09-17T17:09:07Z is in fact
857# necessary. Answer confirmed by PostgreSQL
858#
859do_execsql_test where2-15.1 {
860 DROP TABLE IF EXISTS t1;
861 CREATE TABLE t1(a INTEGER PRIMARY KEY,b INT);
862 INSERT INTO t1 VALUES(12,34),(56,78),(90,12);
863 DROP TABLE IF EXISTS t2;
864 CREATE TABLE t2(x INT UNIQUE);
865 INSERT INTO t2 VALUES(78),(12);
866 SELECT a,b,quote(x),'|'
867 FROM t1 LEFT JOIN (SELECT x FROM t2) AS s1 ON t1.b=s1.x
868 ORDER BY a,EXISTS(SELECT 1 FROM t1 LEFT JOIN (SELECT x AS y FROM t2) AS s2 ON t1.b=s2.y),x;
869} {12 34 NULL | 56 78 78 | 90 12 12 |}
870
871# Demonstrate that CROSS JOIN is a join reordering barrier.
872#
873reset_db
874do_execsql_test where2-16.1 {
875 CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT);
876 CREATE TABLE t2(c INTEGER PRIMARY KEY, d INT);
877 CREATE TABLE t3(e INTEGER PRIMARY KEY, f INT);
878 CREATE TABLE t4(g INTEGER PRIMARY KEY, h INT);
879}
880
881# Here the query planner wants to move t4 forward so that it is in front of
882# t1. Ensure that does not happen.
883do_execsql_test where2-16.2 {
884 EXPLAIN QUERY PLAN
885 SELECT *
886 FROM t1, t2 CROSS JOIN t3, t4
887 WHERE t4.g=1
888 AND t1.a=t4.h
889 AND t2.c=t1.b
890 AND t3.e=t2.d;
891} {~/.* t4 .* t[12] .*/}
892
893# In this case the planner wants to move t1 to come after t4. Ensure that
894# does not happen.
895do_execsql_test where2-16.2 {
896 EXPLAIN QUERY PLAN
897 SELECT *
898 FROM t1, t2 CROSS JOIN t3, t4
899 WHERE t2.c=1
900 AND t3.e=t2.d
901 AND t4.g=t3.f
902 AND t1.a=t4.h;
903} {~/.* t[34] .* t1 .*/}
904
905# 2026-05-18 bug report 2026-05-17T11:46:40Z
906#
907do_execsql_test where2-17.0 {
908 DROP TABLE IF EXISTS t1;
909 CREATE TABLE t1(a TEXT);
910 INSERT INTO t1 VALUES('A'),('b');
911 SELECT * FROM t1 WHERE (a='a' COLLATE nocase) OR a='b' ORDER BY +a;
912} {A b}
913do_execsql_test where2-17.1 {
914 CREATE INDEX t1_a ON t1(a);
915 SELECT * FROM t1 WHERE (a='a' COLLATE nocase) OR a='b' ORDER BY +a;
916} {A b}
917
918finish_test