]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add the sqlite3_whereinfo_hook() API - an experimental API replacing the
authordan <dan@noemail.net>
Tue, 4 Apr 2017 04:23:06 +0000 (04:23 +0000)
committerdan <dan@noemail.net>
Tue, 4 Apr 2017 04:23:06 +0000 (04:23 +0000)
DBCONFIG_WHEREINFO hack on this branch.

FossilOrigin-Name: a54aef35da11f7508a8888a159372036a362fc52afa1df752dc835db334c4330

manifest
manifest.uuid
src/main.c
src/shell_indexes.c
src/sqlite.h.in
src/sqliteInt.h
src/where.c

index 90f504d2aabc3995399a37ecb6ef49042b345741..fc73fb3e18c769dc26b764adaad0c6a3dcd37117 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\slatest\strunk\sinto\sthis\sbranch.
-D 2017-03-31T11:20:20.468
+C Add\sthe\ssqlite3_whereinfo_hook()\sAPI\s-\san\sexperimental\sAPI\sreplacing\sthe\nDBCONFIG_WHEREINFO\shack\son\sthis\sbranch.
+D 2017-04-04T04:23:06.108
 F Makefile.in 1cc758ce3374a32425e4d130c2fe7b026b20de5b8843243de75f087c0a2661fb
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc a4c0613a18663bda56d8cf76079ab6590a7c3602e54befb4bbdef76bcaa38b6a
@@ -36,7 +36,7 @@ F contrib/sqlitecon.tcl 210a913ad63f9f991070821e599d600bd913e0ad
 F doc/lemon.html b5a3c07d33ecb8e019ce8f7660fe2dbbad9d7977
 F doc/pager-invariants.txt 27fed9a70ddad2088750c4a2b493b63853da2710
 F doc/vfs-shm.txt e101f27ea02a8387ce46a05be2b1a902a021d37a
-F ext/README.md fd5f78013b0a2bc6f0067afb19e6ad040e89a10179b4f6f03eee58fac5f169bd w ext/README.txt
+F ext/README.md fd5f78013b0a2bc6f0067afb19e6ad040e89a10179b4f6f03eee58fac5f169bd
 F ext/async/README.txt e12275968f6fde133a80e04387d0e839b0c51f91
 F ext/async/sqlite3async.c 0f3070cc3f5ede78f2b9361fb3b629ce200d7d74
 F ext/async/sqlite3async.h f489b080af7e72aec0e1ee6f1d98ab6cf2e4dcef
@@ -366,7 +366,7 @@ F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
 F src/insert.c d4bb3a135948553d18cf992f76f7ed7b18aa0327f250607b5a6671e55d9947d5
 F src/legacy.c e88ed13c2d531decde75d42c2e35623fb9ce3cb0
 F src/loadext.c a72909474dadce771d3669bf84bf689424f6f87d471fee898589c3ef9b2acfd9
-F src/main.c 36974edf7461f708b337389c59b30ec91257420c129c1f5ee022bc2c2784d320
+F src/main.c 4b93bda0f1f916f4cb618a6fce358f7e01ab060971c586c4cea90ad0c83a83f5
 F src/malloc.c 89c98e3619d362dcffa5c1c639b364b65b474751
 F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
 F src/mem1.c c12a42539b1ba105e3707d0e628ad70e611040d8f5e38cf942cee30c867083de
@@ -403,11 +403,11 @@ F src/resolve.c 3e518b962d932a997fae373366880fc028c75706
 F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
 F src/select.c 2496d0cc6368dabe7ad2e4c7f5ed3ad9aa3b4d11cd90f33fa1d1ef72493f43aa
 F src/shell.c e524688c2544167f835ba43e24309f8707ca60c8ab6eb5c263a12c8618a233b8
-F src/shell_indexes.c 23c4b7f7bb8e9b0a8b912c389a85922939bb0769
-F src/sqlite.h.in 3db120c750634c0acc1ca291140a4cae75cb41cb0507f53c31b838c040954366
+F src/shell_indexes.c 12cc58b62492bddc88f879086b4cd1045dbfd4e52040ecb6c39b971a6aac80ba
+F src/sqlite.h.in ae5c9cbf2e77492c319fca08769575d9695e64718a16d32324944d24e291bcf7
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h 58fd0676d3111d02e62e5a35992a7d3da5d3f88753acc174f2d37b774fbbdd28
-F src/sqliteInt.h 22a0a101c51a0fae4156088f7c613b99a73d20ba6153a1400d1ae3011cf1082c
+F src/sqliteInt.h 9af1569ac748f72169b4ad0004d03ea14c75b59eb955a2c0ecca75a16ea2015f
 F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b
 F src/status.c a9e66593dfb28a9e746cba7153f84d49c1ddc4b1
 F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34
@@ -483,7 +483,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
 F src/wal.c 40c543f0a2195d1b0dc88ef12142bea690009344
 F src/wal.h 06b2a0b599cc0f53ea97f497cf8c6b758c999f71
 F src/walker.c b71a992b413b3a022572eccf29ef4b4890223791
-F src/where.c 30c790ab98b759ac3d7fa564e1321489151fac7d1587525bc5b9698394a41ecf
+F src/where.c cbd1e8562fa37de359963749e1dc3d0dd1a42a894d10a62928bffc709490fde6
 F src/whereInt.h 2d50c2b74a33be44cb68fdecee30b4d93552f1f4
 F src/wherecode.c 677e95413c472c0b413023b6b69a47f40fce1b04
 F src/whereexpr.c 130cdd1a43af71b19755270fb1224874cf55158c
@@ -1571,7 +1571,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 1e3622de8b51fdb219eb1934533a95fe6dc5158f44d358bc2efdcb53a4a9fdab c8000e94cca59dabf83d6cb75b40441aaf793d29880582dc4baa17246449b5fe
-R a9131615092ed98b863a64f66624daea
+P cb721d0b36268a7b0ef493fa4d7f6bcbaa9ead8b1990e3c3fae015fa1d545226
+R 061e66f6528edc67cfbe5cb82be7b02d
 U dan
-Z 924aec6608b5903c59e65e7507c45eb8
+Z d08fd2524fbe07ca7104a150ba1edcbe
index 1b590b1f1ffab40511cc85ce79bf3082ce57e742..64b0731eb56bc5d7915c2c8e0bb4de9c40afc6aa 100644 (file)
@@ -1 +1 @@
-cb721d0b36268a7b0ef493fa4d7f6bcbaa9ead8b1990e3c3fae015fa1d545226
\ No newline at end of file
+a54aef35da11f7508a8888a159372036a362fc52afa1df752dc835db334c4330
\ No newline at end of file
index eed82b0e66833dc72cc8d1406f378da5aadf3ddc..df715852ae15cf2be918200a86e10c089ae932bf 100644 (file)
@@ -801,13 +801,6 @@ int sqlite3_db_config(sqlite3 *db, int op, ...){
       rc = setupLookaside(db, pBuf, sz, cnt);
       break;
     }
-#ifdef SQLITE_SCHEMA_LINT
-    case SQLITE_DBCONFIG_WHEREINFO: {
-      db->xWhereInfo = va_arg(ap, void(*)(void*, int, const char*, int, i64));
-      db->pWhereInfoCtx = va_arg(ap, void*);
-      break;
-    }
-#endif
     default: {
       static const struct {
         int op;      /* The opcode */
@@ -2004,6 +1997,26 @@ void *sqlite3_preupdate_hook(
 }
 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
 
+#ifdef SQLITE_ENABLE_WHEREINFO_HOOK
+/*
+** Register a where-info hook.
+*/
+void *sqlite3_whereinfo_hook(
+  sqlite3 *db,                    /* Register callback with this db handle */
+  void (*xWhereInfo)(void*, int, const char*, int, sqlite3_uint64),
+  void *pCtx                      /* User pointer passed to callback */
+){
+  void *pRet;
+  sqlite3_mutex_enter(db->mutex);
+  pRet = db->pWhereInfoCtx;
+  db->xWhereInfo = xWhereInfo;
+  db->pWhereInfoCtx = pCtx;
+  sqlite3_mutex_leave(db->mutex);
+  return pRet;
+}
+#endif /* SQLITE_ENABLE_WHEREINFO_HOOK */
+
+
 #ifndef SQLITE_OMIT_WAL
 /*
 ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
index c5e172cb974c69560df508a85e169030ffc175c0..efdc24fac3c4e2b94c42625e2445db819d086fd0 100644 (file)
@@ -12,6 +12,7 @@
 */
 
 typedef sqlite3_int64 i64;
+typedef sqlite3_uint64 u64;
 
 typedef struct IdxConstraint IdxConstraint;
 typedef struct IdxContext IdxContext;
@@ -164,7 +165,7 @@ static void idxWhereInfo(
   int eOp, 
   const char *zVal, 
   int iVal, 
-  i64 mask
+  u64 mask
 ){
   IdxContext *p = (IdxContext*)pCtx;
 
@@ -233,32 +234,6 @@ static void idxWhereInfo(
         p->rc = sqlite3_reset(p->pInsertMask);
         break;
       }
-
-      case SQLITE_WHEREINFO_BEGINOR: {
-        IdxWhere *pNew = (IdxWhere*)idxMalloc(&p->rc, sizeof(IdxWhere));
-        if( pNew==0 ) return;
-        pNew->pParent = p->pCurrent;
-        pNew->pNextOr = p->pCurrent->pOr;
-        p->pCurrent->pOr = pNew;
-        p->pCurrent = pNew;
-        break;
-      }
-
-      case SQLITE_WHEREINFO_NEXTOR: {
-        IdxWhere *pNew = (IdxWhere*)idxMalloc(&p->rc, sizeof(IdxWhere));
-        if( pNew==0 ) return;
-        pNew->pParent = p->pCurrent->pParent;
-        assert( p->pCurrent->pSibling==0 );
-        p->pCurrent->pSibling = pNew;
-        p->pCurrent = pNew;
-        break;
-      }
-
-      case SQLITE_WHEREINFO_ENDOR: {
-        assert( p->pCurrent->pParent );
-        p->pCurrent = p->pCurrent->pParent;
-        break;
-      }
     }
   }
 }
@@ -954,9 +929,9 @@ int shellIndexesCommand(
   /* Analyze the SELECT statement in zSql. */
   if( rc==SQLITE_OK ){
     ctx.dbm = dbm;
-    sqlite3_db_config(db, SQLITE_DBCONFIG_WHEREINFO, idxWhereInfo, (void*)&ctx);
+    sqlite3_whereinfo_hook(db, idxWhereInfo, (void*)&ctx);
     rc = idxPrepareStmt(db, &pStmt, pzErrmsg, zSql);
-    sqlite3_db_config(db, SQLITE_DBCONFIG_WHEREINFO, (void*)0, (void*)0);
+    sqlite3_whereinfo_hook(db, 0, 0);
     sqlite3_finalize(pStmt);
   }
 
index f3067b64ebe2f4b3e579516a30a58279503a5701..3f9fb2fccbd24afeac59a855d610fde246254fa0 100644 (file)
@@ -2017,17 +2017,6 @@ struct sqlite3_mem_methods {
 #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
 #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
 
-#define SQLITE_DBCONFIG_WHEREINFO             1007  /* xWhereInfo void* */
-
-#define SQLITE_WHEREINFO_TABLE   1
-#define SQLITE_WHEREINFO_EQUALS  2
-#define SQLITE_WHEREINFO_RANGE   3
-#define SQLITE_WHEREINFO_ORDERBY 4
-#define SQLITE_WHEREINFO_BEGINOR 5
-#define SQLITE_WHEREINFO_ENDOR   6
-#define SQLITE_WHEREINFO_NEXTOR  7
-
-
 /*
 ** CAPI3REF: Enable Or Disable Extended Result Codes
 ** METHOD: sqlite3
@@ -8496,6 +8485,147 @@ SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
 */
 SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
 
+/*
+** This function is used to register a whereinfo hook with the database
+** handle passed as the first argument. Once registered, the whereinfo hook
+** is invoked zero or more times while preparing a query to provide
+** information to the application. It is intended to be used by expert 
+** systems to recommend indexes that could be added to the database in order
+** to improve query response time.
+**
+** An SQLite query plan describes the way data is read from zero or more
+** database tables. For each table read, the data required may be
+** constrained by equality or range constraints on one or more columns, 
+** and it may or may not be required in order sorted by one or more 
+** columns. For example, the following query:
+**
+** <pre>
+**     SELECT * FROM t1 WHERE t1.a = ? ORDER BY t1.b;
+** </pre>
+**
+** reads data from table t1. It requires only those rows for which t1.a
+** is set to a specific value, and requires them sorted in order of
+** column t1.b. Internally, SQLite determines this and attempts to locate
+** an index that can be used to efficiently find the required subset of
+** rows and/or allows the rows to be read from the database in the 
+** required order. In this case, ideally an index of the form:
+**
+** <pre>
+**     CREATE INDEX i1 ON t1(a, b);
+** </pre>
+**
+** The data passed to the whereinfo hook during query preparation do
+** not describe the actual query plan to the application. Instead, it
+** describes the parts of the query that SQLite could use an index to
+** optimize if a suitable index existed. In this case, that only
+** rows with t1.a=? are required, and that they are required sorted
+** in order by t1.b.
+**
+** Each time the whereinfo hook is invoked, the first argument is a
+** copy of the (void*) pointer passed as the second argument to this
+** API function. The second is always one of the SQLITE_WHEREINFO_XXX
+** constants defined below.
+**
+** For each table read by a query, the whereinfo hook is invoked as follows:
+**
+** <ul>
+**   <li> Once with SQLITE_WHEREINFO_TABLE as the second argument. This
+**        indicates the table that subsequent callbacks (until the next
+**        SQLITE_WHEREINFO_TABLE) apply to.
+**
+**   <li> If SQLite requires rows in a specific order, once with
+**        SQLITE_WHEREINFO_ORDERBY for each column of the table that is
+**        one of the sort keys.
+**
+**   <li> If there are any "<col> = ?" constraints that restrict the rows
+**        required by SQLite, once with SQLITE_WHEREINFO_EQUALS for each
+**        such constraint.
+**
+**   <li> If there are any "<col> > ?" constraints (or any other range
+**        constraint) that restrict the rows required by SQLite, once with 
+**        SQLITE_WHEREINFO_RANGE for each such constraint.
+** </ul>
+**
+** The third, fourth and fifth arguments passed to the whereinfo callback
+** are interpreted differently, depending on the SQLITE_WHEREINFO_XXX value
+** as follows:
+**
+** <dl>
+**   <dt> SQLITE_WHEREINFO_TABLE
+**   <dd> The third argument passed in this case is the name of the table.
+**        The fourth is the index of the database in which the table is
+**        located (0 for "main", 1 for "temp" or higher for an attached
+**        database). The fifth argument is a bitmask that indicates which
+**        of the tables columns may be required by the query. If the leftmost
+**        column of the table is used in some way, bit 0 of the bitmask is
+**        set. If the next-to-leftmost is used, bit 1 etc. Bit 63 is used to
+**        represent all columns with an index of 63 or higher. If bit 63
+**        is set, the application should assume that the query requires all
+**        columns from the table with an index of 63 or greater.
+**
+**   <dt> SQLITE_WHEREINFO_ORDERBY
+**   <dd> The third argument passed in this case is the name of the collation
+**        sequence to sort by. The fourth is the index of the table column to
+**        sort by (0 for the leftmost column, 1 for the next-to-leftmost
+**        etc.). The fifth argument is a boolean flag - true for a DESC sort
+**        or false for ASC.
+**
+**   <dt> SQLITE_WHEREINFO_EQUALS
+**   <dd> The third argument passed in this case is the name of the collation
+**        sequence used by the constraint. The fourth is the index of the 
+**        table column in the constraint. If the current table is not part
+**        of a join, then the value passed as the fifth argument is always
+**        zero. Or, if it is part of a join, then the fifth parameter passed
+**        to this callback is a mask of other tables that the current
+**        constraint depends on. For example, in the query:
+**
+**   <pre>
+**      SELECT * FROM t1, t2 WHERE t1.a = (t2.b+1);
+**   </pre>
+**
+**        the fifth parameter passed to the the SQLITE_WHEREINFO_EQUALS
+**        whereinfo callback would have the bit assigned to table "t2"
+**        set to true. There is no way for the application to determine 
+**        the specific bit in the mask assigned to any table, but the bit
+**        assignments are consistent while parsing a single query.
+**
+**   <dt> SQLITE_WHEREINFO_RANGE
+**   <dd> As for SQLITE_WHEREINFO_EQUALS.
+** </dl>
+**
+** Note that if a WHERE clause includes an OR expression, then there may be
+** more than one set of callbacks for a single table. For example, the
+** following SQL:
+**
+** <pre>
+**    SELECT * FROM t1 WHERE t1.a=? OR t1.b=?
+** </pre>
+**
+** Provokes the same callbacks as the following two queries executed in
+** series.
+**
+** <pre>
+**    SELECT * FROM t1 WHERE t1.a=?;
+**    SELECT * FROM t1 WHERE t1.b=?;
+** </pre>
+*/
+SQLITE_EXPERIMENTAL void *sqlite3_whereinfo_hook(
+  sqlite3 *db,                    /* Register callback with this db handle */
+  void (*xWhereInfo)(
+    void*,                        /* Copy of pCtx */
+    int,                          /* SQLITE_WHEREINFO_XXX constant */
+    const char*,
+    int,
+    sqlite3_uint64
+  ),
+  void *pCtx                      /* User pointer passed to callback */
+);
+
+#define SQLITE_WHEREINFO_TABLE   1
+#define SQLITE_WHEREINFO_EQUALS  2
+#define SQLITE_WHEREINFO_RANGE   3
+#define SQLITE_WHEREINFO_ORDERBY 4
+
 /*
 ** Undo the hack that converts floating point types to integer for
 ** builds on processors without floating point support.
index 0f6ffd5175ad17d9f25d0a23f2f9b10a6b4ebf57..c5f5ccf0da0bc10b0d6371508b0cc3c739be7766 100644 (file)
@@ -1431,8 +1431,8 @@ struct sqlite3 {
 #ifdef SQLITE_USER_AUTHENTICATION
   sqlite3_userauth auth;        /* User authentication information */
 #endif
-#ifdef SQLITE_SCHEMA_LINT
-  void (*xWhereInfo)(void*, int, const char*, int, i64);
+#ifdef SQLITE_ENABLE_WHEREINFO_HOOK
+  void (*xWhereInfo)(void*, int, const char*, int, u64);
   void *pWhereInfoCtx;
 #endif
 };
index c7e4ba200ac36bb4d8b9bd772c66c645520569a2..ba2ccfe65e178480e2a0fa93ca3f8092663c4be9 100644 (file)
@@ -3277,7 +3277,7 @@ static int whereLoopAddOr(
   WhereLoopBuilder sSubBuild;
   WhereOrSet sSum, sCur;
   struct SrcList_item *pItem;
-  
+
   pWC = pBuilder->pWC;
   pWCEnd = pWC->a + pWC->nTerm;
   pNew = pBuilder->pNew;
@@ -3294,7 +3294,7 @@ static int whereLoopAddOr(
       WhereTerm *pOrTerm;
       int once = 1;
       int i, j;
-    
+
       sSubBuild = *pBuilder;
       sSubBuild.pOrderBy = 0;
       sSubBuild.pOrSet = &sCur;
@@ -4277,18 +4277,16 @@ static int whereShortCut(WhereLoopBuilder *pBuilder){
   return 0;
 }
 
-#ifdef SQLITE_SCHEMA_LINT
+#ifdef SQLITE_ENABLE_WHEREINFO_HOOK
 static void whereTraceWC(
   Parse *pParse, 
   struct SrcList_item *pItem,
-  WhereClause *pWC,
-  int bOr
+  WhereClause *pWC
 ){
   sqlite3 *db = pParse->db;
   Table *pTab = pItem->pTab;
   void (*x)(void*, int, const char*, int, i64) = db->xWhereInfo;
   void *pCtx = db->pWhereInfoCtx;
-  int bFirst = 1;                 /* True until first callback is made */
   int ii;
 
   /* Issue callbacks for WO_SINGLE constraints */
@@ -4308,30 +4306,15 @@ static void whereTraceWC(
       }else{
         eOp = SQLITE_WHEREINFO_RANGE;
       }
-      if( bOr && !bFirst ) x(pCtx, SQLITE_WHEREINFO_NEXTOR, 0, 0, 0);
       x(pCtx, eOp, (pC ? pC->zName : "BINARY"), ii, pTerm->prereqRight);
-      bFirst = 0;
-    }
-  }
-
-  /* Callbacks for composite - (WO_OR|WO_AND) - constraints */
-  for(ii=0; ii<pWC->nTerm; ii++){
-    WhereTerm *pTerm = &pWC->a[ii];
-    if( pTerm->eOperator & WO_OR ){
-      assert( bOr==0 );
-      x(pCtx, SQLITE_WHEREINFO_BEGINOR, 0, 0, 0);
-      whereTraceWC(pParse, pItem, &pTerm->u.pOrInfo->wc, 1);
-      x(pCtx, SQLITE_WHEREINFO_ENDOR, 0, 0, 0);
-    }
-    if( pTerm->eOperator & WO_AND ){
-      if( bOr && !bFirst ) x(pCtx, SQLITE_WHEREINFO_NEXTOR, 0, 0, 0);
-      whereTraceWC(pParse, pItem, &pTerm->u.pAndInfo->wc, 0);
-      bFirst = 0;
     }
   }
 }
 
-
+/*
+** If there is a where-info hook attached to the database handle, issue all
+** required callbacks for the current sqlite3WhereBegin() call.
+*/
 static void whereTraceBuilder(
   Parse *pParse,
   WhereLoopBuilder *p
@@ -4358,7 +4341,6 @@ static void whereTraceBuilder(
         /* ORDER BY callbacks */
         if( p->pOrderBy ){
           int i;
-          int bFirst = 1;
           for(i=0; i<p->pOrderBy->nExpr; i++){
             Expr *pExpr = p->pOrderBy->a[i].pExpr; 
             CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
@@ -4374,7 +4356,7 @@ static void whereTraceBuilder(
         }
 
         /* WHERE callbacks */
-        whereTraceWC(pParse, pItem, p->pWC, 0);
+        whereTraceWC(pParse, pItem, p->pWC);
       }
     }
   }
@@ -4654,7 +4636,7 @@ WhereInfo *sqlite3WhereBegin(
   }
 #endif
 
-  /* Schema-lint xTrace callback */
+  /* Invoke the where-info hook, if one has been registered. */
   whereTraceBuilder(pParse, &sWLB);
 
   if( nTabList!=1 || whereShortCut(&sWLB)==0 ){