analyzeA.test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # 2013 August 3
  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 contains automated tests used to verify that the current build
  13. # (which must be either ENABLE_STAT3 or ENABLE_STAT4) works with both stat3
  14. # and stat4 data.
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. set testprefix analyzeA
  19. ifcapable !stat4&&!stat3 {
  20. finish_test
  21. return
  22. }
  23. # Populate the stat3 table according to the current contents of the db
  24. #
  25. proc populate_stat3 {{bDropTable 1}} {
  26. # Open a second connection on database "test.db" and run ANALYZE. If this
  27. # is an ENABLE_STAT3 build, this is all that is required to create and
  28. # populate the sqlite_stat3 table.
  29. #
  30. sqlite3 db2 test.db
  31. execsql { ANALYZE }
  32. # Now, if this is an ENABLE_STAT4 build, create and populate the
  33. # sqlite_stat3 table based on the stat4 data gathered by the ANALYZE
  34. # above. Then drop the sqlite_stat4 table.
  35. #
  36. ifcapable stat4 {
  37. db2 func lindex lindex
  38. execsql {
  39. PRAGMA writable_schema = on;
  40. CREATE TABLE sqlite_stat3(tbl,idx,neq,nlt,ndlt,sample);
  41. INSERT INTO sqlite_stat3
  42. SELECT DISTINCT tbl, idx,
  43. lindex(neq,0), lindex(nlt,0), lindex(ndlt,0), test_extract(sample, 0)
  44. FROM sqlite_stat4;
  45. } db2
  46. if {$bDropTable} { execsql {DROP TABLE sqlite_stat4} db2 }
  47. execsql { PRAGMA writable_schema = off }
  48. }
  49. # Modify the database schema cookie to ensure that the other connection
  50. # reloads the schema.
  51. #
  52. execsql {
  53. CREATE TABLE obscure_tbl_nm(x);
  54. DROP TABLE obscure_tbl_nm;
  55. } db2
  56. db2 close
  57. }
  58. # Populate the stat4 table according to the current contents of the db
  59. #
  60. proc populate_stat4 {{bDropTable 1}} {
  61. sqlite3 db2 test.db
  62. execsql { ANALYZE }
  63. ifcapable stat3 {
  64. execsql {
  65. PRAGMA writable_schema = on;
  66. CREATE TABLE sqlite_stat4(tbl,idx,neq,nlt,ndlt,sample);
  67. INSERT INTO sqlite_stat4
  68. SELECT tbl, idx, neq, nlt, ndlt, sqlite_record(sample)
  69. FROM sqlite_stat3;
  70. } db2
  71. if {$bDropTable} { execsql {DROP TABLE sqlite_stat3} db2 }
  72. execsql { PRAGMA writable_schema = off }
  73. }
  74. # Modify the database schema cookie to ensure that the other connection
  75. # reloads the schema.
  76. #
  77. execsql {
  78. CREATE TABLE obscure_tbl_nm(x);
  79. DROP TABLE obscure_tbl_nm;
  80. } db2
  81. db2 close
  82. }
  83. # Populate the stat4 table according to the current contents of the db.
  84. # Leave deceptive data in the stat3 table. This data should be ignored
  85. # in favour of that from the stat4 table.
  86. #
  87. proc populate_both {} {
  88. ifcapable stat4 { populate_stat3 0 }
  89. ifcapable stat3 { populate_stat4 0 }
  90. sqlite3 db2 test.db
  91. execsql {
  92. PRAGMA writable_schema = on;
  93. UPDATE sqlite_stat3 SET idx =
  94. CASE idx WHEN 't1b' THEN 't1c' ELSE 't1b'
  95. END;
  96. PRAGMA writable_schema = off;
  97. CREATE TABLE obscure_tbl_nm(x);
  98. DROP TABLE obscure_tbl_nm;
  99. } db2
  100. db2 close
  101. }
  102. foreach {tn analyze_cmd} {
  103. 1 populate_stat4
  104. 2 populate_stat3
  105. 3 populate_both
  106. } {
  107. reset_db
  108. do_test 1.$tn.1 {
  109. execsql { CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c) }
  110. for {set i 0} {$i < 100} {incr i} {
  111. set c [expr int(pow(1.1,$i)/100)]
  112. set b [expr 125 - int(pow(1.1,99-$i))/100]
  113. execsql {INSERT INTO t1 VALUES($i, $b, $c)}
  114. }
  115. } {}
  116. execsql { CREATE INDEX t1b ON t1(b) }
  117. execsql { CREATE INDEX t1c ON t1(c) }
  118. $analyze_cmd
  119. do_execsql_test 1.$tn.2.1 { SELECT count(*) FROM t1 WHERE b=31 } 1
  120. do_execsql_test 1.$tn.2.2 { SELECT count(*) FROM t1 WHERE c=0 } 49
  121. do_execsql_test 1.$tn.2.3 { SELECT count(*) FROM t1 WHERE b=125 } 49
  122. do_execsql_test 1.$tn.2.4 { SELECT count(*) FROM t1 WHERE c=16 } 1
  123. do_eqp_test 1.$tn.2.5 {
  124. SELECT * FROM t1 WHERE b = 31 AND c = 0;
  125. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
  126. do_eqp_test 1.$tn.2.6 {
  127. SELECT * FROM t1 WHERE b = 125 AND c = 16;
  128. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c=?)}}
  129. do_execsql_test 1.$tn.3.1 {
  130. SELECT count(*) FROM t1 WHERE b BETWEEN 0 AND 50
  131. } {6}
  132. do_execsql_test 1.$tn.3.2 {
  133. SELECT count(*) FROM t1 WHERE c BETWEEN 0 AND 50
  134. } {90}
  135. do_execsql_test 1.$tn.3.3 {
  136. SELECT count(*) FROM t1 WHERE b BETWEEN 75 AND 125
  137. } {90}
  138. do_execsql_test 1.$tn.3.4 {
  139. SELECT count(*) FROM t1 WHERE c BETWEEN 75 AND 125
  140. } {6}
  141. do_eqp_test 1.$tn.3.5 {
  142. SELECT * FROM t1 WHERE b BETWEEN 0 AND 50 AND c BETWEEN 0 AND 50
  143. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}}
  144. do_eqp_test 1.$tn.3.6 {
  145. SELECT * FROM t1 WHERE b BETWEEN 75 AND 125 AND c BETWEEN 75 AND 125
  146. } {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}}
  147. }
  148. finish_test