os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/fts2p.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2008 June 26
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#*************************************************************************
sl@0
    11
# This file exercises some new testing functions in the FTS2 module,
sl@0
    12
# and then uses them to do some basic tests that FTS2 is internally
sl@0
    13
# working as expected.
sl@0
    14
#
sl@0
    15
# $Id: fts2p.test,v 1.1 2008/07/22 23:32:28 shess Exp $
sl@0
    16
#
sl@0
    17
sl@0
    18
set testdir [file dirname $argv0]
sl@0
    19
source $testdir/tester.tcl
sl@0
    20
sl@0
    21
# If SQLITE_ENABLE_FTS2 is not defined, omit this file.
sl@0
    22
ifcapable !fts2 {
sl@0
    23
  finish_test
sl@0
    24
  return
sl@0
    25
}
sl@0
    26
sl@0
    27
#*************************************************************************
sl@0
    28
# Probe to see if support for these functions is compiled in.
sl@0
    29
# TODO(shess): Change main.mk to do the right thing and remove this test.
sl@0
    30
db eval {
sl@0
    31
  DROP TABLE IF EXISTS t1;
sl@0
    32
  CREATE VIRTUAL TABLE t1 USING fts2(c);
sl@0
    33
  INSERT INTO t1 (rowid, c) VALUES (1, 'x');
sl@0
    34
}
sl@0
    35
sl@0
    36
set s {SELECT dump_terms(t1, 1) FROM t1 LIMIT 1}
sl@0
    37
set r {1 {unable to use function dump_terms in the requested context}}
sl@0
    38
if {[catchsql $s]==$r} {
sl@0
    39
  finish_test
sl@0
    40
  return
sl@0
    41
}
sl@0
    42
sl@0
    43
#*************************************************************************
sl@0
    44
# Test that the new functions give appropriate errors.
sl@0
    45
do_test fts2p-0.0 {
sl@0
    46
  catchsql {
sl@0
    47
    SELECT dump_terms(t1, 1) FROM t1 LIMIT 1;
sl@0
    48
  }
sl@0
    49
} {1 {dump_terms: incorrect arguments}}
sl@0
    50
sl@0
    51
do_test fts2p-0.1 {
sl@0
    52
  catchsql {
sl@0
    53
    SELECT dump_terms(t1, 0, 0, 0) FROM t1 LIMIT 1;
sl@0
    54
  }
sl@0
    55
} {1 {dump_terms: incorrect arguments}}
sl@0
    56
sl@0
    57
do_test fts2p-0.2 {
sl@0
    58
  catchsql {
sl@0
    59
    SELECT dump_terms(1, t1) FROM t1 LIMIT 1;
sl@0
    60
  }
sl@0
    61
} {1 {unable to use function dump_terms in the requested context}}
sl@0
    62
sl@0
    63
do_test fts2p-0.3 {
sl@0
    64
  catchsql {
sl@0
    65
    SELECT dump_terms(t1, 16, 16) FROM t1 LIMIT 1;
sl@0
    66
  }
sl@0
    67
} {1 {dump_terms: segment not found}}
sl@0
    68
sl@0
    69
do_test fts2p-0.4 {
sl@0
    70
  catchsql {
sl@0
    71
    SELECT dump_doclist(t1) FROM t1 LIMIT 1;
sl@0
    72
  }
sl@0
    73
} {1 {dump_doclist: incorrect arguments}}
sl@0
    74
sl@0
    75
do_test fts2p-0.5 {
sl@0
    76
  catchsql {
sl@0
    77
    SELECT dump_doclist(t1, NULL) FROM t1 LIMIT 1;
sl@0
    78
  }
sl@0
    79
} {1 {dump_doclist: empty second argument}}
sl@0
    80
sl@0
    81
do_test fts2p-0.6 {
sl@0
    82
  catchsql {
sl@0
    83
    SELECT dump_doclist(t1, '') FROM t1 LIMIT 1;
sl@0
    84
  }
sl@0
    85
} {1 {dump_doclist: empty second argument}}
sl@0
    86
sl@0
    87
do_test fts2p-0.7 {
sl@0
    88
  catchsql {
sl@0
    89
    SELECT dump_doclist(t1, 'a', 0) FROM t1 LIMIT 1;
sl@0
    90
  }
sl@0
    91
} {1 {dump_doclist: incorrect arguments}}
sl@0
    92
sl@0
    93
do_test fts2p-0.8 {
sl@0
    94
  catchsql {
sl@0
    95
    SELECT dump_doclist(t1, 'a', 0, 0, 0) FROM t1 LIMIT 1;
sl@0
    96
  }
sl@0
    97
} {1 {dump_doclist: incorrect arguments}}
sl@0
    98
sl@0
    99
do_test fts2p-0.9 {
sl@0
   100
  catchsql {
sl@0
   101
    SELECT dump_doclist(t1, 'a', 16, 16) FROM t1 LIMIT 1;
sl@0
   102
  }
sl@0
   103
} {1 {dump_doclist: segment not found}}
sl@0
   104
sl@0
   105
#*************************************************************************
sl@0
   106
# Utility function to check for the expected terms in the segment
sl@0
   107
# level/index.  _all version does same but for entire index.
sl@0
   108
proc check_terms {test level index terms} {
sl@0
   109
  # TODO(shess): Figure out why uplevel in do_test can't catch
sl@0
   110
  # $level and $index directly.
sl@0
   111
  set ::level $level
sl@0
   112
  set ::index $index
sl@0
   113
  do_test $test.terms {
sl@0
   114
    execsql {
sl@0
   115
      SELECT dump_terms(t1, $::level, $::index) FROM t1 LIMIT 1;
sl@0
   116
    }
sl@0
   117
  } [list $terms]
sl@0
   118
}
sl@0
   119
proc check_terms_all {test terms} {
sl@0
   120
  do_test $test.terms {
sl@0
   121
    execsql {
sl@0
   122
      SELECT dump_terms(t1) FROM t1 LIMIT 1;
sl@0
   123
    }
sl@0
   124
  } [list $terms]
sl@0
   125
}
sl@0
   126
sl@0
   127
# Utility function to check for the expected doclist for the term in
sl@0
   128
# segment level/index.  _all version does same for entire index.
sl@0
   129
proc check_doclist {test level index term doclist} {
sl@0
   130
  # TODO(shess): Again, why can't the non-:: versions work?
sl@0
   131
  set ::term $term
sl@0
   132
  set ::level $level
sl@0
   133
  set ::index $index
sl@0
   134
  do_test $test {
sl@0
   135
    execsql {
sl@0
   136
      SELECT dump_doclist(t1, $::term, $::level, $::index) FROM t1 LIMIT 1;
sl@0
   137
    }
sl@0
   138
  } [list $doclist]
sl@0
   139
}
sl@0
   140
proc check_doclist_all {test term doclist} {
sl@0
   141
  set ::term $term
sl@0
   142
  do_test $test {
sl@0
   143
    execsql {
sl@0
   144
      SELECT dump_doclist(t1, $::term) FROM t1 LIMIT 1;
sl@0
   145
    }
sl@0
   146
  } [list $doclist]
sl@0
   147
}
sl@0
   148
sl@0
   149
#*************************************************************************
sl@0
   150
# Test the segments resulting from straight-forward inserts.
sl@0
   151
db eval {
sl@0
   152
  DROP TABLE IF EXISTS t1;
sl@0
   153
  CREATE VIRTUAL TABLE t1 USING fts2(c);
sl@0
   154
  INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
sl@0
   155
  INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
sl@0
   156
  INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
sl@0
   157
}
sl@0
   158
sl@0
   159
# Check for expected segments and expected matches.
sl@0
   160
do_test fts2p-1.0.segments {
sl@0
   161
  execsql {
sl@0
   162
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   163
  }
sl@0
   164
} {0 0 0 1 0 2}
sl@0
   165
do_test fts2p-1.0.matches {
sl@0
   166
  execsql {
sl@0
   167
    SELECT OFFSETS(t1) FROM t1
sl@0
   168
     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
sl@0
   169
  }
sl@0
   170
} [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \
sl@0
   171
        {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \
sl@0
   172
        {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}]
sl@0
   173
sl@0
   174
# Check the specifics of the segments constructed.
sl@0
   175
# Logical view of entire index.
sl@0
   176
check_terms_all   fts2p-1.0.1   {a is test that this was}
sl@0
   177
check_doclist_all fts2p-1.0.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]}
sl@0
   178
check_doclist_all fts2p-1.0.1.2 is {[1 0[1]] [3 0[1]]}
sl@0
   179
check_doclist_all fts2p-1.0.1.3 test {[1 0[3]] [2 0[3]] [3 0[3]]}
sl@0
   180
check_doclist_all fts2p-1.0.1.4 that {[2 0[0]]}
sl@0
   181
check_doclist_all fts2p-1.0.1.5 this {[1 0[0]] [3 0[0]]}
sl@0
   182
check_doclist_all fts2p-1.0.1.6 was {[2 0[1]]}
sl@0
   183
sl@0
   184
# Segment 0,0
sl@0
   185
check_terms   fts2p-1.0.2   0 0 {a is test this}
sl@0
   186
check_doclist fts2p-1.0.2.1 0 0 a {[1 0[2]]}
sl@0
   187
check_doclist fts2p-1.0.2.2 0 0 is {[1 0[1]]}
sl@0
   188
check_doclist fts2p-1.0.2.3 0 0 test {[1 0[3]]}
sl@0
   189
check_doclist fts2p-1.0.2.4 0 0 this {[1 0[0]]}
sl@0
   190
sl@0
   191
# Segment 0,1
sl@0
   192
check_terms   fts2p-1.0.3   0 1 {a test that was}
sl@0
   193
check_doclist fts2p-1.0.3.1 0 1 a {[2 0[2]]}
sl@0
   194
check_doclist fts2p-1.0.3.2 0 1 test {[2 0[3]]}
sl@0
   195
check_doclist fts2p-1.0.3.3 0 1 that {[2 0[0]]}
sl@0
   196
check_doclist fts2p-1.0.3.4 0 1 was {[2 0[1]]}
sl@0
   197
sl@0
   198
# Segment 0,2
sl@0
   199
check_terms   fts2p-1.0.4   0 2 {a is test this}
sl@0
   200
check_doclist fts2p-1.0.4.1 0 2 a {[3 0[2]]}
sl@0
   201
check_doclist fts2p-1.0.4.2 0 2 is {[3 0[1]]}
sl@0
   202
check_doclist fts2p-1.0.4.3 0 2 test {[3 0[3]]}
sl@0
   203
check_doclist fts2p-1.0.4.4 0 2 this {[3 0[0]]}
sl@0
   204
sl@0
   205
#*************************************************************************
sl@0
   206
# Test the segments resulting from inserts followed by a delete.
sl@0
   207
db eval {
sl@0
   208
  DROP TABLE IF EXISTS t1;
sl@0
   209
  CREATE VIRTUAL TABLE t1 USING fts2(c);
sl@0
   210
  INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
sl@0
   211
  INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
sl@0
   212
  INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
sl@0
   213
  DELETE FROM t1 WHERE rowid = 1;
sl@0
   214
}
sl@0
   215
sl@0
   216
do_test fts2p-1.1.segments {
sl@0
   217
  execsql {
sl@0
   218
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   219
  }
sl@0
   220
} {0 0 0 1 0 2 0 3}
sl@0
   221
do_test fts2p-1.1.matches {
sl@0
   222
  execsql {
sl@0
   223
    SELECT OFFSETS(t1) FROM t1
sl@0
   224
     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
sl@0
   225
  }
sl@0
   226
} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}}
sl@0
   227
sl@0
   228
check_terms_all fts2p-1.1.1 {a is test that this was}
sl@0
   229
check_doclist_all fts2p-1.1.1.1 a {[2 0[2]] [3 0[2]]}
sl@0
   230
check_doclist_all fts2p-1.1.1.2 is {[3 0[1]]}
sl@0
   231
check_doclist_all fts2p-1.1.1.3 test {[2 0[3]] [3 0[3]]}
sl@0
   232
check_doclist_all fts2p-1.1.1.4 that {[2 0[0]]}
sl@0
   233
check_doclist_all fts2p-1.1.1.5 this {[3 0[0]]}
sl@0
   234
check_doclist_all fts2p-1.1.1.6 was {[2 0[1]]}
sl@0
   235
sl@0
   236
check_terms fts2p-1.1.2 0 0 {a is test this}
sl@0
   237
check_doclist fts2p-1.1.2.1 0 0 a {[1 0[2]]}
sl@0
   238
check_doclist fts2p-1.1.2.2 0 0 is {[1 0[1]]}
sl@0
   239
check_doclist fts2p-1.1.2.3 0 0 test {[1 0[3]]}
sl@0
   240
check_doclist fts2p-1.1.2.4 0 0 this {[1 0[0]]}
sl@0
   241
sl@0
   242
check_terms fts2p-1.1.3 0 1 {a test that was}
sl@0
   243
check_doclist fts2p-1.1.3.1 0 1 a {[2 0[2]]}
sl@0
   244
check_doclist fts2p-1.1.3.2 0 1 test {[2 0[3]]}
sl@0
   245
check_doclist fts2p-1.1.3.3 0 1 that {[2 0[0]]}
sl@0
   246
check_doclist fts2p-1.1.3.4 0 1 was {[2 0[1]]}
sl@0
   247
sl@0
   248
check_terms fts2p-1.1.4 0 2 {a is test this}
sl@0
   249
check_doclist fts2p-1.1.4.1 0 2 a {[3 0[2]]}
sl@0
   250
check_doclist fts2p-1.1.4.2 0 2 is {[3 0[1]]}
sl@0
   251
check_doclist fts2p-1.1.4.3 0 2 test {[3 0[3]]}
sl@0
   252
check_doclist fts2p-1.1.4.4 0 2 this {[3 0[0]]}
sl@0
   253
sl@0
   254
check_terms fts2p-1.1.5 0 3 {a is test this}
sl@0
   255
check_doclist fts2p-1.1.5.1 0 3 a {[1]}
sl@0
   256
check_doclist fts2p-1.1.5.2 0 3 is {[1]}
sl@0
   257
check_doclist fts2p-1.1.5.3 0 3 test {[1]}
sl@0
   258
check_doclist fts2p-1.1.5.4 0 3 this {[1]}
sl@0
   259
sl@0
   260
#*************************************************************************
sl@0
   261
# Test results when all references to certain tokens are deleted.
sl@0
   262
db eval {
sl@0
   263
  DROP TABLE IF EXISTS t1;
sl@0
   264
  CREATE VIRTUAL TABLE t1 USING fts2(c);
sl@0
   265
  INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
sl@0
   266
  INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
sl@0
   267
  INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
sl@0
   268
  DELETE FROM t1 WHERE rowid IN (1,3);
sl@0
   269
}
sl@0
   270
sl@0
   271
# Still 4 segments because 0,3 will contain deletes for rowid 1 and 3.
sl@0
   272
do_test fts2p-1.2.segments {
sl@0
   273
  execsql {
sl@0
   274
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   275
  }
sl@0
   276
} {0 0 0 1 0 2 0 3}
sl@0
   277
do_test fts2p-1.2.matches {
sl@0
   278
  execsql {
sl@0
   279
    SELECT OFFSETS(t1) FROM t1
sl@0
   280
     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
sl@0
   281
  }
sl@0
   282
} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
sl@0
   283
sl@0
   284
check_terms_all fts2p-1.2.1 {a is test that this was}
sl@0
   285
check_doclist_all fts2p-1.2.1.1 a {[2 0[2]]}
sl@0
   286
check_doclist_all fts2p-1.2.1.2 is {}
sl@0
   287
check_doclist_all fts2p-1.2.1.3 test {[2 0[3]]}
sl@0
   288
check_doclist_all fts2p-1.2.1.4 that {[2 0[0]]}
sl@0
   289
check_doclist_all fts2p-1.2.1.5 this {}
sl@0
   290
check_doclist_all fts2p-1.2.1.6 was {[2 0[1]]}
sl@0
   291
sl@0
   292
check_terms fts2p-1.2.2 0 0 {a is test this}
sl@0
   293
check_doclist fts2p-1.2.2.1 0 0 a {[1 0[2]]}
sl@0
   294
check_doclist fts2p-1.2.2.2 0 0 is {[1 0[1]]}
sl@0
   295
check_doclist fts2p-1.2.2.3 0 0 test {[1 0[3]]}
sl@0
   296
check_doclist fts2p-1.2.2.4 0 0 this {[1 0[0]]}
sl@0
   297
sl@0
   298
check_terms fts2p-1.2.3 0 1 {a test that was}
sl@0
   299
check_doclist fts2p-1.2.3.1 0 1 a {[2 0[2]]}
sl@0
   300
check_doclist fts2p-1.2.3.2 0 1 test {[2 0[3]]}
sl@0
   301
check_doclist fts2p-1.2.3.3 0 1 that {[2 0[0]]}
sl@0
   302
check_doclist fts2p-1.2.3.4 0 1 was {[2 0[1]]}
sl@0
   303
sl@0
   304
check_terms fts2p-1.2.4 0 2 {a is test this}
sl@0
   305
check_doclist fts2p-1.2.4.1 0 2 a {[3 0[2]]}
sl@0
   306
check_doclist fts2p-1.2.4.2 0 2 is {[3 0[1]]}
sl@0
   307
check_doclist fts2p-1.2.4.3 0 2 test {[3 0[3]]}
sl@0
   308
check_doclist fts2p-1.2.4.4 0 2 this {[3 0[0]]}
sl@0
   309
sl@0
   310
check_terms fts2p-1.2.5 0 3 {a is test this}
sl@0
   311
check_doclist fts2p-1.2.5.1 0 3 a {[1] [3]}
sl@0
   312
check_doclist fts2p-1.2.5.2 0 3 is {[1] [3]}
sl@0
   313
check_doclist fts2p-1.2.5.3 0 3 test {[1] [3]}
sl@0
   314
check_doclist fts2p-1.2.5.4 0 3 this {[1] [3]}
sl@0
   315
sl@0
   316
#*************************************************************************
sl@0
   317
# Test results when everything is optimized manually.
sl@0
   318
db eval {
sl@0
   319
  DROP TABLE IF EXISTS t1;
sl@0
   320
  CREATE VIRTUAL TABLE t1 USING fts2(c);
sl@0
   321
  INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
sl@0
   322
  INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
sl@0
   323
  INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
sl@0
   324
  DELETE FROM t1 WHERE rowid IN (1,3);
sl@0
   325
  DROP TABLE IF EXISTS t1old;
sl@0
   326
  ALTER TABLE t1 RENAME TO t1old;
sl@0
   327
  CREATE VIRTUAL TABLE t1 USING fts2(c);
sl@0
   328
  INSERT INTO t1 (rowid, c) SELECT rowid, c FROM t1old;
sl@0
   329
  DROP TABLE t1old;
sl@0
   330
}
sl@0
   331
sl@0
   332
# Should be a single optimal segment with the same logical results.
sl@0
   333
do_test fts2p-1.3.segments {
sl@0
   334
  execsql {
sl@0
   335
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   336
  }
sl@0
   337
} {0 0}
sl@0
   338
do_test fts2p-1.3.matches {
sl@0
   339
  execsql {
sl@0
   340
    SELECT OFFSETS(t1) FROM t1
sl@0
   341
     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
sl@0
   342
  }
sl@0
   343
} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
sl@0
   344
sl@0
   345
check_terms_all fts2p-1.3.1 {a test that was}
sl@0
   346
check_doclist_all fts2p-1.3.1.1 a {[2 0[2]]}
sl@0
   347
check_doclist_all fts2p-1.3.1.2 test {[2 0[3]]}
sl@0
   348
check_doclist_all fts2p-1.3.1.3 that {[2 0[0]]}
sl@0
   349
check_doclist_all fts2p-1.3.1.4 was {[2 0[1]]}
sl@0
   350
sl@0
   351
check_terms fts2p-1.3.2 0 0 {a test that was}
sl@0
   352
check_doclist fts2p-1.3.2.1 0 0 a {[2 0[2]]}
sl@0
   353
check_doclist fts2p-1.3.2.2 0 0 test {[2 0[3]]}
sl@0
   354
check_doclist fts2p-1.3.2.3 0 0 that {[2 0[0]]}
sl@0
   355
check_doclist fts2p-1.3.2.4 0 0 was {[2 0[1]]}
sl@0
   356
sl@0
   357
finish_test