From: dan Date: Wed, 2 Mar 2016 16:01:41 +0000 (+0000) Subject: Enhance test_bestindex.c so that it can be used to test plans generated by xBestIndex. X-Git-Tag: version-3.12.0~116 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9cc24c8002c9bbfe0615aee849c8043f7ac702e3;p=thirdparty%2Fsqlite.git Enhance test_bestindex.c so that it can be used to test plans generated by xBestIndex. FossilOrigin-Name: 3c15a9bf45cd7dae2fbd99123b8dd75ce278d6e4 --- diff --git a/manifest b/manifest index bfe67e6715..18c08f648b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sthe\soptional\s-DSERIES_OMIT_CONSTRAINT_VERIFY=0\soption\sto\sthe\sseries.c\nextension\sthat\simplements\sthe\sgenerate_series()\svirtual\stable. -D 2016-03-02T00:58:49.570 +C Enhance\stest_bestindex.c\sso\sthat\sit\scan\sbe\sused\sto\stest\splans\sgenerated\sby\sxBestIndex. +D 2016-03-02T16:01:41.146 F Makefile.in 4e90dc1521879022aa9479268a4cd141d1771142 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 4f319afb7c049d40aff7af6e8c4e7cc2ba18e079 @@ -371,7 +371,7 @@ F src/test9.c bea1e8cf52aa93695487badedd6e1886c321ea60 F src/test_async.c 21e11293a2f72080eda70e1124e9102044531cd8 F src/test_autoext.c dea8a01a7153b9adc97bd26161e4226329546e12 F src/test_backup.c 2e6e6a081870150f20c526a2e9d0d29cda47d803 -F src/test_bestindex.c cd2eb53d7abf082665bca161e6762c4ba395f17a +F src/test_bestindex.c a711473c79e812e4082fbf045ed6c18c99c16058 F src/test_blob.c b2551a9b5573232db5f66f292307c37067937239 F src/test_btree.c 2e9978eca99a9a4bfa8cae949efb00886860a64f F src/test_config.c 0dee90328e3dedf8ba002ee94b6a7e7ea7726fe4 @@ -493,7 +493,7 @@ F test/backup_malloc.test 7162d604ec2b4683c4b3799a48657fb8b5e2d450 F test/badutf.test d5360fc31f643d37a973ab0d8b4fb85799c3169f F test/badutf2.test f5bc7f2d280670ecd79b9cf4f0f1760c607fe51f F test/bc_common.tcl 3eda41ef9cda7d5f6c205462c96228b301da4191 -F test/bestindex1.test 41c763428e403663b3e15b2acba7f07b40b22592 +F test/bestindex1.test ca4a6457c276f564d9db91e22980a9fa08e66e6b F test/between.test 34d375fb5ce1ae283ffe82b6b233e9f38e84fc6c F test/bigfile.test aa74f4e5db51c8e54a1d9de9fa65d01d1eb20b59 F test/bigfile2.test 1b489a3a39ae90c7f027b79110d6b4e1dbc71bfc @@ -1453,7 +1453,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 1d41c161165006d6c2af47e476f05fb13039f8b8 -R 7bc50a1ee335d764bdfff403c9ca0d5d -U drh -Z d249d93e08651d834742ccb923f02a0e +P 3d9daa929c0abe6dc01e800ef343b0eef2f0c76a +R 10e42f20a022f81e947260641e83d066 +U dan +Z 32cbbe1a7e810bc34e8a7796bb6fedf4 diff --git a/manifest.uuid b/manifest.uuid index 67149f5847..a4b1d5378e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -3d9daa929c0abe6dc01e800ef343b0eef2f0c76a \ No newline at end of file +3c15a9bf45cd7dae2fbd99123b8dd75ce278d6e4 \ No newline at end of file diff --git a/src/test_bestindex.c b/src/test_bestindex.c index c0e67299f4..6982ce3ca9 100644 --- a/src/test_bestindex.c +++ b/src/test_bestindex.c @@ -28,8 +28,9 @@ ** CREATE VIRTUAL TABLE x1 USING tcl(tcl_command); ** ** The command [tcl_command] is invoked when the table is first created (or -** connected) and when the xBestIndex() method is invoked. When it is created -** (or connected), it is invoked as follows: +** connected), when the xBestIndex() method is invoked and when the xFilter() +** method is called. When it is created (or connected), it is invoked as +** follows: ** ** tcl_command xConnect ** @@ -67,10 +68,27 @@ ** "cost" (value of estimatedCost field) ** "rows" (value of estimatedRows field) ** "use" (index of used constraint in aConstraint[]) +** "omit" (like "use", but also sets omit flag) ** "idxnum" (value of idxNum field) ** "idxstr" (value of idxStr field) ** ** Refer to code below for further details. +** +** When SQLite calls the xFilter() method, this module invokes the following +** Tcl script: +** +** tcl_command xFilter IDXNUM IDXSTR ARGLIST +** +** IDXNUM and IDXSTR are the values of the idxNum and idxStr parameters +** passed to xFilter. ARGLIST is a Tcl list containing each of the arguments +** passed to xFilter in text form. +** +** As with xBestIndex(), the return value of the script is interpreted as a +** list of key-value pairs. There is currently only one key defined - "sql". +** The value must be the full text of an SQL statement that returns the data +** for the current scan. The leftmost column returned by the SELECT is assumed +** to contain the rowid. Other columns must follow, in order from left to +** right. */ @@ -89,11 +107,13 @@ struct tcl_vtab { sqlite3_vtab base; Tcl_Interp *interp; Tcl_Obj *pCmd; + sqlite3 *db; }; /* A tcl cursor object */ struct tcl_cursor { sqlite3_vtab_cursor base; + sqlite3_stmt *pStmt; /* Read data from here */ }; /* @@ -132,6 +152,7 @@ static int tclConnect( pTab->pCmd = Tcl_NewStringObj(zCmd, -1); pTab->interp = interp; + pTab->db = db; Tcl_IncrRefCount(pTab->pCmd); pScript = Tcl_DuplicateObj(pTab->pCmd); @@ -180,11 +201,29 @@ static int tclOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ */ static int tclClose(sqlite3_vtab_cursor *cur){ tcl_cursor *pCur = (tcl_cursor *)cur; - sqlite3_free(pCur); + if( pCur ){ + sqlite3_finalize(pCur->pStmt); + sqlite3_free(pCur); + } return SQLITE_OK; } -static int tclNext(sqlite3_vtab_cursor *cur){ +static int tclNext(sqlite3_vtab_cursor *pVtabCursor){ + tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor; + tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab); + if( pCsr->pStmt ){ + tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab); + int rc = sqlite3_step(pCsr->pStmt); + if( rc!=SQLITE_ROW ){ + const char *zErr; + rc = sqlite3_finalize(pCsr->pStmt); + pCsr->pStmt = 0; + if( rc!=SQLITE_OK ){ + zErr = sqlite3_errmsg(pTab->db); + pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr); + } + } + } return SQLITE_OK; } @@ -193,19 +232,104 @@ static int tclFilter( int idxNum, const char *idxStr, int argc, sqlite3_value **argv ){ - return SQLITE_OK; + tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor; + tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab); + Tcl_Interp *interp = pTab->interp; + Tcl_Obj *pScript; + Tcl_Obj *pArg; + int ii; + int rc; + + pScript = Tcl_DuplicateObj(pTab->pCmd); + Tcl_IncrRefCount(pScript); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xFilter", -1)); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(idxNum)); + if( idxStr ){ + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(idxStr, -1)); + }else{ + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("", -1)); + } + + pArg = Tcl_NewObj(); + Tcl_IncrRefCount(pArg); + for(ii=0; iibase.zErrMsg = sqlite3_mprintf("%s", zErr); + }else{ + /* Analyze the scripts return value. The return value should be a tcl + ** list object with an even number of elements. The first element of each + ** pair must be one of: + ** + ** "sql" (SQL statement to return data) + */ + Tcl_Obj *pRes = Tcl_GetObjResult(interp); + Tcl_Obj **apElem = 0; + int nElem; + rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem); + if( rc!=TCL_OK ){ + const char *zErr = Tcl_GetStringResult(interp); + rc = SQLITE_ERROR; + pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr); + }else{ + int iArgv = 1; + for(ii=0; rc==SQLITE_OK && iidb, zSql, -1, &pCsr->pStmt, 0); + if( rc!=SQLITE_OK ){ + const char *zErr = sqlite3_errmsg(pTab->db); + pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zErr); + } + }else{ + rc = SQLITE_ERROR; + pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd); + } + } + } + } + + if( rc==SQLITE_OK ){ + rc = tclNext(pVtabCursor); + } + return rc; } -static int tclColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ +static int tclColumn( + sqlite3_vtab_cursor *pVtabCursor, + sqlite3_context *ctx, + int i +){ + tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor; + sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, i+1)); return SQLITE_OK; } -static int tclRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ +static int tclRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){ + tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor; + *pRowid = sqlite3_column_int64(pCsr->pStmt, 0); return SQLITE_OK; } -static int tclEof(sqlite3_vtab_cursor *cur){ - return 1; +static int tclEof(sqlite3_vtab_cursor *pVtabCursor){ + tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor; + return (pCsr->pStmt==0); } static int tclBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ diff --git a/test/bestindex1.test b/test/bestindex1.test index 840219b620..87ec9cc56a 100644 --- a/test/bestindex1.test +++ b/test/bestindex1.test @@ -57,4 +57,100 @@ do_eqp_test 1.2 { 0 0 0 {EXECUTE LIST SUBQUERY 1} } +#------------------------------------------------------------------------- +# +reset_db +register_tcl_module db + +# Parameter $mode may be one of: +# +# "omit" - Implement filtering. Set the omit flag. +# "use" - Implement filtering. Use the constraint, but do not set omit. +# "use2" - Do not implement filtering. Use the constraint anyway. +# +# +proc t1_vtab {mode method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b)" + } + + xBestIndex { + set SQL_FILTER {SELECT * FROM t1x WHERE a='%1%'} + set SQL_SCAN {SELECT * FROM t1x} + + set clist [lindex $args 0] + set idx 0 + for {set idx 0} {$idx < [llength $clist]} {incr idx} { + array unset C + array set C [lindex $clist $idx] + if {$C(column)==0 && $C(op)=="eq" && $C(usable)} { + switch -- $mode { + "omit" { + return [list omit $idx rows 10 cost 10 idxstr $SQL_FILTER] + } + "use" { + return [list use $idx rows 10 cost 10 idxstr $SQL_FILTER] + } + "use2" { + return [list use $idx rows 10 cost 10 idxstr $SQL_SCAN] + } + default { + error "Bad mode - $mode" + } + } + } + } + + return [list idxstr {SELECT * FROM t1x}] + } + + xFilter { + set map [list %1% [lindex $args 2 0]] + set sql [string map $map [lindex $args 1]] + return [list sql $sql] + } + } + + return {} +} + +do_execsql_test 2.1 { + CREATE TABLE t1x(i INTEGER PRIMARY KEY, a, b); + INSERT INTO t1x VALUES(1, 'one', 1); + INSERT INTO t1x VALUES(2, 'two', 2); + INSERT INTO t1x VALUES(3, 'three', 3); + INSERT INTO t1x VALUES(4, 'four', 4); +} + +foreach {tn mode} { + 1 use 2 omit 3 use2 +} { + do_execsql_test 2.2.$mode.1 " + DROP TABLE IF EXISTS t1; + CREATE VIRTUAL TABLE t1 USING tcl(t1_vtab $mode); + " + + do_execsql_test 2.2.$mode.2 {SELECT * FROM t1} {one 1 two 2 three 3 four 4} + do_execsql_test 2.2.$mode.3 {SELECT rowid FROM t1} {1 2 3 4} + do_execsql_test 2.2.$mode.4 {SELECT rowid FROM t1 WHERE a='two'} {2} + + do_execsql_test 2.2.$mode.5 { + SELECT rowid FROM t1 WHERE a IN ('one', 'four') ORDER BY +rowid + } {1 4} + + set plan(use) {0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x}} + set plan(use2) {0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x}} + set plan(omit) { + 0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x WHERE a='%1%'} + 0 0 0 {EXECUTE LIST SUBQUERY 1} + } + + do_eqp_test 2.2.$mode.6 { + SELECT rowid FROM t1 WHERE a IN ('one', 'four') ORDER BY +rowid + } [concat $plan($mode) {0 0 0 {USE TEMP B-TREE FOR ORDER BY}}] +} + finish_test + +