1# 2010 November 02 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 13set testdir [file dirname $argv0] 14source $testdir/tester.tcl 15 16# If SQLITE_ENABLE_FTS3 is not defined, omit this file. 17ifcapable !fts3 { finish_test ; return } 18 19set testprefix fts3offsets 20set sqlite_fts3_enable_parentheses 1 21 22proc extract {offsets text} { 23 set res "" 24 25 set off [list] 26 foreach {t i s n} $offsets { 27 lappend off [list $s $n] 28 } 29 set off [lsort -integer -index 0 $off] 30 31 set iOff 0 32 foreach e $off { 33 foreach {s n} $e {} 34 append res [string range $text $iOff $s-1] 35 append res "(" 36 append res [string range $text $s [expr $s+$n-1]] 37 append res ")" 38 set iOff [expr $s+$n] 39 } 40 append res [string range $text $iOff end] 41 42 set res 43} 44db func extract extract 45 46 47do_execsql_test 1.1.0 { 48 CREATE VIRTUAL TABLE xx USING fts3(x); 49 INSERT INTO xx VALUES('A x x x B C x x'); 50 INSERT INTO xx VALUES('A B C x B x x C'); 51 INSERT INTO xx VALUES('A x x B C x x x'); 52} 53do_execsql_test 1.1.1 { 54 SELECT oid,extract(offsets(xx), x) FROM xx WHERE xx MATCH 'a OR (b NEAR/1 c)'; 55} { 56 1 {(A) x x x (B) (C) x x} 57 2 {(A) (B) (C) x (B) x x C} 58 3 {(A) x x (B) (C) x x x} 59} 60 61do_execsql_test 1.2 { 62 DELETE FROM xx; 63 INSERT INTO xx VALUES('A x x x B C x x'); 64 INSERT INTO xx VALUES('A x x C x x x C'); 65 INSERT INTO xx VALUES('A x x B C x x x'); 66} 67do_execsql_test 1.2.1 { 68 SELECT oid,extract(offsets(xx), x) FROM xx WHERE xx MATCH 'a OR (b NEAR/1 c)'; 69} { 70 1 {(A) x x x (B) (C) x x} 71 2 {(A) x x C x x x C} 72 3 {(A) x x (B) (C) x x x} 73} 74 75do_execsql_test 1.3 { 76 DELETE FROM xx; 77 INSERT INTO xx(rowid, x) VALUES(1, 'A B C'); 78 INSERT INTO xx(rowid, x) VALUES(2, 'A x'); 79 INSERT INTO xx(rowid, x) VALUES(3, 'A B C'); 80 INSERT INTO xx(rowid, x) VALUES(4, 'A B C x x x x x x x B'); 81 INSERT INTO xx(rowid, x) VALUES(5, 'A x x x x x x x x x C'); 82 INSERT INTO xx(rowid, x) VALUES(6, 'A x x x x x x x x x x x B'); 83 INSERT INTO xx(rowid, x) VALUES(7, 'A B C'); 84} 85do_execsql_test 1.3.1 { 86 SELECT oid,extract(offsets(xx), x) FROM xx WHERE xx MATCH 'a OR (b NEAR/1 c)'; 87} { 88 1 {(A) (B) (C)} 89 2 {(A) x} 90 3 {(A) (B) (C)} 91 4 {(A) (B) (C) x x x x x x x B} 92 5 {(A) x x x x x x x x x C} 93 6 {(A) x x x x x x x x x x x B} 94 7 {(A) (B) (C)} 95} 96 97 98do_execsql_test 1.4 { 99 DELETE FROM xx; 100 INSERT INTO xx(rowid, x) VALUES(7, 'A B C'); 101 INSERT INTO xx(rowid, x) VALUES(6, 'A x'); 102 INSERT INTO xx(rowid, x) VALUES(5, 'A B C'); 103 INSERT INTO xx(rowid, x) VALUES(4, 'A B C x x x x x x x B'); 104 INSERT INTO xx(rowid, x) VALUES(3, 'A x x x x x x x x x C'); 105 INSERT INTO xx(rowid, x) VALUES(2, 'A x x x x x x x x x x x B'); 106 INSERT INTO xx(rowid, x) VALUES(1, 'A B C'); 107} 108do_execsql_test 1.4.1 { 109 SELECT oid,extract(offsets(xx), x) FROM xx WHERE xx MATCH 'a OR (b NEAR/1 c)' 110 ORDER BY docid DESC; 111} { 112 7 {(A) (B) (C)} 113 6 {(A) x} 114 5 {(A) (B) (C)} 115 4 {(A) (B) (C) x x x x x x x B} 116 3 {(A) x x x x x x x x x C} 117 2 {(A) x x x x x x x x x x x B} 118 1 {(A) (B) (C)} 119} 120 121do_execsql_test 1.5.0 { 122 CREATE VIRTUAL TABLE x1 USING fts3(x); 123 INSERT INTO x1 VALUES('A A A'); 124 INSERT INTO x1 VALUES('A A A'); 125} 126do_execsql_test 1.5.1 { 127 SELECT offsets(x1) FROM x1 WHERE x1 MATCH 'a OR b AND c NEAR d' 128} { 129 {0 0 0 1 0 0 2 1 0 0 4 1} 130 {0 0 0 1 0 0 2 1 0 0 4 1} 131} 132 133 134set sqlite_fts3_enable_parentheses 0 135finish_test 136