System.Data.SQLite
Check-in [3a02749d71]
Not logged in

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

Overview
Comment:Add 'Changes' property to the connection class. Also, update the core SQLite library to the latest trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3a02749d711e236180c184187c9528fe7ef51622
User & Date: mistachkin 2011-09-18 22:39:35
Context
2011-09-20
17:34
Update core SQLite to 3.7.8 release. check-in: 2fa7c53d4e user: mistachkin tags: trunk
2011-09-18
22:39
Add 'Changes' property to the connection class. Also, update the core SQLite library to the latest trunk. check-in: 3a02749d71 user: mistachkin tags: trunk
2011-09-16
03:37
In the test for ticket [b4a7ddc83f], brace all substituted values in the setup script just in case they may ever contain spaces. check-in: b1727a9afb user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

   654    654   **
   655    655   ** See also: [sqlite3_libversion()],
   656    656   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   657    657   ** [sqlite_version()] and [sqlite_source_id()].
   658    658   */
   659    659   #define SQLITE_VERSION        "3.7.8"
   660    660   #define SQLITE_VERSION_NUMBER 3007008
   661         -#define SQLITE_SOURCE_ID      "2011-08-31 23:57:22 2869ed28299b1c9f355ecc24635830f7f1249126"
          661  +#define SQLITE_SOURCE_ID      "2011-09-17 15:34:50 29c4d0dd43d41650e54824afd70dd40f1b91cc86"
   662    662   
   663    663   /*
   664    664   ** CAPI3REF: Run-Time Library Version Numbers
   665    665   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   666    666   **
   667    667   ** These interfaces provide the same information as the [SQLITE_VERSION],
   668    668   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  7630   7630   ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
  7631   7631   ** on the command-line
  7632   7632   */
  7633   7633   #ifndef SQLITE_TEMP_STORE
  7634   7634   # define SQLITE_TEMP_STORE 1
  7635   7635   #endif
  7636   7636   
  7637         -/*
  7638         -** If all temporary storage is in-memory, then omit the external merge-sort
  7639         -** logic since it is superfluous.
  7640         -*/
  7641         -#if SQLITE_TEMP_STORE==3 && !defined(SQLITE_OMIT_MERGE_SORT)
  7642         -# define SQLITE_OMIT_MERGE_SORT
  7643         -#endif
  7644         -
  7645   7637   /*
  7646   7638   ** GCC does not define the offsetof() macro so we'll have to do it
  7647   7639   ** ourselves.
  7648   7640   */
  7649   7641   #ifndef offsetof
  7650   7642   #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
  7651   7643   #endif
................................................................................
  7980   7972   ** pager.h.
  7981   7973   */
  7982   7974   #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
  7983   7975   #define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
  7984   7976   #define BTREE_MEMORY        4  /* This is an in-memory DB */
  7985   7977   #define BTREE_SINGLE        8  /* The file contains at most 1 b-tree */
  7986   7978   #define BTREE_UNORDERED    16  /* Use of a hash implementation is OK */
  7987         -#define BTREE_SORTER       32  /* Used as accumulator in external merge sort */
  7988   7979   
  7989   7980   SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
  7990   7981   SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
  7991   7982   SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
  7992   7983   SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  7993   7984   SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  7994   7985   SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
................................................................................
  8381   8372   #define OP_Permutation                         23
  8382   8373   #define OP_Compare                             24
  8383   8374   #define OP_Jump                                25
  8384   8375   #define OP_And                                 69   /* same as TK_AND      */
  8385   8376   #define OP_Or                                  68   /* same as TK_OR       */
  8386   8377   #define OP_Not                                 19   /* same as TK_NOT      */
  8387   8378   #define OP_BitNot                              93   /* same as TK_BITNOT   */
  8388         -#define OP_If                                  26
  8389         -#define OP_IfNot                               27
         8379  +#define OP_Once                                26
         8380  +#define OP_If                                  27
         8381  +#define OP_IfNot                               28
  8390   8382   #define OP_IsNull                              73   /* same as TK_ISNULL   */
  8391   8383   #define OP_NotNull                             74   /* same as TK_NOTNULL  */
  8392         -#define OP_Column                              28
  8393         -#define OP_Affinity                            29
  8394         -#define OP_MakeRecord                          30
  8395         -#define OP_Count                               31
  8396         -#define OP_Savepoint                           32
  8397         -#define OP_AutoCommit                          33
  8398         -#define OP_Transaction                         34
  8399         -#define OP_ReadCookie                          35
  8400         -#define OP_SetCookie                           36
  8401         -#define OP_VerifyCookie                        37
  8402         -#define OP_OpenRead                            38
  8403         -#define OP_OpenWrite                           39
  8404         -#define OP_OpenSorter                          40
         8384  +#define OP_Column                              29
         8385  +#define OP_Affinity                            30
         8386  +#define OP_MakeRecord                          31
         8387  +#define OP_Count                               32
         8388  +#define OP_Savepoint                           33
         8389  +#define OP_AutoCommit                          34
         8390  +#define OP_Transaction                         35
         8391  +#define OP_ReadCookie                          36
         8392  +#define OP_SetCookie                           37
         8393  +#define OP_VerifyCookie                        38
         8394  +#define OP_OpenRead                            39
         8395  +#define OP_OpenWrite                           40
  8405   8396   #define OP_OpenAutoindex                       41
  8406   8397   #define OP_OpenEphemeral                       42
  8407         -#define OP_OpenPseudo                          43
  8408         -#define OP_Close                               44
  8409         -#define OP_SeekLt                              45
  8410         -#define OP_SeekLe                              46
  8411         -#define OP_SeekGe                              47
  8412         -#define OP_SeekGt                              48
  8413         -#define OP_Seek                                49
  8414         -#define OP_NotFound                            50
  8415         -#define OP_Found                               51
  8416         -#define OP_IsUnique                            52
  8417         -#define OP_NotExists                           53
  8418         -#define OP_Sequence                            54
  8419         -#define OP_NewRowid                            55
  8420         -#define OP_Insert                              56
  8421         -#define OP_InsertInt                           57
  8422         -#define OP_Delete                              58
  8423         -#define OP_ResetCount                          59
  8424         -#define OP_RowKey                              60
  8425         -#define OP_RowData                             61
  8426         -#define OP_Rowid                               62
  8427         -#define OP_NullRow                             63
  8428         -#define OP_Last                                64
  8429         -#define OP_Sort                                65
  8430         -#define OP_Rewind                              66
  8431         -#define OP_Prev                                67
  8432         -#define OP_Next                                70
  8433         -#define OP_IdxInsert                           71
  8434         -#define OP_IdxDelete                           72
  8435         -#define OP_IdxRowid                            81
  8436         -#define OP_IdxLT                               92
  8437         -#define OP_IdxGE                               95
  8438         -#define OP_Destroy                             96
  8439         -#define OP_Clear                               97
  8440         -#define OP_CreateIndex                         98
  8441         -#define OP_CreateTable                         99
  8442         -#define OP_ParseSchema                        100
  8443         -#define OP_LoadAnalysis                       101
  8444         -#define OP_DropTable                          102
  8445         -#define OP_DropIndex                          103
  8446         -#define OP_DropTrigger                        104
  8447         -#define OP_IntegrityCk                        105
  8448         -#define OP_RowSetAdd                          106
  8449         -#define OP_RowSetRead                         107
  8450         -#define OP_RowSetTest                         108
  8451         -#define OP_Program                            109
  8452         -#define OP_Param                              110
  8453         -#define OP_FkCounter                          111
  8454         -#define OP_FkIfZero                           112
  8455         -#define OP_MemMax                             113
  8456         -#define OP_IfPos                              114
  8457         -#define OP_IfNeg                              115
  8458         -#define OP_IfZero                             116
  8459         -#define OP_AggStep                            117
  8460         -#define OP_AggFinal                           118
  8461         -#define OP_Checkpoint                         119
  8462         -#define OP_JournalMode                        120
  8463         -#define OP_Vacuum                             121
  8464         -#define OP_IncrVacuum                         122
  8465         -#define OP_Expire                             123
  8466         -#define OP_TableLock                          124
  8467         -#define OP_VBegin                             125
  8468         -#define OP_VCreate                            126
  8469         -#define OP_VDestroy                           127
  8470         -#define OP_VOpen                              128
  8471         -#define OP_VFilter                            129
  8472         -#define OP_VColumn                            131
  8473         -#define OP_VNext                              132
  8474         -#define OP_VRename                            133
  8475         -#define OP_VUpdate                            134
  8476         -#define OP_Pagecount                          135
  8477         -#define OP_MaxPgcnt                           136
  8478         -#define OP_Trace                              137
  8479         -#define OP_Noop                               138
  8480         -#define OP_Explain                            139
  8481         -
  8482         -/* The following opcode values are never used */
  8483         -#define OP_NotUsed_140                        140
         8398  +#define OP_SorterOpen                          43
         8399  +#define OP_OpenPseudo                          44
         8400  +#define OP_Close                               45
         8401  +#define OP_SeekLt                              46
         8402  +#define OP_SeekLe                              47
         8403  +#define OP_SeekGe                              48
         8404  +#define OP_SeekGt                              49
         8405  +#define OP_Seek                                50
         8406  +#define OP_NotFound                            51
         8407  +#define OP_Found                               52
         8408  +#define OP_IsUnique                            53
         8409  +#define OP_NotExists                           54
         8410  +#define OP_Sequence                            55
         8411  +#define OP_NewRowid                            56
         8412  +#define OP_Insert                              57
         8413  +#define OP_InsertInt                           58
         8414  +#define OP_Delete                              59
         8415  +#define OP_ResetCount                          60
         8416  +#define OP_SorterCompare                       61
         8417  +#define OP_SorterData                          62
         8418  +#define OP_RowKey                              63
         8419  +#define OP_RowData                             64
         8420  +#define OP_Rowid                               65
         8421  +#define OP_NullRow                             66
         8422  +#define OP_Last                                67
         8423  +#define OP_SorterSort                          70
         8424  +#define OP_Sort                                71
         8425  +#define OP_Rewind                              72
         8426  +#define OP_SorterNext                          81
         8427  +#define OP_Prev                                92
         8428  +#define OP_Next                                95
         8429  +#define OP_SorterInsert                        96
         8430  +#define OP_IdxInsert                           97
         8431  +#define OP_IdxDelete                           98
         8432  +#define OP_IdxRowid                            99
         8433  +#define OP_IdxLT                              100
         8434  +#define OP_IdxGE                              101
         8435  +#define OP_Destroy                            102
         8436  +#define OP_Clear                              103
         8437  +#define OP_CreateIndex                        104
         8438  +#define OP_CreateTable                        105
         8439  +#define OP_ParseSchema                        106
         8440  +#define OP_LoadAnalysis                       107
         8441  +#define OP_DropTable                          108
         8442  +#define OP_DropIndex                          109
         8443  +#define OP_DropTrigger                        110
         8444  +#define OP_IntegrityCk                        111
         8445  +#define OP_RowSetAdd                          112
         8446  +#define OP_RowSetRead                         113
         8447  +#define OP_RowSetTest                         114
         8448  +#define OP_Program                            115
         8449  +#define OP_Param                              116
         8450  +#define OP_FkCounter                          117
         8451  +#define OP_FkIfZero                           118
         8452  +#define OP_MemMax                             119
         8453  +#define OP_IfPos                              120
         8454  +#define OP_IfNeg                              121
         8455  +#define OP_IfZero                             122
         8456  +#define OP_AggStep                            123
         8457  +#define OP_AggFinal                           124
         8458  +#define OP_Checkpoint                         125
         8459  +#define OP_JournalMode                        126
         8460  +#define OP_Vacuum                             127
         8461  +#define OP_IncrVacuum                         128
         8462  +#define OP_Expire                             129
         8463  +#define OP_TableLock                          131
         8464  +#define OP_VBegin                             132
         8465  +#define OP_VCreate                            133
         8466  +#define OP_VDestroy                           134
         8467  +#define OP_VOpen                              135
         8468  +#define OP_VFilter                            136
         8469  +#define OP_VColumn                            137
         8470  +#define OP_VNext                              138
         8471  +#define OP_VRename                            139
         8472  +#define OP_VUpdate                            140
         8473  +#define OP_Pagecount                          146
         8474  +#define OP_MaxPgcnt                           147
         8475  +#define OP_Trace                              148
         8476  +#define OP_Noop                               149
         8477  +#define OP_Explain                            150
  8484   8478   
  8485   8479   
  8486   8480   /* Properties such as "out2" or "jump" that are specified in
  8487   8481   ** comments following the "case" for each opcode in the vdbe.c
  8488   8482   ** are encoded into bitvectors as follows:
  8489   8483   */
  8490   8484   #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
................................................................................
  8494   8488   #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
  8495   8489   #define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
  8496   8490   #define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
  8497   8491   #define OPFLG_INITIALIZER {\
  8498   8492   /*   0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
  8499   8493   /*   8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
  8500   8494   /*  16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
  8501         -/*  24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
  8502         -/*  32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
  8503         -/*  40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11,\
  8504         -/*  48 */ 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02,\
  8505         -/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,\
  8506         -/*  64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x01, 0x08,\
  8507         -/*  72 */ 0x00, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
  8508         -/*  80 */ 0x15, 0x02, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
         8495  +/*  24 */ 0x00, 0x01, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00,\
         8496  +/*  32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
         8497  +/*  40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
         8498  +/*  48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
         8499  +/*  56 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
         8500  +/*  64 */ 0x00, 0x02, 0x00, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
         8501  +/*  72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
         8502  +/*  80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
  8509   8503   /*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x01,\
  8510         -/*  96 */ 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\
  8511         -/* 104 */ 0x00, 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00,\
  8512         -/* 112 */ 0x01, 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00,\
  8513         -/* 120 */ 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\
  8514         -/* 128 */ 0x00, 0x01, 0x02, 0x00, 0x01, 0x00, 0x00, 0x02,\
  8515         -/* 136 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
  8516         -/* 144 */ 0x04, 0x04,}
         8504  +/*  96 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
         8505  +/* 104 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
         8506  +/* 112 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
         8507  +/* 120 */ 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00,\
         8508  +/* 128 */ 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
         8509  +/* 136 */ 0x01, 0x00, 0x01, 0x00, 0x00, 0x04, 0x04, 0x04,\
         8510  +/* 144 */ 0x04, 0x04, 0x02, 0x02, 0x00, 0x00, 0x00,}
  8517   8511   
  8518   8512   /************** End of opcodes.h *********************************************/
  8519   8513   /************** Continuing where we left off in vdbe.h ***********************/
  8520   8514   
  8521   8515   /*
  8522   8516   ** Prototypes for the VDBE interface.  See comments on the implementation
  8523   8517   ** for a description of what each of these routines does.
................................................................................
  8532   8526   SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
  8533   8527   SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
  8534   8528   SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
  8535   8529   SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
  8536   8530   SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
  8537   8531   SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
  8538   8532   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
  8539         -SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
         8533  +SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
  8540   8534   SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
  8541   8535   SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
  8542   8536   SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
  8543   8537   SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
  8544   8538   SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
  8545   8539   SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
  8546   8540   SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
................................................................................
  8564   8558   SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
  8565   8559   SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
  8566   8560   SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
  8567   8561   #ifndef SQLITE_OMIT_TRACE
  8568   8562   SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  8569   8563   #endif
  8570   8564   
  8571         -SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int);
  8572         -SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*);
         8565  +SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  8573   8566   SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
         8567  +SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
  8574   8568   
  8575   8569   #ifndef SQLITE_OMIT_TRIGGER
  8576   8570   SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
  8577   8571   #endif
  8578   8572   
  8579   8573   
  8580   8574   #ifndef NDEBUG
................................................................................
  8747   8741   SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
  8748   8742   SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
  8749   8743   SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
  8750   8744   SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
  8751   8745   SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
  8752   8746   SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
  8753   8747   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
  8754         -#ifndef SQLITE_OMIT_MERGE_SORT
  8755         -SQLITE_PRIVATE int sqlite3PagerUnderStress(Pager*);
  8756         -#endif
  8757   8748   
  8758   8749   /* Functions used to truncate the database file. */
  8759   8750   SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
  8760   8751   
  8761   8752   #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
  8762   8753   SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
  8763   8754   #endif
................................................................................
 10184  10175   */
 10185  10176   struct AggInfo {
 10186  10177     u8 directMode;          /* Direct rendering mode means take data directly
 10187  10178                             ** from source tables rather than from accumulators */
 10188  10179     u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
 10189  10180                             ** than the source table */
 10190  10181     int sortingIdx;         /* Cursor number of the sorting index */
        10182  +  int sortingIdxPTab;     /* Cursor number of pseudo-table */
 10191  10183     ExprList *pGroupBy;     /* The group by clause */
 10192  10184     int nSortingColumn;     /* Number of columns in the sorting index */
 10193  10185     struct AggInfo_col {    /* For each column used in source tables */
 10194  10186       Table *pTab;             /* Source table */
 10195  10187       int iTable;              /* Cursor number of the source table */
 10196  10188       int iColumn;             /* Column number within the source table */
 10197  10189       int iSorterColumn;       /* Column number in the sorting index */
................................................................................
 10493  10485     i16 nAlloc;      /* Number of entries allocated in a[] below */
 10494  10486     struct SrcList_item {
 10495  10487       char *zDatabase;  /* Name of database holding this table */
 10496  10488       char *zName;      /* Name of the table */
 10497  10489       char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
 10498  10490       Table *pTab;      /* An SQL table corresponding to zName */
 10499  10491       Select *pSelect;  /* A SELECT statement used in place of a table name */
 10500         -    u8 isPopulated;   /* Temporary table associated with SELECT is populated */
        10492  +    int addrFillSub;  /* Address of subroutine to manifest a subquery */
        10493  +    int regReturn;    /* Register holding return address of addrFillSub */
 10501  10494       u8 jointype;      /* Type of join between this able and the previous */
 10502  10495       u8 notIndexed;    /* True if there is a NOT INDEXED clause */
 10503  10496       u8 isCorrelated;  /* True if sub-query is correlated */
 10504  10497   #ifndef SQLITE_OMIT_EXPLAIN
 10505  10498       u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
 10506  10499   #endif
 10507  10500       int iCursor;      /* The VDBE cursor number used to access this table */
................................................................................
 10716  10709   */
 10717  10710   #define SF_Distinct        0x0001  /* Output should be DISTINCT */
 10718  10711   #define SF_Resolved        0x0002  /* Identifiers have been resolved */
 10719  10712   #define SF_Aggregate       0x0004  /* Contains aggregate functions */
 10720  10713   #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
 10721  10714   #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
 10722  10715   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
        10716  +#define SF_UseSorter       0x0040  /* Sort using a sorter */
 10723  10717   
 10724  10718   
 10725  10719   /*
 10726  10720   ** The results of a select can be distributed in several ways.  The
 10727  10721   ** "SRT" prefix means "SELECT Result Type".
 10728  10722   */
 10729  10723   #define SRT_Union        1  /* Store result as keys in an index */
................................................................................
 12602  12596     Bool atFirst;         /* True if pointing to first entry */
 12603  12597     Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
 12604  12598     Bool nullRow;         /* True if pointing to a row with no data */
 12605  12599     Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
 12606  12600     Bool isTable;         /* True if a table requiring integer keys */
 12607  12601     Bool isIndex;         /* True if an index containing keys only - no data */
 12608  12602     Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
        12603  +  Bool isSorter;        /* True if a new-style sorter */
 12609  12604     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
 12610  12605     const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
 12611  12606     i64 seqCount;         /* Sequence counter */
 12612  12607     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
 12613  12608     i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
 12614         -  VdbeSorter *pSorter;  /* Sorter object for OP_OpenSorter cursors */
        12609  +  VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
 12615  12610   
 12616  12611     /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
 12617  12612     ** OP_IsUnique opcode on this cursor. */
 12618  12613     int seekResult;
 12619  12614   
 12620  12615     /* Cached information about the header for the data record that the
 12621  12616     ** cursor is currently pointing to.  Only valid if cacheStatus matches
................................................................................
 12945  12940   #ifdef SQLITE_OMIT_MERGE_SORT
 12946  12941   # define sqlite3VdbeSorterInit(Y,Z)      SQLITE_OK
 12947  12942   # define sqlite3VdbeSorterWrite(X,Y,Z)   SQLITE_OK
 12948  12943   # define sqlite3VdbeSorterClose(Y,Z)
 12949  12944   # define sqlite3VdbeSorterRowkey(Y,Z)    SQLITE_OK
 12950  12945   # define sqlite3VdbeSorterRewind(X,Y,Z)  SQLITE_OK
 12951  12946   # define sqlite3VdbeSorterNext(X,Y,Z)    SQLITE_OK
        12947  +# define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
 12952  12948   #else
 12953  12949   SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
 12954         -SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, int);
 12955  12950   SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
 12956  12951   SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
 12957         -SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
 12958  12952   SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
        12953  +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
        12954  +SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
        12955  +SQLITE_PRIVATE int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
 12959  12956   #endif
 12960  12957   
 12961  12958   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 12962  12959   SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
 12963  12960   SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
 12964  12961   #else
 12965  12962   # define sqlite3VdbeEnter(X)
................................................................................
 14538  14535     const char *zFile, 
 14539  14536     sqlite3_file **ppFile, 
 14540  14537     int flags,
 14541  14538     int *pOutFlags
 14542  14539   ){
 14543  14540     int rc = SQLITE_NOMEM;
 14544  14541     sqlite3_file *pFile;
 14545         -  pFile = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile);
        14542  +  pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
 14546  14543     if( pFile ){
 14547  14544       rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
 14548  14545       if( rc!=SQLITE_OK ){
 14549  14546         sqlite3_free(pFile);
 14550  14547       }else{
 14551  14548         *ppFile = pFile;
 14552  14549       }
................................................................................
 22101  22098        /*  19 */ "Not",
 22102  22099        /*  20 */ "AddImm",
 22103  22100        /*  21 */ "MustBeInt",
 22104  22101        /*  22 */ "RealAffinity",
 22105  22102        /*  23 */ "Permutation",
 22106  22103        /*  24 */ "Compare",
 22107  22104        /*  25 */ "Jump",
 22108         -     /*  26 */ "If",
 22109         -     /*  27 */ "IfNot",
 22110         -     /*  28 */ "Column",
 22111         -     /*  29 */ "Affinity",
 22112         -     /*  30 */ "MakeRecord",
 22113         -     /*  31 */ "Count",
 22114         -     /*  32 */ "Savepoint",
 22115         -     /*  33 */ "AutoCommit",
 22116         -     /*  34 */ "Transaction",
 22117         -     /*  35 */ "ReadCookie",
 22118         -     /*  36 */ "SetCookie",
 22119         -     /*  37 */ "VerifyCookie",
 22120         -     /*  38 */ "OpenRead",
 22121         -     /*  39 */ "OpenWrite",
 22122         -     /*  40 */ "OpenSorter",
        22105  +     /*  26 */ "Once",
        22106  +     /*  27 */ "If",
        22107  +     /*  28 */ "IfNot",
        22108  +     /*  29 */ "Column",
        22109  +     /*  30 */ "Affinity",
        22110  +     /*  31 */ "MakeRecord",
        22111  +     /*  32 */ "Count",
        22112  +     /*  33 */ "Savepoint",
        22113  +     /*  34 */ "AutoCommit",
        22114  +     /*  35 */ "Transaction",
        22115  +     /*  36 */ "ReadCookie",
        22116  +     /*  37 */ "SetCookie",
        22117  +     /*  38 */ "VerifyCookie",
        22118  +     /*  39 */ "OpenRead",
        22119  +     /*  40 */ "OpenWrite",
 22123  22120        /*  41 */ "OpenAutoindex",
 22124  22121        /*  42 */ "OpenEphemeral",
 22125         -     /*  43 */ "OpenPseudo",
 22126         -     /*  44 */ "Close",
 22127         -     /*  45 */ "SeekLt",
 22128         -     /*  46 */ "SeekLe",
 22129         -     /*  47 */ "SeekGe",
 22130         -     /*  48 */ "SeekGt",
 22131         -     /*  49 */ "Seek",
 22132         -     /*  50 */ "NotFound",
 22133         -     /*  51 */ "Found",
 22134         -     /*  52 */ "IsUnique",
 22135         -     /*  53 */ "NotExists",
 22136         -     /*  54 */ "Sequence",
 22137         -     /*  55 */ "NewRowid",
 22138         -     /*  56 */ "Insert",
 22139         -     /*  57 */ "InsertInt",
 22140         -     /*  58 */ "Delete",
 22141         -     /*  59 */ "ResetCount",
 22142         -     /*  60 */ "RowKey",
 22143         -     /*  61 */ "RowData",
 22144         -     /*  62 */ "Rowid",
 22145         -     /*  63 */ "NullRow",
 22146         -     /*  64 */ "Last",
 22147         -     /*  65 */ "Sort",
 22148         -     /*  66 */ "Rewind",
 22149         -     /*  67 */ "Prev",
        22122  +     /*  43 */ "SorterOpen",
        22123  +     /*  44 */ "OpenPseudo",
        22124  +     /*  45 */ "Close",
        22125  +     /*  46 */ "SeekLt",
        22126  +     /*  47 */ "SeekLe",
        22127  +     /*  48 */ "SeekGe",
        22128  +     /*  49 */ "SeekGt",
        22129  +     /*  50 */ "Seek",
        22130  +     /*  51 */ "NotFound",
        22131  +     /*  52 */ "Found",
        22132  +     /*  53 */ "IsUnique",
        22133  +     /*  54 */ "NotExists",
        22134  +     /*  55 */ "Sequence",
        22135  +     /*  56 */ "NewRowid",
        22136  +     /*  57 */ "Insert",
        22137  +     /*  58 */ "InsertInt",
        22138  +     /*  59 */ "Delete",
        22139  +     /*  60 */ "ResetCount",
        22140  +     /*  61 */ "SorterCompare",
        22141  +     /*  62 */ "SorterData",
        22142  +     /*  63 */ "RowKey",
        22143  +     /*  64 */ "RowData",
        22144  +     /*  65 */ "Rowid",
        22145  +     /*  66 */ "NullRow",
        22146  +     /*  67 */ "Last",
 22150  22147        /*  68 */ "Or",
 22151  22148        /*  69 */ "And",
 22152         -     /*  70 */ "Next",
 22153         -     /*  71 */ "IdxInsert",
 22154         -     /*  72 */ "IdxDelete",
        22149  +     /*  70 */ "SorterSort",
        22150  +     /*  71 */ "Sort",
        22151  +     /*  72 */ "Rewind",
 22155  22152        /*  73 */ "IsNull",
 22156  22153        /*  74 */ "NotNull",
 22157  22154        /*  75 */ "Ne",
 22158  22155        /*  76 */ "Eq",
 22159  22156        /*  77 */ "Gt",
 22160  22157        /*  78 */ "Le",
 22161  22158        /*  79 */ "Lt",
 22162  22159        /*  80 */ "Ge",
 22163         -     /*  81 */ "IdxRowid",
        22160  +     /*  81 */ "SorterNext",
 22164  22161        /*  82 */ "BitAnd",
 22165  22162        /*  83 */ "BitOr",
 22166  22163        /*  84 */ "ShiftLeft",
 22167  22164        /*  85 */ "ShiftRight",
 22168  22165        /*  86 */ "Add",
 22169  22166        /*  87 */ "Subtract",
 22170  22167        /*  88 */ "Multiply",
 22171  22168        /*  89 */ "Divide",
 22172  22169        /*  90 */ "Remainder",
 22173  22170        /*  91 */ "Concat",
 22174         -     /*  92 */ "IdxLT",
        22171  +     /*  92 */ "Prev",
 22175  22172        /*  93 */ "BitNot",
 22176  22173        /*  94 */ "String8",
 22177         -     /*  95 */ "IdxGE",
 22178         -     /*  96 */ "Destroy",
 22179         -     /*  97 */ "Clear",
 22180         -     /*  98 */ "CreateIndex",
 22181         -     /*  99 */ "CreateTable",
 22182         -     /* 100 */ "ParseSchema",
 22183         -     /* 101 */ "LoadAnalysis",
 22184         -     /* 102 */ "DropTable",
 22185         -     /* 103 */ "DropIndex",
 22186         -     /* 104 */ "DropTrigger",
 22187         -     /* 105 */ "IntegrityCk",
 22188         -     /* 106 */ "RowSetAdd",
 22189         -     /* 107 */ "RowSetRead",
 22190         -     /* 108 */ "RowSetTest",
 22191         -     /* 109 */ "Program",
 22192         -     /* 110 */ "Param",
 22193         -     /* 111 */ "FkCounter",
 22194         -     /* 112 */ "FkIfZero",
 22195         -     /* 113 */ "MemMax",
 22196         -     /* 114 */ "IfPos",
 22197         -     /* 115 */ "IfNeg",
 22198         -     /* 116 */ "IfZero",
 22199         -     /* 117 */ "AggStep",
 22200         -     /* 118 */ "AggFinal",
 22201         -     /* 119 */ "Checkpoint",
 22202         -     /* 120 */ "JournalMode",
 22203         -     /* 121 */ "Vacuum",
 22204         -     /* 122 */ "IncrVacuum",
 22205         -     /* 123 */ "Expire",
 22206         -     /* 124 */ "TableLock",
 22207         -     /* 125 */ "VBegin",
 22208         -     /* 126 */ "VCreate",
 22209         -     /* 127 */ "VDestroy",
 22210         -     /* 128 */ "VOpen",
 22211         -     /* 129 */ "VFilter",
        22174  +     /*  95 */ "Next",
        22175  +     /*  96 */ "SorterInsert",
        22176  +     /*  97 */ "IdxInsert",
        22177  +     /*  98 */ "IdxDelete",
        22178  +     /*  99 */ "IdxRowid",
        22179  +     /* 100 */ "IdxLT",
        22180  +     /* 101 */ "IdxGE",
        22181  +     /* 102 */ "Destroy",
        22182  +     /* 103 */ "Clear",
        22183  +     /* 104 */ "CreateIndex",
        22184  +     /* 105 */ "CreateTable",
        22185  +     /* 106 */ "ParseSchema",
        22186  +     /* 107 */ "LoadAnalysis",
        22187  +     /* 108 */ "DropTable",
        22188  +     /* 109 */ "DropIndex",
        22189  +     /* 110 */ "DropTrigger",
        22190  +     /* 111 */ "IntegrityCk",
        22191  +     /* 112 */ "RowSetAdd",
        22192  +     /* 113 */ "RowSetRead",
        22193  +     /* 114 */ "RowSetTest",
        22194  +     /* 115 */ "Program",
        22195  +     /* 116 */ "Param",
        22196  +     /* 117 */ "FkCounter",
        22197  +     /* 118 */ "FkIfZero",
        22198  +     /* 119 */ "MemMax",
        22199  +     /* 120 */ "IfPos",
        22200  +     /* 121 */ "IfNeg",
        22201  +     /* 122 */ "IfZero",
        22202  +     /* 123 */ "AggStep",
        22203  +     /* 124 */ "AggFinal",
        22204  +     /* 125 */ "Checkpoint",
        22205  +     /* 126 */ "JournalMode",
        22206  +     /* 127 */ "Vacuum",
        22207  +     /* 128 */ "IncrVacuum",
        22208  +     /* 129 */ "Expire",
 22212  22209        /* 130 */ "Real",
 22213         -     /* 131 */ "VColumn",
 22214         -     /* 132 */ "VNext",
 22215         -     /* 133 */ "VRename",
 22216         -     /* 134 */ "VUpdate",
 22217         -     /* 135 */ "Pagecount",
 22218         -     /* 136 */ "MaxPgcnt",
 22219         -     /* 137 */ "Trace",
 22220         -     /* 138 */ "Noop",
 22221         -     /* 139 */ "Explain",
 22222         -     /* 140 */ "NotUsed_140",
        22210  +     /* 131 */ "TableLock",
        22211  +     /* 132 */ "VBegin",
        22212  +     /* 133 */ "VCreate",
        22213  +     /* 134 */ "VDestroy",
        22214  +     /* 135 */ "VOpen",
        22215  +     /* 136 */ "VFilter",
        22216  +     /* 137 */ "VColumn",
        22217  +     /* 138 */ "VNext",
        22218  +     /* 139 */ "VRename",
        22219  +     /* 140 */ "VUpdate",
 22223  22220        /* 141 */ "ToText",
 22224  22221        /* 142 */ "ToBlob",
 22225  22222        /* 143 */ "ToNumeric",
 22226  22223        /* 144 */ "ToInt",
 22227  22224        /* 145 */ "ToReal",
        22225  +     /* 146 */ "Pagecount",
        22226  +     /* 147 */ "MaxPgcnt",
        22227  +     /* 148 */ "Trace",
        22228  +     /* 149 */ "Noop",
        22229  +     /* 150 */ "Explain",
 22228  22230     };
 22229  22231     return azName[i];
 22230  22232   }
 22231  22233   #endif
 22232  22234   
 22233  22235   /************** End of opcodes.c *********************************************/
 22234  22236   /************** Begin file os_os2.c ******************************************/
................................................................................
 32796  32798   SQLITE_API int sqlite3_fullsync_count = 0;
 32797  32799   #endif
 32798  32800   
 32799  32801   /*
 32800  32802   ** Make sure all writes to a particular file are committed to disk.
 32801  32803   */
 32802  32804   static int winSync(sqlite3_file *id, int flags){
 32803         -#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || defined(SQLITE_DEBUG)
 32804         -  winFile *pFile = (winFile*)id;
        32805  +#ifndef SQLITE_NO_SYNC
        32806  +  /*
        32807  +  ** Used only when SQLITE_NO_SYNC is not defined.
        32808  +   */
 32805  32809     BOOL rc;
        32810  +#endif
        32811  +#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
        32812  +    (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
        32813  +  /*
        32814  +  ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
        32815  +  ** OSTRACE() macros.
        32816  +   */
        32817  +  winFile *pFile = (winFile*)id;
 32806  32818   #else
 32807  32819     UNUSED_PARAMETER(id);
 32808  32820   #endif
 32809  32821   
 32810  32822     assert( pFile );
 32811  32823     /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
 32812  32824     assert((flags&0x0F)==SQLITE_SYNC_NORMAL
................................................................................
 38138  38150     u8 fullSync;                /* Do extra syncs of the journal for robustness */
 38139  38151     u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
 38140  38152     u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
 38141  38153     u8 tempFile;                /* zFilename is a temporary file */
 38142  38154     u8 readOnly;                /* True for a read-only database */
 38143  38155     u8 memDb;                   /* True to inhibit all file I/O */
 38144  38156     u8 hasSeenStress;           /* pagerStress() called one or more times */
 38145         -  u8 isSorter;                /* True for a PAGER_SORTER */
 38146  38157   
 38147  38158     /**************************************************************************
 38148  38159     ** The following block contains those class members that change during
 38149  38160     ** routine opertion.  Class members not in this block are either fixed
 38150  38161     ** when the pager is first created or else only change when there is a
 38151  38162     ** significant mode change (such as changing the page_size, locking_mode,
 38152  38163     ** or the journal_mode).  From another view, these class members describe
................................................................................
 38362  38373       assert( p->journalMode==PAGER_JOURNALMODE_OFF 
 38363  38374            || p->journalMode==PAGER_JOURNALMODE_MEMORY 
 38364  38375       );
 38365  38376       assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
 38366  38377       assert( pagerUseWal(p)==0 );
 38367  38378     }
 38368  38379   
 38369         -  /* A sorter is a temp file that never spills to disk and always has
 38370         -  ** the doNotSpill flag set
 38371         -  */
 38372         -  if( p->isSorter ){
 38373         -    assert( p->tempFile );
 38374         -    assert( p->doNotSpill );
 38375         -    assert( p->fd->pMethods==0 );
 38376         -  }
 38377         -
 38378  38380     /* If changeCountDone is set, a RESERVED lock or greater must be held
 38379  38381     ** on the file.
 38380  38382     */
 38381  38383     assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
 38382  38384     assert( p->eLock!=PENDING_LOCK );
 38383  38385   
 38384  38386     switch( p->eState ){
................................................................................
 42074  42076     }else if( memDb ){
 42075  42077       pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
 42076  42078     }
 42077  42079     /* pPager->xBusyHandler = 0; */
 42078  42080     /* pPager->pBusyHandlerArg = 0; */
 42079  42081     pPager->xReiniter = xReinit;
 42080  42082     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
 42081         -#ifndef SQLITE_OMIT_MERGE_SORT
 42082         -  if( flags & PAGER_SORTER ){
 42083         -    pPager->doNotSpill = 1;
 42084         -    pPager->isSorter = 1;
 42085         -  }
 42086         -#endif
 42087  42083   
 42088  42084     *ppPager = pPager;
 42089  42085     return SQLITE_OK;
 42090  42086   }
 42091  42087   
 42092  42088   
 42093  42089   
................................................................................
 43624  43620   /*
 43625  43621   ** Return true if this is an in-memory pager.
 43626  43622   */
 43627  43623   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
 43628  43624     return MEMDB;
 43629  43625   }
 43630  43626   
 43631         -#ifndef SQLITE_OMIT_MERGE_SORT
 43632         -/*
 43633         -** Return true if the pager has seen a pagerStress callback.
 43634         -*/
 43635         -SQLITE_PRIVATE int sqlite3PagerUnderStress(Pager *pPager){
 43636         -  assert( pPager->isSorter );
 43637         -  assert( pPager->doNotSpill );
 43638         -  return pPager->hasSeenStress;
 43639         -}
 43640         -#endif
 43641         -
 43642  43627   /*
 43643  43628   ** Check that there are at least nSavepoint savepoints open. If there are
 43644  43629   ** currently less than nSavepoints open, then open one or more savepoints
 43645  43630   ** to make up the difference. If the number of savepoints is already
 43646  43631   ** equal to nSavepoint, then this function is a no-op.
 43647  43632   **
 43648  43633   ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 
................................................................................
 48925  48910     i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
 48926  48911     int bias,           /* Bias search to the high end */
 48927  48912     int *pRes           /* Write search results here */
 48928  48913   ){
 48929  48914     int rc;                    /* Status code */
 48930  48915     UnpackedRecord *pIdxKey;   /* Unpacked index key */
 48931  48916     char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
        48917  +  char *pFree = 0;
 48932  48918   
 48933  48919     if( pKey ){
 48934  48920       assert( nKey==(i64)(int)nKey );
 48935         -    pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey,
 48936         -                                      aSpace, sizeof(aSpace));
        48921  +    pIdxKey = sqlite3VdbeAllocUnpackedRecord(
        48922  +        pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
        48923  +    );
 48937  48924       if( pIdxKey==0 ) return SQLITE_NOMEM;
        48925  +    sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
 48938  48926     }else{
 48939  48927       pIdxKey = 0;
 48940  48928     }
 48941  48929     rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
 48942         -  if( pKey ){
 48943         -    sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
        48930  +  if( pFree ){
        48931  +    sqlite3DbFree(pCur->pKeyInfo->db, pFree);
 48944  48932     }
 48945  48933     return rc;
 48946  48934   }
 48947  48935   
 48948  48936   /*
 48949  48937   ** Restore the cursor to the position it was in (or as close to as possible)
 48950  48938   ** when saveCursorPosition() was called. Note that this call deletes the 
................................................................................
 50003  49991   
 50004  49992     /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
 50005  49993     assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
 50006  49994   
 50007  49995     /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
 50008  49996     assert( (flags & BTREE_SINGLE)==0 || isTempDb );
 50009  49997   
 50010         -  /* The BTREE_SORTER flag is only used if SQLITE_OMIT_MERGE_SORT is undef */
 50011         -#ifdef SQLITE_OMIT_MERGE_SORT
 50012         -  assert( (flags & BTREE_SORTER)==0 );
 50013         -#endif
 50014         -
 50015         -  /* BTREE_SORTER is always on a BTREE_SINGLE, BTREE_OMIT_JOURNAL */
 50016         -  assert( (flags & BTREE_SORTER)==0 ||
 50017         -          (flags & (BTREE_SINGLE|BTREE_OMIT_JOURNAL))
 50018         -                                        ==(BTREE_SINGLE|BTREE_OMIT_JOURNAL) );
 50019         -
 50020  49998     if( db->flags & SQLITE_NoReadlock ){
 50021  49999       flags |= BTREE_NO_READLOCK;
 50022  50000     }
 50023  50001     if( isMemdb ){
 50024  50002       flags |= BTREE_MEMORY;
 50025         -    flags &= ~BTREE_SORTER;
 50026  50003     }
 50027  50004     if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
 50028  50005       vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
 50029  50006     }
 50030  50007     p = sqlite3MallocZero(sizeof(Btree));
 50031  50008     if( !p ){
 50032  50009       return SQLITE_NOMEM;
................................................................................
 55565  55542       */
 55566  55543       zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
 55567  55544       releasePage(pPage);
 55568  55545     }
 55569  55546     return rc;  
 55570  55547   }
 55571  55548   SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
 55572         -  BtShared *pBt = p->pBt;
 55573  55549     int rc;
 55574  55550     sqlite3BtreeEnter(p);
 55575         -  if( (pBt->openFlags&BTREE_SINGLE) ){
 55576         -    pBt->nPage = 0;
 55577         -    sqlite3PagerTruncateImage(pBt->pPager, 1);
 55578         -    rc = newDatabase(pBt);
 55579         -  }else{
 55580         -    rc = btreeDropTable(p, iTable, piMoved);
 55581         -  }
        55551  +  rc = btreeDropTable(p, iTable, piMoved);
 55582  55552     sqlite3BtreeLeave(p);
 55583  55553     return rc;
 55584  55554   }
 55585  55555   
 55586  55556   
 55587  55557   /*
 55588  55558   ** This function may only be called if the b-tree connection already
................................................................................
 58763  58733       }else if( opcode==OP_VFilter ){
 58764  58734         int n;
 58765  58735         assert( p->nOp - i >= 3 );
 58766  58736         assert( pOp[-1].opcode==OP_Integer );
 58767  58737         n = pOp[-1].p1;
 58768  58738         if( n>nMaxArgs ) nMaxArgs = n;
 58769  58739   #endif
 58770         -    }else if( opcode==OP_Next ){
        58740  +    }else if( opcode==OP_Next || opcode==OP_SorterNext ){
 58771  58741         pOp->p4.xAdvance = sqlite3BtreeNext;
 58772  58742         pOp->p4type = P4_ADVANCE;
 58773  58743       }else if( opcode==OP_Prev ){
 58774  58744         pOp->p4.xAdvance = sqlite3BtreePrevious;
 58775  58745         pOp->p4type = P4_ADVANCE;
 58776  58746       }
 58777  58747   
................................................................................
 59000  58970   */
 59001  58971   SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
 59002  58972     p->pNext = pVdbe->pProgram;
 59003  58973     pVdbe->pProgram = p;
 59004  58974   }
 59005  58975   
 59006  58976   /*
 59007         -** Change N opcodes starting at addr to No-ops.
        58977  +** Change the opcode at addr into OP_Noop
 59008  58978   */
 59009         -SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
        58979  +SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
 59010  58980     if( p->aOp ){
 59011  58981       VdbeOp *pOp = &p->aOp[addr];
 59012  58982       sqlite3 *db = p->db;
 59013         -    while( N-- ){
 59014         -      freeP4(db, pOp->p4type, pOp->p4.p);
 59015         -      memset(pOp, 0, sizeof(pOp[0]));
 59016         -      pOp->opcode = OP_Noop;
 59017         -      pOp++;
 59018         -    }
        58983  +    freeP4(db, pOp->p4type, pOp->p4.p);
        58984  +    memset(pOp, 0, sizeof(pOp[0]));
        58985  +    pOp->opcode = OP_Noop;
 59019  58986     }
 59020  58987   }
 59021  58988   
 59022  58989   /*
 59023  58990   ** Change the value of the P4 operand for a specific instruction.
 59024  58991   ** This routine is useful when a large program is loaded from a
 59025  58992   ** static array using sqlite3VdbeAddOpList but we want to make a
................................................................................
 59167  59134   ** having to double-check to make sure that the result is non-negative. But
 59168  59135   ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
 59169  59136   ** check the value of p->nOp-1 before continuing.
 59170  59137   */
 59171  59138   SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
 59172  59139     /* C89 specifies that the constant "dummy" will be initialized to all
 59173  59140     ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
 59174         -  static const VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
        59141  +  static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
 59175  59142     assert( p->magic==VDBE_MAGIC_INIT );
 59176  59143     if( addr<0 ){
 59177  59144   #ifdef SQLITE_OMIT_TRACE
 59178  59145       if( p->nOp==0 ) return (VdbeOp*)&dummy;
 59179  59146   #endif
 59180  59147       addr = p->nOp - 1;
 59181  59148     }
................................................................................
 61160  61127         }
 61161  61128         return len;
 61162  61129       }
 61163  61130     }
 61164  61131     return 0;
 61165  61132   }
 61166  61133   
 61167         -
 61168         -/*
 61169         -** Given the nKey-byte encoding of a record in pKey[], parse the
 61170         -** record into a UnpackedRecord structure.  Return a pointer to
 61171         -** that structure.
 61172         -**
 61173         -** The calling function might provide szSpace bytes of memory
 61174         -** space at pSpace.  This space can be used to hold the returned
 61175         -** VDbeParsedRecord structure if it is large enough.  If it is
 61176         -** not big enough, space is obtained from sqlite3_malloc().
 61177         -**
 61178         -** The returned structure should be closed by a call to
 61179         -** sqlite3VdbeDeleteUnpackedRecord().
 61180         -*/ 
 61181         -SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(
 61182         -  KeyInfo *pKeyInfo,     /* Information about the record format */
 61183         -  int nKey,              /* Size of the binary record */
 61184         -  const void *pKey,      /* The binary record */
 61185         -  char *pSpace,          /* Unaligned space available to hold the object */
 61186         -  int szSpace            /* Size of pSpace[] in bytes */
 61187         -){
 61188         -  const unsigned char *aKey = (const unsigned char *)pKey;
 61189         -  UnpackedRecord *p;  /* The unpacked record that we will return */
 61190         -  int nByte;          /* Memory space needed to hold p, in bytes */
 61191         -  int d;
 61192         -  u32 idx;
 61193         -  u16 u;              /* Unsigned loop counter */
 61194         -  u32 szHdr;
 61195         -  Mem *pMem;
 61196         -  int nOff;           /* Increase pSpace by this much to 8-byte align it */
 61197         -  
 61198         -  /*
 61199         -  ** We want to shift the pointer pSpace up such that it is 8-byte aligned.
        61134  +/*
        61135  +** This routine is used to allocate sufficient space for an UnpackedRecord
        61136  +** structure large enough to be used with sqlite3VdbeRecordUnpack() if
        61137  +** the first argument is a pointer to KeyInfo structure pKeyInfo.
        61138  +**
        61139  +** The space is either allocated using sqlite3DbMallocRaw() or from within
        61140  +** the unaligned buffer passed via the second and third arguments (presumably
        61141  +** stack space). If the former, then *ppFree is set to a pointer that should
        61142  +** be eventually freed by the caller using sqlite3DbFree(). Or, if the 
        61143  +** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
        61144  +** before returning.
        61145  +**
        61146  +** If an OOM error occurs, NULL is returned.
        61147  +*/
        61148  +SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
        61149  +  KeyInfo *pKeyInfo,              /* Description of the record */
        61150  +  char *pSpace,                   /* Unaligned space available */
        61151  +  int szSpace,                    /* Size of pSpace[] in bytes */
        61152  +  char **ppFree                   /* OUT: Caller should free this pointer */
        61153  +){
        61154  +  UnpackedRecord *p;              /* Unpacked record to return */
        61155  +  int nOff;                       /* Increment pSpace by nOff to align it */
        61156  +  int nByte;                      /* Number of bytes required for *p */
        61157  +
        61158  +  /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
 61200  61159     ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift 
 61201  61160     ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
 61202  61161     */
 61203  61162     nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
 61204         -  pSpace += nOff;
 61205         -  szSpace -= nOff;
 61206  61163     nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
 61207         -  if( nByte>szSpace ){
 61208         -    p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
 61209         -    if( p==0 ) return 0;
 61210         -    p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY;
        61164  +  if( nByte>szSpace+nOff ){
        61165  +    p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
        61166  +    *ppFree = (char *)p;
        61167  +    if( !p ) return 0;
 61211  61168     }else{
 61212         -    p = (UnpackedRecord*)pSpace;
 61213         -    p->flags = UNPACKED_NEED_DESTROY;
        61169  +    p = (UnpackedRecord*)&pSpace[nOff];
        61170  +    *ppFree = 0;
 61214  61171     }
        61172  +
        61173  +  p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
 61215  61174     p->pKeyInfo = pKeyInfo;
 61216  61175     p->nField = pKeyInfo->nField + 1;
 61217         -  p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
        61176  +  return p;
        61177  +}
        61178  +
        61179  +/*
        61180  +** Given the nKey-byte encoding of a record in pKey[], populate the 
        61181  +** UnpackedRecord structure indicated by the fourth argument with the
        61182  +** contents of the decoded record.
        61183  +*/ 
        61184  +SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
        61185  +  KeyInfo *pKeyInfo,     /* Information about the record format */
        61186  +  int nKey,              /* Size of the binary record */
        61187  +  const void *pKey,      /* The binary record */
        61188  +  UnpackedRecord *p      /* Populate this structure before returning. */
        61189  +){
        61190  +  const unsigned char *aKey = (const unsigned char *)pKey;
        61191  +  int d; 
        61192  +  u32 idx;                        /* Offset in aKey[] to read from */
        61193  +  u16 u;                          /* Unsigned loop counter */
        61194  +  u32 szHdr;
        61195  +  Mem *pMem = p->aMem;
        61196  +
        61197  +  p->flags = 0;
 61218  61198     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 61219  61199     idx = getVarint32(aKey, szHdr);
 61220  61200     d = szHdr;
 61221  61201     u = 0;
 61222  61202     while( idx<szHdr && u<p->nField && d<=nKey ){
 61223  61203       u32 serial_type;
 61224  61204   
................................................................................
 61229  61209       pMem->zMalloc = 0;
 61230  61210       d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
 61231  61211       pMem++;
 61232  61212       u++;
 61233  61213     }
 61234  61214     assert( u<=pKeyInfo->nField + 1 );
 61235  61215     p->nField = u;
 61236         -  return (void*)p;
 61237         -}
 61238         -
 61239         -/*
 61240         -** This routine destroys a UnpackedRecord object.
 61241         -*/
 61242         -SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
 61243         -#ifdef SQLITE_DEBUG
 61244         -  int i;
 61245         -  Mem *pMem;
 61246         -
 61247         -  assert( p!=0 );
 61248         -  assert( p->flags & UNPACKED_NEED_DESTROY );
 61249         -  for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
 61250         -    /* The unpacked record is always constructed by the
 61251         -    ** sqlite3VdbeUnpackRecord() function above, which makes all
 61252         -    ** strings and blobs static.  And none of the elements are
 61253         -    ** ever transformed, so there is never anything to delete.
 61254         -    */
 61255         -    if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem);
 61256         -  }
 61257         -#endif
 61258         -  if( p->flags & UNPACKED_NEED_FREE ){
 61259         -    sqlite3DbFree(p->pKeyInfo->db, p);
 61260         -  }
 61261  61216   }
 61262  61217   
 61263  61218   /*
 61264  61219   ** This function compares the two table rows or index records
 61265  61220   ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
 61266  61221   ** or positive integer if key1 is less than, equal to or 
 61267  61222   ** greater than key2.  The {nKey1, pKey1} key must be a blob
................................................................................
 63876  63831         Btree *pX;
 63877  63832         VdbeCursor *pCur;
 63878  63833         Db *pDb;
 63879  63834       } aw;
 63880  63835       struct OP_OpenEphemeral_stack_vars {
 63881  63836         VdbeCursor *pCx;
 63882  63837       } ax;
 63883         -    struct OP_OpenPseudo_stack_vars {
        63838  +    struct OP_SorterOpen_stack_vars {
 63884  63839         VdbeCursor *pCx;
 63885  63840       } ay;
        63841  +    struct OP_OpenPseudo_stack_vars {
        63842  +      VdbeCursor *pCx;
        63843  +    } az;
 63886  63844       struct OP_SeekGt_stack_vars {
 63887  63845         int res;
 63888  63846         int oc;
 63889  63847         VdbeCursor *pC;
 63890  63848         UnpackedRecord r;
 63891  63849         int nField;
 63892  63850         i64 iKey;      /* The rowid we are to seek to */
 63893         -    } az;
        63851  +    } ba;
 63894  63852       struct OP_Seek_stack_vars {
 63895  63853         VdbeCursor *pC;
 63896         -    } ba;
        63854  +    } bb;
 63897  63855       struct OP_Found_stack_vars {
 63898  63856         int alreadyExists;
 63899  63857         VdbeCursor *pC;
 63900  63858         int res;
        63859  +      char *pFree;
 63901  63860         UnpackedRecord *pIdxKey;
 63902  63861         UnpackedRecord r;
 63903  63862         char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
 63904         -    } bb;
        63863  +    } bc;
 63905  63864       struct OP_IsUnique_stack_vars {
 63906  63865         u16 ii;
 63907  63866         VdbeCursor *pCx;
 63908  63867         BtCursor *pCrsr;
 63909  63868         u16 nField;
 63910  63869         Mem *aMx;
 63911  63870         UnpackedRecord r;                  /* B-Tree index search key */
 63912  63871         i64 R;                             /* Rowid stored in register P3 */
 63913         -    } bc;
        63872  +    } bd;
 63914  63873       struct OP_NotExists_stack_vars {
 63915  63874         VdbeCursor *pC;
 63916  63875         BtCursor *pCrsr;
 63917  63876         int res;
 63918  63877         u64 iKey;
 63919         -    } bd;
        63878  +    } be;
 63920  63879       struct OP_NewRowid_stack_vars {
 63921  63880         i64 v;                 /* The new rowid */
 63922  63881         VdbeCursor *pC;        /* Cursor of table to get the new rowid */
 63923  63882         int res;               /* Result of an sqlite3BtreeLast() */
 63924  63883         int cnt;               /* Counter to limit the number of searches */
 63925  63884         Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
 63926  63885         VdbeFrame *pFrame;     /* Root frame of VDBE */
 63927         -    } be;
        63886  +    } bf;
 63928  63887       struct OP_InsertInt_stack_vars {
 63929  63888         Mem *pData;       /* MEM cell holding data for the record to be inserted */
 63930  63889         Mem *pKey;        /* MEM cell holding key  for the record */
 63931  63890         i64 iKey;         /* The integer ROWID or key for the record to be inserted */
 63932  63891         VdbeCursor *pC;   /* Cursor to table into which insert is written */
 63933  63892         int nZero;        /* Number of zero-bytes to append */
 63934  63893         int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 63935  63894         const char *zDb;  /* database name - used by the update hook */
 63936  63895         const char *zTbl; /* Table name - used by the opdate hook */
 63937  63896         int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
 63938         -    } bf;
        63897  +    } bg;
 63939  63898       struct OP_Delete_stack_vars {
 63940  63899         i64 iKey;
 63941  63900         VdbeCursor *pC;
 63942         -    } bg;
        63901  +    } bh;
        63902  +    struct OP_SorterCompare_stack_vars {
        63903  +      VdbeCursor *pC;
        63904  +      int res;
        63905  +    } bi;
        63906  +    struct OP_SorterData_stack_vars {
        63907  +      VdbeCursor *pC;
        63908  +    } bj;
 63943  63909       struct OP_RowData_stack_vars {
 63944  63910         VdbeCursor *pC;
 63945  63911         BtCursor *pCrsr;
 63946  63912         u32 n;
 63947  63913         i64 n64;
 63948         -    } bh;
        63914  +    } bk;
 63949  63915       struct OP_Rowid_stack_vars {
 63950  63916         VdbeCursor *pC;
 63951  63917         i64 v;
 63952  63918         sqlite3_vtab *pVtab;
 63953  63919         const sqlite3_module *pModule;
 63954         -    } bi;
        63920  +    } bl;
 63955  63921       struct OP_NullRow_stack_vars {
 63956  63922         VdbeCursor *pC;
 63957         -    } bj;
        63923  +    } bm;
 63958  63924       struct OP_Last_stack_vars {
 63959  63925         VdbeCursor *pC;
 63960  63926         BtCursor *pCrsr;
 63961  63927         int res;
 63962         -    } bk;
        63928  +    } bn;
 63963  63929       struct OP_Rewind_stack_vars {
 63964  63930         VdbeCursor *pC;
 63965  63931         BtCursor *pCrsr;
 63966  63932         int res;
 63967         -    } bl;
        63933  +    } bo;
 63968  63934       struct OP_Next_stack_vars {
 63969  63935         VdbeCursor *pC;
 63970  63936         int res;
 63971         -    } bm;
        63937  +    } bp;
 63972  63938       struct OP_IdxInsert_stack_vars {
 63973  63939         VdbeCursor *pC;
 63974  63940         BtCursor *pCrsr;
 63975  63941         int nKey;
 63976  63942         const char *zKey;
 63977         -    } bn;
        63943  +    } bq;
 63978  63944       struct OP_IdxDelete_stack_vars {
 63979  63945         VdbeCursor *pC;
 63980  63946         BtCursor *pCrsr;
 63981  63947         int res;
 63982  63948         UnpackedRecord r;
 63983         -    } bo;
        63949  +    } br;
 63984  63950       struct OP_IdxRowid_stack_vars {
 63985  63951         BtCursor *pCrsr;
 63986  63952         VdbeCursor *pC;
 63987  63953         i64 rowid;
 63988         -    } bp;
        63954  +    } bs;
 63989  63955       struct OP_IdxGE_stack_vars {
 63990  63956         VdbeCursor *pC;
 63991  63957         int res;
 63992  63958         UnpackedRecord r;
 63993         -    } bq;
        63959  +    } bt;
 63994  63960       struct OP_Destroy_stack_vars {
 63995  63961         int iMoved;
 63996  63962         int iCnt;
 63997  63963         Vdbe *pVdbe;
 63998  63964         int iDb;
 63999         -    } br;
        63965  +    } bu;
 64000  63966       struct OP_Clear_stack_vars {
 64001  63967         int nChange;
 64002         -    } bs;
        63968  +    } bv;
 64003  63969       struct OP_CreateTable_stack_vars {
 64004  63970         int pgno;
 64005  63971         int flags;
 64006  63972         Db *pDb;
 64007         -    } bt;
        63973  +    } bw;
 64008  63974       struct OP_ParseSchema_stack_vars {
 64009  63975         int iDb;
 64010  63976         const char *zMaster;
 64011  63977         char *zSql;
 64012  63978         InitData initData;
 64013         -    } bu;
        63979  +    } bx;
 64014  63980       struct OP_IntegrityCk_stack_vars {
 64015  63981         int nRoot;      /* Number of tables to check.  (Number of root pages.) */
 64016  63982         int *aRoot;     /* Array of rootpage numbers for tables to be checked */
 64017  63983         int j;          /* Loop counter */
 64018  63984         int nErr;       /* Number of errors reported */
 64019  63985         char *z;        /* Text of the error report */
 64020  63986         Mem *pnErr;     /* Register keeping track of errors remaining */
 64021         -    } bv;
        63987  +    } by;
 64022  63988       struct OP_RowSetRead_stack_vars {
 64023  63989         i64 val;
 64024         -    } bw;
        63990  +    } bz;
 64025  63991       struct OP_RowSetTest_stack_vars {
 64026  63992         int iSet;
 64027  63993         int exists;
 64028         -    } bx;
        63994  +    } ca;
 64029  63995       struct OP_Program_stack_vars {
 64030  63996         int nMem;               /* Number of memory registers for sub-program */
 64031  63997         int nByte;              /* Bytes of runtime space required for sub-program */
 64032  63998         Mem *pRt;               /* Register to allocate runtime space */
 64033  63999         Mem *pMem;              /* Used to iterate through memory cells */
 64034  64000         Mem *pEnd;              /* Last memory cell in new array */
 64035  64001         VdbeFrame *pFrame;      /* New vdbe frame to execute in */
 64036  64002         SubProgram *pProgram;   /* Sub-program to execute */
 64037  64003         void *t;                /* Token identifying trigger */
 64038         -    } by;
        64004  +    } cb;
 64039  64005       struct OP_Param_stack_vars {
 64040  64006         VdbeFrame *pFrame;
 64041  64007         Mem *pIn;
 64042         -    } bz;
        64008  +    } cc;
 64043  64009       struct OP_MemMax_stack_vars {
 64044  64010         Mem *pIn1;
 64045  64011         VdbeFrame *pFrame;
 64046         -    } ca;
        64012  +    } cd;
 64047  64013       struct OP_AggStep_stack_vars {
 64048  64014         int n;
 64049  64015         int i;
 64050  64016         Mem *pMem;
 64051  64017         Mem *pRec;
 64052  64018         sqlite3_context ctx;
 64053  64019         sqlite3_value **apVal;
 64054         -    } cb;
        64020  +    } ce;
 64055  64021       struct OP_AggFinal_stack_vars {
 64056  64022         Mem *pMem;
 64057         -    } cc;
        64023  +    } cf;
 64058  64024       struct OP_Checkpoint_stack_vars {
 64059  64025         int i;                          /* Loop counter */
 64060  64026         int aRes[3];                    /* Results */
 64061  64027         Mem *pMem;                      /* Write results here */
 64062         -    } cd;
        64028  +    } cg;
 64063  64029       struct OP_JournalMode_stack_vars {
 64064  64030         Btree *pBt;                     /* Btree to change journal mode of */
 64065  64031         Pager *pPager;                  /* Pager associated with pBt */
 64066  64032         int eNew;                       /* New journal mode */
 64067  64033         int eOld;                       /* The old journal mode */
 64068  64034         const char *zFilename;          /* Name of database file for pPager */
 64069         -    } ce;
        64035  +    } ch;
 64070  64036       struct OP_IncrVacuum_stack_vars {
 64071  64037         Btree *pBt;
 64072         -    } cf;
        64038  +    } ci;
 64073  64039       struct OP_VBegin_stack_vars {
 64074  64040         VTable *pVTab;
 64075         -    } cg;
        64041  +    } cj;
 64076  64042       struct OP_VOpen_stack_vars {
 64077  64043         VdbeCursor *pCur;
 64078  64044         sqlite3_vtab_cursor *pVtabCursor;
 64079  64045         sqlite3_vtab *pVtab;
 64080  64046         sqlite3_module *pModule;
 64081         -    } ch;
        64047  +    } ck;
 64082  64048       struct OP_VFilter_stack_vars {
 64083  64049         int nArg;
 64084  64050         int iQuery;
 64085  64051         const sqlite3_module *pModule;
 64086  64052         Mem *pQuery;
 64087  64053         Mem *pArgc;
 64088  64054         sqlite3_vtab_cursor *pVtabCursor;
 64089  64055         sqlite3_vtab *pVtab;
 64090  64056         VdbeCursor *pCur;
 64091  64057         int res;
 64092  64058         int i;
 64093  64059         Mem **apArg;
 64094         -    } ci;
        64060  +    } cl;
 64095  64061       struct OP_VColumn_stack_vars {
 64096  64062         sqlite3_vtab *pVtab;
 64097  64063         const sqlite3_module *pModule;
 64098  64064         Mem *pDest;
 64099  64065         sqlite3_context sContext;
 64100         -    } cj;
        64066  +    } cm;
 64101  64067       struct OP_VNext_stack_vars {
 64102  64068         sqlite3_vtab *pVtab;
 64103  64069         const sqlite3_module *pModule;
 64104  64070         int res;
 64105  64071         VdbeCursor *pCur;
 64106         -    } ck;
        64072  +    } cn;
 64107  64073       struct OP_VRename_stack_vars {
 64108  64074         sqlite3_vtab *pVtab;
 64109  64075         Mem *pName;
 64110         -    } cl;
        64076  +    } co;
 64111  64077       struct OP_VUpdate_stack_vars {
 64112  64078         sqlite3_vtab *pVtab;
 64113  64079         sqlite3_module *pModule;
 64114  64080         int nArg;
 64115  64081         int i;
 64116  64082         sqlite_int64 rowid;
 64117  64083         Mem **apArg;
 64118  64084         Mem *pX;
 64119         -    } cm;
        64085  +    } cp;
 64120  64086       struct OP_Trace_stack_vars {
 64121  64087         char *zTrace;
 64122  64088         char *z;
 64123         -    } cn;
        64089  +    } cq;
 64124  64090     } u;
 64125  64091     /* End automatically generated code
 64126  64092     ********************************************************************/
 64127  64093   
 64128  64094     assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 64129  64095     sqlite3VdbeEnter(p);
 64130  64096     if( p->rc==SQLITE_NOMEM ){
................................................................................
 65586  65552       sqlite3VdbeMemSetNull(pOut);
 65587  65553     }else{
 65588  65554       sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
 65589  65555     }
 65590  65556     break;
 65591  65557   }
 65592  65558   
        65559  +/* Opcode: Once P1 P2 * * *
        65560  +**
        65561  +** Jump to P2 if the value in register P1 is a not null or zero.  If
        65562  +** the value is NULL or zero, fall through and change the P1 register
        65563  +** to an integer 1.
        65564  +**
        65565  +** When P1 is not used otherwise in a program, this opcode falls through
        65566  +** once and jumps on all subsequent invocations.  It is the equivalent
        65567  +** of "OP_If P1 P2", followed by "OP_Integer 1 P1".
        65568  +*/
 65593  65569   /* Opcode: If P1 P2 P3 * *
 65594  65570   **
 65595  65571   ** Jump to P2 if the value in register P1 is true.  The value
 65596  65572   ** is considered true if it is numeric and non-zero.  If the value
 65597  65573   ** in P1 is NULL then take the jump if P3 is true.
 65598  65574   */
 65599  65575   /* Opcode: IfNot P1 P2 P3 * *
 65600  65576   **
 65601  65577   ** Jump to P2 if the value in register P1 is False.  The value
 65602  65578   ** is considered true if it has a numeric value of zero.  If the value
 65603  65579   ** in P1 is NULL then take the jump if P3 is true.
 65604  65580   */
        65581  +case OP_Once:               /* jump, in1 */
 65605  65582   case OP_If:                 /* jump, in1 */
 65606  65583   case OP_IfNot: {            /* jump, in1 */
 65607  65584   #if 0  /* local variables moved into u.al */
 65608  65585     int c;
 65609  65586   #endif /* local variables moved into u.al */
 65610  65587     pIn1 = &aMem[pOp->p1];
 65611  65588     if( pIn1->flags & MEM_Null ){
................................................................................
 65616  65593   #else
 65617  65594       u.al.c = sqlite3VdbeRealValue(pIn1)!=0.0;
 65618  65595   #endif
 65619  65596       if( pOp->opcode==OP_IfNot ) u.al.c = !u.al.c;
 65620  65597     }
 65621  65598     if( u.al.c ){
 65622  65599       pc = pOp->p2-1;
        65600  +  }else if( pOp->opcode==OP_Once ){
        65601  +    assert( (pIn1->flags & (MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))==0 );
        65602  +    memAboutToChange(p, pIn1);
        65603  +    pIn1->flags = MEM_Int;
        65604  +    pIn1->u.i = 1;
        65605  +    REGISTER_TRACE(pOp->p1, pIn1);
 65623  65606     }
 65624  65607     break;
 65625  65608   }
 65626  65609   
 65627  65610   /* Opcode: IsNull P1 P2 * * *
 65628  65611   **
 65629  65612   ** Jump to P2 if the value in register P1 is NULL.
................................................................................
 66751  66734   /* Opcode: OpenAutoindex P1 P2 * P4 *
 66752  66735   **
 66753  66736   ** This opcode works the same as OP_OpenEphemeral.  It has a
 66754  66737   ** different name to distinguish its use.  Tables created using
 66755  66738   ** by this opcode will be used for automatically created transient
 66756  66739   ** indices in joins.
 66757  66740   */
 66758         -/* Opcode: OpenSorter P1 P2 * P4 *
 66759         -**
 66760         -** This opcode works like OP_OpenEphemeral except that it opens
 66761         -** a transient index that is specifically designed to sort large
 66762         -** tables using an external merge-sort algorithm.
 66763         -*/
 66764         -case OP_OpenSorter: 
 66765  66741   case OP_OpenAutoindex: 
 66766  66742   case OP_OpenEphemeral: {
 66767  66743   #if 0  /* local variables moved into u.ax */
 66768  66744     VdbeCursor *pCx;
 66769  66745   #endif /* local variables moved into u.ax */
 66770  66746     static const int vfsFlags =
 66771  66747         SQLITE_OPEN_READWRITE |
 66772  66748         SQLITE_OPEN_CREATE |
 66773  66749         SQLITE_OPEN_EXCLUSIVE |
 66774  66750         SQLITE_OPEN_DELETEONCLOSE |
 66775  66751         SQLITE_OPEN_TRANSIENT_DB;
 66776  66752   
 66777  66753     assert( pOp->p1>=0 );
 66778         -  assert( (pOp->opcode==OP_OpenSorter)==((pOp->p5 & BTREE_SORTER)!=0) );
 66779  66754     u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
 66780  66755     if( u.ax.pCx==0 ) goto no_mem;
 66781  66756     u.ax.pCx->nullRow = 1;
 66782  66757     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ax.pCx->pBt,
 66783  66758                           BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
 66784  66759     if( rc==SQLITE_OK ){
 66785  66760       rc = sqlite3BtreeBeginTrans(u.ax.pCx->pBt, 1);
................................................................................
 66805  66780       }else{
 66806  66781         rc = sqlite3BtreeCursor(u.ax.pCx->pBt, MASTER_ROOT, 1, 0, u.ax.pCx->pCursor);
 66807  66782         u.ax.pCx->isTable = 1;
 66808  66783       }
 66809  66784     }
 66810  66785     u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
 66811  66786     u.ax.pCx->isIndex = !u.ax.pCx->isTable;
        66787  +  break;
        66788  +}
        66789  +
        66790  +/* Opcode: OpenSorter P1 P2 * P4 *
        66791  +**
        66792  +** This opcode works like OP_OpenEphemeral except that it opens
        66793  +** a transient index that is specifically designed to sort large
        66794  +** tables using an external merge-sort algorithm.
        66795  +*/
        66796  +case OP_SorterOpen: {
        66797  +#if 0  /* local variables moved into u.ay */
        66798  +  VdbeCursor *pCx;
        66799  +#endif /* local variables moved into u.ay */
 66812  66800   #ifndef SQLITE_OMIT_MERGE_SORT
 66813         -  if( rc==SQLITE_OK && pOp->opcode==OP_OpenSorter ){
 66814         -    rc = sqlite3VdbeSorterInit(db, u.ax.pCx);
 66815         -  }
        66801  +  u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
        66802  +  if( u.ay.pCx==0 ) goto no_mem;
        66803  +  u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
        66804  +  u.ay.pCx->pKeyInfo->enc = ENC(p->db);
        66805  +  u.ay.pCx->isSorter = 1;
        66806  +  rc = sqlite3VdbeSorterInit(db, u.ay.pCx);
        66807  +#else
        66808  +  pOp->opcode = OP_OpenEphemeral;
        66809  +  pc--;
 66816  66810   #endif
 66817  66811     break;
 66818  66812   }
 66819  66813   
 66820  66814   /* Opcode: OpenPseudo P1 P2 P3 * *
 66821  66815   **
 66822  66816   ** Open a new cursor that points to a fake table that contains a single
................................................................................
 66829  66823   ** individual columns using the OP_Column opcode.  The OP_Column opcode
 66830  66824   ** is the only cursor opcode that works with a pseudo-table.
 66831  66825   **
 66832  66826   ** P3 is the number of fields in the records that will be stored by
 66833  66827   ** the pseudo-table.
 66834  66828   */
 66835  66829   case OP_OpenPseudo: {
 66836         -#if 0  /* local variables moved into u.ay */
        66830  +#if 0  /* local variables moved into u.az */
 66837  66831     VdbeCursor *pCx;
 66838         -#endif /* local variables moved into u.ay */
        66832  +#endif /* local variables moved into u.az */
 66839  66833   
 66840  66834     assert( pOp->p1>=0 );
 66841         -  u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
 66842         -  if( u.ay.pCx==0 ) goto no_mem;
 66843         -  u.ay.pCx->nullRow = 1;
 66844         -  u.ay.pCx->pseudoTableReg = pOp->p2;
 66845         -  u.ay.pCx->isTable = 1;
 66846         -  u.ay.pCx->isIndex = 0;
        66835  +  u.az.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
        66836  +  if( u.az.pCx==0 ) goto no_mem;
        66837  +  u.az.pCx->nullRow = 1;
        66838  +  u.az.pCx->pseudoTableReg = pOp->p2;
        66839  +  u.az.pCx->isTable = 1;
        66840  +  u.az.pCx->isIndex = 0;
 66847  66841     break;
 66848  66842   }
 66849  66843   
 66850  66844   /* Opcode: Close P1 * * * *
 66851  66845   **
 66852  66846   ** Close a cursor previously opened as P1.  If P1 is not
 66853  66847   ** currently open, this instruction is a no-op.
................................................................................
 66911  66905   **
 66912  66906   ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
 66913  66907   */
 66914  66908   case OP_SeekLt:         /* jump, in3 */
 66915  66909   case OP_SeekLe:         /* jump, in3 */
 66916  66910   case OP_SeekGe:         /* jump, in3 */
 66917  66911   case OP_SeekGt: {       /* jump, in3 */
 66918         -#if 0  /* local variables moved into u.az */
        66912  +#if 0  /* local variables moved into u.ba */
 66919  66913     int res;
 66920  66914     int oc;
 66921  66915     VdbeCursor *pC;
 66922  66916     UnpackedRecord r;
 66923  66917     int nField;
 66924  66918     i64 iKey;      /* The rowid we are to seek to */
 66925         -#endif /* local variables moved into u.az */
        66919  +#endif /* local variables moved into u.ba */
 66926  66920   
 66927  66921     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 66928  66922     assert( pOp->p2!=0 );
 66929         -  u.az.pC = p->apCsr[pOp->p1];
 66930         -  assert( u.az.pC!=0 );
 66931         -  assert( u.az.pC->pseudoTableReg==0 );
        66923  +  u.ba.pC = p->apCsr[pOp->p1];
        66924  +  assert( u.ba.pC!=0 );
        66925  +  assert( u.ba.pC->pseudoTableReg==0 );
 66932  66926     assert( OP_SeekLe == OP_SeekLt+1 );
 66933  66927     assert( OP_SeekGe == OP_SeekLt+2 );
 66934  66928     assert( OP_SeekGt == OP_SeekLt+3 );
 66935         -  assert( u.az.pC->isOrdered );
 66936         -  if( ALWAYS(u.az.pC->pCursor!=0) ){
 66937         -    u.az.oc = pOp->opcode;
 66938         -    u.az.pC->nullRow = 0;
 66939         -    if( u.az.pC->isTable ){
        66929  +  assert( u.ba.pC->isOrdered );
        66930  +  if( ALWAYS(u.ba.pC->pCursor!=0) ){
        66931  +    u.ba.oc = pOp->opcode;
        66932  +    u.ba.pC->nullRow = 0;
        66933  +    if( u.ba.pC->isTable ){
 66940  66934         /* The input value in P3 might be of any type: integer, real, string,
 66941  66935         ** blob, or NULL.  But it needs to be an integer before we can do
 66942  66936         ** the seek, so covert it. */
 66943  66937         pIn3 = &aMem[pOp->p3];
 66944  66938         applyNumericAffinity(pIn3);
 66945         -      u.az.iKey = sqlite3VdbeIntValue(pIn3);
 66946         -      u.az.pC->rowidIsValid = 0;
        66939  +      u.ba.iKey = sqlite3VdbeIntValue(pIn3);
        66940  +      u.ba.pC->rowidIsValid = 0;
 66947  66941   
 66948  66942         /* If the P3 value could not be converted into an integer without
 66949  66943         ** loss of information, then special processing is required... */
 66950  66944         if( (pIn3->flags & MEM_Int)==0 ){
 66951  66945           if( (pIn3->flags & MEM_Real)==0 ){
 66952  66946             /* If the P3 value cannot be converted into any kind of a number,
 66953  66947             ** then the seek is not possible, so jump to P2 */
................................................................................
 66954  66948             pc = pOp->p2 - 1;
 66955  66949             break;
 66956  66950           }
 66957  66951           /* If we reach this point, then the P3 value must be a floating
 66958  66952           ** point number. */
 66959  66953           assert( (pIn3->flags & MEM_Real)!=0 );
 66960  66954   
 66961         -        if( u.az.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.az.iKey || pIn3->r>0) ){
        66955  +        if( u.ba.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.ba.iKey || pIn3->r>0) ){
 66962  66956             /* The P3 value is too large in magnitude to be expressed as an
 66963  66957             ** integer. */
 66964         -          u.az.res = 1;
        66958  +          u.ba.res = 1;
 66965  66959             if( pIn3->r<0 ){
 66966         -            if( u.az.oc>=OP_SeekGe ){  assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt );
 66967         -              rc = sqlite3BtreeFirst(u.az.pC->pCursor, &u.az.res);
        66960  +            if( u.ba.oc>=OP_SeekGe ){  assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
        66961  +              rc = sqlite3BtreeFirst(u.ba.pC->pCursor, &u.ba.res);
 66968  66962                 if( rc!=SQLITE_OK ) goto abort_due_to_error;
 66969  66963               }
 66970  66964             }else{
 66971         -            if( u.az.oc<=OP_SeekLe ){  assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
 66972         -              rc = sqlite3BtreeLast(u.az.pC->pCursor, &u.az.res);
        66965  +            if( u.ba.oc<=OP_SeekLe ){  assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
        66966  +              rc = sqlite3BtreeLast(u.ba.pC->pCursor, &u.ba.res);
 66973  66967                 if( rc!=SQLITE_OK ) goto abort_due_to_error;
 66974  66968               }
 66975  66969             }
 66976         -          if( u.az.res ){
        66970  +          if( u.ba.res ){
 66977  66971               pc = pOp->p2 - 1;
 66978  66972             }
 66979  66973             break;
 66980         -        }else if( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekGe ){
        66974  +        }else if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekGe ){
 66981  66975             /* Use the ceiling() function to convert real->int */
 66982         -          if( pIn3->r > (double)u.az.iKey ) u.az.iKey++;
        66976  +          if( pIn3->r > (double)u.ba.iKey ) u.ba.iKey++;
 66983  66977           }else{
 66984  66978             /* Use the floor() function to convert real->int */
 66985         -          assert( u.az.oc==OP_SeekLe || u.az.oc==OP_SeekGt );
 66986         -          if( pIn3->r < (double)u.az.iKey ) u.az.iKey--;
        66979  +          assert( u.ba.oc==OP_SeekLe || u.ba.oc==OP_SeekGt );
        66980  +          if( pIn3->r < (double)u.ba.iKey ) u.ba.iKey--;
 66987  66981           }
 66988  66982         }
 66989         -      rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, 0, (u64)u.az.iKey, 0, &u.az.res);
        66983  +      rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, 0, (u64)u.ba.iKey, 0, &u.ba.res);
 66990  66984         if( rc!=SQLITE_OK ){
 66991  66985           goto abort_due_to_error;
 66992  66986         }
 66993         -      if( u.az.res==0 ){
 66994         -        u.az.pC->rowidIsValid = 1;
 66995         -        u.az.pC->lastRowid = u.az.iKey;
        66987  +      if( u.ba.res==0 ){
        66988  +        u.ba.pC->rowidIsValid = 1;
        66989  +        u.ba.pC->lastRowid = u.ba.iKey;
 66996  66990         }
 66997  66991       }else{
 66998         -      u.az.nField = pOp->p4.i;
        66992  +      u.ba.nField = pOp->p4.i;
 66999  66993         assert( pOp->p4type==P4_INT32 );
 67000         -      assert( u.az.nField>0 );
 67001         -      u.az.r.pKeyInfo = u.az.pC->pKeyInfo;
 67002         -      u.az.r.nField = (u16)u.az.nField;
        66994  +      assert( u.ba.nField>0 );
        66995  +      u.ba.r.pKeyInfo = u.ba.pC->pKeyInfo;
        66996  +      u.ba.r.nField = (u16)u.ba.nField;
 67003  66997   
 67004  66998         /* The next line of code computes as follows, only faster:
 67005         -      **   if( u.az.oc==OP_SeekGt || u.az.oc==OP_SeekLe ){
 67006         -      **     u.az.r.flags = UNPACKED_INCRKEY;
        66999  +      **   if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekLe ){
        67000  +      **     u.ba.r.flags = UNPACKED_INCRKEY;
 67007  67001         **   }else{
 67008         -      **     u.az.r.flags = 0;
        67002  +      **     u.ba.r.flags = 0;
 67009  67003         **   }
 67010  67004         */
 67011         -      u.az.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.az.oc - OP_SeekLt)));
 67012         -      assert( u.az.oc!=OP_SeekGt || u.az.r.flags==UNPACKED_INCRKEY );
 67013         -      assert( u.az.oc!=OP_SeekLe || u.az.r.flags==UNPACKED_INCRKEY );
 67014         -      assert( u.az.oc!=OP_SeekGe || u.az.r.flags==0 );
 67015         -      assert( u.az.oc!=OP_SeekLt || u.az.r.flags==0 );
        67005  +      u.ba.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.ba.oc - OP_SeekLt)));
        67006  +      assert( u.ba.oc!=OP_SeekGt || u.ba.r.flags==UNPACKED_INCRKEY );
        67007  +      assert( u.ba.oc!=OP_SeekLe || u.ba.r.flags==UNPACKED_INCRKEY );
        67008  +      assert( u.ba.oc!=OP_SeekGe || u.ba.r.flags==0 );
        67009  +      assert( u.ba.oc!=OP_SeekLt || u.ba.r.flags==0 );
 67016  67010   
 67017         -      u.az.r.aMem = &aMem[pOp->p3];
        67011  +      u.ba.r.aMem = &aMem[pOp->p3];
 67018  67012   #ifdef SQLITE_DEBUG
 67019         -      { int i; for(i=0; i<u.az.r.nField; i++) assert( memIsValid(&u.az.r.aMem[i]) ); }
        67013  +      { int i; for(i=0; i<u.ba.r.nField; i++) assert( memIsValid(&u.ba.r.aMem[i]) ); }
 67020  67014   #endif
 67021         -      ExpandBlob(u.az.r.aMem);
 67022         -      rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, &u.az.r, 0, 0, &u.az.res);
        67015  +      ExpandBlob(u.ba.r.aMem);
        67016  +      rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, &u.ba.r, 0, 0, &u.ba.res);
 67023  67017         if( rc!=SQLITE_OK ){
 67024  67018           goto abort_due_to_error;
 67025  67019         }
 67026         -      u.az.pC->rowidIsValid = 0;
        67020  +      u.ba.pC->rowidIsValid = 0;
 67027  67021       }
 67028         -    u.az.pC->deferredMoveto = 0;
 67029         -    u.az.pC->cacheStatus = CACHE_STALE;
        67022  +    u.ba.pC->deferredMoveto = 0;
        67023  +    u.ba.pC->cacheStatus = CACHE_STALE;
 67030  67024   #ifdef SQLITE_TEST
 67031  67025       sqlite3_search_count++;
 67032  67026   #endif
 67033         -    if( u.az.oc>=OP_SeekGe ){  assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt );
 67034         -      if( u.az.res<0 || (u.az.res==0 && u.az.oc==OP_SeekGt) ){
 67035         -        rc = sqlite3BtreeNext(u.az.pC->pCursor, &u.az.res);
        67027  +    if( u.ba.oc>=OP_SeekGe ){  assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
        67028  +      if( u.ba.res<0 || (u.ba.res==0 && u.ba.oc==OP_SeekGt) ){
        67029  +        rc = sqlite3BtreeNext(u.ba.pC->pCursor, &u.ba.res);
 67036  67030           if( rc!=SQLITE_OK ) goto abort_due_to_error;
 67037         -        u.az.pC->rowidIsValid = 0;
        67031  +        u.ba.pC->rowidIsValid = 0;
 67038  67032         }else{
 67039         -        u.az.res = 0;
        67033  +        u.ba.res = 0;
 67040  67034         }
 67041  67035       }else{
 67042         -      assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
 67043         -      if( u.az.res>0 || (u.az.res==0 && u.az.oc==OP_SeekLt) ){
 67044         -        rc = sqlite3BtreePrevious(u.az.pC->pCursor, &u.az.res);
        67036  +      assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
        67037  +      if( u.ba.res>0 || (u.ba.res==0 && u.ba.oc==OP_SeekLt) ){
        67038  +        rc = sqlite3BtreePrevious(u.ba.pC->pCursor, &u.ba.res);
 67045  67039           if( rc!=SQLITE_OK ) goto abort_due_to_error;
 67046         -        u.az.pC->rowidIsValid = 0;
        67040  +        u.ba.pC->rowidIsValid = 0;
 67047  67041         }else{
 67048         -        /* u.az.res might be negative because the table is empty.  Check to
        67042  +        /* u.ba.res might be negative because the table is empty.  Check to
 67049  67043           ** see if this is the case.
 67050  67044           */
 67051         -        u.az.res = sqlite3BtreeEof(u.az.pC->pCursor);
        67045  +        u.ba.res = sqlite3BtreeEof(u.ba.pC->pCursor);
 67052  67046         }
 67053  67047       }
 67054  67048       assert( pOp->p2>0 );
 67055         -    if( u.az.res ){
        67049  +    if( u.ba.res ){
 67056  67050         pc = pOp->p2 - 1;
 67057  67051       }
 67058  67052     }else{
 67059  67053       /* This happens when attempting to open the sqlite3_master table
 67060  67054       ** for read access returns SQLITE_EMPTY. In this case always
 67061  67055       ** take the jump (since there are no records in the table).
 67062  67056       */
................................................................................
 67071  67065   ** for P1 to move so that it points to the rowid given by P2.
 67072  67066   **
 67073  67067   ** This is actually a deferred seek.  Nothing actually happens until
 67074  67068   ** the cursor is used to read a record.  That way, if no reads
 67075  67069   ** occur, no unnecessary I/O happens.
 67076  67070   */
 67077  67071   case OP_Seek: {    /* in2 */
 67078         -#if 0  /* local variables moved into u.ba */
        67072  +#if 0  /* local variables moved into u.bb */
 67079  67073     VdbeCursor *pC;
 67080         -#endif /* local variables moved into u.ba */
        67074  +#endif /* local variables moved into u.bb */
 67081  67075   
 67082  67076     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67083         -  u.ba.pC = p->apCsr[pOp->p1];
 67084         -  assert( u.ba.pC!=0 );
 67085         -  if( ALWAYS(u.ba.pC->pCursor!=0) ){
 67086         -    assert( u.ba.pC->isTable );
 67087         -    u.ba.pC->nullRow = 0;
        67077  +  u.bb.pC = p->apCsr[pOp->p1];
        67078  +  assert( u.bb.pC!=0 );
        67079  +  if( ALWAYS(u.bb.pC->pCursor!=0) ){
        67080  +    assert( u.bb.pC->isTable );
        67081  +    u.bb.pC->nullRow = 0;
 67088  67082       pIn2 = &aMem[pOp->p2];
 67089         -    u.ba.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
 67090         -    u.ba.pC->rowidIsValid = 0;
 67091         -    u.ba.pC->deferredMoveto = 1;
        67083  +    u.bb.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
        67084  +    u.bb.pC->rowidIsValid = 0;
        67085  +    u.bb.pC->deferredMoveto = 1;
 67092  67086     }
 67093  67087     break;
 67094  67088   }
 67095  67089     
 67096  67090   
 67097  67091   /* Opcode: Found P1 P2 P3 P4 *
 67098  67092   **
................................................................................
 67116  67110   ** falls through to the next instruction and P1 is left pointing at the
 67117  67111   ** matching entry.
 67118  67112   **
 67119  67113   ** See also: Found, NotExists, IsUnique
 67120  67114   */
 67121  67115   case OP_NotFound:       /* jump, in3 */
 67122  67116   case OP_Found: {        /* jump, in3 */
 67123         -#if 0  /* local variables moved into u.bb */
        67117  +#if 0  /* local variables moved into u.bc */
 67124  67118     int alreadyExists;
 67125  67119     VdbeCursor *pC;
 67126  67120     int res;
        67121  +  char *pFree;
 67127  67122     UnpackedRecord *pIdxKey;
 67128  67123     UnpackedRecord r;
 67129  67124     char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
 67130         -#endif /* local variables moved into u.bb */
        67125  +#endif /* local variables moved into u.bc */
 67131  67126   
 67132  67127   #ifdef SQLITE_TEST
 67133  67128     sqlite3_found_count++;
 67134  67129   #endif
 67135  67130   
 67136         -  u.bb.alreadyExists = 0;
        67131  +  u.bc.alreadyExists = 0;
 67137  67132     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67138  67133     assert( pOp->p4type==P4_INT32 );
 67139         -  u.bb.pC = p->apCsr[pOp->p1];
 67140         -  assert( u.bb.pC!=0 );
        67134  +  u.bc.pC = p->apCsr[pOp->p1];
        67135  +  assert( u.bc.pC!=0 );
 67141  67136     pIn3 = &aMem[pOp->p3];
 67142         -  if( ALWAYS(u.bb.pC->pCursor!=0) ){
        67137  +  if( ALWAYS(u.bc.pC->pCursor!=0) ){
 67143  67138   
 67144         -    assert( u.bb.pC->isTable==0 );
        67139  +    assert( u.bc.pC->isTable==0 );
 67145  67140       if( pOp->p4.i>0 ){
 67146         -      u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
 67147         -      u.bb.r.nField = (u16)pOp->p4.i;
 67148         -      u.bb.r.aMem = pIn3;
        67141  +      u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
        67142  +      u.bc.r.nField = (u16)pOp->p4.i;
        67143  +      u.bc.r.aMem = pIn3;
 67149  67144   #ifdef SQLITE_DEBUG
 67150         -      { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
        67145  +      { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
 67151  67146   #endif
 67152         -      u.bb.r.flags = UNPACKED_PREFIX_MATCH;
 67153         -      u.bb.pIdxKey = &u.bb.r;
        67147  +      u.bc.r.flags = UNPACKED_PREFIX_MATCH;
        67148  +      u.bc.pIdxKey = &u.bc.r;
 67154  67149       }else{
        67150  +      u.bc.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
        67151  +          u.bc.pC->pKeyInfo, u.bc.aTempRec, sizeof(u.bc.aTempRec), &u.bc.pFree
        67152  +      );
        67153  +      if( u.bc.pIdxKey==0 ) goto no_mem;
 67155  67154         assert( pIn3->flags & MEM_Blob );
 67156  67155         assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
 67157         -      u.bb.pIdxKey = sqlite3VdbeRecordUnpack(u.bb.pC->pKeyInfo, pIn3->n, pIn3->z,
 67158         -                                        u.bb.aTempRec, sizeof(u.bb.aTempRec));
 67159         -      if( u.bb.pIdxKey==0 ){
 67160         -        goto no_mem;
 67161         -      }
 67162         -      u.bb.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
        67156  +      sqlite3VdbeRecordUnpack(u.bc.pC->pKeyInfo, pIn3->n, pIn3->z, u.bc.pIdxKey);
        67157  +      u.bc.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
 67163  67158       }
 67164         -    rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, u.bb.pIdxKey, 0, 0, &u.bb.res);
        67159  +    rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, u.bc.pIdxKey, 0, 0, &u.bc.res);
 67165  67160       if( pOp->p4.i==0 ){
 67166         -      sqlite3VdbeDeleteUnpackedRecord(u.bb.pIdxKey);
        67161  +      sqlite3DbFree(db, u.bc.pFree);
 67167  67162       }
 67168  67163       if( rc!=SQLITE_OK ){
 67169  67164         break;
 67170  67165       }
 67171         -    u.bb.alreadyExists = (u.bb.res==0);
 67172         -    u.bb.pC->deferredMoveto = 0;
 67173         -    u.bb.pC->cacheStatus = CACHE_STALE;
        67166  +    u.bc.alreadyExists = (u.bc.res==0);
        67167  +    u.bc.pC->deferredMoveto = 0;
        67168  +    u.bc.pC->cacheStatus = CACHE_STALE;
 67174  67169     }
 67175  67170     if( pOp->opcode==OP_Found ){
 67176         -    if( u.bb.alreadyExists ) pc = pOp->p2 - 1;
        67171  +    if( u.bc.alreadyExists ) pc = pOp->p2 - 1;
 67177  67172     }else{
 67178         -    if( !u.bb.alreadyExists ) pc = pOp->p2 - 1;
        67173  +    if( !u.bc.alreadyExists ) pc = pOp->p2 - 1;
 67179  67174     }
 67180  67175     break;
 67181  67176   }
 67182  67177   
 67183  67178   /* Opcode: IsUnique P1 P2 P3 P4 *
 67184  67179   **
 67185  67180   ** Cursor P1 is open on an index b-tree - that is to say, a btree which
................................................................................
 67203  67198   ** to instruction P2. Otherwise, the rowid of the conflicting index
 67204  67199   ** entry is copied to register P3 and control falls through to the next
 67205  67200   ** instruction.
 67206  67201   **
 67207  67202   ** See also: NotFound, NotExists, Found
 67208  67203   */
 67209  67204   case OP_IsUnique: {        /* jump, in3 */
 67210         -#if 0  /* local variables moved into u.bc */
        67205  +#if 0  /* local variables moved into u.bd */
 67211  67206     u16 ii;
 67212  67207     VdbeCursor *pCx;
 67213  67208     BtCursor *pCrsr;
 67214  67209     u16 nField;
 67215  67210     Mem *aMx;
 67216  67211     UnpackedRecord r;                  /* B-Tree index search key */
 67217  67212     i64 R;                             /* Rowid stored in register P3 */
 67218         -#endif /* local variables moved into u.bc */
        67213  +#endif /* local variables moved into u.bd */
 67219  67214   
 67220  67215     pIn3 = &aMem[pOp->p3];
 67221         -  u.bc.aMx = &aMem[pOp->p4.i];
        67216  +  u.bd.aMx = &aMem[pOp->p4.i];
 67222  67217     /* Assert that the values of parameters P1 and P4 are in range. */
 67223  67218     assert( pOp->p4type==P4_INT32 );
 67224  67219     assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
 67225  67220     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67226  67221   
 67227  67222     /* Find the index cursor. */
 67228         -  u.bc.pCx = p->apCsr[pOp->p1];
 67229         -  assert( u.bc.pCx->deferredMoveto==0 );
 67230         -  u.bc.pCx->seekResult = 0;
 67231         -  u.bc.pCx->cacheStatus = CACHE_STALE;
 67232         -  u.bc.pCrsr = u.bc.pCx->pCursor;
        67223  +  u.bd.pCx = p->apCsr[pOp->p1];
        67224  +  assert( u.bd.pCx->deferredMoveto==0 );
        67225  +  u.bd.pCx->seekResult = 0;
        67226  +  u.bd.pCx->cacheStatus = CACHE_STALE;
        67227  +  u.bd.pCrsr = u.bd.pCx->pCursor;
 67233  67228   
 67234  67229     /* If any of the values are NULL, take the jump. */
 67235         -  u.bc.nField = u.bc.pCx->pKeyInfo->nField;
 67236         -  for(u.bc.ii=0; u.bc.ii<u.bc.nField; u.bc.ii++){
 67237         -    if( u.bc.aMx[u.bc.ii].flags & MEM_Null ){
        67230  +  u.bd.nField = u.bd.pCx->pKeyInfo->nField;
        67231  +  for(u.bd.ii=0; u.bd.ii<u.bd.nField; u.bd.ii++){
        67232  +    if( u.bd.aMx[u.bd.ii].flags & MEM_Null ){
 67238  67233         pc = pOp->p2 - 1;
 67239         -      u.bc.pCrsr = 0;
        67234  +      u.bd.pCrsr = 0;
 67240  67235         break;
 67241  67236       }
 67242  67237     }
 67243         -  assert( (u.bc.aMx[u.bc.nField].flags & MEM_Null)==0 );
        67238  +  assert( (u.bd.aMx[u.bd.nField].flags & MEM_Null)==0 );
 67244  67239   
 67245         -  if( u.bc.pCrsr!=0 ){
        67240  +  if( u.bd.pCrsr!=0 ){
 67246  67241       /* Populate the index search key. */
 67247         -    u.bc.r.pKeyInfo = u.bc.pCx->pKeyInfo;
 67248         -    u.bc.r.nField = u.bc.nField + 1;
 67249         -    u.bc.r.flags = UNPACKED_PREFIX_SEARCH;
 67250         -    u.bc.r.aMem = u.bc.aMx;
        67242  +    u.bd.r.pKeyInfo = u.bd.pCx->pKeyInfo;
        67243  +    u.bd.r.nField = u.bd.nField + 1;
        67244  +    u.bd.r.flags = UNPACKED_PREFIX_SEARCH;
        67245  +    u.bd.r.aMem = u.bd.aMx;
 67251  67246   #ifdef SQLITE_DEBUG
 67252         -    { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
        67247  +    { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
 67253  67248   #endif
 67254  67249   
 67255         -    /* Extract the value of u.bc.R from register P3. */
        67250  +    /* Extract the value of u.bd.R from register P3. */
 67256  67251       sqlite3VdbeMemIntegerify(pIn3);
 67257         -    u.bc.R = pIn3->u.i;
        67252  +    u.bd.R = pIn3->u.i;
 67258  67253   
 67259  67254       /* Search the B-Tree index. If no conflicting record is found, jump
 67260  67255       ** to P2. Otherwise, copy the rowid of the conflicting record to
 67261  67256       ** register P3 and fall through to the next instruction.  */
 67262         -    rc = sqlite3BtreeMovetoUnpacked(u.bc.pCrsr, &u.bc.r, 0, 0, &u.bc.pCx->seekResult);
 67263         -    if( (u.bc.r.flags & UNPACKED_PREFIX_SEARCH) || u.bc.r.rowid==u.bc.R ){
        67257  +    rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, &u.bd.r, 0, 0, &u.bd.pCx->seekResult);
        67258  +    if( (u.bd.r.flags & UNPACKED_PREFIX_SEARCH) || u.bd.r.rowid==u.bd.R ){
 67264  67259         pc = pOp->p2 - 1;
 67265  67260       }else{
 67266         -      pIn3->u.i = u.bc.r.rowid;
        67261  +      pIn3->u.i = u.bd.r.rowid;
 67267  67262       }
 67268  67263     }
 67269  67264     break;
 67270  67265   }
 67271  67266   
 67272  67267   /* Opcode: NotExists P1 P2 P3 * *
 67273  67268   **
................................................................................
 67280  67275   ** operation assumes the key is an integer and that P1 is a table whereas
 67281  67276   ** NotFound assumes key is a blob constructed from MakeRecord and
 67282  67277   ** P1 is an index.
 67283  67278   **
 67284  67279   ** See also: Found, NotFound, IsUnique
 67285  67280   */
 67286  67281   case OP_NotExists: {        /* jump, in3 */
 67287         -#if 0  /* local variables moved into u.bd */
        67282  +#if 0  /* local variables moved into u.be */
 67288  67283     VdbeCursor *pC;
 67289  67284     BtCursor *pCrsr;
 67290  67285     int res;
 67291  67286     u64 iKey;
 67292         -#endif /* local variables moved into u.bd */
        67287  +#endif /* local variables moved into u.be */
 67293  67288   
 67294  67289     pIn3 = &aMem[pOp->p3];
 67295  67290     assert( pIn3->flags & MEM_Int );
 67296  67291     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67297         -  u.bd.pC = p->apCsr[pOp->p1];
 67298         -  assert( u.bd.pC!=0 );
 67299         -  assert( u.bd.pC->isTable );
 67300         -  assert( u.bd.pC->pseudoTableReg==0 );
 67301         -  u.bd.pCrsr = u.bd.pC->pCursor;
 67302         -  if( ALWAYS(u.bd.pCrsr!=0) ){
 67303         -    u.bd.res = 0;
 67304         -    u.bd.iKey = pIn3->u.i;
 67305         -    rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, 0, u.bd.iKey, 0, &u.bd.res);
 67306         -    u.bd.pC->lastRowid = pIn3->u.i;
 67307         -    u.bd.pC->rowidIsValid = u.bd.res==0 ?1:0;
 67308         -    u.bd.pC->nullRow = 0;
 67309         -    u.bd.pC->cacheStatus = CACHE_STALE;
 67310         -    u.bd.pC->deferredMoveto = 0;
 67311         -    if( u.bd.res!=0 ){
        67292  +  u.be.pC = p->apCsr[pOp->p1];
        67293  +  assert( u.be.pC!=0 );
        67294  +  assert( u.be.pC->isTable );
        67295  +  assert( u.be.pC->pseudoTableReg==0 );
        67296  +  u.be.pCrsr = u.be.pC->pCursor;
        67297  +  if( ALWAYS(u.be.pCrsr!=0) ){
        67298  +    u.be.res = 0;
        67299  +    u.be.iKey = pIn3->u.i;
        67300  +    rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, 0, u.be.iKey, 0, &u.be.res);
        67301  +    u.be.pC->lastRowid = pIn3->u.i;
        67302  +    u.be.pC->rowidIsValid = u.be.res==0 ?1:0;
        67303  +    u.be.pC->nullRow = 0;
        67304  +    u.be.pC->cacheStatus = CACHE_STALE;
        67305  +    u.be.pC->deferredMoveto = 0;
        67306  +    if( u.be.res!=0 ){
 67312  67307         pc = pOp->p2 - 1;
 67313         -      assert( u.bd.pC->rowidIsValid==0 );
        67308  +      assert( u.be.pC->rowidIsValid==0 );
 67314  67309       }
 67315         -    u.bd.pC->seekResult = u.bd.res;
        67310  +    u.be.pC->seekResult = u.be.res;
 67316  67311     }else{
 67317  67312       /* This happens when an attempt to open a read cursor on the
 67318  67313       ** sqlite_master table returns SQLITE_EMPTY.
 67319  67314       */
 67320  67315       pc = pOp->p2 - 1;
 67321         -    assert( u.bd.pC->rowidIsValid==0 );
 67322         -    u.bd.pC->seekResult = 0;
        67316  +    assert( u.be.pC->rowidIsValid==0 );
        67317  +    u.be.pC->seekResult = 0;
 67323  67318     }
 67324  67319     break;
 67325  67320   }
 67326  67321   
 67327  67322   /* Opcode: Sequence P1 P2 * * *
 67328  67323   **
 67329  67324   ** Find the next available sequence number for cursor P1.
................................................................................
 67350  67345   ** the largest previously generated record number. No new record numbers are
 67351  67346   ** allowed to be less than this value. When this value reaches its maximum, 
 67352  67347   ** an SQLITE_FULL error is generated. The P3 register is updated with the '
 67353  67348   ** generated record number. This P3 mechanism is used to help implement the
 67354  67349   ** AUTOINCREMENT feature.
 67355  67350   */
 67356  67351   case OP_NewRowid: {           /* out2-prerelease */
 67357         -#if 0  /* local variables moved into u.be */
        67352  +#if 0  /* local variables moved into u.bf */
 67358  67353     i64 v;                 /* The new rowid */
 67359  67354     VdbeCursor *pC;        /* Cursor of table to get the new rowid */
 67360  67355     int res;               /* Result of an sqlite3BtreeLast() */
 67361  67356     int cnt;               /* Counter to limit the number of searches */
 67362  67357     Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
 67363  67358     VdbeFrame *pFrame;     /* Root frame of VDBE */
 67364         -#endif /* local variables moved into u.be */
        67359  +#endif /* local variables moved into u.bf */
 67365  67360   
 67366         -  u.be.v = 0;
 67367         -  u.be.res = 0;
        67361  +  u.bf.v = 0;
        67362  +  u.bf.res = 0;
 67368  67363     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67369         -  u.be.pC = p->apCsr[pOp->p1];
 67370         -  assert( u.be.pC!=0 );
 67371         -  if( NEVER(u.be.pC->pCursor==0) ){
        67364  +  u.bf.pC = p->apCsr[pOp->p1];
        67365  +  assert( u.bf.pC!=0 );
        67366  +  if( NEVER(u.bf.pC->pCursor==0) ){
 67372  67367       /* The zero initialization above is all that is needed */
 67373  67368     }else{
 67374  67369       /* The next rowid or record number (different terms for the same
 67375  67370       ** thing) is obtained in a two-step algorithm.
 67376  67371       **
 67377  67372       ** First we attempt to find the largest existing rowid and add one
 67378  67373       ** to that.  But if the largest existing rowid is already the maximum
................................................................................
 67380  67375       ** probabilistic algorithm
 67381  67376       **
 67382  67377       ** The second algorithm is to select a rowid at random and see if
 67383  67378       ** it already exists in the table.  If it does not exist, we have
 67384  67379       ** succeeded.  If the random rowid does exist, we select a new one
 67385  67380       ** and try again, up to 100 times.
 67386  67381       */
 67387         -    assert( u.be.pC->isTable );
        67382  +    assert( u.bf.pC->isTable );
 67388  67383   
 67389  67384   #ifdef SQLITE_32BIT_ROWID
 67390  67385   #   define MAX_ROWID 0x7fffffff
 67391  67386   #else
 67392  67387       /* Some compilers complain about constants of the form 0x7fffffffffffffff.
 67393  67388       ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
 67394  67389       ** to provide the constant while making all compilers happy.
 67395  67390       */
 67396  67391   #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
 67397  67392   #endif
 67398  67393   
 67399         -    if( !u.be.pC->useRandomRowid ){
 67400         -      u.be.v = sqlite3BtreeGetCachedRowid(u.be.pC->pCursor);
 67401         -      if( u.be.v==0 ){
 67402         -        rc = sqlite3BtreeLast(u.be.pC->pCursor, &u.be.res);
        67394  +    if( !u.bf.pC->useRandomRowid ){
        67395  +      u.bf.v = sqlite3BtreeGetCachedRowid(u.bf.pC->pCursor);
        67396  +      if( u.bf.v==0 ){
        67397  +        rc = sqlite3BtreeLast(u.bf.pC->pCursor, &u.bf.res);
 67403  67398           if( rc!=SQLITE_OK ){
 67404  67399             goto abort_due_to_error;
 67405  67400           }
 67406         -        if( u.be.res ){
 67407         -          u.be.v = 1;   /* IMP: R-61914-48074 */
        67401  +        if( u.bf.res ){
        67402  +          u.bf.v = 1;   /* IMP: R-61914-48074 */
 67408  67403           }else{
 67409         -          assert( sqlite3BtreeCursorIsValid(u.be.pC->pCursor) );
 67410         -          rc = sqlite3BtreeKeySize(u.be.pC->pCursor, &u.be.v);
        67404  +          assert( sqlite3BtreeCursorIsValid(u.bf.pC->pCursor) );
        67405  +          rc = sqlite3BtreeKeySize(u.bf.pC->pCursor, &u.bf.v);
 67411  67406             assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
 67412         -          if( u.be.v==MAX_ROWID ){
 67413         -            u.be.pC->useRandomRowid = 1;
        67407  +          if( u.bf.v==MAX_ROWID ){
        67408  +            u.bf.pC->useRandomRowid = 1;
 67414  67409             }else{
 67415         -            u.be.v++;   /* IMP: R-29538-34987 */
        67410  +            u.bf.v++;   /* IMP: R-29538-34987 */
 67416  67411             }
 67417  67412           }
 67418  67413         }
 67419  67414   
 67420  67415   #ifndef SQLITE_OMIT_AUTOINCREMENT
 67421  67416         if( pOp->p3 ){
 67422  67417           /* Assert that P3 is a valid memory cell. */
 67423  67418           assert( pOp->p3>0 );
 67424  67419           if( p->pFrame ){
 67425         -          for(u.be.pFrame=p->pFrame; u.be.pFrame->pParent; u.be.pFrame=u.be.pFrame->pParent);
        67420  +          for(u.bf.pFrame=p->pFrame; u.bf.pFrame->pParent; u.bf.pFrame=u.bf.pFrame->pParent);
 67426  67421             /* Assert that P3 is a valid memory cell. */
 67427         -          assert( pOp->p3<=u.be.pFrame->nMem );
 67428         -          u.be.pMem = &u.be.pFrame->aMem[pOp->p3];
        67422  +          assert( pOp->p3<=u.bf.pFrame->nMem );
        67423  +          u.bf.pMem = &u.bf.pFrame->aMem[pOp->p3];
 67429  67424           }else{
 67430  67425             /* Assert that P3 is a valid memory cell. */
 67431  67426             assert( pOp->p3<=p->nMem );
 67432         -          u.be.pMem = &aMem[pOp->p3];
 67433         -          memAboutToChange(p, u.be.pMem);
        67427  +          u.bf.pMem = &aMem[pOp->p3];
        67428  +          memAboutToChange(p, u.bf.pMem);
 67434  67429           }
 67435         -        assert( memIsValid(u.be.pMem) );
        67430  +        assert( memIsValid(u.bf.pMem) );
 67436  67431   
 67437         -        REGISTER_TRACE(pOp->p3, u.be.pMem);
 67438         -        sqlite3VdbeMemIntegerify(u.be.pMem);
 67439         -        assert( (u.be.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
 67440         -        if( u.be.pMem->u.i==MAX_ROWID || u.be.pC->useRandomRowid ){
        67432  +        REGISTER_TRACE(pOp->p3, u.bf.pMem);
        67433  +        sqlite3VdbeMemIntegerify(u.bf.pMem);
        67434  +        assert( (u.bf.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
        67435  +        if( u.bf.pMem->u.i==MAX_ROWID || u.bf.pC->useRandomRowid ){
 67441  67436             rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
 67442  67437             goto abort_due_to_error;
 67443  67438           }
 67444         -        if( u.be.v<u.be.pMem->u.i+1 ){
 67445         -          u.be.v = u.be.pMem->u.i + 1;
        67439  +        if( u.bf.v<u.bf.pMem->u.i+1 ){
        67440  +          u.bf.v = u.bf.pMem->u.i + 1;
 67446  67441           }
 67447         -        u.be.pMem->u.i = u.be.v;
        67442  +        u.bf.pMem->u.i = u.bf.v;
 67448  67443         }
 67449  67444   #endif
 67450  67445   
 67451         -      sqlite3BtreeSetCachedRowid(u.be.pC->pCursor, u.be.v<MAX_ROWID ? u.be.v+1 : 0);
        67446  +      sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, u.bf.v<MAX_ROWID ? u.bf.v+1 : 0);
 67452  67447       }
 67453         -    if( u.be.pC->useRandomRowid ){
        67448  +    if( u.bf.pC->useRandomRowid ){
 67454  67449         /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
 67455  67450         ** largest possible integer (9223372036854775807) then the database
 67456  67451         ** engine starts picking positive candidate ROWIDs at random until
 67457  67452         ** it finds one that is not previously used. */
 67458  67453         assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
 67459  67454                                ** an AUTOINCREMENT table. */
 67460  67455         /* on the first attempt, simply do one more than previous */
 67461         -      u.be.v = lastRowid;
 67462         -      u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
 67463         -      u.be.v++; /* ensure non-zero */
 67464         -      u.be.cnt = 0;
 67465         -      while(   ((rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v,
 67466         -                                                 0, &u.be.res))==SQLITE_OK)
 67467         -            && (u.be.res==0)
 67468         -            && (++u.be.cnt<100)){
        67456  +      u.bf.v = lastRowid;
        67457  +      u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
        67458  +      u.bf.v++; /* ensure non-zero */
        67459  +      u.bf.cnt = 0;
        67460  +      while(   ((rc = sqlite3BtreeMovetoUnpacked(u.bf.pC->pCursor, 0, (u64)u.bf.v,
        67461  +                                                 0, &u.bf.res))==SQLITE_OK)
        67462  +            && (u.bf.res==0)
        67463  +            && (++u.bf.cnt<100)){
 67469  67464           /* collision - try another random rowid */
 67470         -        sqlite3_randomness(sizeof(u.be.v), &u.be.v);
 67471         -        if( u.be.cnt<5 ){
        67465  +        sqlite3_randomness(sizeof(u.bf.v), &u.bf.v);
        67466  +        if( u.bf.cnt<5 ){
 67472  67467             /* try "small" random rowids for the initial attempts */
 67473         -          u.be.v &= 0xffffff;
        67468  +          u.bf.v &= 0xffffff;
 67474  67469           }else{
 67475         -          u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
        67470  +          u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
 67476  67471           }
 67477         -        u.be.v++; /* ensure non-zero */
        67472  +        u.bf.v++; /* ensure non-zero */
 67478  67473         }
 67479         -      if( rc==SQLITE_OK && u.be.res==0 ){
        67474  +      if( rc==SQLITE_OK && u.bf.res==0 ){
 67480  67475           rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
 67481  67476           goto abort_due_to_error;
 67482  67477         }
 67483         -      assert( u.be.v>0 );  /* EV: R-40812-03570 */
        67478  +      assert( u.bf.v>0 );  /* EV: R-40812-03570 */
 67484  67479       }
 67485         -    u.be.pC->rowidIsValid = 0;
 67486         -    u.be.pC->deferredMoveto = 0;
 67487         -    u.be.pC->cacheStatus = CACHE_STALE;
        67480  +    u.bf.pC->rowidIsValid = 0;
        67481  +    u.bf.pC->deferredMoveto = 0;
        67482  +    u.bf.pC->cacheStatus = CACHE_STALE;
 67488  67483     }
 67489         -  pOut->u.i = u.be.v;
        67484  +  pOut->u.i = u.bf.v;
 67490  67485     break;
 67491  67486   }
 67492  67487   
 67493  67488   /* Opcode: Insert P1 P2 P3 P4 P5
 67494  67489   **
 67495  67490   ** Write an entry into the table of cursor P1.  A new entry is
 67496  67491   ** created if it doesn't already exist or the data for an existing
................................................................................
 67532  67527   /* Opcode: InsertInt P1 P2 P3 P4 P5
 67533  67528   **
 67534  67529   ** This works exactly like OP_Insert except that the key is the
 67535  67530   ** integer value P3, not the value of the integer stored in register P3.
 67536  67531   */
 67537  67532   case OP_Insert: 
 67538  67533   case OP_InsertInt: {
 67539         -#if 0  /* local variables moved into u.bf */
        67534  +#if 0  /* local variables moved into u.bg */
 67540  67535     Mem *pData;       /* MEM cell holding data for the record to be inserted */
 67541  67536     Mem *pKey;        /* MEM cell holding key  for the record */
 67542  67537     i64 iKey;         /* The integer ROWID or key for the record to be inserted */
 67543  67538     VdbeCursor *pC;   /* Cursor to table into which insert is written */
 67544  67539     int nZero;        /* Number of zero-bytes to append */
 67545  67540     int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 67546  67541     const char *zDb;  /* database name - used by the update hook */
 67547  67542     const char *zTbl; /* Table name - used by the opdate hook */
 67548  67543     int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
 67549         -#endif /* local variables moved into u.bf */
        67544  +#endif /* local variables moved into u.bg */
 67550  67545   
 67551         -  u.bf.pData = &aMem[pOp->p2];
        67546  +  u.bg.pData = &aMem[pOp->p2];
 67552  67547     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67553         -  assert( memIsValid(u.bf.pData) );
 67554         -  u.bf.pC = p->apCsr[pOp->p1];
 67555         -  assert( u.bf.pC!=0 );
 67556         -  assert( u.bf.pC->pCursor!=0 );
 67557         -  assert( u.bf.pC->pseudoTableReg==0 );
 67558         -  assert( u.bf.pC->isTable );
 67559         -  REGISTER_TRACE(pOp->p2, u.bf.pData);
        67548  +  assert( memIsValid(u.bg.pData) );
        67549  +  u.bg.pC = p->apCsr[pOp->p1];
        67550  +  assert( u.bg.pC!=0 );
        67551  +  assert( u.bg.pC->pCursor!=0 );
        67552  +  assert( u.bg.pC->pseudoTableReg==0 );
        67553  +  assert( u.bg.pC->isTable );
        67554  +  REGISTER_TRACE(pOp->p2, u.bg.pData);
 67560  67555   
 67561  67556     if( pOp->opcode==OP_Insert ){
 67562         -    u.bf.pKey = &aMem[pOp->p3];
 67563         -    assert( u.bf.pKey->flags & MEM_Int );
 67564         -    assert( memIsValid(u.bf.pKey) );
 67565         -    REGISTER_TRACE(pOp->p3, u.bf.pKey);
 67566         -    u.bf.iKey = u.bf.pKey->u.i;
        67557  +    u.bg.pKey = &aMem[pOp->p3];
        67558  +    assert( u.bg.pKey->flags & MEM_Int );
        67559  +    assert( memIsValid(u.bg.pKey) );
        67560  +    REGISTER_TRACE(pOp->p3, u.bg.pKey);
        67561  +    u.bg.iKey = u.bg.pKey->u.i;
 67567  67562     }else{
 67568  67563       assert( pOp->opcode==OP_InsertInt );
 67569         -    u.bf.iKey = pOp->p3;
        67564  +    u.bg.iKey = pOp->p3;
 67570  67565     }
 67571  67566   
 67572  67567     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 67573         -  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bf.iKey;
 67574         -  if( u.bf.pData->flags & MEM_Null ){
 67575         -    u.bf.pData->z = 0;
 67576         -    u.bf.pData->n = 0;
 67577         -  }else{
 67578         -    assert( u.bf.pData->flags & (MEM_Blob|MEM_Str) );
 67579         -  }
 67580         -  u.bf.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bf.pC->seekResult : 0);
 67581         -  if( u.bf.pData->flags & MEM_Zero ){
 67582         -    u.bf.nZero = u.bf.pData->u.nZero;
 67583         -  }else{
 67584         -    u.bf.nZero = 0;
 67585         -  }
 67586         -  sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, 0);
 67587         -  rc = sqlite3BtreeInsert(u.bf.pC->pCursor, 0, u.bf.iKey,
 67588         -                          u.bf.pData->z, u.bf.pData->n, u.bf.nZero,
 67589         -                          pOp->p5 & OPFLAG_APPEND, u.bf.seekResult
 67590         -  );
 67591         -  u.bf.pC->rowidIsValid = 0;
 67592         -  u.bf.pC->deferredMoveto = 0;
 67593         -  u.bf.pC->cacheStatus = CACHE_STALE;
        67568  +  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bg.iKey;
        67569  +  if( u.bg.pData->flags & MEM_Null ){
        67570  +    u.bg.pData->z = 0;
        67571  +    u.bg.pData->n = 0;
        67572  +  }else{
        67573  +    assert( u.bg.pData->flags & (MEM_Blob|MEM_Str) );
        67574  +  }
        67575  +  u.bg.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bg.pC->seekResult : 0);
        67576  +  if( u.bg.pData->flags & MEM_Zero ){
        67577  +    u.bg.nZero = u.bg.pData->u.nZero;
        67578  +  }else{
        67579  +    u.bg.nZero = 0;
        67580  +  }
        67581  +  sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
        67582  +  rc = sqlite3BtreeInsert(u.bg.pC->pCursor, 0, u.bg.iKey,
        67583  +                          u.bg.pData->z, u.bg.pData->n, u.bg.nZero,
        67584  +                          pOp->p5 & OPFLAG_APPEND, u.bg.seekResult
        67585  +  );
        67586  +  u.bg.pC->rowidIsValid = 0;
        67587  +  u.bg.pC->deferredMoveto = 0;
        67588  +  u.bg.pC->cacheStatus = CACHE_STALE;
 67594  67589   
 67595  67590     /* Invoke the update-hook if required. */
 67596  67591     if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
 67597         -    u.bf.zDb = db->aDb[u.bf.pC->iDb].zName;
 67598         -    u.bf.zTbl = pOp->p4.z;
 67599         -    u.bf.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
 67600         -    assert( u.bf.pC->isTable );
 67601         -    db->xUpdateCallback(db->pUpdateArg, u.bf.op, u.bf.zDb, u.bf.zTbl, u.bf.iKey);
 67602         -    assert( u.bf.pC->iDb>=0 );
        67592  +    u.bg.zDb = db->aDb[u.bg.pC->iDb].zName;
        67593  +    u.bg.zTbl = pOp->p4.z;
        67594  +    u.bg.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
        67595  +    assert( u.bg.pC->isTable );
        67596  +    db->xUpdateCallback(db->pUpdateArg, u.bg.op, u.bg.zDb, u.bg.zTbl, u.bg.iKey);
        67597  +    assert( u.bg.pC->iDb>=0 );
 67603  67598     }
 67604  67599     break;
 67605  67600   }
 67606  67601   
 67607  67602   /* Opcode: Delete P1 P2 * P4 *
 67608  67603   **
 67609  67604   ** Delete the record at which the P1 cursor is currently pointing.
................................................................................
 67621  67616   **
 67622  67617   ** If P4 is not NULL, then it is the name of the table that P1 is
 67623  67618   ** pointing to.  The update hook will be invoked, if it exists.
 67624  67619   ** If P4 is not NULL then the P1 cursor must have been positioned
 67625  67620   ** using OP_NotFound prior to invoking this opcode.
 67626  67621   */
 67627  67622   case OP_Delete: {
 67628         -#if 0  /* local variables moved into u.bg */
        67623  +#if 0  /* local variables moved into u.bh */
 67629  67624     i64 iKey;
 67630  67625     VdbeCursor *pC;
 67631         -#endif /* local variables moved into u.bg */
        67626  +#endif /* local variables moved into u.bh */
 67632  67627   
 67633         -  u.bg.iKey = 0;
        67628  +  u.bh.iKey = 0;
 67634  67629     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67635         -  u.bg.pC = p->apCsr[pOp->p1];
 67636         -  assert( u.bg.pC!=0 );
 67637         -  assert( u.bg.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
        67630  +  u.bh.pC = p->apCsr[pOp->p1];
        67631  +  assert( u.bh.pC!=0 );
        67632  +  assert( u.bh.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
 67638  67633   
 67639         -  /* If the update-hook will be invoked, set u.bg.iKey to the rowid of the
        67634  +  /* If the update-hook will be invoked, set u.bh.iKey to the rowid of the
 67640  67635     ** row being deleted.
 67641  67636     */
 67642  67637     if( db->xUpdateCallback && pOp->p4.z ){
 67643         -    assert( u.bg.pC->isTable );
 67644         -    assert( u.bg.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
 67645         -    u.bg.iKey = u.bg.pC->lastRowid;
        67638  +    assert( u.bh.pC->isTable );
        67639  +    assert( u.bh.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
        67640  +    u.bh.iKey = u.bh.pC->lastRowid;
 67646  67641     }
 67647  67642   
 67648  67643     /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
 67649  67644     ** OP_Column on the same table without any intervening operations that
 67650         -  ** might move or invalidate the cursor.  Hence cursor u.bg.pC is always pointing
        67645  +  ** might move or invalidate the cursor.  Hence cursor u.bh.pC is always pointing
 67651  67646     ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
 67652  67647     ** below is always a no-op and cannot fail.  We will run it anyhow, though,
 67653  67648     ** to guard against future changes to the code generator.
 67654  67649     **/
 67655         -  assert( u.bg.pC->deferredMoveto==0 );
 67656         -  rc = sqlite3VdbeCursorMoveto(u.bg.pC);
        67650  +  assert( u.bh.pC->deferredMoveto==0 );
        67651  +  rc = sqlite3VdbeCursorMoveto(u.bh.pC);
 67657  67652     if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
 67658  67653   
 67659         -  sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
 67660         -  rc = sqlite3BtreeDelete(u.bg.pC->pCursor);
 67661         -  u.bg.pC->cacheStatus = CACHE_STALE;
        67654  +  sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
        67655  +  rc = sqlite3BtreeDelete(u.bh.pC->pCursor);
        67656  +  u.bh.pC->cacheStatus = CACHE_STALE;
 67662  67657   
 67663  67658     /* Invoke the update-hook if required. */
 67664  67659     if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
 67665         -    const char *zDb = db->aDb[u.bg.pC->iDb].zName;
        67660  +    const char *zDb = db->aDb[u.bh.pC->iDb].zName;
 67666  67661       const char *zTbl = pOp->p4.z;
 67667         -    db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bg.iKey);
 67668         -    assert( u.bg.pC->iDb>=0 );
        67662  +    db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bh.iKey);
        67663  +    assert( u.bh.pC->iDb>=0 );
 67669  67664     }
 67670  67665     if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
 67671  67666     break;
 67672  67667   }
 67673  67668   /* Opcode: ResetCount * * * * *
 67674  67669   **
 67675  67670   ** The value of the change counter is copied to the database handle
................................................................................
 67678  67673   ** This is used by trigger programs.
 67679  67674   */
 67680  67675   case OP_ResetCount: {
 67681  67676     sqlite3VdbeSetChanges(db, p->nChange);
 67682  67677     p->nChange = 0;
 67683  67678     break;
 67684  67679   }
        67680  +
        67681  +/* Opcode: SorterCompare P1 P2 P3
        67682  +**
        67683  +** P1 is a sorter cursor. This instruction compares the record blob in 
        67684  +** register P3 with the entry that the sorter cursor currently points to.
        67685  +** If, excluding the rowid fields at the end, the two records are a match,
        67686  +** fall through to the next instruction. Otherwise, jump to instruction P2.
        67687  +*/
        67688  +case OP_SorterCompare: {
        67689  +#if 0  /* local variables moved into u.bi */
        67690  +  VdbeCursor *pC;
        67691  +  int res;
        67692  +#endif /* local variables moved into u.bi */
        67693  +
        67694  +  u.bi.pC = p->apCsr[pOp->p1];
        67695  +  assert( isSorter(u.bi.pC) );
        67696  +  pIn3 = &aMem[pOp->p3];
        67697  +  rc = sqlite3VdbeSorterCompare(u.bi.pC, pIn3, &u.bi.res);
        67698  +  if( u.bi.res ){
        67699  +    pc = pOp->p2-1;
        67700  +  }
        67701  +  break;
        67702  +};
        67703  +
        67704  +/* Opcode: SorterData P1 P2 * * *
        67705  +**
        67706  +** Write into register P2 the current sorter data for sorter cursor P1.
        67707  +*/
        67708  +case OP_SorterData: {
        67709  +#if 0  /* local variables moved into u.bj */
        67710  +  VdbeCursor *pC;
        67711  +#endif /* local variables moved into u.bj */
        67712  +#ifndef SQLITE_OMIT_MERGE_SORT
        67713  +  pOut = &aMem[pOp->p2];
        67714  +  u.bj.pC = p->apCsr[pOp->p1];
        67715  +  assert( u.bj.pC->isSorter );
        67716  +  rc = sqlite3VdbeSorterRowkey(u.bj.pC, pOut);
        67717  +#else
        67718  +  pOp->opcode = OP_RowKey;
        67719  +  pc--;
        67720  +#endif
        67721  +  break;
        67722  +}
 67685  67723   
 67686  67724   /* Opcode: RowData P1 P2 * * *
 67687  67725   **
 67688  67726   ** Write into register P2 the complete row data for cursor P1.
 67689  67727   ** There is no interpretation of the data.  
 67690  67728   ** It is just copied onto the P2 register exactly as 
 67691  67729   ** it is found in the database file.
................................................................................
 67701  67739   ** it is found in the database file.
 67702  67740   **
 67703  67741   ** If the P1 cursor must be pointing to a valid row (not a NULL row)
 67704  67742   ** of a real table, not a pseudo-table.
 67705  67743   */
 67706  67744   case OP_RowKey:
 67707  67745   case OP_RowData: {
 67708         -#if 0  /* local variables moved into u.bh */
        67746  +#if 0  /* local variables moved into u.bk */
 67709  67747     VdbeCursor *pC;
 67710  67748     BtCursor *pCrsr;
 67711  67749     u32 n;
 67712  67750     i64 n64;
 67713         -#endif /* local variables moved into u.bh */
        67751  +#endif /* local variables moved into u.bk */
 67714  67752   
 67715  67753     pOut = &aMem[pOp->p2];
 67716  67754     memAboutToChange(p, pOut);
 67717  67755   
 67718  67756     /* Note that RowKey and RowData are really exactly the same instruction */
 67719  67757     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67720         -  u.bh.pC = p->apCsr[pOp->p1];
 67721         -  assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey );
 67722         -  assert( u.bh.pC->isIndex || pOp->opcode==OP_RowData );
 67723         -  assert( u.bh.pC!=0 );
 67724         -  assert( u.bh.pC->nullRow==0 );
 67725         -  assert( u.bh.pC->pseudoTableReg==0 );
 67726         -
 67727         -  if( isSorter(u.bh.pC) ){
 67728         -    assert( pOp->opcode==OP_RowKey );
 67729         -    rc = sqlite3VdbeSorterRowkey(u.bh.pC, pOut);
 67730         -    break;
 67731         -  }
 67732         -
 67733         -  assert( u.bh.pC->pCursor!=0 );
 67734         -  u.bh.pCrsr = u.bh.pC->pCursor;
 67735         -  assert( sqlite3BtreeCursorIsValid(u.bh.pCrsr) );
        67758  +  u.bk.pC = p->apCsr[pOp->p1];
        67759  +  assert( u.bk.pC->isSorter==0 );
        67760  +  assert( u.bk.pC->isTable || pOp->opcode!=OP_RowData );
        67761  +  assert( u.bk.pC->isIndex || pOp->opcode==OP_RowData );
        67762  +  assert( u.bk.pC!=0 );
        67763  +  assert( u.bk.pC->nullRow==0 );
        67764  +  assert( u.bk.pC->pseudoTableReg==0 );
        67765  +  assert( !u.bk.pC->isSorter );
        67766  +  assert( u.bk.pC->pCursor!=0 );
        67767  +  u.bk.pCrsr = u.bk.pC->pCursor;
        67768  +  assert( sqlite3BtreeCursorIsValid(u.bk.pCrsr) );
 67736  67769   
 67737  67770     /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
 67738  67771     ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
 67739  67772     ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
 67740  67773     ** a no-op and can never fail.  But we leave it in place as a safety.
 67741  67774     */
 67742         -  assert( u.bh.pC->deferredMoveto==0 );
 67743         -  rc = sqlite3VdbeCursorMoveto(u.bh.pC);
        67775  +  assert( u.bk.pC->deferredMoveto==0 );
        67776  +  rc = sqlite3VdbeCursorMoveto(u.bk.pC);
 67744  67777     if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
 67745  67778   
 67746         -  if( u.bh.pC->isIndex ){
 67747         -    assert( !u.bh.pC->isTable );
 67748         -    rc = sqlite3BtreeKeySize(u.bh.pCrsr, &u.bh.n64);
        67779  +  if( u.bk.pC->isIndex ){
        67780  +    assert( !u.bk.pC->isTable );
        67781  +    rc = sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
 67749  67782       assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
 67750         -    if( u.bh.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
        67783  +    if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 67751  67784         goto too_big;
 67752  67785       }
 67753         -    u.bh.n = (u32)u.bh.n64;
        67786  +    u.bk.n = (u32)u.bk.n64;
 67754  67787     }else{
 67755         -    rc = sqlite3BtreeDataSize(u.bh.pCrsr, &u.bh.n);
        67788  +    rc = sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
 67756  67789       assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
 67757         -    if( u.bh.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
        67790  +    if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 67758  67791         goto too_big;
 67759  67792       }
 67760  67793     }
 67761         -  if( sqlite3VdbeMemGrow(pOut, u.bh.n, 0) ){
        67794  +  if( sqlite3VdbeMemGrow(pOut, u.bk.n, 0) ){
 67762  67795       goto no_mem;
 67763  67796     }
 67764         -  pOut->n = u.bh.n;
        67797  +  pOut->n = u.bk.n;
 67765  67798     MemSetTypeFlag(pOut, MEM_Blob);
 67766         -  if( u.bh.pC->isIndex ){
 67767         -    rc = sqlite3BtreeKey(u.bh.pCrsr, 0, u.bh.n, pOut->z);
        67799  +  if( u.bk.pC->isIndex ){
        67800  +    rc = sqlite3BtreeKey(u.bk.pCrsr, 0, u.bk.n, pOut->z);
 67768  67801     }else{
 67769         -    rc = sqlite3BtreeData(u.bh.pCrsr, 0, u.bh.n, pOut->z);
        67802  +    rc = sqlite3BtreeData(u.bk.pCrsr, 0, u.bk.n, pOut->z);
 67770  67803     }
 67771  67804     pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
 67772  67805     UPDATE_MAX_BLOBSIZE(pOut);
 67773  67806     break;
 67774  67807   }
 67775  67808   
 67776  67809   /* Opcode: Rowid P1 P2 * * *
................................................................................
 67779  67812   ** P1 is currently point to.
 67780  67813   **
 67781  67814   ** P1 can be either an ordinary table or a virtual table.  There used to
 67782  67815   ** be a separate OP_VRowid opcode for use with virtual tables, but this
 67783  67816   ** one opcode now works for both table types.
 67784  67817   */
 67785  67818   case OP_Rowid: {                 /* out2-prerelease */
 67786         -#if 0  /* local variables moved into u.bi */
        67819  +#if 0  /* local variables moved into u.bl */
 67787  67820     VdbeCursor *pC;
 67788  67821     i64 v;
 67789  67822     sqlite3_vtab *pVtab;
 67790  67823     const sqlite3_module *pModule;
 67791         -#endif /* local variables moved into u.bi */
        67824  +#endif /* local variables moved into u.bl */
 67792  67825   
 67793  67826     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67794         -  u.bi.pC = p->apCsr[pOp->p1];
 67795         -  assert( u.bi.pC!=0 );
 67796         -  assert( u.bi.pC->pseudoTableReg==0 );
 67797         -  if( u.bi.pC->nullRow ){
        67827  +  u.bl.pC = p->apCsr[pOp->p1];
        67828  +  assert( u.bl.pC!=0 );
        67829  +  assert( u.bl.pC->pseudoTableReg==0 );
        67830  +  if( u.bl.pC->nullRow ){
 67798  67831       pOut->flags = MEM_Null;
 67799  67832       break;
 67800         -  }else if( u.bi.pC->deferredMoveto ){
 67801         -    u.bi.v = u.bi.pC->movetoTarget;
        67833  +  }else if( u.bl.pC->deferredMoveto ){
        67834  +    u.bl.v = u.bl.pC->movetoTarget;
 67802  67835   #ifndef SQLITE_OMIT_VIRTUALTABLE
 67803         -  }else if( u.bi.pC->pVtabCursor ){
 67804         -    u.bi.pVtab = u.bi.pC->pVtabCursor->pVtab;
 67805         -    u.bi.pModule = u.bi.pVtab->pModule;
 67806         -    assert( u.bi.pModule->xRowid );
 67807         -    rc = u.bi.pModule->xRowid(u.bi.pC->pVtabCursor, &u.bi.v);
 67808         -    importVtabErrMsg(p, u.bi.pVtab);
        67836  +  }else if( u.bl.pC->pVtabCursor ){
        67837  +    u.bl.pVtab = u.bl.pC->pVtabCursor->pVtab;
        67838  +    u.bl.pModule = u.bl.pVtab->pModule;
        67839  +    assert( u.bl.pModule->xRowid );
        67840  +    rc = u.bl.pModule->xRowid(u.bl.pC->pVtabCursor, &u.bl.v);
        67841  +    importVtabErrMsg(p, u.bl.pVtab);
 67809  67842   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 67810  67843     }else{
 67811         -    assert( u.bi.pC->pCursor!=0 );
 67812         -    rc = sqlite3VdbeCursorMoveto(u.bi.pC);
        67844  +    assert( u.bl.pC->pCursor!=0 );
        67845  +    rc = sqlite3VdbeCursorMoveto(u.bl.pC);
 67813  67846       if( rc ) goto abort_due_to_error;
 67814         -    if( u.bi.pC->rowidIsValid ){
 67815         -      u.bi.v = u.bi.pC->lastRowid;
        67847  +    if( u.bl.pC->rowidIsValid ){
        67848  +      u.bl.v = u.bl.pC->lastRowid;
 67816  67849       }else{
 67817         -      rc = sqlite3BtreeKeySize(u.bi.pC->pCursor, &u.bi.v);
        67850  +      rc = sqlite3BtreeKeySize(u.bl.pC->pCursor, &u.bl.v);
 67818  67851         assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
 67819  67852       }
 67820  67853     }
 67821         -  pOut->u.i = u.bi.v;
        67854  +  pOut->u.i = u.bl.v;
 67822  67855     break;
 67823  67856   }
 67824  67857   
 67825  67858   /* Opcode: NullRow P1 * * * *
 67826  67859   **
 67827  67860   ** Move the cursor P1 to a null row.  Any OP_Column operations
 67828  67861   ** that occur while the cursor is on the null row will always
 67829  67862   ** write a NULL.
 67830  67863   */
 67831  67864   case OP_NullRow: {
 67832         -#if 0  /* local variables moved into u.bj */
        67865  +#if 0  /* local variables moved into u.bm */
 67833  67866     VdbeCursor *pC;
 67834         -#endif /* local variables moved into u.bj */
        67867  +#endif /* local variables moved into u.bm */
 67835  67868   
 67836  67869     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67837         -  u.bj.pC = p->apCsr[pOp->p1];
 67838         -  assert( u.bj.pC!=0 );
 67839         -  u.bj.pC->nullRow = 1;
 67840         -  u.bj.pC->rowidIsValid = 0;
 67841         -  assert( u.bj.pC->pCursor || u.bj.pC->pVtabCursor );
 67842         -  if( u.bj.pC->pCursor ){
 67843         -    sqlite3BtreeClearCursor(u.bj.pC->pCursor);
        67870  +  u.bm.pC = p->apCsr[pOp->p1];
        67871  +  assert( u.bm.pC!=0 );
        67872  +  u.bm.pC->nullRow = 1;
        67873  +  u.bm.pC->rowidIsValid = 0;
        67874  +  assert( u.bm.pC->pCursor || u.bm.pC->pVtabCursor );
        67875  +  if( u.bm.pC->pCursor ){
        67876  +    sqlite3BtreeClearCursor(u.bm.pC->pCursor);
 67844  67877     }
 67845  67878     break;
 67846  67879   }
 67847  67880   
 67848  67881   /* Opcode: Last P1 P2 * * *
 67849  67882   **
 67850  67883   ** The next use of the Rowid or Column or Next instruction for P1 
 67851  67884   ** will refer to the last entry in the database table or index.
 67852  67885   ** If the table or index is empty and P2>0, then jump immediately to P2.
 67853  67886   ** If P2 is 0 or if the table or index is not empty, fall through
 67854  67887   ** to the following instruction.
 67855  67888   */
 67856  67889   case OP_Last: {        /* jump */
 67857         -#if 0  /* local variables moved into u.bk */
        67890  +#if 0  /* local variables moved into u.bn */
 67858  67891     VdbeCursor *pC;
 67859  67892     BtCursor *pCrsr;
 67860  67893     int res;
 67861         -#endif /* local variables moved into u.bk */
        67894  +#endif /* local variables moved into u.bn */
 67862  67895   
 67863  67896     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67864         -  u.bk.pC = p->apCsr[pOp->p1];
 67865         -  assert( u.bk.pC!=0 );
 67866         -  u.bk.pCrsr = u.bk.pC->pCursor;
 67867         -  if( NEVER(u.bk.pCrsr==0) ){
 67868         -    u.bk.res = 1;
        67897  +  u.bn.pC = p->apCsr[pOp->p1];
        67898  +  assert( u.bn.pC!=0 );
        67899  +  u.bn.pCrsr = u.bn.pC->pCursor;
        67900  +  if( NEVER(u.bn.pCrsr==0) ){
        67901  +    u.bn.res = 1;
 67869  67902     }else{
 67870         -    rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res);
        67903  +    rc = sqlite3BtreeLast(u.bn.pCrsr, &u.bn.res);
 67871  67904     }
 67872         -  u.bk.pC->nullRow = (u8)u.bk.res;
 67873         -  u.bk.pC->deferredMoveto = 0;
 67874         -  u.bk.pC->rowidIsValid = 0;
 67875         -  u.bk.pC->cacheStatus = CACHE_STALE;
 67876         -  if( pOp->p2>0 && u.bk.res ){
        67905  +  u.bn.pC->nullRow = (u8)u.bn.res;
        67906  +  u.bn.pC->deferredMoveto = 0;
        67907  +  u.bn.pC->rowidIsValid = 0;
        67908  +  u.bn.pC->cacheStatus = CACHE_STALE;
        67909  +  if( pOp->p2>0 && u.bn.res ){
 67877  67910       pc = pOp->p2 - 1;
 67878  67911     }
 67879  67912     break;
 67880  67913   }
 67881  67914   
 67882  67915   
 67883  67916   /* Opcode: Sort P1 P2 * * *
................................................................................
 67888  67921   ** Sorting is accomplished by writing records into a sorting index,
 67889  67922   ** then rewinding that index and playing it back from beginning to
 67890  67923   ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
 67891  67924   ** rewinding so that the global variable will be incremented and
 67892  67925   ** regression tests can determine whether or not the optimizer is
 67893  67926   ** correctly optimizing out sorts.
 67894  67927   */
        67928  +case OP_SorterSort:    /* jump */
        67929  +#ifdef SQLITE_OMIT_MERGE_SORT
        67930  +  pOp->opcode = OP_Sort;
        67931  +#endif
 67895  67932   case OP_Sort: {        /* jump */
 67896  67933   #ifdef SQLITE_TEST
 67897  67934     sqlite3_sort_count++;
 67898  67935     sqlite3_search_count--;
 67899  67936   #endif
 67900  67937     p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
 67901  67938     /* Fall through into OP_Rewind */
................................................................................
 67905  67942   ** The next use of the Rowid or Column or Next instruction for P1 
 67906  67943   ** will refer to the first entry in the database table or index.
 67907  67944   ** If the table or index is empty and P2>0, then jump immediately to P2.
 67908  67945   ** If P2 is 0 or if the table or index is not empty, fall through
 67909  67946   ** to the following instruction.
 67910  67947   */
 67911  67948   case OP_Rewind: {        /* jump */
 67912         -#if 0  /* local variables moved into u.bl */
        67949  +#if 0  /* local variables moved into u.bo */
 67913  67950     VdbeCursor *pC;
 67914  67951     BtCursor *pCrsr;
 67915  67952     int res;
 67916         -#endif /* local variables moved into u.bl */
        67953  +#endif /* local variables moved into u.bo */
 67917  67954   
 67918  67955     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67919         -  u.bl.pC = p->apCsr[pOp->p1];
 67920         -  assert( u.bl.pC!=0 );
 67921         -  u.bl.res = 1;
 67922         -  if( isSorter(u.bl.pC) ){
 67923         -    rc = sqlite3VdbeSorterRewind(db, u.bl.pC, &u.bl.res);
        67956  +  u.bo.pC = p->apCsr[pOp->p1];
        67957  +  assert( u.bo.pC!=0 );
        67958  +  assert( u.bo.pC->isSorter==(pOp->opcode==OP_SorterSort) );
        67959  +  u.bo.res = 1;
        67960  +  if( isSorter(u.bo.pC) ){
        67961  +    rc = sqlite3VdbeSorterRewind(db, u.bo.pC, &u.bo.res);
 67924  67962     }else{
 67925         -    u.bl.pCrsr = u.bl.pC->pCursor;
 67926         -    assert( u.bl.pCrsr );
 67927         -    rc = sqlite3BtreeFirst(u.bl.pCrsr, &u.bl.res);
 67928         -    u.bl.pC->atFirst = u.bl.res==0 ?1:0;
 67929         -    u.bl.pC->deferredMoveto = 0;
 67930         -    u.bl.pC->cacheStatus = CACHE_STALE;
 67931         -    u.bl.pC->rowidIsValid = 0;
        67963  +    u.bo.pCrsr = u.bo.pC->pCursor;
        67964  +    assert( u.bo.pCrsr );
        67965  +    rc = sqlite3BtreeFirst(u.bo.pCrsr, &u.bo.res);
        67966  +    u.bo.pC->atFirst = u.bo.res==0 ?1:0;
        67967  +    u.bo.pC->deferredMoveto = 0;
        67968  +    u.bo.pC->cacheStatus = CACHE_STALE;
        67969  +    u.bo.pC->rowidIsValid = 0;
 67932  67970     }
 67933         -  u.bl.pC->nullRow = (u8)u.bl.res;
        67971  +  u.bo.pC->nullRow = (u8)u.bo.res;
 67934  67972     assert( pOp->p2>0 && pOp->p2<p->nOp );
 67935         -  if( u.bl.res ){
        67973  +  if( u.bo.res ){
 67936  67974       pc = pOp->p2 - 1;
 67937  67975     }
 67938  67976     break;
 67939  67977   }
 67940  67978   
 67941  67979   /* Opcode: Next P1 P2 * P4 P5
 67942  67980   **
................................................................................
 67966  68004   **
 67967  68005   ** P4 is always of type P4_ADVANCE. The function pointer points to
 67968  68006   ** sqlite3BtreePrevious().
 67969  68007   **
 67970  68008   ** If P5 is positive and the jump is taken, then event counter
 67971  68009   ** number P5-1 in the prepared statement is incremented.
 67972  68010   */
        68011  +case OP_SorterNext:    /* jump */
        68012  +#ifdef SQLITE_OMIT_MERGE_SORT
        68013  +  pOp->opcode = OP_Next;
        68014  +#endif
 67973  68015   case OP_Prev:          /* jump */
 67974  68016   case OP_Next: {        /* jump */
 67975         -#if 0  /* local variables moved into u.bm */
        68017  +#if 0  /* local variables moved into u.bp */
 67976  68018     VdbeCursor *pC;
 67977  68019     int res;
 67978         -#endif /* local variables moved into u.bm */
        68020  +#endif /* local variables moved into u.bp */
 67979  68021   
 67980  68022     CHECK_FOR_INTERRUPT;
 67981  68023     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 67982  68024     assert( pOp->p5<=ArraySize(p->aCounter) );
 67983         -  u.bm.pC = p->apCsr[pOp->p1];
 67984         -  if( u.bm.pC==0 ){
        68025  +  u.bp.pC = p->apCsr[pOp->p1];
        68026  +  if( u.bp.pC==0 ){
 67985  68027       break;  /* See ticket #2273 */
 67986  68028     }
 67987         -  if( isSorter(u.bm.pC) ){
 67988         -    assert( pOp->opcode==OP_Next );
 67989         -    rc = sqlite3VdbeSorterNext(db, u.bm.pC, &u.bm.res);
        68029  +  assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterNext) );
        68030  +  if( isSorter(u.bp.pC) ){
        68031  +    assert( pOp->opcode==OP_SorterNext );
        68032  +    rc = sqlite3VdbeSorterNext(db, u.bp.pC, &u.bp.res);
 67990  68033     }else{
 67991         -    u.bm.res = 1;
 67992         -    assert( u.bm.pC->deferredMoveto==0 );
 67993         -    assert( u.bm.pC->pCursor );
        68034  +    u.bp.res = 1;
        68035  +    assert( u.bp.pC->deferredMoveto==0 );
        68036  +    assert( u.bp.pC->pCursor );
 67994  68037       assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
 67995  68038       assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
 67996         -    rc = pOp->p4.xAdvance(u.bm.pC->pCursor, &u.bm.res);
        68039  +    rc = pOp->p4.xAdvance(u.bp.pC->pCursor, &u.bp.res);
 67997  68040     }
 67998         -  u.bm.pC->nullRow = (u8)u.bm.res;
 67999         -  u.bm.pC->cacheStatus = CACHE_STALE;
 68000         -  if( u.bm.res==0 ){
        68041  +  u.bp.pC->nullRow = (u8)u.bp.res;
        68042  +  u.bp.pC->cacheStatus = CACHE_STALE;
        68043  +  if( u.bp.res==0 ){
 68001  68044       pc = pOp->p2 - 1;
 68002  68045       if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
 68003  68046   #ifdef SQLITE_TEST
 68004  68047       sqlite3_search_count++;
 68005  68048   #endif
 68006  68049     }
 68007         -  u.bm.pC->rowidIsValid = 0;
        68050  +  u.bp.pC->rowidIsValid = 0;
 68008  68051     break;
 68009  68052   }
 68010  68053   
 68011  68054   /* Opcode: IdxInsert P1 P2 P3 * P5
 68012  68055   **
 68013  68056   ** Register P2 holds an SQL index key made using the
 68014  68057   ** MakeRecord instructions.  This opcode writes that key
................................................................................
 68016  68059   **
 68017  68060   ** P3 is a flag that provides a hint to the b-tree layer that this
 68018  68061   ** insert is likely to be an append.
 68019  68062   **
 68020  68063   ** This instruction only works for indices.  The equivalent instruction
 68021  68064   ** for tables is OP_Insert.
 68022  68065   */
        68066  +case OP_SorterInsert:       /* in2 */
        68067  +#ifdef SQLITE_OMIT_MERGE_SORT
        68068  +  pOp->opcode = OP_IdxInsert;
        68069  +#endif
 68023  68070   case OP_IdxInsert: {        /* in2 */
 68024         -#if 0  /* local variables moved into u.bn */
        68071  +#if 0  /* local variables moved into u.bq */
 68025  68072     VdbeCursor *pC;
 68026  68073     BtCursor *pCrsr;
 68027  68074     int nKey;
 68028  68075     const char *zKey;
 68029         -#endif /* local variables moved into u.bn */
        68076  +#endif /* local variables moved into u.bq */
 68030  68077   
 68031  68078     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68032         -  u.bn.pC = p->apCsr[pOp->p1];
 68033         -  assert( u.bn.pC!=0 );
        68079  +  u.bq.pC = p->apCsr[pOp->p1];
        68080  +  assert( u.bq.pC!=0 );
        68081  +  assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
 68034  68082     pIn2 = &aMem[pOp->p2];
 68035  68083     assert( pIn2->flags & MEM_Blob );
 68036         -  u.bn.pCrsr = u.bn.pC->pCursor;
 68037         -  if( ALWAYS(u.bn.pCrsr!=0) ){
 68038         -    assert( u.bn.pC->isTable==0 );
        68084  +  u.bq.pCrsr = u.bq.pC->pCursor;
        68085  +  if( ALWAYS(u.bq.pCrsr!=0) ){
        68086  +    assert( u.bq.pC->isTable==0 );
 68039  68087       rc = ExpandBlob(pIn2);
 68040  68088       if( rc==SQLITE_OK ){
 68041         -      u.bn.nKey = pIn2->n;
 68042         -      u.bn.zKey = pIn2->z;
 68043         -      rc = sqlite3VdbeSorterWrite(db, u.bn.pC, u.bn.nKey);
 68044         -      if( rc==SQLITE_OK ){
 68045         -        rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3,
 68046         -            ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0)
 68047         -        );
 68048         -        assert( u.bn.pC->deferredMoveto==0 );
        68089  +      if( isSorter(u.bq.pC) ){
        68090  +        rc = sqlite3VdbeSorterWrite(db, u.bq.pC, pIn2);
        68091  +      }else{
        68092  +        u.bq.nKey = pIn2->n;
        68093  +        u.bq.zKey = pIn2->z;
        68094  +        rc = sqlite3BtreeInsert(u.bq.pCrsr, u.bq.zKey, u.bq.nKey, "", 0, 0, pOp->p3,
        68095  +            ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bq.pC->seekResult : 0)
        68096  +            );
        68097  +        assert( u.bq.pC->deferredMoveto==0 );
        68098  +        u.bq.pC->cacheStatus = CACHE_STALE;
 68049  68099         }
 68050         -      u.bn.pC->cacheStatus = CACHE_STALE;
 68051  68100       }
 68052  68101     }
 68053  68102     break;
 68054  68103   }
 68055  68104   
 68056  68105   /* Opcode: IdxDelete P1 P2 P3 * *
 68057  68106   **
 68058  68107   ** The content of P3 registers starting at register P2 form
 68059  68108   ** an unpacked index key. This opcode removes that entry from the 
 68060  68109   ** index opened by cursor P1.
 68061  68110   */
 68062  68111   case OP_IdxDelete: {
 68063         -#if 0  /* local variables moved into u.bo */
        68112  +#if 0  /* local variables moved into u.br */
 68064  68113     VdbeCursor *pC;
 68065  68114     BtCursor *pCrsr;
 68066  68115     int res;
 68067  68116     UnpackedRecord r;
 68068         -#endif /* local variables moved into u.bo */
        68117  +#endif /* local variables moved into u.br */
 68069  68118   
 68070  68119     assert( pOp->p3>0 );
 68071  68120     assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
 68072  68121     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68073         -  u.bo.pC = p->apCsr[pOp->p1];
 68074         -  assert( u.bo.pC!=0 );
 68075         -  u.bo.pCrsr = u.bo.pC->pCursor;
 68076         -  if( ALWAYS(u.bo.pCrsr!=0) ){
 68077         -    u.bo.r.pKeyInfo = u.bo.pC->pKeyInfo;
 68078         -    u.bo.r.nField = (u16)pOp->p3;
 68079         -    u.bo.r.flags = 0;
 68080         -    u.bo.r.aMem = &aMem[pOp->p2];
        68122  +  u.br.pC = p->apCsr[pOp->p1];
        68123  +  assert( u.br.pC!=0 );
        68124  +  u.br.pCrsr = u.br.pC->pCursor;
        68125  +  if( ALWAYS(u.br.pCrsr!=0) ){
        68126  +    u.br.r.pKeyInfo = u.br.pC->pKeyInfo;
        68127  +    u.br.r.nField = (u16)pOp->p3;
        68128  +    u.br.r.flags = 0;
        68129  +    u.br.r.aMem = &aMem[pOp->p2];
 68081  68130   #ifdef SQLITE_DEBUG
 68082         -    { int i; for(i=0; i<u.bo.r.nField; i++) assert( memIsValid(&u.bo.r.aMem[i]) ); }
        68131  +    { int i; for(i=0; i<u.br.r.nField; i++) assert( memIsValid(&u.br.r.aMem[i]) ); }
 68083  68132   #endif
 68084         -    rc = sqlite3BtreeMovetoUnpacked(u.bo.pCrsr, &u.bo.r, 0, 0, &u.bo.res);
 68085         -    if( rc==SQLITE_OK && u.bo.res==0 ){
 68086         -      rc = sqlite3BtreeDelete(u.bo.pCrsr);
        68133  +    rc = sqlite3BtreeMovetoUnpacked(u.br.pCrsr, &u.br.r, 0, 0, &u.br.res);
        68134  +    if( rc==SQLITE_OK && u.br.res==0 ){
        68135  +      rc = sqlite3BtreeDelete(u.br.pCrsr);
 68087  68136       }
 68088         -    assert( u.bo.pC->deferredMoveto==0 );
 68089         -    u.bo.pC->cacheStatus = CACHE_STALE;
        68137  +    assert( u.br.pC->deferredMoveto==0 );
        68138  +    u.br.pC->cacheStatus = CACHE_STALE;
 68090  68139     }
 68091  68140     break;
 68092  68141   }
 68093  68142   
 68094  68143   /* Opcode: IdxRowid P1 P2 * * *
 68095  68144   **
 68096  68145   ** Write into register P2 an integer which is the last entry in the record at
 68097  68146   ** the end of the index key pointed to by cursor P1.  This integer should be
 68098  68147   ** the rowid of the table entry to which this index entry points.
 68099  68148   **
 68100  68149   ** See also: Rowid, MakeRecord.
 68101  68150   */
 68102  68151   case OP_IdxRowid: {              /* out2-prerelease */
 68103         -#if 0  /* local variables moved into u.bp */
        68152  +#if 0  /* local variables moved into u.bs */
 68104  68153     BtCursor *pCrsr;
 68105  68154     VdbeCursor *pC;
 68106  68155     i64 rowid;
 68107         -#endif /* local variables moved into u.bp */
        68156  +#endif /* local variables moved into u.bs */
 68108  68157   
 68109  68158     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68110         -  u.bp.pC = p->apCsr[pOp->p1];
 68111         -  assert( u.bp.pC!=0 );
 68112         -  u.bp.pCrsr = u.bp.pC->pCursor;
        68159  +  u.bs.pC = p->apCsr[pOp->p1];
        68160  +  assert( u.bs.pC!=0 );
        68161  +  u.bs.pCrsr = u.bs.pC->pCursor;
 68113  68162     pOut->flags = MEM_Null;
 68114         -  if( ALWAYS(u.bp.pCrsr!=0) ){
 68115         -    rc = sqlite3VdbeCursorMoveto(u.bp.pC);
        68163  +  if( ALWAYS(u.bs.pCrsr!=0) ){
        68164  +    rc = sqlite3VdbeCursorMoveto(u.bs.pC);
 68116  68165       if( NEVER(rc) ) goto abort_due_to_error;
 68117         -    assert( u.bp.pC->deferredMoveto==0 );
 68118         -    assert( u.bp.pC->isTable==0 );
 68119         -    if( !u.bp.pC->nullRow ){
 68120         -      rc = sqlite3VdbeIdxRowid(db, u.bp.pCrsr, &u.bp.rowid);
        68166  +    assert( u.bs.pC->deferredMoveto==0 );
        68167  +    assert( u.bs.pC->isTable==0 );
        68168  +    if( !u.bs.pC->nullRow ){
        68169  +      rc = sqlite3VdbeIdxRowid(db, u.bs.pCrsr, &u.bs.rowid);
 68121  68170         if( rc!=SQLITE_OK ){
 68122  68171           goto abort_due_to_error;
 68123  68172         }
 68124         -      pOut->u.i = u.bp.rowid;
        68173  +      pOut->u.i = u.bs.rowid;
 68125  68174         pOut->flags = MEM_Int;
 68126  68175       }
 68127  68176     }
 68128  68177     break;
 68129  68178   }
 68130  68179   
 68131  68180   /* Opcode: IdxGE P1 P2 P3 P4 P5
................................................................................
 68152  68201   ** Otherwise fall through to the next instruction.
 68153  68202   **
 68154  68203   ** If P5 is non-zero then the key value is increased by an epsilon prior 
 68155  68204   ** to the comparison.  This makes the opcode work like IdxLE.
 68156  68205   */
 68157  68206   case OP_IdxLT:          /* jump */
 68158  68207   case OP_IdxGE: {        /* jump */
 68159         -#if 0  /* local variables moved into u.bq */
        68208  +#if 0  /* local variables moved into u.bt */
 68160  68209     VdbeCursor *pC;
 68161  68210     int res;
 68162  68211     UnpackedRecord r;
 68163         -#endif /* local variables moved into u.bq */
        68212  +#endif /* local variables moved into u.bt */
 68164  68213   
 68165  68214     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 68166         -  u.bq.pC = p->apCsr[pOp->p1];
 68167         -  assert( u.bq.pC!=0 );
 68168         -  assert( u.bq.pC->isOrdered );
 68169         -  if( ALWAYS(u.bq.pC->pCursor!=0) ){
 68170         -    assert( u.bq.pC->deferredMoveto==0 );
        68215  +  u.bt.pC = p->apCsr[pOp->p1];
        68216  +  assert( u.bt.pC!=0 );
        68217  +  assert( u.bt.pC->isOrdered );
        68218  +  if( ALWAYS(u.bt.pC->pCursor!=0) ){
        68219  +    assert( u.bt.pC->deferredMoveto==0 );
 68171  68220       assert( pOp->p5==0 || pOp->p5==1 );
 68172  68221       assert( pOp->p4type==P4_INT32 );
 68173         -    u.bq.r.pKeyInfo = u.bq.pC->pKeyInfo;
 68174         -    u.bq.r.nField = (u16)pOp->p4.i;
        68222  +    u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
        68223  +    u.bt.r.nField = (u16)pOp->p4.i;
 68175  68224       if( pOp->p5 ){
 68176         -      u.bq.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
        68225  +      u.bt.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
 68177  68226       }else{
 68178         -      u.bq.r.flags = UNPACKED_IGNORE_ROWID;
        68227  +      u.bt.r.flags = UNPACKED_IGNORE_ROWID;
 68179  68228       }
 68180         -    u.bq.r.aMem = &aMem[pOp->p3];
        68229  +    u.bt.r.aMem = &aMem[pOp->p3];
 68181  68230   #ifdef SQLITE_DEBUG
 68182         -    { int i; for(i=0; i<u.bq.r.nField; i++) assert( memIsValid(&u.bq.r.aMem[i]) ); }
        68231  +    { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
 68183  68232   #endif
 68184         -    rc = sqlite3VdbeIdxKeyCompare(u.bq.pC, &u.bq.r, &u.bq.res);
        68233  +    rc = sqlite3VdbeIdxKeyCompare(u.bt.pC, &u.bt.r, &u.bt.res);
 68185  68234       if( pOp->opcode==OP_IdxLT ){
 68186         -      u.bq.res = -u.bq.res;
        68235  +      u.bt.res = -u.bt.res;
 68187  68236       }else{
 68188  68237         assert( pOp->opcode==OP_IdxGE );
 68189         -      u.bq.res++;
        68238  +      u.bt.res++;
 68190  68239       }
 68191         -    if( u.bq.res>0 ){
        68240  +    if( u.bt.res>0 ){
 68192  68241         pc = pOp->p2 - 1 ;
 68193  68242       }
 68194  68243     }
 68195  68244     break;
 68196  68245   }
 68197  68246   
 68198  68247   /* Opcode: Destroy P1 P2 P3 * *
................................................................................
 68212  68261   ** movement was required (because the table being dropped was already 
 68213  68262   ** the last one in the database) then a zero is stored in register P2.
 68214  68263   ** If AUTOVACUUM is disabled then a zero is stored in register P2.
 68215  68264   **
 68216  68265   ** See also: Clear
 68217  68266   */
 68218  68267   case OP_Destroy: {     /* out2-prerelease */
 68219         -#if 0  /* local variables moved into u.br */
        68268  +#if 0  /* local variables moved into u.bu */
 68220  68269     int iMoved;
 68221  68270     int iCnt;
 68222  68271     Vdbe *pVdbe;
 68223  68272     int iDb;
 68224         -#endif /* local variables moved into u.br */
        68273  +#endif /* local variables moved into u.bu */
 68225  68274   #ifndef SQLITE_OMIT_VIRTUALTABLE
 68226         -  u.br.iCnt = 0;
 68227         -  for(u.br.pVdbe=db->pVdbe; u.br.pVdbe; u.br.pVdbe = u.br.pVdbe->pNext){
 68228         -    if( u.br.pVdbe->magic==VDBE_MAGIC_RUN && u.br.pVdbe->inVtabMethod<2 && u.br.pVdbe->pc>=0 ){
 68229         -      u.br.iCnt++;
        68275  +  u.bu.iCnt = 0;
        68276  +  for(u.bu.pVdbe=db->pVdbe; u.bu.pVdbe; u.bu.pVdbe = u.bu.pVdbe->pNext){
        68277  +    if( u.bu.pVdbe->magic==VDBE_MAGIC_RUN && u.bu.pVdbe->inVtabMethod<2 && u.bu.pVdbe->pc>=0 ){
        68278  +      u.bu.iCnt++;
 68230  68279       }
 68231  68280     }
 68232  68281   #else
 68233         -  u.br.iCnt = db->activeVdbeCnt;
        68282  +  u.bu.iCnt = db->activeVdbeCnt;
 68234  68283   #endif
 68235  68284     pOut->flags = MEM_Null;
 68236         -  if( u.br.iCnt>1 ){
        68285  +  if( u.bu.iCnt>1 ){
 68237  68286       rc = SQLITE_LOCKED;
 68238  68287       p->errorAction = OE_Abort;
 68239  68288     }else{
 68240         -    u.br.iDb = pOp->p3;
 68241         -    assert( u.br.iCnt==1 );
 68242         -    assert( (p->btreeMask & (((yDbMask)1)<<u.br.iDb))!=0 );
 68243         -    rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved);
        68289  +    u.bu.iDb = pOp->p3;
        68290  +    assert( u.bu.iCnt==1 );
        68291  +    assert( (p->btreeMask & (((yDbMask)1)<<u.bu.iDb))!=0 );
        68292  +    rc = sqlite3BtreeDropTable(db->aDb[u.bu.iDb].pBt, pOp->p1, &u.bu.iMoved);
 68244  68293       pOut->flags = MEM_Int;
 68245         -    pOut->u.i = u.br.iMoved;
        68294  +    pOut->u.i = u.bu.iMoved;
 68246  68295   #ifndef SQLITE_OMIT_AUTOVACUUM
 68247         -    if( rc==SQLITE_OK && u.br.iMoved!=0 ){
 68248         -      sqlite3RootPageMoved(db, u.br.iDb, u.br.iMoved, pOp->p1);
        68296  +    if( rc==SQLITE_OK && u.bu.iMoved!=0 ){
        68297  +      sqlite3RootPageMoved(db, u.bu.iDb, u.bu.iMoved, pOp->p1);
 68249  68298         /* All OP_Destroy operations occur on the same btree */
 68250         -      assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.br.iDb+1 );
 68251         -      resetSchemaOnFault = u.br.iDb+1;
        68299  +      assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bu.iDb+1 );
        68300  +      resetSchemaOnFault = u.bu.iDb+1;
 68252  68301       }
 68253  68302   #endif
 68254  68303     }
 68255  68304     break;
 68256  68305   }
 68257  68306   
 68258  68307   /* Opcode: Clear P1 P2 P3
................................................................................
 68270  68319   ** count is incremented by the number of rows in the table being cleared. 
 68271  68320   ** If P3 is greater than zero, then the value stored in register P3 is
 68272  68321   ** also incremented by the number of rows in the table being cleared.
 68273  68322   **
 68274  68323   ** See also: Destroy
 68275  68324   */
 68276  68325   case OP_Clear: {
 68277         -#if 0  /* local variables moved into u.bs */
        68326  +#if 0  /* local variables moved into u.bv */
 68278  68327     int nChange;
 68279         -#endif /* local variables moved into u.bs */
        68328  +#endif /* local variables moved into u.bv */
 68280  68329   
 68281         -  u.bs.nChange = 0;
        68330  +  u.bv.nChange = 0;
 68282  68331     assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
 68283  68332     rc = sqlite3BtreeClearTable(
 68284         -      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0)
        68333  +      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bv.nChange : 0)
 68285  68334     );
 68286  68335     if( pOp->p3 ){
 68287         -    p->nChange += u.bs.nChange;
        68336  +    p->nChange += u.bv.nChange;
 68288  68337       if( pOp->p3>0 ){
 68289  68338         assert( memIsValid(&aMem[pOp->p3]) );
 68290  68339         memAboutToChange(p, &aMem[pOp->p3]);
 68291         -      aMem[pOp->p3].u.i += u.bs.nChange;
        68340  +      aMem[pOp->p3].u.i += u.bv.nChange;
 68292  68341       }
 68293  68342     }
 68294  68343     break;
 68295  68344   }
 68296  68345   
 68297  68346   /* Opcode: CreateTable P1 P2 * * *
 68298  68347   **
................................................................................
 68314  68363   ** P1>1.  Write the root page number of the new table into
 68315  68364   ** register P2.
 68316  68365   **
 68317  68366   ** See documentation on OP_CreateTable for additional information.
 68318  68367   */
 68319  68368   case OP_CreateIndex:            /* out2-prerelease */
 68320  68369   case OP_CreateTable: {          /* out2-prerelease */
 68321         -#if 0  /* local variables moved into u.bt */
        68370  +#if 0  /* local variables moved into u.bw */
 68322  68371     int pgno;
 68323  68372     int flags;
 68324  68373     Db *pDb;
 68325         -#endif /* local variables moved into u.bt */
        68374  +#endif /* local variables moved into u.bw */
 68326  68375   
 68327         -  u.bt.pgno = 0;
        68376  +  u.bw.pgno = 0;
 68328  68377     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 68329  68378     assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 68330         -  u.bt.pDb = &db->aDb[pOp->p1];
 68331         -  assert( u.bt.pDb->pBt!=0 );
        68379  +  u.bw.pDb = &db->aDb[pOp->p1];
        68380  +  assert( u.bw.pDb->pBt!=0 );
 68332  68381     if( pOp->opcode==OP_CreateTable ){
 68333         -    /* u.bt.flags = BTREE_INTKEY; */
 68334         -    u.bt.flags = BTREE_INTKEY;
        68382  +    /* u.bw.flags = BTREE_INTKEY; */
        68383  +    u.bw.flags = BTREE_INTKEY;
 68335  68384     }else{
 68336         -    u.bt.flags = BTREE_BLOBKEY;
        68385  +    u.bw.flags = BTREE_BLOBKEY;
 68337  68386     }
 68338         -  rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags);
 68339         -  pOut->u.i = u.bt.pgno;
        68387  +  rc = sqlite3BtreeCreateTable(u.bw.pDb->pBt, &u.bw.pgno, u.bw.flags);
        68388  +  pOut->u.i = u.bw.pgno;
 68340  68389     break;
 68341  68390   }
 68342  68391   
 68343  68392   /* Opcode: ParseSchema P1 * * P4 *
 68344  68393   **
 68345  68394   ** Read and parse all entries from the SQLITE_MASTER table of database P1
 68346  68395   ** that match the WHERE clause P4. 
 68347  68396   **
 68348  68397   ** This opcode invokes the parser to create a new virtual machine,
 68349  68398   ** then runs the new virtual machine.  It is thus a re-entrant opcode.
 68350  68399   */
 68351  68400   case OP_ParseSchema: {
 68352         -#if 0  /* local variables moved into u.bu */
        68401  +#if 0  /* local variables moved into u.bx */
 68353  68402     int iDb;
 68354  68403     const char *zMaster;
 68355  68404     char *zSql;
 68356  68405     InitData initData;
 68357         -#endif /* local variables moved into u.bu */
        68406  +#endif /* local variables moved into u.bx */
 68358  68407   
 68359  68408     /* Any prepared statement that invokes this opcode will hold mutexes
 68360  68409     ** on every btree.  This is a prerequisite for invoking
 68361  68410     ** sqlite3InitCallback().
 68362  68411     */
 68363  68412   #ifdef SQLITE_DEBUG
 68364         -  for(u.bu.iDb=0; u.bu.iDb<db->nDb; u.bu.iDb++){
 68365         -    assert( u.bu.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
        68413  +  for(u.bx.iDb=0; u.bx.iDb<db->nDb; u.bx.iDb++){
        68414  +    assert( u.bx.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bx.iDb].pBt) );
 68366  68415     }
 68367  68416   #endif
 68368  68417   
 68369         -  u.bu.iDb = pOp->p1;
 68370         -  assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb );
 68371         -  assert( DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) );
        68418  +  u.bx.iDb = pOp->p1;
        68419  +  assert( u.bx.iDb>=0 && u.bx.iDb<db->nDb );
        68420  +  assert( DbHasProperty(db, u.bx.iDb, DB_SchemaLoaded) );
 68372  68421     /* Used to be a conditional */ {
 68373         -    u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb);
 68374         -    u.bu.initData.db = db;
 68375         -    u.bu.initData.iDb = pOp->p1;
 68376         -    u.bu.initData.pzErrMsg = &p->zErrMsg;
 68377         -    u.bu.zSql = sqlite3MPrintf(db,
        68422  +    u.bx.zMaster = SCHEMA_TABLE(u.bx.iDb);
        68423  +    u.bx.initData.db = db;
        68424  +    u.bx.initData.iDb = pOp->p1;
        68425  +    u.bx.initData.pzErrMsg = &p->zErrMsg;
        68426  +    u.bx.zSql = sqlite3MPrintf(db,
 68378  68427          "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 68379         -       db->aDb[u.bu.iDb].zName, u.bu.zMaster, pOp->p4.z);
 68380         -    if( u.bu.zSql==0 ){
        68428  +       db->aDb[u.bx.iDb].zName, u.bx.zMaster, pOp->p4.z);
        68429  +    if( u.bx.zSql==0 ){
 68381  68430         rc = SQLITE_NOMEM;
 68382  68431       }else{
 68383  68432         assert( db->init.busy==0 );
 68384  68433         db->init.busy = 1;
 68385         -      u.bu.initData.rc = SQLITE_OK;
        68434  +      u.bx.initData.rc = SQLITE_OK;
 68386  68435         assert( !db->mallocFailed );
 68387         -      rc = sqlite3_exec(db, u.bu.zSql, sqlite3InitCallback, &u.bu.initData, 0);
 68388         -      if( rc==SQLITE_OK ) rc = u.bu.initData.rc;
 68389         -      sqlite3DbFree(db, u.bu.zSql);
        68436  +      rc = sqlite3_exec(db, u.bx.zSql, sqlite3InitCallback, &u.bx.initData, 0);
        68437  +      if( rc==SQLITE_OK ) rc = u.bx.initData.rc;
        68438  +      sqlite3DbFree(db, u.bx.zSql);
 68390  68439         db->init.busy = 0;
 68391  68440       }
 68392  68441     }
 68393  68442     if( rc==SQLITE_NOMEM ){
 68394  68443       goto no_mem;
 68395  68444     }
 68396  68445     break;
................................................................................
 68465  68514   **
 68466  68515   ** If P5 is not zero, the check is done on the auxiliary database
 68467  68516   ** file, not the main database file.
 68468  68517   **
 68469  68518   ** This opcode is used to implement the integrity_check pragma.
 68470  68519   */
 68471  68520   case OP_IntegrityCk: {
 68472         -#if 0  /* local variables moved into u.bv */
        68521  +#if 0  /* local variables moved into u.by */
 68473  68522     int nRoot;      /* Number of tables to check.  (Number of root pages.) */
 68474  68523     int *aRoot;     /* Array of rootpage numbers for tables to be checked */
 68475  68524     int j;          /* Loop counter */
 68476  68525     int nErr;       /* Number of errors reported */
 68477  68526     char *z;        /* Text of the error report */
 68478  68527     Mem *pnErr;     /* Register keeping track of errors remaining */
 68479         -#endif /* local variables moved into u.bv */
        68528  +#endif /* local variables moved into u.by */
 68480  68529   
 68481         -  u.bv.nRoot = pOp->p2;
 68482         -  assert( u.bv.nRoot>0 );
 68483         -  u.bv.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bv.nRoot+1) );
 68484         -  if( u.bv.aRoot==0 ) goto no_mem;
        68530  +  u.by.nRoot = pOp->p2;
        68531  +  assert( u.by.nRoot>0 );
        68532  +  u.by.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.by.nRoot+1) );
        68533  +  if( u.by.aRoot==0 ) goto no_mem;
 68485  68534     assert( pOp->p3>0 && pOp->p3<=p->nMem );
 68486         -  u.bv.pnErr = &aMem[pOp->p3];
 68487         -  assert( (u.bv.pnErr->flags & MEM_Int)!=0 );
 68488         -  assert( (u.bv.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
        68535  +  u.by.pnErr = &aMem[pOp->p3];
        68536  +  assert( (u.by.pnErr->flags & MEM_Int)!=0 );
        68537  +  assert( (u.by.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
 68489  68538     pIn1 = &aMem[pOp->p1];
 68490         -  for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){
 68491         -    u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]);
        68539  +  for(u.by.j=0; u.by.j<u.by.nRoot; u.by.j++){
        68540  +    u.by.aRoot[u.by.j] = (int)sqlite3VdbeIntValue(&pIn1[u.by.j]);
 68492  68541     }
 68493         -  u.bv.aRoot[u.bv.j] = 0;
        68542  +  u.by.aRoot[u.by.j] = 0;
 68494  68543     assert( pOp->p5<db->nDb );
 68495  68544     assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
 68496         -  u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot,
 68497         -                                 (int)u.bv.pnErr->u.i, &u.bv.nErr);
 68498         -  sqlite3DbFree(db, u.bv.aRoot);
 68499         -  u.bv.pnErr->u.i -= u.bv.nErr;
        68545  +  u.by.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.by.aRoot, u.by.nRoot,
        68546  +                                 (int)u.by.pnErr->u.i, &u.by.nErr);
        68547  +  sqlite3DbFree(db, u.by.aRoot);
        68548  +  u.by.pnErr->u.i -= u.by.nErr;
 68500  68549     sqlite3VdbeMemSetNull(pIn1);
 68501         -  if( u.bv.nErr==0 ){
 68502         -    assert( u.bv.z==0 );
 68503         -  }else if( u.bv.z==0 ){
        68550  +  if( u.by.nErr==0 ){
        68551  +    assert( u.by.z==0 );
        68552  +  }else if( u.by.z==0 ){
 68504  68553       goto no_mem;
 68505  68554     }else{
 68506         -    sqlite3VdbeMemSetStr(pIn1, u.bv.z, -1, SQLITE_UTF8, sqlite3_free);
        68555  +    sqlite3VdbeMemSetStr(pIn1, u.by.z, -1, SQLITE_UTF8, sqlite3_free);
 68507  68556     }
 68508  68557     UPDATE_MAX_BLOBSIZE(pIn1);
 68509  68558     sqlite3VdbeChangeEncoding(pIn1, encoding);
 68510  68559     break;
 68511  68560   }
 68512  68561   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 68513  68562   
................................................................................
 68533  68582   /* Opcode: RowSetRead P1 P2 P3 * *
 68534  68583   **
 68535  68584   ** Extract the smallest value from boolean index P1 and put that value into
 68536  68585   ** register P3.  Or, if boolean index P1 is initially empty, leave P3
 68537  68586   ** unchanged and jump to instruction P2.
 68538  68587   */
 68539  68588   case OP_RowSetRead: {       /* jump, in1, out3 */
 68540         -#if 0  /* local variables moved into u.bw */
        68589  +#if 0  /* local variables moved into u.bz */
 68541  68590     i64 val;
 68542         -#endif /* local variables moved into u.bw */
        68591  +#endif /* local variables moved into u.bz */
 68543  68592     CHECK_FOR_INTERRUPT;
 68544  68593     pIn1 = &aMem[pOp->p1];
 68545  68594     if( (pIn1->flags & MEM_RowSet)==0
 68546         -   || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bw.val)==0
        68595  +   || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bz.val)==0
 68547  68596     ){
 68548  68597       /* The boolean index is empty */
 68549  68598       sqlite3VdbeMemSetNull(pIn1);
 68550  68599       pc = pOp->p2 - 1;
 68551  68600     }else{
 68552  68601       /* A value was pulled from the index */
 68553         -    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bw.val);
        68602  +    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bz.val);
 68554  68603     }
 68555  68604     break;
 68556  68605   }
 68557  68606   
 68558  68607   /* Opcode: RowSetTest P1 P2 P3 P4
 68559  68608   **
 68560  68609   ** Register P3 is assumed to hold a 64-bit integer value. If register P1
................................................................................
 68575  68624   ** (b) when P4==-1 there is no need to insert the value, as it will
 68576  68625   ** never be tested for, and (c) when a value that is part of set X is
 68577  68626   ** inserted, there is no need to search to see if the same value was
 68578  68627   ** previously inserted as part of set X (only if it was previously
 68579  68628   ** inserted as part of some other set).
 68580  68629   */
 68581  68630   case OP_RowSetTest: {                     /* jump, in1, in3 */
 68582         -#if 0  /* local variables moved into u.bx */
        68631  +#if 0  /* local variables moved into u.ca */
 68583  68632     int iSet;
 68584  68633     int exists;
 68585         -#endif /* local variables moved into u.bx */
        68634  +#endif /* local variables moved into u.ca */
 68586  68635   
 68587  68636     pIn1 = &aMem[pOp->p1];
 68588  68637     pIn3 = &aMem[pOp->p3];
 68589         -  u.bx.iSet = pOp->p4.i;
        68638  +  u.ca.iSet = pOp->p4.i;
 68590  68639     assert( pIn3->flags&MEM_Int );
 68591  68640   
 68592  68641     /* If there is anything other than a rowset object in memory cell P1,
 68593  68642     ** delete it now and initialize P1 with an empty rowset
 68594  68643     */
 68595  68644     if( (pIn1->flags & MEM_RowSet)==0 ){
 68596  68645       sqlite3VdbeMemSetRowSet(pIn1);
 68597  68646       if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
 68598  68647     }
 68599  68648   
 68600  68649     assert( pOp->p4type==P4_INT32 );
 68601         -  assert( u.bx.iSet==-1 || u.bx.iSet>=0 );
 68602         -  if( u.bx.iSet ){
 68603         -    u.bx.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
 68604         -                               (u8)(u.bx.iSet>=0 ? u.bx.iSet & 0xf : 0xff),
        68650  +  assert( u.ca.iSet==-1 || u.ca.iSet>=0 );
        68651  +  if( u.ca.iSet ){
        68652  +    u.ca.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
        68653  +                               (u8)(u.ca.iSet>=0 ? u.ca.iSet & 0xf : 0xff),
 68605  68654                                  pIn3->u.i);
 68606         -    if( u.bx.exists ){
        68655  +    if( u.ca.exists ){
 68607  68656         pc = pOp->p2 - 1;
 68608  68657         break;
 68609  68658       }
 68610  68659     }
 68611         -  if( u.bx.iSet>=0 ){
        68660  +  if( u.ca.iSet>=0 ){
 68612  68661       sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
 68613  68662     }
 68614  68663     break;
 68615  68664   }
 68616  68665   
 68617  68666   
 68618  68667   #ifndef SQLITE_OMIT_TRIGGER
................................................................................
 68627  68676   ** exception using the RAISE() function. Register P3 contains the address 
 68628  68677   ** of a memory cell in this (the parent) VM that is used to allocate the 
 68629  68678   ** memory required by the sub-vdbe at runtime.
 68630  68679   **
 68631  68680   ** P4 is a pointer to the VM containing the trigger program.
 68632  68681   */
 68633  68682   case OP_Program: {        /* jump */
 68634         -#if 0  /* local variables moved into u.by */
        68683  +#if 0  /* local variables moved into u.cb */
 68635  68684     int nMem;               /* Number of memory registers for sub-program */
 68636  68685     int nByte;              /* Bytes of runtime space required for sub-program */
 68637  68686     Mem *pRt;               /* Register to allocate runtime space */
 68638  68687     Mem *pMem;              /* Used to iterate through memory cells */
 68639  68688     Mem *pEnd;              /* Last memory cell in new array */
 68640  68689     VdbeFrame *pFrame;      /* New vdbe frame to execute in */
 68641  68690     SubProgram *pProgram;   /* Sub-program to execute */
 68642  68691     void *t;                /* Token identifying trigger */
 68643         -#endif /* local variables moved into u.by */
        68692  +#endif /* local variables moved into u.cb */
 68644  68693   
 68645         -  u.by.pProgram = pOp->p4.pProgram;
 68646         -  u.by.pRt = &aMem[pOp->p3];
 68647         -  assert( memIsValid(u.by.pRt) );
 68648         -  assert( u.by.pProgram->nOp>0 );
        68694  +  u.cb.pProgram = pOp->p4.pProgram;
        68695  +  u.cb.pRt = &aMem[pOp->p3];
        68696  +  assert( memIsValid(u.cb.pRt) );
        68697  +  assert( u.cb.pProgram->nOp>0 );
 68649  68698   
 68650  68699     /* If the p5 flag is clear, then recursive invocation of triggers is
 68651  68700     ** disabled for backwards compatibility (p5 is set if this sub-program
 68652  68701     ** is really a trigger, not a foreign key action, and the flag set
 68653  68702     ** and cleared by the "PRAGMA recursive_triggers" command is clear).
 68654  68703     **
 68655  68704     ** It is recursive invocation of triggers, at the SQL level, that is
 68656  68705     ** disabled. In some cases a single trigger may generate more than one
 68657  68706     ** SubProgram (if the trigger may be executed with more than one different
 68658  68707     ** ON CONFLICT algorithm). SubProgram structures associated with a
 68659  68708     ** single trigger all have the same value for the SubProgram.token
 68660  68709     ** variable.  */
 68661  68710     if( pOp->p5 ){
 68662         -    u.by.t = u.by.pProgram->token;
 68663         -    for(u.by.pFrame=p->pFrame; u.by.pFrame && u.by.pFrame->token!=u.by.t; u.by.pFrame=u.by.pFrame->pParent);
 68664         -    if( u.by.pFrame ) break;
        68711  +    u.cb.t = u.cb.pProgram->token;
        68712  +    for(u.cb.pFrame=p->pFrame; u.cb.pFrame && u.cb.pFrame->token!=u.cb.t; u.cb.pFrame=u.cb.pFrame->pParent);
        68713  +    if( u.cb.pFrame ) break;
 68665  68714     }
 68666  68715   
 68667  68716     if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
 68668  68717       rc = SQLITE_ERROR;
 68669  68718       sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
 68670  68719       break;
 68671  68720     }
 68672  68721   
 68673         -  /* Register u.by.pRt is used to store the memory required to save the state
        68722  +  /* Register u.cb.pRt is used to store the memory required to save the state
 68674  68723     ** of the current program, and the memory required at runtime to execute
 68675         -  ** the trigger program. If this trigger has been fired before, then u.by.pRt
        68724  +  ** the trigger program. If this trigger has been fired before, then u.cb.pRt
 68676  68725     ** is already allocated. Otherwise, it must be initialized.  */
 68677         -  if( (u.by.pRt->flags&MEM_Frame)==0 ){
        68726  +  if( (u.cb.pRt->flags&MEM_Frame)==0 ){
 68678  68727       /* SubProgram.nMem is set to the number of memory cells used by the
 68679  68728       ** program stored in SubProgram.aOp. As well as these, one memory
 68680  68729       ** cell is required for each cursor used by the program. Set local
 68681         -    ** variable u.by.nMem (and later, VdbeFrame.nChildMem) to this value.
        68730  +    ** variable u.cb.nMem (and later, VdbeFrame.nChildMem) to this value.
 68682  68731       */
 68683         -    u.by.nMem = u.by.pProgram->nMem + u.by.pProgram->nCsr;
 68684         -    u.by.nByte = ROUND8(sizeof(VdbeFrame))
 68685         -              + u.by.nMem * sizeof(Mem)
 68686         -              + u.by.pProgram->nCsr * sizeof(VdbeCursor *);
 68687         -    u.by.pFrame = sqlite3DbMallocZero(db, u.by.nByte);
 68688         -    if( !u.by.pFrame ){
        68732  +    u.cb.nMem = u.cb.pProgram->nMem + u.cb.pProgram->nCsr;
        68733  +    u.cb.nByte = ROUND8(sizeof(VdbeFrame))
        68734  +              + u.cb.nMem * sizeof(Mem)
        68735  +              + u.cb.pProgram->nCsr * sizeof(VdbeCursor *);
        68736  +    u.cb.pFrame = sqlite3DbMallocZero(db, u.cb.nByte);
        68737  +    if( !u.cb.pFrame ){
 68689  68738         goto no_mem;
 68690  68739       }
 68691         -    sqlite3VdbeMemRelease(u.by.pRt);
 68692         -    u.by.pRt->flags = MEM_Frame;
 68693         -    u.by.pRt->u.pFrame = u.by.pFrame;
 68694         -
 68695         -    u.by.pFrame->v = p;
 68696         -    u.by.pFrame->nChildMem = u.by.nMem;
 68697         -    u.by.pFrame->nChildCsr = u.by.pProgram->nCsr;
 68698         -    u.by.pFrame->pc = pc;
 68699         -    u.by.pFrame->aMem = p->aMem;
 68700         -    u.by.pFrame->nMem = p->nMem;
 68701         -    u.by.pFrame->apCsr = p->apCsr;
 68702         -    u.by.pFrame->nCursor = p->nCursor;
 68703         -    u.by.pFrame->aOp = p->aOp;
 68704         -    u.by.pFrame->nOp = p->nOp;
 68705         -    u.by.pFrame->token = u.by.pProgram->token;
 68706         -
 68707         -    u.by.pEnd = &VdbeFrameMem(u.by.pFrame)[u.by.pFrame->nChildMem];
 68708         -    for(u.by.pMem=VdbeFrameMem(u.by.pFrame); u.by.pMem!=u.by.pEnd; u.by.pMem++){
 68709         -      u.by.pMem->flags = MEM_Null;
 68710         -      u.by.pMem->db = db;
 68711         -    }
 68712         -  }else{
 68713         -    u.by.pFrame = u.by.pRt->u.pFrame;
 68714         -    assert( u.by.pProgram->nMem+u.by.pProgram->nCsr==u.by.pFrame->nChildMem );
 68715         -    assert( u.by.pProgram->nCsr==u.by.pFrame->nChildCsr );
 68716         -    assert( pc==u.by.pFrame->pc );
        68740  +    sqlite3VdbeMemRelease(u.cb.pRt);
        68741  +    u.cb.pRt->flags = MEM_Frame;
        68742  +    u.cb.pRt->u.pFrame = u.cb.pFrame;
        68743  +
        68744  +    u.cb.pFrame->v = p;
        68745  +    u.cb.pFrame->nChildMem = u.cb.nMem;
        68746  +    u.cb.pFrame->nChildCsr = u.cb.pProgram->nCsr;
        68747  +    u.cb.pFrame->pc = pc;
        68748  +    u.cb.pFrame->aMem = p->aMem;
        68749  +    u.cb.pFrame->nMem = p->nMem;
        68750  +    u.cb.pFrame->apCsr = p->apCsr;
        68751  +    u.cb.pFrame->nCursor = p->nCursor;
        68752  +    u.cb.pFrame->aOp = p->aOp;
        68753  +    u.cb.pFrame->nOp = p->nOp;
        68754  +    u.cb.pFrame->token = u.cb.pProgram->token;
        68755  +
        68756  +    u.cb.pEnd = &VdbeFrameMem(u.cb.pFrame)[u.cb.pFrame->nChildMem];
        68757  +    for(u.cb.pMem=VdbeFrameMem(u.cb.pFrame); u.cb.pMem!=u.cb.pEnd; u.cb.pMem++){
        68758  +      u.cb.pMem->flags = MEM_Null;
        68759  +      u.cb.pMem->db = db;
        68760  +    }
        68761  +  }else{
        68762  +    u.cb.pFrame = u.cb.pRt->u.pFrame;
        68763  +    assert( u.cb.pProgram->nMem+u.cb.pProgram->nCsr==u.cb.pFrame->nChildMem );
        68764  +    assert( u.cb.pProgram->nCsr==u.cb.pFrame->nChildCsr );
        68765  +    assert( pc==u.cb.pFrame->pc );
 68717  68766     }
 68718  68767   
 68719  68768     p->nFrame++;
 68720         -  u.by.pFrame->pParent = p->pFrame;
 68721         -  u.by.pFrame->lastRowid = lastRowid;
 68722         -  u.by.pFrame->nChange = p->nChange;
        68769  +  u.cb.pFrame->pParent = p->pFrame;
        68770  +  u.cb.pFrame->lastRowid = lastRowid;
        68771  +  u.cb.pFrame->nChange = p->nChange;
 68723  68772     p->nChange = 0;
 68724         -  p->pFrame = u.by.pFrame;
 68725         -  p->aMem = aMem = &VdbeFrameMem(u.by.pFrame)[-1];
 68726         -  p->nMem = u.by.pFrame->nChildMem;
 68727         -  p->nCursor = (u16)u.by.pFrame->nChildCsr;
        68773  +  p->pFrame = u.cb.pFrame;
        68774  +  p->aMem = aMem = &VdbeFrameMem(u.cb.pFrame)[-1];
        68775  +  p->nMem = u.cb.pFrame->nChildMem;
        68776  +  p->nCursor = (u16)u.cb.pFrame->nChildCsr;
 68728  68777     p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
 68729         -  p->aOp = aOp = u.by.pProgram->aOp;
 68730         -  p->nOp = u.by.pProgram->nOp;
        68778  +  p->aOp = aOp = u.cb.pProgram->aOp;
        68779  +  p->nOp = u.cb.pProgram->nOp;
 68731  68780     pc = -1;
 68732  68781   
 68733  68782     break;
 68734  68783   }
 68735  68784   
 68736  68785   /* Opcode: Param P1 P2 * * *
 68737  68786   **
................................................................................
 68742  68791   ** and old.* values.
 68743  68792   **
 68744  68793   ** The address of the cell in the parent frame is determined by adding
 68745  68794   ** the value of the P1 argument to the value of the P1 argument to the
 68746  68795   ** calling OP_Program instruction.
 68747  68796   */
 68748  68797   case OP_Param: {           /* out2-prerelease */
 68749         -#if 0  /* local variables moved into u.bz */
        68798  +#if 0  /* local variables moved into u.cc */
 68750  68799     VdbeFrame *pFrame;
 68751  68800     Mem *pIn;
 68752         -#endif /* local variables moved into u.bz */
 68753         -  u.bz.pFrame = p->pFrame;
 68754         -  u.bz.pIn = &u.bz.pFrame->aMem[pOp->p1 + u.bz.pFrame->aOp[u.bz.pFrame->pc].p1];
 68755         -  sqlite3VdbeMemShallowCopy(pOut, u.bz.pIn, MEM_Ephem);
        68801  +#endif /* local variables moved into u.cc */
        68802  +  u.cc.pFrame = p->pFrame;
        68803  +  u.cc.pIn = &u.cc.pFrame->aMem[pOp->p1 + u.cc.pFrame->aOp[u.cc.pFrame->pc].p1];
        68804  +  sqlite3VdbeMemShallowCopy(pOut, u.cc.pIn, MEM_Ephem);
 68756  68805     break;
 68757  68806   }
 68758  68807   
 68759  68808   #endif /* #ifndef SQLITE_OMIT_TRIGGER */
 68760  68809   
 68761  68810   #ifndef SQLITE_OMIT_FOREIGN_KEY
 68762  68811   /* Opcode: FkCounter P1 P2 * * *
................................................................................
 68804  68853   ** within a sub-program). Set the value of register P1 to the maximum of 
 68805  68854   ** its current value and the value in register P2.
 68806  68855   **
 68807  68856   ** This instruction throws an error if the memory cell is not initially
 68808  68857   ** an integer.
 68809  68858   */
 68810  68859   case OP_MemMax: {        /* in2 */
 68811         -#if 0  /* local variables moved into u.ca */
        68860  +#if 0  /* local variables moved into u.cd */
 68812  68861     Mem *pIn1;
 68813  68862     VdbeFrame *pFrame;
 68814         -#endif /* local variables moved into u.ca */
        68863  +#endif /* local variables moved into u.cd */
 68815  68864     if( p->pFrame ){
 68816         -    for(u.ca.pFrame=p->pFrame; u.ca.pFrame->pParent; u.ca.pFrame=u.ca.pFrame->pParent);
 68817         -    u.ca.pIn1 = &u.ca.pFrame->aMem[pOp->p1];
        68865  +    for(u.cd.pFrame=p->pFrame; u.cd.pFrame->pParent; u.cd.pFrame=u.cd.pFrame->pParent);
        68866  +    u.cd.pIn1 = &u.cd.pFrame->aMem[pOp->p1];
 68818  68867     }else{
 68819         -    u.ca.pIn1 = &aMem[pOp->p1];
        68868  +    u.cd.pIn1 = &aMem[pOp->p1];
 68820  68869     }
 68821         -  assert( memIsValid(u.ca.pIn1) );
 68822         -  sqlite3VdbeMemIntegerify(u.ca.pIn1);
        68870  +  assert( memIsValid(u.cd.pIn1) );
        68871  +  sqlite3VdbeMemIntegerify(u.cd.pIn1);
 68823  68872     pIn2 = &aMem[pOp->p2];
 68824  68873     sqlite3VdbeMemIntegerify(pIn2);
 68825         -  if( u.ca.pIn1->u.i<pIn2->u.i){
 68826         -    u.ca.pIn1->u.i = pIn2->u.i;
        68874  +  if( u.cd.pIn1->u.i<pIn2->u.i){
        68875  +    u.cd.pIn1->u.i = pIn2->u.i;
 68827  68876     }
 68828  68877     break;
 68829  68878   }
 68830  68879   #endif /* SQLITE_OMIT_AUTOINCREMENT */
 68831  68880   
 68832  68881   /* Opcode: IfPos P1 P2 * * *
 68833  68882   **
................................................................................
 68886  68935   ** structure that specifies the function.  Use register
 68887  68936   ** P3 as the accumulator.
 68888  68937   **
 68889  68938   ** The P5 arguments are taken from register P2 and its
 68890  68939   ** successors.
 68891  68940   */
 68892  68941   case OP_AggStep: {
 68893         -#if 0  /* local variables moved into u.cb */
        68942  +#if 0  /* local variables moved into u.ce */
 68894  68943     int n;
 68895  68944     int i;
 68896  68945     Mem *pMem;
 68897  68946     Mem *pRec;
 68898  68947     sqlite3_context ctx;
 68899  68948     sqlite3_value **apVal;
 68900         -#endif /* local variables moved into u.cb */
        68949  +#endif /* local variables moved into u.ce */
 68901  68950   
 68902         -  u.cb.n = pOp->p5;
 68903         -  assert( u.cb.n>=0 );
 68904         -  u.cb.pRec = &aMem[pOp->p2];
 68905         -  u.cb.apVal = p->apArg;
 68906         -  assert( u.cb.apVal || u.cb.n==0 );
 68907         -  for(u.cb.i=0; u.cb.i<u.cb.n; u.cb.i++, u.cb.pRec++){
 68908         -    assert( memIsValid(u.cb.pRec) );
 68909         -    u.cb.apVal[u.cb.i] = u.cb.pRec;
 68910         -    memAboutToChange(p, u.cb.pRec);
 68911         -    sqlite3VdbeMemStoreType(u.cb.pRec);
        68951  +  u.ce.n = pOp->p5;
        68952  +  assert( u.ce.n>=0 );
        68953  +  u.ce.pRec = &aMem[pOp->p2];
        68954  +  u.ce.apVal = p->apArg;
        68955  +  assert( u.ce.apVal || u.ce.n==0 );
        68956  +  for(u.ce.i=0; u.ce.i<u.ce.n; u.ce.i++, u.ce.pRec++){
        68957  +    assert( memIsValid(u.ce.pRec) );
        68958  +    u.ce.apVal[u.ce.i] = u.ce.pRec;
        68959  +    memAboutToChange(p, u.ce.pRec);
        68960  +    sqlite3VdbeMemStoreType(u.ce.pRec);
 68912  68961     }
 68913         -  u.cb.ctx.pFunc = pOp->p4.pFunc;
        68962  +  u.ce.ctx.pFunc = pOp->p4.pFunc;
 68914  68963     assert( pOp->p3>0 && pOp->p3<=p->nMem );
 68915         -  u.cb.ctx.pMem = u.cb.pMem = &aMem[pOp->p3];
 68916         -  u.cb.pMem->n++;
 68917         -  u.cb.ctx.s.flags = MEM_Null;
 68918         -  u.cb.ctx.s.z = 0;
 68919         -  u.cb.ctx.s.zMalloc = 0;
 68920         -  u.cb.ctx.s.xDel = 0;
 68921         -  u.cb.ctx.s.db = db;
 68922         -  u.cb.ctx.isError = 0;
 68923         -  u.cb.ctx.pColl = 0;
 68924         -  if( u.cb.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
        68964  +  u.ce.ctx.pMem = u.ce.pMem = &aMem[pOp->p3];
        68965  +  u.ce.pMem->n++;
        68966  +  u.ce.ctx.s.flags = MEM_Null;
        68967  +  u.ce.ctx.s.z = 0;
        68968  +  u.ce.ctx.s.zMalloc = 0;
        68969  +  u.ce.ctx.s.xDel = 0;
        68970  +  u.ce.ctx.s.db = db;
        68971  +  u.ce.ctx.isError = 0;
        68972  +  u.ce.ctx.pColl = 0;
        68973  +  if( u.ce.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
 68925  68974       assert( pOp>p->aOp );
 68926  68975       assert( pOp[-1].p4type==P4_COLLSEQ );
 68927  68976       assert( pOp[-1].opcode==OP_CollSeq );
 68928         -    u.cb.ctx.pColl = pOp[-1].p4.pColl;
        68977  +    u.ce.ctx.pColl = pOp[-1].p4.pColl;
 68929  68978     }
 68930         -  (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */
 68931         -  if( u.cb.ctx.isError ){
 68932         -    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s));
 68933         -    rc = u.cb.ctx.isError;
        68979  +  (u.ce.ctx.pFunc->xStep)(&u.ce.ctx, u.ce.n, u.ce.apVal); /* IMP: R-24505-23230 */
        68980  +  if( u.ce.ctx.isError ){
        68981  +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ce.ctx.s));
        68982  +    rc = u.ce.ctx.isError;
 68934  68983     }
 68935  68984   
 68936         -  sqlite3VdbeMemRelease(&u.cb.ctx.s);
        68985  +  sqlite3VdbeMemRelease(&u.ce.ctx.s);
 68937  68986   
 68938  68987     break;
 68939  68988   }
 68940  68989   
 68941  68990   /* Opcode: AggFinal P1 P2 * P4 *
 68942  68991   **
 68943  68992   ** Execute the finalizer function for an aggregate.  P1 is
................................................................................
 68947  68996   ** P4 is a pointer to the FuncDef for this function.  The P2
 68948  68997   ** argument is not used by this opcode.  It is only there to disambiguate
 68949  68998   ** functions that can take varying numbers of arguments.  The
 68950  68999   ** P4 argument is only needed for the degenerate case where
 68951  69000   ** the step function was not previously called.
 68952  69001   */
 68953  69002   case OP_AggFinal: {
 68954         -#if 0  /* local variables moved into u.cc */
        69003  +#if 0  /* local variables moved into u.cf */
 68955  69004     Mem *pMem;
 68956         -#endif /* local variables moved into u.cc */
        69005  +#endif /* local variables moved into u.cf */
 68957  69006     assert( pOp->p1>0 && pOp->p1<=p->nMem );
 68958         -  u.cc.pMem = &aMem[pOp->p1];
 68959         -  assert( (u.cc.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
 68960         -  rc = sqlite3VdbeMemFinalize(u.cc.pMem, pOp->p4.pFunc);
        69007  +  u.cf.pMem = &aMem[pOp->p1];
        69008  +  assert( (u.cf.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
        69009  +  rc = sqlite3VdbeMemFinalize(u.cf.pMem, pOp->p4.pFunc);
 68961  69010     if( rc ){
 68962         -    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cc.pMem));
        69011  +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cf.pMem));
 68963  69012     }
 68964         -  sqlite3VdbeChangeEncoding(u.cc.pMem, encoding);
 68965         -  UPDATE_MAX_BLOBSIZE(u.cc.pMem);
 68966         -  if( sqlite3VdbeMemTooBig(u.cc.pMem) ){
        69013  +  sqlite3VdbeChangeEncoding(u.cf.pMem, encoding);
        69014  +  UPDATE_MAX_BLOBSIZE(u.cf.pMem);
        69015  +  if( sqlite3VdbeMemTooBig(u.cf.pMem) ){
 68967  69016       goto too_big;
 68968  69017     }
 68969  69018     break;
 68970  69019   }
 68971  69020   
 68972  69021   #ifndef SQLITE_OMIT_WAL
 68973  69022   /* Opcode: Checkpoint P1 P2 P3 * *
................................................................................
 68978  69027   ** SQLITE_BUSY or not, respectively.  Write the number of pages in the
 68979  69028   ** WAL after the checkpoint into mem[P3+1] and the number of pages
 68980  69029   ** in the WAL that have been checkpointed after the checkpoint
 68981  69030   ** completes into mem[P3+2].  However on an error, mem[P3+1] and
 68982  69031   ** mem[P3+2] are initialized to -1.
 68983  69032   */
 68984  69033   case OP_Checkpoint: {
 68985         -#if 0  /* local variables moved into u.cd */
        69034  +#if 0  /* local variables moved into u.cg */
 68986  69035     int i;                          /* Loop counter */
 68987  69036     int aRes[3];                    /* Results */
 68988  69037     Mem *pMem;                      /* Write results here */
 68989         -#endif /* local variables moved into u.cd */
        69038  +#endif /* local variables moved into u.cg */
 68990  69039   
 68991         -  u.cd.aRes[0] = 0;
 68992         -  u.cd.aRes[1] = u.cd.aRes[2] = -1;
        69040  +  u.cg.aRes[0] = 0;
        69041  +  u.cg.aRes[1] = u.cg.aRes[2] = -1;
 68993  69042     assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
 68994  69043          || pOp->p2==SQLITE_CHECKPOINT_FULL
 68995  69044          || pOp->p2==SQLITE_CHECKPOINT_RESTART
 68996  69045     );
 68997         -  rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cd.aRes[1], &u.cd.aRes[2]);
        69046  +  rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cg.aRes[1], &u.cg.aRes[2]);
 68998  69047     if( rc==SQLITE_BUSY ){
 68999  69048       rc = SQLITE_OK;
 69000         -    u.cd.aRes[0] = 1;
        69049  +    u.cg.aRes[0] = 1;
 69001  69050     }
 69002         -  for(u.cd.i=0, u.cd.pMem = &aMem[pOp->p3]; u.cd.i<3; u.cd.i++, u.cd.pMem++){
 69003         -    sqlite3VdbeMemSetInt64(u.cd.pMem, (i64)u.cd.aRes[u.cd.i]);
        69051  +  for(u.cg.i=0, u.cg.pMem = &aMem[pOp->p3]; u.cg.i<3; u.cg.i++, u.cg.pMem++){
        69052  +    sqlite3VdbeMemSetInt64(u.cg.pMem, (i64)u.cg.aRes[u.cg.i]);
 69004  69053     }
 69005  69054     break;
 69006  69055   };  
 69007  69056   #endif
 69008  69057   
 69009  69058   #ifndef SQLITE_OMIT_PRAGMA
 69010  69059   /* Opcode: JournalMode P1 P2 P3 * P5
................................................................................
 69015  69064   ** operation. No IO is required.
 69016  69065   **
 69017  69066   ** If changing into or out of WAL mode the procedure is more complicated.
 69018  69067   **
 69019  69068   ** Write a string containing the final journal-mode to register P2.
 69020  69069   */
 69021  69070   case OP_JournalMode: {    /* out2-prerelease */
 69022         -#if 0  /* local variables moved into u.ce */
        69071  +#if 0  /* local variables moved into u.ch */
 69023  69072     Btree *pBt;                     /* Btree to change journal mode of */
 69024  69073     Pager *pPager;                  /* Pager associated with pBt */
 69025  69074     int eNew;                       /* New journal mode */
 69026  69075     int eOld;                       /* The old journal mode */
 69027  69076     const char *zFilename;          /* Name of database file for pPager */
 69028         -#endif /* local variables moved into u.ce */
        69077  +#endif /* local variables moved into u.ch */
 69029  69078   
 69030         -  u.ce.eNew = pOp->p3;
 69031         -  assert( u.ce.eNew==PAGER_JOURNALMODE_DELETE
 69032         -       || u.ce.eNew==PAGER_JOURNALMODE_TRUNCATE
 69033         -       || u.ce.eNew==PAGER_JOURNALMODE_PERSIST
 69034         -       || u.ce.eNew==PAGER_JOURNALMODE_OFF
 69035         -       || u.ce.eNew==PAGER_JOURNALMODE_MEMORY
 69036         -       || u.ce.eNew==PAGER_JOURNALMODE_WAL
 69037         -       || u.ce.eNew==PAGER_JOURNALMODE_QUERY
        69079  +  u.ch.eNew = pOp->p3;
        69080  +  assert( u.ch.eNew==PAGER_JOURNALMODE_DELETE
        69081  +       || u.ch.eNew==PAGER_JOURNALMODE_TRUNCATE
        69082  +       || u.ch.eNew==PAGER_JOURNALMODE_PERSIST
        69083  +       || u.ch.eNew==PAGER_JOURNALMODE_OFF
        69084  +       || u.ch.eNew==PAGER_JOURNALMODE_MEMORY
        69085  +       || u.ch.eNew==PAGER_JOURNALMODE_WAL
        69086  +       || u.ch.eNew==PAGER_JOURNALMODE_QUERY
 69038  69087     );
 69039  69088     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 69040  69089   
 69041         -  u.ce.pBt = db->aDb[pOp->p1].pBt;
 69042         -  u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
 69043         -  u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
 69044         -  if( u.ce.eNew==PAGER_JOURNALMODE_QUERY ) u.ce.eNew = u.ce.eOld;
 69045         -  if( !sqlite3PagerOkToChangeJournalMode(u.ce.pPager) ) u.ce.eNew = u.ce.eOld;
        69090  +  u.ch.pBt = db->aDb[pOp->p1].pBt;
        69091  +  u.ch.pPager = sqlite3BtreePager(u.ch.pBt);
        69092  +  u.ch.eOld = sqlite3PagerGetJournalMode(u.ch.pPager);
        69093  +  if( u.ch.eNew==PAGER_JOURNALMODE_QUERY ) u.ch.eNew = u.ch.eOld;
        69094  +  if( !sqlite3PagerOkToChangeJournalMode(u.ch.pPager) ) u.ch.eNew = u.ch.eOld;
 69046  69095   
 69047  69096   #ifndef SQLITE_OMIT_WAL
 69048         -  u.ce.zFilename = sqlite3PagerFilename(u.ce.pPager);
        69097  +  u.ch.zFilename = sqlite3PagerFilename(u.ch.pPager);
 69049  69098   
 69050  69099     /* Do not allow a transition to journal_mode=WAL for a database
 69051  69100     ** in temporary storage or if the VFS does not support shared memory
 69052  69101     */
 69053         -  if( u.ce.eNew==PAGER_JOURNALMODE_WAL
 69054         -   && (u.ce.zFilename[0]==0                         /* Temp file */
 69055         -       || !sqlite3PagerWalSupported(u.ce.pPager))   /* No shared-memory support */
        69102  +  if( u.ch.eNew==PAGER_JOURNALMODE_WAL
        69103  +   && (u.ch.zFilename[0]==0                         /* Temp file */
        69104  +       || !sqlite3PagerWalSupported(u.ch.pPager))   /* No shared-memory support */
 69056  69105     ){
 69057         -    u.ce.eNew = u.ce.eOld;
        69106  +    u.ch.eNew = u.ch.eOld;
 69058  69107     }
 69059  69108   
 69060         -  if( (u.ce.eNew!=u.ce.eOld)
 69061         -   && (u.ce.eOld==PAGER_JOURNALMODE_WAL || u.ce.eNew==PAGER_JOURNALMODE_WAL)
        69109  +  if( (u.ch.eNew!=u.ch.eOld)
        69110  +   && (u.ch.eOld==PAGER_JOURNALMODE_WAL || u.ch.eNew==PAGER_JOURNALMODE_WAL)
 69062  69111     ){
 69063  69112       if( !db->autoCommit || db->activeVdbeCnt>1 ){
 69064  69113         rc = SQLITE_ERROR;
 69065  69114         sqlite3SetString(&p->zErrMsg, db,
 69066  69115             "cannot change %s wal mode from within a transaction",
 69067         -          (u.ce.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
        69116  +          (u.ch.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
 69068  69117         );
 69069  69118         break;
 69070  69119       }else{
 69071  69120   
 69072         -      if( u.ce.eOld==PAGER_JOURNALMODE_WAL ){
        69121  +      if( u.ch.eOld==PAGER_JOURNALMODE_WAL ){
 69073  69122           /* If leaving WAL mode, close the log file. If successful, the call
 69074  69123           ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
 69075  69124           ** file. An EXCLUSIVE lock may still be held on the database file
 69076  69125           ** after a successful return.
 69077  69126           */
 69078         -        rc = sqlite3PagerCloseWal(u.ce.pPager);
        69127  +        rc = sqlite3PagerCloseWal(u.ch.pPager);
 69079  69128           if( rc==SQLITE_OK ){
 69080         -          sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
        69129  +          sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
 69081  69130           }
 69082         -      }else if( u.ce.eOld==PAGER_JOURNALMODE_MEMORY ){
        69131  +      }else if( u.ch.eOld==PAGER_JOURNALMODE_MEMORY ){
 69083  69132           /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
 69084  69133           ** as an intermediate */
 69085         -        sqlite3PagerSetJournalMode(u.ce.pPager, PAGER_JOURNALMODE_OFF);
        69134  +        sqlite3PagerSetJournalMode(u.ch.pPager, PAGER_JOURNALMODE_OFF);
 69086  69135         }
 69087  69136   
 69088  69137         /* Open a transaction on the database file. Regardless of the journal
 69089  69138         ** mode, this transaction always uses a rollback journal.
 69090  69139         */
 69091         -      assert( sqlite3BtreeIsInTrans(u.ce.pBt)==0 );
        69140  +      assert( sqlite3BtreeIsInTrans(u.ch.pBt)==0 );
 69092  69141         if( rc==SQLITE_OK ){
 69093         -        rc = sqlite3BtreeSetVersion(u.ce.pBt, (u.ce.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
        69142  +        rc = sqlite3BtreeSetVersion(u.ch.pBt, (u.ch.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
 69094  69143         }
 69095  69144       }
 69096  69145     }
 69097  69146   #endif /* ifndef SQLITE_OMIT_WAL */
 69098  69147   
 69099  69148     if( rc ){
 69100         -    u.ce.eNew = u.ce.eOld;
        69149  +    u.ch.eNew = u.ch.eOld;
 69101  69150     }
 69102         -  u.ce.eNew = sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
        69151  +  u.ch.eNew = sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
 69103  69152   
 69104  69153     pOut = &aMem[pOp->p2];
 69105  69154     pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 69106         -  pOut->z = (char *)sqlite3JournalModename(u.ce.eNew);
        69155  +  pOut->z = (char *)sqlite3JournalModename(u.ch.eNew);
 69107  69156     pOut->n = sqlite3Strlen30(pOut->z);
 69108  69157     pOut->enc = SQLITE_UTF8;
 69109  69158     sqlite3VdbeChangeEncoding(pOut, encoding);
 69110  69159     break;
 69111  69160   };
 69112  69161   #endif /* SQLITE_OMIT_PRAGMA */
 69113  69162   
................................................................................
 69128  69177   /* Opcode: IncrVacuum P1 P2 * * *
 69129  69178   **
 69130  69179   ** Perform a single step of the incremental vacuum procedure on
 69131  69180   ** the P1 database. If the vacuum has finished, jump to instruction
 69132  69181   ** P2. Otherwise, fall through to the next instruction.
 69133  69182   */
 69134  69183   case OP_IncrVacuum: {        /* jump */
 69135         -#if 0  /* local variables moved into u.cf */
        69184  +#if 0  /* local variables moved into u.ci */
 69136  69185     Btree *pBt;
 69137         -#endif /* local variables moved into u.cf */
        69186  +#endif /* local variables moved into u.ci */
 69138  69187   
 69139  69188     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 69140  69189     assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 69141         -  u.cf.pBt = db->aDb[pOp->p1].pBt;
 69142         -  rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
        69190  +  u.ci.pBt = db->aDb[pOp->p1].pBt;
        69191  +  rc = sqlite3BtreeIncrVacuum(u.ci.pBt);
 69143  69192     if( rc==SQLITE_DONE ){
 69144  69193       pc = pOp->p2 - 1;
 69145  69194       rc = SQLITE_OK;
 69146  69195     }
 69147  69196     break;
 69148  69197   }
 69149  69198   #endif
................................................................................
 69205  69254   ** xBegin method for that table.
 69206  69255   **
 69207  69256   ** Also, whether or not P4 is set, check that this is not being called from
 69208  69257   ** within a callback to a virtual table xSync() method. If it is, the error
 69209  69258   ** code will be set to SQLITE_LOCKED.
 69210  69259   */
 69211  69260   case OP_VBegin: {
 69212         -#if 0  /* local variables moved into u.cg */
        69261  +#if 0  /* local variables moved into u.cj */
 69213  69262     VTable *pVTab;
 69214         -#endif /* local variables moved into u.cg */
 69215         -  u.cg.pVTab = pOp->p4.pVtab;
 69216         -  rc = sqlite3VtabBegin(db, u.cg.pVTab);
 69217         -  if( u.cg.pVTab ) importVtabErrMsg(p, u.cg.pVTab->pVtab);
        69263  +#endif /* local variables moved into u.cj */
        69264  +  u.cj.pVTab = pOp->p4.pVtab;
        69265  +  rc = sqlite3VtabBegin(db, u.cj.pVTab);
        69266  +  if( u.cj.pVTab ) importVtabErrMsg(p, u.cj.pVTab->pVtab);
 69218  69267     break;
 69219  69268   }
 69220  69269   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69221  69270   
 69222  69271   #ifndef SQLITE_OMIT_VIRTUALTABLE
 69223  69272   /* Opcode: VCreate P1 * * P4 *
 69224  69273   **
................................................................................
 69249  69298   /* Opcode: VOpen P1 * * P4 *
 69250  69299   **
 69251  69300   ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 69252  69301   ** P1 is a cursor number.  This opcode opens a cursor to the virtual
 69253  69302   ** table and stores that cursor in P1.
 69254  69303   */
 69255  69304   case OP_VOpen: {
 69256         -#if 0  /* local variables moved into u.ch */
        69305  +#if 0  /* local variables moved into u.ck */
 69257  69306     VdbeCursor *pCur;
 69258  69307     sqlite3_vtab_cursor *pVtabCursor;
 69259  69308     sqlite3_vtab *pVtab;
 69260  69309     sqlite3_module *pModule;
 69261         -#endif /* local variables moved into u.ch */
        69310  +#endif /* local variables moved into u.ck */
 69262  69311   
 69263         -  u.ch.pCur = 0;
 69264         -  u.ch.pVtabCursor = 0;
 69265         -  u.ch.pVtab = pOp->p4.pVtab->pVtab;
 69266         -  u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule;
 69267         -  assert(u.ch.pVtab && u.ch.pModule);
 69268         -  rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor);
 69269         -  importVtabErrMsg(p, u.ch.pVtab);
        69312  +  u.ck.pCur = 0;
        69313  +  u.ck.pVtabCursor = 0;
        69314  +  u.ck.pVtab = pOp->p4.pVtab->pVtab;
        69315  +  u.ck.pModule = (sqlite3_module *)u.ck.pVtab->pModule;
        69316  +  assert(u.ck.pVtab && u.ck.pModule);
        69317  +  rc = u.ck.pModule->xOpen(u.ck.pVtab, &u.ck.pVtabCursor);
        69318  +  importVtabErrMsg(p, u.ck.pVtab);
 69270  69319     if( SQLITE_OK==rc ){
 69271  69320       /* Initialize sqlite3_vtab_cursor base class */
 69272         -    u.ch.pVtabCursor->pVtab = u.ch.pVtab;
        69321  +    u.ck.pVtabCursor->pVtab = u.ck.pVtab;
 69273  69322   
 69274  69323       /* Initialise vdbe cursor object */
 69275         -    u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
 69276         -    if( u.ch.pCur ){
 69277         -      u.ch.pCur->pVtabCursor = u.ch.pVtabCursor;
 69278         -      u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule;
        69324  +    u.ck.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
        69325  +    if( u.ck.pCur ){
        69326  +      u.ck.pCur->pVtabCursor = u.ck.pVtabCursor;
        69327  +      u.ck.pCur->pModule = u.ck.pVtabCursor->pVtab->pModule;
 69279  69328       }else{
 69280  69329         db->mallocFailed = 1;
 69281         -      u.ch.pModule->xClose(u.ch.pVtabCursor);
        69330  +      u.ck.pModule->xClose(u.ck.pVtabCursor);
 69282  69331       }
 69283  69332     }
 69284  69333     break;
 69285  69334   }
 69286  69335   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69287  69336   
 69288  69337   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 69301  69350   ** xFilter method. Registers P3+2..P3+1+argc are the argc
 69302  69351   ** additional parameters which are passed to
 69303  69352   ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
 69304  69353   **
 69305  69354   ** A jump is made to P2 if the result set after filtering would be empty.
 69306  69355   */
 69307  69356   case OP_VFilter: {   /* jump */
 69308         -#if 0  /* local variables moved into u.ci */
        69357  +#if 0  /* local variables moved into u.cl */
 69309  69358     int nArg;
 69310  69359     int iQuery;
 69311  69360     const sqlite3_module *pModule;
 69312  69361     Mem *pQuery;
 69313  69362     Mem *pArgc;
 69314  69363     sqlite3_vtab_cursor *pVtabCursor;
 69315  69364     sqlite3_vtab *pVtab;
 69316  69365     VdbeCursor *pCur;
 69317  69366     int res;
 69318  69367     int i;
 69319  69368     Mem **apArg;
 69320         -#endif /* local variables moved into u.ci */
        69369  +#endif /* local variables moved into u.cl */
 69321  69370   
 69322         -  u.ci.pQuery = &aMem[pOp->p3];
 69323         -  u.ci.pArgc = &u.ci.pQuery[1];
 69324         -  u.ci.pCur = p->apCsr[pOp->p1];
 69325         -  assert( memIsValid(u.ci.pQuery) );
 69326         -  REGISTER_TRACE(pOp->p3, u.ci.pQuery);
 69327         -  assert( u.ci.pCur->pVtabCursor );
 69328         -  u.ci.pVtabCursor = u.ci.pCur->pVtabCursor;
 69329         -  u.ci.pVtab = u.ci.pVtabCursor->pVtab;
 69330         -  u.ci.pModule = u.ci.pVtab->pModule;
        69371  +  u.cl.pQuery = &aMem[pOp->p3];
        69372  +  u.cl.pArgc = &u.cl.pQuery[1];
        69373  +  u.cl.pCur = p->apCsr[pOp->p1];
        69374  +  assert( memIsValid(u.cl.pQuery) );
        69375  +  REGISTER_TRACE(pOp->p3, u.cl.pQuery);
        69376  +  assert( u.cl.pCur->pVtabCursor );
        69377  +  u.cl.pVtabCursor = u.cl.pCur->pVtabCursor;
        69378  +  u.cl.pVtab = u.cl.pVtabCursor->pVtab;
        69379  +  u.cl.pModule = u.cl.pVtab->pModule;
 69331  69380   
 69332  69381     /* Grab the index number and argc parameters */
 69333         -  assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int );
 69334         -  u.ci.nArg = (int)u.ci.pArgc->u.i;
 69335         -  u.ci.iQuery = (int)u.ci.pQuery->u.i;
        69382  +  assert( (u.cl.pQuery->flags&MEM_Int)!=0 && u.cl.pArgc->flags==MEM_Int );
        69383  +  u.cl.nArg = (int)u.cl.pArgc->u.i;
        69384  +  u.cl.iQuery = (int)u.cl.pQuery->u.i;
 69336  69385   
 69337  69386     /* Invoke the xFilter method */
 69338  69387     {
 69339         -    u.ci.res = 0;
 69340         -    u.ci.apArg = p->apArg;
 69341         -    for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){
 69342         -      u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1];
 69343         -      sqlite3VdbeMemStoreType(u.ci.apArg[u.ci.i]);
        69388  +    u.cl.res = 0;
        69389  +    u.cl.apArg = p->apArg;
        69390  +    for(u.cl.i = 0; u.cl.i<u.cl.nArg; u.cl.i++){
        69391  +      u.cl.apArg[u.cl.i] = &u.cl.pArgc[u.cl.i+1];
        69392  +      sqlite3VdbeMemStoreType(u.cl.apArg[u.cl.i]);
 69344  69393       }
 69345  69394   
 69346  69395       p->inVtabMethod = 1;
 69347         -    rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg);
        69396  +    rc = u.cl.pModule->xFilter(u.cl.pVtabCursor, u.cl.iQuery, pOp->p4.z, u.cl.nArg, u.cl.apArg);
 69348  69397       p->inVtabMethod = 0;
 69349         -    importVtabErrMsg(p, u.ci.pVtab);
        69398  +    importVtabErrMsg(p, u.cl.pVtab);
 69350  69399       if( rc==SQLITE_OK ){
 69351         -      u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor);
        69400  +      u.cl.res = u.cl.pModule->xEof(u.cl.pVtabCursor);
 69352  69401       }
 69353  69402   
 69354         -    if( u.ci.res ){
        69403  +    if( u.cl.res ){
 69355  69404         pc = pOp->p2 - 1;
 69356  69405       }
 69357  69406     }
 69358         -  u.ci.pCur->nullRow = 0;
        69407  +  u.cl.pCur->nullRow = 0;
 69359  69408   
 69360  69409     break;
 69361  69410   }
 69362  69411   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69363  69412   
 69364  69413   #ifndef SQLITE_OMIT_VIRTUALTABLE
 69365  69414   /* Opcode: VColumn P1 P2 P3 * *
 69366  69415   **
 69367  69416   ** Store the value of the P2-th column of
 69368  69417   ** the row of the virtual-table that the 
 69369  69418   ** P1 cursor is pointing to into register P3.
 69370  69419   */
 69371  69420   case OP_VColumn: {
 69372         -#if 0  /* local variables moved into u.cj */
        69421  +#if 0  /* local variables moved into u.cm */
 69373  69422     sqlite3_vtab *pVtab;
 69374  69423     const sqlite3_module *pModule;
 69375  69424     Mem *pDest;
 69376  69425     sqlite3_context sContext;
 69377         -#endif /* local variables moved into u.cj */
        69426  +#endif /* local variables moved into u.cm */
 69378  69427   
 69379  69428     VdbeCursor *pCur = p->apCsr[pOp->p1];
 69380  69429     assert( pCur->pVtabCursor );
 69381  69430     assert( pOp->p3>0 && pOp->p3<=p->nMem );
 69382         -  u.cj.pDest = &aMem[pOp->p3];
 69383         -  memAboutToChange(p, u.cj.pDest);
        69431  +  u.cm.pDest = &aMem[pOp->p3];
        69432  +  memAboutToChange(p, u.cm.pDest);
 69384  69433     if( pCur->nullRow ){
 69385         -    sqlite3VdbeMemSetNull(u.cj.pDest);
        69434  +    sqlite3VdbeMemSetNull(u.cm.pDest);
 69386  69435       break;
 69387  69436     }
 69388         -  u.cj.pVtab = pCur->pVtabCursor->pVtab;
 69389         -  u.cj.pModule = u.cj.pVtab->pModule;
 69390         -  assert( u.cj.pModule->xColumn );
 69391         -  memset(&u.cj.sContext, 0, sizeof(u.cj.sContext));
        69437  +  u.cm.pVtab = pCur->pVtabCursor->pVtab;
        69438  +  u.cm.pModule = u.cm.pVtab->pModule;
        69439  +  assert( u.cm.pModule->xColumn );
        69440  +  memset(&u.cm.sContext, 0, sizeof(u.cm.sContext));
 69392  69441   
 69393  69442     /* The output cell may already have a buffer allocated. Move
 69394         -  ** the current contents to u.cj.sContext.s so in case the user-function
        69443  +  ** the current contents to u.cm.sContext.s so in case the user-function
 69395  69444     ** can use the already allocated buffer instead of allocating a
 69396  69445     ** new one.
 69397  69446     */
 69398         -  sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest);
 69399         -  MemSetTypeFlag(&u.cj.sContext.s, MEM_Null);
        69447  +  sqlite3VdbeMemMove(&u.cm.sContext.s, u.cm.pDest);
        69448  +  MemSetTypeFlag(&u.cm.sContext.s, MEM_Null);
 69400  69449   
 69401         -  rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
 69402         -  importVtabErrMsg(p, u.cj.pVtab);
 69403         -  if( u.cj.sContext.isError ){
 69404         -    rc = u.cj.sContext.isError;
        69450  +  rc = u.cm.pModule->xColumn(pCur->pVtabCursor, &u.cm.sContext, pOp->p2);
        69451  +  importVtabErrMsg(p, u.cm.pVtab);
        69452  +  if( u.cm.sContext.isError ){
        69453  +    rc = u.cm.sContext.isError;
 69405  69454     }
 69406  69455   
 69407  69456     /* Copy the result of the function to the P3 register. We
 69408  69457     ** do this regardless of whether or not an error occurred to ensure any
 69409         -  ** dynamic allocation in u.cj.sContext.s (a Mem struct) is  released.
        69458  +  ** dynamic allocation in u.cm.sContext.s (a Mem struct) is  released.
 69410  69459     */
 69411         -  sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding);
 69412         -  sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s);
 69413         -  REGISTER_TRACE(pOp->p3, u.cj.pDest);
 69414         -  UPDATE_MAX_BLOBSIZE(u.cj.pDest);
        69460  +  sqlite3VdbeChangeEncoding(&u.cm.sContext.s, encoding);
        69461  +  sqlite3VdbeMemMove(u.cm.pDest, &u.cm.sContext.s);
        69462  +  REGISTER_TRACE(pOp->p3, u.cm.pDest);
        69463  +  UPDATE_MAX_BLOBSIZE(u.cm.pDest);
 69415  69464   
 69416         -  if( sqlite3VdbeMemTooBig(u.cj.pDest) ){
        69465  +  if( sqlite3VdbeMemTooBig(u.cm.pDest) ){
 69417  69466       goto too_big;
 69418  69467     }
 69419  69468     break;
 69420  69469   }
 69421  69470   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69422  69471   
 69423  69472   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 69424  69473   /* Opcode: VNext P1 P2 * * *
 69425  69474   **
 69426  69475   ** Advance virtual table P1 to the next row in its result set and
 69427  69476   ** jump to instruction P2.  Or, if the virtual table has reached
 69428  69477   ** the end of its result set, then fall through to the next instruction.
 69429  69478   */
 69430  69479   case OP_VNext: {   /* jump */
 69431         -#if 0  /* local variables moved into u.ck */
        69480  +#if 0  /* local variables moved into u.cn */
 69432  69481     sqlite3_vtab *pVtab;
 69433  69482     const sqlite3_module *pModule;
 69434  69483     int res;
 69435  69484     VdbeCursor *pCur;
 69436         -#endif /* local variables moved into u.ck */
        69485  +#endif /* local variables moved into u.cn */
 69437  69486   
 69438         -  u.ck.res = 0;
 69439         -  u.ck.pCur = p->apCsr[pOp->p1];
 69440         -  assert( u.ck.pCur->pVtabCursor );
 69441         -  if( u.ck.pCur->nullRow ){
        69487  +  u.cn.res = 0;
        69488  +  u.cn.pCur = p->apCsr[pOp->p1];
        69489  +  assert( u.cn.pCur->pVtabCursor );
        69490  +  if( u.cn.pCur->nullRow ){
 69442  69491       break;
 69443  69492     }
 69444         -  u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab;
 69445         -  u.ck.pModule = u.ck.pVtab->pModule;
 69446         -  assert( u.ck.pModule->xNext );
        69493  +  u.cn.pVtab = u.cn.pCur->pVtabCursor->pVtab;
        69494  +  u.cn.pModule = u.cn.pVtab->pModule;
        69495  +  assert( u.cn.pModule->xNext );
 69447  69496   
 69448  69497     /* Invoke the xNext() method of the module. There is no way for the
 69449  69498     ** underlying implementation to return an error if one occurs during
 69450  69499     ** xNext(). Instead, if an error occurs, true is returned (indicating that
 69451  69500     ** data is available) and the error code returned when xColumn or
 69452  69501     ** some other method is next invoked on the save virtual table cursor.
 69453  69502     */
 69454  69503     p->inVtabMethod = 1;
 69455         -  rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor);
        69504  +  rc = u.cn.pModule->xNext(u.cn.pCur->pVtabCursor);
 69456  69505     p->inVtabMethod = 0;
 69457         -  importVtabErrMsg(p, u.ck.pVtab);
        69506  +  importVtabErrMsg(p, u.cn.pVtab);
 69458  69507     if( rc==SQLITE_OK ){
 69459         -    u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor);
        69508  +    u.cn.res = u.cn.pModule->xEof(u.cn.pCur->pVtabCursor);
 69460  69509     }
 69461  69510   
 69462         -  if( !u.ck.res ){
        69511  +  if( !u.cn.res ){
 69463  69512       /* If there is data, jump to P2 */
 69464  69513       pc = pOp->p2 - 1;
 69465  69514     }
 69466  69515     break;
 69467  69516   }
 69468  69517   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 69469  69518   
................................................................................
 69471  69520   /* Opcode: VRename P1 * * P4 *
 69472  69521   **
 69473  69522   ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 69474  69523   ** This opcode invokes the corresponding xRename method. The value
 69475  69524   ** in register P1 is passed as the zName argument to the xRename method.
 69476  69525   */
 69477  69526   case OP_VRename: {
 69478         -#if 0  /* local variables moved into u.cl */
        69527  +#if 0  /* local variables moved into u.co */
 69479  69528     sqlite3_vtab *pVtab;
 69480  69529     Mem *pName;
 69481         -#endif /* local variables moved into u.cl */
        69530  +#endif /* local variables moved into u.co */
 69482  69531   
 69483         -  u.cl.pVtab = pOp->p4.pVtab->pVtab;
 69484         -  u.cl.pName = &aMem[pOp->p1];
 69485         -  assert( u.cl.pVtab->pModule->xRename );
 69486         -  assert( memIsValid(u.cl.pName) );
 69487         -  REGISTER_TRACE(pOp->p1, u.cl.pName);
 69488         -  assert( u.cl.pName->flags & MEM_Str );
 69489         -  rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z);
 69490         -  importVtabErrMsg(p, u.cl.pVtab);
        69532  +  u.co.pVtab = pOp->p4.pVtab->pVtab;
        69533  +  u.co.pName = &aMem[pOp->p1];
        69534  +  assert( u.co.pVtab->pModule->xRename );
        69535  +  assert( memIsValid(u.co.pName) );
        69536  +  REGISTER_TRACE(pOp->p1, u.co.pName);
        69537  +  assert( u.co.pName->flags & MEM_Str );
        69538  +  rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z);
        69539  +  importVtabErrMsg(p, u.co.pVtab);
 69491  69540     p->expired = 0;
 69492  69541   
 69493  69542     break;
 69494  69543   }
 69495  69544   #endif
 69496  69545   
 69497  69546   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 69515  69564   ** a row to delete.
 69516  69565   **
 69517  69566   ** P1 is a boolean flag. If it is set to true and the xUpdate call
 69518  69567   ** is successful, then the value returned by sqlite3_last_insert_rowid() 
 69519  69568   ** is set to the value of the rowid for the row just inserted.
 69520  69569   */
 69521  69570   case OP_VUpdate: {
 69522         -#if 0  /* local variables moved into u.cm */
        69571  +#if 0  /* local variables moved into u.cp */
 69523  69572     sqlite3_vtab *pVtab;
 69524  69573     sqlite3_module *pModule;
 69525  69574     int nArg;
 69526  69575     int i;
 69527  69576     sqlite_int64 rowid;
 69528  69577     Mem **apArg;
 69529  69578     Mem *pX;
 69530         -#endif /* local variables moved into u.cm */
        69579  +#endif /* local variables moved into u.cp */
 69531  69580   
 69532  69581     assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
 69533  69582          || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
 69534  69583     );
 69535         -  u.cm.pVtab = pOp->p4.pVtab->pVtab;
 69536         -  u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
 69537         -  u.cm.nArg = pOp->p2;
        69584  +  u.cp.pVtab = pOp->p4.pVtab->pVtab;
        69585  +  u.cp.pModule = (sqlite3_module *)u.cp.pVtab->pModule;
        69586  +  u.cp.nArg = pOp->p2;
 69538  69587     assert( pOp->p4type==P4_VTAB );
 69539         -  if( ALWAYS(u.cm.pModule->xUpdate) ){
        69588  +  if( ALWAYS(u.cp.pModule->xUpdate) ){
 69540  69589       u8 vtabOnConflict = db->vtabOnConflict;
 69541         -    u.cm.apArg = p->apArg;
 69542         -    u.cm.pX = &aMem[pOp->p3];
 69543         -    for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){
 69544         -      assert( memIsValid(u.cm.pX) );
 69545         -      memAboutToChange(p, u.cm.pX);
 69546         -      sqlite3VdbeMemStoreType(u.cm.pX);
 69547         -      u.cm.apArg[u.cm.i] = u.cm.pX;
 69548         -      u.cm.pX++;
        69590  +    u.cp.apArg = p->apArg;
        69591  +    u.cp.pX = &aMem[pOp->p3];
        69592  +    for(u.cp.i=0; u.cp.i<u.cp.nArg; u.cp.i++){
        69593  +      assert( memIsValid(u.cp.pX) );
        69594  +      memAboutToChange(p, u.cp.pX);
        69595  +      sqlite3VdbeMemStoreType(u.cp.pX);
        69596  +      u.cp.apArg[u.cp.i] = u.cp.pX;
        69597  +      u.cp.pX++;
 69549  69598       }
 69550  69599       db->vtabOnConflict = pOp->p5;
 69551         -    rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid);
        69600  +    rc = u.cp.pModule->xUpdate(u.cp.pVtab, u.cp.nArg, u.cp.apArg, &u.cp.rowid);
 69552  69601       db->vtabOnConflict = vtabOnConflict;
 69553         -    importVtabErrMsg(p, u.cm.pVtab);
        69602  +    importVtabErrMsg(p, u.cp.pVtab);
 69554  69603       if( rc==SQLITE_OK && pOp->p1 ){
 69555         -      assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) );
 69556         -      db->lastRowid = lastRowid = u.cm.rowid;
        69604  +      assert( u.cp.nArg>1 && u.cp.apArg[0] && (u.cp.apArg[0]->flags&MEM_Null) );
        69605  +      db->lastRowid = lastRowid = u.cp.rowid;
 69557  69606       }
 69558  69607       if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
 69559  69608         if( pOp->p5==OE_Ignore ){
 69560  69609           rc = SQLITE_OK;
 69561  69610         }else{
 69562  69611           p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
 69563  69612         }
................................................................................
 69609  69658   #ifndef SQLITE_OMIT_TRACE
 69610  69659   /* Opcode: Trace * * * P4 *
 69611  69660   **
 69612  69661   ** If tracing is enabled (by the sqlite3_trace()) interface, then
 69613  69662   ** the UTF-8 string contained in P4 is emitted on the trace callback.
 69614  69663   */
 69615  69664   case OP_Trace: {
 69616         -#if 0  /* local variables moved into u.cn */
        69665  +#if 0  /* local variables moved into u.cq */
 69617  69666     char *zTrace;
 69618  69667     char *z;
 69619         -#endif /* local variables moved into u.cn */
        69668  +#endif /* local variables moved into u.cq */
 69620  69669   
 69621         -  if( db->xTrace && (u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
 69622         -    u.cn.z = sqlite3VdbeExpandSql(p, u.cn.zTrace);
 69623         -    db->xTrace(db->pTraceArg, u.cn.z);
 69624         -    sqlite3DbFree(db, u.cn.z);
        69670  +  if( db->xTrace && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
        69671  +    u.cq.z = sqlite3VdbeExpandSql(p, u.cq.zTrace);
        69672  +    db->xTrace(db->pTraceArg, u.cq.z);
        69673  +    sqlite3DbFree(db, u.cq.z);
 69625  69674     }
 69626  69675   #ifdef SQLITE_DEBUG
 69627  69676     if( (db->flags & SQLITE_SqlTrace)!=0
 69628         -   && (u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
        69677  +   && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 69629  69678     ){
 69630         -    sqlite3DebugPrintf("SQL-trace: %s\n", u.cn.zTrace);
        69679  +    sqlite3DebugPrintf("SQL-trace: %s\n", u.cq.zTrace);
 69631  69680     }
 69632  69681   #endif /* SQLITE_DEBUG */
 69633  69682     break;
 69634  69683   }
 69635  69684   #endif
 69636  69685   
 69637  69686   
................................................................................
 70026  70075         sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
 70027  70076   
 70028  70077         /* Make sure a mutex is held on the table to be accessed */
 70029  70078         sqlite3VdbeUsesBtree(v, iDb); 
 70030  70079   
 70031  70080         /* Configure the OP_TableLock instruction */
 70032  70081   #ifdef SQLITE_OMIT_SHARED_CACHE
 70033         -      sqlite3VdbeChangeToNoop(v, 2, 1);
        70082  +      sqlite3VdbeChangeToNoop(v, 2);
 70034  70083   #else
 70035  70084         sqlite3VdbeChangeP1(v, 2, iDb);
 70036  70085         sqlite3VdbeChangeP2(v, 2, pTab->tnum);
 70037  70086         sqlite3VdbeChangeP3(v, 2, flags);
 70038  70087         sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
 70039  70088   #endif
 70040  70089   
 70041  70090         /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
 70042  70091         ** parameter of the other to pTab->tnum.  */
 70043         -      sqlite3VdbeChangeToNoop(v, 4 - flags, 1);
        70092  +      sqlite3VdbeChangeToNoop(v, 4 - flags);
 70044  70093         sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
 70045  70094         sqlite3VdbeChangeP3(v, 3 + flags, iDb);
 70046  70095   
 70047  70096         /* Configure the number of columns. Configure the cursor to
 70048  70097         ** think that the table has one more column than it really
 70049  70098         ** does. An OP_Column to retrieve this imaginary column will
 70050  70099         ** always return an SQL NULL. This is useful because it means
................................................................................
 70244  70293   ** memory).
 70245  70294   */
 70246  70295   
 70247  70296   
 70248  70297   #ifndef SQLITE_OMIT_MERGE_SORT
 70249  70298   
 70250  70299   typedef struct VdbeSorterIter VdbeSorterIter;
        70300  +typedef struct SorterRecord SorterRecord;
 70251  70301   
 70252  70302   /*
 70253  70303   ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
 70254  70304   **
 70255  70305   ** As keys are added to the sorter, they are written to disk in a series
 70256  70306   ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
 70257  70307   ** the same as the cache-size allowed for temporary databases. In order
................................................................................
 70315  70365   **     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
 70316  70366   **
 70317  70367   ** In other words, each time we advance to the next sorter element, log2(N)
 70318  70368   ** key comparison operations are required, where N is the number of segments
 70319  70369   ** being merged (rounded up to the next power of 2).
 70320  70370   */
 70321  70371   struct VdbeSorter {
 70322         -  int nWorking;                   /* Start a new b-tree after this many pages */
 70323         -  int nBtree;                     /* Current size of b-tree contents as PMA */
        70372  +  int nInMemory;                  /* Current size of pRecord list as PMA */
 70324  70373     int nTree;                      /* Used size of aTree/aIter (power of 2) */
 70325  70374     VdbeSorterIter *aIter;          /* Array of iterators to merge */
 70326  70375     int *aTree;                     /* Current state of incremental merge */
 70327  70376     i64 iWriteOff;                  /* Current write offset within file pTemp1 */
 70328  70377     i64 iReadOff;                   /* Current read offset within file pTemp1 */
 70329  70378     sqlite3_file *pTemp1;           /* PMA file 1 */
 70330  70379     int nPMA;                       /* Number of PMAs stored in pTemp1 */
        70380  +  SorterRecord *pRecord;          /* Head of in-memory record list */
        70381  +  int mnPmaSize;                  /* Minimum PMA size, in bytes */
        70382  +  int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
        70383  +  UnpackedRecord *pUnpacked;      /* Used to unpack keys */
 70331  70384   };
 70332  70385   
 70333  70386   /*
 70334  70387   ** The following type is an iterator for a PMA. It caches the current key in 
 70335  70388   ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
 70336  70389   */
 70337  70390   struct VdbeSorterIter {
................................................................................
 70339  70392     i64 iEof;                       /* 1 byte past EOF for this iterator */
 70340  70393     sqlite3_file *pFile;            /* File iterator is reading from */
 70341  70394     int nAlloc;                     /* Bytes of space at aAlloc */
 70342  70395     u8 *aAlloc;                     /* Allocated space */
 70343  70396     int nKey;                       /* Number of bytes in key */
 70344  70397     u8 *aKey;                       /* Pointer to current key */
 70345  70398   };
        70399  +
        70400  +/*
        70401  +** A structure to store a single record. All in-memory records are connected
        70402  +** together into a linked list headed at VdbeSorter.pRecord using the 
        70403  +** SorterRecord.pNext pointer.
        70404  +*/
        70405  +struct SorterRecord {
        70406  +  void *pVal;
        70407  +  int nVal;
        70408  +  SorterRecord *pNext;
        70409  +};
 70346  70410   
 70347  70411   /* Minimum allowable value for the VdbeSorter.nWorking variable */
 70348  70412   #define SORTER_MIN_WORKING 10
 70349  70413   
 70350  70414   /* Maximum number of segments to merge in a single pass. */
 70351  70415   #define SORTER_MAX_MERGE_COUNT 16
 70352  70416   
................................................................................
 70368  70432     VdbeSorterIter *pIter           /* Iterator to advance */
 70369  70433   ){
 70370  70434     int rc;                         /* Return Code */
 70371  70435     int nRead;                      /* Number of bytes read */
 70372  70436     int nRec = 0;                   /* Size of record in bytes */
 70373  70437     int iOff = 0;                   /* Size of serialized size varint in bytes */
 70374  70438   
 70375         -  nRead = pIter->iEof - pIter->iReadOff;
 70376         -  if( nRead>5 ) nRead = 5;
        70439  +  assert( pIter->iEof>=pIter->iReadOff );
        70440  +  if( pIter->iEof-pIter->iReadOff>5 ){
        70441  +    nRead = 5;
        70442  +  }else{
        70443  +    nRead = (int)(pIter->iEof - pIter->iReadOff);
        70444  +  }
 70377  70445     if( nRead<=0 ){
 70378  70446       /* This is an EOF condition */
 70379  70447       vdbeSorterIterZero(db, pIter);
 70380  70448       return SQLITE_OK;
 70381  70449     }
 70382  70450   
 70383  70451     rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
................................................................................
 70440  70508   ** (i.e. if no IO error occurs), then *piOffset is set to the offset of
 70441  70509   ** the first byte past the end of the varint before returning. *piVal is
 70442  70510   ** set to the integer value read. If an error occurs, the final values of
 70443  70511   ** both *piOffset and *piVal are undefined.
 70444  70512   */
 70445  70513   static int vdbeSorterReadVarint(
 70446  70514     sqlite3_file *pFile,            /* File to read from */
 70447         -  i64 iEof,                       /* Total number of bytes in file */
 70448  70515     i64 *piOffset,                  /* IN/OUT: Read offset in pFile */
 70449  70516     i64 *piVal                      /* OUT: Value read from file */
 70450  70517   ){
 70451  70518     u8 aVarint[9];                  /* Buffer large enough for a varint */
 70452  70519     i64 iOff = *piOffset;           /* Offset in file to read from */
 70453         -  int nRead = 9;                  /* Number of bytes to read from file */
 70454  70520     int rc;                         /* Return code */
 70455  70521   
 70456         -  assert( iEof>iOff );
 70457         -  if( (iEof-iOff)<nRead ){
 70458         -    nRead = iEof-iOff;
 70459         -  }
 70460         -
 70461         -  rc = sqlite3OsRead(pFile, aVarint, nRead, iOff);
        70522  +  rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
 70462  70523     if( rc==SQLITE_OK ){
 70463  70524       *piOffset += getVarint(aVarint, (u64 *)piVal);
 70464  70525     }
 70465  70526   
 70466  70527     return rc;
 70467  70528   }
 70468  70529   
................................................................................
 70486  70547     pIter->pFile = pSorter->pTemp1;
 70487  70548     pIter->iReadOff = iStart;
 70488  70549     pIter->nAlloc = 128;
 70489  70550     pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
 70490  70551     if( !pIter->aAlloc ){
 70491  70552       rc = SQLITE_NOMEM;
 70492  70553     }else{
 70493         -    i64 iEof = pSorter->iWriteOff;     /* EOF of file pSorter->pTemp1 */
 70494  70554       i64 nByte;                         /* Total size of PMA in bytes */
 70495         -    rc = vdbeSorterReadVarint(pSorter->pTemp1, iEof, &pIter->iReadOff, &nByte);
        70555  +    rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
 70496  70556       *pnByte += nByte;
 70497  70557       pIter->iEof = pIter->iReadOff + nByte;
 70498  70558     }
 70499  70559     if( rc==SQLITE_OK ){
 70500  70560       rc = vdbeSorterIterNext(db, pIter);
 70501  70561     }
 70502  70562     return rc;
 70503  70563   }
 70504  70564   
        70565  +
        70566  +/*
        70567  +** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, 
        70568  +** size nKey2 bytes).  Argument pKeyInfo supplies the collation functions
        70569  +** used by the comparison. If an error occurs, return an SQLite error code.
        70570  +** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
        70571  +** value, depending on whether key1 is smaller, equal to or larger than key2.
        70572  +**
        70573  +** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
        70574  +** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
        70575  +** is true and key1 contains even a single NULL value, it is considered to
        70576  +** be less than key2. Even if key2 also contains NULL values.
        70577  +**
        70578  +** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
        70579  +** has been allocated and contains an unpacked record that is used as key2.
        70580  +*/
        70581  +static void vdbeSorterCompare(
        70582  +  VdbeCursor *pCsr,               /* Cursor object (for pKeyInfo) */
        70583  +  int bOmitRowid,                 /* Ignore rowid field at end of keys */
        70584  +  void *pKey1, int nKey1,         /* Left side of comparison */
        70585  +  void *pKey2, int nKey2,         /* Right side of comparison */
        70586  +  int *pRes                       /* OUT: Result of comparison */
        70587  +){
        70588  +  KeyInfo *pKeyInfo = pCsr->pKeyInfo;
        70589  +  VdbeSorter *pSorter = pCsr->pSorter;
        70590  +  UnpackedRecord *r2 = pSorter->pUnpacked;
        70591  +  int i;
        70592  +
        70593  +  if( pKey2 ){
        70594  +    sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
        70595  +  }
        70596  +
        70597  +  if( bOmitRowid ){
        70598  +    r2->nField = pKeyInfo->nField;
        70599  +    assert( r2->nField>0 );
        70600  +    for(i=0; i<r2->nField; i++){
        70601  +      if( r2->aMem[i].flags & MEM_Null ){
        70602  +        *pRes = -1;
        70603  +        return;
        70604  +      }
        70605  +    }
        70606  +    r2->flags |= UNPACKED_PREFIX_MATCH;
        70607  +  }
        70608  +
        70609  +  *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
        70610  +}
        70611  +
 70505  70612   /*
 70506  70613   ** This function is called to compare two iterator keys when merging 
 70507  70614   ** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
 70508  70615   ** value to recalculate.
 70509  70616   */
 70510  70617   static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
 70511  70618     VdbeSorter *pSorter = pCsr->pSorter;
................................................................................
 70529  70636     p2 = &pSorter->aIter[i2];
 70530  70637   
 70531  70638     if( p1->pFile==0 ){
 70532  70639       iRes = i2;
 70533  70640     }else if( p2->pFile==0 ){
 70534  70641       iRes = i1;
 70535  70642     }else{
 70536         -    char aSpace[150];
 70537         -    UnpackedRecord *r1;
 70538         -
 70539         -    r1 = sqlite3VdbeRecordUnpack(
 70540         -        pCsr->pKeyInfo, p1->nKey, p1->aKey, aSpace, sizeof(aSpace)
        70643  +    int res;
        70644  +    assert( pCsr->pSorter->pUnpacked!=0 );  /* allocated in vdbeSorterMerge() */
        70645  +    vdbeSorterCompare(
        70646  +        pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
 70541  70647       );
 70542         -    if( r1==0 ) return SQLITE_NOMEM;
 70543         -
 70544         -    if( sqlite3VdbeRecordCompare(p2->nKey, p2->aKey, r1)>=0 ){
        70648  +    if( res<=0 ){
 70545  70649         iRes = i1;
 70546  70650       }else{
 70547  70651         iRes = i2;
 70548  70652       }
 70549         -    sqlite3VdbeDeleteUnpackedRecord(r1);
 70550  70653     }
 70551  70654   
 70552  70655     pSorter->aTree[iOut] = iRes;
 70553  70656     return SQLITE_OK;
 70554  70657   }
 70555  70658   
 70556  70659   /*
 70557  70660   ** Initialize the temporary index cursor just opened as a sorter cursor.
 70558  70661   */
 70559  70662   SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
 70560         -  assert( pCsr->pKeyInfo && pCsr->pBt );
 70561         -  pCsr->pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
 70562         -  return (pCsr->pSorter ? SQLITE_OK : SQLITE_NOMEM);
        70663  +  int pgsz;                       /* Page size of main database */
        70664  +  int mxCache;                    /* Cache size */
        70665  +  VdbeSorter *pSorter;            /* The new sorter */
        70666  +  char *d;                        /* Dummy */
        70667  +
        70668  +  assert( pCsr->pKeyInfo && pCsr->pBt==0 );
        70669  +  pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
        70670  +  if( pSorter==0 ){
        70671  +    return SQLITE_NOMEM;
        70672  +  }
        70673  +  
        70674  +  pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
        70675  +  if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
        70676  +  assert( pSorter->pUnpacked==(UnpackedRecord *)d );
        70677  +
        70678  +  if( !sqlite3TempInMemory(db) ){
        70679  +    pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
        70680  +    pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
        70681  +    mxCache = db->aDb[0].pSchema->cache_size;
        70682  +    if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
        70683  +    pSorter->mxPmaSize = mxCache * pgsz;
        70684  +  }
        70685  +
        70686  +  return SQLITE_OK;
        70687  +}
        70688  +
        70689  +/*
        70690  +** Free the list of sorted records starting at pRecord.
        70691  +*/
        70692  +static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
        70693  +  SorterRecord *p;
        70694  +  SorterRecord *pNext;
        70695  +  for(p=pRecord; p; p=pNext){
        70696  +    pNext = p->pNext;
        70697  +    sqlite3DbFree(db, p);
        70698  +  }
 70563  70699   }
 70564  70700   
 70565  70701   /*
 70566  70702   ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
 70567  70703   */
 70568  70704   SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
 70569  70705     VdbeSorter *pSorter = pCsr->pSorter;
................................................................................
 70574  70710           vdbeSorterIterZero(db, &pSorter->aIter[i]);
 70575  70711         }
 70576  70712         sqlite3DbFree(db, pSorter->aIter);
 70577  70713       }
 70578  70714       if( pSorter->pTemp1 ){
 70579  70715         sqlite3OsCloseFree(pSorter->pTemp1);
 70580  70716       }
        70717  +    vdbeSorterRecordFree(db, pSorter->pRecord);
        70718  +    sqlite3DbFree(db, pSorter->pUnpacked);
 70581  70719       sqlite3DbFree(db, pSorter);
 70582  70720       pCsr->pSorter = 0;
 70583  70721     }
 70584  70722   }
 70585  70723   
 70586  70724   /*
 70587  70725   ** Allocate space for a file-handle and open a temporary file. If successful,
................................................................................
 70593  70731     return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
 70594  70732         SQLITE_OPEN_TEMP_JOURNAL |
 70595  70733         SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
 70596  70734         SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &dummy
 70597  70735     );
 70598  70736   }
 70599  70737   
        70738  +/*
        70739  +** Merge the two sorted lists p1 and p2 into a single list.
        70740  +** Set *ppOut to the head of the new list.
        70741  +*/
        70742  +static void vdbeSorterMerge(
        70743  +  VdbeCursor *pCsr,               /* For pKeyInfo */
        70744  +  SorterRecord *p1,               /* First list to merge */
        70745  +  SorterRecord *p2,               /* Second list to merge */
        70746  +  SorterRecord **ppOut            /* OUT: Head of merged list */
        70747  +){
        70748  +  SorterRecord *pFinal = 0;
        70749  +  SorterRecord **pp = &pFinal;
        70750  +  void *pVal2 = p2 ? p2->pVal : 0;
        70751  +
        70752  +  while( p1 && p2 ){
        70753  +    int res;
        70754  +    vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
        70755  +    if( res<=0 ){
        70756  +      *pp = p1;
        70757  +      pp = &p1->pNext;
        70758  +      p1 = p1->pNext;
        70759  +      pVal2 = 0;
        70760  +    }else{
        70761  +      *pp = p2;
        70762  +       pp = &p2->pNext;
        70763  +      p2 = p2->pNext;
        70764  +      if( p2==0 ) break;
        70765  +      pVal2 = p2->pVal;
        70766  +    }
        70767  +  }
        70768  +  *pp = p1 ? p1 : p2;
        70769  +  *ppOut = pFinal;
        70770  +}
 70600  70771   
 70601  70772   /*
 70602         -** Write the current contents of the b-tree to a PMA. Return SQLITE_OK
 70603         -** if successful, or an SQLite error code otherwise.
        70773  +** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
        70774  +** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
        70775  +** occurs.
        70776  +*/
        70777  +static int vdbeSorterSort(VdbeCursor *pCsr){
        70778  +  int i;
        70779  +  SorterRecord **aSlot;
        70780  +  SorterRecord *p;
        70781  +  VdbeSorter *pSorter = pCsr->pSorter;
        70782  +
        70783  +  aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
        70784  +  if( !aSlot ){
        70785  +    return SQLITE_NOMEM;
        70786  +  }
        70787  +
        70788  +  p = pSorter->pRecord;
        70789  +  while( p ){
        70790  +    SorterRecord *pNext = p->pNext;
        70791  +    p->pNext = 0;
        70792  +    for(i=0; aSlot[i]; i++){
        70793  +      vdbeSorterMerge(pCsr, p, aSlot[i], &p);
        70794  +      aSlot[i] = 0;
        70795  +    }
        70796  +    aSlot[i] = p;
        70797  +    p = pNext;
        70798  +  }
        70799  +
        70800  +  p = 0;
        70801  +  for(i=0; i<64; i++){
        70802  +    vdbeSorterMerge(pCsr, p, aSlot[i], &p);
        70803  +  }
        70804  +  pSorter->pRecord = p;
        70805  +
        70806  +  sqlite3_free(aSlot);
        70807  +  return SQLITE_OK;
        70808  +}
        70809  +
        70810  +
        70811  +/*
        70812  +** Write the current contents of the in-memory linked-list to a PMA. Return
        70813  +** SQLITE_OK if successful, or an SQLite error code otherwise.
 70604  70814   **
 70605  70815   ** The format of a PMA is:
 70606  70816   **
 70607  70817   **     * A varint. This varint contains the total number of bytes of content
 70608  70818   **       in the PMA (not including the varint itself).
 70609  70819   **
 70610  70820   **     * One or more records packed end-to-end in order of ascending keys. 
 70611  70821   **       Each record consists of a varint followed by a blob of data (the 
 70612  70822   **       key). The varint is the number of bytes in the blob of data.
 70613  70823   */
 70614         -static int vdbeSorterBtreeToPMA(sqlite3 *db, VdbeCursor *pCsr){
        70824  +static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
 70615  70825     int rc = SQLITE_OK;             /* Return code */
 70616  70826     VdbeSorter *pSorter = pCsr->pSorter;
 70617         -  int res = 0;
 70618  70827   
 70619         -  /* sqlite3BtreeFirst() cannot fail because sorter btrees are always held
 70620         -  ** in memory and so an I/O error is not possible. */
 70621         -  rc = sqlite3BtreeFirst(pCsr->pCursor, &res);
 70622         -  if( NEVER(rc!=SQLITE_OK) || res ) return rc;
 70623         -  assert( pSorter->nBtree>0 );
        70828  +  if( pSorter->nInMemory==0 ){
        70829  +    assert( pSorter->pRecord==0 );
        70830  +    return rc;
        70831  +  }
        70832  +
        70833  +  rc = vdbeSorterSort(pCsr);
 70624  70834   
 70625  70835     /* If the first temporary PMA file has not been opened, open it now. */
 70626         -  if( pSorter->pTemp1==0 ){
        70836  +  if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
 70627  70837       rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
 70628  70838       assert( rc!=SQLITE_OK || pSorter->pTemp1 );
 70629  70839       assert( pSorter->iWriteOff==0 );
 70630  70840       assert( pSorter->nPMA==0 );
 70631  70841     }
 70632  70842   
 70633  70843     if( rc==SQLITE_OK ){
 70634         -    i64 iWriteOff = pSorter->iWriteOff;
 70635         -    void *aMalloc = 0;            /* Array used to hold a single record */
 70636         -    int nMalloc = 0;              /* Allocated size of aMalloc[] in bytes */
        70844  +    i64 iOff = pSorter->iWriteOff;
        70845  +    SorterRecord *p;
        70846  +    SorterRecord *pNext = 0;
        70847  +    static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 70637  70848   
 70638  70849       pSorter->nPMA++;
 70639         -    for(
 70640         -      rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nBtree, &iWriteOff);
 70641         -      rc==SQLITE_OK && res==0;
 70642         -      rc = sqlite3BtreeNext(pCsr->pCursor, &res)
 70643         -    ){
 70644         -      i64 nKey;                   /* Size of this key in bytes */
        70850  +    rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
        70851  +    for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
        70852  +      pNext = p->pNext;
        70853  +      rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
 70645  70854   
 70646         -      /* Write the size of the record in bytes to the output file */
 70647         -      (void)sqlite3BtreeKeySize(pCsr->pCursor, &nKey);
 70648         -      rc = vdbeSorterWriteVarint(pSorter->pTemp1, nKey, &iWriteOff);
 70649         -
 70650         -      /* Make sure the aMalloc[] buffer is large enough for the record */
 70651         -      if( rc==SQLITE_OK && nKey>nMalloc ){
 70652         -        aMalloc = sqlite3DbReallocOrFree(db, aMalloc, nKey);
 70653         -        if( !aMalloc ){ 
 70654         -          rc = SQLITE_NOMEM; 
 70655         -        }else{
 70656         -          nMalloc = nKey;
 70657         -        }
        70855  +      if( rc==SQLITE_OK ){
        70856  +        rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
        70857  +        iOff += p->nVal;
 70658  70858         }
 70659  70859   
 70660         -      /* Write the record itself to the output file */
 70661         -      if( rc==SQLITE_OK ){
 70662         -        /* sqlite3BtreeKey() cannot fail because sorter btrees held in memory */
 70663         -        rc = sqlite3BtreeKey(pCsr->pCursor, 0, nKey, aMalloc);
 70664         -        if( ALWAYS(rc==SQLITE_OK) ){
 70665         -          rc = sqlite3OsWrite(pSorter->pTemp1, aMalloc, nKey, iWriteOff);
 70666         -          iWriteOff += nKey;
 70667         -        }
 70668         -      }
 70669         -
 70670         -      if( rc!=SQLITE_OK ) break;
        70860  +      sqlite3DbFree(db, p);
 70671  70861       }
 70672  70862   
 70673  70863       /* This assert verifies that unless an error has occurred, the size of 
 70674  70864       ** the PMA on disk is the same as the expected size stored in
 70675         -    ** pSorter->nBtree. */ 
 70676         -    assert( rc!=SQLITE_OK || pSorter->nBtree==(
 70677         -          iWriteOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nBtree)
        70865  +    ** pSorter->nInMemory. */ 
        70866  +    assert( rc!=SQLITE_OK || pSorter->nInMemory==(
        70867  +          iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
 70678  70868       ));
 70679  70869   
 70680         -    pSorter->iWriteOff = iWriteOff;
 70681         -    sqlite3DbFree(db, aMalloc);
 70682         -  }
 70683         -
 70684         -  pSorter->nBtree = 0;
 70685         -  return rc;
 70686         -}
 70687         -
 70688         -/*
 70689         -** This function is called on a sorter cursor by the VDBE before each row 
 70690         -** is inserted into VdbeCursor.pCsr. Argument nKey is the size of the key, in
 70691         -** bytes, about to be inserted.
 70692         -**
 70693         -** If it is determined that the temporary b-tree accessed via VdbeCursor.pCsr
 70694         -** is large enough, its contents are written to a sorted PMA on disk and the
 70695         -** tree emptied. This prevents the b-tree (which must be small enough to
 70696         -** fit entirely in the cache in order to support efficient inserts) from
 70697         -** growing too large.
 70698         -**
 70699         -** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
 70700         -*/
 70701         -SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *db, VdbeCursor *pCsr, int nKey){
 70702         -  int rc = SQLITE_OK;             /* Return code */
        70870  +    pSorter->iWriteOff = iOff;
        70871  +    if( rc==SQLITE_OK ){
        70872  +      /* Terminate each file with 8 extra bytes so that from any offset
        70873  +      ** in the file we can always read 9 bytes without a SHORT_READ error */
        70874  +      rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
        70875  +    }
        70876  +    pSorter->pRecord = p;
        70877  +  }
        70878  +
        70879  +  return rc;
        70880  +}
        70881  +
        70882  +/*
        70883  +** Add a record to the sorter.
        70884  +*/
        70885  +SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
        70886  +  sqlite3 *db,                    /* Database handle */
        70887  +  VdbeCursor *pCsr,               /* Sorter cursor */
        70888  +  Mem *pVal                       /* Memory cell containing record */
        70889  +){
 70703  70890     VdbeSorter *pSorter = pCsr->pSorter;
 70704         -  if( pSorter ){
 70705         -    Pager *pPager = sqlite3BtreePager(pCsr->pBt);
 70706         -    int nPage;                    /* Current size of temporary file in pages */
 70707         -
 70708         -    /* Sorters never spill to disk */
 70709         -    assert( sqlite3PagerFile(pPager)->pMethods==0 );
 70710         -
 70711         -    /* Determine how many pages the temporary b-tree has grown to */
 70712         -    sqlite3PagerPagecount(pPager, &nPage);
 70713         -
 70714         -    /* If pSorter->nWorking is still zero, but the temporary file has been
 70715         -    ** created in the file-system, then the most recent insert into the
 70716         -    ** current b-tree segment probably caused the cache to overflow (it is
 70717         -    ** also possible that sqlite3_release_memory() was called). So set the
 70718         -    ** size of the working set to a little less than the current size of the 
 70719         -    ** file in pages.  */
 70720         -    if( pSorter->nWorking==0 && sqlite3PagerUnderStress(pPager) ){
 70721         -      pSorter->nWorking = nPage-5;
 70722         -      if( pSorter->nWorking<SORTER_MIN_WORKING ){
 70723         -        pSorter->nWorking = SORTER_MIN_WORKING;
 70724         -      }
 70725         -    }
 70726         -
 70727         -    /* If the number of pages used by the current b-tree segment is greater
 70728         -    ** than the size of the working set (VdbeSorter.nWorking), start a new
 70729         -    ** segment b-tree.  */
 70730         -    if( pSorter->nWorking && nPage>=pSorter->nWorking ){
 70731         -      BtCursor *p = pCsr->pCursor;/* Cursor structure to close and reopen */
 70732         -      int iRoot;                  /* Root page of new tree */
 70733         -
 70734         -      /* Copy the current contents of the b-tree into a PMA in sorted order.
 70735         -      ** Close the currently open b-tree cursor. */
 70736         -      rc = vdbeSorterBtreeToPMA(db, pCsr);
 70737         -      sqlite3BtreeCloseCursor(p);
 70738         -
 70739         -      if( rc==SQLITE_OK ){
 70740         -        rc = sqlite3BtreeDropTable(pCsr->pBt, 2, 0);
 70741         -#ifdef SQLITE_DEBUG
 70742         -        sqlite3PagerPagecount(pPager, &nPage);
 70743         -        assert( rc!=SQLITE_OK || nPage==1 );
 70744         -#endif
 70745         -      }
 70746         -      if( rc==SQLITE_OK ){
 70747         -        rc = sqlite3BtreeCreateTable(pCsr->pBt, &iRoot, BTREE_BLOBKEY);
 70748         -      }
 70749         -      if( rc==SQLITE_OK ){
 70750         -        assert( iRoot==2 );
 70751         -        rc = sqlite3BtreeCursor(pCsr->pBt, iRoot, 1, pCsr->pKeyInfo, p);
 70752         -      }
 70753         -    }
 70754         -
 70755         -    pSorter->nBtree += sqlite3VarintLen(nKey) + nKey;
 70756         -  }
        70891  +  int rc = SQLITE_OK;             /* Return Code */
        70892  +  SorterRecord *pNew;             /* New list element */
        70893  +
        70894  +  assert( pSorter );
        70895  +  pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
        70896  +
        70897  +  pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
        70898  +  if( pNew==0 ){
        70899  +    rc = SQLITE_NOMEM;
        70900  +  }else{
        70901  +    pNew->pVal = (void *)&pNew[1];
        70902  +    memcpy(pNew->pVal, pVal->z, pVal->n);
        70903  +    pNew->nVal = pVal->n;
        70904  +    pNew->pNext = pSorter->pRecord;
        70905  +    pSorter->pRecord = pNew;
        70906  +  }
        70907  +
        70908  +  /* See if the contents of the sorter should now be written out. They
        70909  +  ** are written out when either of the following are true:
        70910  +  **
        70911  +  **   * The total memory allocated for the in-memory list is greater 
        70912  +  **     than (page-size * cache-size), or
        70913  +  **
        70914  +  **   * The total memory allocated for the in-memory list is greater 
        70915  +  **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
        70916  +  */
        70917  +  if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
        70918  +        (pSorter->nInMemory>pSorter->mxPmaSize)
        70919  +     || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
        70920  +  )){
        70921  +    rc = vdbeSorterListToPMA(db, pCsr);
        70922  +    pSorter->nInMemory = 0;
        70923  +  }
        70924  +
 70757  70925     return rc;
 70758  70926   }
 70759  70927   
 70760  70928   /*
 70761  70929   ** Helper function for sqlite3VdbeSorterRewind(). 
 70762  70930   */
 70763  70931   static int vdbeSorterInitMerge(
................................................................................
 70799  70967     i64 iWrite2 = 0;                /* Write offset for pTemp2 */
 70800  70968     int nIter;                      /* Number of iterators used */
 70801  70969     int nByte;                      /* Bytes of space required for aIter/aTree */
 70802  70970     int N = 2;                      /* Power of 2 >= nIter */
 70803  70971   
 70804  70972     assert( pSorter );
 70805  70973   
 70806         -  /* Write the current b-tree to a PMA. Close the b-tree cursor. */
 70807         -  rc = vdbeSorterBtreeToPMA(db, pCsr);
 70808         -  sqlite3BtreeCloseCursor(pCsr->pCursor);
 70809         -  if( rc!=SQLITE_OK ) return rc;
        70974  +  /* If no data has been written to disk, then do not do so now. Instead,
        70975  +  ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
        70976  +  ** from the in-memory list.  */
 70810  70977     if( pSorter->nPMA==0 ){
 70811         -    *pbEof = 1;
 70812         -    return SQLITE_OK;
        70978  +    *pbEof = !pSorter->pRecord;
        70979  +    assert( pSorter->aTree==0 );
        70980  +    return vdbeSorterSort(pCsr);
 70813  70981     }
        70982  +
        70983  +  /* Write the current b-tree to a PMA. Close the b-tree cursor. */
        70984  +  rc = vdbeSorterListToPMA(db, pCsr);
        70985  +  if( rc!=SQLITE_OK ) return rc;
 70814  70986   
 70815  70987     /* Allocate space for aIter[] and aTree[]. */
 70816  70988     nIter = pSorter->nPMA;
 70817  70989     if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
 70818  70990     assert( nIter>0 );
 70819  70991     while( N<nIter ) N += N;
 70820  70992     nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
................................................................................
 70894  71066   }
 70895  71067   
 70896  71068   /*
 70897  71069   ** Advance to the next element in the sorter.
 70898  71070   */
 70899  71071   SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
 70900  71072     VdbeSorter *pSorter = pCsr->pSorter;
 70901         -  int iPrev = pSorter->aTree[1];  /* Index of iterator to advance */
 70902         -  int i;                          /* Index of aTree[] to recalculate */
 70903         -  int rc;                         /* Return code */
 70904         -
 70905         -  rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
 70906         -  for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
 70907         -    rc = vdbeSorterDoCompare(pCsr, i);
 70908         -  }
 70909         -
 70910         -  *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
 70911         -  return rc;
        71073  +  int rc;                         /* Return code */
        71074  +
        71075  +  if( pSorter->aTree ){
        71076  +    int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
        71077  +    int i;                        /* Index of aTree[] to recalculate */
        71078  +
        71079  +    rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
        71080  +    for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
        71081  +      rc = vdbeSorterDoCompare(pCsr, i);
        71082  +    }
        71083  +
        71084  +    *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
        71085  +  }else{
        71086  +    SorterRecord *pFree = pSorter->pRecord;
        71087  +    pSorter->pRecord = pFree->pNext;
        71088  +    pFree->pNext = 0;
        71089  +    vdbeSorterRecordFree(db, pFree);
        71090  +    *pbEof = !pSorter->pRecord;
        71091  +    rc = SQLITE_OK;
        71092  +  }
        71093  +  return rc;
        71094  +}
        71095  +
        71096  +/*
        71097  +** Return a pointer to a buffer owned by the sorter that contains the 
        71098  +** current key.
        71099  +*/
        71100  +static void *vdbeSorterRowkey(
        71101  +  VdbeSorter *pSorter,            /* Sorter object */
        71102  +  int *pnKey                      /* OUT: Size of current key in bytes */
        71103  +){
        71104  +  void *pKey;
        71105  +  if( pSorter->aTree ){
        71106  +    VdbeSorterIter *pIter;
        71107  +    pIter = &pSorter->aIter[ pSorter->aTree[1] ];
        71108  +    *pnKey = pIter->nKey;
        71109  +    pKey = pIter->aKey;
        71110  +  }else{
        71111  +    *pnKey = pSorter->pRecord->nVal;
        71112  +    pKey = pSorter->pRecord->pVal;
        71113  +  }
        71114  +  return pKey;
 70912  71115   }
 70913  71116   
 70914  71117   /*
 70915  71118   ** Copy the current sorter key into the memory cell pOut.
 70916  71119   */
 70917  71120   SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
 70918  71121     VdbeSorter *pSorter = pCsr->pSorter;
 70919         -  VdbeSorterIter *pIter;
        71122  +  void *pKey; int nKey;           /* Sorter key to copy into pOut */
 70920  71123   
 70921         -  pIter = &pSorter->aIter[ pSorter->aTree[1] ];
 70922         -
 70923         -  /* Coverage testing note: As things are currently, this call will always
 70924         -  ** succeed. This is because the memory cell passed by the VDBE layer 
 70925         -  ** happens to be the same one as was used to assemble the keys before they
 70926         -  ** were passed to the sorter - meaning it is always large enough for the
 70927         -  ** largest key. But this could change very easily, so we leave the call
 70928         -  ** to sqlite3VdbeMemGrow() in. */
 70929         -  if( NEVER(sqlite3VdbeMemGrow(pOut, pIter->nKey, 0)) ){
        71124  +  pKey = vdbeSorterRowkey(pSorter, &nKey);
        71125  +  if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
 70930  71126       return SQLITE_NOMEM;
 70931  71127     }
 70932         -  pOut->n = pIter->nKey;
        71128  +  pOut->n = nKey;
 70933  71129     MemSetTypeFlag(pOut, MEM_Blob);
 70934         -  memcpy(pOut->z, pIter->aKey, pIter->nKey);
        71130  +  memcpy(pOut->z, pKey, nKey);
 70935  71131   
 70936  71132     return SQLITE_OK;
 70937  71133   }
        71134  +
        71135  +/*
        71136  +** Compare the key in memory cell pVal with the key that the sorter cursor
        71137  +** passed as the first argument currently points to. For the purposes of
        71138  +** the comparison, ignore the rowid field at the end of each record.
        71139  +**
        71140  +** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
        71141  +** Otherwise, set *pRes to a negative, zero or positive value if the
        71142  +** key in pVal is smaller than, equal to or larger than the current sorter
        71143  +** key.
        71144  +*/
        71145  +SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
        71146  +  VdbeCursor *pCsr,               /* Sorter cursor */
        71147  +  Mem *pVal,                      /* Value to compare to current sorter key */
        71148  +  int *pRes                       /* OUT: Result of comparison */
        71149  +){
        71150  +  VdbeSorter *pSorter = pCsr->pSorter;
        71151  +  void *pKey; int nKey;           /* Sorter key to compare pVal with */
        71152  +
        71153  +  pKey = vdbeSorterRowkey(pSorter, &nKey);
        71154  +  vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
        71155  +  return SQLITE_OK;
        71156  +}
 70938  71157   
 70939  71158   #endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
 70940  71159   
 70941  71160   /************** End of vdbesort.c ********************************************/
 70942  71161   /************** Begin file journal.c *****************************************/
 70943  71162   /*
 70944  71163   ** 2007 August 22
................................................................................
 73697  73916       struct SrcList_item *pOldItem = &p->a[i];
 73698  73917       Table *pTab;
 73699  73918       pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
 73700  73919       pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 73701  73920       pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
 73702  73921       pNewItem->jointype = pOldItem->jointype;
 73703  73922       pNewItem->iCursor = pOldItem->iCursor;
 73704         -    pNewItem->isPopulated = pOldItem->isPopulated;
        73923  +    pNewItem->addrFillSub = pOldItem->addrFillSub;
        73924  +    pNewItem->regReturn = pOldItem->regReturn;
 73705  73925       pNewItem->isCorrelated = pOldItem->isCorrelated;
 73706  73926       pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
 73707  73927       pNewItem->notIndexed = pOldItem->notIndexed;
 73708  73928       pNewItem->pIndex = pOldItem->pIndex;
 73709  73929       pTab = pNewItem->pTab = pOldItem->pTab;
 73710  73930       if( pTab ){
 73711  73931         pTab->nRef++;
................................................................................
 74257  74477       ** successful here.
 74258  74478       */
 74259  74479       assert(v);
 74260  74480       if( iCol<0 ){
 74261  74481         int iMem = ++pParse->nMem;
 74262  74482         int iAddr;
 74263  74483   
 74264         -      iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
 74265         -      sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
        74484  +      iAddr = sqlite3VdbeAddOp1(v, OP_Once, iMem);
 74266  74485   
 74267  74486         sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 74268  74487         eType = IN_INDEX_ROWID;
 74269  74488   
 74270  74489         sqlite3VdbeJumpHere(v, iAddr);
 74271  74490       }else{
 74272  74491         Index *pIdx;                         /* Iterator variable */
................................................................................
 74289  74508            && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
 74290  74509           ){
 74291  74510             int iMem = ++pParse->nMem;
 74292  74511             int iAddr;
 74293  74512             char *pKey;
 74294  74513     
 74295  74514             pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
 74296         -          iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
 74297         -          sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
        74515  +          iAddr = sqlite3VdbeAddOp1(v, OP_Once, iMem);
 74298  74516     
 74299  74517             sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
 74300  74518                                  pKey,P4_KEYINFO_HANDOFF);
 74301  74519             VdbeComment((v, "%s", pIdx->zName));
 74302  74520             eType = IN_INDEX_INDEX;
 74303  74521   
 74304  74522             sqlite3VdbeJumpHere(v, iAddr);
................................................................................
 74371  74589   #ifndef SQLITE_OMIT_SUBQUERY
 74372  74590   SQLITE_PRIVATE int sqlite3CodeSubselect(
 74373  74591     Parse *pParse,          /* Parsing context */
 74374  74592     Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
 74375  74593     int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
 74376  74594     int isRowid             /* If true, LHS of IN operator is a rowid */
 74377  74595   ){
 74378         -  int testAddr = 0;                       /* One-time test address */
        74596  +  int testAddr = -1;                      /* One-time test address */
 74379  74597     int rReg = 0;                           /* Register storing resulting */
 74380  74598     Vdbe *v = sqlite3GetVdbe(pParse);
 74381  74599     if( NEVER(v==0) ) return 0;
 74382  74600     sqlite3ExprCachePush(pParse);
 74383  74601   
 74384  74602     /* This code must be run in its entirety every time it is encountered
 74385  74603     ** if any of the following is true:
................................................................................
 74389  74607     **    *  We are inside a trigger
 74390  74608     **
 74391  74609     ** If all of the above are false, then we can run this code just once
 74392  74610     ** save the results, and reuse the same result on subsequent invocations.
 74393  74611     */
 74394  74612     if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->pTriggerTab ){
 74395  74613       int mem = ++pParse->nMem;
 74396         -    sqlite3VdbeAddOp1(v, OP_If, mem);
 74397         -    testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
 74398         -    assert( testAddr>0 || pParse->db->mallocFailed );
        74614  +    testAddr = sqlite3VdbeAddOp1(v, OP_Once, mem);
 74399  74615     }
 74400  74616   
 74401  74617   #ifndef SQLITE_OMIT_EXPLAIN
 74402  74618     if( pParse->explain==2 ){
 74403  74619       char *zMsg = sqlite3MPrintf(
 74404         -        pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr?"":"CORRELATED ",
        74620  +        pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
 74405  74621           pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
 74406  74622       );
 74407  74623       sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 74408  74624     }
 74409  74625   #endif
 74410  74626   
 74411  74627     switch( pExpr->op ){
................................................................................
 74489  74705             int iValToIns;
 74490  74706   
 74491  74707             /* If the expression is not constant then we will need to
 74492  74708             ** disable the test that was generated above that makes sure
 74493  74709             ** this code only executes once.  Because for a non-constant
 74494  74710             ** expression we need to rerun this code each time.
 74495  74711             */
 74496         -          if( testAddr && !sqlite3ExprIsConstant(pE2) ){
 74497         -            sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
 74498         -            testAddr = 0;
        74712  +          if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
        74713  +            sqlite3VdbeChangeToNoop(v, testAddr);
        74714  +            testAddr = -1;
 74499  74715             }
 74500  74716   
 74501  74717             /* Evaluate the expression and insert it into the temp table */
 74502  74718             if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
 74503  74719               sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
 74504  74720             }else{
 74505  74721               r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
................................................................................
 74560  74776         }
 74561  74777         rReg = dest.iParm;
 74562  74778         ExprSetIrreducible(pExpr);
 74563  74779         break;
 74564  74780       }
 74565  74781     }
 74566  74782   
 74567         -  if( testAddr ){
 74568         -    sqlite3VdbeJumpHere(v, testAddr-1);
        74783  +  if( testAddr>=0 ){
        74784  +    sqlite3VdbeJumpHere(v, testAddr);
 74569  74785     }
 74570  74786     sqlite3ExprCachePop(pParse, 1);
 74571  74787   
 74572  74788     return rReg;
 74573  74789   }
 74574  74790   #endif /* SQLITE_OMIT_SUBQUERY */
 74575  74791   
................................................................................
 75083  75299         AggInfo *pAggInfo = pExpr->pAggInfo;
 75084  75300         struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
 75085  75301         if( !pAggInfo->directMode ){
 75086  75302           assert( pCol->iMem>0 );
 75087  75303           inReg = pCol->iMem;
 75088  75304           break;
 75089  75305         }else if( pAggInfo->useSortingIdx ){
 75090         -        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
        75306  +        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
 75091  75307                                 pCol->iSorterColumn, target);
 75092  75308           break;
 75093  75309         }
 75094  75310         /* Otherwise, fall thru into the TK_COLUMN case */
 75095  75311       }
 75096  75312       case TK_COLUMN: {
 75097  75313         if( pExpr->iTable<0 ){
................................................................................
 81241  81457   */
 81242  81458   static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
 81243  81459     Table *pTab = pIndex->pTable;  /* The table that is indexed */
 81244  81460     int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
 81245  81461     int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
 81246  81462     int iSorter = iTab;            /* Cursor opened by OpenSorter (if in use) */
 81247  81463     int addr1;                     /* Address of top of loop */
        81464  +  int addr2;                     /* Address to jump to for next iteration */
 81248  81465     int tnum;                      /* Root page of index */
 81249  81466     Vdbe *v;                       /* Generate code into this virtual machine */
 81250  81467     KeyInfo *pKey;                 /* KeyInfo for index */
 81251  81468     int regIdxKey;                 /* Registers containing the index key */
 81252  81469     int regRecord;                 /* Register holding assemblied index record */
 81253  81470     sqlite3 *db = pParse->db;      /* The database connection */
 81254  81471     int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 81255  81472   
 81256         -  /* Set bUseSorter to use OP_OpenSorter, or clear it to insert directly 
 81257         -  ** into the index. The sorter is used unless either OMIT_MERGE_SORT is
 81258         -  ** defined or the system is configured to store temp files in-memory. */
 81259         -#ifdef SQLITE_OMIT_MERGE_SORT
 81260         -  static const int bUseSorter = 0;
 81261         -#else
 81262         -  const int bUseSorter = !sqlite3TempInMemory(pParse->db);
 81263         -#endif
 81264         -
 81265  81473   #ifndef SQLITE_OMIT_AUTHORIZATION
 81266  81474     if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
 81267  81475         db->aDb[iDb].zName ) ){
 81268  81476       return;
 81269  81477     }
 81270  81478   #endif
 81271  81479   
................................................................................
 81283  81491     pKey = sqlite3IndexKeyinfo(pParse, pIndex);
 81284  81492     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
 81285  81493                       (char *)pKey, P4_KEYINFO_HANDOFF);
 81286  81494     if( memRootPage>=0 ){
 81287  81495       sqlite3VdbeChangeP5(v, 1);
 81288  81496     }
 81289  81497   
        81498  +#ifndef SQLITE_OMIT_MERGE_SORT
 81290  81499     /* Open the sorter cursor if we are to use one. */
 81291         -  if( bUseSorter ){
 81292         -    iSorter = pParse->nTab++;
 81293         -    sqlite3VdbeAddOp4(v, OP_OpenSorter, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
 81294         -    sqlite3VdbeChangeP5(v, BTREE_SORTER);
 81295         -  }
        81500  +  iSorter = pParse->nTab++;
        81501  +  sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
        81502  +#endif
 81296  81503   
 81297  81504     /* Open the table. Loop through all rows of the table, inserting index
 81298  81505     ** records into the sorter. */
 81299  81506     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 81300  81507     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
        81508  +  addr2 = addr1 + 1;
 81301  81509     regRecord = sqlite3GetTempReg(pParse);
 81302  81510     regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
 81303  81511   
 81304         -  if( bUseSorter ){
 81305         -    sqlite3VdbeAddOp2(v, OP_IdxInsert, iSorter, regRecord);
 81306         -    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
 81307         -    sqlite3VdbeJumpHere(v, addr1);
 81308         -    addr1 = sqlite3VdbeAddOp2(v, OP_Sort, iSorter, 0);
 81309         -    sqlite3VdbeAddOp2(v, OP_RowKey, iSorter, regRecord);
        81512  +#ifndef SQLITE_OMIT_MERGE_SORT
        81513  +  sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
        81514  +  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
        81515  +  sqlite3VdbeJumpHere(v, addr1);
        81516  +  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
        81517  +  if( pIndex->onError!=OE_None ){
        81518  +    int j2 = sqlite3VdbeCurrentAddr(v) + 3;
        81519  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
        81520  +    addr2 = sqlite3VdbeCurrentAddr(v);
        81521  +    sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
        81522  +    sqlite3HaltConstraint(
        81523  +        pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
        81524  +    );
        81525  +  }else{
        81526  +    addr2 = sqlite3VdbeCurrentAddr(v);
 81310  81527     }
 81311         -
        81528  +  sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
        81529  +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
        81530  +  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
        81531  +#else
 81312  81532     if( pIndex->onError!=OE_None ){
 81313  81533       const int regRowid = regIdxKey + pIndex->nColumn;
 81314  81534       const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
 81315  81535       void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
 81316  81536   
 81317  81537       /* The registers accessed by the OP_IsUnique opcode were allocated
 81318  81538       ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
................................................................................
 81323  81543       ** we can be sure that no other temp registers have been allocated
 81324  81544       ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
 81325  81545       */
 81326  81546       sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
 81327  81547       sqlite3HaltConstraint(
 81328  81548           pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
 81329  81549     }
 81330         -  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, bUseSorter);
        81550  +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
 81331  81551     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
        81552  +#endif
 81332  81553     sqlite3ReleaseTempReg(pParse, regRecord);
 81333         -  sqlite3VdbeAddOp2(v, OP_Next, iSorter, addr1+1);
        81554  +  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
 81334  81555     sqlite3VdbeJumpHere(v, addr1);
 81335  81556   
 81336  81557     sqlite3VdbeAddOp1(v, OP_Close, iTab);
 81337  81558     sqlite3VdbeAddOp1(v, OP_Close, iIdx);
 81338  81559     sqlite3VdbeAddOp1(v, OP_Close, iSorter);
 81339  81560   }
 81340  81561   
................................................................................
 92210  92431     pNew->addrOpenEphm[0] = -1;
 92211  92432     pNew->addrOpenEphm[1] = -1;
 92212  92433     pNew->addrOpenEphm[2] = -1;
 92213  92434     if( db->mallocFailed ) {
 92214  92435       clearSelect(db, pNew);
 92215  92436       if( pNew!=&standin ) sqlite3DbFree(db, pNew);
 92216  92437       pNew = 0;
        92438  +  }else{
        92439  +    assert( pNew->pSrc!=0 || pParse->nErr>0 );
 92217  92440     }
 92218  92441     return pNew;
 92219  92442   }
 92220  92443   
 92221  92444   /*
 92222  92445   ** Delete the given Select structure and all of its substructures.
 92223  92446   */
................................................................................
 92540  92763     Select *pSelect,       /* The whole SELECT statement */
 92541  92764     int regData            /* Register holding data to be sorted */
 92542  92765   ){
 92543  92766     Vdbe *v = pParse->pVdbe;
 92544  92767     int nExpr = pOrderBy->nExpr;
 92545  92768     int regBase = sqlite3GetTempRange(pParse, nExpr+2);
 92546  92769     int regRecord = sqlite3GetTempReg(pParse);
        92770  +  int op;
 92547  92771     sqlite3ExprCacheClear(pParse);
 92548  92772     sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
 92549  92773     sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
 92550  92774     sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
 92551  92775     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
 92552         -  sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord);
        92776  +  if( pSelect->selFlags & SF_UseSorter ){
        92777  +    op = OP_SorterInsert;
        92778  +  }else{
        92779  +    op = OP_IdxInsert;
        92780  +  }
        92781  +  sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
 92553  92782     sqlite3ReleaseTempReg(pParse, regRecord);
 92554  92783     sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
 92555  92784     if( pSelect->iLimit ){
 92556  92785       int addr1, addr2;
 92557  92786       int iLimit;
 92558  92787       if( pSelect->iOffset ){
 92559  92788         iLimit = pSelect->iOffset+1;
................................................................................
 93014  93243     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
 93015  93244       pseudoTab = pParse->nTab++;
 93016  93245       sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
 93017  93246       regRowid = 0;
 93018  93247     }else{
 93019  93248       regRowid = sqlite3GetTempReg(pParse);
 93020  93249     }
 93021         -  addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
 93022         -  codeOffset(v, p, addrContinue);
 93023         -  sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
        93250  +  if( p->selFlags & SF_UseSorter ){
        93251  +    int regSortOut = ++pParse->nMem;
        93252  +    int ptab2 = pParse->nTab++;
        93253  +    sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
        93254  +    addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
        93255  +    codeOffset(v, p, addrContinue);
        93256  +    sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
        93257  +    sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
        93258  +    sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
        93259  +  }else{
        93260  +    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
        93261  +    codeOffset(v, p, addrContinue);
        93262  +    sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
        93263  +  }
 93024  93264     switch( eDest ){
 93025  93265       case SRT_Table:
 93026  93266       case SRT_EphemTab: {
 93027  93267         testcase( eDest==SRT_Table );
 93028  93268         testcase( eDest==SRT_EphemTab );
 93029  93269         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
 93030  93270         sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
................................................................................
 93069  93309     }
 93070  93310     sqlite3ReleaseTempReg(pParse, regRow);
 93071  93311     sqlite3ReleaseTempReg(pParse, regRowid);
 93072  93312   
 93073  93313     /* The bottom of the loop
 93074  93314     */
 93075  93315     sqlite3VdbeResolveLabel(v, addrContinue);
 93076         -  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
        93316  +  if( p->selFlags & SF_UseSorter ){
        93317  +    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
        93318  +  }else{
        93319  +    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
        93320  +  }
 93077  93321     sqlite3VdbeResolveLabel(v, addrBreak);
 93078  93322     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
 93079  93323       sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
 93080  93324     }
 93081  93325   }
 93082  93326   
 93083  93327   /*
................................................................................
 95901  96145   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 95902  96146     for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
 95903  96147       struct SrcList_item *pItem = &pTabList->a[i];
 95904  96148       SelectDest dest;
 95905  96149       Select *pSub = pItem->pSelect;
 95906  96150       int isAggSub;
 95907  96151   
 95908         -    if( pSub==0 || pItem->isPopulated ) continue;
        96152  +    if( pSub==0 ) continue;
        96153  +    if( pItem->addrFillSub ){
        96154  +      sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
        96155  +      continue;
        96156  +    }
 95909  96157   
 95910  96158       /* Increment Parse.nHeight by the height of the largest expression
 95911  96159       ** tree refered to by this, the parent select. The child select
 95912  96160       ** may contain expression trees of at most
 95913  96161       ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
 95914  96162       ** more conservative than necessary, but much easier than enforcing
 95915  96163       ** an exact limit.
 95916  96164       */
 95917  96165       pParse->nHeight += sqlite3SelectExprHeight(p);
 95918  96166   
 95919         -    /* Check to see if the subquery can be absorbed into the parent. */
 95920  96167       isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
 95921  96168       if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
        96169  +      /* This subquery can be absorbed into its parent. */
 95922  96170         if( isAggSub ){
 95923  96171           isAgg = 1;
 95924  96172           p->selFlags |= SF_Aggregate;
 95925  96173         }
 95926  96174         i = -1;
 95927  96175       }else{
        96176  +      /* Generate a subroutine that will fill an ephemeral table with
        96177  +      ** the content of this subquery.  pItem->addrFillSub will point
        96178  +      ** to the address of the generated subroutine.  pItem->regReturn
        96179  +      ** is a register allocated to hold the subroutine return address
        96180  +      */
        96181  +      int topAddr;
        96182  +      int onceAddr = 0;
        96183  +      int retAddr;
        96184  +      assert( pItem->addrFillSub==0 );
        96185  +      pItem->regReturn = ++pParse->nMem;
        96186  +      topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
        96187  +      pItem->addrFillSub = topAddr+1;
        96188  +      VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
        96189  +      if( pItem->isCorrelated==0 && pParse->pTriggerTab==0 ){
        96190  +        /* If the subquery is no correlated and if we are not inside of
        96191  +        ** a trigger, then we only need to compute the value of the subquery
        96192  +        ** once. */
        96193  +        int regOnce = ++pParse->nMem;
        96194  +        onceAddr = sqlite3VdbeAddOp1(v, OP_Once, regOnce);
        96195  +      }
 95928  96196         sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
 95929         -      assert( pItem->isPopulated==0 );
 95930  96197         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
 95931  96198         sqlite3Select(pParse, pSub, &dest);
 95932         -      pItem->isPopulated = 1;
 95933  96199         pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
        96200  +      if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
        96201  +      retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
        96202  +      VdbeComment((v, "end %s", pItem->pTab->zName));
        96203  +      sqlite3VdbeChangeP1(v, topAddr, retAddr);
        96204  +
 95934  96205       }
 95935  96206       if( /*pParse->nErr ||*/ db->mallocFailed ){
 95936  96207         goto select_end;
 95937  96208       }
 95938  96209       pParse->nHeight -= sqlite3SelectExprHeight(p);
 95939  96210       pTabList = p->pSrc;
 95940  96211       if( !IgnorableOrderby(pDest) ){
................................................................................
 96035  96306     }
 96036  96307   
 96037  96308     /* Set the limiter.
 96038  96309     */
 96039  96310     iEnd = sqlite3VdbeMakeLabel(v);
 96040  96311     p->nSelectRow = (double)LARGEST_INT64;
 96041  96312     computeLimitRegisters(pParse, p, iEnd);
        96313  +  if( p->iLimit==0 && addrSortIndex>=0 ){
        96314  +    sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
        96315  +    p->selFlags |= SF_UseSorter;
        96316  +  }
 96042  96317   
 96043  96318     /* Open a virtual index to use for the distinct set.
 96044  96319     */
 96045  96320     if( p->selFlags & SF_Distinct ){
 96046  96321       KeyInfo *pKeyInfo;
 96047  96322       distinct = pParse->nTab++;
 96048  96323       pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
................................................................................
 96063  96338       if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
 96064  96339   
 96065  96340       /* If sorting index that was created by a prior OP_OpenEphemeral 
 96066  96341       ** instruction ended up not being needed, then change the OP_OpenEphemeral
 96067  96342       ** into an OP_Noop.
 96068  96343       */
 96069  96344       if( addrSortIndex>=0 && pOrderBy==0 ){
 96070         -      sqlite3VdbeChangeToNoop(v, addrSortIndex, 1);
        96345  +      sqlite3VdbeChangeToNoop(v, addrSortIndex);
 96071  96346         p->addrOpenEphm[2] = -1;
 96072  96347       }
 96073  96348   
 96074  96349       if( pWInfo->eDistinct ){
 96075  96350         VdbeOp *pOp;                /* No longer required OpenEphemeral instr. */
 96076  96351        
 96077  96352         assert( addrDistinctIndex>=0 );
................................................................................
 96129  96404       int iBMem;          /* First Mem address for previous GROUP BY */
 96130  96405       int iUseFlag;       /* Mem address holding flag indicating that at least
 96131  96406                           ** one row of the input to the aggregator has been
 96132  96407                           ** processed */
 96133  96408       int iAbortFlag;     /* Mem address which causes query abort if positive */
 96134  96409       int groupBySort;    /* Rows come from source in GROUP BY order */
 96135  96410       int addrEnd;        /* End of processing for this SELECT */
        96411  +    int sortPTab = 0;   /* Pseudotable used to decode sorting results */
        96412  +    int sortOut = 0;    /* Output register from the sorter */
 96136  96413   
 96137  96414       /* Remove any and all aliases between the result set and the
 96138  96415       ** GROUP BY clause.
 96139  96416       */
 96140  96417       if( pGroupBy ){
 96141  96418         int k;                        /* Loop counter */
 96142  96419         struct ExprList_item *pItem;  /* For looping over expression in a list */
................................................................................
 96190  96467         int addrTopOfLoop;  /* Top of the input loop */
 96191  96468         int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
 96192  96469         int addrReset;      /* Subroutine for resetting the accumulator */
 96193  96470         int regReset;       /* Return address register for reset subroutine */
 96194  96471   
 96195  96472         /* If there is a GROUP BY clause we might need a sorting index to
 96196  96473         ** implement it.  Allocate that sorting index now.  If it turns out
 96197         -      ** that we do not need it after all, the OpenEphemeral instruction
        96474  +      ** that we do not need it after all, the OP_SorterOpen instruction
 96198  96475         ** will be converted into a Noop.  
 96199  96476         */
 96200  96477         sAggInfo.sortingIdx = pParse->nTab++;
 96201  96478         pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
 96202         -      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, 
        96479  +      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
 96203  96480             sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
 96204  96481             0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
 96205  96482   
 96206  96483         /* Initialize memory locations used by GROUP BY aggregate processing
 96207  96484         */
 96208  96485         iUseFlag = ++pParse->nMem;
 96209  96486         iAbortFlag = ++pParse->nMem;
................................................................................
 96276  96553                 sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
 96277  96554               }
 96278  96555               j++;
 96279  96556             }
 96280  96557           }
 96281  96558           regRecord = sqlite3GetTempReg(pParse);
 96282  96559           sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
 96283         -        sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord);
        96560  +        sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
 96284  96561           sqlite3ReleaseTempReg(pParse, regRecord);
 96285  96562           sqlite3ReleaseTempRange(pParse, regBase, nCol);
 96286  96563           sqlite3WhereEnd(pWInfo);
 96287         -        sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
        96564  +        sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
        96565  +        sortOut = sqlite3GetTempReg(pParse);
        96566  +        sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
        96567  +        sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
 96288  96568           VdbeComment((v, "GROUP BY sort"));
 96289  96569           sAggInfo.useSortingIdx = 1;
 96290  96570           sqlite3ExprCacheClear(pParse);
 96291  96571         }
 96292  96572   
 96293  96573         /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
 96294  96574         ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
 96295  96575         ** Then compare the current GROUP BY terms against the GROUP BY terms
 96296  96576         ** from the previous row currently stored in a0, a1, a2...
 96297  96577         */
 96298  96578         addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
 96299  96579         sqlite3ExprCacheClear(pParse);
        96580  +      if( groupBySort ){
        96581  +        sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
        96582  +      }
 96300  96583         for(j=0; j<pGroupBy->nExpr; j++){
 96301  96584           if( groupBySort ){
 96302         -          sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j);
        96585  +          sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
        96586  +          if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 96303  96587           }else{
 96304  96588             sAggInfo.directMode = 1;
 96305  96589             sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
 96306  96590           }
 96307  96591         }
 96308  96592         sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
 96309  96593                             (char*)pKeyInfo, P4_KEYINFO);
................................................................................
 96334  96618         updateAccumulator(pParse, &sAggInfo);
 96335  96619         sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
 96336  96620         VdbeComment((v, "indicate data in accumulator"));
 96337  96621   
 96338  96622         /* End of the loop
 96339  96623         */
 96340  96624         if( groupBySort ){
 96341         -        sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
        96625  +        sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
 96342  96626         }else{
 96343  96627           sqlite3WhereEnd(pWInfo);
 96344         -        sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
        96628  +        sqlite3VdbeChangeToNoop(v, addrSortingIdx);
 96345  96629         }
 96346  96630   
 96347  96631         /* Output the final row of result
 96348  96632         */
 96349  96633         sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
 96350  96634         VdbeComment((v, "output final row"));
 96351  96635   
................................................................................
100523 100807       }
100524 100808     }
100525 100809     return mask;
100526 100810   }
100527 100811   static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
100528 100812     Bitmask mask = 0;
100529 100813     while( pS ){
       100814  +    SrcList *pSrc = pS->pSrc;
100530 100815       mask |= exprListTableUsage(pMaskSet, pS->pEList);
100531 100816       mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
100532 100817       mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
100533 100818       mask |= exprTableUsage(pMaskSet, pS->pWhere);
100534 100819       mask |= exprTableUsage(pMaskSet, pS->pHaving);
       100820  +    if( ALWAYS(pSrc!=0) ){
       100821  +      int i;
       100822  +      for(i=0; i<pSrc->nSrc; i++){
       100823  +        mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
       100824  +        mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
       100825  +      }
       100826  +    }
100535 100827       pS = pS->pPrior;
100536 100828     }
100537 100829     return mask;
100538 100830   }
100539 100831   
100540 100832   /*
100541 100833   ** Return TRUE if the given operator is one of the operators that is
................................................................................
102050 102342     Bitmask extraCols;          /* Bitmap of additional columns */
102051 102343   
102052 102344     /* Generate code to skip over the creation and initialization of the
102053 102345     ** transient index on 2nd and subsequent iterations of the loop. */
102054 102346     v = pParse->pVdbe;
102055 102347     assert( v!=0 );
102056 102348     regIsInit = ++pParse->nMem;
102057         -  addrInit = sqlite3VdbeAddOp1(v, OP_If, regIsInit);
102058         -  sqlite3VdbeAddOp2(v, OP_Integer, 1, regIsInit);
       102349  +  addrInit = sqlite3VdbeAddOp1(v, OP_Once, regIsInit);
102059 102350   
102060 102351     /* Count the number of columns that will be added to the index
102061 102352     ** and used to match WHERE clause constraints */
102062 102353     nColumn = 0;
102063 102354     pTable = pSrc->pTab;
102064 102355     pWCEnd = &pWC->a[pWC->nTerm];
102065 102356     idxCols = 0;
................................................................................
115987 116278     char *p2 = a2;
115988 116279     char *p;
115989 116280     char *aOut;
115990 116281     int bFirstOut = 0;
115991 116282   
115992 116283     *paOut = 0;
115993 116284     *pnOut = 0;
115994         -  aOut = sqlite3_malloc(n1+n2);
       116285  +
       116286  +  /* Allocate space for the output. Both the input and output doclists
       116287  +  ** are delta encoded. If they are in ascending order (bDescDoclist==0),
       116288  +  ** then the first docid in each list is simply encoded as a varint. For
       116289  +  ** each subsequent docid, the varint stored is the difference between the
       116290  +  ** current and previous docid (a positive number - since the list is in
       116291  +  ** ascending order).
       116292  +  **
       116293  +  ** The first docid written to the output is therefore encoded using the 
       116294  +  ** same number of bytes as it is in whichever of the input lists it is
       116295  +  ** read from. And each subsequent docid read from the same input list 
       116296  +  ** consumes either the same or less bytes as it did in the input (since
       116297  +  ** the difference between it and the previous value in the output must
       116298  +  ** be a positive value less than or equal to the delta value read from 
       116299  +  ** the input list). The same argument applies to all but the first docid
       116300  +  ** read from the 'other' list. And to the contents of all position lists
       116301  +  ** that will be copied and merged from the input to the output.
       116302  +  **
       116303  +  ** However, if the first docid copied to the output is a negative number,
       116304  +  ** then the encoding of the first docid from the 'other' input list may
       116305  +  ** be larger in the output than it was in the input (since the delta value
       116306  +  ** may be a larger positive integer than the actual docid).
       116307  +  **
       116308  +  ** The space required to store the output is therefore the sum of the
       116309  +  ** sizes of the two inputs, plus enough space for exactly one of the input
       116310  +  ** docids to grow. 
       116311  +  **
       116312  +  ** A symetric argument may be made if the doclists are in descending 
       116313  +  ** order.
       116314  +  */
       116315  +  aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
115995 116316     if( !aOut ) return SQLITE_NOMEM;
115996 116317   
115997 116318     p = aOut;
115998 116319     fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
115999 116320     fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
116000 116321     while( p1 || p2 ){
116001 116322       sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
................................................................................
116014 116335         fts3PoslistCopy(&p, &p2);
116015 116336         fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
116016 116337       }
116017 116338     }
116018 116339   
116019 116340     *paOut = aOut;
116020 116341     *pnOut = (p-aOut);
       116342  +  assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
116021 116343     return SQLITE_OK;
116022 116344   }
116023 116345   
116024 116346   /*
116025 116347   ** This function does a "phrase" merge of two doclists. In a phrase merge,
116026 116348   ** the output contains a copy of each position from the right-hand input
116027 116349   ** doclist for which there is a position in the left-hand input doclist

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

   105    105   **
   106    106   ** See also: [sqlite3_libversion()],
   107    107   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   108    108   ** [sqlite_version()] and [sqlite_source_id()].
   109    109   */
   110    110   #define SQLITE_VERSION        "3.7.8"
   111    111   #define SQLITE_VERSION_NUMBER 3007008
   112         -#define SQLITE_SOURCE_ID      "2011-08-31 23:57:22 2869ed28299b1c9f355ecc24635830f7f1249126"
          112  +#define SQLITE_SOURCE_ID      "2011-09-17 15:34:50 29c4d0dd43d41650e54824afd70dd40f1b91cc86"
   113    113   
   114    114   /*
   115    115   ** CAPI3REF: Run-Time Library Version Numbers
   116    116   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   117    117   **
   118    118   ** These interfaces provide the same information as the [SQLITE_VERSION],
   119    119   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros

Changes to System.Data.SQLite/SQLiteConnection.cs.

   661    661         get
   662    662         {
   663    663           return _dataSource;
   664    664         }
   665    665       }
   666    666   
   667    667       /// <summary>
   668         -    /// Returns an empty string
          668  +    /// Returns the string "main".
   669    669       /// </summary>
   670    670   #if !PLATFORM_COMPACTFRAMEWORK
   671    671       [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
   672    672   #endif
   673    673       public override string Database
   674    674       {
   675    675         get
................................................................................
   963    963           return SQLiteVersion;
   964    964           //if (_connectionState != ConnectionState.Open)
   965    965           //  throw new InvalidOperationException();
   966    966   
   967    967           //return _sql.Version;
   968    968         }
   969    969       }
          970  +
          971  +    /// <summary>
          972  +    /// Returns the number of rows changed by the last INSERT, UPDATE, or DELETE statement executed on
          973  +    /// this connection.
          974  +    /// </summary>
          975  +#if !PLATFORM_COMPACTFRAMEWORK
          976  +    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
          977  +#endif
          978  +    public int Changes
          979  +    {
          980  +      get
          981  +      {
          982  +        if (_sql == null)
          983  +          throw new InvalidOperationException("Database connection not valid for getting number of changes.");
          984  +
          985  +        return _sql.Changes;
          986  +      }
          987  +    }
   970    988   
   971    989       /// <summary>
   972    990       /// Returns the version of the underlying SQLite database engine
   973    991       /// </summary>
   974    992       public static string SQLiteVersion
   975    993       {
   976    994         get { return SQLite3.SQLiteVersion; }