System.Data.SQLite
Check-in [a92cbefdad]
Not logged in

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

Overview
Comment:Update SQLite core library to the latest trunk code.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a92cbefdadf8cd10acd8843f1bc4c7fcfbb00947
User & Date: mistachkin 2015-06-26 03:13:46
Context
2015-07-02
01:44
Update SQLite core library to the latest 3.8.11 alpha. check-in: 9d53109ce0 user: mistachkin tags: trunk
2015-06-26
03:13
Update SQLite core library to the latest trunk code. check-in: a92cbefdad user: mistachkin tags: trunk
2015-06-24
22:43
Make sure that manually calling the Cancel method still causes an Interrupt exception to be thrown. Update master release archive manifest. check-in: dae0c2e6fc user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to SQLite.Interop/src/core/sqlite3.c.

   226    226   ** "experimental".  Experimental interfaces are normally new
   227    227   ** features recently added to SQLite.  We do not anticipate changes
   228    228   ** to experimental interfaces but reserve the right to make minor changes
   229    229   ** if experience from use "in the wild" suggest such changes are prudent.
   230    230   **
   231    231   ** The official C-language API documentation for SQLite is derived
   232    232   ** from comments in this file.  This file is the authoritative source
   233         -** on how SQLite interfaces are suppose to operate.
          233  +** on how SQLite interfaces are supposed to operate.
   234    234   **
   235    235   ** The name of this file under configuration management is "sqlite.h.in".
   236    236   ** The makefile makes some minor changes to this file (such as inserting
   237    237   ** the version number) and changes its name to "sqlite3.h" as
   238    238   ** part of the build process.
   239    239   */
   240    240   #ifndef _SQLITE3_H_
................................................................................
   316    316   **
   317    317   ** See also: [sqlite3_libversion()],
   318    318   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   319    319   ** [sqlite_version()] and [sqlite_source_id()].
   320    320   */
   321    321   #define SQLITE_VERSION        "3.8.11"
   322    322   #define SQLITE_VERSION_NUMBER 3008011
   323         -#define SQLITE_SOURCE_ID      "2015-05-30 22:05:17 73fc058b3a74c1b018cff990de793f19a602c12f"
          323  +#define SQLITE_SOURCE_ID      "2015-06-26 02:41:31 015302f15e46a087ec92f3644c6741600dbf4306"
   324    324   
   325    325   /*
   326    326   ** CAPI3REF: Run-Time Library Version Numbers
   327    327   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   328    328   **
   329    329   ** These interfaces provide the same information as the [SQLITE_VERSION],
   330    330   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  9863   9863   #define OP_OpenWrite      55 /* synopsis: root=P2 iDb=P3                   */
  9864   9864   #define OP_OpenAutoindex  56 /* synopsis: nColumn=P2                       */
  9865   9865   #define OP_OpenEphemeral  57 /* synopsis: nColumn=P2                       */
  9866   9866   #define OP_SorterOpen     58
  9867   9867   #define OP_SequenceTest   59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
  9868   9868   #define OP_OpenPseudo     60 /* synopsis: P3 columns in r[P2]              */
  9869   9869   #define OP_Close          61
  9870         -#define OP_SeekLT         62 /* synopsis: key=r[P3@P4]                     */
  9871         -#define OP_SeekLE         63 /* synopsis: key=r[P3@P4]                     */
  9872         -#define OP_SeekGE         64 /* synopsis: key=r[P3@P4]                     */
  9873         -#define OP_SeekGT         65 /* synopsis: key=r[P3@P4]                     */
  9874         -#define OP_Seek           66 /* synopsis: intkey=r[P2]                     */
  9875         -#define OP_NoConflict     67 /* synopsis: key=r[P3@P4]                     */
  9876         -#define OP_NotFound       68 /* synopsis: key=r[P3@P4]                     */
  9877         -#define OP_Found          69 /* synopsis: key=r[P3@P4]                     */
  9878         -#define OP_NotExists      70 /* synopsis: intkey=r[P3]                     */
         9870  +#define OP_ColumnsUsed    62
         9871  +#define OP_SeekLT         63 /* synopsis: key=r[P3@P4]                     */
         9872  +#define OP_SeekLE         64 /* synopsis: key=r[P3@P4]                     */
         9873  +#define OP_SeekGE         65 /* synopsis: key=r[P3@P4]                     */
         9874  +#define OP_SeekGT         66 /* synopsis: key=r[P3@P4]                     */
         9875  +#define OP_Seek           67 /* synopsis: intkey=r[P2]                     */
         9876  +#define OP_NoConflict     68 /* synopsis: key=r[P3@P4]                     */
         9877  +#define OP_NotFound       69 /* synopsis: key=r[P3@P4]                     */
         9878  +#define OP_Found          70 /* synopsis: key=r[P3@P4]                     */
  9879   9879   #define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  9880   9880   #define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  9881         -#define OP_Sequence       73 /* synopsis: r[P2]=cursor[P1].ctr++           */
  9882         -#define OP_NewRowid       74 /* synopsis: r[P2]=rowid                      */
  9883         -#define OP_Insert         75 /* synopsis: intkey=r[P3] data=r[P2]          */
         9881  +#define OP_NotExists      73 /* synopsis: intkey=r[P3]                     */
         9882  +#define OP_Sequence       74 /* synopsis: r[P2]=cursor[P1].ctr++           */
         9883  +#define OP_NewRowid       75 /* synopsis: r[P2]=rowid                      */
  9884   9884   #define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  9885   9885   #define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  9886   9886   #define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  9887   9887   #define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  9888   9888   #define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  9889   9889   #define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  9890   9890   #define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  9891   9891   #define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  9892         -#define OP_InsertInt      84 /* synopsis: intkey=P3 data=r[P2]             */
         9892  +#define OP_Insert         84 /* synopsis: intkey=r[P3] data=r[P2]          */
  9893   9893   #define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  9894   9894   #define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  9895   9895   #define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  9896   9896   #define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  9897   9897   #define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  9898   9898   #define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  9899   9899   #define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  9900   9900   #define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  9901   9901   #define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  9902   9902   #define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  9903         -#define OP_Delete         95
         9903  +#define OP_InsertInt      95 /* synopsis: intkey=P3 data=r[P2]             */
  9904   9904   #define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  9905   9905   #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
  9906         -#define OP_ResetCount     98
  9907         -#define OP_SorterCompare  99 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
  9908         -#define OP_SorterData    100 /* synopsis: r[P2]=data                       */
  9909         -#define OP_RowKey        101 /* synopsis: r[P2]=key                        */
  9910         -#define OP_RowData       102 /* synopsis: r[P2]=data                       */
  9911         -#define OP_Rowid         103 /* synopsis: r[P2]=rowid                      */
  9912         -#define OP_NullRow       104
  9913         -#define OP_Last          105
  9914         -#define OP_SorterSort    106
  9915         -#define OP_Sort          107
  9916         -#define OP_Rewind        108
  9917         -#define OP_SorterInsert  109
  9918         -#define OP_IdxInsert     110 /* synopsis: key=r[P2]                        */
  9919         -#define OP_IdxDelete     111 /* synopsis: key=r[P2@P3]                     */
  9920         -#define OP_IdxRowid      112 /* synopsis: r[P2]=rowid                      */
  9921         -#define OP_IdxLE         113 /* synopsis: key=r[P3@P4]                     */
  9922         -#define OP_IdxGT         114 /* synopsis: key=r[P3@P4]                     */
  9923         -#define OP_IdxLT         115 /* synopsis: key=r[P3@P4]                     */
  9924         -#define OP_IdxGE         116 /* synopsis: key=r[P3@P4]                     */
  9925         -#define OP_Destroy       117
  9926         -#define OP_Clear         118
  9927         -#define OP_ResetSorter   119
  9928         -#define OP_CreateIndex   120 /* synopsis: r[P2]=root iDb=P1                */
  9929         -#define OP_CreateTable   121 /* synopsis: r[P2]=root iDb=P1                */
  9930         -#define OP_ParseSchema   122
  9931         -#define OP_LoadAnalysis  123
  9932         -#define OP_DropTable     124
  9933         -#define OP_DropIndex     125
  9934         -#define OP_DropTrigger   126
  9935         -#define OP_IntegrityCk   127
  9936         -#define OP_RowSetAdd     128 /* synopsis: rowset(P1)=r[P2]                 */
  9937         -#define OP_RowSetRead    129 /* synopsis: r[P3]=rowset(P1)                 */
  9938         -#define OP_RowSetTest    130 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  9939         -#define OP_Program       131
  9940         -#define OP_Param         132
         9906  +#define OP_Delete         98
         9907  +#define OP_ResetCount     99
         9908  +#define OP_SorterCompare 100 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
         9909  +#define OP_SorterData    101 /* synopsis: r[P2]=data                       */
         9910  +#define OP_RowKey        102 /* synopsis: r[P2]=key                        */
         9911  +#define OP_RowData       103 /* synopsis: r[P2]=data                       */
         9912  +#define OP_Rowid         104 /* synopsis: r[P2]=rowid                      */
         9913  +#define OP_NullRow       105
         9914  +#define OP_Last          106
         9915  +#define OP_SorterSort    107
         9916  +#define OP_Sort          108
         9917  +#define OP_Rewind        109
         9918  +#define OP_SorterInsert  110
         9919  +#define OP_IdxInsert     111 /* synopsis: key=r[P2]                        */
         9920  +#define OP_IdxDelete     112 /* synopsis: key=r[P2@P3]                     */
         9921  +#define OP_IdxRowid      113 /* synopsis: r[P2]=rowid                      */
         9922  +#define OP_IdxLE         114 /* synopsis: key=r[P3@P4]                     */
         9923  +#define OP_IdxGT         115 /* synopsis: key=r[P3@P4]                     */
         9924  +#define OP_IdxLT         116 /* synopsis: key=r[P3@P4]                     */
         9925  +#define OP_IdxGE         117 /* synopsis: key=r[P3@P4]                     */
         9926  +#define OP_Destroy       118
         9927  +#define OP_Clear         119
         9928  +#define OP_ResetSorter   120
         9929  +#define OP_CreateIndex   121 /* synopsis: r[P2]=root iDb=P1                */
         9930  +#define OP_CreateTable   122 /* synopsis: r[P2]=root iDb=P1                */
         9931  +#define OP_ParseSchema   123
         9932  +#define OP_LoadAnalysis  124
         9933  +#define OP_DropTable     125
         9934  +#define OP_DropIndex     126
         9935  +#define OP_DropTrigger   127
         9936  +#define OP_IntegrityCk   128
         9937  +#define OP_RowSetAdd     129 /* synopsis: rowset(P1)=r[P2]                 */
         9938  +#define OP_RowSetRead    130 /* synopsis: r[P3]=rowset(P1)                 */
         9939  +#define OP_RowSetTest    131 /* synopsis: if r[P3] in rowset(P1) goto P2   */
         9940  +#define OP_Program       132
  9941   9941   #define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
  9942         -#define OP_FkCounter     134 /* synopsis: fkctr[P1]+=P2                    */
  9943         -#define OP_FkIfZero      135 /* synopsis: if fkctr[P1]==0 goto P2          */
  9944         -#define OP_MemMax        136 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  9945         -#define OP_IfPos         137 /* synopsis: if r[P1]>0 goto P2               */
  9946         -#define OP_IfNeg         138 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2    */
  9947         -#define OP_IfNotZero     139 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */
  9948         -#define OP_DecrJumpZero  140 /* synopsis: if (--r[P1])==0 goto P2          */
  9949         -#define OP_JumpZeroIncr  141 /* synopsis: if (r[P1]++)==0 ) goto P2        */
  9950         -#define OP_AggFinal      142 /* synopsis: accum=r[P1] N=P2                 */
  9951         -#define OP_IncrVacuum    143
  9952         -#define OP_Expire        144
  9953         -#define OP_TableLock     145 /* synopsis: iDb=P1 root=P2 write=P3          */
  9954         -#define OP_VBegin        146
  9955         -#define OP_VCreate       147
  9956         -#define OP_VDestroy      148
  9957         -#define OP_VOpen         149
  9958         -#define OP_VColumn       150 /* synopsis: r[P3]=vcolumn(P2)                */
  9959         -#define OP_VNext         151
  9960         -#define OP_VRename       152
  9961         -#define OP_Pagecount     153
  9962         -#define OP_MaxPgcnt      154
  9963         -#define OP_Init          155 /* synopsis: Start at P2                      */
  9964         -#define OP_Noop          156
  9965         -#define OP_Explain       157
         9942  +#define OP_Param         134
         9943  +#define OP_FkCounter     135 /* synopsis: fkctr[P1]+=P2                    */
         9944  +#define OP_FkIfZero      136 /* synopsis: if fkctr[P1]==0 goto P2          */
         9945  +#define OP_MemMax        137 /* synopsis: r[P1]=max(r[P1],r[P2])           */
         9946  +#define OP_IfPos         138 /* synopsis: if r[P1]>0 goto P2               */
         9947  +#define OP_IfNeg         139 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2    */
         9948  +#define OP_IfNotZero     140 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */
         9949  +#define OP_DecrJumpZero  141 /* synopsis: if (--r[P1])==0 goto P2          */
         9950  +#define OP_JumpZeroIncr  142 /* synopsis: if (r[P1]++)==0 ) goto P2        */
         9951  +#define OP_AggFinal      143 /* synopsis: accum=r[P1] N=P2                 */
         9952  +#define OP_IncrVacuum    144
         9953  +#define OP_Expire        145
         9954  +#define OP_TableLock     146 /* synopsis: iDb=P1 root=P2 write=P3          */
         9955  +#define OP_VBegin        147
         9956  +#define OP_VCreate       148
         9957  +#define OP_VDestroy      149
         9958  +#define OP_VOpen         150
         9959  +#define OP_VColumn       151 /* synopsis: r[P3]=vcolumn(P2)                */
         9960  +#define OP_VNext         152
         9961  +#define OP_VRename       153
         9962  +#define OP_Pagecount     154
         9963  +#define OP_MaxPgcnt      155
         9964  +#define OP_Init          156 /* synopsis: Start at P2                      */
         9965  +#define OP_Noop          157
         9966  +#define OP_Explain       158
  9966   9967   
  9967   9968   
  9968   9969   /* Properties such as "out2" or "jump" that are specified in
  9969   9970   ** comments following the "case" for each opcode in the vdbe.c
  9970   9971   ** are encoded into bitvectors as follows:
  9971   9972   */
  9972   9973   #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
................................................................................
  9979   9980   /*   0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
  9980   9981   /*   8 */ 0x01, 0x01, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
  9981   9982   /*  16 */ 0x01, 0x01, 0x02, 0x12, 0x01, 0x02, 0x03, 0x08,\
  9982   9983   /*  24 */ 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10,\
  9983   9984   /*  32 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x02, 0x03, 0x02,\
  9984   9985   /*  40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
  9985   9986   /*  48 */ 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00, 0x00,\
  9986         -/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09,\
  9987         -/*  64 */ 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x09, 0x26,\
  9988         -/*  72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\
         9987  +/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,\
         9988  +/*  64 */ 0x09, 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x26,\
         9989  +/*  72 */ 0x26, 0x09, 0x10, 0x10, 0x03, 0x03, 0x0b, 0x0b,\
  9989   9990   /*  80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
  9990   9991   /*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
  9991         -/*  96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
  9992         -/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\
  9993         -/* 112 */ 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00, 0x00,\
  9994         -/* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  9995         -/* 128 */ 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00, 0x01,\
  9996         -/* 136 */ 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x01,\
  9997         -/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\
  9998         -/* 152 */ 0x00, 0x10, 0x10, 0x01, 0x00, 0x00,}
         9992  +/*  96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
         9993  +/* 104 */ 0x10, 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04,\
         9994  +/* 112 */ 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00,\
         9995  +/* 120 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
         9996  +/* 128 */ 0x00, 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00,\
         9997  +/* 136 */ 0x01, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00,\
         9998  +/* 144 */ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
         9999  +/* 152 */ 0x01, 0x00, 0x10, 0x10, 0x01, 0x00, 0x00,}
  9999  10000   
 10000  10001   /************** End of opcodes.h *********************************************/
 10001  10002   /************** Continuing where we left off in vdbe.h ***********************/
 10002  10003   
 10003  10004   /*
 10004  10005   ** Prototypes for the VDBE interface.  See comments on the implementation
 10005  10006   ** for a description of what each of these routines does.
................................................................................
 10006  10007   */
 10007  10008   SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
 10008  10009   SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
 10009  10010   SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
 10010  10011   SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
 10011  10012   SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
 10012  10013   SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
        10014  +SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
 10013  10015   SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
 10014  10016   SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
 10015  10017   SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
 10016  10018   SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
 10017  10019   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
 10018  10020   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
 10019  10021   SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
................................................................................
 11437  11439   ** the speed a little by numbering the values consecutively.  
 11438  11440   **
 11439  11441   ** But rather than start with 0 or 1, we begin with 'A'.  That way,
 11440  11442   ** when multiple affinity types are concatenated into a string and
 11441  11443   ** used as the P4 operand, they will be more readable.
 11442  11444   **
 11443  11445   ** Note also that the numeric types are grouped together so that testing
 11444         -** for a numeric type is a single comparison.  And the NONE type is first.
        11446  +** for a numeric type is a single comparison.  And the BLOB type is first.
 11445  11447   */
 11446         -#define SQLITE_AFF_NONE     'A'
        11448  +#define SQLITE_AFF_BLOB     'A'
 11447  11449   #define SQLITE_AFF_TEXT     'B'
 11448  11450   #define SQLITE_AFF_NUMERIC  'C'
 11449  11451   #define SQLITE_AFF_INTEGER  'D'
 11450  11452   #define SQLITE_AFF_REAL     'E'
 11451  11453   
 11452  11454   #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
 11453  11455   
................................................................................
 12196  12198   #ifndef SQLITE_OMIT_EXPLAIN
 12197  12199       u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
 12198  12200   #endif
 12199  12201       int iCursor;      /* The VDBE cursor number used to access this table */
 12200  12202       Expr *pOn;        /* The ON clause of a join */
 12201  12203       IdList *pUsing;   /* The USING clause of a join */
 12202  12204       Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
 12203         -    char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
        12205  +    char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
 12204  12206       Index *pIndex;    /* Index structure corresponding to zIndex, if any */
 12205  12207     } a[1];             /* One entry for each identifier on the list */
 12206  12208   };
 12207  12209   
 12208  12210   /*
 12209  12211   ** Permitted values of the SrcList.a.jointype field
 12210  12212   */
................................................................................
 13002  13004   # define sqlite3Isspace(x)   isspace((unsigned char)(x))
 13003  13005   # define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
 13004  13006   # define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
 13005  13007   # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
 13006  13008   # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
 13007  13009   # define sqlite3Tolower(x)   tolower((unsigned char)(x))
 13008  13010   #endif
        13011  +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 13009  13012   SQLITE_PRIVATE int sqlite3IsIdChar(u8);
        13013  +#endif
 13010  13014   
 13011  13015   /*
 13012  13016   ** Internal function prototypes
 13013  13017   */
 13014  13018   #define sqlite3StrICmp sqlite3_stricmp
 13015  13019   SQLITE_PRIVATE int sqlite3Strlen30(const char*);
 13016  13020   #define sqlite3StrNICmp sqlite3_strnicmp
................................................................................
 13030  13034   SQLITE_PRIVATE int sqlite3MallocSize(void*);
 13031  13035   SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
 13032  13036   SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
 13033  13037   SQLITE_PRIVATE void sqlite3ScratchFree(void*);
 13034  13038   SQLITE_PRIVATE void *sqlite3PageMalloc(int);
 13035  13039   SQLITE_PRIVATE void sqlite3PageFree(void*);
 13036  13040   SQLITE_PRIVATE void sqlite3MemSetDefault(void);
        13041  +#ifndef SQLITE_OMIT_BUILTIN_TEST
 13037  13042   SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
        13043  +#endif
 13038  13044   SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
 13039  13045   
 13040  13046   /*
 13041  13047   ** On systems with ample stack space and that support alloca(), make
 13042  13048   ** use of alloca() to obtain space for large automatic objects.  By default,
 13043  13049   ** obtain space from malloc().
 13044  13050   **
................................................................................
 13106  13112   SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
 13107  13113   #endif
 13108  13114   #if defined(SQLITE_TEST)
 13109  13115   SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
 13110  13116   #endif
 13111  13117   
 13112  13118   #if defined(SQLITE_DEBUG)
 13113         -SQLITE_PRIVATE   TreeView *sqlite3TreeViewPush(TreeView*,u8);
 13114         -SQLITE_PRIVATE   void sqlite3TreeViewPop(TreeView*);
 13115         -SQLITE_PRIVATE   void sqlite3TreeViewLine(TreeView*, const char*, ...);
 13116         -SQLITE_PRIVATE   void sqlite3TreeViewItem(TreeView*, const char*, u8);
 13117  13119   SQLITE_PRIVATE   void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
 13118  13120   SQLITE_PRIVATE   void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
 13119  13121   SQLITE_PRIVATE   void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
 13120  13122   #endif
 13121  13123   
 13122  13124   
 13123  13125   SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
................................................................................
 13178  13180   
 13179  13181   SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
 13180  13182   SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
 13181  13183   SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
 13182  13184   SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
 13183  13185   SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
 13184  13186   SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
        13187  +#ifndef SQLITE_OMIT_BUILTIN_TEST
 13185  13188   SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
        13189  +#endif
 13186  13190   
 13187  13191   SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
 13188  13192   SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
 13189  13193   SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
 13190  13194   SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
 13191  13195   SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
 13192  13196   
................................................................................
 13266  13270   SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
 13267  13271   SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
 13268  13272   SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
 13269  13273   #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
 13270  13274   #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
 13271  13275   SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
 13272  13276   SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
        13277  +SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
 13273  13278   SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
 13274  13279   SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
 13275  13280   SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
 13276  13281   SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
 13277  13282   SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
 13278  13283   SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
 13279  13284   SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
................................................................................
 13282  13287   SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int);
 13283  13288   SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
 13284  13289   SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
 13285  13290   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 13286  13291   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 13287  13292   SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
 13288  13293   SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
        13294  +#ifndef SQLITE_OMIT_BUILTIN_TEST
 13289  13295   SQLITE_PRIVATE void sqlite3PrngSaveState(void);
 13290  13296   SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
        13297  +#endif
 13291  13298   SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
 13292  13299   SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
 13293  13300   SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
 13294  13301   SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
 13295  13302   SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
 13296  13303   SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
 13297  13304   SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
................................................................................
 13501  13508   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 13502  13509   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 13503  13510   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 13504  13511   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 13505  13512   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
 13506  13513   SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
 13507  13514   SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
        13515  +SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
 13508  13516   SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
 13509  13517   SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
 13510  13518   SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
 13511  13519   SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
 13512  13520   SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 13513  13521   SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 13514  13522   SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
................................................................................
 14618  14626     Bool isTable:1;       /* True if a table requiring integer keys */
 14619  14627     Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
 14620  14628     Pgno pgnoRoot;        /* Root page of the open btree cursor */
 14621  14629     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
 14622  14630     i64 seqCount;         /* Sequence counter */
 14623  14631     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
 14624  14632     VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
        14633  +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
        14634  +  u64 maskUsed;         /* Mask of columns used by this cursor */
        14635  +#endif
 14625  14636   
 14626  14637     /* Cached information about the header for the data record that the
 14627  14638     ** cursor is currently pointing to.  Only valid if cacheStatus matches
 14628  14639     ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
 14629  14640     ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
 14630  14641     ** the cache is out of date.
 14631  14642     **
................................................................................
 19191  19202       sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
 19192  19203   
 19193  19204       if( sqlite3GlobalConfig.bCoreMutex ){
 19194  19205         pFrom = sqlite3DefaultMutex();
 19195  19206       }else{
 19196  19207         pFrom = sqlite3NoopMutex();
 19197  19208       }
 19198         -    memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
 19199         -    memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
 19200         -           sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
        19209  +    pTo->xMutexInit = pFrom->xMutexInit;
        19210  +    pTo->xMutexEnd = pFrom->xMutexEnd;
        19211  +    pTo->xMutexFree = pFrom->xMutexFree;
        19212  +    pTo->xMutexEnter = pFrom->xMutexEnter;
        19213  +    pTo->xMutexTry = pFrom->xMutexTry;
        19214  +    pTo->xMutexLeave = pFrom->xMutexLeave;
        19215  +    pTo->xMutexHeld = pFrom->xMutexHeld;
        19216  +    pTo->xMutexNotheld = pFrom->xMutexNotheld;
 19201  19217       pTo->xMutexAlloc = pFrom->xMutexAlloc;
 19202  19218     }
 19203  19219     rc = sqlite3GlobalConfig.mutex.xMutexInit();
 19204  19220   
 19205  19221   #ifdef SQLITE_DEBUG
 19206  19222     GLOBAL(int, mutexIsInit) = 1;
 19207  19223   #endif
................................................................................
 21372  21388     return rc & db->errMask;
 21373  21389   }
 21374  21390   
 21375  21391   /************** End of malloc.c **********************************************/
 21376  21392   /************** Begin file printf.c ******************************************/
 21377  21393   /*
 21378  21394   ** The "printf" code that follows dates from the 1980's.  It is in
 21379         -** the public domain.  The original comments are included here for
 21380         -** completeness.  They are very out-of-date but might be useful as
 21381         -** an historical reference.  Most of the "enhancements" have been backed
 21382         -** out so that the functionality is now the same as standard printf().
        21395  +** the public domain. 
 21383  21396   **
 21384  21397   **************************************************************************
 21385  21398   **
 21386  21399   ** This file contains code for a set of "printf"-like routines.  These
 21387  21400   ** routines format strings much like the printf() from the standard C
 21388  21401   ** library, though the implementation here has enhancements to support
 21389         -** SQLlite.
        21402  +** SQLite.
 21390  21403   */
 21391  21404   
 21392  21405   /*
 21393  21406   ** Conversion types fall into various categories as defined by the
 21394  21407   ** following enumeration.
 21395  21408   */
 21396  21409   #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
................................................................................
 22429  22442     va_end(ap);
 22430  22443     sqlite3StrAccumFinish(&acc);
 22431  22444     fprintf(stdout,"%s", zBuf);
 22432  22445     fflush(stdout);
 22433  22446   }
 22434  22447   #endif
 22435  22448   
 22436         -#ifdef SQLITE_DEBUG
 22437         -/*************************************************************************
 22438         -** Routines for implementing the "TreeView" display of hierarchical
 22439         -** data structures for debugging.
 22440         -**
 22441         -** The main entry points (coded elsewhere) are:
 22442         -**     sqlite3TreeViewExpr(0, pExpr, 0);
 22443         -**     sqlite3TreeViewExprList(0, pList, 0, 0);
 22444         -**     sqlite3TreeViewSelect(0, pSelect, 0);
 22445         -** Insert calls to those routines while debugging in order to display
 22446         -** a diagram of Expr, ExprList, and Select objects.
 22447         -**
 22448         -*/
 22449         -/* Add a new subitem to the tree.  The moreToFollow flag indicates that this
 22450         -** is not the last item in the tree. */
 22451         -SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
        22449  +
        22450  +/*
        22451  +** variable-argument wrapper around sqlite3VXPrintf().
        22452  +*/
        22453  +SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
        22454  +  va_list ap;
        22455  +  va_start(ap,zFormat);
        22456  +  sqlite3VXPrintf(p, bFlags, zFormat, ap);
        22457  +  va_end(ap);
        22458  +}
        22459  +
        22460  +/************** End of printf.c **********************************************/
        22461  +/************** Begin file treeview.c ****************************************/
        22462  +/*
        22463  +** 2015-06-08
        22464  +**
        22465  +** The author disclaims copyright to this source code.  In place of
        22466  +** a legal notice, here is a blessing:
        22467  +**
        22468  +**    May you do good and not evil.
        22469  +**    May you find forgiveness for yourself and forgive others.
        22470  +**    May you share freely, never taking more than you give.
        22471  +**
        22472  +*************************************************************************
        22473  +**
        22474  +** This file contains C code to implement the TreeView debugging routines.
        22475  +** These routines print a parse tree to standard output for debugging and
        22476  +** analysis. 
        22477  +**
        22478  +** The interfaces in this file is only available when compiling
        22479  +** with SQLITE_DEBUG.
        22480  +*/
        22481  +#ifdef SQLITE_DEBUG
        22482  +
        22483  +/*
        22484  +** Add a new subitem to the tree.  The moreToFollow flag indicates that this
        22485  +** is not the last item in the tree.
        22486  +*/
        22487  +static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
 22452  22488     if( p==0 ){
 22453  22489       p = sqlite3_malloc64( sizeof(*p) );
 22454  22490       if( p==0 ) return 0;
 22455  22491       memset(p, 0, sizeof(*p));
 22456  22492     }else{
 22457  22493       p->iLevel++;
 22458  22494     }
 22459  22495     assert( moreToFollow==0 || moreToFollow==1 );
 22460  22496     if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
 22461  22497     return p;
 22462  22498   }
 22463         -/* Finished with one layer of the tree */
 22464         -SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView *p){
        22499  +
        22500  +/*
        22501  +** Finished with one layer of the tree
        22502  +*/
        22503  +static void sqlite3TreeViewPop(TreeView *p){
 22465  22504     if( p==0 ) return;
 22466  22505     p->iLevel--;
 22467  22506     if( p->iLevel<0 ) sqlite3_free(p);
 22468  22507   }
 22469         -/* Generate a single line of output for the tree, with a prefix that contains
 22470         -** all the appropriate tree lines */
 22471         -SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
        22508  +
        22509  +/*
        22510  +** Generate a single line of output for the tree, with a prefix that contains
        22511  +** all the appropriate tree lines
        22512  +*/
        22513  +static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
 22472  22514     va_list ap;
 22473  22515     int i;
 22474  22516     StrAccum acc;
 22475  22517     char zBuf[500];
 22476  22518     sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
 22477  22519     if( p ){
 22478  22520       for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
................................................................................
 22484  22526     sqlite3VXPrintf(&acc, 0, zFormat, ap);
 22485  22527     va_end(ap);
 22486  22528     if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
 22487  22529     sqlite3StrAccumFinish(&acc);
 22488  22530     fprintf(stdout,"%s", zBuf);
 22489  22531     fflush(stdout);
 22490  22532   }
 22491         -/* Shorthand for starting a new tree item that consists of a single label */
 22492         -SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){
 22493         -  p = sqlite3TreeViewPush(p, moreToFollow);
        22533  +
        22534  +/*
        22535  +** Shorthand for starting a new tree item that consists of a single label
        22536  +*/
        22537  +static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
        22538  +  p = sqlite3TreeViewPush(p, moreFollows);
 22494  22539     sqlite3TreeViewLine(p, "%s", zLabel);
 22495  22540   }
        22541  +
        22542  +
        22543  +/*
        22544  +** Generate a human-readable description of a the Select object.
        22545  +*/
        22546  +SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
        22547  +  int n = 0;
        22548  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
        22549  +  sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
        22550  +    ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
        22551  +    ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
        22552  +  );
        22553  +  if( p->pSrc && p->pSrc->nSrc ) n++;
        22554  +  if( p->pWhere ) n++;
        22555  +  if( p->pGroupBy ) n++;
        22556  +  if( p->pHaving ) n++;
        22557  +  if( p->pOrderBy ) n++;
        22558  +  if( p->pLimit ) n++;
        22559  +  if( p->pOffset ) n++;
        22560  +  if( p->pPrior ) n++;
        22561  +  sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
        22562  +  if( p->pSrc && p->pSrc->nSrc ){
        22563  +    int i;
        22564  +    pView = sqlite3TreeViewPush(pView, (n--)>0);
        22565  +    sqlite3TreeViewLine(pView, "FROM");
        22566  +    for(i=0; i<p->pSrc->nSrc; i++){
        22567  +      struct SrcList_item *pItem = &p->pSrc->a[i];
        22568  +      StrAccum x;
        22569  +      char zLine[100];
        22570  +      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
        22571  +      sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
        22572  +      if( pItem->zDatabase ){
        22573  +        sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
        22574  +      }else if( pItem->zName ){
        22575  +        sqlite3XPrintf(&x, 0, " %s", pItem->zName);
        22576  +      }
        22577  +      if( pItem->pTab ){
        22578  +        sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
        22579  +      }
        22580  +      if( pItem->zAlias ){
        22581  +        sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
        22582  +      }
        22583  +      if( pItem->jointype & JT_LEFT ){
        22584  +        sqlite3XPrintf(&x, 0, " LEFT-JOIN");
        22585  +      }
        22586  +      sqlite3StrAccumFinish(&x);
        22587  +      sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
        22588  +      if( pItem->pSelect ){
        22589  +        sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
        22590  +      }
        22591  +      sqlite3TreeViewPop(pView);
        22592  +    }
        22593  +    sqlite3TreeViewPop(pView);
        22594  +  }
        22595  +  if( p->pWhere ){
        22596  +    sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
        22597  +    sqlite3TreeViewExpr(pView, p->pWhere, 0);
        22598  +    sqlite3TreeViewPop(pView);
        22599  +  }
        22600  +  if( p->pGroupBy ){
        22601  +    sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
        22602  +  }
        22603  +  if( p->pHaving ){
        22604  +    sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
        22605  +    sqlite3TreeViewExpr(pView, p->pHaving, 0);
        22606  +    sqlite3TreeViewPop(pView);
        22607  +  }
        22608  +  if( p->pOrderBy ){
        22609  +    sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
        22610  +  }
        22611  +  if( p->pLimit ){
        22612  +    sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
        22613  +    sqlite3TreeViewExpr(pView, p->pLimit, 0);
        22614  +    sqlite3TreeViewPop(pView);
        22615  +  }
        22616  +  if( p->pOffset ){
        22617  +    sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
        22618  +    sqlite3TreeViewExpr(pView, p->pOffset, 0);
        22619  +    sqlite3TreeViewPop(pView);
        22620  +  }
        22621  +  if( p->pPrior ){
        22622  +    const char *zOp = "UNION";
        22623  +    switch( p->op ){
        22624  +      case TK_ALL:         zOp = "UNION ALL";  break;
        22625  +      case TK_INTERSECT:   zOp = "INTERSECT";  break;
        22626  +      case TK_EXCEPT:      zOp = "EXCEPT";     break;
        22627  +    }
        22628  +    sqlite3TreeViewItem(pView, zOp, (n--)>0);
        22629  +    sqlite3TreeViewSelect(pView, p->pPrior, 0);
        22630  +    sqlite3TreeViewPop(pView);
        22631  +  }
        22632  +  sqlite3TreeViewPop(pView);
        22633  +}
        22634  +
        22635  +/*
        22636  +** Generate a human-readable explanation of an expression tree.
        22637  +*/
        22638  +SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
        22639  +  const char *zBinOp = 0;   /* Binary operator */
        22640  +  const char *zUniOp = 0;   /* Unary operator */
        22641  +  char zFlgs[30];
        22642  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
        22643  +  if( pExpr==0 ){
        22644  +    sqlite3TreeViewLine(pView, "nil");
        22645  +    sqlite3TreeViewPop(pView);
        22646  +    return;
        22647  +  }
        22648  +  if( pExpr->flags ){
        22649  +    sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  flags=0x%x",pExpr->flags);
        22650  +  }else{
        22651  +    zFlgs[0] = 0;
        22652  +  }
        22653  +  switch( pExpr->op ){
        22654  +    case TK_AGG_COLUMN: {
        22655  +      sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
        22656  +            pExpr->iTable, pExpr->iColumn, zFlgs);
        22657  +      break;
        22658  +    }
        22659  +    case TK_COLUMN: {
        22660  +      if( pExpr->iTable<0 ){
        22661  +        /* This only happens when coding check constraints */
        22662  +        sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
        22663  +      }else{
        22664  +        sqlite3TreeViewLine(pView, "{%d:%d}%s",
        22665  +                             pExpr->iTable, pExpr->iColumn, zFlgs);
        22666  +      }
        22667  +      break;
        22668  +    }
        22669  +    case TK_INTEGER: {
        22670  +      if( pExpr->flags & EP_IntValue ){
        22671  +        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
        22672  +      }else{
        22673  +        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
        22674  +      }
        22675  +      break;
        22676  +    }
        22677  +#ifndef SQLITE_OMIT_FLOATING_POINT
        22678  +    case TK_FLOAT: {
        22679  +      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
        22680  +      break;
        22681  +    }
        22682  +#endif
        22683  +    case TK_STRING: {
        22684  +      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
        22685  +      break;
        22686  +    }
        22687  +    case TK_NULL: {
        22688  +      sqlite3TreeViewLine(pView,"NULL");
        22689  +      break;
        22690  +    }
        22691  +#ifndef SQLITE_OMIT_BLOB_LITERAL
        22692  +    case TK_BLOB: {
        22693  +      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
        22694  +      break;
        22695  +    }
        22696  +#endif
        22697  +    case TK_VARIABLE: {
        22698  +      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
        22699  +                          pExpr->u.zToken, pExpr->iColumn);
        22700  +      break;
        22701  +    }
        22702  +    case TK_REGISTER: {
        22703  +      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
        22704  +      break;
        22705  +    }
        22706  +    case TK_AS: {
        22707  +      sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
        22708  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
        22709  +      break;
        22710  +    }
        22711  +    case TK_ID: {
        22712  +      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
        22713  +      break;
        22714  +    }
        22715  +#ifndef SQLITE_OMIT_CAST
        22716  +    case TK_CAST: {
        22717  +      /* Expressions of the form:   CAST(pLeft AS token) */
        22718  +      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
        22719  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
        22720  +      break;
        22721  +    }
        22722  +#endif /* SQLITE_OMIT_CAST */
        22723  +    case TK_LT:      zBinOp = "LT";     break;
        22724  +    case TK_LE:      zBinOp = "LE";     break;
        22725  +    case TK_GT:      zBinOp = "GT";     break;
        22726  +    case TK_GE:      zBinOp = "GE";     break;
        22727  +    case TK_NE:      zBinOp = "NE";     break;
        22728  +    case TK_EQ:      zBinOp = "EQ";     break;
        22729  +    case TK_IS:      zBinOp = "IS";     break;
        22730  +    case TK_ISNOT:   zBinOp = "ISNOT";  break;
        22731  +    case TK_AND:     zBinOp = "AND";    break;
        22732  +    case TK_OR:      zBinOp = "OR";     break;
        22733  +    case TK_PLUS:    zBinOp = "ADD";    break;
        22734  +    case TK_STAR:    zBinOp = "MUL";    break;
        22735  +    case TK_MINUS:   zBinOp = "SUB";    break;
        22736  +    case TK_REM:     zBinOp = "REM";    break;
        22737  +    case TK_BITAND:  zBinOp = "BITAND"; break;
        22738  +    case TK_BITOR:   zBinOp = "BITOR";  break;
        22739  +    case TK_SLASH:   zBinOp = "DIV";    break;
        22740  +    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
        22741  +    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
        22742  +    case TK_CONCAT:  zBinOp = "CONCAT"; break;
        22743  +    case TK_DOT:     zBinOp = "DOT";    break;
        22744  +
        22745  +    case TK_UMINUS:  zUniOp = "UMINUS"; break;
        22746  +    case TK_UPLUS:   zUniOp = "UPLUS";  break;
        22747  +    case TK_BITNOT:  zUniOp = "BITNOT"; break;
        22748  +    case TK_NOT:     zUniOp = "NOT";    break;
        22749  +    case TK_ISNULL:  zUniOp = "ISNULL"; break;
        22750  +    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
        22751  +
        22752  +    case TK_COLLATE: {
        22753  +      sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
        22754  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
        22755  +      break;
        22756  +    }
        22757  +
        22758  +    case TK_AGG_FUNCTION:
        22759  +    case TK_FUNCTION: {
        22760  +      ExprList *pFarg;       /* List of function arguments */
        22761  +      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
        22762  +        pFarg = 0;
        22763  +      }else{
        22764  +        pFarg = pExpr->x.pList;
        22765  +      }
        22766  +      if( pExpr->op==TK_AGG_FUNCTION ){
        22767  +        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
        22768  +                             pExpr->op2, pExpr->u.zToken);
        22769  +      }else{
        22770  +        sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
        22771  +      }
        22772  +      if( pFarg ){
        22773  +        sqlite3TreeViewExprList(pView, pFarg, 0, 0);
        22774  +      }
        22775  +      break;
        22776  +    }
        22777  +#ifndef SQLITE_OMIT_SUBQUERY
        22778  +    case TK_EXISTS: {
        22779  +      sqlite3TreeViewLine(pView, "EXISTS-expr");
        22780  +      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
        22781  +      break;
        22782  +    }
        22783  +    case TK_SELECT: {
        22784  +      sqlite3TreeViewLine(pView, "SELECT-expr");
        22785  +      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
        22786  +      break;
        22787  +    }
        22788  +    case TK_IN: {
        22789  +      sqlite3TreeViewLine(pView, "IN");
        22790  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
        22791  +      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
        22792  +        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
        22793  +      }else{
        22794  +        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
        22795  +      }
        22796  +      break;
        22797  +    }
        22798  +#endif /* SQLITE_OMIT_SUBQUERY */
        22799  +
        22800  +    /*
        22801  +    **    x BETWEEN y AND z
        22802  +    **
        22803  +    ** This is equivalent to
        22804  +    **
        22805  +    **    x>=y AND x<=z
        22806  +    **
        22807  +    ** X is stored in pExpr->pLeft.
        22808  +    ** Y is stored in pExpr->pList->a[0].pExpr.
        22809  +    ** Z is stored in pExpr->pList->a[1].pExpr.
        22810  +    */
        22811  +    case TK_BETWEEN: {
        22812  +      Expr *pX = pExpr->pLeft;
        22813  +      Expr *pY = pExpr->x.pList->a[0].pExpr;
        22814  +      Expr *pZ = pExpr->x.pList->a[1].pExpr;
        22815  +      sqlite3TreeViewLine(pView, "BETWEEN");
        22816  +      sqlite3TreeViewExpr(pView, pX, 1);
        22817  +      sqlite3TreeViewExpr(pView, pY, 1);
        22818  +      sqlite3TreeViewExpr(pView, pZ, 0);
        22819  +      break;
        22820  +    }
        22821  +    case TK_TRIGGER: {
        22822  +      /* If the opcode is TK_TRIGGER, then the expression is a reference
        22823  +      ** to a column in the new.* or old.* pseudo-tables available to
        22824  +      ** trigger programs. In this case Expr.iTable is set to 1 for the
        22825  +      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
        22826  +      ** is set to the column of the pseudo-table to read, or to -1 to
        22827  +      ** read the rowid field.
        22828  +      */
        22829  +      sqlite3TreeViewLine(pView, "%s(%d)", 
        22830  +          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
        22831  +      break;
        22832  +    }
        22833  +    case TK_CASE: {
        22834  +      sqlite3TreeViewLine(pView, "CASE");
        22835  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
        22836  +      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
        22837  +      break;
        22838  +    }
        22839  +#ifndef SQLITE_OMIT_TRIGGER
        22840  +    case TK_RAISE: {
        22841  +      const char *zType = "unk";
        22842  +      switch( pExpr->affinity ){
        22843  +        case OE_Rollback:   zType = "rollback";  break;
        22844  +        case OE_Abort:      zType = "abort";     break;
        22845  +        case OE_Fail:       zType = "fail";      break;
        22846  +        case OE_Ignore:     zType = "ignore";    break;
        22847  +      }
        22848  +      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
        22849  +      break;
        22850  +    }
        22851  +#endif
        22852  +    default: {
        22853  +      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
        22854  +      break;
        22855  +    }
        22856  +  }
        22857  +  if( zBinOp ){
        22858  +    sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
        22859  +    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
        22860  +    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
        22861  +  }else if( zUniOp ){
        22862  +    sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
        22863  +    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
        22864  +  }
        22865  +  sqlite3TreeViewPop(pView);
        22866  +}
        22867  +
        22868  +/*
        22869  +** Generate a human-readable explanation of an expression list.
        22870  +*/
        22871  +SQLITE_PRIVATE void sqlite3TreeViewExprList(
        22872  +  TreeView *pView,
        22873  +  const ExprList *pList,
        22874  +  u8 moreToFollow,
        22875  +  const char *zLabel
        22876  +){
        22877  +  int i;
        22878  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
        22879  +  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
        22880  +  if( pList==0 ){
        22881  +    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
        22882  +  }else{
        22883  +    sqlite3TreeViewLine(pView, "%s", zLabel);
        22884  +    for(i=0; i<pList->nExpr; i++){
        22885  +      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
        22886  +    }
        22887  +  }
        22888  +  sqlite3TreeViewPop(pView);
        22889  +}
        22890  +
 22496  22891   #endif /* SQLITE_DEBUG */
 22497  22892   
 22498         -/*
 22499         -** variable-argument wrapper around sqlite3VXPrintf().
 22500         -*/
 22501         -SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
 22502         -  va_list ap;
 22503         -  va_start(ap,zFormat);
 22504         -  sqlite3VXPrintf(p, bFlags, zFormat, ap);
 22505         -  va_end(ap);
 22506         -}
 22507         -
 22508         -/************** End of printf.c **********************************************/
        22893  +/************** End of treeview.c ********************************************/
 22509  22894   /************** Begin file random.c ******************************************/
 22510  22895   /*
 22511  22896   ** 2001 September 15
 22512  22897   **
 22513  22898   ** The author disclaims copyright to this source code.  In place of
 22514  22899   ** a legal notice, here is a blessing:
 22515  22900   **
................................................................................
 25153  25538        /*  55 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 25154  25539        /*  56 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 25155  25540        /*  57 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 25156  25541        /*  58 */ "SorterOpen"       OpHelp(""),
 25157  25542        /*  59 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 25158  25543        /*  60 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 25159  25544        /*  61 */ "Close"            OpHelp(""),
 25160         -     /*  62 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
 25161         -     /*  63 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
 25162         -     /*  64 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
 25163         -     /*  65 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
 25164         -     /*  66 */ "Seek"             OpHelp("intkey=r[P2]"),
 25165         -     /*  67 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
 25166         -     /*  68 */ "NotFound"         OpHelp("key=r[P3@P4]"),
 25167         -     /*  69 */ "Found"            OpHelp("key=r[P3@P4]"),
 25168         -     /*  70 */ "NotExists"        OpHelp("intkey=r[P3]"),
        25545  +     /*  62 */ "ColumnsUsed"      OpHelp(""),
        25546  +     /*  63 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
        25547  +     /*  64 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
        25548  +     /*  65 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
        25549  +     /*  66 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
        25550  +     /*  67 */ "Seek"             OpHelp("intkey=r[P2]"),
        25551  +     /*  68 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
        25552  +     /*  69 */ "NotFound"         OpHelp("key=r[P3@P4]"),
        25553  +     /*  70 */ "Found"            OpHelp("key=r[P3@P4]"),
 25169  25554        /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
 25170  25555        /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
 25171         -     /*  73 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 25172         -     /*  74 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 25173         -     /*  75 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
        25556  +     /*  73 */ "NotExists"        OpHelp("intkey=r[P3]"),
        25557  +     /*  74 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
        25558  +     /*  75 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 25174  25559        /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
 25175  25560        /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
 25176  25561        /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
 25177  25562        /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
 25178  25563        /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
 25179  25564        /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
 25180  25565        /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
 25181  25566        /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
 25182         -     /*  84 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
        25567  +     /*  84 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 25183  25568        /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 25184  25569        /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 25185  25570        /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 25186  25571        /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 25187  25572        /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 25188  25573        /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 25189  25574        /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 25190  25575        /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 25191  25576        /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 25192  25577        /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 25193         -     /*  95 */ "Delete"           OpHelp(""),
        25578  +     /*  95 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 25194  25579        /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 25195  25580        /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 25196         -     /*  98 */ "ResetCount"       OpHelp(""),
 25197         -     /*  99 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 25198         -     /* 100 */ "SorterData"       OpHelp("r[P2]=data"),
 25199         -     /* 101 */ "RowKey"           OpHelp("r[P2]=key"),
 25200         -     /* 102 */ "RowData"          OpHelp("r[P2]=data"),
 25201         -     /* 103 */ "Rowid"            OpHelp("r[P2]=rowid"),
 25202         -     /* 104 */ "NullRow"          OpHelp(""),
 25203         -     /* 105 */ "Last"             OpHelp(""),
 25204         -     /* 106 */ "SorterSort"       OpHelp(""),
 25205         -     /* 107 */ "Sort"             OpHelp(""),
 25206         -     /* 108 */ "Rewind"           OpHelp(""),
 25207         -     /* 109 */ "SorterInsert"     OpHelp(""),
 25208         -     /* 110 */ "IdxInsert"        OpHelp("key=r[P2]"),
 25209         -     /* 111 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 25210         -     /* 112 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 25211         -     /* 113 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
 25212         -     /* 114 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
 25213         -     /* 115 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
 25214         -     /* 116 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
 25215         -     /* 117 */ "Destroy"          OpHelp(""),
 25216         -     /* 118 */ "Clear"            OpHelp(""),
 25217         -     /* 119 */ "ResetSorter"      OpHelp(""),
 25218         -     /* 120 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
 25219         -     /* 121 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
 25220         -     /* 122 */ "ParseSchema"      OpHelp(""),
 25221         -     /* 123 */ "LoadAnalysis"     OpHelp(""),
 25222         -     /* 124 */ "DropTable"        OpHelp(""),
 25223         -     /* 125 */ "DropIndex"        OpHelp(""),
 25224         -     /* 126 */ "DropTrigger"      OpHelp(""),
 25225         -     /* 127 */ "IntegrityCk"      OpHelp(""),
 25226         -     /* 128 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 25227         -     /* 129 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
 25228         -     /* 130 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
 25229         -     /* 131 */ "Program"          OpHelp(""),
 25230         -     /* 132 */ "Param"            OpHelp(""),
        25581  +     /*  98 */ "Delete"           OpHelp(""),
        25582  +     /*  99 */ "ResetCount"       OpHelp(""),
        25583  +     /* 100 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        25584  +     /* 101 */ "SorterData"       OpHelp("r[P2]=data"),
        25585  +     /* 102 */ "RowKey"           OpHelp("r[P2]=key"),
        25586  +     /* 103 */ "RowData"          OpHelp("r[P2]=data"),
        25587  +     /* 104 */ "Rowid"            OpHelp("r[P2]=rowid"),
        25588  +     /* 105 */ "NullRow"          OpHelp(""),
        25589  +     /* 106 */ "Last"             OpHelp(""),
        25590  +     /* 107 */ "SorterSort"       OpHelp(""),
        25591  +     /* 108 */ "Sort"             OpHelp(""),
        25592  +     /* 109 */ "Rewind"           OpHelp(""),
        25593  +     /* 110 */ "SorterInsert"     OpHelp(""),
        25594  +     /* 111 */ "IdxInsert"        OpHelp("key=r[P2]"),
        25595  +     /* 112 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        25596  +     /* 113 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        25597  +     /* 114 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
        25598  +     /* 115 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
        25599  +     /* 116 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
        25600  +     /* 117 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
        25601  +     /* 118 */ "Destroy"          OpHelp(""),
        25602  +     /* 119 */ "Clear"            OpHelp(""),
        25603  +     /* 120 */ "ResetSorter"      OpHelp(""),
        25604  +     /* 121 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
        25605  +     /* 122 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
        25606  +     /* 123 */ "ParseSchema"      OpHelp(""),
        25607  +     /* 124 */ "LoadAnalysis"     OpHelp(""),
        25608  +     /* 125 */ "DropTable"        OpHelp(""),
        25609  +     /* 126 */ "DropIndex"        OpHelp(""),
        25610  +     /* 127 */ "DropTrigger"      OpHelp(""),
        25611  +     /* 128 */ "IntegrityCk"      OpHelp(""),
        25612  +     /* 129 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        25613  +     /* 130 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
        25614  +     /* 131 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
        25615  +     /* 132 */ "Program"          OpHelp(""),
 25231  25616        /* 133 */ "Real"             OpHelp("r[P2]=P4"),
 25232         -     /* 134 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 25233         -     /* 135 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
 25234         -     /* 136 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 25235         -     /* 137 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
 25236         -     /* 138 */ "IfNeg"            OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
 25237         -     /* 139 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"),
 25238         -     /* 140 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
 25239         -     /* 141 */ "JumpZeroIncr"     OpHelp("if (r[P1]++)==0 ) goto P2"),
 25240         -     /* 142 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 25241         -     /* 143 */ "IncrVacuum"       OpHelp(""),
 25242         -     /* 144 */ "Expire"           OpHelp(""),
 25243         -     /* 145 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 25244         -     /* 146 */ "VBegin"           OpHelp(""),
 25245         -     /* 147 */ "VCreate"          OpHelp(""),
 25246         -     /* 148 */ "VDestroy"         OpHelp(""),
 25247         -     /* 149 */ "VOpen"            OpHelp(""),
 25248         -     /* 150 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 25249         -     /* 151 */ "VNext"            OpHelp(""),
 25250         -     /* 152 */ "VRename"          OpHelp(""),
 25251         -     /* 153 */ "Pagecount"        OpHelp(""),
 25252         -     /* 154 */ "MaxPgcnt"         OpHelp(""),
 25253         -     /* 155 */ "Init"             OpHelp("Start at P2"),
 25254         -     /* 156 */ "Noop"             OpHelp(""),
 25255         -     /* 157 */ "Explain"          OpHelp(""),
        25617  +     /* 134 */ "Param"            OpHelp(""),
        25618  +     /* 135 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        25619  +     /* 136 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
        25620  +     /* 137 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        25621  +     /* 138 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
        25622  +     /* 139 */ "IfNeg"            OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
        25623  +     /* 140 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"),
        25624  +     /* 141 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
        25625  +     /* 142 */ "JumpZeroIncr"     OpHelp("if (r[P1]++)==0 ) goto P2"),
        25626  +     /* 143 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        25627  +     /* 144 */ "IncrVacuum"       OpHelp(""),
        25628  +     /* 145 */ "Expire"           OpHelp(""),
        25629  +     /* 146 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        25630  +     /* 147 */ "VBegin"           OpHelp(""),
        25631  +     /* 148 */ "VCreate"          OpHelp(""),
        25632  +     /* 149 */ "VDestroy"         OpHelp(""),
        25633  +     /* 150 */ "VOpen"            OpHelp(""),
        25634  +     /* 151 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        25635  +     /* 152 */ "VNext"            OpHelp(""),
        25636  +     /* 153 */ "VRename"          OpHelp(""),
        25637  +     /* 154 */ "Pagecount"        OpHelp(""),
        25638  +     /* 155 */ "MaxPgcnt"         OpHelp(""),
        25639  +     /* 156 */ "Init"             OpHelp("Start at P2"),
        25640  +     /* 157 */ "Noop"             OpHelp(""),
        25641  +     /* 158 */ "Explain"          OpHelp(""),
 25256  25642     };
 25257  25643     return azName[i];
 25258  25644   }
 25259  25645   #endif
 25260  25646   
 25261  25647   /************** End of opcodes.c *********************************************/
 25262  25648   /************** Begin file os_unix.c *****************************************/
................................................................................
 39298  39684     int szPage;                         /* Size of every page in this cache */
 39299  39685     int szExtra;                        /* Size of extra space for each page */
 39300  39686     u8 bPurgeable;                      /* True if pages are on backing store */
 39301  39687     u8 eCreate;                         /* eCreate value for for xFetch() */
 39302  39688     int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
 39303  39689     void *pStress;                      /* Argument to xStress */
 39304  39690     sqlite3_pcache *pCache;             /* Pluggable cache module */
 39305         -  PgHdr *pPage1;                      /* Reference to page 1 */
 39306  39691   };
 39307  39692   
 39308  39693   /********************************** Linked List Management ********************/
 39309  39694   
 39310  39695   /* Allowed values for second argument to pcacheManageDirtyList() */
 39311  39696   #define PCACHE_DIRTYLIST_REMOVE   1    /* Remove pPage from dirty list */
 39312  39697   #define PCACHE_DIRTYLIST_ADD      2    /* Add pPage to the dirty list */
................................................................................
 39376  39761   
 39377  39762   /*
 39378  39763   ** Wrapper around the pluggable caches xUnpin method. If the cache is
 39379  39764   ** being used for an in-memory database, this function is a no-op.
 39380  39765   */
 39381  39766   static void pcacheUnpin(PgHdr *p){
 39382  39767     if( p->pCache->bPurgeable ){
 39383         -    if( p->pgno==1 ){
 39384         -      p->pCache->pPage1 = 0;
 39385         -    }
 39386  39768       sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
 39387  39769     }
 39388  39770   }
 39389  39771   
 39390  39772   /*
 39391  39773   ** Compute the number of pages of cache requested.  p->szCache is the
 39392  39774   ** cache size requested by the "PRAGMA cache_size" statement.
................................................................................
 39471  39853       );
 39472  39854       if( pNew==0 ) return SQLITE_NOMEM;
 39473  39855       sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
 39474  39856       if( pCache->pCache ){
 39475  39857         sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
 39476  39858       }
 39477  39859       pCache->pCache = pNew;
 39478         -    pCache->pPage1 = 0;
 39479  39860       pCache->szPage = szPage;
 39480  39861     }
 39481  39862     return SQLITE_OK;
 39482  39863   }
 39483  39864   
 39484  39865   /*
 39485  39866   ** Try to obtain a page from the cache.
................................................................................
 39629  40010     if( !pPgHdr->pPage ){
 39630  40011       return pcacheFetchFinishWithInit(pCache, pgno, pPage);
 39631  40012     }
 39632  40013     if( 0==pPgHdr->nRef ){
 39633  40014       pCache->nRef++;
 39634  40015     }
 39635  40016     pPgHdr->nRef++;
 39636         -  if( pgno==1 ){
 39637         -    pCache->pPage1 = pPgHdr;
 39638         -  }
 39639  40017     return pPgHdr;
 39640  40018   }
 39641  40019   
 39642  40020   /*
 39643  40021   ** Decrement the reference count on a page. If the page is clean and the
 39644  40022   ** reference count drops to 0, then it is made eligible for recycling.
 39645  40023   */
................................................................................
 39672  40050   */
 39673  40051   SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
 39674  40052     assert( p->nRef==1 );
 39675  40053     if( p->flags&PGHDR_DIRTY ){
 39676  40054       pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
 39677  40055     }
 39678  40056     p->pCache->nRef--;
 39679         -  if( p->pgno==1 ){
 39680         -    p->pCache->pPage1 = 0;
 39681         -  }
 39682  40057     sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
 39683  40058   }
 39684  40059   
 39685  40060   /*
 39686  40061   ** Make sure the page is marked as dirty. If it isn't dirty already,
 39687  40062   ** make it so.
 39688  40063   */
................................................................................
 39765  40140         */
 39766  40141         assert( p->pgno>0 );
 39767  40142         if( ALWAYS(p->pgno>pgno) ){
 39768  40143           assert( p->flags&PGHDR_DIRTY );
 39769  40144           sqlite3PcacheMakeClean(p);
 39770  40145         }
 39771  40146       }
 39772         -    if( pgno==0 && pCache->pPage1 ){
 39773         -      memset(pCache->pPage1->pData, 0, pCache->szPage);
 39774         -      pgno = 1;
        40147  +    if( pgno==0 && pCache->nRef ){
        40148  +      sqlite3_pcache_page *pPage1;
        40149  +      pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
        40150  +      if( ALWAYS(pPage1) ){  /* Page 1 is always available in cache, because
        40151  +                             ** pCache->nRef>0 */
        40152  +        memset(pPage1->pBuf, 0, pCache->szPage);
        40153  +        pgno = 1;
        40154  +      }
 39775  40155       }
 39776  40156       sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
 39777  40157     }
 39778  40158   }
 39779  40159   
 39780  40160   /*
 39781  40161   ** Close a cache.
................................................................................
 40090  40470   ** compiling for systems that do not support real WSD.
 40091  40471   */
 40092  40472   #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
 40093  40473   
 40094  40474   /*
 40095  40475   ** Macros to enter and leave the PCache LRU mutex.
 40096  40476   */
 40097         -#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
 40098         -#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
        40477  +#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
        40478  +# define pcache1EnterMutex(X)  assert((X)->mutex==0)
        40479  +# define pcache1LeaveMutex(X)  assert((X)->mutex==0)
        40480  +# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
        40481  +#else
        40482  +# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
        40483  +# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
        40484  +# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
        40485  +#endif
 40099  40486   
 40100  40487   /******************************************************************************/
 40101  40488   /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
 40102  40489   
 40103  40490   /*
 40104  40491   ** This function is called during initialization if a static buffer is 
 40105  40492   ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
................................................................................
 40367  40754   /*
 40368  40755   ** This function is used internally to remove the page pPage from the 
 40369  40756   ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
 40370  40757   ** LRU list, then this function is a no-op.
 40371  40758   **
 40372  40759   ** The PGroup mutex must be held when this function is called.
 40373  40760   */
 40374         -static void pcache1PinPage(PgHdr1 *pPage){
        40761  +static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
 40375  40762     PCache1 *pCache;
 40376         -  PGroup *pGroup;
 40377  40763   
 40378  40764     assert( pPage!=0 );
 40379  40765     assert( pPage->isPinned==0 );
 40380  40766     pCache = pPage->pCache;
 40381         -  pGroup = pCache->pGroup;
 40382         -  assert( pPage->pLruNext || pPage==pGroup->pLruTail );
 40383         -  assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
 40384         -  assert( sqlite3_mutex_held(pGroup->mutex) );
        40767  +  assert( pPage->pLruNext || pPage==pCache->pGroup->pLruTail );
        40768  +  assert( pPage->pLruPrev || pPage==pCache->pGroup->pLruHead );
        40769  +  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 40385  40770     if( pPage->pLruPrev ){
 40386  40771       pPage->pLruPrev->pLruNext = pPage->pLruNext;
 40387  40772     }else{
 40388         -    pGroup->pLruHead = pPage->pLruNext;
        40773  +    pCache->pGroup->pLruHead = pPage->pLruNext;
 40389  40774     }
 40390  40775     if( pPage->pLruNext ){
 40391  40776       pPage->pLruNext->pLruPrev = pPage->pLruPrev;
 40392  40777     }else{
 40393         -    pGroup->pLruTail = pPage->pLruPrev;
        40778  +    pCache->pGroup->pLruTail = pPage->pLruPrev;
 40394  40779     }
 40395  40780     pPage->pLruNext = 0;
 40396  40781     pPage->pLruPrev = 0;
 40397  40782     pPage->isPinned = 1;
 40398  40783     pCache->nRecyclable--;
        40784  +  return pPage;
 40399  40785   }
 40400  40786   
 40401  40787   
 40402  40788   /*
 40403  40789   ** Remove the page supplied as an argument from the hash table 
 40404  40790   ** (PCache1.apHash structure) that it is currently stored in.
 40405  40791   **
................................................................................
 40472  40858   /*
 40473  40859   ** Implementation of the sqlite3_pcache.xInit method.
 40474  40860   */
 40475  40861   static int pcache1Init(void *NotUsed){
 40476  40862     UNUSED_PARAMETER(NotUsed);
 40477  40863     assert( pcache1.isInit==0 );
 40478  40864     memset(&pcache1, 0, sizeof(pcache1));
        40865  +#if SQLITE_THREADSAFE
 40479  40866     if( sqlite3GlobalConfig.bCoreMutex ){
 40480  40867       pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
 40481  40868       pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
 40482  40869     }
        40870  +#endif
 40483  40871     pcache1.grp.mxPinned = 10;
 40484  40872     pcache1.isInit = 1;
 40485  40873     return SQLITE_OK;
 40486  40874   }
 40487  40875   
 40488  40876   /*
 40489  40877   ** Implementation of the sqlite3_pcache.xShutdown method.
................................................................................
 40671  41059       }
 40672  41060     }
 40673  41061   
 40674  41062     /* Step 5. If a usable page buffer has still not been found, 
 40675  41063     ** attempt to allocate a new one. 
 40676  41064     */
 40677  41065     if( !pPage ){
 40678         -    if( createFlag==1 ) sqlite3BeginBenignMalloc();
        41066  +    if( createFlag==1 ){ sqlite3BeginBenignMalloc(); }
 40679  41067       pPage = pcache1AllocPage(pCache);
 40680         -    if( createFlag==1 ) sqlite3EndBenignMalloc();
        41068  +    if( createFlag==1 ){ sqlite3EndBenignMalloc(); }
 40681  41069     }
 40682  41070   
 40683  41071     if( pPage ){
 40684  41072       unsigned int h = iKey % pCache->nHash;
 40685  41073       pCache->nPage++;
 40686  41074       pPage->iKey = iKey;
 40687  41075       pPage->pNext = pCache->apHash[h];
................................................................................
 40747  41135   **           unnecessary pages cache entry allocations
 40748  41136   **
 40749  41137   **      then attempt to recycle a page from the LRU list. If it is the right
 40750  41138   **      size, return the recycled buffer. Otherwise, free the buffer and
 40751  41139   **      proceed to step 5. 
 40752  41140   **
 40753  41141   **   5. Otherwise, allocate and return a new page buffer.
        41142  +**
        41143  +** There are two versions of this routine.  pcache1FetchWithMutex() is
        41144  +** the general case.  pcache1FetchNoMutex() is a faster implementation for
        41145  +** the common case where pGroup->mutex is NULL.  The pcache1Fetch() wrapper
        41146  +** invokes the appropriate routine.
 40754  41147   */
 40755         -static sqlite3_pcache_page *pcache1Fetch(
        41148  +static PgHdr1 *pcache1FetchNoMutex(
 40756  41149     sqlite3_pcache *p, 
 40757  41150     unsigned int iKey, 
 40758  41151     int createFlag
 40759  41152   ){
 40760  41153     PCache1 *pCache = (PCache1 *)p;
 40761  41154     PgHdr1 *pPage = 0;
 40762  41155   
 40763         -  assert( offsetof(PgHdr1,page)==0 );
 40764         -  assert( pCache->bPurgeable || createFlag!=1 );
 40765         -  assert( pCache->bPurgeable || pCache->nMin==0 );
 40766         -  assert( pCache->bPurgeable==0 || pCache->nMin==10 );
 40767         -  assert( pCache->nMin==0 || pCache->bPurgeable );
 40768         -  assert( pCache->nHash>0 );
 40769         -  pcache1EnterMutex(pCache->pGroup);
 40770         -
 40771  41156     /* Step 1: Search the hash table for an existing entry. */
 40772  41157     pPage = pCache->apHash[iKey % pCache->nHash];
 40773  41158     while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
 40774  41159   
 40775  41160     /* Step 2: Abort if no existing page is found and createFlag is 0 */
 40776  41161     if( pPage ){
 40777         -    if( !pPage->isPinned ) pcache1PinPage(pPage);
        41162  +    if( !pPage->isPinned ){
        41163  +      return pcache1PinPage(pPage);
        41164  +    }else{
        41165  +      return pPage;
        41166  +    }
 40778  41167     }else if( createFlag ){
 40779  41168       /* Steps 3, 4, and 5 implemented by this subroutine */
 40780         -    pPage = pcache1FetchStage2(pCache, iKey, createFlag);
        41169  +    return pcache1FetchStage2(pCache, iKey, createFlag);
        41170  +  }else{
        41171  +    return 0;
 40781  41172     }
        41173  +}
        41174  +#if PCACHE1_MIGHT_USE_GROUP_MUTEX
        41175  +static PgHdr1 *pcache1FetchWithMutex(
        41176  +  sqlite3_pcache *p, 
        41177  +  unsigned int iKey, 
        41178  +  int createFlag
        41179  +){
        41180  +  PCache1 *pCache = (PCache1 *)p;
        41181  +  PgHdr1 *pPage;
        41182  +
        41183  +  pcache1EnterMutex(pCache->pGroup);
        41184  +  pPage = pcache1FetchNoMutex(p, iKey, createFlag);
 40782  41185     assert( pPage==0 || pCache->iMaxKey>=iKey );
 40783  41186     pcache1LeaveMutex(pCache->pGroup);
 40784         -  return (sqlite3_pcache_page*)pPage;
        41187  +  return pPage;
        41188  +}
        41189  +#endif
        41190  +static sqlite3_pcache_page *pcache1Fetch(
        41191  +  sqlite3_pcache *p, 
        41192  +  unsigned int iKey, 
        41193  +  int createFlag
        41194  +){
        41195  +#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
        41196  +  PCache1 *pCache = (PCache1 *)p;
        41197  +#endif
        41198  +
        41199  +  assert( offsetof(PgHdr1,page)==0 );
        41200  +  assert( pCache->bPurgeable || createFlag!=1 );
        41201  +  assert( pCache->bPurgeable || pCache->nMin==0 );
        41202  +  assert( pCache->bPurgeable==0 || pCache->nMin==10 );
        41203  +  assert( pCache->nMin==0 || pCache->bPurgeable );
        41204  +  assert( pCache->nHash>0 );
        41205  +#if PCACHE1_MIGHT_USE_GROUP_MUTEX
        41206  +  if( pCache->pGroup->mutex ){
        41207  +    return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
        41208  +  }else
        41209  +#endif
        41210  +  {
        41211  +    return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
        41212  +  }
 40785  41213   }
 40786  41214   
 40787  41215   
 40788  41216   /*
 40789  41217   ** Implementation of the sqlite3_pcache.xUnpin method.
 40790  41218   **
 40791  41219   ** Mark a page as unpinned (eligible for asynchronous recycling).
................................................................................
 52328  52756   ** small cells will be rare, but they are possible.
 52329  52757   */
 52330  52758   #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
 52331  52759   
 52332  52760   /* Forward declarations */
 52333  52761   typedef struct MemPage MemPage;
 52334  52762   typedef struct BtLock BtLock;
        52763  +typedef struct CellInfo CellInfo;
 52335  52764   
 52336  52765   /*
 52337  52766   ** This is a magic string that appears at the beginning of every
 52338  52767   ** SQLite database in order to identify the file as a real database.
 52339  52768   **
 52340  52769   ** You can change this value at compile-time by specifying a
 52341  52770   ** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
................................................................................
 52392  52821                          ** non-overflow cell */
 52393  52822     u8 *apOvfl[5];       /* Pointers to the body of overflow cells */
 52394  52823     BtShared *pBt;       /* Pointer to BtShared that this page is part of */
 52395  52824     u8 *aData;           /* Pointer to disk image of the page data */
 52396  52825     u8 *aDataEnd;        /* One byte past the end of usable data */
 52397  52826     u8 *aCellIdx;        /* The cell index area */
 52398  52827     DbPage *pDbPage;     /* Pager page handle */
        52828  +  u16 (*xCellSize)(MemPage*,u8*);             /* cellSizePtr method */
        52829  +  void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
 52399  52830     Pgno pgno;           /* Page number for this page */
 52400  52831   };
 52401  52832   
 52402  52833   /*
 52403  52834   ** The in-memory image of a disk page has the auxiliary information appended
 52404  52835   ** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
 52405  52836   ** that extra information.
................................................................................
 52447  52878   */
 52448  52879   struct Btree {
 52449  52880     sqlite3 *db;       /* The database connection holding this btree */
 52450  52881     BtShared *pBt;     /* Sharable content of this btree */
 52451  52882     u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
 52452  52883     u8 sharable;       /* True if we can share pBt with another db */
 52453  52884     u8 locked;         /* True if db currently has pBt locked */
        52885  +  u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
 52454  52886     int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
 52455  52887     int nBackup;       /* Number of backup operations reading this btree */
 52456  52888     u32 iDataVersion;  /* Combines with pBt->pPager->iDataVersion */
 52457  52889     Btree *pNext;      /* List of other sharable Btrees from the same db */
 52458  52890     Btree *pPrev;      /* Back pointer of the same list */
 52459  52891   #ifndef SQLITE_OMIT_SHARED_CACHE
 52460  52892     BtLock lock;       /* Object used to lock page 1 */
................................................................................
 52557  52989   #define BTS_PENDING          0x0040   /* Waiting for read-locks to clear */
 52558  52990   
 52559  52991   /*
 52560  52992   ** An instance of the following structure is used to hold information
 52561  52993   ** about a cell.  The parseCellPtr() function fills in this structure
 52562  52994   ** based on information extract from the raw disk page.
 52563  52995   */
 52564         -typedef struct CellInfo CellInfo;
 52565  52996   struct CellInfo {
 52566  52997     i64 nKey;      /* The key for INTKEY tables, or nPayload otherwise */
 52567  52998     u8 *pPayload;  /* Pointer to the start of payload */
 52568  52999     u32 nPayload;  /* Bytes of payload */
 52569  53000     u16 nLocal;    /* Amount of payload held locally, not on overflow */
 52570  53001     u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
 52571  53002     u16 nSize;     /* Size of the cell content on the main b-tree page */
................................................................................
 53557  53988   */
 53558  53989   static void invalidateIncrblobCursors(
 53559  53990     Btree *pBtree,          /* The database file to check */
 53560  53991     i64 iRow,               /* The rowid that might be changing */
 53561  53992     int isClearTable        /* True if all rows are being deleted */
 53562  53993   ){
 53563  53994     BtCursor *p;
 53564         -  BtShared *pBt = pBtree->pBt;
        53995  +  if( pBtree->hasIncrblobCur==0 ) return;
 53565  53996     assert( sqlite3BtreeHoldsMutex(pBtree) );
 53566         -  for(p=pBt->pCursor; p; p=p->pNext){
 53567         -    if( (p->curFlags & BTCF_Incrblob)!=0
 53568         -     && (isClearTable || p->info.nKey==iRow)
 53569         -    ){
 53570         -      p->eState = CURSOR_INVALID;
        53997  +  pBtree->hasIncrblobCur = 0;
        53998  +  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
        53999  +    if( (p->curFlags & BTCF_Incrblob)!=0 ){
        54000  +      pBtree->hasIncrblobCur = 1;
        54001  +      if( isClearTable || p->info.nKey==iRow ){
        54002  +        p->eState = CURSOR_INVALID;
        54003  +      }
 53571  54004       }
 53572  54005     }
 53573  54006   }
 53574  54007   
 53575  54008   #else
 53576  54009     /* Stub function when INCRBLOB is omitted */
 53577  54010     #define invalidateIncrblobCursors(x,y,z)
................................................................................
 54023  54456   ** the page, 1 means the second cell, and so forth) return a pointer
 54024  54457   ** to the cell content.
 54025  54458   **
 54026  54459   ** This routine works only for pages that do not contain overflow cells.
 54027  54460   */
 54028  54461   #define findCell(P,I) \
 54029  54462     ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
 54030         -#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
 54031         -
 54032         -
 54033         -/*
 54034         -** This a more complex version of findCell() that works for
 54035         -** pages that do contain overflow cells.
 54036         -*/
 54037         -static u8 *findOverflowCell(MemPage *pPage, int iCell){
 54038         -  int i;
 54039         -  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 54040         -  for(i=pPage->nOverflow-1; i>=0; i--){
 54041         -    int k;
 54042         -    k = pPage->aiOvfl[i];
 54043         -    if( k<=iCell ){
 54044         -      if( k==iCell ){
 54045         -        return pPage->apOvfl[i];
 54046         -      }
 54047         -      iCell--;
 54048         -    }
 54049         -  }
 54050         -  return findCell(pPage, iCell);
 54051         -}
 54052         -
 54053         -/*
 54054         -** Parse a cell content block and fill in the CellInfo structure.  There
 54055         -** are two versions of this function.  btreeParseCell() takes a 
 54056         -** cell index as the second argument and btreeParseCellPtr() 
 54057         -** takes a pointer to the body of the cell as its second argument.
 54058         -*/
        54463  +
        54464  +/*
        54465  +** This is common tail processing for btreeParseCellPtr() and
        54466  +** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
        54467  +** on a single B-tree page.  Make necessary adjustments to the CellInfo
        54468  +** structure.
        54469  +*/
        54470  +static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
        54471  +  MemPage *pPage,         /* Page containing the cell */
        54472  +  u8 *pCell,              /* Pointer to the cell text. */
        54473  +  CellInfo *pInfo         /* Fill in this structure */
        54474  +){
        54475  +  /* If the payload will not fit completely on the local page, we have
        54476  +  ** to decide how much to store locally and how much to spill onto
        54477  +  ** overflow pages.  The strategy is to minimize the amount of unused
        54478  +  ** space on overflow pages while keeping the amount of local storage
        54479  +  ** in between minLocal and maxLocal.
        54480  +  **
        54481  +  ** Warning:  changing the way overflow payload is distributed in any
        54482  +  ** way will result in an incompatible file format.
        54483  +  */
        54484  +  int minLocal;  /* Minimum amount of payload held locally */
        54485  +  int maxLocal;  /* Maximum amount of payload held locally */
        54486  +  int surplus;   /* Overflow payload available for local storage */
        54487  +
        54488  +  minLocal = pPage->minLocal;
        54489  +  maxLocal = pPage->maxLocal;
        54490  +  surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
        54491  +  testcase( surplus==maxLocal );
        54492  +  testcase( surplus==maxLocal+1 );
        54493  +  if( surplus <= maxLocal ){
        54494  +    pInfo->nLocal = (u16)surplus;
        54495  +  }else{
        54496  +    pInfo->nLocal = (u16)minLocal;
        54497  +  }
        54498  +  pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
        54499  +  pInfo->nSize = pInfo->iOverflow + 4;
        54500  +}
        54501  +
        54502  +/*
        54503  +** The following routines are implementations of the MemPage.xParseCell()
        54504  +** method.
        54505  +**
        54506  +** Parse a cell content block and fill in the CellInfo structure.
        54507  +**
        54508  +** btreeParseCellPtr()        =>   table btree leaf nodes
        54509  +** btreeParseCellNoPayload()  =>   table btree internal nodes
        54510  +** btreeParseCellPtrIndex()   =>   index btree nodes
        54511  +**
        54512  +** There is also a wrapper function btreeParseCell() that works for
        54513  +** all MemPage types and that references the cell by index rather than
        54514  +** by pointer.
        54515  +*/
        54516  +static void btreeParseCellPtrNoPayload(
        54517  +  MemPage *pPage,         /* Page containing the cell */
        54518  +  u8 *pCell,              /* Pointer to the cell text. */
        54519  +  CellInfo *pInfo         /* Fill in this structure */
        54520  +){
        54521  +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
        54522  +  assert( pPage->leaf==0 );
        54523  +  assert( pPage->noPayload );
        54524  +  assert( pPage->childPtrSize==4 );
        54525  +  pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
        54526  +  pInfo->nPayload = 0;
        54527  +  pInfo->nLocal = 0;
        54528  +  pInfo->iOverflow = 0;
        54529  +  pInfo->pPayload = 0;
        54530  +  return;
        54531  +}
 54059  54532   static void btreeParseCellPtr(
        54533  +  MemPage *pPage,         /* Page containing the cell */
        54534  +  u8 *pCell,              /* Pointer to the cell text. */
        54535  +  CellInfo *pInfo         /* Fill in this structure */
        54536  +){
        54537  +  u8 *pIter;              /* For scanning through pCell */
        54538  +  u32 nPayload;           /* Number of bytes of cell payload */
        54539  +  u64 iKey;               /* Extracted Key value */
        54540  +
        54541  +  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
        54542  +  assert( pPage->leaf==0 || pPage->leaf==1 );
        54543  +  assert( pPage->intKeyLeaf || pPage->noPayload );
        54544  +  assert( pPage->noPayload==0 );
        54545  +  assert( pPage->intKeyLeaf );
        54546  +  assert( pPage->childPtrSize==0 );
        54547  +  pIter = pCell;
        54548  +
        54549  +  /* The next block of code is equivalent to:
        54550  +  **
        54551  +  **     pIter += getVarint32(pIter, nPayload);
        54552  +  **
        54553  +  ** The code is inlined to avoid a function call.
        54554  +  */
        54555  +  nPayload = *pIter;
        54556  +  if( nPayload>=0x80 ){
        54557  +    u8 *pEnd = &pIter[8];
        54558  +    nPayload &= 0x7f;
        54559  +    do{
        54560  +      nPayload = (nPayload<<7) | (*++pIter & 0x7f);
        54561  +    }while( (*pIter)>=0x80 && pIter<pEnd );
        54562  +  }
        54563  +  pIter++;
        54564  +
        54565  +  /* The next block of code is equivalent to:
        54566  +  **
        54567  +  **     pIter += getVarint(pIter, (u64*)&pInfo->nKey);
        54568  +  **
        54569  +  ** The code is inlined to avoid a function call.
        54570  +  */
        54571  +  iKey = *pIter;
        54572  +  if( iKey>=0x80 ){
        54573  +    u8 *pEnd = &pIter[7];
        54574  +    iKey &= 0x7f;
        54575  +    while(1){
        54576  +      iKey = (iKey<<7) | (*++pIter & 0x7f);
        54577  +      if( (*pIter)<0x80 ) break;
        54578  +      if( pIter>=pEnd ){
        54579  +        iKey = (iKey<<8) | *++pIter;
        54580  +        break;
        54581  +      }
        54582  +    }
        54583  +  }
        54584  +  pIter++;
        54585  +
        54586  +  pInfo->nKey = *(i64*)&iKey;
        54587  +  pInfo->nPayload = nPayload;
        54588  +  pInfo->pPayload = pIter;
        54589  +  testcase( nPayload==pPage->maxLocal );
        54590  +  testcase( nPayload==pPage->maxLocal+1 );
        54591  +  if( nPayload<=pPage->maxLocal ){
        54592  +    /* This is the (easy) common case where the entire payload fits
        54593  +    ** on the local page.  No overflow is required.
        54594  +    */
        54595  +    pInfo->nSize = nPayload + (u16)(pIter - pCell);
        54596  +    if( pInfo->nSize<4 ) pInfo->nSize = 4;
        54597  +    pInfo->nLocal = (u16)nPayload;
        54598  +    pInfo->iOverflow = 0;
        54599  +  }else{
        54600  +    btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
        54601  +  }
        54602  +}
        54603  +static void btreeParseCellPtrIndex(
 54060  54604     MemPage *pPage,         /* Page containing the cell */
 54061  54605     u8 *pCell,              /* Pointer to the cell text. */
 54062  54606     CellInfo *pInfo         /* Fill in this structure */
 54063  54607   ){
 54064  54608     u8 *pIter;              /* For scanning through pCell */
 54065  54609     u32 nPayload;           /* Number of bytes of cell payload */
 54066  54610   
 54067  54611     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 54068  54612     assert( pPage->leaf==0 || pPage->leaf==1 );
 54069         -  if( pPage->intKeyLeaf ){
 54070         -    assert( pPage->childPtrSize==0 );
 54071         -    pIter = pCell + getVarint32(pCell, nPayload);
 54072         -    pIter += getVarint(pIter, (u64*)&pInfo->nKey);
 54073         -  }else if( pPage->noPayload ){
 54074         -    assert( pPage->childPtrSize==4 );
 54075         -    pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
 54076         -    pInfo->nPayload = 0;
 54077         -    pInfo->nLocal = 0;
 54078         -    pInfo->iOverflow = 0;
 54079         -    pInfo->pPayload = 0;
 54080         -    return;
 54081         -  }else{
 54082         -    pIter = pCell + pPage->childPtrSize;
 54083         -    pIter += getVarint32(pIter, nPayload);
 54084         -    pInfo->nKey = nPayload;
        54613  +  assert( pPage->intKeyLeaf==0 );
        54614  +  assert( pPage->noPayload==0 );
        54615  +  pIter = pCell + pPage->childPtrSize;
        54616  +  nPayload = *pIter;
        54617  +  if( nPayload>=0x80 ){
        54618  +    u8 *pEnd = &pIter[8];
        54619  +    nPayload &= 0x7f;
        54620  +    do{
        54621  +      nPayload = (nPayload<<7) | (*++pIter & 0x7f);
        54622  +    }while( *(pIter)>=0x80 && pIter<pEnd );
 54085  54623     }
        54624  +  pIter++;
        54625  +  pInfo->nKey = nPayload;
 54086  54626     pInfo->nPayload = nPayload;
 54087  54627     pInfo->pPayload = pIter;
 54088  54628     testcase( nPayload==pPage->maxLocal );
 54089  54629     testcase( nPayload==pPage->maxLocal+1 );
 54090  54630     if( nPayload<=pPage->maxLocal ){
 54091  54631       /* This is the (easy) common case where the entire payload fits
 54092  54632       ** on the local page.  No overflow is required.
 54093  54633       */
 54094  54634       pInfo->nSize = nPayload + (u16)(pIter - pCell);
 54095  54635       if( pInfo->nSize<4 ) pInfo->nSize = 4;
 54096  54636       pInfo->nLocal = (u16)nPayload;
 54097  54637       pInfo->iOverflow = 0;
 54098  54638     }else{
 54099         -    /* If the payload will not fit completely on the local page, we have
 54100         -    ** to decide how much to store locally and how much to spill onto
 54101         -    ** overflow pages.  The strategy is to minimize the amount of unused
 54102         -    ** space on overflow pages while keeping the amount of local storage
 54103         -    ** in between minLocal and maxLocal.
 54104         -    **
 54105         -    ** Warning:  changing the way overflow payload is distributed in any
 54106         -    ** way will result in an incompatible file format.
 54107         -    */
 54108         -    int minLocal;  /* Minimum amount of payload held locally */
 54109         -    int maxLocal;  /* Maximum amount of payload held locally */
 54110         -    int surplus;   /* Overflow payload available for local storage */
 54111         -
 54112         -    minLocal = pPage->minLocal;
 54113         -    maxLocal = pPage->maxLocal;
 54114         -    surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
 54115         -    testcase( surplus==maxLocal );
 54116         -    testcase( surplus==maxLocal+1 );
 54117         -    if( surplus <= maxLocal ){
 54118         -      pInfo->nLocal = (u16)surplus;
 54119         -    }else{
 54120         -      pInfo->nLocal = (u16)minLocal;
 54121         -    }
 54122         -    pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
 54123         -    pInfo->nSize = pInfo->iOverflow + 4;
        54639  +    btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
 54124  54640     }
 54125  54641   }
 54126  54642   static void btreeParseCell(
 54127  54643     MemPage *pPage,         /* Page containing the cell */
 54128  54644     int iCell,              /* The cell index.  First cell is 0 */
 54129  54645     CellInfo *pInfo         /* Fill in this structure */
 54130  54646   ){
 54131         -  btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo);
        54647  +  pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
 54132  54648   }
 54133  54649   
 54134  54650   /*
        54651  +** The following routines are implementations of the MemPage.xCellSize
        54652  +** method.
        54653  +**
 54135  54654   ** Compute the total number of bytes that a Cell needs in the cell
 54136  54655   ** data area of the btree-page.  The return number includes the cell
 54137  54656   ** data header and the local payload, but not any overflow page or
 54138  54657   ** the space used by the cell pointer.
        54658  +**
        54659  +** cellSizePtrNoPayload()    =>   table internal nodes
        54660  +** cellSizePtr()             =>   all index nodes & table leaf nodes
 54139  54661   */
 54140  54662   static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
 54141  54663     u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
 54142  54664     u8 *pEnd;                                /* End mark for a varint */
 54143  54665     u32 nSize;                               /* Size value to return */
 54144  54666   
 54145  54667   #ifdef SQLITE_DEBUG
 54146  54668     /* The value returned by this function should always be the same as
 54147  54669     ** the (CellInfo.nSize) value found by doing a full parse of the
 54148  54670     ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
 54149  54671     ** this function verifies that this invariant is not violated. */
 54150  54672     CellInfo debuginfo;
 54151         -  btreeParseCellPtr(pPage, pCell, &debuginfo);
        54673  +  pPage->xParseCell(pPage, pCell, &debuginfo);
 54152  54674   #endif
 54153  54675   
 54154         -  if( pPage->noPayload ){
 54155         -    pEnd = &pIter[9];
 54156         -    while( (*pIter++)&0x80 && pIter<pEnd );
 54157         -    assert( pPage->childPtrSize==4 );
 54158         -    return (u16)(pIter - pCell);
 54159         -  }
        54676  +  assert( pPage->noPayload==0 );
 54160  54677     nSize = *pIter;
 54161  54678     if( nSize>=0x80 ){
 54162         -    pEnd = &pIter[9];
        54679  +    pEnd = &pIter[8];
 54163  54680       nSize &= 0x7f;
 54164  54681       do{
 54165  54682         nSize = (nSize<<7) | (*++pIter & 0x7f);
 54166  54683       }while( *(pIter)>=0x80 && pIter<pEnd );
 54167  54684     }
 54168  54685     pIter++;
 54169  54686     if( pPage->intKey ){
................................................................................
 54187  54704         nSize = minLocal;
 54188  54705       }
 54189  54706       nSize += 4 + (u16)(pIter - pCell);
 54190  54707     }
 54191  54708     assert( nSize==debuginfo.nSize || CORRUPT_DB );
 54192  54709     return (u16)nSize;
 54193  54710   }
        54711  +static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
        54712  +  u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
        54713  +  u8 *pEnd;              /* End mark for a varint */
        54714  +
        54715  +#ifdef SQLITE_DEBUG
        54716  +  /* The value returned by this function should always be the same as
        54717  +  ** the (CellInfo.nSize) value found by doing a full parse of the
        54718  +  ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
        54719  +  ** this function verifies that this invariant is not violated. */
        54720  +  CellInfo debuginfo;
        54721  +  pPage->xParseCell(pPage, pCell, &debuginfo);
        54722  +#endif
        54723  +
        54724  +  assert( pPage->childPtrSize==4 );
        54725  +  pEnd = pIter + 9;
        54726  +  while( (*pIter++)&0x80 && pIter<pEnd );
        54727  +  assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
        54728  +  return (u16)(pIter - pCell);
        54729  +}
        54730  +
 54194  54731   
 54195  54732   #ifdef SQLITE_DEBUG
 54196  54733   /* This variation on cellSizePtr() is used inside of assert() statements
 54197  54734   ** only. */
 54198  54735   static u16 cellSize(MemPage *pPage, int iCell){
 54199         -  return cellSizePtr(pPage, findCell(pPage, iCell));
        54736  +  return pPage->xCellSize(pPage, findCell(pPage, iCell));
 54200  54737   }
 54201  54738   #endif
 54202  54739   
 54203  54740   #ifndef SQLITE_OMIT_AUTOVACUUM
 54204  54741   /*
 54205  54742   ** If the cell pCell, part of page pPage contains a pointer
 54206  54743   ** to an overflow page, insert an entry into the pointer-map
 54207  54744   ** for the overflow page.
 54208  54745   */
 54209  54746   static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
 54210  54747     CellInfo info;
 54211  54748     if( *pRC ) return;
 54212  54749     assert( pCell!=0 );
 54213         -  btreeParseCellPtr(pPage, pCell, &info);
        54750  +  pPage->xParseCell(pPage, pCell, &info);
 54214  54751     if( info.iOverflow ){
 54215  54752       Pgno ovfl = get4byte(&pCell[info.iOverflow]);
 54216  54753       ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
 54217  54754     }
 54218  54755   }
 54219  54756   #endif
 54220  54757   
................................................................................
 54270  54807       /* These conditions have already been verified in btreeInitPage()
 54271  54808       ** if PRAGMA cell_size_check=ON.
 54272  54809       */
 54273  54810       if( pc<iCellFirst || pc>iCellLast ){
 54274  54811         return SQLITE_CORRUPT_BKPT;
 54275  54812       }
 54276  54813       assert( pc>=iCellFirst && pc<=iCellLast );
 54277         -    size = cellSizePtr(pPage, &src[pc]);
        54814  +    size = pPage->xCellSize(pPage, &src[pc]);
 54278  54815       cbrk -= size;
 54279  54816       if( cbrk<iCellFirst || pc+size>usableSize ){
 54280  54817         return SQLITE_CORRUPT_BKPT;
 54281  54818       }
 54282  54819       assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
 54283  54820       testcase( cbrk+size==usableSize );
 54284  54821       testcase( pc+size==usableSize );
................................................................................
 54312  54849   ** from the free-list.
 54313  54850   **
 54314  54851   ** If no suitable space can be found on the free-list, return NULL.
 54315  54852   **
 54316  54853   ** This function may detect corruption within pPg.  If corruption is
 54317  54854   ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
 54318  54855   **
 54319         -** If a slot of at least nByte bytes is found but cannot be used because 
 54320         -** there are already at least 60 fragmented bytes on the page, return NULL.
 54321         -** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true.
        54856  +** Slots on the free list that are between 1 and 3 bytes larger than nByte
        54857  +** will be ignored if adding the extra space to the fragmentation count
        54858  +** causes the fragmentation count to exceed 60.
 54322  54859   */
 54323         -static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){
        54860  +static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
 54324  54861     const int hdr = pPg->hdrOffset;
 54325  54862     u8 * const aData = pPg->aData;
 54326         -  int iAddr;
 54327         -  int pc;
        54863  +  int iAddr = hdr + 1;
        54864  +  int pc = get2byte(&aData[iAddr]);
        54865  +  int x;
 54328  54866     int usableSize = pPg->pBt->usableSize;
 54329  54867   
 54330         -  for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){
        54868  +  assert( pc>0 );
        54869  +  do{
 54331  54870       int size;            /* Size of the free slot */
 54332  54871       /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
 54333  54872       ** increasing offset. */
 54334  54873       if( pc>usableSize-4 || pc<iAddr+4 ){
 54335  54874         *pRc = SQLITE_CORRUPT_BKPT;
 54336  54875         return 0;
 54337  54876       }
 54338  54877       /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
 54339  54878       ** freeblock form a big-endian integer which is the size of the freeblock
 54340  54879       ** in bytes, including the 4-byte header. */
 54341  54880       size = get2byte(&aData[pc+2]);
 54342         -    if( size>=nByte ){
 54343         -      int x = size - nByte;
        54881  +    if( (x = size - nByte)>=0 ){
 54344  54882         testcase( x==4 );
 54345  54883         testcase( x==3 );
 54346         -      if( x<4 ){
        54884  +      if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
        54885  +        *pRc = SQLITE_CORRUPT_BKPT;
        54886  +        return 0;
        54887  +      }else if( x<4 ){
 54347  54888           /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
 54348  54889           ** number of bytes in fragments may not exceed 60. */
 54349         -        if( aData[hdr+7]>=60 ){
 54350         -          if( pbDefrag ) *pbDefrag = 1;
 54351         -          return 0;
 54352         -        }
        54890  +        if( aData[hdr+7]>57 ) return 0;
        54891  +
 54353  54892           /* Remove the slot from the free-list. Update the number of
 54354  54893           ** fragmented bytes within the page. */
 54355  54894           memcpy(&aData[iAddr], &aData[pc], 2);
 54356  54895           aData[hdr+7] += (u8)x;
 54357         -      }else if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
 54358         -        *pRc = SQLITE_CORRUPT_BKPT;
 54359         -        return 0;
 54360  54896         }else{
 54361  54897           /* The slot remains on the free-list. Reduce its size to account
 54362  54898            ** for the portion used by the new allocation. */
 54363  54899           put2byte(&aData[pc+2], x);
 54364  54900         }
 54365  54901         return &aData[pc + x];
 54366  54902       }
 54367         -  }
        54903  +    iAddr = pc;
        54904  +    pc = get2byte(&aData[pc]);
        54905  +  }while( pc );
 54368  54906   
 54369  54907     return 0;
 54370  54908   }
 54371  54909   
 54372  54910   /*
 54373  54911   ** Allocate nByte bytes of space from within the B-Tree page passed
 54374  54912   ** as the first argument. Write into *pIdx the index into pPage->aData[]
................................................................................
 54401  54939     gap = pPage->cellOffset + 2*pPage->nCell;
 54402  54940     assert( gap<=65536 );
 54403  54941     /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
 54404  54942     ** and the reserved space is zero (the usual value for reserved space)
 54405  54943     ** then the cell content offset of an empty page wants to be 65536.
 54406  54944     ** However, that integer is too large to be stored in a 2-byte unsigned
 54407  54945     ** integer, so a value of 0 is used in its place. */
 54408         -  top = get2byteNotZero(&data[hdr+5]);
 54409         -  if( gap>top || NEVER((u32)top>pPage->pBt->usableSize) ){
 54410         -    /* The NEVER() is because a oversize "top" value will be blocked from
 54411         -    ** reaching this point by btreeInitPage() or btreeGetUnusedPage() */
 54412         -    return SQLITE_CORRUPT_BKPT;
        54946  +  top = get2byte(&data[hdr+5]);
        54947  +  assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
        54948  +  if( gap>top ){
        54949  +    if( top==0 && pPage->pBt->usableSize==65536 ){
        54950  +      top = 65536;
        54951  +    }else{
        54952  +      return SQLITE_CORRUPT_BKPT;
        54953  +    }
 54413  54954     }
 54414  54955   
 54415  54956     /* If there is enough space between gap and top for one more cell pointer
 54416  54957     ** array entry offset, and if the freelist is not empty, then search the
 54417  54958     ** freelist looking for a free slot big enough to satisfy the request.
 54418  54959     */
 54419  54960     testcase( gap+2==top );
 54420  54961     testcase( gap+1==top );
 54421  54962     testcase( gap==top );
 54422         -  if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){
 54423         -    int bDefrag = 0;
 54424         -    u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag);
 54425         -    if( rc ) return rc;
 54426         -    if( bDefrag ) goto defragment_page;
        54963  +  if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
        54964  +    u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
 54427  54965       if( pSpace ){
 54428  54966         assert( pSpace>=data && (pSpace - data)<65536 );
 54429  54967         *pIdx = (int)(pSpace - data);
 54430  54968         return SQLITE_OK;
        54969  +    }else if( rc ){
        54970  +      return rc;
 54431  54971       }
 54432  54972     }
 54433  54973   
 54434  54974     /* The request could not be fulfilled using a freelist slot.  Check
 54435  54975     ** to see if defragmentation is necessary.
 54436  54976     */
 54437  54977     testcase( gap+2+nByte==top );
 54438  54978     if( gap+2+nByte>top ){
 54439         - defragment_page:
 54440  54979       assert( pPage->nCell>0 || CORRUPT_DB );
 54441  54980       rc = defragmentPage(pPage);
 54442  54981       if( rc ) return rc;
 54443  54982       top = get2byteNotZero(&data[hdr+5]);
 54444  54983       assert( gap+nByte<=top );
 54445  54984     }
 54446  54985   
................................................................................
 54508  55047         iPtr = iFreeBlk;
 54509  55048       }
 54510  55049       if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
 54511  55050       assert( iFreeBlk>iPtr || iFreeBlk==0 );
 54512  55051     
 54513  55052       /* At this point:
 54514  55053       **    iFreeBlk:   First freeblock after iStart, or zero if none
 54515         -    **    iPtr:       The address of a pointer iFreeBlk
        55054  +    **    iPtr:       The address of a pointer to iFreeBlk
 54516  55055       **
 54517  55056       ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
 54518  55057       */
 54519  55058       if( iFreeBlk && iEnd+3>=iFreeBlk ){
 54520  55059         nFrag = iFreeBlk - iEnd;
 54521  55060         if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT;
 54522  55061         iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
        55062  +      if( iEnd > pPage->pBt->usableSize ) return SQLITE_CORRUPT_BKPT;
 54523  55063         iSize = iEnd - iStart;
 54524  55064         iFreeBlk = get2byte(&data[iFreeBlk]);
 54525  55065       }
 54526  55066     
 54527  55067       /* If iPtr is another freeblock (that is, if iPtr is not the freelist
 54528  55068       ** pointer in the page header) then check to see if iStart should be
 54529  55069       ** coalesced onto the end of iPtr.
................................................................................
 54573  55113     BtShared *pBt;     /* A copy of pPage->pBt */
 54574  55114   
 54575  55115     assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
 54576  55116     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 54577  55117     pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
 54578  55118     flagByte &= ~PTF_LEAF;
 54579  55119     pPage->childPtrSize = 4-4*pPage->leaf;
        55120  +  pPage->xCellSize = cellSizePtr;
 54580  55121     pBt = pPage->pBt;
 54581  55122     if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
 54582  55123       /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior
 54583  55124       ** table b-tree page. */
 54584  55125       assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
 54585  55126       /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf
 54586  55127       ** table b-tree page. */
 54587  55128       assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
 54588  55129       pPage->intKey = 1;
 54589         -    pPage->intKeyLeaf = pPage->leaf;
 54590         -    pPage->noPayload = !pPage->leaf;
        55130  +    if( pPage->leaf ){
        55131  +      pPage->intKeyLeaf = 1;
        55132  +      pPage->noPayload = 0;
        55133  +      pPage->xParseCell = btreeParseCellPtr;
        55134  +    }else{
        55135  +      pPage->intKeyLeaf = 0;
        55136  +      pPage->noPayload = 1;
        55137  +      pPage->xCellSize = cellSizePtrNoPayload;
        55138  +      pPage->xParseCell = btreeParseCellPtrNoPayload;
        55139  +    }
 54591  55140       pPage->maxLocal = pBt->maxLeaf;
 54592  55141       pPage->minLocal = pBt->minLeaf;
 54593  55142     }else if( flagByte==PTF_ZERODATA ){
 54594  55143       /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior
 54595  55144       ** index b-tree page. */
 54596  55145       assert( (PTF_ZERODATA)==2 );
 54597  55146       /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf
 54598  55147       ** index b-tree page. */
 54599  55148       assert( (PTF_ZERODATA|PTF_LEAF)==10 );
 54600  55149       pPage->intKey = 0;
 54601  55150       pPage->intKeyLeaf = 0;
 54602  55151       pPage->noPayload = 0;
        55152  +    pPage->xParseCell = btreeParseCellPtrIndex;
 54603  55153       pPage->maxLocal = pBt->maxLocal;
 54604  55154       pPage->minLocal = pBt->minLocal;
 54605  55155     }else{
 54606  55156       /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
 54607  55157       ** an error. */
 54608  55158       return SQLITE_CORRUPT_BKPT;
 54609  55159     }
................................................................................
 54690  55240         for(i=0; i<pPage->nCell; i++){
 54691  55241           pc = get2byte(&data[cellOffset+i*2]);
 54692  55242           testcase( pc==iCellFirst );
 54693  55243           testcase( pc==iCellLast );
 54694  55244           if( pc<iCellFirst || pc>iCellLast ){
 54695  55245             return SQLITE_CORRUPT_BKPT;
 54696  55246           }
 54697         -        sz = cellSizePtr(pPage, &data[pc]);
        55247  +        sz = pPage->xCellSize(pPage, &data[pc]);
 54698  55248           testcase( pc+sz==usableSize );
 54699  55249           if( pc+sz>usableSize ){
 54700  55250             return SQLITE_CORRUPT_BKPT;
 54701  55251           }
 54702  55252         }
 54703  55253         if( !pPage->leaf ) iCellLast++;
 54704  55254       }  
................................................................................
 56186  56736       if( rc ) return rc;
 56187  56737       nCell = pPage->nCell;
 56188  56738   
 56189  56739       for(i=0; i<nCell; i++){
 56190  56740         u8 *pCell = findCell(pPage, i);
 56191  56741         if( eType==PTRMAP_OVERFLOW1 ){
 56192  56742           CellInfo info;
 56193         -        btreeParseCellPtr(pPage, pCell, &info);
        56743  +        pPage->xParseCell(pPage, pCell, &info);
 56194  56744           if( info.iOverflow
 56195  56745            && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
 56196  56746            && iFrom==get4byte(&pCell[info.iOverflow])
 56197  56747           ){
 56198  56748             put4byte(&pCell[info.iOverflow], iTo);
 56199  56749             break;
 56200  56750           }
................................................................................
 57051  57601   /*
 57052  57602   ** Make sure the BtCursor* given in the argument has a valid
 57053  57603   ** BtCursor.info structure.  If it is not already valid, call
 57054  57604   ** btreeParseCell() to fill it in.
 57055  57605   **
 57056  57606   ** BtCursor.info is a cache of the information in the current cell.
 57057  57607   ** Using this cache reduces the number of calls to btreeParseCell().
 57058         -**
 57059         -** 2007-06-25:  There is a bug in some versions of MSVC that cause the
 57060         -** compiler to crash when getCellInfo() is implemented as a macro.
 57061         -** But there is a measureable speed advantage to using the macro on gcc
 57062         -** (when less compiler optimizations like -Os or -O0 are used and the
 57063         -** compiler is not doing aggressive inlining.)  So we use a real function
 57064         -** for MSVC and a macro for everything else.  Ticket #2457.
 57065  57608   */
 57066  57609   #ifndef NDEBUG
 57067  57610     static void assertCellInfo(BtCursor *pCur){
 57068  57611       CellInfo info;
 57069  57612       int iPage = pCur->iPage;
 57070  57613       memset(&info, 0, sizeof(info));
 57071  57614       btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
 57072  57615       assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
 57073  57616     }
 57074  57617   #else
 57075  57618     #define assertCellInfo(x)
 57076  57619   #endif
 57077         -#ifdef _MSC_VER
 57078         -  /* Use a real function in MSVC to work around bugs in that compiler. */
 57079         -  static void getCellInfo(BtCursor *pCur){
 57080         -    if( pCur->info.nSize==0 ){
 57081         -      int iPage = pCur->iPage;
 57082         -      btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
 57083         -      pCur->curFlags |= BTCF_ValidNKey;
 57084         -    }else{
 57085         -      assertCellInfo(pCur);
 57086         -    }
        57620  +static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
        57621  +  if( pCur->info.nSize==0 ){
        57622  +    int iPage = pCur->iPage;
        57623  +    pCur->curFlags |= BTCF_ValidNKey;
        57624  +    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
        57625  +  }else{
        57626  +    assertCellInfo(pCur);
 57087  57627     }
 57088         -#else /* if not _MSC_VER */
 57089         -  /* Use a macro in all other compilers so that the function is inlined */
 57090         -#define getCellInfo(pCur)                                                      \
 57091         -  if( pCur->info.nSize==0 ){                                                   \
 57092         -    int iPage = pCur->iPage;                                                   \
 57093         -    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);        \
 57094         -    pCur->curFlags |= BTCF_ValidNKey;                                          \
 57095         -  }else{                                                                       \
 57096         -    assertCellInfo(pCur);                                                      \
 57097         -  }
 57098         -#endif /* _MSC_VER */
        57628  +}
 57099  57629   
 57100  57630   #ifndef NDEBUG  /* The next routine used only within assert() statements */
 57101  57631   /*
 57102  57632   ** Return true if the given BtCursor is valid.  A valid cursor is one
 57103  57633   ** that is currently pointing to a row in a (non-empty) table.
 57104  57634   ** This is a verification routine is used only within assert() statements.
 57105  57635   */
................................................................................
 58051  58581             **
 58052  58582             ** If the record is corrupt, the xRecordCompare routine may read
 58053  58583             ** up to two varints past the end of the buffer. An extra 18 
 58054  58584             ** bytes of padding is allocated at the end of the buffer in
 58055  58585             ** case this happens.  */
 58056  58586             void *pCellKey;
 58057  58587             u8 * const pCellBody = pCell - pPage->childPtrSize;
 58058         -          btreeParseCellPtr(pPage, pCellBody, &pCur->info);
        58588  +          pPage->xParseCell(pPage, pCellBody, &pCur->info);
 58059  58589             nCell = (int)pCur->info.nKey;
 58060  58590             testcase( nCell<0 );   /* True if key size is 2^32 or more */
 58061  58591             testcase( nCell==0 );  /* Invalid key size:  0x80 0x80 0x00 */
 58062  58592             testcase( nCell==1 );  /* Invalid key size:  0x80 0x80 0x01 */
 58063  58593             testcase( nCell==2 );  /* Minimum legal index key size */
 58064  58594             if( nCell<2 ){
 58065  58595               rc = SQLITE_CORRUPT_BKPT;
................................................................................
 58397  58927     if( n>=mxPage ){
 58398  58928       return SQLITE_CORRUPT_BKPT;
 58399  58929     }
 58400  58930     if( n>0 ){
 58401  58931       /* There are pages on the freelist.  Reuse one of those pages. */
 58402  58932       Pgno iTrunk;
 58403  58933       u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
        58934  +    u32 nSearch = 0;   /* Count of the number of search attempts */
 58404  58935       
 58405  58936       /* If eMode==BTALLOC_EXACT and a query of the pointer-map
 58406  58937       ** shows that the page 'nearby' is somewhere on the free-list, then
 58407  58938       ** the entire-list will be searched for that page.
 58408  58939       */
 58409  58940   #ifndef SQLITE_OMIT_AUTOVACUUM
 58410  58941       if( eMode==BTALLOC_EXACT ){
................................................................................
 58445  58976         }else{
 58446  58977           /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
 58447  58978           ** stores the page number of the first page of the freelist, or zero if
 58448  58979           ** the freelist is empty. */
 58449  58980           iTrunk = get4byte(&pPage1->aData[32]);
 58450  58981         }
 58451  58982         testcase( iTrunk==mxPage );
 58452         -      if( iTrunk>mxPage ){
        58983  +      if( iTrunk>mxPage || nSearch++ > n ){
 58453  58984           rc = SQLITE_CORRUPT_BKPT;
 58454  58985         }else{
 58455  58986           rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
 58456  58987         }
 58457  58988         if( rc ){
 58458  58989           pTrunk = 0;
 58459  58990           goto end_allocate_page;
................................................................................
 58840  59371     CellInfo info;
 58841  59372     Pgno ovflPgno;
 58842  59373     int rc;
 58843  59374     int nOvfl;
 58844  59375     u32 ovflPageSize;
 58845  59376   
 58846  59377     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 58847         -  btreeParseCellPtr(pPage, pCell, &info);
        59378  +  pPage->xParseCell(pPage, pCell, &info);
 58848  59379     *pnSize = info.nSize;
 58849  59380     if( info.iOverflow==0 ){
 58850  59381       return SQLITE_OK;  /* No overflow pages. Return without doing anything */
 58851  59382     }
 58852  59383     if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
 58853  59384       return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
 58854  59385     }
................................................................................
 58994  59525     **
 58995  59526     ** Use a call to btreeParseCellPtr() to verify that the values above
 58996  59527     ** were computed correctly.
 58997  59528     */
 58998  59529   #if SQLITE_DEBUG
 58999  59530     {
 59000  59531       CellInfo info;
 59001         -    btreeParseCellPtr(pPage, pCell, &info);
        59532  +    pPage->xParseCell(pPage, pCell, &info);
 59002  59533       assert( nHeader=(int)(info.pPayload - pCell) );
 59003  59534       assert( info.nKey==nKey );
 59004  59535       assert( *pnSize == info.nSize );
 59005  59536       assert( spaceLeft == info.nLocal );
 59006  59537       assert( pPrior == &pCell[info.iOverflow] );
 59007  59538     }
 59008  59539   #endif
................................................................................
 59164  59695     int sz,           /* Bytes of content in pCell */
 59165  59696     u8 *pTemp,        /* Temp storage space for pCell, if needed */
 59166  59697     Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
 59167  59698     int *pRC          /* Read and write return code from here */
 59168  59699   ){
 59169  59700     int idx = 0;      /* Where to write new cell content in data[] */
 59170  59701     int j;            /* Loop counter */
 59171         -  int end;          /* First byte past the last cell pointer in data[] */
 59172         -  int ins;          /* Index in data[] where new cell pointer is inserted */
 59173         -  int cellOffset;   /* Address of first cell pointer in data[] */
 59174  59702     u8 *data;         /* The content of the whole page */
        59703  +  u8 *pIns;         /* The point in pPage->aCellIdx[] where no cell inserted */
 59175  59704   
 59176  59705     if( *pRC ) return;
 59177  59706   
 59178  59707     assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
 59179  59708     assert( MX_CELL(pPage->pBt)<=10921 );
 59180  59709     assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
 59181  59710     assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
................................................................................
 59182  59711     assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
 59183  59712     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 59184  59713     /* The cell should normally be sized correctly.  However, when moving a
 59185  59714     ** malformed cell from a leaf page to an interior page, if the cell size
 59186  59715     ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
 59187  59716     ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
 59188  59717     ** the term after the || in the following assert(). */
 59189         -  assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
        59718  +  assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
 59190  59719     if( pPage->nOverflow || sz+2>pPage->nFree ){
 59191  59720       if( pTemp ){
 59192  59721         memcpy(pTemp, pCell, sz);
 59193  59722         pCell = pTemp;
 59194  59723       }
 59195  59724       if( iChild ){
 59196  59725         put4byte(pCell, iChild);
 59197  59726       }
 59198  59727       j = pPage->nOverflow++;
 59199  59728       assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
 59200  59729       pPage->apOvfl[j] = pCell;
 59201  59730       pPage->aiOvfl[j] = (u16)i;
        59731  +
        59732  +    /* When multiple overflows occur, they are always sequential and in
        59733  +    ** sorted order.  This invariants arise because multiple overflows can
        59734  +    ** only occur when inserting divider cells into the parent page during
        59735  +    ** balancing, and the dividers are adjacent and sorted.
        59736  +    */
        59737  +    assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
        59738  +    assert( j==0 || i==pPage->aiOvfl[j-1]+1 );   /* Overflows are sequential */
 59202  59739     }else{
 59203  59740       int rc = sqlite3PagerWrite(pPage->pDbPage);
 59204  59741       if( rc!=SQLITE_OK ){
 59205  59742         *pRC = rc;
 59206  59743         return;
 59207  59744       }
 59208  59745       assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 59209  59746       data = pPage->aData;
 59210         -    cellOffset = pPage->cellOffset;
 59211         -    end = cellOffset + 2*pPage->nCell;
 59212         -    ins = cellOffset + 2*i;
        59747  +    assert( &data[pPage->cellOffset]==pPage->aCellIdx );
 59213  59748       rc = allocateSpace(pPage, sz, &idx);
 59214  59749       if( rc ){ *pRC = rc; return; }
 59215         -    /* The allocateSpace() routine guarantees the following two properties
 59216         -    ** if it returns success */
 59217         -    assert( idx >= end+2 );
        59750  +    /* The allocateSpace() routine guarantees the following properties
        59751  +    ** if it returns successfully */
        59752  +    assert( idx >= 0 );
        59753  +    assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
 59218  59754       assert( idx+sz <= (int)pPage->pBt->usableSize );
 59219         -    pPage->nCell++;
 59220  59755       pPage->nFree -= (u16)(2 + sz);
 59221  59756       memcpy(&data[idx], pCell, sz);
 59222  59757       if( iChild ){
 59223  59758         put4byte(&data[idx], iChild);
 59224  59759       }
 59225         -    memmove(&data[ins+2], &data[ins], end-ins);
 59226         -    put2byte(&data[ins], idx);
 59227         -    put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
        59760  +    pIns = pPage->aCellIdx + i*2;
        59761  +    memmove(pIns+2, pIns, 2*(pPage->nCell - i));
        59762  +    put2byte(pIns, idx);
        59763  +    pPage->nCell++;
        59764  +    /* increment the cell count */
        59765  +    if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
        59766  +    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
 59228  59767   #ifndef SQLITE_OMIT_AUTOVACUUM
 59229  59768       if( pPage->pBt->autoVacuum ){
 59230  59769         /* The cell may contain a pointer to an overflow page. If so, write
 59231  59770         ** the entry for the overflow page into the pointer map.
 59232  59771         */
 59233  59772         ptrmapPutOvflPtr(pPage, pCell, pRC);
 59234  59773       }
 59235  59774   #endif
 59236  59775     }
 59237  59776   }
        59777  +
        59778  +/*
        59779  +** A CellArray object contains a cache of pointers and sizes for a
        59780  +** consecutive sequence of cells that might be held multiple pages.
        59781  +*/
        59782  +typedef struct CellArray CellArray;
        59783  +struct CellArray {
        59784  +  int nCell;              /* Number of cells in apCell[] */
        59785  +  MemPage *pRef;          /* Reference page */
        59786  +  u8 **apCell;            /* All cells begin balanced */
        59787  +  u16 *szCell;            /* Local size of all cells in apCell[] */
        59788  +};
        59789  +
        59790  +/*
        59791  +** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
        59792  +** computed.
        59793  +*/
        59794  +static void populateCellCache(CellArray *p, int idx, int N){
        59795  +  assert( idx>=0 && idx+N<=p->nCell );
        59796  +  while( N>0 ){
        59797  +    assert( p->apCell[idx]!=0 );
        59798  +    if( p->szCell[idx]==0 ){
        59799  +      p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
        59800  +    }else{
        59801  +      assert( CORRUPT_DB ||
        59802  +              p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
        59803  +    }
        59804  +    idx++;
        59805  +    N--;
        59806  +  }
        59807  +}
        59808  +
        59809  +/*
        59810  +** Return the size of the Nth element of the cell array
        59811  +*/
        59812  +static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
        59813  +  assert( N>=0 && N<p->nCell );
        59814  +  assert( p->szCell[N]==0 );
        59815  +  p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
        59816  +  return p->szCell[N];
        59817  +}
        59818  +static u16 cachedCellSize(CellArray *p, int N){
        59819  +  assert( N>=0 && N<p->nCell );
        59820  +  if( p->szCell[N] ) return p->szCell[N];
        59821  +  return computeCellSize(p, N);
        59822  +}
 59238  59823   
 59239  59824   /*
 59240  59825   ** Array apCell[] contains pointers to nCell b-tree page cells. The 
 59241  59826   ** szCell[] array contains the size in bytes of each cell. This function
 59242  59827   ** replaces the current contents of page pPg with the contents of the cell
 59243  59828   ** array.
 59244  59829   **
................................................................................
 59245  59830   ** Some of the cells in apCell[] may currently be stored in pPg. This
 59246  59831   ** function works around problems caused by this by making a copy of any 
 59247  59832   ** such cells before overwriting the page data.
 59248  59833   **
 59249  59834   ** The MemPage.nFree field is invalidated by this function. It is the 
 59250  59835   ** responsibility of the caller to set it correctly.
 59251  59836   */
 59252         -static void rebuildPage(
        59837  +static int rebuildPage(
 59253  59838     MemPage *pPg,                   /* Edit this page */
 59254  59839     int nCell,                      /* Final number of cells on page */
 59255  59840     u8 **apCell,                    /* Array of cells */
 59256  59841     u16 *szCell                     /* Array of cell sizes */
 59257  59842   ){
 59258  59843     const int hdr = pPg->hdrOffset;          /* Offset of header on pPg */
 59259  59844     u8 * const aData = pPg->aData;           /* Pointer to data for pPg */
................................................................................
 59270  59855     pData = pEnd;
 59271  59856     for(i=0; i<nCell; i++){
 59272  59857       u8 *pCell = apCell[i];
 59273  59858       if( pCell>aData && pCell<pEnd ){
 59274  59859         pCell = &pTmp[pCell - aData];
 59275  59860       }
 59276  59861       pData -= szCell[i];
 59277         -    memcpy(pData, pCell, szCell[i]);
 59278  59862       put2byte(pCellptr, (pData - aData));
 59279  59863       pCellptr += 2;
 59280         -    assert( szCell[i]==cellSizePtr(pPg, pCell) || CORRUPT_DB );
 59281         -    testcase( szCell[i]==cellSizePtr(pPg,pCell) );
        59864  +    if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
        59865  +    memcpy(pData, pCell, szCell[i]);
        59866  +    assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
        59867  +    testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
 59282  59868     }
 59283  59869   
 59284  59870     /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
 59285  59871     pPg->nCell = nCell;
 59286  59872     pPg->nOverflow = 0;
 59287  59873   
 59288  59874     put2byte(&aData[hdr+1], 0);
 59289  59875     put2byte(&aData[hdr+3], pPg->nCell);
 59290  59876     put2byte(&aData[hdr+5], pData - aData);
 59291  59877     aData[hdr+7] = 0x00;
        59878  +  return SQLITE_OK;
 59292  59879   }
 59293  59880   
 59294  59881   /*
 59295  59882   ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
 59296  59883   ** contains the size in bytes of each such cell. This function attempts to 
 59297  59884   ** add the cells stored in the array to page pPg. If it cannot (because 
 59298  59885   ** the page needs to be defragmented before the cells will fit), non-zero
................................................................................
 59317  59904   ** cells in apCell[], then the cells do not fit and non-zero is returned.
 59318  59905   */
 59319  59906   static int pageInsertArray(
 59320  59907     MemPage *pPg,                   /* Page to add cells to */
 59321  59908     u8 *pBegin,                     /* End of cell-pointer array */
 59322  59909     u8 **ppData,                    /* IN/OUT: Page content -area pointer */
 59323  59910     u8 *pCellptr,                   /* Pointer to cell-pointer area */
        59911  +  int iFirst,                     /* Index of first cell to add */
 59324  59912     int nCell,                      /* Number of cells to add to pPg */
 59325         -  u8 **apCell,                    /* Array of cells */
 59326         -  u16 *szCell                     /* Array of cell sizes */
        59913  +  CellArray *pCArray              /* Array of cells */
 59327  59914   ){
 59328  59915     int i;
 59329  59916     u8 *aData = pPg->aData;
 59330  59917     u8 *pData = *ppData;
 59331         -  const int bFreelist = aData[1] || aData[2];
        59918  +  int iEnd = iFirst + nCell;
 59332  59919     assert( CORRUPT_DB || pPg->hdrOffset==0 );    /* Never called on page 1 */
 59333         -  for(i=0; i<nCell; i++){
 59334         -    int sz = szCell[i];
 59335         -    int rc;
        59920  +  for(i=iFirst; i<iEnd; i++){
        59921  +    int sz, rc;
 59336  59922       u8 *pSlot;
 59337         -    if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){
        59923  +    sz = cachedCellSize(pCArray, i);
        59924  +    if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
 59338  59925         pData -= sz;
 59339  59926         if( pData<pBegin ) return 1;
 59340  59927         pSlot = pData;
 59341  59928       }
 59342         -    memcpy(pSlot, apCell[i], sz);
        59929  +    memcpy(pSlot, pCArray->apCell[i], sz);
 59343  59930       put2byte(pCellptr, (pSlot - aData));
 59344  59931       pCellptr += 2;
 59345  59932     }
 59346  59933     *ppData = pData;
 59347  59934     return 0;
 59348  59935   }
 59349  59936   
................................................................................
 59354  59941   ** within the body of pPg to the pPg free-list. The cell-pointers and other
 59355  59942   ** fields of the page are not updated.
 59356  59943   **
 59357  59944   ** This function returns the total number of cells added to the free-list.
 59358  59945   */
 59359  59946   static int pageFreeArray(
 59360  59947     MemPage *pPg,                   /* Page to edit */
        59948  +  int iFirst,                     /* First cell to delete */
 59361  59949     int nCell,                      /* Cells to delete */
 59362         -  u8 **apCell,                    /* Array of cells */
 59363         -  u16 *szCell                     /* Array of cell sizes */
        59950  +  CellArray *pCArray              /* Array of cells */
 59364  59951   ){
 59365  59952     u8 * const aData = pPg->aData;
 59366  59953     u8 * const pEnd = &aData[pPg->pBt->usableSize];
 59367  59954     u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
 59368  59955     int nRet = 0;
 59369  59956     int i;
        59957  +  int iEnd = iFirst + nCell;
 59370  59958     u8 *pFree = 0;
 59371  59959     int szFree = 0;
 59372  59960   
 59373         -  for(i=0; i<nCell; i++){
 59374         -    u8 *pCell = apCell[i];
        59961  +  for(i=iFirst; i<iEnd; i++){
        59962  +    u8 *pCell = pCArray->apCell[i];
 59375  59963       if( pCell>=pStart && pCell<pEnd ){
 59376         -      int sz = szCell[i];
        59964  +      int sz;
        59965  +      /* No need to use cachedCellSize() here.  The sizes of all cells that
        59966  +      ** are to be freed have already been computing while deciding which
        59967  +      ** cells need freeing */
        59968  +      sz = pCArray->szCell[i];  assert( sz>0 );
 59377  59969         if( pFree!=(pCell + sz) ){
 59378  59970           if( pFree ){
 59379  59971             assert( pFree>aData && (pFree - aData)<65536 );
 59380  59972             freeSpace(pPg, (u16)(pFree - aData), szFree);
 59381  59973           }
 59382  59974           pFree = pCell;
 59383  59975           szFree = sz;
................................................................................
 59404  59996   **
 59405  59997   ** This routine makes the necessary adjustments to pPg so that it contains
 59406  59998   ** the correct cells after being balanced.
 59407  59999   **
 59408  60000   ** The pPg->nFree field is invalid when this function returns. It is the
 59409  60001   ** responsibility of the caller to set it correctly.
 59410  60002   */
 59411         -static void editPage(
        60003  +static int editPage(
 59412  60004     MemPage *pPg,                   /* Edit this page */
 59413  60005     int iOld,                       /* Index of first cell currently on page */
 59414  60006     int iNew,                       /* Index of new first cell on page */
 59415  60007     int nNew,                       /* Final number of cells on page */
 59416         -  u8 **apCell,                    /* Array of cells */
 59417         -  u16 *szCell                     /* Array of cell sizes */
        60008  +  CellArray *pCArray              /* Array of cells and sizes */
 59418  60009   ){
 59419  60010     u8 * const aData = pPg->aData;
 59420  60011     const int hdr = pPg->hdrOffset;
 59421  60012     u8 *pBegin = &pPg->aCellIdx[nNew * 2];
 59422  60013     int nCell = pPg->nCell;       /* Cells stored on pPg */
 59423  60014     u8 *pData;
 59424  60015     u8 *pCellptr;
................................................................................
 59429  60020   #ifdef SQLITE_DEBUG
 59430  60021     u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
 59431  60022     memcpy(pTmp, aData, pPg->pBt->usableSize);
 59432  60023   #endif
 59433  60024   
 59434  60025     /* Remove cells from the start and end of the page */
 59435  60026     if( iOld<iNew ){
 59436         -    int nShift = pageFreeArray(
 59437         -        pPg, iNew-iOld, &apCell[iOld], &szCell[iOld]
 59438         -    );
        60027  +    int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
 59439  60028       memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
 59440  60029       nCell -= nShift;
 59441  60030     }
 59442  60031     if( iNewEnd < iOldEnd ){
 59443         -    nCell -= pageFreeArray(
 59444         -        pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd]
 59445         -    );
        60032  +    nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
 59446  60033     }
 59447  60034   
 59448  60035     pData = &aData[get2byteNotZero(&aData[hdr+5])];
 59449  60036     if( pData<pBegin ) goto editpage_fail;
 59450  60037   
 59451  60038     /* Add cells to the start of the page */
 59452  60039     if( iNew<iOld ){
 59453  60040       int nAdd = MIN(nNew,iOld-iNew);
 59454  60041       assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
 59455  60042       pCellptr = pPg->aCellIdx;
 59456  60043       memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
 59457  60044       if( pageInsertArray(
 59458  60045             pPg, pBegin, &pData, pCellptr,
 59459         -          nAdd, &apCell[iNew], &szCell[iNew]
        60046  +          iNew, nAdd, pCArray
 59460  60047       ) ) goto editpage_fail;
 59461  60048       nCell += nAdd;
 59462  60049     }
 59463  60050   
 59464  60051     /* Add any overflow cells */
 59465  60052     for(i=0; i<pPg->nOverflow; i++){
 59466  60053       int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
 59467  60054       if( iCell>=0 && iCell<nNew ){
 59468  60055         pCellptr = &pPg->aCellIdx[iCell * 2];
 59469  60056         memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
 59470  60057         nCell++;
 59471  60058         if( pageInsertArray(
 59472  60059               pPg, pBegin, &pData, pCellptr,
 59473         -            1, &apCell[iCell + iNew], &szCell[iCell + iNew]
        60060  +            iCell+iNew, 1, pCArray
 59474  60061         ) ) goto editpage_fail;
 59475  60062       }
 59476  60063     }
 59477  60064   
 59478  60065     /* Append cells to the end of the page */
 59479  60066     pCellptr = &pPg->aCellIdx[nCell*2];
 59480  60067     if( pageInsertArray(
 59481  60068           pPg, pBegin, &pData, pCellptr,
 59482         -        nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell]
        60069  +        iNew+nCell, nNew-nCell, pCArray
 59483  60070     ) ) goto editpage_fail;
 59484  60071   
 59485  60072     pPg->nCell = nNew;
 59486  60073     pPg->nOverflow = 0;
 59487  60074   
 59488  60075     put2byte(&aData[hdr+3], pPg->nCell);
 59489  60076     put2byte(&aData[hdr+5], pData - aData);
 59490  60077   
 59491  60078   #ifdef SQLITE_DEBUG
 59492  60079     for(i=0; i<nNew && !CORRUPT_DB; i++){
 59493         -    u8 *pCell = apCell[i+iNew];
        60080  +    u8 *pCell = pCArray->apCell[i+iNew];
 59494  60081       int iOff = get2byte(&pPg->aCellIdx[i*2]);
 59495  60082       if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
 59496  60083         pCell = &pTmp[pCell - aData];
 59497  60084       }
 59498         -    assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) );
        60085  +    assert( 0==memcmp(pCell, &aData[iOff],
        60086  +            pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
 59499  60087     }
 59500  60088   #endif
 59501  60089   
 59502         -  return;
        60090  +  return SQLITE_OK;
 59503  60091    editpage_fail:
 59504  60092     /* Unable to edit this page. Rebuild it from scratch instead. */
 59505         -  rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]);
        60093  +  populateCellCache(pCArray, iNew, nNew);
        60094  +  return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
 59506  60095   }
 59507  60096   
 59508  60097   /*
 59509  60098   ** The following parameters determine how many adjacent pages get involved
 59510  60099   ** in a balancing operation.  NN is the number of neighbors on either side
 59511  60100   ** of the page that participate in the balancing operation.  NB is the
 59512  60101   ** total number of pages that participate, including the target page and
................................................................................
 59564  60153     */
 59565  60154     rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
 59566  60155   
 59567  60156     if( rc==SQLITE_OK ){
 59568  60157   
 59569  60158       u8 *pOut = &pSpace[4];
 59570  60159       u8 *pCell = pPage->apOvfl[0];
 59571         -    u16 szCell = cellSizePtr(pPage, pCell);
        60160  +    u16 szCell = pPage->xCellSize(pPage, pCell);
 59572  60161       u8 *pStop;
 59573  60162   
 59574  60163       assert( sqlite3PagerIswriteable(pNew->pDbPage) );
 59575  60164       assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
 59576  60165       zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
 59577         -    rebuildPage(pNew, 1, &pCell, &szCell);
        60166  +    rc = rebuildPage(pNew, 1, &pCell, &szCell);
        60167  +    if( NEVER(rc) ) return rc;
 59578  60168       pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
 59579  60169   
 59580  60170       /* If this is an auto-vacuum database, update the pointer map
 59581  60171       ** with entries for the new page, and any pointer from the 
 59582  60172       ** cell on the page to an overflow page. If either of these
 59583  60173       ** operations fails, the return code is set, but the contents
 59584  60174       ** of the parent page are still manipulated by thh code below.
................................................................................
 59643  60233       assert( pPage->isInit );
 59644  60234   
 59645  60235       for(j=0; j<pPage->nCell; j++){
 59646  60236         CellInfo info;
 59647  60237         u8 *z;
 59648  60238        
 59649  60239         z = findCell(pPage, j);
 59650         -      btreeParseCellPtr(pPage, z, &info);
        60240  +      pPage->xParseCell(pPage, z, &info);
 59651  60241         if( info.iOverflow ){
 59652  60242           Pgno ovfl = get4byte(&z[info.iOverflow]);
 59653  60243           ptrmapGet(pBt, ovfl, &e, &n);
 59654  60244           assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
 59655  60245         }
 59656  60246         if( !pPage->leaf ){
 59657  60247           Pgno child = get4byte(z);
................................................................................
 59774  60364     MemPage *pParent,               /* Parent page of siblings being balanced */
 59775  60365     int iParentIdx,                 /* Index of "the page" in pParent */
 59776  60366     u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
 59777  60367     int isRoot,                     /* True if pParent is a root-page */
 59778  60368     int bBulk                       /* True if this call is part of a bulk load */
 59779  60369   ){
 59780  60370     BtShared *pBt;               /* The whole database */
 59781         -  int nCell = 0;               /* Number of cells in apCell[] */
 59782  60371     int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
 59783  60372     int nNew = 0;                /* Number of pages in apNew[] */
 59784  60373     int nOld;                    /* Number of pages in apOld[] */
 59785  60374     int i, j, k;                 /* Loop counters */
 59786  60375     int nxDiv;                   /* Next divider slot in pParent->aCell[] */
 59787  60376     int rc = SQLITE_OK;          /* The return code */
 59788  60377     u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
 59789  60378     int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
 59790  60379     int usableSpace;             /* Bytes in pPage beyond the header */
 59791  60380     int pageFlags;               /* Value of pPage->aData[0] */
 59792         -  int subtotal;                /* Subtotal of bytes in cells on one page */
 59793  60381     int iSpace1 = 0;             /* First unused byte of aSpace1[] */
 59794  60382     int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
 59795  60383     int szScratch;               /* Size of scratch memory requested */
 59796  60384     MemPage *apOld[NB];          /* pPage and up to two siblings */
 59797  60385     MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
 59798  60386     u8 *pRight;                  /* Location in parent of right-sibling pointer */
 59799  60387     u8 *apDiv[NB-1];             /* Divider cells in pParent */
 59800         -  int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
 59801         -  int cntOld[NB+2];            /* Old index in aCell[] after i-th page */
        60388  +  int cntNew[NB+2];            /* Index in b.paCell[] of cell after i-th page */
        60389  +  int cntOld[NB+2];            /* Old index in b.apCell[] */
 59802  60390     int szNew[NB+2];             /* Combined size of cells placed on i-th page */
 59803         -  u8 **apCell = 0;             /* All cells begin balanced */
 59804         -  u16 *szCell;                 /* Local size of all cells in apCell[] */
 59805  60391     u8 *aSpace1;                 /* Space for copies of dividers cells */
 59806  60392     Pgno pgno;                   /* Temp var to store a page number in */
 59807  60393     u8 abDone[NB+2];             /* True after i'th new page is populated */
 59808  60394     Pgno aPgno[NB+2];            /* Page numbers of new pages before shuffling */
 59809  60395     Pgno aPgOrder[NB+2];         /* Copy of aPgno[] used for sorting pages */
 59810  60396     u16 aPgFlags[NB+2];          /* flags field of new pages before shuffling */
        60397  +  CellArray b;                  /* Parsed information on cells being balanced */
 59811  60398   
 59812  60399     memset(abDone, 0, sizeof(abDone));
        60400  +  b.nCell = 0;
        60401  +  b.apCell = 0;
 59813  60402     pBt = pParent->pBt;
 59814  60403     assert( sqlite3_mutex_held(pBt->mutex) );
 59815  60404     assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 59816  60405   
 59817  60406   #if 0
 59818  60407     TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
 59819  60408   #endif
................................................................................
 59870  60459       }
 59871  60460       nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
 59872  60461       if( (i--)==0 ) break;
 59873  60462   
 59874  60463       if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
 59875  60464         apDiv[i] = pParent->apOvfl[0];
 59876  60465         pgno = get4byte(apDiv[i]);
 59877         -      szNew[i] = cellSizePtr(pParent, apDiv[i]);
        60466  +      szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
 59878  60467         pParent->nOverflow = 0;
 59879  60468       }else{
 59880  60469         apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
 59881  60470         pgno = get4byte(apDiv[i]);
 59882         -      szNew[i] = cellSizePtr(pParent, apDiv[i]);
        60471  +      szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
 59883  60472   
 59884  60473         /* Drop the cell from the parent page. apDiv[i] still points to
 59885  60474         ** the cell within the parent, even though it has been dropped.
 59886  60475         ** This is safe because dropping a cell only overwrites the first
 59887  60476         ** four bytes of it, and this function does not need the first
 59888  60477         ** four bytes of the divider cell. So the pointer is safe to use
 59889  60478         ** later on.  
................................................................................
 59914  60503     ** alignment */
 59915  60504     nMaxCells = (nMaxCells + 3)&~3;
 59916  60505   
 59917  60506     /*
 59918  60507     ** Allocate space for memory structures
 59919  60508     */
 59920  60509     szScratch =
 59921         -       nMaxCells*sizeof(u8*)                       /* apCell */
 59922         -     + nMaxCells*sizeof(u16)                       /* szCell */
        60510  +       nMaxCells*sizeof(u8*)                       /* b.apCell */
        60511  +     + nMaxCells*sizeof(u16)                       /* b.szCell */
 59923  60512        + pBt->pageSize;                              /* aSpace1 */
 59924  60513   
 59925  60514     /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
 59926  60515     ** that is more than 6 times the database page size. */
 59927  60516     assert( szScratch<=6*(int)pBt->pageSize );
 59928         -  apCell = sqlite3ScratchMalloc( szScratch ); 
 59929         -  if( apCell==0 ){
        60517  +  b.apCell = sqlite3ScratchMalloc( szScratch ); 
        60518  +  if( b.apCell==0 ){
 59930  60519       rc = SQLITE_NOMEM;
 59931  60520       goto balance_cleanup;
 59932  60521     }
 59933         -  szCell = (u16*)&apCell[nMaxCells];
 59934         -  aSpace1 = (u8*)&szCell[nMaxCells];
        60522  +  b.szCell = (u16*)&b.apCell[nMaxCells];
        60523  +  aSpace1 = (u8*)&b.szCell[nMaxCells];
 59935  60524     assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
 59936  60525   
 59937  60526     /*
 59938  60527     ** Load pointers to all cells on sibling pages and the divider cells
 59939         -  ** into the local apCell[] array.  Make copies of the divider cells
        60528  +  ** into the local b.apCell[] array.  Make copies of the divider cells
 59940  60529     ** into space obtained from aSpace1[]. The divider cells have already
 59941  60530     ** been removed from pParent.
 59942  60531     **
 59943  60532     ** If the siblings are on leaf pages, then the child pointers of the
 59944  60533     ** divider cells are stripped from the cells before they are copied
 59945         -  ** into aSpace1[].  In this way, all cells in apCell[] are without
        60534  +  ** into aSpace1[].  In this way, all cells in b.apCell[] are without
 59946  60535     ** child pointers.  If siblings are not leaves, then all cell in
 59947         -  ** apCell[] include child pointers.  Either way, all cells in apCell[]
        60536  +  ** b.apCell[] include child pointers.  Either way, all cells in b.apCell[]
 59948  60537     ** are alike.
 59949  60538     **
 59950  60539     ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
 59951  60540     **       leafData:  1 if pPage holds key+data and pParent holds only keys.
 59952  60541     */
 59953         -  leafCorrection = apOld[0]->leaf*4;
 59954         -  leafData = apOld[0]->intKeyLeaf;
        60542  +  b.pRef = apOld[0];
        60543  +  leafCorrection = b.pRef->leaf*4;
        60544  +  leafData = b.pRef->intKeyLeaf;
 59955  60545     for(i=0; i<nOld; i++){
 59956         -    int limit;
 59957  60546       MemPage *pOld = apOld[i];
        60547  +    int limit = pOld->nCell;
        60548  +    u8 *aData = pOld->aData;
        60549  +    u16 maskPage = pOld->maskPage;
        60550  +    u8 *piCell = aData + pOld->cellOffset;
        60551  +    u8 *piEnd;
 59958  60552   
 59959  60553       /* Verify that all sibling pages are of the same "type" (table-leaf,
 59960  60554       ** table-interior, index-leaf, or index-interior).
 59961  60555       */
 59962  60556       if( pOld->aData[0]!=apOld[0]->aData[0] ){
 59963  60557         rc = SQLITE_CORRUPT_BKPT;
 59964  60558         goto balance_cleanup;
 59965  60559       }
 59966  60560   
 59967         -    limit = pOld->nCell+pOld->nOverflow;
        60561  +    /* Load b.apCell[] with pointers to all cells in pOld.  If pOld
        60562  +    ** constains overflow cells, include them in the b.apCell[] array
        60563  +    ** in the correct spot.
        60564  +    **
        60565  +    ** Note that when there are multiple overflow cells, it is always the
        60566  +    ** case that they are sequential and adjacent.  This invariant arises
        60567  +    ** because multiple overflows can only occurs when inserting divider
        60568  +    ** cells into a parent on a prior balance, and divider cells are always
        60569  +    ** adjacent and are inserted in order.  There is an assert() tagged
        60570  +    ** with "NOTE 1" in the overflow cell insertion loop to prove this
        60571  +    ** invariant.
        60572  +    **
        60573  +    ** This must be done in advance.  Once the balance starts, the cell
        60574  +    ** offset section of the btree page will be overwritten and we will no
        60575  +    ** long be able to find the cells if a pointer to each cell is not saved
        60576  +    ** first.
        60577  +    */
        60578  +    memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*limit);
 59968  60579       if( pOld->nOverflow>0 ){
 59969         -      for(j=0; j<limit; j++){
 59970         -        assert( nCell<nMaxCells );
 59971         -        apCell[nCell] = findOverflowCell(pOld, j);
 59972         -        szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
 59973         -        nCell++;
 59974         -      }
 59975         -    }else{
 59976         -      u8 *aData = pOld->aData;
 59977         -      u16 maskPage = pOld->maskPage;
 59978         -      u16 cellOffset = pOld->cellOffset;
        60580  +      memset(&b.szCell[b.nCell+limit], 0, sizeof(b.szCell[0])*pOld->nOverflow);
        60581  +      limit = pOld->aiOvfl[0];
 59979  60582         for(j=0; j<limit; j++){
 59980         -        assert( nCell<nMaxCells );
 59981         -        apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
 59982         -        szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
 59983         -        nCell++;
        60583  +        b.apCell[b.nCell] = aData + (maskPage & get2byte(piCell));
        60584  +        piCell += 2;
        60585  +        b.nCell++;
 59984  60586         }
 59985         -    }       
 59986         -    cntOld[i] = nCell;
        60587  +      for(k=0; k<pOld->nOverflow; k++){
        60588  +        assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
        60589  +        b.apCell[b.nCell] = pOld->apOvfl[k];
        60590  +        b.nCell++;
        60591  +      }
        60592  +    }
        60593  +    piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
        60594  +    while( piCell<piEnd ){
        60595  +      assert( b.nCell<nMaxCells );
        60596  +      b.apCell[b.nCell] = aData + (maskPage & get2byte(piCell));
        60597  +      piCell += 2;
        60598  +      b.nCell++;
        60599  +    }
        60600  +
        60601  +    cntOld[i] = b.nCell;
 59987  60602       if( i<nOld-1 && !leafData){
 59988  60603         u16 sz = (u16)szNew[i];
 59989  60604         u8 *pTemp;
 59990         -      assert( nCell<nMaxCells );
 59991         -      szCell[nCell] = sz;
        60605  +      assert( b.nCell<nMaxCells );
        60606  +      b.szCell[b.nCell] = sz;
 59992  60607         pTemp = &aSpace1[iSpace1];
 59993  60608         iSpace1 += sz;
 59994  60609         assert( sz<=pBt->maxLocal+23 );
 59995  60610         assert( iSpace1 <= (int)pBt->pageSize );
 59996  60611         memcpy(pTemp, apDiv[i], sz);
 59997         -      apCell[nCell] = pTemp+leafCorrection;
        60612  +      b.apCell[b.nCell] = pTemp+leafCorrection;
 59998  60613         assert( leafCorrection==0 || leafCorrection==4 );
 59999         -      szCell[nCell] = szCell[nCell] - leafCorrection;
        60614  +      b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
 60000  60615         if( !pOld->leaf ){
 60001  60616           assert( leafCorrection==0 );
 60002  60617           assert( pOld->hdrOffset==0 );
 60003  60618           /* The right pointer of the child page pOld becomes the left
 60004  60619           ** pointer of the divider cell */
 60005         -        memcpy(apCell[nCell], &pOld->aData[8], 4);
        60620  +        memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
 60006  60621         }else{
 60007  60622           assert( leafCorrection==4 );
 60008         -        while( szCell[nCell]<4 ){
        60623  +        while( b.szCell[b.nCell]<4 ){
 60009  60624             /* Do not allow any cells smaller than 4 bytes. If a smaller cell
 60010  60625             ** does exist, pad it with 0x00 bytes. */
 60011         -          assert( szCell[nCell]==3 || CORRUPT_DB );
 60012         -          assert( apCell[nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
        60626  +          assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
        60627  +          assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
 60013  60628             aSpace1[iSpace1++] = 0x00;
 60014         -          szCell[nCell]++;
        60629  +          b.szCell[b.nCell]++;
 60015  60630           }
 60016  60631         }
 60017         -      nCell++;
        60632  +      b.nCell++;
 60018  60633       }
 60019  60634     }
 60020  60635   
 60021  60636     /*
 60022         -  ** Figure out the number of pages needed to hold all nCell cells.
        60637  +  ** Figure out the number of pages needed to hold all b.nCell cells.
 60023  60638     ** Store this number in "k".  Also compute szNew[] which is the total
 60024  60639     ** size of all cells on the i-th page and cntNew[] which is the index
 60025         -  ** in apCell[] of the cell that divides page i from page i+1.  
 60026         -  ** cntNew[k] should equal nCell.
        60640  +  ** in b.apCell[] of the cell that divides page i from page i+1.  
        60641  +  ** cntNew[k] should equal b.nCell.
 60027  60642     **
 60028  60643     ** Values computed by this block:
 60029  60644     **
 60030  60645     **           k: The total number of sibling pages
 60031  60646     **    szNew[i]: Spaced used on the i-th sibling page.
 60032         -  **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
        60647  +  **   cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
 60033  60648     **              the right of the i-th sibling page.
 60034  60649     ** usableSpace: Number of bytes of space available on each sibling.
 60035  60650     ** 
 60036  60651     */
 60037  60652     usableSpace = pBt->usableSize - 12 + leafCorrection;
 60038         -  for(subtotal=k=i=0; i<nCell; i++){
 60039         -    assert( i<nMaxCells );
 60040         -    subtotal += szCell[i] + 2;
 60041         -    if( subtotal > usableSpace ){
 60042         -      szNew[k] = subtotal - szCell[i] - 2;
 60043         -      cntNew[k] = i;
 60044         -      if( leafData ){ i--; }
 60045         -      subtotal = 0;
 60046         -      k++;
 60047         -      if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
 60048         -    }
 60049         -  }
 60050         -  szNew[k] = subtotal;
 60051         -  cntNew[k] = nCell;
 60052         -  k++;
        60653  +  for(i=0; i<nOld; i++){
        60654  +    MemPage *p = apOld[i];
        60655  +    szNew[i] = usableSpace - p->nFree;
        60656  +    if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
        60657  +    for(j=0; j<p->nOverflow; j++){
        60658  +      szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
        60659  +    }
        60660  +    cntNew[i] = cntOld[i];
        60661  +  }
        60662  +  k = nOld;
        60663  +  for(i=0; i<k; i++){
        60664  +    int sz;
        60665  +    while( szNew[i]>usableSpace ){
        60666  +      if( i+1>=k ){
        60667  +        k = i+2;
        60668  +        if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
        60669  +        szNew[k-1] = 0;
        60670  +        cntNew[k-1] = b.nCell;
        60671  +      }
        60672  +      sz = 2 + cachedCellSize(&b, cntNew[i]-1);
        60673  +      szNew[i] -= sz;
        60674  +      if( !leafData ){
        60675  +        if( cntNew[i]<b.nCell ){
        60676  +          sz = 2 + cachedCellSize(&b, cntNew[i]);
        60677  +        }else{
        60678  +          sz = 0;
        60679  +        }
        60680  +      }
        60681  +      szNew[i+1] += sz;
        60682  +      cntNew[i]--;
        60683  +    }
        60684  +    while( cntNew[i]<b.nCell ){
        60685  +      sz = 2 + cachedCellSize(&b, cntNew[i]);
        60686  +      if( szNew[i]+sz>usableSpace ) break;
        60687  +      szNew[i] += sz;
        60688  +      cntNew[i]++;
        60689  +      if( !leafData ){
        60690  +        if( cntNew[i]<b.nCell ){
        60691  +          sz = 2 + cachedCellSize(&b, cntNew[i]);
        60692  +        }else{
        60693  +          sz = 0;
        60694  +        }
        60695  +      }
        60696  +      szNew[i+1] -= sz;
        60697  +    }
        60698  +    if( cntNew[i]>=b.nCell ){
        60699  +      k = i+1;
        60700  +    }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
        60701  +      rc = SQLITE_CORRUPT_BKPT;
        60702  +      goto balance_cleanup;
        60703  +    }
        60704  +  }
 60053  60705   
 60054  60706     /*
 60055  60707     ** The packing computed by the previous block is biased toward the siblings
 60056  60708     ** on the left side (siblings with smaller keys). The left siblings are
 60057  60709     ** always nearly full, while the right-most sibling might be nearly empty.
 60058  60710     ** The next block of code attempts to adjust the packing of siblings to
 60059  60711     ** get a better balance.
................................................................................
 60066  60718       int szRight = szNew[i];  /* Size of sibling on the right */
 60067  60719       int szLeft = szNew[i-1]; /* Size of sibling on the left */
 60068  60720       int r;              /* Index of right-most cell in left sibling */
 60069  60721       int d;              /* Index of first cell to the left of right sibling */
 60070  60722   
 60071  60723       r = cntNew[i-1] - 1;
 60072  60724       d = r + 1 - leafData;
 60073         -    assert( d<nMaxCells );
 60074         -    assert( r<nMaxCells );
 60075         -    while( szRight==0 
 60076         -       || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) 
 60077         -    ){
 60078         -      szRight += szCell[d] + 2;
 60079         -      szLeft -= szCell[r] + 2;
 60080         -      cntNew[i-1]--;
 60081         -      r = cntNew[i-1] - 1;
 60082         -      d = r + 1 - leafData;
 60083         -    }
        60725  +    (void)cachedCellSize(&b, d);
        60726  +    do{
        60727  +      assert( d<nMaxCells );
        60728  +      assert( r<nMaxCells );
        60729  +      (void)cachedCellSize(&b, r);
        60730  +      if( szRight!=0
        60731  +       && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+2)) ){
        60732  +        break;
        60733  +      }
        60734  +      szRight += b.szCell[d] + 2;
        60735  +      szLeft -= b.szCell[r] + 2;
        60736  +      cntNew[i-1] = r;
        60737  +      r--;
        60738  +      d--;
        60739  +    }while( r>=0 );
 60084  60740       szNew[i] = szRight;
 60085  60741       szNew[i-1] = szLeft;
        60742  +    if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
        60743  +      rc = SQLITE_CORRUPT_BKPT;
        60744  +      goto balance_cleanup;
        60745  +    }
 60086  60746     }
 60087  60747   
 60088  60748     /* Sanity check:  For a non-corrupt database file one of the follwing
 60089  60749     ** must be true:
 60090  60750     **    (1) We found one or more cells (cntNew[0])>0), or
 60091  60751     **    (2) pPage is a virtual root page.  A virtual root page is when
 60092  60752     **        the real root page is page 1 and we are the only child of
................................................................................
 60114  60774       }else{
 60115  60775         assert( i>0 );
 60116  60776         rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
 60117  60777         if( rc ) goto balance_cleanup;
 60118  60778         zeroPage(pNew, pageFlags);
 60119  60779         apNew[i] = pNew;
 60120  60780         nNew++;
 60121         -      cntOld[i] = nCell;
        60781  +      cntOld[i] = b.nCell;
 60122  60782   
 60123  60783         /* Set the pointer-map entry for the new sibling page. */
 60124  60784         if( ISAUTOVACUUM ){
 60125  60785           ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
 60126  60786           if( rc!=SQLITE_OK ){
 60127  60787             goto balance_cleanup;
 60128  60788           }
................................................................................
 60219  60879       MemPage *pNew = apNew[0];
 60220  60880       u8 *aOld = pNew->aData;
 60221  60881       int cntOldNext = pNew->nCell + pNew->nOverflow;
 60222  60882       int usableSize = pBt->usableSize;
 60223  60883       int iNew = 0;
 60224  60884       int iOld = 0;
 60225  60885   
 60226         -    for(i=0; i<nCell; i++){
 60227         -      u8 *pCell = apCell[i];
        60886  +    for(i=0; i<b.nCell; i++){
        60887  +      u8 *pCell = b.apCell[i];
 60228  60888         if( i==cntOldNext ){
 60229  60889           MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
 60230  60890           cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
 60231  60891           aOld = pOld->aData;
 60232  60892         }
 60233  60893         if( i==cntNew[iNew] ){
 60234  60894           pNew = apNew[++iNew];
................................................................................
 60245  60905          || pNew->pgno!=aPgno[iOld]
 60246  60906          || pCell<aOld
 60247  60907          || pCell>=&aOld[usableSize]
 60248  60908         ){
 60249  60909           if( !leafCorrection ){
 60250  60910             ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
 60251  60911           }
 60252         -        if( szCell[i]>pNew->minLocal ){
        60912  +        if( cachedCellSize(&b,i)>pNew->minLocal ){
 60253  60913             ptrmapPutOvflPtr(pNew, pCell, &rc);
 60254  60914           }
        60915  +        if( rc ) goto balance_cleanup;
 60255  60916         }
 60256  60917       }
 60257  60918     }
 60258  60919   
 60259  60920     /* Insert new divider cells into pParent. */
 60260  60921     for(i=0; i<nNew-1; i++){
 60261  60922       u8 *pCell;
 60262  60923       u8 *pTemp;
 60263  60924       int sz;
 60264  60925       MemPage *pNew = apNew[i];
 60265  60926       j = cntNew[i];
 60266  60927   
 60267  60928       assert( j<nMaxCells );
 60268         -    pCell = apCell[j];
 60269         -    sz = szCell[j] + leafCorrection;
        60929  +    assert( b.apCell[j]!=0 );
        60930  +    pCell = b.apCell[j];
        60931  +    sz = b.szCell[j] + leafCorrection;
 60270  60932       pTemp = &aOvflSpace[iOvflSpace];
 60271  60933       if( !pNew->leaf ){
 60272  60934         memcpy(&pNew->aData[8], pCell, 4);
 60273  60935       }else if( leafData ){
 60274  60936         /* If the tree is a leaf-data tree, and the siblings are leaves, 
 60275         -      ** then there is no divider cell in apCell[]. Instead, the divider 
        60937  +      ** then there is no divider cell in b.apCell[]. Instead, the divider 
 60276  60938         ** cell consists of the integer key for the right-most cell of 
 60277  60939         ** the sibling-page assembled above only.
 60278  60940         */
 60279  60941         CellInfo info;
 60280  60942         j--;
 60281         -      btreeParseCellPtr(pNew, apCell[j], &info);
        60943  +      pNew->xParseCell(pNew, b.apCell[j], &info);
 60282  60944         pCell = pTemp;
 60283  60945         sz = 4 + putVarint(&pCell[4], info.nKey);
 60284  60946         pTemp = 0;
 60285  60947       }else{
 60286  60948         pCell -= 4;
 60287  60949         /* Obscure case for non-leaf-data trees: If the cell at pCell was
 60288  60950         ** previously stored on a leaf node, and its reported size was 4
................................................................................
 60291  60953         ** any cell). But it is important to pass the correct size to 
 60292  60954         ** insertCell(), so reparse the cell now.
 60293  60955         **
 60294  60956         ** Note that this can never happen in an SQLite data file, as all
 60295  60957         ** cells are at least 4 bytes. It only happens in b-trees used
 60296  60958         ** to evaluate "IN (SELECT ...)" and similar clauses.
 60297  60959         */
 60298         -      if( szCell[j]==4 ){
        60960  +      if( b.szCell[j]==4 ){
 60299  60961           assert(leafCorrection==4);
 60300         -        sz = cellSizePtr(pParent, pCell);
        60962  +        sz = pParent->xCellSize(pParent, pCell);
 60301  60963         }
 60302  60964       }
 60303  60965       iOvflSpace += sz;
 60304  60966       assert( sz<=pBt->maxLocal+23 );
 60305  60967       assert( iOvflSpace <= (int)pBt->pageSize );
 60306  60968       insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
 60307  60969       if( rc!=SQLITE_OK ) goto balance_cleanup;
................................................................................
 60349  61011         ** only after iPg+1 has already been updated. */
 60350  61012         assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
 60351  61013   
 60352  61014         if( iPg==0 ){
 60353  61015           iNew = iOld = 0;
 60354  61016           nNewCell = cntNew[0];
 60355  61017         }else{
 60356         -        iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell;
        61018  +        iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
 60357  61019           iNew = cntNew[iPg-1] + !leafData;
 60358  61020           nNewCell = cntNew[iPg] - iNew;
 60359  61021         }
 60360  61022   
 60361         -      editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell);
        61023  +      rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
        61024  +      if( rc ) goto balance_cleanup;
 60362  61025         abDone[iPg]++;
 60363  61026         apNew[iPg]->nFree = usableSpace-szNew[iPg];
 60364  61027         assert( apNew[iPg]->nOverflow==0 );
 60365  61028         assert( apNew[iPg]->nCell==nNewCell );
 60366  61029       }
 60367  61030     }
 60368  61031   
................................................................................
 60405  61068         u32 key = get4byte(&apNew[i]->aData[8]);
 60406  61069         ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
 60407  61070       }
 60408  61071     }
 60409  61072   
 60410  61073     assert( pParent->isInit );
 60411  61074     TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
 60412         -          nOld, nNew, nCell));
        61075  +          nOld, nNew, b.nCell));
 60413  61076   
 60414  61077     /* Free any old pages that were not reused as new pages.
 60415  61078     */
 60416  61079     for(i=nNew; i<nOld; i++){
 60417  61080       freePage(apOld[i], &rc);
 60418  61081     }
 60419  61082   
................................................................................
 60428  61091     }
 60429  61092   #endif
 60430  61093   
 60431  61094     /*
 60432  61095     ** Cleanup before returning.
 60433  61096     */
 60434  61097   balance_cleanup:
 60435         -  sqlite3ScratchFree(apCell);
        61098  +  sqlite3ScratchFree(b.apCell);
 60436  61099     for(i=0; i<nOld; i++){
 60437  61100       releasePage(apOld[i]);
 60438  61101     }
 60439  61102     for(i=0; i<nNew; i++){
 60440  61103       releasePage(apNew[i]);
 60441  61104     }
 60442  61105   
................................................................................
 60738  61401             pCur->pgnoRoot, nKey, nData, pPage->pgno,
 60739  61402             loc==0 ? "overwrite" : "new entry"));
 60740  61403     assert( pPage->isInit );
 60741  61404     newCell = pBt->pTmpSpace;
 60742  61405     assert( newCell!=0 );
 60743  61406     rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
 60744  61407     if( rc ) goto end_insert;
 60745         -  assert( szNew==cellSizePtr(pPage, newCell) );
        61408  +  assert( szNew==pPage->xCellSize(pPage, newCell) );
 60746  61409     assert( szNew <= MX_CELL_SIZE(pBt) );
 60747  61410     idx = pCur->aiIdx[pCur->iPage];
 60748  61411     if( loc==0 ){
 60749  61412       u16 szOld;
 60750  61413       assert( idx<pPage->nCell );
 60751  61414       rc = sqlite3PagerWrite(pPage->pDbPage);
 60752  61415       if( rc ){
................................................................................
 60880  61543       MemPage *pLeaf = pCur->apPage[pCur->iPage];
 60881  61544       int nCell;
 60882  61545       Pgno n = pCur->apPage[iCellDepth+1]->pgno;
 60883  61546       unsigned char *pTmp;
 60884  61547   
 60885  61548       pCell = findCell(pLeaf, pLeaf->nCell-1);
 60886  61549       if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
 60887         -    nCell = cellSizePtr(pLeaf, pCell);
        61550  +    nCell = pLeaf->xCellSize(pLeaf, pCell);
 60888  61551       assert( MX_CELL_SIZE(pBt) >= nCell );
 60889  61552       pTmp = pBt->pTmpSpace;
 60890  61553       assert( pTmp!=0 );
 60891  61554       rc = sqlite3PagerWrite(pLeaf->pDbPage);
 60892  61555       insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
 60893  61556       dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
 60894  61557       if( rc ) return rc;
................................................................................
 61774  62437   
 61775  62438       /* Check payload overflow pages
 61776  62439       */
 61777  62440       pCheck->zPfx = "On tree page %d cell %d: ";
 61778  62441       pCheck->v1 = iPage;
 61779  62442       pCheck->v2 = i;
 61780  62443       pCell = findCell(pPage,i);
 61781         -    btreeParseCellPtr(pPage, pCell, &info);
        62444  +    pPage->xParseCell(pPage, pCell, &info);
 61782  62445       sz = info.nPayload;
 61783  62446       /* For intKey pages, check that the keys are in order.
 61784  62447       */
 61785  62448       if( pPage->intKey ){
 61786  62449         if( i==0 ){
 61787  62450           nMinKey = nMaxKey = info.nKey;
 61788  62451         }else if( info.nKey <= nMaxKey ){
................................................................................
 61892  62555       cellStart = hdr + 12 - 4*pPage->leaf;
 61893  62556       /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
 61894  62557       ** integer offsets to the cell contents. */
 61895  62558       for(i=0; i<nCell; i++){
 61896  62559         int pc = get2byte(&data[cellStart+i*2]);
 61897  62560         u32 size = 65536;
 61898  62561         if( pc<=usableSize-4 ){
 61899         -        size = cellSizePtr(pPage, &data[pc]);
        62562  +        size = pPage->xCellSize(pPage, &data[pc]);
 61900  62563         }
 61901  62564         if( (int)(pc+size-1)>=usableSize ){
 61902  62565           pCheck->zPfx = 0;
 61903  62566           checkAppendMsg(pCheck,
 61904  62567               "Corruption detected in cell %d on page %d",i,iPage);
 61905  62568         }else{
 61906  62569           btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
................................................................................
 62290  62953   }
 62291  62954   
 62292  62955   /* 
 62293  62956   ** Mark this cursor as an incremental blob cursor.
 62294  62957   */
 62295  62958   SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
 62296  62959     pCur->curFlags |= BTCF_Incrblob;
        62960  +  pCur->pBtree->hasIncrblobCur = 1;
 62297  62961   }
 62298  62962   #endif
 62299  62963   
 62300  62964   /*
 62301  62965   ** Set both the "read version" (single byte at byte offset 18) and 
 62302  62966   ** "write version" (single byte at byte offset 19) fields in the database
 62303  62967   ** header to iVersion.
................................................................................
 63737  64401   ** is forced.  In other words, the value is converted into the desired
 63738  64402   ** affinity even if that results in loss of data.  This routine is
 63739  64403   ** used (for example) to implement the SQL "cast()" operator.
 63740  64404   */
 63741  64405   SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
 63742  64406     if( pMem->flags & MEM_Null ) return;
 63743  64407     switch( aff ){
 63744         -    case SQLITE_AFF_NONE: {   /* Really a cast to BLOB */
        64408  +    case SQLITE_AFF_BLOB: {   /* Really a cast to BLOB */
 63745  64409         if( (pMem->flags & MEM_Blob)==0 ){
 63746  64410           sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
 63747  64411           assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
 63748  64412           MemSetTypeFlag(pMem, MEM_Blob);
 63749  64413         }else{
 63750  64414           pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
 63751  64415         }
................................................................................
 63926  64590   
 63927  64591   /*
 63928  64592   ** Make an shallow copy of pFrom into pTo.  Prior contents of
 63929  64593   ** pTo are freed.  The pFrom->z field is not duplicated.  If
 63930  64594   ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
 63931  64595   ** and flags gets srcType (either MEM_Ephem or MEM_Static).
 63932  64596   */
        64597  +static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
        64598  +  vdbeMemClearExternAndSetNull(pTo);
        64599  +  assert( !VdbeMemDynamic(pTo) );
        64600  +  sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
        64601  +}
 63933  64602   SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
 63934  64603     assert( (pFrom->flags & MEM_RowSet)==0 );
 63935  64604     assert( pTo->db==pFrom->db );
 63936         -  if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
        64605  +  if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
 63937  64606     memcpy(pTo, pFrom, MEMCELLSIZE);
 63938  64607     if( (pFrom->flags&MEM_Static)==0 ){
 63939  64608       pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
 63940  64609       assert( srcType==MEM_Ephem || srcType==MEM_Static );
 63941  64610       pTo->flags |= srcType;
 63942  64611     }
 63943  64612   }
................................................................................
 64095  64764   ** pMem->zMalloc space will be allocated if necessary.  The calling routine
 64096  64765   ** is responsible for making sure that the pMem object is eventually
 64097  64766   ** destroyed.
 64098  64767   **
 64099  64768   ** If this routine fails for any reason (malloc returns NULL or unable
 64100  64769   ** to read from the disk) then the pMem is left in an inconsistent state.
 64101  64770   */
        64771  +static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
        64772  +  BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
        64773  +  u32 offset,       /* Offset from the start of data to return bytes from. */
        64774  +  u32 amt,          /* Number of bytes to return. */
        64775  +  int key,          /* If true, retrieve from the btree key, not data. */
        64776  +  Mem *pMem         /* OUT: Return data in this Mem structure. */
        64777  +){
        64778  +  int rc;
        64779  +  pMem->flags = MEM_Null;
        64780  +  if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
        64781  +    if( key ){
        64782  +      rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
        64783  +    }else{
        64784  +      rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
        64785  +    }
        64786  +    if( rc==SQLITE_OK ){
        64787  +      pMem->z[amt] = 0;
        64788  +      pMem->z[amt+1] = 0;
        64789  +      pMem->flags = MEM_Blob|MEM_Term;
        64790  +      pMem->n = (int)amt;
        64791  +    }else{
        64792  +      sqlite3VdbeMemRelease(pMem);
        64793  +    }
        64794  +  }
        64795  +  return rc;
        64796  +}
 64102  64797   SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
 64103  64798     BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
 64104  64799     u32 offset,       /* Offset from the start of data to return bytes from. */
 64105  64800     u32 amt,          /* Number of bytes to return. */
 64106  64801     int key,          /* If true, retrieve from the btree key, not data. */
 64107  64802     Mem *pMem         /* OUT: Return data in this Mem structure. */
 64108  64803   ){
................................................................................
 64124  64819     assert( zData!=0 );
 64125  64820   
 64126  64821     if( offset+amt<=available ){
 64127  64822       pMem->z = &zData[offset];
 64128  64823       pMem->flags = MEM_Blob|MEM_Ephem;
 64129  64824       pMem->n = (int)amt;
 64130  64825     }else{
 64131         -    pMem->flags = MEM_Null;
 64132         -    if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
 64133         -      if( key ){
 64134         -        rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
 64135         -      }else{
 64136         -        rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
 64137         -      }
 64138         -      if( rc==SQLITE_OK ){
 64139         -        pMem->z[amt] = 0;
 64140         -        pMem->z[amt+1] = 0;
 64141         -        pMem->flags = MEM_Blob|MEM_Term;
 64142         -        pMem->n = (int)amt;
 64143         -      }else{
 64144         -        sqlite3VdbeMemRelease(pMem);
 64145         -      }
 64146         -    }
        64826  +    rc = vdbeMemFromBtreeResize(pCur, offset, amt, key, pMem);
 64147  64827     }
 64148  64828   
 64149  64829     return rc;
 64150  64830   }
 64151  64831   
 64152  64832   /*
 64153  64833   ** The pVal argument is known to be a value other than NULL.
................................................................................
 64460  65140       if( ExprHasProperty(pExpr, EP_IntValue) ){
 64461  65141         sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
 64462  65142       }else{
 64463  65143         zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
 64464  65144         if( zVal==0 ) goto no_mem;
 64465  65145         sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
 64466  65146       }
 64467         -    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
        65147  +    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
 64468  65148         sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
 64469  65149       }else{
 64470  65150         sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
 64471  65151       }
 64472  65152       if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
 64473  65153       if( enc!=SQLITE_UTF8 ){
 64474  65154         rc = sqlite3VdbeChangeEncoding(pVal, enc);
................................................................................
 64827  65507   SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
 64828  65508     if( !v ) return;
 64829  65509     sqlite3VdbeMemRelease((Mem *)v);
 64830  65510     sqlite3DbFree(((Mem*)v)->db, v);
 64831  65511   }
 64832  65512   
 64833  65513   /*
 64834         -** Return the number of bytes in the sqlite3_value object assuming
 64835         -** that it uses the encoding "enc"
        65514  +** The sqlite3ValueBytes() routine returns the number of bytes in the
        65515  +** sqlite3_value object assuming that it uses the encoding "enc".
        65516  +** The valueBytes() routine is a helper function.
 64836  65517   */
        65518  +static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
        65519  +  return valueToText(pVal, enc)!=0 ? pVal->n : 0;
        65520  +}
 64837  65521   SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
 64838  65522     Mem *p = (Mem*)pVal;
 64839         -  if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
        65523  +  assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
        65524  +  if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
        65525  +    return p->n;
        65526  +  }
        65527  +  if( (p->flags & MEM_Blob)!=0 ){
 64840  65528       if( p->flags & MEM_Zero ){
 64841  65529         return p->n + p->u.nZero;
 64842  65530       }else{
 64843  65531         return p->n;
 64844  65532       }
 64845  65533     }
 64846         -  return 0;
        65534  +  if( p->flags & MEM_Null ) return 0;
        65535  +  return valueBytes(pVal, enc);
 64847  65536   }
 64848  65537   
 64849  65538   /************** End of vdbemem.c *********************************************/
 64850  65539   /************** Begin file vdbeaux.c *****************************************/
 64851  65540   /*
 64852  65541   ** 2003 September 6
 64853  65542   **
................................................................................
 65076  65765     const char *zP4,    /* The P4 operand */
 65077  65766     int p4type          /* P4 operand type */
 65078  65767   ){
 65079  65768     int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
 65080  65769     sqlite3VdbeChangeP4(p, addr, zP4, p4type);
 65081  65770     return addr;
 65082  65771   }
        65772  +
        65773  +/*
        65774  +** Add an opcode that includes the p4 value with a P4_INT64 type.
        65775  +*/
        65776  +SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
        65777  +  Vdbe *p,            /* Add the opcode to this VM */
        65778  +  int op,             /* The new opcode */
        65779  +  int p1,             /* The P1 operand */
        65780  +  int p2,             /* The P2 operand */
        65781  +  int p3,             /* The P3 operand */
        65782  +  const u8 *zP4,      /* The P4 operand */
        65783  +  int p4type          /* P4 operand type */
        65784  +){
        65785  +  char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8);
        65786  +  if( p4copy ) memcpy(p4copy, zP4, 8);
        65787  +  return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
        65788  +}
 65083  65789   
 65084  65790   /*
 65085  65791   ** Add an OP_ParseSchema opcode.  This routine is broken out from
 65086  65792   ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
 65087  65793   ** as having been used.
 65088  65794   **
 65089  65795   ** The zWhere string must have been obtained from sqlite3_malloc().
................................................................................
 65241  65947   **
 65242  65948   **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 65243  65949   **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 65244  65950   **   *  OP_Destroy
 65245  65951   **   *  OP_VUpdate
 65246  65952   **   *  OP_VRename
 65247  65953   **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
        65954  +**   *  OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...)
 65248  65955   **
 65249  65956   ** Then check that the value of Parse.mayAbort is true if an
 65250  65957   ** ABORT may be thrown, or false otherwise. Return true if it does
 65251  65958   ** match, or false otherwise. This function is intended to be used as
 65252  65959   ** part of an assert statement in the compiler. Similar to:
 65253  65960   **
 65254  65961   **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
 65255  65962   */
 65256  65963   SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
 65257  65964     int hasAbort = 0;
 65258  65965     int hasFkCounter = 0;
        65966  +  int hasCreateTable = 0;
        65967  +  int hasInitCoroutine = 0;
 65259  65968     Op *pOp;
 65260  65969     VdbeOpIter sIter;
 65261  65970     memset(&sIter, 0, sizeof(sIter));
 65262  65971     sIter.v = v;
 65263  65972   
 65264  65973     while( (pOp = opIterNext(&sIter))!=0 ){
 65265  65974       int opcode = pOp->opcode;
................................................................................
 65266  65975       if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
 65267  65976        || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
 65268  65977         && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
 65269  65978       ){
 65270  65979         hasAbort = 1;
 65271  65980         break;
 65272  65981       }
        65982  +    if( opcode==OP_CreateTable ) hasCreateTable = 1;
        65983  +    if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
 65273  65984   #ifndef SQLITE_OMIT_FOREIGN_KEY
 65274  65985       if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
 65275  65986         hasFkCounter = 1;
 65276  65987       }
 65277  65988   #endif
 65278  65989     }
 65279  65990     sqlite3DbFree(v->db, sIter.apSub);
 65280  65991   
 65281  65992     /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
 65282  65993     ** If malloc failed, then the while() loop above may not have iterated
 65283  65994     ** through all opcodes and hasAbort may be set incorrectly. Return
 65284  65995     ** true for this case to prevent the assert() in the callers frame
 65285  65996     ** from failing.  */
 65286         -  return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter );
        65997  +  return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
        65998  +              || (hasCreateTable && hasInitCoroutine) );
 65287  65999   }
 65288  66000   #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
 65289  66001   
 65290  66002   /*
 65291  66003   ** Loop through the program looking for P2 values that are negative
 65292  66004   ** on jump instructions.  Each such value is a label.  Resolve the
 65293  66005   ** label by setting the P2 value to its correct non-zero value.
................................................................................
 66062  66774   }
 66063  66775   #endif
 66064  66776   
 66065  66777   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 66066  66778   /*
 66067  66779   ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
 66068  66780   */
 66069         -SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
        66781  +static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
 66070  66782     int i;
 66071  66783     sqlite3 *db;
 66072  66784     Db *aDb;
 66073  66785     int nDb;
 66074         -  if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
 66075  66786     db = p->db;
 66076  66787     aDb = db->aDb;
 66077  66788     nDb = db->nDb;
 66078  66789     for(i=0; i<nDb; i++){
 66079  66790       if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
 66080  66791         sqlite3BtreeLeave(aDb[i].pBt);
 66081  66792       }
 66082  66793     }
        66794  +}
        66795  +SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
        66796  +  if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
        66797  +  vdbeLeave(p);
 66083  66798   }
 66084  66799   #endif
 66085  66800   
 66086  66801   #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 66087  66802   /*
 66088  66803   ** Print a single opcode.  This routine is used for debugging only.
 66089  66804   */
................................................................................
 71120  71835   **    is not possible.  Note that the integer representation is
 71121  71836   **    always preferred, even if the affinity is REAL, because
 71122  71837   **    an integer representation is more space efficient on disk.
 71123  71838   **
 71124  71839   ** SQLITE_AFF_TEXT:
 71125  71840   **    Convert pRec to a text representation.
 71126  71841   **
 71127         -** SQLITE_AFF_NONE:
        71842  +** SQLITE_AFF_BLOB:
 71128  71843   **    No-op.  pRec is unchanged.
 71129  71844   */
 71130  71845   static void applyAffinity(
 71131  71846     Mem *pRec,          /* The value to apply affinity to */
 71132  71847     char affinity,      /* The affinity to be applied */
 71133  71848     u8 enc              /* Use this text encoding */
 71134  71849   ){
................................................................................
 71521  72236     assert( p->explain==0 );
 71522  72237     p->pResultSet = 0;
 71523  72238     db->busyHandler.nBusy = 0;
 71524  72239     if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 71525  72240     sqlite3VdbeIOTraceSql(p);
 71526  72241   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 71527  72242     if( db->xProgress ){
        72243  +    u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
 71528  72244       assert( 0 < db->nProgressOps );
 71529         -    nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
 71530         -    if( nProgressLimit==0 ){
 71531         -      nProgressLimit = db->nProgressOps;
 71532         -    }else{
 71533         -      nProgressLimit %= (unsigned)db->nProgressOps;
 71534         -    }
        72245  +    nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
 71535  72246     }
 71536  72247   #endif
 71537  72248   #ifdef SQLITE_DEBUG
 71538  72249     sqlite3BeginBenignMalloc();
 71539  72250     if( p->pc==0
 71540  72251      && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
 71541  72252     ){
................................................................................
 72719  73430   ** <li value="100"> INTEGER
 72720  73431   ** <li value="101"> REAL
 72721  73432   ** </ul>
 72722  73433   **
 72723  73434   ** A NULL value is not changed by this routine.  It remains NULL.
 72724  73435   */
 72725  73436   case OP_Cast: {                  /* in1 */
 72726         -  assert( pOp->p2>=SQLITE_AFF_NONE && pOp->p2<=SQLITE_AFF_REAL );
        73437  +  assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
 72727  73438     testcase( pOp->p2==SQLITE_AFF_TEXT );
 72728         -  testcase( pOp->p2==SQLITE_AFF_NONE );
        73439  +  testcase( pOp->p2==SQLITE_AFF_BLOB );
 72729  73440     testcase( pOp->p2==SQLITE_AFF_NUMERIC );
 72730  73441     testcase( pOp->p2==SQLITE_AFF_INTEGER );
 72731  73442     testcase( pOp->p2==SQLITE_AFF_REAL );
 72732  73443     pIn1 = &aMem[pOp->p1];
 72733  73444     memAboutToChange(p, pIn1);
 72734  73445     rc = ExpandBlob(pIn1);
 72735  73446     sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
................................................................................
 73532  74243   ** P4 may be a string that is P2 characters long.  The nth character of the
 73533  74244   ** string indicates the column affinity that should be used for the nth
 73534  74245   ** field of the index key.
 73535  74246   **
 73536  74247   ** The mapping from character to affinity is given by the SQLITE_AFF_
 73537  74248   ** macros defined in sqliteInt.h.
 73538  74249   **
 73539         -** If P4 is NULL then all index fields have the affinity NONE.
        74250  +** If P4 is NULL then all index fields have the affinity BLOB.
 73540  74251   */
 73541  74252   case OP_MakeRecord: {
 73542  74253     u8 *zNewRecord;        /* A buffer to hold the data for the new record */
 73543  74254     Mem *pRec;             /* The new record */
 73544  74255     u64 nData;             /* Number of bytes of data space */
 73545  74256     int nHdr;              /* Number of bytes of header space */
 73546  74257     i64 nByte;             /* Data space required for this record */
................................................................................
 74449  75160   */
 74450  75161   case OP_Close: {
 74451  75162     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 74452  75163     sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
 74453  75164     p->apCsr[pOp->p1] = 0;
 74454  75165     break;
 74455  75166   }
        75167  +
        75168  +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
        75169  +/* Opcode: ColumnsUsed P1 * * P4 *
        75170  +**
        75171  +** This opcode (which only exists if SQLite was compiled with
        75172  +** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
        75173  +** table or index for cursor P1 are used.  P4 is a 64-bit integer
        75174  +** (P4_INT64) in which the first 63 bits are one for each of the
        75175  +** first 63 columns of the table or index that are actually used
        75176  +** by the cursor.  The high-order bit is set if any column after
        75177  +** the 64th is used.
        75178  +*/
        75179  +case OP_ColumnsUsed: {
        75180  +  VdbeCursor *pC;
        75181  +  pC = p->apCsr[pOp->p1];
        75182  +  assert( pC->pCursor );
        75183  +  pC->maskUsed = *(u64*)pOp->p4.pI64;
        75184  +  break;
        75185  +}
        75186  +#endif
 74456  75187   
 74457  75188   /* Opcode: SeekGE P1 P2 P3 P4 *
 74458  75189   ** Synopsis: key=r[P3@P4]
 74459  75190   **
 74460  75191   ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 74461  75192   ** use the value in register P3 as the key.  If cursor P1 refers 
 74462  75193   ** to an SQL index, then P3 is the first in an array of P4 registers 
................................................................................
 82719  83450           if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
 82720  83451             sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
 82721  83452                 "the GROUP BY clause");
 82722  83453             return WRC_Abort;
 82723  83454           }
 82724  83455         }
 82725  83456       }
        83457  +
        83458  +    /* If this is part of a compound SELECT, check that it has the right
        83459  +    ** number of expressions in the select list. */
        83460  +    if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
        83461  +      sqlite3SelectWrongNumTermsError(pParse, p->pNext);
        83462  +      return WRC_Abort;
        83463  +    }
 82726  83464   
 82727  83465       /* Advance to the next term of the compound
 82728  83466       */
 82729  83467       p = p->pPrior;
 82730  83468       nCompound++;
 82731  83469     }
 82732  83470   
................................................................................
 83087  83825     if( aff1 && aff2 ){
 83088  83826       /* Both sides of the comparison are columns. If one has numeric
 83089  83827       ** affinity, use that. Otherwise use no affinity.
 83090  83828       */
 83091  83829       if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
 83092  83830         return SQLITE_AFF_NUMERIC;
 83093  83831       }else{
 83094         -      return SQLITE_AFF_NONE;
        83832  +      return SQLITE_AFF_BLOB;
 83095  83833       }
 83096  83834     }else if( !aff1 && !aff2 ){
 83097  83835       /* Neither side of the comparison is a column.  Compare the
 83098  83836       ** results directly.
 83099  83837       */
 83100         -    return SQLITE_AFF_NONE;
        83838  +    return SQLITE_AFF_BLOB;
 83101  83839     }else{
 83102  83840       /* One side is a column, the other is not. Use the columns affinity. */
 83103  83841       assert( aff1==0 || aff2==0 );
 83104  83842       return (aff1 + aff2);
 83105  83843     }
 83106  83844   }
 83107  83845   
................................................................................
 83117  83855     assert( pExpr->pLeft );
 83118  83856     aff = sqlite3ExprAffinity(pExpr->pLeft);
 83119  83857     if( pExpr->pRight ){
 83120  83858       aff = sqlite3CompareAffinity(pExpr->pRight, aff);
 83121  83859     }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 83122  83860       aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
 83123  83861     }else if( !aff ){
 83124         -    aff = SQLITE_AFF_NONE;
        83862  +    aff = SQLITE_AFF_BLOB;
 83125  83863     }
 83126  83864     return aff;
 83127  83865   }
 83128  83866   
 83129  83867   /*
 83130  83868   ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
 83131  83869   ** idx_affinity is the affinity of an indexed column. Return true
 83132  83870   ** if the index with affinity idx_affinity may be used to implement
 83133  83871   ** the comparison in pExpr.
 83134  83872   */
 83135  83873   SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
 83136  83874     char aff = comparisonAffinity(pExpr);
 83137  83875     switch( aff ){
 83138         -    case SQLITE_AFF_NONE:
        83876  +    case SQLITE_AFF_BLOB:
 83139  83877         return 1;
 83140  83878       case SQLITE_AFF_TEXT:
 83141  83879         return idx_affinity==SQLITE_AFF_TEXT;
 83142  83880       default:
 83143  83881         return sqlite3IsNumericAffinity(idx_affinity);
 83144  83882     }
 83145  83883   }
................................................................................
 83937  84675       pNewItem->jointype = pOldItem->jointype;
 83938  84676       pNewItem->iCursor = pOldItem->iCursor;
 83939  84677       pNewItem->addrFillSub = pOldItem->addrFillSub;
 83940  84678       pNewItem->regReturn = pOldItem->regReturn;
 83941  84679       pNewItem->isCorrelated = pOldItem->isCorrelated;
 83942  84680       pNewItem->viaCoroutine = pOldItem->viaCoroutine;
 83943  84681       pNewItem->isRecursive = pOldItem->isRecursive;
 83944         -    pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
        84682  +    pNewItem->zIndexedBy = sqlite3DbStrDup(db, pOldItem->zIndexedBy);
 83945  84683       pNewItem->notIndexed = pOldItem->notIndexed;
 83946  84684       pNewItem->pIndex = pOldItem->pIndex;
 83947  84685       pTab = pNewItem->pTab = pOldItem->pTab;
 83948  84686       if( pTab ){
 83949  84687         pTab->nRef++;
 83950  84688       }
 83951  84689       pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
................................................................................
 84164  84902   ** Walker.eCode value determines the type of "constant" we are looking
 84165  84903   ** for.
 84166  84904   **
 84167  84905   ** These callback routines are used to implement the following:
 84168  84906   **
 84169  84907   **     sqlite3ExprIsConstant()                  pWalker->eCode==1
 84170  84908   **     sqlite3ExprIsConstantNotJoin()           pWalker->eCode==2
 84171         -**     sqlite3ExprRefOneTableOnly()             pWalker->eCode==3
        84909  +**     sqlite3ExprIsTableConstant()             pWalker->eCode==3
 84172  84910   **     sqlite3ExprIsConstantOrFunction()        pWalker->eCode==4 or 5
 84173  84911   **
 84174  84912   ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
 84175  84913   ** is found to not be a constant.
 84176  84914   **
 84177  84915   ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
 84178  84916   ** in a CREATE TABLE statement.  The Walker.eCode value is 5 when parsing
................................................................................
 84272  85010   ** an ON or USING clause.
 84273  85011   */
 84274  85012   SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
 84275  85013     return exprIsConst(p, 2, 0);
 84276  85014   }
 84277  85015   
 84278  85016   /*
 84279         -** Walk an expression tree.  Return non-zero if the expression constant
        85017  +** Walk an expression tree.  Return non-zero if the expression is constant
 84280  85018   ** for any single row of the table with cursor iCur.  In other words, the
 84281  85019   ** expression must not refer to any non-deterministic function nor any
 84282  85020   ** table other than iCur.
 84283  85021   */
 84284  85022   SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
 84285  85023     return exprIsConst(p, 3, iCur);
 84286  85024   }
................................................................................
 84378  85116   ** This routine is used to determine if the OP_Affinity operation
 84379  85117   ** can be omitted.  When in doubt return FALSE.  A false negative
 84380  85118   ** is harmless.  A false positive, however, can result in the wrong
 84381  85119   ** answer.
 84382  85120   */
 84383  85121   SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
 84384  85122     u8 op;
 84385         -  if( aff==SQLITE_AFF_NONE ) return 1;
        85123  +  if( aff==SQLITE_AFF_BLOB ) return 1;
 84386  85124     while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
 84387  85125     op = p->op;
 84388  85126     if( op==TK_REGISTER ) op = p->op2;
 84389  85127     switch( op ){
 84390  85128       case TK_INTEGER: {
 84391  85129         return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
 84392  85130       }
................................................................................
 84829  85567           */
 84830  85568           int i;
 84831  85569           ExprList *pList = pExpr->x.pList;
 84832  85570           struct ExprList_item *pItem;
 84833  85571           int r1, r2, r3;
 84834  85572   
 84835  85573           if( !affinity ){
 84836         -          affinity = SQLITE_AFF_NONE;
        85574  +          affinity = SQLITE_AFF_BLOB;
 84837  85575           }
 84838  85576           if( pKeyInfo ){
 84839  85577             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 84840  85578             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 84841  85579           }
 84842  85580   
 84843  85581           /* Loop through each expression in <exprlist>. */
................................................................................
 85104  85842     }
 85105  85843     sqlite3ReleaseTempReg(pParse, r1);
 85106  85844     sqlite3ExprCachePop(pParse);
 85107  85845     VdbeComment((v, "end IN expr"));
 85108  85846   }
 85109  85847   #endif /* SQLITE_OMIT_SUBQUERY */
 85110  85848   
 85111         -/*
 85112         -** Duplicate an 8-byte value
 85113         -*/
 85114         -static char *dup8bytes(Vdbe *v, const char *in){
 85115         -  char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
 85116         -  if( out ){
 85117         -    memcpy(out, in, 8);
 85118         -  }
 85119         -  return out;
 85120         -}
 85121         -
 85122  85849   #ifndef SQLITE_OMIT_FLOATING_POINT
 85123  85850   /*
 85124  85851   ** Generate an instruction that will put the floating point
 85125  85852   ** value described by z[0..n-1] into register iMem.
 85126  85853   **
 85127  85854   ** The z[] string will probably not be zero-terminated.  But the 
 85128  85855   ** z[n] character is guaranteed to be something that does not look
 85129  85856   ** like the continuation of the number.
 85130  85857   */
 85131  85858   static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
 85132  85859     if( ALWAYS(z!=0) ){
 85133  85860       double value;
 85134         -    char *zV;
 85135  85861       sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
 85136  85862       assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
 85137  85863       if( negateFlag ) value = -value;
 85138         -    zV = dup8bytes(v, (char*)&value);
 85139         -    sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
        85864  +    sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
 85140  85865     }
 85141  85866   }
 85142  85867   #endif
 85143  85868   
 85144  85869   
 85145  85870   /*
 85146  85871   ** Generate an instruction that will put the integer describe by
................................................................................
 85158  85883     }else{
 85159  85884       int c;
 85160  85885       i64 value;
 85161  85886       const char *z = pExpr->u.zToken;
 85162  85887       assert( z!=0 );
 85163  85888       c = sqlite3DecOrHexToI64(z, &value);
 85164  85889       if( c==0 || (c==2 && negFlag) ){
 85165         -      char *zV;
 85166  85890         if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
 85167         -      zV = dup8bytes(v, (char*)&value);
 85168         -      sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
        85891  +      sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
 85169  85892       }else{
 85170  85893   #ifdef SQLITE_OMIT_FLOATING_POINT
 85171  85894         sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
 85172  85895   #else
 85173  85896   #ifndef SQLITE_OMIT_HEX_INTEGER
 85174  85897         if( sqlite3_strnicmp(z,"0x",2)==0 ){
 85175  85898           sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
................................................................................
 85766  86489         }
 85767  86490   
 85768  86491         /* The UNLIKELY() function is a no-op.  The result is the value
 85769  86492         ** of the first argument.
 85770  86493         */
 85771  86494         if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
 85772  86495           assert( nFarg>=1 );
 85773         -        sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
        86496  +        inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
 85774  86497           break;
 85775  86498         }
 85776  86499   
 85777  86500         for(i=0; i<nFarg; i++){
 85778  86501           if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
 85779  86502             testcase( i==31 );
 85780  86503             constMask |= MASKBIT32(i);
................................................................................
 86207  86930     assert( pExpr->op!=TK_REGISTER );
 86208  86931     sqlite3ExprCode(pParse, pExpr, target);
 86209  86932     iMem = ++pParse->nMem;
 86210  86933     sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
 86211  86934     exprToRegister(pExpr, iMem);
 86212  86935   }
 86213  86936   
 86214         -#ifdef SQLITE_DEBUG
 86215         -/*
 86216         -** Generate a human-readable explanation of an expression tree.
 86217         -*/
 86218         -SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
 86219         -  const char *zBinOp = 0;   /* Binary operator */
 86220         -  const char *zUniOp = 0;   /* Unary operator */
 86221         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
 86222         -  if( pExpr==0 ){
 86223         -    sqlite3TreeViewLine(pView, "nil");
 86224         -    sqlite3TreeViewPop(pView);
 86225         -    return;
 86226         -  }
 86227         -  switch( pExpr->op ){
 86228         -    case TK_AGG_COLUMN: {
 86229         -      sqlite3TreeViewLine(pView, "AGG{%d:%d}",
 86230         -            pExpr->iTable, pExpr->iColumn);
 86231         -      break;
 86232         -    }
 86233         -    case TK_COLUMN: {
 86234         -      if( pExpr->iTable<0 ){
 86235         -        /* This only happens when coding check constraints */
 86236         -        sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
 86237         -      }else{
 86238         -        sqlite3TreeViewLine(pView, "{%d:%d}",
 86239         -                             pExpr->iTable, pExpr->iColumn);
 86240         -      }
 86241         -      break;
 86242         -    }
 86243         -    case TK_INTEGER: {
 86244         -      if( pExpr->flags & EP_IntValue ){
 86245         -        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
 86246         -      }else{
 86247         -        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
 86248         -      }
 86249         -      break;
 86250         -    }
 86251         -#ifndef SQLITE_OMIT_FLOATING_POINT
 86252         -    case TK_FLOAT: {
 86253         -      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
 86254         -      break;
 86255         -    }
 86256         -#endif
 86257         -    case TK_STRING: {
 86258         -      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
 86259         -      break;
 86260         -    }
 86261         -    case TK_NULL: {
 86262         -      sqlite3TreeViewLine(pView,"NULL");
 86263         -      break;
 86264         -    }
 86265         -#ifndef SQLITE_OMIT_BLOB_LITERAL
 86266         -    case TK_BLOB: {
 86267         -      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
 86268         -      break;
 86269         -    }
 86270         -#endif
 86271         -    case TK_VARIABLE: {
 86272         -      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
 86273         -                          pExpr->u.zToken, pExpr->iColumn);
 86274         -      break;
 86275         -    }
 86276         -    case TK_REGISTER: {
 86277         -      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
 86278         -      break;
 86279         -    }
 86280         -    case TK_AS: {
 86281         -      sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
 86282         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 86283         -      break;
 86284         -    }
 86285         -    case TK_ID: {
 86286         -      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
 86287         -      break;
 86288         -    }
 86289         -#ifndef SQLITE_OMIT_CAST
 86290         -    case TK_CAST: {
 86291         -      /* Expressions of the form:   CAST(pLeft AS token) */
 86292         -      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
 86293         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 86294         -      break;
 86295         -    }
 86296         -#endif /* SQLITE_OMIT_CAST */
 86297         -    case TK_LT:      zBinOp = "LT";     break;
 86298         -    case TK_LE:      zBinOp = "LE";     break;
 86299         -    case TK_GT:      zBinOp = "GT";     break;
 86300         -    case TK_GE:      zBinOp = "GE";     break;
 86301         -    case TK_NE:      zBinOp = "NE";     break;
 86302         -    case TK_EQ:      zBinOp = "EQ";     break;
 86303         -    case TK_IS:      zBinOp = "IS";     break;
 86304         -    case TK_ISNOT:   zBinOp = "ISNOT";  break;
 86305         -    case TK_AND:     zBinOp = "AND";    break;
 86306         -    case TK_OR:      zBinOp = "OR";     break;
 86307         -    case TK_PLUS:    zBinOp = "ADD";    break;
 86308         -    case TK_STAR:    zBinOp = "MUL";    break;
 86309         -    case TK_MINUS:   zBinOp = "SUB";    break;
 86310         -    case TK_REM:     zBinOp = "REM";    break;
 86311         -    case TK_BITAND:  zBinOp = "BITAND"; break;
 86312         -    case TK_BITOR:   zBinOp = "BITOR";  break;
 86313         -    case TK_SLASH:   zBinOp = "DIV";    break;
 86314         -    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
 86315         -    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
 86316         -    case TK_CONCAT:  zBinOp = "CONCAT"; break;
 86317         -    case TK_DOT:     zBinOp = "DOT";    break;
 86318         -
 86319         -    case TK_UMINUS:  zUniOp = "UMINUS"; break;
 86320         -    case TK_UPLUS:   zUniOp = "UPLUS";  break;
 86321         -    case TK_BITNOT:  zUniOp = "BITNOT"; break;
 86322         -    case TK_NOT:     zUniOp = "NOT";    break;
 86323         -    case TK_ISNULL:  zUniOp = "ISNULL"; break;
 86324         -    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
 86325         -
 86326         -    case TK_COLLATE: {
 86327         -      sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
 86328         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 86329         -      break;
 86330         -    }
 86331         -
 86332         -    case TK_AGG_FUNCTION:
 86333         -    case TK_FUNCTION: {
 86334         -      ExprList *pFarg;       /* List of function arguments */
 86335         -      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
 86336         -        pFarg = 0;
 86337         -      }else{
 86338         -        pFarg = pExpr->x.pList;
 86339         -      }
 86340         -      if( pExpr->op==TK_AGG_FUNCTION ){
 86341         -        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
 86342         -                             pExpr->op2, pExpr->u.zToken);
 86343         -      }else{
 86344         -        sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
 86345         -      }
 86346         -      if( pFarg ){
 86347         -        sqlite3TreeViewExprList(pView, pFarg, 0, 0);
 86348         -      }
 86349         -      break;
 86350         -    }
 86351         -#ifndef SQLITE_OMIT_SUBQUERY
 86352         -    case TK_EXISTS: {
 86353         -      sqlite3TreeViewLine(pView, "EXISTS-expr");
 86354         -      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
 86355         -      break;
 86356         -    }
 86357         -    case TK_SELECT: {
 86358         -      sqlite3TreeViewLine(pView, "SELECT-expr");
 86359         -      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
 86360         -      break;
 86361         -    }
 86362         -    case TK_IN: {
 86363         -      sqlite3TreeViewLine(pView, "IN");
 86364         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
 86365         -      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 86366         -        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
 86367         -      }else{
 86368         -        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
 86369         -      }
 86370         -      break;
 86371         -    }
 86372         -#endif /* SQLITE_OMIT_SUBQUERY */
 86373         -
 86374         -    /*
 86375         -    **    x BETWEEN y AND z
 86376         -    **
 86377         -    ** This is equivalent to
 86378         -    **
 86379         -    **    x>=y AND x<=z
 86380         -    **
 86381         -    ** X is stored in pExpr->pLeft.
 86382         -    ** Y is stored in pExpr->pList->a[0].pExpr.
 86383         -    ** Z is stored in pExpr->pList->a[1].pExpr.
 86384         -    */
 86385         -    case TK_BETWEEN: {
 86386         -      Expr *pX = pExpr->pLeft;
 86387         -      Expr *pY = pExpr->x.pList->a[0].pExpr;
 86388         -      Expr *pZ = pExpr->x.pList->a[1].pExpr;
 86389         -      sqlite3TreeViewLine(pView, "BETWEEN");
 86390         -      sqlite3TreeViewExpr(pView, pX, 1);
 86391         -      sqlite3TreeViewExpr(pView, pY, 1);
 86392         -      sqlite3TreeViewExpr(pView, pZ, 0);
 86393         -      break;
 86394         -    }
 86395         -    case TK_TRIGGER: {
 86396         -      /* If the opcode is TK_TRIGGER, then the expression is a reference
 86397         -      ** to a column in the new.* or old.* pseudo-tables available to
 86398         -      ** trigger programs. In this case Expr.iTable is set to 1 for the
 86399         -      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
 86400         -      ** is set to the column of the pseudo-table to read, or to -1 to
 86401         -      ** read the rowid field.
 86402         -      */
 86403         -      sqlite3TreeViewLine(pView, "%s(%d)", 
 86404         -          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
 86405         -      break;
 86406         -    }
 86407         -    case TK_CASE: {
 86408         -      sqlite3TreeViewLine(pView, "CASE");
 86409         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
 86410         -      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
 86411         -      break;
 86412         -    }
 86413         -#ifndef SQLITE_OMIT_TRIGGER
 86414         -    case TK_RAISE: {
 86415         -      const char *zType = "unk";
 86416         -      switch( pExpr->affinity ){
 86417         -        case OE_Rollback:   zType = "rollback";  break;
 86418         -        case OE_Abort:      zType = "abort";     break;
 86419         -        case OE_Fail:       zType = "fail";      break;
 86420         -        case OE_Ignore:     zType = "ignore";    break;
 86421         -      }
 86422         -      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
 86423         -      break;
 86424         -    }
 86425         -#endif
 86426         -    default: {
 86427         -      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
 86428         -      break;
 86429         -    }
 86430         -  }
 86431         -  if( zBinOp ){
 86432         -    sqlite3TreeViewLine(pView, "%s", zBinOp);
 86433         -    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
 86434         -    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
 86435         -  }else if( zUniOp ){
 86436         -    sqlite3TreeViewLine(pView, "%s", zUniOp);
 86437         -    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 86438         -  }
 86439         -  sqlite3TreeViewPop(pView);
 86440         -}
 86441         -#endif /* SQLITE_DEBUG */
 86442         -
 86443         -#ifdef SQLITE_DEBUG
 86444         -/*
 86445         -** Generate a human-readable explanation of an expression list.
 86446         -*/
 86447         -SQLITE_PRIVATE void sqlite3TreeViewExprList(
 86448         -  TreeView *pView,
 86449         -  const ExprList *pList,
 86450         -  u8 moreToFollow,
 86451         -  const char *zLabel
 86452         -){
 86453         -  int i;
 86454         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
 86455         -  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
 86456         -  if( pList==0 ){
 86457         -    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
 86458         -  }else{
 86459         -    sqlite3TreeViewLine(pView, "%s", zLabel);
 86460         -    for(i=0; i<pList->nExpr; i++){
 86461         -      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
 86462         -#if 0
 86463         -     if( pList->a[i].zName ){
 86464         -        sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
 86465         -      }
 86466         -      if( pList->a[i].bSpanIsTab ){
 86467         -        sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
 86468         -      }
 86469         -#endif
 86470         -    }
 86471         -  }
 86472         -  sqlite3TreeViewPop(pView);
 86473         -}
 86474         -#endif /* SQLITE_DEBUG */
 86475         -
 86476  86937   /*
 86477  86938   ** Generate code that pushes the value of every element of the given
 86478  86939   ** expression list into a sequence of registers beginning at target.
 86479  86940   **
 86480  86941   ** Return the number of elements evaluated.
 86481  86942   **
 86482  86943   ** The SQLITE_ECEL_DUP flag prevents the arguments from being
................................................................................
 86859  87320         }
 86860  87321         break;
 86861  87322       }
 86862  87323     }
 86863  87324     sqlite3ReleaseTempReg(pParse, regFree1);
 86864  87325     sqlite3ReleaseTempReg(pParse, regFree2);
 86865  87326   }
        87327  +
        87328  +/*
        87329  +** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
        87330  +** code generation, and that copy is deleted after code generation. This
        87331  +** ensures that the original pExpr is unchanged.
        87332  +*/
        87333  +SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
        87334  +  sqlite3 *db = pParse->db;
        87335  +  Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
        87336  +  if( db->mallocFailed==0 ){
        87337  +    sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
        87338  +  }
        87339  +  sqlite3ExprDelete(db, pCopy);
        87340  +}
        87341  +
 86866  87342   
 86867  87343   /*
 86868  87344   ** Do a deep comparison of two expression trees.  Return 0 if the two
 86869  87345   ** expressions are completely identical.  Return 1 if they differ only
 86870  87346   ** by a COLLATE operator at the top level.  Return 2 if there are differences
 86871  87347   ** other than the top-level COLLATE operator.
 86872  87348   **
................................................................................
 88012  88488   
 88013  88489     /* Ensure the default expression is something that sqlite3ValueFromExpr()
 88014  88490     ** can handle (i.e. not CURRENT_TIME etc.)
 88015  88491     */
 88016  88492     if( pDflt ){
 88017  88493       sqlite3_value *pVal = 0;
 88018  88494       int rc;
 88019         -    rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal);
        88495  +    rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
 88020  88496       assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
 88021  88497       if( rc!=SQLITE_OK ){
 88022  88498         db->mallocFailed = 1;
 88023  88499         return;
 88024  88500       }
 88025  88501       if( !pVal ){
 88026  88502         sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
................................................................................
 91872  92348     ** indices.  Hence, the record number for the table must be allocated
 91873  92349     ** now.
 91874  92350     */
 91875  92351     if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
 91876  92352       int j1;
 91877  92353       int fileFormat;
 91878  92354       int reg1, reg2, reg3;
 91879         -    sqlite3BeginWriteOperation(pParse, 0, iDb);
        92355  +    sqlite3BeginWriteOperation(pParse, 1, iDb);
 91880  92356   
 91881  92357   #ifndef SQLITE_OMIT_VIRTUALTABLE
 91882  92358       if( isVirtual ){
 91883  92359         sqlite3VdbeAddOp0(v, OP_VBegin);
 91884  92360       }
 91885  92361   #endif
 91886  92362   
................................................................................
 91988  92464       p->aCol = aNew;
 91989  92465     }
 91990  92466     pCol = &p->aCol[p->nCol];
 91991  92467     memset(pCol, 0, sizeof(p->aCol[0]));
 91992  92468     pCol->zName = z;
 91993  92469    
 91994  92470     /* If there is no type specified, columns have the default affinity
 91995         -  ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
        92471  +  ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will
 91996  92472     ** be called next to set pCol->affinity correctly.
 91997  92473     */
 91998         -  pCol->affinity = SQLITE_AFF_NONE;
        92474  +  pCol->affinity = SQLITE_AFF_BLOB;
 91999  92475     pCol->szEst = 1;
 92000  92476     p->nCol++;
 92001  92477   }
 92002  92478   
 92003  92479   /*
 92004  92480   ** This routine is called by the parser while in the middle of
 92005  92481   ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
................................................................................
 92026  92502   **
 92027  92503   ** Substring     | Affinity
 92028  92504   ** --------------------------------
 92029  92505   ** 'INT'         | SQLITE_AFF_INTEGER
 92030  92506   ** 'CHAR'        | SQLITE_AFF_TEXT
 92031  92507   ** 'CLOB'        | SQLITE_AFF_TEXT
 92032  92508   ** 'TEXT'        | SQLITE_AFF_TEXT
 92033         -** 'BLOB'        | SQLITE_AFF_NONE
        92509  +** 'BLOB'        | SQLITE_AFF_BLOB
 92034  92510   ** 'REAL'        | SQLITE_AFF_REAL
 92035  92511   ** 'FLOA'        | SQLITE_AFF_REAL
 92036  92512   ** 'DOUB'        | SQLITE_AFF_REAL
 92037  92513   **
 92038  92514   ** If none of the substrings in the above table are found,
 92039  92515   ** SQLITE_AFF_NUMERIC is returned.
 92040  92516   */
................................................................................
 92052  92528         zChar = zIn;
 92053  92529       }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
 92054  92530         aff = SQLITE_AFF_TEXT;
 92055  92531       }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
 92056  92532         aff = SQLITE_AFF_TEXT;
 92057  92533       }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
 92058  92534           && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
 92059         -      aff = SQLITE_AFF_NONE;
        92535  +      aff = SQLITE_AFF_BLOB;
 92060  92536         if( zIn[0]=='(' ) zChar = zIn;
 92061  92537   #ifndef SQLITE_OMIT_FLOATING_POINT
 92062  92538       }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
 92063  92539           && aff==SQLITE_AFF_NUMERIC ){
 92064  92540         aff = SQLITE_AFF_REAL;
 92065  92541       }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
 92066  92542           && aff==SQLITE_AFF_NUMERIC ){
................................................................................
 92444  92920     }
 92445  92921     sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
 92446  92922     k = sqlite3Strlen30(zStmt);
 92447  92923     identPut(zStmt, &k, p->zName);
 92448  92924     zStmt[k++] = '(';
 92449  92925     for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
 92450  92926       static const char * const azType[] = {
 92451         -        /* SQLITE_AFF_NONE    */ "",
        92927  +        /* SQLITE_AFF_BLOB    */ "",
 92452  92928           /* SQLITE_AFF_TEXT    */ " TEXT",
 92453  92929           /* SQLITE_AFF_NUMERIC */ " NUM",
 92454  92930           /* SQLITE_AFF_INTEGER */ " INT",
 92455  92931           /* SQLITE_AFF_REAL    */ " REAL"
 92456  92932       };
 92457  92933       int len;
 92458  92934       const char *zType;
 92459  92935   
 92460  92936       sqlite3_snprintf(n-k, &zStmt[k], zSep);
 92461  92937       k += sqlite3Strlen30(&zStmt[k]);
 92462  92938       zSep = zSep2;
 92463  92939       identPut(zStmt, &k, pCol->zName);
 92464         -    assert( pCol->affinity-SQLITE_AFF_NONE >= 0 );
 92465         -    assert( pCol->affinity-SQLITE_AFF_NONE < ArraySize(azType) );
 92466         -    testcase( pCol->affinity==SQLITE_AFF_NONE );
        92940  +    assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
        92941  +    assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
        92942  +    testcase( pCol->affinity==SQLITE_AFF_BLOB );
 92467  92943       testcase( pCol->affinity==SQLITE_AFF_TEXT );
 92468  92944       testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
 92469  92945       testcase( pCol->affinity==SQLITE_AFF_INTEGER );
 92470  92946       testcase( pCol->affinity==SQLITE_AFF_REAL );
 92471  92947       
 92472         -    zType = azType[pCol->affinity - SQLITE_AFF_NONE];
        92948  +    zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
 92473  92949       len = sqlite3Strlen30(zType);
 92474         -    assert( pCol->affinity==SQLITE_AFF_NONE 
        92950  +    assert( pCol->affinity==SQLITE_AFF_BLOB 
 92475  92951               || pCol->affinity==sqlite3AffinityType(zType, 0) );
 92476  92952       memcpy(&zStmt[k], zType, len);
 92477  92953       k += len;
 92478  92954       assert( k<=n );
 92479  92955     }
 92480  92956     sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
 92481  92957     return zStmt;
................................................................................
 92820  93296         int addrInsLoop;    /* Top of the loop for inserting rows */
 92821  93297         Table *pSelTab;     /* A table that describes the SELECT results */
 92822  93298   
 92823  93299         regYield = ++pParse->nMem;
 92824  93300         regRec = ++pParse->nMem;
 92825  93301         regRowid = ++pParse->nMem;
 92826  93302         assert(pParse->nTab==1);
        93303  +      sqlite3MayAbort(pParse);
 92827  93304         sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
 92828  93305         sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
 92829  93306         pParse->nTab = 2;
 92830  93307         addrTop = sqlite3VdbeCurrentAddr(v) + 1;
 92831  93308         sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
 92832  93309         sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
 92833  93310         sqlite3Select(pParse, pSelect, &dest);
................................................................................
 94597  95074     int i;
 94598  95075     struct SrcList_item *pItem;
 94599  95076     if( pList==0 ) return;
 94600  95077     for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
 94601  95078       sqlite3DbFree(db, pItem->zDatabase);
 94602  95079       sqlite3DbFree(db, pItem->zName);
 94603  95080       sqlite3DbFree(db, pItem->zAlias);
 94604         -    sqlite3DbFree(db, pItem->zIndex);
        95081  +    sqlite3DbFree(db, pItem->zIndexedBy);
 94605  95082       sqlite3DeleteTable(db, pItem->pTab);
 94606  95083       sqlite3SelectDelete(db, pItem->pSelect);
 94607  95084       sqlite3ExprDelete(db, pItem->pOn);
 94608  95085       sqlite3IdListDelete(db, pItem->pUsing);
 94609  95086     }
 94610  95087     sqlite3DbFree(db, pList);
 94611  95088   }
................................................................................
 94670  95147   ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
 94671  95148   ** element of the source-list passed as the second argument.
 94672  95149   */
 94673  95150   SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
 94674  95151     assert( pIndexedBy!=0 );
 94675  95152     if( p && ALWAYS(p->nSrc>0) ){
 94676  95153       struct SrcList_item *pItem = &p->a[p->nSrc-1];
 94677         -    assert( pItem->notIndexed==0 && pItem->zIndex==0 );
        95154  +    assert( pItem->notIndexed==0 && pItem->zIndexedBy==0 );
 94678  95155       if( pIndexedBy->n==1 && !pIndexedBy->z ){
 94679  95156         /* A "NOT INDEXED" clause was supplied. See parse.y 
 94680  95157         ** construct "indexed_opt" for details. */
 94681  95158         pItem->notIndexed = 1;
 94682  95159       }else{
 94683         -      pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
        95160  +      pItem->zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
 94684  95161       }
 94685  95162     }
 94686  95163   }
 94687  95164   
 94688  95165   /*
 94689  95166   ** When building up a FROM clause in the parser, the join operator
 94690  95167   ** is initially attached to the left operand.  But the code generator
................................................................................
 96500  96977     int nCol;
 96501  96978   
 96502  96979     if( piPartIdxLabel ){
 96503  96980       if( pIdx->pPartIdxWhere ){
 96504  96981         *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
 96505  96982         pParse->iPartIdxTab = iDataCur;
 96506  96983         sqlite3ExprCachePush(pParse);
 96507         -      sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
 96508         -                         SQLITE_JUMPIFNULL);
        96984  +      sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
        96985  +                            SQLITE_JUMPIFNULL);
 96509  96986       }else{
 96510  96987         *piPartIdxLabel = 0;
 96511  96988       }
 96512  96989     }
 96513  96990     nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
 96514  96991     regBase = sqlite3GetTempRange(pParse, nCol);
 96515  96992     if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
................................................................................
 97117  97594     u8 matchOne;
 97118  97595     u8 matchSet;
 97119  97596     u8 noCase;
 97120  97597   };
 97121  97598   
 97122  97599   /*
 97123  97600   ** For LIKE and GLOB matching on EBCDIC machines, assume that every
 97124         -** character is exactly one byte in size.  Also, all characters are
 97125         -** able to participate in upper-case-to-lower-case mappings in EBCDIC
 97126         -** whereas only characters less than 0x80 do in ASCII.
        97601  +** character is exactly one byte in size.  Also, provde the Utf8Read()
        97602  +** macro for fast reading of the next character in the common case where
        97603  +** the next character is ASCII.
 97127  97604   */
 97128  97605   #if defined(SQLITE_EBCDIC)
 97129  97606   # define sqlite3Utf8Read(A)        (*((*A)++))
 97130         -# define GlobUpperToLower(A)       A = sqlite3UpperToLower[A]
 97131         -# define GlobUpperToLowerAscii(A)  A = sqlite3UpperToLower[A]
        97607  +# define Utf8Read(A)               (*(A++))
 97132  97608   #else
 97133         -# define GlobUpperToLower(A)       if( A<=0x7f ){ A = sqlite3UpperToLower[A]; }
 97134         -# define GlobUpperToLowerAscii(A)  A = sqlite3UpperToLower[A]
        97609  +# define Utf8Read(A)               (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
 97135  97610   #endif
 97136  97611   
 97137  97612   static const struct compareInfo globInfo = { '*', '?', '[', 0 };
 97138  97613   /* The correct SQL-92 behavior is for the LIKE operator to ignore
 97139  97614   ** case.  Thus  'a' LIKE 'A' would be true. */
 97140  97615   static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
 97141  97616   /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
................................................................................
 97169  97644   **      '%'       Matches any sequence of zero or more characters
 97170  97645   **
 97171  97646   ***     '_'       Matches any one character
 97172  97647   **
 97173  97648   **      Ec        Where E is the "esc" character and c is any other
 97174  97649   **                character, including '%', '_', and esc, match exactly c.
 97175  97650   **
 97176         -** The comments through this routine usually assume glob matching.
        97651  +** The comments within this routine usually assume glob matching.
 97177  97652   **
 97178  97653   ** This routine is usually quick, but can be N**2 in the worst case.
 97179  97654   */
 97180  97655   static int patternCompare(
 97181  97656     const u8 *zPattern,              /* The glob pattern */
 97182  97657     const u8 *zString,               /* The string to compare against the glob */
 97183  97658     const struct compareInfo *pInfo, /* Information about how to do the compare */
................................................................................
 97193  97668     /* The GLOB operator does not have an ESCAPE clause.  And LIKE does not
 97194  97669     ** have the matchSet operator.  So we either have to look for one or
 97195  97670     ** the other, never both.  Hence the single variable matchOther is used
 97196  97671     ** to store the one we have to look for.
 97197  97672     */
 97198  97673     matchOther = esc ? esc : pInfo->matchSet;
 97199  97674   
 97200         -  while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
        97675  +  while( (c = Utf8Read(zPattern))!=0 ){
 97201  97676       if( c==matchAll ){  /* Match "*" */
 97202  97677         /* Skip over multiple "*" characters in the pattern.  If there
 97203  97678         ** are also "?" characters, skip those as well, but consume a
 97204  97679         ** single character of the input string for each "?" skipped */
 97205         -      while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
 97206         -               || c == matchOne ){
        97680  +      while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
 97207  97681           if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
 97208  97682             return 0;
 97209  97683           }
 97210  97684         }
 97211  97685         if( c==0 ){
 97212  97686           return 1;   /* "*" at the end of the pattern matches */
 97213  97687         }else if( c==matchOther ){
................................................................................
 97244  97718             cx = c;
 97245  97719           }
 97246  97720           while( (c2 = *(zString++))!=0 ){
 97247  97721             if( c2!=c && c2!=cx ) continue;
 97248  97722             if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
 97249  97723           }
 97250  97724         }else{
 97251         -        while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
        97725  +        while( (c2 = Utf8Read(zString))!=0 ){
 97252  97726             if( c2!=c ) continue;
 97253  97727             if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
 97254  97728           }
 97255  97729         }
 97256  97730         return 0;
 97257  97731       }
 97258  97732       if( c==matchOther ){
................................................................................
 97290  97764           }
 97291  97765           if( c2==0 || (seen ^ invert)==0 ){
 97292  97766             return 0;
 97293  97767           }
 97294  97768           continue;
 97295  97769         }
 97296  97770       }
 97297         -    c2 = sqlite3Utf8Read(&zString);
        97771  +    c2 = Utf8Read(zString);
 97298  97772       if( c==c2 ) continue;
 97299  97773       if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){
 97300  97774         continue;
 97301  97775       }
 97302  97776       if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
 97303  97777       return 0;
 97304  97778     }
................................................................................
 99804 100278   /*
 99805 100279   ** Return a pointer to the column affinity string associated with index
 99806 100280   ** pIdx. A column affinity string has one character for each column in 
 99807 100281   ** the table, according to the affinity of the column:
 99808 100282   **
 99809 100283   **  Character      Column affinity
 99810 100284   **  ------------------------------
 99811         -**  'A'            NONE
       100285  +**  'A'            BLOB
 99812 100286   **  'B'            TEXT
 99813 100287   **  'C'            NUMERIC
 99814 100288   **  'D'            INTEGER
 99815 100289   **  'F'            REAL
 99816 100290   **
 99817 100291   ** An extra 'D' is appended to the end of the string to cover the
 99818 100292   ** rowid that appears as the last column in every index.
................................................................................
 99847 100321     }
 99848 100322    
 99849 100323     return pIdx->zColAff;
 99850 100324   }
 99851 100325   
 99852 100326   /*
 99853 100327   ** Compute the affinity string for table pTab, if it has not already been
 99854         -** computed.  As an optimization, omit trailing SQLITE_AFF_NONE affinities.
       100328  +** computed.  As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
 99855 100329   **
 99856         -** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values) and
       100330  +** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
 99857 100331   ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
 99858 100332   ** for register iReg and following.  Or if affinities exists and iReg==0,
 99859 100333   ** then just set the P4 operand of the previous opcode (which should  be
 99860 100334   ** an OP_MakeRecord) to the affinity string.
 99861 100335   **
 99862 100336   ** A column affinity string has one character per column:
 99863 100337   **
 99864 100338   **  Character      Column affinity
 99865 100339   **  ------------------------------
 99866         -**  'A'            NONE
       100340  +**  'A'            BLOB
 99867 100341   **  'B'            TEXT
 99868 100342   **  'C'            NUMERIC
 99869 100343   **  'D'            INTEGER
 99870 100344   **  'E'            REAL
 99871 100345   */
 99872 100346   SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
 99873 100347     int i;
................................................................................
 99881 100355       }
 99882 100356   
 99883 100357       for(i=0; i<pTab->nCol; i++){
 99884 100358         zColAff[i] = pTab->aCol[i].affinity;
 99885 100359       }
 99886 100360       do{
 99887 100361         zColAff[i--] = 0;
 99888         -    }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE );
       100362  +    }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
 99889 100363       pTab->zColAff = zColAff;
 99890 100364     }
 99891 100365     i = sqlite3Strlen30(zColAff);
 99892 100366     if( i ){
 99893 100367       if( iReg ){
 99894 100368         sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
 99895 100369       }else{
................................................................................
101129 101603       iThisCur = iIdxCur+ix;
101130 101604       addrUniqueOk = sqlite3VdbeMakeLabel(v);
101131 101605   
101132 101606       /* Skip partial indices for which the WHERE clause is not true */
101133 101607       if( pIdx->pPartIdxWhere ){
101134 101608         sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
101135 101609         pParse->ckBase = regNewData+1;
101136         -      sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
101137         -                         SQLITE_JUMPIFNULL);
       101610  +      sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
       101611  +                            SQLITE_JUMPIFNULL);
101138 101612         pParse->ckBase = 0;
101139 101613       }
101140 101614   
101141 101615       /* Create a record for this index entry as it should appear after
101142 101616       ** the insert or update.  Store that record in the aRegIdx[ix] register
101143 101617       */
101144 101618       regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);
................................................................................
102240 102714     void *(*realloc64)(void*,sqlite3_uint64);
102241 102715     void (*reset_auto_extension)(void);
102242 102716     void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
102243 102717                           void(*)(void*));
102244 102718     void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
102245 102719                            void(*)(void*), unsigned char);
102246 102720     int (*strglob)(const char*,const char*);
102247         -  sqlite3_value (*value_dup)(const sqlite3_value*);
       102721  +  /* Version 3.8.11 and later */
       102722  +  sqlite3_value *(*value_dup)(const sqlite3_value*);
102248 102723     void (*value_free)(sqlite3_value*);
102249 102724   };
102250 102725   
102251 102726   /*
102252 102727   ** The following macros redefine the API routines so that they are
102253 102728   ** redirected through the global sqlite3_api structure.
102254 102729   **
................................................................................
102880 103355     sqlite3_load_extension,
102881 103356     sqlite3_malloc64,
102882 103357     sqlite3_msize,
102883 103358     sqlite3_realloc64,
102884 103359     sqlite3_reset_auto_extension,
102885 103360     sqlite3_result_blob64,
102886 103361     sqlite3_result_text64,
102887         -  sqlite3_strglob
       103362  +  sqlite3_strglob,
       103363  +  /* Version 3.8.11 and later */
       103364  +  (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
       103365  +  sqlite3_value_free
102888 103366   };
102889 103367   
102890 103368   /*
102891 103369   ** Attempt to load an SQLite extension library contained in the file
102892 103370   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
102893 103371   ** default entry point name (sqlite3_extension_init) is used.  Use
102894 103372   ** of the default name is recommended.
................................................................................
106615 107093   /*
106616 107094   ** Trace output macros
106617 107095   */
106618 107096   #if SELECTTRACE_ENABLED
106619 107097   /***/ int sqlite3SelectTrace = 0;
106620 107098   # define SELECTTRACE(K,P,S,X)  \
106621 107099     if(sqlite3SelectTrace&(K))   \
106622         -    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\
       107100  +    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
       107101  +        (S)->zSelName,(S)),\
106623 107102       sqlite3DebugPrintf X
106624 107103   #else
106625 107104   # define SELECTTRACE(K,P,S,X)
106626 107105   #endif
106627 107106   
106628 107107   
106629 107108   /*
................................................................................
106959 107438   */
106960 107439   static void setJoinExpr(Expr *p, int iTable){
106961 107440     while( p ){
106962 107441       ExprSetProperty(p, EP_FromJoin);
106963 107442       assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
106964 107443       ExprSetVVAProperty(p, EP_NoReduce);
106965 107444       p->iRightJoinTable = (i16)iTable;
       107445  +    if( p->op==TK_FUNCTION && p->x.pList ){
       107446  +      int i;
       107447  +      for(i=0; i<p->x.pList->nExpr; i++){
       107448  +        setJoinExpr(p->x.pList->a[i].pExpr, iTable);
       107449  +      }
       107450  +    }
106966 107451       setJoinExpr(p->pLeft, iTable);
106967 107452       p = p->pRight;
106968 107453     } 
106969 107454   }
106970 107455   
106971 107456   /*
106972 107457   ** This routine processes the join information for a SELECT statement.
................................................................................
107368 107853         case WHERE_DISTINCT_UNIQUE: {
107369 107854           sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
107370 107855           break;
107371 107856         }
107372 107857   
107373 107858         default: {
107374 107859           assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
107375         -        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
       107860  +        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
       107861  +                     regResult);
107376 107862           break;
107377 107863         }
107378 107864       }
107379 107865       if( pSort==0 ){
107380 107866         codeOffset(v, p->iOffset, iContinue);
107381 107867       }
107382 107868     }
................................................................................
107421 107907         if( eDest==SRT_DistFifo ){
107422 107908           /* If the destination is DistFifo, then cursor (iParm+1) is open
107423 107909           ** on an ephemeral index. If the current row is already present
107424 107910           ** in the index, do not write it to the output. If not, add the
107425 107911           ** current row to the index and proceed with writing it to the
107426 107912           ** output table as well.  */
107427 107913           int addr = sqlite3VdbeCurrentAddr(v) + 4;
107428         -        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
       107914  +        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
       107915  +        VdbeCoverage(v);
107429 107916           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
107430 107917           assert( pSort==0 );
107431 107918         }
107432 107919   #endif
107433 107920         if( pSort ){
107434 107921           pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, 1, nPrefixReg);
107435 107922         }else{
................................................................................
107904 108391   ** The declaration type for any expression other than a column is NULL.
107905 108392   **
107906 108393   ** This routine has either 3 or 6 parameters depending on whether or not
107907 108394   ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
107908 108395   */
107909 108396   #ifdef SQLITE_ENABLE_COLUMN_METADATA
107910 108397   # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
       108398  +#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
       108399  +# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
       108400  +#endif
107911 108401   static const char *columnTypeImpl(
107912 108402     NameContext *pNC, 
107913 108403     Expr *pExpr,
       108404  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
107914 108405     const char **pzOrigDb,
107915 108406     const char **pzOrigTab,
107916 108407     const char **pzOrigCol,
       108408  +#endif
107917 108409     u8 *pEstWidth
107918 108410   ){
       108411  +  char const *zType = 0;
       108412  +  int j;
       108413  +  u8 estWidth = 1;
       108414  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
107919 108415     char const *zOrigDb = 0;
107920 108416     char const *zOrigTab = 0;
107921 108417     char const *zOrigCol = 0;
107922         -#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
107923         -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
107924         -static const char *columnTypeImpl(
107925         -  NameContext *pNC, 
107926         -  Expr *pExpr,
107927         -  u8 *pEstWidth
107928         -){
107929         -#endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
107930         -  char const *zType = 0;
107931         -  int j;
107932         -  u8 estWidth = 1;
       108418  +#endif
107933 108419   
107934 108420     if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
107935 108421     switch( pExpr->op ){
107936 108422       case TK_AGG_COLUMN:
107937 108423       case TK_COLUMN: {
107938 108424         /* The expression is a column. Locate the table the column is being
107939 108425         ** extracted from in NameContext.pSrcList. This table may be real
................................................................................
107978 108464   
107979 108465         assert( pTab && pExpr->pTab==pTab );
107980 108466         if( pS ){
107981 108467           /* The "table" is actually a sub-select or a view in the FROM clause
107982 108468           ** of the SELECT statement. Return the declaration type and origin
107983 108469           ** data for the result-set column of the sub-select.
107984 108470           */
107985         -        if( iCol>=0 && iCol<pS->pEList->nExpr ){
       108471  +        if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
107986 108472             /* If iCol is less than zero, then the expression requests the
107987 108473             ** rowid of the sub-select or view. This expression is legal (see 
107988 108474             ** test case misc2.2.2) - it always evaluates to NULL.
       108475  +          **
       108476  +          ** The ALWAYS() is because iCol>=pS->pEList->nExpr will have been
       108477  +          ** caught already by name resolution.
107989 108478             */
107990 108479             NameContext sNC;
107991 108480             Expr *p = pS->pEList->a[iCol].pExpr;
107992 108481             sNC.pSrcList = pS->pSrc;
107993 108482             sNC.pNext = pNC;
107994 108483             sNC.pParse = pNC->pParse;
107995 108484             zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth); 
................................................................................
108299 108788     if( db->mallocFailed ) return;
108300 108789     memset(&sNC, 0, sizeof(sNC));
108301 108790     sNC.pSrcList = pSelect->pSrc;
108302 108791     a = pSelect->pEList->a;
108303 108792     for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
108304 108793       p = a[i].pExpr;
108305 108794       if( pCol->zType==0 ){
108306         -      pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
       108795  +      pCol->zType = sqlite3DbStrDup(db, 
       108796  +                        columnType(&sNC, p,0,0,0, &pCol->szEst));
108307 108797       }
108308 108798       szAll += pCol->szEst;
108309 108799       pCol->affinity = sqlite3ExprAffinity(p);
108310         -    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
       108800  +    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
108311 108801       pColl = sqlite3ExprCollSeq(pParse, p);
108312 108802       if( pColl && pCol->zColl==0 ){
108313 108803         pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
108314 108804       }
108315 108805     }
108316 108806     pTab->szTabRow = sqlite3LogEst(szAll*4);
108317 108807   }
................................................................................
108459 108949     CollSeq *pRet;
108460 108950     if( p->pPrior ){
108461 108951       pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
108462 108952     }else{
108463 108953       pRet = 0;
108464 108954     }
108465 108955     assert( iCol>=0 );
108466         -  if( pRet==0 && iCol<p->pEList->nExpr ){
       108956  +  /* iCol must be less than p->pEList->nExpr.  Otherwise an error would
       108957  +  ** have been thrown during name resolution and we would not have gotten
       108958  +  ** this far */
       108959  +  if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
108467 108960       pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
108468 108961     }
108469 108962     return pRet;
108470 108963   }
108471 108964   
108472 108965   /*
108473 108966   ** The select statement passed as the second parameter is a compound SELECT
................................................................................
108678 109171     SelectDest *pDest     /* What to do with query results */
108679 109172   );
108680 109173   
108681 109174   /*
108682 109175   ** Error message for when two or more terms of a compound select have different
108683 109176   ** size result sets.
108684 109177   */
108685         -static void selectWrongNumTermsError(Parse *pParse, Select *p){
       109178  +SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
108686 109179     if( p->selFlags & SF_Values ){
108687 109180       sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
108688 109181     }else{
108689 109182       sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
108690 109183         " do not have the same number of result columns", selectOpName(p->op));
108691 109184     }
108692 109185   }
................................................................................
108704 109197   */
108705 109198   static int multiSelectValues(
108706 109199     Parse *pParse,        /* Parsing context */
108707 109200     Select *p,            /* The right-most of SELECTs to be coded */
108708 109201     SelectDest *pDest     /* What to do with query results */
108709 109202   ){
108710 109203     Select *pPrior;
108711         -  int nExpr = p->pEList->nExpr;
108712 109204     int nRow = 1;
108713 109205     int rc = 0;
108714 109206     assert( p->selFlags & SF_MultiValue );
108715 109207     do{
108716 109208       assert( p->selFlags & SF_Values );
108717 109209       assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
108718 109210       assert( p->pLimit==0 );
108719 109211       assert( p->pOffset==0 );
108720         -    if( p->pEList->nExpr!=nExpr ){
108721         -      selectWrongNumTermsError(pParse, p);
108722         -      return 1;
108723         -    }
       109212  +    assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
108724 109213       if( p->pPrior==0 ) break;
108725 109214       assert( p->pPrior->pNext==p );
108726 109215       p = p->pPrior;
108727 109216       nRow++;
108728 109217     }while(1);
108729 109218     while( p ){
108730 109219       pPrior = p->pPrior;
................................................................................
108825 109314       goto multi_select_end;
108826 109315     }
108827 109316   
108828 109317     /* Make sure all SELECTs in the statement have the same number of elements
108829 109318     ** in their result sets.
108830 109319     */
108831 109320     assert( p->pEList && pPrior->pEList );
108832         -  if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
108833         -    selectWrongNumTermsError(pParse, p);
108834         -    rc = 1;
108835         -    goto multi_select_end;
108836         -  }
       109321  +  assert( p->pEList->nExpr==pPrior->pEList->nExpr );
108837 109322   
108838 109323   #ifndef SQLITE_OMIT_CTE
108839 109324     if( p->selFlags & SF_Recursive ){
108840 109325       generateWithRecursiveQuery(pParse, p, &dest);
108841 109326     }else
108842 109327   #endif
108843 109328   
................................................................................
109448 109933     ** collation.
109449 109934     */
109450 109935     aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
109451 109936     if( aPermute ){
109452 109937       struct ExprList_item *pItem;
109453 109938       for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
109454 109939         assert( pItem->u.x.iOrderByCol>0 );
109455         -      /* assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ) is also true
109456         -      ** but only for well-formed SELECT statements. */
109457         -      testcase( pItem->u.x.iOrderByCol > p->pEList->nExpr );
       109940  +      assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
109458 109941         aPermute[i] = pItem->u.x.iOrderByCol - 1;
109459 109942       }
109460 109943       pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
109461 109944     }else{
109462 109945       pKeyMerge = 0;
109463 109946     }
109464 109947   
................................................................................
109809 110292   **   (8)  The subquery does not use LIMIT or the outer query is not a join.
109810 110293   **
109811 110294   **   (9)  The subquery does not use LIMIT or the outer query does not use
109812 110295   **        aggregates.
109813 110296   **
109814 110297   **  (**)  Restriction (10) was removed from the code on 2005-02-05 but we
109815 110298   **        accidently carried the comment forward until 2014-09-15.  Original
109816         -**        text: "The subquery does not use aggregates or the outer query does not
109817         -**        use LIMIT."
       110299  +**        text: "The subquery does not use aggregates or the outer query 
       110300  +**        does not use LIMIT."
109818 110301   **
109819 110302   **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
109820 110303   **
109821 110304   **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
109822 110305   **        a separate restriction deriving from ticket #350.
109823 110306   **
109824 110307   **  (13)  The subquery and outer query do not both use LIMIT.
................................................................................
110020 110503       if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
110021 110504         return 0;
110022 110505       }
110023 110506       for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
110024 110507         testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
110025 110508         testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
110026 110509         assert( pSub->pSrc!=0 );
       110510  +      assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
110027 110511         if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
110028 110512          || (pSub1->pPrior && pSub1->op!=TK_ALL) 
110029 110513          || pSub1->pSrc->nSrc<1
110030         -       || pSub->pEList->nExpr!=pSub1->pEList->nExpr
110031 110514         ){
110032 110515           return 0;
110033 110516         }
110034 110517         testcase( pSub1->pSrc->nSrc>1 );
110035 110518       }
110036 110519   
110037 110520       /* Restriction 18. */
................................................................................
110303 110786     /* Finially, delete what is left of the subquery and return
110304 110787     ** success.
110305 110788     */
110306 110789     sqlite3SelectDelete(db, pSub1);
110307 110790   
110308 110791   #if SELECTTRACE_ENABLED
110309 110792     if( sqlite3SelectTrace & 0x100 ){
110310         -    sqlite3DebugPrintf("After flattening:\n");
       110793  +    SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
110311 110794       sqlite3TreeViewSelect(0, p, 0);
110312 110795     }
110313 110796   #endif
110314 110797   
110315 110798     return 1;
       110799  +}
       110800  +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
       110801  +
       110802  +
       110803  +
       110804  +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
       110805  +/*
       110806  +** Make copies of relevant WHERE clause terms of the outer query into
       110807  +** the WHERE clause of subquery.  Example:
       110808  +**
       110809  +**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
       110810  +**
       110811  +** Transformed into:
       110812  +**
       110813  +**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
       110814  +**     WHERE x=5 AND y=10;
       110815  +**
       110816  +** The hope is that the terms added to the inner query will make it more
       110817  +** efficient.
       110818  +**
       110819  +** Do not attempt this optimization if:
       110820  +**
       110821  +**   (1) The inner query is an aggregate.  (In that case, we'd really want
       110822  +**       to copy the outer WHERE-clause terms onto the HAVING clause of the
       110823  +**       inner query.  But they probably won't help there so do not bother.)
       110824  +**
       110825  +**   (2) The inner query is the recursive part of a common table expression.
       110826  +**
       110827  +**   (3) The inner query has a LIMIT clause (since the changes to the WHERE
       110828  +**       close would change the meaning of the LIMIT).
       110829  +**
       110830  +**   (4) The inner query is the right operand of a LEFT JOIN.  (The caller
       110831  +**       enforces this restriction since this routine does not have enough
       110832  +**       information to know.)
       110833  +**
       110834  +** Return 0 if no changes are made and non-zero if one or more WHERE clause
       110835  +** terms are duplicated into the subquery.
       110836  +*/
       110837  +static int pushDownWhereTerms(
       110838  +  sqlite3 *db,          /* The database connection (for malloc()) */
       110839  +  Select *pSubq,        /* The subquery whose WHERE clause is to be augmented */
       110840  +  Expr *pWhere,         /* The WHERE clause of the outer query */
       110841  +  int iCursor           /* Cursor number of the subquery */
       110842  +){
       110843  +  Expr *pNew;
       110844  +  int nChng = 0;
       110845  +  if( pWhere==0 ) return 0;
       110846  +  if( (pSubq->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){
       110847  +     return 0; /* restrictions (1) and (2) */
       110848  +  }
       110849  +  if( pSubq->pLimit!=0 ){
       110850  +     return 0; /* restriction (3) */
       110851  +  }
       110852  +  while( pWhere->op==TK_AND ){
       110853  +    nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor);
       110854  +    pWhere = pWhere->pLeft;
       110855  +  }
       110856  +  if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
       110857  +    nChng++;
       110858  +    while( pSubq ){
       110859  +      pNew = sqlite3ExprDup(db, pWhere, 0);
       110860  +      pNew = substExpr(db, pNew, iCursor, pSubq->pEList);
       110861  +      pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew);
       110862  +      pSubq = pSubq->pPrior;
       110863  +    }
       110864  +  }
       110865  +  return nChng;
110316 110866   }
110317 110867   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
110318 110868   
110319 110869   /*
110320 110870   ** Based on the contents of the AggInfo structure indicated by the first
110321 110871   ** argument, this function checks if the following are true:
110322 110872   **
................................................................................
110395 110945   ** If the source-list item passed as an argument was augmented with an
110396 110946   ** INDEXED BY clause, then try to locate the specified index. If there
110397 110947   ** was such a clause and the named index cannot be found, return 
110398 110948   ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate 
110399 110949   ** pFrom->pIndex and return SQLITE_OK.
110400 110950   */
110401 110951   SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
110402         -  if( pFrom->pTab && pFrom->zIndex ){
       110952  +  if( pFrom->pTab && pFrom->zIndexedBy ){
110403 110953       Table *pTab = pFrom->pTab;
110404         -    char *zIndex = pFrom->zIndex;
       110954  +    char *zIndexedBy = pFrom->zIndexedBy;
110405 110955       Index *pIdx;
110406 110956       for(pIdx=pTab->pIndex; 
110407         -        pIdx && sqlite3StrICmp(pIdx->zName, zIndex); 
       110957  +        pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy); 
110408 110958           pIdx=pIdx->pNext
110409 110959       );
110410 110960       if( !pIdx ){
110411         -      sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
       110961  +      sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
110412 110962         pParse->checkSchema = 1;
110413 110963         return SQLITE_ERROR;
110414 110964       }
110415 110965       pFrom->pIndex = pIdx;
110416 110966     }
110417 110967     return SQLITE_OK;
110418 110968   }
................................................................................
111341 111891       p->pOrderBy = 0;
111342 111892       p->selFlags &= ~SF_Distinct;
111343 111893     }
111344 111894     sqlite3SelectPrep(pParse, p, 0);
111345 111895     memset(&sSort, 0, sizeof(sSort));
111346 111896     sSort.pOrderBy = p->pOrderBy;
111347 111897     pTabList = p->pSrc;
111348         -  pEList = p->pEList;
111349 111898     if( pParse->nErr || db->mallocFailed ){
111350 111899       goto select_end;
111351 111900     }
       111901  +  assert( p->pEList!=0 );
111352 111902     isAgg = (p->selFlags & SF_Aggregate)!=0;
111353         -  assert( pEList!=0 );
111354 111903   #if SELECTTRACE_ENABLED
111355 111904     if( sqlite3SelectTrace & 0x100 ){
111356 111905       SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
111357 111906       sqlite3TreeViewSelect(0, p, 0);
111358 111907     }
111359 111908   #endif
111360 111909   
111361 111910   
111362         -  /* Begin generating code.
111363         -  */
111364         -  v = sqlite3GetVdbe(pParse);
111365         -  if( v==0 ) goto select_end;
111366         -
111367 111911     /* If writing to memory or generating a set
111368 111912     ** only a single column may be output.
111369 111913     */
111370 111914   #ifndef SQLITE_OMIT_SUBQUERY
111371         -  if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
       111915  +  if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){
111372 111916       goto select_end;
111373 111917     }
111374 111918   #endif
       111919  +
       111920  +  /* Try to flatten subqueries in the FROM clause up into the main query
       111921  +  */
       111922  +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
       111923  +  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
       111924  +    struct SrcList_item *pItem = &pTabList->a[i];
       111925  +    Select *pSub = pItem->pSelect;
       111926  +    int isAggSub;
       111927  +    if( pSub==0 ) continue;
       111928  +    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
       111929  +    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
       111930  +      /* This subquery can be absorbed into its parent. */
       111931  +      if( isAggSub ){
       111932  +        isAgg = 1;
       111933  +        p->selFlags |= SF_Aggregate;
       111934  +      }
       111935  +      i = -1;
       111936  +    }
       111937  +    pTabList = p->pSrc;
       111938  +    if( db->mallocFailed ) goto select_end;
       111939  +    if( !IgnorableOrderby(pDest) ){
       111940  +      sSort.pOrderBy = p->pOrderBy;
       111941  +    }
       111942  +  }
       111943  +#endif
       111944  +
       111945  +  /* Get a pointer the VDBE under construction, allocating a new VDBE if one
       111946  +  ** does not already exist */
       111947  +  v = sqlite3GetVdbe(pParse);
       111948  +  if( v==0 ) goto select_end;
       111949  +
       111950  +#ifndef SQLITE_OMIT_COMPOUND_SELECT
       111951  +  /* Handle compound SELECT statements using the separate multiSelect()
       111952  +  ** procedure.
       111953  +  */
       111954  +  if( p->pPrior ){
       111955  +    rc = multiSelect(pParse, p, pDest);
       111956  +    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
       111957  +#if SELECTTRACE_ENABLED
       111958  +    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
       111959  +    pParse->nSelectIndent--;
       111960  +#endif
       111961  +    return rc;
       111962  +  }
       111963  +#endif
111375 111964   
111376 111965     /* Generate code for all sub-queries in the FROM clause
111377 111966     */
111378 111967   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
111379         -  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
       111968  +  for(i=0; i<pTabList->nSrc; i++){
111380 111969       struct SrcList_item *pItem = &pTabList->a[i];
111381 111970       SelectDest dest;
111382 111971       Select *pSub = pItem->pSelect;
111383         -    int isAggSub;
111384         -
111385 111972       if( pSub==0 ) continue;
111386 111973   
111387 111974       /* Sometimes the code for a subquery will be generated more than
111388 111975       ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
111389 111976       ** for example.  In that case, do not regenerate the code to manifest
111390 111977       ** a view or the co-routine to implement a view.  The first instance
111391 111978       ** is sufficient, though the subroutine to manifest the view does need
................................................................................
111402 111989       ** may contain expression trees of at most
111403 111990       ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
111404 111991       ** more conservative than necessary, but much easier than enforcing
111405 111992       ** an exact limit.
111406 111993       */
111407 111994       pParse->nHeight += sqlite3SelectExprHeight(p);
111408 111995   
111409         -    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
111410         -    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
111411         -      /* This subquery can be absorbed into its parent. */
111412         -      if( isAggSub ){
111413         -        isAgg = 1;
111414         -        p->selFlags |= SF_Aggregate;
       111996  +    /* Make copies of constant WHERE-clause terms in the outer query down
       111997  +    ** inside the subquery.  This can help the subquery to run more efficiently.
       111998  +    */
       111999  +    if( (pItem->jointype & JT_OUTER)==0
       112000  +     && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor)
       112001  +    ){
       112002  +#if SELECTTRACE_ENABLED
       112003  +      if( sqlite3SelectTrace & 0x100 ){
       112004  +        SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
       112005  +        sqlite3TreeViewSelect(0, p, 0);
111415 112006         }
111416         -      i = -1;
111417         -    }else if( pTabList->nSrc==1
111418         -           && (p->selFlags & SF_All)==0
111419         -           && OptimizationEnabled(db, SQLITE_SubqCoroutine)
       112007  +#endif
       112008  +    }
       112009  +
       112010  +    /* Generate code to implement the subquery
       112011  +    */
       112012  +    if( pTabList->nSrc==1
       112013  +     && (p->selFlags & SF_All)==0
       112014  +     && OptimizationEnabled(db, SQLITE_SubqCoroutine)
111420 112015       ){
111421 112016         /* Implement a co-routine that will return a single row of the result
111422 112017         ** set on each invocation.
111423 112018         */
111424 112019         int addrTop = sqlite3VdbeCurrentAddr(v)+1;
111425 112020         pItem->regReturn = ++pParse->nMem;
111426 112021         sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
................................................................................
111463 112058         pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
111464 112059         if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
111465 112060         retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
111466 112061         VdbeComment((v, "end %s", pItem->pTab->zName));
111467 112062         sqlite3VdbeChangeP1(v, topAddr, retAddr);
111468 112063         sqlite3ClearTempRegCache(pParse);
111469 112064       }
111470         -    if( /*pParse->nErr ||*/ db->mallocFailed ){
111471         -      goto select_end;
111472         -    }
       112065  +    if( db->mallocFailed ) goto select_end;
111473 112066       pParse->nHeight -= sqlite3SelectExprHeight(p);
111474         -    pTabList = p->pSrc;
111475         -    if( !IgnorableOrderby(pDest) ){
111476         -      sSort.pOrderBy = p->pOrderBy;
111477         -    }
111478 112067     }
       112068  +#endif
       112069  +
       112070  +  /* Various elements of the SELECT copied into local variables for
       112071  +  ** convenience */
111479 112072     pEList = p->pEList;
111480         -#endif
111481 112073     pWhere = p->pWhere;
111482 112074     pGroupBy = p->pGroupBy;
111483 112075     pHaving = p->pHaving;
111484 112076     sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
111485 112077   
111486         -#ifndef SQLITE_OMIT_COMPOUND_SELECT
111487         -  /* If there is are a sequence of queries, do the earlier ones first.
111488         -  */
111489         -  if( p->pPrior ){
111490         -    rc = multiSelect(pParse, p, pDest);
111491         -    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
111492 112078   #if SELECTTRACE_ENABLED
111493         -    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
111494         -    pParse->nSelectIndent--;
111495         -#endif
111496         -    return rc;
       112079  +  if( sqlite3SelectTrace & 0x400 ){
       112080  +    SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
       112081  +    sqlite3TreeViewSelect(0, p, 0);
111497 112082     }
111498 112083   #endif
111499 112084   
111500 112085     /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
111501 112086     ** if the select-list is the same as the ORDER BY list, then this query
111502 112087     ** can be rewritten as a GROUP BY. In other words, this:
111503 112088     **
................................................................................
111509 112094     **
111510 112095     ** The second form is preferred as a single index (or temp-table) may be 
111511 112096     ** used for both the ORDER BY and DISTINCT processing. As originally 
111512 112097     ** written the query must use a temp-table for at least one of the ORDER 
111513 112098     ** BY and DISTINCT, and an index or separate temp-table for the other.
111514 112099     */
111515 112100     if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
111516         -   && sqlite3ExprListCompare(sSort.pOrderBy, p->pEList, -1)==0
       112101  +   && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
111517 112102     ){
111518 112103       p->selFlags &= ~SF_Distinct;
111519         -    p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
111520         -    pGroupBy = p->pGroupBy;
       112104  +    pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
111521 112105       /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
111522 112106       ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
111523 112107       ** original setting of the SF_Distinct flag, not the current setting */
111524 112108       assert( sDistinct.isTnct );
111525 112109     }
111526 112110   
111527         -  /* If there is an ORDER BY clause, then this sorting
111528         -  ** index might end up being unused if the data can be 
111529         -  ** extracted in pre-sorted order.  If that is the case, then the
111530         -  ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
111531         -  ** we figure out that the sorting index is not needed.  The addrSortIndex
111532         -  ** variable is used to facilitate that change.
       112111  +  /* If there is an ORDER BY clause, then create an ephemeral index to
       112112  +  ** do the sorting.  But this sorting ephemeral index might end up
       112113  +  ** being unused if the data can be extracted in pre-sorted order.
       112114  +  ** If that is the case, then the OP_OpenEphemeral instruction will be
       112115  +  ** changed to an OP_Noop once we figure out that the sorting index is
       112116  +  ** not needed.  The sSort.addrSortIndex variable is used to facilitate
       112117  +  ** that change.
111533 112118     */
111534 112119     if( sSort.pOrderBy ){
111535 112120       KeyInfo *pKeyInfo;
111536 112121       pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
111537 112122       sSort.iECursor = pParse->nTab++;
111538 112123       sSort.addrSortIndex =
111539 112124         sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
................................................................................
111556 112141     p->nSelectRow = LARGEST_INT64;
111557 112142     computeLimitRegisters(pParse, p, iEnd);
111558 112143     if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
111559 112144       sqlite3VdbeGetOp(v, sSort.addrSortIndex)->opcode = OP_SorterOpen;
111560 112145       sSort.sortFlags |= SORTFLAG_UseSorter;
111561 112146     }
111562 112147   
111563         -  /* Open a virtual index to use for the distinct set.
       112148  +  /* Open an ephemeral index to use for the distinct set.
111564 112149     */
111565 112150     if( p->selFlags & SF_Distinct ){
111566 112151       sDistinct.tabTnct = pParse->nTab++;
111567 112152       sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
111568         -                                sDistinct.tabTnct, 0, 0,
111569         -                                (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
111570         -                                P4_KEYINFO);
       112153  +                             sDistinct.tabTnct, 0, 0,
       112154  +                             (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
       112155  +                             P4_KEYINFO);
111571 112156       sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
111572 112157       sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
111573 112158     }else{
111574 112159       sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
111575 112160     }
111576 112161   
111577 112162     if( !isAgg && pGroupBy==0 ){
................................................................................
111641 112226           pItem->u.x.iAlias = 0;
111642 112227         }
111643 112228         if( p->nSelectRow>100 ) p->nSelectRow = 100;
111644 112229       }else{
111645 112230         p->nSelectRow = 1;
111646 112231       }
111647 112232   
111648         -
111649 112233       /* If there is both a GROUP BY and an ORDER BY clause and they are
111650 112234       ** identical, then it may be possible to disable the ORDER BY clause 
111651 112235       ** on the grounds that the GROUP BY will cause elements to come out 
111652         -    ** in the correct order. It also may not - the GROUP BY may use a
       112236  +    ** in the correct order. It also may not - the GROUP BY might use a
111653 112237       ** database index that causes rows to be grouped together as required
111654 112238       ** but not actually sorted. Either way, record the fact that the
111655 112239       ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
111656 112240       ** variable.  */
111657 112241       if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
111658 112242         orderByGrp = 1;
111659 112243       }
................................................................................
111823 112407         ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
111824 112408         ** Then compare the current GROUP BY terms against the GROUP BY terms
111825 112409         ** from the previous row currently stored in a0, a1, a2...
111826 112410         */
111827 112411         addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
111828 112412         sqlite3ExprCacheClear(pParse);
111829 112413         if( groupBySort ){
111830         -        sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, sortOut,sortPTab);
       112414  +        sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
       112415  +                          sortOut, sortPTab);
111831 112416         }
111832 112417         for(j=0; j<pGroupBy->nExpr; j++){
111833 112418           if( groupBySort ){
111834 112419             sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
111835 112420           }else{
111836 112421             sAggInfo.directMode = 1;
111837 112422             sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
................................................................................
111895 112480         */
111896 112481         addrSetAbort = sqlite3VdbeCurrentAddr(v);
111897 112482         sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
111898 112483         VdbeComment((v, "set abort flag"));
111899 112484         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
111900 112485         sqlite3VdbeResolveLabel(v, addrOutputRow);
111901 112486         addrOutputRow = sqlite3VdbeCurrentAddr(v);
111902         -      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v);
       112487  +      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
       112488  +      VdbeCoverage(v);
111903 112489         VdbeComment((v, "Groupby result generator entry point"));
111904 112490         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
111905 112491         finalizeAggFunctions(pParse, &sAggInfo);
111906 112492         sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
111907 112493         selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
111908 112494                         &sDistinct, pDest,
111909 112495                         addrOutputRow+1, addrSetAbort);
................................................................................
112059 112645       explainTempTable(pParse, "DISTINCT");
112060 112646     }
112061 112647   
112062 112648     /* If there is an ORDER BY clause, then we need to sort the results
112063 112649     ** and send them to the callback one by one.
112064 112650     */
112065 112651     if( sSort.pOrderBy ){
112066         -    explainTempTable(pParse, sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
       112652  +    explainTempTable(pParse,
       112653  +                     sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
112067 112654       generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
112068 112655     }
112069 112656   
112070 112657     /* Jump here to skip this query
112071 112658     */
112072 112659     sqlite3VdbeResolveLabel(v, iEnd);
112073 112660   
................................................................................
112092 112679   #if SELECTTRACE_ENABLED
112093 112680     SELECTTRACE(1,pParse,p,("end processing\n"));
112094 112681     pParse->nSelectIndent--;
112095 112682   #endif
112096 112683     return rc;
112097 112684   }
112098 112685   
112099         -#ifdef SQLITE_DEBUG
112100         -/*
112101         -** Generate a human-readable description of a the Select object.
112102         -*/
112103         -SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
112104         -  int n = 0;
112105         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
112106         -  sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
112107         -    ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
112108         -    ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
112109         -  );
112110         -  if( p->pSrc && p->pSrc->nSrc ) n++;
112111         -  if( p->pWhere ) n++;
112112         -  if( p->pGroupBy ) n++;
112113         -  if( p->pHaving ) n++;
112114         -  if( p->pOrderBy ) n++;
112115         -  if( p->pLimit ) n++;
112116         -  if( p->pOffset ) n++;
112117         -  if( p->pPrior ) n++;
112118         -  sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
112119         -  if( p->pSrc && p->pSrc->nSrc ){
112120         -    int i;
112121         -    pView = sqlite3TreeViewPush(pView, (n--)>0);
112122         -    sqlite3TreeViewLine(pView, "FROM");
112123         -    for(i=0; i<p->pSrc->nSrc; i++){
112124         -      struct SrcList_item *pItem = &p->pSrc->a[i];
112125         -      StrAccum x;
112126         -      char zLine[100];
112127         -      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
112128         -      sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
112129         -      if( pItem->zDatabase ){
112130         -        sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
112131         -      }else if( pItem->zName ){
112132         -        sqlite3XPrintf(&x, 0, " %s", pItem->zName);
112133         -      }
112134         -      if( pItem->pTab ){
112135         -        sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
112136         -      }
112137         -      if( pItem->zAlias ){
112138         -        sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
112139         -      }
112140         -      if( pItem->jointype & JT_LEFT ){
112141         -        sqlite3XPrintf(&x, 0, " LEFT-JOIN");
112142         -      }
112143         -      sqlite3StrAccumFinish(&x);
112144         -      sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
112145         -      if( pItem->pSelect ){
112146         -        sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
112147         -      }
112148         -      sqlite3TreeViewPop(pView);
112149         -    }
112150         -    sqlite3TreeViewPop(pView);
112151         -  }
112152         -  if( p->pWhere ){
112153         -    sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
112154         -    sqlite3TreeViewExpr(pView, p->pWhere, 0);
112155         -    sqlite3TreeViewPop(pView);
112156         -  }
112157         -  if( p->pGroupBy ){
112158         -    sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
112159         -  }
112160         -  if( p->pHaving ){
112161         -    sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
112162         -    sqlite3TreeViewExpr(pView, p->pHaving, 0);
112163         -    sqlite3TreeViewPop(pView);
112164         -  }
112165         -  if( p->pOrderBy ){
112166         -    sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
112167         -  }
112168         -  if( p->pLimit ){
112169         -    sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
112170         -    sqlite3TreeViewExpr(pView, p->pLimit, 0);
112171         -    sqlite3TreeViewPop(pView);
112172         -  }
112173         -  if( p->pOffset ){
112174         -    sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
112175         -    sqlite3TreeViewExpr(pView, p->pOffset, 0);
112176         -    sqlite3TreeViewPop(pView);
112177         -  }
112178         -  if( p->pPrior ){
112179         -    const char *zOp = "UNION";
112180         -    switch( p->op ){
112181         -      case TK_ALL:         zOp = "UNION ALL";  break;
112182         -      case TK_INTERSECT:   zOp = "INTERSECT";  break;
112183         -      case TK_EXCEPT:      zOp = "EXCEPT";     break;
112184         -    }
112185         -    sqlite3TreeViewItem(pView, zOp, (n--)>0);
112186         -    sqlite3TreeViewSelect(pView, p->pPrior, 0);
112187         -    sqlite3TreeViewPop(pView);
112188         -  }
112189         -  sqlite3TreeViewPop(pView);
112190         -}
112191         -#endif /* SQLITE_DEBUG */
112192         -
112193 112686   /************** End of select.c **********************************************/
112194 112687   /************** Begin file table.c *******************************************/
112195 112688   /*
112196 112689   ** 2001 September 15
112197 112690   **
112198 112691   ** The author disclaims copyright to this source code.  In place of
112199 112692   ** a legal notice, here is a blessing:
................................................................................
115810 116303     sqlite3_mutex_leave(db->mutex);
115811 116304     return rc;
115812 116305   }
115813 116306   
115814 116307   #endif /* SQLITE_OMIT_VIRTUALTABLE */
115815 116308   
115816 116309   /************** End of vtab.c ************************************************/
115817         -/************** Begin file where.c *******************************************/
       116310  +/************** Begin file wherecode.c ***************************************/
115818 116311   /*
115819         -** 2001 September 15
       116312  +** 2015-06-06
115820 116313   **
115821 116314   ** The author disclaims copyright to this source code.  In place of
115822 116315   ** a legal notice, here is a blessing:
115823 116316   **
115824 116317   **    May you do good and not evil.
115825 116318   **    May you find forgiveness for yourself and forgive others.
115826 116319   **    May you share freely, never taking more than you give.
115827 116320   **
115828 116321   *************************************************************************
115829 116322   ** This module contains C code that generates VDBE code used to process
115830         -** the WHERE clause of SQL statements.  This module is responsible for
115831         -** generating the code that loops through a table looking for applicable
115832         -** rows.  Indices are selected and used to speed the search when doing
115833         -** so is applicable.  Because this module is responsible for selecting
115834         -** indices, you might also think of this module as the "query optimizer".
       116323  +** the WHERE clause of SQL statements.
       116324  +**
       116325  +** This file was split off from where.c on 2015-06-06 in order to reduce the
       116326  +** size of where.c and make it easier to edit.  This file contains the routines
       116327  +** that actually generate the bulk of the WHERE loop code.  The original where.c
       116328  +** file retains the code that does query planning and analysis.
115835 116329   */
115836         -/************** Include whereInt.h in the middle of where.c ******************/
       116330  +/************** Include whereInt.h in the middle of wherecode.c **************/
115837 116331   /************** Begin file whereInt.h ****************************************/
115838 116332   /*
115839 116333   ** 2013-11-12
115840 116334   **
115841 116335   ** The author disclaims copyright to this source code.  In place of
115842 116336   ** a legal notice, here is a blessing:
115843 116337   **
................................................................................
115852 116346   ** a separate source file for easier editing.
115853 116347   */
115854 116348   
115855 116349   /*
115856 116350   ** Trace output macros
115857 116351   */
115858 116352   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
115859         -/***/ int sqlite3WhereTrace = 0;
       116353  +/***/ int sqlite3WhereTrace;
115860 116354   #endif
115861 116355   #if defined(SQLITE_DEBUG) \
115862 116356       && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
115863 116357   # define WHERETRACE(K,X)  if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
115864 116358   # define WHERETRACE_ENABLED 1
115865 116359   #else
115866 116360   # define WHERETRACE(K,X)
................................................................................
115994 116488   */
115995 116489   #define N_OR_COST 3
115996 116490   struct WhereOrSet {
115997 116491     u16 n;                      /* Number of valid a[] entries */
115998 116492     WhereOrCost a[N_OR_COST];   /* Set of best costs */
115999 116493   };
116000 116494   
116001         -
116002         -/* Forward declaration of methods */
116003         -static int whereLoopResize(sqlite3*, WhereLoop*, int);
116004         -
116005 116495   /*
116006 116496   ** Each instance of this object holds a sequence of WhereLoop objects
116007 116497   ** that implement some or all of a query plan.
116008 116498   **
116009 116499   ** Think of each WhereLoop object as a node in a graph with arcs
116010 116500   ** showing dependencies and costs for travelling between nodes.  (That is
116011 116501   ** not a completely accurate description because WhereLoop costs are a
................................................................................
116205 116695   ** no gaps.
116206 116696   */
116207 116697   struct WhereMaskSet {
116208 116698     int n;                        /* Number of assigned cursor values */
116209 116699     int ix[BMS];                  /* Cursor assigned to each bit */
116210 116700   };
116211 116701   
       116702  +/*
       116703  +** Initialize a WhereMaskSet object
       116704  +*/
       116705  +#define initMaskSet(P)  (P)->n=0
       116706  +
116212 116707   /*
116213 116708   ** This object is a convenience wrapper holding all information needed
116214 116709   ** to construct WhereLoop objects for a particular query.
116215 116710   */
116216 116711   struct WhereLoopBuilder {
116217 116712     WhereInfo *pWInfo;        /* Information about this WHERE */
116218 116713     WhereClause *pWC;         /* WHERE clause terms */
................................................................................
116255 116750     int iBreak;               /* Jump here to break out of the loop */
116256 116751     int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
116257 116752     int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
116258 116753     WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
116259 116754     WhereClause sWC;          /* Decomposition of the WHERE clause */
116260 116755     WhereLevel a[1];          /* Information about each nest loop in WHERE */
116261 116756   };
       116757  +
       116758  +/*
       116759  +** Private interfaces - callable only by other where.c routines.
       116760  +**
       116761  +** where.c:
       116762  +*/
       116763  +SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
       116764  +SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
       116765  +  WhereClause *pWC,     /* The WHERE clause to be searched */
       116766  +  int iCur,             /* Cursor number of LHS */
       116767  +  int iColumn,          /* Column number of LHS */
       116768  +  Bitmask notReady,     /* RHS must not overlap with this mask */
       116769  +  u32 op,               /* Mask of WO_xx values describing operator */
       116770  +  Index *pIdx           /* Must be compatible with this index, if not NULL */
       116771  +);
       116772  +
       116773  +/* wherecode.c: */
       116774  +#ifndef SQLITE_OMIT_EXPLAIN
       116775  +SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
       116776  +  Parse *pParse,                  /* Parse context */
       116777  +  SrcList *pTabList,              /* Table list this loop refers to */
       116778  +  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
       116779  +  int iLevel,                     /* Value for "level" column of output */
       116780  +  int iFrom,                      /* Value for "from" column of output */
       116781  +  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
       116782  +);
       116783  +#else
       116784  +# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0
       116785  +#endif /* SQLITE_OMIT_EXPLAIN */
       116786  +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
       116787  +SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
       116788  +  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
       116789  +  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
       116790  +  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
       116791  +  int addrExplain                 /* Address of OP_Explain (or 0) */
       116792  +);
       116793  +#else
       116794  +# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
       116795  +#endif
       116796  +SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
       116797  +  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
       116798  +  int iLevel,          /* Which level of pWInfo->a[] should be coded */
       116799  +  Bitmask notReady     /* Which tables are currently available */
       116800  +);
       116801  +
       116802  +/* whereexpr.c: */
       116803  +SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
       116804  +SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
       116805  +SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
       116806  +SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
       116807  +SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
       116808  +SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
       116809  +
       116810  +
       116811  +
       116812  +
116262 116813   
116263 116814   /*
116264 116815   ** Bitmasks for the operators on WhereTerm objects.  These are all
116265 116816   ** operators that are of interest to the query planner.  An
116266 116817   ** OR-ed combination of these values can be used when searching for
116267 116818   ** particular WhereTerms within a WhereClause.
116268 116819   */
................................................................................
116305 116856   #define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
116306 116857   #define WHERE_AUTO_INDEX   0x00004000  /* Uses an ephemeral index */
116307 116858   #define WHERE_SKIPSCAN     0x00008000  /* Uses the skip-scan algorithm */
116308 116859   #define WHERE_UNQ_WANTED   0x00010000  /* WHERE_ONEROW would have been helpful*/
116309 116860   #define WHERE_PARTIALIDX   0x00020000  /* The automatic index is partial */
116310 116861   
116311 116862   /************** End of whereInt.h ********************************************/
116312         -/************** Continuing where we left off in where.c **********************/
116313         -
116314         -/*
116315         -** Return the estimated number of output rows from a WHERE clause
116316         -*/
116317         -SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
116318         -  return sqlite3LogEstToInt(pWInfo->nRowOut);
116319         -}
116320         -
116321         -/*
116322         -** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
116323         -** WHERE clause returns outputs for DISTINCT processing.
116324         -*/
116325         -SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
116326         -  return pWInfo->eDistinct;
116327         -}
116328         -
116329         -/*
116330         -** Return TRUE if the WHERE clause returns rows in ORDER BY order.
116331         -** Return FALSE if the output needs to be sorted.
116332         -*/
116333         -SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
116334         -  return pWInfo->nOBSat;
116335         -}
116336         -
116337         -/*
116338         -** Return the VDBE address or label to jump to in order to continue
116339         -** immediately with the next row of a WHERE clause.
116340         -*/
116341         -SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
116342         -  assert( pWInfo->iContinue!=0 );
116343         -  return pWInfo->iContinue;
116344         -}
116345         -
116346         -/*
116347         -** Return the VDBE address or label to jump to in order to break
116348         -** out of a WHERE loop.
116349         -*/
116350         -SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
116351         -  return pWInfo->iBreak;
116352         -}
116353         -
116354         -/*
116355         -** Return TRUE if an UPDATE or DELETE statement can operate directly on
116356         -** the rowids returned by a WHERE clause.  Return FALSE if doing an
116357         -** UPDATE or DELETE might change subsequent WHERE clause results.
116358         -**
116359         -** If the ONEPASS optimization is used (if this routine returns true)
116360         -** then also write the indices of open cursors used by ONEPASS
116361         -** into aiCur[0] and aiCur[1].  iaCur[0] gets the cursor of the data
116362         -** table and iaCur[1] gets the cursor used by an auxiliary index.
116363         -** Either value may be -1, indicating that cursor is not used.
116364         -** Any cursors returned will have been opened for writing.
116365         -**
116366         -** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
116367         -** unable to use the ONEPASS optimization.
116368         -*/
116369         -SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
116370         -  memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
116371         -  return pWInfo->okOnePass;
116372         -}
116373         -
116374         -/*
116375         -** Move the content of pSrc into pDest
116376         -*/
116377         -static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
116378         -  pDest->n = pSrc->n;
116379         -  memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
116380         -}
116381         -
116382         -/*
116383         -** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
116384         -**
116385         -** The new entry might overwrite an existing entry, or it might be
116386         -** appended, or it might be discarded.  Do whatever is the right thing
116387         -** so that pSet keeps the N_OR_COST best entries seen so far.
116388         -*/
116389         -static int whereOrInsert(
116390         -  WhereOrSet *pSet,      /* The WhereOrSet to be updated */
116391         -  Bitmask prereq,        /* Prerequisites of the new entry */
116392         -  LogEst rRun,           /* Run-cost of the new entry */
116393         -  LogEst nOut            /* Number of outputs for the new entry */
116394         -){
116395         -  u16 i;
116396         -  WhereOrCost *p;
116397         -  for(i=pSet->n, p=pSet->a; i>0; i--, p++){
116398         -    if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
116399         -      goto whereOrInsert_done;
116400         -    }
116401         -    if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
116402         -      return 0;
116403         -    }
116404         -  }
116405         -  if( pSet->n<N_OR_COST ){
116406         -    p = &pSet->a[pSet->n++];
116407         -    p->nOut = nOut;
116408         -  }else{
116409         -    p = pSet->a;
116410         -    for(i=1; i<pSet->n; i++){
116411         -      if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
116412         -    }
116413         -    if( p->rRun<=rRun ) return 0;
116414         -  }
116415         -whereOrInsert_done:
116416         -  p->prereq = prereq;
116417         -  p->rRun = rRun;
116418         -  if( p->nOut>nOut ) p->nOut = nOut;
116419         -  return 1;
116420         -}
116421         -
116422         -/*
116423         -** Initialize a preallocated WhereClause structure.
116424         -*/
116425         -static void whereClauseInit(
116426         -  WhereClause *pWC,        /* The WhereClause to be initialized */
116427         -  WhereInfo *pWInfo        /* The WHERE processing context */
116428         -){
116429         -  pWC->pWInfo = pWInfo;
116430         -  pWC->pOuter = 0;
116431         -  pWC->nTerm = 0;
116432         -  pWC->nSlot = ArraySize(pWC->aStatic);
116433         -  pWC->a = pWC->aStatic;
116434         -}
116435         -
116436         -/* Forward reference */
116437         -static void whereClauseClear(WhereClause*);
       116863  +/************** Continuing where we left off in wherecode.c ******************/
       116864  +
       116865  +#ifndef SQLITE_OMIT_EXPLAIN
       116866  +/*
       116867  +** This routine is a helper for explainIndexRange() below
       116868  +**
       116869  +** pStr holds the text of an expression that we are building up one term
       116870  +** at a time.  This routine adds a new term to the end of the expression.
       116871  +** Terms are separated by AND so add the "AND" text for second and subsequent
       116872  +** terms only.
       116873  +*/
       116874  +static void explainAppendTerm(
       116875  +  StrAccum *pStr,             /* The text expression being built */
       116876  +  int iTerm,                  /* Index of this term.  First is zero */
       116877  +  const char *zColumn,        /* Name of the column */
       116878  +  const char *zOp             /* Name of the operator */
       116879  +){
       116880  +  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
       116881  +  sqlite3StrAccumAppendAll(pStr, zColumn);
       116882  +  sqlite3StrAccumAppend(pStr, zOp, 1);
       116883  +  sqlite3StrAccumAppend(pStr, "?", 1);
       116884  +}
       116885  +
       116886  +/*
       116887  +** Argument pLevel describes a strategy for scanning table pTab. This 
       116888  +** function appends text to pStr that describes the subset of table
       116889  +** rows scanned by the strategy in the form of an SQL expression.
       116890  +**
       116891  +** For example, if the query:
       116892  +**
       116893  +**   SELECT * FROM t1 WHERE a=1 AND b>2;
       116894  +**
       116895  +** is run and there is an index on (a, b), then this function returns a
       116896  +** string similar to:
       116897  +**
       116898  +**   "a=? AND b>?"
       116899  +*/
       116900  +static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
       116901  +  Index *pIndex = pLoop->u.btree.pIndex;
       116902  +  u16 nEq = pLoop->u.btree.nEq;
       116903  +  u16 nSkip = pLoop->nSkip;
       116904  +  int i, j;
       116905  +  Column *aCol = pTab->aCol;
       116906  +  i16 *aiColumn = pIndex->aiColumn;
       116907  +
       116908  +  if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
       116909  +  sqlite3StrAccumAppend(pStr, " (", 2);
       116910  +  for(i=0; i<nEq; i++){
       116911  +    char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
       116912  +    if( i>=nSkip ){
       116913  +      explainAppendTerm(pStr, i, z, "=");
       116914  +    }else{
       116915  +      if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
       116916  +      sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
       116917  +    }
       116918  +  }
       116919  +
       116920  +  j = i;
       116921  +  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
       116922  +    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
       116923  +    explainAppendTerm(pStr, i++, z, ">");
       116924  +  }
       116925  +  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
       116926  +    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
       116927  +    explainAppendTerm(pStr, i, z, "<");
       116928  +  }
       116929  +  sqlite3StrAccumAppend(pStr, ")", 1);
       116930  +}
       116931  +
       116932  +/*
       116933  +** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
       116934  +** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
       116935  +** defined at compile-time. If it is not a no-op, a single OP_Explain opcode 
       116936  +** is added to the output to describe the table scan strategy in pLevel.
       116937  +**
       116938  +** If an OP_Explain opcode is added to the VM, its address is returned.
       116939  +** Otherwise, if no OP_Explain is coded, zero is returned.
       116940  +*/
       116941  +SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
       116942  +  Parse *pParse,                  /* Parse context */
       116943  +  SrcList *pTabList,              /* Table list this loop refers to */
       116944  +  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
       116945  +  int iLevel,                     /* Value for "level" column of output */
       116946  +  int iFrom,                      /* Value for "from" column of output */
       116947  +  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
       116948  +){
       116949  +  int ret = 0;
       116950  +#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
       116951  +  if( pParse->explain==2 )
       116952  +#endif
       116953  +  {
       116954  +    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
       116955  +    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
       116956  +    sqlite3 *db = pParse->db;     /* Database handle */
       116957  +    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
       116958  +    int isSearch;                 /* True for a SEARCH. False for SCAN. */
       116959  +    WhereLoop *pLoop;             /* The controlling WhereLoop object */
       116960  +    u32 flags;                    /* Flags that describe this loop */
       116961  +    char *zMsg;                   /* Text to add to EQP output */
       116962  +    StrAccum str;                 /* EQP output string */
       116963  +    char zBuf[100];               /* Initial space for EQP output string */
       116964  +
       116965  +    pLoop = pLevel->pWLoop;
       116966  +    flags = pLoop->wsFlags;
       116967  +    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
       116968  +
       116969  +    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
       116970  +            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
       116971  +            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
       116972  +
       116973  +    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
       116974  +    sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
       116975  +    if( pItem->pSelect ){
       116976  +      sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
       116977  +    }else{
       116978  +      sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
       116979  +    }
       116980  +
       116981  +    if( pItem->zAlias ){
       116982  +      sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
       116983  +    }
       116984  +    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
       116985  +      const char *zFmt = 0;
       116986  +      Index *pIdx;
       116987  +
       116988  +      assert( pLoop->u.btree.pIndex!=0 );
       116989  +      pIdx = pLoop->u.btree.pIndex;
       116990  +      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
       116991  +      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
       116992  +        if( isSearch ){
       116993  +          zFmt = "PRIMARY KEY";
       116994  +        }
       116995  +      }else if( flags & WHERE_PARTIALIDX ){
       116996  +        zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
       116997  +      }else if( flags & WHERE_AUTO_INDEX ){
       116998  +        zFmt = "AUTOMATIC COVERING INDEX";
       116999  +      }else if( flags & WHERE_IDX_ONLY ){
       117000  +        zFmt = "COVERING INDEX %s";
       117001  +      }else{
       117002  +        zFmt = "INDEX %s";
       117003  +      }
       117004  +      if( zFmt ){
       117005  +        sqlite3StrAccumAppend(&str, " USING ", 7);
       117006  +        sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
       117007  +        explainIndexRange(&str, pLoop, pItem->pTab);
       117008  +      }
       117009  +    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
       117010  +      const char *zRange;
       117011  +      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
       117012  +        zRange = "(rowid=?)";
       117013  +      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
       117014  +        zRange = "(rowid>? AND rowid<?)";
       117015  +      }else if( flags&WHERE_BTM_LIMIT ){
       117016  +        zRange = "(rowid>?)";
       117017  +      }else{
       117018  +        assert( flags&WHERE_TOP_LIMIT);
       117019  +        zRange = "(rowid<?)";
       117020  +      }
       117021  +      sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
       117022  +      sqlite3StrAccumAppendAll(&str, zRange);
       117023  +    }
       117024  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       117025  +    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
       117026  +      sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
       117027  +                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
       117028  +    }
       117029  +#endif
       117030  +#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
       117031  +    if( pLoop->nOut>=10 ){
       117032  +      sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
       117033  +    }else{
       117034  +      sqlite3StrAccumAppend(&str, " (~1 row)", 9);
       117035  +    }
       117036  +#endif
       117037  +    zMsg = sqlite3StrAccumFinish(&str);
       117038  +    ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
       117039  +  }
       117040  +  return ret;
       117041  +}
       117042  +#endif /* SQLITE_OMIT_EXPLAIN */
       117043  +
       117044  +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
       117045  +/*
       117046  +** Configure the VM passed as the first argument with an
       117047  +** sqlite3_stmt_scanstatus() entry corresponding to the scan used to 
       117048  +** implement level pLvl. Argument pSrclist is a pointer to the FROM 
       117049  +** clause that the scan reads data from.
       117050  +**
       117051  +** If argument addrExplain is not 0, it must be the address of an 
       117052  +** OP_Explain instruction that describes the same loop.
       117053  +*/
       117054  +SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
       117055  +  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
       117056  +  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
       117057  +  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
       117058  +  int addrExplain                 /* Address of OP_Explain (or 0) */
       117059  +){
       117060  +  const char *zObj = 0;
       117061  +  WhereLoop *pLoop = pLvl->pWLoop;
       117062  +  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
       117063  +    zObj = pLoop->u.btree.pIndex->zName;
       117064  +  }else{
       117065  +    zObj = pSrclist->a[pLvl->iFrom].zName;
       117066  +  }
       117067  +  sqlite3VdbeScanStatus(
       117068  +      v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
       117069  +  );
       117070  +}
       117071  +#endif
       117072  +
       117073  +
       117074  +/*
       117075  +** Disable a term in the WHERE clause.  Except, do not disable the term
       117076  +** if it controls a LEFT OUTER JOIN and it did not originate in the ON
       117077  +** or USING clause of that join.
       117078  +**
       117079  +** Consider the term t2.z='ok' in the following queries:
       117080  +**
       117081  +**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
       117082  +**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
       117083  +**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
       117084  +**
       117085  +** The t2.z='ok' is disabled in the in (2) because it originates
       117086  +** in the ON clause.  The term is disabled in (3) because it is not part
       117087  +** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
       117088  +**
       117089  +** Disabling a term causes that term to not be tested in the inner loop
       117090  +** of the join.  Disabling is an optimization.  When terms are satisfied
       117091  +** by indices, we disable them to prevent redundant tests in the inner
       117092  +** loop.  We would get the correct results if nothing were ever disabled,
       117093  +** but joins might run a little slower.  The trick is to disable as much
       117094  +** as we can without disabling too much.  If we disabled in (1), we'd get
       117095  +** the wrong answer.  See ticket #813.
       117096  +**
       117097  +** If all the children of a term are disabled, then that term is also
       117098  +** automatically disabled.  In this way, terms get disabled if derived
       117099  +** virtual terms are tested first.  For example:
       117100  +**
       117101  +**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
       117102  +**      \___________/     \______/     \_____/
       117103  +**         parent          child1       child2
       117104  +**
       117105  +** Only the parent term was in the original WHERE clause.  The child1
       117106  +** and child2 terms were added by the LIKE optimization.  If both of
       117107  +** the virtual child terms are valid, then testing of the parent can be 
       117108  +** skipped.
       117109  +**
       117110  +** Usually the parent term is marked as TERM_CODED.  But if the parent
       117111  +** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
       117112  +** The TERM_LIKECOND marking indicates that the term should be coded inside
       117113  +** a conditional such that is only evaluated on the second pass of a
       117114  +** LIKE-optimization loop, when scanning BLOBs instead of strings.
       117115  +*/
       117116  +static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
       117117  +  int nLoop = 0;
       117118  +  while( pTerm
       117119  +      && (pTerm->wtFlags & TERM_CODED)==0
       117120  +      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
       117121  +      && (pLevel->notReady & pTerm->prereqAll)==0
       117122  +  ){
       117123  +    if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
       117124  +      pTerm->wtFlags |= TERM_LIKECOND;
       117125  +    }else{
       117126  +      pTerm->wtFlags |= TERM_CODED;
       117127  +    }
       117128  +    if( pTerm->iParent<0 ) break;
       117129  +    pTerm = &pTerm->pWC->a[pTerm->iParent];
       117130  +    pTerm->nChild--;
       117131  +    if( pTerm->nChild!=0 ) break;
       117132  +    nLoop++;
       117133  +  }
       117134  +}
       117135  +
       117136  +/*
       117137  +** Code an OP_Affinity opcode to apply the column affinity string zAff
       117138  +** to the n registers starting at base. 
       117139  +**
       117140  +** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
       117141  +** beginning and end of zAff are ignored.  If all entries in zAff are
       117142  +** SQLITE_AFF_BLOB, then no code gets generated.
       117143  +**
       117144  +** This routine makes its own copy of zAff so that the caller is free
       117145  +** to modify zAff after this routine returns.
       117146  +*/
       117147  +static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
       117148  +  Vdbe *v = pParse->pVdbe;
       117149  +  if( zAff==0 ){
       117150  +    assert( pParse->db->mallocFailed );
       117151  +    return;
       117152  +  }
       117153  +  assert( v!=0 );
       117154  +
       117155  +  /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
       117156  +  ** and end of the affinity string.
       117157  +  */
       117158  +  while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
       117159  +    n--;
       117160  +    base++;
       117161  +    zAff++;
       117162  +  }
       117163  +  while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
       117164  +    n--;
       117165  +  }
       117166  +
       117167  +  /* Code the OP_Affinity opcode if there is anything left to do. */
       117168  +  if( n>0 ){
       117169  +    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
       117170  +    sqlite3VdbeChangeP4(v, -1, zAff, n);
       117171  +    sqlite3ExprCacheAffinityChange(pParse, base, n);
       117172  +  }
       117173  +}
       117174  +
       117175  +
       117176  +/*
       117177  +** Generate code for a single equality term of the WHERE clause.  An equality
       117178  +** term can be either X=expr or X IN (...).   pTerm is the term to be 
       117179  +** coded.
       117180  +**
       117181  +** The current value for the constraint is left in register iReg.
       117182  +**
       117183  +** For a constraint of the form X=expr, the expression is evaluated and its
       117184  +** result is left on the stack.  For constraints of the form X IN (...)
       117185  +** this routine sets up a loop that will iterate over all values of X.
       117186  +*/
       117187  +static int codeEqualityTerm(
       117188  +  Parse *pParse,      /* The parsing context */
       117189  +  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
       117190  +  WhereLevel *pLevel, /* The level of the FROM clause we are working on */
       117191  +  int iEq,            /* Index of the equality term within this level */
       117192  +  int bRev,           /* True for reverse-order IN operations */
       117193  +  int iTarget         /* Attempt to leave results in this register */
       117194  +){
       117195  +  Expr *pX = pTerm->pExpr;
       117196  +  Vdbe *v = pParse->pVdbe;
       117197  +  int iReg;                  /* Register holding results */
       117198  +
       117199  +  assert( iTarget>0 );
       117200  +  if( pX->op==TK_EQ || pX->op==TK_IS ){
       117201  +    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
       117202  +  }else if( pX->op==TK_ISNULL ){
       117203  +    iReg = iTarget;
       117204  +    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
       117205  +#ifndef SQLITE_OMIT_SUBQUERY
       117206  +  }else{
       117207  +    int eType;
       117208  +    int iTab;
       117209  +    struct InLoop *pIn;
       117210  +    WhereLoop *pLoop = pLevel->pWLoop;
       117211  +
       117212  +    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
       117213  +      && pLoop->u.btree.pIndex!=0
       117214  +      && pLoop->u.btree.pIndex->aSortOrder[iEq]
       117215  +    ){
       117216  +      testcase( iEq==0 );
       117217  +      testcase( bRev );
       117218  +      bRev = !bRev;
       117219  +    }
       117220  +    assert( pX->op==TK_IN );
       117221  +    iReg = iTarget;
       117222  +    eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
       117223  +    if( eType==IN_INDEX_INDEX_DESC ){
       117224  +      testcase( bRev );
       117225  +      bRev = !bRev;
       117226  +    }
       117227  +    iTab = pX->iTable;
       117228  +    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
       117229  +    VdbeCoverageIf(v, bRev);
       117230  +    VdbeCoverageIf(v, !bRev);
       117231  +    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
       117232  +    pLoop->wsFlags |= WHERE_IN_ABLE;
       117233  +    if( pLevel->u.in.nIn==0 ){
       117234  +      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
       117235  +    }
       117236  +    pLevel->u.in.nIn++;
       117237  +    pLevel->u.in.aInLoop =
       117238  +       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
       117239  +                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
       117240  +    pIn = pLevel->u.in.aInLoop;
       117241  +    if( pIn ){
       117242  +      pIn += pLevel->u.in.nIn - 1;
       117243  +      pIn->iCur = iTab;
       117244  +      if( eType==IN_INDEX_ROWID ){
       117245  +        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
       117246  +      }else{
       117247  +        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
       117248  +      }
       117249  +      pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
       117250  +      sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
       117251  +    }else{
       117252  +      pLevel->u.in.nIn = 0;
       117253  +    }
       117254  +#endif
       117255  +  }
       117256  +  disableTerm(pLevel, pTerm);
       117257  +  return iReg;
       117258  +}
       117259  +
       117260  +/*
       117261  +** Generate code that will evaluate all == and IN constraints for an
       117262  +** index scan.
       117263  +**
       117264  +** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
       117265  +** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
       117266  +** The index has as many as three equality constraints, but in this
       117267  +** example, the third "c" value is an inequality.  So only two 
       117268  +** constraints are coded.  This routine will generate code to evaluate
       117269  +** a==5 and b IN (1,2,3).  The current values for a and b will be stored
       117270  +** in consecutive registers and the index of the first register is returned.
       117271  +**
       117272  +** In the example above nEq==2.  But this subroutine works for any value
       117273  +** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
       117274  +** The only thing it does is allocate the pLevel->iMem memory cell and
       117275  +** compute the affinity string.
       117276  +**
       117277  +** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
       117278  +** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
       117279  +** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
       117280  +** occurs after the nEq quality constraints.
       117281  +**
       117282  +** This routine allocates a range of nEq+nExtraReg memory cells and returns
       117283  +** the index of the first memory cell in that range. The code that
       117284  +** calls this routine will use that memory range to store keys for
       117285  +** start and termination conditions of the loop.
       117286  +** key value of the loop.  If one or more IN operators appear, then
       117287  +** this routine allocates an additional nEq memory cells for internal
       117288  +** use.
       117289  +**
       117290  +** Before returning, *pzAff is set to point to a buffer containing a
       117291  +** copy of the column affinity string of the index allocated using
       117292  +** sqlite3DbMalloc(). Except, entries in the copy of the string associated
       117293  +** with equality constraints that use BLOB or NONE affinity are set to
       117294  +** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
       117295  +**
       117296  +**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
       117297  +**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
       117298  +**
       117299  +** In the example above, the index on t1(a) has TEXT affinity. But since
       117300  +** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
       117301  +** no conversion should be attempted before using a t2.b value as part of
       117302  +** a key to search the index. Hence the first byte in the returned affinity
       117303  +** string in this example would be set to SQLITE_AFF_BLOB.
       117304  +*/
       117305  +static int codeAllEqualityTerms(
       117306  +  Parse *pParse,        /* Parsing context */
       117307  +  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
       117308  +  int bRev,             /* Reverse the order of IN operators */
       117309  +  int nExtraReg,        /* Number of extra registers to allocate */
       117310  +  char **pzAff          /* OUT: Set to point to affinity string */
       117311  +){
       117312  +  u16 nEq;                      /* The number of == or IN constraints to code */
       117313  +  u16 nSkip;                    /* Number of left-most columns to skip */
       117314  +  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
       117315  +  Index *pIdx;                  /* The index being used for this loop */
       117316  +  WhereTerm *pTerm;             /* A single constraint term */
       117317  +  WhereLoop *pLoop;             /* The WhereLoop object */
       117318  +  int j;                        /* Loop counter */
       117319  +  int regBase;                  /* Base register */
       117320  +  int nReg;                     /* Number of registers to allocate */
       117321  +  char *zAff;                   /* Affinity string to return */
       117322  +
       117323  +  /* This module is only called on query plans that use an index. */
       117324  +  pLoop = pLevel->pWLoop;
       117325  +  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
       117326  +  nEq = pLoop->u.btree.nEq;
       117327  +  nSkip = pLoop->nSkip;
       117328  +  pIdx = pLoop->u.btree.pIndex;
       117329  +  assert( pIdx!=0 );
       117330  +
       117331  +  /* Figure out how many memory cells we will need then allocate them.
       117332  +  */
       117333  +  regBase = pParse->nMem + 1;
       117334  +  nReg = pLoop->u.btree.nEq + nExtraReg;
       117335  +  pParse->nMem += nReg;
       117336  +
       117337  +  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
       117338  +  if( !zAff ){
       117339  +    pParse->db->mallocFailed = 1;
       117340  +  }
       117341  +
       117342  +  if( nSkip ){
       117343  +    int iIdxCur = pLevel->iIdxCur;
       117344  +    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
       117345  +    VdbeCoverageIf(v, bRev==0);
       117346  +    VdbeCoverageIf(v, bRev!=0);
       117347  +    VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
       117348  +    j = sqlite3VdbeAddOp0(v, OP_Goto);
       117349  +    pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
       117350  +                            iIdxCur, 0, regBase, nSkip);
       117351  +    VdbeCoverageIf(v, bRev==0);
       117352  +    VdbeCoverageIf(v, bRev!=0);
       117353  +    sqlite3VdbeJumpHere(v, j);
       117354  +    for(j=0; j<nSkip; j++){
       117355  +      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
       117356  +      assert( pIdx->aiColumn[j]>=0 );
       117357  +      VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
       117358  +    }
       117359  +  }    
       117360  +
       117361  +  /* Evaluate the equality constraints
       117362  +  */
       117363  +  assert( zAff==0 || (int)strlen(zAff)>=nEq );
       117364  +  for(j=nSkip; j<nEq; j++){
       117365  +    int r1;
       117366  +    pTerm = pLoop->aLTerm[j];
       117367  +    assert( pTerm!=0 );
       117368  +    /* The following testcase is true for indices with redundant columns. 
       117369  +    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
       117370  +    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
       117371  +    testcase( pTerm->wtFlags & TERM_VIRTUAL );
       117372  +    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
       117373  +    if( r1!=regBase+j ){
       117374  +      if( nReg==1 ){
       117375  +        sqlite3ReleaseTempReg(pParse, regBase);
       117376  +        regBase = r1;
       117377  +      }else{
       117378  +        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
       117379  +      }
       117380  +    }
       117381  +    testcase( pTerm->eOperator & WO_ISNULL );
       117382  +    testcase( pTerm->eOperator & WO_IN );
       117383  +    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
       117384  +      Expr *pRight = pTerm->pExpr->pRight;
       117385  +      if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
       117386  +        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
       117387  +        VdbeCoverage(v);
       117388  +      }
       117389  +      if( zAff ){
       117390  +        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
       117391  +          zAff[j] = SQLITE_AFF_BLOB;
       117392  +        }
       117393  +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
       117394  +          zAff[j] = SQLITE_AFF_BLOB;
       117395  +        }
       117396  +      }
       117397  +    }
       117398  +  }
       117399  +  *pzAff = zAff;
       117400  +  return regBase;
       117401  +}
       117402  +
       117403  +/*
       117404  +** If the most recently coded instruction is a constant range contraint
       117405  +** that originated from the LIKE optimization, then change the P3 to be
       117406  +** pLoop->iLikeRepCntr and set P5.
       117407  +**
       117408  +** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
       117409  +** expression: "x>='ABC' AND x<'abd'".  But this requires that the range
       117410  +** scan loop run twice, once for strings and a second time for BLOBs.
       117411  +** The OP_String opcodes on the second pass convert the upper and lower
       117412  +** bound string contants to blobs.  This routine makes the necessary changes
       117413  +** to the OP_String opcodes for that to happen.
       117414  +*/
       117415  +static void whereLikeOptimizationStringFixup(
       117416  +  Vdbe *v,                /* prepared statement under construction */
       117417  +  WhereLevel *pLevel,     /* The loop that contains the LIKE operator */
       117418  +  WhereTerm *pTerm        /* The upper or lower bound just coded */
       117419  +){
       117420  +  if( pTerm->wtFlags & TERM_LIKEOPT ){
       117421  +    VdbeOp *pOp;
       117422  +    assert( pLevel->iLikeRepCntr>0 );
       117423  +    pOp = sqlite3VdbeGetOp(v, -1);
       117424  +    assert( pOp!=0 );
       117425  +    assert( pOp->opcode==OP_String8 
       117426  +            || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
       117427  +    pOp->p3 = pLevel->iLikeRepCntr;
       117428  +    pOp->p5 = 1;
       117429  +  }
       117430  +}
       117431  +
       117432  +
       117433  +/*
       117434  +** Generate code for the start of the iLevel-th loop in the WHERE clause
       117435  +** implementation described by pWInfo.
       117436  +*/
       117437  +SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
       117438  +  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
       117439  +  int iLevel,          /* Which level of pWInfo->a[] should be coded */
       117440  +  Bitmask notReady     /* Which tables are currently available */
       117441  +){
       117442  +  int j, k;            /* Loop counters */
       117443  +  int iCur;            /* The VDBE cursor for the table */
       117444  +  int addrNxt;         /* Where to jump to continue with the next IN case */
       117445  +  int omitTable;       /* True if we use the index only */
       117446  +  int bRev;            /* True if we need to scan in reverse order */
       117447  +  WhereLevel *pLevel;  /* The where level to be coded */
       117448  +  WhereLoop *pLoop;    /* The WhereLoop object being coded */
       117449  +  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
       117450  +  WhereTerm *pTerm;               /* A WHERE clause term */
       117451  +  Parse *pParse;                  /* Parsing context */
       117452  +  sqlite3 *db;                    /* Database connection */
       117453  +  Vdbe *v;                        /* The prepared stmt under constructions */
       117454  +  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
       117455  +  int addrBrk;                    /* Jump here to break out of the loop */
       117456  +  int addrCont;                   /* Jump here to continue with next cycle */
       117457  +  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
       117458  +  int iReleaseReg = 0;      /* Temp register to free before returning */
       117459  +
       117460  +  pParse = pWInfo->pParse;
       117461  +  v = pParse->pVdbe;
       117462  +  pWC = &pWInfo->sWC;
       117463  +  db = pParse->db;
       117464  +  pLevel = &pWInfo->a[iLevel];
       117465  +  pLoop = pLevel->pWLoop;
       117466  +  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
       117467  +  iCur = pTabItem->iCursor;
       117468  +  pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
       117469  +  bRev = (pWInfo->revMask>>iLevel)&1;
       117470  +  omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
       117471  +           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
       117472  +  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
       117473  +
       117474  +  /* Create labels for the "break" and "continue" instructions
       117475  +  ** for the current loop.  Jump to addrBrk to break out of a loop.
       117476  +  ** Jump to cont to go immediately to the next iteration of the
       117477  +  ** loop.
       117478  +  **
       117479  +  ** When there is an IN operator, we also have a "addrNxt" label that
       117480  +  ** means to continue with the next IN value combination.  When
       117481  +  ** there are no IN operators in the constraints, the "addrNxt" label
       117482  +  ** is the same as "addrBrk".
       117483  +  */
       117484  +  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
       117485  +  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
       117486  +
       117487  +  /* If this is the right table of a LEFT OUTER JOIN, allocate and
       117488  +  ** initialize a memory cell that records if this table matches any
       117489  +  ** row of the left table of the join.
       117490  +  */
       117491  +  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
       117492  +    pLevel->iLeftJoin = ++pParse->nMem;
       117493  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
       117494  +    VdbeComment((v, "init LEFT JOIN no-match flag"));
       117495  +  }
       117496  +
       117497  +  /* Special case of a FROM clause subquery implemented as a co-routine */
       117498  +  if( pTabItem->viaCoroutine ){
       117499  +    int regYield = pTabItem->regReturn;
       117500  +    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
       117501  +    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
       117502  +    VdbeCoverage(v);
       117503  +    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
       117504  +    pLevel->op = OP_Goto;
       117505  +  }else
       117506  +
       117507  +#ifndef SQLITE_OMIT_VIRTUALTABLE
       117508  +  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
       117509  +    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
       117510  +    **          to access the data.
       117511  +    */
       117512  +    int iReg;   /* P3 Value for OP_VFilter */
       117513  +    int addrNotFound;
       117514  +    int nConstraint = pLoop->nLTerm;
       117515  +
       117516  +    sqlite3ExprCachePush(pParse);
       117517  +    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
       117518  +    addrNotFound = pLevel->addrBrk;
       117519  +    for(j=0; j<nConstraint; j++){
       117520  +      int iTarget = iReg+j+2;
       117521  +      pTerm = pLoop->aLTerm[j];
       117522  +      if( pTerm==0 ) continue;
       117523  +      if( pTerm->eOperator & WO_IN ){
       117524  +        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
       117525  +        addrNotFound = pLevel->addrNxt;
       117526  +      }else{
       117527  +        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
       117528  +      }
       117529  +    }
       117530  +    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
       117531  +    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
       117532  +    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
       117533  +                      pLoop->u.vtab.idxStr,
       117534  +                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
       117535  +    VdbeCoverage(v);
       117536  +    pLoop->u.vtab.needFree = 0;
       117537  +    for(j=0; j<nConstraint && j<16; j++){
       117538  +      if( (pLoop->u.vtab.omitMask>>j)&1 ){
       117539  +        disableTerm(pLevel, pLoop->aLTerm[j]);
       117540  +      }
       117541  +    }
       117542  +    pLevel->op = OP_VNext;
       117543  +    pLevel->p1 = iCur;
       117544  +    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
       117545  +    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
       117546  +    sqlite3ExprCachePop(pParse);
       117547  +  }else
       117548  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
       117549  +
       117550  +  if( (pLoop->wsFlags & WHERE_IPK)!=0
       117551  +   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
       117552  +  ){
       117553  +    /* Case 2:  We can directly reference a single row using an
       117554  +    **          equality comparison against the ROWID field.  Or
       117555  +    **          we reference multiple rows using a "rowid IN (...)"
       117556  +    **          construct.
       117557  +    */
       117558  +    assert( pLoop->u.btree.nEq==1 );
       117559  +    pTerm = pLoop->aLTerm[0];
       117560  +    assert( pTerm!=0 );
       117561  +    assert( pTerm->pExpr!=0 );
       117562  +    assert( omitTable==0 );
       117563  +    testcase( pTerm->wtFlags & TERM_VIRTUAL );
       117564  +    iReleaseReg = ++pParse->nMem;
       117565  +    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
       117566  +    if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
       117567  +    addrNxt = pLevel->addrNxt;
       117568  +    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
       117569  +    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
       117570  +    VdbeCoverage(v);
       117571  +    sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
       117572  +    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
       117573  +    VdbeComment((v, "pk"));
       117574  +    pLevel->op = OP_Noop;
       117575  +  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
       117576  +         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
       117577  +  ){
       117578  +    /* Case 3:  We have an inequality comparison against the ROWID field.
       117579  +    */
       117580  +    int testOp = OP_Noop;
       117581  +    int start;
       117582  +    int memEndValue = 0;
       117583  +    WhereTerm *pStart, *pEnd;
       117584  +
       117585  +    assert( omitTable==0 );
       117586  +    j = 0;
       117587  +    pStart = pEnd = 0;
       117588  +    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
       117589  +    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
       117590  +    assert( pStart!=0 || pEnd!=0 );
       117591  +    if( bRev ){
       117592  +      pTerm = pStart;
       117593  +      pStart = pEnd;
       117594  +      pEnd = pTerm;
       117595  +    }
       117596  +    if( pStart ){
       117597  +      Expr *pX;             /* The expression that defines the start bound */
       117598  +      int r1, rTemp;        /* Registers for holding the start boundary */
       117599  +
       117600  +      /* The following constant maps TK_xx codes into corresponding 
       117601  +      ** seek opcodes.  It depends on a particular ordering of TK_xx
       117602  +      */
       117603  +      const u8 aMoveOp[] = {
       117604  +           /* TK_GT */  OP_SeekGT,
       117605  +           /* TK_LE */  OP_SeekLE,
       117606  +           /* TK_LT */  OP_SeekLT,
       117607  +           /* TK_GE */  OP_SeekGE
       117608  +      };
       117609  +      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
       117610  +      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
       117611  +      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
       117612  +
       117613  +      assert( (pStart->wtFlags & TERM_VNULL)==0 );
       117614  +      testcase( pStart->wtFlags & TERM_VIRTUAL );
       117615  +      pX = pStart->pExpr;
       117616  +      assert( pX!=0 );
       117617  +      testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
       117618  +      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
       117619  +      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
       117620  +      VdbeComment((v, "pk"));
       117621  +      VdbeCoverageIf(v, pX->op==TK_GT);
       117622  +      VdbeCoverageIf(v, pX->op==TK_LE);
       117623  +      VdbeCoverageIf(v, pX->op==TK_LT);
       117624  +      VdbeCoverageIf(v, pX->op==TK_GE);
       117625  +      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
       117626  +      sqlite3ReleaseTempReg(pParse, rTemp);
       117627  +      disableTerm(pLevel, pStart);
       117628  +    }else{
       117629  +      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
       117630  +      VdbeCoverageIf(v, bRev==0);
       117631  +      VdbeCoverageIf(v, bRev!=0);
       117632  +    }
       117633  +    if( pEnd ){
       117634  +      Expr *pX;
       117635  +      pX = pEnd->pExpr;
       117636  +      assert( pX!=0 );
       117637  +      assert( (pEnd->wtFlags & TERM_VNULL)==0 );
       117638  +      testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
       117639  +      testcase( pEnd->wtFlags & TERM_VIRTUAL );
       117640  +      memEndValue = ++pParse->nMem;
       117641  +      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
       117642  +      if( pX->op==TK_LT || pX->op==TK_GT ){
       117643  +        testOp = bRev ? OP_Le : OP_Ge;
       117644  +      }else{
       117645  +        testOp = bRev ? OP_Lt : OP_Gt;
       117646  +      }
       117647  +      disableTerm(pLevel, pEnd);
       117648  +    }
       117649  +    start = sqlite3VdbeCurrentAddr(v);
       117650  +    pLevel->op = bRev ? OP_Prev : OP_Next;
       117651  +    pLevel->p1 = iCur;
       117652  +    pLevel->p2 = start;
       117653  +    assert( pLevel->p5==0 );
       117654  +    if( testOp!=OP_Noop ){
       117655  +      iRowidReg = ++pParse->nMem;
       117656  +      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
       117657  +      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
       117658  +      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
       117659  +      VdbeCoverageIf(v, testOp==OP_Le);
       117660  +      VdbeCoverageIf(v, testOp==OP_Lt);
       117661  +      VdbeCoverageIf(v, testOp==OP_Ge);
       117662  +      VdbeCoverageIf(v, testOp==OP_Gt);
       117663  +      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
       117664  +    }
       117665  +  }else if( pLoop->wsFlags & WHERE_INDEXED ){
       117666  +    /* Case 4: A scan using an index.
       117667  +    **
       117668  +    **         The WHERE clause may contain zero or more equality 
       117669  +    **         terms ("==" or "IN" operators) that refer to the N
       117670  +    **         left-most columns of the index. It may also contain
       117671  +    **         inequality constraints (>, <, >= or <=) on the indexed
       117672  +    **         column that immediately follows the N equalities. Only 
       117673  +    **         the right-most column can be an inequality - the rest must
       117674  +    **         use the "==" and "IN" operators. For example, if the 
       117675  +    **         index is on (x,y,z), then the following clauses are all 
       117676  +    **         optimized:
       117677  +    **
       117678  +    **            x=5
       117679  +    **            x=5 AND y=10
       117680  +    **            x=5 AND y<10
       117681  +    **            x=5 AND y>5 AND y<10
       117682  +    **            x=5 AND y=5 AND z<=10
       117683  +    **
       117684  +    **         The z<10 term of the following cannot be used, only
       117685  +    **         the x=5 term:
       117686  +    **
       117687  +    **            x=5 AND z<10
       117688  +    **
       117689  +    **         N may be zero if there are inequality constraints.
       117690  +    **         If there are no inequality constraints, then N is at
       117691  +    **         least one.
       117692  +    **
       117693  +    **         This case is also used when there are no WHERE clause
       117694  +    **         constraints but an index is selected anyway, in order
       117695  +    **         to force the output order to conform to an ORDER BY.
       117696  +    */  
       117697  +    static const u8 aStartOp[] = {
       117698  +      0,
       117699  +      0,
       117700  +      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
       117701  +      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
       117702  +      OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
       117703  +      OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
       117704  +      OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
       117705  +      OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
       117706  +    };
       117707  +    static const u8 aEndOp[] = {
       117708  +      OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
       117709  +      OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
       117710  +      OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
       117711  +      OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
       117712  +    };
       117713  +    u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
       117714  +    int regBase;                 /* Base register holding constraint values */
       117715  +    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
       117716  +    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
       117717  +    int startEq;                 /* True if range start uses ==, >= or <= */
       117718  +    int endEq;                   /* True if range end uses ==, >= or <= */
       117719  +    int start_constraints;       /* Start of range is constrained */
       117720  +    int nConstraint;             /* Number of constraint terms */
       117721  +    Index *pIdx;                 /* The index we will be using */
       117722  +    int iIdxCur;                 /* The VDBE cursor for the index */
       117723  +    int nExtraReg = 0;           /* Number of extra registers needed */
       117724  +    int op;                      /* Instruction opcode */
       117725  +    char *zStartAff;             /* Affinity for start of range constraint */
       117726  +    char cEndAff = 0;            /* Affinity for end of range constraint */
       117727  +    u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
       117728  +    u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
       117729  +
       117730  +    pIdx = pLoop->u.btree.pIndex;
       117731  +    iIdxCur = pLevel->iIdxCur;
       117732  +    assert( nEq>=pLoop->nSkip );
       117733  +
       117734  +    /* If this loop satisfies a sort order (pOrderBy) request that 
       117735  +    ** was passed to this function to implement a "SELECT min(x) ..." 
       117736  +    ** query, then the caller will only allow the loop to run for
       117737  +    ** a single iteration. This means that the first row returned
       117738  +    ** should not have a NULL value stored in 'x'. If column 'x' is
       117739  +    ** the first one after the nEq equality constraints in the index,
       117740  +    ** this requires some special handling.
       117741  +    */
       117742  +    assert( pWInfo->pOrderBy==0
       117743  +         || pWInfo->pOrderBy->nExpr==1
       117744  +         || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
       117745  +    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
       117746  +     && pWInfo->nOBSat>0
       117747  +     && (pIdx->nKeyCol>nEq)
       117748  +    ){
       117749  +      assert( pLoop->nSkip==0 );
       117750  +      bSeekPastNull = 1;
       117751  +      nExtraReg = 1;
       117752  +    }
       117753  +
       117754  +    /* Find any inequality constraint terms for the start and end 
       117755  +    ** of the range. 
       117756  +    */
       117757  +    j = nEq;
       117758  +    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
       117759  +      pRangeStart = pLoop->aLTerm[j++];
       117760  +      nExtraReg = 1;
       117761  +      /* Like optimization range constraints always occur in pairs */
       117762  +      assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || 
       117763  +              (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
       117764  +    }
       117765  +    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
       117766  +      pRangeEnd = pLoop->aLTerm[j++];
       117767  +      nExtraReg = 1;
       117768  +      if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
       117769  +        assert( pRangeStart!=0 );                     /* LIKE opt constraints */
       117770  +        assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
       117771  +        pLevel->iLikeRepCntr = ++pParse->nMem;
       117772  +        testcase( bRev );
       117773  +        testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
       117774  +        sqlite3VdbeAddOp2(v, OP_Integer,
       117775  +                          bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
       117776  +                          pLevel->iLikeRepCntr);
       117777  +        VdbeComment((v, "LIKE loop counter"));
       117778  +        pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
       117779  +      }
       117780  +      if( pRangeStart==0
       117781  +       && (j = pIdx->aiColumn[nEq])>=0 
       117782  +       && pIdx->pTable->aCol[j].notNull==0
       117783  +      ){
       117784  +        bSeekPastNull = 1;
       117785  +      }
       117786  +    }
       117787  +    assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
       117788  +
       117789  +    /* Generate code to evaluate all constraint terms using == or IN
       117790  +    ** and store the values of those terms in an array of registers
       117791  +    ** starting at regBase.
       117792  +    */
       117793  +    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
       117794  +    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
       117795  +    if( zStartAff ) cEndAff = zStartAff[nEq];
       117796  +    addrNxt = pLevel->addrNxt;
       117797  +
       117798  +    /* If we are doing a reverse order scan on an ascending index, or
       117799  +    ** a forward order scan on a descending index, interchange the 
       117800  +    ** start and end terms (pRangeStart and pRangeEnd).
       117801  +    */
       117802  +    if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
       117803  +     || (bRev && pIdx->nKeyCol==nEq)
       117804  +    ){
       117805  +      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
       117806  +      SWAP(u8, bSeekPastNull, bStopAtNull);
       117807  +    }
       117808  +
       117809  +    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
       117810  +    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
       117811  +    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
       117812  +    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
       117813  +    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
       117814  +    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
       117815  +    start_constraints = pRangeStart || nEq>0;
       117816  +
       117817  +    /* Seek the index cursor to the start of the range. */
       117818  +    nConstraint = nEq;
       117819  +    if( pRangeStart ){
       117820  +      Expr *pRight = pRangeStart->pExpr->pRight;
       117821  +      sqlite3ExprCode(pParse, pRight, regBase+nEq);
       117822  +      whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
       117823  +      if( (pRangeStart->wtFlags & TERM_VNULL)==0
       117824  +       && sqlite3ExprCanBeNull(pRight)
       117825  +      ){
       117826  +        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
       117827  +        VdbeCoverage(v);
       117828  +      }
       117829  +      if( zStartAff ){
       117830  +        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){
       117831  +          /* Since the comparison is to be performed with no conversions
       117832  +          ** applied to the operands, set the affinity to apply to pRight to 
       117833  +          ** SQLITE_AFF_BLOB.  */
       117834  +          zStartAff[nEq] = SQLITE_AFF_BLOB;
       117835  +        }
       117836  +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
       117837  +          zStartAff[nEq] = SQLITE_AFF_BLOB;
       117838  +        }
       117839  +      }  
       117840  +      nConstraint++;
       117841  +      testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
       117842  +    }else if( bSeekPastNull ){
       117843  +      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
       117844  +      nConstraint++;
       117845  +      startEq = 0;
       117846  +      start_constraints = 1;
       117847  +    }
       117848  +    codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
       117849  +    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
       117850  +    assert( op!=0 );
       117851  +    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
       117852  +    VdbeCoverage(v);
       117853  +    VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
       117854  +    VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
       117855  +    VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
       117856  +    VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
       117857  +    VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
       117858  +    VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
       117859  +
       117860  +    /* Load the value for the inequality constraint at the end of the
       117861  +    ** range (if any).
       117862  +    */
       117863  +    nConstraint = nEq;
       117864  +    if( pRangeEnd ){
       117865  +      Expr *pRight = pRangeEnd->pExpr->pRight;
       117866  +      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
       117867  +      sqlite3ExprCode(pParse, pRight, regBase+nEq);
       117868  +      whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
       117869  +      if( (pRangeEnd->wtFlags & TERM_VNULL)==0
       117870  +       && sqlite3ExprCanBeNull(pRight)
       117871  +      ){
       117872  +        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
       117873  +        VdbeCoverage(v);
       117874  +      }
       117875  +      if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB
       117876  +       && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
       117877  +      ){
       117878  +        codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
       117879  +      }
       117880  +      nConstraint++;
       117881  +      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
       117882  +    }else if( bStopAtNull ){
       117883  +      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
       117884  +      endEq = 0;
       117885  +      nConstraint++;
       117886  +    }
       117887  +    sqlite3DbFree(db, zStartAff);
       117888  +
       117889  +    /* Top of the loop body */
       117890  +    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
       117891  +
       117892  +    /* Check if the index cursor is past the end of the range. */
       117893  +    if( nConstraint ){
       117894  +      op = aEndOp[bRev*2 + endEq];
       117895  +      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
       117896  +      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
       117897  +      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
       117898  +      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
       117899  +      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
       117900  +    }
       117901  +
       117902  +    /* Seek the table cursor, if required */
       117903  +    disableTerm(pLevel, pRangeStart);
       117904  +    disableTerm(pLevel, pRangeEnd);
       117905  +    if( omitTable ){
       117906  +      /* pIdx is a covering index.  No need to access the main table. */
       117907  +    }else if( HasRowid(pIdx->pTable) ){
       117908  +      iRowidReg = ++pParse->nMem;
       117909  +      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
       117910  +      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
       117911  +      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
       117912  +    }else if( iCur!=iIdxCur ){
       117913  +      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
       117914  +      iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
       117915  +      for(j=0; j<pPk->nKeyCol; j++){
       117916  +        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
       117917  +        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
       117918  +      }
       117919  +      sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
       117920  +                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
       117921  +    }
       117922  +
       117923  +    /* Record the instruction used to terminate the loop. Disable 
       117924  +    ** WHERE clause terms made redundant by the index range scan.
       117925  +    */
       117926  +    if( pLoop->wsFlags & WHERE_ONEROW ){
       117927  +      pLevel->op = OP_Noop;
       117928  +    }else if( bRev ){
       117929  +      pLevel->op = OP_Prev;
       117930  +    }else{
       117931  +      pLevel->op = OP_Next;
       117932  +    }
       117933  +    pLevel->p1 = iIdxCur;
       117934  +    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
       117935  +    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
       117936  +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
       117937  +    }else{
       117938  +      assert( pLevel->p5==0 );
       117939  +    }
       117940  +  }else
       117941  +
       117942  +#ifndef SQLITE_OMIT_OR_OPTIMIZATION
       117943  +  if( pLoop->wsFlags & WHERE_MULTI_OR ){
       117944  +    /* Case 5:  Two or more separately indexed terms connected by OR
       117945  +    **
       117946  +    ** Example:
       117947  +    **
       117948  +    **   CREATE TABLE t1(a,b,c,d);
       117949  +    **   CREATE INDEX i1 ON t1(a);
       117950  +    **   CREATE INDEX i2 ON t1(b);
       117951  +    **   CREATE INDEX i3 ON t1(c);
       117952  +    **
       117953  +    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
       117954  +    **
       117955  +    ** In the example, there are three indexed terms connected by OR.
       117956  +    ** The top of the loop looks like this:
       117957  +    **
       117958  +    **          Null       1                # Zero the rowset in reg 1
       117959  +    **
       117960  +    ** Then, for each indexed term, the following. The arguments to
       117961  +    ** RowSetTest are such that the rowid of the current row is inserted
       117962  +    ** into the RowSet. If it is already present, control skips the
       117963  +    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
       117964  +    **
       117965  +    **        sqlite3WhereBegin(<term>)
       117966  +    **          RowSetTest                  # Insert rowid into rowset
       117967  +    **          Gosub      2 A
       117968  +    **        sqlite3WhereEnd()
       117969  +    **
       117970  +    ** Following the above, code to terminate the loop. Label A, the target
       117971  +    ** of the Gosub above, jumps to the instruction right after the Goto.
       117972  +    **
       117973  +    **          Null       1                # Zero the rowset in reg 1
       117974  +    **          Goto       B                # The loop is finished.
       117975  +    **
       117976  +    **       A: <loop body>                 # Return data, whatever.
       117977  +    **
       117978  +    **          Return     2                # Jump back to the Gosub
       117979  +    **
       117980  +    **       B: <after the loop>
       117981  +    **
       117982  +    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
       117983  +    ** use an ephemeral index instead of a RowSet to record the primary
       117984  +    ** keys of the rows we have already seen.
       117985  +    **
       117986  +    */
       117987  +    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
       117988  +    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
       117989  +    Index *pCov = 0;             /* Potential covering index (or NULL) */
       117990  +    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
       117991  +
       117992  +    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
       117993  +    int regRowset = 0;                        /* Register for RowSet object */
       117994  +    int regRowid = 0;                         /* Register holding rowid */
       117995  +    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
       117996  +    int iRetInit;                             /* Address of regReturn init */
       117997  +    int untestedTerms = 0;             /* Some terms not completely tested */
       117998  +    int ii;                            /* Loop counter */
       117999  +    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
       118000  +    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
       118001  +    Table *pTab = pTabItem->pTab;
       118002  +   
       118003  +    pTerm = pLoop->aLTerm[0];
       118004  +    assert( pTerm!=0 );
       118005  +    assert( pTerm->eOperator & WO_OR );
       118006  +    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
       118007  +    pOrWc = &pTerm->u.pOrInfo->wc;
       118008  +    pLevel->op = OP_Return;
       118009  +    pLevel->p1 = regReturn;
       118010  +
       118011  +    /* Set up a new SrcList in pOrTab containing the table being scanned
       118012  +    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
       118013  +    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
       118014  +    */
       118015  +    if( pWInfo->nLevel>1 ){
       118016  +      int nNotReady;                 /* The number of notReady tables */
       118017  +      struct SrcList_item *origSrc;     /* Original list of tables */
       118018  +      nNotReady = pWInfo->nLevel - iLevel - 1;
       118019  +      pOrTab = sqlite3StackAllocRaw(db,
       118020  +                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
       118021  +      if( pOrTab==0 ) return notReady;
       118022  +      pOrTab->nAlloc = (u8)(nNotReady + 1);
       118023  +      pOrTab->nSrc = pOrTab->nAlloc;
       118024  +      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
       118025  +      origSrc = pWInfo->pTabList->a;
       118026  +      for(k=1; k<=nNotReady; k++){
       118027  +        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
       118028  +      }
       118029  +    }else{
       118030  +      pOrTab = pWInfo->pTabList;
       118031  +    }
       118032  +
       118033  +    /* Initialize the rowset register to contain NULL. An SQL NULL is 
       118034  +    ** equivalent to an empty rowset.  Or, create an ephemeral index
       118035  +    ** capable of holding primary keys in the case of a WITHOUT ROWID.
       118036  +    **
       118037  +    ** Also initialize regReturn to contain the address of the instruction 
       118038  +    ** immediately following the OP_Return at the bottom of the loop. This
       118039  +    ** is required in a few obscure LEFT JOIN cases where control jumps
       118040  +    ** over the top of the loop into the body of it. In this case the 
       118041  +    ** correct response for the end-of-loop code (the OP_Return) is to 
       118042  +    ** fall through to the next instruction, just as an OP_Next does if
       118043  +    ** called on an uninitialized cursor.
       118044  +    */
       118045  +    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
       118046  +      if( HasRowid(pTab) ){
       118047  +        regRowset = ++pParse->nMem;
       118048  +        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
       118049  +      }else{
       118050  +        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
       118051  +        regRowset = pParse->nTab++;
       118052  +        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
       118053  +        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
       118054  +      }
       118055  +      regRowid = ++pParse->nMem;
       118056  +    }
       118057  +    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
       118058  +
       118059  +    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
       118060  +    ** Then for every term xN, evaluate as the subexpression: xN AND z
       118061  +    ** That way, terms in y that are factored into the disjunction will
       118062  +    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
       118063  +    **
       118064  +    ** Actually, each subexpression is converted to "xN AND w" where w is
       118065  +    ** the "interesting" terms of z - terms that did not originate in the
       118066  +    ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
       118067  +    ** indices.
       118068  +    **
       118069  +    ** This optimization also only applies if the (x1 OR x2 OR ...) term
       118070  +    ** is not contained in the ON clause of a LEFT JOIN.
       118071  +    ** See ticket http://www.sqlite.org/src/info/f2369304e4
       118072  +    */
       118073  +    if( pWC->nTerm>1 ){
       118074  +      int iTerm;
       118075  +      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
       118076  +        Expr *pExpr = pWC->a[iTerm].pExpr;
       118077  +        if( &pWC->a[iTerm] == pTerm ) continue;
       118078  +        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
       118079  +        if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
       118080  +        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
       118081  +        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
       118082  +        pExpr = sqlite3ExprDup(db, pExpr, 0);
       118083  +        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
       118084  +      }
       118085  +      if( pAndExpr ){
       118086  +        pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
       118087  +      }
       118088  +    }
       118089  +
       118090  +    /* Run a separate WHERE clause for each term of the OR clause.  After
       118091  +    ** eliminating duplicates from other WHERE clauses, the action for each
       118092  +    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
       118093  +    */
       118094  +    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE
       118095  +                | WHERE_FORCE_TABLE
       118096  +                | WHERE_ONETABLE_ONLY
       118097  +                | WHERE_NO_AUTOINDEX;
       118098  +    for(ii=0; ii<pOrWc->nTerm; ii++){
       118099  +      WhereTerm *pOrTerm = &pOrWc->a[ii];
       118100  +      if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
       118101  +        WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
       118102  +        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
       118103  +        int j1 = 0;                     /* Address of jump operation */
       118104  +        if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
       118105  +          pAndExpr->pLeft = pOrExpr;
       118106  +          pOrExpr = pAndExpr;
       118107  +        }
       118108  +        /* Loop through table entries that match term pOrTerm. */
       118109  +        WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
       118110  +        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
       118111  +                                      wctrlFlags, iCovCur);
       118112  +        assert( pSubWInfo || pParse->nErr || db->mallocFailed );
       118113  +        if( pSubWInfo ){
       118114  +          WhereLoop *pSubLoop;
       118115  +          int addrExplain = sqlite3WhereExplainOneScan(
       118116  +              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
       118117  +          );
       118118  +          sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
       118119  +
       118120  +          /* This is the sub-WHERE clause body.  First skip over
       118121  +          ** duplicate rows from prior sub-WHERE clauses, and record the
       118122  +          ** rowid (or PRIMARY KEY) for the current row so that the same
       118123  +          ** row will be skipped in subsequent sub-WHERE clauses.
       118124  +          */
       118125  +          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
       118126  +            int r;
       118127  +            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
       118128  +            if( HasRowid(pTab) ){
       118129  +              r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
       118130  +              j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
       118131  +              VdbeCoverage(v);
       118132  +            }else{
       118133  +              Index *pPk = sqlite3PrimaryKeyIndex(pTab);
       118134  +              int nPk = pPk->nKeyCol;
       118135  +              int iPk;
       118136  +
       118137  +              /* Read the PK into an array of temp registers. */
       118138  +              r = sqlite3GetTempRange(pParse, nPk);
       118139  +              for(iPk=0; iPk<nPk; iPk++){
       118140  +                int iCol = pPk->aiColumn[iPk];
       118141  +                sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
       118142  +              }
       118143  +
       118144  +              /* Check if the temp table already contains this key. If so,
       118145  +              ** the row has already been included in the result set and
       118146  +              ** can be ignored (by jumping past the Gosub below). Otherwise,
       118147  +              ** insert the key into the temp table and proceed with processing
       118148  +              ** the row.
       118149  +              **
       118150  +              ** Use some of the same optimizations as OP_RowSetTest: If iSet
       118151  +              ** is zero, assume that the key cannot already be present in
       118152  +              ** the temp table. And if iSet is -1, assume that there is no 
       118153  +              ** need to insert the key into the temp table, as it will never 
       118154  +              ** be tested for.  */ 
       118155  +              if( iSet ){
       118156  +                j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
       118157  +                VdbeCoverage(v);
       118158  +              }
       118159  +              if( iSet>=0 ){
       118160  +                sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
       118161  +                sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
       118162  +                if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
       118163  +              }
       118164  +
       118165  +              /* Release the array of temp registers */
       118166  +              sqlite3ReleaseTempRange(pParse, r, nPk);
       118167  +            }
       118168  +          }
       118169  +
       118170  +          /* Invoke the main loop body as a subroutine */
       118171  +          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
       118172  +
       118173  +          /* Jump here (skipping the main loop body subroutine) if the
       118174  +          ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
       118175  +          if( j1 ) sqlite3VdbeJumpHere(v, j1);
       118176  +
       118177  +          /* The pSubWInfo->untestedTerms flag means that this OR term
       118178  +          ** contained one or more AND term from a notReady table.  The
       118179  +          ** terms from the notReady table could not be tested and will
       118180  +          ** need to be tested later.
       118181  +          */
       118182  +          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
       118183  +
       118184  +          /* If all of the OR-connected terms are optimized using the same
       118185  +          ** index, and the index is opened using the same cursor number
       118186  +          ** by each call to sqlite3WhereBegin() made by this loop, it may
       118187  +          ** be possible to use that index as a covering index.
       118188  +          **
       118189  +          ** If the call to sqlite3WhereBegin() above resulted in a scan that
       118190  +          ** uses an index, and this is either the first OR-connected term
       118191  +          ** processed or the index is the same as that used by all previous
       118192  +          ** terms, set pCov to the candidate covering index. Otherwise, set 
       118193  +          ** pCov to NULL to indicate that no candidate covering index will 
       118194  +          ** be available.
       118195  +          */
       118196  +          pSubLoop = pSubWInfo->a[0].pWLoop;
       118197  +          assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
       118198  +          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
       118199  +           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
       118200  +           && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
       118201  +          ){
       118202  +            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
       118203  +            pCov = pSubLoop->u.btree.pIndex;
       118204  +            wctrlFlags |= WHERE_REOPEN_IDX;
       118205  +          }else{
       118206  +            pCov = 0;
       118207  +          }
       118208  +
       118209  +          /* Finish the loop through table entries that match term pOrTerm. */
       118210  +          sqlite3WhereEnd(pSubWInfo);
       118211  +        }
       118212  +      }
       118213  +    }
       118214  +    pLevel->u.pCovidx = pCov;
       118215  +    if( pCov ) pLevel->iIdxCur = iCovCur;
       118216  +    if( pAndExpr ){
       118217  +      pAndExpr->pLeft = 0;
       118218  +      sqlite3ExprDelete(db, pAndExpr);
       118219  +    }
       118220  +    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
       118221  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
       118222  +    sqlite3VdbeResolveLabel(v, iLoopBody);
       118223  +
       118224  +    if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
       118225  +    if( !untestedTerms ) disableTerm(pLevel, pTerm);
       118226  +  }else
       118227  +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
       118228  +
       118229  +  {
       118230  +    /* Case 6:  There is no usable index.  We must do a complete
       118231  +    **          scan of the entire table.
       118232  +    */
       118233  +    static const u8 aStep[] = { OP_Next, OP_Prev };
       118234  +    static const u8 aStart[] = { OP_Rewind, OP_Last };
       118235  +    assert( bRev==0 || bRev==1 );
       118236  +    if( pTabItem->isRecursive ){
       118237  +      /* Tables marked isRecursive have only a single row that is stored in
       118238  +      ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
       118239  +      pLevel->op = OP_Noop;
       118240  +    }else{
       118241  +      pLevel->op = aStep[bRev];
       118242  +      pLevel->p1 = iCur;
       118243  +      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
       118244  +      VdbeCoverageIf(v, bRev==0);
       118245  +      VdbeCoverageIf(v, bRev!=0);
       118246  +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
       118247  +    }
       118248  +  }
       118249  +
       118250  +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
       118251  +  pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
       118252  +#endif
       118253  +
       118254  +  /* Insert code to test every subexpression that can be completely
       118255  +  ** computed using the current set of tables.
       118256  +  */
       118257  +  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
       118258  +    Expr *pE;
       118259  +    int skipLikeAddr = 0;
       118260  +    testcase( pTerm->wtFlags & TERM_VIRTUAL );
       118261  +    testcase( pTerm->wtFlags & TERM_CODED );
       118262  +    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
       118263  +    if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
       118264  +      testcase( pWInfo->untestedTerms==0
       118265  +               && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
       118266  +      pWInfo->untestedTerms = 1;
       118267  +      continue;
       118268  +    }
       118269  +    pE = pTerm->pExpr;
       118270  +    assert( pE!=0 );
       118271  +    if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
       118272  +      continue;
       118273  +    }
       118274  +    if( pTerm->wtFlags & TERM_LIKECOND ){
       118275  +      assert( pLevel->iLikeRepCntr>0 );
       118276  +      skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
       118277  +      VdbeCoverage(v);
       118278  +    }
       118279  +    sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
       118280  +    if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
       118281  +    pTerm->wtFlags |= TERM_CODED;
       118282  +  }
       118283  +
       118284  +  /* Insert code to test for implied constraints based on transitivity
       118285  +  ** of the "==" operator.
       118286  +  **
       118287  +  ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
       118288  +  ** and we are coding the t1 loop and the t2 loop has not yet coded,
       118289  +  ** then we cannot use the "t1.a=t2.b" constraint, but we can code
       118290  +  ** the implied "t1.a=123" constraint.
       118291  +  */
       118292  +  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
       118293  +    Expr *pE, *pEAlt;
       118294  +    WhereTerm *pAlt;
       118295  +    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
       118296  +    if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
       118297  +    if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
       118298  +    if( pTerm->leftCursor!=iCur ) continue;
       118299  +    if( pLevel->iLeftJoin ) continue;
       118300  +    pE = pTerm->pExpr;
       118301  +    assert( !ExprHasProperty(pE, EP_FromJoin) );
       118302  +    assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
       118303  +    pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
       118304  +                    WO_EQ|WO_IN|WO_IS, 0);
       118305  +    if( pAlt==0 ) continue;
       118306  +    if( pAlt->wtFlags & (TERM_CODED) ) continue;
       118307  +    testcase( pAlt->eOperator & WO_EQ );
       118308  +    testcase( pAlt->eOperator & WO_IS );
       118309  +    testcase( pAlt->eOperator & WO_IN );
       118310  +    VdbeModuleComment((v, "begin transitive constraint"));
       118311  +    pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
       118312  +    if( pEAlt ){
       118313  +      *pEAlt = *pAlt->pExpr;
       118314  +      pEAlt->pLeft = pE->pLeft;
       118315  +      sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
       118316  +      sqlite3StackFree(db, pEAlt);
       118317  +    }
       118318  +  }
       118319  +
       118320  +  /* For a LEFT OUTER JOIN, generate code that will record the fact that
       118321  +  ** at least one row of the right table has matched the left table.  
       118322  +  */
       118323  +  if( pLevel->iLeftJoin ){
       118324  +    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
       118325  +    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
       118326  +    VdbeComment((v, "record LEFT JOIN hit"));
       118327  +    sqlite3ExprCacheClear(pParse);
       118328  +    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
       118329  +      testcase( pTerm->wtFlags & TERM_VIRTUAL );
       118330  +      testcase( pTerm->wtFlags & TERM_CODED );
       118331  +      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
       118332  +      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
       118333  +        assert( pWInfo->untestedTerms );
       118334  +        continue;
       118335  +      }
       118336  +      assert( pTerm->pExpr );
       118337  +      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
       118338  +      pTerm->wtFlags |= TERM_CODED;
       118339  +    }
       118340  +  }
       118341  +
       118342  +  return pLevel->notReady;
       118343  +}
       118344  +
       118345  +/************** End of wherecode.c *******************************************/
       118346  +/************** Begin file whereexpr.c ***************************************/
       118347  +/*
       118348  +** 2015-06-08
       118349  +**
       118350  +** The author disclaims copyright to this source code.  In place of
       118351  +** a legal notice, here is a blessing:
       118352  +**
       118353  +**    May you do good and not evil.
       118354  +**    May you find forgiveness for yourself and forgive others.
       118355  +**    May you share freely, never taking more than you give.
       118356  +**
       118357  +*************************************************************************
       118358  +** This module contains C code that generates VDBE code used to process
       118359  +** the WHERE clause of SQL statements.
       118360  +**
       118361  +** This file was originally part of where.c but was split out to improve
       118362  +** readability and editabiliity.  This file contains utility routines for
       118363  +** analyzing Expr objects in the WHERE clause.
       118364  +*/
       118365  +
       118366  +/* Forward declarations */
       118367  +static void exprAnalyze(SrcList*, WhereClause*, int);
116438 118368   
116439 118369   /*
116440 118370   ** Deallocate all memory associated with a WhereOrInfo object.
116441 118371   */
116442 118372   static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
116443         -  whereClauseClear(&p->wc);
       118373  +  sqlite3WhereClauseClear(&p->wc);
116444 118374     sqlite3DbFree(db, p);
116445 118375   }
116446 118376   
116447 118377   /*
116448 118378   ** Deallocate all memory associated with a WhereAndInfo object.
116449 118379   */
116450 118380   static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
116451         -  whereClauseClear(&p->wc);
       118381  +  sqlite3WhereClauseClear(&p->wc);
116452 118382     sqlite3DbFree(db, p);
116453 118383   }
116454 118384   
116455         -/*
116456         -** Deallocate a WhereClause structure.  The WhereClause structure
116457         -** itself is not freed.  This routine is the inverse of whereClauseInit().
116458         -*/
116459         -static void whereClauseClear(WhereClause *pWC){
116460         -  int i;
116461         -  WhereTerm *a;
116462         -  sqlite3 *db = pWC->pWInfo->pParse->db;
116463         -  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
116464         -    if( a->wtFlags & TERM_DYNAMIC ){
116465         -      sqlite3ExprDelete(db, a->pExpr);
116466         -    }
116467         -    if( a->wtFlags & TERM_ORINFO ){
116468         -      whereOrInfoDelete(db, a->u.pOrInfo);
116469         -    }else if( a->wtFlags & TERM_ANDINFO ){
116470         -      whereAndInfoDelete(db, a->u.pAndInfo);
116471         -    }
116472         -  }
116473         -  if( pWC->a!=pWC->aStatic ){
116474         -    sqlite3DbFree(db, pWC->a);
116475         -  }
116476         -}
116477         -
116478 118385   /*
116479 118386   ** Add a single new WhereTerm entry to the WhereClause object pWC.
116480 118387   ** The new WhereTerm object is constructed from Expr p and with wtFlags.
116481 118388   ** The index in pWC->a[] of the new WhereTerm is returned on success.
116482 118389   ** 0 is returned if the new WhereTerm could not be added due to a memory
116483 118390   ** allocation error.  The memory allocation failure will be recorded in
116484 118391   ** the db->mallocFailed flag so that higher-level functions can detect it.
................................................................................
116525 118432     pTerm->pExpr = sqlite3ExprSkipCollate(p);
116526 118433     pTerm->wtFlags = wtFlags;
116527 118434     pTerm->pWC = pWC;
116528 118435     pTerm->iParent = -1;
116529 118436     return idx;
116530 118437   }
116531 118438   
116532         -/*
116533         -** This routine identifies subexpressions in the WHERE clause where
116534         -** each subexpression is separated by the AND operator or some other
116535         -** operator specified in the op parameter.  The WhereClause structure
116536         -** is filled with pointers to subexpressions.  For example:
116537         -**
116538         -**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
116539         -**           \________/     \_______________/     \________________/
116540         -**            slot[0]            slot[1]               slot[2]
116541         -**
116542         -** The original WHERE clause in pExpr is unaltered.  All this routine
116543         -** does is make slot[] entries point to substructure within pExpr.
116544         -**
116545         -** In the previous sentence and in the diagram, "slot[]" refers to
116546         -** the WhereClause.a[] array.  The slot[] array grows as needed to contain
116547         -** all terms of the WHERE clause.
116548         -*/
116549         -static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
116550         -  Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
116551         -  pWC->op = op;
116552         -  if( pE2==0 ) return;
116553         -  if( pE2->op!=op ){
116554         -    whereClauseInsert(pWC, pExpr, 0);
116555         -  }else{
116556         -    whereSplit(pWC, pE2->pLeft, op);
116557         -    whereSplit(pWC, pE2->pRight, op);
116558         -  }
116559         -}
116560         -
116561         -/*
116562         -** Initialize a WhereMaskSet object
116563         -*/
116564         -#define initMaskSet(P)  (P)->n=0
116565         -
116566         -/*
116567         -** Return the bitmask for the given cursor number.  Return 0 if
116568         -** iCursor is not in the set.
116569         -*/
116570         -static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
116571         -  int i;
116572         -  assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
116573         -  for(i=0; i<pMaskSet->n; i++){
116574         -    if( pMaskSet->ix[i]==iCursor ){
116575         -      return MASKBIT(i);
116576         -    }
116577         -  }
116578         -  return 0;
116579         -}
116580         -
116581         -/*
116582         -** Create a new mask for cursor iCursor.
116583         -**
116584         -** There is one cursor per table in the FROM clause.  The number of
116585         -** tables in the FROM clause is limited by a test early in the
116586         -** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
116587         -** array will never overflow.
116588         -*/
116589         -static void createMask(WhereMaskSet *pMaskSet, int iCursor){
116590         -  assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
116591         -  pMaskSet->ix[pMaskSet->n++] = iCursor;
116592         -}
116593         -
116594         -/*
116595         -** These routines walk (recursively) an expression tree and generate
116596         -** a bitmask indicating which tables are used in that expression
116597         -** tree.
116598         -*/
116599         -static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
116600         -static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
116601         -static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
116602         -  Bitmask mask = 0;
116603         -  if( p==0 ) return 0;
116604         -  if( p->op==TK_COLUMN ){
116605         -    mask = getMask(pMaskSet, p->iTable);
116606         -    return mask;
116607         -  }
116608         -  mask = exprTableUsage(pMaskSet, p->pRight);
116609         -  mask |= exprTableUsage(pMaskSet, p->pLeft);
116610         -  if( ExprHasProperty(p, EP_xIsSelect) ){
116611         -    mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
116612         -  }else{
116613         -    mask |= exprListTableUsage(pMaskSet, p->x.pList);
116614         -  }
116615         -  return mask;
116616         -}
116617         -static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
116618         -  int i;
116619         -  Bitmask mask = 0;
116620         -  if( pList ){
116621         -    for(i=0; i<pList->nExpr; i++){
116622         -      mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
116623         -    }
116624         -  }
116625         -  return mask;
116626         -}
116627         -static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
116628         -  Bitmask mask = 0;
116629         -  while( pS ){
116630         -    SrcList *pSrc = pS->pSrc;
116631         -    mask |= exprListTableUsage(pMaskSet, pS->pEList);
116632         -    mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
116633         -    mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
116634         -    mask |= exprTableUsage(pMaskSet, pS->pWhere);
116635         -    mask |= exprTableUsage(pMaskSet, pS->pHaving);
116636         -    if( ALWAYS(pSrc!=0) ){
116637         -      int i;
116638         -      for(i=0; i<pSrc->nSrc; i++){
116639         -        mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
116640         -        mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
116641         -      }
116642         -    }
116643         -    pS = pS->pPrior;
116644         -  }
116645         -  return mask;
116646         -}
116647         -
116648 118439   /*
116649 118440   ** Return TRUE if the given operator is one of the operators that is
116650 118441   ** allowed for an indexable WHERE clause term.  The allowed operators are
116651 118442   ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
116652 118443   */
116653 118444   static int allowedOp(int op){
116654 118445     assert( TK_GT>TK_EQ && TK_GT<TK_GE );
................................................................................
116721 118512     assert( op!=TK_LE || c==WO_LE );
116722 118513     assert( op!=TK_GT || c==WO_GT );
116723 118514     assert( op!=TK_GE || c==WO_GE );
116724 118515     assert( op!=TK_IS || c==WO_IS );
116725 118516     return c;
116726 118517   }
116727 118518   
116728         -/*
116729         -** Advance to the next WhereTerm that matches according to the criteria
116730         -** established when the pScan object was initialized by whereScanInit().
116731         -** Return NULL if there are no more matching WhereTerms.
116732         -*/
116733         -static WhereTerm *whereScanNext(WhereScan *pScan){
116734         -  int iCur;            /* The cursor on the LHS of the term */
116735         -  int iColumn;         /* The column on the LHS of the term.  -1 for IPK */
116736         -  Expr *pX;            /* An expression being tested */
116737         -  WhereClause *pWC;    /* Shorthand for pScan->pWC */
116738         -  WhereTerm *pTerm;    /* The term being tested */
116739         -  int k = pScan->k;    /* Where to start scanning */
116740         -
116741         -  while( pScan->iEquiv<=pScan->nEquiv ){
116742         -    iCur = pScan->aEquiv[pScan->iEquiv-2];
116743         -    iColumn = pScan->aEquiv[pScan->iEquiv-1];
116744         -    while( (pWC = pScan->pWC)!=0 ){
116745         -      for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
116746         -        if( pTerm->leftCursor==iCur
116747         -         && pTerm->u.leftColumn==iColumn
116748         -         && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
116749         -        ){
116750         -          if( (pTerm->eOperator & WO_EQUIV)!=0
116751         -           && pScan->nEquiv<ArraySize(pScan->aEquiv)
116752         -          ){
116753         -            int j;
116754         -            pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
116755         -            assert( pX->op==TK_COLUMN );
116756         -            for(j=0; j<pScan->nEquiv; j+=2){
116757         -              if( pScan->aEquiv[j]==pX->iTable
116758         -               && pScan->aEquiv[j+1]==pX->iColumn ){
116759         -                  break;
116760         -              }
116761         -            }
116762         -            if( j==pScan->nEquiv ){
116763         -              pScan->aEquiv[j] = pX->iTable;
116764         -              pScan->aEquiv[j+1] = pX->iColumn;
116765         -              pScan->nEquiv += 2;
116766         -            }
116767         -          }
116768         -          if( (pTerm->eOperator & pScan->opMask)!=0 ){
116769         -            /* Verify the affinity and collating sequence match */
116770         -            if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
116771         -              CollSeq *pColl;
116772         -              Parse *pParse = pWC->pWInfo->pParse;
116773         -              pX = pTerm->pExpr;
116774         -              if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
116775         -                continue;
116776         -              }
116777         -              assert(pX->pLeft);
116778         -              pColl = sqlite3BinaryCompareCollSeq(pParse,
116779         -                                                  pX->pLeft, pX->pRight);
116780         -              if( pColl==0 ) pColl = pParse->db->pDfltColl;
116781         -              if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
116782         -                continue;
116783         -              }
116784         -            }
116785         -            if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
116786         -             && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
116787         -             && pX->iTable==pScan->aEquiv[0]
116788         -             && pX->iColumn==pScan->aEquiv[1]
116789         -            ){
116790         -              testcase( pTerm->eOperator & WO_IS );
116791         -              continue;
116792         -            }
116793         -            pScan->k = k+1;
116794         -            return pTerm;
116795         -          }
116796         -        }
116797         -      }
116798         -      pScan->pWC = pScan->pWC->pOuter;
116799         -      k = 0;
116800         -    }
116801         -    pScan->pWC = pScan->pOrigWC;
116802         -    k = 0;
116803         -    pScan->iEquiv += 2;
116804         -  }
116805         -  return 0;
116806         -}
116807         -
116808         -/*
116809         -** Initialize a WHERE clause scanner object.  Return a pointer to the
116810         -** first match.  Return NULL if there are no matches.
116811         -**
116812         -** The scanner will be searching the WHERE clause pWC.  It will look
116813         -** for terms of the form "X <op> <expr>" where X is column iColumn of table
116814         -** iCur.  The <op> must be one of the operators described by opMask.
116815         -**
116816         -** If the search is for X and the WHERE clause contains terms of the
116817         -** form X=Y then this routine might also return terms of the form
116818         -** "Y <op> <expr>".  The number of levels of transitivity is limited,
116819         -** but is enough to handle most commonly occurring SQL statements.
116820         -**
116821         -** If X is not the INTEGER PRIMARY KEY then X must be compatible with
116822         -** index pIdx.
116823         -*/
116824         -static WhereTerm *whereScanInit(
116825         -  WhereScan *pScan,       /* The WhereScan object being initialized */
116826         -  WhereClause *pWC,       /* The WHERE clause to be scanned */
116827         -  int iCur,               /* Cursor to scan for */
116828         -  int iColumn,            /* Column to scan for */
116829         -  u32 opMask,             /* Operator(s) to scan for */
116830         -  Index *pIdx             /* Must be compatible with this index */
116831         -){
116832         -  int j;
116833         -
116834         -  /* memset(pScan, 0, sizeof(*pScan)); */
116835         -  pScan->pOrigWC = pWC;
116836         -  pScan->pWC = pWC;
116837         -  if( pIdx && iColumn>=0 ){
116838         -    pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
116839         -    for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
116840         -      if( NEVER(j>pIdx->nColumn) ) return 0;
116841         -    }
116842         -    pScan->zCollName = pIdx->azColl[j];
116843         -  }else{
116844         -    pScan->idxaff = 0;
116845         -    pScan->zCollName = 0;
116846         -  }
116847         -  pScan->opMask = opMask;
116848         -  pScan->k = 0;
116849         -  pScan->aEquiv[0] = iCur;
116850         -  pScan->aEquiv[1] = iColumn;
116851         -  pScan->nEquiv = 2;
116852         -  pScan->iEquiv = 2;
116853         -  return whereScanNext(pScan);
116854         -}
116855         -
116856         -/*
116857         -** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
116858         -** where X is a reference to the iColumn of table iCur and <op> is one of
116859         -** the WO_xx operator codes specified by the op parameter.
116860         -** Return a pointer to the term.  Return 0 if not found.
116861         -**
116862         -** The term returned might by Y=<expr> if there is another constraint in
116863         -** the WHERE clause that specifies that X=Y.  Any such constraints will be
116864         -** identified by the WO_EQUIV bit in the pTerm->eOperator field.  The
116865         -** aEquiv[] array holds X and all its equivalents, with each SQL variable
116866         -** taking up two slots in aEquiv[].  The first slot is for the cursor number
116867         -** and the second is for the column number.  There are 22 slots in aEquiv[]
116868         -** so that means we can look for X plus up to 10 other equivalent values.
116869         -** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
116870         -** and ... and A9=A10 and A10=<expr>.
116871         -**
116872         -** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
116873         -** then try for the one with no dependencies on <expr> - in other words where
116874         -** <expr> is a constant expression of some kind.  Only return entries of
116875         -** the form "X <op> Y" where Y is a column in another table if no terms of
116876         -** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
116877         -** exist, try to return a term that does not use WO_EQUIV.
116878         -*/
116879         -static WhereTerm *findTerm(
116880         -  WhereClause *pWC,     /* The WHERE clause to be searched */
116881         -  int iCur,             /* Cursor number of LHS */
116882         -  int iColumn,          /* Column number of LHS */
116883         -  Bitmask notReady,     /* RHS must not overlap with this mask */
116884         -  u32 op,               /* Mask of WO_xx values describing operator */
116885         -  Index *pIdx           /* Must be compatible with this index, if not NULL */
116886         -){
116887         -  WhereTerm *pResult = 0;
116888         -  WhereTerm *p;
116889         -  WhereScan scan;
116890         -
116891         -  p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
116892         -  op &= WO_EQ|WO_IS;
116893         -  while( p ){
116894         -    if( (p->prereqRight & notReady)==0 ){
116895         -      if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
116896         -        testcase( p->eOperator & WO_IS );
116897         -        return p;
116898         -      }
116899         -      if( pResult==0 ) pResult = p;
116900         -    }
116901         -    p = whereScanNext(&scan);
116902         -  }
116903         -  return pResult;
116904         -}
116905         -
116906         -/* Forward reference */
116907         -static void exprAnalyze(SrcList*, WhereClause*, int);
116908         -
116909         -/*
116910         -** Call exprAnalyze on all terms in a WHERE clause.  
116911         -*/
116912         -static void exprAnalyzeAll(
116913         -  SrcList *pTabList,       /* the FROM clause */
116914         -  WhereClause *pWC         /* the WHERE clause to be analyzed */
116915         -){
116916         -  int i;
116917         -  for(i=pWC->nTerm-1; i>=0; i--){
116918         -    exprAnalyze(pTabList, pWC, i);
116919         -  }
116920         -}
116921 118519   
116922 118520   #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
116923 118521   /*
116924 118522   ** Check to see if the given expression is a LIKE or GLOB operator that
116925 118523   ** can be optimized using inequality constraints.  Return TRUE if it is
116926 118524   ** so and false if not.
116927 118525   **
................................................................................
116968 118566     assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
116969 118567   
116970 118568     pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
116971 118569     op = pRight->op;
116972 118570     if( op==TK_VARIABLE ){
116973 118571       Vdbe *pReprepare = pParse->pReprepare;
116974 118572       int iCol = pRight->iColumn;
116975         -    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
       118573  +    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
116976 118574       if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
116977 118575         z = (char *)sqlite3_value_text(pVal);
116978 118576       }
116979 118577       sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
116980 118578       assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
116981 118579     }else if( op==TK_STRING ){
116982 118580       z = pRight->u.zToken;
................................................................................
117179 118777   **
117180 118778   ** then create a new virtual term like this:
117181 118779   **
117182 118780   **      x IN (expr1,expr2,expr3)
117183 118781   **
117184 118782   ** CASE 2:
117185 118783   **
117186         -** If there are exactly two disjuncts one side has x>A and the other side
       118784  +** If there are exactly two disjuncts and one side has x>A and the other side
117187 118785   ** has x=A (for the same x and A) then add a new virtual conjunct term to the
117188 118786   ** WHERE clause of the form "x>=A".  Example:
117189 118787   **
117190 118788   **      x>A OR (x=A AND y>B)    adds:    x>=A
117191 118789   **
117192 118790   ** The added conjunct can sometimes be helpful in query planning.
117193 118791   **
................................................................................
117208 118806   **
117209 118807   ** From another point of view, "indexable" means that the subterm could
117210 118808   ** potentially be used with an index if an appropriate index exists.
117211 118809   ** This analysis does not consider whether or not the index exists; that
117212 118810   ** is decided elsewhere.  This analysis only looks at whether subterms
117213 118811   ** appropriate for indexing exist.
117214 118812   **
117215         -** All examples A through E above satisfy case 2.  But if a term
       118813  +** All examples A through E above satisfy case 3.  But if a term
117216 118814   ** also satisfies case 1 (such as B) we know that the optimizer will
117217         -** always prefer case 1, so in that case we pretend that case 2 is not
       118815  +** always prefer case 1, so in that case we pretend that case 3 is not
117218 118816   ** satisfied.
117219 118817   **
117220 118818   ** It might be the case that multiple tables are indexable.  For example,
117221 118819   ** (E) above is indexable on tables P, Q, and R.
117222 118820   **
117223         -** Terms that satisfy case 2 are candidates for lookup by using
       118821  +** Terms that satisfy case 3 are candidates for lookup by using
117224 118822   ** separate indices to find rowids for each subterm and composing
117225 118823   ** the union of all rowids using a RowSet object.  This is similar
117226 118824   ** to "bitmap indices" in other database engines.
117227 118825   **
117228 118826   ** OTHERWISE:
117229 118827   **
117230         -** If neither case 1 nor case 2 apply, then leave the eOperator set to
       118828  +** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
117231 118829   ** zero.  This term is not useful for search.
117232 118830   */
117233 118831   static void exprAnalyzeOrTerm(
117234 118832     SrcList *pSrc,            /* the FROM clause */
117235 118833     WhereClause *pWC,         /* the complete WHERE clause */
117236 118834     int idxTerm               /* Index of the OR-term to be analyzed */
117237 118835   ){
................................................................................
117254 118852     */
117255 118853     assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
117256 118854     assert( pExpr->op==TK_OR );
117257 118855     pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
117258 118856     if( pOrInfo==0 ) return;
117259 118857     pTerm->wtFlags |= TERM_ORINFO;
117260 118858     pOrWc = &pOrInfo->wc;
117261         -  whereClauseInit(pOrWc, pWInfo);
117262         -  whereSplit(pOrWc, pExpr, TK_OR);
117263         -  exprAnalyzeAll(pSrc, pOrWc);
       118859  +  sqlite3WhereClauseInit(pOrWc, pWInfo);
       118860  +  sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
       118861  +  sqlite3WhereExprAnalyze(pSrc, pOrWc);
117264 118862     if( db->mallocFailed ) return;
117265 118863     assert( pOrWc->nTerm>=2 );
117266 118864   
117267 118865     /*
117268         -  ** Compute the set of tables that might satisfy cases 1 or 2.
       118866  +  ** Compute the set of tables that might satisfy cases 1 or 3.
117269 118867     */
117270 118868     indexable = ~(Bitmask)0;
117271 118869     chngToIN = ~(Bitmask)0;
117272 118870     for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
117273 118871       if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
117274 118872         WhereAndInfo *pAndInfo;
117275 118873         assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
................................................................................
117280 118878           WhereTerm *pAndTerm;
117281 118879           int j;
117282 118880           Bitmask b = 0;
117283 118881           pOrTerm->u.pAndInfo = pAndInfo;
117284 118882           pOrTerm->wtFlags |= TERM_ANDINFO;
117285 118883           pOrTerm->eOperator = WO_AND;
117286 118884           pAndWC = &pAndInfo->wc;
117287         -        whereClauseInit(pAndWC, pWC->pWInfo);
117288         -        whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
117289         -        exprAnalyzeAll(pSrc, pAndWC);
       118885  +        sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
       118886  +        sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
       118887  +        sqlite3WhereExprAnalyze(pSrc, pAndWC);
117290 118888           pAndWC->pOuter = pWC;
117291 118889           testcase( db->mallocFailed );
117292 118890           if( !db->mallocFailed ){
117293 118891             for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
117294 118892               assert( pAndTerm->pExpr );
117295 118893               if( allowedOp(pAndTerm->pExpr->op) ){
117296         -              b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
       118894  +              b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
117297 118895               }
117298 118896             }
117299 118897           }
117300 118898           indexable &= b;
117301 118899         }
117302 118900       }else if( pOrTerm->wtFlags & TERM_COPIED ){
117303 118901         /* Skip this term for now.  We revisit it when we process the
117304 118902         ** corresponding TERM_VIRTUAL term */
117305 118903       }else{
117306 118904         Bitmask b;
117307         -      b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
       118905  +      b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
117308 118906         if( pOrTerm->wtFlags & TERM_VIRTUAL ){
117309 118907           WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
117310         -        b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
       118908  +        b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
117311 118909         }
117312 118910         indexable &= b;
117313 118911         if( (pOrTerm->eOperator & WO_EQ)==0 ){
117314 118912           chngToIN = 0;
117315 118913         }else{
117316 118914           chngToIN &= b;
117317 118915         }
................................................................................
117379 118977           pOrTerm->wtFlags &= ~TERM_OR_OK;
117380 118978           if( pOrTerm->leftCursor==iCursor ){
117381 118979             /* This is the 2-bit case and we are on the second iteration and
117382 118980             ** current term is from the first iteration.  So skip this term. */
117383 118981             assert( j==1 );
117384 118982             continue;
117385 118983           }
117386         -        if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
       118984  +        if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
       118985  +                                            pOrTerm->leftCursor))==0 ){
117387 118986             /* This term must be of the form t1.a==t2.b where t2 is in the
117388 118987             ** chngToIN set but t1 is not.  This term will be either preceded
117389 118988             ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
117390 118989             ** and use its inversion. */
117391 118990             testcase( pOrTerm->wtFlags & TERM_COPIED );
117392 118991             testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
117393 118992             assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
................................................................................
117398 118997           break;
117399 118998         }
117400 118999         if( i<0 ){
117401 119000           /* No candidate table+column was found.  This can only occur
117402 119001           ** on the second iteration */
117403 119002           assert( j==1 );
117404 119003           assert( IsPowerOfTwo(chngToIN) );
117405         -        assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
       119004  +        assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
117406 119005           break;
117407 119006         }
117408 119007         testcase( j==1 );
117409 119008   
117410 119009         /* We have found a candidate table and column.  Check to see if that
117411 119010         ** table and column is common to every term in the OR clause */
117412 119011         okToChngToIN = 1;
................................................................................
117476 119075   
117477 119076   /*
117478 119077   ** We already know that pExpr is a binary operator where both operands are
117479 119078   ** column references.  This routine checks to see if pExpr is an equivalence
117480 119079   ** relation:
117481 119080   **   1.  The SQLITE_Transitive optimization must be enabled
117482 119081   **   2.  Must be either an == or an IS operator
117483         -**   3.  Not originating the ON clause of an OUTER JOIN
       119082  +**   3.  Not originating in the ON clause of an OUTER JOIN
117484 119083   **   4.  The affinities of A and B must be compatible
117485 119084   **   5a. Both operands use the same collating sequence OR
117486 119085   **   5b. The overall collating sequence is BINARY
117487 119086   ** If this routine returns TRUE, that means that the RHS can be substituted
117488 119087   ** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
117489 119088   ** This is an optimization.  No harm comes from returning 0.  But if 1 is
117490 119089   ** returned when it should not be, then incorrect answers might result.
................................................................................
117509 119108     /* Since pLeft and pRight are both a column references, their collating
117510 119109     ** sequence should always be defined. */
117511 119110     zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
117512 119111     pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
117513 119112     zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
117514 119113     return sqlite3StrICmp(zColl1, zColl2)==0;
117515 119114   }
       119115  +
       119116  +/*
       119117  +** Recursively walk the expressions of a SELECT statement and generate
       119118  +** a bitmask indicating which tables are used in that expression
       119119  +** tree.
       119120  +*/
       119121  +static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
       119122  +  Bitmask mask = 0;
       119123  +  while( pS ){
       119124  +    SrcList *pSrc = pS->pSrc;
       119125  +    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
       119126  +    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
       119127  +    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
       119128  +    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
       119129  +    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
       119130  +    if( ALWAYS(pSrc!=0) ){
       119131  +      int i;
       119132  +      for(i=0; i<pSrc->nSrc; i++){
       119133  +        mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
       119134  +        mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
       119135  +      }
       119136  +    }
       119137  +    pS = pS->pPrior;
       119138  +  }
       119139  +  return mask;
       119140  +}
117516 119141   
117517 119142   /*
117518 119143   ** The input to this routine is an WhereTerm structure with only the
117519 119144   ** "pExpr" field filled in.  The job of this routine is to analyze the
117520 119145   ** subexpression and populate all the other fields of the WhereTerm
117521 119146   ** structure.
117522 119147   **
................................................................................
117554 119179     if( db->mallocFailed ){
117555 119180       return;
117556 119181     }
117557 119182     pTerm = &pWC->a[idxTerm];
117558 119183     pMaskSet = &pWInfo->sMaskSet;
117559 119184     pExpr = pTerm->pExpr;
117560 119185     assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
117561         -  prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
       119186  +  prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
117562 119187     op = pExpr->op;
117563 119188     if( op==TK_IN ){
117564 119189       assert( pExpr->pRight==0 );
117565 119190       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
117566         -      pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
       119191  +      pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
117567 119192       }else{
117568         -      pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
       119193  +      pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
117569 119194       }
117570 119195     }else if( op==TK_ISNULL ){
117571 119196       pTerm->prereqRight = 0;
117572 119197     }else{
117573         -    pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
       119198  +    pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
117574 119199     }
117575         -  prereqAll = exprTableUsage(pMaskSet, pExpr);
       119200  +  prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr);
117576 119201     if( ExprHasProperty(pExpr, EP_FromJoin) ){
117577         -    Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
       119202  +    Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
117578 119203       prereqAll |= x;
117579 119204       extraRight = x-1;  /* ON clause terms may not be used with an index
117580 119205                          ** on left table of a LEFT JOIN.  Ticket #3015 */
117581 119206     }
117582 119207     pTerm->prereqAll = prereqAll;
117583 119208     pTerm->leftCursor = -1;
117584 119209     pTerm->iParent = -1;
................................................................................
117775 119400       int idxNew;
117776 119401       Expr *pRight, *pLeft;
117777 119402       WhereTerm *pNewTerm;
117778 119403       Bitmask prereqColumn, prereqExpr;
117779 119404   
117780 119405       pRight = pExpr->x.pList->a[0].pExpr;
117781 119406       pLeft = pExpr->x.pList->a[1].pExpr;
117782         -    prereqExpr = exprTableUsage(pMaskSet, pRight);
117783         -    prereqColumn = exprTableUsage(pMaskSet, pLeft);
       119407  +    prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
       119408  +    prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
117784 119409       if( (prereqExpr & prereqColumn)==0 ){
117785 119410         Expr *pNewExpr;
117786 119411         pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
117787 119412                                 0, sqlite3ExprDup(db, pRight, 0), 0);
117788 119413         idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
117789 119414         testcase( idxNew==0 );
117790 119415         pNewTerm = &pWC->a[idxNew];
................................................................................
117839 119464   #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
117840 119465   
117841 119466     /* Prevent ON clause terms of a LEFT JOIN from being used to drive
117842 119467     ** an index for tables to the left of the join.
117843 119468     */
117844 119469     pTerm->prereqRight |= extraRight;
117845 119470   }
       119471  +
       119472  +/***************************************************************************
       119473  +** Routines with file scope above.  Interface to the rest of the where.c
       119474  +** subsystem follows.
       119475  +***************************************************************************/
       119476  +
       119477  +/*
       119478  +** This routine identifies subexpressions in the WHERE clause where
       119479  +** each subexpression is separated by the AND operator or some other
       119480  +** operator specified in the op parameter.  The WhereClause structure
       119481  +** is filled with pointers to subexpressions.  For example:
       119482  +**
       119483  +**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
       119484  +**           \________/     \_______________/     \________________/
       119485  +**            slot[0]            slot[1]               slot[2]
       119486  +**
       119487  +** The original WHERE clause in pExpr is unaltered.  All this routine
       119488  +** does is make slot[] entries point to substructure within pExpr.
       119489  +**
       119490  +** In the previous sentence and in the diagram, "slot[]" refers to
       119491  +** the WhereClause.a[] array.  The slot[] array grows as needed to contain
       119492  +** all terms of the WHERE clause.
       119493  +*/
       119494  +SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
       119495  +  Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
       119496  +  pWC->op = op;
       119497  +  if( pE2==0 ) return;
       119498  +  if( pE2->op!=op ){
       119499  +    whereClauseInsert(pWC, pExpr, 0);
       119500  +  }else{
       119501  +    sqlite3WhereSplit(pWC, pE2->pLeft, op);
       119502  +    sqlite3WhereSplit(pWC, pE2->pRight, op);
       119503  +  }
       119504  +}
       119505  +
       119506  +/*
       119507  +** Initialize a preallocated WhereClause structure.
       119508  +*/
       119509  +SQLITE_PRIVATE void sqlite3WhereClauseInit(
       119510  +  WhereClause *pWC,        /* The WhereClause to be initialized */
       119511  +  WhereInfo *pWInfo        /* The WHERE processing context */
       119512  +){
       119513  +  pWC->pWInfo = pWInfo;
       119514  +  pWC->pOuter = 0;
       119515  +  pWC->nTerm = 0;
       119516  +  pWC->nSlot = ArraySize(pWC->aStatic);
       119517  +  pWC->a = pWC->aStatic;
       119518  +}
       119519  +
       119520  +/*
       119521  +** Deallocate a WhereClause structure.  The WhereClause structure
       119522  +** itself is not freed.  This routine is the inverse of sqlite3WhereClauseInit().
       119523  +*/
       119524  +SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
       119525  +  int i;
       119526  +  WhereTerm *a;
       119527  +  sqlite3 *db = pWC->pWInfo->pParse->db;
       119528  +  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
       119529  +    if( a->wtFlags & TERM_DYNAMIC ){
       119530  +      sqlite3ExprDelete(db, a->pExpr);
       119531  +    }
       119532  +    if( a->wtFlags & TERM_ORINFO ){
       119533  +      whereOrInfoDelete(db, a->u.pOrInfo);
       119534  +    }else if( a->wtFlags & TERM_ANDINFO ){
       119535  +      whereAndInfoDelete(db, a->u.pAndInfo);
       119536  +    }
       119537  +  }
       119538  +  if( pWC->a!=pWC->aStatic ){
       119539  +    sqlite3DbFree(db, pWC->a);
       119540  +  }
       119541  +}
       119542  +
       119543  +
       119544  +/*
       119545  +** These routines walk (recursively) an expression tree and generate
       119546  +** a bitmask indicating which tables are used in that expression
       119547  +** tree.
       119548  +*/
       119549  +SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
       119550  +  Bitmask mask = 0;
       119551  +  if( p==0 ) return 0;
       119552  +  if( p->op==TK_COLUMN ){
       119553  +    mask = sqlite3WhereGetMask(pMaskSet, p->iTable);
       119554  +    return mask;
       119555  +  }
       119556  +  mask = sqlite3WhereExprUsage(pMaskSet, p->pRight);
       119557  +  mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
       119558  +  if( ExprHasProperty(p, EP_xIsSelect) ){
       119559  +    mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
       119560  +  }else{
       119561  +    mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
       119562  +  }
       119563  +  return mask;
       119564  +}
       119565  +SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
       119566  +  int i;
       119567  +  Bitmask mask = 0;
       119568  +  if( pList ){
       119569  +    for(i=0; i<pList->nExpr; i++){
       119570  +      mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
       119571  +    }
       119572  +  }
       119573  +  return mask;
       119574  +}
       119575  +
       119576  +
       119577  +/*
       119578  +** Call exprAnalyze on all terms in a WHERE clause.  
       119579  +**
       119580  +** Note that exprAnalyze() might add new virtual terms onto the
       119581  +** end of the WHERE clause.  We do not want to analyze these new
       119582  +** virtual terms, so start analyzing at the end and work forward
       119583  +** so that the added virtual terms are never processed.
       119584  +*/
       119585  +SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
       119586  +  SrcList *pTabList,       /* the FROM clause */
       119587  +  WhereClause *pWC         /* the WHERE clause to be analyzed */
       119588  +){
       119589  +  int i;
       119590  +  for(i=pWC->nTerm-1; i>=0; i--){
       119591  +    exprAnalyze(pTabList, pWC, i);
       119592  +  }
       119593  +}
       119594  +
       119595  +/************** End of whereexpr.c *******************************************/
       119596  +/************** Begin file where.c *******************************************/
       119597  +/*
       119598  +** 2001 September 15
       119599  +**
       119600  +** The author disclaims copyright to this source code.  In place of
       119601  +** a legal notice, here is a blessing:
       119602  +**
       119603  +**    May you do good and not evil.
       119604  +**    May you find forgiveness for yourself and forgive others.
       119605  +**    May you share freely, never taking more than you give.
       119606  +**
       119607  +*************************************************************************
       119608  +** This module contains C code that generates VDBE code used to process
       119609  +** the WHERE clause of SQL statements.  This module is responsible for
       119610  +** generating the code that loops through a table looking for applicable
       119611  +** rows.  Indices are selected and used to speed the search when doing
       119612  +** so is applicable.  Because this module is responsible for selecting
       119613  +** indices, you might also think of this module as the "query optimizer".
       119614  +*/
       119615  +
       119616  +/* Forward declaration of methods */
       119617  +static int whereLoopResize(sqlite3*, WhereLoop*, int);
       119618  +
       119619  +/* Test variable that can be set to enable WHERE tracing */
       119620  +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
       119621  +/***/ int sqlite3WhereTrace = 0;
       119622  +#endif
       119623  +
       119624  +
       119625  +/*
       119626  +** Return the estimated number of output rows from a WHERE clause
       119627  +*/
       119628  +SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
       119629  +  return sqlite3LogEstToInt(pWInfo->nRowOut);
       119630  +}
       119631  +
       119632  +/*
       119633  +** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
       119634  +** WHERE clause returns outputs for DISTINCT processing.
       119635  +*/
       119636  +SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
       119637  +  return pWInfo->eDistinct;
       119638  +}
       119639  +
       119640  +/*
       119641  +** Return TRUE if the WHERE clause returns rows in ORDER BY order.
       119642  +** Return FALSE if the output needs to be sorted.
       119643  +*/
       119644  +SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
       119645  +  return pWInfo->nOBSat;
       119646  +}
       119647  +
       119648  +/*
       119649  +** Return the VDBE address or label to jump to in order to continue
       119650  +** immediately with the next row of a WHERE clause.
       119651  +*/
       119652  +SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
       119653  +  assert( pWInfo->iContinue!=0 );
       119654  +  return pWInfo->iContinue;
       119655  +}
       119656  +
       119657  +/*
       119658  +** Return the VDBE address or label to jump to in order to break
       119659  +** out of a WHERE loop.
       119660  +*/
       119661  +SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
       119662  +  return pWInfo->iBreak;
       119663  +}
       119664  +
       119665  +/*
       119666  +** Return TRUE if an UPDATE or DELETE statement can operate directly on
       119667  +** the rowids returned by a WHERE clause.  Return FALSE if doing an
       119668  +** UPDATE or DELETE might change subsequent WHERE clause results.
       119669  +**
       119670  +** If the ONEPASS optimization is used (if this routine returns true)
       119671  +** then also write the indices of open cursors used by ONEPASS
       119672  +** into aiCur[0] and aiCur[1].  iaCur[0] gets the cursor of the data
       119673  +** table and iaCur[1] gets the cursor used by an auxiliary index.
       119674  +** Either value may be -1, indicating that cursor is not used.
       119675  +** Any cursors returned will have been opened for writing.
       119676  +**
       119677  +** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
       119678  +** unable to use the ONEPASS optimization.
       119679  +*/
       119680  +SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
       119681  +  memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
       119682  +  return pWInfo->okOnePass;
       119683  +}
       119684  +
       119685  +/*
       119686  +** Move the content of pSrc into pDest
       119687  +*/
       119688  +static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
       119689  +  pDest->n = pSrc->n;
       119690  +  memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
       119691  +}
       119692  +
       119693  +/*
       119694  +** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
       119695  +**
       119696  +** The new entry might overwrite an existing entry, or it might be
       119697  +** appended, or it might be discarded.  Do whatever is the right thing
       119698  +** so that pSet keeps the N_OR_COST best entries seen so far.
       119699  +*/
       119700  +static int whereOrInsert(
       119701  +  WhereOrSet *pSet,      /* The WhereOrSet to be updated */
       119702  +  Bitmask prereq,        /* Prerequisites of the new entry */
       119703  +  LogEst rRun,           /* Run-cost of the new entry */
       119704  +  LogEst nOut            /* Number of outputs for the new entry */
       119705  +){
       119706  +  u16 i;
       119707  +  WhereOrCost *p;
       119708  +  for(i=pSet->n, p=pSet->a; i>0; i--, p++){
       119709  +    if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
       119710  +      goto whereOrInsert_done;
       119711  +    }
       119712  +    if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
       119713  +      return 0;
       119714  +    }
       119715  +  }
       119716  +  if( pSet->n<N_OR_COST ){
       119717  +    p = &pSet->a[pSet->n++];
       119718  +    p->nOut = nOut;
       119719  +  }else{
       119720  +    p = pSet->a;
       119721  +    for(i=1; i<pSet->n; i++){
       119722  +      if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
       119723  +    }
       119724  +    if( p->rRun<=rRun ) return 0;
       119725  +  }
       119726  +whereOrInsert_done:
       119727  +  p->prereq = prereq;
       119728  +  p->rRun = rRun;
       119729  +  if( p->nOut>nOut ) p->nOut = nOut;
       119730  +  return 1;
       119731  +}
       119732  +
       119733  +/*
       119734  +** Return the bitmask for the given cursor number.  Return 0 if
       119735  +** iCursor is not in the set.
       119736  +*/
       119737  +SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
       119738  +  int i;
       119739  +  assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
       119740  +  for(i=0; i<pMaskSet->n; i++){
       119741  +    if( pMaskSet->ix[i]==iCursor ){
       119742  +      return MASKBIT(i);
       119743  +    }
       119744  +  }
       119745  +  return 0;
       119746  +}
       119747  +
       119748  +/*
       119749  +** Create a new mask for cursor iCursor.
       119750  +**
       119751  +** There is one cursor per table in the FROM clause.  The number of
       119752  +** tables in the FROM clause is limited by a test early in the
       119753  +** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
       119754  +** array will never overflow.
       119755  +*/
       119756  +static void createMask(WhereMaskSet *pMaskSet, int iCursor){
       119757  +  assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
       119758  +  pMaskSet->ix[pMaskSet->n++] = iCursor;
       119759  +}
       119760  +
       119761  +/*
       119762  +** Advance to the next WhereTerm that matches according to the criteria
       119763  +** established when the pScan object was initialized by whereScanInit().
       119764  +** Return NULL if there are no more matching WhereTerms.
       119765  +*/
       119766  +static WhereTerm *whereScanNext(WhereScan *pScan){
       119767  +  int iCur;            /* The cursor on the LHS of the term */
       119768  +  int iColumn;         /* The column on the LHS of the term.  -1 for IPK */
       119769  +  Expr *pX;            /* An expression being tested */
       119770  +  WhereClause *pWC;    /* Shorthand for pScan->pWC */
       119771  +  WhereTerm *pTerm;    /* The term being tested */
       119772  +  int k = pScan->k;    /* Where to start scanning */
       119773  +
       119774  +  while( pScan->iEquiv<=pScan->nEquiv ){
       119775  +    iCur = pScan->aEquiv[pScan->iEquiv-2];
       119776  +    iColumn = pScan->aEquiv[pScan->iEquiv-1];
       119777  +    while( (pWC = pScan->pWC)!=0 ){
       119778  +      for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
       119779  +        if( pTerm->leftCursor==iCur
       119780  +         && pTerm->u.leftColumn==iColumn
       119781  +         && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
       119782  +        ){
       119783  +          if( (pTerm->eOperator & WO_EQUIV)!=0
       119784  +           && pScan->nEquiv<ArraySize(pScan->aEquiv)
       119785  +          ){
       119786  +            int j;
       119787  +            pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
       119788  +            assert( pX->op==TK_COLUMN );
       119789  +            for(j=0; j<pScan->nEquiv; j+=2){
       119790  +              if( pScan->aEquiv[j]==pX->iTable
       119791  +               && pScan->aEquiv[j+1]==pX->iColumn ){
       119792  +                  break;
       119793  +              }
       119794  +            }
       119795  +            if( j==pScan->nEquiv ){
       119796  +              pScan->aEquiv[j] = pX->iTable;
       119797  +              pScan->aEquiv[j+1] = pX->iColumn;
       119798  +              pScan->nEquiv += 2;
       119799  +            }
       119800  +          }
       119801  +          if( (pTerm->eOperator & pScan->opMask)!=0 ){
       119802  +            /* Verify the affinity and collating sequence match */
       119803  +            if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
       119804  +              CollSeq *pColl;
       119805  +              Parse *pParse = pWC->pWInfo->pParse;
       119806  +              pX = pTerm->pExpr;
       119807  +              if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
       119808  +                continue;
       119809  +              }
       119810  +              assert(pX->pLeft);
       119811  +              pColl = sqlite3BinaryCompareCollSeq(pParse,
       119812  +                                                  pX->pLeft, pX->pRight);
       119813  +              if( pColl==0 ) pColl = pParse->db->pDfltColl;
       119814  +              if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
       119815  +                continue;
       119816  +              }
       119817  +            }
       119818  +            if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
       119819  +             && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
       119820  +             && pX->iTable==pScan->aEquiv[0]
       119821  +             && pX->iColumn==pScan->aEquiv[1]
       119822  +            ){
       119823  +              testcase( pTerm->eOperator & WO_IS );
       119824  +              continue;
       119825  +            }
       119826  +            pScan->k = k+1;
       119827  +            return pTerm;
       119828  +          }
       119829  +        }
       119830  +      }
       119831  +      pScan->pWC = pScan->pWC->pOuter;
       119832  +      k = 0;
       119833  +    }
       119834  +    pScan->pWC = pScan->pOrigWC;
       119835  +    k = 0;
       119836  +    pScan->iEquiv += 2;
       119837  +  }
       119838  +  return 0;
       119839  +}
       119840  +
       119841  +/*
       119842  +** Initialize a WHERE clause scanner object.  Return a pointer to the
       119843  +** first match.  Return NULL if there are no matches.
       119844  +**
       119845  +** The scanner will be searching the WHERE clause pWC.  It will look
       119846  +** for terms of the form "X <op> <expr>" where X is column iColumn of table
       119847  +** iCur.  The <op> must be one of the operators described by opMask.
       119848  +**
       119849  +** If the search is for X and the WHERE clause contains terms of the
       119850  +** form X=Y then this routine might also return terms of the form
       119851  +** "Y <op> <expr>".  The number of levels of transitivity is limited,
       119852  +** but is enough to handle most commonly occurring SQL statements.
       119853  +**
       119854  +** If X is not the INTEGER PRIMARY KEY then X must be compatible with
       119855  +** index pIdx.
       119856  +*/
       119857  +static WhereTerm *whereScanInit(
       119858  +  WhereScan *pScan,       /* The WhereScan object being initialized */
       119859  +  WhereClause *pWC,       /* The WHERE clause to be scanned */
       119860  +  int iCur,               /* Cursor to scan for */
       119861  +  int iColumn,            /* Column to scan for */
       119862  +  u32 opMask,             /* Operator(s) to scan for */
       119863  +  Index *pIdx             /* Must be compatible with this index */
       119864  +){
       119865  +  int j;
       119866  +
       119867  +  /* memset(pScan, 0, sizeof(*pScan)); */
       119868  +  pScan->pOrigWC = pWC;
       119869  +  pScan->pWC = pWC;
       119870  +  if( pIdx && iColumn>=0 ){
       119871  +    pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
       119872  +    for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
       119873  +      if( NEVER(j>pIdx->nColumn) ) return 0;
       119874  +    }
       119875  +    pScan->zCollName = pIdx->azColl[j];
       119876  +  }else{
       119877  +    pScan->idxaff = 0;
       119878  +    pScan->zCollName = 0;
       119879  +  }
       119880  +  pScan->opMask = opMask;
       119881  +  pScan->k = 0;
       119882  +  pScan->aEquiv[0] = iCur;
       119883  +  pScan->aEquiv[1] = iColumn;
       119884  +  pScan->nEquiv = 2;
       119885  +  pScan->iEquiv = 2;
       119886  +  return whereScanNext(pScan);
       119887  +}
       119888  +
       119889  +/*
       119890  +** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
       119891  +** where X is a reference to the iColumn of table iCur and <op> is one of
       119892  +** the WO_xx operator codes specified by the op parameter.
       119893  +** Return a pointer to the term.  Return 0 if not found.
       119894  +**
       119895  +** The term returned might by Y=<expr> if there is another constraint in
       119896  +** the WHERE clause that specifies that X=Y.  Any such constraints will be
       119897  +** identified by the WO_EQUIV bit in the pTerm->eOperator field.  The
       119898  +** aEquiv[] array holds X and all its equivalents, with each SQL variable
       119899  +** taking up two slots in aEquiv[].  The first slot is for the cursor number
       119900  +** and the second is for the column number.  There are 22 slots in aEquiv[]
       119901  +** so that means we can look for X plus up to 10 other equivalent values.
       119902  +** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
       119903  +** and ... and A9=A10 and A10=<expr>.
       119904  +**
       119905  +** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
       119906  +** then try for the one with no dependencies on <expr> - in other words where
       119907  +** <expr> is a constant expression of some kind.  Only return entries of
       119908  +** the form "X <op> Y" where Y is a column in another table if no terms of
       119909  +** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
       119910  +** exist, try to return a term that does not use WO_EQUIV.
       119911  +*/
       119912  +SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
       119913  +  WhereClause *pWC,     /* The WHERE clause to be searched */
       119914  +  int iCur,             /* Cursor number of LHS */
       119915  +  int iColumn,          /* Column number of LHS */
       119916  +  Bitmask notReady,     /* RHS must not overlap with this mask */
       119917  +  u32 op,               /* Mask of WO_xx values describing operator */
       119918  +  Index *pIdx           /* Must be compatible with this index, if not NULL */
       119919  +){
       119920  +  WhereTerm *pResult = 0;
       119921  +  WhereTerm *p;
       119922  +  WhereScan scan;
       119923  +
       119924  +  p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
       119925  +  op &= WO_EQ|WO_IS;
       119926  +  while( p ){
       119927  +    if( (p->prereqRight & notReady)==0 ){
       119928  +      if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
       119929  +        testcase( p->eOperator & WO_IS );
       119930  +        return p;
       119931  +      }
       119932  +      if( pResult==0 ) pResult = p;
       119933  +    }
       119934  +    p = whereScanNext(&scan);
       119935  +  }
       119936  +  return pResult;
       119937  +}
117846 119938   
117847 119939   /*
117848 119940   ** This function searches pList for an entry that matches the iCol-th column
117849 119941   ** of index pIdx.
117850 119942   **
117851 119943   ** If such an expression is found, its index in pList->a[] is returned. If
117852 119944   ** no expression is found, -1 is returned.
................................................................................
117877 119969     return -1;
117878 119970   }
117879 119971   
117880 119972   /*
117881 119973   ** Return true if the DISTINCT expression-list passed as the third argument
117882 119974   ** is redundant.
117883 119975   **
117884         -** A DISTINCT list is redundant if the database contains some subset of
117885         -** columns that are unique and non-null.
       119976  +** A DISTINCT list is redundant if any subset of the columns in the
       119977  +** DISTINCT list are collectively unique and individually non-null.
117886 119978   */
117887 119979   static int isDistinctRedundant(
117888 119980     Parse *pParse,            /* Parsing context */
117889 119981     SrcList *pTabList,        /* The FROM clause */
117890 119982     WhereClause *pWC,         /* The WHERE clause */
117891 119983     ExprList *pDistinct       /* The result set that needs to be DISTINCT */
117892 119984   ){
................................................................................
117924 120016     **   3. All of those index columns for which the WHERE clause does not
117925 120017     **      contain a "col=X" term are subject to a NOT NULL constraint.
117926 120018     */
117927 120019     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117928 120020       if( !IsUniqueIndex(pIdx) ) continue;
117929 120021       for(i=0; i<pIdx->nKeyCol; i++){
117930 120022         i16 iCol = pIdx->aiColumn[i];
117931         -      if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
       120023  +      if( 0==sqlite3WhereFindTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
117932 120024           int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
117933 120025           if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
117934 120026             break;
117935 120027           }
117936 120028         }
117937 120029       }
117938 120030       if( i==pIdx->nKeyCol ){
................................................................................
118255 120347   ** Allocate and populate an sqlite3_index_info structure. It is the 
118256 120348   ** responsibility of the caller to eventually release the structure
118257 120349   ** by passing the pointer returned by this function to sqlite3_free().
118258 120350   */
118259 120351   static sqlite3_index_info *allocateIndexInfo(
118260 120352     Parse *pParse,
118261 120353     WhereClause *pWC,
       120354  +  Bitmask mUnusable,              /* Ignore terms with these prereqs */
118262 120355     struct SrcList_item *pSrc,
118263 120356     ExprList *pOrderBy
118264 120357   ){
118265 120358     int i, j;
118266 120359     int nTerm;
118267 120360     struct sqlite3_index_constraint *pIdxCons;
118268 120361     struct sqlite3_index_orderby *pIdxOrderBy;
................................................................................
118271 120364     int nOrderBy;
118272 120365     sqlite3_index_info *pIdxInfo;
118273 120366   
118274 120367     /* Count the number of possible WHERE clause constraints referring
118275 120368     ** to this virtual table */
118276 120369     for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118277 120370       if( pTerm->leftCursor != pSrc->iCursor ) continue;
       120371  +    if( pTerm->prereqRight & mUnusable ) continue;
118278 120372       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118279 120373       testcase( pTerm->eOperator & WO_IN );
118280 120374       testcase( pTerm->eOperator & WO_ISNULL );
118281 120375       testcase( pTerm->eOperator & WO_IS );
118282 120376       testcase( pTerm->eOperator & WO_ALL );
118283 120377       if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
118284 120378       if( pTerm->wtFlags & TERM_VNULL ) continue;
................................................................................
118325 120419     *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
118326 120420     *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
118327 120421                                                                      pUsage;
118328 120422   
118329 120423     for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118330 120424       u8 op;
118331 120425       if( pTerm->leftCursor != pSrc->iCursor ) continue;
       120426  +    if( pTerm->prereqRight & mUnusable ) continue;
118332 120427       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118333 120428       testcase( pTerm->eOperator & WO_IN );
118334 120429       testcase( pTerm->eOperator & WO_IS );
118335 120430       testcase( pTerm->eOperator & WO_ISNULL );
118336 120431       testcase( pTerm->eOperator & WO_ALL );
118337 120432       if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
118338 120433       if( pTerm->wtFlags & TERM_VNULL ) continue;
................................................................................
119046 121141       WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
119047 121142     }
119048 121143     assert( pBuilder->nRecValid==nRecValid );
119049 121144     return rc;
119050 121145   }
119051 121146   #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
119052 121147   
119053         -/*
119054         -** Disable a term in the WHERE clause.  Except, do not disable the term
119055         -** if it controls a LEFT OUTER JOIN and it did not originate in the ON
119056         -** or USING clause of that join.
119057         -**
119058         -** Consider the term t2.z='ok' in the following queries:
119059         -**
119060         -**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
119061         -**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
119062         -**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
119063         -**
119064         -** The t2.z='ok' is disabled in the in (2) because it originates
119065         -** in the ON clause.  The term is disabled in (3) because it is not part
119066         -** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
119067         -**
119068         -** Disabling a term causes that term to not be tested in the inner loop
119069         -** of the join.  Disabling is an optimization.  When terms are satisfied
119070         -** by indices, we disable them to prevent redundant tests in the inner
119071         -** loop.  We would get the correct results if nothing were ever disabled,
119072         -** but joins might run a little slower.  The trick is to disable as much
119073         -** as we can without disabling too much.  If we disabled in (1), we'd get
119074         -** the wrong answer.  See ticket #813.
119075         -**
119076         -** If all the children of a term are disabled, then that term is also
119077         -** automatically disabled.  In this way, terms get disabled if derived
119078         -** virtual terms are tested first.  For example:
119079         -**
119080         -**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
119081         -**      \___________/     \______/     \_____/
119082         -**         parent          child1       child2
119083         -**
119084         -** Only the parent term was in the original WHERE clause.  The child1
119085         -** and child2 terms were added by the LIKE optimization.  If both of
119086         -** the virtual child terms are valid, then testing of the parent can be 
119087         -** skipped.
119088         -**
119089         -** Usually the parent term is marked as TERM_CODED.  But if the parent
119090         -** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
119091         -** The TERM_LIKECOND marking indicates that the term should be coded inside
119092         -** a conditional such that is only evaluated on the second pass of a
119093         -** LIKE-optimization loop, when scanning BLOBs instead of strings.
119094         -*/
119095         -static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
119096         -  int nLoop = 0;
119097         -  while( pTerm
119098         -      && (pTerm->wtFlags & TERM_CODED)==0
119099         -      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
119100         -      && (pLevel->notReady & pTerm->prereqAll)==0
119101         -  ){
119102         -    if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
119103         -      pTerm->wtFlags |= TERM_LIKECOND;
119104         -    }else{
119105         -      pTerm->wtFlags |= TERM_CODED;
119106         -    }
119107         -    if( pTerm->iParent<0 ) break;
119108         -    pTerm = &pTerm->pWC->a[pTerm->iParent];
119109         -    pTerm->nChild--;
119110         -    if( pTerm->nChild!=0 ) break;
119111         -    nLoop++;
119112         -  }
119113         -}
119114         -
119115         -/*
119116         -** Code an OP_Affinity opcode to apply the column affinity string zAff
119117         -** to the n registers starting at base. 
119118         -**
119119         -** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
119120         -** beginning and end of zAff are ignored.  If all entries in zAff are
119121         -** SQLITE_AFF_NONE, then no code gets generated.
119122         -**
119123         -** This routine makes its own copy of zAff so that the caller is free
119124         -** to modify zAff after this routine returns.
119125         -*/
119126         -static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
119127         -  Vdbe *v = pParse->pVdbe;
119128         -  if( zAff==0 ){
119129         -    assert( pParse->db->mallocFailed );
119130         -    return;
119131         -  }
119132         -  assert( v!=0 );
119133         -
119134         -  /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
119135         -  ** and end of the affinity string.
119136         -  */
119137         -  while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
119138         -    n--;
119139         -    base++;
119140         -    zAff++;
119141         -  }
119142         -  while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
119143         -    n--;
119144         -  }
119145         -
119146         -  /* Code the OP_Affinity opcode if there is anything left to do. */
119147         -  if( n>0 ){
119148         -    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
119149         -    sqlite3VdbeChangeP4(v, -1, zAff, n);
119150         -    sqlite3ExprCacheAffinityChange(pParse, base, n);
119151         -  }
119152         -}
119153         -
119154         -
119155         -/*
119156         -** Generate code for a single equality term of the WHERE clause.  An equality
119157         -** term can be either X=expr or X IN (...).   pTerm is the term to be 
119158         -** coded.
119159         -**
119160         -** The current value for the constraint is left in register iReg.
119161         -**
119162         -** For a constraint of the form X=expr, the expression is evaluated and its
119163         -** result is left on the stack.  For constraints of the form X IN (...)
119164         -** this routine sets up a loop that will iterate over all values of X.
119165         -*/
119166         -static int codeEqualityTerm(
119167         -  Parse *pParse,      /* The parsing context */
119168         -  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
119169         -  WhereLevel *pLevel, /* The level of the FROM clause we are working on */
119170         -  int iEq,            /* Index of the equality term within this level */
119171         -  int bRev,           /* True for reverse-order IN operations */
119172         -  int iTarget         /* Attempt to leave results in this register */
119173         -){
119174         -  Expr *pX = pTerm->pExpr;
119175         -  Vdbe *v = pParse->pVdbe;
119176         -  int iReg;                  /* Register holding results */
119177         -
119178         -  assert( iTarget>0 );
119179         -  if( pX->op==TK_EQ || pX->op==TK_IS ){
119180         -    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
119181         -  }else if( pX->op==TK_ISNULL ){
119182         -    iReg = iTarget;
119183         -    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
119184         -#ifndef SQLITE_OMIT_SUBQUERY
119185         -  }else{
119186         -    int eType;
119187         -    int iTab;
119188         -    struct InLoop *pIn;
119189         -    WhereLoop *pLoop = pLevel->pWLoop;
119190         -
119191         -    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
119192         -      && pLoop->u.btree.pIndex!=0
119193         -      && pLoop->u.btree.pIndex->aSortOrder[iEq]
119194         -    ){
119195         -      testcase( iEq==0 );
119196         -      testcase( bRev );
119197         -      bRev = !bRev;
119198         -    }
119199         -    assert( pX->op==TK_IN );
119200         -    iReg = iTarget;
119201         -    eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
119202         -    if( eType==IN_INDEX_INDEX_DESC ){
119203         -      testcase( bRev );
119204         -      bRev = !bRev;
119205         -    }
119206         -    iTab = pX->iTable;
119207         -    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
119208         -    VdbeCoverageIf(v, bRev);
119209         -    VdbeCoverageIf(v, !bRev);
119210         -    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
119211         -    pLoop->wsFlags |= WHERE_IN_ABLE;
119212         -    if( pLevel->u.in.nIn==0 ){
119213         -      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
119214         -    }
119215         -    pLevel->u.in.nIn++;
119216         -    pLevel->u.in.aInLoop =
119217         -       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
119218         -                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
119219         -    pIn = pLevel->u.in.aInLoop;
119220         -    if( pIn ){
119221         -      pIn += pLevel->u.in.nIn - 1;
119222         -      pIn->iCur = iTab;
119223         -      if( eType==IN_INDEX_ROWID ){
119224         -        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
119225         -      }else{
119226         -        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
119227         -      }
119228         -      pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
119229         -      sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
119230         -    }else{
119231         -      pLevel->u.in.nIn = 0;
119232         -    }
119233         -#endif
119234         -  }
119235         -  disableTerm(pLevel, pTerm);
119236         -  return iReg;
119237         -}
119238         -
119239         -/*
119240         -** Generate code that will evaluate all == and IN constraints for an
119241         -** index scan.
119242         -**
119243         -** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
119244         -** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
119245         -** The index has as many as three equality constraints, but in this
119246         -** example, the third "c" value is an inequality.  So only two 
119247         -** constraints are coded.  This routine will generate code to evaluate
119248         -** a==5 and b IN (1,2,3).  The current values for a and b will be stored
119249         -** in consecutive registers and the index of the first register is returned.
119250         -**
119251         -** In the example above nEq==2.  But this subroutine works for any value
119252         -** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
119253         -** The only thing it does is allocate the pLevel->iMem memory cell and
119254         -** compute the affinity string.
119255         -**
119256         -** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
119257         -** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
119258         -** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
119259         -** occurs after the nEq quality constraints.
119260         -**
119261         -** This routine allocates a range of nEq+nExtraReg memory cells and returns
119262         -** the index of the first memory cell in that range. The code that
119263         -** calls this routine will use that memory range to store keys for
119264         -** start and termination conditions of the loop.
119265         -** key value of the loop.  If one or more IN operators appear, then
119266         -** this routine allocates an additional nEq memory cells for internal
119267         -** use.
119268         -**
119269         -** Before returning, *pzAff is set to point to a buffer containing a
119270         -** copy of the column affinity string of the index allocated using
119271         -** sqlite3DbMalloc(). Except, entries in the copy of the string associated
119272         -** with equality constraints that use NONE affinity are set to
119273         -** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
119274         -**
119275         -**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
119276         -**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
119277         -**
119278         -** In the example above, the index on t1(a) has TEXT affinity. But since
119279         -** the right hand side of the equality constraint (t2.b) has NONE affinity,
119280         -** no conversion should be attempted before using a t2.b value as part of
119281         -** a key to search the index. Hence the first byte in the returned affinity
119282         -** string in this example would be set to SQLITE_AFF_NONE.
119283         -*/
119284         -static int codeAllEqualityTerms(
119285         -  Parse *pParse,        /* Parsing context */
119286         -  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
119287         -  int bRev,             /* Reverse the order of IN operators */
119288         -  int nExtraReg,        /* Number of extra registers to allocate */
119289         -  char **pzAff          /* OUT: Set to point to affinity string */
119290         -){
119291         -  u16 nEq;                      /* The number of == or IN constraints to code */
119292         -  u16 nSkip;                    /* Number of left-most columns to skip */
119293         -  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
119294         -  Index *pIdx;                  /* The index being used for this loop */
119295         -  WhereTerm *pTerm;             /* A single constraint term */
119296         -  WhereLoop *pLoop;             /* The WhereLoop object */
119297         -  int j;                        /* Loop counter */
119298         -  int regBase;                  /* Base register */
119299         -  int nReg;                     /* Number of registers to allocate */
119300         -  char *zAff;                   /* Affinity string to return */
119301         -
119302         -  /* This module is only called on query plans that use an index. */
119303         -  pLoop = pLevel->pWLoop;
119304         -  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
119305         -  nEq = pLoop->u.btree.nEq;
119306         -  nSkip = pLoop->nSkip;
119307         -  pIdx = pLoop->u.btree.pIndex;
119308         -  assert( pIdx!=0 );
119309         -
119310         -  /* Figure out how many memory cells we will need then allocate them.
119311         -  */
119312         -  regBase = pParse->nMem + 1;
119313         -  nReg = pLoop->u.btree.nEq + nExtraReg;
119314         -  pParse->nMem += nReg;
119315         -
119316         -  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
119317         -  if( !zAff ){
119318         -    pParse->db->mallocFailed = 1;
119319         -  }
119320         -
119321         -  if( nSkip ){
119322         -    int iIdxCur = pLevel->iIdxCur;
119323         -    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
119324         -    VdbeCoverageIf(v, bRev==0);
119325         -    VdbeCoverageIf(v, bRev!=0);
119326         -    VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
119327         -    j = sqlite3VdbeAddOp0(v, OP_Goto);
119328         -    pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
119329         -                            iIdxCur, 0, regBase, nSkip);
119330         -    VdbeCoverageIf(v, bRev==0);
119331         -    VdbeCoverageIf(v, bRev!=0);
119332         -    sqlite3VdbeJumpHere(v, j);
119333         -    for(j=0; j<nSkip; j++){
119334         -      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
119335         -      assert( pIdx->aiColumn[j]>=0 );
119336         -      VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
119337         -    }
119338         -  }    
119339         -
119340         -  /* Evaluate the equality constraints
119341         -  */
119342         -  assert( zAff==0 || (int)strlen(zAff)>=nEq );
119343         -  for(j=nSkip; j<nEq; j++){
119344         -    int r1;
119345         -    pTerm = pLoop->aLTerm[j];
119346         -    assert( pTerm!=0 );
119347         -    /* The following testcase is true for indices with redundant columns. 
119348         -    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
119349         -    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
119350         -    testcase( pTerm->wtFlags & TERM_VIRTUAL );
119351         -    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
119352         -    if( r1!=regBase+j ){
119353         -      if( nReg==1 ){
119354         -        sqlite3ReleaseTempReg(pParse, regBase);
119355         -        regBase = r1;
119356         -      }else{
119357         -        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
119358         -      }
119359         -    }
119360         -    testcase( pTerm->eOperator & WO_ISNULL );
119361         -    testcase( pTerm->eOperator & WO_IN );
119362         -    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
119363         -      Expr *pRight = pTerm->pExpr->pRight;
119364         -      if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
119365         -        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
119366         -        VdbeCoverage(v);
119367         -      }
119368         -      if( zAff ){
119369         -        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
119370         -          zAff[j] = SQLITE_AFF_NONE;
119371         -        }
119372         -        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
119373         -          zAff[j] = SQLITE_AFF_NONE;
119374         -        }
119375         -      }
119376         -    }
119377         -  }
119378         -  *pzAff = zAff;
119379         -  return regBase;
119380         -}
119381         -
119382         -#ifndef SQLITE_OMIT_EXPLAIN
119383         -/*
119384         -** This routine is a helper for explainIndexRange() below
119385         -**
119386         -** pStr holds the text of an expression that we are building up one term
119387         -** at a time.  This routine adds a new term to the end of the expression.
119388         -** Terms are separated by AND so add the "AND" text for second and subsequent
119389         -** terms only.
119390         -*/
119391         -static void explainAppendTerm(
119392         -  StrAccum *pStr,             /* The text expression being built */
119393         -  int iTerm,                  /* Index of this term.  First is zero */
119394         -  const char *zColumn,        /* Name of the column */
119395         -  const char *zOp             /* Name of the operator */
119396         -){
119397         -  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
119398         -  sqlite3StrAccumAppendAll(pStr, zColumn);
119399         -  sqlite3StrAccumAppend(pStr, zOp, 1);
119400         -  sqlite3StrAccumAppend(pStr, "?", 1);
119401         -}
119402         -
119403         -/*
119404         -** Argument pLevel describes a strategy for scanning table pTab. This 
119405         -** function appends text to pStr that describes the subset of table
119406         -** rows scanned by the strategy in the form of an SQL expression.
119407         -**
119408         -** For example, if the query:
119409         -**
119410         -**   SELECT * FROM t1 WHERE a=1 AND b>2;
119411         -**
119412         -** is run and there is an index on (a, b), then this function returns a
119413         -** string similar to:
119414         -**
119415         -**   "a=? AND b>?"
119416         -*/
119417         -static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
119418         -  Index *pIndex = pLoop->u.btree.pIndex;
119419         -  u16 nEq = pLoop->u.btree.nEq;
119420         -  u16 nSkip = pLoop->nSkip;
119421         -  int i, j;
119422         -  Column *aCol = pTab->aCol;
119423         -  i16 *aiColumn = pIndex->aiColumn;
119424         -
119425         -  if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
119426         -  sqlite3StrAccumAppend(pStr, " (", 2);
119427         -  for(i=0; i<nEq; i++){
119428         -    char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
119429         -    if( i>=nSkip ){
119430         -      explainAppendTerm(pStr, i, z, "=");
119431         -    }else{
119432         -      if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
119433         -      sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
119434         -    }
119435         -  }
119436         -
119437         -  j = i;
119438         -  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
119439         -    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
119440         -    explainAppendTerm(pStr, i++, z, ">");
119441         -  }
119442         -  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
119443         -    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
119444         -    explainAppendTerm(pStr, i, z, "<");
119445         -  }
119446         -  sqlite3StrAccumAppend(pStr, ")", 1);
119447         -}
119448         -
119449         -/*
119450         -** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
119451         -** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
119452         -** defined at compile-time. If it is not a no-op, a single OP_Explain opcode 
119453         -** is added to the output to describe the table scan strategy in pLevel.
119454         -**
119455         -** If an OP_Explain opcode is added to the VM, its address is returned.
119456         -** Otherwise, if no OP_Explain is coded, zero is returned.
119457         -*/
119458         -static int explainOneScan(
119459         -  Parse *pParse,                  /* Parse context */
119460         -  SrcList *pTabList,              /* Table list this loop refers to */
119461         -  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
119462         -  int iLevel,                     /* Value for "level" column of output */
119463         -  int iFrom,                      /* Value for "from" column of output */
119464         -  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
119465         -){
119466         -  int ret = 0;
119467         -#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
119468         -  if( pParse->explain==2 )
119469         -#endif
119470         -  {
119471         -    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
119472         -    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
119473         -    sqlite3 *db = pParse->db;     /* Database handle */
119474         -    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
119475         -    int isSearch;                 /* True for a SEARCH. False for SCAN. */
119476         -    WhereLoop *pLoop;             /* The controlling WhereLoop object */
119477         -    u32 flags;                    /* Flags that describe this loop */
119478         -    char *zMsg;                   /* Text to add to EQP output */
119479         -    StrAccum str;                 /* EQP output string */
119480         -    char zBuf[100];               /* Initial space for EQP output string */
119481         -
119482         -    pLoop = pLevel->pWLoop;
119483         -    flags = pLoop->wsFlags;
119484         -    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
119485         -
119486         -    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
119487         -            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
119488         -            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
119489         -
119490         -    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
119491         -    sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
119492         -    if( pItem->pSelect ){
119493         -      sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
119494         -    }else{
119495         -      sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
119496         -    }
119497         -
119498         -    if( pItem->zAlias ){
119499         -      sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
119500         -    }
119501         -    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
119502         -      const char *zFmt = 0;
119503         -      Index *pIdx;
119504         -
119505         -      assert( pLoop->u.btree.pIndex!=0 );
119506         -      pIdx = pLoop->u.btree.pIndex;
119507         -      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
119508         -      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
119509         -        if( isSearch ){
119510         -          zFmt = "PRIMARY KEY";
119511         -        }
119512         -      }else if( flags & WHERE_PARTIALIDX ){
119513         -        zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
119514         -      }else if( flags & WHERE_AUTO_INDEX ){
119515         -        zFmt = "AUTOMATIC COVERING INDEX";
119516         -      }else if( flags & WHERE_IDX_ONLY ){
119517         -        zFmt = "COVERING INDEX %s";
119518         -      }else{
119519         -        zFmt = "INDEX %s";
119520         -      }
119521         -      if( zFmt ){
119522         -        sqlite3StrAccumAppend(&str, " USING ", 7);
119523         -        sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
119524         -        explainIndexRange(&str, pLoop, pItem->pTab);
119525         -      }
119526         -    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
119527         -      const char *zRange;
119528         -      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
119529         -        zRange = "(rowid=?)";
119530         -      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
119531         -        zRange = "(rowid>? AND rowid<?)";
119532         -      }else if( flags&WHERE_BTM_LIMIT ){
119533         -        zRange = "(rowid>?)";
119534         -      }else{
119535         -        assert( flags&WHERE_TOP_LIMIT);
119536         -        zRange = "(rowid<?)";
119537         -      }
119538         -      sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
119539         -      sqlite3StrAccumAppendAll(&str, zRange);
119540         -    }
119541         -#ifndef SQLITE_OMIT_VIRTUALTABLE
119542         -    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
119543         -      sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
119544         -                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
119545         -    }
119546         -#endif
119547         -#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
119548         -    if( pLoop->nOut>=10 ){
119549         -      sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
119550         -    }else{
119551         -      sqlite3StrAccumAppend(&str, " (~1 row)", 9);
119552         -    }
119553         -#endif
119554         -    zMsg = sqlite3StrAccumFinish(&str);
119555         -    ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
119556         -  }
119557         -  return ret;
119558         -}
119559         -#else
119560         -# define explainOneScan(u,v,w,x,y,z) 0
119561         -#endif /* SQLITE_OMIT_EXPLAIN */
119562         -
119563         -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
119564         -/*
119565         -** Configure the VM passed as the first argument with an
119566         -** sqlite3_stmt_scanstatus() entry corresponding to the scan used to 
119567         -** implement level pLvl. Argument pSrclist is a pointer to the FROM 
119568         -** clause that the scan reads data from.
119569         -**
119570         -** If argument addrExplain is not 0, it must be the address of an 
119571         -** OP_Explain instruction that describes the same loop.
119572         -*/
119573         -static void addScanStatus(
119574         -  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
119575         -  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
119576         -  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
119577         -  int addrExplain                 /* Address of OP_Explain (or 0) */
119578         -){
119579         -  const char *zObj = 0;
119580         -  WhereLoop *pLoop = pLvl->pWLoop;
119581         -  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
119582         -    zObj = pLoop->u.btree.pIndex->zName;
119583         -  }else{
119584         -    zObj = pSrclist->a[pLvl->iFrom].zName;
119585         -  }
119586         -  sqlite3VdbeScanStatus(
119587         -      v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
119588         -  );
119589         -}
119590         -#else
119591         -# define addScanStatus(a, b, c, d) ((void)d)
119592         -#endif
119593         -
119594         -/*
119595         -** If the most recently coded instruction is a constant range contraint
119596         -** that originated from the LIKE optimization, then change the P3 to be
119597         -** pLoop->iLikeRepCntr and set P5.
119598         -**
119599         -** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
119600         -** expression: "x>='ABC' AND x<'abd'".  But this requires that the range
119601         -** scan loop run twice, once for strings and a second time for BLOBs.
119602         -** The OP_String opcodes on the second pass convert the upper and lower
119603         -** bound string contants to blobs.  This routine makes the necessary changes
119604         -** to the OP_String opcodes for that to happen.
119605         -*/
119606         -static void whereLikeOptimizationStringFixup(
119607         -  Vdbe *v,                /* prepared statement under construction */
119608         -  WhereLevel *pLevel,     /* The loop that contains the LIKE operator */
119609         -  WhereTerm *pTerm        /* The upper or lower bound just coded */
119610         -){
119611         -  if( pTerm->wtFlags & TERM_LIKEOPT ){
119612         -    VdbeOp *pOp;
119613         -    assert( pLevel->iLikeRepCntr>0 );
119614         -    pOp = sqlite3VdbeGetOp(v, -1);
119615         -    assert( pOp!=0 );
119616         -    assert( pOp->opcode==OP_String8 
119617         -            || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
119618         -    pOp->p3 = pLevel->iLikeRepCntr;
119619         -    pOp->p5 = 1;
119620         -  }
119621         -}
119622         -
119623         -/*
119624         -** Generate code for the start of the iLevel-th loop in the WHERE clause
119625         -** implementation described by pWInfo.
119626         -*/
119627         -static Bitmask codeOneLoopStart(
119628         -  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
119629         -  int iLevel,          /* Which level of pWInfo->a[] should be coded */
119630         -  Bitmask notReady     /* Which tables are currently available */
119631         -){
119632         -  int j, k;            /* Loop counters */
119633         -  int iCur;            /* The VDBE cursor for the table */
119634         -  int addrNxt;         /* Where to jump to continue with the next IN case */
119635         -  int omitTable;       /* True if we use the index only */
119636         -  int bRev;            /* True if we need to scan in reverse order */
119637         -  WhereLevel *pLevel;  /* The where level to be coded */
119638         -  WhereLoop *pLoop;    /* The WhereLoop object being coded */
119639         -  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
119640         -  WhereTerm *pTerm;               /* A WHERE clause term */
119641         -  Parse *pParse;                  /* Parsing context */
119642         -  sqlite3 *db;                    /* Database connection */
119643         -  Vdbe *v;                        /* The prepared stmt under constructions */
119644         -  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
119645         -  int addrBrk;                    /* Jump here to break out of the loop */
119646         -  int addrCont;                   /* Jump here to continue with next cycle */
119647         -  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
119648         -  int iReleaseReg = 0;      /* Temp register to free before returning */
119649         -
119650         -  pParse = pWInfo->pParse;
119651         -  v = pParse->pVdbe;
119652         -  pWC = &pWInfo->sWC;
119653         -  db = pParse->db;
119654         -  pLevel = &pWInfo->a[iLevel];
119655         -  pLoop = pLevel->pWLoop;
119656         -  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
119657         -  iCur = pTabItem->iCursor;
119658         -  pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
119659         -  bRev = (pWInfo->revMask>>iLevel)&1;
119660         -  omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
119661         -           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
119662         -  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
119663         -
119664         -  /* Create labels for the "break" and "continue" instructions
119665         -  ** for the current loop.  Jump to addrBrk to break out of a loop.
119666         -  ** Jump to cont to go immediately to the next iteration of the
119667         -  ** loop.
119668         -  **
119669         -  ** When there is an IN operator, we also have a "addrNxt" label that
119670         -  ** means to continue with the next IN value combination.  When
119671         -  ** there are no IN operators in the constraints, the "addrNxt" label
119672         -  ** is the same as "addrBrk".
119673         -  */
119674         -  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
119675         -  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
119676         -
119677         -  /* If this is the right table of a LEFT OUTER JOIN, allocate and
119678         -  ** initialize a memory cell that records if this table matches any
119679         -  ** row of the left table of the join.
119680         -  */
119681         -  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
119682         -    pLevel->iLeftJoin = ++pParse->nMem;
119683         -    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
119684         -    VdbeComment((v, "init LEFT JOIN no-match flag"));
119685         -  }
119686         -
119687         -  /* Special case of a FROM clause subquery implemented as a co-routine */
119688         -  if( pTabItem->viaCoroutine ){
119689         -    int regYield = pTabItem->regReturn;
119690         -    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
119691         -    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
119692         -    VdbeCoverage(v);
119693         -    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
119694         -    pLevel->op = OP_Goto;
119695         -  }else
119696         -
119697         -#ifndef SQLITE_OMIT_VIRTUALTABLE
119698         -  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
119699         -    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
119700         -    **          to access the data.
119701         -    */
119702         -    int iReg;   /* P3 Value for OP_VFilter */
119703         -    int addrNotFound;
119704         -    int nConstraint = pLoop->nLTerm;
119705         -
119706         -    sqlite3ExprCachePush(pParse);
119707         -    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
119708         -    addrNotFound = pLevel->addrBrk;
119709         -    for(j=0; j<nConstraint; j++){
119710         -      int iTarget = iReg+j+2;
119711         -      pTerm = pLoop->aLTerm[j];
119712         -      if( pTerm==0 ) continue;
119713         -      if( pTerm->eOperator & WO_IN ){
119714         -        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
119715         -        addrNotFound = pLevel->addrNxt;
119716         -      }else{
119717         -        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
119718         -      }
119719         -    }
119720         -    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
119721         -    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
119722         -    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
119723         -                      pLoop->u.vtab.idxStr,
119724         -                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
119725         -    VdbeCoverage(v);
119726         -    pLoop->u.vtab.needFree = 0;
119727         -    for(j=0; j<nConstraint && j<16; j++){
119728         -      if( (pLoop->u.vtab.omitMask>>j)&1 ){
119729         -        disableTerm(pLevel, pLoop->aLTerm[j]);
119730         -      }
119731         -    }
119732         -    pLevel->op = OP_VNext;
119733         -    pLevel->p1 = iCur;
119734         -    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
119735         -    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
119736         -    sqlite3ExprCachePop(pParse);
119737         -  }else
119738         -#endif /* SQLITE_OMIT_VIRTUALTABLE */
119739         -
119740         -  if( (pLoop->wsFlags & WHERE_IPK)!=0
119741         -   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
119742         -  ){
119743         -    /* Case 2:  We can directly reference a single row using an
119744         -    **          equality comparison against the ROWID field.  Or
119745         -    **          we reference multiple rows using a "rowid IN (...)"
119746         -    **          construct.
119747         -    */
119748         -    assert( pLoop->u.btree.nEq==1 );
119749         -    pTerm = pLoop->aLTerm[0];
119750         -    assert( pTerm!=0 );
119751         -    assert( pTerm->pExpr!=0 );
119752         -    assert( omitTable==0 );
119753         -    testcase( pTerm->wtFlags & TERM_VIRTUAL );
119754         -    iReleaseReg = ++pParse->nMem;
119755         -    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
119756         -    if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
119757         -    addrNxt = pLevel->addrNxt;
119758         -    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
119759         -    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
119760         -    VdbeCoverage(v);
119761         -    sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
119762         -    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
119763         -    VdbeComment((v, "pk"));
119764         -    pLevel->op = OP_Noop;
119765         -  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
119766         -         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
119767         -  ){
119768         -    /* Case 3:  We have an inequality comparison against the ROWID field.
119769         -    */
119770         -    int testOp = OP_Noop;
119771         -    int start;
119772         -    int memEndValue = 0;
119773         -    WhereTerm *pStart, *pEnd;
119774         -
119775         -    assert( omitTable==0 );
119776         -    j = 0;
119777         -    pStart = pEnd = 0;
119778         -    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
119779         -    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
119780         -    assert( pStart!=0 || pEnd!=0 );
119781         -    if( bRev ){
119782         -      pTerm = pStart;
119783         -      pStart = pEnd;
119784         -      pEnd = pTerm;
119785         -    }
119786         -    if( pStart ){
119787         -      Expr *pX;             /* The expression that defines the start bound */
119788         -      int r1, rTemp;        /* Registers for holding the start boundary */
119789         -
119790         -      /* The following constant maps TK_xx codes into corresponding 
119791         -      ** seek opcodes.  It depends on a particular ordering of TK_xx
119792         -      */
119793         -      const u8 aMoveOp[] = {
119794         -           /* TK_GT */  OP_SeekGT,
119795         -           /* TK_LE */  OP_SeekLE,
119796         -           /* TK_LT */  OP_SeekLT,
119797         -           /* TK_GE */  OP_SeekGE
119798         -      };
119799         -      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
119800         -      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
119801         -      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
119802         -
119803         -      assert( (pStart->wtFlags & TERM_VNULL)==0 );
119804         -      testcase( pStart->wtFlags & TERM_VIRTUAL );
119805         -      pX = pStart->pExpr;
119806         -      assert( pX!=0 );
119807         -      testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
119808         -      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
119809         -      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
119810         -      VdbeComment((v, "pk"));
119811         -      VdbeCoverageIf(v, pX->op==TK_GT);
119812         -      VdbeCoverageIf(v, pX->op==TK_LE);
119813         -      VdbeCoverageIf(v, pX->op==TK_LT);
119814         -      VdbeCoverageIf(v, pX->op==TK_GE);
119815         -      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
119816         -      sqlite3ReleaseTempReg(pParse, rTemp);
119817         -      disableTerm(pLevel, pStart);
119818         -    }else{
119819         -      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
119820         -      VdbeCoverageIf(v, bRev==0);
119821         -      VdbeCoverageIf(v, bRev!=0);
119822         -    }
119823         -    if( pEnd ){
119824         -      Expr *pX;
119825         -      pX = pEnd->pExpr;
119826         -      assert( pX!=0 );
119827         -      assert( (pEnd->wtFlags & TERM_VNULL)==0 );
119828         -      testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
119829         -      testcase( pEnd->wtFlags & TERM_VIRTUAL );
119830         -      memEndValue = ++pParse->nMem;
119831         -      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
119832         -      if( pX->op==TK_LT || pX->op==TK_GT ){
119833         -        testOp = bRev ? OP_Le : OP_Ge;
119834         -      }else{
119835         -        testOp = bRev ? OP_Lt : OP_Gt;
119836         -      }
119837         -      disableTerm(pLevel, pEnd);
119838         -    }
119839         -    start = sqlite3VdbeCurrentAddr(v);
119840         -    pLevel->op = bRev ? OP_Prev : OP_Next;
119841         -    pLevel->p1 = iCur;
119842         -    pLevel->p2 = start;
119843         -    assert( pLevel->p5==0 );
119844         -    if( testOp!=OP_Noop ){
119845         -      iRowidReg = ++pParse->nMem;
119846         -      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
119847         -      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
119848         -      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
119849         -      VdbeCoverageIf(v, testOp==OP_Le);
119850         -      VdbeCoverageIf(v, testOp==OP_Lt);
119851         -      VdbeCoverageIf(v, testOp==OP_Ge);
119852         -      VdbeCoverageIf(v, testOp==OP_Gt);
119853         -      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
119854         -    }
119855         -  }else if( pLoop->wsFlags & WHERE_INDEXED ){
119856         -    /* Case 4: A scan using an index.
119857         -    **
119858         -    **         The WHERE clause may contain zero or more equality 
119859         -    **         terms ("==" or "IN" operators) that refer to the N
119860         -    **         left-most columns of the index. It may also contain
119861         -    **         inequality constraints (>, <, >= or <=) on the indexed
119862         -    **         column that immediately follows the N equalities. Only 
119863         -    **         the right-most column can be an inequality - the rest must
119864         -    **         use the "==" and "IN" operators. For example, if the 
119865         -    **         index is on (x,y,z), then the following clauses are all 
119866         -    **         optimized:
119867         -    **
119868         -    **            x=5
119869         -    **            x=5 AND y=10
119870         -    **            x=5 AND y<10
119871         -    **            x=5 AND y>5 AND y<10
119872         -    **            x=5 AND y=5 AND z<=10
119873         -    **
119874         -    **         The z<10 term of the following cannot be used, only
119875         -    **         the x=5 term:
119876         -    **
119877         -    **            x=5 AND z<10
119878         -    **
119879         -    **         N may be zero if there are inequality constraints.
119880         -    **         If there are no inequality constraints, then N is at
119881         -    **         least one.
119882         -    **
119883         -    **         This case is also used when there are no WHERE clause
119884         -    **         constraints but an index is selected anyway, in order
119885         -    **         to force the output order to conform to an ORDER BY.
119886         -    */  
119887         -    static const u8 aStartOp[] = {
119888         -      0,
119889         -      0,
119890         -      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
119891         -      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
119892         -      OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
119893         -      OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
119894         -      OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
119895         -      OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
119896         -    };
119897         -    static const u8 aEndOp[] = {
119898         -      OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
119899         -      OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
119900         -      OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
119901         -      OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
119902         -    };
119903         -    u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
119904         -    int regBase;                 /* Base register holding constraint values */
119905         -    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
119906         -    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
119907         -    int startEq;                 /* True if range start uses ==, >= or <= */
119908         -    int endEq;                   /* True if range end uses ==, >= or <= */
119909         -    int start_constraints;       /* Start of range is constrained */
119910         -    int nConstraint;             /* Number of constraint terms */
119911         -    Index *pIdx;                 /* The index we will be using */
119912         -    int iIdxCur;                 /* The VDBE cursor for the index */
119913         -    int nExtraReg = 0;           /* Number of extra registers needed */
119914         -    int op;                      /* Instruction opcode */
119915         -    char *zStartAff;             /* Affinity for start of range constraint */
119916         -    char cEndAff = 0;            /* Affinity for end of range constraint */
119917         -    u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
119918         -    u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
119919         -
119920         -    pIdx = pLoop->u.btree.pIndex;
119921         -    iIdxCur = pLevel->iIdxCur;
119922         -    assert( nEq>=pLoop->nSkip );
119923         -
119924         -    /* If this loop satisfies a sort order (pOrderBy) request that 
119925         -    ** was passed to this function to implement a "SELECT min(x) ..." 
119926         -    ** query, then the caller will only allow the loop to run for
119927         -    ** a single iteration. This means that the first row returned
119928         -    ** should not have a NULL value stored in 'x'. If column 'x' is
119929         -    ** the first one after the nEq equality constraints in the index,
119930         -    ** this requires some special handling.
119931         -    */
119932         -    assert( pWInfo->pOrderBy==0
119933         -         || pWInfo->pOrderBy->nExpr==1
119934         -         || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
119935         -    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
119936         -     && pWInfo->nOBSat>0
119937         -     && (pIdx->nKeyCol>nEq)
119938         -    ){
119939         -      assert( pLoop->nSkip==0 );
119940         -      bSeekPastNull = 1;
119941         -      nExtraReg = 1;
119942         -    }
119943         -
119944         -    /* Find any inequality constraint terms for the start and end 
119945         -    ** of the range. 
119946         -    */
119947         -    j = nEq;
119948         -    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
119949         -      pRangeStart = pLoop->aLTerm[j++];
119950         -      nExtraReg = 1;
119951         -      /* Like optimization range constraints always occur in pairs */
119952         -      assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || 
119953         -              (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
119954         -    }
119955         -    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
119956         -      pRangeEnd = pLoop->aLTerm[j++];
119957         -      nExtraReg = 1;
119958         -      if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
119959         -        assert( pRangeStart!=0 );                     /* LIKE opt constraints */
119960         -        assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
119961         -        pLevel->iLikeRepCntr = ++pParse->nMem;
119962         -        testcase( bRev );
119963         -        testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
119964         -        sqlite3VdbeAddOp2(v, OP_Integer,
119965         -                          bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
119966         -                          pLevel->iLikeRepCntr);
119967         -        VdbeComment((v, "LIKE loop counter"));
119968         -        pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
119969         -      }
119970         -      if( pRangeStart==0
119971         -       && (j = pIdx->aiColumn[nEq])>=0 
119972         -       && pIdx->pTable->aCol[j].notNull==0
119973         -      ){
119974         -        bSeekPastNull = 1;
119975         -      }
119976         -    }
119977         -    assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
119978         -
119979         -    /* Generate code to evaluate all constraint terms using == or IN
119980         -    ** and store the values of those terms in an array of registers
119981         -    ** starting at regBase.
119982         -    */
119983         -    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
119984         -    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
119985         -    if( zStartAff ) cEndAff = zStartAff[nEq];
119986         -    addrNxt = pLevel->addrNxt;
119987         -
119988         -    /* If we are doing a reverse order scan on an ascending index, or
119989         -    ** a forward order scan on a descending index, interchange the 
119990         -    ** start and end terms (pRangeStart and pRangeEnd).
119991         -    */
119992         -    if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
119993         -     || (bRev && pIdx->nKeyCol==nEq)
119994         -    ){
119995         -      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
119996         -      SWAP(u8, bSeekPastNull, bStopAtNull);
119997         -    }
119998         -
119999         -    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
120000         -    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
120001         -    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
120002         -    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
120003         -    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
120004         -    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
120005         -    start_constraints = pRangeStart || nEq>0;
120006         -
120007         -    /* Seek the index cursor to the start of the range. */
120008         -    nConstraint = nEq;
120009         -    if( pRangeStart ){
120010         -      Expr *pRight = pRangeStart->pExpr->pRight;
120011         -      sqlite3ExprCode(pParse, pRight, regBase+nEq);
120012         -      whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
120013         -      if( (pRangeStart->wtFlags & TERM_VNULL)==0
120014         -       && sqlite3ExprCanBeNull(pRight)
120015         -      ){
120016         -        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
120017         -        VdbeCoverage(v);
120018         -      }
120019         -      if( zStartAff ){
120020         -        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
120021         -          /* Since the comparison is to be performed with no conversions
120022         -          ** applied to the operands, set the affinity to apply to pRight to 
120023         -          ** SQLITE_AFF_NONE.  */
120024         -          zStartAff[nEq] = SQLITE_AFF_NONE;
120025         -        }
120026         -        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
120027         -          zStartAff[nEq] = SQLITE_AFF_NONE;
120028         -        }
120029         -      }  
120030         -      nConstraint++;
120031         -      testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
120032         -    }else if( bSeekPastNull ){
120033         -      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
120034         -      nConstraint++;
120035         -      startEq = 0;
120036         -      start_constraints = 1;
120037         -    }
120038         -    codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
120039         -    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
120040         -    assert( op!=0 );
120041         -    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
120042         -    VdbeCoverage(v);
120043         -    VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
120044         -    VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
120045         -    VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
120046         -    VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
120047         -    VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
120048         -    VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
120049         -
120050         -    /* Load the value for the inequality constraint at the end of the
120051         -    ** range (if any).
120052         -    */
120053         -    nConstraint = nEq;
120054         -    if( pRangeEnd ){
120055         -      Expr *pRight = pRangeEnd->pExpr->pRight;
120056         -      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
120057         -      sqlite3ExprCode(pParse, pRight, regBase+nEq);
120058         -      whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
120059         -      if( (pRangeEnd->wtFlags & TERM_VNULL)==0
120060         -       && sqlite3ExprCanBeNull(pRight)
120061         -      ){
120062         -        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
120063         -        VdbeCoverage(v);
120064         -      }
120065         -      if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
120066         -       && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
120067         -      ){
120068         -        codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
120069         -      }
120070         -      nConstraint++;
120071         -      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
120072         -    }else if( bStopAtNull ){
120073         -      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
120074         -      endEq = 0;
120075         -      nConstraint++;
120076         -    }
120077         -    sqlite3DbFree(db, zStartAff);
120078         -
120079         -    /* Top of the loop body */
120080         -    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
120081         -
120082         -    /* Check if the index cursor is past the end of the range. */
120083         -    if( nConstraint ){
120084         -      op = aEndOp[bRev*2 + endEq];
120085         -      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
120086         -      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
120087         -      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
120088         -      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
120089         -      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
120090         -    }
120091         -
120092         -    /* Seek the table cursor, if required */
120093         -    disableTerm(pLevel, pRangeStart);
120094         -    disableTerm(pLevel, pRangeEnd);
120095         -    if( omitTable ){
120096         -      /* pIdx is a covering index.  No need to access the main table. */
120097         -    }else if( HasRowid(pIdx->pTable) ){
120098         -      iRowidReg = ++pParse->nMem;
120099         -      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
120100         -      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
120101         -      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
120102         -    }else if( iCur!=iIdxCur ){
120103         -      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
120104         -      iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
120105         -      for(j=0; j<pPk->nKeyCol; j++){
120106         -        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
120107         -        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
120108         -      }
120109         -      sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
120110         -                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
120111         -    }
120112         -
120113         -    /* Record the instruction used to terminate the loop. Disable 
120114         -    ** WHERE clause terms made redundant by the index range scan.
120115         -    */
120116         -    if( pLoop->wsFlags & WHERE_ONEROW ){
120117         -      pLevel->op = OP_Noop;
120118         -    }else if( bRev ){
120119         -      pLevel->op = OP_Prev;
120120         -    }else{
120121         -      pLevel->op = OP_Next;
120122         -    }
120123         -    pLevel->p1 = iIdxCur;
120124         -    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
120125         -    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
120126         -      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
120127         -    }else{
120128         -      assert( pLevel->p5==0 );
120129         -    }
120130         -  }else
120131         -
120132         -#ifndef SQLITE_OMIT_OR_OPTIMIZATION
120133         -  if( pLoop->wsFlags & WHERE_MULTI_OR ){
120134         -    /* Case 5:  Two or more separately indexed terms connected by OR
120135         -    **
120136         -    ** Example:
120137         -    **
120138         -    **   CREATE TABLE t1(a,b,c,d);
120139         -    **   CREATE INDEX i1 ON t1(a);
120140         -    **   CREATE INDEX i2 ON t1(b);
120141         -    **   CREATE INDEX i3 ON t1(c);
120142         -    **
120143         -    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
120144         -    **
120145         -    ** In the example, there are three indexed terms connected by OR.
120146         -    ** The top of the loop looks like this:
120147         -    **
120148         -    **          Null       1                # Zero the rowset in reg 1
120149         -    **
120150         -    ** Then, for each indexed term, the following. The arguments to
120151         -    ** RowSetTest are such that the rowid of the current row is inserted
120152         -    ** into the RowSet. If it is already present, control skips the
120153         -    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
120154         -    **
120155         -    **        sqlite3WhereBegin(<term>)
120156         -    **          RowSetTest                  # Insert rowid into rowset
120157         -    **          Gosub      2 A
120158         -    **        sqlite3WhereEnd()
120159         -    **
120160         -    ** Following the above, code to terminate the loop. Label A, the target
120161         -    ** of the Gosub above, jumps to the instruction right after the Goto.
120162         -    **
120163         -    **          Null       1                # Zero the rowset in reg 1
120164         -    **          Goto       B                # The loop is finished.
120165         -    **
120166         -    **       A: <loop body>                 # Return data, whatever.
120167         -    **
120168         -    **          Return     2                # Jump back to the Gosub
120169         -    **
120170         -    **       B: <after the loop>
120171         -    **
120172         -    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
120173         -    ** use an ephemeral index instead of a RowSet to record the primary
120174         -    ** keys of the rows we have already seen.
120175         -    **
120176         -    */
120177         -    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
120178         -    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
120179         -    Index *pCov = 0;             /* Potential covering index (or NULL) */
120180         -    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
120181         -
120182         -    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
120183         -    int regRowset = 0;                        /* Register for RowSet object */
120184         -    int regRowid = 0;                         /* Register holding rowid */
120185         -    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
120186         -    int iRetInit;                             /* Address of regReturn init */
120187         -    int untestedTerms = 0;             /* Some terms not completely tested */
120188         -    int ii;                            /* Loop counter */
120189         -    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
120190         -    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
120191         -    Table *pTab = pTabItem->pTab;
120192         -   
120193         -    pTerm = pLoop->aLTerm[0];
120194         -    assert( pTerm!=0 );
120195         -    assert( pTerm->eOperator & WO_OR );
120196         -    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
120197         -    pOrWc = &pTerm->u.pOrInfo->wc;
120198         -    pLevel->op = OP_Return;
120199         -    pLevel->p1 = regReturn;
120200         -
120201         -    /* Set up a new SrcList in pOrTab containing the table being scanned
120202         -    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
120203         -    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
120204         -    */
120205         -    if( pWInfo->nLevel>1 ){
120206         -      int nNotReady;                 /* The number of notReady tables */
120207         -      struct SrcList_item *origSrc;     /* Original list of tables */
120208         -      nNotReady = pWInfo->nLevel - iLevel - 1;
120209         -      pOrTab = sqlite3StackAllocRaw(db,
120210         -                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
120211         -      if( pOrTab==0 ) return notReady;
120212         -      pOrTab->nAlloc = (u8)(nNotReady + 1);
120213         -      pOrTab->nSrc = pOrTab->nAlloc;
120214         -      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
120215         -      origSrc = pWInfo->pTabList->a;
120216         -      for(k=1; k<=nNotReady; k++){
120217         -        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
120218         -      }
120219         -    }else{
120220         -      pOrTab = pWInfo->pTabList;
120221         -    }
120222         -
120223         -    /* Initialize the rowset register to contain NULL. An SQL NULL is 
120224         -    ** equivalent to an empty rowset.  Or, create an ephemeral index
120225         -    ** capable of holding primary keys in the case of a WITHOUT ROWID.
120226         -    **
120227         -    ** Also initialize regReturn to contain the address of the instruction 
120228         -    ** immediately following the OP_Return at the bottom of the loop. This
120229         -    ** is required in a few obscure LEFT JOIN cases where control jumps
120230         -    ** over the top of the loop into the body of it. In this case the 
120231         -    ** correct response for the end-of-loop code (the OP_Return) is to 
120232         -    ** fall through to the next instruction, just as an OP_Next does if
120233         -    ** called on an uninitialized cursor.
120234         -    */
120235         -    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
120236         -      if( HasRowid(pTab) ){
120237         -        regRowset = ++pParse->nMem;
120238         -        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
120239         -      }else{
120240         -        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120241         -        regRowset = pParse->nTab++;
120242         -        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
120243         -        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
120244         -      }
120245         -      regRowid = ++pParse->nMem;
120246         -    }
120247         -    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
120248         -
120249         -    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
120250         -    ** Then for every term xN, evaluate as the subexpression: xN AND z
120251         -    ** That way, terms in y that are factored into the disjunction will
120252         -    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
120253         -    **
120254         -    ** Actually, each subexpression is converted to "xN AND w" where w is
120255         -    ** the "interesting" terms of z - terms that did not originate in the
120256         -    ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
120257         -    ** indices.
120258         -    **
120259         -    ** This optimization also only applies if the (x1 OR x2 OR ...) term
120260         -    ** is not contained in the ON clause of a LEFT JOIN.
120261         -    ** See ticket http://www.sqlite.org/src/info/f2369304e4
120262         -    */
120263         -    if( pWC->nTerm>1 ){
120264         -      int iTerm;
120265         -      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
120266         -        Expr *pExpr = pWC->a[iTerm].pExpr;
120267         -        if( &pWC->a[iTerm] == pTerm ) continue;
120268         -        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
120269         -        if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
120270         -        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
120271         -        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
120272         -        pExpr = sqlite3ExprDup(db, pExpr, 0);
120273         -        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
120274         -      }
120275         -      if( pAndExpr ){
120276         -        pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
120277         -      }
120278         -    }
120279         -
120280         -    /* Run a separate WHERE clause for each term of the OR clause.  After
120281         -    ** eliminating duplicates from other WHERE clauses, the action for each
120282         -    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
120283         -    */
120284         -    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE
120285         -                | WHERE_FORCE_TABLE
120286         -                | WHERE_ONETABLE_ONLY
120287         -                | WHERE_NO_AUTOINDEX;
120288         -    for(ii=0; ii<pOrWc->nTerm; ii++){
120289         -      WhereTerm *pOrTerm = &pOrWc->a[ii];
120290         -      if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
120291         -        WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
120292         -        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
120293         -        int j1 = 0;                     /* Address of jump operation */
120294         -        if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
120295         -          pAndExpr->pLeft = pOrExpr;
120296         -          pOrExpr = pAndExpr;
120297         -        }