xref: /sqlite-3.40.0/src/vdbe.c (revision ef5ecb41)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** The code in this file implements execution method of the
13 ** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
14 ** handles housekeeping details such as creating and deleting
15 ** VDBE instances.  This file is solely interested in executing
16 ** the VDBE program.
17 **
18 ** In the external interface, an "sqlite3_stmt*" is an opaque pointer
19 ** to a VDBE.
20 **
21 ** The SQL parser generates a program which is then executed by
22 ** the VDBE to do the work of the SQL statement.  VDBE programs are
23 ** similar in form to assembly language.  The program consists of
24 ** a linear sequence of operations.  Each operation has an opcode
25 ** and 3 operands.  Operands P1 and P2 are integers.  Operand P3
26 ** is a null-terminated string.   The P2 operand must be non-negative.
27 ** Opcodes will typically ignore one or more operands.  Many opcodes
28 ** ignore all three operands.
29 **
30 ** Computation results are stored on a stack.  Each entry on the
31 ** stack is either an integer, a null-terminated string, a floating point
32 ** number, or the SQL "NULL" value.  An inplicit conversion from one
33 ** type to the other occurs as necessary.
34 **
35 ** Most of the code in this file is taken up by the sqlite3VdbeExec()
36 ** function which does the work of interpreting a VDBE program.
37 ** But other routines are also provided to help in building up
38 ** a program instruction by instruction.
39 **
40 ** Various scripts scan this source file in order to generate HTML
41 ** documentation, headers files, or other derived files.  The formatting
42 ** of the code in this file is, therefore, important.  See other comments
43 ** in this file for details.  If in doubt, do not deviate from existing
44 ** commenting and indentation practices when changing or adding code.
45 **
46 ** $Id: vdbe.c,v 1.363 2004/06/09 21:01:11 drh Exp $
47 */
48 #include "sqliteInt.h"
49 #include "os.h"
50 #include <ctype.h>
51 #include "vdbeInt.h"
52 
53 /*
54 ** The following global variable is incremented every time a cursor
55 ** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes.  The test
56 ** procedures use this information to make sure that indices are
57 ** working correctly.  This variable has no function other than to
58 ** help verify the correct operation of the library.
59 */
60 int sqlite3_search_count = 0;
61 
62 /*
63 ** When this global variable is positive, it gets decremented once before
64 ** each instruction in the VDBE.  When reaches zero, the SQLITE_Interrupt
65 ** of the db.flags field is set in order to simulate and interrupt.
66 **
67 ** This facility is used for testing purposes only.  It does not function
68 ** in an ordinary build.
69 */
70 int sqlite3_interrupt_count = 0;
71 
72 /*
73 ** Release the memory associated with the given stack level.  This
74 ** leaves the Mem.flags field in an inconsistent state.
75 */
76 #define Release(P) if((P)->flags&MEM_Dyn){ sqliteFree((P)->z); }
77 
78 /*
79 ** Convert the given stack entity into a string if it isn't one
80 ** already. Return non-zero if a malloc() fails.
81 */
82 #define Stringify(P, enc) \
83    if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
84      { goto no_mem; }
85 
86 /*
87 ** Convert the given stack entity into a string that has been obtained
88 ** from sqliteMalloc().  This is different from Stringify() above in that
89 ** Stringify() will use the NBFS bytes of static string space if the string
90 ** will fit but this routine always mallocs for space.
91 ** Return non-zero if we run out of memory.
92 */
93 #define Dynamicify(P,enc) sqlite3VdbeMemDynamicify(P)
94 
95 
96 /*
97 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
98 ** a pointer to a dynamically allocated string where some other entity
99 ** is responsible for deallocating that string.  Because the stack entry
100 ** does not control the string, it might be deleted without the stack
101 ** entry knowing it.
102 **
103 ** This routine converts an ephemeral string into a dynamically allocated
104 ** string that the stack entry itself controls.  In other words, it
105 ** converts an MEM_Ephem string into an MEM_Dyn string.
106 */
107 #define Deephemeralize(P) \
108    if( ((P)->flags&MEM_Ephem)!=0 \
109        && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
110 
111 /*
112 ** Convert the given stack entity into a integer if it isn't one
113 ** already.
114 **
115 ** Any prior string or real representation is invalidated.
116 ** NULLs are converted into 0.
117 */
118 #define Integerify(P, enc) \
119     if((P)->flags!=MEM_Int){ sqlite3VdbeMemIntegerify(P); }
120 
121 /*
122 ** Get a valid Real representation for the given stack element.
123 **
124 ** Any prior string or integer representation is retained.
125 ** NULLs are converted into 0.0.
126 */
127 #define Realify(P,enc) \
128     if(((P)->flags&MEM_Real)==0){ sqlite3VdbeMemRealify(P); }
129 
130 /*
131 ** Argument pMem points at a memory cell that will be passed to a
132 ** user-defined function or returned to the user as the result of a query.
133 ** The second argument, 'db_enc' is the text encoding used by the vdbe for
134 ** stack variables.  This routine sets the pMem->enc and pMem->type
135 ** variables used by the sqlite3_value_*() routines.
136 */
137 #define storeTypeInfo(A,B) _storeTypeInfo(A)
138 static void _storeTypeInfo(Mem *pMem){
139   int flags = pMem->flags;
140   if( flags & MEM_Null ){
141     pMem->type = SQLITE_NULL;
142   }
143   else if( flags & MEM_Int ){
144     pMem->type = SQLITE_INTEGER;
145   }
146   else if( flags & MEM_Real ){
147     pMem->type = SQLITE_FLOAT;
148   }
149   else if( flags & MEM_Str ){
150     pMem->type = SQLITE_TEXT;
151   }else{
152     pMem->type = SQLITE_BLOB;
153   }
154 }
155 
156 /*
157 ** Insert a new aggregate element and make it the element that
158 ** has focus.
159 **
160 ** Return 0 on success and 1 if memory is exhausted.
161 */
162 static int AggInsert(Agg *p, char *zKey, int nKey){
163   AggElem *pElem, *pOld;
164   int i;
165   Mem *pMem;
166   pElem = sqliteMalloc( sizeof(AggElem) + nKey +
167                         (p->nMem-1)*sizeof(pElem->aMem[0]) );
168   if( pElem==0 ) return 1;
169   pElem->zKey = (char*)&pElem->aMem[p->nMem];
170   memcpy(pElem->zKey, zKey, nKey);
171   pElem->nKey = nKey;
172   pOld = sqlite3HashInsert(&p->hash, pElem->zKey, pElem->nKey, pElem);
173   if( pOld!=0 ){
174     assert( pOld==pElem );  /* Malloc failed on insert */
175     sqliteFree(pOld);
176     return 0;
177   }
178   for(i=0, pMem=pElem->aMem; i<p->nMem; i++, pMem++){
179     pMem->flags = MEM_Null;
180   }
181   p->pCurrent = pElem;
182   return 0;
183 }
184 
185 /*
186 ** Get the AggElem currently in focus
187 */
188 #define AggInFocus(P)   ((P).pCurrent ? (P).pCurrent : _AggInFocus(&(P)))
189 static AggElem *_AggInFocus(Agg *p){
190   HashElem *pElem = sqliteHashFirst(&p->hash);
191   if( pElem==0 ){
192     AggInsert(p,"",1);
193     pElem = sqliteHashFirst(&p->hash);
194   }
195   return pElem ? sqliteHashData(pElem) : 0;
196 }
197 
198 /*
199 ** Pop the stack N times.
200 */
201 static void popStack(Mem **ppTos, int N){
202   Mem *pTos = *ppTos;
203   while( N>0 ){
204     N--;
205     Release(pTos);
206     pTos--;
207   }
208   *ppTos = pTos;
209 }
210 
211 /*
212 ** The parameters are pointers to the head of two sorted lists
213 ** of Sorter structures.  Merge these two lists together and return
214 ** a single sorted list.  This routine forms the core of the merge-sort
215 ** algorithm.
216 **
217 ** In the case of a tie, left sorts in front of right.
218 */
219 static Sorter *Merge(Sorter *pLeft, Sorter *pRight, KeyInfo *pKeyInfo){
220   Sorter sHead;
221   Sorter *pTail;
222   pTail = &sHead;
223   pTail->pNext = 0;
224   while( pLeft && pRight ){
225     int c = sqlite3VdbeRecordCompare(pKeyInfo, pLeft->nKey, pLeft->zKey,
226                                      pRight->nKey, pRight->zKey);
227     if( c<=0 ){
228       pTail->pNext = pLeft;
229       pLeft = pLeft->pNext;
230     }else{
231       pTail->pNext = pRight;
232       pRight = pRight->pNext;
233     }
234     pTail = pTail->pNext;
235   }
236   if( pLeft ){
237     pTail->pNext = pLeft;
238   }else if( pRight ){
239     pTail->pNext = pRight;
240   }
241   return sHead.pNext;
242 }
243 
244 /*
245 ** Make sure there is space in the Vdbe structure to hold at least
246 ** mxCursor cursors.  If there is not currently enough space, then
247 ** allocate more.
248 **
249 ** If a memory allocation error occurs, return 1.  Return 0 if
250 ** everything works.
251 */
252 static int expandCursorArraySize(Vdbe *p, int mxCursor){
253   if( mxCursor>=p->nCursor ){
254     p->apCsr = sqliteRealloc( p->apCsr, (mxCursor+1)*sizeof(Cursor*) );
255     if( p->apCsr==0 ) return 1;
256     while( p->nCursor<=mxCursor ){
257       Cursor *pC;
258       p->apCsr[p->nCursor++] = pC = sqliteMalloc( sizeof(Cursor) );
259       if( pC==0 ) return 1;
260     }
261   }
262   return 0;
263 }
264 
265 /*
266 ** Apply any conversion required by the supplied column affinity to
267 ** memory cell pRec. affinity may be one of:
268 **
269 ** SQLITE_AFF_NUMERIC
270 ** SQLITE_AFF_TEXT
271 ** SQLITE_AFF_NONE
272 ** SQLITE_AFF_INTEGER
273 **
274 */
275 static void applyAffinity(Mem *pRec, char affinity, u8 enc){
276   switch( affinity ){
277     case SQLITE_AFF_INTEGER:
278     case SQLITE_AFF_NUMERIC:
279       if( 0==(pRec->flags&(MEM_Real|MEM_Int)) ){
280         /* pRec does not have a valid integer or real representation.
281         ** Attempt a conversion if pRec has a string representation and
282         ** it looks like a number.
283         */
284         int realnum;
285         sqlite3VdbeMemNulTerminate(pRec);
286         if( pRec->flags&MEM_Str && sqlite3IsNumber(pRec->z, &realnum, enc) ){
287           if( realnum ){
288             Realify(pRec, enc);
289           }else{
290             Integerify(pRec, enc);
291           }
292         }
293       }
294 
295       if( affinity==SQLITE_AFF_INTEGER ){
296         /* For INTEGER affinity, try to convert a real value to an int */
297         if( (pRec->flags&MEM_Real) && !(pRec->flags&MEM_Int) ){
298           pRec->i = pRec->r;
299           if( ((double)pRec->i)==pRec->r ){
300             pRec->flags |= MEM_Int;
301           }
302         }
303       }
304       break;
305 
306     case SQLITE_AFF_TEXT:
307       /* Only attempt the conversion if there is an integer or real
308       ** representation (blob and NULL do not get converted) but no string
309       ** representation.
310       */
311       if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
312         sqlite3VdbeMemStringify(pRec, enc);
313       }
314       pRec->flags &= ~(MEM_Real|MEM_Int);
315 
316       break;
317 
318     case SQLITE_AFF_NONE:
319       /* Affinity NONE. Do nothing. */
320       break;
321 
322     default:
323       assert(0);
324   }
325 }
326 
327 #ifndef NDEBUG
328 /*
329 ** Write a nice string representation of the contents of cell pMem
330 ** into buffer zBuf, length nBuf.
331 */
332 void prettyPrintMem(Mem *pMem, char *zBuf, int nBuf){
333   char *zCsr = zBuf;
334   int f = pMem->flags;
335 
336   if( f&MEM_Blob ){
337     int i;
338     char c;
339     if( f & MEM_Dyn ){
340       c = 'z';
341       assert( (f & (MEM_Static|MEM_Ephem))==0 );
342     }else if( f & MEM_Static ){
343       c = 't';
344       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
345     }else if( f & MEM_Ephem ){
346       c = 'e';
347       assert( (f & (MEM_Static|MEM_Dyn))==0 );
348     }else{
349       c = 's';
350     }
351 
352     zCsr += sprintf(zCsr, "%c", c);
353     zCsr += sprintf(zCsr, "%d[", pMem->n);
354     for(i=0; i<16 && i<pMem->n; i++){
355       zCsr += sprintf(zCsr, "%02X ", ((int)pMem->z[i] & 0xFF));
356     }
357     for(i=0; i<16 && i<pMem->n; i++){
358       char z = pMem->z[i];
359       if( z<32 || z>126 ) *zCsr++ = '.';
360       else *zCsr++ = z;
361     }
362 
363     zCsr += sprintf(zCsr, "]");
364     *zCsr = '\0';
365   }else if( f & MEM_Str ){
366     int j, k;
367     zBuf[0] = ' ';
368     if( f & MEM_Dyn ){
369       zBuf[1] = 'z';
370       assert( (f & (MEM_Static|MEM_Ephem))==0 );
371     }else if( f & MEM_Static ){
372       zBuf[1] = 't';
373       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
374     }else if( f & MEM_Ephem ){
375       zBuf[1] = 'e';
376       assert( (f & (MEM_Static|MEM_Dyn))==0 );
377     }else{
378       zBuf[1] = 's';
379     }
380     k = 2;
381     k += sprintf(&zBuf[k], "%d", pMem->n);
382     zBuf[k++] = '[';
383     for(j=0; j<15 && j<pMem->n; j++){
384       u8 c = pMem->z[j];
385 /*
386       if( c==0 && j==pMem->n-1 ) break;
387             zBuf[k++] = "0123456789ABCDEF"[c>>4];
388             zBuf[k++] = "0123456789ABCDEF"[c&0xf];
389 */
390       if( c>=0x20 && c<0x7f ){
391         zBuf[k++] = c;
392       }else{
393         zBuf[k++] = '.';
394       }
395     }
396     zBuf[k++] = ']';
397     zBuf[k++] = 0;
398   }
399 }
400 
401 /* Temporary - this is useful in conjunction with prettyPrintMem whilst
402 ** debugging.
403 */
404 char zGdbBuf[100];
405 #endif
406 
407 
408 #ifdef VDBE_PROFILE
409 /*
410 ** The following routine only works on pentium-class processors.
411 ** It uses the RDTSC opcode to read cycle count value out of the
412 ** processor and returns that value.  This can be used for high-res
413 ** profiling.
414 */
415 __inline__ unsigned long long int hwtime(void){
416   unsigned long long int x;
417   __asm__("rdtsc\n\t"
418           "mov %%edx, %%ecx\n\t"
419           :"=A" (x));
420   return x;
421 }
422 #endif
423 
424 /*
425 ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
426 ** sqlite3_interrupt() routine has been called.  If it has been, then
427 ** processing of the VDBE program is interrupted.
428 **
429 ** This macro added to every instruction that does a jump in order to
430 ** implement a loop.  This test used to be on every single instruction,
431 ** but that meant we more testing that we needed.  By only testing the
432 ** flag on jump instructions, we get a (small) speed improvement.
433 */
434 #define CHECK_FOR_INTERRUPT \
435    if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt;
436 
437 
438 /*
439 ** Execute as much of a VDBE program as we can then return.
440 **
441 ** sqlite3VdbeMakeReady() must be called before this routine in order to
442 ** close the program with a final OP_Halt and to set up the callbacks
443 ** and the error message pointer.
444 **
445 ** Whenever a row or result data is available, this routine will either
446 ** invoke the result callback (if there is one) or return with
447 ** SQLITE_ROW.
448 **
449 ** If an attempt is made to open a locked database, then this routine
450 ** will either invoke the busy callback (if there is one) or it will
451 ** return SQLITE_BUSY.
452 **
453 ** If an error occurs, an error message is written to memory obtained
454 ** from sqliteMalloc() and p->zErrMsg is made to point to that memory.
455 ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
456 **
457 ** If the callback ever returns non-zero, then the program exits
458 ** immediately.  There will be no error message but the p->rc field is
459 ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
460 **
461 ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
462 ** routine to return SQLITE_ERROR.
463 **
464 ** Other fatal errors return SQLITE_ERROR.
465 **
466 ** After this routine has finished, sqlite3VdbeFinalize() should be
467 ** used to clean up the mess that was left behind.
468 */
469 int sqlite3VdbeExec(
470   Vdbe *p                    /* The VDBE */
471 ){
472   int pc;                    /* The program counter */
473   Op *pOp;                   /* Current operation */
474   int rc = SQLITE_OK;        /* Value to return */
475   sqlite *db = p->db;        /* The database */
476   Mem *pTos;                 /* Top entry in the operand stack */
477   char zBuf[100];            /* Space to sprintf() an integer */
478 #ifdef VDBE_PROFILE
479   unsigned long long start;  /* CPU clock count at start of opcode */
480   int origPc;                /* Program counter at start of opcode */
481 #endif
482 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
483   int nProgressOps = 0;      /* Opcodes executed since progress callback. */
484 #endif
485 
486   if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
487   assert( db->magic==SQLITE_MAGIC_BUSY );
488   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
489   p->rc = SQLITE_OK;
490   assert( p->explain==0 );
491   if( sqlite3_malloc_failed ) goto no_mem;
492   pTos = p->pTos;
493   if( p->popStack ){
494     popStack(&pTos, p->popStack);
495     p->popStack = 0;
496   }
497   p->resOnStack = 0;
498   CHECK_FOR_INTERRUPT;
499   for(pc=p->pc; rc==SQLITE_OK; pc++){
500     assert( pc>=0 && pc<p->nOp );
501     assert( pTos<=&p->aStack[pc] );
502 #ifdef VDBE_PROFILE
503     origPc = pc;
504     start = hwtime();
505 #endif
506     pOp = &p->aOp[pc];
507 
508     /* Only allow tracing if NDEBUG is not defined.
509     */
510 #ifndef NDEBUG
511     if( p->trace ){
512       sqlite3VdbePrintOp(p->trace, pc, pOp);
513     }
514 #endif
515 
516     /* Check to see if we need to simulate an interrupt.  This only happens
517     ** if we have a special test build.
518     */
519 #ifdef SQLITE_TEST
520     if( sqlite3_interrupt_count>0 ){
521       sqlite3_interrupt_count--;
522       if( sqlite3_interrupt_count==0 ){
523         sqlite3_interrupt(db);
524       }
525     }
526 #endif
527 
528 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
529     /* Call the progress callback if it is configured and the required number
530     ** of VDBE ops have been executed (either since this invocation of
531     ** sqlite3VdbeExec() or since last time the progress callback was called).
532     ** If the progress callback returns non-zero, exit the virtual machine with
533     ** a return code SQLITE_ABORT.
534     */
535     if( db->xProgress ){
536       if( db->nProgressOps==nProgressOps ){
537         if( db->xProgress(db->pProgressArg)!=0 ){
538           rc = SQLITE_ABORT;
539           continue; /* skip to the next iteration of the for loop */
540         }
541         nProgressOps = 0;
542       }
543       nProgressOps++;
544     }
545 #endif
546 
547     switch( pOp->opcode ){
548 
549 /*****************************************************************************
550 ** What follows is a massive switch statement where each case implements a
551 ** separate instruction in the virtual machine.  If we follow the usual
552 ** indentation conventions, each case should be indented by 6 spaces.  But
553 ** that is a lot of wasted space on the left margin.  So the code within
554 ** the switch statement will break with convention and be flush-left. Another
555 ** big comment (similar to this one) will mark the point in the code where
556 ** we transition back to normal indentation.
557 **
558 ** The formatting of each case is important.  The makefile for SQLite
559 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
560 ** file looking for lines that begin with "case OP_".  The opcodes.h files
561 ** will be filled with #defines that give unique integer values to each
562 ** opcode and the opcodes.c file is filled with an array of strings where
563 ** each string is the symbolic name for the corresponding opcode.
564 **
565 ** Documentation about VDBE opcodes is generated by scanning this file
566 ** for lines of that contain "Opcode:".  That line and all subsequent
567 ** comment lines are used in the generation of the opcode.html documentation
568 ** file.
569 **
570 ** SUMMARY:
571 **
572 **     Formatting is important to scripts that scan this file.
573 **     Do not deviate from the formatting style currently in use.
574 **
575 *****************************************************************************/
576 
577 /* Opcode:  Goto * P2 *
578 **
579 ** An unconditional jump to address P2.
580 ** The next instruction executed will be
581 ** the one at index P2 from the beginning of
582 ** the program.
583 */
584 case OP_Goto: {
585   CHECK_FOR_INTERRUPT;
586   pc = pOp->p2 - 1;
587   break;
588 }
589 
590 /* Opcode:  Gosub * P2 *
591 **
592 ** Push the current address plus 1 onto the return address stack
593 ** and then jump to address P2.
594 **
595 ** The return address stack is of limited depth.  If too many
596 ** OP_Gosub operations occur without intervening OP_Returns, then
597 ** the return address stack will fill up and processing will abort
598 ** with a fatal error.
599 */
600 case OP_Gosub: {
601   if( p->returnDepth>=sizeof(p->returnStack)/sizeof(p->returnStack[0]) ){
602     sqlite3SetString(&p->zErrMsg, "return address stack overflow", (char*)0);
603     p->rc = SQLITE_INTERNAL;
604     return SQLITE_ERROR;
605   }
606   p->returnStack[p->returnDepth++] = pc+1;
607   pc = pOp->p2 - 1;
608   break;
609 }
610 
611 /* Opcode:  Return * * *
612 **
613 ** Jump immediately to the next instruction after the last unreturned
614 ** OP_Gosub.  If an OP_Return has occurred for all OP_Gosubs, then
615 ** processing aborts with a fatal error.
616 */
617 case OP_Return: {
618   if( p->returnDepth<=0 ){
619     sqlite3SetString(&p->zErrMsg, "return address stack underflow", (char*)0);
620     p->rc = SQLITE_INTERNAL;
621     return SQLITE_ERROR;
622   }
623   p->returnDepth--;
624   pc = p->returnStack[p->returnDepth] - 1;
625   break;
626 }
627 
628 /* Opcode:  Halt P1 P2 *
629 **
630 ** Exit immediately.  All open cursors, Lists, Sorts, etc are closed
631 ** automatically.
632 **
633 ** P1 is the result code returned by sqlite3_exec().  For a normal
634 ** halt, this should be SQLITE_OK (0).  For errors, it can be some
635 ** other value.  If P1!=0 then P2 will determine whether or not to
636 ** rollback the current transaction.  Do not rollback if P2==OE_Fail.
637 ** Do the rollback if P2==OE_Rollback.  If P2==OE_Abort, then back
638 ** out all changes that have occurred during this execution of the
639 ** VDBE, but do not rollback the transaction.
640 **
641 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
642 ** every program.  So a jump past the last instruction of the program
643 ** is the same as executing Halt.
644 */
645 case OP_Halt: {
646   p->magic = VDBE_MAGIC_HALT;
647   p->pTos = pTos;
648   if( pOp->p1!=SQLITE_OK ){
649     p->rc = pOp->p1;
650     p->errorAction = pOp->p2;
651     if( pOp->p3 ){
652       sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
653     }
654     return SQLITE_ERROR;
655   }else{
656     p->rc = SQLITE_OK;
657     return SQLITE_DONE;
658   }
659 }
660 
661 /* Opcode: Integer P1 * P3
662 **
663 ** The integer value P1 is pushed onto the stack.  If P3 is not zero
664 ** then it is assumed to be a string representation of the same integer.
665 ** If P1 is zero and P3 is not zero, then the value is derived from P3.
666 */
667 case OP_Integer: {
668   pTos++;
669   if( pOp->p3==0 ){
670     pTos->flags = MEM_Int;
671     pTos->i = pOp->p1;
672   }else{
673     pTos->flags = MEM_Str|MEM_Static|MEM_Term;
674     pTos->z = pOp->p3;
675     pTos->n = strlen(pTos->z);
676     pTos->enc = TEXT_Utf8;
677     Integerify(pTos, 0);
678   }
679   break;
680 }
681 
682 /* Opcode: Real * * P3
683 **
684 ** The string value P3 is converted to a real and pushed on to the stack.
685 */
686 case OP_Real: {
687   pTos++;
688   pTos->flags = MEM_Str|MEM_Static|MEM_Term;
689   pTos->z = pOp->p3;
690   pTos->n = strlen(pTos->z);
691   pTos->enc = TEXT_Utf8;
692   Realify(pTos, 0);
693   break;
694 }
695 
696 /* Opcode: String8 * * P3
697 **
698 ** P3 points to a nul terminated UTF-8 string. This opcode is transformed
699 ** into an OP_String before it is executed for the first time.
700 */
701 case OP_String8: {
702   pOp->opcode = OP_String;
703 
704   if( db->enc!=TEXT_Utf8 && pOp->p3 ){
705     if( db->enc==TEXT_Utf16le ){
706       pOp->p3 = sqlite3utf8to16le(pOp->p3, -1);
707     }else{
708       pOp->p3 = sqlite3utf8to16be(pOp->p3, -1);
709     }
710     if( !pOp->p3 ) goto no_mem;
711   }
712 
713   /* Fall through to the next case, OP_String */
714 }
715 
716 /* Opcode: String * * P3
717 **
718 ** The string value P3 is pushed onto the stack.  If P3==0 then a
719 ** NULL is pushed onto the stack. P3 is assumed to be a nul terminated
720 ** string encoded with the database native encoding.
721 */
722 case OP_String: {
723   pTos++;
724   if( pOp->p3 ){
725     pTos->flags = MEM_Str|MEM_Static|MEM_Term;
726     pTos->z = pOp->p3;
727     if( db->enc==TEXT_Utf8 ){
728       pTos->n = strlen(pTos->z);
729     }else{
730       pTos->n  = sqlite3utf16ByteLen(pTos->z, -1);
731     }
732     pTos->enc = db->enc;
733   }else{
734     pTos->flags = MEM_Null;
735   }
736   break;
737 }
738 
739 /* Opcode: HexBlob * * P3
740 **
741 ** P3 is an SQL hex encoding of a blob. The blob is pushed
742 ** onto the vdbe stack.
743 **
744 ** The first time this instruction executes, in transforms
745 ** itself into a 'Blob' opcode with a binary blob as P3.
746 */
747 case OP_HexBlob: {
748   pOp->opcode = OP_Blob;
749   pOp->p1 = strlen(pOp->p3)/2;
750   if( pOp->p1 ){
751     char *zBlob = sqlite3HexToBlob(pOp->p3);
752     if( !zBlob ) goto no_mem;
753     if( pOp->p3type==P3_DYNAMIC ){
754       sqliteFree(pOp->p3);
755     }
756     pOp->p3 = zBlob;
757     pOp->p3type = P3_DYNAMIC;
758   }else{
759     pOp->p3type = P3_STATIC;
760     pOp->p3 = "";
761   }
762 
763   /* Fall through to the next case, OP_Blob. */
764 }
765 
766 /* Opcode: Blob P1 * P3
767 **
768 ** P3 points to a blob of data P1 bytes long. Push this
769 ** value onto the stack. This instruction is not coded directly
770 ** by the compiler. Instead, the compiler layer specifies
771 ** an OP_HexBlob opcode, with the hex string representation of
772 ** the blob as P3. This opcode is transformed to an OP_Blob
773 ** before execution (within the sqlite3_prepare() function).
774 */
775 case OP_Blob: {
776   pTos++;
777   sqlite3VdbeMemSetStr(pTos, pOp->p3, pOp->p1, 0, 0);
778   break;
779 }
780 
781 /* Opcode: Variable P1 * *
782 **
783 ** Push the value of variable P1 onto the stack.  A variable is
784 ** an unknown in the original SQL string as handed to sqlite3_compile().
785 ** Any occurance of the '?' character in the original SQL is considered
786 ** a variable.  Variables in the SQL string are number from left to
787 ** right beginning with 1.  The values of variables are set using the
788 ** sqlite3_bind() API.
789 */
790 case OP_Variable: {
791   int j = pOp->p1 - 1;
792   assert( j>=0 && j<p->nVar );
793 
794   pTos++;
795   memcpy(pTos, &p->apVar[j], sizeof(*pTos)-NBFS);
796   if( pTos->flags&(MEM_Str|MEM_Blob) ){
797     pTos->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Short);
798     pTos->flags |= MEM_Static;
799   }
800   break;
801 }
802 
803 /* Opcode: Utf16le_8 * * *
804 **
805 ** The element on the top of the stack must be a little-endian UTF-16
806 ** encoded string. It is translated in-place to UTF-8.
807 */
808 case OP_Utf16le_8: {
809   rc = SQLITE_INTERNAL;
810   break;
811 }
812 
813 /* Opcode: Utf16be_8 * * *
814 **
815 ** The element on the top of the stack must be a big-endian UTF-16
816 ** encoded string. It is translated in-place to UTF-8.
817 */
818 case OP_Utf16be_8: {
819   rc = SQLITE_INTERNAL;
820   break;
821 }
822 
823 /* Opcode: Utf8_16be * * *
824 **
825 ** The element on the top of the stack must be a UTF-8 encoded
826 ** string. It is translated to big-endian UTF-16.
827 */
828 case OP_Utf8_16be: {
829   rc = SQLITE_INTERNAL;
830   break;
831 }
832 
833 /* Opcode: Utf8_16le * * *
834 **
835 ** The element on the top of the stack must be a UTF-8 encoded
836 ** string. It is translated to little-endian UTF-16.
837 */
838 case OP_Utf8_16le: {
839   rc = SQLITE_INTERNAL;
840   break;
841 }
842 
843 /*
844 ** Opcode: UtfSwab
845 **
846 ** The element on the top of the stack must be an UTF-16 encoded
847 ** string. Every second byte is exchanged, so as to translate
848 ** the string from little-endian to big-endian or vice versa.
849 */
850 case OP_UtfSwab: {
851   rc = SQLITE_INTERNAL;
852   break;
853 }
854 
855 /* Opcode: Pop P1 * *
856 **
857 ** P1 elements are popped off of the top of stack and discarded.
858 */
859 case OP_Pop: {
860   assert( pOp->p1>=0 );
861   popStack(&pTos, pOp->p1);
862   assert( pTos>=&p->aStack[-1] );
863   break;
864 }
865 
866 /* Opcode: Dup P1 P2 *
867 **
868 ** A copy of the P1-th element of the stack
869 ** is made and pushed onto the top of the stack.
870 ** The top of the stack is element 0.  So the
871 ** instruction "Dup 0 0 0" will make a copy of the
872 ** top of the stack.
873 **
874 ** If the content of the P1-th element is a dynamically
875 ** allocated string, then a new copy of that string
876 ** is made if P2==0.  If P2!=0, then just a pointer
877 ** to the string is copied.
878 **
879 ** Also see the Pull instruction.
880 */
881 case OP_Dup: {
882   Mem *pFrom = &pTos[-pOp->p1];
883   assert( pFrom<=pTos && pFrom>=p->aStack );
884   pTos++;
885   memcpy(pTos, pFrom, sizeof(*pFrom)-NBFS);
886   if( pTos->flags & (MEM_Str|MEM_Blob) ){
887     if( pOp->p2 && (pTos->flags & (MEM_Dyn|MEM_Ephem)) ){
888       pTos->flags &= ~MEM_Dyn;
889       pTos->flags |= MEM_Ephem;
890     }else if( pTos->flags & MEM_Short ){
891       memcpy(pTos->zShort, pFrom->zShort, pTos->n+2);
892       pTos->z = pTos->zShort;
893     }else if( (pTos->flags & MEM_Static)==0 ){
894       pTos->z = sqliteMallocRaw(pFrom->n+2);
895       if( sqlite3_malloc_failed ) goto no_mem;
896       memcpy(pTos->z, pFrom->z, pFrom->n);
897       memcpy(&pTos->z[pTos->n], "\0", 2);
898       pTos->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short);
899       pTos->flags |= MEM_Dyn|MEM_Term;
900     }
901   }
902   break;
903 }
904 
905 /* Opcode: Pull P1 * *
906 **
907 ** The P1-th element is removed from its current location on
908 ** the stack and pushed back on top of the stack.  The
909 ** top of the stack is element 0, so "Pull 0 0 0" is
910 ** a no-op.  "Pull 1 0 0" swaps the top two elements of
911 ** the stack.
912 **
913 ** See also the Dup instruction.
914 */
915 case OP_Pull: {
916   Mem *pFrom = &pTos[-pOp->p1];
917   int i;
918   Mem ts;
919 
920   ts = *pFrom;
921   Deephemeralize(pTos);
922   for(i=0; i<pOp->p1; i++, pFrom++){
923     Deephemeralize(&pFrom[1]);
924     assert( (pFrom->flags & MEM_Ephem)==0 );
925     *pFrom = pFrom[1];
926     if( pFrom->flags & MEM_Short ){
927       assert( pFrom->flags & (MEM_Str|MEM_Blob) );
928       assert( pFrom->z==pFrom[1].zShort );
929       pFrom->z = pFrom->zShort;
930     }
931   }
932   *pTos = ts;
933   if( pTos->flags & MEM_Short ){
934     assert( pTos->flags & (MEM_Str|MEM_Blob) );
935     assert( pTos->z==pTos[-pOp->p1].zShort );
936     pTos->z = pTos->zShort;
937   }
938   break;
939 }
940 
941 /* Opcode: Push P1 * *
942 **
943 ** Overwrite the value of the P1-th element down on the
944 ** stack (P1==0 is the top of the stack) with the value
945 ** of the top of the stack.  Then pop the top of the stack.
946 */
947 case OP_Push: {
948   Mem *pTo = &pTos[-pOp->p1];
949 
950   assert( pTo>=p->aStack );
951   Deephemeralize(pTos);
952   Release(pTo);
953   *pTo = *pTos;
954   if( pTo->flags & MEM_Short ){
955     assert( pTo->z==pTos->zShort );
956     pTo->z = pTo->zShort;
957   }
958   pTos--;
959   break;
960 }
961 
962 
963 /* Opcode: ColumnName P1 P2 P3
964 **
965 ** P3 becomes the P1-th column name (first is 0).  An array of pointers
966 ** to all column names is passed as the 4th parameter to the callback.
967 ** If P2==1 then this is the last column in the result set and thus the
968 ** number of columns in the result set will be P1.  There must be at least
969 ** one OP_ColumnName with a P2==1 before invoking OP_Callback and the
970 ** number of columns specified in OP_Callback must one more than the P1
971 ** value of the OP_ColumnName that has P2==1.
972 */
973 case OP_ColumnName: {
974   assert(0);
975   assert( pOp->p1>=0 && pOp->p1<p->nOp );
976   p->azColName[pOp->p1] = pOp->p3;
977   p->nCallback = 0;
978   assert( !pOp->p2 || p->nResColumn==(pOp->p1+1) );
979   /* if( pOp->p2 ) p->nResColumn = pOp->p1+1; */
980   break;
981 }
982 
983 /* Opcode: Callback P1 * *
984 **
985 ** Pop P1 values off the stack and form them into an array.  Then
986 ** invoke the callback function using the newly formed array as the
987 ** 3rd parameter.
988 */
989 case OP_Callback: {
990   int i;
991   assert( p->nResColumn==pOp->p1 );
992 
993   for(i=0; i<pOp->p1; i++){
994     Mem *pVal = &pTos[0-i];
995     sqlite3VdbeMemNulTerminate(pVal);
996     storeTypeInfo(pVal, db->enc);
997   }
998 
999   p->resOnStack = 1;
1000   p->nCallback++;
1001   p->popStack = pOp->p1;
1002   p->pc = pc + 1;
1003   p->pTos = pTos;
1004   return SQLITE_ROW;
1005 }
1006 
1007 /* Opcode: Concat P1 P2 P3
1008 **
1009 ** Look at the first P1 elements of the stack.  Append them all
1010 ** together with the lowest element first.  Use P3 as a separator.
1011 ** Put the result on the top of the stack.  The original P1 elements
1012 ** are popped from the stack if P2==0 and retained if P2==1.  If
1013 ** any element of the stack is NULL, then the result is NULL.
1014 **
1015 ** If P3 is NULL, then use no separator.  When P1==1, this routine
1016 ** makes a copy of the top stack element into memory obtained
1017 ** from sqliteMalloc().
1018 */
1019 case OP_Concat: {
1020   char *zNew;
1021   int nByte;
1022   int nField;
1023   int i, j;
1024   Mem *pTerm;
1025   Mem mSep;     /* Memory cell containing the seperator string, if any */
1026 
1027   /* FIX ME: Eventually, P3 will be in database native encoding. But for
1028   ** now it is always UTF-8. So set up zSep to hold the native encoding of
1029   ** P3.
1030   */
1031   if( pOp->p3 ){
1032     mSep.z = pOp->p3;
1033     mSep.n = strlen(mSep.z);
1034     mSep.flags = MEM_Str|MEM_Static|MEM_Term;
1035     mSep.enc = TEXT_Utf8;
1036     sqlite3VdbeChangeEncoding(&mSep, db->enc);
1037   }else{
1038     mSep.flags = MEM_Null;
1039     mSep.n = 0;
1040   }
1041 
1042   /* Loop through the stack elements to see how long the result will be. */
1043   nField = pOp->p1;
1044   pTerm = &pTos[1-nField];
1045   nByte = (nField-1)*mSep.n;
1046   for(i=0; i<nField; i++, pTerm++){
1047     assert( pOp->p2==0 || (pTerm->flags&MEM_Str) );
1048     if( pTerm->flags&MEM_Null ){
1049       nByte = -1;
1050       break;
1051     }
1052     Stringify(pTerm, db->enc);
1053     nByte += pTerm->n;
1054   }
1055 
1056   if( nByte<0 ){
1057     /* If nByte is less than zero, then there is a NULL value on the stack.
1058     ** In this case just pop the values off the stack (if required) and
1059     ** push on a NULL.
1060     */
1061     if( pOp->p2==0 ){
1062       popStack(&pTos, nField);
1063     }
1064     pTos++;
1065     pTos->flags = MEM_Null;
1066   }else{
1067     /* Otherwise malloc() space for the result and concatenate all the
1068     ** stack values.
1069     */
1070     zNew = sqliteMallocRaw( nByte+2 );
1071     if( zNew==0 ) goto no_mem;
1072     j = 0;
1073     pTerm = &pTos[1-nField];
1074     for(i=j=0; i<nField; i++, pTerm++){
1075       int n = pTerm->n;
1076       assert( pTerm->flags & MEM_Str );
1077       memcpy(&zNew[j], pTerm->z, n);
1078       j += n;
1079       if( i<nField-1 && !(mSep.flags|MEM_Null) ){
1080         memcpy(&zNew[j], mSep.z, mSep.n);
1081         j += mSep.n;
1082       }
1083     }
1084     zNew[j] = 0;
1085     zNew[j+1] = 0;
1086     assert( j==nByte );
1087 
1088     if( pOp->p2==0 ){
1089       popStack(&pTos, nField);
1090     }
1091     pTos++;
1092     pTos->n = j;
1093     pTos->flags = MEM_Str|MEM_Dyn|MEM_Term;
1094     pTos->enc = db->enc;
1095     pTos->z = zNew;
1096   }
1097   break;
1098 }
1099 
1100 /* Opcode: Add * * *
1101 **
1102 ** Pop the top two elements from the stack, add them together,
1103 ** and push the result back onto the stack.  If either element
1104 ** is a string then it is converted to a double using the atof()
1105 ** function before the addition.
1106 ** If either operand is NULL, the result is NULL.
1107 */
1108 /* Opcode: Multiply * * *
1109 **
1110 ** Pop the top two elements from the stack, multiply them together,
1111 ** and push the result back onto the stack.  If either element
1112 ** is a string then it is converted to a double using the atof()
1113 ** function before the multiplication.
1114 ** If either operand is NULL, the result is NULL.
1115 */
1116 /* Opcode: Subtract * * *
1117 **
1118 ** Pop the top two elements from the stack, subtract the
1119 ** first (what was on top of the stack) from the second (the
1120 ** next on stack)
1121 ** and push the result back onto the stack.  If either element
1122 ** is a string then it is converted to a double using the atof()
1123 ** function before the subtraction.
1124 ** If either operand is NULL, the result is NULL.
1125 */
1126 /* Opcode: Divide * * *
1127 **
1128 ** Pop the top two elements from the stack, divide the
1129 ** first (what was on top of the stack) from the second (the
1130 ** next on stack)
1131 ** and push the result back onto the stack.  If either element
1132 ** is a string then it is converted to a double using the atof()
1133 ** function before the division.  Division by zero returns NULL.
1134 ** If either operand is NULL, the result is NULL.
1135 */
1136 /* Opcode: Remainder * * *
1137 **
1138 ** Pop the top two elements from the stack, divide the
1139 ** first (what was on top of the stack) from the second (the
1140 ** next on stack)
1141 ** and push the remainder after division onto the stack.  If either element
1142 ** is a string then it is converted to a double using the atof()
1143 ** function before the division.  Division by zero returns NULL.
1144 ** If either operand is NULL, the result is NULL.
1145 */
1146 case OP_Add:
1147 case OP_Subtract:
1148 case OP_Multiply:
1149 case OP_Divide:
1150 case OP_Remainder: {
1151   Mem *pNos = &pTos[-1];
1152   assert( pNos>=p->aStack );
1153   if( ((pTos->flags | pNos->flags) & MEM_Null)!=0 ){
1154     Release(pTos);
1155     pTos--;
1156     Release(pTos);
1157     pTos->flags = MEM_Null;
1158   }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){
1159     i64 a, b;
1160     a = pTos->i;
1161     b = pNos->i;
1162     switch( pOp->opcode ){
1163       case OP_Add:         b += a;       break;
1164       case OP_Subtract:    b -= a;       break;
1165       case OP_Multiply:    b *= a;       break;
1166       case OP_Divide: {
1167         if( a==0 ) goto divide_by_zero;
1168         b /= a;
1169         break;
1170       }
1171       default: {
1172         if( a==0 ) goto divide_by_zero;
1173         b %= a;
1174         break;
1175       }
1176     }
1177     Release(pTos);
1178     pTos--;
1179     Release(pTos);
1180     pTos->i = b;
1181     pTos->flags = MEM_Int;
1182   }else{
1183     double a, b;
1184     Realify(pTos, db->enc);
1185     Realify(pNos, db->enc);
1186     a = pTos->r;
1187     b = pNos->r;
1188     switch( pOp->opcode ){
1189       case OP_Add:         b += a;       break;
1190       case OP_Subtract:    b -= a;       break;
1191       case OP_Multiply:    b *= a;       break;
1192       case OP_Divide: {
1193         if( a==0.0 ) goto divide_by_zero;
1194         b /= a;
1195         break;
1196       }
1197       default: {
1198         int ia = (int)a;
1199         int ib = (int)b;
1200         if( ia==0.0 ) goto divide_by_zero;
1201         b = ib % ia;
1202         break;
1203       }
1204     }
1205     Release(pTos);
1206     pTos--;
1207     Release(pTos);
1208     pTos->r = b;
1209     pTos->flags = MEM_Real;
1210   }
1211   break;
1212 
1213 divide_by_zero:
1214   Release(pTos);
1215   pTos--;
1216   Release(pTos);
1217   pTos->flags = MEM_Null;
1218   break;
1219 }
1220 
1221 /* Opcode: Function P1 P2 P3
1222 **
1223 ** Invoke a user function (P3 is a pointer to a Function structure that
1224 ** defines the function) with P1 arguments taken from the stack.  Pop all
1225 ** arguments from the stack and push back the result.
1226 **
1227 ** P2 is a 32-bit bitmask indicating whether or not each argument to the
1228 ** function was determined to be constant at compile time. If the first
1229 ** argument was constant then bit 0 of P2 is set. This is used to determine
1230 ** whether meta data associated with a user function argument using the
1231 ** sqlite3_set_auxdata() API may be safely retained until the next
1232 ** invocation of this opcode.
1233 **
1234 ** See also: AggFunc
1235 */
1236 case OP_Function: {
1237   int i;
1238   Mem *pArg;
1239   sqlite3_context ctx;
1240   sqlite3_value **apVal;
1241   int n = pOp->p1;
1242 
1243   n = pOp->p1;
1244   apVal = p->apArg;
1245   assert( apVal || n==0 );
1246 
1247   pArg = &pTos[1-n];
1248   for(i=0; i<n; i++, pArg++){
1249     apVal[i] = pArg;
1250     storeTypeInfo(pArg, db->enc);
1251   }
1252 
1253   assert( pOp->p3type==P3_FUNCDEF || pOp->p3type==P3_VDBEFUNC );
1254   if( pOp->p3type==P3_FUNCDEF ){
1255     ctx.pFunc = (FuncDef*)pOp->p3;
1256     ctx.pVdbeFunc = 0;
1257   }else{
1258     ctx.pVdbeFunc = (VdbeFunc*)pOp->p3;
1259     ctx.pFunc = ctx.pVdbeFunc->pFunc;
1260   }
1261 
1262   ctx.s.flags = MEM_Null;
1263   ctx.s.z = 0;
1264   ctx.isError = 0;
1265   ctx.isStep = 0;
1266   if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1267   (*ctx.pFunc->xFunc)(&ctx, n, apVal);
1268   if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1269   popStack(&pTos, n);
1270 
1271   /* If any auxilary data functions have been called by this user function,
1272   ** immediately call the destructor for any non-static values.
1273   */
1274   if( ctx.pVdbeFunc ){
1275     int mask = pOp->p2;
1276     for(i=0; i<ctx.pVdbeFunc->nAux; i++){
1277       struct AuxData *pAux = &ctx.pVdbeFunc->apAux[i];
1278       if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1279         pAux->xDelete(pAux->pAux);
1280         pAux->pAux = 0;
1281       }
1282     }
1283     pOp->p3 = (char *)ctx.pVdbeFunc;
1284     pOp->p3type = P3_VDBEFUNC;
1285   }
1286 
1287   /* Copy the result of the function to the top of the stack */
1288   pTos++;
1289   *pTos = ctx.s;
1290   if( pTos->flags & MEM_Short ){
1291     pTos->z = pTos->zShort;
1292   }
1293   /* If the function returned an error, throw an exception */
1294   if( ctx.isError ){
1295     sqlite3SetString(&p->zErrMsg,
1296        (pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0);
1297     rc = SQLITE_ERROR;
1298   }
1299   break;
1300 }
1301 
1302 /* Opcode: BitAnd * * *
1303 **
1304 ** Pop the top two elements from the stack.  Convert both elements
1305 ** to integers.  Push back onto the stack the bit-wise AND of the
1306 ** two elements.
1307 ** If either operand is NULL, the result is NULL.
1308 */
1309 /* Opcode: BitOr * * *
1310 **
1311 ** Pop the top two elements from the stack.  Convert both elements
1312 ** to integers.  Push back onto the stack the bit-wise OR of the
1313 ** two elements.
1314 ** If either operand is NULL, the result is NULL.
1315 */
1316 /* Opcode: ShiftLeft * * *
1317 **
1318 ** Pop the top two elements from the stack.  Convert both elements
1319 ** to integers.  Push back onto the stack the top element shifted
1320 ** left by N bits where N is the second element on the stack.
1321 ** If either operand is NULL, the result is NULL.
1322 */
1323 /* Opcode: ShiftRight * * *
1324 **
1325 ** Pop the top two elements from the stack.  Convert both elements
1326 ** to integers.  Push back onto the stack the top element shifted
1327 ** right by N bits where N is the second element on the stack.
1328 ** If either operand is NULL, the result is NULL.
1329 */
1330 case OP_BitAnd:
1331 case OP_BitOr:
1332 case OP_ShiftLeft:
1333 case OP_ShiftRight: {
1334   Mem *pNos = &pTos[-1];
1335   int a, b;
1336 
1337   assert( pNos>=p->aStack );
1338   if( (pTos->flags | pNos->flags) & MEM_Null ){
1339     popStack(&pTos, 2);
1340     pTos++;
1341     pTos->flags = MEM_Null;
1342     break;
1343   }
1344   Integerify(pTos, db->enc);
1345   Integerify(pNos, db->enc);
1346   a = pTos->i;
1347   b = pNos->i;
1348   switch( pOp->opcode ){
1349     case OP_BitAnd:      a &= b;     break;
1350     case OP_BitOr:       a |= b;     break;
1351     case OP_ShiftLeft:   a <<= b;    break;
1352     case OP_ShiftRight:  a >>= b;    break;
1353     default:   /* CANT HAPPEN */     break;
1354   }
1355   /* FIX ME: Because constant P3 values sometimes need to be translated,
1356   ** the following assert() can fail. When P3 is always in the native text
1357   ** encoding, this assert() will be valid again. Until then, the Release()
1358   ** is neeed instead.
1359   assert( (pTos->flags & MEM_Dyn)==0 );
1360   assert( (pNos->flags & MEM_Dyn)==0 );
1361   */
1362   Release(pTos);
1363   pTos--;
1364   Release(pTos);
1365   pTos->i = a;
1366   pTos->flags = MEM_Int;
1367   break;
1368 }
1369 
1370 /* Opcode: AddImm  P1 * *
1371 **
1372 ** Add the value P1 to whatever is on top of the stack.  The result
1373 ** is always an integer.
1374 **
1375 ** To force the top of the stack to be an integer, just add 0.
1376 */
1377 case OP_AddImm: {
1378   assert( pTos>=p->aStack );
1379   Integerify(pTos, db->enc);
1380   pTos->i += pOp->p1;
1381   break;
1382 }
1383 
1384 /* Opcode: ForceInt P1 P2 *
1385 **
1386 ** Convert the top of the stack into an integer.  If the current top of
1387 ** the stack is not numeric (meaning that is is a NULL or a string that
1388 ** does not look like an integer or floating point number) then pop the
1389 ** stack and jump to P2.  If the top of the stack is numeric then
1390 ** convert it into the least integer that is greater than or equal to its
1391 ** current value if P1==0, or to the least integer that is strictly
1392 ** greater than its current value if P1==1.
1393 */
1394 case OP_ForceInt: {
1395   int v;
1396   assert( pTos>=p->aStack );
1397   if( (pTos->flags & (MEM_Int|MEM_Real))==0 && ((pTos->flags & MEM_Str)==0
1398       || sqlite3IsNumber(pTos->z, 0, db->enc)==0) ){
1399     Release(pTos);
1400     pTos--;
1401     pc = pOp->p2 - 1;
1402     break;
1403   }
1404   if( pTos->flags & MEM_Int ){
1405     v = pTos->i + (pOp->p1!=0);
1406   }else{
1407     Realify(pTos, db->enc);
1408     v = (int)pTos->r;
1409     if( pTos->r>(double)v ) v++;
1410     if( pOp->p1 && pTos->r==(double)v ) v++;
1411   }
1412   Release(pTos);
1413   pTos->i = v;
1414   pTos->flags = MEM_Int;
1415   break;
1416 }
1417 
1418 /* Opcode: MustBeInt P1 P2 *
1419 **
1420 ** Force the top of the stack to be an integer.  If the top of the
1421 ** stack is not an integer and cannot be converted into an integer
1422 ** with out data loss, then jump immediately to P2, or if P2==0
1423 ** raise an SQLITE_MISMATCH exception.
1424 **
1425 ** If the top of the stack is not an integer and P2 is not zero and
1426 ** P1 is 1, then the stack is popped.  In all other cases, the depth
1427 ** of the stack is unchanged.
1428 */
1429 case OP_MustBeInt: {
1430   assert( pTos>=p->aStack );
1431   if( pTos->flags & MEM_Int ){
1432     /* Do nothing */
1433   }else if( pTos->flags & MEM_Real ){
1434     int i = (int)pTos->r;
1435     double r = (double)i;
1436     if( r!=pTos->r ){
1437       goto mismatch;
1438     }
1439     pTos->i = i;
1440   }else if( pTos->flags & MEM_Str ){
1441     i64 v;
1442     if( sqlite3VdbeChangeEncoding(pTos, TEXT_Utf8)
1443        || sqlite3VdbeMemNulTerminate(pTos) ){
1444       goto no_mem;
1445     }
1446     if( !sqlite3atoi64(pTos->z, &v) ){
1447       double r;
1448       if( !sqlite3IsNumber(pTos->z, 0, TEXT_Utf8) ){
1449         goto mismatch;
1450       }
1451       Realify(pTos, TEXT_Utf8);
1452       v = (int)pTos->r;
1453       r = (double)v;
1454       if( r!=pTos->r ){
1455         goto mismatch;
1456       }
1457     }
1458     pTos->i = v;
1459   }else{
1460     goto mismatch;
1461   }
1462   Release(pTos);
1463   pTos->flags = MEM_Int;
1464   break;
1465 
1466 mismatch:
1467   if( pOp->p2==0 ){
1468     rc = SQLITE_MISMATCH;
1469     goto abort_due_to_error;
1470   }else{
1471     if( pOp->p1 ) popStack(&pTos, 1);
1472     pc = pOp->p2 - 1;
1473   }
1474   break;
1475 }
1476 
1477 /* Opcode: Eq P1 P2 P3
1478 **
1479 ** Pop the top two elements from the stack.  If they are equal, then
1480 ** jump to instruction P2.  Otherwise, continue to the next instruction.
1481 **
1482 ** The least significant byte of P1 may be either 0x00 or 0x01. If either
1483 ** operand is NULL (and thus if the result is unknown) then take the jump
1484 ** only if the least significant byte of P1 is 0x01.
1485 **
1486 ** The second least significant byte of P1 must be an affinity character -
1487 ** 'n', 't', 'i' or 'o' - or 0x00. An attempt is made to coerce both values
1488 ** according to the affinity before the comparison is made. If the byte is
1489 ** 0x00, then numeric affinity is used.
1490 **
1491 ** Once any conversions have taken place, and neither value is NULL,
1492 ** the values are compared. If both values are blobs, or both are text,
1493 ** then memcmp() is used to determine the results of the comparison. If
1494 ** both values are numeric, then a numeric comparison is used. If the
1495 ** two values are of different types, then they are inequal.
1496 **
1497 ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
1498 ** stack if the jump would have been taken, or a 0 if not.  Push a
1499 ** NULL if either operand was NULL.
1500 **
1501 ** If P3 is not NULL it is a pointer to a collating sequence (a CollSeq
1502 ** structure) that defines how to compare text.
1503 */
1504 /* Opcode: Ne P1 P2 P3
1505 **
1506 ** This works just like the Eq opcode except that the jump is taken if
1507 ** the operands from the stack are not equal.  See the Eq opcode for
1508 ** additional information.
1509 */
1510 /* Opcode: Lt P1 P2 P3
1511 **
1512 ** This works just like the Eq opcode except that the jump is taken if
1513 ** the 2nd element down on the stack is less than the top of the stack.
1514 ** See the Eq opcode for additional information.
1515 */
1516 /* Opcode: Le P1 P2 P3
1517 **
1518 ** This works just like the Eq opcode except that the jump is taken if
1519 ** the 2nd element down on the stack is less than or equal to the
1520 ** top of the stack.  See the Eq opcode for additional information.
1521 */
1522 /* Opcode: Gt P1 P2 P3
1523 **
1524 ** This works just like the Eq opcode except that the jump is taken if
1525 ** the 2nd element down on the stack is greater than the top of the stack.
1526 ** See the Eq opcode for additional information.
1527 */
1528 /* Opcode: Ge P1 P2 P3
1529 **
1530 ** This works just like the Eq opcode except that the jump is taken if
1531 ** the 2nd element down on the stack is greater than or equal to the
1532 ** top of the stack.  See the Eq opcode for additional information.
1533 */
1534 case OP_Eq:
1535 case OP_Ne:
1536 case OP_Lt:
1537 case OP_Le:
1538 case OP_Gt:
1539 case OP_Ge: {
1540   Mem *pNos;
1541   int flags;
1542   int res;
1543   char affinity;
1544 
1545   pNos = &pTos[-1];
1546   flags = pTos->flags|pNos->flags;
1547 
1548   /* If either value is a NULL P2 is not zero, take the jump if the least
1549   ** significant byte of P1 is true. If P2 is zero, then push a NULL onto
1550   ** the stack.
1551   */
1552   if( flags&MEM_Null ){
1553     popStack(&pTos, 2);
1554     if( pOp->p2 ){
1555       if( (pOp->p1&0xFF) ) pc = pOp->p2-1;
1556     }else{
1557       pTos++;
1558       pTos->flags = MEM_Null;
1559     }
1560     break;
1561   }
1562 
1563   affinity = (pOp->p1>>8)&0xFF;
1564   if( affinity ){
1565     applyAffinity(pNos, affinity, db->enc);
1566     applyAffinity(pTos, affinity, db->enc);
1567   }
1568 
1569   assert( pOp->p3type==P3_COLLSEQ || pOp->p3==0 );
1570   res = sqlite3MemCompare(pNos, pTos, (CollSeq*)pOp->p3);
1571   switch( pOp->opcode ){
1572     case OP_Eq:    res = res==0;     break;
1573     case OP_Ne:    res = res!=0;     break;
1574     case OP_Lt:    res = res<0;      break;
1575     case OP_Le:    res = res<=0;     break;
1576     case OP_Gt:    res = res>0;      break;
1577     default:       res = res>=0;     break;
1578   }
1579 
1580   popStack(&pTos, 2);
1581   if( pOp->p2 ){
1582     if( res ){
1583       pc = pOp->p2-1;
1584     }
1585   }else{
1586     pTos++;
1587     pTos->flags = MEM_Int;
1588     pTos->i = res;
1589   }
1590   break;
1591 }
1592 
1593 /* Opcode: And * * *
1594 **
1595 ** Pop two values off the stack.  Take the logical AND of the
1596 ** two values and push the resulting boolean value back onto the
1597 ** stack.
1598 */
1599 /* Opcode: Or * * *
1600 **
1601 ** Pop two values off the stack.  Take the logical OR of the
1602 ** two values and push the resulting boolean value back onto the
1603 ** stack.
1604 */
1605 case OP_And:
1606 case OP_Or: {
1607   Mem *pNos = &pTos[-1];
1608   int v1, v2;    /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
1609 
1610   assert( pNos>=p->aStack );
1611   if( pTos->flags & MEM_Null ){
1612     v1 = 2;
1613   }else{
1614     Integerify(pTos, db->enc);
1615     v1 = pTos->i==0;
1616   }
1617   if( pNos->flags & MEM_Null ){
1618     v2 = 2;
1619   }else{
1620     Integerify(pNos, db->enc);
1621     v2 = pNos->i==0;
1622   }
1623   if( pOp->opcode==OP_And ){
1624     static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
1625     v1 = and_logic[v1*3+v2];
1626   }else{
1627     static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
1628     v1 = or_logic[v1*3+v2];
1629   }
1630   popStack(&pTos, 2);
1631   pTos++;
1632   if( v1==2 ){
1633     pTos->flags = MEM_Null;
1634   }else{
1635     pTos->i = v1==0;
1636     pTos->flags = MEM_Int;
1637   }
1638   break;
1639 }
1640 
1641 /* Opcode: Negative * * *
1642 **
1643 ** Treat the top of the stack as a numeric quantity.  Replace it
1644 ** with its additive inverse.  If the top of the stack is NULL
1645 ** its value is unchanged.
1646 */
1647 /* Opcode: AbsValue * * *
1648 **
1649 ** Treat the top of the stack as a numeric quantity.  Replace it
1650 ** with its absolute value. If the top of the stack is NULL
1651 ** its value is unchanged.
1652 */
1653 case OP_Negative:
1654 case OP_AbsValue: {
1655   assert( pTos>=p->aStack );
1656   if( pTos->flags & MEM_Real ){
1657     Release(pTos);
1658     if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1659       pTos->r = -pTos->r;
1660     }
1661     pTos->flags = MEM_Real;
1662   }else if( pTos->flags & MEM_Int ){
1663     Release(pTos);
1664     if( pOp->opcode==OP_Negative || pTos->i<0 ){
1665       pTos->i = -pTos->i;
1666     }
1667     pTos->flags = MEM_Int;
1668   }else if( pTos->flags & MEM_Null ){
1669     /* Do nothing */
1670   }else{
1671     Realify(pTos, db->enc);
1672     Release(pTos);
1673     if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1674       pTos->r = -pTos->r;
1675     }
1676     pTos->flags = MEM_Real;
1677   }
1678   break;
1679 }
1680 
1681 /* Opcode: Not * * *
1682 **
1683 ** Interpret the top of the stack as a boolean value.  Replace it
1684 ** with its complement.  If the top of the stack is NULL its value
1685 ** is unchanged.
1686 */
1687 case OP_Not: {
1688   assert( pTos>=p->aStack );
1689   if( pTos->flags & MEM_Null ) break;  /* Do nothing to NULLs */
1690   Integerify(pTos, db->enc);
1691   Release(pTos);
1692   pTos->i = !pTos->i;
1693   pTos->flags = MEM_Int;
1694   break;
1695 }
1696 
1697 /* Opcode: BitNot * * *
1698 **
1699 ** Interpret the top of the stack as an value.  Replace it
1700 ** with its ones-complement.  If the top of the stack is NULL its
1701 ** value is unchanged.
1702 */
1703 case OP_BitNot: {
1704   assert( pTos>=p->aStack );
1705   if( pTos->flags & MEM_Null ) break;  /* Do nothing to NULLs */
1706   Integerify(pTos, db->enc);
1707   Release(pTos);
1708   pTos->i = ~pTos->i;
1709   pTos->flags = MEM_Int;
1710   break;
1711 }
1712 
1713 /* Opcode: Noop * * *
1714 **
1715 ** Do nothing.  This instruction is often useful as a jump
1716 ** destination.
1717 */
1718 case OP_Noop: {
1719   break;
1720 }
1721 
1722 /* Opcode: If P1 P2 *
1723 **
1724 ** Pop a single boolean from the stack.  If the boolean popped is
1725 ** true, then jump to p2.  Otherwise continue to the next instruction.
1726 ** An integer is false if zero and true otherwise.  A string is
1727 ** false if it has zero length and true otherwise.
1728 **
1729 ** If the value popped of the stack is NULL, then take the jump if P1
1730 ** is true and fall through if P1 is false.
1731 */
1732 /* Opcode: IfNot P1 P2 *
1733 **
1734 ** Pop a single boolean from the stack.  If the boolean popped is
1735 ** false, then jump to p2.  Otherwise continue to the next instruction.
1736 ** An integer is false if zero and true otherwise.  A string is
1737 ** false if it has zero length and true otherwise.
1738 **
1739 ** If the value popped of the stack is NULL, then take the jump if P1
1740 ** is true and fall through if P1 is false.
1741 */
1742 case OP_If:
1743 case OP_IfNot: {
1744   int c;
1745   assert( pTos>=p->aStack );
1746   if( pTos->flags & MEM_Null ){
1747     c = pOp->p1;
1748   }else{
1749     Integerify(pTos, db->enc);
1750     c = pTos->i;
1751     if( pOp->opcode==OP_IfNot ) c = !c;
1752   }
1753   /* FIX ME: Because constant P3 values sometimes need to be translated,
1754   ** the following assert() can fail. When P3 is always in the native text
1755   ** encoding, this assert() will be valid again. Until then, the Release()
1756   ** is neeed instead.
1757   assert( (pTos->flags & MEM_Dyn)==0 );
1758   */
1759   Release(pTos);
1760   pTos--;
1761   if( c ) pc = pOp->p2-1;
1762   break;
1763 }
1764 
1765 /* Opcode: IsNull P1 P2 *
1766 **
1767 ** If any of the top abs(P1) values on the stack are NULL, then jump
1768 ** to P2.  Pop the stack P1 times if P1>0.   If P1<0 leave the stack
1769 ** unchanged.
1770 */
1771 case OP_IsNull: {
1772   int i, cnt;
1773   Mem *pTerm;
1774   cnt = pOp->p1;
1775   if( cnt<0 ) cnt = -cnt;
1776   pTerm = &pTos[1-cnt];
1777   assert( pTerm>=p->aStack );
1778   for(i=0; i<cnt; i++, pTerm++){
1779     if( pTerm->flags & MEM_Null ){
1780       pc = pOp->p2-1;
1781       break;
1782     }
1783   }
1784   if( pOp->p1>0 ) popStack(&pTos, cnt);
1785   break;
1786 }
1787 
1788 /* Opcode: NotNull P1 P2 *
1789 **
1790 ** Jump to P2 if the top P1 values on the stack are all not NULL.  Pop the
1791 ** stack if P1 times if P1 is greater than zero.  If P1 is less than
1792 ** zero then leave the stack unchanged.
1793 */
1794 case OP_NotNull: {
1795   int i, cnt;
1796   cnt = pOp->p1;
1797   if( cnt<0 ) cnt = -cnt;
1798   assert( &pTos[1-cnt] >= p->aStack );
1799   for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
1800   if( i>=cnt ) pc = pOp->p2-1;
1801   if( pOp->p1>0 ) popStack(&pTos, cnt);
1802   break;
1803 }
1804 
1805 /* Opcode: SetNumColumns P1 P2 *
1806 **
1807 ** Before the OP_Column opcode can be executed on a cursor, this
1808 ** opcode must be called to set the number of fields in the table.
1809 **
1810 ** This opcode sets the number of columns for cursor P1 to P2.
1811 */
1812 case OP_SetNumColumns: {
1813   assert( (pOp->p1)<p->nCursor );
1814   p->apCsr[pOp->p1]->nField = pOp->p2;
1815   break;
1816 }
1817 
1818 /* Opcode: IdxColumn P1 * *
1819 **
1820 ** P1 is a cursor opened on an index. Push the first field from the
1821 ** current index key onto the stack.
1822 */
1823 /* Opcode: Column P1 P2 *
1824 **
1825 ** Interpret the data that cursor P1 points to as a structure built using
1826 ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
1827 ** information about the format of the data.) Push onto the stack the value
1828 ** of the P2-th column contained in the data.
1829 **
1830 ** If the KeyAsData opcode has previously executed on this cursor, then the
1831 ** field might be extracted from the key rather than the data.
1832 **
1833 ** If P1 is negative, then the record is stored on the stack rather than in
1834 ** a table.  For P1==-1, the top of the stack is used.  For P1==-2, the
1835 ** next on the stack is used.  And so forth.  The value pushed is always
1836 ** just a pointer into the record which is stored further down on the
1837 ** stack.  The column value is not copied. The number of columns in the
1838 ** record is stored on the stack just above the record itself.
1839 */
1840 case OP_IdxColumn:
1841 case OP_Column: {
1842   int payloadSize;   /* Number of bytes in the record */
1843   int p1 = pOp->p1;  /* P1 value of the opcode */
1844   int p2 = pOp->p2;  /* column number to retrieve */
1845   Cursor *pC = 0;    /* The VDBE cursor */
1846   char *zRec;        /* Pointer to record-data from stack or pseudo-table. */
1847   BtCursor *pCrsr;   /* The BTree cursor */
1848   u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
1849   u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
1850   u32 nField;        /* number of fields in the record */
1851   u32 szHdr;         /* Number of bytes in the record header */
1852   int len;           /* The length of the serialized data for the column */
1853   int offset = 0;    /* Offset into the data */
1854   int idx;           /* Index into the header */
1855   int i;             /* Loop counter */
1856   char *zData;       /* Part of the record being decoded */
1857   Mem sMem;          /* For storing the record being decoded */
1858 
1859   sMem.flags = 0;
1860   assert( p1<p->nCursor );
1861   pTos++;
1862 
1863   /* This block sets the variable payloadSize, and if the data is coming
1864   ** from the stack or from a pseudo-table zRec. If the data is coming
1865   ** from a real cursor, then zRec is left as NULL.
1866   **
1867   ** We also compute the number of columns in the record.  For cursors,
1868   ** the number of columns is stored in the Cursor.nField element.  For
1869   ** records on the stack, the next entry down on the stack is an integer
1870   ** which is the number of records.
1871   */
1872   if( p1<0 ){
1873     Mem *pRec = &pTos[p1];
1874     Mem *pCnt = &pRec[-1];
1875     assert( pRec>=p->aStack );
1876     assert( pRec->flags & MEM_Blob );
1877     payloadSize = pRec->n;
1878     zRec = pRec->z;
1879     assert( pCnt>=p->aStack );
1880     assert( pCnt->flags & MEM_Int );
1881     nField = pCnt->i;
1882   }else if( (pC = p->apCsr[p1])->pCursor!=0 ){
1883     sqlite3VdbeCursorMoveto(pC);
1884     zRec = 0;
1885     pCrsr = pC->pCursor;
1886     if( pC->nullRow ){
1887       payloadSize = 0;
1888     }else if( pC->cacheValid ){
1889       payloadSize = pC->payloadSize;
1890     }else if( pC->keyAsData ){
1891       i64 payloadSize64;
1892       sqlite3BtreeKeySize(pCrsr, &payloadSize64);
1893       payloadSize = payloadSize64;
1894     }else{
1895       sqlite3BtreeDataSize(pCrsr, &payloadSize);
1896     }
1897     nField = pC->nField;
1898   }else if( pC->pseudoTable ){
1899     payloadSize = pC->nData;
1900     zRec = pC->pData;
1901     pC->cacheValid = 0;
1902     assert( payloadSize==0 || zRec!=0 );
1903     nField = pC->nField;
1904   }else{
1905     payloadSize = 0;
1906   }
1907 
1908   /* If payloadSize is 0, then just push a NULL onto the stack. */
1909   if( payloadSize==0 ){
1910     pTos->flags = MEM_Null;
1911     break;
1912   }
1913 
1914   assert( p2<nField );
1915 
1916   /* Read and parse the table header.  Store the results of the parse
1917   ** into the record header cache fields of the cursor.
1918   */
1919   if( pC && pC->cacheValid ){
1920     aType = pC->aType;
1921     aOffset = pC->aOffset;
1922   }else{
1923     int avail;    /* Number of bytes of available data */
1924     if( pC && pC->aType ){
1925       aType = pC->aType;
1926     }else{
1927       aType = sqliteMallocRaw( 2*nField*sizeof(aType) );
1928     }
1929     aOffset = &aType[nField];
1930     if( aType==0 ){
1931       goto no_mem;
1932     }
1933 
1934     /* Figure out how many bytes are in the header */
1935     if( zRec ){
1936       zData = zRec;
1937     }else{
1938       if( pC->keyAsData ){
1939         zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
1940       }else{
1941         zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
1942       }
1943     }
1944     idx = sqlite3GetVarint32(zData, &szHdr);
1945 
1946     /* Get the complete header text */
1947     if( !zRec && avail<idx ){
1948       rc = sqlite3VdbeMemFromBtree(pCrsr, 0, szHdr, pC->keyAsData, &sMem);
1949       if( rc!=SQLITE_OK ){
1950         goto abort_due_to_error;
1951       }
1952       zData = sMem.z;
1953     }
1954 
1955     /* Scan the header and use it to fill in the aType[] and aOffset[]
1956     ** arrays.  aType[i] will contain the type integer for the i-th
1957     ** column and aOffset[i] will contain the offset from the beginning
1958     ** of the record to the start of the data for the i-th column
1959     */
1960     offset = szHdr;
1961     i = 0;
1962     while( idx<szHdr && i<nField && offset<=payloadSize ){
1963       aOffset[i] = offset;
1964       idx += sqlite3GetVarint32(&zData[idx], &aType[i]);
1965       offset += sqlite3VdbeSerialTypeLen(aType[i]);
1966       i++;
1967     }
1968     Release(&sMem);
1969     sMem.flags = MEM_Null;
1970 
1971     /* The header should end at the start of data and the data should
1972     ** end at last byte of the record. If this is not the case then
1973     ** we are dealing with a malformed record.
1974     */
1975     if( idx!=szHdr || offset!=payloadSize ){
1976       sqliteFree(aType);
1977       if( pC ) pC->aType = 0;
1978       rc = SQLITE_CORRUPT;
1979       break;
1980     }
1981 
1982     /* Remember all aType and aColumn information if we have a cursor
1983     ** to remember it in. */
1984     if( pC ){
1985       pC->payloadSize = payloadSize;
1986       pC->aType = aType;
1987       pC->aOffset = aOffset;
1988       pC->cacheValid = 1;
1989     }
1990   }
1991 
1992   /* Get the column information.
1993   */
1994   if( zRec ){
1995     zData = &zRec[aOffset[p2]];
1996   }else{
1997     len = sqlite3VdbeSerialTypeLen(aType[p2]);
1998     sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->keyAsData, &sMem);
1999     zData = sMem.z;
2000   }
2001   sqlite3VdbeSerialGet(zData, aType[p2], pTos);
2002   sqlite3VdbeMemMakeWriteable(pTos);
2003   pTos->enc = db->enc;
2004   if( rc!=SQLITE_OK ){
2005     goto abort_due_to_error;
2006   }
2007   Release(&sMem);
2008 
2009   /* Release the aType[] memory if we are not dealing with cursor */
2010   if( !pC ){
2011     sqliteFree(aType);
2012   }
2013   break;
2014 }
2015 
2016 /* Opcode: MakeKey P1 P2 P3
2017 **
2018 ** Convert the top P1 entries of the stack into a single entry suitable
2019 ** for use as the key in an index. If P2 is zero, then the original
2020 ** entries are popped off the stack. If P2 is not zero, the original
2021 ** entries remain on the stack.
2022 **
2023 ** P3 is interpreted in the same way as for MakeIdxKey.
2024 */
2025 /* Opcode: MakeIdxKey P1 P2 P3
2026 **
2027 ** Convert the top P1 entries of the stack into a single entry suitable
2028 ** for use as the key in an index.  In addition, take one additional integer
2029 ** off of the stack, treat that integer as an eight-byte record number, and
2030 ** append the integer to the key as a varint.  Thus a total of P1+1 entries
2031 ** are popped from the stack for this instruction and a single entry is
2032 ** pushed back.
2033 **
2034 ** If P2 is not zero and one or more of the P1 entries that go into the
2035 ** generated key is NULL, then jump to P2 after the new key has been
2036 ** pushed on the stack.  In other words, jump to P2 if the key is
2037 ** guaranteed to be unique.  This jump can be used to skip a subsequent
2038 ** uniqueness test.
2039 **
2040 ** P3 may be a string that is P1 characters long.  The nth character of the
2041 ** string indicates the column affinity that should be used for the nth
2042 ** field of the index key (i.e. the first character of P3 corresponds to the
2043 ** lowest element on the stack).
2044 **
2045 **  Character      Column affinity
2046 **  ------------------------------
2047 **  'n'            NUMERIC
2048 **  'i'            INTEGER
2049 **  't'            TEXT
2050 **  'o'            NONE
2051 **
2052 ** If P3 is NULL then datatype coercion occurs.
2053 */
2054 /* Opcode MakeRecord P1 * P3
2055 **
2056 ** Convert the top P1 entries of the stack into a single entry
2057 ** suitable for use as a data record in a database table.  The
2058 ** details of the format are irrelavant as long as the OP_Column
2059 ** opcode can decode the record later.  Refer to source code
2060 ** comments for the details of the record format.
2061 **
2062 ** P3 may be a string that is P1 characters long.  The nth character of the
2063 ** string indicates the column affinity that should be used for the nth
2064 ** field of the index key (i.e. the first character of P3 corresponds to the
2065 ** lowest element on the stack).
2066 **
2067 **  Character      Column affinity
2068 **  ------------------------------
2069 **  'n'            NUMERIC
2070 **  'i'            INTEGER
2071 **  't'            TEXT
2072 **  'o'            NONE
2073 **
2074 ** If P3 is NULL then all index fields have the affinity NONE.
2075 */
2076 /* Opcode MakeRecord P1 P2 P3
2077 **
2078 ** Convert the top abs(P1) entries of the stack into a single entry
2079 ** suitable for use as a data record in a database table or as a key
2080 ** in an index.  The details of the format are irrelavant as long as
2081 ** the OP_Column opcode can decode the record later and as long as the
2082 ** sqlite3VdbeRecordCompare function will correctly compare two encoded
2083 ** records.  Refer to source code comments for the details of the record
2084 ** format.
2085 **
2086 ** The original stack entries are popped from the stack if P1>0 but
2087 ** remain on the stack if P1<0.
2088 **
2089 ** If P2 is not zero and one or more of the entries are NULL, then jump
2090 ** to P2.  This feature can be used to skip a uniqueness test on indices.
2091 **
2092 ** P3 may be a string that is P1 characters long.  The nth character of the
2093 ** string indicates the column affinity that should be used for the nth
2094 ** field of the index key (i.e. the first character of P3 corresponds to the
2095 ** lowest element on the stack).
2096 **
2097 **  Character      Column affinity
2098 **  ------------------------------
2099 **  'n'            NUMERIC
2100 **  'i'            INTEGER
2101 **  't'            TEXT
2102 **  'o'            NONE
2103 **
2104 ** If P3 is NULL then all index fields have the affinity NONE.
2105 */
2106 case OP_MakeKey:
2107 case OP_MakeIdxKey:
2108 case OP_MakeRecord: {
2109   /* Assuming the record contains N fields, the record format looks
2110   ** like this:
2111   **
2112   ** ------------------------------------------------------------------------
2113   ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2114   ** ------------------------------------------------------------------------
2115   **
2116   ** Data(0) is taken from the lowest element of the stack and data(N-1) is
2117   ** the top of the stack.
2118   **
2119   ** Each type field is a varint representing the serial type of the
2120   ** corresponding data element (see sqlite3VdbeSerialType()). The
2121   ** hdr-size field is also a varint which is the offset from the beginning
2122   ** of the record to data0.
2123   */
2124   int nField = pOp->p1;
2125   unsigned char *zNewRecord;
2126   unsigned char *zCsr;
2127   char *zAffinity;
2128   Mem *pRec;
2129   Mem *pRowid;
2130   int nData = 0;     /* Number of bytes of data space */
2131   int nHdr = 0;      /* Number of bytes of header space */
2132   int nByte = 0;     /* Space required for this record */
2133   int addRowid;      /* True to append a rowid column at the end */
2134   u32 serial_type;   /* Type field */
2135   int containsNull;  /* True if any of the data fields are NULL */
2136   char zTemp[NBFS];  /* Space to hold small records */
2137 
2138   Mem *pData0 = &pTos[1-nField];
2139   assert( pData0>=p->aStack );
2140   zAffinity = pOp->p3;
2141   addRowid = pOp->opcode==OP_MakeIdxKey;
2142   containsNull = 0;
2143 
2144   /* Loop through the elements that will make up the record to figure
2145   ** out how much space is required for the new record.
2146   */
2147   for(pRec=pData0; pRec<=pTos; pRec++){
2148     if( zAffinity ){
2149       applyAffinity(pRec, zAffinity[pRec-pData0], db->enc);
2150     }
2151     if( pRec->flags&MEM_Null ){
2152       containsNull = 1;
2153     }
2154     serial_type = sqlite3VdbeSerialType(pRec);
2155     nData += sqlite3VdbeSerialTypeLen(serial_type);
2156     nHdr += sqlite3VarintLen(serial_type);
2157   }
2158 
2159   /* If we have to append a varint rowid to this record, set 'rowid'
2160   ** to the value of the rowid and increase nByte by the amount of space
2161   ** required to store it and the 0x00 seperator byte.
2162   */
2163   if( addRowid ){
2164     pRowid = &pTos[0-nField];
2165     assert( pRowid>=p->aStack );
2166     Integerify(pRowid, db->enc);
2167     serial_type = sqlite3VdbeSerialType(pRowid);
2168     nData += sqlite3VdbeSerialTypeLen(serial_type);
2169     nHdr += sqlite3VarintLen(serial_type);
2170   }
2171 
2172   /* Add the initial header varint and total the size */
2173   nHdr += sqlite3VarintLen(nHdr);
2174   nByte = nHdr+nData;
2175 
2176   if( nByte>MAX_BYTES_PER_ROW ){
2177     rc = SQLITE_TOOBIG;
2178     goto abort_due_to_error;
2179   }
2180 
2181   /* Allocate space for the new record. */
2182   if( nByte>sizeof(zTemp) ){
2183     zNewRecord = sqliteMallocRaw(nByte);
2184     if( !zNewRecord ){
2185       goto no_mem;
2186     }
2187   }else{
2188     zNewRecord = zTemp;
2189   }
2190 
2191   /* Write the record */
2192   zCsr = zNewRecord;
2193   zCsr += sqlite3PutVarint(zCsr, nHdr);
2194   for(pRec=pData0; pRec<=pTos; pRec++){
2195     serial_type = sqlite3VdbeSerialType(pRec);
2196     zCsr += sqlite3PutVarint(zCsr, serial_type);      /* serial type */
2197   }
2198   if( addRowid ){
2199     zCsr += sqlite3PutVarint(zCsr, sqlite3VdbeSerialType(pRowid));
2200   }
2201   for(pRec=pData0; pRec<=pTos; pRec++){
2202     zCsr += sqlite3VdbeSerialPut(zCsr, pRec);  /* serial data */
2203   }
2204   if( addRowid ){
2205     zCsr += sqlite3VdbeSerialPut(zCsr, pRowid);
2206   }
2207 
2208   /* If zCsr has not been advanced exactly nByte bytes, then one
2209   ** of the sqlite3PutVarint() or sqlite3VdbeSerialPut() calls above
2210   ** failed. This indicates a corrupted memory cell or code bug.
2211   */
2212   if( zCsr!=(zNewRecord+nByte) ){
2213     rc = SQLITE_INTERNAL;
2214     goto abort_due_to_error;
2215   }
2216 
2217   /* Pop nField entries from the stack and push the new entry on */
2218   if( addRowid || pOp->p2==0 ){
2219     popStack(&pTos, nField+addRowid);
2220   }
2221   pTos++;
2222   pTos->n = nByte;
2223   if( nByte<=sizeof(zTemp) ){
2224     assert( zNewRecord==(unsigned char *)zTemp );
2225     pTos->z = pTos->zShort;
2226     memcpy(pTos->zShort, zTemp, nByte);
2227     pTos->flags = MEM_Blob | MEM_Short;
2228   }else{
2229     assert( zNewRecord!=(unsigned char *)zTemp );
2230     pTos->z = zNewRecord;
2231     pTos->flags = MEM_Blob | MEM_Dyn;
2232   }
2233 
2234   /* If P2 is non-zero, and if the key contains a NULL value, and if this
2235   ** was an OP_MakeIdxKey instruction, not OP_MakeKey, jump to P2.
2236   */
2237   if( pOp->p2 && containsNull && addRowid ){
2238     pc = pOp->p2 - 1;
2239   }
2240   break;
2241 }
2242 
2243 /* Opcode: Statement P1 * *
2244 **
2245 ** Begin an individual statement transaction which is part of a larger
2246 ** BEGIN..COMMIT transaction.  This is needed so that the statement
2247 ** can be rolled back after an error without having to roll back the
2248 ** entire transaction.  The statement transaction will automatically
2249 ** commit when the VDBE halts.
2250 **
2251 ** The statement is begun on the database file with index P1.  The main
2252 ** database file has an index of 0 and the file used for temporary tables
2253 ** has an index of 1.
2254 */
2255 case OP_Statement: {
2256   int i = pOp->p1;
2257   Btree *pBt;
2258   if( i>=0 && i<db->nDb && (pBt = db->aDb[i].pBt) && !(db->autoCommit) ){
2259     assert( sqlite3BtreeIsInTrans(pBt) );
2260     if( !sqlite3BtreeIsInStmt(pBt) ){
2261       rc = sqlite3BtreeBeginStmt(pBt);
2262     }
2263   }
2264   break;
2265 }
2266 
2267 /* Opcode: AutoCommit P1 P2 *
2268 **
2269 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
2270 ** back any currently active btree transactions.
2271 */
2272 case OP_AutoCommit: {
2273   u8 i = pOp->p1;
2274   u8 rollback = pOp->p2;
2275 
2276   assert( i==1 || i==0 );
2277   assert( i==1 || rollback==0 );
2278 
2279   if( i!=db->autoCommit ){
2280     db->autoCommit = i;
2281     p->autoCommitOn |= i;
2282     if( pOp->p2 ){
2283       sqlite3RollbackAll(db);
2284     }
2285   }else{
2286     sqlite3SetString(&p->zErrMsg,
2287         (!i)?"cannot start a transaction within a transaction":(
2288         (rollback)?"cannot rollback - no transaction is active":
2289                    "cannot commit - no transaction is active"), 0);
2290 
2291     rc = SQLITE_ERROR;
2292   }
2293   break;
2294 }
2295 
2296 /* Opcode: Transaction P1 P2 *
2297 **
2298 ** Begin a transaction.  The transaction ends when a Commit or Rollback
2299 ** opcode is encountered.  Depending on the ON CONFLICT setting, the
2300 ** transaction might also be rolled back if an error is encountered.
2301 **
2302 ** P1 is the index of the database file on which the transaction is
2303 ** started.  Index 0 is the main database file and index 1 is the
2304 ** file used for temporary tables.
2305 **
2306 ** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
2307 ** obtained on the database file when a write-transaction is started.  No
2308 ** other process can start another write transaction while this transaction is
2309 ** underway.  Starting a write transaction also creates a rollback journal. A
2310 ** write transaction must be started before any changes can be made to the
2311 ** database.
2312 **
2313 ** If P2 is zero, then a read-lock is obtained on the database file.
2314 */
2315 case OP_Transaction: {
2316   int i = pOp->p1;
2317   Btree *pBt;
2318 
2319   assert( i>=0 && i<db->nDb );
2320   pBt = db->aDb[i].pBt;
2321 
2322   if( pBt ){
2323     if( db->nMaster<0 ){
2324       db->nMaster = strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt))+20;
2325     }
2326     rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, db->nMaster);
2327     if( rc==SQLITE_BUSY ){
2328         if( db->busyHandler.xFunc==0 ){
2329           p->pc = pc;
2330           p->rc = SQLITE_BUSY;
2331           p->pTos = pTos;
2332           return SQLITE_BUSY;
2333         }else{
2334           sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
2335         }
2336     }
2337     if( rc!=SQLITE_OK && rc!=SQLITE_READONLY && rc!=SQLITE_BUSY ){
2338       goto abort_due_to_error;
2339     }
2340   }
2341   break;
2342 }
2343 
2344 /* Opcode: ReadCookie P1 P2 *
2345 **
2346 ** Read cookie number P2 from database P1 and push it onto the stack.
2347 ** P2==0 is the schema version.  P2==1 is the database format.
2348 ** P2==2 is the recommended pager cache size, and so forth.  P1==0 is
2349 ** the main database file and P1==1 is the database file used to store
2350 ** temporary tables.
2351 **
2352 ** There must be a read-lock on the database (either a transaction
2353 ** must be started or there must be an open cursor) before
2354 ** executing this instruction.
2355 */
2356 case OP_ReadCookie: {
2357   int iMeta;
2358   assert( pOp->p2<SQLITE_N_BTREE_META );
2359   assert( pOp->p1>=0 && pOp->p1<db->nDb );
2360   assert( db->aDb[pOp->p1].pBt!=0 );
2361   /* The indexing of meta values at the schema layer is off by one from
2362   ** the indexing in the btree layer.  The btree considers meta[0] to
2363   ** be the number of free pages in the database (a read-only value)
2364   ** and meta[1] to be the schema cookie.  The schema layer considers
2365   ** meta[1] to be the schema cookie.  So we have to shift the index
2366   ** by one in the following statement.
2367   */
2368   rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1 + pOp->p2, &iMeta);
2369   pTos++;
2370   pTos->i = iMeta;
2371   pTos->flags = MEM_Int;
2372   break;
2373 }
2374 
2375 /* Opcode: SetCookie P1 P2 *
2376 **
2377 ** Write the top of the stack into cookie number P2 of database P1.
2378 ** P2==0 is the schema version.  P2==1 is the database format.
2379 ** P2==2 is the recommended pager cache size, and so forth.  P1==0 is
2380 ** the main database file and P1==1 is the database file used to store
2381 ** temporary tables.
2382 **
2383 ** A transaction must be started before executing this opcode.
2384 */
2385 case OP_SetCookie: {
2386   assert( pOp->p2<SQLITE_N_BTREE_META );
2387   assert( pOp->p1>=0 && pOp->p1<db->nDb );
2388   assert( db->aDb[pOp->p1].pBt!=0 );
2389   assert( pTos>=p->aStack );
2390   Integerify(pTos, db->enc);
2391   /* See note about index shifting on OP_ReadCookie */
2392   rc = sqlite3BtreeUpdateMeta(db->aDb[pOp->p1].pBt, 1+pOp->p2, (int)pTos->i);
2393   Release(pTos);
2394   pTos--;
2395   break;
2396 }
2397 
2398 /* Opcode: VerifyCookie P1 P2 *
2399 **
2400 ** Check the value of global database parameter number 0 (the
2401 ** schema version) and make sure it is equal to P2.
2402 ** P1 is the database number which is 0 for the main database file
2403 ** and 1 for the file holding temporary tables and some higher number
2404 ** for auxiliary databases.
2405 **
2406 ** The cookie changes its value whenever the database schema changes.
2407 ** This operation is used to detect when that the cookie has changed
2408 ** and that the current process needs to reread the schema.
2409 **
2410 ** Either a transaction needs to have been started or an OP_Open needs
2411 ** to be executed (to establish a read lock) before this opcode is
2412 ** invoked.
2413 */
2414 case OP_VerifyCookie: {
2415   int iMeta;
2416   assert( pOp->p1>=0 && pOp->p1<db->nDb );
2417   rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1, &iMeta);
2418   if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
2419     sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0);
2420     rc = SQLITE_SCHEMA;
2421   }
2422   break;
2423 }
2424 
2425 /* Opcode: OpenRead P1 P2 P3
2426 **
2427 ** Open a read-only cursor for the database table whose root page is
2428 ** P2 in a database file.  The database file is determined by an
2429 ** integer from the top of the stack.  0 means the main database and
2430 ** 1 means the database used for temporary tables.  Give the new
2431 ** cursor an identifier of P1.  The P1 values need not be contiguous
2432 ** but all P1 values should be small integers.  It is an error for
2433 ** P1 to be negative.
2434 **
2435 ** If P2==0 then take the root page number from the next of the stack.
2436 **
2437 ** There will be a read lock on the database whenever there is an
2438 ** open cursor.  If the database was unlocked prior to this instruction
2439 ** then a read lock is acquired as part of this instruction.  A read
2440 ** lock allows other processes to read the database but prohibits
2441 ** any other process from modifying the database.  The read lock is
2442 ** released when all cursors are closed.  If this instruction attempts
2443 ** to get a read lock but fails, the script terminates with an
2444 ** SQLITE_BUSY error code.
2445 **
2446 ** The P3 value is a pointer to a KeyInfo structure that defines the
2447 ** content and collating sequence of indices.  P3 is NULL for cursors
2448 ** that are not pointing to indices.
2449 **
2450 ** See also OpenWrite.
2451 */
2452 /* Opcode: OpenWrite P1 P2 P3
2453 **
2454 ** Open a read/write cursor named P1 on the table or index whose root
2455 ** page is P2.  If P2==0 then take the root page number from the stack.
2456 **
2457 ** The P3 value is a pointer to a KeyInfo structure that defines the
2458 ** content and collating sequence of indices.  P3 is NULL for cursors
2459 ** that are not pointing to indices.
2460 **
2461 ** This instruction works just like OpenRead except that it opens the cursor
2462 ** in read/write mode.  For a given table, there can be one or more read-only
2463 ** cursors or a single read/write cursor but not both.
2464 **
2465 ** See also OpenRead.
2466 */
2467 case OP_OpenRead:
2468 case OP_OpenWrite: {
2469   int i = pOp->p1;
2470   int p2 = pOp->p2;
2471   int wrFlag;
2472   Btree *pX;
2473   int iDb;
2474   Cursor *pCur;
2475 
2476   assert( pTos>=p->aStack );
2477   Integerify(pTos, db->enc);
2478   iDb = pTos->i;
2479   pTos--;
2480   assert( iDb>=0 && iDb<db->nDb );
2481   pX = db->aDb[iDb].pBt;
2482   assert( pX!=0 );
2483   wrFlag = pOp->opcode==OP_OpenWrite;
2484   if( p2<=0 ){
2485     assert( pTos>=p->aStack );
2486     Integerify(pTos, db->enc);
2487     p2 = pTos->i;
2488     pTos--;
2489     if( p2<2 ){
2490       sqlite3SetString(&p->zErrMsg, "root page number less than 2", (char*)0);
2491       rc = SQLITE_INTERNAL;
2492       break;
2493     }
2494   }
2495   assert( i>=0 );
2496   if( expandCursorArraySize(p, i) ) goto no_mem;
2497   pCur = p->apCsr[i];
2498   sqlite3VdbeCleanupCursor(pCur);
2499   pCur->nullRow = 1;
2500   if( pX==0 ) break;
2501   /* We always provide a key comparison function.  If the table being
2502   ** opened is of type INTKEY, the comparision function will be ignored. */
2503   rc = sqlite3BtreeCursor(pX, p2, wrFlag,
2504            sqlite3VdbeRecordCompare, pOp->p3,
2505            &pCur->pCursor);
2506   pCur->pKeyInfo = (KeyInfo*)pOp->p3;
2507   if( pCur->pKeyInfo ){
2508     pCur->pIncrKey = &pCur->pKeyInfo->incrKey;
2509     pCur->pKeyInfo->enc = p->db->enc;
2510   }else{
2511     pCur->pIncrKey = &pCur->bogusIncrKey;
2512   }
2513   switch( rc ){
2514     case SQLITE_BUSY: {
2515       if( db->busyHandler.xFunc ){
2516         p->pc = pc;
2517         p->rc = SQLITE_BUSY;
2518         p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
2519         return SQLITE_BUSY;
2520       }else{
2521         sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
2522       }
2523       break;
2524     }
2525     case SQLITE_OK: {
2526       int flags = sqlite3BtreeFlags(pCur->pCursor);
2527       pCur->intKey = (flags & BTREE_INTKEY)!=0;
2528       pCur->zeroData = (flags & BTREE_ZERODATA)!=0;
2529       break;
2530     }
2531     case SQLITE_EMPTY: {
2532       rc = SQLITE_OK;
2533       break;
2534     }
2535     default: {
2536       goto abort_due_to_error;
2537     }
2538   }
2539   break;
2540 }
2541 
2542 /* Opcode: OpenTemp P1 * P3
2543 **
2544 ** Open a new cursor to a transient table.
2545 ** The transient cursor is always opened read/write even if
2546 ** the main database is read-only.  The transient table is deleted
2547 ** automatically when the cursor is closed.
2548 **
2549 ** The cursor points to a BTree table if P3==0 and to a BTree index
2550 ** if P3 is not 0.  If P3 is not NULL, it points to a KeyInfo structure
2551 ** that defines the format of keys in the index.
2552 **
2553 ** This opcode is used for tables that exist for the duration of a single
2554 ** SQL statement only.  Tables created using CREATE TEMPORARY TABLE
2555 ** are opened using OP_OpenRead or OP_OpenWrite.  "Temporary" in the
2556 ** context of this opcode means for the duration of a single SQL statement
2557 ** whereas "Temporary" in the context of CREATE TABLE means for the duration
2558 ** of the connection to the database.  Same word; different meanings.
2559 */
2560 case OP_OpenTemp: {
2561   int i = pOp->p1;
2562   Cursor *pCx;
2563   assert( i>=0 );
2564   if( expandCursorArraySize(p, i) ) goto no_mem;
2565   pCx = p->apCsr[i];
2566   sqlite3VdbeCleanupCursor(pCx);
2567   memset(pCx, 0, sizeof(*pCx));
2568   pCx->nullRow = 1;
2569   rc = sqlite3BtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt);
2570 
2571   if( rc==SQLITE_OK ){
2572     rc = sqlite3BtreeBeginTrans(pCx->pBt, 1, 0);
2573   }
2574   if( rc==SQLITE_OK ){
2575     /* If a transient index is required, create it by calling
2576     ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
2577     ** opening it. If a transient table is required, just use the
2578     ** automatically created table with root-page 1 (an INTKEY table).
2579     */
2580     if( pOp->p3 ){
2581       int pgno;
2582       assert( pOp->p3type==P3_KEYINFO );
2583       rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
2584       if( rc==SQLITE_OK ){
2585         assert( pgno==MASTER_ROOT+1 );
2586         rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeRecordCompare,
2587             pOp->p3, &pCx->pCursor);
2588         pCx->pKeyInfo = (KeyInfo*)pOp->p3;
2589         pCx->pKeyInfo->enc = p->db->enc;
2590         pCx->pIncrKey = &pCx->pKeyInfo->incrKey;
2591       }
2592     }else{
2593       rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor);
2594       pCx->intKey = 1;
2595       pCx->pIncrKey = &pCx->bogusIncrKey;
2596     }
2597   }
2598   break;
2599 }
2600 
2601 /* Opcode: OpenPseudo P1 * *
2602 **
2603 ** Open a new cursor that points to a fake table that contains a single
2604 ** row of data.  Any attempt to write a second row of data causes the
2605 ** first row to be deleted.  All data is deleted when the cursor is
2606 ** closed.
2607 **
2608 ** A pseudo-table created by this opcode is useful for holding the
2609 ** NEW or OLD tables in a trigger.
2610 */
2611 case OP_OpenPseudo: {
2612   int i = pOp->p1;
2613   Cursor *pCx;
2614   assert( i>=0 );
2615   if( expandCursorArraySize(p, i) ) goto no_mem;
2616   pCx = p->apCsr[i];
2617   sqlite3VdbeCleanupCursor(pCx);
2618   memset(pCx, 0, sizeof(*pCx));
2619   pCx->nullRow = 1;
2620   pCx->pseudoTable = 1;
2621   pCx->pIncrKey = &pCx->bogusIncrKey;
2622   break;
2623 }
2624 
2625 /* Opcode: Close P1 * *
2626 **
2627 ** Close a cursor previously opened as P1.  If P1 is not
2628 ** currently open, this instruction is a no-op.
2629 */
2630 case OP_Close: {
2631   int i = pOp->p1;
2632   if( i>=0 && i<p->nCursor ){
2633     sqlite3VdbeCleanupCursor(p->apCsr[i]);
2634   }
2635   break;
2636 }
2637 
2638 /* Opcode: MoveGe P1 P2 *
2639 **
2640 ** Pop the top of the stack and use its value as a key.  Reposition
2641 ** cursor P1 so that it points to the smallest entry that is greater
2642 ** than or equal to the key that was popped ffrom the stack.
2643 ** If there are no records greater than or equal to the key and P2
2644 ** is not zero, then jump to P2.
2645 **
2646 ** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe
2647 */
2648 /* Opcode: MoveGt P1 P2 *
2649 **
2650 ** Pop the top of the stack and use its value as a key.  Reposition
2651 ** cursor P1 so that it points to the smallest entry that is greater
2652 ** than the key from the stack.
2653 ** If there are no records greater than the key and P2 is not zero,
2654 ** then jump to P2.
2655 **
2656 ** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe
2657 */
2658 /* Opcode: MoveLt P1 P2 *
2659 **
2660 ** Pop the top of the stack and use its value as a key.  Reposition
2661 ** cursor P1 so that it points to the largest entry that is less
2662 ** than the key from the stack.
2663 ** If there are no records less than the key and P2 is not zero,
2664 ** then jump to P2.
2665 **
2666 ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe
2667 */
2668 /* Opcode: MoveLe P1 P2 *
2669 **
2670 ** Pop the top of the stack and use its value as a key.  Reposition
2671 ** cursor P1 so that it points to the largest entry that is less than
2672 ** or equal to the key that was popped from the stack.
2673 ** If there are no records less than or eqal to the key and P2 is not zero,
2674 ** then jump to P2.
2675 **
2676 ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt
2677 */
2678 case OP_MoveLt:
2679 case OP_MoveLe:
2680 case OP_MoveGe:
2681 case OP_MoveGt: {
2682   int i = pOp->p1;
2683   Cursor *pC;
2684 
2685   assert( pTos>=p->aStack );
2686   assert( i>=0 && i<p->nCursor );
2687   pC = p->apCsr[i];
2688   if( pC->pCursor!=0 ){
2689     int res, oc;
2690     oc = pOp->opcode;
2691     pC->nullRow = 0;
2692     *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe;
2693     if( pC->intKey ){
2694       i64 iKey;
2695       assert( !pOp->p3 );
2696       Integerify(pTos, db->enc);
2697       iKey = intToKey(pTos->i);
2698       if( pOp->p2==0 && pOp->opcode==OP_MoveGe ){
2699         pC->movetoTarget = iKey;
2700         pC->deferredMoveto = 1;
2701         Release(pTos);
2702         pTos--;
2703         break;
2704       }
2705       sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, &res);
2706       pC->lastRecno = pTos->i;
2707       pC->recnoIsValid = res==0;
2708     }else{
2709       Stringify(pTos, db->enc);
2710       sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
2711       pC->recnoIsValid = 0;
2712     }
2713     pC->deferredMoveto = 0;
2714     pC->cacheValid = 0;
2715     *pC->pIncrKey = 0;
2716     sqlite3_search_count++;
2717     if( oc==OP_MoveGe || oc==OP_MoveGt ){
2718       if( res<0 ){
2719         sqlite3BtreeNext(pC->pCursor, &res);
2720         pC->recnoIsValid = 0;
2721         if( res && pOp->p2>0 ){
2722           pc = pOp->p2 - 1;
2723         }
2724       }
2725     }else{
2726       assert( oc==OP_MoveLt || oc==OP_MoveLe );
2727       if( res>=0 ){
2728         sqlite3BtreePrevious(pC->pCursor, &res);
2729         pC->recnoIsValid = 0;
2730       }else{
2731         /* res might be negative because the table is empty.  Check to
2732         ** see if this is the case.
2733         */
2734         res = sqlite3BtreeEof(pC->pCursor);
2735       }
2736       if( res && pOp->p2>0 ){
2737         pc = pOp->p2 - 1;
2738       }
2739     }
2740   }
2741   Release(pTos);
2742   pTos--;
2743   break;
2744 }
2745 
2746 /* Opcode: Distinct P1 P2 *
2747 **
2748 ** Use the top of the stack as a string key.  If a record with that key does
2749 ** not exist in the table of cursor P1, then jump to P2.  If the record
2750 ** does already exist, then fall thru.  The cursor is left pointing
2751 ** at the record if it exists. The key is not popped from the stack.
2752 **
2753 ** This operation is similar to NotFound except that this operation
2754 ** does not pop the key from the stack.
2755 **
2756 ** See also: Found, NotFound, MoveTo, IsUnique, NotExists
2757 */
2758 /* Opcode: Found P1 P2 *
2759 **
2760 ** Use the top of the stack as a string key.  If a record with that key
2761 ** does exist in table of P1, then jump to P2.  If the record
2762 ** does not exist, then fall thru.  The cursor is left pointing
2763 ** to the record if it exists.  The key is popped from the stack.
2764 **
2765 ** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists
2766 */
2767 /* Opcode: NotFound P1 P2 *
2768 **
2769 ** Use the top of the stack as a string key.  If a record with that key
2770 ** does not exist in table of P1, then jump to P2.  If the record
2771 ** does exist, then fall thru.  The cursor is left pointing to the
2772 ** record if it exists.  The key is popped from the stack.
2773 **
2774 ** The difference between this operation and Distinct is that
2775 ** Distinct does not pop the key from the stack.
2776 **
2777 ** See also: Distinct, Found, MoveTo, NotExists, IsUnique
2778 */
2779 case OP_Distinct:
2780 case OP_NotFound:
2781 case OP_Found: {
2782   int i = pOp->p1;
2783   int alreadyExists = 0;
2784   Cursor *pC;
2785   assert( pTos>=p->aStack );
2786   assert( i>=0 && i<p->nCursor );
2787   if( (pC = p->apCsr[i])->pCursor!=0 ){
2788     int res, rx;
2789     assert( pC->intKey==0 );
2790     Stringify(pTos, db->enc);
2791     rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
2792     alreadyExists = rx==SQLITE_OK && res==0;
2793     pC->deferredMoveto = 0;
2794     pC->cacheValid = 0;
2795   }
2796   if( pOp->opcode==OP_Found ){
2797     if( alreadyExists ) pc = pOp->p2 - 1;
2798   }else{
2799     if( !alreadyExists ) pc = pOp->p2 - 1;
2800   }
2801   if( pOp->opcode!=OP_Distinct ){
2802     Release(pTos);
2803     pTos--;
2804   }
2805   break;
2806 }
2807 
2808 /* Opcode: IsUnique P1 P2 *
2809 **
2810 ** The top of the stack is an integer record number.  Call this
2811 ** record number R.  The next on the stack is an index key created
2812 ** using MakeIdxKey.  Call it K.  This instruction pops R from the
2813 ** stack but it leaves K unchanged.
2814 **
2815 ** P1 is an index.  So it has no data and its key consists of a
2816 ** record generated by OP_MakeIdxKey.  This key contains one or more
2817 ** fields followed by a ROWID field.
2818 **
2819 ** This instruction asks if there is an entry in P1 where the
2820 ** fields matches K but the rowid is different from R.
2821 ** If there is no such entry, then there is an immediate
2822 ** jump to P2.  If any entry does exist where the index string
2823 ** matches K but the record number is not R, then the record
2824 ** number for that entry is pushed onto the stack and control
2825 ** falls through to the next instruction.
2826 **
2827 ** See also: Distinct, NotFound, NotExists, Found
2828 */
2829 case OP_IsUnique: {
2830   int i = pOp->p1;
2831   Mem *pNos = &pTos[-1];
2832   Cursor *pCx;
2833   BtCursor *pCrsr;
2834   i64 R;
2835 
2836   /* Pop the value R off the top of the stack
2837   */
2838   assert( pNos>=p->aStack );
2839   Integerify(pTos, db->enc);
2840   R = pTos->i;
2841   pTos--;
2842   assert( i>=0 && i<=p->nCursor );
2843   pCx = p->apCsr[i];
2844   pCrsr = pCx->pCursor;
2845   if( pCrsr!=0 ){
2846     int res, rc;
2847     i64 v;         /* The record number on the P1 entry that matches K */
2848     char *zKey;    /* The value of K */
2849     int nKey;      /* Number of bytes in K */
2850     int len;       /* Number of bytes in K without the rowid at the end */
2851     int szRowid;   /* Size of the rowid column at the end of zKey */
2852 
2853     /* Make sure K is a string and make zKey point to K
2854     */
2855     Stringify(pNos, db->enc);
2856     zKey = pNos->z;
2857     nKey = pNos->n;
2858 
2859     szRowid = sqlite3VdbeIdxRowidLen(nKey, zKey);
2860     len = nKey-szRowid;
2861 
2862     /* Search for an entry in P1 where all but the last four bytes match K.
2863     ** If there is no such entry, jump immediately to P2.
2864     */
2865     assert( pCx->deferredMoveto==0 );
2866     pCx->cacheValid = 0;
2867     rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res);
2868     if( rc!=SQLITE_OK ) goto abort_due_to_error;
2869     if( res<0 ){
2870       rc = sqlite3BtreeNext(pCrsr, &res);
2871       if( res ){
2872         pc = pOp->p2 - 1;
2873         break;
2874       }
2875     }
2876     rc = sqlite3VdbeIdxKeyCompare(pCx, len, zKey, &res);
2877     if( rc!=SQLITE_OK ) goto abort_due_to_error;
2878     if( res>0 ){
2879       pc = pOp->p2 - 1;
2880       break;
2881     }
2882 
2883     /* At this point, pCrsr is pointing to an entry in P1 where all but
2884     ** the final entry (the rowid) matches K.  Check to see if the
2885     ** final rowid column is different from R.  If it equals R then jump
2886     ** immediately to P2.
2887     */
2888     rc = sqlite3VdbeIdxRowid(pCrsr, &v);
2889     if( rc!=SQLITE_OK ){
2890       goto abort_due_to_error;
2891     }
2892     if( v==R ){
2893       pc = pOp->p2 - 1;
2894       break;
2895     }
2896 
2897     /* The final varint of the key is different from R.  Push it onto
2898     ** the stack.  (The record number of an entry that violates a UNIQUE
2899     ** constraint.)
2900     */
2901     pTos++;
2902     pTos->i = v;
2903     pTos->flags = MEM_Int;
2904   }
2905   break;
2906 }
2907 
2908 /* Opcode: NotExists P1 P2 *
2909 **
2910 ** Use the top of the stack as a integer key.  If a record with that key
2911 ** does not exist in table of P1, then jump to P2.  If the record
2912 ** does exist, then fall thru.  The cursor is left pointing to the
2913 ** record if it exists.  The integer key is popped from the stack.
2914 **
2915 ** The difference between this operation and NotFound is that this
2916 ** operation assumes the key is an integer and NotFound assumes it
2917 ** is a string.
2918 **
2919 ** See also: Distinct, Found, MoveTo, NotFound, IsUnique
2920 */
2921 case OP_NotExists: {
2922   int i = pOp->p1;
2923   Cursor *pC;
2924   BtCursor *pCrsr;
2925   assert( pTos>=p->aStack );
2926   assert( i>=0 && i<p->nCursor );
2927   if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
2928     int res, rx;
2929     u64 iKey;
2930     assert( pTos->flags & MEM_Int );
2931     assert( p->apCsr[i]->intKey );
2932     iKey = intToKey(pTos->i);
2933     rx = sqlite3BtreeMoveto(pCrsr, 0, iKey, &res);
2934     pC->lastRecno = pTos->i;
2935     pC->recnoIsValid = res==0;
2936     pC->nullRow = 0;
2937     pC->cacheValid = 0;
2938     if( rx!=SQLITE_OK || res!=0 ){
2939       pc = pOp->p2 - 1;
2940       pC->recnoIsValid = 0;
2941     }
2942   }
2943   Release(pTos);
2944   pTos--;
2945   break;
2946 }
2947 
2948 /* Opcode: NewRecno P1 * *
2949 **
2950 ** Get a new integer record number used as the key to a table.
2951 ** The record number is not previously used as a key in the database
2952 ** table that cursor P1 points to.  The new record number is pushed
2953 ** onto the stack.
2954 */
2955 case OP_NewRecno: {
2956   int i = pOp->p1;
2957   i64 v = 0;
2958   Cursor *pC;
2959   assert( i>=0 && i<p->nCursor );
2960   if( (pC = p->apCsr[i])->pCursor==0 ){
2961     /* The zero initialization above is all that is needed */
2962   }else{
2963     /* The next rowid or record number (different terms for the same
2964     ** thing) is obtained in a two-step algorithm.
2965     **
2966     ** First we attempt to find the largest existing rowid and add one
2967     ** to that.  But if the largest existing rowid is already the maximum
2968     ** positive integer, we have to fall through to the second
2969     ** probabilistic algorithm
2970     **
2971     ** The second algorithm is to select a rowid at random and see if
2972     ** it already exists in the table.  If it does not exist, we have
2973     ** succeeded.  If the random rowid does exist, we select a new one
2974     ** and try again, up to 1000 times.
2975     **
2976     ** For a table with less than 2 billion entries, the probability
2977     ** of not finding a unused rowid is about 1.0e-300.  This is a
2978     ** non-zero probability, but it is still vanishingly small and should
2979     ** never cause a problem.  You are much, much more likely to have a
2980     ** hardware failure than for this algorithm to fail.
2981     **
2982     ** The analysis in the previous paragraph assumes that you have a good
2983     ** source of random numbers.  Is a library function like lrand48()
2984     ** good enough?  Maybe. Maybe not. It's hard to know whether there
2985     ** might be subtle bugs is some implementations of lrand48() that
2986     ** could cause problems. To avoid uncertainty, SQLite uses its own
2987     ** random number generator based on the RC4 algorithm.
2988     **
2989     ** To promote locality of reference for repetitive inserts, the
2990     ** first few attempts at chosing a random rowid pick values just a little
2991     ** larger than the previous rowid.  This has been shown experimentally
2992     ** to double the speed of the COPY operation.
2993     */
2994     int res, rx, cnt;
2995     i64 x;
2996     cnt = 0;
2997     assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
2998     assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
2999     if( !pC->useRandomRowid ){
3000       if( pC->nextRowidValid ){
3001         v = pC->nextRowid;
3002       }else{
3003         rx = sqlite3BtreeLast(pC->pCursor, &res);
3004         if( res ){
3005           v = 1;
3006         }else{
3007           sqlite3BtreeKeySize(pC->pCursor, (u64*)&v);
3008           v = keyToInt(v);
3009           if( v==0x7fffffffffffffff ){
3010             pC->useRandomRowid = 1;
3011           }else{
3012             v++;
3013           }
3014         }
3015       }
3016       if( v<0x7fffffffffffffff ){
3017         pC->nextRowidValid = 1;
3018         pC->nextRowid = v+1;
3019       }else{
3020         pC->nextRowidValid = 0;
3021       }
3022     }
3023     if( pC->useRandomRowid ){
3024       v = db->priorNewRowid;
3025       cnt = 0;
3026       do{
3027         if( v==0 || cnt>2 ){
3028           sqlite3Randomness(sizeof(v), &v);
3029           if( cnt<5 ) v &= 0xffffff;
3030         }else{
3031           unsigned char r;
3032           sqlite3Randomness(1, &r);
3033           v += r + 1;
3034         }
3035         if( v==0 ) continue;
3036         x = intToKey(v);
3037         rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, &res);
3038         cnt++;
3039       }while( cnt<1000 && rx==SQLITE_OK && res==0 );
3040       db->priorNewRowid = v;
3041       if( rx==SQLITE_OK && res==0 ){
3042         rc = SQLITE_FULL;
3043         goto abort_due_to_error;
3044       }
3045     }
3046     pC->recnoIsValid = 0;
3047     pC->deferredMoveto = 0;
3048     pC->cacheValid = 0;
3049   }
3050   pTos++;
3051   pTos->i = v;
3052   pTos->flags = MEM_Int;
3053   break;
3054 }
3055 
3056 /* Opcode: PutIntKey P1 P2 *
3057 **
3058 ** Write an entry into the table of cursor P1.  A new entry is
3059 ** created if it doesn't already exist or the data for an existing
3060 ** entry is overwritten.  The data is the value on the top of the
3061 ** stack.  The key is the next value down on the stack.  The key must
3062 ** be an integer.  The stack is popped twice by this instruction.
3063 **
3064 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
3065 ** incremented (otherwise not).  If the OPFLAG_CSCHANGE flag is set,
3066 ** then the current statement change count is incremented (otherwise not).
3067 ** If the OPFLAG_LASTROWID flag of P2 is set, then rowid is
3068 ** stored for subsequent return by the sqlite3_last_insert_rowid() function
3069 ** (otherwise it's unmodified).
3070 */
3071 /* Opcode: PutStrKey P1 * *
3072 **
3073 ** Write an entry into the table of cursor P1.  A new entry is
3074 ** created if it doesn't already exist or the data for an existing
3075 ** entry is overwritten.  The data is the value on the top of the
3076 ** stack.  The key is the next value down on the stack.  The key must
3077 ** be a string.  The stack is popped twice by this instruction.
3078 **
3079 ** P1 may not be a pseudo-table opened using the OpenPseudo opcode.
3080 */
3081 case OP_PutIntKey:
3082 case OP_PutStrKey: {
3083   Mem *pNos = &pTos[-1];
3084   int i = pOp->p1;
3085   Cursor *pC;
3086   assert( pNos>=p->aStack );
3087   assert( i>=0 && i<p->nCursor );
3088   if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){
3089     char *zKey;
3090     i64 nKey;
3091     i64 iKey;
3092     if( pOp->opcode==OP_PutStrKey ){
3093       Stringify(pNos, db->enc);
3094       nKey = pNos->n;
3095       zKey = pNos->z;
3096     }else{
3097       assert( pNos->flags & MEM_Int );
3098 
3099       /* If the table is an INTKEY table, set nKey to the value of
3100       ** the integer key, and zKey to NULL. Otherwise, set nKey to
3101       ** sizeof(i64) and point zKey at iKey. iKey contains the integer
3102       ** key in the on-disk byte order.
3103       */
3104       iKey = intToKey(pNos->i);
3105       if( pC->intKey ){
3106         nKey = intToKey(pNos->i);
3107         zKey = 0;
3108       }else{
3109         nKey = sizeof(i64);
3110         zKey = (char*)&iKey;
3111       }
3112 
3113       if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
3114       if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i;
3115       if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
3116       if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){
3117         pC->nextRowidValid = 0;
3118       }
3119     }
3120     if( pTos->flags & MEM_Null ){
3121       pTos->z = 0;
3122       pTos->n = 0;
3123     }else{
3124       assert( pTos->flags & (MEM_Blob|MEM_Str) );
3125     }
3126     if( pC->pseudoTable ){
3127       /* PutStrKey does not work for pseudo-tables.
3128       ** The following assert makes sure we are not trying to use
3129       ** PutStrKey on a pseudo-table
3130       */
3131       assert( pOp->opcode==OP_PutIntKey );
3132       sqliteFree(pC->pData);
3133       pC->iKey = iKey;
3134       pC->nData = pTos->n;
3135       if( pTos->flags & MEM_Dyn ){
3136         pC->pData = pTos->z;
3137         pTos->flags = MEM_Null;
3138       }else{
3139         pC->pData = sqliteMallocRaw( pC->nData+2 );
3140         if( pC->pData ){
3141           memcpy(pC->pData, pTos->z, pC->nData);
3142         }
3143         pC->pData[pC->nData] = 0;
3144         pC->pData[pC->nData+1] = 0;
3145       }
3146       pC->nullRow = 0;
3147     }else{
3148       rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, pTos->z, pTos->n);
3149     }
3150     pC->recnoIsValid = 0;
3151     pC->deferredMoveto = 0;
3152     pC->cacheValid = 0;
3153   }
3154   popStack(&pTos, 2);
3155   break;
3156 }
3157 
3158 /* Opcode: Delete P1 P2 *
3159 **
3160 ** Delete the record at which the P1 cursor is currently pointing.
3161 **
3162 ** The cursor will be left pointing at either the next or the previous
3163 ** record in the table. If it is left pointing at the next record, then
3164 ** the next Next instruction will be a no-op.  Hence it is OK to delete
3165 ** a record from within an Next loop.
3166 **
3167 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
3168 ** incremented (otherwise not).  If OPFLAG_CSCHANGE flag is set,
3169 ** then the current statement change count is incremented (otherwise not).
3170 **
3171 ** If P1 is a pseudo-table, then this instruction is a no-op.
3172 */
3173 case OP_Delete: {
3174   int i = pOp->p1;
3175   Cursor *pC;
3176   assert( i>=0 && i<p->nCursor );
3177   pC = p->apCsr[i];
3178   if( pC->pCursor!=0 ){
3179     sqlite3VdbeCursorMoveto(pC);
3180     rc = sqlite3BtreeDelete(pC->pCursor);
3181     pC->nextRowidValid = 0;
3182     pC->cacheValid = 0;
3183   }
3184   if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
3185   if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
3186   break;
3187 }
3188 
3189 /* Opcode: SetCounts * * *
3190 **
3191 ** Called at end of statement.  Updates lsChange (last statement change count)
3192 ** and resets csChange (current statement change count) to 0.
3193 */
3194 case OP_SetCounts: {
3195   db->lsChange=db->csChange;
3196   db->csChange=0;
3197   break;
3198 }
3199 
3200 /* Opcode: KeyAsData P1 P2 *
3201 **
3202 ** Turn the key-as-data mode for cursor P1 either on (if P2==1) or
3203 ** off (if P2==0).  In key-as-data mode, the OP_Column opcode pulls
3204 ** data off of the key rather than the data.  This is used for
3205 ** processing compound selects.
3206 */
3207 case OP_KeyAsData: {
3208   int i = pOp->p1;
3209   Cursor *pC;
3210   assert( i>=0 && i<p->nCursor );
3211   pC = p->apCsr[i];
3212   pC->keyAsData = pOp->p2;
3213   break;
3214 }
3215 
3216 /* Opcode: RowData P1 * *
3217 **
3218 ** Push onto the stack the complete row data for cursor P1.
3219 ** There is no interpretation of the data.  It is just copied
3220 ** onto the stack exactly as it is found in the database file.
3221 **
3222 ** If the cursor is not pointing to a valid row, a NULL is pushed
3223 ** onto the stack.
3224 */
3225 /* Opcode: RowKey P1 * *
3226 **
3227 ** Push onto the stack the complete row key for cursor P1.
3228 ** There is no interpretation of the key.  It is just copied
3229 ** onto the stack exactly as it is found in the database file.
3230 **
3231 ** If the cursor is not pointing to a valid row, a NULL is pushed
3232 ** onto the stack.
3233 */
3234 case OP_RowKey:
3235 case OP_RowData: {
3236   int i = pOp->p1;
3237   Cursor *pC;
3238   int n;
3239 
3240   pTos++;
3241   assert( i>=0 && i<p->nCursor );
3242   pC = p->apCsr[i];
3243   if( pC->nullRow ){
3244     pTos->flags = MEM_Null;
3245   }else if( pC->pCursor!=0 ){
3246     BtCursor *pCrsr = pC->pCursor;
3247     sqlite3VdbeCursorMoveto(pC);
3248     if( pC->nullRow ){
3249       pTos->flags = MEM_Null;
3250       break;
3251     }else if( pC->keyAsData || pOp->opcode==OP_RowKey ){
3252       i64 n64;
3253       assert( !pC->intKey );
3254       sqlite3BtreeKeySize(pCrsr, &n64);
3255       n = n64;
3256     }else{
3257       sqlite3BtreeDataSize(pCrsr, &n);
3258     }
3259     pTos->n = n;
3260     if( n<=NBFS ){
3261       pTos->flags = MEM_Blob | MEM_Short;
3262       pTos->z = pTos->zShort;
3263     }else{
3264       char *z = sqliteMallocRaw( n );
3265       if( z==0 ) goto no_mem;
3266       pTos->flags = MEM_Blob | MEM_Dyn;
3267       pTos->z = z;
3268     }
3269     if( pC->keyAsData || pOp->opcode==OP_RowKey ){
3270       sqlite3BtreeKey(pCrsr, 0, n, pTos->z);
3271     }else{
3272       sqlite3BtreeData(pCrsr, 0, n, pTos->z);
3273     }
3274   }else if( pC->pseudoTable ){
3275     pTos->n = pC->nData;
3276     pTos->z = pC->pData;
3277     pTos->flags = MEM_Blob|MEM_Ephem;
3278   }else{
3279     pTos->flags = MEM_Null;
3280   }
3281   break;
3282 }
3283 
3284 /* Opcode: Recno P1 * *
3285 **
3286 ** Push onto the stack an integer which is the first 4 bytes of the
3287 ** the key to the current entry in a sequential scan of the database
3288 ** file P1.  The sequential scan should have been started using the
3289 ** Next opcode.
3290 */
3291 case OP_Recno: {
3292   int i = pOp->p1;
3293   Cursor *pC;
3294   i64 v;
3295 
3296   assert( i>=0 && i<p->nCursor );
3297   pC = p->apCsr[i];
3298   sqlite3VdbeCursorMoveto(pC);
3299   pTos++;
3300   if( pC->recnoIsValid ){
3301     v = pC->lastRecno;
3302   }else if( pC->pseudoTable ){
3303     v = keyToInt(pC->iKey);
3304   }else if( pC->nullRow || pC->pCursor==0 ){
3305     pTos->flags = MEM_Null;
3306     break;
3307   }else{
3308     assert( pC->pCursor!=0 );
3309     sqlite3BtreeKeySize(pC->pCursor, (u64*)&v);
3310     v = keyToInt(v);
3311   }
3312   pTos->i = v;
3313   pTos->flags = MEM_Int;
3314   break;
3315 }
3316 
3317 /* Opcode: FullKey P1 * *
3318 **
3319 ** Extract the complete key from the record that cursor P1 is currently
3320 ** pointing to and push the key onto the stack as a string.
3321 **
3322 ** Compare this opcode to Recno.  The Recno opcode extracts the first
3323 ** 4 bytes of the key and pushes those bytes onto the stack as an
3324 ** integer.  This instruction pushes the entire key as a string.
3325 **
3326 ** This opcode may not be used on a pseudo-table.
3327 */
3328 case OP_FullKey: {
3329   int i = pOp->p1;
3330   BtCursor *pCrsr;
3331   Cursor *pC;
3332 
3333   assert( p->apCsr[i]->keyAsData );
3334   assert( !p->apCsr[i]->pseudoTable );
3335   assert( i>=0 && i<p->nCursor );
3336   pTos++;
3337   if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3338     u64 amt;
3339     char *z;
3340 
3341     sqlite3VdbeCursorMoveto(pC);
3342     assert( pC->intKey==0 );
3343     sqlite3BtreeKeySize(pCrsr, &amt);
3344     if( amt<=0 ){
3345       rc = SQLITE_CORRUPT;
3346       goto abort_due_to_error;
3347     }
3348     if( amt>NBFS ){
3349       z = sqliteMallocRaw( amt );
3350       if( z==0 ) goto no_mem;
3351       pTos->flags = MEM_Blob | MEM_Dyn;
3352     }else{
3353       z = pTos->zShort;
3354       pTos->flags = MEM_Blob | MEM_Short;
3355     }
3356     sqlite3BtreeKey(pCrsr, 0, amt, z);
3357     pTos->z = z;
3358     pTos->n = amt;
3359   }
3360   break;
3361 }
3362 
3363 /* Opcode: NullRow P1 * *
3364 **
3365 ** Move the cursor P1 to a null row.  Any OP_Column operations
3366 ** that occur while the cursor is on the null row will always push
3367 ** a NULL onto the stack.
3368 */
3369 case OP_NullRow: {
3370   int i = pOp->p1;
3371   Cursor *pC;
3372 
3373   assert( i>=0 && i<p->nCursor );
3374   pC = p->apCsr[i];
3375   pC->nullRow = 1;
3376   pC->recnoIsValid = 0;
3377   break;
3378 }
3379 
3380 /* Opcode: Last P1 P2 *
3381 **
3382 ** The next use of the Recno or Column or Next instruction for P1
3383 ** will refer to the last entry in the database table or index.
3384 ** If the table or index is empty and P2>0, then jump immediately to P2.
3385 ** If P2 is 0 or if the table or index is not empty, fall through
3386 ** to the following instruction.
3387 */
3388 case OP_Last: {
3389   int i = pOp->p1;
3390   Cursor *pC;
3391   BtCursor *pCrsr;
3392 
3393   assert( i>=0 && i<p->nCursor );
3394   pC = p->apCsr[i];
3395   if( (pCrsr = pC->pCursor)!=0 ){
3396     int res;
3397     rc = sqlite3BtreeLast(pCrsr, &res);
3398     pC->nullRow = res;
3399     pC->deferredMoveto = 0;
3400     pC->cacheValid = 0;
3401     if( res && pOp->p2>0 ){
3402       pc = pOp->p2 - 1;
3403     }
3404   }else{
3405     pC->nullRow = 0;
3406   }
3407   break;
3408 }
3409 
3410 /* Opcode: Rewind P1 P2 *
3411 **
3412 ** The next use of the Recno or Column or Next instruction for P1
3413 ** will refer to the first entry in the database table or index.
3414 ** If the table or index is empty and P2>0, then jump immediately to P2.
3415 ** If P2 is 0 or if the table or index is not empty, fall through
3416 ** to the following instruction.
3417 */
3418 case OP_Rewind: {
3419   int i = pOp->p1;
3420   Cursor *pC;
3421   BtCursor *pCrsr;
3422   int res;
3423 
3424   assert( i>=0 && i<p->nCursor );
3425   pC = p->apCsr[i];
3426   if( (pCrsr = pC->pCursor)!=0 ){
3427     rc = sqlite3BtreeFirst(pCrsr, &res);
3428     pC->atFirst = res==0;
3429     pC->deferredMoveto = 0;
3430     pC->cacheValid = 0;
3431   }else{
3432     res = 1;
3433   }
3434   pC->nullRow = res;
3435   if( res && pOp->p2>0 ){
3436     pc = pOp->p2 - 1;
3437   }
3438   break;
3439 }
3440 
3441 /* Opcode: Next P1 P2 *
3442 **
3443 ** Advance cursor P1 so that it points to the next key/data pair in its
3444 ** table or index.  If there are no more key/value pairs then fall through
3445 ** to the following instruction.  But if the cursor advance was successful,
3446 ** jump immediately to P2.
3447 **
3448 ** See also: Prev
3449 */
3450 /* Opcode: Prev P1 P2 *
3451 **
3452 ** Back up cursor P1 so that it points to the previous key/data pair in its
3453 ** table or index.  If there is no previous key/value pairs then fall through
3454 ** to the following instruction.  But if the cursor backup was successful,
3455 ** jump immediately to P2.
3456 */
3457 case OP_Prev:
3458 case OP_Next: {
3459   Cursor *pC;
3460   BtCursor *pCrsr;
3461 
3462   CHECK_FOR_INTERRUPT;
3463   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3464   pC = p->apCsr[pOp->p1];
3465   if( (pCrsr = pC->pCursor)!=0 ){
3466     int res;
3467     if( pC->nullRow ){
3468       res = 1;
3469     }else{
3470       assert( pC->deferredMoveto==0 );
3471       rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
3472                                   sqlite3BtreePrevious(pCrsr, &res);
3473       pC->nullRow = res;
3474       pC->cacheValid = 0;
3475     }
3476     if( res==0 ){
3477       pc = pOp->p2 - 1;
3478       sqlite3_search_count++;
3479     }
3480   }else{
3481     pC->nullRow = 1;
3482   }
3483   pC->recnoIsValid = 0;
3484   break;
3485 }
3486 
3487 /* Opcode: IdxPut P1 P2 P3
3488 **
3489 ** The top of the stack holds a SQL index key made using the
3490 ** MakeIdxKey instruction.  This opcode writes that key into the
3491 ** index P1.  Data for the entry is nil.
3492 **
3493 ** If P2==1, then the key must be unique.  If the key is not unique,
3494 ** the program aborts with a SQLITE_CONSTRAINT error and the database
3495 ** is rolled back.  If P3 is not null, then it becomes part of the
3496 ** error message returned with the SQLITE_CONSTRAINT.
3497 */
3498 case OP_IdxPut: {
3499   int i = pOp->p1;
3500   Cursor *pC;
3501   BtCursor *pCrsr;
3502   assert( pTos>=p->aStack );
3503   assert( i>=0 && i<p->nCursor );
3504   assert( pTos->flags & MEM_Blob );
3505   if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3506     int nKey = pTos->n;
3507     const char *zKey = pTos->z;
3508     if( pOp->p2 ){
3509       int res;
3510       int len;
3511 
3512       /* 'len' is the length of the key minus the rowid at the end */
3513       len = nKey - sqlite3VdbeIdxRowidLen(nKey, zKey);
3514 
3515       rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res);
3516       if( rc!=SQLITE_OK ) goto abort_due_to_error;
3517       while( res!=0 && !sqlite3BtreeEof(pCrsr) ){
3518         int c;
3519         if( sqlite3VdbeIdxKeyCompare(pC, len, zKey, &c)==SQLITE_OK && c==0 ){
3520           rc = SQLITE_CONSTRAINT;
3521           if( pOp->p3 && pOp->p3[0] ){
3522             sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
3523           }
3524           goto abort_due_to_error;
3525         }
3526         if( res<0 ){
3527           sqlite3BtreeNext(pCrsr, &res);
3528           res = +1;
3529         }else{
3530           break;
3531         }
3532       }
3533     }
3534     assert( pC->intKey==0 );
3535     rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0);
3536     assert( pC->deferredMoveto==0 );
3537     pC->cacheValid = 0;
3538   }
3539   Release(pTos);
3540   pTos--;
3541   break;
3542 }
3543 
3544 /* Opcode: IdxDelete P1 * *
3545 **
3546 ** The top of the stack is an index key built using the MakeIdxKey opcode.
3547 ** This opcode removes that entry from the index.
3548 */
3549 case OP_IdxDelete: {
3550   int i = pOp->p1;
3551   Cursor *pC;
3552   BtCursor *pCrsr;
3553   assert( pTos>=p->aStack );
3554   assert( pTos->flags & MEM_Blob );
3555   assert( i>=0 && i<p->nCursor );
3556   if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3557     int rx, res;
3558     rx = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, &res);
3559     if( rx==SQLITE_OK && res==0 ){
3560       rc = sqlite3BtreeDelete(pCrsr);
3561     }
3562     assert( pC->deferredMoveto==0 );
3563     pC->cacheValid = 0;
3564   }
3565   Release(pTos);
3566   pTos--;
3567   break;
3568 }
3569 
3570 /* Opcode: IdxRecno P1 * *
3571 **
3572 ** Push onto the stack an integer which is the varint located at the
3573 ** end of the index key pointed to by cursor P1.  These integer should be
3574 ** the record number of the table entry to which this index entry points.
3575 **
3576 ** See also: Recno, MakeIdxKey.
3577 */
3578 case OP_IdxRecno: {
3579   int i = pOp->p1;
3580   BtCursor *pCrsr;
3581   Cursor *pC;
3582 
3583   assert( i>=0 && i<p->nCursor );
3584   pTos++;
3585   if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3586     i64 rowid;
3587 
3588     assert( pC->deferredMoveto==0 );
3589     assert( pC->intKey==0 );
3590     if( pC->nullRow ){
3591       pTos->flags = MEM_Null;
3592     }else{
3593       rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
3594       if( rc!=SQLITE_OK ){
3595         goto abort_due_to_error;
3596       }
3597       pTos->flags = MEM_Int;
3598       pTos->i = rowid;
3599     }
3600 
3601 #if 0
3602     /* Read the final 9 bytes of the key into buf[]. If the whole key is
3603     ** less than 9 bytes then just load the whole thing. Set len to the
3604     ** number of bytes read.
3605     */
3606     sqlite3BtreeKeySize(pCrsr, &sz);
3607     len = ((sz>10)?10:sz);
3608     rc = sqlite3BtreeKey(pCrsr, sz-len, len, buf);
3609     if( rc!=SQLITE_OK ){
3610       goto abort_due_to_error;
3611     }
3612 
3613     len--;
3614     if( buf[len]&0x80 ){
3615       /* If the last byte read has the 0x80 bit set, then the key does
3616       ** not end with a varint. Push a NULL onto the stack instead.
3617       */
3618       pTos->flags = MEM_Null;
3619     }else{
3620       /* Find the start of the varint by searching backwards for a 0x00
3621       ** byte. If one does not exists, then intepret the whole 9 bytes as a
3622       ** varint.
3623       */
3624       while( len && buf[len-1] ){
3625         len--;
3626       }
3627       sqlite3GetVarint32(&buf[len], &sz);
3628       pTos->flags = MEM_Int;
3629       pTos->i = sz;
3630     }
3631 #endif
3632   }else{
3633     pTos->flags = MEM_Null;
3634   }
3635   break;
3636 }
3637 
3638 /* Opcode: IdxGT P1 P2 *
3639 **
3640 ** The top of the stack is an index entry that omits the ROWID.  Compare
3641 ** the top of stack against the index that P1 is currently pointing to.
3642 ** Ignore the ROWID on the P1 index.
3643 **
3644 ** The top of the stack might have fewer columns that P1.
3645 **
3646 ** If the P1 index entry is greater than the top of the stack
3647 ** then jump to P2.  Otherwise fall through to the next instruction.
3648 ** In either case, the stack is popped once.
3649 */
3650 /* Opcode: IdxGE P1 P2 P3
3651 **
3652 ** The top of the stack is an index entry that omits the ROWID.  Compare
3653 ** the top of stack against the index that P1 is currently pointing to.
3654 ** Ignore the ROWID on the P1 index.
3655 **
3656 ** If the P1 index entry is greater than or equal to the top of the stack
3657 ** then jump to P2.  Otherwise fall through to the next instruction.
3658 ** In either case, the stack is popped once.
3659 **
3660 ** If P3 is the "+" string (or any other non-NULL string) then the
3661 ** index taken from the top of the stack is temporarily increased by
3662 ** an epsilon prior to the comparison.  This make the opcode work
3663 ** like IdxGT except that if the key from the stack is a prefix of
3664 ** the key in the cursor, the result is false whereas it would be
3665 ** true with IdxGT.
3666 */
3667 /* Opcode: IdxLT P1 P2 P3
3668 **
3669 ** The top of the stack is an index entry that omits the ROWID.  Compare
3670 ** the top of stack against the index that P1 is currently pointing to.
3671 ** Ignore the ROWID on the P1 index.
3672 **
3673 ** If the P1 index entry is less than  the top of the stack
3674 ** then jump to P2.  Otherwise fall through to the next instruction.
3675 ** In either case, the stack is popped once.
3676 **
3677 ** If P3 is the "+" string (or any other non-NULL string) then the
3678 ** index taken from the top of the stack is temporarily increased by
3679 ** an epsilon prior to the comparison.  This makes the opcode work
3680 ** like IdxLE.
3681 */
3682 case OP_IdxLT:
3683 case OP_IdxGT:
3684 case OP_IdxGE: {
3685   int i= pOp->p1;
3686   BtCursor *pCrsr;
3687   Cursor *pC;
3688 
3689   assert( i>=0 && i<p->nCursor );
3690   assert( pTos>=p->aStack );
3691   if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3692     int res, rc;
3693 
3694     assert( pTos->flags & MEM_Blob );  /* Created using OP_Make*Key */
3695     Stringify(pTos, db->enc);
3696     assert( pC->deferredMoveto==0 );
3697     *pC->pIncrKey = pOp->p3!=0;
3698     assert( pOp->p3==0 || pOp->opcode!=OP_IdxGT );
3699     rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, pTos->z, &res);
3700     *pC->pIncrKey = 0;
3701     if( rc!=SQLITE_OK ){
3702       break;
3703     }
3704     if( pOp->opcode==OP_IdxLT ){
3705       res = -res;
3706     }else if( pOp->opcode==OP_IdxGE ){
3707       res++;
3708     }
3709     if( res>0 ){
3710       pc = pOp->p2 - 1 ;
3711     }
3712   }
3713   Release(pTos);
3714   pTos--;
3715   break;
3716 }
3717 
3718 /* Opcode: IdxIsNull P1 P2 *
3719 **
3720 ** The top of the stack contains an index entry such as might be generated
3721 ** by the MakeIdxKey opcode.  This routine looks at the first P1 fields of
3722 ** that key.  If any of the first P1 fields are NULL, then a jump is made
3723 ** to address P2.  Otherwise we fall straight through.
3724 **
3725 ** The index entry is always popped from the stack.
3726 */
3727 case OP_IdxIsNull: {
3728   int i = pOp->p1;
3729   int k, n;
3730   const char *z;
3731   u32 serial_type;
3732 
3733   assert( pTos>=p->aStack );
3734   assert( pTos->flags & MEM_Blob );
3735   z = pTos->z;
3736   n = pTos->n;
3737   k = sqlite3GetVarint32(z, &serial_type);
3738   for(; k<n && i>0; i--){
3739     k += sqlite3GetVarint32(&z[k], &serial_type);
3740     if( serial_type==0 ){   /* Serial type 0 is a NULL */
3741       pc = pOp->p2-1;
3742       break;
3743     }
3744   }
3745   Release(pTos);
3746   pTos--;
3747   break;
3748 }
3749 
3750 /* Opcode: Destroy P1 P2 *
3751 **
3752 ** Delete an entire database table or index whose root page in the database
3753 ** file is given by P1.
3754 **
3755 ** The table being destroyed is in the main database file if P2==0.  If
3756 ** P2==1 then the table to be clear is in the auxiliary database file
3757 ** that is used to store tables create using CREATE TEMPORARY TABLE.
3758 **
3759 ** See also: Clear
3760 */
3761 case OP_Destroy: {
3762   rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1);
3763   break;
3764 }
3765 
3766 /* Opcode: Clear P1 P2 *
3767 **
3768 ** Delete all contents of the database table or index whose root page
3769 ** in the database file is given by P1.  But, unlike Destroy, do not
3770 ** remove the table or index from the database file.
3771 **
3772 ** The table being clear is in the main database file if P2==0.  If
3773 ** P2==1 then the table to be clear is in the auxiliary database file
3774 ** that is used to store tables create using CREATE TEMPORARY TABLE.
3775 **
3776 ** See also: Destroy
3777 */
3778 case OP_Clear: {
3779   rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
3780   break;
3781 }
3782 
3783 /* Opcode: CreateTable * P2 P3
3784 **
3785 ** Allocate a new table in the main database file if P2==0 or in the
3786 ** auxiliary database file if P2==1.  Push the page number
3787 ** for the root page of the new table onto the stack.
3788 **
3789 ** The root page number is also written to a memory location that P3
3790 ** points to.  This is the mechanism is used to write the root page
3791 ** number into the parser's internal data structures that describe the
3792 ** new table.
3793 **
3794 ** The difference between a table and an index is this:  A table must
3795 ** have a 4-byte integer key and can have arbitrary data.  An index
3796 ** has an arbitrary key but no data.
3797 **
3798 ** See also: CreateIndex
3799 */
3800 /* Opcode: CreateIndex * P2 P3
3801 **
3802 ** Allocate a new index in the main database file if P2==0 or in the
3803 ** auxiliary database file if P2==1.  Push the page number of the
3804 ** root page of the new index onto the stack.
3805 **
3806 ** See documentation on OP_CreateTable for additional information.
3807 */
3808 case OP_CreateIndex:
3809 case OP_CreateTable: {
3810   int pgno;
3811   int flags;
3812   assert( pOp->p3!=0 && pOp->p3type==P3_POINTER );
3813   assert( pOp->p2>=0 && pOp->p2<db->nDb );
3814   assert( db->aDb[pOp->p2].pBt!=0 );
3815   if( pOp->opcode==OP_CreateTable ){
3816     /* flags = BTREE_INTKEY; */
3817     flags = BTREE_LEAFDATA|BTREE_INTKEY;
3818   }else{
3819     flags = BTREE_ZERODATA;
3820   }
3821   rc = sqlite3BtreeCreateTable(db->aDb[pOp->p2].pBt, &pgno, flags);
3822   pTos++;
3823   if( rc==SQLITE_OK ){
3824     pTos->i = pgno;
3825     pTos->flags = MEM_Int;
3826     *(u32*)pOp->p3 = pgno;
3827     pOp->p3 = 0;
3828   }else{
3829     pTos->flags = MEM_Null;
3830   }
3831   break;
3832 }
3833 
3834 /* Opcode: IntegrityCk * P2 *
3835 **
3836 ** Do an analysis of the currently open database.  Push onto the
3837 ** stack the text of an error message describing any problems.
3838 ** If there are no errors, push a "ok" onto the stack.
3839 **
3840 ** The root page numbers of all tables in the database are integer
3841 ** values on the stack.  This opcode pulls as many integers as it
3842 ** can off of the stack and uses those numbers as the root pages.
3843 **
3844 ** If P2 is not zero, the check is done on the auxiliary database
3845 ** file, not the main database file.
3846 **
3847 ** This opcode is used for testing purposes only.
3848 */
3849 case OP_IntegrityCk: {
3850   int nRoot;
3851   int *aRoot;
3852   int j;
3853   char *z;
3854 
3855   for(nRoot=0; &pTos[-nRoot]>=p->aStack; nRoot++){
3856     if( (pTos[-nRoot].flags & MEM_Int)==0 ) break;
3857   }
3858   assert( nRoot>0 );
3859   aRoot = sqliteMallocRaw( sizeof(int*)*(nRoot+1) );
3860   if( aRoot==0 ) goto no_mem;
3861   for(j=0; j<nRoot; j++){
3862     Mem *pMem = &pTos[-j];
3863     aRoot[j] = pMem->i;
3864   }
3865   aRoot[j] = 0;
3866   popStack(&pTos, nRoot);
3867   pTos++;
3868   z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot);
3869   if( z==0 || z[0]==0 ){
3870     if( z ) sqliteFree(z);
3871     pTos->z = "ok";
3872     pTos->n = 2;
3873     pTos->flags = MEM_Str | MEM_Static | MEM_Term;
3874   }else{
3875     pTos->z = z;
3876     pTos->n = strlen(z);
3877     pTos->flags = MEM_Str | MEM_Dyn | MEM_Term;
3878   }
3879   pTos->enc = TEXT_Utf8;
3880   sqlite3VdbeChangeEncoding(pTos, db->enc);
3881   sqliteFree(aRoot);
3882   break;
3883 }
3884 
3885 /* Opcode: ListWrite * * *
3886 **
3887 ** Write the integer on the top of the stack
3888 ** into the temporary storage list.
3889 */
3890 case OP_ListWrite: {
3891   Keylist *pKeylist;
3892   assert( pTos>=p->aStack );
3893   pKeylist = p->pList;
3894   if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){
3895     pKeylist = sqliteMallocRaw( sizeof(Keylist)+999*sizeof(pKeylist->aKey[0]) );
3896     if( pKeylist==0 ) goto no_mem;
3897     pKeylist->nKey = 1000;
3898     pKeylist->nRead = 0;
3899     pKeylist->nUsed = 0;
3900     pKeylist->pNext = p->pList;
3901     p->pList = pKeylist;
3902   }
3903   Integerify(pTos, db->enc);
3904   pKeylist->aKey[pKeylist->nUsed++] = pTos->i;
3905   Release(pTos);
3906   pTos--;
3907   break;
3908 }
3909 
3910 /* Opcode: ListRewind * * *
3911 **
3912 ** Rewind the temporary buffer back to the beginning.
3913 */
3914 case OP_ListRewind: {
3915   /* What this opcode codes, really, is reverse the order of the
3916   ** linked list of Keylist structures so that they are read out
3917   ** in the same order that they were read in. */
3918   Keylist *pRev, *pTop;
3919   pRev = 0;
3920   while( p->pList ){
3921     pTop = p->pList;
3922     p->pList = pTop->pNext;
3923     pTop->pNext = pRev;
3924     pRev = pTop;
3925   }
3926   p->pList = pRev;
3927   break;
3928 }
3929 
3930 /* Opcode: ListRead * P2 *
3931 **
3932 ** Attempt to read an integer from the temporary storage buffer
3933 ** and push it onto the stack.  If the storage buffer is empty,
3934 ** push nothing but instead jump to P2.
3935 */
3936 case OP_ListRead: {
3937   Keylist *pKeylist;
3938   CHECK_FOR_INTERRUPT;
3939   pKeylist = p->pList;
3940   if( pKeylist!=0 ){
3941     assert( pKeylist->nRead>=0 );
3942     assert( pKeylist->nRead<pKeylist->nUsed );
3943     assert( pKeylist->nRead<pKeylist->nKey );
3944     pTos++;
3945     pTos->i = pKeylist->aKey[pKeylist->nRead++];
3946     pTos->flags = MEM_Int;
3947     if( pKeylist->nRead>=pKeylist->nUsed ){
3948       p->pList = pKeylist->pNext;
3949       sqliteFree(pKeylist);
3950     }
3951   }else{
3952     pc = pOp->p2 - 1;
3953   }
3954   break;
3955 }
3956 
3957 /* Opcode: ListReset * * *
3958 **
3959 ** Reset the temporary storage buffer so that it holds nothing.
3960 */
3961 case OP_ListReset: {
3962   if( p->pList ){
3963     sqlite3VdbeKeylistFree(p->pList);
3964     p->pList = 0;
3965   }
3966   break;
3967 }
3968 
3969 /* Opcode: ListPush * * *
3970 **
3971 ** Save the current Vdbe list such that it can be restored by a ListPop
3972 ** opcode. The list is empty after this is executed.
3973 */
3974 case OP_ListPush: {
3975   p->keylistStackDepth++;
3976   assert(p->keylistStackDepth > 0);
3977   p->keylistStack = sqliteRealloc(p->keylistStack,
3978           sizeof(Keylist *) * p->keylistStackDepth);
3979   if( p->keylistStack==0 ) goto no_mem;
3980   p->keylistStack[p->keylistStackDepth - 1] = p->pList;
3981   p->pList = 0;
3982   break;
3983 }
3984 
3985 /* Opcode: ListPop * * *
3986 **
3987 ** Restore the Vdbe list to the state it was in when ListPush was last
3988 ** executed.
3989 */
3990 case OP_ListPop: {
3991   assert(p->keylistStackDepth > 0);
3992   p->keylistStackDepth--;
3993   sqlite3VdbeKeylistFree(p->pList);
3994   p->pList = p->keylistStack[p->keylistStackDepth];
3995   p->keylistStack[p->keylistStackDepth] = 0;
3996   if( p->keylistStackDepth == 0 ){
3997     sqliteFree(p->keylistStack);
3998     p->keylistStack = 0;
3999   }
4000   break;
4001 }
4002 
4003 /* Opcode: ContextPush * * *
4004 **
4005 ** Save the current Vdbe context such that it can be restored by a ContextPop
4006 ** opcode. The context stores the last insert row id, the last statement change
4007 ** count, and the current statement change count.
4008 */
4009 case OP_ContextPush: {
4010   p->contextStackDepth++;
4011   assert(p->contextStackDepth > 0);
4012   p->contextStack = sqliteRealloc(p->contextStack,
4013           sizeof(Context) * p->contextStackDepth);
4014   if( p->contextStack==0 ) goto no_mem;
4015   p->contextStack[p->contextStackDepth - 1].lastRowid = p->db->lastRowid;
4016   p->contextStack[p->contextStackDepth - 1].lsChange = p->db->lsChange;
4017   p->contextStack[p->contextStackDepth - 1].csChange = p->db->csChange;
4018   break;
4019 }
4020 
4021 /* Opcode: ContextPop * * *
4022 **
4023 ** Restore the Vdbe context to the state it was in when contextPush was last
4024 ** executed. The context stores the last insert row id, the last statement
4025 ** change count, and the current statement change count.
4026 */
4027 case OP_ContextPop: {
4028   assert(p->contextStackDepth > 0);
4029   p->contextStackDepth--;
4030   p->db->lastRowid = p->contextStack[p->contextStackDepth].lastRowid;
4031   p->db->lsChange = p->contextStack[p->contextStackDepth].lsChange;
4032   p->db->csChange = p->contextStack[p->contextStackDepth].csChange;
4033   if( p->contextStackDepth == 0 ){
4034     sqliteFree(p->contextStack);
4035     p->contextStack = 0;
4036   }
4037   break;
4038 }
4039 
4040 /* Opcode: SortPut * * *
4041 **
4042 ** The TOS is the key and the NOS is the data.  Pop both from the stack
4043 ** and put them on the sorter.  The key and data should have been
4044 ** made using SortMakeKey and SortMakeRec, respectively.
4045 */
4046 case OP_SortPut: {
4047   Mem *pNos = &pTos[-1];
4048   Sorter *pSorter;
4049   assert( pNos>=p->aStack );
4050   Stringify(pNos, db->enc);
4051   if( Dynamicify(pTos, db->enc) || Dynamicify(pNos, db->enc) ) goto no_mem;
4052   pSorter = sqliteMallocRaw( sizeof(Sorter) );
4053   if( pSorter==0 ) goto no_mem;
4054   pSorter->pNext = p->pSort;
4055   p->pSort = pSorter;
4056   assert( pTos->flags & MEM_Dyn );
4057   pSorter->nKey = pTos->n;
4058   pSorter->zKey = pTos->z;
4059   assert( pNos->flags & MEM_Dyn );
4060   pSorter->nData = pNos->n;
4061   pSorter->pData = pNos->z;
4062   pTos -= 2;
4063   break;
4064 }
4065 
4066 /* Opcode: Sort * * P3
4067 **
4068 ** Sort all elements on the sorter.  The algorithm is a
4069 ** mergesort.  The P3 argument is a pointer to a KeyInfo structure
4070 ** that describes the keys to be sorted.
4071 */
4072 case OP_Sort: {
4073   int i;
4074   KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
4075   Sorter *pElem;
4076   Sorter *apSorter[NSORT];
4077   pKeyInfo->enc = p->db->enc;
4078   for(i=0; i<NSORT; i++){
4079     apSorter[i] = 0;
4080   }
4081   while( p->pSort ){
4082     pElem = p->pSort;
4083     p->pSort = pElem->pNext;
4084     pElem->pNext = 0;
4085     for(i=0; i<NSORT-1; i++){
4086     if( apSorter[i]==0 ){
4087         apSorter[i] = pElem;
4088         break;
4089       }else{
4090         pElem = Merge(apSorter[i], pElem, pKeyInfo);
4091         apSorter[i] = 0;
4092       }
4093     }
4094     if( i>=NSORT-1 ){
4095       apSorter[NSORT-1] = Merge(apSorter[NSORT-1],pElem, pKeyInfo);
4096     }
4097   }
4098   pElem = 0;
4099   for(i=0; i<NSORT; i++){
4100     pElem = Merge(apSorter[i], pElem, pKeyInfo);
4101   }
4102   p->pSort = pElem;
4103   break;
4104 }
4105 
4106 /* Opcode: SortNext * P2 *
4107 **
4108 ** Push the data for the topmost element in the sorter onto the
4109 ** stack, then remove the element from the sorter.  If the sorter
4110 ** is empty, push nothing on the stack and instead jump immediately
4111 ** to instruction P2.
4112 */
4113 case OP_SortNext: {
4114   Sorter *pSorter = p->pSort;
4115   CHECK_FOR_INTERRUPT;
4116   if( pSorter!=0 ){
4117     p->pSort = pSorter->pNext;
4118     pTos++;
4119     pTos->z = pSorter->pData;
4120     pTos->n = pSorter->nData;
4121     pTos->flags = MEM_Blob|MEM_Dyn|MEM_Term;
4122     pTos->enc = 0;
4123     sqliteFree(pSorter->zKey);
4124     sqliteFree(pSorter);
4125   }else{
4126     pc = pOp->p2 - 1;
4127   }
4128   break;
4129 }
4130 
4131 /* Opcode: SortReset * * *
4132 **
4133 ** Remove any elements that remain on the sorter.
4134 */
4135 case OP_SortReset: {
4136   sqlite3VdbeSorterReset(p);
4137   break;
4138 }
4139 
4140 /* Opcode: MemStore P1 P2 *
4141 **
4142 ** Write the top of the stack into memory location P1.
4143 ** P1 should be a small integer since space is allocated
4144 ** for all memory locations between 0 and P1 inclusive.
4145 **
4146 ** After the data is stored in the memory location, the
4147 ** stack is popped once if P2 is 1.  If P2 is zero, then
4148 ** the original data remains on the stack.
4149 */
4150 case OP_MemStore: {
4151   int i = pOp->p1;
4152   Mem *pMem;
4153   assert( pTos>=p->aStack );
4154   if( i>=p->nMem ){
4155     int nOld = p->nMem;
4156     Mem *aMem;
4157     p->nMem = i + 5;
4158     aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0]));
4159     if( aMem==0 ) goto no_mem;
4160     if( aMem!=p->aMem ){
4161       int j;
4162       for(j=0; j<nOld; j++){
4163         if( aMem[j].flags & MEM_Short ){
4164           aMem[j].z = aMem[j].zShort;
4165         }
4166       }
4167     }
4168     p->aMem = aMem;
4169     if( nOld<p->nMem ){
4170       memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld));
4171     }
4172   }
4173   Deephemeralize(pTos);
4174   pMem = &p->aMem[i];
4175   Release(pMem);
4176   *pMem = *pTos;
4177   if( pMem->flags & MEM_Dyn ){
4178     if( pOp->p2 ){
4179       pTos->flags = MEM_Null;
4180     }else{
4181       pMem->z = sqliteMallocRaw( pMem->n+2 );
4182       if( pMem->z==0 ) goto no_mem;
4183       memcpy(pMem->z, pTos->z, pMem->n);
4184       memcpy(&pMem->z[pMem->n], "\000", 2);
4185       pMem->flags |= MEM_Term;
4186     }
4187   }else if( pMem->flags & MEM_Short ){
4188     pMem->z = pMem->zShort;
4189   }
4190   if( pOp->p2 ){
4191     Release(pTos);
4192     pTos--;
4193   }
4194   break;
4195 }
4196 
4197 /* Opcode: MemLoad P1 * *
4198 **
4199 ** Push a copy of the value in memory location P1 onto the stack.
4200 **
4201 ** If the value is a string, then the value pushed is a pointer to
4202 ** the string that is stored in the memory location.  If the memory
4203 ** location is subsequently changed (using OP_MemStore) then the
4204 ** value pushed onto the stack will change too.
4205 */
4206 case OP_MemLoad: {
4207   int i = pOp->p1;
4208   assert( i>=0 && i<p->nMem );
4209   pTos++;
4210   memcpy(pTos, &p->aMem[i], sizeof(pTos[0])-NBFS);;
4211   if( pTos->flags & (MEM_Str|MEM_Blob) ){
4212     pTos->flags |= MEM_Ephem;
4213     pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
4214   }
4215   break;
4216 }
4217 
4218 /* Opcode: MemIncr P1 P2 *
4219 **
4220 ** Increment the integer valued memory cell P1 by 1.  If P2 is not zero
4221 ** and the result after the increment is greater than zero, then jump
4222 ** to P2.
4223 **
4224 ** This instruction throws an error if the memory cell is not initially
4225 ** an integer.
4226 */
4227 case OP_MemIncr: {
4228   int i = pOp->p1;
4229   Mem *pMem;
4230   assert( i>=0 && i<p->nMem );
4231   pMem = &p->aMem[i];
4232   assert( pMem->flags==MEM_Int );
4233   pMem->i++;
4234   if( pOp->p2>0 && pMem->i>0 ){
4235      pc = pOp->p2 - 1;
4236   }
4237   break;
4238 }
4239 
4240 /* Opcode: AggReset * P2 *
4241 **
4242 ** Reset the aggregator so that it no longer contains any data.
4243 ** Future aggregator elements will contain P2 values each.
4244 */
4245 case OP_AggReset: {
4246   sqlite3VdbeAggReset(&p->agg);
4247   p->agg.nMem = pOp->p2;
4248   p->agg.apFunc = sqliteMalloc( p->agg.nMem*sizeof(p->agg.apFunc[0]) );
4249   if( p->agg.apFunc==0 ) goto no_mem;
4250   break;
4251 }
4252 
4253 /* Opcode: AggInit * P2 P3
4254 **
4255 ** Initialize the function parameters for an aggregate function.
4256 ** The aggregate will operate out of aggregate column P2.
4257 ** P3 is a pointer to the FuncDef structure for the function.
4258 */
4259 case OP_AggInit: {
4260   int i = pOp->p2;
4261   assert( i>=0 && i<p->agg.nMem );
4262   p->agg.apFunc[i] = (FuncDef*)pOp->p3;
4263   break;
4264 }
4265 
4266 /* Opcode: AggFunc * P2 P3
4267 **
4268 ** Execute the step function for an aggregate.  The
4269 ** function has P2 arguments.  P3 is a pointer to the FuncDef
4270 ** structure that specifies the function.
4271 **
4272 ** The top of the stack must be an integer which is the index of
4273 ** the aggregate column that corresponds to this aggregate function.
4274 ** Ideally, this index would be another parameter, but there are
4275 ** no free parameters left.  The integer is popped from the stack.
4276 */
4277 case OP_AggFunc: {
4278   int n = pOp->p2;
4279   int i;
4280   Mem *pMem, *pRec;
4281   sqlite3_context ctx;
4282   sqlite3_value **apVal;
4283 
4284   assert( n>=0 );
4285   assert( pTos->flags==MEM_Int );
4286   pRec = &pTos[-n];
4287   assert( pRec>=p->aStack );
4288 
4289   apVal = p->apArg;
4290   assert( apVal || n==0 );
4291 
4292   for(i=0; i<n; i++, pRec++){
4293     apVal[i] = pRec;
4294     storeTypeInfo(pRec, db->enc);
4295   }
4296   i = pTos->i;
4297   assert( i>=0 && i<p->agg.nMem );
4298   ctx.pFunc = (FuncDef*)pOp->p3;
4299   pMem = &p->agg.pCurrent->aMem[i];
4300   ctx.s.z = pMem->zShort;  /* Space used for small aggregate contexts */
4301   ctx.pAgg = pMem->z;
4302   ctx.cnt = ++pMem->i;
4303   ctx.isError = 0;
4304   ctx.isStep = 1;
4305   (ctx.pFunc->xStep)(&ctx, n, apVal);
4306   pMem->z = ctx.pAgg;
4307   pMem->flags = MEM_AggCtx;
4308   popStack(&pTos, n+1);
4309   if( ctx.isError ){
4310     rc = SQLITE_ERROR;
4311   }
4312   break;
4313 }
4314 
4315 /* Opcode: AggFocus * P2 *
4316 **
4317 ** Pop the top of the stack and use that as an aggregator key.  If
4318 ** an aggregator with that same key already exists, then make the
4319 ** aggregator the current aggregator and jump to P2.  If no aggregator
4320 ** with the given key exists, create one and make it current but
4321 ** do not jump.
4322 **
4323 ** The order of aggregator opcodes is important.  The order is:
4324 ** AggReset AggFocus AggNext.  In other words, you must execute
4325 ** AggReset first, then zero or more AggFocus operations, then
4326 ** zero or more AggNext operations.  You must not execute an AggFocus
4327 ** in between an AggNext and an AggReset.
4328 */
4329 case OP_AggFocus: {
4330   AggElem *pElem;
4331   char *zKey;
4332   int nKey;
4333 
4334   assert( pTos>=p->aStack );
4335   Stringify(pTos, db->enc);
4336   zKey = pTos->z;
4337   nKey = pTos->n;
4338   pElem = sqlite3HashFind(&p->agg.hash, zKey, nKey);
4339   if( pElem ){
4340     p->agg.pCurrent = pElem;
4341     pc = pOp->p2 - 1;
4342   }else{
4343     AggInsert(&p->agg, zKey, nKey);
4344     if( sqlite3_malloc_failed ) goto no_mem;
4345   }
4346   Release(pTos);
4347   pTos--;
4348   break;
4349 }
4350 
4351 /* Opcode: AggSet * P2 *
4352 **
4353 ** Move the top of the stack into the P2-th field of the current
4354 ** aggregate.  String values are duplicated into new memory.
4355 */
4356 case OP_AggSet: {
4357   AggElem *pFocus = AggInFocus(p->agg);
4358   Mem *pMem;
4359   int i = pOp->p2;
4360   assert( pTos>=p->aStack );
4361   if( pFocus==0 ) goto no_mem;
4362   assert( i>=0 && i<p->agg.nMem );
4363   Deephemeralize(pTos);
4364   pMem = &pFocus->aMem[i];
4365   Release(pMem);
4366   *pMem = *pTos;
4367   if( pMem->flags & MEM_Dyn ){
4368     pTos->flags = MEM_Null;
4369   }else if( pMem->flags & MEM_Short ){
4370     pMem->z = pMem->zShort;
4371   }
4372   pTos--;
4373   break;
4374 }
4375 
4376 /* Opcode: AggGet * P2 *
4377 **
4378 ** Push a new entry onto the stack which is a copy of the P2-th field
4379 ** of the current aggregate.  Strings are not duplicated so
4380 ** string values will be ephemeral.
4381 */
4382 case OP_AggGet: {
4383   AggElem *pFocus = AggInFocus(p->agg);
4384   Mem *pMem;
4385   int i = pOp->p2;
4386   if( pFocus==0 ) goto no_mem;
4387   assert( i>=0 && i<p->agg.nMem );
4388   pTos++;
4389   pMem = &pFocus->aMem[i];
4390   *pTos = *pMem;
4391   if( pTos->flags & (MEM_Str|MEM_Blob) ){
4392     pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
4393     pTos->flags |= MEM_Ephem;
4394   }
4395   if( pTos->flags&MEM_Str ){
4396     sqlite3VdbeChangeEncoding(pTos, db->enc);
4397   }
4398   break;
4399 }
4400 
4401 /* Opcode: AggNext * P2 *
4402 **
4403 ** Make the next aggregate value the current aggregate.  The prior
4404 ** aggregate is deleted.  If all aggregate values have been consumed,
4405 ** jump to P2.
4406 **
4407 ** The order of aggregator opcodes is important.  The order is:
4408 ** AggReset AggFocus AggNext.  In other words, you must execute
4409 ** AggReset first, then zero or more AggFocus operations, then
4410 ** zero or more AggNext operations.  You must not execute an AggFocus
4411 ** in between an AggNext and an AggReset.
4412 */
4413 case OP_AggNext: {
4414   CHECK_FOR_INTERRUPT;
4415   if( p->agg.pSearch==0 ){
4416     p->agg.pSearch = sqliteHashFirst(&p->agg.hash);
4417   }else{
4418     p->agg.pSearch = sqliteHashNext(p->agg.pSearch);
4419   }
4420   if( p->agg.pSearch==0 ){
4421     pc = pOp->p2 - 1;
4422   } else {
4423     int i;
4424     sqlite3_context ctx;
4425     Mem *aMem;
4426     p->agg.pCurrent = sqliteHashData(p->agg.pSearch);
4427     aMem = p->agg.pCurrent->aMem;
4428     for(i=0; i<p->agg.nMem; i++){
4429       int freeCtx;
4430       if( p->agg.apFunc[i]==0 ) continue;
4431       if( p->agg.apFunc[i]->xFinalize==0 ) continue;
4432       ctx.s.flags = MEM_Null;
4433       ctx.s.z = aMem[i].zShort;
4434       ctx.pAgg = (void*)aMem[i].z;
4435       freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort;
4436       ctx.cnt = aMem[i].i;
4437       ctx.isStep = 0;
4438       ctx.pFunc = p->agg.apFunc[i];
4439       (*p->agg.apFunc[i]->xFinalize)(&ctx);
4440       if( freeCtx ){
4441         sqliteFree( aMem[i].z );
4442       }
4443       aMem[i] = ctx.s;
4444       if( aMem[i].flags & MEM_Short ){
4445         aMem[i].z = aMem[i].zShort;
4446       }
4447     }
4448   }
4449   break;
4450 }
4451 
4452 /* Opcode: Vacuum * * *
4453 **
4454 ** Vacuum the entire database.  This opcode will cause other virtual
4455 ** machines to be created and run.  It may not be called from within
4456 ** a transaction.
4457 */
4458 case OP_Vacuum: {
4459   if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4460   rc = sqlite3RunVacuum(&p->zErrMsg, db);
4461   if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4462   break;
4463 }
4464 
4465 /* An other opcode is illegal...
4466 */
4467 default: {
4468   sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",pOp->opcode);
4469   sqlite3SetString(&p->zErrMsg, "unknown opcode ", zBuf, (char*)0);
4470   rc = SQLITE_INTERNAL;
4471   break;
4472 }
4473 
4474 /*****************************************************************************
4475 ** The cases of the switch statement above this line should all be indented
4476 ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
4477 ** readability.  From this point on down, the normal indentation rules are
4478 ** restored.
4479 *****************************************************************************/
4480     }
4481 
4482 #ifdef VDBE_PROFILE
4483     {
4484       long long elapse = hwtime() - start;
4485       pOp->cycles += elapse;
4486       pOp->cnt++;
4487 #if 0
4488         fprintf(stdout, "%10lld ", elapse);
4489         sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
4490 #endif
4491     }
4492 #endif
4493 
4494     /* The following code adds nothing to the actual functionality
4495     ** of the program.  It is only here for testing and debugging.
4496     ** On the other hand, it does burn CPU cycles every time through
4497     ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
4498     */
4499 #ifndef NDEBUG
4500     /* Sanity checking on the top element of the stack */
4501     if( pTos>=p->aStack ){
4502       sqlite3VdbeMemSanity(pTos, db->enc);
4503     }
4504     if( pc<-1 || pc>=p->nOp ){
4505       sqlite3SetString(&p->zErrMsg, "jump destination out of range", (char*)0);
4506       rc = SQLITE_INTERNAL;
4507     }
4508     if( p->trace && pTos>=p->aStack ){
4509       int i;
4510       fprintf(p->trace, "Stack:");
4511       for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
4512         if( pTos[i].flags & MEM_Null ){
4513           fprintf(p->trace, " NULL");
4514         }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
4515           fprintf(p->trace, " si:%lld", pTos[i].i);
4516         }else if( pTos[i].flags & MEM_Int ){
4517           fprintf(p->trace, " i:%lld", pTos[i].i);
4518         }else if( pTos[i].flags & MEM_Real ){
4519           fprintf(p->trace, " r:%g", pTos[i].r);
4520         }else{
4521           char zBuf[100];
4522           prettyPrintMem(&pTos[i], zBuf, 100);
4523           fprintf(p->trace, " ");
4524           fprintf(p->trace, zBuf);
4525         }
4526       }
4527       if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
4528       fprintf(p->trace,"\n");
4529     }
4530 #endif
4531   }  /* The end of the for(;;) loop the loops through opcodes */
4532 
4533   /* If we reach this point, it means that execution is finished.
4534   */
4535 vdbe_halt:
4536   if( rc ){
4537     p->rc = rc;
4538     rc = SQLITE_ERROR;
4539   }else{
4540     rc = SQLITE_DONE;
4541   }
4542   p->magic = VDBE_MAGIC_HALT;
4543   p->pTos = pTos;
4544   return rc;
4545 
4546   /* Jump to here if a malloc() fails.  It's hard to get a malloc()
4547   ** to fail on a modern VM computer, so this code is untested.
4548   */
4549 no_mem:
4550   sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
4551   rc = SQLITE_NOMEM;
4552   goto vdbe_halt;
4553 
4554   /* Jump to here for an SQLITE_MISUSE error.
4555   */
4556 abort_due_to_misuse:
4557   rc = SQLITE_MISUSE;
4558   /* Fall thru into abort_due_to_error */
4559 
4560   /* Jump to here for any other kind of fatal error.  The "rc" variable
4561   ** should hold the error number.
4562   */
4563 abort_due_to_error:
4564   if( p->zErrMsg==0 ){
4565     if( sqlite3_malloc_failed ) rc = SQLITE_NOMEM;
4566     sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
4567   }
4568   goto vdbe_halt;
4569 
4570   /* Jump to here if the sqlite3_interrupt() API sets the interrupt
4571   ** flag.
4572   */
4573 abort_due_to_interrupt:
4574   assert( db->flags & SQLITE_Interrupt );
4575   db->flags &= ~SQLITE_Interrupt;
4576   if( db->magic!=SQLITE_MAGIC_BUSY ){
4577     rc = SQLITE_MISUSE;
4578   }else{
4579     rc = SQLITE_INTERRUPT;
4580   }
4581   sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
4582   goto vdbe_halt;
4583 }
4584