/ Check-in [50f33681]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Split out some source code into new files: wherecode.c, whereexpr.c, and treeview.c. Other minor refactoring changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 50f336818c8509d8b8bde282e9399d2b2b5ea70a
User & Date: drh 2015-06-08 17:40:30
Context
2015-06-08
17:42
Fix typo in comment. No changes to code. check-in: e49c2917 user: mistachkin tags: trunk
17:40
Split out some source code into new files: wherecode.c, whereexpr.c, and treeview.c. Other minor refactoring changes. check-in: 50f33681 user: drh tags: trunk
15:08
Factor out the TreeView parse tree printing module into a separate file. check-in: c32ce54c user: drh tags: view-optimization
2015-06-05
20:27
Provide one final Select tree dump prior to WHERE clause analysis when ".selecttrace 0x400" tracing bit is set with SELECTTRACE_ENABLED. Analysis and debug changes only - normal builds are unaffected. check-in: 283bf0b6 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   177    177            icu.lo insert.lo journal.lo legacy.lo loadext.lo \
   178    178            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
   179    179            memjournal.lo \
   180    180            mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
   181    181            notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
   182    182            pager.lo parse.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
   183    183            random.lo resolve.lo rowset.lo rtree.lo select.lo status.lo \
   184         -         table.lo threads.lo tokenize.lo trigger.lo \
          184  +         table.lo threads.lo tokenize.lo treeview.lo trigger.lo \
   185    185            update.lo util.lo vacuum.lo \
   186    186            vdbe.lo vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
   187         -         vdbetrace.lo wal.lo walker.lo where.lo utf.lo vtab.lo
          187  +         vdbetrace.lo wal.lo walker.lo where.lo wherecode.lo whereexpr.lo \
          188  +         utf.lo vtab.lo
   188    189   
   189    190   # Object files for the amalgamation.
   190    191   #
   191    192   LIBOBJS1 = sqlite3.lo
   192    193   
   193    194   # Determine the real value of LIBOBJ based on the 'configure' script
   194    195   #
................................................................................
   269    270     $(TOP)/src/sqlite3ext.h \
   270    271     $(TOP)/src/sqliteInt.h \
   271    272     $(TOP)/src/sqliteLimit.h \
   272    273     $(TOP)/src/table.c \
   273    274     $(TOP)/src/threads.c \
   274    275     $(TOP)/src/tclsqlite.c \
   275    276     $(TOP)/src/tokenize.c \
          277  +  $(TOP)/src/treeview.c \
   276    278     $(TOP)/src/trigger.c \
   277    279     $(TOP)/src/utf.c \
   278    280     $(TOP)/src/update.c \
   279    281     $(TOP)/src/util.c \
   280    282     $(TOP)/src/vacuum.c \
   281    283     $(TOP)/src/vdbe.c \
   282    284     $(TOP)/src/vdbe.h \
................................................................................
   289    291     $(TOP)/src/vdbeInt.h \
   290    292     $(TOP)/src/vtab.c \
   291    293     $(TOP)/src/vxworks.h \
   292    294     $(TOP)/src/wal.c \
   293    295     $(TOP)/src/wal.h \
   294    296     $(TOP)/src/walker.c \
   295    297     $(TOP)/src/where.c \
          298  +  $(TOP)/src/wherecode.c \
          299  +  $(TOP)/src/whereexpr.c \
   296    300     $(TOP)/src/whereInt.h
   297    301   
   298    302   # Source code for extensions
   299    303   #
   300    304   SRC += \
   301    305     $(TOP)/ext/fts1/fts1.c \
   302    306     $(TOP)/ext/fts1/fts1.h \
................................................................................
   450    454     $(TOP)/src/util.c \
   451    455     $(TOP)/src/vdbeapi.c \
   452    456     $(TOP)/src/vdbeaux.c \
   453    457     $(TOP)/src/vdbe.c \
   454    458     $(TOP)/src/vdbemem.c \
   455    459     $(TOP)/src/vdbetrace.c \
   456    460     $(TOP)/src/where.c \
          461  +  $(TOP)/src/wherecode.c \
          462  +  $(TOP)/src/whereexpr.c \
   457    463     parse.c \
   458    464     $(TOP)/ext/fts3/fts3.c \
   459    465     $(TOP)/ext/fts3/fts3_aux.c \
   460    466     $(TOP)/ext/fts3/fts3_expr.c \
   461    467     $(TOP)/ext/fts3/fts3_term.c \
   462    468     $(TOP)/ext/fts3/fts3_tokenizer.c \
   463    469     $(TOP)/ext/fts3/fts3_write.c \
................................................................................
   797    803   
   798    804   threads.lo:	$(TOP)/src/threads.c $(HDR)
   799    805   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/threads.c
   800    806   
   801    807   tokenize.lo:	$(TOP)/src/tokenize.c keywordhash.h $(HDR)
   802    808   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/tokenize.c
   803    809   
          810  +treeview.lo:	$(TOP)/src/treeview.c $(HDR)
          811  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/treeview.c
          812  +
   804    813   trigger.lo:	$(TOP)/src/trigger.c $(HDR)
   805    814   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/trigger.c
   806    815   
   807    816   update.lo:	$(TOP)/src/update.c $(HDR)
   808    817   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/update.c
   809    818   
   810    819   utf.lo:	$(TOP)/src/utf.c $(HDR)
................................................................................
   845    854   
   846    855   walker.lo:	$(TOP)/src/walker.c $(HDR)
   847    856   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/walker.c
   848    857   
   849    858   where.lo:	$(TOP)/src/where.c $(HDR)
   850    859   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/where.c
   851    860   
          861  +wherecode.lo:	$(TOP)/src/wherecode.c $(HDR)
          862  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/wherecode.c
          863  +
          864  +whereexpr.lo:	$(TOP)/src/whereexpr.c $(HDR)
          865  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/whereexpr.c
          866  +
   852    867   tclsqlite.lo:	$(TOP)/src/tclsqlite.c $(HDR)
   853    868   	$(LTCOMPILE) -DUSE_TCL_STUBS=1 -c $(TOP)/src/tclsqlite.c
   854    869   
   855    870   tclsqlite-shell.lo:	$(TOP)/src/tclsqlite.c $(HDR)
   856    871   	$(LTCOMPILE) -DTCLSH=1 -o $@ -c $(TOP)/src/tclsqlite.c
   857    872   
   858    873   tclsqlite-stubs.lo:	$(TOP)/src/tclsqlite.c $(HDR)

Changes to Makefile.msc.

   831    831            icu.lo insert.lo journal.lo legacy.lo loadext.lo \
   832    832            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
   833    833            memjournal.lo \
   834    834            mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
   835    835            notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
   836    836            pager.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
   837    837            random.lo resolve.lo rowset.lo rtree.lo select.lo status.lo \
   838         -         table.lo threads.lo tokenize.lo trigger.lo \
          838  +         table.lo threads.lo tokenize.lo treeview.lo trigger.lo \
   839    839            update.lo util.lo vacuum.lo \
   840    840            vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
   841         -         vdbetrace.lo wal.lo walker.lo where.lo utf.lo vtab.lo
          841  +         vdbetrace.lo wal.lo walker.lo where.lo wherecode.lo whereexpr.lo \
          842  +         utf.lo vtab.lo
   842    843   
   843    844   # Object files for the amalgamation.
   844    845   #
   845    846   LIBOBJS1 = sqlite3.lo
   846    847   
   847    848   # Determine the real value of LIBOBJ based on the 'configure' script
   848    849   #
................................................................................
   935    936     $(TOP)\src\sqlite3ext.h \
   936    937     $(TOP)\src\sqliteInt.h \
   937    938     $(TOP)\src\sqliteLimit.h \
   938    939     $(TOP)\src\table.c \
   939    940     $(TOP)\src\threads.c \
   940    941     $(TOP)\src\tclsqlite.c \
   941    942     $(TOP)\src\tokenize.c \
          943  +  $(TOP)\src\treeview.c \
   942    944     $(TOP)\src\trigger.c \
   943    945     $(TOP)\src\utf.c \
   944    946     $(TOP)\src\update.c \
   945    947     $(TOP)\src\util.c \
   946    948     $(TOP)\src\vacuum.c \
   947    949     $(TOP)\src\vdbe.c \
   948    950     $(TOP)\src\vdbe.h \
................................................................................
   955    957     $(TOP)\src\vdbeInt.h \
   956    958     $(TOP)\src\vtab.c \
   957    959     $(TOP)\src\vxworks.h \
   958    960     $(TOP)\src\wal.c \
   959    961     $(TOP)\src\wal.h \
   960    962     $(TOP)\src\walker.c \
   961    963     $(TOP)\src\where.c \
          964  +  $(TOP)\src\wherecode.c \
          965  +  $(TOP)\src\whereexpr.c \
   962    966     $(TOP)\src\whereInt.h
   963    967   
   964    968   # Source code for extensions
   965    969   #
   966    970   SRC3 = \
   967    971     $(TOP)\ext\fts1\fts1.c \
   968    972     $(TOP)\ext\fts1\fts1.h \
................................................................................
  1117   1121     $(TOP)\src\vdbeapi.c \
  1118   1122     $(TOP)\src\vdbeaux.c \
  1119   1123     $(TOP)\src\vdbe.c \
  1120   1124     $(TOP)\src\vdbemem.c \
  1121   1125     $(TOP)\src\vdbesort.c \
  1122   1126     $(TOP)\src\vdbetrace.c \
  1123   1127     $(TOP)\src\where.c \
         1128  +  $(TOP)\src\wherecode.c \
         1129  +  $(TOP)\src\whereexpr.c \
  1124   1130     parse.c \
  1125   1131     $(TOP)\ext\fts3\fts3.c \
  1126   1132     $(TOP)\ext\fts3\fts3_aux.c \
  1127   1133     $(TOP)\ext\fts3\fts3_expr.c \
  1128   1134     $(TOP)\ext\fts3\fts3_tokenizer.c \
  1129   1135     $(TOP)\ext\fts3\fts3_tokenize_vtab.c \
  1130   1136     $(TOP)\ext\fts3\fts3_unicode.c \
................................................................................
  1478   1484   
  1479   1485   threads.lo:	$(TOP)\src\threads.c $(HDR)
  1480   1486   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\threads.c
  1481   1487   
  1482   1488   tokenize.lo:	$(TOP)\src\tokenize.c keywordhash.h $(HDR)
  1483   1489   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\tokenize.c
  1484   1490   
         1491  +treeview.lo:	$(TOP)\src\treeview.c $(HDR)
         1492  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\treeview.c
         1493  +
  1485   1494   trigger.lo:	$(TOP)\src\trigger.c $(HDR)
  1486   1495   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\trigger.c
  1487   1496   
  1488   1497   update.lo:	$(TOP)\src\update.c $(HDR)
  1489   1498   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\update.c
  1490   1499   
  1491   1500   utf.lo:	$(TOP)\src\utf.c $(HDR)
................................................................................
  1526   1535   
  1527   1536   walker.lo:	$(TOP)\src\walker.c $(HDR)
  1528   1537   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\walker.c
  1529   1538   
  1530   1539   where.lo:	$(TOP)\src\where.c $(HDR)
  1531   1540   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\where.c
  1532   1541   
         1542  +wherecode.lo:	$(TOP)\src\wherecode.c $(HDR)
         1543  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\wherecode.c
         1544  +
         1545  +whereexpr.lo:	$(TOP)\src\whereexpr.c $(HDR)
         1546  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\whereexpr.c
         1547  +
  1533   1548   tclsqlite.lo:	$(TOP)\src\tclsqlite.c $(HDR)
  1534   1549   	$(LTCOMPILE) $(NO_WARN) -DUSE_TCL_STUBS=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
  1535   1550   
  1536   1551   tclsqlite-shell.lo:	$(TOP)\src\tclsqlite.c $(HDR)
  1537   1552   	$(LTCOMPILE) $(NO_WARN) -DTCLSH=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
  1538   1553   
  1539   1554   tclsqlite3.exe:	tclsqlite-shell.lo $(SQLITE3C) $(LIBRESOBJS)

Changes to main.mk.

    62     62            icu.o insert.o journal.o legacy.o loadext.o \
    63     63            main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \
    64     64            memjournal.o \
    65     65            mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
    66     66            notify.o opcodes.o os.o os_unix.o os_win.o \
    67     67            pager.o pcache.o pcache1.o pragma.o prepare.o printf.o \
    68     68            random.o resolve.o rowset.o rtree.o select.o sqlite3ota.o status.o \
    69         -         table.o threads.o tokenize.o trigger.o \
           69  +         table.o threads.o tokenize.o treeview.o trigger.o \
    70     70            update.o userauth.o util.o vacuum.o \
    71     71            vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \
    72         -	 vdbetrace.o wal.o walker.o where.o utf.o vtab.o
           72  +	 vdbetrace.o wal.o walker.o where.o wherecode.o whereexpr.o \
           73  +         utf.o vtab.o
    73     74   
    74     75   
    75     76   
    76     77   # All of the source code files.
    77     78   #
    78     79   SRC = \
    79     80     $(TOP)/src/alter.c \
................................................................................
   147    148     $(TOP)/src/sqlite3ext.h \
   148    149     $(TOP)/src/sqliteInt.h \
   149    150     $(TOP)/src/sqliteLimit.h \
   150    151     $(TOP)/src/table.c \
   151    152     $(TOP)/src/tclsqlite.c \
   152    153     $(TOP)/src/threads.c \
   153    154     $(TOP)/src/tokenize.c \
          155  +  $(TOP)/src/treeview.c \
   154    156     $(TOP)/src/trigger.c \
   155    157     $(TOP)/src/utf.c \
   156    158     $(TOP)/src/update.c \
   157    159     $(TOP)/src/util.c \
   158    160     $(TOP)/src/vacuum.c \
   159    161     $(TOP)/src/vdbe.c \
   160    162     $(TOP)/src/vdbe.h \
................................................................................
   167    169     $(TOP)/src/vdbeInt.h \
   168    170     $(TOP)/src/vtab.c \
   169    171     $(TOP)/src/vxworks.h \
   170    172     $(TOP)/src/wal.c \
   171    173     $(TOP)/src/wal.h \
   172    174     $(TOP)/src/walker.c \
   173    175     $(TOP)/src/where.c \
          176  +  $(TOP)/src/wherecode.c \
          177  +  $(TOP)/src/whereexpr.c \
   174    178     $(TOP)/src/whereInt.h
   175    179   
   176    180   # Source code for extensions
   177    181   #
   178    182   SRC += \
   179    183     $(TOP)/ext/fts1/fts1.c \
   180    184     $(TOP)/ext/fts1/fts1.h \
................................................................................
   333    337     $(TOP)/src/utf.c \
   334    338     $(TOP)/src/util.c \
   335    339     $(TOP)/src/vdbeapi.c \
   336    340     $(TOP)/src/vdbeaux.c \
   337    341     $(TOP)/src/vdbe.c \
   338    342     $(TOP)/src/vdbemem.c \
   339    343     $(TOP)/src/where.c \
          344  +  $(TOP)/src/wherecode.c \
          345  +  $(TOP)/src/whereexpr.c \
   340    346     parse.c \
   341    347     $(TOP)/ext/fts3/fts3.c \
   342    348     $(TOP)/ext/fts3/fts3_aux.c \
   343    349     $(TOP)/ext/fts3/fts3_expr.c \
   344    350     $(TOP)/ext/fts3/fts3_tokenizer.c \
   345    351     $(TOP)/ext/fts3/fts3_write.c \
   346    352     $(TOP)/ext/async/sqlite3async.c 

Changes to src/expr.c.

  3307   3307     assert( pExpr->op!=TK_REGISTER );
  3308   3308     sqlite3ExprCode(pParse, pExpr, target);
  3309   3309     iMem = ++pParse->nMem;
  3310   3310     sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
  3311   3311     exprToRegister(pExpr, iMem);
  3312   3312   }
  3313   3313   
  3314         -#ifdef SQLITE_DEBUG
  3315         -/*
  3316         -** Generate a human-readable explanation of an expression tree.
  3317         -*/
  3318         -void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
  3319         -  const char *zBinOp = 0;   /* Binary operator */
  3320         -  const char *zUniOp = 0;   /* Unary operator */
  3321         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
  3322         -  if( pExpr==0 ){
  3323         -    sqlite3TreeViewLine(pView, "nil");
  3324         -    sqlite3TreeViewPop(pView);
  3325         -    return;
  3326         -  }
  3327         -  switch( pExpr->op ){
  3328         -    case TK_AGG_COLUMN: {
  3329         -      sqlite3TreeViewLine(pView, "AGG{%d:%d}",
  3330         -            pExpr->iTable, pExpr->iColumn);
  3331         -      break;
  3332         -    }
  3333         -    case TK_COLUMN: {
  3334         -      if( pExpr->iTable<0 ){
  3335         -        /* This only happens when coding check constraints */
  3336         -        sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
  3337         -      }else{
  3338         -        sqlite3TreeViewLine(pView, "{%d:%d}",
  3339         -                             pExpr->iTable, pExpr->iColumn);
  3340         -      }
  3341         -      break;
  3342         -    }
  3343         -    case TK_INTEGER: {
  3344         -      if( pExpr->flags & EP_IntValue ){
  3345         -        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
  3346         -      }else{
  3347         -        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
  3348         -      }
  3349         -      break;
  3350         -    }
  3351         -#ifndef SQLITE_OMIT_FLOATING_POINT
  3352         -    case TK_FLOAT: {
  3353         -      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
  3354         -      break;
  3355         -    }
  3356         -#endif
  3357         -    case TK_STRING: {
  3358         -      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
  3359         -      break;
  3360         -    }
  3361         -    case TK_NULL: {
  3362         -      sqlite3TreeViewLine(pView,"NULL");
  3363         -      break;
  3364         -    }
  3365         -#ifndef SQLITE_OMIT_BLOB_LITERAL
  3366         -    case TK_BLOB: {
  3367         -      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
  3368         -      break;
  3369         -    }
  3370         -#endif
  3371         -    case TK_VARIABLE: {
  3372         -      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
  3373         -                          pExpr->u.zToken, pExpr->iColumn);
  3374         -      break;
  3375         -    }
  3376         -    case TK_REGISTER: {
  3377         -      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
  3378         -      break;
  3379         -    }
  3380         -    case TK_AS: {
  3381         -      sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
  3382         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3383         -      break;
  3384         -    }
  3385         -    case TK_ID: {
  3386         -      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
  3387         -      break;
  3388         -    }
  3389         -#ifndef SQLITE_OMIT_CAST
  3390         -    case TK_CAST: {
  3391         -      /* Expressions of the form:   CAST(pLeft AS token) */
  3392         -      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
  3393         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3394         -      break;
  3395         -    }
  3396         -#endif /* SQLITE_OMIT_CAST */
  3397         -    case TK_LT:      zBinOp = "LT";     break;
  3398         -    case TK_LE:      zBinOp = "LE";     break;
  3399         -    case TK_GT:      zBinOp = "GT";     break;
  3400         -    case TK_GE:      zBinOp = "GE";     break;
  3401         -    case TK_NE:      zBinOp = "NE";     break;
  3402         -    case TK_EQ:      zBinOp = "EQ";     break;
  3403         -    case TK_IS:      zBinOp = "IS";     break;
  3404         -    case TK_ISNOT:   zBinOp = "ISNOT";  break;
  3405         -    case TK_AND:     zBinOp = "AND";    break;
  3406         -    case TK_OR:      zBinOp = "OR";     break;
  3407         -    case TK_PLUS:    zBinOp = "ADD";    break;
  3408         -    case TK_STAR:    zBinOp = "MUL";    break;
  3409         -    case TK_MINUS:   zBinOp = "SUB";    break;
  3410         -    case TK_REM:     zBinOp = "REM";    break;
  3411         -    case TK_BITAND:  zBinOp = "BITAND"; break;
  3412         -    case TK_BITOR:   zBinOp = "BITOR";  break;
  3413         -    case TK_SLASH:   zBinOp = "DIV";    break;
  3414         -    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
  3415         -    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
  3416         -    case TK_CONCAT:  zBinOp = "CONCAT"; break;
  3417         -    case TK_DOT:     zBinOp = "DOT";    break;
  3418         -
  3419         -    case TK_UMINUS:  zUniOp = "UMINUS"; break;
  3420         -    case TK_UPLUS:   zUniOp = "UPLUS";  break;
  3421         -    case TK_BITNOT:  zUniOp = "BITNOT"; break;
  3422         -    case TK_NOT:     zUniOp = "NOT";    break;
  3423         -    case TK_ISNULL:  zUniOp = "ISNULL"; break;
  3424         -    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
  3425         -
  3426         -    case TK_COLLATE: {
  3427         -      sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
  3428         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3429         -      break;
  3430         -    }
  3431         -
  3432         -    case TK_AGG_FUNCTION:
  3433         -    case TK_FUNCTION: {
  3434         -      ExprList *pFarg;       /* List of function arguments */
  3435         -      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
  3436         -        pFarg = 0;
  3437         -      }else{
  3438         -        pFarg = pExpr->x.pList;
  3439         -      }
  3440         -      if( pExpr->op==TK_AGG_FUNCTION ){
  3441         -        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
  3442         -                             pExpr->op2, pExpr->u.zToken);
  3443         -      }else{
  3444         -        sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
  3445         -      }
  3446         -      if( pFarg ){
  3447         -        sqlite3TreeViewExprList(pView, pFarg, 0, 0);
  3448         -      }
  3449         -      break;
  3450         -    }
  3451         -#ifndef SQLITE_OMIT_SUBQUERY
  3452         -    case TK_EXISTS: {
  3453         -      sqlite3TreeViewLine(pView, "EXISTS-expr");
  3454         -      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
  3455         -      break;
  3456         -    }
  3457         -    case TK_SELECT: {
  3458         -      sqlite3TreeViewLine(pView, "SELECT-expr");
  3459         -      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
  3460         -      break;
  3461         -    }
  3462         -    case TK_IN: {
  3463         -      sqlite3TreeViewLine(pView, "IN");
  3464         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
  3465         -      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  3466         -        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
  3467         -      }else{
  3468         -        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
  3469         -      }
  3470         -      break;
  3471         -    }
  3472         -#endif /* SQLITE_OMIT_SUBQUERY */
  3473         -
  3474         -    /*
  3475         -    **    x BETWEEN y AND z
  3476         -    **
  3477         -    ** This is equivalent to
  3478         -    **
  3479         -    **    x>=y AND x<=z
  3480         -    **
  3481         -    ** X is stored in pExpr->pLeft.
  3482         -    ** Y is stored in pExpr->pList->a[0].pExpr.
  3483         -    ** Z is stored in pExpr->pList->a[1].pExpr.
  3484         -    */
  3485         -    case TK_BETWEEN: {
  3486         -      Expr *pX = pExpr->pLeft;
  3487         -      Expr *pY = pExpr->x.pList->a[0].pExpr;
  3488         -      Expr *pZ = pExpr->x.pList->a[1].pExpr;
  3489         -      sqlite3TreeViewLine(pView, "BETWEEN");
  3490         -      sqlite3TreeViewExpr(pView, pX, 1);
  3491         -      sqlite3TreeViewExpr(pView, pY, 1);
  3492         -      sqlite3TreeViewExpr(pView, pZ, 0);
  3493         -      break;
  3494         -    }
  3495         -    case TK_TRIGGER: {
  3496         -      /* If the opcode is TK_TRIGGER, then the expression is a reference
  3497         -      ** to a column in the new.* or old.* pseudo-tables available to
  3498         -      ** trigger programs. In this case Expr.iTable is set to 1 for the
  3499         -      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
  3500         -      ** is set to the column of the pseudo-table to read, or to -1 to
  3501         -      ** read the rowid field.
  3502         -      */
  3503         -      sqlite3TreeViewLine(pView, "%s(%d)", 
  3504         -          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
  3505         -      break;
  3506         -    }
  3507         -    case TK_CASE: {
  3508         -      sqlite3TreeViewLine(pView, "CASE");
  3509         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
  3510         -      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
  3511         -      break;
  3512         -    }
  3513         -#ifndef SQLITE_OMIT_TRIGGER
  3514         -    case TK_RAISE: {
  3515         -      const char *zType = "unk";
  3516         -      switch( pExpr->affinity ){
  3517         -        case OE_Rollback:   zType = "rollback";  break;
  3518         -        case OE_Abort:      zType = "abort";     break;
  3519         -        case OE_Fail:       zType = "fail";      break;
  3520         -        case OE_Ignore:     zType = "ignore";    break;
  3521         -      }
  3522         -      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
  3523         -      break;
  3524         -    }
  3525         -#endif
  3526         -    default: {
  3527         -      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
  3528         -      break;
  3529         -    }
  3530         -  }
  3531         -  if( zBinOp ){
  3532         -    sqlite3TreeViewLine(pView, "%s", zBinOp);
  3533         -    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
  3534         -    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
  3535         -  }else if( zUniOp ){
  3536         -    sqlite3TreeViewLine(pView, "%s", zUniOp);
  3537         -    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3538         -  }
  3539         -  sqlite3TreeViewPop(pView);
  3540         -}
  3541         -#endif /* SQLITE_DEBUG */
  3542         -
  3543         -#ifdef SQLITE_DEBUG
  3544         -/*
  3545         -** Generate a human-readable explanation of an expression list.
  3546         -*/
  3547         -void sqlite3TreeViewExprList(
  3548         -  TreeView *pView,
  3549         -  const ExprList *pList,
  3550         -  u8 moreToFollow,
  3551         -  const char *zLabel
  3552         -){
  3553         -  int i;
  3554         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
  3555         -  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
  3556         -  if( pList==0 ){
  3557         -    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
  3558         -  }else{
  3559         -    sqlite3TreeViewLine(pView, "%s", zLabel);
  3560         -    for(i=0; i<pList->nExpr; i++){
  3561         -      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
  3562         -#if 0
  3563         -     if( pList->a[i].zName ){
  3564         -        sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
  3565         -      }
  3566         -      if( pList->a[i].bSpanIsTab ){
  3567         -        sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
  3568         -      }
  3569         -#endif
  3570         -    }
  3571         -  }
  3572         -  sqlite3TreeViewPop(pView);
  3573         -}
  3574         -#endif /* SQLITE_DEBUG */
  3575         -
  3576   3314   /*
  3577   3315   ** Generate code that pushes the value of every element of the given
  3578   3316   ** expression list into a sequence of registers beginning at target.
  3579   3317   **
  3580   3318   ** Return the number of elements evaluated.
  3581   3319   **
  3582   3320   ** The SQLITE_ECEL_DUP flag prevents the arguments from being

Changes to src/printf.c.

     1      1   /*
     2      2   ** The "printf" code that follows dates from the 1980's.  It is in
     3         -** the public domain.  The original comments are included here for
     4         -** completeness.  They are very out-of-date but might be useful as
     5         -** an historical reference.  Most of the "enhancements" have been backed
     6         -** out so that the functionality is now the same as standard printf().
            3  +** the public domain. 
     7      4   **
     8      5   **************************************************************************
     9      6   **
    10      7   ** This file contains code for a set of "printf"-like routines.  These
    11      8   ** routines format strings much like the printf() from the standard C
    12      9   ** library, though the implementation here has enhancements to support
    13     10   ** SQLlite.
................................................................................
  1054   1051     va_end(ap);
  1055   1052     sqlite3StrAccumFinish(&acc);
  1056   1053     fprintf(stdout,"%s", zBuf);
  1057   1054     fflush(stdout);
  1058   1055   }
  1059   1056   #endif
  1060   1057   
  1061         -#ifdef SQLITE_DEBUG
  1062         -/*************************************************************************
  1063         -** Routines for implementing the "TreeView" display of hierarchical
  1064         -** data structures for debugging.
  1065         -**
  1066         -** The main entry points (coded elsewhere) are:
  1067         -**     sqlite3TreeViewExpr(0, pExpr, 0);
  1068         -**     sqlite3TreeViewExprList(0, pList, 0, 0);
  1069         -**     sqlite3TreeViewSelect(0, pSelect, 0);
  1070         -** Insert calls to those routines while debugging in order to display
  1071         -** a diagram of Expr, ExprList, and Select objects.
  1072         -**
  1073         -*/
  1074         -/* Add a new subitem to the tree.  The moreToFollow flag indicates that this
  1075         -** is not the last item in the tree. */
  1076         -TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
  1077         -  if( p==0 ){
  1078         -    p = sqlite3_malloc64( sizeof(*p) );
  1079         -    if( p==0 ) return 0;
  1080         -    memset(p, 0, sizeof(*p));
  1081         -  }else{
  1082         -    p->iLevel++;
  1083         -  }
  1084         -  assert( moreToFollow==0 || moreToFollow==1 );
  1085         -  if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
  1086         -  return p;
  1087         -}
  1088         -/* Finished with one layer of the tree */
  1089         -void sqlite3TreeViewPop(TreeView *p){
  1090         -  if( p==0 ) return;
  1091         -  p->iLevel--;
  1092         -  if( p->iLevel<0 ) sqlite3_free(p);
  1093         -}
  1094         -/* Generate a single line of output for the tree, with a prefix that contains
  1095         -** all the appropriate tree lines */
  1096         -void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
  1097         -  va_list ap;
  1098         -  int i;
  1099         -  StrAccum acc;
  1100         -  char zBuf[500];
  1101         -  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
  1102         -  if( p ){
  1103         -    for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
  1104         -      sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
  1105         -    }
  1106         -    sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
  1107         -  }
  1108         -  va_start(ap, zFormat);
  1109         -  sqlite3VXPrintf(&acc, 0, zFormat, ap);
  1110         -  va_end(ap);
  1111         -  if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
  1112         -  sqlite3StrAccumFinish(&acc);
  1113         -  fprintf(stdout,"%s", zBuf);
  1114         -  fflush(stdout);
  1115         -}
  1116         -/* Shorthand for starting a new tree item that consists of a single label */
  1117         -void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){
  1118         -  p = sqlite3TreeViewPush(p, moreToFollow);
  1119         -  sqlite3TreeViewLine(p, "%s", zLabel);
  1120         -}
  1121         -#endif /* SQLITE_DEBUG */
  1122   1058   
  1123   1059   /*
  1124   1060   ** variable-argument wrapper around sqlite3VXPrintf().
  1125   1061   */
  1126   1062   void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
  1127   1063     va_list ap;
  1128   1064     va_start(ap,zFormat);
  1129   1065     sqlite3VXPrintf(p, bFlags, zFormat, ap);
  1130   1066     va_end(ap);
  1131   1067   }

Changes to src/select.c.

    17     17   /*
    18     18   ** Trace output macros
    19     19   */
    20     20   #if SELECTTRACE_ENABLED
    21     21   /***/ int sqlite3SelectTrace = 0;
    22     22   # define SELECTTRACE(K,P,S,X)  \
    23     23     if(sqlite3SelectTrace&(K))   \
    24         -    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\
           24  +    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
           25  +        (S)->zSelName,(S)),\
    25     26       sqlite3DebugPrintf X
    26     27   #else
    27     28   # define SELECTTRACE(K,P,S,X)
    28     29   #endif
    29     30   
    30     31   
    31     32   /*
................................................................................
   770    771         case WHERE_DISTINCT_UNIQUE: {
   771    772           sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
   772    773           break;
   773    774         }
   774    775   
   775    776         default: {
   776    777           assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
   777         -        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
          778  +        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
          779  +                     regResult);
   778    780           break;
   779    781         }
   780    782       }
   781    783       if( pSort==0 ){
   782    784         codeOffset(v, p->iOffset, iContinue);
   783    785       }
   784    786     }
................................................................................
   823    825         if( eDest==SRT_DistFifo ){
   824    826           /* If the destination is DistFifo, then cursor (iParm+1) is open
   825    827           ** on an ephemeral index. If the current row is already present
   826    828           ** in the index, do not write it to the output. If not, add the
   827    829           ** current row to the index and proceed with writing it to the
   828    830           ** output table as well.  */
   829    831           int addr = sqlite3VdbeCurrentAddr(v) + 4;
   830         -        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
          832  +        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
          833  +        VdbeCoverage(v);
   831    834           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
   832    835           assert( pSort==0 );
   833    836         }
   834    837   #endif
   835    838         if( pSort ){
   836    839           pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, 1, nPrefixReg);
   837    840         }else{
................................................................................
  1306   1309   ** The declaration type for any expression other than a column is NULL.
  1307   1310   **
  1308   1311   ** This routine has either 3 or 6 parameters depending on whether or not
  1309   1312   ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
  1310   1313   */
  1311   1314   #ifdef SQLITE_ENABLE_COLUMN_METADATA
  1312   1315   # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
         1316  +#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
         1317  +# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
         1318  +#endif
  1313   1319   static const char *columnTypeImpl(
  1314   1320     NameContext *pNC, 
  1315   1321     Expr *pExpr,
         1322  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1316   1323     const char **pzOrigDb,
  1317   1324     const char **pzOrigTab,
  1318   1325     const char **pzOrigCol,
         1326  +#endif
  1319   1327     u8 *pEstWidth
  1320   1328   ){
         1329  +  char const *zType = 0;
         1330  +  int j;
         1331  +  u8 estWidth = 1;
         1332  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1321   1333     char const *zOrigDb = 0;
  1322   1334     char const *zOrigTab = 0;
  1323   1335     char const *zOrigCol = 0;
  1324         -#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
  1325         -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
  1326         -static const char *columnTypeImpl(
  1327         -  NameContext *pNC, 
  1328         -  Expr *pExpr,
  1329         -  u8 *pEstWidth
  1330         -){
  1331         -#endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
  1332         -  char const *zType = 0;
  1333         -  int j;
  1334         -  u8 estWidth = 1;
         1336  +#endif
  1335   1337   
  1336   1338     if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
  1337   1339     switch( pExpr->op ){
  1338   1340       case TK_AGG_COLUMN:
  1339   1341       case TK_COLUMN: {
  1340   1342         /* The expression is a column. Locate the table the column is being
  1341   1343         ** extracted from in NameContext.pSrcList. This table may be real
................................................................................
  1701   1703     if( db->mallocFailed ) return;
  1702   1704     memset(&sNC, 0, sizeof(sNC));
  1703   1705     sNC.pSrcList = pSelect->pSrc;
  1704   1706     a = pSelect->pEList->a;
  1705   1707     for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
  1706   1708       p = a[i].pExpr;
  1707   1709       if( pCol->zType==0 ){
  1708         -      pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
         1710  +      pCol->zType = sqlite3DbStrDup(db, 
         1711  +                        columnType(&sNC, p,0,0,0, &pCol->szEst));
  1709   1712       }
  1710   1713       szAll += pCol->szEst;
  1711   1714       pCol->affinity = sqlite3ExprAffinity(p);
  1712   1715       if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
  1713   1716       pColl = sqlite3ExprCollSeq(pParse, p);
  1714   1717       if( pColl && pCol->zColl==0 ){
  1715   1718         pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
................................................................................
  3211   3214   **   (8)  The subquery does not use LIMIT or the outer query is not a join.
  3212   3215   **
  3213   3216   **   (9)  The subquery does not use LIMIT or the outer query does not use
  3214   3217   **        aggregates.
  3215   3218   **
  3216   3219   **  (**)  Restriction (10) was removed from the code on 2005-02-05 but we
  3217   3220   **        accidently carried the comment forward until 2014-09-15.  Original
  3218         -**        text: "The subquery does not use aggregates or the outer query does not
  3219         -**        use LIMIT."
         3221  +**        text: "The subquery does not use aggregates or the outer query 
         3222  +**        does not use LIMIT."
  3220   3223   **
  3221   3224   **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
  3222   3225   **
  3223   3226   **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
  3224   3227   **        a separate restriction deriving from ticket #350.
  3225   3228   **
  3226   3229   **  (13)  The subquery and outer query do not both use LIMIT.
................................................................................
  4810   4813       p->pOrderBy = 0;
  4811   4814       p->selFlags &= ~SF_Distinct;
  4812   4815     }
  4813   4816     sqlite3SelectPrep(pParse, p, 0);
  4814   4817     memset(&sSort, 0, sizeof(sSort));
  4815   4818     sSort.pOrderBy = p->pOrderBy;
  4816   4819     pTabList = p->pSrc;
  4817         -  pEList = p->pEList;
  4818   4820     if( pParse->nErr || db->mallocFailed ){
  4819   4821       goto select_end;
  4820   4822     }
         4823  +  assert( p->pEList!=0 );
  4821   4824     isAgg = (p->selFlags & SF_Aggregate)!=0;
  4822         -  assert( pEList!=0 );
  4823   4825   #if SELECTTRACE_ENABLED
  4824   4826     if( sqlite3SelectTrace & 0x100 ){
  4825   4827       SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
  4826   4828       sqlite3TreeViewSelect(0, p, 0);
  4827   4829     }
  4828   4830   #endif
  4829   4831   
  4830   4832   
  4831         -  /* Begin generating code.
  4832         -  */
  4833         -  v = sqlite3GetVdbe(pParse);
  4834         -  if( v==0 ) goto select_end;
  4835         -
  4836   4833     /* If writing to memory or generating a set
  4837   4834     ** only a single column may be output.
  4838   4835     */
  4839   4836   #ifndef SQLITE_OMIT_SUBQUERY
  4840         -  if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
         4837  +  if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){
  4841   4838       goto select_end;
  4842   4839     }
  4843   4840   #endif
         4841  +
         4842  +  /* Try to flatten subqueries in the FROM clause up into the main query
         4843  +  */
         4844  +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
         4845  +  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
         4846  +    struct SrcList_item *pItem = &pTabList->a[i];
         4847  +    Select *pSub = pItem->pSelect;
         4848  +    int isAggSub;
         4849  +    if( pSub==0 ) continue;
         4850  +    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
         4851  +    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
         4852  +      /* This subquery can be absorbed into its parent. */
         4853  +      if( isAggSub ){
         4854  +        isAgg = 1;
         4855  +        p->selFlags |= SF_Aggregate;
         4856  +      }
         4857  +      i = -1;
         4858  +    }
         4859  +    pTabList = p->pSrc;
         4860  +    if( db->mallocFailed ) goto select_end;
         4861  +    if( !IgnorableOrderby(pDest) ){
         4862  +      sSort.pOrderBy = p->pOrderBy;
         4863  +    }
         4864  +  }
         4865  +#endif
         4866  +
         4867  +  /* Get a pointer the VDBE under construction, allocating a new VDBE if one
         4868  +  ** does not already exist */
         4869  +  v = sqlite3GetVdbe(pParse);
         4870  +  if( v==0 ) goto select_end;
         4871  +
         4872  +#ifndef SQLITE_OMIT_COMPOUND_SELECT
         4873  +  /* Handle compound SELECT statements using the separate multiSelect()
         4874  +  ** procedure.
         4875  +  */
         4876  +  if( p->pPrior ){
         4877  +    rc = multiSelect(pParse, p, pDest);
         4878  +    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
         4879  +#if SELECTTRACE_ENABLED
         4880  +    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
         4881  +    pParse->nSelectIndent--;
         4882  +#endif
         4883  +    return rc;
         4884  +  }
         4885  +#endif
  4844   4886   
  4845   4887     /* Generate code for all sub-queries in the FROM clause
  4846   4888     */
  4847   4889   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  4848         -  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
         4890  +  for(i=0; i<pTabList->nSrc; i++){
  4849   4891       struct SrcList_item *pItem = &pTabList->a[i];
  4850   4892       SelectDest dest;
  4851   4893       Select *pSub = pItem->pSelect;
  4852         -    int isAggSub;
  4853         -
  4854   4894       if( pSub==0 ) continue;
  4855   4895   
  4856   4896       /* Sometimes the code for a subquery will be generated more than
  4857   4897       ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
  4858   4898       ** for example.  In that case, do not regenerate the code to manifest
  4859   4899       ** a view or the co-routine to implement a view.  The first instance
  4860   4900       ** is sufficient, though the subroutine to manifest the view does need
................................................................................
  4871   4911       ** may contain expression trees of at most
  4872   4912       ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
  4873   4913       ** more conservative than necessary, but much easier than enforcing
  4874   4914       ** an exact limit.
  4875   4915       */
  4876   4916       pParse->nHeight += sqlite3SelectExprHeight(p);
  4877   4917   
  4878         -    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
  4879         -    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
  4880         -      /* This subquery can be absorbed into its parent. */
  4881         -      if( isAggSub ){
  4882         -        isAgg = 1;
  4883         -        p->selFlags |= SF_Aggregate;
  4884         -      }
  4885         -      i = -1;
  4886         -    }else{
  4887         -      if( (pItem->jointype & JT_OUTER)==0
  4888         -       && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor)
  4889         -      ){
         4918  +    /* Make copies of constant WHERE-clause terms in the outer query down
         4919  +    ** inside the subquery.  This can help the subquery to run more efficiently.
         4920  +    */
         4921  +    if( (pItem->jointype & JT_OUTER)==0
         4922  +     && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor)
         4923  +    ){
  4890   4924   #if SELECTTRACE_ENABLED
  4891         -        if( sqlite3SelectTrace & 0x100 ){
  4892         -          SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
  4893         -          sqlite3TreeViewSelect(0, p, 0);
  4894         -        }
         4925  +      if( sqlite3SelectTrace & 0x100 ){
         4926  +        SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
         4927  +        sqlite3TreeViewSelect(0, p, 0);
         4928  +      }
  4895   4929   #endif
  4896         -      }
  4897         -      if( pTabList->nSrc==1
  4898         -       && (p->selFlags & SF_All)==0
  4899         -       && OptimizationEnabled(db, SQLITE_SubqCoroutine)
  4900         -      ){
  4901         -        /* Implement a co-routine that will return a single row of the result
  4902         -        ** set on each invocation.
  4903         -        */
  4904         -        int addrTop = sqlite3VdbeCurrentAddr(v)+1;
  4905         -        pItem->regReturn = ++pParse->nMem;
  4906         -        sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
  4907         -        VdbeComment((v, "%s", pItem->pTab->zName));
  4908         -        pItem->addrFillSub = addrTop;
  4909         -        sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
  4910         -        explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
  4911         -        sqlite3Select(pParse, pSub, &dest);
  4912         -        pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
  4913         -        pItem->viaCoroutine = 1;
  4914         -        pItem->regResult = dest.iSdst;
  4915         -        sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
  4916         -        sqlite3VdbeJumpHere(v, addrTop-1);
  4917         -        sqlite3ClearTempRegCache(pParse);
         4930  +    }
         4931  +
         4932  +    /* Generate code to implement the subquery
         4933  +    */
         4934  +    if( pTabList->nSrc==1
         4935  +     && (p->selFlags & SF_All)==0
         4936  +     && OptimizationEnabled(db, SQLITE_SubqCoroutine)
         4937  +    ){
         4938  +      /* Implement a co-routine that will return a single row of the result
         4939  +      ** set on each invocation.
         4940  +      */
         4941  +      int addrTop = sqlite3VdbeCurrentAddr(v)+1;
         4942  +      pItem->regReturn = ++pParse->nMem;
         4943  +      sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
         4944  +      VdbeComment((v, "%s", pItem->pTab->zName));
         4945  +      pItem->addrFillSub = addrTop;
         4946  +      sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
         4947  +      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
         4948  +      sqlite3Select(pParse, pSub, &dest);
         4949  +      pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
         4950  +      pItem->viaCoroutine = 1;
         4951  +      pItem->regResult = dest.iSdst;
         4952  +      sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
         4953  +      sqlite3VdbeJumpHere(v, addrTop-1);
         4954  +      sqlite3ClearTempRegCache(pParse);
         4955  +    }else{
         4956  +      /* Generate a subroutine that will fill an ephemeral table with
         4957  +      ** the content of this subquery.  pItem->addrFillSub will point
         4958  +      ** to the address of the generated subroutine.  pItem->regReturn
         4959  +      ** is a register allocated to hold the subroutine return address
         4960  +      */
         4961  +      int topAddr;
         4962  +      int onceAddr = 0;
         4963  +      int retAddr;
         4964  +      assert( pItem->addrFillSub==0 );
         4965  +      pItem->regReturn = ++pParse->nMem;
         4966  +      topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
         4967  +      pItem->addrFillSub = topAddr+1;
         4968  +      if( pItem->isCorrelated==0 ){
         4969  +        /* If the subquery is not correlated and if we are not inside of
         4970  +        ** a trigger, then we only need to compute the value of the subquery
         4971  +        ** once. */
         4972  +        onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
         4973  +        VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
  4918   4974         }else{
  4919         -        /* Generate a subroutine that will fill an ephemeral table with
  4920         -        ** the content of this subquery.  pItem->addrFillSub will point
  4921         -        ** to the address of the generated subroutine.  pItem->regReturn
  4922         -        ** is a register allocated to hold the subroutine return address
  4923         -        */
  4924         -        int topAddr;
  4925         -        int onceAddr = 0;
  4926         -        int retAddr;
  4927         -        assert( pItem->addrFillSub==0 );
  4928         -        pItem->regReturn = ++pParse->nMem;
  4929         -        topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
  4930         -        pItem->addrFillSub = topAddr+1;
  4931         -        if( pItem->isCorrelated==0 ){
  4932         -          /* If the subquery is not correlated and if we are not inside of
  4933         -          ** a trigger, then we only need to compute the value of the subquery
  4934         -          ** once. */
  4935         -          onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
  4936         -          VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
  4937         -        }else{
  4938         -          VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
  4939         -        }
  4940         -        sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
  4941         -        explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
  4942         -        sqlite3Select(pParse, pSub, &dest);
  4943         -        pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
  4944         -        if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
  4945         -        retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
  4946         -        VdbeComment((v, "end %s", pItem->pTab->zName));
  4947         -        sqlite3VdbeChangeP1(v, topAddr, retAddr);
  4948         -        sqlite3ClearTempRegCache(pParse);
         4975  +        VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
  4949   4976         }
         4977  +      sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
         4978  +      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
         4979  +      sqlite3Select(pParse, pSub, &dest);
         4980  +      pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
         4981  +      if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
         4982  +      retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
         4983  +      VdbeComment((v, "end %s", pItem->pTab->zName));
         4984  +      sqlite3VdbeChangeP1(v, topAddr, retAddr);
         4985  +      sqlite3ClearTempRegCache(pParse);
  4950   4986       }
  4951         -    if( db->mallocFailed ){
  4952         -      goto select_end;
  4953         -    }
         4987  +    if( db->mallocFailed ) goto select_end;
  4954   4988       pParse->nHeight -= sqlite3SelectExprHeight(p);
  4955         -    pTabList = p->pSrc;
  4956         -    if( !IgnorableOrderby(pDest) ){
  4957         -      sSort.pOrderBy = p->pOrderBy;
  4958         -    }
  4959   4989     }
  4960         -  pEList = p->pEList;
  4961   4990   #endif
         4991  +
         4992  +  /* Various elements of the SELECT copied into local variables for
         4993  +  ** convenience */
         4994  +  pEList = p->pEList;
  4962   4995     pWhere = p->pWhere;
  4963   4996     pGroupBy = p->pGroupBy;
  4964   4997     pHaving = p->pHaving;
  4965   4998     sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
  4966   4999   
  4967   5000   #if SELECTTRACE_ENABLED
  4968   5001     if( sqlite3SelectTrace & 0x400 ){
  4969   5002       SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
  4970   5003       sqlite3TreeViewSelect(0, p, 0);
  4971   5004     }
  4972         -#endif
  4973         -
  4974         -#ifndef SQLITE_OMIT_COMPOUND_SELECT
  4975         -  /* If there is are a sequence of queries, do the earlier ones first.
  4976         -  */
  4977         -  if( p->pPrior ){
  4978         -    rc = multiSelect(pParse, p, pDest);
  4979         -    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
  4980         -#if SELECTTRACE_ENABLED
  4981         -    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
  4982         -    pParse->nSelectIndent--;
  4983         -#endif
  4984         -    return rc;
  4985         -  }
  4986   5005   #endif
  4987   5006   
  4988   5007     /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
  4989   5008     ** if the select-list is the same as the ORDER BY list, then this query
  4990   5009     ** can be rewritten as a GROUP BY. In other words, this:
  4991   5010     **
  4992   5011     **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
................................................................................
  4997   5016     **
  4998   5017     ** The second form is preferred as a single index (or temp-table) may be 
  4999   5018     ** used for both the ORDER BY and DISTINCT processing. As originally 
  5000   5019     ** written the query must use a temp-table for at least one of the ORDER 
  5001   5020     ** BY and DISTINCT, and an index or separate temp-table for the other.
  5002   5021     */
  5003   5022     if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
  5004         -   && sqlite3ExprListCompare(sSort.pOrderBy, p->pEList, -1)==0
         5023  +   && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
  5005   5024     ){
  5006   5025       p->selFlags &= ~SF_Distinct;
  5007         -    p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
  5008         -    pGroupBy = p->pGroupBy;
         5026  +    pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
  5009   5027       /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
  5010   5028       ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
  5011   5029       ** original setting of the SF_Distinct flag, not the current setting */
  5012   5030       assert( sDistinct.isTnct );
  5013   5031     }
  5014   5032   
  5015         -  /* If there is an ORDER BY clause, then this sorting
  5016         -  ** index might end up being unused if the data can be 
  5017         -  ** extracted in pre-sorted order.  If that is the case, then the
  5018         -  ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
  5019         -  ** we figure out that the sorting index is not needed.  The addrSortIndex
  5020         -  ** variable is used to facilitate that change.
         5033  +  /* If there is an ORDER BY clause, then create an ephemeral index to
         5034  +  ** do the sorting.  But this sorting ephemeral index might end up
         5035  +  ** being unused if the data can be extracted in pre-sorted order.
         5036  +  ** If that is the case, then the OP_OpenEphemeral instruction will be
         5037  +  ** changed to an OP_Noop once we figure out that the sorting index is
         5038  +  ** not needed.  The sSort.addrSortIndex variable is used to facilitate
         5039  +  ** that change.
  5021   5040     */
  5022   5041     if( sSort.pOrderBy ){
  5023   5042       KeyInfo *pKeyInfo;
  5024   5043       pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
  5025   5044       sSort.iECursor = pParse->nTab++;
  5026   5045       sSort.addrSortIndex =
  5027   5046         sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
................................................................................
  5044   5063     p->nSelectRow = LARGEST_INT64;
  5045   5064     computeLimitRegisters(pParse, p, iEnd);
  5046   5065     if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
  5047   5066       sqlite3VdbeGetOp(v, sSort.addrSortIndex)->opcode = OP_SorterOpen;
  5048   5067       sSort.sortFlags |= SORTFLAG_UseSorter;
  5049   5068     }
  5050   5069   
  5051         -  /* Open a virtual index to use for the distinct set.
         5070  +  /* Open an ephemeral index to use for the distinct set.
  5052   5071     */
  5053   5072     if( p->selFlags & SF_Distinct ){
  5054   5073       sDistinct.tabTnct = pParse->nTab++;
  5055   5074       sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
  5056         -                                sDistinct.tabTnct, 0, 0,
  5057         -                                (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
  5058         -                                P4_KEYINFO);
         5075  +                             sDistinct.tabTnct, 0, 0,
         5076  +                             (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
         5077  +                             P4_KEYINFO);
  5059   5078       sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
  5060   5079       sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
  5061   5080     }else{
  5062   5081       sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
  5063   5082     }
  5064   5083   
  5065   5084     if( !isAgg && pGroupBy==0 ){
................................................................................
  5129   5148           pItem->u.x.iAlias = 0;
  5130   5149         }
  5131   5150         if( p->nSelectRow>100 ) p->nSelectRow = 100;
  5132   5151       }else{
  5133   5152         p->nSelectRow = 1;
  5134   5153       }
  5135   5154   
  5136         -
  5137   5155       /* If there is both a GROUP BY and an ORDER BY clause and they are
  5138   5156       ** identical, then it may be possible to disable the ORDER BY clause 
  5139   5157       ** on the grounds that the GROUP BY will cause elements to come out 
  5140         -    ** in the correct order. It also may not - the GROUP BY may use a
         5158  +    ** in the correct order. It also may not - the GROUP BY might use a
  5141   5159       ** database index that causes rows to be grouped together as required
  5142   5160       ** but not actually sorted. Either way, record the fact that the
  5143   5161       ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
  5144   5162       ** variable.  */
  5145   5163       if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
  5146   5164         orderByGrp = 1;
  5147   5165       }
................................................................................
  5311   5329         ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
  5312   5330         ** Then compare the current GROUP BY terms against the GROUP BY terms
  5313   5331         ** from the previous row currently stored in a0, a1, a2...
  5314   5332         */
  5315   5333         addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
  5316   5334         sqlite3ExprCacheClear(pParse);
  5317   5335         if( groupBySort ){
  5318         -        sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, sortOut,sortPTab);
         5336  +        sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
         5337  +                          sortOut, sortPTab);
  5319   5338         }
  5320   5339         for(j=0; j<pGroupBy->nExpr; j++){
  5321   5340           if( groupBySort ){
  5322   5341             sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
  5323   5342           }else{
  5324   5343             sAggInfo.directMode = 1;
  5325   5344             sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
................................................................................
  5383   5402         */
  5384   5403         addrSetAbort = sqlite3VdbeCurrentAddr(v);
  5385   5404         sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
  5386   5405         VdbeComment((v, "set abort flag"));
  5387   5406         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  5388   5407         sqlite3VdbeResolveLabel(v, addrOutputRow);
  5389   5408         addrOutputRow = sqlite3VdbeCurrentAddr(v);
  5390         -      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v);
         5409  +      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
         5410  +      VdbeCoverage(v);
  5391   5411         VdbeComment((v, "Groupby result generator entry point"));
  5392   5412         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  5393   5413         finalizeAggFunctions(pParse, &sAggInfo);
  5394   5414         sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
  5395   5415         selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
  5396   5416                         &sDistinct, pDest,
  5397   5417                         addrOutputRow+1, addrSetAbort);
................................................................................
  5547   5567       explainTempTable(pParse, "DISTINCT");
  5548   5568     }
  5549   5569   
  5550   5570     /* If there is an ORDER BY clause, then we need to sort the results
  5551   5571     ** and send them to the callback one by one.
  5552   5572     */
  5553   5573     if( sSort.pOrderBy ){
  5554         -    explainTempTable(pParse, sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
         5574  +    explainTempTable(pParse,
         5575  +                     sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
  5555   5576       generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
  5556   5577     }
  5557   5578   
  5558   5579     /* Jump here to skip this query
  5559   5580     */
  5560   5581     sqlite3VdbeResolveLabel(v, iEnd);
  5561   5582   
................................................................................
  5579   5600     sqlite3DbFree(db, sAggInfo.aFunc);
  5580   5601   #if SELECTTRACE_ENABLED
  5581   5602     SELECTTRACE(1,pParse,p,("end processing\n"));
  5582   5603     pParse->nSelectIndent--;
  5583   5604   #endif
  5584   5605     return rc;
  5585   5606   }
  5586         -
  5587         -#ifdef SQLITE_DEBUG
  5588         -/*
  5589         -** Generate a human-readable description of a the Select object.
  5590         -*/
  5591         -void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
  5592         -  int n = 0;
  5593         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
  5594         -  sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
  5595         -    ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
  5596         -    ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
  5597         -  );
  5598         -  if( p->pSrc && p->pSrc->nSrc ) n++;
  5599         -  if( p->pWhere ) n++;
  5600         -  if( p->pGroupBy ) n++;
  5601         -  if( p->pHaving ) n++;
  5602         -  if( p->pOrderBy ) n++;
  5603         -  if( p->pLimit ) n++;
  5604         -  if( p->pOffset ) n++;
  5605         -  if( p->pPrior ) n++;
  5606         -  sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
  5607         -  if( p->pSrc && p->pSrc->nSrc ){
  5608         -    int i;
  5609         -    pView = sqlite3TreeViewPush(pView, (n--)>0);
  5610         -    sqlite3TreeViewLine(pView, "FROM");
  5611         -    for(i=0; i<p->pSrc->nSrc; i++){
  5612         -      struct SrcList_item *pItem = &p->pSrc->a[i];
  5613         -      StrAccum x;
  5614         -      char zLine[100];
  5615         -      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
  5616         -      sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
  5617         -      if( pItem->zDatabase ){
  5618         -        sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
  5619         -      }else if( pItem->zName ){
  5620         -        sqlite3XPrintf(&x, 0, " %s", pItem->zName);
  5621         -      }
  5622         -      if( pItem->pTab ){
  5623         -        sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
  5624         -      }
  5625         -      if( pItem->zAlias ){
  5626         -        sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
  5627         -      }
  5628         -      if( pItem->jointype & JT_LEFT ){
  5629         -        sqlite3XPrintf(&x, 0, " LEFT-JOIN");
  5630         -      }
  5631         -      sqlite3StrAccumFinish(&x);
  5632         -      sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
  5633         -      if( pItem->pSelect ){
  5634         -        sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
  5635         -      }
  5636         -      sqlite3TreeViewPop(pView);
  5637         -    }
  5638         -    sqlite3TreeViewPop(pView);
  5639         -  }
  5640         -  if( p->pWhere ){
  5641         -    sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
  5642         -    sqlite3TreeViewExpr(pView, p->pWhere, 0);
  5643         -    sqlite3TreeViewPop(pView);
  5644         -  }
  5645         -  if( p->pGroupBy ){
  5646         -    sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
  5647         -  }
  5648         -  if( p->pHaving ){
  5649         -    sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
  5650         -    sqlite3TreeViewExpr(pView, p->pHaving, 0);
  5651         -    sqlite3TreeViewPop(pView);
  5652         -  }
  5653         -  if( p->pOrderBy ){
  5654         -    sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
  5655         -  }
  5656         -  if( p->pLimit ){
  5657         -    sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
  5658         -    sqlite3TreeViewExpr(pView, p->pLimit, 0);
  5659         -    sqlite3TreeViewPop(pView);
  5660         -  }
  5661         -  if( p->pOffset ){
  5662         -    sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
  5663         -    sqlite3TreeViewExpr(pView, p->pOffset, 0);
  5664         -    sqlite3TreeViewPop(pView);
  5665         -  }
  5666         -  if( p->pPrior ){
  5667         -    const char *zOp = "UNION";
  5668         -    switch( p->op ){
  5669         -      case TK_ALL:         zOp = "UNION ALL";  break;
  5670         -      case TK_INTERSECT:   zOp = "INTERSECT";  break;
  5671         -      case TK_EXCEPT:      zOp = "EXCEPT";     break;
  5672         -    }
  5673         -    sqlite3TreeViewItem(pView, zOp, (n--)>0);
  5674         -    sqlite3TreeViewSelect(pView, p->pPrior, 0);
  5675         -    sqlite3TreeViewPop(pView);
  5676         -  }
  5677         -  sqlite3TreeViewPop(pView);
  5678         -}
  5679         -#endif /* SQLITE_DEBUG */

Changes to src/sqliteInt.h.

  3166   3166     void sqlite3DebugPrintf(const char*, ...);
  3167   3167   #endif
  3168   3168   #if defined(SQLITE_TEST)
  3169   3169     void *sqlite3TestTextToPtr(const char*);
  3170   3170   #endif
  3171   3171   
  3172   3172   #if defined(SQLITE_DEBUG)
  3173         -  TreeView *sqlite3TreeViewPush(TreeView*,u8);
  3174         -  void sqlite3TreeViewPop(TreeView*);
  3175         -  void sqlite3TreeViewLine(TreeView*, const char*, ...);
  3176         -  void sqlite3TreeViewItem(TreeView*, const char*, u8);
  3177   3173     void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
  3178   3174     void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
  3179   3175     void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
  3180   3176   #endif
  3181   3177   
  3182   3178   
  3183   3179   void sqlite3SetString(char **, sqlite3*, const char*);

Added src/treeview.c.

            1  +/*
            2  +** 2015-06-08
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This file contains C code to implement the TreeView debugging routines.
           14  +** These routines print a parse tree to standard output for debugging and
           15  +** analysis. 
           16  +**
           17  +** The interfaces in this file is only available when compiling
           18  +** with SQLITE_DEBUG.
           19  +*/
           20  +#include "sqliteInt.h"
           21  +#ifdef SQLITE_DEBUG
           22  +
           23  +/*
           24  +** Add a new subitem to the tree.  The moreToFollow flag indicates that this
           25  +** is not the last item in the tree.
           26  +*/
           27  +static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
           28  +  if( p==0 ){
           29  +    p = sqlite3_malloc64( sizeof(*p) );
           30  +    if( p==0 ) return 0;
           31  +    memset(p, 0, sizeof(*p));
           32  +  }else{
           33  +    p->iLevel++;
           34  +  }
           35  +  assert( moreToFollow==0 || moreToFollow==1 );
           36  +  if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
           37  +  return p;
           38  +}
           39  +
           40  +/*
           41  +** Finished with one layer of the tree
           42  +*/
           43  +static void sqlite3TreeViewPop(TreeView *p){
           44  +  if( p==0 ) return;
           45  +  p->iLevel--;
           46  +  if( p->iLevel<0 ) sqlite3_free(p);
           47  +}
           48  +
           49  +/*
           50  +** Generate a single line of output for the tree, with a prefix that contains
           51  +** all the appropriate tree lines
           52  +*/
           53  +static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
           54  +  va_list ap;
           55  +  int i;
           56  +  StrAccum acc;
           57  +  char zBuf[500];
           58  +  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
           59  +  if( p ){
           60  +    for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
           61  +      sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
           62  +    }
           63  +    sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
           64  +  }
           65  +  va_start(ap, zFormat);
           66  +  sqlite3VXPrintf(&acc, 0, zFormat, ap);
           67  +  va_end(ap);
           68  +  if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
           69  +  sqlite3StrAccumFinish(&acc);
           70  +  fprintf(stdout,"%s", zBuf);
           71  +  fflush(stdout);
           72  +}
           73  +
           74  +/*
           75  +** Shorthand for starting a new tree item that consists of a single label
           76  +*/
           77  +static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
           78  +  p = sqlite3TreeViewPush(p, moreFollows);
           79  +  sqlite3TreeViewLine(p, "%s", zLabel);
           80  +}
           81  +
           82  +
           83  +/*
           84  +** Generate a human-readable description of a the Select object.
           85  +*/
           86  +void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
           87  +  int n = 0;
           88  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
           89  +  sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
           90  +    ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
           91  +    ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
           92  +  );
           93  +  if( p->pSrc && p->pSrc->nSrc ) n++;
           94  +  if( p->pWhere ) n++;
           95  +  if( p->pGroupBy ) n++;
           96  +  if( p->pHaving ) n++;
           97  +  if( p->pOrderBy ) n++;
           98  +  if( p->pLimit ) n++;
           99  +  if( p->pOffset ) n++;
          100  +  if( p->pPrior ) n++;
          101  +  sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
          102  +  if( p->pSrc && p->pSrc->nSrc ){
          103  +    int i;
          104  +    pView = sqlite3TreeViewPush(pView, (n--)>0);
          105  +    sqlite3TreeViewLine(pView, "FROM");
          106  +    for(i=0; i<p->pSrc->nSrc; i++){
          107  +      struct SrcList_item *pItem = &p->pSrc->a[i];
          108  +      StrAccum x;
          109  +      char zLine[100];
          110  +      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
          111  +      sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
          112  +      if( pItem->zDatabase ){
          113  +        sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
          114  +      }else if( pItem->zName ){
          115  +        sqlite3XPrintf(&x, 0, " %s", pItem->zName);
          116  +      }
          117  +      if( pItem->pTab ){
          118  +        sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
          119  +      }
          120  +      if( pItem->zAlias ){
          121  +        sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
          122  +      }
          123  +      if( pItem->jointype & JT_LEFT ){
          124  +        sqlite3XPrintf(&x, 0, " LEFT-JOIN");
          125  +      }
          126  +      sqlite3StrAccumFinish(&x);
          127  +      sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
          128  +      if( pItem->pSelect ){
          129  +        sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
          130  +      }
          131  +      sqlite3TreeViewPop(pView);
          132  +    }
          133  +    sqlite3TreeViewPop(pView);
          134  +  }
          135  +  if( p->pWhere ){
          136  +    sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
          137  +    sqlite3TreeViewExpr(pView, p->pWhere, 0);
          138  +    sqlite3TreeViewPop(pView);
          139  +  }
          140  +  if( p->pGroupBy ){
          141  +    sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
          142  +  }
          143  +  if( p->pHaving ){
          144  +    sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
          145  +    sqlite3TreeViewExpr(pView, p->pHaving, 0);
          146  +    sqlite3TreeViewPop(pView);
          147  +  }
          148  +  if( p->pOrderBy ){
          149  +    sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
          150  +  }
          151  +  if( p->pLimit ){
          152  +    sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
          153  +    sqlite3TreeViewExpr(pView, p->pLimit, 0);
          154  +    sqlite3TreeViewPop(pView);
          155  +  }
          156  +  if( p->pOffset ){
          157  +    sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
          158  +    sqlite3TreeViewExpr(pView, p->pOffset, 0);
          159  +    sqlite3TreeViewPop(pView);
          160  +  }
          161  +  if( p->pPrior ){
          162  +    const char *zOp = "UNION";
          163  +    switch( p->op ){
          164  +      case TK_ALL:         zOp = "UNION ALL";  break;
          165  +      case TK_INTERSECT:   zOp = "INTERSECT";  break;
          166  +      case TK_EXCEPT:      zOp = "EXCEPT";     break;
          167  +    }
          168  +    sqlite3TreeViewItem(pView, zOp, (n--)>0);
          169  +    sqlite3TreeViewSelect(pView, p->pPrior, 0);
          170  +    sqlite3TreeViewPop(pView);
          171  +  }
          172  +  sqlite3TreeViewPop(pView);
          173  +}
          174  +
          175  +/*
          176  +** Generate a human-readable explanation of an expression tree.
          177  +*/
          178  +void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
          179  +  const char *zBinOp = 0;   /* Binary operator */
          180  +  const char *zUniOp = 0;   /* Unary operator */
          181  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
          182  +  if( pExpr==0 ){
          183  +    sqlite3TreeViewLine(pView, "nil");
          184  +    sqlite3TreeViewPop(pView);
          185  +    return;
          186  +  }
          187  +  switch( pExpr->op ){
          188  +    case TK_AGG_COLUMN: {
          189  +      sqlite3TreeViewLine(pView, "AGG{%d:%d}",
          190  +            pExpr->iTable, pExpr->iColumn);
          191  +      break;
          192  +    }
          193  +    case TK_COLUMN: {
          194  +      if( pExpr->iTable<0 ){
          195  +        /* This only happens when coding check constraints */
          196  +        sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
          197  +      }else{
          198  +        sqlite3TreeViewLine(pView, "{%d:%d}",
          199  +                             pExpr->iTable, pExpr->iColumn);
          200  +      }
          201  +      break;
          202  +    }
          203  +    case TK_INTEGER: {
          204  +      if( pExpr->flags & EP_IntValue ){
          205  +        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
          206  +      }else{
          207  +        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
          208  +      }
          209  +      break;
          210  +    }
          211  +#ifndef SQLITE_OMIT_FLOATING_POINT
          212  +    case TK_FLOAT: {
          213  +      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
          214  +      break;
          215  +    }
          216  +#endif
          217  +    case TK_STRING: {
          218  +      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
          219  +      break;
          220  +    }
          221  +    case TK_NULL: {
          222  +      sqlite3TreeViewLine(pView,"NULL");
          223  +      break;
          224  +    }
          225  +#ifndef SQLITE_OMIT_BLOB_LITERAL
          226  +    case TK_BLOB: {
          227  +      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
          228  +      break;
          229  +    }
          230  +#endif
          231  +    case TK_VARIABLE: {
          232  +      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
          233  +                          pExpr->u.zToken, pExpr->iColumn);
          234  +      break;
          235  +    }
          236  +    case TK_REGISTER: {
          237  +      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
          238  +      break;
          239  +    }
          240  +    case TK_AS: {
          241  +      sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
          242  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
          243  +      break;
          244  +    }
          245  +    case TK_ID: {
          246  +      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
          247  +      break;
          248  +    }
          249  +#ifndef SQLITE_OMIT_CAST
          250  +    case TK_CAST: {
          251  +      /* Expressions of the form:   CAST(pLeft AS token) */
          252  +      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
          253  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
          254  +      break;
          255  +    }
          256  +#endif /* SQLITE_OMIT_CAST */
          257  +    case TK_LT:      zBinOp = "LT";     break;
          258  +    case TK_LE:      zBinOp = "LE";     break;
          259  +    case TK_GT:      zBinOp = "GT";     break;
          260  +    case TK_GE:      zBinOp = "GE";     break;
          261  +    case TK_NE:      zBinOp = "NE";     break;
          262  +    case TK_EQ:      zBinOp = "EQ";     break;
          263  +    case TK_IS:      zBinOp = "IS";     break;
          264  +    case TK_ISNOT:   zBinOp = "ISNOT";  break;
          265  +    case TK_AND:     zBinOp = "AND";    break;
          266  +    case TK_OR:      zBinOp = "OR";     break;
          267  +    case TK_PLUS:    zBinOp = "ADD";    break;
          268  +    case TK_STAR:    zBinOp = "MUL";    break;
          269  +    case TK_MINUS:   zBinOp = "SUB";    break;
          270  +    case TK_REM:     zBinOp = "REM";    break;
          271  +    case TK_BITAND:  zBinOp = "BITAND"; break;
          272  +    case TK_BITOR:   zBinOp = "BITOR";  break;
          273  +    case TK_SLASH:   zBinOp = "DIV";    break;
          274  +    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
          275  +    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
          276  +    case TK_CONCAT:  zBinOp = "CONCAT"; break;
          277  +    case TK_DOT:     zBinOp = "DOT";    break;
          278  +
          279  +    case TK_UMINUS:  zUniOp = "UMINUS"; break;
          280  +    case TK_UPLUS:   zUniOp = "UPLUS";  break;
          281  +    case TK_BITNOT:  zUniOp = "BITNOT"; break;
          282  +    case TK_NOT:     zUniOp = "NOT";    break;
          283  +    case TK_ISNULL:  zUniOp = "ISNULL"; break;
          284  +    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
          285  +
          286  +    case TK_COLLATE: {
          287  +      sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
          288  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
          289  +      break;
          290  +    }
          291  +
          292  +    case TK_AGG_FUNCTION:
          293  +    case TK_FUNCTION: {
          294  +      ExprList *pFarg;       /* List of function arguments */
          295  +      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
          296  +        pFarg = 0;
          297  +      }else{
          298  +        pFarg = pExpr->x.pList;
          299  +      }
          300  +      if( pExpr->op==TK_AGG_FUNCTION ){
          301  +        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
          302  +                             pExpr->op2, pExpr->u.zToken);
          303  +      }else{
          304  +        sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
          305  +      }
          306  +      if( pFarg ){
          307  +        sqlite3TreeViewExprList(pView, pFarg, 0, 0);
          308  +      }
          309  +      break;
          310  +    }
          311  +#ifndef SQLITE_OMIT_SUBQUERY
          312  +    case TK_EXISTS: {
          313  +      sqlite3TreeViewLine(pView, "EXISTS-expr");
          314  +      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
          315  +      break;
          316  +    }
          317  +    case TK_SELECT: {
          318  +      sqlite3TreeViewLine(pView, "SELECT-expr");
          319  +      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
          320  +      break;
          321  +    }
          322  +    case TK_IN: {
          323  +      sqlite3TreeViewLine(pView, "IN");
          324  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
          325  +      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
          326  +        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
          327  +      }else{
          328  +        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
          329  +      }
          330  +      break;
          331  +    }
          332  +#endif /* SQLITE_OMIT_SUBQUERY */
          333  +
          334  +    /*
          335  +    **    x BETWEEN y AND z
          336  +    **
          337  +    ** This is equivalent to
          338  +    **
          339  +    **    x>=y AND x<=z
          340  +    **
          341  +    ** X is stored in pExpr->pLeft.
          342  +    ** Y is stored in pExpr->pList->a[0].pExpr.
          343  +    ** Z is stored in pExpr->pList->a[1].pExpr.
          344  +    */
          345  +    case TK_BETWEEN: {
          346  +      Expr *pX = pExpr->pLeft;
          347  +      Expr *pY = pExpr->x.pList->a[0].pExpr;
          348  +      Expr *pZ = pExpr->x.pList->a[1].pExpr;
          349  +      sqlite3TreeViewLine(pView, "BETWEEN");
          350  +      sqlite3TreeViewExpr(pView, pX, 1);
          351  +      sqlite3TreeViewExpr(pView, pY, 1);
          352  +      sqlite3TreeViewExpr(pView, pZ, 0);
          353  +      break;
          354  +    }
          355  +    case TK_TRIGGER: {
          356  +      /* If the opcode is TK_TRIGGER, then the expression is a reference
          357  +      ** to a column in the new.* or old.* pseudo-tables available to
          358  +      ** trigger programs. In this case Expr.iTable is set to 1 for the
          359  +      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
          360  +      ** is set to the column of the pseudo-table to read, or to -1 to
          361  +      ** read the rowid field.
          362  +      */
          363  +      sqlite3TreeViewLine(pView, "%s(%d)", 
          364  +          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
          365  +      break;
          366  +    }
          367  +    case TK_CASE: {
          368  +      sqlite3TreeViewLine(pView, "CASE");
          369  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
          370  +      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
          371  +      break;
          372  +    }
          373  +#ifndef SQLITE_OMIT_TRIGGER
          374  +    case TK_RAISE: {
          375  +      const char *zType = "unk";
          376  +      switch( pExpr->affinity ){
          377  +        case OE_Rollback:   zType = "rollback";  break;
          378  +        case OE_Abort:      zType = "abort";     break;
          379  +        case OE_Fail:       zType = "fail";      break;
          380  +        case OE_Ignore:     zType = "ignore";    break;
          381  +      }
          382  +      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
          383  +      break;
          384  +    }
          385  +#endif
          386  +    default: {
          387  +      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
          388  +      break;
          389  +    }
          390  +  }
          391  +  if( zBinOp ){
          392  +    sqlite3TreeViewLine(pView, "%s", zBinOp);
          393  +    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
          394  +    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
          395  +  }else if( zUniOp ){
          396  +    sqlite3TreeViewLine(pView, "%s", zUniOp);
          397  +    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
          398  +  }
          399  +  sqlite3TreeViewPop(pView);
          400  +}
          401  +
          402  +/*
          403  +** Generate a human-readable explanation of an expression list.
          404  +*/
          405  +void sqlite3TreeViewExprList(
          406  +  TreeView *pView,
          407  +  const ExprList *pList,
          408  +  u8 moreToFollow,
          409  +  const char *zLabel
          410  +){
          411  +  int i;
          412  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
          413  +  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
          414  +  if( pList==0 ){
          415  +    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
          416  +  }else{
          417  +    sqlite3TreeViewLine(pView, "%s", zLabel);
          418  +    for(i=0; i<pList->nExpr; i++){
          419  +      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
          420  +    }
          421  +  }
          422  +  sqlite3TreeViewPop(pView);
          423  +}
          424  +
          425  +#endif /* SQLITE_DEBUG */

Changes to src/where.c.

    14     14   ** generating the code that loops through a table looking for applicable
    15     15   ** rows.  Indices are selected and used to speed the search when doing
    16     16   ** so is applicable.  Because this module is responsible for selecting
    17     17   ** indices, you might also think of this module as the "query optimizer".
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "whereInt.h"
           21  +
           22  +/* Forward declaration of methods */
           23  +static int whereLoopResize(sqlite3*, WhereLoop*, int);
           24  +
           25  +/* Test variable that can be set to enable WHERE tracing */
           26  +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
           27  +/***/ int sqlite3WhereTrace = 0;
           28  +#endif
           29  +
    21     30   
    22     31   /*
    23     32   ** Return the estimated number of output rows from a WHERE clause
    24     33   */
    25     34   u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
    26     35     return sqlite3LogEstToInt(pWInfo->nRowOut);
    27     36   }
................................................................................
   123    132   whereOrInsert_done:
   124    133     p->prereq = prereq;
   125    134     p->rRun = rRun;
   126    135     if( p->nOut>nOut ) p->nOut = nOut;
   127    136     return 1;
   128    137   }
   129    138   
   130         -/*
   131         -** Initialize a preallocated WhereClause structure.
   132         -*/
   133         -static void whereClauseInit(
   134         -  WhereClause *pWC,        /* The WhereClause to be initialized */
   135         -  WhereInfo *pWInfo        /* The WHERE processing context */
   136         -){
   137         -  pWC->pWInfo = pWInfo;
   138         -  pWC->pOuter = 0;
   139         -  pWC->nTerm = 0;
   140         -  pWC->nSlot = ArraySize(pWC->aStatic);
   141         -  pWC->a = pWC->aStatic;
   142         -}
   143         -
   144         -/* Forward reference */
   145         -static void whereClauseClear(WhereClause*);
   146         -
   147         -/*
   148         -** Deallocate all memory associated with a WhereOrInfo object.
   149         -*/
   150         -static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
   151         -  whereClauseClear(&p->wc);
   152         -  sqlite3DbFree(db, p);
   153         -}
   154         -
   155         -/*
   156         -** Deallocate all memory associated with a WhereAndInfo object.
   157         -*/
   158         -static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
   159         -  whereClauseClear(&p->wc);
   160         -  sqlite3DbFree(db, p);
   161         -}
   162         -
   163         -/*
   164         -** Deallocate a WhereClause structure.  The WhereClause structure
   165         -** itself is not freed.  This routine is the inverse of whereClauseInit().
   166         -*/
   167         -static void whereClauseClear(WhereClause *pWC){
   168         -  int i;
   169         -  WhereTerm *a;
   170         -  sqlite3 *db = pWC->pWInfo->pParse->db;
   171         -  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
   172         -    if( a->wtFlags & TERM_DYNAMIC ){
   173         -      sqlite3ExprDelete(db, a->pExpr);
   174         -    }
   175         -    if( a->wtFlags & TERM_ORINFO ){
   176         -      whereOrInfoDelete(db, a->u.pOrInfo);
   177         -    }else if( a->wtFlags & TERM_ANDINFO ){
   178         -      whereAndInfoDelete(db, a->u.pAndInfo);
   179         -    }
   180         -  }
   181         -  if( pWC->a!=pWC->aStatic ){
   182         -    sqlite3DbFree(db, pWC->a);
   183         -  }
   184         -}
   185         -
   186         -/*
   187         -** Add a single new WhereTerm entry to the WhereClause object pWC.
   188         -** The new WhereTerm object is constructed from Expr p and with wtFlags.
   189         -** The index in pWC->a[] of the new WhereTerm is returned on success.
   190         -** 0 is returned if the new WhereTerm could not be added due to a memory
   191         -** allocation error.  The memory allocation failure will be recorded in
   192         -** the db->mallocFailed flag so that higher-level functions can detect it.
   193         -**
   194         -** This routine will increase the size of the pWC->a[] array as necessary.
   195         -**
   196         -** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
   197         -** for freeing the expression p is assumed by the WhereClause object pWC.
   198         -** This is true even if this routine fails to allocate a new WhereTerm.
   199         -**
   200         -** WARNING:  This routine might reallocate the space used to store
   201         -** WhereTerms.  All pointers to WhereTerms should be invalidated after
   202         -** calling this routine.  Such pointers may be reinitialized by referencing
   203         -** the pWC->a[] array.
   204         -*/
   205         -static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
   206         -  WhereTerm *pTerm;
   207         -  int idx;
   208         -  testcase( wtFlags & TERM_VIRTUAL );
   209         -  if( pWC->nTerm>=pWC->nSlot ){
   210         -    WhereTerm *pOld = pWC->a;
   211         -    sqlite3 *db = pWC->pWInfo->pParse->db;
   212         -    pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
   213         -    if( pWC->a==0 ){
   214         -      if( wtFlags & TERM_DYNAMIC ){
   215         -        sqlite3ExprDelete(db, p);
   216         -      }
   217         -      pWC->a = pOld;
   218         -      return 0;
   219         -    }
   220         -    memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
   221         -    if( pOld!=pWC->aStatic ){
   222         -      sqlite3DbFree(db, pOld);
   223         -    }
   224         -    pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
   225         -    memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm));
   226         -  }
   227         -  pTerm = &pWC->a[idx = pWC->nTerm++];
   228         -  if( p && ExprHasProperty(p, EP_Unlikely) ){
   229         -    pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
   230         -  }else{
   231         -    pTerm->truthProb = 1;
   232         -  }
   233         -  pTerm->pExpr = sqlite3ExprSkipCollate(p);
   234         -  pTerm->wtFlags = wtFlags;
   235         -  pTerm->pWC = pWC;
   236         -  pTerm->iParent = -1;
   237         -  return idx;
   238         -}
   239         -
   240         -/*
   241         -** This routine identifies subexpressions in the WHERE clause where
   242         -** each subexpression is separated by the AND operator or some other
   243         -** operator specified in the op parameter.  The WhereClause structure
   244         -** is filled with pointers to subexpressions.  For example:
   245         -**
   246         -**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
   247         -**           \________/     \_______________/     \________________/
   248         -**            slot[0]            slot[1]               slot[2]
   249         -**
   250         -** The original WHERE clause in pExpr is unaltered.  All this routine
   251         -** does is make slot[] entries point to substructure within pExpr.
   252         -**
   253         -** In the previous sentence and in the diagram, "slot[]" refers to
   254         -** the WhereClause.a[] array.  The slot[] array grows as needed to contain
   255         -** all terms of the WHERE clause.
   256         -*/
   257         -static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
   258         -  Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
   259         -  pWC->op = op;
   260         -  if( pE2==0 ) return;
   261         -  if( pE2->op!=op ){
   262         -    whereClauseInsert(pWC, pExpr, 0);
   263         -  }else{
   264         -    whereSplit(pWC, pE2->pLeft, op);
   265         -    whereSplit(pWC, pE2->pRight, op);
   266         -  }
   267         -}
   268         -
   269         -/*
   270         -** Initialize a WhereMaskSet object
   271         -*/
   272         -#define initMaskSet(P)  (P)->n=0
   273         -
   274    139   /*
   275    140   ** Return the bitmask for the given cursor number.  Return 0 if
   276    141   ** iCursor is not in the set.
   277    142   */
   278         -static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
          143  +Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
   279    144     int i;
   280    145     assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
   281    146     for(i=0; i<pMaskSet->n; i++){
   282    147       if( pMaskSet->ix[i]==iCursor ){
   283    148         return MASKBIT(i);
   284    149       }
   285    150     }
................................................................................
   295    160   ** array will never overflow.
   296    161   */
   297    162   static void createMask(WhereMaskSet *pMaskSet, int iCursor){
   298    163     assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
   299    164     pMaskSet->ix[pMaskSet->n++] = iCursor;
   300    165   }
   301    166   
   302         -/*
   303         -** These routines walk (recursively) an expression tree and generate
   304         -** a bitmask indicating which tables are used in that expression
   305         -** tree.
   306         -*/
   307         -static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
   308         -static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
   309         -static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
   310         -  Bitmask mask = 0;
   311         -  if( p==0 ) return 0;
   312         -  if( p->op==TK_COLUMN ){
   313         -    mask = getMask(pMaskSet, p->iTable);
   314         -    return mask;
   315         -  }
   316         -  mask = exprTableUsage(pMaskSet, p->pRight);
   317         -  mask |= exprTableUsage(pMaskSet, p->pLeft);
   318         -  if( ExprHasProperty(p, EP_xIsSelect) ){
   319         -    mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
   320         -  }else{
   321         -    mask |= exprListTableUsage(pMaskSet, p->x.pList);
   322         -  }
   323         -  return mask;
   324         -}
   325         -static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
   326         -  int i;
   327         -  Bitmask mask = 0;
   328         -  if( pList ){
   329         -    for(i=0; i<pList->nExpr; i++){
   330         -      mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
   331         -    }
   332         -  }
   333         -  return mask;
   334         -}
   335         -static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
   336         -  Bitmask mask = 0;
   337         -  while( pS ){
   338         -    SrcList *pSrc = pS->pSrc;
   339         -    mask |= exprListTableUsage(pMaskSet, pS->pEList);
   340         -    mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
   341         -    mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
   342         -    mask |= exprTableUsage(pMaskSet, pS->pWhere);
   343         -    mask |= exprTableUsage(pMaskSet, pS->pHaving);
   344         -    if( ALWAYS(pSrc!=0) ){
   345         -      int i;
   346         -      for(i=0; i<pSrc->nSrc; i++){
   347         -        mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
   348         -        mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
   349         -      }
   350         -    }
   351         -    pS = pS->pPrior;
   352         -  }
   353         -  return mask;
   354         -}
   355         -
   356         -/*
   357         -** Return TRUE if the given operator is one of the operators that is
   358         -** allowed for an indexable WHERE clause term.  The allowed operators are
   359         -** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
   360         -*/
   361         -static int allowedOp(int op){
   362         -  assert( TK_GT>TK_EQ && TK_GT<TK_GE );
   363         -  assert( TK_LT>TK_EQ && TK_LT<TK_GE );
   364         -  assert( TK_LE>TK_EQ && TK_LE<TK_GE );
   365         -  assert( TK_GE==TK_EQ+4 );
   366         -  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
   367         -}
   368         -
   369         -/*
   370         -** Commute a comparison operator.  Expressions of the form "X op Y"
   371         -** are converted into "Y op X".
   372         -**
   373         -** If left/right precedence rules come into play when determining the
   374         -** collating sequence, then COLLATE operators are adjusted to ensure
   375         -** that the collating sequence does not change.  For example:
   376         -** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
   377         -** the left hand side of a comparison overrides any collation sequence 
   378         -** attached to the right. For the same reason the EP_Collate flag
   379         -** is not commuted.
   380         -*/
   381         -static void exprCommute(Parse *pParse, Expr *pExpr){
   382         -  u16 expRight = (pExpr->pRight->flags & EP_Collate);
   383         -  u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
   384         -  assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
   385         -  if( expRight==expLeft ){
   386         -    /* Either X and Y both have COLLATE operator or neither do */
   387         -    if( expRight ){
   388         -      /* Both X and Y have COLLATE operators.  Make sure X is always
   389         -      ** used by clearing the EP_Collate flag from Y. */
   390         -      pExpr->pRight->flags &= ~EP_Collate;
   391         -    }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
   392         -      /* Neither X nor Y have COLLATE operators, but X has a non-default
   393         -      ** collating sequence.  So add the EP_Collate marker on X to cause
   394         -      ** it to be searched first. */
   395         -      pExpr->pLeft->flags |= EP_Collate;
   396         -    }
   397         -  }
   398         -  SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
   399         -  if( pExpr->op>=TK_GT ){
   400         -    assert( TK_LT==TK_GT+2 );
   401         -    assert( TK_GE==TK_LE+2 );
   402         -    assert( TK_GT>TK_EQ );
   403         -    assert( TK_GT<TK_LE );
   404         -    assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
   405         -    pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
   406         -  }
   407         -}
   408         -
   409         -/*
   410         -** Translate from TK_xx operator to WO_xx bitmask.
   411         -*/
   412         -static u16 operatorMask(int op){
   413         -  u16 c;
   414         -  assert( allowedOp(op) );
   415         -  if( op==TK_IN ){
   416         -    c = WO_IN;
   417         -  }else if( op==TK_ISNULL ){
   418         -    c = WO_ISNULL;
   419         -  }else if( op==TK_IS ){
   420         -    c = WO_IS;
   421         -  }else{
   422         -    assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
   423         -    c = (u16)(WO_EQ<<(op-TK_EQ));
   424         -  }
   425         -  assert( op!=TK_ISNULL || c==WO_ISNULL );
   426         -  assert( op!=TK_IN || c==WO_IN );
   427         -  assert( op!=TK_EQ || c==WO_EQ );
   428         -  assert( op!=TK_LT || c==WO_LT );
   429         -  assert( op!=TK_LE || c==WO_LE );
   430         -  assert( op!=TK_GT || c==WO_GT );
   431         -  assert( op!=TK_GE || c==WO_GE );
   432         -  assert( op!=TK_IS || c==WO_IS );
   433         -  return c;
   434         -}
   435         -
   436    167   /*
   437    168   ** Advance to the next WhereTerm that matches according to the criteria
   438    169   ** established when the pScan object was initialized by whereScanInit().
   439    170   ** Return NULL if there are no more matching WhereTerms.
   440    171   */
   441    172   static WhereTerm *whereScanNext(WhereScan *pScan){
   442    173     int iCur;            /* The cursor on the LHS of the term */
................................................................................
   580    311   ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
   581    312   ** then try for the one with no dependencies on <expr> - in other words where
   582    313   ** <expr> is a constant expression of some kind.  Only return entries of
   583    314   ** the form "X <op> Y" where Y is a column in another table if no terms of
   584    315   ** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
   585    316   ** exist, try to return a term that does not use WO_EQUIV.
   586    317   */
   587         -static WhereTerm *findTerm(
          318  +WhereTerm *sqlite3WhereFindTerm(
   588    319     WhereClause *pWC,     /* The WHERE clause to be searched */
   589    320     int iCur,             /* Cursor number of LHS */
   590    321     int iColumn,          /* Column number of LHS */
   591    322     Bitmask notReady,     /* RHS must not overlap with this mask */
   592    323     u32 op,               /* Mask of WO_xx values describing operator */
   593    324     Index *pIdx           /* Must be compatible with this index, if not NULL */
   594    325   ){
................................................................................
   607    338         if( pResult==0 ) pResult = p;
   608    339       }
   609    340       p = whereScanNext(&scan);
   610    341     }
   611    342     return pResult;
   612    343   }
   613    344   
   614         -/* Forward reference */
   615         -static void exprAnalyze(SrcList*, WhereClause*, int);
   616         -
   617         -/*
   618         -** Call exprAnalyze on all terms in a WHERE clause.  
   619         -*/
   620         -static void exprAnalyzeAll(
   621         -  SrcList *pTabList,       /* the FROM clause */
   622         -  WhereClause *pWC         /* the WHERE clause to be analyzed */
   623         -){
   624         -  int i;
   625         -  for(i=pWC->nTerm-1; i>=0; i--){
   626         -    exprAnalyze(pTabList, pWC, i);
   627         -  }
   628         -}
   629         -
   630         -#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
   631         -/*
   632         -** Check to see if the given expression is a LIKE or GLOB operator that
   633         -** can be optimized using inequality constraints.  Return TRUE if it is
   634         -** so and false if not.
   635         -**
   636         -** In order for the operator to be optimizible, the RHS must be a string
   637         -** literal that does not begin with a wildcard.  The LHS must be a column
   638         -** that may only be NULL, a string, or a BLOB, never a number. (This means
   639         -** that virtual tables cannot participate in the LIKE optimization.)  The
   640         -** collating sequence for the column on the LHS must be appropriate for
   641         -** the operator.
   642         -*/
   643         -static int isLikeOrGlob(
   644         -  Parse *pParse,    /* Parsing and code generating context */
   645         -  Expr *pExpr,      /* Test this expression */
   646         -  Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
   647         -  int *pisComplete, /* True if the only wildcard is % in the last character */
   648         -  int *pnoCase      /* True if uppercase is equivalent to lowercase */
   649         -){
   650         -  const char *z = 0;         /* String on RHS of LIKE operator */
   651         -  Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
   652         -  ExprList *pList;           /* List of operands to the LIKE operator */
   653         -  int c;                     /* One character in z[] */
   654         -  int cnt;                   /* Number of non-wildcard prefix characters */
   655         -  char wc[3];                /* Wildcard characters */
   656         -  sqlite3 *db = pParse->db;  /* Database connection */
   657         -  sqlite3_value *pVal = 0;
   658         -  int op;                    /* Opcode of pRight */
   659         -
   660         -  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
   661         -    return 0;
   662         -  }
   663         -#ifdef SQLITE_EBCDIC
   664         -  if( *pnoCase ) return 0;
   665         -#endif
   666         -  pList = pExpr->x.pList;
   667         -  pLeft = pList->a[1].pExpr;
   668         -  if( pLeft->op!=TK_COLUMN 
   669         -   || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
   670         -   || IsVirtual(pLeft->pTab)  /* Value might be numeric */
   671         -  ){
   672         -    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
   673         -    ** be the name of an indexed column with TEXT affinity. */
   674         -    return 0;
   675         -  }
   676         -  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
   677         -
   678         -  pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
   679         -  op = pRight->op;
   680         -  if( op==TK_VARIABLE ){
   681         -    Vdbe *pReprepare = pParse->pReprepare;
   682         -    int iCol = pRight->iColumn;
   683         -    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
   684         -    if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
   685         -      z = (char *)sqlite3_value_text(pVal);
   686         -    }
   687         -    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
   688         -    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
   689         -  }else if( op==TK_STRING ){
   690         -    z = pRight->u.zToken;
   691         -  }
   692         -  if( z ){
   693         -    cnt = 0;
   694         -    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
   695         -      cnt++;
   696         -    }
   697         -    if( cnt!=0 && 255!=(u8)z[cnt-1] ){
   698         -      Expr *pPrefix;
   699         -      *pisComplete = c==wc[0] && z[cnt+1]==0;
   700         -      pPrefix = sqlite3Expr(db, TK_STRING, z);
   701         -      if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
   702         -      *ppPrefix = pPrefix;
   703         -      if( op==TK_VARIABLE ){
   704         -        Vdbe *v = pParse->pVdbe;
   705         -        sqlite3VdbeSetVarmask(v, pRight->iColumn);
   706         -        if( *pisComplete && pRight->u.zToken[1] ){
   707         -          /* If the rhs of the LIKE expression is a variable, and the current
   708         -          ** value of the variable means there is no need to invoke the LIKE
   709         -          ** function, then no OP_Variable will be added to the program.
   710         -          ** This causes problems for the sqlite3_bind_parameter_name()
   711         -          ** API. To work around them, add a dummy OP_Variable here.
   712         -          */ 
   713         -          int r1 = sqlite3GetTempReg(pParse);
   714         -          sqlite3ExprCodeTarget(pParse, pRight, r1);
   715         -          sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
   716         -          sqlite3ReleaseTempReg(pParse, r1);
   717         -        }
   718         -      }
   719         -    }else{
   720         -      z = 0;
   721         -    }
   722         -  }
   723         -
   724         -  sqlite3ValueFree(pVal);
   725         -  return (z!=0);
   726         -}
   727         -#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
   728         -
   729         -
   730         -#ifndef SQLITE_OMIT_VIRTUALTABLE
   731         -/*
   732         -** Check to see if the given expression is of the form
   733         -**
   734         -**         column MATCH expr
   735         -**
   736         -** If it is then return TRUE.  If not, return FALSE.
   737         -*/
   738         -static int isMatchOfColumn(
   739         -  Expr *pExpr      /* Test this expression */
   740         -){
   741         -  ExprList *pList;
   742         -
   743         -  if( pExpr->op!=TK_FUNCTION ){
   744         -    return 0;
   745         -  }
   746         -  if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
   747         -    return 0;
   748         -  }
   749         -  pList = pExpr->x.pList;
   750         -  if( pList->nExpr!=2 ){
   751         -    return 0;
   752         -  }
   753         -  if( pList->a[1].pExpr->op != TK_COLUMN ){
   754         -    return 0;
   755         -  }
   756         -  return 1;
   757         -}
   758         -#endif /* SQLITE_OMIT_VIRTUALTABLE */
   759         -
   760         -/*
   761         -** If the pBase expression originated in the ON or USING clause of
   762         -** a join, then transfer the appropriate markings over to derived.
   763         -*/
   764         -static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
   765         -  if( pDerived ){
   766         -    pDerived->flags |= pBase->flags & EP_FromJoin;
   767         -    pDerived->iRightJoinTable = pBase->iRightJoinTable;
   768         -  }
   769         -}
   770         -
   771         -/*
   772         -** Mark term iChild as being a child of term iParent
   773         -*/
   774         -static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
   775         -  pWC->a[iChild].iParent = iParent;
   776         -  pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
   777         -  pWC->a[iParent].nChild++;
   778         -}
   779         -
   780         -/*
   781         -** Return the N-th AND-connected subterm of pTerm.  Or if pTerm is not
   782         -** a conjunction, then return just pTerm when N==0.  If N is exceeds
   783         -** the number of available subterms, return NULL.
   784         -*/
   785         -static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
   786         -  if( pTerm->eOperator!=WO_AND ){
   787         -    return N==0 ? pTerm : 0;
   788         -  }
   789         -  if( N<pTerm->u.pAndInfo->wc.nTerm ){
   790         -    return &pTerm->u.pAndInfo->wc.a[N];
   791         -  }
   792         -  return 0;
   793         -}
   794         -
   795         -/*
   796         -** Subterms pOne and pTwo are contained within WHERE clause pWC.  The
   797         -** two subterms are in disjunction - they are OR-ed together.
   798         -**
   799         -** If these two terms are both of the form:  "A op B" with the same
   800         -** A and B values but different operators and if the operators are
   801         -** compatible (if one is = and the other is <, for example) then
   802         -** add a new virtual AND term to pWC that is the combination of the
   803         -** two.
   804         -**
   805         -** Some examples:
   806         -**
   807         -**    x<y OR x=y    -->     x<=y
   808         -**    x=y OR x=y    -->     x=y
   809         -**    x<=y OR x<y   -->     x<=y
   810         -**
   811         -** The following is NOT generated:
   812         -**
   813         -**    x<y OR x>y    -->     x!=y     
   814         -*/
   815         -static void whereCombineDisjuncts(
   816         -  SrcList *pSrc,         /* the FROM clause */
   817         -  WhereClause *pWC,      /* The complete WHERE clause */
   818         -  WhereTerm *pOne,       /* First disjunct */
   819         -  WhereTerm *pTwo        /* Second disjunct */
   820         -){
   821         -  u16 eOp = pOne->eOperator | pTwo->eOperator;
   822         -  sqlite3 *db;           /* Database connection (for malloc) */
   823         -  Expr *pNew;            /* New virtual expression */
   824         -  int op;                /* Operator for the combined expression */
   825         -  int idxNew;            /* Index in pWC of the next virtual term */
   826         -
   827         -  if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
   828         -  if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
   829         -  if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
   830         -   && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
   831         -  assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
   832         -  assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
   833         -  if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
   834         -  if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
   835         -  /* If we reach this point, it means the two subterms can be combined */
   836         -  if( (eOp & (eOp-1))!=0 ){
   837         -    if( eOp & (WO_LT|WO_LE) ){
   838         -      eOp = WO_LE;
   839         -    }else{
   840         -      assert( eOp & (WO_GT|WO_GE) );
   841         -      eOp = WO_GE;
   842         -    }
   843         -  }
   844         -  db = pWC->pWInfo->pParse->db;
   845         -  pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
   846         -  if( pNew==0 ) return;
   847         -  for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
   848         -  pNew->op = op;
   849         -  idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
   850         -  exprAnalyze(pSrc, pWC, idxNew);
   851         -}
   852         -
   853         -#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
   854         -/*
   855         -** Analyze a term that consists of two or more OR-connected
   856         -** subterms.  So in:
   857         -**
   858         -**     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
   859         -**                          ^^^^^^^^^^^^^^^^^^^^
   860         -**
   861         -** This routine analyzes terms such as the middle term in the above example.
   862         -** A WhereOrTerm object is computed and attached to the term under
   863         -** analysis, regardless of the outcome of the analysis.  Hence:
   864         -**
   865         -**     WhereTerm.wtFlags   |=  TERM_ORINFO
   866         -**     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
   867         -**
   868         -** The term being analyzed must have two or more of OR-connected subterms.
   869         -** A single subterm might be a set of AND-connected sub-subterms.
   870         -** Examples of terms under analysis:
   871         -**
   872         -**     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
   873         -**     (B)     x=expr1 OR expr2=x OR x=expr3
   874         -**     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
   875         -**     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
   876         -**     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
   877         -**     (F)     x>A OR (x=A AND y>=B)
   878         -**
   879         -** CASE 1:
   880         -**
   881         -** If all subterms are of the form T.C=expr for some single column of C and
   882         -** a single table T (as shown in example B above) then create a new virtual
   883         -** term that is an equivalent IN expression.  In other words, if the term
   884         -** being analyzed is:
   885         -**
   886         -**      x = expr1  OR  expr2 = x  OR  x = expr3
   887         -**
   888         -** then create a new virtual term like this:
   889         -**
   890         -**      x IN (expr1,expr2,expr3)
   891         -**
   892         -** CASE 2:
   893         -**
   894         -** If there are exactly two disjuncts one side has x>A and the other side
   895         -** has x=A (for the same x and A) then add a new virtual conjunct term to the
   896         -** WHERE clause of the form "x>=A".  Example:
   897         -**
   898         -**      x>A OR (x=A AND y>B)    adds:    x>=A
   899         -**
   900         -** The added conjunct can sometimes be helpful in query planning.
   901         -**
   902         -** CASE 3:
   903         -**
   904         -** If all subterms are indexable by a single table T, then set
   905         -**
   906         -**     WhereTerm.eOperator              =  WO_OR
   907         -**     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
   908         -**
   909         -** A subterm is "indexable" if it is of the form
   910         -** "T.C <op> <expr>" where C is any column of table T and 
   911         -** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
   912         -** A subterm is also indexable if it is an AND of two or more
   913         -** subsubterms at least one of which is indexable.  Indexable AND 
   914         -** subterms have their eOperator set to WO_AND and they have
   915         -** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
   916         -**
   917         -** From another point of view, "indexable" means that the subterm could
   918         -** potentially be used with an index if an appropriate index exists.
   919         -** This analysis does not consider whether or not the index exists; that
   920         -** is decided elsewhere.  This analysis only looks at whether subterms
   921         -** appropriate for indexing exist.
   922         -**
   923         -** All examples A through E above satisfy case 2.  But if a term
   924         -** also satisfies case 1 (such as B) we know that the optimizer will
   925         -** always prefer case 1, so in that case we pretend that case 2 is not
   926         -** satisfied.
   927         -**
   928         -** It might be the case that multiple tables are indexable.  For example,
   929         -** (E) above is indexable on tables P, Q, and R.
   930         -**
   931         -** Terms that satisfy case 2 are candidates for lookup by using
   932         -** separate indices to find rowids for each subterm and composing
   933         -** the union of all rowids using a RowSet object.  This is similar
   934         -** to "bitmap indices" in other database engines.
   935         -**
   936         -** OTHERWISE:
   937         -**
   938         -** If neither case 1 nor case 2 apply, then leave the eOperator set to
   939         -** zero.  This term is not useful for search.
   940         -*/
   941         -static void exprAnalyzeOrTerm(
   942         -  SrcList *pSrc,            /* the FROM clause */
   943         -  WhereClause *pWC,         /* the complete WHERE clause */
   944         -  int idxTerm               /* Index of the OR-term to be analyzed */
   945         -){
   946         -  WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
   947         -  Parse *pParse = pWInfo->pParse;         /* Parser context */
   948         -  sqlite3 *db = pParse->db;               /* Database connection */
   949         -  WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
   950         -  Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
   951         -  int i;                                  /* Loop counters */
   952         -  WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
   953         -  WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
   954         -  WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
   955         -  Bitmask chngToIN;         /* Tables that might satisfy case 1 */
   956         -  Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
   957         -
   958         -  /*
   959         -  ** Break the OR clause into its separate subterms.  The subterms are
   960         -  ** stored in a WhereClause structure containing within the WhereOrInfo
   961         -  ** object that is attached to the original OR clause term.
   962         -  */
   963         -  assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
   964         -  assert( pExpr->op==TK_OR );
   965         -  pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
   966         -  if( pOrInfo==0 ) return;
   967         -  pTerm->wtFlags |= TERM_ORINFO;
   968         -  pOrWc = &pOrInfo->wc;
   969         -  whereClauseInit(pOrWc, pWInfo);
   970         -  whereSplit(pOrWc, pExpr, TK_OR);
   971         -  exprAnalyzeAll(pSrc, pOrWc);
   972         -  if( db->mallocFailed ) return;
   973         -  assert( pOrWc->nTerm>=2 );
   974         -
   975         -  /*
   976         -  ** Compute the set of tables that might satisfy cases 1 or 2.
   977         -  */
   978         -  indexable = ~(Bitmask)0;
   979         -  chngToIN = ~(Bitmask)0;
   980         -  for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
   981         -    if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
   982         -      WhereAndInfo *pAndInfo;
   983         -      assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
   984         -      chngToIN = 0;
   985         -      pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
   986         -      if( pAndInfo ){
   987         -        WhereClause *pAndWC;
   988         -        WhereTerm *pAndTerm;
   989         -        int j;
   990         -        Bitmask b = 0;
   991         -        pOrTerm->u.pAndInfo = pAndInfo;
   992         -        pOrTerm->wtFlags |= TERM_ANDINFO;
   993         -        pOrTerm->eOperator = WO_AND;
   994         -        pAndWC = &pAndInfo->wc;
   995         -        whereClauseInit(pAndWC, pWC->pWInfo);
   996         -        whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
   997         -        exprAnalyzeAll(pSrc, pAndWC);
   998         -        pAndWC->pOuter = pWC;
   999         -        testcase( db->mallocFailed );
  1000         -        if( !db->mallocFailed ){
  1001         -          for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
  1002         -            assert( pAndTerm->pExpr );
  1003         -            if( allowedOp(pAndTerm->pExpr->op) ){
  1004         -              b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
  1005         -            }
  1006         -          }
  1007         -        }
  1008         -        indexable &= b;
  1009         -      }
  1010         -    }else if( pOrTerm->wtFlags & TERM_COPIED ){
  1011         -      /* Skip this term for now.  We revisit it when we process the
  1012         -      ** corresponding TERM_VIRTUAL term */
  1013         -    }else{
  1014         -      Bitmask b;
  1015         -      b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
  1016         -      if( pOrTerm->wtFlags & TERM_VIRTUAL ){
  1017         -        WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
  1018         -        b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
  1019         -      }
  1020         -      indexable &= b;
  1021         -      if( (pOrTerm->eOperator & WO_EQ)==0 ){
  1022         -        chngToIN = 0;
  1023         -      }else{
  1024         -        chngToIN &= b;
  1025         -      }
  1026         -    }
  1027         -  }
  1028         -
  1029         -  /*
  1030         -  ** Record the set of tables that satisfy case 3.  The set might be
  1031         -  ** empty.
  1032         -  */
  1033         -  pOrInfo->indexable = indexable;
  1034         -  pTerm->eOperator = indexable==0 ? 0 : WO_OR;
  1035         -
  1036         -  /* For a two-way OR, attempt to implementation case 2.
  1037         -  */
  1038         -  if( indexable && pOrWc->nTerm==2 ){
  1039         -    int iOne = 0;
  1040         -    WhereTerm *pOne;
  1041         -    while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
  1042         -      int iTwo = 0;
  1043         -      WhereTerm *pTwo;
  1044         -      while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
  1045         -        whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
  1046         -      }
  1047         -    }
  1048         -  }
  1049         -
  1050         -  /*
  1051         -  ** chngToIN holds a set of tables that *might* satisfy case 1.  But
  1052         -  ** we have to do some additional checking to see if case 1 really
  1053         -  ** is satisfied.
  1054         -  **
  1055         -  ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
  1056         -  ** that there is no possibility of transforming the OR clause into an
  1057         -  ** IN operator because one or more terms in the OR clause contain
  1058         -  ** something other than == on a column in the single table.  The 1-bit
  1059         -  ** case means that every term of the OR clause is of the form
  1060         -  ** "table.column=expr" for some single table.  The one bit that is set
  1061         -  ** will correspond to the common table.  We still need to check to make
  1062         -  ** sure the same column is used on all terms.  The 2-bit case is when
  1063         -  ** the all terms are of the form "table1.column=table2.column".  It
  1064         -  ** might be possible to form an IN operator with either table1.column
  1065         -  ** or table2.column as the LHS if either is common to every term of
  1066         -  ** the OR clause.
  1067         -  **
  1068         -  ** Note that terms of the form "table.column1=table.column2" (the
  1069         -  ** same table on both sizes of the ==) cannot be optimized.
  1070         -  */
  1071         -  if( chngToIN ){
  1072         -    int okToChngToIN = 0;     /* True if the conversion to IN is valid */
  1073         -    int iColumn = -1;         /* Column index on lhs of IN operator */
  1074         -    int iCursor = -1;         /* Table cursor common to all terms */
  1075         -    int j = 0;                /* Loop counter */
  1076         -
  1077         -    /* Search for a table and column that appears on one side or the
  1078         -    ** other of the == operator in every subterm.  That table and column
  1079         -    ** will be recorded in iCursor and iColumn.  There might not be any
  1080         -    ** such table and column.  Set okToChngToIN if an appropriate table
  1081         -    ** and column is found but leave okToChngToIN false if not found.
  1082         -    */
  1083         -    for(j=0; j<2 && !okToChngToIN; j++){
  1084         -      pOrTerm = pOrWc->a;
  1085         -      for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
  1086         -        assert( pOrTerm->eOperator & WO_EQ );
  1087         -        pOrTerm->wtFlags &= ~TERM_OR_OK;
  1088         -        if( pOrTerm->leftCursor==iCursor ){
  1089         -          /* This is the 2-bit case and we are on the second iteration and
  1090         -          ** current term is from the first iteration.  So skip this term. */
  1091         -          assert( j==1 );
  1092         -          continue;
  1093         -        }
  1094         -        if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
  1095         -          /* This term must be of the form t1.a==t2.b where t2 is in the
  1096         -          ** chngToIN set but t1 is not.  This term will be either preceded
  1097         -          ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
  1098         -          ** and use its inversion. */
  1099         -          testcase( pOrTerm->wtFlags & TERM_COPIED );
  1100         -          testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
  1101         -          assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
  1102         -          continue;
  1103         -        }
  1104         -        iColumn = pOrTerm->u.leftColumn;
  1105         -        iCursor = pOrTerm->leftCursor;
  1106         -        break;
  1107         -      }
  1108         -      if( i<0 ){
  1109         -        /* No candidate table+column was found.  This can only occur
  1110         -        ** on the second iteration */
  1111         -        assert( j==1 );
  1112         -        assert( IsPowerOfTwo(chngToIN) );
  1113         -        assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
  1114         -        break;
  1115         -      }
  1116         -      testcase( j==1 );
  1117         -
  1118         -      /* We have found a candidate table and column.  Check to see if that
  1119         -      ** table and column is common to every term in the OR clause */
  1120         -      okToChngToIN = 1;
  1121         -      for(; i>=0 && okToChngToIN; i--, pOrTerm++){
  1122         -        assert( pOrTerm->eOperator & WO_EQ );
  1123         -        if( pOrTerm->leftCursor!=iCursor ){
  1124         -          pOrTerm->wtFlags &= ~TERM_OR_OK;
  1125         -        }else if( pOrTerm->u.leftColumn!=iColumn ){
  1126         -          okToChngToIN = 0;
  1127         -        }else{
  1128         -          int affLeft, affRight;
  1129         -          /* If the right-hand side is also a column, then the affinities
  1130         -          ** of both right and left sides must be such that no type
  1131         -          ** conversions are required on the right.  (Ticket #2249)
  1132         -          */
  1133         -          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
  1134         -          affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
  1135         -          if( affRight!=0 && affRight!=affLeft ){
  1136         -            okToChngToIN = 0;
  1137         -          }else{
  1138         -            pOrTerm->wtFlags |= TERM_OR_OK;
  1139         -          }
  1140         -        }
  1141         -      }
  1142         -    }
  1143         -
  1144         -    /* At this point, okToChngToIN is true if original pTerm satisfies
  1145         -    ** case 1.  In that case, construct a new virtual term that is 
  1146         -    ** pTerm converted into an IN operator.
  1147         -    */
  1148         -    if( okToChngToIN ){
  1149         -      Expr *pDup;            /* A transient duplicate expression */
  1150         -      ExprList *pList = 0;   /* The RHS of the IN operator */
  1151         -      Expr *pLeft = 0;       /* The LHS of the IN operator */
  1152         -      Expr *pNew;            /* The complete IN operator */
  1153         -
  1154         -      for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
  1155         -        if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
  1156         -        assert( pOrTerm->eOperator & WO_EQ );
  1157         -        assert( pOrTerm->leftCursor==iCursor );
  1158         -        assert( pOrTerm->u.leftColumn==iColumn );
  1159         -        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
  1160         -        pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
  1161         -        pLeft = pOrTerm->pExpr->pLeft;
  1162         -      }
  1163         -      assert( pLeft!=0 );
  1164         -      pDup = sqlite3ExprDup(db, pLeft, 0);
  1165         -      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
  1166         -      if( pNew ){
  1167         -        int idxNew;
  1168         -        transferJoinMarkings(pNew, pExpr);
  1169         -        assert( !ExprHasProperty(pNew, EP_xIsSelect) );
  1170         -        pNew->x.pList = pList;
  1171         -        idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
  1172         -        testcase( idxNew==0 );
  1173         -        exprAnalyze(pSrc, pWC, idxNew);
  1174         -        pTerm = &pWC->a[idxTerm];
  1175         -        markTermAsChild(pWC, idxNew, idxTerm);
  1176         -      }else{
  1177         -        sqlite3ExprListDelete(db, pList);
  1178         -      }
  1179         -      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 3 */
  1180         -    }
  1181         -  }
  1182         -}
  1183         -#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
  1184         -
  1185         -/*
  1186         -** We already know that pExpr is a binary operator where both operands are
  1187         -** column references.  This routine checks to see if pExpr is an equivalence
  1188         -** relation:
  1189         -**   1.  The SQLITE_Transitive optimization must be enabled
  1190         -**   2.  Must be either an == or an IS operator
  1191         -**   3.  Not originating the ON clause of an OUTER JOIN
  1192         -**   4.  The affinities of A and B must be compatible
  1193         -**   5a. Both operands use the same collating sequence OR
  1194         -**   5b. The overall collating sequence is BINARY
  1195         -** If this routine returns TRUE, that means that the RHS can be substituted
  1196         -** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
  1197         -** This is an optimization.  No harm comes from returning 0.  But if 1 is
  1198         -** returned when it should not be, then incorrect answers might result.
  1199         -*/
  1200         -static int termIsEquivalence(Parse *pParse, Expr *pExpr){
  1201         -  char aff1, aff2;
  1202         -  CollSeq *pColl;
  1203         -  const char *zColl1, *zColl2;
  1204         -  if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
  1205         -  if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
  1206         -  if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
  1207         -  aff1 = sqlite3ExprAffinity(pExpr->pLeft);
  1208         -  aff2 = sqlite3ExprAffinity(pExpr->pRight);
  1209         -  if( aff1!=aff2
  1210         -   && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
  1211         -  ){
  1212         -    return 0;
  1213         -  }
  1214         -  pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
  1215         -  if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
  1216         -  pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  1217         -  /* Since pLeft and pRight are both a column references, their collating
  1218         -  ** sequence should always be defined. */
  1219         -  zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
  1220         -  pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
  1221         -  zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
  1222         -  return sqlite3StrICmp(zColl1, zColl2)==0;
  1223         -}
  1224         -
  1225         -/*
  1226         -** The input to this routine is an WhereTerm structure with only the
  1227         -** "pExpr" field filled in.  The job of this routine is to analyze the
  1228         -** subexpression and populate all the other fields of the WhereTerm
  1229         -** structure.
  1230         -**
  1231         -** If the expression is of the form "<expr> <op> X" it gets commuted
  1232         -** to the standard form of "X <op> <expr>".
  1233         -**
  1234         -** If the expression is of the form "X <op> Y" where both X and Y are
  1235         -** columns, then the original expression is unchanged and a new virtual
  1236         -** term of the form "Y <op> X" is added to the WHERE clause and
  1237         -** analyzed separately.  The original term is marked with TERM_COPIED
  1238         -** and the new term is marked with TERM_DYNAMIC (because it's pExpr
  1239         -** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
  1240         -** is a commuted copy of a prior term.)  The original term has nChild=1
  1241         -** and the copy has idxParent set to the index of the original term.
  1242         -*/
  1243         -static void exprAnalyze(
  1244         -  SrcList *pSrc,            /* the FROM clause */
  1245         -  WhereClause *pWC,         /* the WHERE clause */
  1246         -  int idxTerm               /* Index of the term to be analyzed */
  1247         -){
  1248         -  WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
  1249         -  WhereTerm *pTerm;                /* The term to be analyzed */
  1250         -  WhereMaskSet *pMaskSet;          /* Set of table index masks */
  1251         -  Expr *pExpr;                     /* The expression to be analyzed */
  1252         -  Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
  1253         -  Bitmask prereqAll;               /* Prerequesites of pExpr */
  1254         -  Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
  1255         -  Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
  1256         -  int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
  1257         -  int noCase = 0;                  /* uppercase equivalent to lowercase */
  1258         -  int op;                          /* Top-level operator.  pExpr->op */
  1259         -  Parse *pParse = pWInfo->pParse;  /* Parsing context */
  1260         -  sqlite3 *db = pParse->db;        /* Database connection */
  1261         -
  1262         -  if( db->mallocFailed ){
  1263         -    return;
  1264         -  }
  1265         -  pTerm = &pWC->a[idxTerm];
  1266         -  pMaskSet = &pWInfo->sMaskSet;
  1267         -  pExpr = pTerm->pExpr;
  1268         -  assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
  1269         -  prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
  1270         -  op = pExpr->op;
  1271         -  if( op==TK_IN ){
  1272         -    assert( pExpr->pRight==0 );
  1273         -    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  1274         -      pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
  1275         -    }else{
  1276         -      pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
  1277         -    }
  1278         -  }else if( op==TK_ISNULL ){
  1279         -    pTerm->prereqRight = 0;
  1280         -  }else{
  1281         -    pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
  1282         -  }
  1283         -  prereqAll = exprTableUsage(pMaskSet, pExpr);
  1284         -  if( ExprHasProperty(pExpr, EP_FromJoin) ){
  1285         -    Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
  1286         -    prereqAll |= x;
  1287         -    extraRight = x-1;  /* ON clause terms may not be used with an index
  1288         -                       ** on left table of a LEFT JOIN.  Ticket #3015 */
  1289         -  }
  1290         -  pTerm->prereqAll = prereqAll;
  1291         -  pTerm->leftCursor = -1;
  1292         -  pTerm->iParent = -1;
  1293         -  pTerm->eOperator = 0;
  1294         -  if( allowedOp(op) ){
  1295         -    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
  1296         -    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
  1297         -    u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
  1298         -    if( pLeft->op==TK_COLUMN ){
  1299         -      pTerm->leftCursor = pLeft->iTable;
  1300         -      pTerm->u.leftColumn = pLeft->iColumn;
  1301         -      pTerm->eOperator = operatorMask(op) & opMask;
  1302         -    }
  1303         -    if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
  1304         -    if( pRight && pRight->op==TK_COLUMN ){
  1305         -      WhereTerm *pNew;
  1306         -      Expr *pDup;
  1307         -      u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
  1308         -      if( pTerm->leftCursor>=0 ){
  1309         -        int idxNew;
  1310         -        pDup = sqlite3ExprDup(db, pExpr, 0);
  1311         -        if( db->mallocFailed ){
  1312         -          sqlite3ExprDelete(db, pDup);
  1313         -          return;
  1314         -        }
  1315         -        idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
  1316         -        if( idxNew==0 ) return;
  1317         -        pNew = &pWC->a[idxNew];
  1318         -        markTermAsChild(pWC, idxNew, idxTerm);
  1319         -        if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
  1320         -        pTerm = &pWC->a[idxTerm];
  1321         -        pTerm->wtFlags |= TERM_COPIED;
  1322         -
  1323         -        if( termIsEquivalence(pParse, pDup) ){
  1324         -          pTerm->eOperator |= WO_EQUIV;
  1325         -          eExtraOp = WO_EQUIV;
  1326         -        }
  1327         -      }else{
  1328         -        pDup = pExpr;
  1329         -        pNew = pTerm;
  1330         -      }
  1331         -      exprCommute(pParse, pDup);
  1332         -      pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
  1333         -      pNew->leftCursor = pLeft->iTable;
  1334         -      pNew->u.leftColumn = pLeft->iColumn;
  1335         -      testcase( (prereqLeft | extraRight) != prereqLeft );
  1336         -      pNew->prereqRight = prereqLeft | extraRight;
  1337         -      pNew->prereqAll = prereqAll;
  1338         -      pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
  1339         -    }
  1340         -  }
  1341         -
  1342         -#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
  1343         -  /* If a term is the BETWEEN operator, create two new virtual terms
  1344         -  ** that define the range that the BETWEEN implements.  For example:
  1345         -  **
  1346         -  **      a BETWEEN b AND c
  1347         -  **
  1348         -  ** is converted into:
  1349         -  **
  1350         -  **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
  1351         -  **
  1352         -  ** The two new terms are added onto the end of the WhereClause object.
  1353         -  ** The new terms are "dynamic" and are children of the original BETWEEN
  1354         -  ** term.  That means that if the BETWEEN term is coded, the children are
  1355         -  ** skipped.  Or, if the children are satisfied by an index, the original
  1356         -  ** BETWEEN term is skipped.
  1357         -  */
  1358         -  else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
  1359         -    ExprList *pList = pExpr->x.pList;
  1360         -    int i;
  1361         -    static const u8 ops[] = {TK_GE, TK_LE};
  1362         -    assert( pList!=0 );
  1363         -    assert( pList->nExpr==2 );
  1364         -    for(i=0; i<2; i++){
  1365         -      Expr *pNewExpr;
  1366         -      int idxNew;
  1367         -      pNewExpr = sqlite3PExpr(pParse, ops[i], 
  1368         -                             sqlite3ExprDup(db, pExpr->pLeft, 0),
  1369         -                             sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
  1370         -      transferJoinMarkings(pNewExpr, pExpr);
  1371         -      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  1372         -      testcase( idxNew==0 );
  1373         -      exprAnalyze(pSrc, pWC, idxNew);
  1374         -      pTerm = &pWC->a[idxTerm];
  1375         -      markTermAsChild(pWC, idxNew, idxTerm);
  1376         -    }
  1377         -  }
  1378         -#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
  1379         -
  1380         -#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
  1381         -  /* Analyze a term that is composed of two or more subterms connected by
  1382         -  ** an OR operator.
  1383         -  */
  1384         -  else if( pExpr->op==TK_OR ){
  1385         -    assert( pWC->op==TK_AND );
  1386         -    exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
  1387         -    pTerm = &pWC->a[idxTerm];
  1388         -  }
  1389         -#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  1390         -
  1391         -#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
  1392         -  /* Add constraints to reduce the search space on a LIKE or GLOB
  1393         -  ** operator.
  1394         -  **
  1395         -  ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
  1396         -  **
  1397         -  **          x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
  1398         -  **
  1399         -  ** The last character of the prefix "abc" is incremented to form the
  1400         -  ** termination condition "abd".  If case is not significant (the default
  1401         -  ** for LIKE) then the lower-bound is made all uppercase and the upper-
  1402         -  ** bound is made all lowercase so that the bounds also work when comparing
  1403         -  ** BLOBs.
  1404         -  */
  1405         -  if( pWC->op==TK_AND 
  1406         -   && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
  1407         -  ){
  1408         -    Expr *pLeft;       /* LHS of LIKE/GLOB operator */
  1409         -    Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
  1410         -    Expr *pNewExpr1;
  1411         -    Expr *pNewExpr2;
  1412         -    int idxNew1;
  1413         -    int idxNew2;
  1414         -    const char *zCollSeqName;     /* Name of collating sequence */
  1415         -    const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
  1416         -
  1417         -    pLeft = pExpr->x.pList->a[1].pExpr;
  1418         -    pStr2 = sqlite3ExprDup(db, pStr1, 0);
  1419         -
  1420         -    /* Convert the lower bound to upper-case and the upper bound to
  1421         -    ** lower-case (upper-case is less than lower-case in ASCII) so that
  1422         -    ** the range constraints also work for BLOBs
  1423         -    */
  1424         -    if( noCase && !pParse->db->mallocFailed ){
  1425         -      int i;
  1426         -      char c;
  1427         -      pTerm->wtFlags |= TERM_LIKE;
  1428         -      for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
  1429         -        pStr1->u.zToken[i] = sqlite3Toupper(c);
  1430         -        pStr2->u.zToken[i] = sqlite3Tolower(c);
  1431         -      }
  1432         -    }
  1433         -
  1434         -    if( !db->mallocFailed ){
  1435         -      u8 c, *pC;       /* Last character before the first wildcard */
  1436         -      pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
  1437         -      c = *pC;
  1438         -      if( noCase ){
  1439         -        /* The point is to increment the last character before the first
  1440         -        ** wildcard.  But if we increment '@', that will push it into the
  1441         -        ** alphabetic range where case conversions will mess up the 
  1442         -        ** inequality.  To avoid this, make sure to also run the full
  1443         -        ** LIKE on all candidate expressions by clearing the isComplete flag
  1444         -        */
  1445         -        if( c=='A'-1 ) isComplete = 0;
  1446         -        c = sqlite3UpperToLower[c];
  1447         -      }
  1448         -      *pC = c + 1;
  1449         -    }
  1450         -    zCollSeqName = noCase ? "NOCASE" : "BINARY";
  1451         -    pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
  1452         -    pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
  1453         -           sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
  1454         -           pStr1, 0);
  1455         -    transferJoinMarkings(pNewExpr1, pExpr);
  1456         -    idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
  1457         -    testcase( idxNew1==0 );
  1458         -    exprAnalyze(pSrc, pWC, idxNew1);
  1459         -    pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
  1460         -    pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
  1461         -           sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
  1462         -           pStr2, 0);
  1463         -    transferJoinMarkings(pNewExpr2, pExpr);
  1464         -    idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
  1465         -    testcase( idxNew2==0 );
  1466         -    exprAnalyze(pSrc, pWC, idxNew2);
  1467         -    pTerm = &pWC->a[idxTerm];
  1468         -    if( isComplete ){
  1469         -      markTermAsChild(pWC, idxNew1, idxTerm);
  1470         -      markTermAsChild(pWC, idxNew2, idxTerm);
  1471         -    }
  1472         -  }
  1473         -#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
  1474         -
  1475         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  1476         -  /* Add a WO_MATCH auxiliary term to the constraint set if the
  1477         -  ** current expression is of the form:  column MATCH expr.
  1478         -  ** This information is used by the xBestIndex methods of
  1479         -  ** virtual tables.  The native query optimizer does not attempt
  1480         -  ** to do anything with MATCH functions.
  1481         -  */
  1482         -  if( isMatchOfColumn(pExpr) ){
  1483         -    int idxNew;
  1484         -    Expr *pRight, *pLeft;
  1485         -    WhereTerm *pNewTerm;
  1486         -    Bitmask prereqColumn, prereqExpr;
  1487         -
  1488         -    pRight = pExpr->x.pList->a[0].pExpr;
  1489         -    pLeft = pExpr->x.pList->a[1].pExpr;
  1490         -    prereqExpr = exprTableUsage(pMaskSet, pRight);
  1491         -    prereqColumn = exprTableUsage(pMaskSet, pLeft);
  1492         -    if( (prereqExpr & prereqColumn)==0 ){
  1493         -      Expr *pNewExpr;
  1494         -      pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
  1495         -                              0, sqlite3ExprDup(db, pRight, 0), 0);
  1496         -      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  1497         -      testcase( idxNew==0 );
  1498         -      pNewTerm = &pWC->a[idxNew];
  1499         -      pNewTerm->prereqRight = prereqExpr;
  1500         -      pNewTerm->leftCursor = pLeft->iTable;
  1501         -      pNewTerm->u.leftColumn = pLeft->iColumn;
  1502         -      pNewTerm->eOperator = WO_MATCH;
  1503         -      markTermAsChild(pWC, idxNew, idxTerm);
  1504         -      pTerm = &pWC->a[idxTerm];
  1505         -      pTerm->wtFlags |= TERM_COPIED;
  1506         -      pNewTerm->prereqAll = pTerm->prereqAll;
  1507         -    }
  1508         -  }
  1509         -#endif /* SQLITE_OMIT_VIRTUALTABLE */
  1510         -
  1511         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1512         -  /* When sqlite_stat3 histogram data is available an operator of the
  1513         -  ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
  1514         -  ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
  1515         -  ** virtual term of that form.
  1516         -  **
  1517         -  ** Note that the virtual term must be tagged with TERM_VNULL.
  1518         -  */
  1519         -  if( pExpr->op==TK_NOTNULL
  1520         -   && pExpr->pLeft->op==TK_COLUMN
  1521         -   && pExpr->pLeft->iColumn>=0
  1522         -   && OptimizationEnabled(db, SQLITE_Stat34)
  1523         -  ){
  1524         -    Expr *pNewExpr;
  1525         -    Expr *pLeft = pExpr->pLeft;
  1526         -    int idxNew;
  1527         -    WhereTerm *pNewTerm;
  1528         -
  1529         -    pNewExpr = sqlite3PExpr(pParse, TK_GT,
  1530         -                            sqlite3ExprDup(db, pLeft, 0),
  1531         -                            sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
  1532         -
  1533         -    idxNew = whereClauseInsert(pWC, pNewExpr,
  1534         -                              TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
  1535         -    if( idxNew ){
  1536         -      pNewTerm = &pWC->a[idxNew];
  1537         -      pNewTerm->prereqRight = 0;
  1538         -      pNewTerm->leftCursor = pLeft->iTable;
  1539         -      pNewTerm->u.leftColumn = pLeft->iColumn;
  1540         -      pNewTerm->eOperator = WO_GT;
  1541         -      markTermAsChild(pWC, idxNew, idxTerm);
  1542         -      pTerm = &pWC->a[idxTerm];
  1543         -      pTerm->wtFlags |= TERM_COPIED;
  1544         -      pNewTerm->prereqAll = pTerm->prereqAll;
  1545         -    }
  1546         -  }
  1547         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1548         -
  1549         -  /* Prevent ON clause terms of a LEFT JOIN from being used to drive
  1550         -  ** an index for tables to the left of the join.
  1551         -  */
  1552         -  pTerm->prereqRight |= extraRight;
  1553         -}
  1554         -
  1555    345   /*
  1556    346   ** This function searches pList for an entry that matches the iCol-th column
  1557    347   ** of index pIdx.
  1558    348   **
  1559    349   ** If such an expression is found, its index in pList->a[] is returned. If
  1560    350   ** no expression is found, -1 is returned.
  1561    351   */
................................................................................
  1585    375     return -1;
  1586    376   }
  1587    377   
  1588    378   /*
  1589    379   ** Return true if the DISTINCT expression-list passed as the third argument
  1590    380   ** is redundant.
  1591    381   **
  1592         -** A DISTINCT list is redundant if the database contains some subset of
  1593         -** columns that are unique and non-null.
          382  +** A DISTINCT list is redundant if any subset of the columns in the
          383  +** DISTINCT list are collectively unique and individually non-null.
  1594    384   */
  1595    385   static int isDistinctRedundant(
  1596    386     Parse *pParse,            /* Parsing context */
  1597    387     SrcList *pTabList,        /* The FROM clause */
  1598    388     WhereClause *pWC,         /* The WHERE clause */
  1599    389     ExprList *pDistinct       /* The result set that needs to be DISTINCT */
  1600    390   ){
................................................................................
  1632    422     **   3. All of those index columns for which the WHERE clause does not
  1633    423     **      contain a "col=X" term are subject to a NOT NULL constraint.
  1634    424     */
  1635    425     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1636    426       if( !IsUniqueIndex(pIdx) ) continue;
  1637    427       for(i=0; i<pIdx->nKeyCol; i++){
  1638    428         i16 iCol = pIdx->aiColumn[i];
  1639         -      if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
          429  +      if( 0==sqlite3WhereFindTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
  1640    430           int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
  1641    431           if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
  1642    432             break;
  1643    433           }
  1644    434         }
  1645    435       }
  1646    436       if( i==pIdx->nKeyCol ){
................................................................................
  2754   1544       WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
  2755   1545     }
  2756   1546     assert( pBuilder->nRecValid==nRecValid );
  2757   1547     return rc;
  2758   1548   }
  2759   1549   #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  2760   1550   
  2761         -/*
  2762         -** Disable a term in the WHERE clause.  Except, do not disable the term
  2763         -** if it controls a LEFT OUTER JOIN and it did not originate in the ON
  2764         -** or USING clause of that join.
  2765         -**
  2766         -** Consider the term t2.z='ok' in the following queries:
  2767         -**
  2768         -**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
  2769         -**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
  2770         -**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
  2771         -**
  2772         -** The t2.z='ok' is disabled in the in (2) because it originates
  2773         -** in the ON clause.  The term is disabled in (3) because it is not part
  2774         -** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
  2775         -**
  2776         -** Disabling a term causes that term to not be tested in the inner loop
  2777         -** of the join.  Disabling is an optimization.  When terms are satisfied
  2778         -** by indices, we disable them to prevent redundant tests in the inner
  2779         -** loop.  We would get the correct results if nothing were ever disabled,
  2780         -** but joins might run a little slower.  The trick is to disable as much
  2781         -** as we can without disabling too much.  If we disabled in (1), we'd get
  2782         -** the wrong answer.  See ticket #813.
  2783         -**
  2784         -** If all the children of a term are disabled, then that term is also
  2785         -** automatically disabled.  In this way, terms get disabled if derived
  2786         -** virtual terms are tested first.  For example:
  2787         -**
  2788         -**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
  2789         -**      \___________/     \______/     \_____/
  2790         -**         parent          child1       child2
  2791         -**
  2792         -** Only the parent term was in the original WHERE clause.  The child1
  2793         -** and child2 terms were added by the LIKE optimization.  If both of
  2794         -** the virtual child terms are valid, then testing of the parent can be 
  2795         -** skipped.
  2796         -**
  2797         -** Usually the parent term is marked as TERM_CODED.  But if the parent
  2798         -** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
  2799         -** The TERM_LIKECOND marking indicates that the term should be coded inside
  2800         -** a conditional such that is only evaluated on the second pass of a
  2801         -** LIKE-optimization loop, when scanning BLOBs instead of strings.
  2802         -*/
  2803         -static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
  2804         -  int nLoop = 0;
  2805         -  while( pTerm
  2806         -      && (pTerm->wtFlags & TERM_CODED)==0
  2807         -      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
  2808         -      && (pLevel->notReady & pTerm->prereqAll)==0
  2809         -  ){
  2810         -    if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
  2811         -      pTerm->wtFlags |= TERM_LIKECOND;
  2812         -    }else{
  2813         -      pTerm->wtFlags |= TERM_CODED;
  2814         -    }
  2815         -    if( pTerm->iParent<0 ) break;
  2816         -    pTerm = &pTerm->pWC->a[pTerm->iParent];
  2817         -    pTerm->nChild--;
  2818         -    if( pTerm->nChild!=0 ) break;
  2819         -    nLoop++;
  2820         -  }
  2821         -}
  2822         -
  2823         -/*
  2824         -** Code an OP_Affinity opcode to apply the column affinity string zAff
  2825         -** to the n registers starting at base. 
  2826         -**
  2827         -** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
  2828         -** beginning and end of zAff are ignored.  If all entries in zAff are
  2829         -** SQLITE_AFF_BLOB, then no code gets generated.
  2830         -**
  2831         -** This routine makes its own copy of zAff so that the caller is free
  2832         -** to modify zAff after this routine returns.
  2833         -*/
  2834         -static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
  2835         -  Vdbe *v = pParse->pVdbe;
  2836         -  if( zAff==0 ){
  2837         -    assert( pParse->db->mallocFailed );
  2838         -    return;
  2839         -  }
  2840         -  assert( v!=0 );
  2841         -
  2842         -  /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
  2843         -  ** and end of the affinity string.
  2844         -  */
  2845         -  while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
  2846         -    n--;
  2847         -    base++;
  2848         -    zAff++;
  2849         -  }
  2850         -  while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
  2851         -    n--;
  2852         -  }
  2853         -
  2854         -  /* Code the OP_Affinity opcode if there is anything left to do. */
  2855         -  if( n>0 ){
  2856         -    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
  2857         -    sqlite3VdbeChangeP4(v, -1, zAff, n);
  2858         -    sqlite3ExprCacheAffinityChange(pParse, base, n);
  2859         -  }
  2860         -}
  2861         -
  2862         -
  2863         -/*
  2864         -** Generate code for a single equality term of the WHERE clause.  An equality
  2865         -** term can be either X=expr or X IN (...).   pTerm is the term to be 
  2866         -** coded.
  2867         -**
  2868         -** The current value for the constraint is left in register iReg.
  2869         -**
  2870         -** For a constraint of the form X=expr, the expression is evaluated and its
  2871         -** result is left on the stack.  For constraints of the form X IN (...)
  2872         -** this routine sets up a loop that will iterate over all values of X.
  2873         -*/
  2874         -static int codeEqualityTerm(
  2875         -  Parse *pParse,      /* The parsing context */
  2876         -  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
  2877         -  WhereLevel *pLevel, /* The level of the FROM clause we are working on */
  2878         -  int iEq,            /* Index of the equality term within this level */
  2879         -  int bRev,           /* True for reverse-order IN operations */
  2880         -  int iTarget         /* Attempt to leave results in this register */
  2881         -){
  2882         -  Expr *pX = pTerm->pExpr;
  2883         -  Vdbe *v = pParse->pVdbe;
  2884         -  int iReg;                  /* Register holding results */
  2885         -
  2886         -  assert( iTarget>0 );
  2887         -  if( pX->op==TK_EQ || pX->op==TK_IS ){
  2888         -    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
  2889         -  }else if( pX->op==TK_ISNULL ){
  2890         -    iReg = iTarget;
  2891         -    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
  2892         -#ifndef SQLITE_OMIT_SUBQUERY
  2893         -  }else{
  2894         -    int eType;
  2895         -    int iTab;
  2896         -    struct InLoop *pIn;
  2897         -    WhereLoop *pLoop = pLevel->pWLoop;
  2898         -
  2899         -    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
  2900         -      && pLoop->u.btree.pIndex!=0
  2901         -      && pLoop->u.btree.pIndex->aSortOrder[iEq]
  2902         -    ){
  2903         -      testcase( iEq==0 );
  2904         -      testcase( bRev );
  2905         -      bRev = !bRev;
  2906         -    }
  2907         -    assert( pX->op==TK_IN );
  2908         -    iReg = iTarget;
  2909         -    eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
  2910         -    if( eType==IN_INDEX_INDEX_DESC ){
  2911         -      testcase( bRev );
  2912         -      bRev = !bRev;
  2913         -    }
  2914         -    iTab = pX->iTable;
  2915         -    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
  2916         -    VdbeCoverageIf(v, bRev);
  2917         -    VdbeCoverageIf(v, !bRev);
  2918         -    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
  2919         -    pLoop->wsFlags |= WHERE_IN_ABLE;
  2920         -    if( pLevel->u.in.nIn==0 ){
  2921         -      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
  2922         -    }
  2923         -    pLevel->u.in.nIn++;
  2924         -    pLevel->u.in.aInLoop =
  2925         -       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
  2926         -                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
  2927         -    pIn = pLevel->u.in.aInLoop;
  2928         -    if( pIn ){
  2929         -      pIn += pLevel->u.in.nIn - 1;
  2930         -      pIn->iCur = iTab;
  2931         -      if( eType==IN_INDEX_ROWID ){
  2932         -        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
  2933         -      }else{
  2934         -        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
  2935         -      }
  2936         -      pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
  2937         -      sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
  2938         -    }else{
  2939         -      pLevel->u.in.nIn = 0;
  2940         -    }
  2941         -#endif
  2942         -  }
  2943         -  disableTerm(pLevel, pTerm);
  2944         -  return iReg;
  2945         -}
  2946         -
  2947         -/*
  2948         -** Generate code that will evaluate all == and IN constraints for an
  2949         -** index scan.
  2950         -**
  2951         -** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
  2952         -** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
  2953         -** The index has as many as three equality constraints, but in this
  2954         -** example, the third "c" value is an inequality.  So only two 
  2955         -** constraints are coded.  This routine will generate code to evaluate
  2956         -** a==5 and b IN (1,2,3).  The current values for a and b will be stored
  2957         -** in consecutive registers and the index of the first register is returned.
  2958         -**
  2959         -** In the example above nEq==2.  But this subroutine works for any value
  2960         -** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
  2961         -** The only thing it does is allocate the pLevel->iMem memory cell and
  2962         -** compute the affinity string.
  2963         -**
  2964         -** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
  2965         -** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
  2966         -** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
  2967         -** occurs after the nEq quality constraints.
  2968         -**
  2969         -** This routine allocates a range of nEq+nExtraReg memory cells and returns
  2970         -** the index of the first memory cell in that range. The code that
  2971         -** calls this routine will use that memory range to store keys for
  2972         -** start and termination conditions of the loop.
  2973         -** key value of the loop.  If one or more IN operators appear, then
  2974         -** this routine allocates an additional nEq memory cells for internal
  2975         -** use.
  2976         -**
  2977         -** Before returning, *pzAff is set to point to a buffer containing a
  2978         -** copy of the column affinity string of the index allocated using
  2979         -** sqlite3DbMalloc(). Except, entries in the copy of the string associated
  2980         -** with equality constraints that use BLOB or NONE affinity are set to
  2981         -** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
  2982         -**
  2983         -**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
  2984         -**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
  2985         -**
  2986         -** In the example above, the index on t1(a) has TEXT affinity. But since
  2987         -** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
  2988         -** no conversion should be attempted before using a t2.b value as part of
  2989         -** a key to search the index. Hence the first byte in the returned affinity
  2990         -** string in this example would be set to SQLITE_AFF_BLOB.
  2991         -*/
  2992         -static int codeAllEqualityTerms(
  2993         -  Parse *pParse,        /* Parsing context */
  2994         -  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
  2995         -  int bRev,             /* Reverse the order of IN operators */
  2996         -  int nExtraReg,        /* Number of extra registers to allocate */
  2997         -  char **pzAff          /* OUT: Set to point to affinity string */
  2998         -){
  2999         -  u16 nEq;                      /* The number of == or IN constraints to code */
  3000         -  u16 nSkip;                    /* Number of left-most columns to skip */
  3001         -  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
  3002         -  Index *pIdx;                  /* The index being used for this loop */
  3003         -  WhereTerm *pTerm;             /* A single constraint term */
  3004         -  WhereLoop *pLoop;             /* The WhereLoop object */
  3005         -  int j;                        /* Loop counter */
  3006         -  int regBase;                  /* Base register */
  3007         -  int nReg;                     /* Number of registers to allocate */
  3008         -  char *zAff;                   /* Affinity string to return */
  3009         -
  3010         -  /* This module is only called on query plans that use an index. */
  3011         -  pLoop = pLevel->pWLoop;
  3012         -  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
  3013         -  nEq = pLoop->u.btree.nEq;
  3014         -  nSkip = pLoop->nSkip;
  3015         -  pIdx = pLoop->u.btree.pIndex;
  3016         -  assert( pIdx!=0 );
  3017         -
  3018         -  /* Figure out how many memory cells we will need then allocate them.
  3019         -  */
  3020         -  regBase = pParse->nMem + 1;
  3021         -  nReg = pLoop->u.btree.nEq + nExtraReg;
  3022         -  pParse->nMem += nReg;
  3023         -
  3024         -  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
  3025         -  if( !zAff ){
  3026         -    pParse->db->mallocFailed = 1;
  3027         -  }
  3028         -
  3029         -  if( nSkip ){
  3030         -    int iIdxCur = pLevel->iIdxCur;
  3031         -    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
  3032         -    VdbeCoverageIf(v, bRev==0);
  3033         -    VdbeCoverageIf(v, bRev!=0);
  3034         -    VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
  3035         -    j = sqlite3VdbeAddOp0(v, OP_Goto);
  3036         -    pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
  3037         -                            iIdxCur, 0, regBase, nSkip);
  3038         -    VdbeCoverageIf(v, bRev==0);
  3039         -    VdbeCoverageIf(v, bRev!=0);
  3040         -    sqlite3VdbeJumpHere(v, j);
  3041         -    for(j=0; j<nSkip; j++){
  3042         -      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
  3043         -      assert( pIdx->aiColumn[j]>=0 );
  3044         -      VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
  3045         -    }
  3046         -  }    
  3047         -
  3048         -  /* Evaluate the equality constraints
  3049         -  */
  3050         -  assert( zAff==0 || (int)strlen(zAff)>=nEq );
  3051         -  for(j=nSkip; j<nEq; j++){
  3052         -    int r1;
  3053         -    pTerm = pLoop->aLTerm[j];
  3054         -    assert( pTerm!=0 );
  3055         -    /* The following testcase is true for indices with redundant columns. 
  3056         -    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
  3057         -    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
  3058         -    testcase( pTerm->wtFlags & TERM_VIRTUAL );
  3059         -    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
  3060         -    if( r1!=regBase+j ){
  3061         -      if( nReg==1 ){
  3062         -        sqlite3ReleaseTempReg(pParse, regBase);
  3063         -        regBase = r1;
  3064         -      }else{
  3065         -        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
  3066         -      }
  3067         -    }
  3068         -    testcase( pTerm->eOperator & WO_ISNULL );
  3069         -    testcase( pTerm->eOperator & WO_IN );
  3070         -    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
  3071         -      Expr *pRight = pTerm->pExpr->pRight;
  3072         -      if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
  3073         -        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
  3074         -        VdbeCoverage(v);
  3075         -      }
  3076         -      if( zAff ){
  3077         -        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
  3078         -          zAff[j] = SQLITE_AFF_BLOB;
  3079         -        }
  3080         -        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
  3081         -          zAff[j] = SQLITE_AFF_BLOB;
  3082         -        }
  3083         -      }
  3084         -    }
  3085         -  }
  3086         -  *pzAff = zAff;
  3087         -  return regBase;
  3088         -}
  3089         -
  3090         -#ifndef SQLITE_OMIT_EXPLAIN
  3091         -/*
  3092         -** This routine is a helper for explainIndexRange() below
  3093         -**
  3094         -** pStr holds the text of an expression that we are building up one term
  3095         -** at a time.  This routine adds a new term to the end of the expression.
  3096         -** Terms are separated by AND so add the "AND" text for second and subsequent
  3097         -** terms only.
  3098         -*/
  3099         -static void explainAppendTerm(
  3100         -  StrAccum *pStr,             /* The text expression being built */
  3101         -  int iTerm,                  /* Index of this term.  First is zero */
  3102         -  const char *zColumn,        /* Name of the column */
  3103         -  const char *zOp             /* Name of the operator */
  3104         -){
  3105         -  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
  3106         -  sqlite3StrAccumAppendAll(pStr, zColumn);
  3107         -  sqlite3StrAccumAppend(pStr, zOp, 1);
  3108         -  sqlite3StrAccumAppend(pStr, "?", 1);
  3109         -}
  3110         -
  3111         -/*
  3112         -** Argument pLevel describes a strategy for scanning table pTab. This 
  3113         -** function appends text to pStr that describes the subset of table
  3114         -** rows scanned by the strategy in the form of an SQL expression.
  3115         -**
  3116         -** For example, if the query:
  3117         -**
  3118         -**   SELECT * FROM t1 WHERE a=1 AND b>2;
  3119         -**
  3120         -** is run and there is an index on (a, b), then this function returns a
  3121         -** string similar to:
  3122         -**
  3123         -**   "a=? AND b>?"
  3124         -*/
  3125         -static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
  3126         -  Index *pIndex = pLoop->u.btree.pIndex;
  3127         -  u16 nEq = pLoop->u.btree.nEq;
  3128         -  u16 nSkip = pLoop->nSkip;
  3129         -  int i, j;
  3130         -  Column *aCol = pTab->aCol;
  3131         -  i16 *aiColumn = pIndex->aiColumn;
  3132         -
  3133         -  if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
  3134         -  sqlite3StrAccumAppend(pStr, " (", 2);
  3135         -  for(i=0; i<nEq; i++){
  3136         -    char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
  3137         -    if( i>=nSkip ){
  3138         -      explainAppendTerm(pStr, i, z, "=");
  3139         -    }else{
  3140         -      if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
  3141         -      sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
  3142         -    }
  3143         -  }
  3144         -
  3145         -  j = i;
  3146         -  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
  3147         -    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
  3148         -    explainAppendTerm(pStr, i++, z, ">");
  3149         -  }
  3150         -  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
  3151         -    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
  3152         -    explainAppendTerm(pStr, i, z, "<");
  3153         -  }
  3154         -  sqlite3StrAccumAppend(pStr, ")", 1);
  3155         -}
  3156         -
  3157         -/*
  3158         -** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
  3159         -** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
  3160         -** defined at compile-time. If it is not a no-op, a single OP_Explain opcode 
  3161         -** is added to the output to describe the table scan strategy in pLevel.
  3162         -**
  3163         -** If an OP_Explain opcode is added to the VM, its address is returned.
  3164         -** Otherwise, if no OP_Explain is coded, zero is returned.
  3165         -*/
  3166         -static int explainOneScan(
  3167         -  Parse *pParse,                  /* Parse context */
  3168         -  SrcList *pTabList,              /* Table list this loop refers to */
  3169         -  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
  3170         -  int iLevel,                     /* Value for "level" column of output */
  3171         -  int iFrom,                      /* Value for "from" column of output */
  3172         -  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
  3173         -){
  3174         -  int ret = 0;
  3175         -#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
  3176         -  if( pParse->explain==2 )
  3177         -#endif
  3178         -  {
  3179         -    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
  3180         -    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
  3181         -    sqlite3 *db = pParse->db;     /* Database handle */
  3182         -    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
  3183         -    int isSearch;                 /* True for a SEARCH. False for SCAN. */
  3184         -    WhereLoop *pLoop;             /* The controlling WhereLoop object */
  3185         -    u32 flags;                    /* Flags that describe this loop */
  3186         -    char *zMsg;                   /* Text to add to EQP output */
  3187         -    StrAccum str;                 /* EQP output string */
  3188         -    char zBuf[100];               /* Initial space for EQP output string */
  3189         -
  3190         -    pLoop = pLevel->pWLoop;
  3191         -    flags = pLoop->wsFlags;
  3192         -    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
  3193         -
  3194         -    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
  3195         -            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
  3196         -            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
  3197         -
  3198         -    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
  3199         -    sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
  3200         -    if( pItem->pSelect ){
  3201         -      sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
  3202         -    }else{
  3203         -      sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
  3204         -    }
  3205         -
  3206         -    if( pItem->zAlias ){
  3207         -      sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
  3208         -    }
  3209         -    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
  3210         -      const char *zFmt = 0;
  3211         -      Index *pIdx;
  3212         -
  3213         -      assert( pLoop->u.btree.pIndex!=0 );
  3214         -      pIdx = pLoop->u.btree.pIndex;
  3215         -      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
  3216         -      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
  3217         -        if( isSearch ){
  3218         -          zFmt = "PRIMARY KEY";
  3219         -        }
  3220         -      }else if( flags & WHERE_PARTIALIDX ){
  3221         -        zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
  3222         -      }else if( flags & WHERE_AUTO_INDEX ){
  3223         -        zFmt = "AUTOMATIC COVERING INDEX";
  3224         -      }else if( flags & WHERE_IDX_ONLY ){
  3225         -        zFmt = "COVERING INDEX %s";
  3226         -      }else{
  3227         -        zFmt = "INDEX %s";
  3228         -      }
  3229         -      if( zFmt ){
  3230         -        sqlite3StrAccumAppend(&str, " USING ", 7);
  3231         -        sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
  3232         -        explainIndexRange(&str, pLoop, pItem->pTab);
  3233         -      }
  3234         -    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
  3235         -      const char *zRange;
  3236         -      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
  3237         -        zRange = "(rowid=?)";
  3238         -      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
  3239         -        zRange = "(rowid>? AND rowid<?)";
  3240         -      }else if( flags&WHERE_BTM_LIMIT ){
  3241         -        zRange = "(rowid>?)";
  3242         -      }else{
  3243         -        assert( flags&WHERE_TOP_LIMIT);
  3244         -        zRange = "(rowid<?)";
  3245         -      }
  3246         -      sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
  3247         -      sqlite3StrAccumAppendAll(&str, zRange);
  3248         -    }
  3249         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  3250         -    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
  3251         -      sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
  3252         -                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
  3253         -    }
  3254         -#endif
  3255         -#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
  3256         -    if( pLoop->nOut>=10 ){
  3257         -      sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
  3258         -    }else{
  3259         -      sqlite3StrAccumAppend(&str, " (~1 row)", 9);
  3260         -    }
  3261         -#endif
  3262         -    zMsg = sqlite3StrAccumFinish(&str);
  3263         -    ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
  3264         -  }
  3265         -  return ret;
  3266         -}
  3267         -#else
  3268         -# define explainOneScan(u,v,w,x,y,z) 0
  3269         -#endif /* SQLITE_OMIT_EXPLAIN */
  3270         -
  3271         -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
  3272         -/*
  3273         -** Configure the VM passed as the first argument with an
  3274         -** sqlite3_stmt_scanstatus() entry corresponding to the scan used to 
  3275         -** implement level pLvl. Argument pSrclist is a pointer to the FROM 
  3276         -** clause that the scan reads data from.
  3277         -**
  3278         -** If argument addrExplain is not 0, it must be the address of an 
  3279         -** OP_Explain instruction that describes the same loop.
  3280         -*/
  3281         -static void addScanStatus(
  3282         -  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
  3283         -  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
  3284         -  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
  3285         -  int addrExplain                 /* Address of OP_Explain (or 0) */
  3286         -){
  3287         -  const char *zObj = 0;
  3288         -  WhereLoop *pLoop = pLvl->pWLoop;
  3289         -  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
  3290         -    zObj = pLoop->u.btree.pIndex->zName;
  3291         -  }else{
  3292         -    zObj = pSrclist->a[pLvl->iFrom].zName;
  3293         -  }
  3294         -  sqlite3VdbeScanStatus(
  3295         -      v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
  3296         -  );
  3297         -}
  3298         -#else
  3299         -# define addScanStatus(a, b, c, d) ((void)d)
  3300         -#endif
  3301         -
  3302         -/*
  3303         -** If the most recently coded instruction is a constant range contraint
  3304         -** that originated from the LIKE optimization, then change the P3 to be
  3305         -** pLoop->iLikeRepCntr and set P5.
  3306         -**
  3307         -** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
  3308         -** expression: "x>='ABC' AND x<'abd'".  But this requires that the range
  3309         -** scan loop run twice, once for strings and a second time for BLOBs.
  3310         -** The OP_String opcodes on the second pass convert the upper and lower
  3311         -** bound string contants to blobs.  This routine makes the necessary changes
  3312         -** to the OP_String opcodes for that to happen.
  3313         -*/
  3314         -static void whereLikeOptimizationStringFixup(
  3315         -  Vdbe *v,                /* prepared statement under construction */
  3316         -  WhereLevel *pLevel,     /* The loop that contains the LIKE operator */
  3317         -  WhereTerm *pTerm        /* The upper or lower bound just coded */
  3318         -){
  3319         -  if( pTerm->wtFlags & TERM_LIKEOPT ){
  3320         -    VdbeOp *pOp;
  3321         -    assert( pLevel->iLikeRepCntr>0 );
  3322         -    pOp = sqlite3VdbeGetOp(v, -1);
  3323         -    assert( pOp!=0 );
  3324         -    assert( pOp->opcode==OP_String8 
  3325         -            || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
  3326         -    pOp->p3 = pLevel->iLikeRepCntr;
  3327         -    pOp->p5 = 1;
  3328         -  }
  3329         -}
  3330         -
  3331         -/*
  3332         -** Generate code for the start of the iLevel-th loop in the WHERE clause
  3333         -** implementation described by pWInfo.
  3334         -*/
  3335         -static Bitmask codeOneLoopStart(
  3336         -  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
  3337         -  int iLevel,          /* Which level of pWInfo->a[] should be coded */
  3338         -  Bitmask notReady     /* Which tables are currently available */
  3339         -){
  3340         -  int j, k;            /* Loop counters */
  3341         -  int iCur;            /* The VDBE cursor for the table */
  3342         -  int addrNxt;         /* Where to jump to continue with the next IN case */
  3343         -  int omitTable;       /* True if we use the index only */
  3344         -  int bRev;            /* True if we need to scan in reverse order */
  3345         -  WhereLevel *pLevel;  /* The where level to be coded */
  3346         -  WhereLoop *pLoop;    /* The WhereLoop object being coded */
  3347         -  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
  3348         -  WhereTerm *pTerm;               /* A WHERE clause term */
  3349         -  Parse *pParse;                  /* Parsing context */
  3350         -  sqlite3 *db;                    /* Database connection */
  3351         -  Vdbe *v;                        /* The prepared stmt under constructions */
  3352         -  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
  3353         -  int addrBrk;                    /* Jump here to break out of the loop */
  3354         -  int addrCont;                   /* Jump here to continue with next cycle */
  3355         -  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
  3356         -  int iReleaseReg = 0;      /* Temp register to free before returning */
  3357         -
  3358         -  pParse = pWInfo->pParse;
  3359         -  v = pParse->pVdbe;
  3360         -  pWC = &pWInfo->sWC;
  3361         -  db = pParse->db;
  3362         -  pLevel = &pWInfo->a[iLevel];
  3363         -  pLoop = pLevel->pWLoop;
  3364         -  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
  3365         -  iCur = pTabItem->iCursor;
  3366         -  pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
  3367         -  bRev = (pWInfo->revMask>>iLevel)&1;
  3368         -  omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
  3369         -           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
  3370         -  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
  3371         -
  3372         -  /* Create labels for the "break" and "continue" instructions
  3373         -  ** for the current loop.  Jump to addrBrk to break out of a loop.
  3374         -  ** Jump to cont to go immediately to the next iteration of the
  3375         -  ** loop.
  3376         -  **
  3377         -  ** When there is an IN operator, we also have a "addrNxt" label that
  3378         -  ** means to continue with the next IN value combination.  When
  3379         -  ** there are no IN operators in the constraints, the "addrNxt" label
  3380         -  ** is the same as "addrBrk".
  3381         -  */
  3382         -  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
  3383         -  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
  3384         -
  3385         -  /* If this is the right table of a LEFT OUTER JOIN, allocate and
  3386         -  ** initialize a memory cell that records if this table matches any
  3387         -  ** row of the left table of the join.
  3388         -  */
  3389         -  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
  3390         -    pLevel->iLeftJoin = ++pParse->nMem;
  3391         -    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
  3392         -    VdbeComment((v, "init LEFT JOIN no-match flag"));
  3393         -  }
  3394         -
  3395         -  /* Special case of a FROM clause subquery implemented as a co-routine */
  3396         -  if( pTabItem->viaCoroutine ){
  3397         -    int regYield = pTabItem->regReturn;
  3398         -    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
  3399         -    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
  3400         -    VdbeCoverage(v);
  3401         -    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
  3402         -    pLevel->op = OP_Goto;
  3403         -  }else
  3404         -
  3405         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  3406         -  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  3407         -    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
  3408         -    **          to access the data.
  3409         -    */
  3410         -    int iReg;   /* P3 Value for OP_VFilter */
  3411         -    int addrNotFound;
  3412         -    int nConstraint = pLoop->nLTerm;
  3413         -
  3414         -    sqlite3ExprCachePush(pParse);
  3415         -    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
  3416         -    addrNotFound = pLevel->addrBrk;
  3417         -    for(j=0; j<nConstraint; j++){
  3418         -      int iTarget = iReg+j+2;
  3419         -      pTerm = pLoop->aLTerm[j];
  3420         -      if( pTerm==0 ) continue;
  3421         -      if( pTerm->eOperator & WO_IN ){
  3422         -        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
  3423         -        addrNotFound = pLevel->addrNxt;
  3424         -      }else{
  3425         -        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
  3426         -      }
  3427         -    }
  3428         -    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
  3429         -    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
  3430         -    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
  3431         -                      pLoop->u.vtab.idxStr,
  3432         -                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
  3433         -    VdbeCoverage(v);
  3434         -    pLoop->u.vtab.needFree = 0;
  3435         -    for(j=0; j<nConstraint && j<16; j++){
  3436         -      if( (pLoop->u.vtab.omitMask>>j)&1 ){
  3437         -        disableTerm(pLevel, pLoop->aLTerm[j]);
  3438         -      }
  3439         -    }
  3440         -    pLevel->op = OP_VNext;
  3441         -    pLevel->p1 = iCur;
  3442         -    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  3443         -    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
  3444         -    sqlite3ExprCachePop(pParse);
  3445         -  }else
  3446         -#endif /* SQLITE_OMIT_VIRTUALTABLE */
  3447         -
  3448         -  if( (pLoop->wsFlags & WHERE_IPK)!=0
  3449         -   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
  3450         -  ){
  3451         -    /* Case 2:  We can directly reference a single row using an
  3452         -    **          equality comparison against the ROWID field.  Or
  3453         -    **          we reference multiple rows using a "rowid IN (...)"
  3454         -    **          construct.
  3455         -    */
  3456         -    assert( pLoop->u.btree.nEq==1 );
  3457         -    pTerm = pLoop->aLTerm[0];
  3458         -    assert( pTerm!=0 );
  3459         -    assert( pTerm->pExpr!=0 );
  3460         -    assert( omitTable==0 );
  3461         -    testcase( pTerm->wtFlags & TERM_VIRTUAL );
  3462         -    iReleaseReg = ++pParse->nMem;
  3463         -    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
  3464         -    if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
  3465         -    addrNxt = pLevel->addrNxt;
  3466         -    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
  3467         -    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
  3468         -    VdbeCoverage(v);
  3469         -    sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
  3470         -    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  3471         -    VdbeComment((v, "pk"));
  3472         -    pLevel->op = OP_Noop;
  3473         -  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
  3474         -         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
  3475         -  ){
  3476         -    /* Case 3:  We have an inequality comparison against the ROWID field.
  3477         -    */
  3478         -    int testOp = OP_Noop;
  3479         -    int start;
  3480         -    int memEndValue = 0;
  3481         -    WhereTerm *pStart, *pEnd;
  3482         -
  3483         -    assert( omitTable==0 );
  3484         -    j = 0;
  3485         -    pStart = pEnd = 0;
  3486         -    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
  3487         -    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
  3488         -    assert( pStart!=0 || pEnd!=0 );
  3489         -    if( bRev ){
  3490         -      pTerm = pStart;
  3491         -      pStart = pEnd;
  3492         -      pEnd = pTerm;
  3493         -    }
  3494         -    if( pStart ){
  3495         -      Expr *pX;             /* The expression that defines the start bound */
  3496         -      int r1, rTemp;        /* Registers for holding the start boundary */
  3497         -
  3498         -      /* The following constant maps TK_xx codes into corresponding 
  3499         -      ** seek opcodes.  It depends on a particular ordering of TK_xx
  3500         -      */
  3501         -      const u8 aMoveOp[] = {
  3502         -           /* TK_GT */  OP_SeekGT,
  3503         -           /* TK_LE */  OP_SeekLE,
  3504         -           /* TK_LT */  OP_SeekLT,
  3505         -           /* TK_GE */  OP_SeekGE
  3506         -      };
  3507         -      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
  3508         -      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
  3509         -      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
  3510         -
  3511         -      assert( (pStart->wtFlags & TERM_VNULL)==0 );
  3512         -      testcase( pStart->wtFlags & TERM_VIRTUAL );
  3513         -      pX = pStart->pExpr;
  3514         -      assert( pX!=0 );
  3515         -      testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
  3516         -      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
  3517         -      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
  3518         -      VdbeComment((v, "pk"));
  3519         -      VdbeCoverageIf(v, pX->op==TK_GT);
  3520         -      VdbeCoverageIf(v, pX->op==TK_LE);
  3521         -      VdbeCoverageIf(v, pX->op==TK_LT);
  3522         -      VdbeCoverageIf(v, pX->op==TK_GE);
  3523         -      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
  3524         -      sqlite3ReleaseTempReg(pParse, rTemp);
  3525         -      disableTerm(pLevel, pStart);
  3526         -    }else{
  3527         -      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
  3528         -      VdbeCoverageIf(v, bRev==0);
  3529         -      VdbeCoverageIf(v, bRev!=0);
  3530         -    }
  3531         -    if( pEnd ){
  3532         -      Expr *pX;
  3533         -      pX = pEnd->pExpr;
  3534         -      assert( pX!=0 );
  3535         -      assert( (pEnd->wtFlags & TERM_VNULL)==0 );
  3536         -      testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
  3537         -      testcase( pEnd->wtFlags & TERM_VIRTUAL );
  3538         -      memEndValue = ++pParse->nMem;
  3539         -      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
  3540         -      if( pX->op==TK_LT || pX->op==TK_GT ){
  3541         -        testOp = bRev ? OP_Le : OP_Ge;
  3542         -      }else{
  3543         -        testOp = bRev ? OP_Lt : OP_Gt;
  3544         -      }
  3545         -      disableTerm(pLevel, pEnd);
  3546         -    }
  3547         -    start = sqlite3VdbeCurrentAddr(v);
  3548         -    pLevel->op = bRev ? OP_Prev : OP_Next;
  3549         -    pLevel->p1 = iCur;
  3550         -    pLevel->p2 = start;
  3551         -    assert( pLevel->p5==0 );
  3552         -    if( testOp!=OP_Noop ){
  3553         -      iRowidReg = ++pParse->nMem;
  3554         -      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
  3555         -      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  3556         -      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
  3557         -      VdbeCoverageIf(v, testOp==OP_Le);
  3558         -      VdbeCoverageIf(v, testOp==OP_Lt);
  3559         -      VdbeCoverageIf(v, testOp==OP_Ge);
  3560         -      VdbeCoverageIf(v, testOp==OP_Gt);
  3561         -      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
  3562         -    }
  3563         -  }else if( pLoop->wsFlags & WHERE_INDEXED ){
  3564         -    /* Case 4: A scan using an index.
  3565         -    **
  3566         -    **         The WHERE clause may contain zero or more equality 
  3567         -    **         terms ("==" or "IN" operators) that refer to the N
  3568         -    **         left-most columns of the index. It may also contain
  3569         -    **         inequality constraints (>, <, >= or <=) on the indexed
  3570         -    **         column that immediately follows the N equalities. Only 
  3571         -    **         the right-most column can be an inequality - the rest must
  3572         -    **         use the "==" and "IN" operators. For example, if the 
  3573         -    **         index is on (x,y,z), then the following clauses are all 
  3574         -    **         optimized:
  3575         -    **
  3576         -    **            x=5
  3577         -    **            x=5 AND y=10
  3578         -    **            x=5 AND y<10
  3579         -    **            x=5 AND y>5 AND y<10
  3580         -    **            x=5 AND y=5 AND z<=10
  3581         -    **
  3582         -    **         The z<10 term of the following cannot be used, only
  3583         -    **         the x=5 term:
  3584         -    **
  3585         -    **            x=5 AND z<10
  3586         -    **
  3587         -    **         N may be zero if there are inequality constraints.
  3588         -    **         If there are no inequality constraints, then N is at
  3589         -    **         least one.
  3590         -    **
  3591         -    **         This case is also used when there are no WHERE clause
  3592         -    **         constraints but an index is selected anyway, in order
  3593         -    **         to force the output order to conform to an ORDER BY.
  3594         -    */  
  3595         -    static const u8 aStartOp[] = {
  3596         -      0,
  3597         -      0,
  3598         -      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
  3599         -      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
  3600         -      OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
  3601         -      OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
  3602         -      OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
  3603         -      OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
  3604         -    };
  3605         -    static const u8 aEndOp[] = {
  3606         -      OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
  3607         -      OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
  3608         -      OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
  3609         -      OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
  3610         -    };
  3611         -    u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
  3612         -    int regBase;                 /* Base register holding constraint values */
  3613         -    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
  3614         -    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
  3615         -    int startEq;                 /* True if range start uses ==, >= or <= */
  3616         -    int endEq;                   /* True if range end uses ==, >= or <= */
  3617         -    int start_constraints;       /* Start of range is constrained */
  3618         -    int nConstraint;             /* Number of constraint terms */
  3619         -    Index *pIdx;                 /* The index we will be using */
  3620         -    int iIdxCur;                 /* The VDBE cursor for the index */
  3621         -    int nExtraReg = 0;           /* Number of extra registers needed */
  3622         -    int op;                      /* Instruction opcode */
  3623         -    char *zStartAff;             /* Affinity for start of range constraint */
  3624         -    char cEndAff = 0;            /* Affinity for end of range constraint */
  3625         -    u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
  3626         -    u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
  3627         -
  3628         -    pIdx = pLoop->u.btree.pIndex;
  3629         -    iIdxCur = pLevel->iIdxCur;
  3630         -    assert( nEq>=pLoop->nSkip );
  3631         -
  3632         -    /* If this loop satisfies a sort order (pOrderBy) request that 
  3633         -    ** was passed to this function to implement a "SELECT min(x) ..." 
  3634         -    ** query, then the caller will only allow the loop to run for
  3635         -    ** a single iteration. This means that the first row returned
  3636         -    ** should not have a NULL value stored in 'x'. If column 'x' is
  3637         -    ** the first one after the nEq equality constraints in the index,
  3638         -    ** this requires some special handling.
  3639         -    */
  3640         -    assert( pWInfo->pOrderBy==0
  3641         -         || pWInfo->pOrderBy->nExpr==1
  3642         -         || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
  3643         -    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
  3644         -     && pWInfo->nOBSat>0
  3645         -     && (pIdx->nKeyCol>nEq)
  3646         -    ){
  3647         -      assert( pLoop->nSkip==0 );
  3648         -      bSeekPastNull = 1;
  3649         -      nExtraReg = 1;
  3650         -    }
  3651         -
  3652         -    /* Find any inequality constraint terms for the start and end 
  3653         -    ** of the range. 
  3654         -    */
  3655         -    j = nEq;
  3656         -    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
  3657         -      pRangeStart = pLoop->aLTerm[j++];
  3658         -      nExtraReg = 1;
  3659         -      /* Like optimization range constraints always occur in pairs */
  3660         -      assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || 
  3661         -              (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
  3662         -    }
  3663         -    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
  3664         -      pRangeEnd = pLoop->aLTerm[j++];
  3665         -      nExtraReg = 1;
  3666         -      if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
  3667         -        assert( pRangeStart!=0 );                     /* LIKE opt constraints */
  3668         -        assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
  3669         -        pLevel->iLikeRepCntr = ++pParse->nMem;
  3670         -        testcase( bRev );
  3671         -        testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
  3672         -        sqlite3VdbeAddOp2(v, OP_Integer,
  3673         -                          bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
  3674         -                          pLevel->iLikeRepCntr);
  3675         -        VdbeComment((v, "LIKE loop counter"));
  3676         -        pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
  3677         -      }
  3678         -      if( pRangeStart==0
  3679         -       && (j = pIdx->aiColumn[nEq])>=0 
  3680         -       && pIdx->pTable->aCol[j].notNull==0
  3681         -      ){
  3682         -        bSeekPastNull = 1;
  3683         -      }
  3684         -    }
  3685         -    assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
  3686         -
  3687         -    /* Generate code to evaluate all constraint terms using == or IN
  3688         -    ** and store the values of those terms in an array of registers
  3689         -    ** starting at regBase.
  3690         -    */
  3691         -    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
  3692         -    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
  3693         -    if( zStartAff ) cEndAff = zStartAff[nEq];
  3694         -    addrNxt = pLevel->addrNxt;
  3695         -
  3696         -    /* If we are doing a reverse order scan on an ascending index, or
  3697         -    ** a forward order scan on a descending index, interchange the 
  3698         -    ** start and end terms (pRangeStart and pRangeEnd).
  3699         -    */
  3700         -    if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
  3701         -     || (bRev && pIdx->nKeyCol==nEq)
  3702         -    ){
  3703         -      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
  3704         -      SWAP(u8, bSeekPastNull, bStopAtNull);
  3705         -    }
  3706         -
  3707         -    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
  3708         -    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
  3709         -    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
  3710         -    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
  3711         -    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
  3712         -    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
  3713         -    start_constraints = pRangeStart || nEq>0;
  3714         -
  3715         -    /* Seek the index cursor to the start of the range. */
  3716         -    nConstraint = nEq;
  3717         -    if( pRangeStart ){
  3718         -      Expr *pRight = pRangeStart->pExpr->pRight;
  3719         -      sqlite3ExprCode(pParse, pRight, regBase+nEq);
  3720         -      whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
  3721         -      if( (pRangeStart->wtFlags & TERM_VNULL)==0
  3722         -       && sqlite3ExprCanBeNull(pRight)
  3723         -      ){
  3724         -        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
  3725         -        VdbeCoverage(v);
  3726         -      }
  3727         -      if( zStartAff ){
  3728         -        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){
  3729         -          /* Since the comparison is to be performed with no conversions
  3730         -          ** applied to the operands, set the affinity to apply to pRight to 
  3731         -          ** SQLITE_AFF_BLOB.  */
  3732         -          zStartAff[nEq] = SQLITE_AFF_BLOB;
  3733         -        }
  3734         -        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
  3735         -          zStartAff[nEq] = SQLITE_AFF_BLOB;
  3736         -        }
  3737         -      }  
  3738         -      nConstraint++;
  3739         -      testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
  3740         -    }else if( bSeekPastNull ){
  3741         -      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
  3742         -      nConstraint++;
  3743         -      startEq = 0;
  3744         -      start_constraints = 1;
  3745         -    }
  3746         -    codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
  3747         -    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
  3748         -    assert( op!=0 );
  3749         -    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
  3750         -    VdbeCoverage(v);
  3751         -    VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
  3752         -    VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
  3753         -    VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
  3754         -    VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
  3755         -    VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
  3756         -    VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
  3757         -
  3758         -    /* Load the value for the inequality constraint at the end of the
  3759         -    ** range (if any).
  3760         -    */
  3761         -    nConstraint = nEq;
  3762         -    if( pRangeEnd ){
  3763         -      Expr *pRight = pRangeEnd->pExpr->pRight;
  3764         -      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
  3765         -      sqlite3ExprCode(pParse, pRight, regBase+nEq);
  3766         -      whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
  3767         -      if( (pRangeEnd->wtFlags & TERM_VNULL)==0
  3768         -       && sqlite3ExprCanBeNull(pRight)
  3769         -      ){
  3770         -        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
  3771         -        VdbeCoverage(v);
  3772         -      }
  3773         -      if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB
  3774         -       && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
  3775         -      ){
  3776         -        codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
  3777         -      }
  3778         -      nConstraint++;
  3779         -      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
  3780         -    }else if( bStopAtNull ){
  3781         -      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
  3782         -      endEq = 0;
  3783         -      nConstraint++;
  3784         -    }
  3785         -    sqlite3DbFree(db, zStartAff);
  3786         -
  3787         -    /* Top of the loop body */
  3788         -    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  3789         -
  3790         -    /* Check if the index cursor is past the end of the range. */
  3791         -    if( nConstraint ){
  3792         -      op = aEndOp[bRev*2 + endEq];
  3793         -      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
  3794         -      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
  3795         -      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
  3796         -      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
  3797         -      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
  3798         -    }
  3799         -
  3800         -    /* Seek the table cursor, if required */
  3801         -    disableTerm(pLevel, pRangeStart);
  3802         -    disableTerm(pLevel, pRangeEnd);
  3803         -    if( omitTable ){
  3804         -      /* pIdx is a covering index.  No need to access the main table. */
  3805         -    }else if( HasRowid(pIdx->pTable) ){
  3806         -      iRowidReg = ++pParse->nMem;
  3807         -      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
  3808         -      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  3809         -      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
  3810         -    }else if( iCur!=iIdxCur ){
  3811         -      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
  3812         -      iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
  3813         -      for(j=0; j<pPk->nKeyCol; j++){
  3814         -        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
  3815         -        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
  3816         -      }
  3817         -      sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
  3818         -                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
  3819         -    }
  3820         -
  3821         -    /* Record the instruction used to terminate the loop. Disable 
  3822         -    ** WHERE clause terms made redundant by the index range scan.
  3823         -    */
  3824         -    if( pLoop->wsFlags & WHERE_ONEROW ){
  3825         -      pLevel->op = OP_Noop;
  3826         -    }else if( bRev ){
  3827         -      pLevel->op = OP_Prev;
  3828         -    }else{
  3829         -      pLevel->op = OP_Next;
  3830         -    }
  3831         -    pLevel->p1 = iIdxCur;
  3832         -    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
  3833         -    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
  3834         -      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  3835         -    }else{
  3836         -      assert( pLevel->p5==0 );
  3837         -    }
  3838         -  }else
  3839         -
  3840         -#ifndef SQLITE_OMIT_OR_OPTIMIZATION
  3841         -  if( pLoop->wsFlags & WHERE_MULTI_OR ){
  3842         -    /* Case 5:  Two or more separately indexed terms connected by OR
  3843         -    **
  3844         -    ** Example:
  3845         -    **
  3846         -    **   CREATE TABLE t1(a,b,c,d);
  3847         -    **   CREATE INDEX i1 ON t1(a);
  3848         -    **   CREATE INDEX i2 ON t1(b);
  3849         -    **   CREATE INDEX i3 ON t1(c);
  3850         -    **
  3851         -    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
  3852         -    **
  3853         -    ** In the example, there are three indexed terms connected by OR.
  3854         -    ** The top of the loop looks like this:
  3855         -    **
  3856         -    **          Null       1                # Zero the rowset in reg 1
  3857         -    **
  3858         -    ** Then, for each indexed term, the following. The arguments to
  3859         -    ** RowSetTest are such that the rowid of the current row is inserted
  3860         -    ** into the RowSet. If it is already present, control skips the
  3861         -    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
  3862         -    **
  3863         -    **        sqlite3WhereBegin(<term>)
  3864         -    **          RowSetTest                  # Insert rowid into rowset
  3865         -    **          Gosub      2 A
  3866         -    **        sqlite3WhereEnd()
  3867         -    **
  3868         -    ** Following the above, code to terminate the loop. Label A, the target
  3869         -    ** of the Gosub above, jumps to the instruction right after the Goto.
  3870         -    **
  3871         -    **          Null       1                # Zero the rowset in reg 1
  3872         -    **          Goto       B                # The loop is finished.
  3873         -    **
  3874         -    **       A: <loop body>                 # Return data, whatever.
  3875         -    **
  3876         -    **          Return     2                # Jump back to the Gosub
  3877         -    **
  3878         -    **       B: <after the loop>
  3879         -    **
  3880         -    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
  3881         -    ** use an ephemeral index instead of a RowSet to record the primary
  3882         -    ** keys of the rows we have already seen.
  3883         -    **
  3884         -    */
  3885         -    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
  3886         -    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
  3887         -    Index *pCov = 0;             /* Potential covering index (or NULL) */
  3888         -    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
  3889         -
  3890         -    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
  3891         -    int regRowset = 0;                        /* Register for RowSet object */
  3892         -    int regRowid = 0;                         /* Register holding rowid */
  3893         -    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
  3894         -    int iRetInit;                             /* Address of regReturn init */
  3895         -    int untestedTerms = 0;             /* Some terms not completely tested */
  3896         -    int ii;                            /* Loop counter */
  3897         -    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
  3898         -    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
  3899         -    Table *pTab = pTabItem->pTab;
  3900         -   
  3901         -    pTerm = pLoop->aLTerm[0];
  3902         -    assert( pTerm!=0 );
  3903         -    assert( pTerm->eOperator & WO_OR );
  3904         -    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
  3905         -    pOrWc = &pTerm->u.pOrInfo->wc;
  3906         -    pLevel->op = OP_Return;
  3907         -    pLevel->p1 = regReturn;
  3908         -
  3909         -    /* Set up a new SrcList in pOrTab containing the table being scanned
  3910         -    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
  3911         -    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
  3912         -    */
  3913         -    if( pWInfo->nLevel>1 ){
  3914         -      int nNotReady;                 /* The number of notReady tables */
  3915         -      struct SrcList_item *origSrc;     /* Original list of tables */
  3916         -      nNotReady = pWInfo->nLevel - iLevel - 1;
  3917         -      pOrTab = sqlite3StackAllocRaw(db,
  3918         -                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
  3919         -      if( pOrTab==0 ) return notReady;
  3920         -      pOrTab->nAlloc = (u8)(nNotReady + 1);
  3921         -      pOrTab->nSrc = pOrTab->nAlloc;
  3922         -      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
  3923         -      origSrc = pWInfo->pTabList->a;
  3924         -      for(k=1; k<=nNotReady; k++){
  3925         -        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
  3926         -      }
  3927         -    }else{
  3928         -      pOrTab = pWInfo->pTabList;
  3929         -    }
  3930         -
  3931         -    /* Initialize the rowset register to contain NULL. An SQL NULL is 
  3932         -    ** equivalent to an empty rowset.  Or, create an ephemeral index
  3933         -    ** capable of holding primary keys in the case of a WITHOUT ROWID.
  3934         -    **
  3935         -    ** Also initialize regReturn to contain the address of the instruction 
  3936         -    ** immediately following the OP_Return at the bottom of the loop. This
  3937         -    ** is required in a few obscure LEFT JOIN cases where control jumps
  3938         -    ** over the top of the loop into the body of it. In this case the 
  3939         -    ** correct response for the end-of-loop code (the OP_Return) is to 
  3940         -    ** fall through to the next instruction, just as an OP_Next does if
  3941         -    ** called on an uninitialized cursor.
  3942         -    */
  3943         -    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  3944         -      if( HasRowid(pTab) ){
  3945         -        regRowset = ++pParse->nMem;
  3946         -        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
  3947         -      }else{
  3948         -        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
  3949         -        regRowset = pParse->nTab++;
  3950         -        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
  3951         -        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
  3952         -      }
  3953         -      regRowid = ++pParse->nMem;
  3954         -    }
  3955         -    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
  3956         -
  3957         -    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
  3958         -    ** Then for every term xN, evaluate as the subexpression: xN AND z
  3959         -    ** That way, terms in y that are factored into the disjunction will
  3960         -    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
  3961         -    **
  3962         -    ** Actually, each subexpression is converted to "xN AND w" where w is
  3963         -    ** the "interesting" terms of z - terms that did not originate in the
  3964         -    ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
  3965         -    ** indices.
  3966         -    **
  3967         -    ** This optimization also only applies if the (x1 OR x2 OR ...) term
  3968         -    ** is not contained in the ON clause of a LEFT JOIN.
  3969         -    ** See ticket http://www.sqlite.org/src/info/f2369304e4
  3970         -    */
  3971         -    if( pWC->nTerm>1 ){
  3972         -      int iTerm;
  3973         -      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
  3974         -        Expr *pExpr = pWC->a[iTerm].pExpr;
  3975         -        if( &pWC->a[iTerm] == pTerm ) continue;
  3976         -        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
  3977         -        if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
  3978         -        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
  3979         -        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
  3980         -        pExpr = sqlite3ExprDup(db, pExpr, 0);
  3981         -        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
  3982         -      }
  3983         -      if( pAndExpr ){
  3984         -        pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
  3985         -      }
  3986         -    }
  3987         -
  3988         -    /* Run a separate WHERE clause for each term of the OR clause.  After
  3989         -    ** eliminating duplicates from other WHERE clauses, the action for each
  3990         -    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
  3991         -    */
  3992         -    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE
  3993         -                | WHERE_FORCE_TABLE
  3994         -                | WHERE_ONETABLE_ONLY
  3995         -                | WHERE_NO_AUTOINDEX;
  3996         -    for(ii=0; ii<pOrWc->nTerm; ii++){
  3997         -      WhereTerm *pOrTerm = &pOrWc->a[ii];
  3998         -      if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
  3999         -        WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
  4000         -        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
  4001         -        int j1 = 0;                     /* Address of jump operation */
  4002         -        if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
  4003         -          pAndExpr->pLeft = pOrExpr;
  4004         -          pOrExpr = pAndExpr;
  4005         -        }
  4006         -        /* Loop through table entries that match term pOrTerm. */
  4007         -        WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
  4008         -        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
  4009         -                                      wctrlFlags, iCovCur);
  4010         -        assert( pSubWInfo || pParse->nErr || db->mallocFailed );
  4011         -        if( pSubWInfo ){
  4012         -          WhereLoop *pSubLoop;
  4013         -          int addrExplain = explainOneScan(
  4014         -              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
  4015         -          );
  4016         -          addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
  4017         -
  4018         -          /* This is the sub-WHERE clause body.  First skip over
  4019         -          ** duplicate rows from prior sub-WHERE clauses, and record the
  4020         -          ** rowid (or PRIMARY KEY) for the current row so that the same
  4021         -          ** row will be skipped in subsequent sub-WHERE clauses.
  4022         -          */
  4023         -          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  4024         -            int r;
  4025         -            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
  4026         -            if( HasRowid(pTab) ){
  4027         -              r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
  4028         -              j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
  4029         -              VdbeCoverage(v);
  4030         -            }else{
  4031         -              Index *pPk = sqlite3PrimaryKeyIndex(pTab);
  4032         -              int nPk = pPk->nKeyCol;
  4033         -              int iPk;
  4034         -
  4035         -              /* Read the PK into an array of temp registers. */
  4036         -              r = sqlite3GetTempRange(pParse, nPk);
  4037         -              for(iPk=0; iPk<nPk; iPk++){
  4038         -                int iCol = pPk->aiColumn[iPk];
  4039         -                sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
  4040         -              }
  4041         -
  4042         -              /* Check if the temp table already contains this key. If so,
  4043         -              ** the row has already been included in the result set and
  4044         -              ** can be ignored (by jumping past the Gosub below). Otherwise,
  4045         -              ** insert the key into the temp table and proceed with processing
  4046         -              ** the row.
  4047         -              **
  4048         -              ** Use some of the same optimizations as OP_RowSetTest: If iSet
  4049         -              ** is zero, assume that the key cannot already be present in
  4050         -              ** the temp table. And if iSet is -1, assume that there is no 
  4051         -              ** need to insert the key into the temp table, as it will never 
  4052         -              ** be tested for.  */ 
  4053         -              if( iSet ){
  4054         -                j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
  4055         -                VdbeCoverage(v);
  4056         -              }
  4057         -              if( iSet>=0 ){
  4058         -                sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
  4059         -                sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
  4060         -                if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  4061         -              }
  4062         -
  4063         -              /* Release the array of temp registers */
  4064         -              sqlite3ReleaseTempRange(pParse, r, nPk);
  4065         -            }
  4066         -          }
  4067         -
  4068         -          /* Invoke the main loop body as a subroutine */
  4069         -          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
  4070         -
  4071         -          /* Jump here (skipping the main loop body subroutine) if the
  4072         -          ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
  4073         -          if( j1 ) sqlite3VdbeJumpHere(v, j1);
  4074         -
  4075         -          /* The pSubWInfo->untestedTerms flag means that this OR term
  4076         -          ** contained one or more AND term from a notReady table.  The
  4077         -          ** terms from the notReady table could not be tested and will
  4078         -          ** need to be tested later.
  4079         -          */
  4080         -          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
  4081         -
  4082         -          /* If all of the OR-connected terms are optimized using the same
  4083         -          ** index, and the index is opened using the same cursor number
  4084         -          ** by each call to sqlite3WhereBegin() made by this loop, it may
  4085         -          ** be possible to use that index as a covering index.
  4086         -          **
  4087         -          ** If the call to sqlite3WhereBegin() above resulted in a scan that
  4088         -          ** uses an index, and this is either the first OR-connected term
  4089         -          ** processed or the index is the same as that used by all previous
  4090         -          ** terms, set pCov to the candidate covering index. Otherwise, set 
  4091         -          ** pCov to NULL to indicate that no candidate covering index will 
  4092         -          ** be available.
  4093         -          */
  4094         -          pSubLoop = pSubWInfo->a[0].pWLoop;
  4095         -          assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
  4096         -          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
  4097         -           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
  4098         -           && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
  4099         -          ){
  4100         -            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
  4101         -            pCov = pSubLoop->u.btree.pIndex;
  4102         -            wctrlFlags |= WHERE_REOPEN_IDX;
  4103         -          }else{
  4104         -            pCov = 0;
  4105         -          }
  4106         -
  4107         -          /* Finish the loop through table entries that match term pOrTerm. */
  4108         -          sqlite3WhereEnd(pSubWInfo);
  4109         -        }
  4110         -      }
  4111         -    }
  4112         -    pLevel->u.pCovidx = pCov;
  4113         -    if( pCov ) pLevel->iIdxCur = iCovCur;
  4114         -    if( pAndExpr ){
  4115         -      pAndExpr->pLeft = 0;
  4116         -      sqlite3ExprDelete(db, pAndExpr);
  4117         -    }
  4118         -    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
  4119         -    sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
  4120         -    sqlite3VdbeResolveLabel(v, iLoopBody);
  4121         -
  4122         -    if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
  4123         -    if( !untestedTerms ) disableTerm(pLevel, pTerm);
  4124         -  }else
  4125         -#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  4126         -
  4127         -  {
  4128         -    /* Case 6:  There is no usable index.  We must do a complete
  4129         -    **          scan of the entire table.
  4130         -    */
  4131         -    static const u8 aStep[] = { OP_Next, OP_Prev };
  4132         -    static const u8 aStart[] = { OP_Rewind, OP_Last };
  4133         -    assert( bRev==0 || bRev==1 );
  4134         -    if( pTabItem->isRecursive ){
  4135         -      /* Tables marked isRecursive have only a single row that is stored in
  4136         -      ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
  4137         -      pLevel->op = OP_Noop;
  4138         -    }else{
  4139         -      pLevel->op = aStep[bRev];
  4140         -      pLevel->p1 = iCur;
  4141         -      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
  4142         -      VdbeCoverageIf(v, bRev==0);
  4143         -      VdbeCoverageIf(v, bRev!=0);
  4144         -      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  4145         -    }
  4146         -  }
  4147         -
  4148         -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
  4149         -  pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
  4150         -#endif
  4151         -
  4152         -  /* Insert code to test every subexpression that can be completely
  4153         -  ** computed using the current set of tables.
  4154         -  */
  4155         -  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
  4156         -    Expr *pE;
  4157         -    int skipLikeAddr = 0;
  4158         -    testcase( pTerm->wtFlags & TERM_VIRTUAL );
  4159         -    testcase( pTerm->wtFlags & TERM_CODED );
  4160         -    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  4161         -    if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
  4162         -      testcase( pWInfo->untestedTerms==0
  4163         -               && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
  4164         -      pWInfo->untestedTerms = 1;
  4165         -      continue;
  4166         -    }
  4167         -    pE = pTerm->pExpr;
  4168         -    assert( pE!=0 );
  4169         -    if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
  4170         -      continue;
  4171         -    }
  4172         -    if( pTerm->wtFlags & TERM_LIKECOND ){
  4173         -      assert( pLevel->iLikeRepCntr>0 );
  4174         -      skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
  4175         -      VdbeCoverage(v);
  4176         -    }
  4177         -    sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
  4178         -    if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
  4179         -    pTerm->wtFlags |= TERM_CODED;
  4180         -  }
  4181         -
  4182         -  /* Insert code to test for implied constraints based on transitivity
  4183         -  ** of the "==" operator.
  4184         -  **
  4185         -  ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
  4186         -  ** and we are coding the t1 loop and the t2 loop has not yet coded,
  4187         -  ** then we cannot use the "t1.a=t2.b" constraint, but we can code
  4188         -  ** the implied "t1.a=123" constraint.
  4189         -  */
  4190         -  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
  4191         -    Expr *pE, *pEAlt;
  4192         -    WhereTerm *pAlt;
  4193         -    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  4194         -    if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
  4195         -    if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
  4196         -    if( pTerm->leftCursor!=iCur ) continue;
  4197         -    if( pLevel->iLeftJoin ) continue;
  4198         -    pE = pTerm->pExpr;
  4199         -    assert( !ExprHasProperty(pE, EP_FromJoin) );
  4200         -    assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
  4201         -    pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
  4202         -                    WO_EQ|WO_IN|WO_IS, 0);
  4203         -    if( pAlt==0 ) continue;
  4204         -    if( pAlt->wtFlags & (TERM_CODED) ) continue;
  4205         -    testcase( pAlt->eOperator & WO_EQ );
  4206         -    testcase( pAlt->eOperator & WO_IS );
  4207         -    testcase( pAlt->eOperator & WO_IN );
  4208         -    VdbeModuleComment((v, "begin transitive constraint"));
  4209         -    pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
  4210         -    if( pEAlt ){
  4211         -      *pEAlt = *pAlt->pExpr;
  4212         -      pEAlt->pLeft = pE->pLeft;
  4213         -      sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
  4214         -      sqlite3StackFree(db, pEAlt);
  4215         -    }
  4216         -  }
  4217         -
  4218         -  /* For a LEFT OUTER JOIN, generate code that will record the fact that
  4219         -  ** at least one row of the right table has matched the left table.  
  4220         -  */
  4221         -  if( pLevel->iLeftJoin ){
  4222         -    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
  4223         -    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
  4224         -    VdbeComment((v, "record LEFT JOIN hit"));
  4225         -    sqlite3ExprCacheClear(pParse);
  4226         -    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
  4227         -      testcase( pTerm->wtFlags & TERM_VIRTUAL );
  4228         -      testcase( pTerm->wtFlags & TERM_CODED );
  4229         -      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  4230         -      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
  4231         -        assert( pWInfo->untestedTerms );
  4232         -        continue;
  4233         -      }
  4234         -      assert( pTerm->pExpr );
  4235         -      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
  4236         -      pTerm->wtFlags |= TERM_CODED;
  4237         -    }
  4238         -  }
  4239         -
  4240         -  return pLevel->notReady;
  4241         -}
  4242   1551   
  4243   1552   #ifdef WHERETRACE_ENABLED
  4244   1553   /*
  4245   1554   ** Print the content of a WhereTerm object
  4246   1555   */
  4247   1556   static void whereTermPrint(WhereTerm *pTerm, int iTerm){
  4248   1557     if( pTerm==0 ){
................................................................................
  4401   1710       int i;
  4402   1711       for(i=0; i<pWInfo->nLevel; i++){
  4403   1712         WhereLevel *pLevel = &pWInfo->a[i];
  4404   1713         if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
  4405   1714           sqlite3DbFree(db, pLevel->u.in.aInLoop);
  4406   1715         }
  4407   1716       }
  4408         -    whereClauseClear(&pWInfo->sWC);
         1717  +    sqlite3WhereClauseClear(&pWInfo->sWC);
  4409   1718       while( pWInfo->pLoops ){
  4410   1719         WhereLoop *p = pWInfo->pLoops;
  4411   1720         pWInfo->pLoops = p->pNextLoop;
  4412   1721         whereLoopDelete(db, p);
  4413   1722       }
  4414   1723       sqlite3DbFree(db, pWInfo);
  4415   1724     }
................................................................................
  5661   2970     WhereLoop *pNew;
  5662   2971   
  5663   2972     /* Loop over the tables in the join, from left to right */
  5664   2973     pNew = pBuilder->pNew;
  5665   2974     whereLoopInit(pNew);
  5666   2975     for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
  5667   2976       pNew->iTab = iTab;
  5668         -    pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
         2977  +    pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
  5669   2978       if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
  5670   2979         mExtra = mPrior;
  5671   2980       }
  5672   2981       priorJoinType = pItem->jointype;
  5673   2982       if( IsVirtual(pItem->pTab) ){
  5674   2983         rc = whereLoopAddVirtual(pBuilder, mExtra);
  5675   2984       }else{
................................................................................
  5782   3091       ** loops.
  5783   3092       */
  5784   3093       for(i=0; i<nOrderBy; i++){
  5785   3094         if( MASKBIT(i) & obSat ) continue;
  5786   3095         pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
  5787   3096         if( pOBExpr->op!=TK_COLUMN ) continue;
  5788   3097         if( pOBExpr->iTable!=iCur ) continue;
  5789         -      pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
         3098  +      pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
  5790   3099                          ~ready, WO_EQ|WO_ISNULL|WO_IS, 0);
  5791   3100         if( pTerm==0 ) continue;
  5792   3101         if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
  5793   3102           const char *z1, *z2;
  5794   3103           pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
  5795   3104           if( !pColl ) pColl = db->pDfltColl;
  5796   3105           z1 = pColl->zName;
................................................................................
  5919   3228       if( isOrderDistinct ){
  5920   3229         orderDistinctMask |= pLoop->maskSelf;
  5921   3230         for(i=0; i<nOrderBy; i++){
  5922   3231           Expr *p;
  5923   3232           Bitmask mTerm;
  5924   3233           if( MASKBIT(i) & obSat ) continue;
  5925   3234           p = pOrderBy->a[i].pExpr;
  5926         -        mTerm = exprTableUsage(&pWInfo->sMaskSet,p);
         3235  +        mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
  5927   3236           if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
  5928   3237           if( (mTerm&~orderDistinctMask)==0 ){
  5929   3238             obSat |= MASKBIT(i);
  5930   3239           }
  5931   3240         }
  5932   3241       }
  5933   3242     } /* End the loop over all WhereLoops from outer-most down to inner-most */
................................................................................
  6398   3707     if( IsVirtual(pTab) ) return 0;
  6399   3708     if( pItem->zIndexedBy ) return 0;
  6400   3709     iCur = pItem->iCursor;
  6401   3710     pWC = &pWInfo->sWC;
  6402   3711     pLoop = pBuilder->pNew;
  6403   3712     pLoop->wsFlags = 0;
  6404   3713     pLoop->nSkip = 0;
  6405         -  pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
         3714  +  pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
  6406   3715     if( pTerm ){
  6407   3716       testcase( pTerm->eOperator & WO_IS );
  6408   3717       pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
  6409   3718       pLoop->aLTerm[0] = pTerm;
  6410   3719       pLoop->nLTerm = 1;
  6411   3720       pLoop->u.btree.nEq = 1;
  6412   3721       /* TUNING: Cost of a rowid lookup is 10 */
................................................................................
  6417   3726         assert( pLoop->aLTermSpace==pLoop->aLTerm );
  6418   3727         if( !IsUniqueIndex(pIdx)
  6419   3728          || pIdx->pPartIdxWhere!=0 
  6420   3729          || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) 
  6421   3730         ) continue;
  6422   3731         opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
  6423   3732         for(j=0; j<pIdx->nKeyCol; j++){
  6424         -        pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
         3733  +        pTerm = sqlite3WhereFindTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
  6425   3734           if( pTerm==0 ) break;
  6426   3735           testcase( pTerm->eOperator & WO_IS );
  6427   3736           pLoop->aLTerm[j] = pTerm;
  6428   3737         }
  6429   3738         if( j!=pIdx->nKeyCol ) continue;
  6430   3739         pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
  6431   3740         if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
................................................................................
  6438   3747         pLoop->rRun = 39;  /* 39==sqlite3LogEst(15) */
  6439   3748         break;
  6440   3749       }
  6441   3750     }
  6442   3751     if( pLoop->wsFlags ){
  6443   3752       pLoop->nOut = (LogEst)1;
  6444   3753       pWInfo->a[0].pWLoop = pLoop;
  6445         -    pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
         3754  +    pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
  6446   3755       pWInfo->a[0].iTabCur = iCur;
  6447   3756       pWInfo->nRowOut = 1;
  6448   3757       if( pWInfo->pOrderBy ) pWInfo->nOBSat =  pWInfo->pOrderBy->nExpr;
  6449   3758       if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
  6450   3759         pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
  6451   3760       }
  6452   3761   #ifdef SQLITE_DEBUG
................................................................................
  6632   3941     sWLB.pNew->cId = '*';
  6633   3942   #endif
  6634   3943   
  6635   3944     /* Split the WHERE clause into separate subexpressions where each
  6636   3945     ** subexpression is separated by an AND operator.
  6637   3946     */
  6638   3947     initMaskSet(pMaskSet);
  6639         -  whereClauseInit(&pWInfo->sWC, pWInfo);
  6640         -  whereSplit(&pWInfo->sWC, pWhere, TK_AND);
         3948  +  sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
         3949  +  sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
  6641   3950       
  6642   3951     /* Special case: a WHERE clause that is constant.  Evaluate the
  6643   3952     ** expression and either jump over all of the code or fall thru.
  6644   3953     */
  6645   3954     for(ii=0; ii<sWLB.pWC->nTerm; ii++){
  6646   3955       if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
  6647   3956         sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
................................................................................
  6678   3987     for(ii=0; ii<pTabList->nSrc; ii++){
  6679   3988       createMask(pMaskSet, pTabList->a[ii].iCursor);
  6680   3989     }
  6681   3990   #ifndef NDEBUG
  6682   3991     {
  6683   3992       Bitmask toTheLeft = 0;
  6684   3993       for(ii=0; ii<pTabList->nSrc; ii++){
  6685         -      Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
         3994  +      Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
  6686   3995         assert( (m-1)==toTheLeft );
  6687   3996         toTheLeft |= m;
  6688   3997       }
  6689   3998     }
  6690   3999   #endif
  6691   4000   
  6692         -  /* Analyze all of the subexpressions.  Note that exprAnalyze() might
  6693         -  ** add new virtual terms onto the end of the WHERE clause.  We do not
  6694         -  ** want to analyze these virtual terms, so start analyzing at the end
  6695         -  ** and work forward so that the added virtual terms are never processed.
  6696         -  */
  6697         -  exprAnalyzeAll(pTabList, &pWInfo->sWC);
  6698         -  if( db->mallocFailed ){
  6699         -    goto whereBeginError;
  6700         -  }
         4001  +  /* Analyze all of the subexpressions. */
         4002  +  sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
         4003  +  if( db->mallocFailed ) goto whereBeginError;
  6701   4004   
  6702   4005     if( wctrlFlags & WHERE_WANT_DISTINCT ){
  6703   4006       if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
  6704   4007         /* The DISTINCT marking is pointless.  Ignore it. */
  6705   4008         pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
  6706   4009       }else if( pOrderBy==0 ){
  6707   4010         /* Try to ORDER BY the result set to make distinct processing easier */
................................................................................
  6709   4012         pWInfo->pOrderBy = pResultSet;
  6710   4013       }
  6711   4014     }
  6712   4015   
  6713   4016     /* Construct the WhereLoop objects */
  6714   4017     WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
  6715   4018   #if defined(WHERETRACE_ENABLED)
  6716         -  /* Display all terms of the WHERE clause */
  6717         -  if( sqlite3WhereTrace & 0x100 ){
         4019  +  if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
  6718   4020       int i;
  6719   4021       for(i=0; i<sWLB.pWC->nTerm; i++){
  6720   4022         whereTermPrint(&sWLB.pWC->a[i], i);
  6721   4023       }
  6722   4024     }
  6723   4025   #endif
  6724   4026   
  6725   4027     if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
  6726   4028       rc = whereLoopAddAll(&sWLB);
  6727   4029       if( rc ) goto whereBeginError;
  6728   4030     
  6729         -    /* Display all of the WhereLoop objects if wheretrace is enabled */
  6730         -#ifdef WHERETRACE_ENABLED /* !=0 */
  6731         -    if( sqlite3WhereTrace ){
         4031  +#ifdef WHERETRACE_ENABLED
         4032  +    if( sqlite3WhereTrace ){    /* Display all of the WhereLoop objects */
  6732   4033         WhereLoop *p;
  6733   4034         int i;
  6734         -      static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
  6735         -                                       "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
         4035  +      static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
         4036  +                                             "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
  6736   4037         for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
  6737   4038           p->cId = zLabel[i%sizeof(zLabel)];
  6738   4039           whereLoopPrint(p, sWLB.pWC);
  6739   4040         }
  6740   4041       }
  6741   4042   #endif
  6742   4043     
................................................................................
  6749   4050     }
  6750   4051     if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
  6751   4052        pWInfo->revMask = (Bitmask)(-1);
  6752   4053     }
  6753   4054     if( pParse->nErr || NEVER(db->mallocFailed) ){
  6754   4055       goto whereBeginError;
  6755   4056     }
  6756         -#ifdef WHERETRACE_ENABLED /* !=0 */
         4057  +#ifdef WHERETRACE_ENABLED
  6757   4058     if( sqlite3WhereTrace ){
  6758   4059       sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
  6759   4060       if( pWInfo->nOBSat>0 ){
  6760   4061         sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
  6761   4062       }
  6762   4063       switch( pWInfo->eDistinct ){
  6763   4064         case WHERE_DISTINCT_UNIQUE: {
................................................................................
  6780   4081     }
  6781   4082   #endif
  6782   4083     /* Attempt to omit tables from the join that do not effect the result */
  6783   4084     if( pWInfo->nLevel>=2
  6784   4085      && pResultSet!=0
  6785   4086      && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
  6786   4087     ){
  6787         -    Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
  6788         -    if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
         4088  +    Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
         4089  +    if( sWLB.pOrderBy ){
         4090  +      tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
         4091  +    }
  6789   4092       while( pWInfo->nLevel>=2 ){
  6790   4093         WhereTerm *pTerm, *pEnd;
  6791   4094         pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
  6792   4095         if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
  6793   4096         if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
  6794   4097          && (pLoop->wsFlags & WHERE_ONEROW)==0
  6795   4098         ){
................................................................................
  6812   4115     }
  6813   4116     WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
  6814   4117     pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
  6815   4118   
  6816   4119     /* If the caller is an UPDATE or DELETE statement that is requesting
  6817   4120     ** to use a one-pass algorithm, determine if this is appropriate.
  6818   4121     ** The one-pass algorithm only works if the WHERE clause constrains
  6819         -  ** the statement to update a single row.
         4122  +  ** the statement to update or delete a single row.
  6820   4123     */
  6821   4124     assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
  6822   4125     if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 
  6823   4126      && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
  6824   4127       pWInfo->okOnePass = 1;
  6825   4128       if( HasRowid(pTabList->a[0].pTab) ){
  6826   4129         pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY;
  6827   4130       }
  6828   4131     }
  6829   4132   
  6830   4133     /* Open all tables in the pTabList and any indices selected for
  6831   4134     ** searching those tables.
  6832   4135     */
  6833         -  notReady = ~(Bitmask)0;
  6834   4136     for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
  6835   4137       Table *pTab;     /* Table to open */
  6836   4138       int iDb;         /* Index of database containing table/index */
  6837   4139       struct SrcList_item *pTabItem;
  6838   4140   
  6839   4141       pTabItem = &pTabList->a[pLevel->iFrom];
  6840   4142       pTab = pTabItem->pTab;
................................................................................
  6915   4217           ){
  6916   4218             sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
  6917   4219           }
  6918   4220           VdbeComment((v, "%s", pIx->zName));
  6919   4221         }
  6920   4222       }
  6921   4223       if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
  6922         -    notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
  6923   4224     }
  6924   4225     pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
  6925   4226     if( db->mallocFailed ) goto whereBeginError;
  6926   4227   
  6927   4228     /* Generate the code to do the search.  Each iteration of the for
  6928   4229     ** loop below generates code for a single nested loop of the VM
  6929   4230     ** program.
................................................................................
  6937   4238   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  6938   4239       if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
  6939   4240         constructAutomaticIndex(pParse, &pWInfo->sWC,
  6940   4241                   &pTabList->a[pLevel->iFrom], notReady, pLevel);
  6941   4242         if( db->mallocFailed ) goto whereBeginError;
  6942   4243       }
  6943   4244   #endif
  6944         -    addrExplain = explainOneScan(
         4245  +    addrExplain = sqlite3WhereExplainOneScan(
  6945   4246           pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
  6946   4247       );
  6947   4248       pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
  6948         -    notReady = codeOneLoopStart(pWInfo, ii, notReady);
         4249  +    notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady);
  6949   4250       pWInfo->iContinue = pLevel->addrCont;
  6950   4251       if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){
  6951         -      addScanStatus(v, pTabList, pLevel, addrExplain);
         4252  +      sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
  6952   4253       }
  6953   4254     }
  6954   4255   
  6955   4256     /* Done. */
  6956   4257     VdbeModuleComment((v, "Begin WHERE-core"));
  6957   4258     return pWInfo;
  6958   4259   

Changes to src/whereInt.h.

    15     15   ** a separate source file for easier editing.
    16     16   */
    17     17   
    18     18   /*
    19     19   ** Trace output macros
    20     20   */
    21     21   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
    22         -/***/ int sqlite3WhereTrace = 0;
           22  +/***/ int sqlite3WhereTrace;
    23     23   #endif
    24     24   #if defined(SQLITE_DEBUG) \
    25     25       && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
    26     26   # define WHERETRACE(K,X)  if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
    27     27   # define WHERETRACE_ENABLED 1
    28     28   #else
    29     29   # define WHERETRACE(K,X)
................................................................................
   157    157   */
   158    158   #define N_OR_COST 3
   159    159   struct WhereOrSet {
   160    160     u16 n;                      /* Number of valid a[] entries */
   161    161     WhereOrCost a[N_OR_COST];   /* Set of best costs */
   162    162   };
   163    163   
   164         -
   165         -/* Forward declaration of methods */
   166         -static int whereLoopResize(sqlite3*, WhereLoop*, int);
   167         -
   168    164   /*
   169    165   ** Each instance of this object holds a sequence of WhereLoop objects
   170    166   ** that implement some or all of a query plan.
   171    167   **
   172    168   ** Think of each WhereLoop object as a node in a graph with arcs
   173    169   ** showing dependencies and costs for travelling between nodes.  (That is
   174    170   ** not a completely accurate description because WhereLoop costs are a
................................................................................
   368    364   ** no gaps.
   369    365   */
   370    366   struct WhereMaskSet {
   371    367     int n;                        /* Number of assigned cursor values */
   372    368     int ix[BMS];                  /* Cursor assigned to each bit */
   373    369   };
   374    370   
          371  +/*
          372  +** Initialize a WhereMaskSet object
          373  +*/
          374  +#define initMaskSet(P)  (P)->n=0
          375  +
   375    376   /*
   376    377   ** This object is a convenience wrapper holding all information needed
   377    378   ** to construct WhereLoop objects for a particular query.
   378    379   */
   379    380   struct WhereLoopBuilder {
   380    381     WhereInfo *pWInfo;        /* Information about this WHERE */
   381    382     WhereClause *pWC;         /* WHERE clause terms */
................................................................................
   418    419     int iBreak;               /* Jump here to break out of the loop */
   419    420     int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
   420    421     int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
   421    422     WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
   422    423     WhereClause sWC;          /* Decomposition of the WHERE clause */
   423    424     WhereLevel a[1];          /* Information about each nest loop in WHERE */
   424    425   };
          426  +
          427  +/*
          428  +** Private interfaces - callable only by other where.c routines.
          429  +**
          430  +** where.c:
          431  +*/
          432  +Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
          433  +WhereTerm *sqlite3WhereFindTerm(
          434  +  WhereClause *pWC,     /* The WHERE clause to be searched */
          435  +  int iCur,             /* Cursor number of LHS */
          436  +  int iColumn,          /* Column number of LHS */
          437  +  Bitmask notReady,     /* RHS must not overlap with this mask */
          438  +  u32 op,               /* Mask of WO_xx values describing operator */
          439  +  Index *pIdx           /* Must be compatible with this index, if not NULL */
          440  +);
          441  +
          442  +/* wherecode.c: */
          443  +#ifndef SQLITE_OMIT_EXPLAIN
          444  +int sqlite3WhereExplainOneScan(
          445  +  Parse *pParse,                  /* Parse context */
          446  +  SrcList *pTabList,              /* Table list this loop refers to */
          447  +  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
          448  +  int iLevel,                     /* Value for "level" column of output */
          449  +  int iFrom,                      /* Value for "from" column of output */
          450  +  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
          451  +);
          452  +#else
          453  +# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0
          454  +#endif /* SQLITE_OMIT_EXPLAIN */
          455  +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
          456  +void sqlite3WhereAddScanStatus(
          457  +  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
          458  +  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
          459  +  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
          460  +  int addrExplain                 /* Address of OP_Explain (or 0) */
          461  +);
          462  +#else
          463  +# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
          464  +#endif
          465  +Bitmask sqlite3WhereCodeOneLoopStart(
          466  +  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
          467  +  int iLevel,          /* Which level of pWInfo->a[] should be coded */
          468  +  Bitmask notReady     /* Which tables are currently available */
          469  +);
          470  +
          471  +/* whereexpr.c: */
          472  +void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
          473  +void sqlite3WhereClauseClear(WhereClause*);
          474  +void sqlite3WhereSplit(WhereClause*,Expr*,u8);
          475  +Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
          476  +Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
          477  +void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
          478  +
          479  +
          480  +
          481  +
   425    482   
   426    483   /*
   427    484   ** Bitmasks for the operators on WhereTerm objects.  These are all
   428    485   ** operators that are of interest to the query planner.  An
   429    486   ** OR-ed combination of these values can be used when searching for
   430    487   ** particular WhereTerms within a WhereClause.
   431    488   */

Added src/wherecode.c.

            1  +/*
            2  +** 2015-06-06
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +** This module contains C code that generates VDBE code used to process
           13  +** the WHERE clause of SQL statements.
           14  +**
           15  +** This file was split off from where.c on 2015-06-06 in order to reduce the
           16  +** size of where.c and make it easier to edit.  This file contains the routines
           17  +** that actually generate the bulk of the WHERE loop code.  The original where.c
           18  +** file retains the code that does query planning and analysis.
           19  +*/
           20  +#include "sqliteInt.h"
           21  +#include "whereInt.h"
           22  +
           23  +#ifndef SQLITE_OMIT_EXPLAIN
           24  +/*
           25  +** This routine is a helper for explainIndexRange() below
           26  +**
           27  +** pStr holds the text of an expression that we are building up one term
           28  +** at a time.  This routine adds a new term to the end of the expression.
           29  +** Terms are separated by AND so add the "AND" text for second and subsequent
           30  +** terms only.
           31  +*/
           32  +static void explainAppendTerm(
           33  +  StrAccum *pStr,             /* The text expression being built */
           34  +  int iTerm,                  /* Index of this term.  First is zero */
           35  +  const char *zColumn,        /* Name of the column */
           36  +  const char *zOp             /* Name of the operator */
           37  +){
           38  +  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
           39  +  sqlite3StrAccumAppendAll(pStr, zColumn);
           40  +  sqlite3StrAccumAppend(pStr, zOp, 1);
           41  +  sqlite3StrAccumAppend(pStr, "?", 1);
           42  +}
           43  +
           44  +/*
           45  +** Argument pLevel describes a strategy for scanning table pTab. This 
           46  +** function appends text to pStr that describes the subset of table
           47  +** rows scanned by the strategy in the form of an SQL expression.
           48  +**
           49  +** For example, if the query:
           50  +**
           51  +**   SELECT * FROM t1 WHERE a=1 AND b>2;
           52  +**
           53  +** is run and there is an index on (a, b), then this function returns a
           54  +** string similar to:
           55  +**
           56  +**   "a=? AND b>?"
           57  +*/
           58  +static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
           59  +  Index *pIndex = pLoop->u.btree.pIndex;
           60  +  u16 nEq = pLoop->u.btree.nEq;
           61  +  u16 nSkip = pLoop->nSkip;
           62  +  int i, j;
           63  +  Column *aCol = pTab->aCol;
           64  +  i16 *aiColumn = pIndex->aiColumn;
           65  +
           66  +  if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
           67  +  sqlite3StrAccumAppend(pStr, " (", 2);
           68  +  for(i=0; i<nEq; i++){
           69  +    char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
           70  +    if( i>=nSkip ){
           71  +      explainAppendTerm(pStr, i, z, "=");
           72  +    }else{
           73  +      if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
           74  +      sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
           75  +    }
           76  +  }
           77  +
           78  +  j = i;
           79  +  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
           80  +    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
           81  +    explainAppendTerm(pStr, i++, z, ">");
           82  +  }
           83  +  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
           84  +    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
           85  +    explainAppendTerm(pStr, i, z, "<");
           86  +  }
           87  +  sqlite3StrAccumAppend(pStr, ")", 1);
           88  +}
           89  +
           90  +/*
           91  +** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
           92  +** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
           93  +** defined at compile-time. If it is not a no-op, a single OP_Explain opcode 
           94  +** is added to the output to describe the table scan strategy in pLevel.
           95  +**
           96  +** If an OP_Explain opcode is added to the VM, its address is returned.
           97  +** Otherwise, if no OP_Explain is coded, zero is returned.
           98  +*/
           99  +int sqlite3WhereExplainOneScan(
          100  +  Parse *pParse,                  /* Parse context */
          101  +  SrcList *pTabList,              /* Table list this loop refers to */
          102  +  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
          103  +  int iLevel,                     /* Value for "level" column of output */
          104  +  int iFrom,                      /* Value for "from" column of output */
          105  +  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
          106  +){
          107  +  int ret = 0;
          108  +#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
          109  +  if( pParse->explain==2 )
          110  +#endif
          111  +  {
          112  +    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
          113  +    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
          114  +    sqlite3 *db = pParse->db;     /* Database handle */
          115  +    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
          116  +    int isSearch;                 /* True for a SEARCH. False for SCAN. */
          117  +    WhereLoop *pLoop;             /* The controlling WhereLoop object */
          118  +    u32 flags;                    /* Flags that describe this loop */
          119  +    char *zMsg;                   /* Text to add to EQP output */
          120  +    StrAccum str;                 /* EQP output string */
          121  +    char zBuf[100];               /* Initial space for EQP output string */
          122  +
          123  +    pLoop = pLevel->pWLoop;
          124  +    flags = pLoop->wsFlags;
          125  +    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
          126  +
          127  +    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
          128  +            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
          129  +            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
          130  +
          131  +    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
          132  +    sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
          133  +    if( pItem->pSelect ){
          134  +      sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
          135  +    }else{
          136  +      sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
          137  +    }
          138  +
          139  +    if( pItem->zAlias ){
          140  +      sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
          141  +    }
          142  +    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
          143  +      const char *zFmt = 0;
          144  +      Index *pIdx;
          145  +
          146  +      assert( pLoop->u.btree.pIndex!=0 );
          147  +      pIdx = pLoop->u.btree.pIndex;
          148  +      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
          149  +      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
          150  +        if( isSearch ){
          151  +          zFmt = "PRIMARY KEY";
          152  +        }
          153  +      }else if( flags & WHERE_PARTIALIDX ){
          154  +        zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
          155  +      }else if( flags & WHERE_AUTO_INDEX ){
          156  +        zFmt = "AUTOMATIC COVERING INDEX";
          157  +      }else if( flags & WHERE_IDX_ONLY ){
          158  +        zFmt = "COVERING INDEX %s";
          159  +      }else{
          160  +        zFmt = "INDEX %s";
          161  +      }
          162  +      if( zFmt ){
          163  +        sqlite3StrAccumAppend(&str, " USING ", 7);
          164  +        sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
          165  +        explainIndexRange(&str, pLoop, pItem->pTab);
          166  +      }
          167  +    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
          168  +      const char *zRange;
          169  +      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
          170  +        zRange = "(rowid=?)";
          171  +      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
          172  +        zRange = "(rowid>? AND rowid<?)";
          173  +      }else if( flags&WHERE_BTM_LIMIT ){
          174  +        zRange = "(rowid>?)";
          175  +      }else{
          176  +        assert( flags&WHERE_TOP_LIMIT);
          177  +        zRange = "(rowid<?)";
          178  +      }
          179  +      sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
          180  +      sqlite3StrAccumAppendAll(&str, zRange);
          181  +    }
          182  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          183  +    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
          184  +      sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
          185  +                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
          186  +    }
          187  +#endif
          188  +#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
          189  +    if( pLoop->nOut>=10 ){
          190  +      sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
          191  +    }else{
          192  +      sqlite3StrAccumAppend(&str, " (~1 row)", 9);
          193  +    }
          194  +#endif
          195  +    zMsg = sqlite3StrAccumFinish(&str);
          196  +    ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
          197  +  }
          198  +  return ret;
          199  +}
          200  +#endif /* SQLITE_OMIT_EXPLAIN */
          201  +
          202  +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
          203  +/*
          204  +** Configure the VM passed as the first argument with an
          205  +** sqlite3_stmt_scanstatus() entry corresponding to the scan used to 
          206  +** implement level pLvl. Argument pSrclist is a pointer to the FROM 
          207  +** clause that the scan reads data from.
          208  +**
          209  +** If argument addrExplain is not 0, it must be the address of an 
          210  +** OP_Explain instruction that describes the same loop.
          211  +*/
          212  +void sqlite3WhereAddScanStatus(
          213  +  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
          214  +  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
          215  +  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
          216  +  int addrExplain                 /* Address of OP_Explain (or 0) */
          217  +){
          218  +  const char *zObj = 0;
          219  +  WhereLoop *pLoop = pLvl->pWLoop;
          220  +  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
          221  +    zObj = pLoop->u.btree.pIndex->zName;
          222  +  }else{
          223  +    zObj = pSrclist->a[pLvl->iFrom].zName;
          224  +  }
          225  +  sqlite3VdbeScanStatus(
          226  +      v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
          227  +  );
          228  +}
          229  +#endif
          230  +
          231  +
          232  +/*
          233  +** Disable a term in the WHERE clause.  Except, do not disable the term
          234  +** if it controls a LEFT OUTER JOIN and it did not originate in the ON
          235  +** or USING clause of that join.
          236  +**
          237  +** Consider the term t2.z='ok' in the following queries:
          238  +**
          239  +**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
          240  +**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
          241  +**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
          242  +**
          243  +** The t2.z='ok' is disabled in the in (2) because it originates
          244  +** in the ON clause.  The term is disabled in (3) because it is not part
          245  +** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
          246  +**
          247  +** Disabling a term causes that term to not be tested in the inner loop
          248  +** of the join.  Disabling is an optimization.  When terms are satisfied
          249  +** by indices, we disable them to prevent redundant tests in the inner
          250  +** loop.  We would get the correct results if nothing were ever disabled,
          251  +** but joins might run a little slower.  The trick is to disable as much
          252  +** as we can without disabling too much.  If we disabled in (1), we'd get
          253  +** the wrong answer.  See ticket #813.
          254  +**
          255  +** If all the children of a term are disabled, then that term is also
          256  +** automatically disabled.  In this way, terms get disabled if derived
          257  +** virtual terms are tested first.  For example:
          258  +**
          259  +**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
          260  +**      \___________/     \______/     \_____/
          261  +**         parent          child1       child2
          262  +**
          263  +** Only the parent term was in the original WHERE clause.  The child1
          264  +** and child2 terms were added by the LIKE optimization.  If both of
          265  +** the virtual child terms are valid, then testing of the parent can be 
          266  +** skipped.
          267  +**
          268  +** Usually the parent term is marked as TERM_CODED.  But if the parent
          269  +** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
          270  +** The TERM_LIKECOND marking indicates that the term should be coded inside
          271  +** a conditional such that is only evaluated on the second pass of a
          272  +** LIKE-optimization loop, when scanning BLOBs instead of strings.
          273  +*/
          274  +static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
          275  +  int nLoop = 0;
          276  +  while( pTerm
          277  +      && (pTerm->wtFlags & TERM_CODED)==0
          278  +      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
          279  +      && (pLevel->notReady & pTerm->prereqAll)==0
          280  +  ){
          281  +    if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
          282  +      pTerm->wtFlags |= TERM_LIKECOND;
          283  +    }else{
          284  +      pTerm->wtFlags |= TERM_CODED;
          285  +    }
          286  +    if( pTerm->iParent<0 ) break;
          287  +    pTerm = &pTerm->pWC->a[pTerm->iParent];
          288  +    pTerm->nChild--;
          289  +    if( pTerm->nChild!=0 ) break;
          290  +    nLoop++;
          291  +  }
          292  +}
          293  +
          294  +/*
          295  +** Code an OP_Affinity opcode to apply the column affinity string zAff
          296  +** to the n registers starting at base. 
          297  +**
          298  +** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
          299  +** beginning and end of zAff are ignored.  If all entries in zAff are
          300  +** SQLITE_AFF_BLOB, then no code gets generated.
          301  +**
          302  +** This routine makes its own copy of zAff so that the caller is free
          303  +** to modify zAff after this routine returns.
          304  +*/
          305  +static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
          306  +  Vdbe *v = pParse->pVdbe;
          307  +  if( zAff==0 ){
          308  +    assert( pParse->db->mallocFailed );
          309  +    return;
          310  +  }
          311  +  assert( v!=0 );
          312  +
          313  +  /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
          314  +  ** and end of the affinity string.
          315  +  */
          316  +  while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
          317  +    n--;
          318  +    base++;
          319  +    zAff++;
          320  +  }
          321  +  while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
          322  +    n--;
          323  +  }
          324  +
          325  +  /* Code the OP_Affinity opcode if there is anything left to do. */
          326  +  if( n>0 ){
          327  +    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
          328  +    sqlite3VdbeChangeP4(v, -1, zAff, n);
          329  +    sqlite3ExprCacheAffinityChange(pParse, base, n);
          330  +  }
          331  +}
          332  +
          333  +
          334  +/*
          335  +** Generate code for a single equality term of the WHERE clause.  An equality
          336  +** term can be either X=expr or X IN (...).   pTerm is the term to be 
          337  +** coded.
          338  +**
          339  +** The current value for the constraint is left in register iReg.
          340  +**
          341  +** For a constraint of the form X=expr, the expression is evaluated and its
          342  +** result is left on the stack.  For constraints of the form X IN (...)
          343  +** this routine sets up a loop that will iterate over all values of X.
          344  +*/
          345  +static int codeEqualityTerm(
          346  +  Parse *pParse,      /* The parsing context */
          347  +  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
          348  +  WhereLevel *pLevel, /* The level of the FROM clause we are working on */
          349  +  int iEq,            /* Index of the equality term within this level */
          350  +  int bRev,           /* True for reverse-order IN operations */
          351  +  int iTarget         /* Attempt to leave results in this register */
          352  +){
          353  +  Expr *pX = pTerm->pExpr;
          354  +  Vdbe *v = pParse->pVdbe;
          355  +  int iReg;                  /* Register holding results */
          356  +
          357  +  assert( iTarget>0 );
          358  +  if( pX->op==TK_EQ || pX->op==TK_IS ){
          359  +    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
          360  +  }else if( pX->op==TK_ISNULL ){
          361  +    iReg = iTarget;
          362  +    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
          363  +#ifndef SQLITE_OMIT_SUBQUERY
          364  +  }else{
          365  +    int eType;
          366  +    int iTab;
          367  +    struct InLoop *pIn;
          368  +    WhereLoop *pLoop = pLevel->pWLoop;
          369  +
          370  +    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
          371  +      && pLoop->u.btree.pIndex!=0
          372  +      && pLoop->u.btree.pIndex->aSortOrder[iEq]
          373  +    ){
          374  +      testcase( iEq==0 );
          375  +      testcase( bRev );
          376  +      bRev = !bRev;
          377  +    }
          378  +    assert( pX->op==TK_IN );
          379  +    iReg = iTarget;
          380  +    eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
          381  +    if( eType==IN_INDEX_INDEX_DESC ){
          382  +      testcase( bRev );
          383  +      bRev = !bRev;
          384  +    }
          385  +    iTab = pX->iTable;
          386  +    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
          387  +    VdbeCoverageIf(v, bRev);
          388  +    VdbeCoverageIf(v, !bRev);
          389  +    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
          390  +    pLoop->wsFlags |= WHERE_IN_ABLE;
          391  +    if( pLevel->u.in.nIn==0 ){
          392  +      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
          393  +    }
          394  +    pLevel->u.in.nIn++;
          395  +    pLevel->u.in.aInLoop =
          396  +       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
          397  +                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
          398  +    pIn = pLevel->u.in.aInLoop;
          399  +    if( pIn ){
          400  +      pIn += pLevel->u.in.nIn - 1;
          401  +      pIn->iCur = iTab;
          402  +      if( eType==IN_INDEX_ROWID ){
          403  +        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
          404  +      }else{
          405  +        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
          406  +      }
          407  +      pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
          408  +      sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
          409  +    }else{
          410  +      pLevel->u.in.nIn = 0;
          411  +    }
          412  +#endif
          413  +  }
          414  +  disableTerm(pLevel, pTerm);
          415  +  return iReg;
          416  +}
          417  +
          418  +/*
          419  +** Generate code that will evaluate all == and IN constraints for an
          420  +** index scan.
          421  +**
          422  +** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
          423  +** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
          424  +** The index has as many as three equality constraints, but in this
          425  +** example, the third "c" value is an inequality.  So only two 
          426  +** constraints are coded.  This routine will generate code to evaluate
          427  +** a==5 and b IN (1,2,3).  The current values for a and b will be stored
          428  +** in consecutive registers and the index of the first register is returned.
          429  +**
          430  +** In the example above nEq==2.  But this subroutine works for any value
          431  +** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
          432  +** The only thing it does is allocate the pLevel->iMem memory cell and
          433  +** compute the affinity string.
          434  +**
          435  +** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
          436  +** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
          437  +** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
          438  +** occurs after the nEq quality constraints.
          439  +**
          440  +** This routine allocates a range of nEq+nExtraReg memory cells and returns
          441  +** the index of the first memory cell in that range. The code that
          442  +** calls this routine will use that memory range to store keys for
          443  +** start and termination conditions of the loop.
          444  +** key value of the loop.  If one or more IN operators appear, then
          445  +** this routine allocates an additional nEq memory cells for internal
          446  +** use.
          447  +**
          448  +** Before returning, *pzAff is set to point to a buffer containing a
          449  +** copy of the column affinity string of the index allocated using
          450  +** sqlite3DbMalloc(). Except, entries in the copy of the string associated
          451  +** with equality constraints that use BLOB or NONE affinity are set to
          452  +** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
          453  +**
          454  +**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
          455  +**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
          456  +**
          457  +** In the example above, the index on t1(a) has TEXT affinity. But since
          458  +** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
          459  +** no conversion should be attempted before using a t2.b value as part of
          460  +** a key to search the index. Hence the first byte in the returned affinity
          461  +** string in this example would be set to SQLITE_AFF_BLOB.
          462  +*/
          463  +static int codeAllEqualityTerms(
          464  +  Parse *pParse,        /* Parsing context */
          465  +  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
          466  +  int bRev,             /* Reverse the order of IN operators */
          467  +  int nExtraReg,        /* Number of extra registers to allocate */
          468  +  char **pzAff          /* OUT: Set to point to affinity string */
          469  +){
          470  +  u16 nEq;                      /* The number of == or IN constraints to code */
          471  +  u16 nSkip;                    /* Number of left-most columns to skip */
          472  +  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
          473  +  Index *pIdx;                  /* The index being used for this loop */
          474  +  WhereTerm *pTerm;             /* A single constraint term */
          475  +  WhereLoop *pLoop;             /* The WhereLoop object */
          476  +  int j;                        /* Loop counter */
          477  +  int regBase;                  /* Base register */
          478  +  int nReg;                     /* Number of registers to allocate */
          479  +  char *zAff;                   /* Affinity string to return */
          480  +
          481  +  /* This module is only called on query plans that use an index. */
          482  +  pLoop = pLevel->pWLoop;
          483  +  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
          484  +  nEq = pLoop->u.btree.nEq;
          485  +  nSkip = pLoop->nSkip;
          486  +  pIdx = pLoop->u.btree.pIndex;
          487  +  assert( pIdx!=0 );
          488  +
          489  +  /* Figure out how many memory cells we will need then allocate them.
          490  +  */
          491  +  regBase = pParse->nMem + 1;
          492  +  nReg = pLoop->u.btree.nEq + nExtraReg;
          493  +  pParse->nMem += nReg;
          494  +
          495  +  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
          496  +  if( !zAff ){
          497  +    pParse->db->mallocFailed = 1;
          498  +  }
          499  +
          500  +  if( nSkip ){
          501  +    int iIdxCur = pLevel->iIdxCur;
          502  +    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
          503  +    VdbeCoverageIf(v, bRev==0);
          504  +    VdbeCoverageIf(v, bRev!=0);
          505  +    VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
          506  +    j = sqlite3VdbeAddOp0(v, OP_Goto);
          507  +    pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
          508  +                            iIdxCur, 0, regBase, nSkip);
          509  +    VdbeCoverageIf(v, bRev==0);
          510  +    VdbeCoverageIf(v, bRev!=0);
          511  +    sqlite3VdbeJumpHere(v, j);
          512  +    for(j=0; j<nSkip; j++){
          513  +      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
          514  +      assert( pIdx->aiColumn[j]>=0 );
          515  +      VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
          516  +    }
          517  +  }    
          518  +
          519  +  /* Evaluate the equality constraints
          520  +  */
          521  +  assert( zAff==0 || (int)strlen(zAff)>=nEq );
          522  +  for(j=nSkip; j<nEq; j++){
          523  +    int r1;
          524  +    pTerm = pLoop->aLTerm[j];
          525  +    assert( pTerm!=0 );
          526  +    /* The following testcase is true for indices with redundant columns. 
          527  +    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
          528  +    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
          529  +    testcase( pTerm->wtFlags & TERM_VIRTUAL );
          530  +    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
          531  +    if( r1!=regBase+j ){
          532  +      if( nReg==1 ){
          533  +        sqlite3ReleaseTempReg(pParse, regBase);
          534  +        regBase = r1;
          535  +      }else{
          536  +        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
          537  +      }
          538  +    }
          539  +    testcase( pTerm->eOperator & WO_ISNULL );
          540  +    testcase( pTerm->eOperator & WO_IN );
          541  +    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
          542  +      Expr *pRight = pTerm->pExpr->pRight;
          543  +      if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
          544  +        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
          545  +        VdbeCoverage(v);
          546  +      }
          547  +      if( zAff ){
          548  +        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
          549  +          zAff[j] = SQLITE_AFF_BLOB;
          550  +        }
          551  +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
          552  +          zAff[j] = SQLITE_AFF_BLOB;
          553  +        }
          554  +      }
          555  +    }
          556  +  }
          557  +  *pzAff = zAff;
          558  +  return regBase;
          559  +}
          560  +
          561  +/*
          562  +** If the most recently coded instruction is a constant range contraint
          563  +** that originated from the LIKE optimization, then change the P3 to be
          564  +** pLoop->iLikeRepCntr and set P5.
          565  +**
          566  +** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
          567  +** expression: "x>='ABC' AND x<'abd'".  But this requires that the range
          568  +** scan loop run twice, once for strings and a second time for BLOBs.
          569  +** The OP_String opcodes on the second pass convert the upper and lower
          570  +** bound string contants to blobs.  This routine makes the necessary changes
          571  +** to the OP_String opcodes for that to happen.
          572  +*/
          573  +static void whereLikeOptimizationStringFixup(
          574  +  Vdbe *v,                /* prepared statement under construction */
          575  +  WhereLevel *pLevel,     /* The loop that contains the LIKE operator */
          576  +  WhereTerm *pTerm        /* The upper or lower bound just coded */
          577  +){
          578  +  if( pTerm->wtFlags & TERM_LIKEOPT ){
          579  +    VdbeOp *pOp;
          580  +    assert( pLevel->iLikeRepCntr>0 );
          581  +    pOp = sqlite3VdbeGetOp(v, -1);
          582  +    assert( pOp!=0 );
          583  +    assert( pOp->opcode==OP_String8 
          584  +            || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
          585  +    pOp->p3 = pLevel->iLikeRepCntr;
          586  +    pOp->p5 = 1;
          587  +  }
          588  +}
          589  +
          590  +
          591  +/*
          592  +** Generate code for the start of the iLevel-th loop in the WHERE clause
          593  +** implementation described by pWInfo.
          594  +*/
          595  +Bitmask sqlite3WhereCodeOneLoopStart(
          596  +  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
          597  +  int iLevel,          /* Which level of pWInfo->a[] should be coded */
          598  +  Bitmask notReady     /* Which tables are currently available */
          599  +){
          600  +  int j, k;            /* Loop counters */
          601  +  int iCur;            /* The VDBE cursor for the table */
          602  +  int addrNxt;         /* Where to jump to continue with the next IN case */
          603  +  int omitTable;       /* True if we use the index only */
          604  +  int bRev;            /* True if we need to scan in reverse order */
          605  +  WhereLevel *pLevel;  /* The where level to be coded */
          606  +  WhereLoop *pLoop;    /* The WhereLoop object being coded */
          607  +  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
          608  +  WhereTerm *pTerm;               /* A WHERE clause term */
          609  +  Parse *pParse;                  /* Parsing context */
          610  +  sqlite3 *db;                    /* Database connection */
          611  +  Vdbe *v;                        /* The prepared stmt under constructions */
          612  +  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
          613  +  int addrBrk;                    /* Jump here to break out of the loop */
          614  +  int addrCont;                   /* Jump here to continue with next cycle */
          615  +  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
          616  +  int iReleaseReg = 0;      /* Temp register to free before returning */
          617  +
          618  +  pParse = pWInfo->pParse;
          619  +  v = pParse->pVdbe;
          620  +  pWC = &pWInfo->sWC;
          621  +  db = pParse->db;
          622  +  pLevel = &pWInfo->a[iLevel];
          623  +  pLoop = pLevel->pWLoop;
          624  +  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
          625  +  iCur = pTabItem->iCursor;
          626  +  pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
          627  +  bRev = (pWInfo->revMask>>iLevel)&1;
          628  +  omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
          629  +           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
          630  +  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
          631  +
          632  +  /* Create labels for the "break" and "continue" instructions
          633  +  ** for the current loop.  Jump to addrBrk to break out of a loop.
          634  +  ** Jump to cont to go immediately to the next iteration of the
          635  +  ** loop.
          636  +  **
          637  +  ** When there is an IN operator, we also have a "addrNxt" label that
          638  +  ** means to continue with the next IN value combination.  When
          639  +  ** there are no IN operators in the constraints, the "addrNxt" label
          640  +  ** is the same as "addrBrk".
          641  +  */
          642  +  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
          643  +  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
          644  +
          645  +  /* If this is the right table of a LEFT OUTER JOIN, allocate and
          646  +  ** initialize a memory cell that records if this table matches any
          647  +  ** row of the left table of the join.
          648  +  */
          649  +  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
          650  +    pLevel->iLeftJoin = ++pParse->nMem;
          651  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
          652  +    VdbeComment((v, "init LEFT JOIN no-match flag"));
          653  +  }
          654  +
          655  +  /* Special case of a FROM clause subquery implemented as a co-routine */
          656  +  if( pTabItem->viaCoroutine ){
          657  +    int regYield = pTabItem->regReturn;
          658  +    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
          659  +    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
          660  +    VdbeCoverage(v);
          661  +    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
          662  +    pLevel->op = OP_Goto;
          663  +  }else
          664  +
          665  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          666  +  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
          667  +    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
          668  +    **          to access the data.
          669  +    */
          670  +    int iReg;   /* P3 Value for OP_VFilter */
          671  +    int addrNotFound;
          672  +    int nConstraint = pLoop->nLTerm;
          673  +
          674  +    sqlite3ExprCachePush(pParse);
          675  +    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
          676  +    addrNotFound = pLevel->addrBrk;
          677  +    for(j=0; j<nConstraint; j++){
          678  +      int iTarget = iReg+j+2;
          679  +      pTerm = pLoop->aLTerm[j];
          680  +      if( pTerm==0 ) continue;
          681  +      if( pTerm->eOperator & WO_IN ){
          682  +        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
          683  +        addrNotFound = pLevel->addrNxt;
          684  +      }else{
          685  +        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
          686  +      }
          687  +    }
          688  +    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
          689  +    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
          690  +    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
          691  +                      pLoop->u.vtab.idxStr,
          692  +                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
          693  +    VdbeCoverage(v);
          694  +    pLoop->u.vtab.needFree = 0;
          695  +    for(j=0; j<nConstraint && j<16; j++){
          696  +      if( (pLoop->u.vtab.omitMask>>j)&1 ){
          697  +        disableTerm(pLevel, pLoop->aLTerm[j]);
          698  +      }
          699  +    }
          700  +    pLevel->op = OP_VNext;
          701  +    pLevel->p1 = iCur;
          702  +    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
          703  +    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
          704  +    sqlite3ExprCachePop(pParse);
          705  +  }else
          706  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
          707  +
          708  +  if( (pLoop->wsFlags & WHERE_IPK)!=0
          709  +   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
          710  +  ){
          711  +    /* Case 2:  We can directly reference a single row using an
          712  +    **          equality comparison against the ROWID field.  Or
          713  +    **          we reference multiple rows using a "rowid IN (...)"
          714  +    **          construct.
          715  +    */
          716  +    assert( pLoop->u.btree.nEq==1 );
          717  +    pTerm = pLoop->aLTerm[0];
          718  +    assert( pTerm!=0 );
          719  +    assert( pTerm->pExpr!=0 );
          720  +    assert( omitTable==0 );
          721  +    testcase( pTerm->wtFlags & TERM_VIRTUAL );
          722  +    iReleaseReg = ++pParse->nMem;
          723  +    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
          724  +    if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
          725  +    addrNxt = pLevel->addrNxt;
          726  +    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
          727  +    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
          728  +    VdbeCoverage(v);
          729  +    sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
          730  +    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
          731  +    VdbeComment((v, "pk"));
          732  +    pLevel->op = OP_Noop;
          733  +  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
          734  +         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
          735  +  ){
          736  +    /* Case 3:  We have an inequality comparison against the ROWID field.
          737  +    */
          738  +    int testOp = OP_Noop;
          739  +    int start;
          740  +    int memEndValue = 0;
          741  +    WhereTerm *pStart, *pEnd;
          742  +
          743  +    assert( omitTable==0 );
          744  +    j = 0;
          745  +    pStart = pEnd = 0;
          746  +    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
          747  +    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
          748  +    assert( pStart!=0 || pEnd!=0 );
          749  +    if( bRev ){
          750  +      pTerm = pStart;
          751  +      pStart = pEnd;
          752  +      pEnd = pTerm;
          753  +    }
          754  +    if( pStart ){
          755  +      Expr *pX;             /* The expression that defines the start bound */
          756  +      int r1, rTemp;        /* Registers for holding the start boundary */
          757  +
          758  +      /* The following constant maps TK_xx codes into corresponding 
          759  +      ** seek opcodes.  It depends on a particular ordering of TK_xx
          760  +      */
          761  +      const u8 aMoveOp[] = {
          762  +           /* TK_GT */  OP_SeekGT,
          763  +           /* TK_LE */  OP_SeekLE,
          764  +           /* TK_LT */  OP_SeekLT,
          765  +           /* TK_GE */  OP_SeekGE
          766  +      };
          767  +      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
          768  +      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
          769  +      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
          770  +
          771  +      assert( (pStart->wtFlags & TERM_VNULL)==0 );
          772  +      testcase( pStart->wtFlags & TERM_VIRTUAL );
          773  +      pX = pStart->pExpr;
          774  +      assert( pX!=0 );
          775  +      testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
          776  +      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
          777  +      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
          778  +      VdbeComment((v, "pk"));
          779  +      VdbeCoverageIf(v, pX->op==TK_GT);
          780  +      VdbeCoverageIf(v, pX->op==TK_LE);
          781  +      VdbeCoverageIf(v, pX->op==TK_LT);
          782  +      VdbeCoverageIf(v, pX->op==TK_GE);
          783  +      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
          784  +      sqlite3ReleaseTempReg(pParse, rTemp);
          785  +      disableTerm(pLevel, pStart);
          786  +    }else{
          787  +      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
          788  +      VdbeCoverageIf(v, bRev==0);
          789  +      VdbeCoverageIf(v, bRev!=0);
          790  +    }
          791  +    if( pEnd ){
          792  +      Expr *pX;
          793  +      pX = pEnd->pExpr;
          794  +      assert( pX!=0 );
          795  +      assert( (pEnd->wtFlags & TERM_VNULL)==0 );
          796  +      testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
          797  +      testcase( pEnd->wtFlags & TERM_VIRTUAL );
          798  +      memEndValue = ++pParse->nMem;
          799  +      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
          800  +      if( pX->op==TK_LT || pX->op==TK_GT ){
          801  +        testOp = bRev ? OP_Le : OP_Ge;
          802  +      }else{
          803  +        testOp = bRev ? OP_Lt : OP_Gt;
          804  +      }
          805  +      disableTerm(pLevel, pEnd);
          806  +    }
          807  +    start = sqlite3VdbeCurrentAddr(v);
          808  +    pLevel->op = bRev ? OP_Prev : OP_Next;
          809  +    pLevel->p1 = iCur;
          810  +    pLevel->p2 = start;
          811  +    assert( pLevel->p5==0 );
          812  +    if( testOp!=OP_Noop ){
          813  +      iRowidReg = ++pParse->nMem;
          814  +      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
          815  +      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
          816  +      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
          817  +      VdbeCoverageIf(v, testOp==OP_Le);
          818  +      VdbeCoverageIf(v, testOp==OP_Lt);
          819  +      VdbeCoverageIf(v, testOp==OP_Ge);
          820  +      VdbeCoverageIf(v, testOp==OP_Gt);
          821  +      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
          822  +    }
          823  +  }else if( pLoop->wsFlags & WHERE_INDEXED ){
          824  +    /* Case 4: A scan using an index.
          825  +    **
          826  +    **         The WHERE clause may contain zero or more equality 
          827  +    **         terms ("==" or "IN" operators) that refer to the N
          828  +    **         left-most columns of the index. It may also contain
          829  +    **         inequality constraints (>, <, >= or <=) on the indexed
          830  +    **         column that immediately follows the N equalities. Only 
          831  +    **         the right-most column can be an inequality - the rest must
          832  +    **         use the "==" and "IN" operators. For example, if the 
          833  +    **         index is on (x,y,z), then the following clauses are all 
          834  +    **         optimized:
          835  +    **
          836  +    **            x=5
          837  +    **            x=5 AND y=10
          838  +    **            x=5 AND y<10
          839  +    **            x=5 AND y>5 AND y<10
          840  +    **            x=5 AND y=5 AND z<=10
          841  +    **
          842  +    **         The z<10 term of the following cannot be used, only
          843  +    **         the x=5 term:
          844  +    **
          845  +    **            x=5 AND z<10
          846  +    **
          847  +    **         N may be zero if there are inequality constraints.
          848  +    **         If there are no inequality constraints, then N is at
          849  +    **         least one.
          850  +    **
          851  +    **         This case is also used when there are no WHERE clause
          852  +    **         constraints but an index is selected anyway, in order
          853  +    **         to force the output order to conform to an ORDER BY.
          854  +    */  
          855  +    static const u8 aStartOp[] = {
          856  +      0,
          857  +      0,
          858  +      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
          859  +      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
          860  +      OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
          861  +      OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
          862  +      OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
          863  +      OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
          864  +    };
          865  +    static const u8 aEndOp[] = {
          866  +      OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
          867  +      OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
          868  +      OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
          869  +      OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
          870  +    };
          871  +    u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
          872  +    int regBase;                 /* Base register holding constraint values */
          873  +    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
          874  +    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
          875  +    int startEq;                 /* True if range start uses ==, >= or <= */
          876  +    int endEq;                   /* True if range end uses ==, >= or <= */
          877  +    int start_constraints;       /* Start of range is constrained */
          878  +    int nConstraint;             /* Number of constraint terms */
          879  +    Index *pIdx;                 /* The index we will be using */
          880  +    int iIdxCur;                 /* The VDBE cursor for the index */
          881  +    int nExtraReg = 0;           /* Number of extra registers needed */
          882  +    int op;                      /* Instruction opcode */
          883  +    char *zStartAff;             /* Affinity for start of range constraint */
          884  +    char cEndAff = 0;            /* Affinity for end of range constraint */
          885  +    u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
          886  +    u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
          887  +
          888  +    pIdx = pLoop->u.btree.pIndex;
          889  +    iIdxCur = pLevel->iIdxCur;
          890  +    assert( nEq>=pLoop->nSkip );
          891  +
          892  +    /* If this loop satisfies a sort order (pOrderBy) request that 
          893  +    ** was passed to this function to implement a "SELECT min(x) ..." 
          894  +    ** query, then the caller will only allow the loop to run for
          895  +    ** a single iteration. This means that the first row returned
          896  +    ** should not have a NULL value stored in 'x'. If column 'x' is
          897  +    ** the first one after the nEq equality constraints in the index,
          898  +    ** this requires some special handling.
          899  +    */
          900  +    assert( pWInfo->pOrderBy==0
          901  +         || pWInfo->pOrderBy->nExpr==1
          902  +         || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
          903  +    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
          904  +     && pWInfo->nOBSat>0
          905  +     && (pIdx->nKeyCol>nEq)
          906  +    ){
          907  +      assert( pLoop->nSkip==0 );
          908  +      bSeekPastNull = 1;
          909  +      nExtraReg = 1;
          910  +    }
          911  +
          912  +    /* Find any inequality constraint terms for the start and end 
          913  +    ** of the range. 
          914  +    */
          915  +    j = nEq;
          916  +    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
          917  +      pRangeStart = pLoop->aLTerm[j++];
          918  +      nExtraReg = 1;
          919  +      /* Like optimization range constraints always occur in pairs */
          920  +      assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || 
          921  +              (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
          922  +    }
          923  +    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
          924  +      pRangeEnd = pLoop->aLTerm[j++];
          925  +      nExtraReg = 1;
          926  +      if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
          927  +        assert( pRangeStart!=0 );                     /* LIKE opt constraints */
          928  +        assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
          929  +        pLevel->iLikeRepCntr = ++pParse->nMem;
          930  +        testcase( bRev );
          931  +        testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
          932  +        sqlite3VdbeAddOp2(v, OP_Integer,
          933  +                          bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
          934  +                          pLevel->iLikeRepCntr);
          935  +        VdbeComment((v, "LIKE loop counter"));
          936  +        pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
          937  +      }
          938  +      if( pRangeStart==0
          939  +       && (j = pIdx->aiColumn[nEq])>=0 
          940  +       && pIdx->pTable->aCol[j].notNull==0
          941  +      ){
          942  +        bSeekPastNull = 1;
          943  +      }
          944  +    }
          945  +    assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
          946  +
          947  +    /* Generate code to evaluate all constraint terms using == or IN
          948  +    ** and store the values of those terms in an array of registers
          949  +    ** starting at regBase.
          950  +    */
          951  +    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
          952  +    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
          953  +    if( zStartAff ) cEndAff = zStartAff[nEq];
          954  +    addrNxt = pLevel->addrNxt;
          955  +
          956  +    /* If we are doing a reverse order scan on an ascending index, or
          957  +    ** a forward order scan on a descending index, interchange the 
          958  +    ** start and end terms (pRangeStart and pRangeEnd).
          959  +    */
          960  +    if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
          961  +     || (bRev && pIdx->nKeyCol==nEq)
          962  +    ){
          963  +      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
          964  +      SWAP(u8, bSeekPastNull, bStopAtNull);
          965  +    }
          966  +
          967  +    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
          968  +    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
          969  +    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
          970  +    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
          971  +    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
          972  +    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
          973  +    start_constraints = pRangeStart || nEq>0;
          974  +
          975  +    /* Seek the index cursor to the start of the range. */
          976  +    nConstraint = nEq;
          977  +    if( pRangeStart ){
          978  +      Expr *pRight = pRangeStart->pExpr->pRight;
          979  +      sqlite3ExprCode(pParse, pRight, regBase+nEq);
          980  +      whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
          981  +      if( (pRangeStart->wtFlags & TERM_VNULL)==0
          982  +       && sqlite3ExprCanBeNull(pRight)
          983  +      ){
          984  +        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
          985  +        VdbeCoverage(v);
          986  +      }
          987  +      if( zStartAff ){
          988  +        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){
          989  +          /* Since the comparison is to be performed with no conversions
          990  +          ** applied to the operands, set the affinity to apply to pRight to 
          991  +          ** SQLITE_AFF_BLOB.  */
          992  +          zStartAff[nEq] = SQLITE_AFF_BLOB;
          993  +        }
          994  +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
          995  +          zStartAff[nEq] = SQLITE_AFF_BLOB;
          996  +        }
          997  +      }  
          998  +      nConstraint++;
          999  +      testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
         1000  +    }else if( bSeekPastNull ){
         1001  +      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
         1002  +      nConstraint++;
         1003  +      startEq = 0;
         1004  +      start_constraints = 1;
         1005  +    }
         1006  +    codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
         1007  +    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
         1008  +    assert( op!=0 );
         1009  +    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
         1010  +    VdbeCoverage(v);
         1011  +    VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
         1012  +    VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
         1013  +    VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
         1014  +    VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
         1015  +    VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
         1016  +    VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
         1017  +
         1018  +    /* Load the value for the inequality constraint at the end of the
         1019  +    ** range (if any).
         1020  +    */
         1021  +    nConstraint = nEq;
         1022  +    if( pRangeEnd ){
         1023  +      Expr *pRight = pRangeEnd->pExpr->pRight;
         1024  +      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
         1025  +      sqlite3ExprCode(pParse, pRight, regBase+nEq);
         1026  +      whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
         1027  +      if( (pRangeEnd->wtFlags & TERM_VNULL)==0
         1028  +       && sqlite3ExprCanBeNull(pRight)
         1029  +      ){
         1030  +        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
         1031  +        VdbeCoverage(v);
         1032  +      }
         1033  +      if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB
         1034  +       && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
         1035  +      ){
         1036  +        codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
         1037  +      }
         1038  +      nConstraint++;
         1039  +      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
         1040  +    }else if( bStopAtNull ){
         1041  +      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
         1042  +      endEq = 0;
         1043  +      nConstraint++;
         1044  +    }
         1045  +    sqlite3DbFree(db, zStartAff);
         1046  +
         1047  +    /* Top of the loop body */
         1048  +    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
         1049  +
         1050  +    /* Check if the index cursor is past the end of the range. */
         1051  +    if( nConstraint ){
         1052  +      op = aEndOp[bRev*2 + endEq];
         1053  +      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
         1054  +      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
         1055  +      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
         1056  +      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
         1057  +      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
         1058  +    }
         1059  +
         1060  +    /* Seek the table cursor, if required */
         1061  +    disableTerm(pLevel, pRangeStart);
         1062  +    disableTerm(pLevel, pRangeEnd);
         1063  +    if( omitTable ){
         1064  +      /* pIdx is a covering index.  No need to access the main table. */
         1065  +    }else if( HasRowid(pIdx->pTable) ){
         1066  +      iRowidReg = ++pParse->nMem;
         1067  +      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
         1068  +      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
         1069  +      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
         1070  +    }else if( iCur!=iIdxCur ){
         1071  +      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
         1072  +      iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
         1073  +      for(j=0; j<pPk->nKeyCol; j++){
         1074  +        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
         1075  +        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
         1076  +      }
         1077  +      sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
         1078  +                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
         1079  +    }
         1080  +
         1081  +    /* Record the instruction used to terminate the loop. Disable 
         1082  +    ** WHERE clause terms made redundant by the index range scan.
         1083  +    */
         1084  +    if( pLoop->wsFlags & WHERE_ONEROW ){
         1085  +      pLevel->op = OP_Noop;
         1086  +    }else if( bRev ){
         1087  +      pLevel->op = OP_Prev;
         1088  +    }else{
         1089  +      pLevel->op = OP_Next;
         1090  +    }
         1091  +    pLevel->p1 = iIdxCur;
         1092  +    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
         1093  +    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
         1094  +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
         1095  +    }else{
         1096  +      assert( pLevel->p5==0 );
         1097  +    }
         1098  +  }else
         1099  +
         1100  +#ifndef SQLITE_OMIT_OR_OPTIMIZATION
         1101  +  if( pLoop->wsFlags & WHERE_MULTI_OR ){
         1102  +    /* Case 5:  Two or more separately indexed terms connected by OR
         1103  +    **
         1104  +    ** Example:
         1105  +    **
         1106  +    **   CREATE TABLE t1(a,b,c,d);
         1107  +    **   CREATE INDEX i1 ON t1(a);
         1108  +    **   CREATE INDEX i2 ON t1(b);
         1109  +    **   CREATE INDEX i3 ON t1(c);
         1110  +    **
         1111  +    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
         1112  +    **
         1113  +    ** In the example, there are three indexed terms connected by OR.
         1114  +    ** The top of the loop looks like this:
         1115  +    **
         1116  +    **          Null       1                # Zero the rowset in reg 1
         1117  +    **
         1118  +    ** Then, for each indexed term, the following. The arguments to
         1119  +    ** RowSetTest are such that the rowid of the current row is inserted
         1120  +    ** into the RowSet. If it is already present, control skips the
         1121  +    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
         1122  +    **
         1123  +    **        sqlite3WhereBegin(<term>)
         1124  +    **          RowSetTest                  # Insert rowid into rowset
         1125  +    **          Gosub      2 A
         1126  +    **        sqlite3WhereEnd()
         1127  +    **
         1128  +    ** Following the above, code to terminate the loop. Label A, the target
         1129  +    ** of the Gosub above, jumps to the instruction right after the Goto.
         1130  +    **
         1131  +    **          Null       1                # Zero the rowset in reg 1
         1132  +    **          Goto       B                # The loop is finished.
         1133  +    **
         1134  +    **       A: <loop body>                 # Return data, whatever.
         1135  +    **
         1136  +    **          Return     2                # Jump back to the Gosub
         1137  +    **
         1138  +    **       B: <after the loop>
         1139  +    **
         1140  +    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
         1141  +    ** use an ephemeral index instead of a RowSet to record the primary
         1142  +    ** keys of the rows we have already seen.
         1143  +    **
         1144  +    */
         1145  +    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
         1146  +    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
         1147  +    Index *pCov = 0;             /* Potential covering index (or NULL) */
         1148  +    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
         1149  +
         1150  +    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
         1151  +    int regRowset = 0;                        /* Register for RowSet object */
         1152  +    int regRowid = 0;                         /* Register holding rowid */
         1153  +    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
         1154  +    int iRetInit;                             /* Address of regReturn init */
         1155  +    int untestedTerms = 0;             /* Some terms not completely tested */
         1156  +    int ii;                            /* Loop counter */
         1157  +    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
         1158  +    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
         1159  +    Table *pTab = pTabItem->pTab;
         1160  +   
         1161  +    pTerm = pLoop->aLTerm[0];
         1162  +    assert( pTerm!=0 );
         1163  +    assert( pTerm->eOperator & WO_OR );
         1164  +    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
         1165  +    pOrWc = &pTerm->u.pOrInfo->wc;
         1166  +    pLevel->op = OP_Return;
         1167  +    pLevel->p1 = regReturn;
         1168  +
         1169  +    /* Set up a new SrcList in pOrTab containing the table being scanned
         1170  +    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
         1171  +    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
         1172  +    */
         1173  +    if( pWInfo->nLevel>1 ){
         1174  +      int nNotReady;                 /* The number of notReady tables */
         1175  +      struct SrcList_item *origSrc;     /* Original list of tables */
         1176  +      nNotReady = pWInfo->nLevel - iLevel - 1;
         1177  +      pOrTab = sqlite3StackAllocRaw(db,
         1178  +                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
         1179  +      if( pOrTab==0 ) return notReady;
         1180  +      pOrTab->nAlloc = (u8)(nNotReady + 1);
         1181  +      pOrTab->nSrc = pOrTab->nAlloc;
         1182  +      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
         1183  +      origSrc = pWInfo->pTabList->a;
         1184  +      for(k=1; k<=nNotReady; k++){
         1185  +        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
         1186  +      }
         1187  +    }else{
         1188  +      pOrTab = pWInfo->pTabList;
         1189  +    }
         1190  +
         1191  +    /* Initialize the rowset register to contain NULL. An SQL NULL is 
         1192  +    ** equivalent to an empty rowset.  Or, create an ephemeral index
         1193  +    ** capable of holding primary keys in the case of a WITHOUT ROWID.
         1194  +    **
         1195  +    ** Also initialize regReturn to contain the address of the instruction 
         1196  +    ** immediately following the OP_Return at the bottom of the loop. This
         1197  +    ** is required in a few obscure LEFT JOIN cases where control jumps
         1198  +    ** over the top of the loop into the body of it. In this case the 
         1199  +    ** correct response for the end-of-loop code (the OP_Return) is to 
         1200  +    ** fall through to the next instruction, just as an OP_Next does if
         1201  +    ** called on an uninitialized cursor.
         1202  +    */
         1203  +    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
         1204  +      if( HasRowid(pTab) ){
         1205  +        regRowset = ++pParse->nMem;
         1206  +        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
         1207  +      }else{
         1208  +        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
         1209  +        regRowset = pParse->nTab++;
         1210  +        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
         1211  +        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
         1212  +      }
         1213  +      regRowid = ++pParse->nMem;
         1214  +    }
         1215  +    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
         1216  +
         1217  +    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
         1218  +    ** Then for every term xN, evaluate as the subexpression: xN AND z
         1219  +    ** That way, terms in y that are factored into the disjunction will
         1220  +    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
         1221  +    **
         1222  +    ** Actually, each subexpression is converted to "xN AND w" where w is
         1223  +    ** the "interesting" terms of z - terms that did not originate in the
         1224  +    ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
         1225  +    ** indices.
         1226  +    **
         1227  +    ** This optimization also only applies if the (x1 OR x2 OR ...) term
         1228  +    ** is not contained in the ON clause of a LEFT JOIN.
         1229  +    ** See ticket http://www.sqlite.org/src/info/f2369304e4
         1230  +    */
         1231  +    if( pWC->nTerm>1 ){
         1232  +      int iTerm;
         1233  +      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
         1234  +        Expr *pExpr = pWC->a[iTerm].pExpr;
         1235  +        if( &pWC->a[iTerm] == pTerm ) continue;
         1236  +        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
         1237  +        if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
         1238  +        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
         1239  +        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
         1240  +        pExpr = sqlite3ExprDup(db, pExpr, 0);
         1241  +        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
         1242  +      }
         1243  +      if( pAndExpr ){
         1244  +        pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
         1245  +      }
         1246  +    }
         1247  +
         1248  +    /* Run a separate WHERE clause for each term of the OR clause.  After
         1249  +    ** eliminating duplicates from other WHERE clauses, the action for each
         1250  +    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
         1251  +    */
         1252  +    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE
         1253  +                | WHERE_FORCE_TABLE
         1254  +                | WHERE_ONETABLE_ONLY
         1255  +                | WHERE_NO_AUTOINDEX;
         1256  +    for(ii=0; ii<pOrWc->nTerm; ii++){
         1257  +      WhereTerm *pOrTerm = &pOrWc->a[ii];
         1258  +      if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
         1259  +        WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
         1260  +        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
         1261  +        int j1 = 0;                     /* Address of jump operation */
         1262  +        if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
         1263  +          pAndExpr->pLeft = pOrExpr;
         1264  +          pOrExpr = pAndExpr;
         1265  +        }
         1266  +        /* Loop through table entries that match term pOrTerm. */
         1267  +        WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
         1268  +        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
         1269  +                                      wctrlFlags, iCovCur);
         1270  +        assert( pSubWInfo || pParse->nErr || db->mallocFailed );
         1271  +        if( pSubWInfo ){
         1272  +          WhereLoop *pSubLoop;
         1273  +          int addrExplain = sqlite3WhereExplainOneScan(
         1274  +              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
         1275  +          );
         1276  +          sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
         1277  +
         1278  +          /* This is the sub-WHERE clause body.  First skip over
         1279  +          ** duplicate rows from prior sub-WHERE clauses, and record the
         1280  +          ** rowid (or PRIMARY KEY) for the current row so that the same
         1281  +          ** row will be skipped in subsequent sub-WHERE clauses.
         1282  +          */
         1283  +          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
         1284  +            int r;
         1285  +            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
         1286  +            if( HasRowid(pTab) ){
         1287  +              r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
         1288  +              j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
         1289  +              VdbeCoverage(v);
         1290  +            }else{
         1291  +              Index *pPk = sqlite3PrimaryKeyIndex(pTab);
         1292  +              int nPk = pPk->nKeyCol;
         1293  +              int iPk;
         1294  +
         1295  +              /* Read the PK into an array of temp registers. */
         1296  +              r = sqlite3GetTempRange(pParse, nPk);
         1297  +              for(iPk=0; iPk<nPk; iPk++){
         1298  +                int iCol = pPk->aiColumn[iPk];
         1299  +                sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
         1300  +              }
         1301  +
         1302  +              /* Check if the temp table already contains this key. If so,
         1303  +              ** the row has already been included in the result set and
         1304  +              ** can be ignored (by jumping past the Gosub below). Otherwise,
         1305  +              ** insert the key into the temp table and proceed with processing
         1306  +              ** the row.
         1307  +              **
         1308  +              ** Use some of the same optimizations as OP_RowSetTest: If iSet
         1309  +              ** is zero, assume that the key cannot already be present in
         1310  +              ** the temp table. And if iSet is -1, assume that there is no 
         1311  +              ** need to insert the key into the temp table, as it will never 
         1312  +              ** be tested for.  */ 
         1313  +              if( iSet ){
         1314  +                j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
         1315  +                VdbeCoverage(v);
         1316  +              }
         1317  +              if( iSet>=0 ){
         1318  +                sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
         1319  +                sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
         1320  +                if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
         1321  +              }
         1322  +
         1323  +              /* Release the array of temp registers */
         1324  +              sqlite3ReleaseTempRange(pParse, r, nPk);
         1325  +            }
         1326  +          }
         1327  +
         1328  +          /* Invoke the main loop body as a subroutine */
         1329  +          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
         1330  +
         1331  +          /* Jump here (skipping the main loop body subroutine) if the
         1332  +          ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
         1333  +          if( j1 ) sqlite3VdbeJumpHere(v, j1);
         1334  +
         1335  +          /* The pSubWInfo->untestedTerms flag means that this OR term
         1336  +          ** contained one or more AND term from a notReady table.  The
         1337  +          ** terms from the notReady table could not be tested and will
         1338  +          ** need to be tested later.
         1339  +          */
         1340  +          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
         1341  +
         1342  +          /* If all of the OR-connected terms are optimized using the same
         1343  +          ** index, and the index is opened using the same cursor number
         1344  +          ** by each call to sqlite3WhereBegin() made by this loop, it may
         1345  +          ** be possible to use that index as a covering index.
         1346  +          **
         1347  +          ** If the call to sqlite3WhereBegin() above resulted in a scan that
         1348  +          ** uses an index, and this is either the first OR-connected term
         1349  +          ** processed or the index is the same as that used by all previous
         1350  +          ** terms, set pCov to the candidate covering index. Otherwise, set 
         1351  +          ** pCov to NULL to indicate that no candidate covering index will 
         1352  +          ** be available.
         1353  +          */
         1354  +          pSubLoop = pSubWInfo->a[0].pWLoop;
         1355  +          assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
         1356  +          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
         1357  +           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
         1358  +           && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
         1359  +          ){
         1360  +            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
         1361  +            pCov = pSubLoop->u.btree.pIndex;
         1362  +            wctrlFlags |= WHERE_REOPEN_IDX;
         1363  +          }else{
         1364  +            pCov = 0;
         1365  +          }
         1366  +
         1367  +          /* Finish the loop through table entries that match term pOrTerm. */
         1368  +          sqlite3WhereEnd(pSubWInfo);
         1369  +        }
         1370  +      }
         1371  +    }
         1372  +    pLevel->u.pCovidx = pCov;
         1373  +    if( pCov ) pLevel->iIdxCur = iCovCur;
         1374  +    if( pAndExpr ){
         1375  +      pAndExpr->pLeft = 0;
         1376  +      sqlite3ExprDelete(db, pAndExpr);
         1377  +    }
         1378  +    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
         1379  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
         1380  +    sqlite3VdbeResolveLabel(v, iLoopBody);
         1381  +
         1382  +    if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
         1383  +    if( !untestedTerms ) disableTerm(pLevel, pTerm);
         1384  +  }else
         1385  +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
         1386  +
         1387  +  {
         1388  +    /* Case 6:  There is no usable index.  We must do a complete
         1389  +    **          scan of the entire table.
         1390  +    */
         1391  +    static const u8 aStep[] = { OP_Next, OP_Prev };
         1392  +    static const u8 aStart[] = { OP_Rewind, OP_Last };
         1393  +    assert( bRev==0 || bRev==1 );
         1394  +    if( pTabItem->isRecursive ){
         1395  +      /* Tables marked isRecursive have only a single row that is stored in
         1396  +      ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
         1397  +      pLevel->op = OP_Noop;
         1398  +    }else{
         1399  +      pLevel->op = aStep[bRev];
         1400  +      pLevel->p1 = iCur;
         1401  +      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
         1402  +      VdbeCoverageIf(v, bRev==0);
         1403  +      VdbeCoverageIf(v, bRev!=0);
         1404  +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
         1405  +    }
         1406  +  }
         1407  +
         1408  +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
         1409  +  pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
         1410  +#endif
         1411  +
         1412  +  /* Insert code to test every subexpression that can be completely
         1413  +  ** computed using the current set of tables.
         1414  +  */
         1415  +  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
         1416  +    Expr *pE;
         1417  +    int skipLikeAddr = 0;
         1418  +    testcase( pTerm->wtFlags & TERM_VIRTUAL );
         1419  +    testcase( pTerm->wtFlags & TERM_CODED );
         1420  +    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
         1421  +    if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
         1422  +      testcase( pWInfo->untestedTerms==0
         1423  +               && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
         1424  +      pWInfo->untestedTerms = 1;
         1425  +      continue;
         1426  +    }
         1427  +    pE = pTerm->pExpr;
         1428  +    assert( pE!=0 );
         1429  +    if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
         1430  +      continue;
         1431  +    }
         1432  +    if( pTerm->wtFlags & TERM_LIKECOND ){
         1433  +      assert( pLevel->iLikeRepCntr>0 );
         1434  +      skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
         1435  +      VdbeCoverage(v);
         1436  +    }
         1437  +    sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
         1438  +    if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
         1439  +    pTerm->wtFlags |= TERM_CODED;
         1440  +  }
         1441  +
         1442  +  /* Insert code to test for implied constraints based on transitivity
         1443  +  ** of the "==" operator.
         1444  +  **
         1445  +  ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
         1446  +  ** and we are coding the t1 loop and the t2 loop has not yet coded,
         1447  +  ** then we cannot use the "t1.a=t2.b" constraint, but we can code
         1448  +  ** the implied "t1.a=123" constraint.
         1449  +  */
         1450  +  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
         1451  +    Expr *pE, *pEAlt;
         1452  +    WhereTerm *pAlt;
         1453  +    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
         1454  +    if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
         1455  +    if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
         1456  +    if( pTerm->leftCursor!=iCur ) continue;
         1457  +    if( pLevel->iLeftJoin ) continue;
         1458  +    pE = pTerm->pExpr;
         1459  +    assert( !ExprHasProperty(pE, EP_FromJoin) );
         1460  +    assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
         1461  +    pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
         1462  +                    WO_EQ|WO_IN|WO_IS, 0);
         1463  +    if( pAlt==0 ) continue;
         1464  +    if( pAlt->wtFlags & (TERM_CODED) ) continue;
         1465  +    testcase( pAlt->eOperator & WO_EQ );
         1466  +    testcase( pAlt->eOperator & WO_IS );
         1467  +    testcase( pAlt->eOperator & WO_IN );
         1468  +    VdbeModuleComment((v, "begin transitive constraint"));
         1469  +    pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
         1470  +    if( pEAlt ){
         1471  +      *pEAlt = *pAlt->pExpr;
         1472  +      pEAlt->pLeft = pE->pLeft;
         1473  +      sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
         1474  +      sqlite3StackFree(db, pEAlt);
         1475  +    }
         1476  +  }
         1477  +
         1478  +  /* For a LEFT OUTER JOIN, generate code that will record the fact that
         1479  +  ** at least one row of the right table has matched the left table.  
         1480  +  */
         1481  +  if( pLevel->iLeftJoin ){
         1482  +    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
         1483  +    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
         1484  +    VdbeComment((v, "record LEFT JOIN hit"));
         1485  +    sqlite3ExprCacheClear(pParse);
         1486  +    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
         1487  +      testcase( pTerm->wtFlags & TERM_VIRTUAL );
         1488  +      testcase( pTerm->wtFlags & TERM_CODED );
         1489  +      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
         1490  +      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
         1491  +        assert( pWInfo->untestedTerms );
         1492  +        continue;
         1493  +      }
         1494  +      assert( pTerm->pExpr );
         1495  +      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
         1496  +      pTerm->wtFlags |= TERM_CODED;
         1497  +    }
         1498  +  }
         1499  +
         1500  +  return pLevel->notReady;
         1501  +}

Added src/whereexpr.c.

            1  +/*
            2  +** 2015-06-08
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +** This module contains C code that generates VDBE code used to process
           13  +** the WHERE clause of SQL statements.
           14  +**
           15  +** This file was originally part of where.c but was split out to improve
           16  +** readability and editabiliity.  This file contains utility routines for
           17  +** analyzing Expr objects in the WHERE clause.
           18  +*/
           19  +#include "sqliteInt.h"
           20  +#include "whereInt.h"
           21  +
           22  +/* Forward declarations */
           23  +static void exprAnalyze(SrcList*, WhereClause*, int);
           24  +
           25  +/*
           26  +** Deallocate all memory associated with a WhereOrInfo object.
           27  +*/
           28  +static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
           29  +  sqlite3WhereClauseClear(&p->wc);
           30  +  sqlite3DbFree(db, p);
           31  +}
           32  +
           33  +/*
           34  +** Deallocate all memory associated with a WhereAndInfo object.
           35  +*/
           36  +static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
           37  +  sqlite3WhereClauseClear(&p->wc);
           38  +  sqlite3DbFree(db, p);
           39  +}
           40  +
           41  +/*
           42  +** Add a single new WhereTerm entry to the WhereClause object pWC.
           43  +** The new WhereTerm object is constructed from Expr p and with wtFlags.
           44  +** The index in pWC->a[] of the new WhereTerm is returned on success.
           45  +** 0 is returned if the new WhereTerm could not be added due to a memory
           46  +** allocation error.  The memory allocation failure will be recorded in
           47  +** the db->mallocFailed flag so that higher-level functions can detect it.
           48  +**
           49  +** This routine will increase the size of the pWC->a[] array as necessary.
           50  +**
           51  +** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
           52  +** for freeing the expression p is assumed by the WhereClause object pWC.
           53  +** This is true even if this routine fails to allocate a new WhereTerm.
           54  +**
           55  +** WARNING:  This routine might reallocate the space used to store
           56  +** WhereTerms.  All pointers to WhereTerms should be invalidated after
           57  +** calling this routine.  Such pointers may be reinitialized by referencing
           58  +** the pWC->a[] array.
           59  +*/
           60  +static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
           61  +  WhereTerm *pTerm;
           62  +  int idx;
           63  +  testcase( wtFlags & TERM_VIRTUAL );
           64  +  if( pWC->nTerm>=pWC->nSlot ){
           65  +    WhereTerm *pOld = pWC->a;
           66  +    sqlite3 *db = pWC->pWInfo->pParse->db;
           67  +    pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
           68  +    if( pWC->a==0 ){
           69  +      if( wtFlags & TERM_DYNAMIC ){
           70  +        sqlite3ExprDelete(db, p);
           71  +      }
           72  +      pWC->a = pOld;
           73  +      return 0;
           74  +    }
           75  +    memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
           76  +    if( pOld!=pWC->aStatic ){
           77  +      sqlite3DbFree(db, pOld);
           78  +    }
           79  +    pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
           80  +    memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm));
           81  +  }
           82  +  pTerm = &pWC->a[idx = pWC->nTerm++];
           83  +  if( p && ExprHasProperty(p, EP_Unlikely) ){
           84  +    pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
           85  +  }else{
           86  +    pTerm->truthProb = 1;
           87  +  }
           88  +  pTerm->pExpr = sqlite3ExprSkipCollate(p);
           89  +  pTerm->wtFlags = wtFlags;
           90  +  pTerm->pWC = pWC;
           91  +  pTerm->iParent = -1;
           92  +  return idx;
           93  +}
           94  +
           95  +/*
           96  +** Return TRUE if the given operator is one of the operators that is
           97  +** allowed for an indexable WHERE clause term.  The allowed operators are
           98  +** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
           99  +*/
          100  +static int allowedOp(int op){
          101  +  assert( TK_GT>TK_EQ && TK_GT<TK_GE );
          102  +  assert( TK_LT>TK_EQ && TK_LT<TK_GE );
          103  +  assert( TK_LE>TK_EQ && TK_LE<TK_GE );
          104  +  assert( TK_GE==TK_EQ+4 );
          105  +  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
          106  +}
          107  +
          108  +/*
          109  +** Commute a comparison operator.  Expressions of the form "X op Y"
          110  +** are converted into "Y op X".
          111  +**
          112  +** If left/right precedence rules come into play when determining the
          113  +** collating sequence, then COLLATE operators are adjusted to ensure
          114  +** that the collating sequence does not change.  For example:
          115  +** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
          116  +** the left hand side of a comparison overrides any collation sequence 
          117  +** attached to the right. For the same reason the EP_Collate flag
          118  +** is not commuted.
          119  +*/
          120  +static void exprCommute(Parse *pParse, Expr *pExpr){
          121  +  u16 expRight = (pExpr->pRight->flags & EP_Collate);
          122  +  u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
          123  +  assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
          124  +  if( expRight==expLeft ){
          125  +    /* Either X and Y both have COLLATE operator or neither do */
          126  +    if( expRight ){
          127  +      /* Both X and Y have COLLATE operators.  Make sure X is always
          128  +      ** used by clearing the EP_Collate flag from Y. */
          129  +      pExpr->pRight->flags &= ~EP_Collate;
          130  +    }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
          131  +      /* Neither X nor Y have COLLATE operators, but X has a non-default
          132  +      ** collating sequence.  So add the EP_Collate marker on X to cause
          133  +      ** it to be searched first. */
          134  +      pExpr->pLeft->flags |= EP_Collate;
          135  +    }
          136  +  }
          137  +  SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
          138  +  if( pExpr->op>=TK_GT ){
          139  +    assert( TK_LT==TK_GT+2 );
          140  +    assert( TK_GE==TK_LE+2 );
          141  +    assert( TK_GT>TK_EQ );
          142  +    assert( TK_GT<TK_LE );
          143  +    assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
          144  +    pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
          145  +  }
          146  +}
          147  +
          148  +/*
          149  +** Translate from TK_xx operator to WO_xx bitmask.
          150  +*/
          151  +static u16 operatorMask(int op){
          152  +  u16 c;
          153  +  assert( allowedOp(op) );
          154  +  if( op==TK_IN ){
          155  +    c = WO_IN;
          156  +  }else if( op==TK_ISNULL ){
          157  +    c = WO_ISNULL;
          158  +  }else if( op==TK_IS ){
          159  +    c = WO_IS;
          160  +  }else{
          161  +    assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
          162  +    c = (u16)(WO_EQ<<(op-TK_EQ));
          163  +  }
          164  +  assert( op!=TK_ISNULL || c==WO_ISNULL );
          165  +  assert( op!=TK_IN || c==WO_IN );
          166  +  assert( op!=TK_EQ || c==WO_EQ );
          167  +  assert( op!=TK_LT || c==WO_LT );
          168  +  assert( op!=TK_LE || c==WO_LE );
          169  +  assert( op!=TK_GT || c==WO_GT );
          170  +  assert( op!=TK_GE || c==WO_GE );
          171  +  assert( op!=TK_IS || c==WO_IS );
          172  +  return c;
          173  +}
          174  +
          175  +
          176  +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
          177  +/*
          178  +** Check to see if the given expression is a LIKE or GLOB operator that
          179  +** can be optimized using inequality constraints.  Return TRUE if it is
          180  +** so and false if not.
          181  +**
          182  +** In order for the operator to be optimizible, the RHS must be a string
          183  +** literal that does not begin with a wildcard.  The LHS must be a column
          184  +** that may only be NULL, a string, or a BLOB, never a number. (This means
          185  +** that virtual tables cannot participate in the LIKE optimization.)  The
          186  +** collating sequence for the column on the LHS must be appropriate for
          187  +** the operator.
          188  +*/
          189  +static int isLikeOrGlob(
          190  +  Parse *pParse,    /* Parsing and code generating context */
          191  +  Expr *pExpr,      /* Test this expression */
          192  +  Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
          193  +  int *pisComplete, /* True if the only wildcard is % in the last character */
          194  +  int *pnoCase      /* True if uppercase is equivalent to lowercase */
          195  +){
          196  +  const char *z = 0;         /* String on RHS of LIKE operator */
          197  +  Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
          198  +  ExprList *pList;           /* List of operands to the LIKE operator */
          199  +  int c;                     /* One character in z[] */
          200  +  int cnt;                   /* Number of non-wildcard prefix characters */
          201  +  char wc[3];                /* Wildcard characters */
          202  +  sqlite3 *db = pParse->db;  /* Database connection */
          203  +  sqlite3_value *pVal = 0;
          204  +  int op;                    /* Opcode of pRight */
          205  +
          206  +  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
          207  +    return 0;
          208  +  }
          209  +#ifdef SQLITE_EBCDIC
          210  +  if( *pnoCase ) return 0;
          211  +#endif
          212  +  pList = pExpr->x.pList;
          213  +  pLeft = pList->a[1].pExpr;
          214  +  if( pLeft->op!=TK_COLUMN 
          215  +   || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
          216  +   || IsVirtual(pLeft->pTab)  /* Value might be numeric */
          217  +  ){
          218  +    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
          219  +    ** be the name of an indexed column with TEXT affinity. */
          220  +    return 0;
          221  +  }
          222  +  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
          223  +
          224  +  pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
          225  +  op = pRight->op;
          226  +  if( op==TK_VARIABLE ){
          227  +    Vdbe *pReprepare = pParse->pReprepare;
          228  +    int iCol = pRight->iColumn;
          229  +    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
          230  +    if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
          231  +      z = (char *)sqlite3_value_text(pVal);
          232  +    }
          233  +    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
          234  +    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
          235  +  }else if( op==TK_STRING ){
          236  +    z = pRight->u.zToken;
          237  +  }
          238  +  if( z ){
          239  +    cnt = 0;
          240  +    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
          241  +      cnt++;
          242  +    }
          243  +    if( cnt!=0 && 255!=(u8)z[cnt-1] ){
          244  +      Expr *pPrefix;
          245  +      *pisComplete = c==wc[0] && z[cnt+1]==0;
          246  +      pPrefix = sqlite3Expr(db, TK_STRING, z);
          247  +      if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
          248  +      *ppPrefix = pPrefix;
          249  +      if( op==TK_VARIABLE ){
          250  +        Vdbe *v = pParse->pVdbe;
          251  +        sqlite3VdbeSetVarmask(v, pRight->iColumn);
          252  +        if( *pisComplete && pRight->u.zToken[1] ){
          253  +          /* If the rhs of the LIKE expression is a variable, and the current
          254  +          ** value of the variable means there is no need to invoke the LIKE
          255  +          ** function, then no OP_Variable will be added to the program.
          256  +          ** This causes problems for the sqlite3_bind_parameter_name()
          257  +          ** API. To work around them, add a dummy OP_Variable here.
          258  +          */ 
          259  +          int r1 = sqlite3GetTempReg(pParse);
          260  +          sqlite3ExprCodeTarget(pParse, pRight, r1);
          261  +          sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
          262  +          sqlite3ReleaseTempReg(pParse, r1);
          263  +        }
          264  +      }
          265  +    }else{
          266  +      z = 0;
          267  +    }
          268  +  }
          269  +
          270  +  sqlite3ValueFree(pVal);
          271  +  return (z!=0);
          272  +}
          273  +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
          274  +
          275  +
          276  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          277  +/*
          278  +** Check to see if the given expression is of the form
          279  +**
          280  +**         column MATCH expr
          281  +**
          282  +** If it is then return TRUE.  If not, return FALSE.
          283  +*/
          284  +static int isMatchOfColumn(
          285  +  Expr *pExpr      /* Test this expression */
          286  +){
          287  +  ExprList *pList;
          288  +
          289  +  if( pExpr->op!=TK_FUNCTION ){
          290  +    return 0;
          291  +  }
          292  +  if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
          293  +    return 0;
          294  +  }
          295  +  pList = pExpr->x.pList;
          296  +  if( pList->nExpr!=2 ){
          297  +    return 0;
          298  +  }
          299  +  if( pList->a[1].pExpr->op != TK_COLUMN ){
          300  +    return 0;
          301  +  }
          302  +  return 1;
          303  +}
          304  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
          305  +
          306  +/*
          307  +** If the pBase expression originated in the ON or USING clause of
          308  +** a join, then transfer the appropriate markings over to derived.
          309  +*/
          310  +static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
          311  +  if( pDerived ){
          312  +    pDerived->flags |= pBase->flags & EP_FromJoin;
          313  +    pDerived->iRightJoinTable = pBase->iRightJoinTable;
          314  +  }
          315  +}
          316  +
          317  +/*
          318  +** Mark term iChild as being a child of term iParent
          319  +*/
          320  +static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
          321  +  pWC->a[iChild].iParent = iParent;
          322  +  pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
          323  +  pWC->a[iParent].nChild++;
          324  +}
          325  +
          326  +/*
          327  +** Return the N-th AND-connected subterm of pTerm.  Or if pTerm is not
          328  +** a conjunction, then return just pTerm when N==0.  If N is exceeds
          329  +** the number of available subterms, return NULL.
          330  +*/
          331  +static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
          332  +  if( pTerm->eOperator!=WO_AND ){
          333  +    return N==0 ? pTerm : 0;
          334  +  }
          335  +  if( N<pTerm->u.pAndInfo->wc.nTerm ){
          336  +    return &pTerm->u.pAndInfo->wc.a[N];
          337  +  }
          338  +  return 0;
          339  +}
          340  +
          341  +/*
          342  +** Subterms pOne and pTwo are contained within WHERE clause pWC.  The
          343  +** two subterms are in disjunction - they are OR-ed together.
          344  +**
          345  +** If these two terms are both of the form:  "A op B" with the same
          346  +** A and B values but different operators and if the operators are
          347  +** compatible (if one is = and the other is <, for example) then
          348  +** add a new virtual AND term to pWC that is the combination of the
          349  +** two.
          350  +**
          351  +** Some examples:
          352  +**
          353  +**    x<y OR x=y    -->     x<=y
          354  +**    x=y OR x=y    -->     x=y
          355  +**    x<=y OR x<y   -->     x<=y
          356  +**
          357  +** The following is NOT generated:
          358  +**
          359  +**    x<y OR x>y    -->     x!=y     
          360  +*/
          361  +static void whereCombineDisjuncts(
          362  +  SrcList *pSrc,         /* the FROM clause */
          363  +  WhereClause *pWC,      /* The complete WHERE clause */
          364  +  WhereTerm *pOne,       /* First disjunct */
          365  +  WhereTerm *pTwo        /* Second disjunct */
          366  +){
          367  +  u16 eOp = pOne->eOperator | pTwo->eOperator;
          368  +  sqlite3 *db;           /* Database connection (for malloc) */
          369  +  Expr *pNew;            /* New virtual expression */
          370  +  int op;                /* Operator for the combined expression */
          371  +  int idxNew;            /* Index in pWC of the next virtual term */
          372  +
          373  +  if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
          374  +  if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
          375  +  if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
          376  +   && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
          377  +  assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
          378  +  assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
          379  +  if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
          380  +  if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
          381  +  /* If we reach this point, it means the two subterms can be combined */
          382  +  if( (eOp & (eOp-1))!=0 ){
          383  +    if( eOp & (WO_LT|WO_LE) ){
          384  +      eOp = WO_LE;
          385  +    }else{
          386  +      assert( eOp & (WO_GT|WO_GE) );
          387  +      eOp = WO_GE;
          388  +    }
          389  +  }
          390  +  db = pWC->pWInfo->pParse->db;
          391  +  pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
          392  +  if( pNew==0 ) return;
          393  +  for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
          394  +  pNew->op = op;
          395  +  idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
          396  +  exprAnalyze(pSrc, pWC, idxNew);
          397  +}
          398  +
          399  +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
          400  +/*
          401  +** Analyze a term that consists of two or more OR-connected
          402  +** subterms.  So in:
          403  +**
          404  +**     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
          405  +**                          ^^^^^^^^^^^^^^^^^^^^
          406  +**
          407  +** This routine analyzes terms such as the middle term in the above example.
          408  +** A WhereOrTerm object is computed and attached to the term under
          409  +** analysis, regardless of the outcome of the analysis.  Hence:
          410  +**
          411  +**     WhereTerm.wtFlags   |=  TERM_ORINFO
          412  +**     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
          413  +**
          414  +** The term being analyzed must have two or more of OR-connected subterms.
          415  +** A single subterm might be a set of AND-connected sub-subterms.
          416  +** Examples of terms under analysis:
          417  +**
          418  +**     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
          419  +**     (B)     x=expr1 OR expr2=x OR x=expr3
          420  +**     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
          421  +**     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
          422  +**     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
          423  +**     (F)     x>A OR (x=A AND y>=B)
          424  +**
          425  +** CASE 1:
          426  +**
          427  +** If all subterms are of the form T.C=expr for some single column of C and
          428  +** a single table T (as shown in example B above) then create a new virtual
          429  +** term that is an equivalent IN expression.  In other words, if the term
          430  +** being analyzed is:
          431  +**
          432  +**      x = expr1  OR  expr2 = x  OR  x = expr3
          433  +**
          434  +** then create a new virtual term like this:
          435  +**
          436  +**      x IN (expr1,expr2,expr3)
          437  +**
          438  +** CASE 2:
          439  +**
          440  +** If there are exactly two disjuncts and one side has x>A and the other side
          441  +** has x=A (for the same x and A) then add a new virtual conjunct term to the
          442  +** WHERE clause of the form "x>=A".  Example:
          443  +**
          444  +**      x>A OR (x=A AND y>B)    adds:    x>=A
          445  +**
          446  +** The added conjunct can sometimes be helpful in query planning.
          447  +**
          448  +** CASE 3:
          449  +**
          450  +** If all subterms are indexable by a single table T, then set
          451  +**
          452  +**     WhereTerm.eOperator              =  WO_OR
          453  +**     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
          454  +**
          455  +** A subterm is "indexable" if it is of the form
          456  +** "T.C <op> <expr>" where C is any column of table T and 
          457  +** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
          458  +** A subterm is also indexable if it is an AND of two or more
          459  +** subsubterms at least one of which is indexable.  Indexable AND 
          460  +** subterms have their eOperator set to WO_AND and they have
          461  +** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
          462  +**
          463  +** From another point of view, "indexable" means that the subterm could
          464  +** potentially be used with an index if an appropriate index exists.
          465  +** This analysis does not consider whether or not the index exists; that
          466  +** is decided elsewhere.  This analysis only looks at whether subterms
          467  +** appropriate for indexing exist.
          468  +**
          469  +** All examples A through E above satisfy case 3.  But if a term
          470  +** also satisfies case 1 (such as B) we know that the optimizer will
          471  +** always prefer case 1, so in that case we pretend that case 3 is not
          472  +** satisfied.
          473  +**
          474  +** It might be the case that multiple tables are indexable.  For example,
          475  +** (E) above is indexable on tables P, Q, and R.
          476  +**
          477  +** Terms that satisfy case 3 are candidates for lookup by using
          478  +** separate indices to find rowids for each subterm and composing
          479  +** the union of all rowids using a RowSet object.  This is similar
          480  +** to "bitmap indices" in other database engines.
          481  +**
          482  +** OTHERWISE:
          483  +**
          484  +** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
          485  +** zero.  This term is not useful for search.
          486  +*/
          487  +static void exprAnalyzeOrTerm(
          488  +  SrcList *pSrc,            /* the FROM clause */
          489  +  WhereClause *pWC,         /* the complete WHERE clause */
          490  +  int idxTerm               /* Index of the OR-term to be analyzed */
          491  +){
          492  +  WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
          493  +  Parse *pParse = pWInfo->pParse;         /* Parser context */
          494  +  sqlite3 *db = pParse->db;               /* Database connection */
          495  +  WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
          496  +  Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
          497  +  int i;                                  /* Loop counters */
          498  +  WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
          499  +  WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
          500  +  WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
          501  +  Bitmask chngToIN;         /* Tables that might satisfy case 1 */
          502  +  Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
          503  +
          504  +  /*
          505  +  ** Break the OR clause into its separate subterms.  The subterms are
          506  +  ** stored in a WhereClause structure containing within the WhereOrInfo
          507  +  ** object that is attached to the original OR clause term.
          508  +  */
          509  +  assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
          510  +  assert( pExpr->op==TK_OR );
          511  +  pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
          512  +  if( pOrInfo==0 ) return;
          513  +  pTerm->wtFlags |= TERM_ORINFO;
          514  +  pOrWc = &pOrInfo->wc;
          515  +  sqlite3WhereClauseInit(pOrWc, pWInfo);
          516  +  sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
          517  +  sqlite3WhereExprAnalyze(pSrc, pOrWc);
          518  +  if( db->mallocFailed ) return;
          519  +  assert( pOrWc->nTerm>=2 );
          520  +
          521  +  /*
          522  +  ** Compute the set of tables that might satisfy cases 1 or 3.
          523  +  */
          524  +  indexable = ~(Bitmask)0;
          525  +  chngToIN = ~(Bitmask)0;
          526  +  for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
          527  +    if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
          528  +      WhereAndInfo *pAndInfo;
          529  +      assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
          530  +      chngToIN = 0;
          531  +      pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
          532  +      if( pAndInfo ){
          533  +        WhereClause *pAndWC;
          534  +        WhereTerm *pAndTerm;
          535  +        int j;
          536  +        Bitmask b = 0;
          537  +        pOrTerm->u.pAndInfo = pAndInfo;
          538  +        pOrTerm->wtFlags |= TERM_ANDINFO;
          539  +        pOrTerm->eOperator = WO_AND;
          540  +        pAndWC = &pAndInfo->wc;
          541  +        sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
          542  +        sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
          543  +        sqlite3WhereExprAnalyze(pSrc, pAndWC);
          544  +        pAndWC->pOuter = pWC;
          545  +        testcase( db->mallocFailed );
          546  +        if( !db->mallocFailed ){
          547  +          for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
          548  +            assert( pAndTerm->pExpr );
          549  +            if( allowedOp(pAndTerm->pExpr->op) ){
          550  +              b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
          551  +            }
          552  +          }
          553  +        }
          554  +        indexable &= b;
          555  +      }
          556  +    }else if( pOrTerm->wtFlags & TERM_COPIED ){
          557  +      /* Skip this term for now.  We revisit it when we process the
          558  +      ** corresponding TERM_VIRTUAL term */
          559  +    }else{
          560  +      Bitmask b;
          561  +      b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
          562  +      if( pOrTerm->wtFlags & TERM_VIRTUAL ){
          563  +        WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
          564  +        b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
          565  +      }
          566  +      indexable &= b;
          567  +      if( (pOrTerm->eOperator & WO_EQ)==0 ){
          568  +        chngToIN = 0;
          569  +      }else{
          570  +        chngToIN &= b;
          571  +      }
          572  +    }
          573  +  }
          574  +
          575  +  /*
          576  +  ** Record the set of tables that satisfy case 3.  The set might be
          577  +  ** empty.
          578  +  */
          579  +  pOrInfo->indexable = indexable;
          580  +  pTerm->eOperator = indexable==0 ? 0 : WO_OR;
          581  +
          582  +  /* For a two-way OR, attempt to implementation case 2.
          583  +  */
          584  +  if( indexable && pOrWc->nTerm==2 ){
          585  +    int iOne = 0;
          586  +    WhereTerm *pOne;
          587  +    while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
          588  +      int iTwo = 0;
          589  +      WhereTerm *pTwo;
          590  +      while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
          591  +        whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
          592  +      }
          593  +    }
          594  +  }
          595  +
          596  +  /*
          597  +  ** chngToIN holds a set of tables that *might* satisfy case 1.  But
          598  +  ** we have to do some additional checking to see if case 1 really
          599  +  ** is satisfied.
          600  +  **
          601  +  ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
          602  +  ** that there is no possibility of transforming the OR clause into an
          603  +  ** IN operator because one or more terms in the OR clause contain
          604  +  ** something other than == on a column in the single table.  The 1-bit
          605  +  ** case means that every term of the OR clause is of the form
          606  +  ** "table.column=expr" for some single table.  The one bit that is set
          607  +  ** will correspond to the common table.  We still need to check to make
          608  +  ** sure the same column is used on all terms.  The 2-bit case is when
          609  +  ** the all terms are of the form "table1.column=table2.column".  It
          610  +  ** might be possible to form an IN operator with either table1.column
          611  +  ** or table2.column as the LHS if either is common to every term of
          612  +  ** the OR clause.
          613  +  **
          614  +  ** Note that terms of the form "table.column1=table.column2" (the
          615  +  ** same table on both sizes of the ==) cannot be optimized.
          616  +  */
          617  +  if( chngToIN ){
          618  +    int okToChngToIN = 0;     /* True if the conversion to IN is valid */
          619  +    int iColumn = -1;         /* Column index on lhs of IN operator */
          620  +    int iCursor = -1;         /* Table cursor common to all terms */
          621  +    int j = 0;                /* Loop counter */
          622  +
          623  +    /* Search for a table and column that appears on one side or the
          624  +    ** other of the == operator in every subterm.  That table and column
          625  +    ** will be recorded in iCursor and iColumn.  There might not be any
          626  +    ** such table and column.  Set okToChngToIN if an appropriate table
          627  +    ** and column is found but leave okToChngToIN false if not found.
          628  +    */
          629  +    for(j=0; j<2 && !okToChngToIN; j++){
          630  +      pOrTerm = pOrWc->a;
          631  +      for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
          632  +        assert( pOrTerm->eOperator & WO_EQ );
          633  +        pOrTerm->wtFlags &= ~TERM_OR_OK;
          634  +        if( pOrTerm->leftCursor==iCursor ){
          635  +          /* This is the 2-bit case and we are on the second iteration and
          636  +          ** current term is from the first iteration.  So skip this term. */
          637  +          assert( j==1 );
          638  +          continue;
          639  +        }
          640  +        if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
          641  +                                            pOrTerm->leftCursor))==0 ){
          642  +          /* This term must be of the form t1.a==t2.b where t2 is in the
          643  +          ** chngToIN set but t1 is not.  This term will be either preceded
          644  +          ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
          645  +          ** and use its inversion. */
          646  +          testcase( pOrTerm->wtFlags & TERM_COPIED );
          647  +          testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
          648  +          assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
          649  +          continue;
          650  +        }
          651  +        iColumn = pOrTerm->u.leftColumn;
          652  +        iCursor = pOrTerm->leftCursor;
          653  +        break;
          654  +      }
          655  +      if( i<0 ){
          656  +        /* No candidate table+column was found.  This can only occur
          657  +        ** on the second iteration */
          658  +        assert( j==1 );
          659  +        assert( IsPowerOfTwo(chngToIN) );
          660  +        assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
          661  +        break;
          662  +      }
          663  +      testcase( j==1 );
          664  +
          665  +      /* We have found a candidate table and column.  Check to see if that
          666  +      ** table and column is common to every term in the OR clause */
          667  +      okToChngToIN = 1;
          668  +      for(; i>=0 && okToChngToIN; i--, pOrTerm++){
          669  +        assert( pOrTerm->eOperator & WO_EQ );
          670  +        if( pOrTerm->leftCursor!=iCursor ){
          671  +          pOrTerm->wtFlags &= ~TERM_OR_OK;
          672  +        }else if( pOrTerm->u.leftColumn!=iColumn ){
          673  +          okToChngToIN = 0;
          674  +        }else{
          675  +          int affLeft, affRight;
          676  +          /* If the right-hand side is also a column, then the affinities
          677  +          ** of both right and left sides must be such that no type
          678  +          ** conversions are required on the right.  (Ticket #2249)
          679  +          */
          680  +          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
          681  +          affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
          682  +          if( affRight!=0 && affRight!=affLeft ){
          683  +            okToChngToIN = 0;
          684  +          }else{
          685  +            pOrTerm->wtFlags |= TERM_OR_OK;
          686  +          }
          687  +        }
          688  +      }
          689  +    }
          690  +
          691  +    /* At this point, okToChngToIN is true if original pTerm satisfies
          692  +    ** case 1.  In that case, construct a new virtual term that is 
          693  +    ** pTerm converted into an IN operator.
          694  +    */
          695  +    if( okToChngToIN ){
          696  +      Expr *pDup;            /* A transient duplicate expression */
          697  +      ExprList *pList = 0;   /* The RHS of the IN operator */
          698  +      Expr *pLeft = 0;       /* The LHS of the IN operator */
          699  +      Expr *pNew;            /* The complete IN operator */
          700  +
          701  +      for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
          702  +        if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
          703  +        assert( pOrTerm->eOperator & WO_EQ );
          704  +        assert( pOrTerm->leftCursor==iCursor );
          705  +        assert( pOrTerm->u.leftColumn==iColumn );
          706  +        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
          707  +        pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
          708  +        pLeft = pOrTerm->pExpr->pLeft;
          709  +      }
          710  +      assert( pLeft!=0 );
          711  +      pDup = sqlite3ExprDup(db, pLeft, 0);
          712  +      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
          713  +      if( pNew ){
          714  +        int idxNew;
          715  +        transferJoinMarkings(pNew, pExpr);
          716  +        assert( !ExprHasProperty(pNew, EP_xIsSelect) );
          717  +        pNew->x.pList = pList;
          718  +        idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
          719  +        testcase( idxNew==0 );
          720  +        exprAnalyze(pSrc, pWC, idxNew);
          721  +        pTerm = &pWC->a[idxTerm];
          722  +        markTermAsChild(pWC, idxNew, idxTerm);
          723  +      }else{
          724  +        sqlite3ExprListDelete(db, pList);
          725  +      }
          726  +      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 3 */
          727  +    }
          728  +  }
          729  +}
          730  +#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
          731  +
          732  +/*
          733  +** We already know that pExpr is a binary operator where both operands are
          734  +** column references.  This routine checks to see if pExpr is an equivalence
          735  +** relation:
          736  +**   1.  The SQLITE_Transitive optimization must be enabled
          737  +**   2.  Must be either an == or an IS operator
          738  +**   3.  Not originating in the ON clause of an OUTER JOIN
          739  +**   4.  The affinities of A and B must be compatible
          740  +**   5a. Both operands use the same collating sequence OR
          741  +**   5b. The overall collating sequence is BINARY
          742  +** If this routine returns TRUE, that means that the RHS can be substituted
          743  +** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
          744  +** This is an optimization.  No harm comes from returning 0.  But if 1 is
          745  +** returned when it should not be, then incorrect answers might result.
          746  +*/
          747  +static int termIsEquivalence(Parse *pParse, Expr *pExpr){
          748  +  char aff1, aff2;
          749  +  CollSeq *pColl;
          750  +  const char *zColl1, *zColl2;
          751  +  if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
          752  +  if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
          753  +  if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
          754  +  aff1 = sqlite3ExprAffinity(pExpr->pLeft);
          755  +  aff2 = sqlite3ExprAffinity(pExpr->pRight);
          756  +  if( aff1!=aff2
          757  +   && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
          758  +  ){
          759  +    return 0;
          760  +  }
          761  +  pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
          762  +  if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
          763  +  pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
          764  +  /* Since pLeft and pRight are both a column references, their collating
          765  +  ** sequence should always be defined. */
          766  +  zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
          767  +  pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
          768  +  zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
          769  +  return sqlite3StrICmp(zColl1, zColl2)==0;
          770  +}
          771  +
          772  +/*
          773  +** Recursively walk the expressions of a SELECT statement and generate
          774  +** a bitmask indicating which tables are used in that expression
          775  +** tree.
          776  +*/
          777  +static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
          778  +  Bitmask mask = 0;
          779  +  while( pS ){
          780  +    SrcList *pSrc = pS->pSrc;
          781  +    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
          782  +    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
          783  +    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
          784  +    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
          785  +    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
          786  +    if( ALWAYS(pSrc!=0) ){
          787  +      int i;
          788  +      for(i=0; i<pSrc->nSrc; i++){
          789  +        mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
          790  +        mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
          791  +      }
          792  +    }
          793  +    pS = pS->pPrior;
          794  +  }
          795  +  return mask;
          796  +}
          797  +
          798  +/*
          799  +** The input to this routine is an WhereTerm structure with only the
          800  +** "pExpr" field filled in.  The job of this routine is to analyze the
          801  +** subexpression and populate all the other fields of the WhereTerm
          802  +** structure.
          803  +**
          804  +** If the expression is of the form "<expr> <op> X" it gets commuted
          805  +** to the standard form of "X <op> <expr>".
          806  +**
          807  +** If the expression is of the form "X <op> Y" where both X and Y are
          808  +** columns, then the original expression is unchanged and a new virtual
          809  +** term of the form "Y <op> X" is added to the WHERE clause and
          810  +** analyzed separately.  The original term is marked with TERM_COPIED
          811  +** and the new term is marked with TERM_DYNAMIC (because it's pExpr
          812  +** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
          813  +** is a commuted copy of a prior term.)  The original term has nChild=1
          814  +** and the copy has idxParent set to the index of the original term.
          815  +*/
          816  +static void exprAnalyze(
          817  +  SrcList *pSrc,            /* the FROM clause */
          818  +  WhereClause *pWC,         /* the WHERE clause */
          819  +  int idxTerm               /* Index of the term to be analyzed */
          820  +){
          821  +  WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
          822  +  WhereTerm *pTerm;                /* The term to be analyzed */
          823  +  WhereMaskSet *pMaskSet;          /* Set of table index masks */
          824  +  Expr *pExpr;                     /* The expression to be analyzed */
          825  +  Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
          826  +  Bitmask prereqAll;               /* Prerequesites of pExpr */
          827  +  Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
          828  +  Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
          829  +  int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
          830  +  int noCase = 0;                  /* uppercase equivalent to lowercase */
          831  +  int op;                          /* Top-level operator.  pExpr->op */
          832  +  Parse *pParse = pWInfo->pParse;  /* Parsing context */
          833  +  sqlite3 *db = pParse->db;        /* Database connection */
          834  +
          835  +  if( db->mallocFailed ){
          836  +    return;
          837  +  }
          838  +  pTerm = &pWC->a[idxTerm];
          839  +  pMaskSet = &pWInfo->sMaskSet;
          840  +  pExpr = pTerm->pExpr;
          841  +  assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
          842  +  prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
          843  +  op = pExpr->op;
          844  +  if( op==TK_IN ){
          845  +    assert( pExpr->pRight==0 );
          846  +    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
          847  +      pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
          848  +    }else{
          849  +      pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
          850  +    }
          851  +  }else if( op==TK_ISNULL ){
          852  +    pTerm->prereqRight = 0;
          853  +  }else{
          854  +    pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
          855  +  }
          856  +  prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr);
          857  +  if( ExprHasProperty(pExpr, EP_FromJoin) ){
          858  +    Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
          859  +    prereqAll |= x;
          860  +    extraRight = x-1;  /* ON clause terms may not be used with an index
          861  +                       ** on left table of a LEFT JOIN.  Ticket #3015 */
          862  +  }
          863  +  pTerm->prereqAll = prereqAll;
          864  +  pTerm->leftCursor = -1;
          865  +  pTerm->iParent = -1;
          866  +  pTerm->eOperator = 0;
          867  +  if( allowedOp(op) ){
          868  +    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
          869  +    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
          870  +    u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
          871  +    if( pLeft->op==TK_COLUMN ){
          872  +      pTerm->leftCursor = pLeft->iTable;
          873  +      pTerm->u.leftColumn = pLeft->iColumn;
          874  +      pTerm->eOperator = operatorMask(op) & opMask;
          875  +    }
          876  +    if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
          877  +    if( pRight && pRight->op==TK_COLUMN ){
          878  +      WhereTerm *pNew;
          879  +      Expr *pDup;
          880  +      u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
          881  +      if( pTerm->leftCursor>=0 ){
          882  +        int idxNew;
          883  +        pDup = sqlite3ExprDup(db, pExpr, 0);
          884  +        if( db->mallocFailed ){
          885  +          sqlite3ExprDelete(db, pDup);
          886  +          return;
          887  +        }
          888  +        idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
          889  +        if( idxNew==0 ) return;
          890  +        pNew = &pWC->a[idxNew];
          891  +        markTermAsChild(pWC, idxNew, idxTerm);
          892  +        if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
          893  +        pTerm = &pWC->a[idxTerm];
          894  +        pTerm->wtFlags |= TERM_COPIED;
          895  +
          896  +        if( termIsEquivalence(pParse, pDup) ){
          897  +          pTerm->eOperator |= WO_EQUIV;
          898  +          eExtraOp = WO_EQUIV;
          899  +        }
          900  +      }else{
          901  +        pDup = pExpr;
          902  +        pNew = pTerm;
          903  +      }
          904  +      exprCommute(pParse, pDup);
          905  +      pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
          906  +      pNew->leftCursor = pLeft->iTable;
          907  +      pNew->u.leftColumn = pLeft->iColumn;
          908  +      testcase( (prereqLeft | extraRight) != prereqLeft );
          909  +      pNew->prereqRight = prereqLeft | extraRight;
          910  +      pNew->prereqAll = prereqAll;
          911  +      pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
          912  +    }
          913  +  }
          914  +
          915  +#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
          916  +  /* If a term is the BETWEEN operator, create two new virtual terms
          917  +  ** that define the range that the BETWEEN implements.  For example:
          918  +  **
          919  +  **      a BETWEEN b AND c
          920  +  **
          921  +  ** is converted into:
          922  +  **
          923  +  **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
          924  +  **
          925  +  ** The two new terms are added onto the end of the WhereClause object.
          926  +  ** The new terms are "dynamic" and are children of the original BETWEEN
          927  +  ** term.  That means that if the BETWEEN term is coded, the children are
          928  +  ** skipped.  Or, if the children are satisfied by an index, the original
          929  +  ** BETWEEN term is skipped.
          930  +  */
          931  +  else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
          932  +    ExprList *pList = pExpr->x.pList;
          933  +    int i;
          934  +    static const u8 ops[] = {TK_GE, TK_LE};
          935  +    assert( pList!=0 );
          936  +    assert( pList->nExpr==2 );
          937  +    for(i=0; i<2; i++){
          938  +      Expr *pNewExpr;
          939  +      int idxNew;
          940  +      pNewExpr = sqlite3PExpr(pParse, ops[i], 
          941  +                             sqlite3ExprDup(db, pExpr->pLeft, 0),
          942  +                             sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
          943  +      transferJoinMarkings(pNewExpr, pExpr);
          944  +      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
          945  +      testcase( idxNew==0 );
          946  +      exprAnalyze(pSrc, pWC, idxNew);
          947  +      pTerm = &pWC->a[idxTerm];
          948  +      markTermAsChild(pWC, idxNew, idxTerm);
          949  +    }
          950  +  }
          951  +#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
          952  +
          953  +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
          954  +  /* Analyze a term that is composed of two or more subterms connected by
          955  +  ** an OR operator.
          956  +  */
          957  +  else if( pExpr->op==TK_OR ){
          958  +    assert( pWC->op==TK_AND );
          959  +    exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
          960  +    pTerm = &pWC->a[idxTerm];
          961  +  }
          962  +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
          963  +
          964  +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
          965  +  /* Add constraints to reduce the search space on a LIKE or GLOB
          966  +  ** operator.
          967  +  **
          968  +  ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
          969  +  **
          970  +  **          x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
          971  +  **
          972  +  ** The last character of the prefix "abc" is incremented to form the
          973  +  ** termination condition "abd".  If case is not significant (the default
          974  +  ** for LIKE) then the lower-bound is made all uppercase and the upper-
          975  +  ** bound is made all lowercase so that the bounds also work when comparing
          976  +  ** BLOBs.
          977  +  */
          978  +  if( pWC->op==TK_AND 
          979  +   && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
          980  +  ){
          981  +    Expr *pLeft;       /* LHS of LIKE/GLOB operator */
          982  +    Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
          983  +    Expr *pNewExpr1;
          984  +    Expr *pNewExpr2;
          985  +    int idxNew1;
          986  +    int idxNew2;
          987  +    const char *zCollSeqName;     /* Name of collating sequence */
          988  +    const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
          989  +
          990  +    pLeft = pExpr->x.pList->a[1].pExpr;
          991  +    pStr2 = sqlite3ExprDup(db, pStr1, 0);
          992  +
          993  +    /* Convert the lower bound to upper-case and the upper bound to
          994  +    ** lower-case (upper-case is less than lower-case in ASCII) so that
          995  +    ** the range constraints also work for BLOBs
          996  +    */
          997  +    if( noCase && !pParse->db->mallocFailed ){
          998  +      int i;
          999  +      char c;
         1000  +      pTerm->wtFlags |= TERM_LIKE;
         1001  +      for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
         1002  +        pStr1->u.zToken[i] = sqlite3Toupper(c);
         1003  +        pStr2->u.zToken[i] = sqlite3Tolower(c);
         1004  +      }
         1005  +    }
         1006  +
         1007  +    if( !db->mallocFailed ){
         1008  +      u8 c, *pC;       /* Last character before the first wildcard */
         1009  +      pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
         1010  +      c = *pC;
         1011  +      if( noCase ){
         1012  +        /* The point is to increment the last character before the first
         1013  +        ** wildcard.  But if we increment '@', that will push it into the
         1014  +        ** alphabetic range where case conversions will mess up the 
         1015  +        ** inequality.  To avoid this, make sure to also run the full
         1016  +        ** LIKE on all candidate expressions by clearing the isComplete flag
         1017  +        */
         1018  +        if( c=='A'-1 ) isComplete = 0;
         1019  +        c = sqlite3UpperToLower[c];
         1020  +      }
         1021  +      *pC = c + 1;
         1022  +    }
         1023  +    zCollSeqName = noCase ? "NOCASE" : "BINARY";
         1024  +    pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
         1025  +    pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
         1026  +           sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
         1027  +           pStr1, 0);
         1028  +    transferJoinMarkings(pNewExpr1, pExpr);
         1029  +    idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
         1030  +    testcase( idxNew1==0 );
         1031  +    exprAnalyze(pSrc, pWC, idxNew1);
         1032  +    pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
         1033  +    pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
         1034  +           sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
         1035  +           pStr2, 0);
         1036  +    transferJoinMarkings(pNewExpr2, pExpr);
         1037  +    idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
         1038  +    testcase( idxNew2==0 );
         1039  +    exprAnalyze(pSrc, pWC, idxNew2);
         1040  +    pTerm = &pWC->a[idxTerm];
         1041  +    if( isComplete ){
         1042  +      markTermAsChild(pWC, idxNew1, idxTerm);
         1043  +      markTermAsChild(pWC, idxNew2, idxTerm);
         1044  +    }
         1045  +  }
         1046  +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
         1047  +
         1048  +#ifndef SQLITE_OMIT_VIRTUALTABLE
         1049  +  /* Add a WO_MATCH auxiliary term to the constraint set if the
         1050  +  ** current expression is of the form:  column MATCH expr.
         1051  +  ** This information is used by the xBestIndex methods of
         1052  +  ** virtual tables.  The native query optimizer does not attempt
         1053  +  ** to do anything with MATCH functions.
         1054  +  */
         1055  +  if( isMatchOfColumn(pExpr) ){
         1056  +    int idxNew;
         1057  +    Expr *pRight, *pLeft;
         1058  +    WhereTerm *pNewTerm;
         1059  +    Bitmask prereqColumn, prereqExpr;
         1060  +
         1061  +    pRight = pExpr->x.pList->a[0].pExpr;
         1062  +    pLeft = pExpr->x.pList->a[1].pExpr;
         1063  +    prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
         1064  +    prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
         1065  +    if( (prereqExpr & prereqColumn)==0 ){
         1066  +      Expr *pNewExpr;
         1067  +      pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
         1068  +                              0, sqlite3ExprDup(db, pRight, 0), 0);
         1069  +      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
         1070  +      testcase( idxNew==0 );
         1071  +      pNewTerm = &pWC->a[idxNew];
         1072  +      pNewTerm->prereqRight = prereqExpr;
         1073  +      pNewTerm->leftCursor = pLeft->iTable;
         1074  +      pNewTerm->u.leftColumn = pLeft->iColumn;
         1075  +      pNewTerm->eOperator = WO_MATCH;
         1076  +      markTermAsChild(pWC, idxNew, idxTerm);
         1077  +      pTerm = &pWC->a[idxTerm];
         1078  +      pTerm->wtFlags |= TERM_COPIED;
         1079  +      pNewTerm->prereqAll = pTerm->prereqAll;
         1080  +    }
         1081  +  }
         1082  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
         1083  +
         1084  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1085  +  /* When sqlite_stat3 histogram data is available an operator of the
         1086  +  ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
         1087  +  ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
         1088  +  ** virtual term of that form.
         1089  +  **
         1090  +  ** Note that the virtual term must be tagged with TERM_VNULL.
         1091  +  */
         1092  +  if( pExpr->op==TK_NOTNULL
         1093  +   && pExpr->pLeft->op==TK_COLUMN
         1094  +   && pExpr->pLeft->iColumn>=0
         1095  +   && OptimizationEnabled(db, SQLITE_Stat34)
         1096  +  ){
         1097  +    Expr *pNewExpr;
         1098  +    Expr *pLeft = pExpr->pLeft;
         1099  +    int idxNew;
         1100  +    WhereTerm *pNewTerm;
         1101  +
         1102  +    pNewExpr = sqlite3PExpr(pParse, TK_GT,
         1103  +                            sqlite3ExprDup(db, pLeft, 0),
         1104  +                            sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
         1105  +
         1106  +    idxNew = whereClauseInsert(pWC, pNewExpr,
         1107  +                              TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
         1108  +    if( idxNew ){
         1109  +      pNewTerm = &pWC->a[idxNew];
         1110  +      pNewTerm->prereqRight = 0;
         1111  +      pNewTerm->leftCursor = pLeft->iTable;
         1112  +      pNewTerm->u.leftColumn = pLeft->iColumn;
         1113  +      pNewTerm->eOperator = WO_GT;
         1114  +      markTermAsChild(pWC, idxNew, idxTerm);
         1115  +      pTerm = &pWC->a[idxTerm];
         1116  +      pTerm->wtFlags |= TERM_COPIED;
         1117  +      pNewTerm->prereqAll = pTerm->prereqAll;
         1118  +    }
         1119  +  }
         1120  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1121  +
         1122  +  /* Prevent ON clause terms of a LEFT JOIN from being used to drive
         1123  +  ** an index for tables to the left of the join.
         1124  +  */
         1125  +  pTerm->prereqRight |= extraRight;
         1126  +}
         1127  +
         1128  +/***************************************************************************
         1129  +** Routines with file scope above.  Interface to the rest of the where.c
         1130  +** subsystem follows.
         1131  +***************************************************************************/
         1132  +
         1133  +/*
         1134  +** This routine identifies subexpressions in the WHERE clause where
         1135  +** each subexpression is separated by the AND operator or some other
         1136  +** operator specified in the op parameter.  The WhereClause structure
         1137  +** is filled with pointers to subexpressions.  For example:
         1138  +**
         1139  +**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
         1140  +**           \________/     \_______________/     \________________/
         1141  +**            slot[0]            slot[1]               slot[2]
         1142  +**
         1143  +** The original WHERE clause in pExpr is unaltered.  All this routine
         1144  +** does is make slot[] entries point to substructure within pExpr.
         1145  +**
         1146  +** In the previous sentence and in the diagram, "slot[]" refers to
         1147  +** the WhereClause.a[] array.  The slot[] array grows as needed to contain
         1148  +** all terms of the WHERE clause.
         1149  +*/
         1150  +void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
         1151  +  Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
         1152  +  pWC->op = op;
         1153  +  if( pE2==0 ) return;
         1154  +  if( pE2->op!=op ){
         1155  +    whereClauseInsert(pWC, pExpr, 0);
         1156  +  }else{
         1157  +    sqlite3WhereSplit(pWC, pE2->pLeft, op);
         1158  +    sqlite3WhereSplit(pWC, pE2->pRight, op);
         1159  +  }
         1160  +}
         1161  +
         1162  +/*
         1163  +** Initialize a preallocated WhereClause structure.
         1164  +*/
         1165  +void sqlite3WhereClauseInit(
         1166  +  WhereClause *pWC,        /* The WhereClause to be initialized */
         1167  +  WhereInfo *pWInfo        /* The WHERE processing context */
         1168  +){
         1169  +  pWC->pWInfo = pWInfo;
         1170  +  pWC->pOuter = 0;
         1171  +  pWC->nTerm = 0;
         1172  +  pWC->nSlot = ArraySize(pWC->aStatic);
         1173  +  pWC->a = pWC->aStatic;
         1174  +}
         1175  +
         1176  +/*
         1177  +** Deallocate a WhereClause structure.  The WhereClause structure
         1178  +** itself is not freed.  This routine is the inverse of sqlite3WhereClauseInit().
         1179  +*/
         1180  +void sqlite3WhereClauseClear(WhereClause *pWC){
         1181  +  int i;
         1182  +  WhereTerm *a;
         1183  +  sqlite3 *db = pWC->pWInfo->pParse->db;
         1184  +  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
         1185  +    if( a->wtFlags & TERM_DYNAMIC ){
         1186  +      sqlite3ExprDelete(db, a->pExpr);
         1187  +    }
         1188  +    if( a->wtFlags & TERM_ORINFO ){
         1189  +      whereOrInfoDelete(db, a->u.pOrInfo);
         1190  +    }else if( a->wtFlags & TERM_ANDINFO ){
         1191  +      whereAndInfoDelete(db, a->u.pAndInfo);
         1192  +    }
         1193  +  }
         1194  +  if( pWC->a!=pWC->aStatic ){
         1195  +    sqlite3DbFree(db, pWC->a);
         1196  +  }
         1197  +}
         1198  +
         1199  +
         1200  +/*
         1201  +** These routines walk (recursively) an expression tree and generate
         1202  +** a bitmask indicating which tables are used in that expression
         1203  +** tree.
         1204  +*/
         1205  +Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
         1206  +  Bitmask mask = 0;
         1207  +  if( p==0 ) return 0;
         1208  +  if( p->op==TK_COLUMN ){
         1209  +    mask = sqlite3WhereGetMask(pMaskSet, p->iTable);
         1210  +    return mask;
         1211  +  }
         1212  +  mask = sqlite3WhereExprUsage(pMaskSet, p->pRight);
         1213  +  mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
         1214  +  if( ExprHasProperty(p, EP_xIsSelect) ){
         1215  +    mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
         1216  +  }else{
         1217  +    mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
         1218  +  }
         1219  +  return mask;
         1220  +}
         1221  +Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
         1222  +  int i;
         1223  +  Bitmask mask = 0;
         1224  +  if( pList ){
         1225  +    for(i=0; i<pList->nExpr; i++){
         1226  +      mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
         1227  +    }
         1228  +  }
         1229  +  return mask;
         1230  +}
         1231  +
         1232  +
         1233  +/*
         1234  +** Call exprAnalyze on all terms in a WHERE clause.  
         1235  +**
         1236  +** Note that exprAnalyze() might add new virtual terms onto the
         1237  +** end of the WHERE clause.  We do not want to analyze these new
         1238  +** virtual terms, so start analyzing at the end and work forward
         1239  +** so that the added virtual terms are never processed.
         1240  +*/
         1241  +void sqlite3WhereExprAnalyze(
         1242  +  SrcList *pTabList,       /* the FROM clause */
         1243  +  WhereClause *pWC         /* the WHERE clause to be analyzed */
         1244  +){
         1245  +  int i;
         1246  +  for(i=pWC->nTerm-1; i>=0; i--){
         1247  +    exprAnalyze(pTabList, pWC, i);
         1248  +  }
         1249  +}

Changes to tool/mksqlite3c.tcl.

   285    285      mem5.c
   286    286      mutex.c
   287    287      mutex_noop.c
   288    288      mutex_unix.c
   289    289      mutex_w32.c
   290    290      malloc.c
   291    291      printf.c
          292  +   treeview.c
   292    293      random.c
   293    294      threads.c
   294    295      utf.c
   295    296      util.c
   296    297      hash.c
   297    298      opcodes.c
   298    299   
................................................................................
   339    340      prepare.c
   340    341      select.c
   341    342      table.c
   342    343      trigger.c
   343    344      update.c
   344    345      vacuum.c
   345    346      vtab.c
          347  +   wherecode.c
          348  +   whereexpr.c
   346    349      where.c
   347    350   
   348    351      parse.c
   349    352   
   350    353      tokenize.c
   351    354      complete.c
   352    355