]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Merge in the Expr.flags expansion to 32-bits. Use an extra bit to help
authordrh <drh@noemail.net>
Thu, 12 Sep 2013 17:29:25 +0000 (17:29 +0000)
committerdrh <drh@noemail.net>
Thu, 12 Sep 2013 17:29:25 +0000 (17:29 +0000)
optimize the sqlite3ExprSkipCollate() routine.

FossilOrigin-Name: 4c84d1b4c20f18921dd705bf67e8225975b83e86

1  2 
manifest
manifest.uuid
src/expr.c
src/resolve.c
src/sqliteInt.h
src/where.c

diff --cc manifest
index 8e7446d805f65422475ba64e6f9420fb7dc2ae39,16e249cf48d633daaafe7ae495135d64a65c3670..dbb1271f7ce1ba0cabf5440dc0e45dac8f6f943c
+++ b/manifest
@@@ -1,5 -1,5 +1,5 @@@
- C Improvements\sto\slikelihood\sprocessing\sso\sthat\scommuting\san\sunindexed\sterm\sin\nthe\sWHERE\sclause\sdoes\snot\schange\sthe\squery\splan.
- D 2013-09-11T17:39:09.434
 -C Increase\sthe\snumber\sof\sbits\savailable\sin\sExpr.flags.\s\sOther\stweaks\saimed\sat\nmaking\sexpression\sprocessing\smore\srobust.
 -D 2013-09-12T16:50:49.178
++C Merge\sin\sthe\sExpr.flags\sexpansion\sto\s32-bits.\s\sUse\san\sextra\sbit\sto\shelp\noptimize\sthe\ssqlite3ExprSkipCollate()\sroutine.
++D 2013-09-12T17:29:25.629
  F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
  F Makefile.in 5e41da95d92656a5004b03d3576e8b226858a28e
  F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@@ -172,10 -172,10 +172,10 @@@ F src/complete.c dc1d136c0feee03c2f7550
  F src/ctime.c ea4b7f3623a0fcb1146e7f245d7410033e86859c
  F src/date.c 067a81c9942c497aafd2c260e13add8a7d0c7dd4
  F src/delete.c 2dc64ca360b7d7da481183ea920a813a0c203c97
- F src/expr.c d0ed048b3b3c97fddbcccb8df43cc39b89e682c2
 -F src/expr.c 7f93e51fd7ca0d3ea1be2715a29c86a69e7b1e93
++F src/expr.c 3104c01672d0b16d50c3a21e8db312bee64b68e8
  F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
  F src/fkey.c be866cd8c4fa6cae98ba33109578fd1a3311ee5b
 -F src/func.c 7650d35651bb0ca903d24f4e5e944bfdaac9f152
 +F src/func.c 0aca17c8bc750fad4856e6098ed5e2597b641a75
  F src/global.c 5caf4deab621abb45b4c607aad1bd21c20aac759
  F src/hash.c ac3470bbf1ca4ae4e306a8ecb0fdf1731810ffe4
  F src/hash.h 8890a25af81fb85a9ad7790d32eedab4b994da22
@@@ -214,14 -214,14 +214,14 @@@ F src/pragma.c 3aa3d8c8623b7b71c5b1bfb7
  F src/prepare.c fa6988589f39af8504a61731614cd4f6ae71554f
  F src/printf.c da9119eb31a187a4b99f60aa4a225141c0ebb74b
  F src/random.c 0b2dbc37fdfbfa6bd455b091dfcef5bdb32dba68
- F src/resolve.c 0e5b31ef428a76782c6ea8244d820f94f55c8110
 -F src/resolve.c d336be12493bb376c6756c214379c727cd8c01a8
++F src/resolve.c 062de789002f1ec5ccc35f7412eb6b4528421e66
  F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0
- F src/select.c 9239586282bd146ec5843a2cde7d54cd7816cf78
+ F src/select.c d96bcdbc2e7de89cb72febeccd232746f67839fd
  F src/shell.c d920a891ca09b8bd262cced7fb0ab9d723f7a747
  F src/sqlite.h.in ec40aa958a270416fb04b4f72210357bf163d2c5
  F src/sqlite3.rc fea433eb0a59f4c9393c8e6d76a6e2596b1fe0c0
  F src/sqlite3ext.h 886f5a34de171002ad46fae8c36a7d8051c190fc
- F src/sqliteInt.h ac21b3aeb0251a4096a21a0750277f5701adfe0c
 -F src/sqliteInt.h e4cb0c8700ca77889570bf30cf75a5c56f297615
++F src/sqliteInt.h 5d368cb028e3c0e329fd5e8c41959a4310dff201
  F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
  F src/status.c 7ac05a5c7017d0b9f0b4bcd701228b784f987158
  F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
@@@ -289,8 -289,8 +289,8 @@@ F src/vdbetrace.c e7ec40e1999ff3c641442
  F src/vtab.c 5a423b042eb1402ef77697d03d6a67378d97bc8d
  F src/wal.c 7dc3966ef98b74422267e7e6e46e07ff6c6eb1b4
  F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
- F src/walker.c 4fa43583d0a84b48f93b1e88f11adf2065be4e73
- F src/where.c 47a9d554aa12e37d00c87ed6557edff3f96d1980
+ F src/walker.c e9e593d5bb798c3e67fc3893dfe7055c9e7d8d74
 -F src/where.c b5d59b899b85aa03800905ecdc0a17565d51a6ab
++F src/where.c d2f58a441ba8709a99df6654b26a50b7ca7dada4
  F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
  F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
  F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6
@@@ -1112,7 -1112,10 +1113,7 @@@ F tool/warnings-clang.sh f6aa929dc20ef1
  F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
  F tool/wherecosttest.c f407dc4c79786982a475261866a161cd007947ae
  F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
- P 5d00cce74a7aefaf30022ae971ab1e0451e0ad6e
- R aedc9ce3e88fcb8ddfac4258d6222c86
 -P 75a8a8c1b39725d36db627536d0c69401f8e0815
 -R 14e40117cf5ed4a868935b4adf8b8b0d
 -T *branch * expr-tuning
 -T *sym-expr-tuning *
 -T -sym-trunk *
++P 6e6bded055cdbc902731687c86d92c39a3ba5904 579a512538528cf4bb4381ba393c5d9e7310086b
++R dc2c889ddc263fd768cedb826f211ae2
  U drh
- Z 4fd6b6c258c46144822c202b8357c6fb
 -Z e578fc31598c846285e1a40bbc280fc4
++Z cf223babd50a8b4daf3af8d47e55c943
diff --cc manifest.uuid
index b142052f40c1a424fae4719fe570642a87961003,5ea118d1db200e331952a611d2e64aa30e85655b..3cd12f684cca45d666ad62cdd4b5c7f8024402bd
@@@ -1,1 -1,1 +1,1 @@@
- 6e6bded055cdbc902731687c86d92c39a3ba5904
 -579a512538528cf4bb4381ba393c5d9e7310086b
++4c84d1b4c20f18921dd705bf67e8225975b83e86
diff --cc src/expr.c
index ece02938407480472f3862d6f9935b5736029b3d,82623b14ba1625677f1c5aab690c8f17d9eac35f..e5d8a61299801d3152e86cfcbe682df492fc196e
@@@ -70,7 -70,7 +70,7 @@@ Expr *sqlite3ExprAddCollateToken(Parse 
      Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
      if( pNew ){
        pNew->pLeft = pExpr;
--      pNew->flags |= EP_Collate;
++      pNew->flags |= EP_Collate|EP_Skip;
        pExpr = pNew;
      }
    }
@@@ -85,21 -85,13 +85,21 @@@ Expr *sqlite3ExprAddCollateString(Pars
  }
  
  /*
--** Skip over any TK_COLLATE and/or TK_AS operators at the root of
--** an expression.
++** Skip over any TK_COLLATE or TK_AS operators and any unlikely()
++** or likelihood() function at the root of an expression.
  */
  Expr *sqlite3ExprSkipCollate(Expr *pExpr){
-   while( pExpr ){
-     if( pExpr->op==TK_COLLATE || pExpr->op==TK_AS ){
-       pExpr = pExpr->pLeft;
-     }else if( ExprHasAnyProperty(pExpr, EP_Hint) ){
 -  while( pExpr && (pExpr->op==TK_COLLATE || pExpr->op==TK_AS) ){
 -    pExpr = pExpr->pLeft;
 -  }
++  while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
++    if( ExprHasProperty(pExpr, EP_Unlikely) ){
 +      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 +      assert( pExpr->x.pList->nExpr>0 );
++      assert( pExpr->op==TK_FUNCTION );
 +      pExpr = pExpr->x.pList->a[0].pExpr;
 +    }else{
-       break;
++      assert( pExpr->op==TK_COLLATE || pExpr->op==TK_AS );
++      pExpr = pExpr->pLeft;
 +    }
-   }
++  }   
    return pExpr;
  }
  
@@@ -2324,6 -2315,6 +2323,16 @@@ static int usedAsColumnCache(Parse *pPa
  }
  #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
  
++/*
++** Convert an expression node to a TK_REGISTER
++*/
++static void exprToRegister(Expr *p, int iReg){
++  p->op2 = p->op;
++  p->op = TK_REGISTER;
++  p->iTable = iReg;
++  ExprClearProperty(p, EP_Skip);
++}
++
  /*
  ** Generate code into the current Vdbe to evaluate the given
  ** expression.  Attempt to store the results in register "target".
@@@ -2895,9 -2877,9 +2903,8 @@@ int sqlite3ExprCodeTarget(Parse *pParse
          cacheX = *pX;
          testcase( pX->op==TK_COLUMN );
          testcase( pX->op==TK_REGISTER );
--        cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
++        exprToRegister(&cacheX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
          testcase( regFree1==0 );
--        cacheX.op = TK_REGISTER;
          opCompare.op = TK_EQ;
          opCompare.pLeft = &cacheX;
          pTest = &opCompare;
@@@ -3039,9 -3021,9 +3046,7 @@@ int sqlite3ExprCodeAndCache(Parse *pPar
      int iMem;
      iMem = ++pParse->nMem;
      sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
--    pExpr->iTable = iMem;
--    pExpr->op2 = pExpr->op;
--    pExpr->op = TK_REGISTER;
++    exprToRegister(pExpr, iMem);
    }
    return inReg;
  }
@@@ -3420,9 -3402,9 +3425,7 @@@ static int evalConstExpr(Walker *pWalke
      ** but suboptimal, so we want to know about the situation to fix it.
      ** Hence the following assert: */
      assert( r2==r1 );
--    pExpr->op2 = pExpr->op;
--    pExpr->op = TK_REGISTER;
--    pExpr->iTable = r2;
++    exprToRegister(pExpr, r2);
      return WRC_Prune;
    }
    return WRC_Continue;
@@@ -3520,9 -3502,9 +3523,7 @@@ static void exprCodeBetween
    compRight.op = TK_LE;
    compRight.pLeft = &exprX;
    compRight.pRight = pExpr->x.pList->a[1].pExpr;
--  exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
--  exprX.op2 = exprX.op;
--  exprX.op = TK_REGISTER;
++  exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
    if( jumpIfTrue ){
      sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
    }else{
diff --cc src/resolve.c
index 0abd61d39974b058d5906fe76bfadea078ac6ab0,54ce3adf54bb62c2b0239f8fc1587960941a1470..c1e8cb7987a410f3a5840298e6c9090d65c84de1
@@@ -107,6 -107,6 +107,7 @@@ static void resolveAlias
      incrAggFunctionDepth(pDup, nSubquery);
      pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
      if( pDup==0 ) return;
++    ExprSetProperty(pDup, EP_Skip);
      if( pEList->a[iCol].iAlias==0 ){
        pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
      }
@@@ -696,19 -683,6 +697,19 @@@ static int resolveExprStep(Walker *pWal
          }
        }else{
          is_agg = pDef->xFunc==0;
-           ExprSetProperty(pExpr, EP_Hint);
 +        if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
++          ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
 +          if( n==2 ){
 +            pExpr->iTable = exprProbability(pList->a[1].pExpr);
 +            if( pExpr->iTable<0 ){
 +              sqlite3ErrorMsg(pParse, "second argument to likelihood() must be a "
 +                                      "constant between 0.0 and 1.0");
 +              pNC->nErr++;
 +            }
 +          }else{
 +            pExpr->iTable = 62;  /* TUNING:  Default 2nd arg to unlikely() is 0.0625 */
 +          }             
 +        }
        }
  #ifndef SQLITE_OMIT_AUTHORIZATION
        if( pDef ){
diff --cc src/sqliteInt.h
index 268fef36fee2c491ae57b0d56861d44c5f8bace4,75856136214ae8aca31ffc43b7f5a11169590dd7..3612240844860bc0b297e298009899d494ec87b8
@@@ -1753,8 -1750,7 +1751,8 @@@ struct Expr 
  #endif
    int iTable;            /* TK_COLUMN: cursor number of table holding column
                           ** TK_REGISTER: register number
 -                         ** TK_TRIGGER: 1 -> new, 0 -> old */
 +                         ** TK_TRIGGER: 1 -> new, 0 -> old
-                          ** EP_Hint:  1000 times likelihood */
++                         ** EP_Unlikely:  1000 times likelihood */
    ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
                           ** TK_VARIABLE: variable number (always >= 1). */
    i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  /*
  ** The following are the meanings of bits in the Expr.flags field.
  */
- #define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
- #define EP_Agg        0x0002  /* Contains one or more aggregate functions */
- #define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
- #define EP_Error      0x0008  /* Expression contains one or more errors */
- #define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
- #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
- #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
- #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
- #define EP_Collate    0x0100  /* Tree contains a TK_COLLATE opeartor */
- #define EP_FixedDest  0x0200  /* Result needed in a specific register */
- #define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
- #define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
- #define EP_Hint       0x1000  /* The UNLIKELY() SQL function */
- #define EP_Reduced    0x2000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
- #define EP_TokenOnly  0x4000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
- #define EP_Static     0x8000  /* Held in memory not obtained from malloc() */
- /*
- ** The following are the meanings of bits in the Expr.flags2 field.
- */
- #define EP2_MallocedToken  0x0001  /* Need to sqlite3DbFree() Expr.zToken */
- #define EP2_Irreducible    0x0002  /* Cannot EXPRDUP_REDUCE this Expr */
+ #define EP_FromJoin  0x000001 /* Originated in ON or USING clause of a join */
+ #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
+ #define EP_Resolved  0x000004 /* IDs have been resolved to COLUMNs */
+ #define EP_Error     0x000008 /* Expression contains one or more errors */
+ #define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
+ #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
+ #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
+ #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
+ #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE opeartor */
+ #define EP_FixedDest 0x000200 /* Result needed in a specific register */
+ #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
+ #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
 -#define EP_Hint      0x001000 /* Not used */
++#define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
+ #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
+ #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
+ #define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
+ #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
+ #define EP_Irreduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
++#define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
  
  /*
  ** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
diff --cc src/where.c
index 86863451fb938b114613e5960e7a22124f54697a,fb097d2af40ec84a3374c8fec4c1efedd0b5fb58..b8ec788bc31ecbc313d70a27ae4140406abecddf
@@@ -689,11 -684,6 +689,11 @@@ static int whereClauseInsert(WhereClaus
      pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
    }
    pTerm = &pWC->a[idx = pWC->nTerm++];
-   if( p && ExprHasAnyProperty(p, EP_Hint) ){
++  if( p && ExprHasProperty(p, EP_Unlikely) ){
 +    pTerm->truthProb = whereCost(p->iTable) - 99;
 +  }else{
 +    pTerm->truthProb = -1;
 +  }
    pTerm->pExpr = sqlite3ExprSkipCollate(p);
    pTerm->wtFlags = wtFlags;
    pTerm->pWC = pWC;