]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Backport http://www.sqlite.org/cvstrac/chngview?cn=5489 from fts3.
authorshess <shess@noemail.net>
Tue, 29 Jul 2008 20:38:17 +0000 (20:38 +0000)
committershess <shess@noemail.net>
Tue, 29 Jul 2008 20:38:17 +0000 (20:38 +0000)
Re-used prepared statement from fts2 cursor. (CVS 5499)

FossilOrigin-Name: 02870ed21dae2601a656b2f30c3ca0041e9cb60f

ext/fts2/fts2.c
manifest
manifest.uuid
test/fts2r.test [new file with mode: 0644]

index d3a3596b8e9bf60ca5f96b11611fb5fbd82dacd2..226b213e0e69a6688d71f520a99063ef3636c2a5 100644 (file)
@@ -3777,18 +3777,38 @@ static int fulltextFilter(
   fulltext_cursor *c = (fulltext_cursor *) pCursor;
   fulltext_vtab *v = cursor_vtab(c);
   int rc;
-  char *zSql;
 
   TRACE(("FTS2 Filter %p\n",pCursor));
 
-  zSql = sqlite3_mprintf("select rowid, * from %%_content %s",
-                          idxNum==QUERY_GENERIC ? "" : "where rowid=?");
-  sqlite3_finalize(c->pStmt);
-  rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, zSql);
-  sqlite3_free(zSql);
-  if( rc!=SQLITE_OK ) return rc;
+  /* If the cursor has a statement that was not prepared according to
+  ** idxNum, clear it.  I believe all calls to fulltextFilter with a
+  ** given cursor will have the same idxNum , but in this case it's
+  ** easy to be safe.
+  */
+  if( c->pStmt && c->iCursorType!=idxNum ){
+    sqlite3_finalize(c->pStmt);
+    c->pStmt = NULL;
+  }
+
+  /* Get a fresh statement appropriate to idxNum. */
+  /* TODO(shess): Add a prepared-statement cache in the vt structure.
+  ** The cache must handle multiple open cursors.  Easier to cache the
+  ** statement variants at the vt to reduce malloc/realloc/free here.
+  ** Or we could have a StringBuffer variant which allowed stack
+  ** construction for small values.
+  */
+  if( !c->pStmt ){
+    char *zSql = sqlite3_mprintf("select rowid, * from %%_content %s",
+                                 idxNum==QUERY_GENERIC ? "" : "where rowid=?");
+    rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, zSql);
+    sqlite3_free(zSql);
+    if( rc!=SQLITE_OK ) return rc;
+    c->iCursorType = idxNum;
+  }else{
+    sqlite3_reset(c->pStmt);
+    assert( c->iCursorType==idxNum );
+  }
 
-  c->iCursorType = idxNum;
   switch( idxNum ){
     case QUERY_GENERIC:
       break;
index ebf686f06dcf2b6f47602441d02d9d1e37debf19..fa9707b89b8f9c1ef0657352aad52103ceac5c1b 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\ssome\ssimple\stests\sto\smake\ssure\sthat\sthe\sdifferent\sfulltextFilter\nquery\spaths\sare\sbeing\sexercised.\s(CVS\s5498)
-D 2008-07-29T20:24:46
+C Backport\shttp://www.sqlite.org/cvstrac/chngview?cn=5489\sfrom\sfts3.\nRe-used\sprepared\sstatement\sfrom\sfts2\scursor.\s(CVS\s5499)
+D 2008-07-29T20:38:18
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in bbb62eecc851379aef5a48a1bf8787eb13e6ec06
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -39,7 +39,7 @@ F ext/fts1/simple_tokenizer.c 1844d72f7194c3fd3d7e4173053911bf0661b70d
 F ext/fts1/tokenizer.h 0c53421b832366d20d720d21ea3e1f6e66a36ef9
 F ext/fts2/README.tokenizers 21e3684ea5a095b55d70f6878b4ce6af5932dfb7
 F ext/fts2/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d
-F ext/fts2/fts2.c 5f6f8fa8f756d37a5eb271ea42e56a103f965ef5
+F ext/fts2/fts2.c bc78da57642edfcf7ba2fb8a8771f97663449104
 F ext/fts2/fts2.h da5f76c65163301d1068a971fd32f4119e3c95fa
 F ext/fts2/fts2_hash.c 2689e42e1107ea67207f725cf69cf8972d00cf93
 F ext/fts2/fts2_hash.h 9a5b1be94664139f93217a0770d7144425cffb3a
@@ -318,6 +318,7 @@ F test/fts2n.test 12b9c5352128cebd1c6b8395e43788d4b09087c2
 F test/fts2o.test c6a79567d85403dc4d15b89f3f9799a0a0aef065
 F test/fts2p.test 4b48c35c91e6a7dbf5ac8d1e5691823cc999aafb
 F test/fts2q.test b2fbbe038b7a31a52a6079b215e71226d8c6a682
+F test/fts2r.test b154c30b63061d8725e320fba1a39e2201cadd5e
 F test/fts2token.test d8070b241a15ff13592a9ae4a8b7c171af6f445a
 F test/fts3.test efb41507c90f47e8af2a9101d7460cddeb84656b
 F test/fts3aa.test 432d1d5c41939bb5405d4d6c80a9ec759b363393
@@ -613,7 +614,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P c449a95c4f7abd2bfb92bed0e3a9ae874350ce79
-R b698c84f6b41998f7179c968db38e9a8
+P ae96d960e6e31315d25c7e5c3fb8363ed1b35350
+R c8b28180ba8665612880184e0c20b27c
 U shess
-Z 8007ccac7fb34b00a3c12fd8cb677718
+Z 55f351b9e5be2316c863f86a07e2dcf1
index cf6a2a09210db76445533067f283d292836c942e..960db5dce0890b720a978b9826845ae6de3693ba 100644 (file)
@@ -1 +1 @@
-ae96d960e6e31315d25c7e5c3fb8363ed1b35350
\ No newline at end of file
+02870ed21dae2601a656b2f30c3ca0041e9cb60f
\ No newline at end of file
diff --git a/test/fts2r.test b/test/fts2r.test
new file mode 100644 (file)
index 0000000..c0be367
--- /dev/null
@@ -0,0 +1,121 @@
+# 2008 July 29
+#
+# 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.
+#
+#*************************************************************************
+# These tests exercise the various types of fts2 cursors.
+#
+# $Id: fts2r.test,v 1.1 2008/07/29 20:38:18 shess Exp $
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+# If SQLITE_ENABLE_FTS2 is not defined, omit this file.
+ifcapable !fts2 {
+  finish_test
+  return
+}
+
+#*************************************************************************
+# Test table scan (QUERY_GENERIC).  This kind of query happens for
+# queries with no WHERE clause, or for WHERE clauses which cannot be
+# satisfied by an index.
+db eval {
+  DROP TABLE IF EXISTS t1;
+  CREATE VIRTUAL TABLE t1 USING fts2(c);
+  INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
+  INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
+  INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
+}
+
+do_test fts2e-1.1 {
+  execsql {
+    SELECT rowid FROM t1 ORDER BY rowid;
+  }
+} {1 2 3}
+
+do_test fts2e-1.2 {
+  execsql {
+    SELECT rowid FROM t1 WHERE c LIKE '%test' ORDER BY rowid;
+  }
+} {1 2 3}
+
+do_test fts2e-1.3 {
+  execsql {
+    SELECT rowid FROM t1 WHERE c LIKE 'That%' ORDER BY rowid;
+  }
+} {2}
+
+#*************************************************************************
+# Test lookup by rowid (QUERY_ROWID).  This kind of query happens for
+# queries which select by the rowid implicit index.
+db eval {
+  DROP TABLE IF EXISTS t1;
+  DROP TABLE IF EXISTS t2;
+  CREATE VIRTUAL TABLE t1 USING fts2(c);
+  CREATE TABLE t2(id INTEGER PRIMARY KEY AUTOINCREMENT, weight INTEGER UNIQUE);
+  INSERT INTO t2 VALUES (null, 10);
+  INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'This is a test');
+  INSERT INTO t2 VALUES (null, 5);
+  INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'That was a test');
+  INSERT INTO t2 VALUES (null, 20);
+  INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'This is a test');
+}
+
+# TODO(shess): This actually is doing QUERY_GENERIC?  I'd have
+# expected QUERY_ROWID in this case, as for a very large table the
+# full scan is less efficient.
+do_test fts2e-2.1 {
+  execsql {
+    SELECT rowid FROM t1 WHERE rowid in (1, 2, 10);
+  }
+} {1 2}
+
+do_test fts2e-2.2 {
+  execsql {
+    SELECT t1.rowid, weight FROM t1, t2 WHERE t2.id = t1.rowid ORDER BY weight;
+  }
+} {2 5 1 10 3 20}
+
+do_test fts2e-2.3 {
+  execsql {
+    SELECT t1.rowid, weight FROM t1, t2
+           WHERE t2.weight>5 AND t2.id = t1.rowid ORDER BY weight;
+  }
+} {1 10 3 20}
+
+#*************************************************************************
+# Test lookup by MATCH (QUERY_FULLTEXT).  This is the fulltext index.
+db eval {
+  DROP TABLE IF EXISTS t1;
+  DROP TABLE IF EXISTS t2;
+  CREATE VIRTUAL TABLE t1 USING fts2(c);
+  CREATE TABLE t2(id INTEGER PRIMARY KEY AUTOINCREMENT, weight INTEGER UNIQUE);
+  INSERT INTO t2 VALUES (null, 10);
+  INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'This is a test');
+  INSERT INTO t2 VALUES (null, 5);
+  INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'That was a test');
+  INSERT INTO t2 VALUES (null, 20);
+  INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'This is a test');
+}
+
+do_test fts2e-3.1 {
+  execsql {
+    SELECT rowid FROM t1 WHERE t1 MATCH 'this' ORDER BY rowid;
+  }
+} {1 3}
+
+do_test fts2e-3.2 {
+  execsql {
+    SELECT t1.rowid, weight FROM t1, t2
+     WHERE t1 MATCH 'this' AND t1.rowid = t2.id ORDER BY weight;
+  }
+} {1 10 3 20}
+
+finish_test