fts5ah.test 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # 2014 June 17
  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. # This file implements regression tests for SQLite library. The
  12. # focus of this script is testing the FTS5 module.
  13. #
  14. # TESTRUNNER: slow
  15. source [file join [file dirname [info script]] fts5_common.tcl]
  16. set testprefix fts5ah
  17. # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
  18. ifcapable !fts5 {
  19. finish_test
  20. return
  21. }
  22. foreach_detail_mode $testprefix {
  23. #-------------------------------------------------------------------------
  24. # This file contains tests for very large doclists.
  25. #
  26. set Y [list]
  27. set W [list]
  28. do_test 1.0 {
  29. execsql { CREATE VIRTUAL TABLE t1 USING fts5(a, detail=%DETAIL%) }
  30. execsql { INSERT INTO t1(t1, rank) VALUES('pgsz', 128) }
  31. set v {w w w w w w w w w w w w w w w w w w w w}
  32. execsql { INSERT INTO t1(rowid, a) VALUES(0, $v) }
  33. for {set i 1} {$i <= 10000} {incr i} {
  34. set v {x x x x x x x x x x x x x x x x x x x x}
  35. if {($i % 2139)==0} {lset v 3 Y ; lappend Y $i}
  36. if {($i % 1577)==0} {lset v 5 W ; lappend W $i}
  37. execsql { INSERT INTO t1 VALUES($v) }
  38. }
  39. set v {w w w w w w w w w w w w w w w w w w w w}
  40. execsql { INSERT INTO t1 VALUES($v) }
  41. } {}
  42. do_execsql_test 1.1.1 {
  43. SELECT rowid FROM t1 WHERE t1 MATCH 'x AND w'
  44. } [lsort -integer -incr $W]
  45. do_execsql_test 1.1.2 {
  46. SELECT rowid FROM t1 WHERE t1 MATCH 'x* AND w*'
  47. } [lsort -integer -incr $W]
  48. do_execsql_test 1.2 {
  49. SELECT rowid FROM t1 WHERE t1 MATCH 'y AND x'
  50. } [lsort -integer -incr $Y]
  51. do_execsql_test 1.3 {
  52. INSERT INTO t1(t1) VALUES('integrity-check');
  53. }
  54. proc reads {} {
  55. db one {SELECT t1 FROM t1 WHERE t1 MATCH '*reads'}
  56. }
  57. proc execsql_reads {sql} {
  58. set nRead [reads]
  59. execsql $sql
  60. expr [reads] - $nRead
  61. }
  62. do_test 1.4 {
  63. set nRead [reads]
  64. execsql { SELECT rowid FROM t1 WHERE t1 MATCH 'x' }
  65. set nReadX [expr [reads] - $nRead]
  66. #puts -nonewline "(nReadX=$nReadX)"
  67. if {[detail_is_full]} { set expect 1000 }
  68. if {[detail_is_col]} { set expect 250 }
  69. if {[detail_is_none]} { set expect 80 }
  70. expr $nReadX>$expect
  71. } {1}
  72. do_test 1.5 {
  73. set fwd [execsql_reads {SELECT rowid FROM t1 WHERE t1 MATCH 'x' }]
  74. set bwd [execsql_reads {
  75. SELECT rowid FROM t1 WHERE t1 MATCH 'x' ORDER BY 1 ASC
  76. }]
  77. expr {$bwd < $fwd + 12}
  78. } {1}
  79. foreach {tn q res} "
  80. 1 { SELECT rowid FROM t1 WHERE t1 MATCH 'w + x' } [list $W]
  81. 2 { SELECT rowid FROM t1 WHERE t1 MATCH 'x + w' } [list $W]
  82. 3 { SELECT rowid FROM t1 WHERE t1 MATCH 'x AND w' } [list $W]
  83. 4 { SELECT rowid FROM t1 WHERE t1 MATCH 'y AND x' } [list $Y]
  84. " {
  85. if {[detail_is_full]==0 && ($tn==1 || $tn==2)} continue
  86. if {[detail_is_full]} { set ratio 8 }
  87. if {[detail_is_col]} { set ratio 4 }
  88. if {[detail_is_none]} { set ratio 2 }
  89. do_test 1.6.$tn.1 {
  90. set n [execsql_reads $q]
  91. #puts -nonewline "(n=$n nReadX=$nReadX)"
  92. expr {$n < ($nReadX / $ratio)}
  93. } {1}
  94. do_test 1.6.$tn.2 {
  95. set n [execsql_reads "$q ORDER BY rowid DESC"]
  96. #puts -nonewline "(n=$n nReadX=$nReadX)"
  97. expr {$n < ($nReadX / $ratio)}
  98. } {1}
  99. do_execsql_test 1.6.$tn.3 $q [lsort -int -incr $res]
  100. do_execsql_test 1.6.$tn.4 "$q ORDER BY rowid DESC" [lsort -int -decr $res]
  101. }
  102. #-------------------------------------------------------------------------
  103. # Now test that adding range constraints on the rowid field reduces the
  104. # number of pages loaded from disk.
  105. #
  106. foreach {tn fraction tail cnt} {
  107. 1 0.6 {rowid > 5000} 5000
  108. 2 0.2 {rowid > 9000} 1000
  109. 3 0.2 {rowid < 1000} 999
  110. 4 0.2 {rowid BETWEEN 4000 AND 5000} 1001
  111. 5 0.6 {rowid >= 5000} 5001
  112. 6 0.2 {rowid >= 9000} 1001
  113. 7 0.2 {rowid <= 1000} 1000
  114. 8 0.6 {rowid > '5000'} 5000
  115. 9 0.2 {rowid > '9000'} 1000
  116. 10 0.1 {rowid = 444} 1
  117. } {
  118. set q "SELECT rowid FROM t1 WHERE t1 MATCH 'x' AND $tail"
  119. set n [execsql_reads $q]
  120. set ret [llength [execsql $q]]
  121. # Because the position lists for 'x' are quite long in this db, the
  122. # advantage is a bit smaller in detail=none mode. Update $fraction to
  123. # reflect this.
  124. if {[detail_is_none] && $fraction<0.5} { set fraction [expr $fraction*2] }
  125. do_test "1.7.$tn.asc.(n=$n ret=$ret)" {
  126. expr {$n < ($fraction*$nReadX) && $ret==$cnt}
  127. } {1}
  128. set q "SELECT rowid FROM t1 WHERE t1 MATCH 'x' AND $tail ORDER BY rowid DESC"
  129. set n [execsql_reads $q]
  130. set ret [llength [execsql $q]]
  131. do_test "1.7.$tn.desc.(n=$n ret=$ret)" {
  132. expr {$n < 2*$fraction*$nReadX && $ret==$cnt}
  133. } {1}
  134. }
  135. do_execsql_test 1.8.1 {
  136. SELECT count(*) FROM t1 WHERE t1 MATCH 'x' AND +rowid < 'text';
  137. } {10000}
  138. do_execsql_test 1.8.2 {
  139. SELECT count(*) FROM t1 WHERE t1 MATCH 'x' AND rowid < 'text';
  140. } {10000}
  141. do_execsql_test 1.8.3 {
  142. SELECT count(*) FROM t1 WHERE t1 MATCH 'x' AND rowid<5000 AND rowid < 'text';
  143. } {4999}
  144. do_execsql_test 1.8.4 {
  145. SELECT count(*) FROM t1 WHERE t1 MATCH 'x' AND rowid>5000 AND rowid > 'text';
  146. } {0}
  147. do_catchsql_test 1.9 {
  148. SELECT * FROM t1('*xy');
  149. } {1 {unknown special query: xy}}
  150. } ;# foreach_detail_mode
  151. #db eval {SELECT rowid, fts5_decode(rowid, block) aS r FROM t1_data} {puts $r}
  152. finish_test