]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Update the NGQP to record which loops need be run in reverse order to
authordrh <drh@noemail.net>
Mon, 27 May 2013 17:59:37 +0000 (17:59 +0000)
committerdrh <drh@noemail.net>
Mon, 27 May 2013 17:59:37 +0000 (17:59 +0000)
satisfy ORDER BY clauses.

FossilOrigin-Name: 211f7a5374fe20a02535edc8b799a8a7136ff6b3

manifest
manifest.uuid
src/sqliteInt.h
src/where.c

index 010739cba8564bf095f4395c5af49f1ff0fd7e8b..8b6a0ac134a403180e832d25ab48ae14a197dea6 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Record\sin\sthe\sWhereLoop\sobject\sthe\sset\sof\svirtual\stable\sconstraints\sthat\nneed\snot\sbe\sseparately\schecked.
-D 2013-05-24T14:52:03.958
+C Update\sthe\sNGQP\sto\srecord\swhich\sloops\sneed\sbe\srun\sin\sreverse\sorder\sto\nsatisfy\sORDER\sBY\sclauses.
+D 2013-05-27T17:59:37.917
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in f6b58b7bdf6535f0f0620c486dd59aa4662c0b4f
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -196,7 +196,7 @@ F src/shell.c 9a18124ff209ca308d786c99a466e8e270193ff3
 F src/sqlite.h.in 5a5a22a9b192d81a9e5dee00274e3a0484c4afb1
 F src/sqlite3.rc fea433eb0a59f4c9393c8e6d76a6e2596b1fe0c0
 F src/sqlite3ext.h d936f797812c28b81b26ed18345baf8db28a21a5
-F src/sqliteInt.h 0b0ba16c1449d0e7ef6ff78039ffef38f58f585a
+F src/sqliteInt.h 9e12a56e05420b5c7aa40aca4d44ee4d9535b68a
 F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
 F src/status.c bedc37ec1a6bb9399944024d63f4c769971955a9
 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
@@ -265,7 +265,7 @@ F src/vtab.c b05e5f1f4902461ba9f5fc49bb7eb7c3a0741a83
 F src/wal.c 436bfceb141b9423c45119e68e444358ee0ed35d
 F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
 F src/walker.c 4fa43583d0a84b48f93b1e88f11adf2065be4e73
-F src/where.c 2fd11c00ffc37a65b762c8b2bbf5710723eefdd7
+F src/where.c 3f4f25df651e8cea5b02d94aeee28838e9c5e2c3
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
 F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6
@@ -1066,7 +1066,7 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
-P 7c8f992c04504a132686ad0652efdff19e43480d
-R 91429a505370ccdd08f4d1fe64f3bf63
+P b49fa74561f38c945be6149693678fd6518c2de4
+R dbfb376dc6b826c13c37ea187ec4a9c4
 U drh
-Z 676fd5c7993f5cc12a1abed75be6a4c4
+Z 716f8aec8e11bc86fa358274cd806e0b
index bec1afe37d7e40e9558d3523d302330a17295a22..a1b72955fd3295cb80c144c487078d5140fabfc5 100644 (file)
@@ -1 +1 @@
-b49fa74561f38c945be6149693678fd6518c2de4
\ No newline at end of file
+211f7a5374fe20a02535edc8b799a8a7136ff6b3
\ No newline at end of file
index c1a4f766c88b96e600e2aafffc50c9bbfdf53905..79f5d23492f7690f1151b21af7af187c0e57e677 100644 (file)
@@ -2051,6 +2051,7 @@ struct WhereInfo {
   SrcList *pTabList;        /* List of tables in the join */
   ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
   ExprList *pDistinct;      /* DISTINCT ON values, or NULL */
+  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
   u16 nOBSat;               /* Number of ORDER BY terms satisfied by indices */
   u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
   u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
index 6416dd95030d72b76e21312aad21621ef05f4740..b0b953af30825b5db76962cd424f0dffdb20aba1 100644 (file)
@@ -5839,8 +5839,9 @@ static int wherePathSatisfiesOrderBy(
   WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
   Bitmask *pRevMask     /* Mask of WhereLoops to run in reverse order */
 ){
-  u8 revSet;
-  u8 rev;
+  u8 revSet;            /* True if rev is known */
+  u8 rev;               /* Composite sort order */
+  u8 revIdx;            /* Index sort order */
   u8 isUnique;
   u8 requireUnique = 0;
   u16 nColumn;
@@ -5890,7 +5891,7 @@ static int wherePathSatisfiesOrderBy(
   ** column reference */
   nOrderBy = pOrderBy->nExpr;
   for(i=0; i<nOrderBy; i++){
-    pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[nUsed].pExpr);
+    pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
     if( pOBExpr->op!=TK_COLUMN ) return 0;
   }
     
@@ -5902,7 +5903,7 @@ static int wherePathSatisfiesOrderBy(
       if( (pLoop->wsFlags & WHERE_COLUMN_IN)!=0 ) isUnique = 0;
       if( pLoop->u.btree.nEq!=1 ) isUnique = 0;
       pIndex = 0;
-      nColumn = 1;
+      nColumn = 0;
     }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
       return 0;
     }else{
@@ -5921,31 +5922,31 @@ static int wherePathSatisfiesOrderBy(
     iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
     j = 0;
     revSet = rev = 0;
-    for(j=0; j<nColumn && nUsed<nOrderBy; j++, nUsed++){
+    for(j=0; j<=nColumn && nUsed<nOrderBy; j++, nUsed++){
       pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[nUsed].pExpr);
       assert( pOBExpr->op==TK_COLUMN );
       if( pOBExpr->iTable!=iCur ) break;
-      if( pIndex==0 ){
-        if( pOBExpr->iColumn<0 && j==0 ){
-          isUnique = 1;
-          rev = pOrderBy->a[nUsed].sortOrder;
-        }else if( isUnique ){
-          continue;
-        }else{
-          return 0;
-        }
-      }
       if( isUnique ) continue;
-      iColumn = pIndex->aiColumn[j];
-      if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
+      if( j<nColumn ){
+        /* Normal index columns */
+        iColumn = pIndex->aiColumn[j];
+        revIdx = pIndex->aSortOrder[j];
+        if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
+      }else{
+        /* The ROWID column at the end */
+        iColumn = -1;
+        revIdx = 0;
+      }
       if( pOBExpr->iColumn!=iColumn ) return 0;
-      pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[nUsed].pExpr);
-      if( !pColl ) pColl = db->pDfltColl;
-      if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) return 0;
+      if( iColumn>=0 ){
+        pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[nUsed].pExpr);
+        if( !pColl ) pColl = db->pDfltColl;
+        if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) return 0;
+      }
       if( revSet ){
-        if( pIndex->aSortOrder[j]!=rev ) return 0;
+        if( (rev ^ revIdx)!=pOrderBy->a[nUsed].sortOrder ) return 0;
       }else{
-        rev = pIndex->aSortOrder[j];
+        rev = revIdx ^ pOrderBy->a[nUsed].sortOrder;
         revSet = 1;
       }
     }
@@ -6000,6 +6001,9 @@ static int wherePathSolver(WhereInfo *pWInfo, double nRowEst){
   db = pWInfo->pParse->db;
   nLoop = pWInfo->nLevel;
   assert( nLoop<=pWInfo->pTabList->nSrc );
+#ifdef WHERETRACE_ENABLED
+  if( sqlite3WhereTrace>=2 ) sqlite3DebugPrintf("---- begin solver\n");
+#endif
 
   /* Allocate and initialize space for aTo and aFrom */
   ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
@@ -6020,28 +6024,14 @@ static int wherePathSolver(WhereInfo *pWInfo, double nRowEst){
   /* Precompute the cost of sorting the final result set, if the caller
   ** to sqlite3WhereBegin() was concerned about sorting */
   rSortCost = (double)0;
-  if( pWInfo->pOrderBy==0 || nRowEst<0.0 ){
+  if( pWInfo->pOrderBy==0 || nRowEst<=0.0 ){
     aFrom[0].isOrderedValid = 1;
   }else{
     /* Compute an estimate on the cost to sort the entire result set */
-#if 0
-    rSortCost = (double)1;
-    for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pNext){
-      pNext = pWLoop->pNextLoop;
-      rCost = pWLoop->nOut;
-      while( pNext && pNext->iTab==pWLoop->iTab ){
-        if( pNext->nOut<rCost ) rCost = pNext->nOut;
-        pNext = pNext->pNextLoop;
-      }
-      rSortCost *= rCost;
-    }
-    rSortCost *= estLog(rSortCost);
-#else
     rSortCost = nRowEst*estLog(nRowEst);
-#endif
 #ifdef WHERETRACE_ENABLED
     if( sqlite3WhereTrace>=2 ){
-      sqlite3DebugPrintf("--solver sort cost=%-7.2g\n", rSortCost);
+      sqlite3DebugPrintf("---- sort cost=%-7.2g\n", rSortCost);
     }
 #endif
   }
@@ -6162,9 +6152,14 @@ static int wherePathSolver(WhereInfo *pWInfo, double nRowEst){
     if( sqlite3WhereTrace>=2 ){
       sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
       for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
-        sqlite3DebugPrintf(" %s cost=%-7.2g nrow=%-7.2g order=%c\n",
+        sqlite3DebugPrintf(" %s cost=%-7.2g nrow=%-7.2g order=%c",
            wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
            pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
+        if( pTo->isOrderedValid && pTo->isOrdered ){
+          sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
+        }else{
+          sqlite3DebugPrintf("\n");
+        }
       }
     }
 #endif
@@ -6192,6 +6187,7 @@ static int wherePathSolver(WhereInfo *pWInfo, double nRowEst){
   }
   if( pFrom->isOrdered ){
     pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
+    pWInfo->revMask = pFrom->revLoop;
   }
   pWInfo->nRowOut = pFrom->nRow;
 
@@ -6468,9 +6464,9 @@ WhereInfo *sqlite3WhereBegin(
 #ifdef WHERETRACE_ENABLED
   if( sqlite3WhereTrace ){
     int ii;
-    sqlite3DebugPrintf("------------ Solution -------------");
+    sqlite3DebugPrintf("---- Solution");
     if( pWInfo->nOBSat ){
-      sqlite3DebugPrintf(" ORDER BY omitted\n");
+      sqlite3DebugPrintf(" ORDER BY omitted rev=0x%llx\n", pWInfo->revMask);
     }else{
       sqlite3DebugPrintf("\n");
     }