summaryrefslogtreecommitdiffstats
path: root/test/fts3offsets.test
blob: 24c339dc124d482b1b7ca2383ac95af04535dbfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 2010 November 02
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# If SQLITE_ENABLE_FTS3 is not defined, omit this file.
ifcapable !fts3 { finish_test ; return }

set testprefix fts3offsets
set sqlite_fts3_enable_parentheses 1

proc extract {offsets text} {
  set res ""

  set off [list]
  foreach {t i s n} $offsets {
    lappend off [list $s $n]
  }
  set off [lsort -integer -index 0 $off]

  set iOff 0
  foreach e $off {
    foreach {s n} $e {}
    append res [string range $text $iOff $s-1]
    append res "("
    append res [string range $text $s [expr $s+$n-1]]
    append res ")"
    set iOff [expr $s+$n]
  }
  append res [string range $text $iOff end]
  
  set res
}
db func extract extract


do_execsql_test 1.1.0 {
  CREATE VIRTUAL TABLE xx USING fts3(x);
  INSERT INTO xx VALUES('A x x x B C x x');
  INSERT INTO xx VALUES('A B C x B x x C');
  INSERT INTO xx VALUES('A x x B C x x x');
}
do_execsql_test 1.1.1 {
  SELECT oid,extract(offsets(xx), x) FROM xx WHERE xx MATCH 'a OR (b NEAR/1 c)';
} {
  1 {(A) x x x (B) (C) x x} 
  2 {(A) (B) (C) x (B) x x C} 
  3 {(A) x x (B) (C) x x x}
}

do_execsql_test 1.2 {
  DELETE FROM xx;
  INSERT INTO xx VALUES('A x x x B C x x');
  INSERT INTO xx VALUES('A x x C x x x C');
  INSERT INTO xx VALUES('A x x B C x x x');
}
do_execsql_test 1.2.1 {
  SELECT oid,extract(offsets(xx), x) FROM xx WHERE xx MATCH 'a OR (b NEAR/1 c)';
} {
  1 {(A) x x x (B) (C) x x}
  2 {(A) x x C x x x C} 
  3 {(A) x x (B) (C) x x x}
}

do_execsql_test 1.3 {
  DELETE FROM xx;
  INSERT INTO xx(rowid, x) VALUES(1, 'A B C');
  INSERT INTO xx(rowid, x) VALUES(2, 'A x');
  INSERT INTO xx(rowid, x) VALUES(3, 'A B C');
  INSERT INTO xx(rowid, x) VALUES(4, 'A B C x x x x x x x B');
  INSERT INTO xx(rowid, x) VALUES(5, 'A x x x x x x x x x C');
  INSERT INTO xx(rowid, x) VALUES(6, 'A x x x x x x x x x x x B');
  INSERT INTO xx(rowid, x) VALUES(7, 'A B C');
}
do_execsql_test 1.3.1 {
  SELECT oid,extract(offsets(xx), x) FROM xx WHERE xx MATCH 'a OR (b NEAR/1 c)';
} {
  1 {(A) (B) (C)}
  2 {(A) x}
  3 {(A) (B) (C)}
  4 {(A) (B) (C) x x x x x x x B}
  5 {(A) x x x x x x x x x C}
  6 {(A) x x x x x x x x x x x B} 
  7 {(A) (B) (C)}
}


do_execsql_test 1.4 {
  DELETE FROM xx;
  INSERT INTO xx(rowid, x) VALUES(7, 'A B C');
  INSERT INTO xx(rowid, x) VALUES(6, 'A x');
  INSERT INTO xx(rowid, x) VALUES(5, 'A B C');
  INSERT INTO xx(rowid, x) VALUES(4, 'A B C x x x x x x x B');
  INSERT INTO xx(rowid, x) VALUES(3, 'A x x x x x x x x x C');
  INSERT INTO xx(rowid, x) VALUES(2, 'A x x x x x x x x x x x B');
  INSERT INTO xx(rowid, x) VALUES(1, 'A B C');
}
do_execsql_test 1.4.1 {
  SELECT oid,extract(offsets(xx), x) FROM xx WHERE xx MATCH 'a OR (b NEAR/1 c)'
  ORDER BY docid DESC;
} {
  7 {(A) (B) (C)}
  6 {(A) x}
  5 {(A) (B) (C)}
  4 {(A) (B) (C) x x x x x x x B}
  3 {(A) x x x x x x x x x C}
  2 {(A) x x x x x x x x x x x B} 
  1 {(A) (B) (C)}
}

do_execsql_test 1.5.0 {
  CREATE VIRTUAL TABLE x1 USING fts3(x);
  INSERT INTO x1 VALUES('A A A');
  INSERT INTO x1 VALUES('A A A');
}
do_execsql_test 1.5.1 {
  SELECT offsets(x1) FROM x1 WHERE x1 MATCH 'a OR b AND c NEAR d'
} {
  {0 0 0 1 0 0 2 1 0 0 4 1} 
  {0 0 0 1 0 0 2 1 0 0 4 1}
}


set sqlite_fts3_enable_parentheses 0
finish_test