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