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
21 ifcapable !stat4&&!stat3 {
26 #----------------------------------------------------------------------
29 # analyze3-1.*: Test that the values of bound parameters are considered
30 # in the same way as constants when planning queries that
31 # use range constraints.
33 # analyze3-2.*: Test that the values of bound parameters are considered
34 # in the same way as constants when planning queries that
35 # use LIKE expressions in the WHERE clause.
37 # analyze3-3.*: Test that binding to a variable does not invalidate the
38 # query plan when there is no way in which replanning the
39 # query may produce a superior outcome.
41 # analyze3-4.*: Test that SQL or authorization callback errors occuring
42 # within sqlite3Reprepare() are handled correctly.
44 # analyze3-5.*: Check that the query plans of applicable statements are
45 # invalidated if the values of SQL parameter are modified
46 # using the clear_bindings() or transfer_bindings() APIs.
48 # analyze3-6.*: Test that the problem fixed by commit [127a5b776d] is fixed.
50 # analyze3-7.*: Test that some memory leaks discovered by fuzz testing
54 proc getvar {varname} { uplevel #0 set $varname }
55 db function var getvar
57 proc eqp {sql {db db}} {
58 uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
61 proc sf_execsql {sql {db db}} {
62 set ::sqlite_search_count 0
63 set r [uplevel [list execsql $sql $db]]
65 concat $::sqlite_search_count [$db status step] $r
68 #-------------------------------------------------------------------------
71 # Create a table with two columns. Populate the first column (affinity
72 # INTEGER) with integer values from 100 to 1100. Create an index on this
73 # column. ANALYZE the table.
75 # analyze3-1.1.2 - 3.1.3
76 # Show that there are two possible plans for querying the table with
77 # a range constraint on the indexed column - "full table scan" or "use
78 # the index". When the range is specified using literal values, SQLite
79 # is able to pick the best plan based on the samples in sqlite_stat3.
81 # analyze3-1.1.4 - 3.1.9
82 # Show that using SQL variables produces the same results as using
83 # literal values to constrain the range scan.
85 # These tests also check that the compiler code considers column
86 # affinities when estimating the number of rows scanned by the "use
89 do_test analyze3-1.1.1 {
92 CREATE TABLE t1(x INTEGER, y);
93 CREATE INDEX i1 ON t1(x);
95 for {set i 0} {$i < 1000} {incr i} {
96 execsql { INSERT INTO t1 VALUES($i+100, $i) }
104 execsql { SELECT count(*)>0 FROM sqlite_stat4; }
106 execsql { SELECT count(*)>0 FROM sqlite_stat3; }
110 do_execsql_test analyze3-1.1.x {
111 SELECT count(*) FROM t1 WHERE x>200 AND x<300;
112 SELECT count(*) FROM t1 WHERE x>0 AND x<1100;
115 # The first of the following two SELECT statements visits 99 rows. So
116 # it is better to use the index. But the second visits every row in
117 # the table (1000 in total) so it is better to do a full-table scan.
119 do_eqp_test analyze3-1.1.2 {
120 SELECT sum(y) FROM t1 WHERE x>200 AND x<300
121 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?)}}
122 do_eqp_test analyze3-1.1.3 {
123 SELECT sum(y) FROM t1 WHERE x>0 AND x<1100
124 } {0 0 0 {SCAN TABLE t1}}
126 # 2017-06-26: Verify that the SQLITE_DBCONFIG_ENABLE_QPSG setting disables
127 # the use of bound parameters by STAT4
132 do_eqp_test analyze3-1.1.3.100 {
133 SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
134 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?)}}
137 do_eqp_test analyze3-1.1.3.101 {
138 SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
139 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?)}}
142 do_eqp_test analyze3-1.1.3.102 {
143 SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
144 } {0 0 0 {SCAN TABLE t1}}
146 sqlite3_db_config db ENABLE_QPSG 1
147 do_eqp_test analyze3-1.1.3.103 {
148 SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
149 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?)}}
151 sqlite3_db_config db ENABLE_QPSG 0
152 do_eqp_test analyze3-1.1.3.104 {
153 SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
154 } {0 0 0 {SCAN TABLE t1}}
156 do_test analyze3-1.1.4 {
157 sf_execsql { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 }
159 do_test analyze3-1.1.5 {
160 set l [string range "200" 0 end]
161 set u [string range "300" 0 end]
162 sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
164 do_test analyze3-1.1.6 {
165 set l [expr int(200)]
166 set u [expr int(300)]
167 sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
169 do_test analyze3-1.1.7 {
170 sf_execsql { SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 }
172 do_test analyze3-1.1.8 {
173 set l [string range "0" 0 end]
174 set u [string range "1100" 0 end]
175 sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
177 do_test analyze3-1.1.9 {
179 set u [expr int(1100)]
180 sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
184 # The following tests are similar to the block above. The difference is
185 # that the indexed column has TEXT affinity in this case. In the tests
186 # above the affinity is INTEGER.
188 do_test analyze3-1.2.1 {
191 CREATE TABLE t2(x TEXT, y);
192 INSERT INTO t2 SELECT * FROM t1;
193 CREATE INDEX i2 ON t2(x);
198 do_execsql_test analyze3-2.1.x {
199 SELECT count(*) FROM t2 WHERE x>1 AND x<2;
200 SELECT count(*) FROM t2 WHERE x>0 AND x<99;
202 do_eqp_test analyze3-1.2.2 {
203 SELECT sum(y) FROM t2 WHERE x>1 AND x<2
204 } {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?)}}
205 do_eqp_test analyze3-1.2.3 {
206 SELECT sum(y) FROM t2 WHERE x>0 AND x<99
207 } {0 0 0 {SCAN TABLE t2}}
209 do_test analyze3-1.2.4 {
210 sf_execsql { SELECT sum(y) FROM t2 WHERE x>12 AND x<20 }
212 do_test analyze3-1.2.5 {
213 set l [string range "12" 0 end]
214 set u [string range "20" 0 end]
215 sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
216 } {161 0 text text 4760}
217 do_test analyze3-1.2.6 {
220 sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
221 } {161 0 integer integer 4760}
222 do_test analyze3-1.2.7 {
223 sf_execsql { SELECT sum(y) FROM t2 WHERE x>0 AND x<99 }
225 do_test analyze3-1.2.8 {
226 set l [string range "0" 0 end]
227 set u [string range "99" 0 end]
228 sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
229 } {999 999 text text 490555}
230 do_test analyze3-1.2.9 {
233 sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
234 } {999 999 integer integer 490555}
236 # Same tests a third time. This time, column x has INTEGER affinity and
237 # is not the leftmost column of the table. This triggered a bug causing
238 # SQLite to use sub-optimal query plans in 3.6.18 and earlier.
240 do_test analyze3-1.3.1 {
243 CREATE TABLE t3(y TEXT, x INTEGER);
244 INSERT INTO t3 SELECT y, x FROM t1;
245 CREATE INDEX i3 ON t3(x);
250 do_execsql_test analyze3-1.3.x {
251 SELECT count(*) FROM t3 WHERE x>200 AND x<300;
252 SELECT count(*) FROM t3 WHERE x>0 AND x<1100
254 do_eqp_test analyze3-1.3.2 {
255 SELECT sum(y) FROM t3 WHERE x>200 AND x<300
256 } {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?)}}
257 do_eqp_test analyze3-1.3.3 {
258 SELECT sum(y) FROM t3 WHERE x>0 AND x<1100
259 } {0 0 0 {SCAN TABLE t3}}
261 do_test analyze3-1.3.4 {
262 sf_execsql { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 }
264 do_test analyze3-1.3.5 {
265 set l [string range "200" 0 end]
266 set u [string range "300" 0 end]
267 sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
269 do_test analyze3-1.3.6 {
270 set l [expr int(200)]
271 set u [expr int(300)]
272 sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
274 do_test analyze3-1.3.7 {
275 sf_execsql { SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 }
277 do_test analyze3-1.3.8 {
278 set l [string range "0" 0 end]
279 set u [string range "1100" 0 end]
280 sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
282 do_test analyze3-1.3.9 {
284 set u [expr int(1100)]
285 sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
288 #-------------------------------------------------------------------------
289 # Test that the values of bound SQL variables may be used for the LIKE
293 do_test analyze3-2.1 {
295 PRAGMA case_sensitive_like=off;
297 CREATE TABLE t1(a, b TEXT COLLATE nocase);
298 CREATE INDEX i1 ON t1(b);
300 for {set i 0} {$i < 1000} {incr i} {
302 append t [lindex {a b c d e f g h i j} [expr $i/100]]
303 append t [lindex {a b c d e f g h i j} [expr ($i/10)%10]]
304 append t [lindex {a b c d e f g h i j} [expr ($i%10)]]
305 execsql { INSERT INTO t1 VALUES($i, $t) }
309 do_eqp_test analyze3-2.2 {
310 SELECT count(a) FROM t1 WHERE b LIKE 'a%'
311 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (b>? AND b<?)}}
312 do_eqp_test analyze3-2.3 {
313 SELECT count(a) FROM t1 WHERE b LIKE '%a'
314 } {0 0 0 {SCAN TABLE t1}}
316 # Return the first argument if like_match_blobs is true (the default)
317 # or the second argument if not
320 ifcapable like_match_blobs {return $a}
324 do_test analyze3-2.4 {
325 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE 'a%' }
326 } [list [ilmb 102 101] 0 100]
327 do_test analyze3-2.5 {
328 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE '%a' }
331 do_test analyze3-2.6 {
333 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
334 } [list [ilmb 102 101] 0 100]
335 do_test analyze3-2.7 {
337 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
339 do_test analyze3-2.8 {
341 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
342 } [list [ilmb 102 101] 0 0]
343 do_test analyze3-2.9 {
345 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
346 } [list [ilmb 12 11] 0 0]
347 do_test analyze3-2.10 {
349 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
350 } [list [ilmb 3 2] 0 1]
351 do_test analyze3-2.11 {
353 sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
354 } [list [ilmb 102 101] 0 10]
357 #-------------------------------------------------------------------------
358 # This block of tests checks that statements are correctly marked as
359 # expired when the values bound to any parameters that may affect the
360 # query plan are modified.
369 do_test analyze3-3.1 {
372 CREATE TABLE t1(a, b, c);
373 CREATE INDEX i1 ON t1(b);
375 for {set i 0} {$i < 100} {incr i} {
376 execsql { INSERT INTO t1 VALUES($i, $i, $i) }
381 do_test analyze3-3.2.1 {
382 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b>?" -1 dummy]
385 do_test analyze3-3.2.2 {
386 sqlite3_bind_text $S 1 "abc" 3
389 do_test analyze3-3.2.4 {
393 do_test analyze3-3.2.5 {
394 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b=?" -1 dummy]
397 do_test analyze3-3.2.6 {
398 sqlite3_bind_text $S 1 "abc" 3
401 do_test analyze3-3.2.7 {
405 do_test analyze3-3.4.1 {
406 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
409 do_test analyze3-3.4.2 {
410 sqlite3_bind_text $S 1 "abc" 3
413 do_test analyze3-3.4.3 {
414 sqlite3_bind_text $S 2 "def" 3
417 do_test analyze3-3.4.4 {
418 sqlite3_bind_text $S 2 "ghi" 3
421 do_test analyze3-3.4.5 {
424 do_test analyze3-3.4.6 {
428 do_test analyze3-3.5.1 {
429 set S [sqlite3_prepare_v2 db {
430 SELECT * FROM t1 WHERE a IN (
431 ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
432 ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
433 ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31
438 do_test analyze3-3.5.2 {
439 sqlite3_bind_text $S 31 "abc" 3
442 do_test analyze3-3.5.3 {
443 sqlite3_bind_text $S 32 "def" 3
446 do_test analyze3-3.5.5 {
450 do_test analyze3-3.6.1 {
451 set S [sqlite3_prepare_v2 db {
452 SELECT * FROM t1 WHERE a IN (
453 ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
454 ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
455 ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
460 do_test analyze3-3.6.2 {
461 sqlite3_bind_text $S 32 "abc" 3
464 do_test analyze3-3.6.3 {
465 sqlite3_bind_text $S 33 "def" 3
468 do_test analyze3-3.6.5 {
472 do_test analyze3-3.7.1 {
473 set S [sqlite3_prepare_v2 db {
474 SELECT * FROM t1 WHERE a IN (
475 ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?33,
476 ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
477 ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
482 do_test analyze3-3.7.2 {
483 sqlite3_bind_text $S 32 "abc" 3
486 do_test analyze3-3.7.3 {
487 sqlite3_bind_text $S 33 "def" 3
490 do_test analyze3-3.7.4 {
491 sqlite3_bind_text $S 10 "def" 3
494 do_test analyze3-3.7.6 {
498 do_test analyze3-3.8.1 {
500 CREATE TABLE t4(x, y TEXT COLLATE NOCASE);
501 CREATE INDEX i4 ON t4(y);
504 do_test analyze3-3.8.2 {
505 set S [sqlite3_prepare_v2 db {
506 SELECT * FROM t4 WHERE x != ? AND y LIKE ?
510 do_test analyze3-3.8.3 {
511 sqlite3_bind_text $S 1 "abc" 3
514 do_test analyze3-3.8.4 {
515 sqlite3_bind_text $S 2 "def" 3
518 do_test analyze3-3.8.7 {
519 sqlite3_bind_text $S 2 "ghi%" 4
522 do_test analyze3-3.8.8 {
525 do_test analyze3-3.8.9 {
526 sqlite3_bind_text $S 2 "ghi%def" 7
529 do_test analyze3-3.8.10 {
532 do_test analyze3-3.8.11 {
533 sqlite3_bind_text $S 2 "%ab" 3
536 do_test analyze3-3.8.12 {
539 do_test analyze3-3.8.12 {
540 sqlite3_bind_text $S 2 "%de" 3
543 do_test analyze3-3.8.13 {
546 do_test analyze3-3.8.14 {
550 #-------------------------------------------------------------------------
551 # These tests check that errors encountered while repreparing an SQL
552 # statement within sqlite3Reprepare() are handled correctly.
555 # Check a schema error.
557 do_test analyze3-4.1.1 {
558 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
561 do_test analyze3-4.1.2 {
563 sqlite3_bind_text $S 2 "abc" 3
564 execsql { DROP TABLE t1 }
567 do_test analyze3-4.1.3 {
571 # Check an authorization error.
573 do_test analyze3-4.2.1 {
576 CREATE TABLE t1(a, b, c);
577 CREATE INDEX i1 ON t1(b);
579 for {set i 0} {$i < 100} {incr i} {
580 execsql { INSERT INTO t1 VALUES($i, $i, $i) }
584 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
589 if {[lindex $args 0] == "SQLITE_READ"} {return SQLITE_DENY}
592 do_test analyze3-4.2.2 {
594 sqlite3_bind_text $S 2 "abc" 3
597 do_test analyze3-4.2.4 {
601 # Check the effect of an authorization error that occurs in a re-prepare
602 # performed by sqlite3_step() is the same as one that occurs within
603 # sqlite3Reprepare().
605 do_test analyze3-4.3.1 {
607 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
608 execsql { CREATE TABLE t2(d, e, f) }
612 do_test analyze3-4.3.2 {
617 #-------------------------------------------------------------------------
618 # Test that modifying bound variables using the clear_bindings() or
619 # transfer_bindings() APIs works.
621 # analyze3-5.1.*: sqlite3_clear_bindings()
622 # analyze3-5.2.*: sqlite3_transfer_bindings()
624 do_test analyze3-5.1.1 {
627 CREATE TABLE t1(x TEXT COLLATE NOCASE);
628 CREATE INDEX i1 ON t1(x);
629 INSERT INTO t1 VALUES('aaa');
630 INSERT INTO t1 VALUES('abb');
631 INSERT INTO t1 VALUES('acc');
632 INSERT INTO t1 VALUES('baa');
633 INSERT INTO t1 VALUES('bbb');
634 INSERT INTO t1 VALUES('bcc');
637 set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
638 sqlite3_bind_text $S 1 "a%" 2
640 while { "SQLITE_ROW" == [sqlite3_step $S] } {
641 lappend R [sqlite3_column_text $S 0]
643 concat [sqlite3_reset $S] $R
644 } {SQLITE_OK aaa abb acc}
645 do_test analyze3-5.1.2 {
646 sqlite3_clear_bindings $S
648 while { "SQLITE_ROW" == [sqlite3_step $S] } {
649 lappend R [sqlite3_column_text $S 0]
651 concat [sqlite3_reset $S] $R
653 do_test analyze3-5.1.3 {
657 do_test analyze3-5.1.1 {
658 set S1 [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
659 sqlite3_bind_text $S1 1 "b%" 2
661 while { "SQLITE_ROW" == [sqlite3_step $S1] } {
662 lappend R [sqlite3_column_text $S1 0]
664 concat [sqlite3_reset $S1] $R
665 } {SQLITE_OK baa bbb bcc}
667 do_test analyze3-5.1.2 {
668 set S2 [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x = ?" -1 dummy]
669 sqlite3_bind_text $S2 1 "a%" 2
670 sqlite3_transfer_bindings $S2 $S1
672 while { "SQLITE_ROW" == [sqlite3_step $S1] } {
673 lappend R [sqlite3_column_text $S1 0]
675 concat [sqlite3_reset $S1] $R
676 } {SQLITE_OK aaa abb acc}
677 do_test analyze3-5.1.3 {
682 #-------------------------------------------------------------------------
684 do_test analyze3-6.1 {
685 execsql { DROP TABLE IF EXISTS t1 }
687 execsql { CREATE TABLE t1(a, b, c) }
688 for {set i 0} {$i < 1000} {incr i} {
689 execsql "INSERT INTO t1 VALUES([expr $i/100], 'x', [expr $i/10])"
692 CREATE INDEX i1 ON t1(a, b);
693 CREATE INDEX i2 ON t1(c);
699 do_eqp_test analyze3-6-3 {
700 SELECT * FROM t1 WHERE a = 5 AND c = 13;
701 } {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (c=?)}}
703 do_eqp_test analyze3-6-2 {
704 SELECT * FROM t1 WHERE a = 5 AND b > 'w' AND c = 13;
705 } {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (c=?)}}
707 #-----------------------------------------------------------------------------
709 # Memory leak in sqlite3Stat4ProbeFree(). (Discovered while fuzzing.)
711 do_execsql_test analyze-7.1 {
712 DROP TABLE IF EXISTS t1;
713 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
714 INSERT INTO t1 VALUES(1,1,'0000');
715 CREATE INDEX t0b ON t1(b);
717 SELECT c FROM t1 WHERE b=3 AND a BETWEEN 30 AND hex(1);
720 # At one point duplicate stat1 entries were causing a memory leak.
723 do_execsql_test 7.2 {
724 CREATE TABLE t1(a,b,c);
725 CREATE INDEX t1a ON t1(a);
727 SELECT * FROM sqlite_stat1;
728 INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t1','t1a','12000');
729 INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t1','t1a','12000');
730 ANALYZE sqlite_master;