os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/fts3d.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 implements regression tests for SQLite library.  The focus
sl@0
    12
# of this script is testing the FTS3 module's optimize() function.
sl@0
    13
#
sl@0
    14
# $Id: fts3d.test,v 1.2 2008/07/15 21:32:07 shess Exp $
sl@0
    15
#
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
# If SQLITE_ENABLE_FTS3 is not defined, omit this file.
sl@0
    21
ifcapable !fts3 {
sl@0
    22
  finish_test
sl@0
    23
  return
sl@0
    24
}
sl@0
    25
sl@0
    26
#*************************************************************************
sl@0
    27
# Probe to see if support for the FTS3 dump_* functions is compiled in.
sl@0
    28
# TODO(shess): Change main.mk to do the right thing and remove this test.
sl@0
    29
db eval {
sl@0
    30
  DROP TABLE IF EXISTS t1;
sl@0
    31
  CREATE VIRTUAL TABLE t1 USING fts3(c);
sl@0
    32
  INSERT INTO t1 (docid, c) VALUES (1, 'x');
sl@0
    33
}
sl@0
    34
sl@0
    35
set s {SELECT dump_terms(t1, 1) FROM t1 LIMIT 1}
sl@0
    36
set r {1 {unable to use function dump_terms in the requested context}}
sl@0
    37
if {[catchsql $s]==$r} {
sl@0
    38
  finish_test
sl@0
    39
  return
sl@0
    40
}
sl@0
    41
sl@0
    42
#*************************************************************************
sl@0
    43
# Utility function to check for the expected terms in the segment
sl@0
    44
# level/index.  _all version does same but for entire index.
sl@0
    45
proc check_terms {test level index terms} {
sl@0
    46
  # TODO(shess): Figure out why uplevel in do_test can't catch
sl@0
    47
  # $level and $index directly.
sl@0
    48
  set ::level $level
sl@0
    49
  set ::index $index
sl@0
    50
  do_test $test.terms {
sl@0
    51
    execsql {
sl@0
    52
      SELECT dump_terms(t1, $::level, $::index) FROM t1 LIMIT 1;
sl@0
    53
    }
sl@0
    54
  } [list $terms]
sl@0
    55
}
sl@0
    56
proc check_terms_all {test terms} {
sl@0
    57
  do_test $test.terms {
sl@0
    58
    execsql {
sl@0
    59
      SELECT dump_terms(t1) FROM t1 LIMIT 1;
sl@0
    60
    }
sl@0
    61
  } [list $terms]
sl@0
    62
}
sl@0
    63
sl@0
    64
# Utility function to check for the expected doclist for the term in
sl@0
    65
# segment level/index.  _all version does same for entire index.
sl@0
    66
proc check_doclist {test level index term doclist} {
sl@0
    67
  # TODO(shess): Again, why can't the non-:: versions work?
sl@0
    68
  set ::term $term
sl@0
    69
  set ::level $level
sl@0
    70
  set ::index $index
sl@0
    71
  do_test $test {
sl@0
    72
    execsql {
sl@0
    73
      SELECT dump_doclist(t1, $::term, $::level, $::index) FROM t1 LIMIT 1;
sl@0
    74
    }
sl@0
    75
  } [list $doclist]
sl@0
    76
}
sl@0
    77
proc check_doclist_all {test term doclist} {
sl@0
    78
  set ::term $term
sl@0
    79
  do_test $test {
sl@0
    80
    execsql {
sl@0
    81
      SELECT dump_doclist(t1, $::term) FROM t1 LIMIT 1;
sl@0
    82
    }
sl@0
    83
  } [list $doclist]
sl@0
    84
}
sl@0
    85
sl@0
    86
#*************************************************************************
sl@0
    87
# Test results when all rows are deleted and one is added back.
sl@0
    88
# Previously older segments would continue to exist, but now the index
sl@0
    89
# should be dropped when the table is empty.  The results should look
sl@0
    90
# exactly like we never added the earlier rows in the first place.
sl@0
    91
db eval {
sl@0
    92
  DROP TABLE IF EXISTS t1;
sl@0
    93
  CREATE VIRTUAL TABLE t1 USING fts3(c);
sl@0
    94
  INSERT INTO t1 (docid, c) VALUES (1, 'This is a test');
sl@0
    95
  INSERT INTO t1 (docid, c) VALUES (2, 'That was a test');
sl@0
    96
  INSERT INTO t1 (docid, c) VALUES (3, 'This is a test');
sl@0
    97
  DELETE FROM t1 WHERE 1=1; -- Delete each row rather than dropping table.
sl@0
    98
  INSERT INTO t1 (docid, c) VALUES (1, 'This is a test');
sl@0
    99
}
sl@0
   100
sl@0
   101
# Should be a single initial segment.
sl@0
   102
do_test fts3d-1.segments {
sl@0
   103
  execsql {
sl@0
   104
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   105
  }
sl@0
   106
} {0 0}
sl@0
   107
do_test fts3d-1.matches {
sl@0
   108
  execsql {
sl@0
   109
    SELECT OFFSETS(t1) FROM t1
sl@0
   110
     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY docid;
sl@0
   111
  }
sl@0
   112
} {{0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}}
sl@0
   113
sl@0
   114
check_terms_all fts3d-1.1 {a is test this}
sl@0
   115
check_doclist_all fts3d-1.1.1 a {[1 0[2]]}
sl@0
   116
check_doclist_all fts3d-1.1.2 is {[1 0[1]]}
sl@0
   117
check_doclist_all fts3d-1.1.3 test {[1 0[3]]}
sl@0
   118
check_doclist_all fts3d-1.1.4 this {[1 0[0]]}
sl@0
   119
sl@0
   120
check_terms   fts3d-1.2   0 0 {a is test this}
sl@0
   121
check_doclist fts3d-1.2.1 0 0 a {[1 0[2]]}
sl@0
   122
check_doclist fts3d-1.2.2 0 0 is {[1 0[1]]}
sl@0
   123
check_doclist fts3d-1.2.3 0 0 test {[1 0[3]]}
sl@0
   124
check_doclist fts3d-1.2.4 0 0 this {[1 0[0]]}
sl@0
   125
sl@0
   126
#*************************************************************************
sl@0
   127
# Test results when everything is optimized manually.
sl@0
   128
# NOTE(shess): This is a copy of fts3c-1.3.  I've pulled a copy here
sl@0
   129
# because fts3d-2 and fts3d-3 should have identical results.
sl@0
   130
db eval {
sl@0
   131
  DROP TABLE IF EXISTS t1;
sl@0
   132
  CREATE VIRTUAL TABLE t1 USING fts3(c);
sl@0
   133
  INSERT INTO t1 (docid, c) VALUES (1, 'This is a test');
sl@0
   134
  INSERT INTO t1 (docid, c) VALUES (2, 'That was a test');
sl@0
   135
  INSERT INTO t1 (docid, c) VALUES (3, 'This is a test');
sl@0
   136
  DELETE FROM t1 WHERE docid IN (1,3);
sl@0
   137
  DROP TABLE IF EXISTS t1old;
sl@0
   138
  ALTER TABLE t1 RENAME TO t1old;
sl@0
   139
  CREATE VIRTUAL TABLE t1 USING fts3(c);
sl@0
   140
  INSERT INTO t1 (docid, c) SELECT docid, c FROM t1old;
sl@0
   141
  DROP TABLE t1old;
sl@0
   142
}
sl@0
   143
sl@0
   144
# Should be a single optimal segment with the same logical results.
sl@0
   145
do_test fts3d-2.segments {
sl@0
   146
  execsql {
sl@0
   147
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   148
  }
sl@0
   149
} {0 0}
sl@0
   150
do_test fts3d-2.matches {
sl@0
   151
  execsql {
sl@0
   152
    SELECT OFFSETS(t1) FROM t1
sl@0
   153
     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY docid;
sl@0
   154
  }
sl@0
   155
} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
sl@0
   156
sl@0
   157
check_terms_all fts3d-2.1 {a test that was}
sl@0
   158
check_doclist_all fts3d-2.1.1 a {[2 0[2]]}
sl@0
   159
check_doclist_all fts3d-2.1.2 test {[2 0[3]]}
sl@0
   160
check_doclist_all fts3d-2.1.3 that {[2 0[0]]}
sl@0
   161
check_doclist_all fts3d-2.1.4 was {[2 0[1]]}
sl@0
   162
sl@0
   163
check_terms fts3d-2.2 0 0 {a test that was}
sl@0
   164
check_doclist fts3d-2.2.1 0 0 a {[2 0[2]]}
sl@0
   165
check_doclist fts3d-2.2.2 0 0 test {[2 0[3]]}
sl@0
   166
check_doclist fts3d-2.2.3 0 0 that {[2 0[0]]}
sl@0
   167
check_doclist fts3d-2.2.4 0 0 was {[2 0[1]]}
sl@0
   168
sl@0
   169
#*************************************************************************
sl@0
   170
# Test results when everything is optimized via optimize().
sl@0
   171
db eval {
sl@0
   172
  DROP TABLE IF EXISTS t1;
sl@0
   173
  CREATE VIRTUAL TABLE t1 USING fts3(c);
sl@0
   174
  INSERT INTO t1 (docid, c) VALUES (1, 'This is a test');
sl@0
   175
  INSERT INTO t1 (docid, c) VALUES (2, 'That was a test');
sl@0
   176
  INSERT INTO t1 (docid, c) VALUES (3, 'This is a test');
sl@0
   177
  DELETE FROM t1 WHERE docid IN (1,3);
sl@0
   178
  SELECT OPTIMIZE(t1) FROM t1 LIMIT 1;
sl@0
   179
}
sl@0
   180
sl@0
   181
# Should be a single optimal segment with the same logical results.
sl@0
   182
do_test fts3d-3.segments {
sl@0
   183
  execsql {
sl@0
   184
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   185
  }
sl@0
   186
} {0 0}
sl@0
   187
do_test fts3d-3.matches {
sl@0
   188
  execsql {
sl@0
   189
    SELECT OFFSETS(t1) FROM t1
sl@0
   190
     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY docid;
sl@0
   191
  }
sl@0
   192
} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
sl@0
   193
sl@0
   194
check_terms_all fts3d-3.1 {a test that was}
sl@0
   195
check_doclist_all fts3d-3.1.1 a {[2 0[2]]}
sl@0
   196
check_doclist_all fts3d-3.1.2 test {[2 0[3]]}
sl@0
   197
check_doclist_all fts3d-3.1.3 that {[2 0[0]]}
sl@0
   198
check_doclist_all fts3d-3.1.4 was {[2 0[1]]}
sl@0
   199
sl@0
   200
check_terms fts3d-3.2 0 0 {a test that was}
sl@0
   201
check_doclist fts3d-3.2.1 0 0 a {[2 0[2]]}
sl@0
   202
check_doclist fts3d-3.2.2 0 0 test {[2 0[3]]}
sl@0
   203
check_doclist fts3d-3.2.3 0 0 that {[2 0[0]]}
sl@0
   204
check_doclist fts3d-3.2.4 0 0 was {[2 0[1]]}
sl@0
   205
sl@0
   206
#*************************************************************************
sl@0
   207
# Test optimize() against a table involving segment merges.
sl@0
   208
# NOTE(shess): Since there's no transaction, each of the INSERT/UPDATE
sl@0
   209
# statements generates a segment.
sl@0
   210
db eval {
sl@0
   211
  DROP TABLE IF EXISTS t1;
sl@0
   212
  CREATE VIRTUAL TABLE t1 USING fts3(c);
sl@0
   213
sl@0
   214
  INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
sl@0
   215
  INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
sl@0
   216
  INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
sl@0
   217
sl@0
   218
  UPDATE t1 SET c = 'This is a test one' WHERE rowid = 1;
sl@0
   219
  UPDATE t1 SET c = 'That was a test one' WHERE rowid = 2;
sl@0
   220
  UPDATE t1 SET c = 'This is a test one' WHERE rowid = 3;
sl@0
   221
sl@0
   222
  UPDATE t1 SET c = 'This is a test two' WHERE rowid = 1;
sl@0
   223
  UPDATE t1 SET c = 'That was a test two' WHERE rowid = 2;
sl@0
   224
  UPDATE t1 SET c = 'This is a test two' WHERE rowid = 3;
sl@0
   225
sl@0
   226
  UPDATE t1 SET c = 'This is a test three' WHERE rowid = 1;
sl@0
   227
  UPDATE t1 SET c = 'That was a test three' WHERE rowid = 2;
sl@0
   228
  UPDATE t1 SET c = 'This is a test three' WHERE rowid = 3;
sl@0
   229
sl@0
   230
  UPDATE t1 SET c = 'This is a test four' WHERE rowid = 1;
sl@0
   231
  UPDATE t1 SET c = 'That was a test four' WHERE rowid = 2;
sl@0
   232
  UPDATE t1 SET c = 'This is a test four' WHERE rowid = 3;
sl@0
   233
sl@0
   234
  UPDATE t1 SET c = 'This is a test' WHERE rowid = 1;
sl@0
   235
  UPDATE t1 SET c = 'That was a test' WHERE rowid = 2;
sl@0
   236
  UPDATE t1 SET c = 'This is a test' WHERE rowid = 3;
sl@0
   237
}
sl@0
   238
sl@0
   239
# 2 segments in level 0, 1 in level 1 (18 segments created, 16
sl@0
   240
# merged).
sl@0
   241
do_test fts3d-4.segments {
sl@0
   242
  execsql {
sl@0
   243
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   244
  }
sl@0
   245
} {0 0 0 1 1 0}
sl@0
   246
sl@0
   247
do_test fts3d-4.matches {
sl@0
   248
  execsql {
sl@0
   249
    SELECT OFFSETS(t1) FROM t1
sl@0
   250
     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY docid;
sl@0
   251
  }
sl@0
   252
} [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \
sl@0
   253
        {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \
sl@0
   254
        {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}]
sl@0
   255
sl@0
   256
check_terms_all fts3d-4.1      {a four is one test that this three two was}
sl@0
   257
check_doclist_all fts3d-4.1.1  a {[1 0[2]] [2 0[2]] [3 0[2]]}
sl@0
   258
check_doclist_all fts3d-4.1.2  four {}
sl@0
   259
check_doclist_all fts3d-4.1.3  is {[1 0[1]] [3 0[1]]}
sl@0
   260
check_doclist_all fts3d-4.1.4  one {}
sl@0
   261
check_doclist_all fts3d-4.1.5  test {[1 0[3]] [2 0[3]] [3 0[3]]}
sl@0
   262
check_doclist_all fts3d-4.1.6  that {[2 0[0]]}
sl@0
   263
check_doclist_all fts3d-4.1.7  this {[1 0[0]] [3 0[0]]}
sl@0
   264
check_doclist_all fts3d-4.1.8  three {}
sl@0
   265
check_doclist_all fts3d-4.1.9  two {}
sl@0
   266
check_doclist_all fts3d-4.1.10 was {[2 0[1]]}
sl@0
   267
sl@0
   268
check_terms fts3d-4.2     0 0 {a four test that was}
sl@0
   269
check_doclist fts3d-4.2.1 0 0 a {[2 0[2]]}
sl@0
   270
check_doclist fts3d-4.2.2 0 0 four {[2]}
sl@0
   271
check_doclist fts3d-4.2.3 0 0 test {[2 0[3]]}
sl@0
   272
check_doclist fts3d-4.2.4 0 0 that {[2 0[0]]}
sl@0
   273
check_doclist fts3d-4.2.5 0 0 was {[2 0[1]]}
sl@0
   274
sl@0
   275
check_terms fts3d-4.3     0 1 {a four is test this}
sl@0
   276
check_doclist fts3d-4.3.1 0 1 a {[3 0[2]]}
sl@0
   277
check_doclist fts3d-4.3.2 0 1 four {[3]}
sl@0
   278
check_doclist fts3d-4.3.3 0 1 is {[3 0[1]]}
sl@0
   279
check_doclist fts3d-4.3.4 0 1 test {[3 0[3]]}
sl@0
   280
check_doclist fts3d-4.3.5 0 1 this {[3 0[0]]}
sl@0
   281
sl@0
   282
check_terms fts3d-4.4      1 0 {a four is one test that this three two was}
sl@0
   283
check_doclist fts3d-4.4.1  1 0 a {[1 0[2]] [2 0[2]] [3 0[2]]}
sl@0
   284
check_doclist fts3d-4.4.2  1 0 four {[1] [2 0[4]] [3 0[4]]}
sl@0
   285
check_doclist fts3d-4.4.3  1 0 is {[1 0[1]] [3 0[1]]}
sl@0
   286
check_doclist fts3d-4.4.4  1 0 one {[1] [2] [3]}
sl@0
   287
check_doclist fts3d-4.4.5  1 0 test {[1 0[3]] [2 0[3]] [3 0[3]]}
sl@0
   288
check_doclist fts3d-4.4.6  1 0 that {[2 0[0]]}
sl@0
   289
check_doclist fts3d-4.4.7  1 0 this {[1 0[0]] [3 0[0]]}
sl@0
   290
check_doclist fts3d-4.4.8  1 0 three {[1] [2] [3]}
sl@0
   291
check_doclist fts3d-4.4.9  1 0 two {[1] [2] [3]}
sl@0
   292
check_doclist fts3d-4.4.10 1 0 was {[2 0[1]]}
sl@0
   293
sl@0
   294
# Optimize should leave the result in the level of the highest-level
sl@0
   295
# prior segment.
sl@0
   296
do_test fts3d-4.5 {
sl@0
   297
  execsql {
sl@0
   298
    SELECT OPTIMIZE(t1) FROM t1 LIMIT 1;
sl@0
   299
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   300
  }
sl@0
   301
} {{Index optimized} 1 0}
sl@0
   302
sl@0
   303
# Identical to fts3d-4.matches.
sl@0
   304
do_test fts3d-4.5.matches {
sl@0
   305
  execsql {
sl@0
   306
    SELECT OFFSETS(t1) FROM t1
sl@0
   307
     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY docid;
sl@0
   308
  }
sl@0
   309
} [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \
sl@0
   310
        {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \
sl@0
   311
        {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}]
sl@0
   312
sl@0
   313
check_terms_all fts3d-4.5.1     {a is test that this was}
sl@0
   314
check_doclist_all fts3d-4.5.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]}
sl@0
   315
check_doclist_all fts3d-4.5.1.2 is {[1 0[1]] [3 0[1]]}
sl@0
   316
check_doclist_all fts3d-4.5.1.3 test {[1 0[3]] [2 0[3]] [3 0[3]]}
sl@0
   317
check_doclist_all fts3d-4.5.1.4 that {[2 0[0]]}
sl@0
   318
check_doclist_all fts3d-4.5.1.5 this {[1 0[0]] [3 0[0]]}
sl@0
   319
check_doclist_all fts3d-4.5.1.6 was {[2 0[1]]}
sl@0
   320
sl@0
   321
check_terms fts3d-4.5.2     1 0 {a is test that this was}
sl@0
   322
check_doclist fts3d-4.5.2.1 1 0 a {[1 0[2]] [2 0[2]] [3 0[2]]}
sl@0
   323
check_doclist fts3d-4.5.2.2 1 0 is {[1 0[1]] [3 0[1]]}
sl@0
   324
check_doclist fts3d-4.5.2.3 1 0 test {[1 0[3]] [2 0[3]] [3 0[3]]}
sl@0
   325
check_doclist fts3d-4.5.2.4 1 0 that {[2 0[0]]}
sl@0
   326
check_doclist fts3d-4.5.2.5 1 0 this {[1 0[0]] [3 0[0]]}
sl@0
   327
check_doclist fts3d-4.5.2.6 1 0 was {[2 0[1]]}
sl@0
   328
sl@0
   329
# Re-optimizing does nothing.
sl@0
   330
do_test fts3d-5.0 {
sl@0
   331
  execsql {
sl@0
   332
    SELECT OPTIMIZE(t1) FROM t1 LIMIT 1;
sl@0
   333
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   334
  }
sl@0
   335
} {{Index already optimal} 1 0}
sl@0
   336
sl@0
   337
# Even if we move things around, still does nothing.
sl@0
   338
do_test fts3d-5.1 {
sl@0
   339
  execsql {
sl@0
   340
    UPDATE t1_segdir SET level = 2 WHERE level = 1 AND idx = 0;
sl@0
   341
    SELECT OPTIMIZE(t1) FROM t1 LIMIT 1;
sl@0
   342
    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
sl@0
   343
  }
sl@0
   344
} {{Index already optimal} 2 0}
sl@0
   345
sl@0
   346
finish_test