3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the LIKE and GLOB operators and
13 # in particular the optimizations that occur to help those operators
16 # $Id: like.test,v 1.13 2009/06/07 23:45:11 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 # Create some sample data to work with.
25 CREATE TABLE t1(x TEXT);
43 db eval {INSERT INTO t1 VALUES(:str)}
46 SELECT count(*) FROM t1;
50 # Test that both case sensitive and insensitive version of LIKE work.
54 SELECT x FROM t1 WHERE x LIKE 'abc' ORDER BY 1;
59 SELECT x FROM t1 WHERE x GLOB 'abc' ORDER BY 1;
64 SELECT x FROM t1 WHERE x LIKE 'ABC' ORDER BY 1;
69 SELECT x FROM t1 WHERE x LIKE 'aBc' ORDER BY 1;
74 PRAGMA case_sensitive_like=on;
75 SELECT x FROM t1 WHERE x LIKE 'abc' ORDER BY 1;
80 PRAGMA case_sensitive_like; -- no argument; does not change setting
81 SELECT x FROM t1 WHERE x LIKE 'abc' ORDER BY 1;
86 SELECT x FROM t1 WHERE x GLOB 'abc' ORDER BY 1;
91 SELECT x FROM t1 WHERE x LIKE 'ABC' ORDER BY 1;
96 SELECT x FROM t1 WHERE x LIKE 'aBc' ORDER BY 1;
101 PRAGMA case_sensitive_like=off;
102 SELECT x FROM t1 WHERE x LIKE 'abc' ORDER BY 1;
107 PRAGMA case_sensitive_like; -- No argument, does not change setting.
108 SELECT x FROM t1 WHERE x LIKE 'abc' ORDER BY 1;
112 # Tests of the REGEXP operator
115 proc test_regexp {a b} {
116 return [regexp $a $b]
118 db function regexp -argcount 2 test_regexp
120 SELECT x FROM t1 WHERE x REGEXP 'abc' ORDER BY 1;
122 } {{ABC abc xyz} abc abcd}
125 SELECT x FROM t1 WHERE x REGEXP '^abc' ORDER BY 1;
129 # Tests of the MATCH operator
132 proc test_match {a b} {
133 return [string match $a $b]
135 db function match -argcount 2 test_match
137 SELECT x FROM t1 WHERE x MATCH '*abc*' ORDER BY 1;
139 } {{ABC abc xyz} abc abcd}
142 SELECT x FROM t1 WHERE x MATCH 'abc*' ORDER BY 1;
146 # For the remaining tests, we need to have the like optimizations
149 ifcapable !like_opt {
154 # This procedure executes the SQL. Then it appends to the result the
155 # "sort" or "nosort" keyword (as in the cksort procedure above) then
156 # it appends the ::sqlite_query_plan variable.
158 proc queryplan {sql} {
159 set ::sqlite_sort_count 0
160 set data [execsql $sql]
161 if {$::sqlite_sort_count} {set x sort} {set x nosort}
163 return [concat $data $::sqlite_query_plan]
166 # Perform tests on the like optimization.
168 # With no index on t1.x and with case sensitivity turned off, no optimization
172 set sqlite_like_count 0
174 SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
176 } {ABC {ABC abc xyz} abc abcd sort t1 {}}
178 set sqlite_like_count
181 # With an index on t1.x and case sensitivity on, optimize completely.
184 set sqlite_like_count 0
186 PRAGMA case_sensitive_like=on;
187 CREATE INDEX i1 ON t1(x);
190 SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
192 } {abc abcd nosort {} i1}
194 set sqlite_like_count
197 # The LIKE optimization still works when the RHS is a string with no
198 # wildcard. Ticket [e090183531fc2747]
202 SELECT x FROM t1 WHERE x LIKE 'a' ORDER BY 1;
207 SELECT x FROM t1 WHERE x LIKE 'ab' ORDER BY 1;
212 SELECT x FROM t1 WHERE x LIKE 'abcd' ORDER BY 1;
214 } {abcd nosort {} i1}
217 SELECT x FROM t1 WHERE x LIKE 'abcde' ORDER BY 1;
222 # Partial optimization when the pattern does not end in '%'
225 set sqlite_like_count 0
227 SELECT x FROM t1 WHERE x LIKE 'a_c' ORDER BY 1;
231 set sqlite_like_count
234 set sqlite_like_count 0
236 SELECT x FROM t1 WHERE x LIKE 'ab%d' ORDER BY 1;
238 } {abcd abd nosort {} i1}
240 set sqlite_like_count
243 set sqlite_like_count 0
245 SELECT x FROM t1 WHERE x LIKE 'a_c%' ORDER BY 1;
247 } {abc abcd nosort {} i1}
249 set sqlite_like_count
252 # No optimization when the pattern begins with a wildcard.
253 # Note that the index is still used but only for sorting.
256 set sqlite_like_count 0
258 SELECT x FROM t1 WHERE x LIKE '%bcd' ORDER BY 1;
260 } {abcd bcd nosort {} i1}
262 set sqlite_like_count
265 # No optimization for case insensitive LIKE
268 set sqlite_like_count 0
270 PRAGMA case_sensitive_like=off;
271 SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
273 } {ABC {ABC abc xyz} abc abcd nosort {} i1}
275 set sqlite_like_count
278 # No optimization without an index.
281 set sqlite_like_count 0
283 PRAGMA case_sensitive_like=on;
285 SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
287 } {abc abcd sort t1 {}}
289 set sqlite_like_count
292 # No GLOB optimization without an index.
295 set sqlite_like_count 0
297 SELECT x FROM t1 WHERE x GLOB 'abc*' ORDER BY 1;
299 } {abc abcd sort t1 {}}
301 set sqlite_like_count
304 # GLOB is optimized regardless of the case_sensitive_like setting.
307 set sqlite_like_count 0
309 CREATE INDEX i1 ON t1(x);
310 SELECT x FROM t1 WHERE x GLOB 'abc*' ORDER BY 1;
312 } {abc abcd nosort {} i1}
314 set sqlite_like_count
317 set sqlite_like_count 0
319 PRAGMA case_sensitive_like=on;
320 SELECT x FROM t1 WHERE x GLOB 'abc*' ORDER BY 1;
322 } {abc abcd nosort {} i1}
324 set sqlite_like_count
327 set sqlite_like_count 0
329 PRAGMA case_sensitive_like=off;
330 SELECT x FROM t1 WHERE x GLOB 'a[bc]d' ORDER BY 1;
332 } {abd acd nosort {} i1}
334 set sqlite_like_count
337 # GLOB optimization when there is no wildcard. Ticket [e090183531fc2747]
341 SELECT x FROM t1 WHERE x GLOB 'a' ORDER BY 1;
346 SELECT x FROM t1 WHERE x GLOB 'abcd' ORDER BY 1;
348 } {abcd nosort {} i1}
351 SELECT x FROM t1 WHERE x GLOB 'abcde' ORDER BY 1;
357 # No optimization if the LHS of the LIKE is not a column name or
358 # if the RHS is not a string.
361 execsql {PRAGMA case_sensitive_like=on}
362 set sqlite_like_count 0
364 SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1
366 } {abc abcd nosort {} i1}
368 set sqlite_like_count
371 set sqlite_like_count 0
373 SELECT x FROM t1 WHERE +x LIKE 'abc%' ORDER BY 1
375 } {abc abcd nosort {} i1}
377 set sqlite_like_count
380 set sqlite_like_count 0
382 SELECT x FROM t1 WHERE x LIKE ('ab' || 'c%') ORDER BY 1
384 } {abc abcd nosort {} i1}
386 set sqlite_like_count
389 # Collating sequences on the index disable the LIKE optimization.
390 # Or if the NOCASE collating sequence is used, the LIKE optimization
391 # is enabled when case_sensitive_like is OFF.
394 execsql {PRAGMA case_sensitive_like=off}
395 set sqlite_like_count 0
397 SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1
399 } {ABC {ABC abc xyz} abc abcd nosort {} i1}
401 set sqlite_like_count
405 CREATE TABLE t2(x TEXT COLLATE NOCASE);
406 INSERT INTO t2 SELECT * FROM t1;
407 CREATE INDEX i2 ON t2(x COLLATE NOCASE);
409 set sqlite_like_count 0
411 SELECT x FROM t2 WHERE x LIKE 'abc%' ORDER BY 1
413 } {abc ABC {ABC abc xyz} abcd nosort {} i2}
415 set sqlite_like_count
419 PRAGMA case_sensitive_like=on;
421 set sqlite_like_count 0
423 SELECT x FROM t2 WHERE x LIKE 'abc%' ORDER BY 1
425 } {abc abcd nosort {} i2}
427 set sqlite_like_count
431 PRAGMA case_sensitive_like=off;
433 set sqlite_like_count 0
435 SELECT x FROM t2 WHERE x GLOB 'abc*' ORDER BY 1
437 } {abc abcd nosort {} i2}
439 set sqlite_like_count
442 execsql {PRAGMA case_sensitive_like=off}
443 set sqlite_like_count 0
445 SELECT x FROM t1 WHERE x LIKE 'ABC%' ORDER BY 1
447 } {ABC {ABC abc xyz} abc abcd nosort {} i1}
449 set sqlite_like_count
452 set sqlite_like_count 0
454 SELECT x FROM t2 WHERE x LIKE 'ABC%' ORDER BY 1
456 } {abc ABC {ABC abc xyz} abcd nosort {} i2}
458 set sqlite_like_count
462 PRAGMA case_sensitive_like=on;
464 set sqlite_like_count 0
466 SELECT x FROM t2 WHERE x LIKE 'ABC%' ORDER BY 1
468 } {ABC {ABC abc xyz} nosort {} i2}
470 set sqlite_like_count
474 PRAGMA case_sensitive_like=off;
476 set sqlite_like_count 0
478 SELECT x FROM t2 WHERE x GLOB 'ABC*' ORDER BY 1
480 } {ABC {ABC abc xyz} nosort {} i2}
482 set sqlite_like_count
485 # Boundary case. The prefix for a LIKE comparison is rounded up
486 # when constructing the comparison. Example: "ab" becomes "ac".
487 # In other words, the last character is increased by one.
489 # Make sure this happens correctly when the last character is a
490 # "z" and we are doing case-insensitive comparisons.
496 PRAGMA case_sensitive_like=off;
497 INSERT INTO t2 VALUES('ZZ-upper-upper');
498 INSERT INTO t2 VALUES('zZ-lower-upper');
499 INSERT INTO t2 VALUES('Zz-upper-lower');
500 INSERT INTO t2 VALUES('zz-lower-lower');
503 SELECT x FROM t2 WHERE x LIKE 'zz%';
505 } {zz-lower-lower zZ-lower-upper Zz-upper-lower ZZ-upper-upper nosort {} i2}
508 SELECT x FROM t2 WHERE x LIKE 'zZ%';
510 } {zz-lower-lower zZ-lower-upper Zz-upper-lower ZZ-upper-upper nosort {} i2}
513 SELECT x FROM t2 WHERE x LIKE 'Zz%';
515 } {zz-lower-lower zZ-lower-upper Zz-upper-lower ZZ-upper-upper nosort {} i2}
518 SELECT x FROM t2 WHERE x LIKE 'ZZ%';
520 } {zz-lower-lower zZ-lower-upper Zz-upper-lower ZZ-upper-upper nosort {} i2}
523 PRAGMA case_sensitive_like=on;
524 CREATE TABLE t3(x TEXT);
525 CREATE INDEX i3 ON t3(x);
526 INSERT INTO t3 VALUES('ZZ-upper-upper');
527 INSERT INTO t3 VALUES('zZ-lower-upper');
528 INSERT INTO t3 VALUES('Zz-upper-lower');
529 INSERT INTO t3 VALUES('zz-lower-lower');
530 SELECT x FROM t3 WHERE x LIKE 'zz%';
532 } {zz-lower-lower nosort {} i3}
535 SELECT x FROM t3 WHERE x LIKE 'zZ%';
537 } {zZ-lower-upper nosort {} i3}
540 SELECT x FROM t3 WHERE x LIKE 'Zz%';
542 } {Zz-upper-lower nosort {} i3}
545 SELECT x FROM t3 WHERE x LIKE 'ZZ%';
547 } {ZZ-upper-upper nosort {} i3}
552 # Make sure the LIKE prefix optimization does not strip off leading
553 # characters of the like pattern that happen to be quote characters.
556 foreach x { 'abc 'bcd 'def 'ax } {
557 set x2 '[string map {' ''} $x]'
558 db eval "INSERT INTO t2 VALUES($x2)"
561 SELECT * FROM t2 WHERE x LIKE '''a%'
567 SELECT rowid, * FROM t1 WHERE rowid GLOB '1*' ORDER BY rowid;
569 } {1 a 10 ABC 11 CDE 12 {ABC abc xyz}}
573 # Overloading the LIKE function with -1 for the number of arguments
574 # will overload both the 2-argument and the 3-argument LIKE.
579 INSERT INTO t8 VALUES('abcdef');
580 INSERT INTO t8 VALUES('ghijkl');
581 INSERT INTO t8 VALUES('mnopqr');
582 SELECT 1, x FROM t8 WHERE x LIKE '%h%';
583 SELECT 2, x FROM t8 WHERE x LIKE '%h%' ESCAPE 'x';
585 } {1 ghijkl 2 ghijkl}
587 proc newlike {args} {return 1} ;# Alternative LIKE is always return TRUE
588 db function like newlike ;# Uses -1 for nArg in sqlite3_create_function
591 SELECT 1, x FROM t8 WHERE x LIKE '%h%';
592 SELECT 2, x FROM t8 WHERE x LIKE '%h%' ESCAPE 'x';
594 } {1 ghijkl 2 ghijkl}
596 db function like -argcount 2 newlike
598 SELECT 1, x FROM t8 WHERE x LIKE '%h%';
599 SELECT 2, x FROM t8 WHERE x LIKE '%h%' ESCAPE 'x';
601 } {1 abcdef 1 ghijkl 1 mnopqr 2 ghijkl}
603 db function like -argcount 3 newlike
605 SELECT 1, x FROM t8 WHERE x LIKE '%h%';
606 SELECT 2, x FROM t8 WHERE x LIKE '%h%' ESCAPE 'x';
608 } {1 abcdef 1 ghijkl 1 mnopqr 2 abcdef 2 ghijkl 2 mnopqr}
611 ifcapable like_opt&&!icu {
612 # Evaluate SQL. Return the result set followed by the
613 # and the number of full-scan steps.
617 proc count_steps {sql} {
619 lappend r scan [db status step] sort [db status sort]
623 SELECT x FROM t2 WHERE x LIKE 'x%'
625 } {xyz scan 0 sort 0}
628 SELECT x FROM t2 WHERE x LIKE '_y%'
630 } {xyz scan 19 sort 0}
632 set res [sqlite3_exec_hex db {
633 SELECT x FROM t2 WHERE x LIKE '%78%25'
638 set res [sqlite3_exec_hex db {
639 EXPLAIN QUERY PLAN SELECT x FROM t2 WHERE x LIKE '%78%25'
641 regexp {INDEX i2} $res
645 sqlite3_exec_hex db {INSERT INTO t2 VALUES('%ffhello')}
646 set res [sqlite3_exec_hex db {
647 SELECT substr(x,2) AS x FROM t2 WHERE +x LIKE '%ff%25'
651 set res [sqlite3_exec_hex db {
652 SELECT substr(x,2) AS x FROM t2 WHERE x LIKE '%ff%25'
657 set res [sqlite3_exec_hex db {
658 EXPLAIN QUERY PLAN SELECT x FROM t2 WHERE x LIKE '%ff%25'
660 regexp {INDEX i2} $res
664 set res [sqlite3_exec_hex db {
665 SELECT x FROM t2 WHERE x LIKE '%fe%25'
670 set res [sqlite3_exec_hex db {
671 EXPLAIN QUERY PLAN SELECT x FROM t2 WHERE x LIKE '%fe%25'
673 regexp {INDEX i2} $res
677 # Do an SQL statement. Append the search count to the end of the result.
680 set ::sqlite_search_count 0
681 set ::sqlite_like_count 0
682 return [concat [execsql $sql] scan $::sqlite_search_count \
683 like $::sqlite_like_count]
686 # The LIKE and GLOB optimizations do not work on columns with
687 # affinity other than TEXT.
695 a INTEGER PRIMARY KEY,
696 b INTEGER COLLATE nocase UNIQUE,
697 c NUMBER COLLATE nocase UNIQUE,
698 d BLOB COLLATE nocase UNIQUE,
699 e COLLATE nocase UNIQUE,
700 f TEXT COLLATE nocase UNIQUE
702 INSERT INTO t10 VALUES(1,1,1,1,1,1);
703 INSERT INTO t10 VALUES(12,12,12,12,12,12);
704 INSERT INTO t10 VALUES(123,123,123,123,123,123);
705 INSERT INTO t10 VALUES(234,234,234,234,234,234);
706 INSERT INTO t10 VALUES(345,345,345,345,345,345);
707 INSERT INTO t10 VALUES(45,45,45,45,45,45);
710 SELECT a FROM t10 WHERE b LIKE '12%' ORDER BY +a;
712 } {12 123 scan 5 like 6}
715 SELECT a FROM t10 WHERE c LIKE '12%' ORDER BY +a;
717 } {12 123 scan 5 like 6}
720 SELECT a FROM t10 WHERE d LIKE '12%' ORDER BY +a;
722 } {12 123 scan 5 like 6}
725 SELECT a FROM t10 WHERE e LIKE '12%' ORDER BY +a;
727 } {12 123 scan 5 like 6}
730 SELECT a FROM t10 WHERE f LIKE '12%' ORDER BY +a;
732 } {12 123 scan 3 like 0}
735 SELECT a FROM t10 WHERE a LIKE '12%' ORDER BY +a;
737 } {12 123 scan 5 like 6}
741 a INTEGER PRIMARY KEY,
748 INSERT INTO t10b SELECT * FROM t10;
751 SELECT a FROM t10b WHERE b GLOB '12*' ORDER BY +a;
753 } {12 123 scan 5 like 6}
756 SELECT a FROM t10b WHERE c GLOB '12*' ORDER BY +a;
758 } {12 123 scan 5 like 6}
761 SELECT a FROM t10b WHERE d GLOB '12*' ORDER BY +a;
763 } {12 123 scan 5 like 6}
766 SELECT a FROM t10b WHERE e GLOB '12*' ORDER BY +a;
768 } {12 123 scan 5 like 6}
771 SELECT a FROM t10b WHERE f GLOB '12*' ORDER BY +a;
773 } {12 123 scan 3 like 0}
776 SELECT a FROM t10b WHERE a GLOB '12*' ORDER BY +a;
778 } {12 123 scan 5 like 6}
781 # LIKE and GLOB where the default collating sequence is not appropriate
782 # but an index with the appropriate collating sequence exists.
787 a INTEGER PRIMARY KEY,
788 b TEXT COLLATE nocase,
789 c TEXT COLLATE binary
791 INSERT INTO t11 VALUES(1, 'a','a');
792 INSERT INTO t11 VALUES(2, 'ab','ab');
793 INSERT INTO t11 VALUES(3, 'abc','abc');
794 INSERT INTO t11 VALUES(4, 'abcd','abcd');
795 INSERT INTO t11 VALUES(5, 'A','A');
796 INSERT INTO t11 VALUES(6, 'AB','AB');
797 INSERT INTO t11 VALUES(7, 'ABC','ABC');
798 INSERT INTO t11 VALUES(8, 'ABCD','ABCD');
799 INSERT INTO t11 VALUES(9, 'x','x');
800 INSERT INTO t11 VALUES(10, 'yz','yz');
801 INSERT INTO t11 VALUES(11, 'X','X');
802 INSERT INTO t11 VALUES(12, 'YZ','YZ');
803 SELECT count(*) FROM t11;
808 PRAGMA case_sensitive_like=OFF;
809 SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY a;
811 } {abc abcd ABC ABCD nosort t11 *}
814 PRAGMA case_sensitive_like=ON;
815 SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY a;
817 } {abc abcd nosort t11 *}
820 PRAGMA case_sensitive_like=OFF;
821 CREATE INDEX t11b ON t11(b);
822 SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY +a;
824 } {abc abcd ABC ABCD sort {} t11b}
827 PRAGMA case_sensitive_like=ON;
828 SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY a;
830 } {abc abcd nosort t11 *}
833 PRAGMA case_sensitive_like=OFF;
835 CREATE INDEX t11bnc ON t11(b COLLATE nocase);
836 SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY +a;
838 } {abc abcd ABC ABCD sort {} t11bnc}
841 CREATE INDEX t11bb ON t11(b COLLATE binary);
842 SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY +a;
844 } {abc abcd ABC ABCD sort {} t11bnc}
847 PRAGMA case_sensitive_like=ON;
848 SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY +a;
850 } {abc abcd sort {} t11bb}
853 PRAGMA case_sensitive_like=OFF;
854 SELECT b FROM t11 WHERE b GLOB 'abc*' ORDER BY +a;
856 } {abc abcd sort {} t11bb}
859 CREATE INDEX t11cnc ON t11(c COLLATE nocase);
860 CREATE INDEX t11cb ON t11(c COLLATE binary);
861 SELECT c FROM t11 WHERE c LIKE 'abc%' ORDER BY +a;
863 } {abc abcd ABC ABCD sort {} t11cnc}
866 SELECT c FROM t11 WHERE c GLOB 'abc*' ORDER BY +a;
868 } {abc abcd sort {} t11cb}