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 #***********************************************************************
12 # This file implements regression tests for SQLite library. This file
13 # implements tests for range and LIKE constraints that use bound variables
14 # instead of literal constant arguments.
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set testprefix analyze3
26 # This test cannot be run with the sqlite3_prepare() permutation, as it
27 # tests that stat4 data can be used to influence the plans of queries
28 # based on bound variable values. And this is not possible when using
29 # sqlite3_prepare() - as queries cannot be internally re-prepared after
30 # binding values are available.
31 if {[permutation]=="prepare"} {
36 #----------------------------------------------------------------------
39 # analyze3-1.*: Test that the values of bound parameters are considered
40 # in the same way as constants when planning queries that
41 # use range constraints.
43 # analyze3-2.*: Test that the values of bound parameters are considered
44 # in the same way as constants when planning queries that
45 # use LIKE expressions in the WHERE clause.
47 # analyze3-3.*: Test that binding to a variable does not invalidate the
48 # query plan when there is no way in which replanning the
49 # query may produce a superior outcome.
51 # analyze3-4.*: Test that SQL or authorization callback errors occuring
52 # within sqlite3Reprepare() are handled correctly.
54 # analyze3-5.*: Check that the query plans of applicable statements are
55 # invalidated if the values of SQL parameter are modified
56 # using the clear_bindings() or transfer_bindings() APIs.
58 # analyze3-6.*: Test that the problem fixed by commit [127a5b776d] is fixed.
60 # analyze3-7.*: Test that some memory leaks discovered by fuzz testing
64 proc getvar {varname} { uplevel #0 set $varname }
65 db function var getvar
67 proc eqp {sql {db db}} {
68 uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
71 proc sf_execsql {sql {db db}} {
72 set ::sqlite_search_count 0
73 set r [uplevel [list execsql $sql $db]]
75 concat $::sqlite_search_count [$db status step] $r
78 #-------------------------------------------------------------------------
81 # Create a table with two columns. Populate the first column (affinity
82 # INTEGER) with integer values from 100 to 1100. Create an index on this
83 # column. ANALYZE the table.
85 # analyze3-1.1.2 - 3.1.3
86 # Show that there are two possible plans for querying the table with
87 # a range constraint on the indexed column - "full table scan" or "use
88 # the index". When the range is specified using literal values, SQLite
89 # is able to pick the best plan based on the samples in sqlite_stat3.
91 # analyze3-1.1.4 - 3.1.9
92 # Show that using SQL variables produces the same results as using
93 # literal values to constrain the range scan.
95 # These tests also check that the compiler code considers column
96 # affinities when estimating the number of rows scanned by the "use
99 do_test analyze3-1.1.1 {
102 CREATE TABLE t1(x INTEGER, y);
103 CREATE INDEX i1 ON t1(x);
105 for {set i 0} {$i < 1000} {incr i} {
106 execsql { INSERT INTO t1 VALUES($i+100, $i) }
113 execsql { SELECT count(*)>0 FROM sqlite_stat4; }
116 do_execsql_test analyze3-1.1.x {
117 SELECT count(*) FROM t1 WHERE x>200 AND x<300;
118 SELECT count(*) FROM t1 WHERE x>0 AND x<1100;
121 # The first of the following two SELECT statements visits 99 rows. So
122 # it is better to use the index. But the second visits every row in
123 # the table (1000 in total) so it is better to do a full-table scan.
125 do_eqp_test analyze3-1.1.2 {
126 SELECT sum(y) FROM t1 WHERE x>200 AND x<300
127 } {SEARCH t1 USING INDEX i1 (x>? AND x<?)}
128 do_eqp_test analyze3-1.1.3 {
129 SELECT sum(y) FROM t1 WHERE x>0 AND x<1100
132 # 2017-06-26: Verify that the SQLITE_DBCONFIG_ENABLE_QPSG setting disables
133 # the use of bound parameters by STAT4
138 do_eqp_test analyze3-1.1.3.100 {
139 SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
140 } {SEARCH t1 USING INDEX i1 (x>? AND x<?)}
143 do_eqp_test analyze3-1.1.3.101 {
144 SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
145 } {SEARCH t1 USING INDEX i1 (x>? AND x<?)}
148 do_eqp_test analyze3-1.1.3.102 {
149 SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
152 sqlite3_db_config db ENABLE_QPSG 1
153 do_eqp_test analyze3-1.1.3.103 {
154 SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
155 } {SEARCH t1 USING INDEX i1 (x>? AND x<?)}
157 sqlite3_db_config db ENABLE_QPSG 0
158 do_eqp_test analyze3-1.1.3.104 {
159 SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
162 do_test analyze3-1.1.4 {
163 sf_execsql { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 }
165 do_test analyze3-1.1.5 {
166 set l [string range "200" 0 end]
167 set u [string range "300" 0 end]
168 sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
170 do_test analyze3-1.1.6 {
171 set l [expr int(200)]
172 set u [expr int(300)]
173 sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
175 do_test analyze3-1.1.7 {
176 sf_execsql { SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 }
178 do_test analyze3-1.1.8 {
179 set l [string range "0" 0 end]
180 set u [string range "1100" 0 end]
181 sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
183 do_test analyze3-1.1.9 {
185 set u [expr int(1100)]
186 sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
190 # The following tests are similar to the block above. The difference is
191 # that the indexed column has TEXT affinity in this case. In the tests
192 # above the affinity is INTEGER.
194 do_test analyze3-1.2.1 {
197 CREATE TABLE t2(x TEXT, y);
198 INSERT INTO t2 SELECT * FROM t1;
199 CREATE INDEX i2 ON t2(x);
204 do_execsql_test analyze3-2.1.x {
205 SELECT count(*) FROM t2 WHERE x>1 AND x<2;
206 SELECT count(*) FROM t2 WHERE x>0 AND x<99;
208 do_eqp_test analyze3-1.2.2 {
209 SELECT sum(y) FROM t2 WHERE x>1 AND x<2
210 } {SEARCH t2 USING INDEX i2 (x>? AND x<?)}
211 do_eqp_test analyze3-1.2.3 {
212 SELECT sum(y) FROM t2 WHERE x>0 AND x<99
215 do_test analyze3-1.2.4 {
216 sf_execsql { SELECT sum(y) FROM t2 WHERE x>12 AND x<20 }
218 do_test analyze3-1.2.5 {
219 set l [string range "12" 0 end]
220 set u [string range "20" 0 end]
221 sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
222 } {161 0 text text 4760}
223 do_test analyze3-1.2.6 {
226 sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
227 } {161 0 integer integer 4760}
228 do_test analyze3-1.2.7 {
229 sf_execsql { SELECT sum(y) FROM t2 WHERE x>0 AND x<99 }
231 do_test analyze3-1.2.8 {
232 set l [string range "0" 0 end]
233 set u [string range "99" 0 end]
234 sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
235 } {999 999 text text 490555}
236 do_test analyze3-1.2.9 {
239 sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
240 } {999 999 integer integer 490555}
242 # Same tests a third time. This time, column x has INTEGER affinity and
243 # is not the leftmost column of the table. This triggered a bug causing
244 # SQLite to use sub-optimal query plans in 3.6.18 and earlier.
246 do_test analyze3-1.3.1 {
249 CREATE TABLE t3(y TEXT, x INTEGER);
250 INSERT INTO t3 SELECT y, x FROM t1;
251 CREATE INDEX i3 ON t3(x);
256 do_execsql_test analyze3-1.3.x {
257 SELECT count(*) FROM t3 WHERE x>200 AND x<300;
258 SELECT count(*) FROM t3 WHERE x>0 AND x<1100
260 do_eqp_test analyze3-1.3.2 {
261 SELECT sum(y) FROM t3 WHERE x>200 AND x<300
262 } {SEARCH t3 USING INDEX i3 (x>? AND x<?)}
263 do_eqp_test analyze3-1.3.3 {
264 SELECT sum(y) FROM t3 WHERE x>0 AND x<1100
267 do_test analyze3-1.3.4 {
268 sf_execsql { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 }
270 do_test analyze3-1.3.5 {
271 set l [string range "200" 0 end]
272 set u [string range "300" 0 end]
273 sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
275 do_test analyze3-1.3.6 {
276 set l [expr int(200)]
277 set u [expr int(300)]
278 sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
280 do_test analyze3-1.3.7 {
281 sf_execsql { SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 }
283 do_test analyze3-1.3.8 {
284 set l [string range "0" 0 end]
285 set u [string range "1100" 0 end]
286 sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
288 do_test analyze3-1.3.9 {
290 set u [expr int(1100)]
291 sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
294 #-------------------------------------------------------------------------
295 # Test that the values of bound SQL variables may be used for the LIKE
299 do_test analyze3-2.1 {
301 PRAGMA case_sensitive_like=off;
303 CREATE TABLE t1(a, b TEXT COLLATE nocase);
304 CREATE INDEX i1 ON t1(b);
306 for {set i 0} {$i < 1000} {incr i} {
308 append t [lindex {a b c d e f g h i j} [expr $i/100]]
309 append t [lindex {a b c d e f g h i j} [expr ($i/10)%10]]
310 append t [lindex {a b c d e f g h i j} [expr ($i%10)]]
311 execsql { INSERT INTO t1 VALUES($i, $t) }
315 do_eqp_test analyze3-2.2 {
316 SELECT count(a) FROM t1 WHERE b LIKE 'a%'
317 } {SEARCH t1 USING INDEX i1 (b>? AND b<?)}
318 do_eqp_test analyze3-2.3 {
319 SELECT count(a) FROM t1 WHERE b LIKE '%a'
322 # Return the first argument if like_match_blobs is true (the default)
323 # or the second argument if not
326 ifcapable like_match_blobs {return $a}
330 do_test analyze3-2.4 {
331 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE 'a%' }
332 } [list [ilmb 102 101] 0 100]
333 do_test analyze3-2.5 {
334 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE '%a' }
337 do_test analyze3-2.6 {
339 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
340 } [list [ilmb 102 101] 0 100]
341 do_test analyze3-2.7 {
343 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
345 do_test analyze3-2.8 {
347 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
348 } [list [ilmb 102 101] 0 0]
349 do_test analyze3-2.9 {
351 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
352 } [list [ilmb 12 11] 0 0]
353 do_test analyze3-2.10 {
355 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
356 } [list [ilmb 3 2] 0 1]
357 do_test analyze3-2.11 {
359 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
360 } [list [ilmb 102 101] 0 10]
363 #-------------------------------------------------------------------------
364 # This block of tests checks that statements are correctly marked as
365 # expired when the values bound to any parameters that may affect the
366 # query plan are modified.
375 do_test analyze3-3.1 {
378 CREATE TABLE t1(a, b, c);
379 CREATE INDEX i1 ON t1(b);
381 for {set i 0} {$i < 100} {incr i} {
382 execsql { INSERT INTO t1 VALUES($i, $i, $i) }
387 do_test analyze3-3.2.1 {
388 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b>?" -1 dummy]
391 do_test analyze3-3.2.2 {
392 sqlite3_bind_text $S 1 "abc" 3
395 do_test analyze3-3.2.4 {
399 do_test analyze3-3.2.5 {
400 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b=?" -1 dummy]
403 do_test analyze3-3.2.6 {
404 sqlite3_bind_text $S 1 "abc" 3
407 do_test analyze3-3.2.7 {
411 do_test analyze3-3.4.1 {
412 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
415 do_test analyze3-3.4.2 {
416 sqlite3_bind_text $S 1 "abc" 3
419 do_test analyze3-3.4.3 {
420 sqlite3_bind_text $S 2 "def" 3
423 do_test analyze3-3.4.4 {
424 sqlite3_bind_text $S 2 "ghi" 3
427 do_test analyze3-3.4.5 {
430 do_test analyze3-3.4.6 {
434 do_test analyze3-3.5.1 {
435 set S [sqlite3_prepare_v2 db {
436 SELECT * FROM t1 WHERE a IN (
437 ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
438 ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
439 ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31
444 do_test analyze3-3.5.2 {
445 sqlite3_bind_text $S 31 "abc" 3
448 do_test analyze3-3.5.3 {
449 sqlite3_bind_text $S 32 "def" 3
452 do_test analyze3-3.5.5 {
456 do_test analyze3-3.6.1 {
457 set S [sqlite3_prepare_v2 db {
458 SELECT * FROM t1 WHERE a IN (
459 ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
460 ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
461 ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
466 do_test analyze3-3.6.2 {
467 sqlite3_bind_text $S 32 "abc" 3
470 do_test analyze3-3.6.3 {
471 sqlite3_bind_text $S 33 "def" 3
474 do_test analyze3-3.6.5 {
478 do_test analyze3-3.7.1 {
479 set S [sqlite3_prepare_v2 db {
480 SELECT * FROM t1 WHERE a IN (
481 ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?33,
482 ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
483 ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
488 do_test analyze3-3.7.2 {
489 sqlite3_bind_text $S 32 "abc" 3
492 do_test analyze3-3.7.3 {
493 sqlite3_bind_text $S 33 "def" 3
496 do_test analyze3-3.7.4 {
497 sqlite3_bind_text $S 10 "def" 3
500 do_test analyze3-3.7.6 {
504 do_test analyze3-3.8.1 {
506 CREATE TABLE t4(x, y TEXT COLLATE NOCASE);
507 CREATE INDEX i4 ON t4(y);
510 do_test analyze3-3.8.2 {
511 set S [sqlite3_prepare_v2 db {
512 SELECT * FROM t4 WHERE x != ? AND y LIKE ?
516 do_test analyze3-3.8.3 {
517 sqlite3_bind_text $S 1 "abc" 3
520 do_test analyze3-3.8.4 {
521 sqlite3_bind_text $S 2 "def" 3
524 do_test analyze3-3.8.7 {
525 sqlite3_bind_text $S 2 "ghi%" 4
528 do_test analyze3-3.8.8 {
531 do_test analyze3-3.8.9 {
532 sqlite3_bind_text $S 2 "ghi%def" 7
535 do_test analyze3-3.8.10 {
538 do_test analyze3-3.8.11 {
539 sqlite3_bind_text $S 2 "%ab" 3
542 do_test analyze3-3.8.12 {
545 do_test analyze3-3.8.12 {
546 sqlite3_bind_text $S 2 "%de" 3
549 do_test analyze3-3.8.13 {
552 do_test analyze3-3.8.14 {
556 #-------------------------------------------------------------------------
557 # These tests check that errors encountered while repreparing an SQL
558 # statement within sqlite3Reprepare() are handled correctly.
561 # Check a schema error.
563 do_test analyze3-4.1.1 {
564 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
567 do_test analyze3-4.1.2 {
569 sqlite3_bind_text $S 2 "abc" 3
570 execsql { DROP TABLE t1 }
573 do_test analyze3-4.1.3 {
577 # Check an authorization error.
579 do_test analyze3-4.2.1 {
582 CREATE TABLE t1(a, b, c);
583 CREATE INDEX i1 ON t1(b);
585 for {set i 0} {$i < 100} {incr i} {
586 execsql { INSERT INTO t1 VALUES($i, $i, $i) }
590 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
595 if {[lindex $args 0] == "SQLITE_READ"} {return SQLITE_DENY}
598 do_test analyze3-4.2.2 {
600 sqlite3_bind_text $S 2 "abc" 3
603 do_test analyze3-4.2.4 {
607 # Check the effect of an authorization error that occurs in a re-prepare
608 # performed by sqlite3_step() is the same as one that occurs within
609 # sqlite3Reprepare().
611 do_test analyze3-4.3.1 {
613 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
614 execsql { CREATE TABLE t2(d, e, f) }
618 do_test analyze3-4.3.2 {
623 #-------------------------------------------------------------------------
624 # Test that modifying bound variables using the clear_bindings() or
625 # transfer_bindings() APIs works.
627 # analyze3-5.1.*: sqlite3_clear_bindings()
628 # analyze3-5.2.*: sqlite3_transfer_bindings()
630 do_test analyze3-5.1.1 {
633 CREATE TABLE t1(x TEXT COLLATE NOCASE);
634 CREATE INDEX i1 ON t1(x);
635 INSERT INTO t1 VALUES('aaa');
636 INSERT INTO t1 VALUES('abb');
637 INSERT INTO t1 VALUES('acc');
638 INSERT INTO t1 VALUES('baa');
639 INSERT INTO t1 VALUES('bbb');
640 INSERT INTO t1 VALUES('bcc');
643 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
644 sqlite3_bind_text $S 1 "a%" 2
646 while { "SQLITE_ROW" == [sqlite3_step $S] } {
647 lappend R [sqlite3_column_text $S 0]
649 concat [sqlite3_reset $S] $R
650 } {SQLITE_OK aaa abb acc}
651 do_test analyze3-5.1.2 {
652 sqlite3_clear_bindings $S
654 while { "SQLITE_ROW" == [sqlite3_step $S] } {
655 lappend R [sqlite3_column_text $S 0]
657 concat [sqlite3_reset $S] $R
659 do_test analyze3-5.1.3 {
663 do_test analyze3-5.1.1 {
664 set S1 [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
665 sqlite3_bind_text $S1 1 "b%" 2
667 while { "SQLITE_ROW" == [sqlite3_step $S1] } {
668 lappend R [sqlite3_column_text $S1 0]
670 concat [sqlite3_reset $S1] $R
671 } {SQLITE_OK baa bbb bcc}
673 do_test analyze3-5.1.2 {
674 set S2 [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x = ?" -1 dummy]
675 sqlite3_bind_text $S2 1 "a%" 2
676 sqlite3_transfer_bindings $S2 $S1
678 while { "SQLITE_ROW" == [sqlite3_step $S1] } {
679 lappend R [sqlite3_column_text $S1 0]
681 concat [sqlite3_reset $S1] $R
682 } {SQLITE_OK aaa abb acc}
683 do_test analyze3-5.1.3 {
688 #-------------------------------------------------------------------------
690 do_test analyze3-6.1 {
691 execsql { DROP TABLE IF EXISTS t1 }
693 execsql { CREATE TABLE t1(a, b, c) }
694 for {set i 0} {$i < 1000} {incr i} {
695 execsql "INSERT INTO t1 VALUES([expr $i/100], 'x', [expr $i/10])"
698 CREATE INDEX i1 ON t1(a, b);
699 CREATE INDEX i2 ON t1(c);
705 do_eqp_test analyze3-6-3 {
706 SELECT * FROM t1 WHERE a = 5 AND c = 13;
707 } {SEARCH t1 USING INDEX i2 (c=?)}
709 do_eqp_test analyze3-6-2 {
710 SELECT * FROM t1 WHERE a = 5 AND b > 'w' AND c = 13;
711 } {SEARCH t1 USING INDEX i2 (c=?)}
713 #-----------------------------------------------------------------------------
715 # Memory leak in sqlite3Stat4ProbeFree(). (Discovered while fuzzing.)
717 do_execsql_test analyze-7.1 {
718 DROP TABLE IF EXISTS t1;
719 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
720 INSERT INTO t1 VALUES(1,1,'0000');
721 CREATE INDEX t0b ON t1(b);
723 SELECT c FROM t1 WHERE b=3 AND a BETWEEN 30 AND hex(1);
726 # At one point duplicate stat1 entries were causing a memory leak.
729 do_execsql_test 7.2 {
730 CREATE TABLE t1(a,b,c);
731 CREATE INDEX t1a ON t1(a);
733 SELECT * FROM sqlite_stat1;
734 INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t1','t1a','12000');
735 INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t1','t1a','12000');
736 ANALYZE sqlite_master;