/ Check-in [7e10e60b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Improvements to comments. No code changes. (CVS 618)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:7e10e60bb78b680bb822908a0b7763f8f5e3ee5a
User & Date: drh 2002-06-14 20:58:45
Context
2002-06-14
22:38
Make the LIMIT clause work even if the destination of the SELECT is something other than a callback. (Ticket #66) (CVS 619) check-in: 699cf362 user: drh tags: trunk
20:58
Improvements to comments. No code changes. (CVS 618) check-in: 7e10e60b user: drh tags: trunk
20:54
Fix for bug #72: Make sure the file descriptors are properly closed if sqliteInit() fails for any reason. (CVS 617) check-in: e67ac0fe user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/util.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Utility functions used throughout sqlite.
    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17         -** $Id: util.c,v 1.45 2002/05/31 15:51:25 drh Exp $
           17  +** $Id: util.c,v 1.46 2002/06/14 20:58:45 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <stdarg.h>
    21     21   #include <ctype.h>
    22     22   
    23     23   /*
    24     24   ** If malloc() ever fails, this global variable gets set to 1.
................................................................................
  1080   1080   ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
  1081   1081   ** when this routine is called.
  1082   1082   **
  1083   1083   ** This routine is a attempt to detect if two threads use the
  1084   1084   ** same sqlite* pointer at the same time.  There is a race 
  1085   1085   ** condition so it is possible that the error is not detected.
  1086   1086   ** But usually the problem will be seen.  The result will be an
  1087         -** error which can be used to debugging the application that is
         1087  +** error which can be used to debug the application that is
  1088   1088   ** using SQLite incorrectly.
  1089   1089   */
  1090   1090   int sqliteSafetyOn(sqlite *db){
  1091   1091     if( db->magic==SQLITE_MAGIC_OPEN ){
  1092   1092       db->magic = SQLITE_MAGIC_BUSY;
  1093   1093       return 0;
  1094   1094     }else{

Changes to src/where.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This module contains C code that generates VDBE code used to process
    13     13   ** the WHERE clause of SQL statements.  Also found here are subroutines
    14     14   ** to generate VDBE code to evaluate expressions.
    15     15   **
    16         -** $Id: where.c,v 1.50 2002/06/09 01:55:20 drh Exp $
           16  +** $Id: where.c,v 1.51 2002/06/14 20:58:45 drh Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   
    20     20   /*
    21     21   ** The query generator uses an array of instances of this structure to
    22     22   ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
    23     23   ** clause subexpression is separated from the others by an AND operator.
................................................................................
    99     99     }
   100    100     return mask;
   101    101   }
   102    102   
   103    103   /*
   104    104   ** Return TRUE if the given operator is one of the operators that is
   105    105   ** allowed for an indexable WHERE clause.  The allowed operators are
   106         -** "=", "<", ">", "<=", and ">=".
          106  +** "=", "<", ">", "<=", ">=", and "IN".
   107    107   */
   108    108   static int allowedOp(int op){
   109    109     switch( op ){
   110    110       case TK_LT:
   111    111       case TK_LE:
   112    112       case TK_GT:
   113    113       case TK_GE:
................................................................................
   152    152   ** Generating the beginning of the loop used for WHERE clause processing.
   153    153   ** The return value is a pointer to an (opaque) structure that contains
   154    154   ** information needed to terminate the loop.  Later, the calling routine
   155    155   ** should invoke sqliteWhereEnd() with the return value of this function
   156    156   ** in order to complete the WHERE clause processing.
   157    157   **
   158    158   ** If an error occurs, this routine returns NULL.
          159  +**
          160  +** The basic idea is to do a nested loop, one loop for each table in
          161  +** the FROM clause of a select.  (INSERT and UPDATE statements are the
          162  +** same as a SELECT with only a single table in the FROM clause.)  For
          163  +** example, if the SQL is this:
          164  +**
          165  +**       SELECT * FROM t1, t2, t3 WHERE ...;
          166  +**
          167  +** Then the code generated is conceptually like the following:
          168  +**
          169  +**      foreach row1 in t1 do       \    Code generated
          170  +**        foreach row2 in t2 do      |-- by sqliteWhereBegin()
          171  +**          foreach row3 in t3 do   /
          172  +**            ...
          173  +**          end                     \    Code generated
          174  +**        end                        |-- by sqliteWhereEnd()
          175  +**      end                         /
          176  +**
          177  +** There are Btree cursors associated with each table.  t1 uses cursor
          178  +** "base".  t2 uses cursor "base+1".  And so forth.  This routine generates
          179  +** the code to open those cursors.  sqliteWhereEnd() generates the code
          180  +** to close them.
          181  +**
          182  +** If the WHERE clause is empty, the foreach loops must each scan their
          183  +** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
          184  +** the tables have indices and there are terms in the WHERE clause that
          185  +** refer to those indices, a complete table scan can be avoided and the
          186  +** code will run much faster.  Most of the work of this routine is checking
          187  +** to see if there are indices that can be used to speed up the loop.
          188  +**
          189  +** Terms of the WHERE clause are also used to limit which rows actually
          190  +** make it to the "..." in the middle of the loop.  After each "foreach",
          191  +** terms of the WHERE clause that use only terms in that loop and outer
          192  +** loops are evaluated and if false a jump is made around all subsequent
          193  +** inner loops (or around the "..." if the test occurs within the inner-
          194  +** most loop)
          195  +**
          196  +** OUTER JOINS
          197  +**
          198  +** An outer join of tables t1 and t2 is conceptally coded as follows:
          199  +**
          200  +**    foreach row1 in t1 do
          201  +**      flag = 0
          202  +**      foreach row2 in t2 do
          203  +**        start:
          204  +**          ...
          205  +**          flag = 1
          206  +**      end
          207  +**      if flag==0 goto start
          208  +**    end
          209  +**
          210  +** In words, if the right
   159    211   */
   160    212   WhereInfo *sqliteWhereBegin(
   161    213     Parse *pParse,       /* The parser context */
   162    214     int base,            /* VDBE cursor index for left-most table in pTabList */
   163    215     SrcList *pTabList,   /* A list of all tables to be scanned */
   164    216     Expr *pWhere,        /* The WHERE clause */
   165    217     int pushKey          /* If TRUE, leave the table key on the stack */
................................................................................
   174    226     int haveKey;         /* True if KEY is on the stack */
   175    227     int aDirect[32];     /* If TRUE, then index this table using ROWID */
   176    228     int iDirectEq[32];   /* Term of the form ROWID==X for the N-th table */
   177    229     int iDirectLt[32];   /* Term of the form ROWID<X or ROWID<=X */
   178    230     int iDirectGt[32];   /* Term of the form ROWID>X or ROWID>=X */
   179    231     ExprInfo aExpr[50];  /* The WHERE clause is divided into these expressions */
   180    232   
          233  +  /* pushKey is only allowed if there is a single table (as in an INSERT or
          234  +  ** UPDATE statement)
          235  +  */
          236  +  assert( pushKey==0 || pTabList->nSrc==1 );
          237  +  
   181    238     /* Allocate space for aOrder[] and aiMem[]. */
   182    239     aOrder = sqliteMalloc( sizeof(int) * pTabList->nSrc );
   183    240   
   184    241     /* Allocate and initialize the WhereInfo structure that will become the
   185    242     ** return value.
   186    243     */
   187    244     pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
................................................................................
   328    385       ** This scoring system is designed so that the score can later be
   329    386       ** used to determine how the index is used.  If the score&3 is 0
   330    387       ** then all constraints are equalities.  If score&1 is not 0 then
   331    388       ** there is an inequality used as a termination key.  (ex: "x<...")
   332    389       ** If score&2 is not 0 then there is an inequality used as the
   333    390       ** start key.  (ex: "x>...");
   334    391       **
   335         -    ** The IN operator as in "<expr> IN (...)" is treated the same as
   336         -    ** an equality comparison.
          392  +    ** The IN operator (as in "<expr> IN (...)") is treated the same as
          393  +    ** an equality comparison except that it can only be used on the
          394  +    ** left-most column of an index and other terms of the WHERE clause
          395  +    ** cannot be used in conjunction with the IN operator to help satisfy
          396  +    ** other columns of the index.
   337    397       */
   338    398       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   339         -      int eqMask = 0;  /* Index columns covered by an x=... constraint */
   340         -      int ltMask = 0;  /* Index columns covered by an x<... constraint */
   341         -      int gtMask = 0;  /* Index columns covered by an x>... constraint */
   342         -      int inMask = 0;  /* Index columns covered by an x IN .. constraint */
          399  +      int eqMask = 0;  /* Index columns covered by an x=... term */
          400  +      int ltMask = 0;  /* Index columns covered by an x<... term */
          401  +      int gtMask = 0;  /* Index columns covered by an x>... term */
          402  +      int inMask = 0;  /* Index columns covered by an x IN .. term */
   343    403         int nEq, m, score;
   344    404   
   345    405         if( pIdx->isDropped ) continue;   /* Ignore dropped indices */
   346    406         if( pIdx->nColumn>32 ) continue;  /* Ignore indices too many columns */
   347    407         for(j=0; j<nExpr; j++){
   348    408           if( aExpr[j].idxLeft==idx 
   349    409                && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
................................................................................
   464    524       int j, k;
   465    525       int idx = aOrder[i];
   466    526       Index *pIdx;
   467    527       WhereLevel *pLevel = &pWInfo->a[i];
   468    528   
   469    529       /* If this is the right table of a LEFT OUTER JOIN, allocate and
   470    530       ** initialize a memory cell that record if this table matches any
   471         -    ** row of the left table in the join.
          531  +    ** row of the left table of the join.
   472    532       */
   473    533       if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
   474    534         if( !pParse->nMem ) pParse->nMem++;
   475    535         pLevel->iLeftJoin = pParse->nMem++;
   476    536         sqliteVdbeAddOp(v, OP_String, 0, 0);
   477    537         sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
   478    538       }
   479    539   
   480    540       pIdx = pLevel->pIdx;
   481    541       pLevel->inOp = OP_Noop;
   482    542       if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
   483    543         /* Case 1:  We can directly reference a single row using an
   484         -      **          equality comparison against the ROWID field.
          544  +      **          equality comparison against the ROWID field.  Or
          545  +      **          we reference multiple rows using a "rowid IN (...)"
          546  +      **          construct.
   485    547         */
   486    548         k = iDirectEq[i];
   487    549         assert( k<nExpr );
   488    550         assert( aExpr[k].p!=0 );
   489    551         assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
   490    552         brk = pLevel->brk = sqliteVdbeMakeLabel(v);
   491    553         if( aExpr[k].idxLeft==idx ){
................................................................................
   511    573         aExpr[k].p = 0;
   512    574         cont = pLevel->cont = sqliteVdbeMakeLabel(v);
   513    575         sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
   514    576         haveKey = 0;
   515    577         sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
   516    578         pLevel->op = OP_Noop;
   517    579       }else if( pIdx!=0 && pLevel->score%4==0 ){
   518         -      /* Case 2:  All index constraints are equality operators.
          580  +      /* Case 2:  There is an index and all terms of the WHERE clause that
          581  +      **          refer to the index use the "==" or "IN" operators.
   519    582         */
   520    583         int start;
   521    584         int testOp;
   522    585         int nColumn = pLevel->score/4;
   523    586         brk = pLevel->brk = sqliteVdbeMakeLabel(v);
   524    587         for(j=0; j<nColumn; j++){
   525    588           for(k=0; k<nExpr; k++){
................................................................................
   642    705         if( testOp!=OP_Noop ){
   643    706           sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
   644    707           sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
   645    708           sqliteVdbeAddOp(v, testOp, 0, brk);
   646    709         }
   647    710         haveKey = 0;
   648    711       }else if( pIdx==0 ){
   649         -      /* Case 4:  There was no usable index.  We must do a complete
          712  +      /* Case 4:  There is no usable index.  We must do a complete
   650    713         **          scan of the entire database table.
   651    714         */
   652    715         int start;
   653    716   
   654    717         brk = pLevel->brk = sqliteVdbeMakeLabel(v);
   655    718         cont = pLevel->cont = sqliteVdbeMakeLabel(v);
   656    719         sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
   657    720         start = sqliteVdbeCurrentAddr(v);
   658    721         pLevel->op = OP_Next;
   659    722         pLevel->p1 = base+idx;
   660    723         pLevel->p2 = start;
   661    724         haveKey = 0;
   662    725       }else{
   663         -      /* Case 5: The contraint on the right-most index field is
   664         -      **         an inequality.
          726  +      /* Case 5: The WHERE clause term that refers to the right-most
          727  +      **         column of the index is an inequality.  For example, if
          728  +      **         the index is on (x,y,z) and the WHERE clause is of the
          729  +      **         form "x=5 AND y<10" then this case is used.  Only the
          730  +      **         right-most column can be an inequality - the rest must
          731  +      **         use the "==" operator.
   665    732         */
   666    733         int score = pLevel->score;
   667    734         int nEqColumn = score/4;
   668    735         int start;
   669    736         int leFlag, geFlag;
   670    737         int testOp;
   671    738   
................................................................................
   691    758               sqliteExprCode(pParse, aExpr[k].p->pLeft);
   692    759               aExpr[k].p = 0;
   693    760               break;
   694    761             }
   695    762           }
   696    763         }
   697    764   
   698         -      /* Duplicate the equality contraint values because they will all be
          765  +      /* Duplicate the equality term values because they will all be
   699    766         ** used twice: once to make the termination key and once to make the
   700    767         ** start key.
   701    768         */
   702    769         for(j=0; j<nEqColumn; j++){
   703    770           sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
   704    771         }
   705    772   
   706    773         /* Generate the termination key.  This is the key value that
   707    774         ** will end the search.  There is no termination key if there
   708         -      ** are no equality contraints and no "X<..." constraint.
          775  +      ** are no equality terms and no "X<..." term.
   709    776         */
   710    777         if( (score & 1)!=0 ){
   711    778           for(k=0; k<nExpr; k++){
   712    779             Expr *pExpr = aExpr[k].p;
   713    780             if( pExpr==0 ) continue;
   714    781             if( aExpr[k].idxLeft==idx 
   715    782                && (pExpr->op==TK_LT || pExpr->op==TK_LE)
................................................................................
   743    810           if( leFlag ){
   744    811             sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
   745    812           }
   746    813           sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
   747    814         }
   748    815   
   749    816         /* Generate the start key.  This is the key that defines the lower
   750         -      ** bound on the search.  There is no start key if there are not
   751         -      ** equality constraints and if there is no "X>..." constraint.  In
          817  +      ** bound on the search.  There is no start key if there are no
          818  +      ** equality terms and if there is no "X>..." term.  In
   752    819         ** that case, generate a "Rewind" instruction in place of the
   753    820         ** start key search.
   754    821         */
   755    822         if( (score & 2)!=0 ){
   756    823           for(k=0; k<nExpr; k++){
   757    824             Expr *pExpr = aExpr[k].p;
   758    825             if( pExpr==0 ) continue;
................................................................................
   846    913       sqliteVdbeAddOp(v, OP_Recno, base, 0);
   847    914     }
   848    915     sqliteFree(aOrder);
   849    916     return pWInfo;
   850    917   }
   851    918   
   852    919   /*
   853         -** Generate the end of the WHERE loop.
          920  +** Generate the end of the WHERE loop.  See comments on 
          921  +** sqliteWhereBegin() for additional information.
   854    922   */
   855    923   void sqliteWhereEnd(WhereInfo *pWInfo){
   856    924     Vdbe *v = pWInfo->pParse->pVdbe;
   857    925     int i;
   858    926     int base = pWInfo->base;
   859    927     WhereLevel *pLevel;
   860    928     SrcList *pTabList = pWInfo->pTabList;