PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/sqlite/test/fts3an.test

#
Unknown | 218 lines | 180 code | 38 blank | 0 comment | 0 complexity | 3c5fbc1a2e0ad6cd701813d58a4182f5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # 2007 April 26
  2. #
  3. # The author disclaims copyright to this source code.
  4. #
  5. #*************************************************************************
  6. # This file implements tests for prefix-searching in the fts3
  7. # component of the SQLite library.
  8. #
  9. # $Id: fts3an.test,v 1.2 2007/12/13 21:54:11 drh Exp $
  10. #
  11. set testdir [file dirname $argv0]
  12. source $testdir/tester.tcl
  13. # If SQLITE_ENABLE_FTS3 is defined, omit this file.
  14. ifcapable !fts3 {
  15. finish_test
  16. return
  17. }
  18. # A large string to prime the pump with.
  19. set text {
  20. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas
  21. iaculis mollis ipsum. Praesent rhoncus placerat justo. Duis non quam
  22. sed turpis posuere placerat. Curabitur et lorem in lorem porttitor
  23. aliquet. Pellentesque bibendum tincidunt diam. Vestibulum blandit
  24. ante nec elit. In sapien diam, facilisis eget, dictum sed, viverra
  25. at, felis. Vestibulum magna. Sed magna dolor, vestibulum rhoncus,
  26. ornare vel, vulputate sit amet, felis. Integer malesuada, tellus at
  27. luctus gravida, diam nunc porta nibh, nec imperdiet massa metus eu
  28. lectus. Aliquam nisi. Nunc fringilla nulla at lectus. Suspendisse
  29. potenti. Cum sociis natoque penatibus et magnis dis parturient
  30. montes, nascetur ridiculus mus. Pellentesque odio nulla, feugiat eu,
  31. suscipit nec, consequat quis, risus.
  32. }
  33. db eval {
  34. CREATE VIRTUAL TABLE t1 USING fts3(c);
  35. INSERT INTO t1(rowid, c) VALUES(1, $text);
  36. INSERT INTO t1(rowid, c) VALUES(2, 'Another lovely row');
  37. }
  38. # Exact match
  39. do_test fts3an-1.1 {
  40. execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lorem'"
  41. } {1}
  42. # And a prefix
  43. do_test fts3an-1.2 {
  44. execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lore*'"
  45. } {1}
  46. # Prefix includes exact match
  47. do_test fts3an-1.3 {
  48. execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lorem*'"
  49. } {1}
  50. # Make certain everything isn't considered a prefix!
  51. do_test fts3an-1.4 {
  52. execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lore'"
  53. } {}
  54. # Prefix across multiple rows.
  55. do_test fts3an-1.5 {
  56. execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lo*'"
  57. } {1 2}
  58. # Likewise, with multiple hits in one document.
  59. do_test fts3an-1.6 {
  60. execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'l*'"
  61. } {1 2}
  62. # Prefix which should only hit one document.
  63. do_test fts3an-1.7 {
  64. execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lov*'"
  65. } {2}
  66. # * not at end is dropped.
  67. do_test fts3an-1.8 {
  68. execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lo *'"
  69. } {}
  70. # Stand-alone * is dropped.
  71. do_test fts3an-1.9 {
  72. execsql "SELECT rowid FROM t1 WHERE t1 MATCH '*'"
  73. } {}
  74. # Phrase-query prefix.
  75. do_test fts3an-1.10 {
  76. execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"lovely r*\"'"
  77. } {2}
  78. do_test fts3an-1.11 {
  79. execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"lovely r\"'"
  80. } {}
  81. # Phrase query with multiple prefix matches.
  82. do_test fts3an-1.12 {
  83. execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"a* l*\"'"
  84. } {1 2}
  85. # Phrase query with multiple prefix matches.
  86. do_test fts3an-1.13 {
  87. execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"a* l* row\"'"
  88. } {2}
  89. # Test across updates (and, by implication, deletes).
  90. # Version of text without "lorem".
  91. regsub -all {[Ll]orem} $text '' ntext
  92. db eval {
  93. CREATE VIRTUAL TABLE t2 USING fts3(c);
  94. INSERT INTO t2(rowid, c) VALUES(1, $text);
  95. INSERT INTO t2(rowid, c) VALUES(2, 'Another lovely row');
  96. UPDATE t2 SET c = $ntext WHERE rowid = 1;
  97. }
  98. # Can't see lorem as an exact match.
  99. do_test fts3an-2.1 {
  100. execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lorem'"
  101. } {}
  102. # Can't see a prefix of lorem, either.
  103. do_test fts3an-2.2 {
  104. execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lore*'"
  105. } {}
  106. # Can see lovely in the other document.
  107. do_test fts3an-2.3 {
  108. execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lo*'"
  109. } {2}
  110. # Can still see other hits.
  111. do_test fts3an-2.4 {
  112. execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'l*'"
  113. } {1 2}
  114. # Prefix which should only hit one document.
  115. do_test fts3an-2.5 {
  116. execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lov*'"
  117. } {2}
  118. # Test with a segment which will have multiple levels in the tree.
  119. # Build a big document with lots of unique terms.
  120. set bigtext $text
  121. foreach c {a b c d e} {
  122. regsub -all {[A-Za-z]+} $bigtext "&$c" t
  123. append bigtext $t
  124. }
  125. # Populate a table with many copies of the big document, so that we
  126. # can test the number of hits found. Populate $ret with the expected
  127. # hit counts for each row. offsets() returns 4 elements for every
  128. # hit. We'll have 6 hits for row 1, 1 for row 2, and 6*(2^5)==192 for
  129. # $bigtext.
  130. set ret {6 1}
  131. db eval {
  132. BEGIN;
  133. CREATE VIRTUAL TABLE t3 USING fts3(c);
  134. INSERT INTO t3(rowid, c) VALUES(1, $text);
  135. INSERT INTO t3(rowid, c) VALUES(2, 'Another lovely row');
  136. }
  137. for {set i 0} {$i<68} {incr i} {
  138. db eval {INSERT INTO t3(rowid, c) VALUES(3+$i, $bigtext)}
  139. lappend ret 192
  140. }
  141. db eval {COMMIT;}
  142. # Test that we get the expected number of hits.
  143. do_test fts3an-3.1 {
  144. set t {}
  145. db eval {SELECT offsets(t3) as o FROM t3 WHERE t3 MATCH 'l*'} {
  146. set l [llength $o]
  147. lappend t [expr {$l/4}]
  148. }
  149. set t
  150. } $ret
  151. # Test a boundary condition: More than 2^16 terms that match a searched for
  152. # prefix in a single segment.
  153. #
  154. puts "This next test can take a little while (~ 30 seconds)..."
  155. do_test fts3an-4.1 {
  156. execsql { CREATE VIRTUAL TABLE ft USING fts3(x) }
  157. execsql BEGIN
  158. execsql { INSERT INTO ft VALUES(NULL) }
  159. execsql { INSERT INTO ft SELECT * FROM ft } ;# 2
  160. execsql { INSERT INTO ft SELECT * FROM ft } ;# 4
  161. execsql { INSERT INTO ft SELECT * FROM ft } ;# 8
  162. execsql { INSERT INTO ft SELECT * FROM ft } ;# 16
  163. execsql { INSERT INTO ft SELECT * FROM ft } ;# 32
  164. execsql { INSERT INTO ft SELECT * FROM ft } ;# 64
  165. execsql { INSERT INTO ft SELECT * FROM ft } ;# 128
  166. execsql { INSERT INTO ft SELECT * FROM ft } ;# 256
  167. execsql { INSERT INTO ft SELECT * FROM ft } ;# 512
  168. execsql { INSERT INTO ft SELECT * FROM ft } ;# 1024
  169. execsql { INSERT INTO ft SELECT * FROM ft } ;# 2048
  170. execsql { INSERT INTO ft SELECT * FROM ft } ;# 4096
  171. execsql { INSERT INTO ft SELECT * FROM ft } ;# 8192
  172. execsql { INSERT INTO ft SELECT * FROM ft } ;# 16384
  173. execsql { INSERT INTO ft SELECT * FROM ft } ;# 32768
  174. execsql { INSERT INTO ft SELECT * FROM ft } ;# 65536
  175. execsql { INSERT INTO ft SELECT * FROM ft } ;# 131072
  176. execsql COMMIT
  177. execsql { UPDATE ft SET x = 'abc' || rowid }
  178. execsql { SELECT count(*) FROM ft WHERE x MATCH 'abc*' }
  179. } {131072}
  180. finish_test