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 / autoindex1.test

692 lines · 24779 bytes Raw Blame History
1# 2010 April 07
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 script is testing automatic index creation logic.
13#
14# EVIDENCE-OF: R-34271-33106 PRAGMA automatic_index; PRAGMA
15# automatic_index = boolean; Query, set, or clear the automatic indexing
16# capability.
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# If the library is not compiled with automatic index support then
22# skip all tests in this file.
23#
24ifcapable {!autoindex} {
25 finish_test
26 return
27}
28set testprefix autoindex
29
30# Setup for logging
31db close
32sqlite3_shutdown
33test_sqlite3_log [list lappend ::log]
34set ::log [list]
35sqlite3 db test.db
36
37
38# With automatic index turned off, we do a full scan of the T2 table
39do_test autoindex1-100 {
40 db eval {
41 CREATE TABLE t1(a,b);
42 INSERT INTO t1 VALUES(1,11);
43 INSERT INTO t1 VALUES(2,22);
44 INSERT INTO t1 SELECT a+2, b+22 FROM t1;
45 INSERT INTO t1 SELECT a+4, b+44 FROM t1;
46 CREATE TABLE t2(c,d);
47 INSERT INTO t2 SELECT a, 900+b FROM t1;
48 }
49 db eval {
50 PRAGMA automatic_index=OFF;
51 SELECT b, d FROM t1 JOIN t2 ON a=c ORDER BY b;
52 }
53} {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
54do_test autoindex1-101 {
55 db status step
56} {63}
57do_test autoindex1-102 {
58 db status autoindex
59} {0}
60
61# With autoindex turned on, we build an index once and then use that index
62# to find T2 values.
63do_test autoindex1-110 {
64 db eval {
65 PRAGMA automatic_index=ON;
66 SELECT b, d FROM t1 JOIN t2 ON a=c ORDER BY b;
67 }
68} {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
69do_test autoindex1-111 {
70 db status step
71} {7}
72do_test autoindex1-112 {
73 db status autoindex
74} {7}
75do_test autoindex1-113 {
76 set ::log
77} {SQLITE_WARNING_AUTOINDEX {automatic index on t2(c)}}
78
79db close
80sqlite3_shutdown
81test_sqlite3_log
82sqlite3_initialize
83sqlite3 db test.db
84
85# The same test as above, but this time the T2 query is a subquery rather
86# than a join.
87do_test autoindex1-200 {
88 db eval {
89 PRAGMA automatic_index=OFF;
90 SELECT b, (SELECT d FROM t2 WHERE c=a) FROM t1;
91 }
92} {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
93do_test autoindex1-201 {
94 db status step
95} {35}
96do_test autoindex1-202 {
97 db status autoindex
98} {0}
99do_test autoindex1-210 {
100 db eval {
101 PRAGMA automatic_index=ON;
102 ANALYZE;
103 UPDATE sqlite_stat1 SET stat='10000' WHERE tbl='t1';
104 -- Table t2 actually contains 8 rows.
105 UPDATE sqlite_stat1 SET stat='16' WHERE tbl='t2';
106 ANALYZE sqlite_master;
107 SELECT b, (SELECT d FROM t2 WHERE c=a) FROM t1;
108 }
109} {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
110do_test autoindex1-211 {
111 db status step
112} {7}
113do_test autoindex1-212 {
114 db status autoindex
115} {7}
116
117
118# Modify the second table of the join while the join is in progress
119#
120do_execsql_test autoindex1-299 {
121 UPDATE sqlite_stat1 SET stat='10000' WHERE tbl='t2';
122 ANALYZE sqlite_master;
123 EXPLAIN QUERY PLAN
124 SELECT b, d FROM t1 CROSS JOIN t2 ON (c=a);
125} {/AUTOMATIC COVERING INDEX/}
126do_test autoindex1-300 {
127 set r {}
128 db eval {SELECT b, d FROM t1 CROSS JOIN t2 ON (c=a)} {
129 lappend r $b $d
130 db eval {UPDATE t2 SET d=d+1}
131 }
132 set r
133} {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
134do_test autoindex1-310 {
135 db eval {SELECT d FROM t2 ORDER BY d}
136} {919 930 941 952 963 974 985 996}
137
138# The next test does a 10-way join on unindexed tables. Without
139# automatic indices, the join will take a long time to complete.
140# With automatic indices, it should only take about a second.
141#
142do_test autoindex1-400 {
143 db eval {
144 CREATE TABLE t4(a, b);
145 INSERT INTO t4 VALUES(1,2);
146 INSERT INTO t4 VALUES(2,3);
147 }
148 for {set n 2} {$n<4096} {set n [expr {$n+$n}]} {
149 db eval {INSERT INTO t4 SELECT a+$n, b+$n FROM t4}
150 }
151 db eval {
152 SELECT count(*) FROM t4;
153 }
154} {4096}
155do_test autoindex1-401 {
156 db eval {
157 SELECT count(*)
158 FROM t4 AS x1
159 JOIN t4 AS x2 ON x2.a=x1.b
160 JOIN t4 AS x3 ON x3.a=x2.b
161 JOIN t4 AS x4 ON x4.a=x3.b
162 JOIN t4 AS x5 ON x5.a=x4.b
163 JOIN t4 AS x6 ON x6.a=x5.b
164 JOIN t4 AS x7 ON x7.a=x6.b
165 JOIN t4 AS x8 ON x8.a=x7.b
166 JOIN t4 AS x9 ON x9.a=x8.b
167 JOIN t4 AS x10 ON x10.a=x9.b;
168 }
169} {4087}
170
171# Ticket [8011086c85c6c404014c947fcf3eb9f42b184a0d] from 2010-07-08
172# Make sure automatic indices are not created for the RHS of an IN expression
173# that is not a correlated subquery.
174#
175do_execsql_test autoindex1-500 {
176 CREATE TABLE t501(a INTEGER PRIMARY KEY, b);
177 CREATE TABLE t502(x INTEGER PRIMARY KEY, y);
178 INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t501',null,'1000000');
179 INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t502',null,'1000');
180 ANALYZE sqlite_master;
181}
182do_eqp_test autoindex1-500.1 {
183 SELECT b FROM t501
184 WHERE t501.a IN (SELECT x FROM t502 WHERE y=?);
185} {
186 QUERY PLAN
187 |--SEARCH t501 USING INTEGER PRIMARY KEY (rowid=?)
188 `--LIST SUBQUERY xxxxxx
189 `--SCAN t502
190}
191do_eqp_test autoindex1-501 {
192 SELECT b FROM t501
193 WHERE t501.a IN (SELECT x FROM t502 WHERE y=t501.b);
194} {
195 QUERY PLAN
196 |--SCAN t501
197 `--CORRELATED LIST SUBQUERY xxxxxx
198 |--BLOOM FILTER ON t502 (y=?)
199 `--SEARCH t502 USING AUTOMATIC COVERING INDEX (y=?)
200}
201do_eqp_test autoindex1-502 {
202 SELECT b FROM t501
203 WHERE t501.a=123
204 AND t501.a IN (SELECT x FROM t502 WHERE y=t501.b);
205} {
206 QUERY PLAN
207 |--SEARCH t501 USING INTEGER PRIMARY KEY (rowid=?)
208 `--CORRELATED LIST SUBQUERY xxxxxx
209 `--SCAN t502
210}
211
212# The following code checks a performance regression reported on the
213# mailing list on 2010-10-19. The problem is that the nRowEst field
214# of ephermeral tables was not being initialized correctly and so no
215# automatic index was being created for the emphemeral table when it was
216# used as part of a join.
217#
218do_execsql_test autoindex1-600 {
219 CREATE TABLE flock_owner(
220 owner_rec_id INTEGER CONSTRAINT flock_owner_key PRIMARY KEY,
221 flock_no VARCHAR(6) NOT NULL REFERENCES flock (flock_no),
222 owner_person_id INTEGER NOT NULL REFERENCES person (person_id),
223 owner_change_date TEXT, last_changed TEXT NOT NULL,
224 CONSTRAINT fo_owner_date UNIQUE (flock_no, owner_change_date)
225 );
226 CREATE TABLE sheep (
227 Sheep_No char(7) NOT NULL,
228 Date_of_Birth char(8),
229 Sort_DoB text,
230 Flock_Book_Vol char(2),
231 Breeder_No char(6),
232 Breeder_Person integer,
233 Originating_Flock char(6),
234 Registering_Flock char(6),
235 Tag_Prefix char(9),
236 Tag_No char(15),
237 Sort_Tag_No integer,
238 Breeders_Temp_Tag char(15),
239 Sex char(1),
240 Sheep_Name char(32),
241 Sire_No char(7),
242 Dam_No char(7),
243 Register_Code char(1),
244 Colour char(48),
245 Colour_Code char(2),
246 Pattern_Code char(8),
247 Horns char(1),
248 Litter_Size char(1),
249 Coeff_of_Inbreeding real,
250 Date_of_Registration text,
251 Date_Last_Changed text,
252 UNIQUE(Sheep_No));
253 CREATE INDEX fo_flock_no_index
254 ON flock_owner (flock_no);
255 CREATE INDEX fo_owner_change_date_index
256 ON flock_owner (owner_change_date);
257 CREATE INDEX fo_owner_person_id_index
258 ON flock_owner (owner_person_id);
259 CREATE INDEX sheep_org_flock_index
260 ON sheep (originating_flock);
261 CREATE INDEX sheep_reg_flock_index
262 ON sheep (registering_flock);
263}
264do_eqp_test autoindex1-600a {
265 SELECT x.sheep_no, x.registering_flock, x.date_of_registration
266 FROM sheep x LEFT JOIN
267 (SELECT s.sheep_no, prev.flock_no, prev.owner_person_id,
268 s.date_of_registration, prev.owner_change_date
269 FROM sheep s JOIN flock_owner prev ON s.registering_flock =
270 prev.flock_no
271 AND (prev.owner_change_date <= s.date_of_registration || ' 00:00:00')
272 WHERE NOT EXISTS
273 (SELECT 'x' FROM flock_owner later
274 WHERE prev.flock_no = later.flock_no
275 AND later.owner_change_date > prev.owner_change_date
276 AND later.owner_change_date <= s.date_of_registration||' 00:00:00')
277 ) y ON x.sheep_no = y.sheep_no
278 WHERE y.sheep_no IS NULL
279 ORDER BY x.registering_flock;
280} {
281 QUERY PLAN
282 |--MATERIALIZE y
283 | |--SCAN s
284 | |--SEARCH prev USING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date<?)
285 | `--CORRELATED SCALAR SUBQUERY xxxxxx
286 | `--SEARCH later USING COVERING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date>? AND owner_change_date<?)
287 |--SCAN x USING INDEX sheep_reg_flock_index
288 |--BLOOM FILTER ON y (sheep_no=?)
289 `--SEARCH y USING AUTOMATIC COVERING INDEX (sheep_no=?) LEFT-JOIN
290}
291
292
293do_execsql_test autoindex1-700 {
294 CREATE TABLE t5(a, b, c);
295}
296do_eqp_test autoindex1-700a {
297 SELECT a FROM t5 WHERE b=10 ORDER BY c;
298} {
299 QUERY PLAN
300 |--SCAN t5
301 `--USE TEMP B-TREE FOR ORDER BY
302}
303
304# The following checks a performance issue reported on the sqlite-dev
305# mailing list on 2013-01-10
306#
307do_execsql_test autoindex1-800 {
308 CREATE TABLE accounts(
309 _id INTEGER PRIMARY KEY AUTOINCREMENT,
310 account_name TEXT,
311 account_type TEXT,
312 data_set TEXT
313 );
314 CREATE TABLE data(
315 _id INTEGER PRIMARY KEY AUTOINCREMENT,
316 package_id INTEGER REFERENCES package(_id),
317 mimetype_id INTEGER REFERENCES mimetype(_id) NOT NULL,
318 raw_contact_id INTEGER REFERENCES raw_contacts(_id) NOT NULL,
319 is_read_only INTEGER NOT NULL DEFAULT 0,
320 is_primary INTEGER NOT NULL DEFAULT 0,
321 is_super_primary INTEGER NOT NULL DEFAULT 0,
322 data_version INTEGER NOT NULL DEFAULT 0,
323 data1 TEXT,
324 data2 TEXT,
325 data3 TEXT,
326 data4 TEXT,
327 data5 TEXT,
328 data6 TEXT,
329 data7 TEXT,
330 data8 TEXT,
331 data9 TEXT,
332 data10 TEXT,
333 data11 TEXT,
334 data12 TEXT,
335 data13 TEXT,
336 data14 TEXT,
337 data15 TEXT,
338 data_sync1 TEXT,
339 data_sync2 TEXT,
340 data_sync3 TEXT,
341 data_sync4 TEXT
342 );
343 CREATE TABLE mimetypes(
344 _id INTEGER PRIMARY KEY AUTOINCREMENT,
345 mimetype TEXT NOT NULL
346 );
347 CREATE TABLE raw_contacts(
348 _id INTEGER PRIMARY KEY AUTOINCREMENT,
349 account_id INTEGER REFERENCES accounts(_id),
350 sourceid TEXT,
351 raw_contact_is_read_only INTEGER NOT NULL DEFAULT 0,
352 version INTEGER NOT NULL DEFAULT 1,
353 dirty INTEGER NOT NULL DEFAULT 0,
354 deleted INTEGER NOT NULL DEFAULT 0,
355 contact_id INTEGER REFERENCES contacts(_id),
356 aggregation_mode INTEGER NOT NULL DEFAULT 0,
357 aggregation_needed INTEGER NOT NULL DEFAULT 1,
358 custom_ringtone TEXT,
359 send_to_voicemail INTEGER NOT NULL DEFAULT 0,
360 times_contacted INTEGER NOT NULL DEFAULT 0,
361 last_time_contacted INTEGER,
362 starred INTEGER NOT NULL DEFAULT 0,
363 display_name TEXT,
364 display_name_alt TEXT,
365 display_name_source INTEGER NOT NULL DEFAULT 0,
366 phonetic_name TEXT,
367 phonetic_name_style TEXT,
368 sort_key TEXT,
369 sort_key_alt TEXT,
370 name_verified INTEGER NOT NULL DEFAULT 0,
371 sync1 TEXT,
372 sync2 TEXT,
373 sync3 TEXT,
374 sync4 TEXT,
375 sync_uid TEXT,
376 sync_version INTEGER NOT NULL DEFAULT 1,
377 has_calendar_event INTEGER NOT NULL DEFAULT 0,
378 modified_time INTEGER,
379 is_restricted INTEGER DEFAULT 0,
380 yp_source TEXT,
381 method_selected INTEGER DEFAULT 0,
382 custom_vibration_type INTEGER DEFAULT 0,
383 custom_ringtone_path TEXT,
384 message_notification TEXT,
385 message_notification_path TEXT
386 );
387 CREATE INDEX data_mimetype_data1_index ON data (mimetype_id,data1);
388 CREATE INDEX data_raw_contact_id ON data (raw_contact_id);
389 CREATE UNIQUE INDEX mime_type ON mimetypes (mimetype);
390 CREATE INDEX raw_contact_sort_key1_index ON raw_contacts (sort_key);
391 CREATE INDEX raw_contact_sort_key2_index ON raw_contacts (sort_key_alt);
392 CREATE INDEX raw_contacts_contact_id_index ON raw_contacts (contact_id);
393 CREATE INDEX raw_contacts_source_id_account_id_index
394 ON raw_contacts (sourceid, account_id);
395 ANALYZE sqlite_master;
396 INSERT INTO sqlite_stat1
397 VALUES('raw_contacts','raw_contact_sort_key2_index','1600 4');
398 INSERT INTO sqlite_stat1
399 VALUES('raw_contacts','raw_contact_sort_key1_index','1600 4');
400 INSERT INTO sqlite_stat1
401 VALUES('raw_contacts','raw_contacts_source_id_account_id_index',
402 '1600 1600 1600');
403 INSERT INTO sqlite_stat1
404 VALUES('raw_contacts','raw_contacts_contact_id_index','1600 1');
405 INSERT INTO sqlite_stat1 VALUES('mimetypes','mime_type','12 1');
406 INSERT INTO sqlite_stat1
407 VALUES('data','data_mimetype_data1_index','9819 2455 3');
408 INSERT INTO sqlite_stat1 VALUES('data','data_raw_contact_id','9819 7');
409 INSERT INTO sqlite_stat1 VALUES('accounts',NULL,'1');
410 DROP TABLE IF EXISTS sqlite_stat3;
411 ANALYZE sqlite_master;
412
413 EXPLAIN QUERY PLAN
414 SELECT * FROM
415 data JOIN mimetypes ON (data.mimetype_id=mimetypes._id)
416 JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id)
417 JOIN accounts ON (raw_contacts.account_id=accounts._id)
418 WHERE mimetype_id=10 AND data14 IS NOT NULL;
419} {/SEARCH data .*SEARCH raw_contacts/}
420do_execsql_test autoindex1-801 {
421 EXPLAIN QUERY PLAN
422 SELECT * FROM
423 data JOIN mimetypes ON (data.mimetype_id=mimetypes._id)
424 JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id)
425 JOIN accounts ON (raw_contacts.account_id=accounts._id)
426 WHERE mimetypes._id=10 AND data14 IS NOT NULL;
427} {/SEARCH data .*SEARCH raw_contacts/}
428
429# Another test case from an important user of SQLite. The key feature of
430# this test is that the "aggindex" subquery should make use of an
431# automatic index. If it does, the query is fast. If it does not, the
432# query is deathly slow. It worked OK in 3.7.17 but started going slow
433# with version 3.8.0. The problem was fixed for 3.8.7 by reducing the
434# cost estimate for automatic indexes on views and subqueries.
435#
436db close
437forcedelete test.db
438sqlite3 db test.db
439do_execsql_test autoindex1-900 {
440 CREATE TABLE messages (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, message_id, document_id BLOB, in_reply_to, remote_id INTEGER, sender INTEGER, subject_prefix, subject INTEGER, date_sent INTEGER, date_received INTEGER, date_created INTEGER, date_last_viewed INTEGER, mailbox INTEGER, remote_mailbox INTEGER, original_mailbox INTEGER, flags INTEGER, read, flagged, size INTEGER, color, encoding, type INTEGER, pad, conversation_id INTEGER DEFAULT -1, snippet TEXT DEFAULT NULL, fuzzy_ancestor INTEGER DEFAULT NULL, automated_conversation INTEGER DEFAULT 0, root_status INTEGER DEFAULT -1, conversation_position INTEGER DEFAULT -1);
441 CREATE INDEX date_index ON messages(date_received);
442 CREATE INDEX date_last_viewed_index ON messages(date_last_viewed);
443 CREATE INDEX date_created_index ON messages(date_created);
444 CREATE INDEX message_message_id_mailbox_index ON messages(message_id, mailbox);
445 CREATE INDEX message_document_id_index ON messages(document_id);
446 CREATE INDEX message_read_index ON messages(read);
447 CREATE INDEX message_flagged_index ON messages(flagged);
448 CREATE INDEX message_mailbox_index ON messages(mailbox, date_received);
449 CREATE INDEX message_remote_mailbox_index ON messages(remote_mailbox, remote_id);
450 CREATE INDEX message_type_index ON messages(type);
451 CREATE INDEX message_conversation_id_conversation_position_index ON messages(conversation_id, conversation_position);
452 CREATE INDEX message_fuzzy_ancestor_index ON messages(fuzzy_ancestor);
453 CREATE INDEX message_subject_fuzzy_ancestor_index ON messages(subject, fuzzy_ancestor);
454 CREATE INDEX message_sender_subject_automated_conversation_index ON messages(sender, subject, automated_conversation);
455 CREATE INDEX message_sender_index ON messages(sender);
456 CREATE INDEX message_root_status ON messages(root_status);
457 CREATE TABLE subjects (ROWID INTEGER PRIMARY KEY, subject COLLATE RTRIM, normalized_subject COLLATE RTRIM);
458 CREATE INDEX subject_subject_index ON subjects(subject);
459 CREATE INDEX subject_normalized_subject_index ON subjects(normalized_subject);
460 CREATE TABLE addresses (ROWID INTEGER PRIMARY KEY, address COLLATE NOCASE, comment, UNIQUE(address, comment));
461 CREATE INDEX addresses_address_index ON addresses(address);
462 CREATE TABLE mailboxes (ROWID INTEGER PRIMARY KEY, url UNIQUE, total_count INTEGER DEFAULT 0, unread_count INTEGER DEFAULT 0, unseen_count INTEGER DEFAULT 0, deleted_count INTEGER DEFAULT 0, unread_count_adjusted_for_duplicates INTEGER DEFAULT 0, change_identifier, source INTEGER, alleged_change_identifier);
463 CREATE INDEX mailboxes_source_index ON mailboxes(source);
464 CREATE TABLE labels (ROWID INTEGER PRIMARY KEY, message_id INTEGER NOT NULL, mailbox_id INTEGER NOT NULL, UNIQUE(message_id, mailbox_id));
465 CREATE INDEX labels_message_id_mailbox_id_index ON labels(message_id, mailbox_id);
466 CREATE INDEX labels_mailbox_id_index ON labels(mailbox_id);
467
468 explain query plan
469 SELECT messages.ROWID,
470 messages.message_id,
471 messages.remote_id,
472 messages.date_received,
473 messages.date_sent,
474 messages.flags,
475 messages.size,
476 messages.color,
477 messages.date_last_viewed,
478 messages.subject_prefix,
479 subjects.subject,
480 sender.comment,
481 sender.address,
482 NULL,
483 messages.mailbox,
484 messages.original_mailbox,
485 NULL,
486 NULL,
487 messages.type,
488 messages.document_id,
489 sender,
490 NULL,
491 messages.conversation_id,
492 messages.conversation_position,
493 agglabels.labels
494 FROM mailboxes AS mailbox
495 JOIN messages ON mailbox.ROWID = messages.mailbox
496 LEFT OUTER JOIN subjects ON messages.subject = subjects.ROWID
497 LEFT OUTER JOIN addresses AS sender ON messages.sender = sender.ROWID
498 LEFT OUTER JOIN (
499 SELECT message_id, group_concat(mailbox_id) as labels
500 FROM labels GROUP BY message_id
501 ) AS agglabels ON messages.ROWID = agglabels.message_id
502 WHERE (mailbox.url = 'imap://email.app@imap.gmail.com/%5BGmail%5D/All%20Mail')
503 AND (messages.ROWID IN (
504 SELECT labels.message_id
505 FROM labels JOIN mailboxes ON labels.mailbox_id = mailboxes.ROWID
506 WHERE mailboxes.url = 'imap://email.app@imap.gmail.com/INBOX'))
507 AND messages.mailbox in (6,12,18,24,30,36,42,1,7,13,19,25,31,37,43,2,8,
508 14,20,26,32,38,3,9,15,21,27,33,39,4,10,16,22,28,
509 34,40,5,11,17,23,35,41)
510 ORDER BY date_received DESC;
511} {/agglabels USING AUTOMATIC COVERING INDEX/}
512
513# A test case for VIEWs
514#
515do_execsql_test autoindex1-901 {
516 CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z);
517 CREATE TABLE t2(a, b);
518 CREATE VIEW agg2 AS SELECT a, sum(b) AS m FROM t2 GROUP BY a;
519 EXPLAIN QUERY PLAN
520 SELECT t1.z, agg2.m
521 FROM t1 JOIN agg2 ON t1.y=agg2.m
522 WHERE t1.x IN (1,2,3);
523} {/USING AUTOMATIC COVERING INDEX/}
524
525# 2015-04-15: A NULL CollSeq pointer in automatic index creation.
526#
527do_execsql_test autoindex1-920 {
528 CREATE TABLE t920(x);
529 INSERT INTO t920 VALUES(3),(4),(5);
530 SELECT * FROM t920,(SELECT 0 FROM t920),(VALUES(9)) WHERE 5 IN (x);
531} {5 0 9 5 0 9 5 0 9}
532
533#-------------------------------------------------------------------------
534# An IS term from the WHERE clause of a LEFT JOIN cannot be used as an
535# index driver for the RHS of a LEFT JOIN. Prior to this being fixed,
536# the following SELECT count(*) would incorrectly return 1.
537#
538do_execsql_test autoindex1-1010 {
539 CREATE TABLE t11(w);
540 CREATE TABLE t12(y);
541 INSERT INTO t11 VALUES(NULL);
542 INSERT INTO t12 VALUES('notnull');
543}
544do_execsql_test autoindex1-1020 {
545 SELECT count(*) FROM t11 LEFT JOIN t12 WHERE t12.y IS t11.w;
546} 0
547
548# 2022-04-25
549# https://sqlite.org/forum/forumpost/0d3200f4f3bcd3a3
550#
551reset_db
552do_execsql_test autoindex-1100 {
553 CREATE TABLE t1(a INT, b INT);
554 CREATE TABLE t2(c INT, d INT);
555 CREATE TABLE t3(e TEXT, f TEXT);
556 INSERT INTO t1 VALUES(1, 1);
557 INSERT INTO t2 VALUES(1, 2);
558 INSERT INTO t3 VALUES('abc', 'def');
559} {}
560do_execsql_test autoindex-1110 {
561 SELECT * FROM t1, t2 LEFT JOIN t3 ON (t2.d=1) WHERE t2.c = +t1.a;
562} {1 1 1 2 {} {}}
563do_execsql_test autoindex-1120 {
564 SELECT * FROM t1 LEFT JOIN t2 ON (t2.c=+t1.a) LEFT JOIN t3 ON (t2.d IS NULL);
565} {1 1 1 2 {} {}}
566
567# 2025-01-18
568# Added support for automatic indexes on WITHOUT ROWID tables.
569#
570reset_db
571do_execsql_test autoindex-1200 {
572 CREATE TABLE t1(a INT, b INT, x INT, PRIMARY KEY(a,b)) WITHOUT ROWID;
573 INSERT INTO t1 VALUES(1,2,90),(1,3,91),(1,4,92);
574 CREATE TABLE t2a(c INTEGER PRIMARY KEY, i1 INT);
575 CREATE TABLE t2b(i1 INTEGER PRIMARY KEY, d INT);
576 CREATE VIEW t2(c,d) AS SELECT c, d FROM t2a NATURAL JOIN t2b;
577 INSERT INTO t2a VALUES(3,93),(4,94),(5,95),(6,96),(7,97);
578 INSERT INTO t2b VALUES(91,11),(92,22),(93,33),(94,44),(95,55);
579 CREATE TABLE dual(dummy TEXT);
580 INSERT INTO dual(dummy) VALUES('x');
581}
582db null NULL
583do_execsql_test autoindex-1210 {
584 SELECT t1.*, t2.* FROM t2 LEFT OUTER JOIN t1 ON b=c ORDER BY +b;
585} {
586 NULL NULL NULL 5 55
587 1 3 91 3 33
588 1 4 92 4 44
589}
590do_execsql_test autoindex-1211 {
591 EXPLAIN QUERY PLAN
592 SELECT t1.*, t2.* FROM t2 LEFT OUTER JOIN t1 ON b=c ORDER BY +b;
593} {/SEARCH t1 USING AUTOMATIC COVERING INDEX/}
594
595#-------------------------------------------------------------------------
596reset_db
597do_execsql_test 1300 {
598 CREATE TABLE t2(x INT, y TEXT, PRIMARY KEY(x,y)) WITHOUT ROWID;
599 CREATE TABLE t3(c TEXT COLLATE NOCASE);
600 INSERT INTO t2 VALUES(2,'two'),(2,'TWO');
601 INSERT INTO t3 VALUES('two');
602}
603
604do_execsql_test 1310 {
605 SELECT t3.c, t2.x, t2.y FROM t3 CROSS JOIN t2 ON t3.c=t2.y ORDER BY t2.y;
606} {two 2 TWO two 2 two}
607
608do_execsql_test 1320 {
609 PRAGMA automatic_index=OFF;
610 SELECT t3.c, t2.x, t2.y FROM t3 CROSS JOIN t2 ON t3.c=t2.y ORDER BY t2.y;
611} {two 2 TWO two 2 two}
612
613# The PRIMARY KEY uses a different collation (BINARY) from the column's
614# declared collation (NOCASE). The comparison uses NOCASE, so the PK
615# column must still be added to the automatic index a second time.
616#
617reset_db
618do_execsql_test 1400 {
619 CREATE TABLE t2(x INT, y TEXT COLLATE NOCASE, PRIMARY KEY(x, y COLLATE BINARY))
620 WITHOUT ROWID;
621 CREATE TABLE t3(c TEXT);
622 INSERT INTO t2 VALUES(2,'two'),(2,'TWO');
623 INSERT INTO t3 VALUES('two');
624}
625do_execsql_test 1410 {
626 SELECT t3.c, t2.x, t2.y FROM t3 CROSS JOIN t2 ON t2.y=t3.c ORDER BY t2.y;
627} {two 2 TWO two 2 two}
628do_execsql_test 1420 {
629 PRAGMA automatic_index=OFF;
630 SELECT t3.c, t2.x, t2.y FROM t3 CROSS JOIN t2 ON t2.y=t3.c ORDER BY t2.y;
631} {two 2 TWO two 2 two}
632
633# The PRIMARY KEY and the comparison both use NOCASE. Values are unique
634# under NOCASE, so no extra column is required.
635#
636reset_db
637do_execsql_test 1500 {
638 CREATE TABLE t2(x INT, y TEXT, PRIMARY KEY(x, y COLLATE NOCASE)) WITHOUT ROWID;
639 CREATE TABLE t3(c TEXT COLLATE NOCASE);
640 INSERT INTO t2 VALUES(2,'two'),(3,'TWO');
641 INSERT INTO t3 VALUES('two');
642}
643do_execsql_test 1510 {
644 SELECT t3.c, t2.x, t2.y FROM t3 CROSS JOIN t2 ON t3.c=t2.y ORDER BY t2.x;
645} {two 2 two two 3 TWO}
646do_eqp_test 1520 {
647 SELECT t3.c, t2.x, t2.y FROM t3 CROSS JOIN t2 ON t3.c=t2.y;
648} {/SEARCH t2 USING AUTOMATIC COVERING INDEX/}
649
650# 2026-11-03: A WITHOUT ROWID table with a PRIMARY KEY column whose index
651# is BMS-1 (63) or greater. The automatic index must include the PK
652# column(s) to keep entries unique, even if the query does not otherwise
653# reference any column >= 63. Otherwise rows with equal values in the
654# indexed column(s) are collapsed into a single index entry.
655#
656foreach {tn pk} {
657 1 {c65}
658 2 {c63}
659 3 {c64}
660 4 {c1, c65}
661 5 {c65, c1}
662 6 {c63, c64, c65}
663} {
664 reset_db
665 do_execsql_test 1600.$tn.0 "
666 CREATE TABLE w(c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,c21,c22,c23,c24,c25,c26,c27,c28,c29,c30,c31,c32,c33,c34,c35,c36,c37,c38,c39,c40,c41,c42,c43,c44,c45,c46,c47,c48,c49,c50,c51,c52,c53,c54,c55,c56,c57,c58,c59,c60,c61,c62,c63,c64,c65, PRIMARY KEY($pk)) WITHOUT ROWID;
667 "
668 do_execsql_test 1600.$tn.1 {
669 INSERT INTO w(c0, c1, c63, c64, c65) VALUES
670 (1, 1, 1, 1, 1), (1, 2, 2, 2, 2), (1, 3, 3, 3, 3);
671 CREATE TABLE t(a);
672 INSERT INTO t VALUES(1),(1);
673 }
674 do_eqp_test 1600.$tn.2 {
675 SELECT t.rowid, w.c0 FROM t CROSS JOIN w ON w.c0=t.a;
676 } {/SEARCH w USING AUTOMATIC COVERING INDEX/}
677 do_execsql_test 1600.$tn.3 {
678 SELECT t.rowid, w.c0 FROM t CROSS JOIN w ON w.c0=t.a;
679 } {1 1 1 1 1 1 2 1 2 1 2 1}
680 do_execsql_test 1600.$tn.4 {
681 SELECT count(*) FROM t CROSS JOIN w ON w.c0=t.a;
682 } {6}
683 do_execsql_test 1600.$tn.5 {
684 SELECT t.rowid, w.c65 FROM t CROSS JOIN w ON w.c0=t.a ORDER BY 1, 2;
685 } {1 1 1 2 1 3 2 1 2 2 2 3}
686 do_execsql_test 1600.$tn.6 {
687 PRAGMA automatic_index=OFF;
688 SELECT count(*) FROM t CROSS JOIN w ON w.c0=t.a;
689 } {6}
690}
691
692finish_test