1
0

analyze3.test 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. # 2009 August 06
  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. #
  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.
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. ifcapable !stat4&&!stat3 {
  19. finish_test
  20. return
  21. }
  22. #----------------------------------------------------------------------
  23. # Test Organization:
  24. #
  25. # analyze3-1.*: Test that the values of bound parameters are considered
  26. # in the same way as constants when planning queries that
  27. # use range constraints.
  28. #
  29. # analyze3-2.*: Test that the values of bound parameters are considered
  30. # in the same way as constants when planning queries that
  31. # use LIKE expressions in the WHERE clause.
  32. #
  33. # analyze3-3.*: Test that binding to a variable does not invalidate the
  34. # query plan when there is no way in which replanning the
  35. # query may produce a superior outcome.
  36. #
  37. # analyze3-4.*: Test that SQL or authorization callback errors occuring
  38. # within sqlite3Reprepare() are handled correctly.
  39. #
  40. # analyze3-5.*: Check that the query plans of applicable statements are
  41. # invalidated if the values of SQL parameter are modified
  42. # using the clear_bindings() or transfer_bindings() APIs.
  43. #
  44. # analyze3-6.*: Test that the problem fixed by commit [127a5b776d] is fixed.
  45. #
  46. proc getvar {varname} { uplevel #0 set $varname }
  47. db function var getvar
  48. proc eqp {sql {db db}} {
  49. uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
  50. }
  51. proc sf_execsql {sql {db db}} {
  52. set ::sqlite_search_count 0
  53. set r [uplevel [list execsql $sql $db]]
  54. concat $::sqlite_search_count [$db status step] $r
  55. }
  56. #-------------------------------------------------------------------------
  57. #
  58. # analyze3-1.1.1:
  59. # Create a table with two columns. Populate the first column (affinity
  60. # INTEGER) with integer values from 100 to 1100. Create an index on this
  61. # column. ANALYZE the table.
  62. #
  63. # analyze3-1.1.2 - 3.1.3
  64. # Show that there are two possible plans for querying the table with
  65. # a range constraint on the indexed column - "full table scan" or "use
  66. # the index". When the range is specified using literal values, SQLite
  67. # is able to pick the best plan based on the samples in sqlite_stat3.
  68. #
  69. # analyze3-1.1.4 - 3.1.9
  70. # Show that using SQL variables produces the same results as using
  71. # literal values to constrain the range scan.
  72. #
  73. # These tests also check that the compiler code considers column
  74. # affinities when estimating the number of rows scanned by the "use
  75. # index strategy".
  76. #
  77. do_test analyze3-1.1.1 {
  78. execsql {
  79. BEGIN;
  80. CREATE TABLE t1(x INTEGER, y);
  81. CREATE INDEX i1 ON t1(x);
  82. }
  83. for {set i 0} {$i < 1000} {incr i} {
  84. execsql { INSERT INTO t1 VALUES($i+100, $i) }
  85. }
  86. execsql {
  87. COMMIT;
  88. ANALYZE;
  89. }
  90. ifcapable stat4 {
  91. execsql { SELECT count(*)>0 FROM sqlite_stat4; }
  92. } else {
  93. execsql { SELECT count(*)>0 FROM sqlite_stat3; }
  94. }
  95. } {1}
  96. do_eqp_test analyze3-1.1.2 {
  97. SELECT sum(y) FROM t1 WHERE x>200 AND x<300
  98. } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?)}}
  99. do_eqp_test analyze3-1.1.3 {
  100. SELECT sum(y) FROM t1 WHERE x>0 AND x<1100
  101. } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?)}}
  102. do_test analyze3-1.1.4 {
  103. sf_execsql { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 }
  104. } {199 0 14850}
  105. do_test analyze3-1.1.5 {
  106. set l [string range "200" 0 end]
  107. set u [string range "300" 0 end]
  108. sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
  109. } {199 0 14850}
  110. do_test analyze3-1.1.6 {
  111. set l [expr int(200)]
  112. set u [expr int(300)]
  113. sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
  114. } {199 0 14850}
  115. do_test analyze3-1.1.7 {
  116. sf_execsql { SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 }
  117. } {2000 0 499500}
  118. do_test analyze3-1.1.8 {
  119. set l [string range "0" 0 end]
  120. set u [string range "1100" 0 end]
  121. sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
  122. } {2000 0 499500}
  123. do_test analyze3-1.1.9 {
  124. set l [expr int(0)]
  125. set u [expr int(1100)]
  126. sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
  127. } {2000 0 499500}
  128. # The following tests are similar to the block above. The difference is
  129. # that the indexed column has TEXT affinity in this case. In the tests
  130. # above the affinity is INTEGER.
  131. #
  132. do_test analyze3-1.2.1 {
  133. execsql {
  134. BEGIN;
  135. CREATE TABLE t2(x TEXT, y);
  136. INSERT INTO t2 SELECT * FROM t1;
  137. CREATE INDEX i2 ON t2(x);
  138. COMMIT;
  139. ANALYZE;
  140. }
  141. } {}
  142. do_eqp_test analyze3-1.2.2 {
  143. SELECT sum(y) FROM t2 WHERE x>1 AND x<2
  144. } {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?)}}
  145. do_eqp_test analyze3-1.2.3 {
  146. SELECT sum(y) FROM t2 WHERE x>0 AND x<99
  147. } {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?)}}
  148. do_test analyze3-1.2.4 {
  149. sf_execsql { SELECT sum(y) FROM t2 WHERE x>12 AND x<20 }
  150. } {161 0 4760}
  151. do_test analyze3-1.2.5 {
  152. set l [string range "12" 0 end]
  153. set u [string range "20" 0 end]
  154. sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
  155. } {161 0 text text 4760}
  156. do_test analyze3-1.2.6 {
  157. set l [expr int(12)]
  158. set u [expr int(20)]
  159. sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
  160. } {161 0 integer integer 4760}
  161. do_test analyze3-1.2.7 {
  162. sf_execsql { SELECT sum(y) FROM t2 WHERE x>0 AND x<99 }
  163. } {1981 0 490555}
  164. do_test analyze3-1.2.8 {
  165. set l [string range "0" 0 end]
  166. set u [string range "99" 0 end]
  167. sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
  168. } {1981 0 text text 490555}
  169. do_test analyze3-1.2.9 {
  170. set l [expr int(0)]
  171. set u [expr int(99)]
  172. sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
  173. } {1981 0 integer integer 490555}
  174. # Same tests a third time. This time, column x has INTEGER affinity and
  175. # is not the leftmost column of the table. This triggered a bug causing
  176. # SQLite to use sub-optimal query plans in 3.6.18 and earlier.
  177. #
  178. do_test analyze3-1.3.1 {
  179. execsql {
  180. BEGIN;
  181. CREATE TABLE t3(y TEXT, x INTEGER);
  182. INSERT INTO t3 SELECT y, x FROM t1;
  183. CREATE INDEX i3 ON t3(x);
  184. COMMIT;
  185. ANALYZE;
  186. }
  187. } {}
  188. do_eqp_test analyze3-1.3.2 {
  189. SELECT sum(y) FROM t3 WHERE x>200 AND x<300
  190. } {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?)}}
  191. do_eqp_test analyze3-1.3.3 {
  192. SELECT sum(y) FROM t3 WHERE x>0 AND x<1100
  193. } {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?)}}
  194. do_test analyze3-1.3.4 {
  195. sf_execsql { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 }
  196. } {199 0 14850}
  197. do_test analyze3-1.3.5 {
  198. set l [string range "200" 0 end]
  199. set u [string range "300" 0 end]
  200. sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
  201. } {199 0 14850}
  202. do_test analyze3-1.3.6 {
  203. set l [expr int(200)]
  204. set u [expr int(300)]
  205. sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
  206. } {199 0 14850}
  207. do_test analyze3-1.3.7 {
  208. sf_execsql { SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 }
  209. } {2000 0 499500}
  210. do_test analyze3-1.3.8 {
  211. set l [string range "0" 0 end]
  212. set u [string range "1100" 0 end]
  213. sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
  214. } {2000 0 499500}
  215. do_test analyze3-1.3.9 {
  216. set l [expr int(0)]
  217. set u [expr int(1100)]
  218. sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
  219. } {2000 0 499500}
  220. #-------------------------------------------------------------------------
  221. # Test that the values of bound SQL variables may be used for the LIKE
  222. # optimization.
  223. #
  224. drop_all_tables
  225. do_test analyze3-2.1 {
  226. execsql {
  227. PRAGMA case_sensitive_like=off;
  228. BEGIN;
  229. CREATE TABLE t1(a, b TEXT COLLATE nocase);
  230. CREATE INDEX i1 ON t1(b);
  231. }
  232. for {set i 0} {$i < 1000} {incr i} {
  233. set t ""
  234. append t [lindex {a b c d e f g h i j} [expr $i/100]]
  235. append t [lindex {a b c d e f g h i j} [expr ($i/10)%10]]
  236. append t [lindex {a b c d e f g h i j} [expr ($i%10)]]
  237. execsql { INSERT INTO t1 VALUES($i, $t) }
  238. }
  239. execsql COMMIT
  240. } {}
  241. do_eqp_test analyze3-2.2 {
  242. SELECT count(a) FROM t1 WHERE b LIKE 'a%'
  243. } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (b>? AND b<?)}}
  244. do_eqp_test analyze3-2.3 {
  245. SELECT count(a) FROM t1 WHERE b LIKE '%a'
  246. } {0 0 0 {SCAN TABLE t1}}
  247. do_test analyze3-2.4 {
  248. sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE 'a%' }
  249. } {101 0 100}
  250. do_test analyze3-2.5 {
  251. sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE '%a' }
  252. } {999 999 100}
  253. do_test analyze3-2.4 {
  254. set like "a%"
  255. sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
  256. } {101 0 100}
  257. do_test analyze3-2.5 {
  258. set like "%a"
  259. sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
  260. } {999 999 100}
  261. do_test analyze3-2.6 {
  262. set like "a"
  263. sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
  264. } {101 0 0}
  265. do_test analyze3-2.7 {
  266. set like "ab"
  267. sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
  268. } {11 0 0}
  269. do_test analyze3-2.8 {
  270. set like "abc"
  271. sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
  272. } {2 0 1}
  273. do_test analyze3-2.9 {
  274. set like "a_c"
  275. sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
  276. } {101 0 10}
  277. #-------------------------------------------------------------------------
  278. # This block of tests checks that statements are correctly marked as
  279. # expired when the values bound to any parameters that may affect the
  280. # query plan are modified.
  281. #
  282. drop_all_tables
  283. db auth auth
  284. proc auth {args} {
  285. set ::auth 1
  286. return SQLITE_OK
  287. }
  288. do_test analyze3-3.1 {
  289. execsql {
  290. BEGIN;
  291. CREATE TABLE t1(a, b, c);
  292. CREATE INDEX i1 ON t1(b);
  293. }
  294. for {set i 0} {$i < 100} {incr i} {
  295. execsql { INSERT INTO t1 VALUES($i, $i, $i) }
  296. }
  297. execsql COMMIT
  298. execsql ANALYZE
  299. } {}
  300. do_test analyze3-3.2.1 {
  301. set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b>?" -1 dummy]
  302. sqlite3_expired $S
  303. } {0}
  304. do_test analyze3-3.2.2 {
  305. sqlite3_bind_text $S 1 "abc" 3
  306. sqlite3_expired $S
  307. } {1}
  308. do_test analyze3-3.2.4 {
  309. sqlite3_finalize $S
  310. } {SQLITE_OK}
  311. do_test analyze3-3.2.5 {
  312. set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b=?" -1 dummy]
  313. sqlite3_expired $S
  314. } {0}
  315. do_test analyze3-3.2.6 {
  316. sqlite3_bind_text $S 1 "abc" 3
  317. sqlite3_expired $S
  318. } {1}
  319. do_test analyze3-3.2.7 {
  320. sqlite3_finalize $S
  321. } {SQLITE_OK}
  322. do_test analyze3-3.4.1 {
  323. set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  324. sqlite3_expired $S
  325. } {0}
  326. do_test analyze3-3.4.2 {
  327. sqlite3_bind_text $S 1 "abc" 3
  328. sqlite3_expired $S
  329. } {0}
  330. do_test analyze3-3.4.3 {
  331. sqlite3_bind_text $S 2 "def" 3
  332. sqlite3_expired $S
  333. } {1}
  334. do_test analyze3-3.4.4 {
  335. sqlite3_bind_text $S 2 "ghi" 3
  336. sqlite3_expired $S
  337. } {1}
  338. do_test analyze3-3.4.5 {
  339. sqlite3_expired $S
  340. } {1}
  341. do_test analyze3-3.4.6 {
  342. sqlite3_finalize $S
  343. } {SQLITE_OK}
  344. do_test analyze3-3.5.1 {
  345. set S [sqlite3_prepare_v2 db {
  346. SELECT * FROM t1 WHERE a IN (
  347. ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
  348. ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
  349. ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31
  350. ) AND b>?32;
  351. } -1 dummy]
  352. sqlite3_expired $S
  353. } {0}
  354. do_test analyze3-3.5.2 {
  355. sqlite3_bind_text $S 31 "abc" 3
  356. sqlite3_expired $S
  357. } {0}
  358. do_test analyze3-3.5.3 {
  359. sqlite3_bind_text $S 32 "def" 3
  360. sqlite3_expired $S
  361. } {1}
  362. do_test analyze3-3.5.5 {
  363. sqlite3_finalize $S
  364. } {SQLITE_OK}
  365. do_test analyze3-3.6.1 {
  366. set S [sqlite3_prepare_v2 db {
  367. SELECT * FROM t1 WHERE a IN (
  368. ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
  369. ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
  370. ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
  371. ) AND b>?33;
  372. } -1 dummy]
  373. sqlite3_expired $S
  374. } {0}
  375. do_test analyze3-3.6.2 {
  376. sqlite3_bind_text $S 32 "abc" 3
  377. sqlite3_expired $S
  378. } {1}
  379. do_test analyze3-3.6.3 {
  380. sqlite3_bind_text $S 33 "def" 3
  381. sqlite3_expired $S
  382. } {1}
  383. do_test analyze3-3.6.5 {
  384. sqlite3_finalize $S
  385. } {SQLITE_OK}
  386. do_test analyze3-3.7.1 {
  387. set S [sqlite3_prepare_v2 db {
  388. SELECT * FROM t1 WHERE a IN (
  389. ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?33,
  390. ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
  391. ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
  392. ) AND b>?10;
  393. } -1 dummy]
  394. sqlite3_expired $S
  395. } {0}
  396. do_test analyze3-3.7.2 {
  397. sqlite3_bind_text $S 32 "abc" 3
  398. sqlite3_expired $S
  399. } {0}
  400. do_test analyze3-3.7.3 {
  401. sqlite3_bind_text $S 33 "def" 3
  402. sqlite3_expired $S
  403. } {0}
  404. do_test analyze3-3.7.4 {
  405. sqlite3_bind_text $S 10 "def" 3
  406. sqlite3_expired $S
  407. } {1}
  408. do_test analyze3-3.7.6 {
  409. sqlite3_finalize $S
  410. } {SQLITE_OK}
  411. do_test analyze3-3.8.1 {
  412. execsql {
  413. CREATE TABLE t4(x, y TEXT COLLATE NOCASE);
  414. CREATE INDEX i4 ON t4(y);
  415. }
  416. } {}
  417. do_test analyze3-3.8.2 {
  418. set S [sqlite3_prepare_v2 db {
  419. SELECT * FROM t4 WHERE x != ? AND y LIKE ?
  420. } -1 dummy]
  421. sqlite3_expired $S
  422. } {0}
  423. do_test analyze3-3.8.3 {
  424. sqlite3_bind_text $S 1 "abc" 3
  425. sqlite3_expired $S
  426. } {0}
  427. do_test analyze3-3.8.4 {
  428. sqlite3_bind_text $S 2 "def" 3
  429. sqlite3_expired $S
  430. } {1}
  431. do_test analyze3-3.8.7 {
  432. sqlite3_bind_text $S 2 "ghi%" 4
  433. sqlite3_expired $S
  434. } {1}
  435. do_test analyze3-3.8.8 {
  436. sqlite3_expired $S
  437. } {1}
  438. do_test analyze3-3.8.9 {
  439. sqlite3_bind_text $S 2 "ghi%def" 7
  440. sqlite3_expired $S
  441. } {1}
  442. do_test analyze3-3.8.10 {
  443. sqlite3_expired $S
  444. } {1}
  445. do_test analyze3-3.8.11 {
  446. sqlite3_bind_text $S 2 "%ab" 3
  447. sqlite3_expired $S
  448. } {1}
  449. do_test analyze3-3.8.12 {
  450. sqlite3_expired $S
  451. } {1}
  452. do_test analyze3-3.8.12 {
  453. sqlite3_bind_text $S 2 "%de" 3
  454. sqlite3_expired $S
  455. } {1}
  456. do_test analyze3-3.8.13 {
  457. sqlite3_expired $S
  458. } {1}
  459. do_test analyze3-3.8.14 {
  460. sqlite3_finalize $S
  461. } {SQLITE_OK}
  462. #-------------------------------------------------------------------------
  463. # These tests check that errors encountered while repreparing an SQL
  464. # statement within sqlite3Reprepare() are handled correctly.
  465. #
  466. # Check a schema error.
  467. #
  468. do_test analyze3-4.1.1 {
  469. set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  470. sqlite3_step $S
  471. } {SQLITE_DONE}
  472. do_test analyze3-4.1.2 {
  473. sqlite3_reset $S
  474. sqlite3_bind_text $S 2 "abc" 3
  475. execsql { DROP TABLE t1 }
  476. sqlite3_step $S
  477. } {SQLITE_ERROR}
  478. do_test analyze3-4.1.3 {
  479. sqlite3_finalize $S
  480. } {SQLITE_ERROR}
  481. # Check an authorization error.
  482. #
  483. do_test analyze3-4.2.1 {
  484. execsql {
  485. BEGIN;
  486. CREATE TABLE t1(a, b, c);
  487. CREATE INDEX i1 ON t1(b);
  488. }
  489. for {set i 0} {$i < 100} {incr i} {
  490. execsql { INSERT INTO t1 VALUES($i, $i, $i) }
  491. }
  492. execsql COMMIT
  493. execsql ANALYZE
  494. set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  495. sqlite3_step $S
  496. } {SQLITE_DONE}
  497. db auth auth
  498. proc auth {args} {
  499. if {[lindex $args 0] == "SQLITE_READ"} {return SQLITE_DENY}
  500. return SQLITE_OK
  501. }
  502. do_test analyze3-4.2.2 {
  503. sqlite3_reset $S
  504. sqlite3_bind_text $S 2 "abc" 3
  505. sqlite3_step $S
  506. } {SQLITE_AUTH}
  507. do_test analyze3-4.2.4 {
  508. sqlite3_finalize $S
  509. } {SQLITE_AUTH}
  510. # Check the effect of an authorization error that occurs in a re-prepare
  511. # performed by sqlite3_step() is the same as one that occurs within
  512. # sqlite3Reprepare().
  513. #
  514. do_test analyze3-4.3.1 {
  515. db auth {}
  516. set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
  517. execsql { CREATE TABLE t2(d, e, f) }
  518. db auth auth
  519. sqlite3_step $S
  520. } {SQLITE_AUTH}
  521. do_test analyze3-4.3.2 {
  522. sqlite3_finalize $S
  523. } {SQLITE_AUTH}
  524. db auth {}
  525. #-------------------------------------------------------------------------
  526. # Test that modifying bound variables using the clear_bindings() or
  527. # transfer_bindings() APIs works.
  528. #
  529. # analyze3-5.1.*: sqlite3_clear_bindings()
  530. # analyze3-5.2.*: sqlite3_transfer_bindings()
  531. #
  532. do_test analyze3-5.1.1 {
  533. drop_all_tables
  534. execsql {
  535. CREATE TABLE t1(x TEXT COLLATE NOCASE);
  536. CREATE INDEX i1 ON t1(x);
  537. INSERT INTO t1 VALUES('aaa');
  538. INSERT INTO t1 VALUES('abb');
  539. INSERT INTO t1 VALUES('acc');
  540. INSERT INTO t1 VALUES('baa');
  541. INSERT INTO t1 VALUES('bbb');
  542. INSERT INTO t1 VALUES('bcc');
  543. }
  544. set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
  545. sqlite3_bind_text $S 1 "a%" 2
  546. set R [list]
  547. while { "SQLITE_ROW" == [sqlite3_step $S] } {
  548. lappend R [sqlite3_column_text $S 0]
  549. }
  550. concat [sqlite3_reset $S] $R
  551. } {SQLITE_OK aaa abb acc}
  552. do_test analyze3-5.1.2 {
  553. sqlite3_clear_bindings $S
  554. set R [list]
  555. while { "SQLITE_ROW" == [sqlite3_step $S] } {
  556. lappend R [sqlite3_column_text $S 0]
  557. }
  558. concat [sqlite3_reset $S] $R
  559. } {SQLITE_OK}
  560. do_test analyze3-5.1.3 {
  561. sqlite3_finalize $S
  562. } {SQLITE_OK}
  563. do_test analyze3-5.1.1 {
  564. set S1 [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
  565. sqlite3_bind_text $S1 1 "b%" 2
  566. set R [list]
  567. while { "SQLITE_ROW" == [sqlite3_step $S1] } {
  568. lappend R [sqlite3_column_text $S1 0]
  569. }
  570. concat [sqlite3_reset $S1] $R
  571. } {SQLITE_OK baa bbb bcc}
  572. do_test analyze3-5.1.2 {
  573. set S2 [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x = ?" -1 dummy]
  574. sqlite3_bind_text $S2 1 "a%" 2
  575. sqlite3_transfer_bindings $S2 $S1
  576. set R [list]
  577. while { "SQLITE_ROW" == [sqlite3_step $S1] } {
  578. lappend R [sqlite3_column_text $S1 0]
  579. }
  580. concat [sqlite3_reset $S1] $R
  581. } {SQLITE_OK aaa abb acc}
  582. do_test analyze3-5.1.3 {
  583. sqlite3_finalize $S2
  584. sqlite3_finalize $S1
  585. } {SQLITE_OK}
  586. #-------------------------------------------------------------------------
  587. do_test analyze3-6.1 {
  588. execsql { DROP TABLE IF EXISTS t1 }
  589. execsql BEGIN
  590. execsql { CREATE TABLE t1(a, b, c) }
  591. for {set i 0} {$i < 1000} {incr i} {
  592. execsql "INSERT INTO t1 VALUES([expr $i/100], 'x', [expr $i/10])"
  593. }
  594. execsql {
  595. CREATE INDEX i1 ON t1(a, b);
  596. CREATE INDEX i2 ON t1(c);
  597. }
  598. execsql COMMIT
  599. execsql ANALYZE
  600. } {}
  601. do_eqp_test analyze3-6-3 {
  602. SELECT * FROM t1 WHERE a = 5 AND c = 13;
  603. } {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (c=?)}}
  604. do_eqp_test analyze3-6-2 {
  605. SELECT * FROM t1 WHERE a = 5 AND b > 'w' AND c = 13;
  606. } {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (c=?)}}
  607. finish_test