Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(476)

Side by Side Diff: third_party/sqlite/amalgamation/sqlite3.04.c

Issue 2755803002: NCI: trybot test for sqlite 3.17 import. (Closed)
Patch Set: also clang on Linux i386 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /************** Begin file vdbe.c ********************************************/
2 /*
3 ** 2001 September 15
4 **
5 ** The author disclaims copyright to this source code. In place of
6 ** a legal notice, here is a blessing:
7 **
8 ** May you do good and not evil.
9 ** May you find forgiveness for yourself and forgive others.
10 ** May you share freely, never taking more than you give.
11 **
12 *************************************************************************
13 ** The code in this file implements the function that runs the
14 ** bytecode of a prepared statement.
15 **
16 ** Various scripts scan this source file in order to generate HTML
17 ** documentation, headers files, or other derived files. The formatting
18 ** of the code in this file is, therefore, important. See other comments
19 ** in this file for details. If in doubt, do not deviate from existing
20 ** commenting and indentation practices when changing or adding code.
21 */
22 /* #include "sqliteInt.h" */
23 /* #include "vdbeInt.h" */
24
25 /*
26 ** Invoke this macro on memory cells just prior to changing the
27 ** value of the cell. This macro verifies that shallow copies are
28 ** not misused. A shallow copy of a string or blob just copies a
29 ** pointer to the string or blob, not the content. If the original
30 ** is changed while the copy is still in use, the string or blob might
31 ** be changed out from under the copy. This macro verifies that nothing
32 ** like that ever happens.
33 */
34 #ifdef SQLITE_DEBUG
35 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
36 #else
37 # define memAboutToChange(P,M)
38 #endif
39
40 /*
41 ** The following global variable is incremented every time a cursor
42 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
43 ** procedures use this information to make sure that indices are
44 ** working correctly. This variable has no function other than to
45 ** help verify the correct operation of the library.
46 */
47 #ifdef SQLITE_TEST
48 SQLITE_API int sqlite3_search_count = 0;
49 #endif
50
51 /*
52 ** When this global variable is positive, it gets decremented once before
53 ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
54 ** field of the sqlite3 structure is set in order to simulate an interrupt.
55 **
56 ** This facility is used for testing purposes only. It does not function
57 ** in an ordinary build.
58 */
59 #ifdef SQLITE_TEST
60 SQLITE_API int sqlite3_interrupt_count = 0;
61 #endif
62
63 /*
64 ** The next global variable is incremented each type the OP_Sort opcode
65 ** is executed. The test procedures use this information to make sure that
66 ** sorting is occurring or not occurring at appropriate times. This variable
67 ** has no function other than to help verify the correct operation of the
68 ** library.
69 */
70 #ifdef SQLITE_TEST
71 SQLITE_API int sqlite3_sort_count = 0;
72 #endif
73
74 /*
75 ** The next global variable records the size of the largest MEM_Blob
76 ** or MEM_Str that has been used by a VDBE opcode. The test procedures
77 ** use this information to make sure that the zero-blob functionality
78 ** is working correctly. This variable has no function other than to
79 ** help verify the correct operation of the library.
80 */
81 #ifdef SQLITE_TEST
82 SQLITE_API int sqlite3_max_blobsize = 0;
83 static void updateMaxBlobsize(Mem *p){
84 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
85 sqlite3_max_blobsize = p->n;
86 }
87 }
88 #endif
89
90 /*
91 ** This macro evaluates to true if either the update hook or the preupdate
92 ** hook are enabled for database connect DB.
93 */
94 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
95 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
96 #else
97 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
98 #endif
99
100 /*
101 ** The next global variable is incremented each time the OP_Found opcode
102 ** is executed. This is used to test whether or not the foreign key
103 ** operation implemented using OP_FkIsZero is working. This variable
104 ** has no function other than to help verify the correct operation of the
105 ** library.
106 */
107 #ifdef SQLITE_TEST
108 SQLITE_API int sqlite3_found_count = 0;
109 #endif
110
111 /*
112 ** Test a register to see if it exceeds the current maximum blob size.
113 ** If it does, record the new maximum blob size.
114 */
115 #if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
116 # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
117 #else
118 # define UPDATE_MAX_BLOBSIZE(P)
119 #endif
120
121 /*
122 ** Invoke the VDBE coverage callback, if that callback is defined. This
123 ** feature is used for test suite validation only and does not appear an
124 ** production builds.
125 **
126 ** M is an integer, 2 or 3, that indices how many different ways the
127 ** branch can go. It is usually 2. "I" is the direction the branch
128 ** goes. 0 means falls through. 1 means branch is taken. 2 means the
129 ** second alternative branch is taken.
130 **
131 ** iSrcLine is the source code line (from the __LINE__ macro) that
132 ** generated the VDBE instruction. This instrumentation assumes that all
133 ** source code is in a single file (the amalgamation). Special values 1
134 ** and 2 for the iSrcLine parameter mean that this particular branch is
135 ** always taken or never taken, respectively.
136 */
137 #if !defined(SQLITE_VDBE_COVERAGE)
138 # define VdbeBranchTaken(I,M)
139 #else
140 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
141 static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
142 if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
143 M = iSrcLine;
144 /* Assert the truth of VdbeCoverageAlwaysTaken() and
145 ** VdbeCoverageNeverTaken() */
146 assert( (M & I)==I );
147 }else{
148 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
149 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
150 iSrcLine,I,M);
151 }
152 }
153 #endif
154
155 /*
156 ** Convert the given register into a string if it isn't one
157 ** already. Return non-zero if a malloc() fails.
158 */
159 #define Stringify(P, enc) \
160 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
161 { goto no_mem; }
162
163 /*
164 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
165 ** a pointer to a dynamically allocated string where some other entity
166 ** is responsible for deallocating that string. Because the register
167 ** does not control the string, it might be deleted without the register
168 ** knowing it.
169 **
170 ** This routine converts an ephemeral string into a dynamically allocated
171 ** string that the register itself controls. In other words, it
172 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
173 */
174 #define Deephemeralize(P) \
175 if( ((P)->flags&MEM_Ephem)!=0 \
176 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
177
178 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
179 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
180
181 /*
182 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
183 ** if we run out of memory.
184 */
185 static VdbeCursor *allocateCursor(
186 Vdbe *p, /* The virtual machine */
187 int iCur, /* Index of the new VdbeCursor */
188 int nField, /* Number of fields in the table or index */
189 int iDb, /* Database the cursor belongs to, or -1 */
190 u8 eCurType /* Type of the new cursor */
191 ){
192 /* Find the memory cell that will be used to store the blob of memory
193 ** required for this VdbeCursor structure. It is convenient to use a
194 ** vdbe memory cell to manage the memory allocation required for a
195 ** VdbeCursor structure for the following reasons:
196 **
197 ** * Sometimes cursor numbers are used for a couple of different
198 ** purposes in a vdbe program. The different uses might require
199 ** different sized allocations. Memory cells provide growable
200 ** allocations.
201 **
202 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
203 ** be freed lazily via the sqlite3_release_memory() API. This
204 ** minimizes the number of malloc calls made by the system.
205 **
206 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
207 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
208 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
209 */
210 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
211
212 int nByte;
213 VdbeCursor *pCx = 0;
214 nByte =
215 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
216 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
217
218 assert( iCur>=0 && iCur<p->nCursor );
219 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
220 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
221 p->apCsr[iCur] = 0;
222 }
223 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
224 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
225 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
226 pCx->eCurType = eCurType;
227 pCx->iDb = iDb;
228 pCx->nField = nField;
229 pCx->aOffset = &pCx->aType[nField];
230 if( eCurType==CURTYPE_BTREE ){
231 pCx->uc.pCursor = (BtCursor*)
232 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
233 sqlite3BtreeCursorZero(pCx->uc.pCursor);
234 }
235 }
236 return pCx;
237 }
238
239 /*
240 ** Try to convert a value into a numeric representation if we can
241 ** do so without loss of information. In other words, if the string
242 ** looks like a number, convert it into a number. If it does not
243 ** look like a number, leave it alone.
244 **
245 ** If the bTryForInt flag is true, then extra effort is made to give
246 ** an integer representation. Strings that look like floating point
247 ** values but which have no fractional component (example: '48.00')
248 ** will have a MEM_Int representation when bTryForInt is true.
249 **
250 ** If bTryForInt is false, then if the input string contains a decimal
251 ** point or exponential notation, the result is only MEM_Real, even
252 ** if there is an exact integer representation of the quantity.
253 */
254 static void applyNumericAffinity(Mem *pRec, int bTryForInt){
255 double rValue;
256 i64 iValue;
257 u8 enc = pRec->enc;
258 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
259 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
260 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
261 pRec->u.i = iValue;
262 pRec->flags |= MEM_Int;
263 }else{
264 pRec->u.r = rValue;
265 pRec->flags |= MEM_Real;
266 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
267 }
268 }
269
270 /*
271 ** Processing is determine by the affinity parameter:
272 **
273 ** SQLITE_AFF_INTEGER:
274 ** SQLITE_AFF_REAL:
275 ** SQLITE_AFF_NUMERIC:
276 ** Try to convert pRec to an integer representation or a
277 ** floating-point representation if an integer representation
278 ** is not possible. Note that the integer representation is
279 ** always preferred, even if the affinity is REAL, because
280 ** an integer representation is more space efficient on disk.
281 **
282 ** SQLITE_AFF_TEXT:
283 ** Convert pRec to a text representation.
284 **
285 ** SQLITE_AFF_BLOB:
286 ** No-op. pRec is unchanged.
287 */
288 static void applyAffinity(
289 Mem *pRec, /* The value to apply affinity to */
290 char affinity, /* The affinity to be applied */
291 u8 enc /* Use this text encoding */
292 ){
293 if( affinity>=SQLITE_AFF_NUMERIC ){
294 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
295 || affinity==SQLITE_AFF_NUMERIC );
296 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
297 if( (pRec->flags & MEM_Real)==0 ){
298 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
299 }else{
300 sqlite3VdbeIntegerAffinity(pRec);
301 }
302 }
303 }else if( affinity==SQLITE_AFF_TEXT ){
304 /* Only attempt the conversion to TEXT if there is an integer or real
305 ** representation (blob and NULL do not get converted) but no string
306 ** representation. It would be harmless to repeat the conversion if
307 ** there is already a string rep, but it is pointless to waste those
308 ** CPU cycles. */
309 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
310 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
311 sqlite3VdbeMemStringify(pRec, enc, 1);
312 }
313 }
314 pRec->flags &= ~(MEM_Real|MEM_Int);
315 }
316 }
317
318 /*
319 ** Try to convert the type of a function argument or a result column
320 ** into a numeric representation. Use either INTEGER or REAL whichever
321 ** is appropriate. But only do the conversion if it is possible without
322 ** loss of information and return the revised type of the argument.
323 */
324 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
325 int eType = sqlite3_value_type(pVal);
326 if( eType==SQLITE_TEXT ){
327 Mem *pMem = (Mem*)pVal;
328 applyNumericAffinity(pMem, 0);
329 eType = sqlite3_value_type(pVal);
330 }
331 return eType;
332 }
333
334 /*
335 ** Exported version of applyAffinity(). This one works on sqlite3_value*,
336 ** not the internal Mem* type.
337 */
338 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
339 sqlite3_value *pVal,
340 u8 affinity,
341 u8 enc
342 ){
343 applyAffinity((Mem *)pVal, affinity, enc);
344 }
345
346 /*
347 ** pMem currently only holds a string type (or maybe a BLOB that we can
348 ** interpret as a string if we want to). Compute its corresponding
349 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
350 ** accordingly.
351 */
352 static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
353 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
354 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
355 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
356 return 0;
357 }
358 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
359 return MEM_Int;
360 }
361 return MEM_Real;
362 }
363
364 /*
365 ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
366 ** none.
367 **
368 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
369 ** But it does set pMem->u.r and pMem->u.i appropriately.
370 */
371 static u16 numericType(Mem *pMem){
372 if( pMem->flags & (MEM_Int|MEM_Real) ){
373 return pMem->flags & (MEM_Int|MEM_Real);
374 }
375 if( pMem->flags & (MEM_Str|MEM_Blob) ){
376 return computeNumericType(pMem);
377 }
378 return 0;
379 }
380
381 #ifdef SQLITE_DEBUG
382 /*
383 ** Write a nice string representation of the contents of cell pMem
384 ** into buffer zBuf, length nBuf.
385 */
386 SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
387 char *zCsr = zBuf;
388 int f = pMem->flags;
389
390 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
391
392 if( f&MEM_Blob ){
393 int i;
394 char c;
395 if( f & MEM_Dyn ){
396 c = 'z';
397 assert( (f & (MEM_Static|MEM_Ephem))==0 );
398 }else if( f & MEM_Static ){
399 c = 't';
400 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
401 }else if( f & MEM_Ephem ){
402 c = 'e';
403 assert( (f & (MEM_Static|MEM_Dyn))==0 );
404 }else{
405 c = 's';
406 }
407
408 sqlite3_snprintf(100, zCsr, "%c", c);
409 zCsr += sqlite3Strlen30(zCsr);
410 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
411 zCsr += sqlite3Strlen30(zCsr);
412 for(i=0; i<16 && i<pMem->n; i++){
413 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
414 zCsr += sqlite3Strlen30(zCsr);
415 }
416 for(i=0; i<16 && i<pMem->n; i++){
417 char z = pMem->z[i];
418 if( z<32 || z>126 ) *zCsr++ = '.';
419 else *zCsr++ = z;
420 }
421
422 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
423 zCsr += sqlite3Strlen30(zCsr);
424 if( f & MEM_Zero ){
425 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
426 zCsr += sqlite3Strlen30(zCsr);
427 }
428 *zCsr = '\0';
429 }else if( f & MEM_Str ){
430 int j, k;
431 zBuf[0] = ' ';
432 if( f & MEM_Dyn ){
433 zBuf[1] = 'z';
434 assert( (f & (MEM_Static|MEM_Ephem))==0 );
435 }else if( f & MEM_Static ){
436 zBuf[1] = 't';
437 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
438 }else if( f & MEM_Ephem ){
439 zBuf[1] = 'e';
440 assert( (f & (MEM_Static|MEM_Dyn))==0 );
441 }else{
442 zBuf[1] = 's';
443 }
444 k = 2;
445 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
446 k += sqlite3Strlen30(&zBuf[k]);
447 zBuf[k++] = '[';
448 for(j=0; j<15 && j<pMem->n; j++){
449 u8 c = pMem->z[j];
450 if( c>=0x20 && c<0x7f ){
451 zBuf[k++] = c;
452 }else{
453 zBuf[k++] = '.';
454 }
455 }
456 zBuf[k++] = ']';
457 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
458 k += sqlite3Strlen30(&zBuf[k]);
459 zBuf[k++] = 0;
460 }
461 }
462 #endif
463
464 #ifdef SQLITE_DEBUG
465 /*
466 ** Print the value of a register for tracing purposes:
467 */
468 static void memTracePrint(Mem *p){
469 if( p->flags & MEM_Undefined ){
470 printf(" undefined");
471 }else if( p->flags & MEM_Null ){
472 printf(" NULL");
473 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
474 printf(" si:%lld", p->u.i);
475 }else if( p->flags & MEM_Int ){
476 printf(" i:%lld", p->u.i);
477 #ifndef SQLITE_OMIT_FLOATING_POINT
478 }else if( p->flags & MEM_Real ){
479 printf(" r:%g", p->u.r);
480 #endif
481 }else if( p->flags & MEM_RowSet ){
482 printf(" (rowset)");
483 }else{
484 char zBuf[200];
485 sqlite3VdbeMemPrettyPrint(p, zBuf);
486 printf(" %s", zBuf);
487 }
488 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
489 }
490 static void registerTrace(int iReg, Mem *p){
491 printf("REG[%d] = ", iReg);
492 memTracePrint(p);
493 printf("\n");
494 }
495 #endif
496
497 #ifdef SQLITE_DEBUG
498 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
499 #else
500 # define REGISTER_TRACE(R,M)
501 #endif
502
503
504 #ifdef VDBE_PROFILE
505
506 /*
507 ** hwtime.h contains inline assembler code for implementing
508 ** high-performance timing routines.
509 */
510 /************** Include hwtime.h in the middle of vdbe.c *********************/
511 /************** Begin file hwtime.h ******************************************/
512 /*
513 ** 2008 May 27
514 **
515 ** The author disclaims copyright to this source code. In place of
516 ** a legal notice, here is a blessing:
517 **
518 ** May you do good and not evil.
519 ** May you find forgiveness for yourself and forgive others.
520 ** May you share freely, never taking more than you give.
521 **
522 ******************************************************************************
523 **
524 ** This file contains inline asm code for retrieving "high-performance"
525 ** counters for x86 class CPUs.
526 */
527 #ifndef SQLITE_HWTIME_H
528 #define SQLITE_HWTIME_H
529
530 /*
531 ** The following routine only works on pentium-class (or newer) processors.
532 ** It uses the RDTSC opcode to read the cycle count value out of the
533 ** processor and returns that value. This can be used for high-res
534 ** profiling.
535 */
536 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
537 (defined(i386) || defined(__i386__) || defined(_M_IX86))
538
539 #if defined(__GNUC__)
540
541 __inline__ sqlite_uint64 sqlite3Hwtime(void){
542 unsigned int lo, hi;
543 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
544 return (sqlite_uint64)hi << 32 | lo;
545 }
546
547 #elif defined(_MSC_VER)
548
549 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
550 __asm {
551 rdtsc
552 ret ; return value at EDX:EAX
553 }
554 }
555
556 #endif
557
558 #elif (defined(__GNUC__) && defined(__x86_64__))
559
560 __inline__ sqlite_uint64 sqlite3Hwtime(void){
561 unsigned long val;
562 __asm__ __volatile__ ("rdtsc" : "=A" (val));
563 return val;
564 }
565
566 #elif (defined(__GNUC__) && defined(__ppc__))
567
568 __inline__ sqlite_uint64 sqlite3Hwtime(void){
569 unsigned long long retval;
570 unsigned long junk;
571 __asm__ __volatile__ ("\n\
572 1: mftbu %1\n\
573 mftb %L0\n\
574 mftbu %0\n\
575 cmpw %0,%1\n\
576 bne 1b"
577 : "=r" (retval), "=r" (junk));
578 return retval;
579 }
580
581 #else
582
583 #error Need implementation of sqlite3Hwtime() for your platform.
584
585 /*
586 ** To compile without implementing sqlite3Hwtime() for your platform,
587 ** you can remove the above #error and use the following
588 ** stub function. You will lose timing support for many
589 ** of the debugging and testing utilities, but it should at
590 ** least compile and run.
591 */
592 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
593
594 #endif
595
596 #endif /* !defined(SQLITE_HWTIME_H) */
597
598 /************** End of hwtime.h **********************************************/
599 /************** Continuing where we left off in vdbe.c ***********************/
600
601 #endif
602
603 #ifndef NDEBUG
604 /*
605 ** This function is only called from within an assert() expression. It
606 ** checks that the sqlite3.nTransaction variable is correctly set to
607 ** the number of non-transaction savepoints currently in the
608 ** linked list starting at sqlite3.pSavepoint.
609 **
610 ** Usage:
611 **
612 ** assert( checkSavepointCount(db) );
613 */
614 static int checkSavepointCount(sqlite3 *db){
615 int n = 0;
616 Savepoint *p;
617 for(p=db->pSavepoint; p; p=p->pNext) n++;
618 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
619 return 1;
620 }
621 #endif
622
623 /*
624 ** Return the register of pOp->p2 after first preparing it to be
625 ** overwritten with an integer value.
626 */
627 static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
628 sqlite3VdbeMemSetNull(pOut);
629 pOut->flags = MEM_Int;
630 return pOut;
631 }
632 static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
633 Mem *pOut;
634 assert( pOp->p2>0 );
635 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
636 pOut = &p->aMem[pOp->p2];
637 memAboutToChange(p, pOut);
638 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
639 return out2PrereleaseWithClear(pOut);
640 }else{
641 pOut->flags = MEM_Int;
642 return pOut;
643 }
644 }
645
646
647 /*
648 ** Execute as much of a VDBE program as we can.
649 ** This is the core of sqlite3_step().
650 */
651 SQLITE_PRIVATE int sqlite3VdbeExec(
652 Vdbe *p /* The VDBE */
653 ){
654 Op *aOp = p->aOp; /* Copy of p->aOp */
655 Op *pOp = aOp; /* Current operation */
656 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
657 Op *pOrigOp; /* Value of pOp at the top of the loop */
658 #endif
659 #ifdef SQLITE_DEBUG
660 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
661 #endif
662 int rc = SQLITE_OK; /* Value to return */
663 sqlite3 *db = p->db; /* The database */
664 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
665 u8 encoding = ENC(db); /* The database encoding */
666 int iCompare = 0; /* Result of last comparison */
667 unsigned nVmStep = 0; /* Number of virtual machine steps */
668 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
669 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
670 #endif
671 Mem *aMem = p->aMem; /* Copy of p->aMem */
672 Mem *pIn1 = 0; /* 1st input operand */
673 Mem *pIn2 = 0; /* 2nd input operand */
674 Mem *pIn3 = 0; /* 3rd input operand */
675 Mem *pOut = 0; /* Output operand */
676 #ifdef VDBE_PROFILE
677 u64 start; /* CPU clock count at start of opcode */
678 #endif
679 /*** INSERT STACK UNION HERE ***/
680
681 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
682 sqlite3VdbeEnter(p);
683 if( p->rc==SQLITE_NOMEM ){
684 /* This happens if a malloc() inside a call to sqlite3_column_text() or
685 ** sqlite3_column_text16() failed. */
686 goto no_mem;
687 }
688 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
689 assert( p->bIsReader || p->readOnly!=0 );
690 p->iCurrentTime = 0;
691 assert( p->explain==0 );
692 p->pResultSet = 0;
693 db->busyHandler.nBusy = 0;
694 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
695 sqlite3VdbeIOTraceSql(p);
696 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
697 if( db->xProgress ){
698 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
699 assert( 0 < db->nProgressOps );
700 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
701 }
702 #endif
703 #ifdef SQLITE_DEBUG
704 sqlite3BeginBenignMalloc();
705 if( p->pc==0
706 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
707 ){
708 int i;
709 int once = 1;
710 sqlite3VdbePrintSql(p);
711 if( p->db->flags & SQLITE_VdbeListing ){
712 printf("VDBE Program Listing:\n");
713 for(i=0; i<p->nOp; i++){
714 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
715 }
716 }
717 if( p->db->flags & SQLITE_VdbeEQP ){
718 for(i=0; i<p->nOp; i++){
719 if( aOp[i].opcode==OP_Explain ){
720 if( once ) printf("VDBE Query Plan:\n");
721 printf("%s\n", aOp[i].p4.z);
722 once = 0;
723 }
724 }
725 }
726 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
727 }
728 sqlite3EndBenignMalloc();
729 #endif
730 for(pOp=&aOp[p->pc]; 1; pOp++){
731 /* Errors are detected by individual opcodes, with an immediate
732 ** jumps to abort_due_to_error. */
733 assert( rc==SQLITE_OK );
734
735 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
736 #ifdef VDBE_PROFILE
737 start = sqlite3Hwtime();
738 #endif
739 nVmStep++;
740 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
741 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
742 #endif
743
744 /* Only allow tracing if SQLITE_DEBUG is defined.
745 */
746 #ifdef SQLITE_DEBUG
747 if( db->flags & SQLITE_VdbeTrace ){
748 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
749 }
750 #endif
751
752
753 /* Check to see if we need to simulate an interrupt. This only happens
754 ** if we have a special test build.
755 */
756 #ifdef SQLITE_TEST
757 if( sqlite3_interrupt_count>0 ){
758 sqlite3_interrupt_count--;
759 if( sqlite3_interrupt_count==0 ){
760 sqlite3_interrupt(db);
761 }
762 }
763 #endif
764
765 /* Sanity checking on other operands */
766 #ifdef SQLITE_DEBUG
767 {
768 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
769 if( (opProperty & OPFLG_IN1)!=0 ){
770 assert( pOp->p1>0 );
771 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
772 assert( memIsValid(&aMem[pOp->p1]) );
773 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
774 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
775 }
776 if( (opProperty & OPFLG_IN2)!=0 ){
777 assert( pOp->p2>0 );
778 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
779 assert( memIsValid(&aMem[pOp->p2]) );
780 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
781 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
782 }
783 if( (opProperty & OPFLG_IN3)!=0 ){
784 assert( pOp->p3>0 );
785 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
786 assert( memIsValid(&aMem[pOp->p3]) );
787 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
788 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
789 }
790 if( (opProperty & OPFLG_OUT2)!=0 ){
791 assert( pOp->p2>0 );
792 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
793 memAboutToChange(p, &aMem[pOp->p2]);
794 }
795 if( (opProperty & OPFLG_OUT3)!=0 ){
796 assert( pOp->p3>0 );
797 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
798 memAboutToChange(p, &aMem[pOp->p3]);
799 }
800 }
801 #endif
802 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
803 pOrigOp = pOp;
804 #endif
805
806 switch( pOp->opcode ){
807
808 /*****************************************************************************
809 ** What follows is a massive switch statement where each case implements a
810 ** separate instruction in the virtual machine. If we follow the usual
811 ** indentation conventions, each case should be indented by 6 spaces. But
812 ** that is a lot of wasted space on the left margin. So the code within
813 ** the switch statement will break with convention and be flush-left. Another
814 ** big comment (similar to this one) will mark the point in the code where
815 ** we transition back to normal indentation.
816 **
817 ** The formatting of each case is important. The makefile for SQLite
818 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
819 ** file looking for lines that begin with "case OP_". The opcodes.h files
820 ** will be filled with #defines that give unique integer values to each
821 ** opcode and the opcodes.c file is filled with an array of strings where
822 ** each string is the symbolic name for the corresponding opcode. If the
823 ** case statement is followed by a comment of the form "/# same as ... #/"
824 ** that comment is used to determine the particular value of the opcode.
825 **
826 ** Other keywords in the comment that follows each case are used to
827 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
828 ** Keywords include: in1, in2, in3, out2, out3. See
829 ** the mkopcodeh.awk script for additional information.
830 **
831 ** Documentation about VDBE opcodes is generated by scanning this file
832 ** for lines of that contain "Opcode:". That line and all subsequent
833 ** comment lines are used in the generation of the opcode.html documentation
834 ** file.
835 **
836 ** SUMMARY:
837 **
838 ** Formatting is important to scripts that scan this file.
839 ** Do not deviate from the formatting style currently in use.
840 **
841 *****************************************************************************/
842
843 /* Opcode: Goto * P2 * * *
844 **
845 ** An unconditional jump to address P2.
846 ** The next instruction executed will be
847 ** the one at index P2 from the beginning of
848 ** the program.
849 **
850 ** The P1 parameter is not actually used by this opcode. However, it
851 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
852 ** that this Goto is the bottom of a loop and that the lines from P2 down
853 ** to the current line should be indented for EXPLAIN output.
854 */
855 case OP_Goto: { /* jump */
856 jump_to_p2_and_check_for_interrupt:
857 pOp = &aOp[pOp->p2 - 1];
858
859 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
860 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
861 ** completion. Check to see if sqlite3_interrupt() has been called
862 ** or if the progress callback needs to be invoked.
863 **
864 ** This code uses unstructured "goto" statements and does not look clean.
865 ** But that is not due to sloppy coding habits. The code is written this
866 ** way for performance, to avoid having to run the interrupt and progress
867 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
868 ** faster according to "valgrind --tool=cachegrind" */
869 check_for_interrupt:
870 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
871 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
872 /* Call the progress callback if it is configured and the required number
873 ** of VDBE ops have been executed (either since this invocation of
874 ** sqlite3VdbeExec() or since last time the progress callback was called).
875 ** If the progress callback returns non-zero, exit the virtual machine with
876 ** a return code SQLITE_ABORT.
877 */
878 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
879 assert( db->nProgressOps!=0 );
880 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
881 if( db->xProgress(db->pProgressArg) ){
882 rc = SQLITE_INTERRUPT;
883 goto abort_due_to_error;
884 }
885 }
886 #endif
887
888 break;
889 }
890
891 /* Opcode: Gosub P1 P2 * * *
892 **
893 ** Write the current address onto register P1
894 ** and then jump to address P2.
895 */
896 case OP_Gosub: { /* jump */
897 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
898 pIn1 = &aMem[pOp->p1];
899 assert( VdbeMemDynamic(pIn1)==0 );
900 memAboutToChange(p, pIn1);
901 pIn1->flags = MEM_Int;
902 pIn1->u.i = (int)(pOp-aOp);
903 REGISTER_TRACE(pOp->p1, pIn1);
904
905 /* Most jump operations do a goto to this spot in order to update
906 ** the pOp pointer. */
907 jump_to_p2:
908 pOp = &aOp[pOp->p2 - 1];
909 break;
910 }
911
912 /* Opcode: Return P1 * * * *
913 **
914 ** Jump to the next instruction after the address in register P1. After
915 ** the jump, register P1 becomes undefined.
916 */
917 case OP_Return: { /* in1 */
918 pIn1 = &aMem[pOp->p1];
919 assert( pIn1->flags==MEM_Int );
920 pOp = &aOp[pIn1->u.i];
921 pIn1->flags = MEM_Undefined;
922 break;
923 }
924
925 /* Opcode: InitCoroutine P1 P2 P3 * *
926 **
927 ** Set up register P1 so that it will Yield to the coroutine
928 ** located at address P3.
929 **
930 ** If P2!=0 then the coroutine implementation immediately follows
931 ** this opcode. So jump over the coroutine implementation to
932 ** address P2.
933 **
934 ** See also: EndCoroutine
935 */
936 case OP_InitCoroutine: { /* jump */
937 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
938 assert( pOp->p2>=0 && pOp->p2<p->nOp );
939 assert( pOp->p3>=0 && pOp->p3<p->nOp );
940 pOut = &aMem[pOp->p1];
941 assert( !VdbeMemDynamic(pOut) );
942 pOut->u.i = pOp->p3 - 1;
943 pOut->flags = MEM_Int;
944 if( pOp->p2 ) goto jump_to_p2;
945 break;
946 }
947
948 /* Opcode: EndCoroutine P1 * * * *
949 **
950 ** The instruction at the address in register P1 is a Yield.
951 ** Jump to the P2 parameter of that Yield.
952 ** After the jump, register P1 becomes undefined.
953 **
954 ** See also: InitCoroutine
955 */
956 case OP_EndCoroutine: { /* in1 */
957 VdbeOp *pCaller;
958 pIn1 = &aMem[pOp->p1];
959 assert( pIn1->flags==MEM_Int );
960 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
961 pCaller = &aOp[pIn1->u.i];
962 assert( pCaller->opcode==OP_Yield );
963 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
964 pOp = &aOp[pCaller->p2 - 1];
965 pIn1->flags = MEM_Undefined;
966 break;
967 }
968
969 /* Opcode: Yield P1 P2 * * *
970 **
971 ** Swap the program counter with the value in register P1. This
972 ** has the effect of yielding to a coroutine.
973 **
974 ** If the coroutine that is launched by this instruction ends with
975 ** Yield or Return then continue to the next instruction. But if
976 ** the coroutine launched by this instruction ends with
977 ** EndCoroutine, then jump to P2 rather than continuing with the
978 ** next instruction.
979 **
980 ** See also: InitCoroutine
981 */
982 case OP_Yield: { /* in1, jump */
983 int pcDest;
984 pIn1 = &aMem[pOp->p1];
985 assert( VdbeMemDynamic(pIn1)==0 );
986 pIn1->flags = MEM_Int;
987 pcDest = (int)pIn1->u.i;
988 pIn1->u.i = (int)(pOp - aOp);
989 REGISTER_TRACE(pOp->p1, pIn1);
990 pOp = &aOp[pcDest];
991 break;
992 }
993
994 /* Opcode: HaltIfNull P1 P2 P3 P4 P5
995 ** Synopsis: if r[P3]=null halt
996 **
997 ** Check the value in register P3. If it is NULL then Halt using
998 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the
999 ** value in register P3 is not NULL, then this routine is a no-op.
1000 ** The P5 parameter should be 1.
1001 */
1002 case OP_HaltIfNull: { /* in3 */
1003 pIn3 = &aMem[pOp->p3];
1004 if( (pIn3->flags & MEM_Null)==0 ) break;
1005 /* Fall through into OP_Halt */
1006 }
1007
1008 /* Opcode: Halt P1 P2 * P4 P5
1009 **
1010 ** Exit immediately. All open cursors, etc are closed
1011 ** automatically.
1012 **
1013 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1014 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1015 ** For errors, it can be some other value. If P1!=0 then P2 will determine
1016 ** whether or not to rollback the current transaction. Do not rollback
1017 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1018 ** then back out all changes that have occurred during this execution of the
1019 ** VDBE, but do not rollback the transaction.
1020 **
1021 ** If P4 is not null then it is an error message string.
1022 **
1023 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
1024 **
1025 ** 0: (no change)
1026 ** 1: NOT NULL contraint failed: P4
1027 ** 2: UNIQUE constraint failed: P4
1028 ** 3: CHECK constraint failed: P4
1029 ** 4: FOREIGN KEY constraint failed: P4
1030 **
1031 ** If P5 is not zero and P4 is NULL, then everything after the ":" is
1032 ** omitted.
1033 **
1034 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
1035 ** every program. So a jump past the last instruction of the program
1036 ** is the same as executing Halt.
1037 */
1038 case OP_Halt: {
1039 VdbeFrame *pFrame;
1040 int pcx;
1041
1042 pcx = (int)(pOp - aOp);
1043 if( pOp->p1==SQLITE_OK && p->pFrame ){
1044 /* Halt the sub-program. Return control to the parent frame. */
1045 pFrame = p->pFrame;
1046 p->pFrame = pFrame->pParent;
1047 p->nFrame--;
1048 sqlite3VdbeSetChanges(db, p->nChange);
1049 pcx = sqlite3VdbeFrameRestore(pFrame);
1050 if( pOp->p2==OE_Ignore ){
1051 /* Instruction pcx is the OP_Program that invoked the sub-program
1052 ** currently being halted. If the p2 instruction of this OP_Halt
1053 ** instruction is set to OE_Ignore, then the sub-program is throwing
1054 ** an IGNORE exception. In this case jump to the address specified
1055 ** as the p2 of the calling OP_Program. */
1056 pcx = p->aOp[pcx].p2-1;
1057 }
1058 aOp = p->aOp;
1059 aMem = p->aMem;
1060 pOp = &aOp[pcx];
1061 break;
1062 }
1063 p->rc = pOp->p1;
1064 p->errorAction = (u8)pOp->p2;
1065 p->pc = pcx;
1066 assert( pOp->p5<=4 );
1067 if( p->rc ){
1068 if( pOp->p5 ){
1069 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1070 "FOREIGN KEY" };
1071 testcase( pOp->p5==1 );
1072 testcase( pOp->p5==2 );
1073 testcase( pOp->p5==3 );
1074 testcase( pOp->p5==4 );
1075 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1076 if( pOp->p4.z ){
1077 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1078 }
1079 }else{
1080 sqlite3VdbeError(p, "%s", pOp->p4.z);
1081 }
1082 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
1083 }
1084 rc = sqlite3VdbeHalt(p);
1085 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
1086 if( rc==SQLITE_BUSY ){
1087 p->rc = SQLITE_BUSY;
1088 }else{
1089 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
1090 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
1091 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
1092 }
1093 goto vdbe_return;
1094 }
1095
1096 /* Opcode: Integer P1 P2 * * *
1097 ** Synopsis: r[P2]=P1
1098 **
1099 ** The 32-bit integer value P1 is written into register P2.
1100 */
1101 case OP_Integer: { /* out2 */
1102 pOut = out2Prerelease(p, pOp);
1103 pOut->u.i = pOp->p1;
1104 break;
1105 }
1106
1107 /* Opcode: Int64 * P2 * P4 *
1108 ** Synopsis: r[P2]=P4
1109 **
1110 ** P4 is a pointer to a 64-bit integer value.
1111 ** Write that value into register P2.
1112 */
1113 case OP_Int64: { /* out2 */
1114 pOut = out2Prerelease(p, pOp);
1115 assert( pOp->p4.pI64!=0 );
1116 pOut->u.i = *pOp->p4.pI64;
1117 break;
1118 }
1119
1120 #ifndef SQLITE_OMIT_FLOATING_POINT
1121 /* Opcode: Real * P2 * P4 *
1122 ** Synopsis: r[P2]=P4
1123 **
1124 ** P4 is a pointer to a 64-bit floating point value.
1125 ** Write that value into register P2.
1126 */
1127 case OP_Real: { /* same as TK_FLOAT, out2 */
1128 pOut = out2Prerelease(p, pOp);
1129 pOut->flags = MEM_Real;
1130 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
1131 pOut->u.r = *pOp->p4.pReal;
1132 break;
1133 }
1134 #endif
1135
1136 /* Opcode: String8 * P2 * P4 *
1137 ** Synopsis: r[P2]='P4'
1138 **
1139 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
1140 ** into a String opcode before it is executed for the first time. During
1141 ** this transformation, the length of string P4 is computed and stored
1142 ** as the P1 parameter.
1143 */
1144 case OP_String8: { /* same as TK_STRING, out2 */
1145 assert( pOp->p4.z!=0 );
1146 pOut = out2Prerelease(p, pOp);
1147 pOp->opcode = OP_String;
1148 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
1149
1150 #ifndef SQLITE_OMIT_UTF16
1151 if( encoding!=SQLITE_UTF8 ){
1152 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
1153 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
1154 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
1155 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
1156 assert( VdbeMemDynamic(pOut)==0 );
1157 pOut->szMalloc = 0;
1158 pOut->flags |= MEM_Static;
1159 if( pOp->p4type==P4_DYNAMIC ){
1160 sqlite3DbFree(db, pOp->p4.z);
1161 }
1162 pOp->p4type = P4_DYNAMIC;
1163 pOp->p4.z = pOut->z;
1164 pOp->p1 = pOut->n;
1165 }
1166 testcase( rc==SQLITE_TOOBIG );
1167 #endif
1168 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1169 goto too_big;
1170 }
1171 assert( rc==SQLITE_OK );
1172 /* Fall through to the next case, OP_String */
1173 }
1174
1175 /* Opcode: String P1 P2 P3 P4 P5
1176 ** Synopsis: r[P2]='P4' (len=P1)
1177 **
1178 ** The string value P4 of length P1 (bytes) is stored in register P2.
1179 **
1180 ** If P3 is not zero and the content of register P3 is equal to P5, then
1181 ** the datatype of the register P2 is converted to BLOB. The content is
1182 ** the same sequence of bytes, it is merely interpreted as a BLOB instead
1183 ** of a string, as if it had been CAST. In other words:
1184 **
1185 ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
1186 */
1187 case OP_String: { /* out2 */
1188 assert( pOp->p4.z!=0 );
1189 pOut = out2Prerelease(p, pOp);
1190 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1191 pOut->z = pOp->p4.z;
1192 pOut->n = pOp->p1;
1193 pOut->enc = encoding;
1194 UPDATE_MAX_BLOBSIZE(pOut);
1195 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
1196 if( pOp->p3>0 ){
1197 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
1198 pIn3 = &aMem[pOp->p3];
1199 assert( pIn3->flags & MEM_Int );
1200 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
1201 }
1202 #endif
1203 break;
1204 }
1205
1206 /* Opcode: Null P1 P2 P3 * *
1207 ** Synopsis: r[P2..P3]=NULL
1208 **
1209 ** Write a NULL into registers P2. If P3 greater than P2, then also write
1210 ** NULL into register P3 and every register in between P2 and P3. If P3
1211 ** is less than P2 (typically P3 is zero) then only register P2 is
1212 ** set to NULL.
1213 **
1214 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1215 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1216 ** OP_Ne or OP_Eq.
1217 */
1218 case OP_Null: { /* out2 */
1219 int cnt;
1220 u16 nullFlag;
1221 pOut = out2Prerelease(p, pOp);
1222 cnt = pOp->p3-pOp->p2;
1223 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
1224 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
1225 pOut->n = 0;
1226 while( cnt>0 ){
1227 pOut++;
1228 memAboutToChange(p, pOut);
1229 sqlite3VdbeMemSetNull(pOut);
1230 pOut->flags = nullFlag;
1231 pOut->n = 0;
1232 cnt--;
1233 }
1234 break;
1235 }
1236
1237 /* Opcode: SoftNull P1 * * * *
1238 ** Synopsis: r[P1]=NULL
1239 **
1240 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1241 ** instruction, but do not free any string or blob memory associated with
1242 ** the register, so that if the value was a string or blob that was
1243 ** previously copied using OP_SCopy, the copies will continue to be valid.
1244 */
1245 case OP_SoftNull: {
1246 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
1247 pOut = &aMem[pOp->p1];
1248 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
1249 break;
1250 }
1251
1252 /* Opcode: Blob P1 P2 * P4 *
1253 ** Synopsis: r[P2]=P4 (len=P1)
1254 **
1255 ** P4 points to a blob of data P1 bytes long. Store this
1256 ** blob in register P2.
1257 */
1258 case OP_Blob: { /* out2 */
1259 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
1260 pOut = out2Prerelease(p, pOp);
1261 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
1262 pOut->enc = encoding;
1263 UPDATE_MAX_BLOBSIZE(pOut);
1264 break;
1265 }
1266
1267 /* Opcode: Variable P1 P2 * P4 *
1268 ** Synopsis: r[P2]=parameter(P1,P4)
1269 **
1270 ** Transfer the values of bound parameter P1 into register P2
1271 **
1272 ** If the parameter is named, then its name appears in P4.
1273 ** The P4 value is used by sqlite3_bind_parameter_name().
1274 */
1275 case OP_Variable: { /* out2 */
1276 Mem *pVar; /* Value being transferred */
1277
1278 assert( pOp->p1>0 && pOp->p1<=p->nVar );
1279 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
1280 pVar = &p->aVar[pOp->p1 - 1];
1281 if( sqlite3VdbeMemTooBig(pVar) ){
1282 goto too_big;
1283 }
1284 pOut = &aMem[pOp->p2];
1285 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1286 UPDATE_MAX_BLOBSIZE(pOut);
1287 break;
1288 }
1289
1290 /* Opcode: Move P1 P2 P3 * *
1291 ** Synopsis: r[P2@P3]=r[P1@P3]
1292 **
1293 ** Move the P3 values in register P1..P1+P3-1 over into
1294 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
1295 ** left holding a NULL. It is an error for register ranges
1296 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1297 ** for P3 to be less than 1.
1298 */
1299 case OP_Move: {
1300 int n; /* Number of registers left to copy */
1301 int p1; /* Register to copy from */
1302 int p2; /* Register to copy to */
1303
1304 n = pOp->p3;
1305 p1 = pOp->p1;
1306 p2 = pOp->p2;
1307 assert( n>0 && p1>0 && p2>0 );
1308 assert( p1+n<=p2 || p2+n<=p1 );
1309
1310 pIn1 = &aMem[p1];
1311 pOut = &aMem[p2];
1312 do{
1313 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1314 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
1315 assert( memIsValid(pIn1) );
1316 memAboutToChange(p, pOut);
1317 sqlite3VdbeMemMove(pOut, pIn1);
1318 #ifdef SQLITE_DEBUG
1319 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
1320 pOut->pScopyFrom += pOp->p2 - p1;
1321 }
1322 #endif
1323 Deephemeralize(pOut);
1324 REGISTER_TRACE(p2++, pOut);
1325 pIn1++;
1326 pOut++;
1327 }while( --n );
1328 break;
1329 }
1330
1331 /* Opcode: Copy P1 P2 P3 * *
1332 ** Synopsis: r[P2@P3+1]=r[P1@P3+1]
1333 **
1334 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
1335 **
1336 ** This instruction makes a deep copy of the value. A duplicate
1337 ** is made of any string or blob constant. See also OP_SCopy.
1338 */
1339 case OP_Copy: {
1340 int n;
1341
1342 n = pOp->p3;
1343 pIn1 = &aMem[pOp->p1];
1344 pOut = &aMem[pOp->p2];
1345 assert( pOut!=pIn1 );
1346 while( 1 ){
1347 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1348 Deephemeralize(pOut);
1349 #ifdef SQLITE_DEBUG
1350 pOut->pScopyFrom = 0;
1351 #endif
1352 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1353 if( (n--)==0 ) break;
1354 pOut++;
1355 pIn1++;
1356 }
1357 break;
1358 }
1359
1360 /* Opcode: SCopy P1 P2 * * *
1361 ** Synopsis: r[P2]=r[P1]
1362 **
1363 ** Make a shallow copy of register P1 into register P2.
1364 **
1365 ** This instruction makes a shallow copy of the value. If the value
1366 ** is a string or blob, then the copy is only a pointer to the
1367 ** original and hence if the original changes so will the copy.
1368 ** Worse, if the original is deallocated, the copy becomes invalid.
1369 ** Thus the program must guarantee that the original will not change
1370 ** during the lifetime of the copy. Use OP_Copy to make a complete
1371 ** copy.
1372 */
1373 case OP_SCopy: { /* out2 */
1374 pIn1 = &aMem[pOp->p1];
1375 pOut = &aMem[pOp->p2];
1376 assert( pOut!=pIn1 );
1377 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1378 #ifdef SQLITE_DEBUG
1379 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1380 #endif
1381 break;
1382 }
1383
1384 /* Opcode: IntCopy P1 P2 * * *
1385 ** Synopsis: r[P2]=r[P1]
1386 **
1387 ** Transfer the integer value held in register P1 into register P2.
1388 **
1389 ** This is an optimized version of SCopy that works only for integer
1390 ** values.
1391 */
1392 case OP_IntCopy: { /* out2 */
1393 pIn1 = &aMem[pOp->p1];
1394 assert( (pIn1->flags & MEM_Int)!=0 );
1395 pOut = &aMem[pOp->p2];
1396 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1397 break;
1398 }
1399
1400 /* Opcode: ResultRow P1 P2 * * *
1401 ** Synopsis: output=r[P1@P2]
1402 **
1403 ** The registers P1 through P1+P2-1 contain a single row of
1404 ** results. This opcode causes the sqlite3_step() call to terminate
1405 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1406 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
1407 ** the result row.
1408 */
1409 case OP_ResultRow: {
1410 Mem *pMem;
1411 int i;
1412 assert( p->nResColumn==pOp->p2 );
1413 assert( pOp->p1>0 );
1414 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
1415
1416 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1417 /* Run the progress counter just before returning.
1418 */
1419 if( db->xProgress!=0
1420 && nVmStep>=nProgressLimit
1421 && db->xProgress(db->pProgressArg)!=0
1422 ){
1423 rc = SQLITE_INTERRUPT;
1424 goto abort_due_to_error;
1425 }
1426 #endif
1427
1428 /* If this statement has violated immediate foreign key constraints, do
1429 ** not return the number of rows modified. And do not RELEASE the statement
1430 ** transaction. It needs to be rolled back. */
1431 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1432 assert( db->flags&SQLITE_CountRows );
1433 assert( p->usesStmtJournal );
1434 goto abort_due_to_error;
1435 }
1436
1437 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1438 ** DML statements invoke this opcode to return the number of rows
1439 ** modified to the user. This is the only way that a VM that
1440 ** opens a statement transaction may invoke this opcode.
1441 **
1442 ** In case this is such a statement, close any statement transaction
1443 ** opened by this VM before returning control to the user. This is to
1444 ** ensure that statement-transactions are always nested, not overlapping.
1445 ** If the open statement-transaction is not closed here, then the user
1446 ** may step another VM that opens its own statement transaction. This
1447 ** may lead to overlapping statement transactions.
1448 **
1449 ** The statement transaction is never a top-level transaction. Hence
1450 ** the RELEASE call below can never fail.
1451 */
1452 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
1453 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1454 assert( rc==SQLITE_OK );
1455
1456 /* Invalidate all ephemeral cursor row caches */
1457 p->cacheCtr = (p->cacheCtr + 2)|1;
1458
1459 /* Make sure the results of the current row are \000 terminated
1460 ** and have an assigned type. The results are de-ephemeralized as
1461 ** a side effect.
1462 */
1463 pMem = p->pResultSet = &aMem[pOp->p1];
1464 for(i=0; i<pOp->p2; i++){
1465 assert( memIsValid(&pMem[i]) );
1466 Deephemeralize(&pMem[i]);
1467 assert( (pMem[i].flags & MEM_Ephem)==0
1468 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
1469 sqlite3VdbeMemNulTerminate(&pMem[i]);
1470 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
1471 }
1472 if( db->mallocFailed ) goto no_mem;
1473
1474 if( db->mTrace & SQLITE_TRACE_ROW ){
1475 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1476 }
1477
1478 /* Return SQLITE_ROW
1479 */
1480 p->pc = (int)(pOp - aOp) + 1;
1481 rc = SQLITE_ROW;
1482 goto vdbe_return;
1483 }
1484
1485 /* Opcode: Concat P1 P2 P3 * *
1486 ** Synopsis: r[P3]=r[P2]+r[P1]
1487 **
1488 ** Add the text in register P1 onto the end of the text in
1489 ** register P2 and store the result in register P3.
1490 ** If either the P1 or P2 text are NULL then store NULL in P3.
1491 **
1492 ** P3 = P2 || P1
1493 **
1494 ** It is illegal for P1 and P3 to be the same register. Sometimes,
1495 ** if P3 is the same register as P2, the implementation is able
1496 ** to avoid a memcpy().
1497 */
1498 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
1499 i64 nByte;
1500
1501 pIn1 = &aMem[pOp->p1];
1502 pIn2 = &aMem[pOp->p2];
1503 pOut = &aMem[pOp->p3];
1504 assert( pIn1!=pOut );
1505 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
1506 sqlite3VdbeMemSetNull(pOut);
1507 break;
1508 }
1509 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
1510 Stringify(pIn1, encoding);
1511 Stringify(pIn2, encoding);
1512 nByte = pIn1->n + pIn2->n;
1513 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1514 goto too_big;
1515 }
1516 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
1517 goto no_mem;
1518 }
1519 MemSetTypeFlag(pOut, MEM_Str);
1520 if( pOut!=pIn2 ){
1521 memcpy(pOut->z, pIn2->z, pIn2->n);
1522 }
1523 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1524 pOut->z[nByte]=0;
1525 pOut->z[nByte+1] = 0;
1526 pOut->flags |= MEM_Term;
1527 pOut->n = (int)nByte;
1528 pOut->enc = encoding;
1529 UPDATE_MAX_BLOBSIZE(pOut);
1530 break;
1531 }
1532
1533 /* Opcode: Add P1 P2 P3 * *
1534 ** Synopsis: r[P3]=r[P1]+r[P2]
1535 **
1536 ** Add the value in register P1 to the value in register P2
1537 ** and store the result in register P3.
1538 ** If either input is NULL, the result is NULL.
1539 */
1540 /* Opcode: Multiply P1 P2 P3 * *
1541 ** Synopsis: r[P3]=r[P1]*r[P2]
1542 **
1543 **
1544 ** Multiply the value in register P1 by the value in register P2
1545 ** and store the result in register P3.
1546 ** If either input is NULL, the result is NULL.
1547 */
1548 /* Opcode: Subtract P1 P2 P3 * *
1549 ** Synopsis: r[P3]=r[P2]-r[P1]
1550 **
1551 ** Subtract the value in register P1 from the value in register P2
1552 ** and store the result in register P3.
1553 ** If either input is NULL, the result is NULL.
1554 */
1555 /* Opcode: Divide P1 P2 P3 * *
1556 ** Synopsis: r[P3]=r[P2]/r[P1]
1557 **
1558 ** Divide the value in register P1 by the value in register P2
1559 ** and store the result in register P3 (P3=P2/P1). If the value in
1560 ** register P1 is zero, then the result is NULL. If either input is
1561 ** NULL, the result is NULL.
1562 */
1563 /* Opcode: Remainder P1 P2 P3 * *
1564 ** Synopsis: r[P3]=r[P2]%r[P1]
1565 **
1566 ** Compute the remainder after integer register P2 is divided by
1567 ** register P1 and store the result in register P3.
1568 ** If the value in register P1 is zero the result is NULL.
1569 ** If either operand is NULL, the result is NULL.
1570 */
1571 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1572 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1573 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1574 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1575 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
1576 char bIntint; /* Started out as two integer operands */
1577 u16 flags; /* Combined MEM_* flags from both inputs */
1578 u16 type1; /* Numeric type of left operand */
1579 u16 type2; /* Numeric type of right operand */
1580 i64 iA; /* Integer value of left operand */
1581 i64 iB; /* Integer value of right operand */
1582 double rA; /* Real value of left operand */
1583 double rB; /* Real value of right operand */
1584
1585 pIn1 = &aMem[pOp->p1];
1586 type1 = numericType(pIn1);
1587 pIn2 = &aMem[pOp->p2];
1588 type2 = numericType(pIn2);
1589 pOut = &aMem[pOp->p3];
1590 flags = pIn1->flags | pIn2->flags;
1591 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1592 if( (type1 & type2 & MEM_Int)!=0 ){
1593 iA = pIn1->u.i;
1594 iB = pIn2->u.i;
1595 bIntint = 1;
1596 switch( pOp->opcode ){
1597 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1598 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1599 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
1600 case OP_Divide: {
1601 if( iA==0 ) goto arithmetic_result_is_null;
1602 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
1603 iB /= iA;
1604 break;
1605 }
1606 default: {
1607 if( iA==0 ) goto arithmetic_result_is_null;
1608 if( iA==-1 ) iA = 1;
1609 iB %= iA;
1610 break;
1611 }
1612 }
1613 pOut->u.i = iB;
1614 MemSetTypeFlag(pOut, MEM_Int);
1615 }else{
1616 bIntint = 0;
1617 fp_math:
1618 rA = sqlite3VdbeRealValue(pIn1);
1619 rB = sqlite3VdbeRealValue(pIn2);
1620 switch( pOp->opcode ){
1621 case OP_Add: rB += rA; break;
1622 case OP_Subtract: rB -= rA; break;
1623 case OP_Multiply: rB *= rA; break;
1624 case OP_Divide: {
1625 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
1626 if( rA==(double)0 ) goto arithmetic_result_is_null;
1627 rB /= rA;
1628 break;
1629 }
1630 default: {
1631 iA = (i64)rA;
1632 iB = (i64)rB;
1633 if( iA==0 ) goto arithmetic_result_is_null;
1634 if( iA==-1 ) iA = 1;
1635 rB = (double)(iB % iA);
1636 break;
1637 }
1638 }
1639 #ifdef SQLITE_OMIT_FLOATING_POINT
1640 pOut->u.i = rB;
1641 MemSetTypeFlag(pOut, MEM_Int);
1642 #else
1643 if( sqlite3IsNaN(rB) ){
1644 goto arithmetic_result_is_null;
1645 }
1646 pOut->u.r = rB;
1647 MemSetTypeFlag(pOut, MEM_Real);
1648 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
1649 sqlite3VdbeIntegerAffinity(pOut);
1650 }
1651 #endif
1652 }
1653 break;
1654
1655 arithmetic_result_is_null:
1656 sqlite3VdbeMemSetNull(pOut);
1657 break;
1658 }
1659
1660 /* Opcode: CollSeq P1 * * P4
1661 **
1662 ** P4 is a pointer to a CollSeq struct. If the next call to a user function
1663 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1664 ** be returned. This is used by the built-in min(), max() and nullif()
1665 ** functions.
1666 **
1667 ** If P1 is not zero, then it is a register that a subsequent min() or
1668 ** max() aggregate will set to 1 if the current row is not the minimum or
1669 ** maximum. The P1 register is initialized to 0 by this instruction.
1670 **
1671 ** The interface used by the implementation of the aforementioned functions
1672 ** to retrieve the collation sequence set by this opcode is not available
1673 ** publicly. Only built-in functions have access to this feature.
1674 */
1675 case OP_CollSeq: {
1676 assert( pOp->p4type==P4_COLLSEQ );
1677 if( pOp->p1 ){
1678 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1679 }
1680 break;
1681 }
1682
1683 /* Opcode: Function0 P1 P2 P3 P4 P5
1684 ** Synopsis: r[P3]=func(r[P2@P5])
1685 **
1686 ** Invoke a user function (P4 is a pointer to a FuncDef object that
1687 ** defines the function) with P5 arguments taken from register P2 and
1688 ** successors. The result of the function is stored in register P3.
1689 ** Register P3 must not be one of the function inputs.
1690 **
1691 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
1692 ** function was determined to be constant at compile time. If the first
1693 ** argument was constant then bit 0 of P1 is set. This is used to determine
1694 ** whether meta data associated with a user function argument using the
1695 ** sqlite3_set_auxdata() API may be safely retained until the next
1696 ** invocation of this opcode.
1697 **
1698 ** See also: Function, AggStep, AggFinal
1699 */
1700 /* Opcode: Function P1 P2 P3 P4 P5
1701 ** Synopsis: r[P3]=func(r[P2@P5])
1702 **
1703 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
1704 ** contains a pointer to the function to be run) with P5 arguments taken
1705 ** from register P2 and successors. The result of the function is stored
1706 ** in register P3. Register P3 must not be one of the function inputs.
1707 **
1708 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
1709 ** function was determined to be constant at compile time. If the first
1710 ** argument was constant then bit 0 of P1 is set. This is used to determine
1711 ** whether meta data associated with a user function argument using the
1712 ** sqlite3_set_auxdata() API may be safely retained until the next
1713 ** invocation of this opcode.
1714 **
1715 ** SQL functions are initially coded as OP_Function0 with P4 pointing
1716 ** to a FuncDef object. But on first evaluation, the P4 operand is
1717 ** automatically converted into an sqlite3_context object and the operation
1718 ** changed to this OP_Function opcode. In this way, the initialization of
1719 ** the sqlite3_context object occurs only once, rather than once for each
1720 ** evaluation of the function.
1721 **
1722 ** See also: Function0, AggStep, AggFinal
1723 */
1724 case OP_Function0: {
1725 int n;
1726 sqlite3_context *pCtx;
1727
1728 assert( pOp->p4type==P4_FUNCDEF );
1729 n = pOp->p5;
1730 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
1731 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
1732 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
1733 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
1734 if( pCtx==0 ) goto no_mem;
1735 pCtx->pOut = 0;
1736 pCtx->pFunc = pOp->p4.pFunc;
1737 pCtx->iOp = (int)(pOp - aOp);
1738 pCtx->pVdbe = p;
1739 pCtx->argc = n;
1740 pOp->p4type = P4_FUNCCTX;
1741 pOp->p4.pCtx = pCtx;
1742 pOp->opcode = OP_Function;
1743 /* Fall through into OP_Function */
1744 }
1745 case OP_Function: {
1746 int i;
1747 sqlite3_context *pCtx;
1748
1749 assert( pOp->p4type==P4_FUNCCTX );
1750 pCtx = pOp->p4.pCtx;
1751
1752 /* If this function is inside of a trigger, the register array in aMem[]
1753 ** might change from one evaluation to the next. The next block of code
1754 ** checks to see if the register array has changed, and if so it
1755 ** reinitializes the relavant parts of the sqlite3_context object */
1756 pOut = &aMem[pOp->p3];
1757 if( pCtx->pOut != pOut ){
1758 pCtx->pOut = pOut;
1759 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
1760 }
1761
1762 memAboutToChange(p, pCtx->pOut);
1763 #ifdef SQLITE_DEBUG
1764 for(i=0; i<pCtx->argc; i++){
1765 assert( memIsValid(pCtx->argv[i]) );
1766 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
1767 }
1768 #endif
1769 MemSetTypeFlag(pCtx->pOut, MEM_Null);
1770 pCtx->fErrorOrAux = 0;
1771 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
1772
1773 /* If the function returned an error, throw an exception */
1774 if( pCtx->fErrorOrAux ){
1775 if( pCtx->isError ){
1776 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
1777 rc = pCtx->isError;
1778 }
1779 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
1780 if( rc ) goto abort_due_to_error;
1781 }
1782
1783 /* Copy the result of the function into register P3 */
1784 if( pOut->flags & (MEM_Str|MEM_Blob) ){
1785 sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
1786 if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
1787 }
1788
1789 REGISTER_TRACE(pOp->p3, pCtx->pOut);
1790 UPDATE_MAX_BLOBSIZE(pCtx->pOut);
1791 break;
1792 }
1793
1794 /* Opcode: BitAnd P1 P2 P3 * *
1795 ** Synopsis: r[P3]=r[P1]&r[P2]
1796 **
1797 ** Take the bit-wise AND of the values in register P1 and P2 and
1798 ** store the result in register P3.
1799 ** If either input is NULL, the result is NULL.
1800 */
1801 /* Opcode: BitOr P1 P2 P3 * *
1802 ** Synopsis: r[P3]=r[P1]|r[P2]
1803 **
1804 ** Take the bit-wise OR of the values in register P1 and P2 and
1805 ** store the result in register P3.
1806 ** If either input is NULL, the result is NULL.
1807 */
1808 /* Opcode: ShiftLeft P1 P2 P3 * *
1809 ** Synopsis: r[P3]=r[P2]<<r[P1]
1810 **
1811 ** Shift the integer value in register P2 to the left by the
1812 ** number of bits specified by the integer in register P1.
1813 ** Store the result in register P3.
1814 ** If either input is NULL, the result is NULL.
1815 */
1816 /* Opcode: ShiftRight P1 P2 P3 * *
1817 ** Synopsis: r[P3]=r[P2]>>r[P1]
1818 **
1819 ** Shift the integer value in register P2 to the right by the
1820 ** number of bits specified by the integer in register P1.
1821 ** Store the result in register P3.
1822 ** If either input is NULL, the result is NULL.
1823 */
1824 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1825 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1826 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1827 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
1828 i64 iA;
1829 u64 uA;
1830 i64 iB;
1831 u8 op;
1832
1833 pIn1 = &aMem[pOp->p1];
1834 pIn2 = &aMem[pOp->p2];
1835 pOut = &aMem[pOp->p3];
1836 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
1837 sqlite3VdbeMemSetNull(pOut);
1838 break;
1839 }
1840 iA = sqlite3VdbeIntValue(pIn2);
1841 iB = sqlite3VdbeIntValue(pIn1);
1842 op = pOp->opcode;
1843 if( op==OP_BitAnd ){
1844 iA &= iB;
1845 }else if( op==OP_BitOr ){
1846 iA |= iB;
1847 }else if( iB!=0 ){
1848 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1849
1850 /* If shifting by a negative amount, shift in the other direction */
1851 if( iB<0 ){
1852 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1853 op = 2*OP_ShiftLeft + 1 - op;
1854 iB = iB>(-64) ? -iB : 64;
1855 }
1856
1857 if( iB>=64 ){
1858 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1859 }else{
1860 memcpy(&uA, &iA, sizeof(uA));
1861 if( op==OP_ShiftLeft ){
1862 uA <<= iB;
1863 }else{
1864 uA >>= iB;
1865 /* Sign-extend on a right shift of a negative number */
1866 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1867 }
1868 memcpy(&iA, &uA, sizeof(iA));
1869 }
1870 }
1871 pOut->u.i = iA;
1872 MemSetTypeFlag(pOut, MEM_Int);
1873 break;
1874 }
1875
1876 /* Opcode: AddImm P1 P2 * * *
1877 ** Synopsis: r[P1]=r[P1]+P2
1878 **
1879 ** Add the constant P2 to the value in register P1.
1880 ** The result is always an integer.
1881 **
1882 ** To force any register to be an integer, just add 0.
1883 */
1884 case OP_AddImm: { /* in1 */
1885 pIn1 = &aMem[pOp->p1];
1886 memAboutToChange(p, pIn1);
1887 sqlite3VdbeMemIntegerify(pIn1);
1888 pIn1->u.i += pOp->p2;
1889 break;
1890 }
1891
1892 /* Opcode: MustBeInt P1 P2 * * *
1893 **
1894 ** Force the value in register P1 to be an integer. If the value
1895 ** in P1 is not an integer and cannot be converted into an integer
1896 ** without data loss, then jump immediately to P2, or if P2==0
1897 ** raise an SQLITE_MISMATCH exception.
1898 */
1899 case OP_MustBeInt: { /* jump, in1 */
1900 pIn1 = &aMem[pOp->p1];
1901 if( (pIn1->flags & MEM_Int)==0 ){
1902 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1903 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
1904 if( (pIn1->flags & MEM_Int)==0 ){
1905 if( pOp->p2==0 ){
1906 rc = SQLITE_MISMATCH;
1907 goto abort_due_to_error;
1908 }else{
1909 goto jump_to_p2;
1910 }
1911 }
1912 }
1913 MemSetTypeFlag(pIn1, MEM_Int);
1914 break;
1915 }
1916
1917 #ifndef SQLITE_OMIT_FLOATING_POINT
1918 /* Opcode: RealAffinity P1 * * * *
1919 **
1920 ** If register P1 holds an integer convert it to a real value.
1921 **
1922 ** This opcode is used when extracting information from a column that
1923 ** has REAL affinity. Such column values may still be stored as
1924 ** integers, for space efficiency, but after extraction we want them
1925 ** to have only a real value.
1926 */
1927 case OP_RealAffinity: { /* in1 */
1928 pIn1 = &aMem[pOp->p1];
1929 if( pIn1->flags & MEM_Int ){
1930 sqlite3VdbeMemRealify(pIn1);
1931 }
1932 break;
1933 }
1934 #endif
1935
1936 #ifndef SQLITE_OMIT_CAST
1937 /* Opcode: Cast P1 P2 * * *
1938 ** Synopsis: affinity(r[P1])
1939 **
1940 ** Force the value in register P1 to be the type defined by P2.
1941 **
1942 ** <ul>
1943 ** <li value="97"> TEXT
1944 ** <li value="98"> BLOB
1945 ** <li value="99"> NUMERIC
1946 ** <li value="100"> INTEGER
1947 ** <li value="101"> REAL
1948 ** </ul>
1949 **
1950 ** A NULL value is not changed by this routine. It remains NULL.
1951 */
1952 case OP_Cast: { /* in1 */
1953 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
1954 testcase( pOp->p2==SQLITE_AFF_TEXT );
1955 testcase( pOp->p2==SQLITE_AFF_BLOB );
1956 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1957 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1958 testcase( pOp->p2==SQLITE_AFF_REAL );
1959 pIn1 = &aMem[pOp->p1];
1960 memAboutToChange(p, pIn1);
1961 rc = ExpandBlob(pIn1);
1962 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
1963 UPDATE_MAX_BLOBSIZE(pIn1);
1964 if( rc ) goto abort_due_to_error;
1965 break;
1966 }
1967 #endif /* SQLITE_OMIT_CAST */
1968
1969 /* Opcode: Eq P1 P2 P3 P4 P5
1970 ** Synopsis: IF r[P3]==r[P1]
1971 **
1972 ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1973 ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1974 ** store the result of comparison in register P2.
1975 **
1976 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1977 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1978 ** to coerce both inputs according to this affinity before the
1979 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1980 ** affinity is used. Note that the affinity conversions are stored
1981 ** back into the input registers P1 and P3. So this opcode can cause
1982 ** persistent changes to registers P1 and P3.
1983 **
1984 ** Once any conversions have taken place, and neither value is NULL,
1985 ** the values are compared. If both values are blobs then memcmp() is
1986 ** used to determine the results of the comparison. If both values
1987 ** are text, then the appropriate collating function specified in
1988 ** P4 is used to do the comparison. If P4 is not specified then
1989 ** memcmp() is used to compare text string. If both values are
1990 ** numeric, then a numeric comparison is used. If the two values
1991 ** are of different types, then numbers are considered less than
1992 ** strings and strings are considered less than blobs.
1993 **
1994 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1995 ** true or false and is never NULL. If both operands are NULL then the result
1996 ** of comparison is true. If either operand is NULL then the result is false.
1997 ** If neither operand is NULL the result is the same as it would be if
1998 ** the SQLITE_NULLEQ flag were omitted from P5.
1999 **
2000 ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
2001 ** content of r[P2] is only changed if the new value is NULL or 0 (false).
2002 ** In other words, a prior r[P2] value will not be overwritten by 1 (true).
2003 */
2004 /* Opcode: Ne P1 P2 P3 P4 P5
2005 ** Synopsis: IF r[P3]!=r[P1]
2006 **
2007 ** This works just like the Eq opcode except that the jump is taken if
2008 ** the operands in registers P1 and P3 are not equal. See the Eq opcode for
2009 ** additional information.
2010 **
2011 ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
2012 ** content of r[P2] is only changed if the new value is NULL or 1 (true).
2013 ** In other words, a prior r[P2] value will not be overwritten by 0 (false).
2014 */
2015 /* Opcode: Lt P1 P2 P3 P4 P5
2016 ** Synopsis: IF r[P3]<r[P1]
2017 **
2018 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
2019 ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
2020 ** the result of comparison (0 or 1 or NULL) into register P2.
2021 **
2022 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
2023 ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
2024 ** bit is clear then fall through if either operand is NULL.
2025 **
2026 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
2027 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
2028 ** to coerce both inputs according to this affinity before the
2029 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
2030 ** affinity is used. Note that the affinity conversions are stored
2031 ** back into the input registers P1 and P3. So this opcode can cause
2032 ** persistent changes to registers P1 and P3.
2033 **
2034 ** Once any conversions have taken place, and neither value is NULL,
2035 ** the values are compared. If both values are blobs then memcmp() is
2036 ** used to determine the results of the comparison. If both values
2037 ** are text, then the appropriate collating function specified in
2038 ** P4 is used to do the comparison. If P4 is not specified then
2039 ** memcmp() is used to compare text string. If both values are
2040 ** numeric, then a numeric comparison is used. If the two values
2041 ** are of different types, then numbers are considered less than
2042 ** strings and strings are considered less than blobs.
2043 */
2044 /* Opcode: Le P1 P2 P3 P4 P5
2045 ** Synopsis: IF r[P3]<=r[P1]
2046 **
2047 ** This works just like the Lt opcode except that the jump is taken if
2048 ** the content of register P3 is less than or equal to the content of
2049 ** register P1. See the Lt opcode for additional information.
2050 */
2051 /* Opcode: Gt P1 P2 P3 P4 P5
2052 ** Synopsis: IF r[P3]>r[P1]
2053 **
2054 ** This works just like the Lt opcode except that the jump is taken if
2055 ** the content of register P3 is greater than the content of
2056 ** register P1. See the Lt opcode for additional information.
2057 */
2058 /* Opcode: Ge P1 P2 P3 P4 P5
2059 ** Synopsis: IF r[P3]>=r[P1]
2060 **
2061 ** This works just like the Lt opcode except that the jump is taken if
2062 ** the content of register P3 is greater than or equal to the content of
2063 ** register P1. See the Lt opcode for additional information.
2064 */
2065 case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
2066 case OP_Ne: /* same as TK_NE, jump, in1, in3 */
2067 case OP_Lt: /* same as TK_LT, jump, in1, in3 */
2068 case OP_Le: /* same as TK_LE, jump, in1, in3 */
2069 case OP_Gt: /* same as TK_GT, jump, in1, in3 */
2070 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
2071 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
2072 char affinity; /* Affinity to use for comparison */
2073 u16 flags1; /* Copy of initial value of pIn1->flags */
2074 u16 flags3; /* Copy of initial value of pIn3->flags */
2075
2076 pIn1 = &aMem[pOp->p1];
2077 pIn3 = &aMem[pOp->p3];
2078 flags1 = pIn1->flags;
2079 flags3 = pIn3->flags;
2080 if( (flags1 | flags3)&MEM_Null ){
2081 /* One or both operands are NULL */
2082 if( pOp->p5 & SQLITE_NULLEQ ){
2083 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
2084 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
2085 ** or not both operands are null.
2086 */
2087 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
2088 assert( (flags1 & MEM_Cleared)==0 );
2089 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
2090 if( (flags1&flags3&MEM_Null)!=0
2091 && (flags3&MEM_Cleared)==0
2092 ){
2093 res = 0; /* Operands are equal */
2094 }else{
2095 res = 1; /* Operands are not equal */
2096 }
2097 }else{
2098 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2099 ** then the result is always NULL.
2100 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2101 */
2102 if( pOp->p5 & SQLITE_STOREP2 ){
2103 pOut = &aMem[pOp->p2];
2104 iCompare = 1; /* Operands are not equal */
2105 memAboutToChange(p, pOut);
2106 MemSetTypeFlag(pOut, MEM_Null);
2107 REGISTER_TRACE(pOp->p2, pOut);
2108 }else{
2109 VdbeBranchTaken(2,3);
2110 if( pOp->p5 & SQLITE_JUMPIFNULL ){
2111 goto jump_to_p2;
2112 }
2113 }
2114 break;
2115 }
2116 }else{
2117 /* Neither operand is NULL. Do a comparison. */
2118 affinity = pOp->p5 & SQLITE_AFF_MASK;
2119 if( affinity>=SQLITE_AFF_NUMERIC ){
2120 if( (flags1 | flags3)&MEM_Str ){
2121 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
2122 applyNumericAffinity(pIn1,0);
2123 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
2124 flags3 = pIn3->flags;
2125 }
2126 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
2127 applyNumericAffinity(pIn3,0);
2128 }
2129 }
2130 /* Handle the common case of integer comparison here, as an
2131 ** optimization, to avoid a call to sqlite3MemCompare() */
2132 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
2133 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
2134 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
2135 res = 0;
2136 goto compare_op;
2137 }
2138 }else if( affinity==SQLITE_AFF_TEXT ){
2139 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
2140 testcase( pIn1->flags & MEM_Int );
2141 testcase( pIn1->flags & MEM_Real );
2142 sqlite3VdbeMemStringify(pIn1, encoding, 1);
2143 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2144 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
2145 assert( pIn1!=pIn3 );
2146 }
2147 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
2148 testcase( pIn3->flags & MEM_Int );
2149 testcase( pIn3->flags & MEM_Real );
2150 sqlite3VdbeMemStringify(pIn3, encoding, 1);
2151 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2152 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
2153 }
2154 }
2155 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
2156 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
2157 }
2158 compare_op:
2159 switch( pOp->opcode ){
2160 case OP_Eq: res2 = res==0; break;
2161 case OP_Ne: res2 = res; break;
2162 case OP_Lt: res2 = res<0; break;
2163 case OP_Le: res2 = res<=0; break;
2164 case OP_Gt: res2 = res>0; break;
2165 default: res2 = res>=0; break;
2166 }
2167
2168 /* Undo any changes made by applyAffinity() to the input registers. */
2169 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2170 pIn1->flags = flags1;
2171 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2172 pIn3->flags = flags3;
2173
2174 if( pOp->p5 & SQLITE_STOREP2 ){
2175 pOut = &aMem[pOp->p2];
2176 iCompare = res;
2177 res2 = res2!=0; /* For this path res2 must be exactly 0 or 1 */
2178 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
2179 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
2180 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2181 ** is only used in contexts where either:
2182 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2183 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2184 ** Therefore it is not necessary to check the content of r[P2] for
2185 ** NULL. */
2186 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
2187 assert( res2==0 || res2==1 );
2188 testcase( res2==0 && pOp->opcode==OP_Eq );
2189 testcase( res2==1 && pOp->opcode==OP_Eq );
2190 testcase( res2==0 && pOp->opcode==OP_Ne );
2191 testcase( res2==1 && pOp->opcode==OP_Ne );
2192 if( (pOp->opcode==OP_Eq)==res2 ) break;
2193 }
2194 memAboutToChange(p, pOut);
2195 MemSetTypeFlag(pOut, MEM_Int);
2196 pOut->u.i = res2;
2197 REGISTER_TRACE(pOp->p2, pOut);
2198 }else{
2199 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2200 if( res2 ){
2201 goto jump_to_p2;
2202 }
2203 }
2204 break;
2205 }
2206
2207 /* Opcode: ElseNotEq * P2 * * *
2208 **
2209 ** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
2210 ** If result of an OP_Eq comparison on the same two operands
2211 ** would have be NULL or false (0), then then jump to P2.
2212 ** If the result of an OP_Eq comparison on the two previous operands
2213 ** would have been true (1), then fall through.
2214 */
2215 case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
2216 assert( pOp>aOp );
2217 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
2218 assert( pOp[-1].p5 & SQLITE_STOREP2 );
2219 VdbeBranchTaken(iCompare!=0, 2);
2220 if( iCompare!=0 ) goto jump_to_p2;
2221 break;
2222 }
2223
2224
2225 /* Opcode: Permutation * * * P4 *
2226 **
2227 ** Set the permutation used by the OP_Compare operator in the next
2228 ** instruction. The permutation is stored in the P4 operand.
2229 **
2230 ** The permutation is only valid until the next OP_Compare that has
2231 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2232 ** occur immediately prior to the OP_Compare.
2233 **
2234 ** The first integer in the P4 integer array is the length of the array
2235 ** and does not become part of the permutation.
2236 */
2237 case OP_Permutation: {
2238 assert( pOp->p4type==P4_INTARRAY );
2239 assert( pOp->p4.ai );
2240 assert( pOp[1].opcode==OP_Compare );
2241 assert( pOp[1].p5 & OPFLAG_PERMUTE );
2242 break;
2243 }
2244
2245 /* Opcode: Compare P1 P2 P3 P4 P5
2246 ** Synopsis: r[P1@P3] <-> r[P2@P3]
2247 **
2248 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2249 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
2250 ** the comparison for use by the next OP_Jump instruct.
2251 **
2252 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2253 ** determined by the most recent OP_Permutation operator. If the
2254 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2255 ** order.
2256 **
2257 ** P4 is a KeyInfo structure that defines collating sequences and sort
2258 ** orders for the comparison. The permutation applies to registers
2259 ** only. The KeyInfo elements are used sequentially.
2260 **
2261 ** The comparison is a sort comparison, so NULLs compare equal,
2262 ** NULLs are less than numbers, numbers are less than strings,
2263 ** and strings are less than blobs.
2264 */
2265 case OP_Compare: {
2266 int n;
2267 int i;
2268 int p1;
2269 int p2;
2270 const KeyInfo *pKeyInfo;
2271 int idx;
2272 CollSeq *pColl; /* Collating sequence to use on this term */
2273 int bRev; /* True for DESCENDING sort order */
2274 int *aPermute; /* The permutation */
2275
2276 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2277 aPermute = 0;
2278 }else{
2279 assert( pOp>aOp );
2280 assert( pOp[-1].opcode==OP_Permutation );
2281 assert( pOp[-1].p4type==P4_INTARRAY );
2282 aPermute = pOp[-1].p4.ai + 1;
2283 assert( aPermute!=0 );
2284 }
2285 n = pOp->p3;
2286 pKeyInfo = pOp->p4.pKeyInfo;
2287 assert( n>0 );
2288 assert( pKeyInfo!=0 );
2289 p1 = pOp->p1;
2290 p2 = pOp->p2;
2291 #if SQLITE_DEBUG
2292 if( aPermute ){
2293 int k, mx = 0;
2294 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
2295 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2296 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
2297 }else{
2298 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2299 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
2300 }
2301 #endif /* SQLITE_DEBUG */
2302 for(i=0; i<n; i++){
2303 idx = aPermute ? aPermute[i] : i;
2304 assert( memIsValid(&aMem[p1+idx]) );
2305 assert( memIsValid(&aMem[p2+idx]) );
2306 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2307 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
2308 assert( i<pKeyInfo->nField );
2309 pColl = pKeyInfo->aColl[i];
2310 bRev = pKeyInfo->aSortOrder[i];
2311 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
2312 if( iCompare ){
2313 if( bRev ) iCompare = -iCompare;
2314 break;
2315 }
2316 }
2317 break;
2318 }
2319
2320 /* Opcode: Jump P1 P2 P3 * *
2321 **
2322 ** Jump to the instruction at address P1, P2, or P3 depending on whether
2323 ** in the most recent OP_Compare instruction the P1 vector was less than
2324 ** equal to, or greater than the P2 vector, respectively.
2325 */
2326 case OP_Jump: { /* jump */
2327 if( iCompare<0 ){
2328 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
2329 }else if( iCompare==0 ){
2330 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
2331 }else{
2332 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
2333 }
2334 break;
2335 }
2336
2337 /* Opcode: And P1 P2 P3 * *
2338 ** Synopsis: r[P3]=(r[P1] && r[P2])
2339 **
2340 ** Take the logical AND of the values in registers P1 and P2 and
2341 ** write the result into register P3.
2342 **
2343 ** If either P1 or P2 is 0 (false) then the result is 0 even if
2344 ** the other input is NULL. A NULL and true or two NULLs give
2345 ** a NULL output.
2346 */
2347 /* Opcode: Or P1 P2 P3 * *
2348 ** Synopsis: r[P3]=(r[P1] || r[P2])
2349 **
2350 ** Take the logical OR of the values in register P1 and P2 and
2351 ** store the answer in register P3.
2352 **
2353 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2354 ** even if the other input is NULL. A NULL and false or two NULLs
2355 ** give a NULL output.
2356 */
2357 case OP_And: /* same as TK_AND, in1, in2, out3 */
2358 case OP_Or: { /* same as TK_OR, in1, in2, out3 */
2359 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2360 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2361
2362 pIn1 = &aMem[pOp->p1];
2363 if( pIn1->flags & MEM_Null ){
2364 v1 = 2;
2365 }else{
2366 v1 = sqlite3VdbeIntValue(pIn1)!=0;
2367 }
2368 pIn2 = &aMem[pOp->p2];
2369 if( pIn2->flags & MEM_Null ){
2370 v2 = 2;
2371 }else{
2372 v2 = sqlite3VdbeIntValue(pIn2)!=0;
2373 }
2374 if( pOp->opcode==OP_And ){
2375 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
2376 v1 = and_logic[v1*3+v2];
2377 }else{
2378 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
2379 v1 = or_logic[v1*3+v2];
2380 }
2381 pOut = &aMem[pOp->p3];
2382 if( v1==2 ){
2383 MemSetTypeFlag(pOut, MEM_Null);
2384 }else{
2385 pOut->u.i = v1;
2386 MemSetTypeFlag(pOut, MEM_Int);
2387 }
2388 break;
2389 }
2390
2391 /* Opcode: Not P1 P2 * * *
2392 ** Synopsis: r[P2]= !r[P1]
2393 **
2394 ** Interpret the value in register P1 as a boolean value. Store the
2395 ** boolean complement in register P2. If the value in register P1 is
2396 ** NULL, then a NULL is stored in P2.
2397 */
2398 case OP_Not: { /* same as TK_NOT, in1, out2 */
2399 pIn1 = &aMem[pOp->p1];
2400 pOut = &aMem[pOp->p2];
2401 sqlite3VdbeMemSetNull(pOut);
2402 if( (pIn1->flags & MEM_Null)==0 ){
2403 pOut->flags = MEM_Int;
2404 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
2405 }
2406 break;
2407 }
2408
2409 /* Opcode: BitNot P1 P2 * * *
2410 ** Synopsis: r[P1]= ~r[P1]
2411 **
2412 ** Interpret the content of register P1 as an integer. Store the
2413 ** ones-complement of the P1 value into register P2. If P1 holds
2414 ** a NULL then store a NULL in P2.
2415 */
2416 case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
2417 pIn1 = &aMem[pOp->p1];
2418 pOut = &aMem[pOp->p2];
2419 sqlite3VdbeMemSetNull(pOut);
2420 if( (pIn1->flags & MEM_Null)==0 ){
2421 pOut->flags = MEM_Int;
2422 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
2423 }
2424 break;
2425 }
2426
2427 /* Opcode: Once P1 P2 * * *
2428 **
2429 ** If the P1 value is equal to the P1 value on the OP_Init opcode at
2430 ** instruction 0, then jump to P2. If the two P1 values differ, then
2431 ** set the P1 value on this opcode to equal the P1 value on the OP_Init
2432 ** and fall through.
2433 */
2434 case OP_Once: { /* jump */
2435 assert( p->aOp[0].opcode==OP_Init );
2436 VdbeBranchTaken(p->aOp[0].p1==pOp->p1, 2);
2437 if( p->aOp[0].p1==pOp->p1 ){
2438 goto jump_to_p2;
2439 }else{
2440 pOp->p1 = p->aOp[0].p1;
2441 }
2442 break;
2443 }
2444
2445 /* Opcode: If P1 P2 P3 * *
2446 **
2447 ** Jump to P2 if the value in register P1 is true. The value
2448 ** is considered true if it is numeric and non-zero. If the value
2449 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
2450 */
2451 /* Opcode: IfNot P1 P2 P3 * *
2452 **
2453 ** Jump to P2 if the value in register P1 is False. The value
2454 ** is considered false if it has a numeric value of zero. If the value
2455 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
2456 */
2457 case OP_If: /* jump, in1 */
2458 case OP_IfNot: { /* jump, in1 */
2459 int c;
2460 pIn1 = &aMem[pOp->p1];
2461 if( pIn1->flags & MEM_Null ){
2462 c = pOp->p3;
2463 }else{
2464 #ifdef SQLITE_OMIT_FLOATING_POINT
2465 c = sqlite3VdbeIntValue(pIn1)!=0;
2466 #else
2467 c = sqlite3VdbeRealValue(pIn1)!=0.0;
2468 #endif
2469 if( pOp->opcode==OP_IfNot ) c = !c;
2470 }
2471 VdbeBranchTaken(c!=0, 2);
2472 if( c ){
2473 goto jump_to_p2;
2474 }
2475 break;
2476 }
2477
2478 /* Opcode: IsNull P1 P2 * * *
2479 ** Synopsis: if r[P1]==NULL goto P2
2480 **
2481 ** Jump to P2 if the value in register P1 is NULL.
2482 */
2483 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
2484 pIn1 = &aMem[pOp->p1];
2485 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
2486 if( (pIn1->flags & MEM_Null)!=0 ){
2487 goto jump_to_p2;
2488 }
2489 break;
2490 }
2491
2492 /* Opcode: NotNull P1 P2 * * *
2493 ** Synopsis: if r[P1]!=NULL goto P2
2494 **
2495 ** Jump to P2 if the value in register P1 is not NULL.
2496 */
2497 case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
2498 pIn1 = &aMem[pOp->p1];
2499 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
2500 if( (pIn1->flags & MEM_Null)==0 ){
2501 goto jump_to_p2;
2502 }
2503 break;
2504 }
2505
2506 /* Opcode: Column P1 P2 P3 P4 P5
2507 ** Synopsis: r[P3]=PX
2508 **
2509 ** Interpret the data that cursor P1 points to as a structure built using
2510 ** the MakeRecord instruction. (See the MakeRecord opcode for additional
2511 ** information about the format of the data.) Extract the P2-th column
2512 ** from this record. If there are less that (P2+1)
2513 ** values in the record, extract a NULL.
2514 **
2515 ** The value extracted is stored in register P3.
2516 **
2517 ** If the column contains fewer than P2 fields, then extract a NULL. Or,
2518 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
2519 ** the result.
2520 **
2521 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2522 ** then the cache of the cursor is reset prior to extracting the column.
2523 ** The first OP_Column against a pseudo-table after the value of the content
2524 ** register has changed should have this bit set.
2525 **
2526 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2527 ** the result is guaranteed to only be used as the argument of a length()
2528 ** or typeof() function, respectively. The loading of large blobs can be
2529 ** skipped for length() and all content loading can be skipped for typeof().
2530 */
2531 case OP_Column: {
2532 int p2; /* column number to retrieve */
2533 VdbeCursor *pC; /* The VDBE cursor */
2534 BtCursor *pCrsr; /* The BTree cursor */
2535 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
2536 int len; /* The length of the serialized data for the column */
2537 int i; /* Loop counter */
2538 Mem *pDest; /* Where to write the extracted value */
2539 Mem sMem; /* For storing the record being decoded */
2540 const u8 *zData; /* Part of the record being decoded */
2541 const u8 *zHdr; /* Next unparsed byte of the header */
2542 const u8 *zEndHdr; /* Pointer to first byte after the header */
2543 u32 offset; /* Offset into the data */
2544 u64 offset64; /* 64-bit offset */
2545 u32 avail; /* Number of bytes of available data */
2546 u32 t; /* A type code from the record header */
2547 Mem *pReg; /* PseudoTable input register */
2548
2549 pC = p->apCsr[pOp->p1];
2550 p2 = pOp->p2;
2551
2552 /* If the cursor cache is stale, bring it up-to-date */
2553 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
2554 if( rc ) goto abort_due_to_error;
2555
2556 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
2557 pDest = &aMem[pOp->p3];
2558 memAboutToChange(p, pDest);
2559 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2560 assert( pC!=0 );
2561 assert( p2<pC->nField );
2562 aOffset = pC->aOffset;
2563 assert( pC->eCurType!=CURTYPE_VTAB );
2564 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2565 assert( pC->eCurType!=CURTYPE_SORTER );
2566
2567 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
2568 if( pC->nullRow ){
2569 if( pC->eCurType==CURTYPE_PSEUDO ){
2570 assert( pC->uc.pseudoTableReg>0 );
2571 pReg = &aMem[pC->uc.pseudoTableReg];
2572 assert( pReg->flags & MEM_Blob );
2573 assert( memIsValid(pReg) );
2574 pC->payloadSize = pC->szRow = avail = pReg->n;
2575 pC->aRow = (u8*)pReg->z;
2576 }else{
2577 sqlite3VdbeMemSetNull(pDest);
2578 goto op_column_out;
2579 }
2580 }else{
2581 pCrsr = pC->uc.pCursor;
2582 assert( pC->eCurType==CURTYPE_BTREE );
2583 assert( pCrsr );
2584 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2585 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
2586 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &avail);
2587 assert( avail<=65536 ); /* Maximum page size is 64KiB */
2588 if( pC->payloadSize <= (u32)avail ){
2589 pC->szRow = pC->payloadSize;
2590 }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
2591 goto too_big;
2592 }else{
2593 pC->szRow = avail;
2594 }
2595 }
2596 pC->cacheStatus = p->cacheCtr;
2597 pC->iHdrOffset = getVarint32(pC->aRow, offset);
2598 pC->nHdrParsed = 0;
2599 aOffset[0] = offset;
2600
2601
2602 if( avail<offset ){ /*OPTIMIZATION-IF-FALSE*/
2603 /* pC->aRow does not have to hold the entire row, but it does at least
2604 ** need to cover the header of the record. If pC->aRow does not contain
2605 ** the complete header, then set it to zero, forcing the header to be
2606 ** dynamically allocated. */
2607 pC->aRow = 0;
2608 pC->szRow = 0;
2609
2610 /* Make sure a corrupt database has not given us an oversize header.
2611 ** Do this now to avoid an oversize memory allocation.
2612 **
2613 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2614 ** types use so much data space that there can only be 4096 and 32 of
2615 ** them, respectively. So the maximum header length results from a
2616 ** 3-byte type for each of the maximum of 32768 columns plus three
2617 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2618 */
2619 if( offset > 98307 || offset > pC->payloadSize ){
2620 rc = SQLITE_CORRUPT_BKPT;
2621 goto abort_due_to_error;
2622 }
2623 }else if( offset>0 ){ /*OPTIMIZATION-IF-TRUE*/
2624 /* The following goto is an optimization. It can be omitted and
2625 ** everything will still work. But OP_Column is measurably faster
2626 ** by skipping the subsequent conditional, which is always true.
2627 */
2628 zData = pC->aRow;
2629 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
2630 goto op_column_read_header;
2631 }
2632 }
2633
2634 /* Make sure at least the first p2+1 entries of the header have been
2635 ** parsed and valid information is in aOffset[] and pC->aType[].
2636 */
2637 if( pC->nHdrParsed<=p2 ){
2638 /* If there is more header available for parsing in the record, try
2639 ** to extract additional fields up through the p2+1-th field
2640 */
2641 if( pC->iHdrOffset<aOffset[0] ){
2642 /* Make sure zData points to enough of the record to cover the header. */
2643 if( pC->aRow==0 ){
2644 memset(&sMem, 0, sizeof(sMem));
2645 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
2646 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2647 zData = (u8*)sMem.z;
2648 }else{
2649 zData = pC->aRow;
2650 }
2651
2652 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
2653 op_column_read_header:
2654 i = pC->nHdrParsed;
2655 offset64 = aOffset[i];
2656 zHdr = zData + pC->iHdrOffset;
2657 zEndHdr = zData + aOffset[0];
2658 do{
2659 if( (t = zHdr[0])<0x80 ){
2660 zHdr++;
2661 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
2662 }else{
2663 zHdr += sqlite3GetVarint32(zHdr, &t);
2664 offset64 += sqlite3VdbeSerialTypeLen(t);
2665 }
2666 pC->aType[i++] = t;
2667 aOffset[i] = (u32)(offset64 & 0xffffffff);
2668 }while( i<=p2 && zHdr<zEndHdr );
2669
2670 /* The record is corrupt if any of the following are true:
2671 ** (1) the bytes of the header extend past the declared header size
2672 ** (2) the entire header was used but not all data was used
2673 ** (3) the end of the data extends beyond the end of the record.
2674 */
2675 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2676 || (offset64 > pC->payloadSize)
2677 ){
2678 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
2679 rc = SQLITE_CORRUPT_BKPT;
2680 goto abort_due_to_error;
2681 }
2682
2683 pC->nHdrParsed = i;
2684 pC->iHdrOffset = (u32)(zHdr - zData);
2685 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
2686 }else{
2687 t = 0;
2688 }
2689
2690 /* If after trying to extract new entries from the header, nHdrParsed is
2691 ** still not up to p2, that means that the record has fewer than p2
2692 ** columns. So the result will be either the default value or a NULL.
2693 */
2694 if( pC->nHdrParsed<=p2 ){
2695 if( pOp->p4type==P4_MEM ){
2696 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2697 }else{
2698 sqlite3VdbeMemSetNull(pDest);
2699 }
2700 goto op_column_out;
2701 }
2702 }else{
2703 t = pC->aType[p2];
2704 }
2705
2706 /* Extract the content for the p2+1-th column. Control can only
2707 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
2708 ** all valid.
2709 */
2710 assert( p2<pC->nHdrParsed );
2711 assert( rc==SQLITE_OK );
2712 assert( sqlite3VdbeCheckMemInvariants(pDest) );
2713 if( VdbeMemDynamic(pDest) ){
2714 sqlite3VdbeMemSetNull(pDest);
2715 }
2716 assert( t==pC->aType[p2] );
2717 if( pC->szRow>=aOffset[p2+1] ){
2718 /* This is the common case where the desired content fits on the original
2719 ** page - where the content is not on an overflow page */
2720 zData = pC->aRow + aOffset[p2];
2721 if( t<12 ){
2722 sqlite3VdbeSerialGet(zData, t, pDest);
2723 }else{
2724 /* If the column value is a string, we need a persistent value, not
2725 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2726 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2727 */
2728 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2729 pDest->n = len = (t-12)/2;
2730 pDest->enc = encoding;
2731 if( pDest->szMalloc < len+2 ){
2732 pDest->flags = MEM_Null;
2733 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2734 }else{
2735 pDest->z = pDest->zMalloc;
2736 }
2737 memcpy(pDest->z, zData, len);
2738 pDest->z[len] = 0;
2739 pDest->z[len+1] = 0;
2740 pDest->flags = aFlag[t&1];
2741 }
2742 }else{
2743 pDest->enc = encoding;
2744 /* This branch happens only when content is on overflow pages */
2745 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2746 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2747 || (len = sqlite3VdbeSerialTypeLen(t))==0
2748 ){
2749 /* Content is irrelevant for
2750 ** 1. the typeof() function,
2751 ** 2. the length(X) function if X is a blob, and
2752 ** 3. if the content length is zero.
2753 ** So we might as well use bogus content rather than reading
2754 ** content from disk. */
2755 static u8 aZero[8]; /* This is the bogus content */
2756 sqlite3VdbeSerialGet(aZero, t, pDest);
2757 }else{
2758 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
2759 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2760 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2761 pDest->flags &= ~MEM_Ephem;
2762 }
2763 }
2764
2765 op_column_out:
2766 UPDATE_MAX_BLOBSIZE(pDest);
2767 REGISTER_TRACE(pOp->p3, pDest);
2768 break;
2769 }
2770
2771 /* Opcode: Affinity P1 P2 * P4 *
2772 ** Synopsis: affinity(r[P1@P2])
2773 **
2774 ** Apply affinities to a range of P2 registers starting with P1.
2775 **
2776 ** P4 is a string that is P2 characters long. The nth character of the
2777 ** string indicates the column affinity that should be used for the nth
2778 ** memory cell in the range.
2779 */
2780 case OP_Affinity: {
2781 const char *zAffinity; /* The affinity to be applied */
2782 char cAff; /* A single character of affinity */
2783
2784 zAffinity = pOp->p4.z;
2785 assert( zAffinity!=0 );
2786 assert( zAffinity[pOp->p2]==0 );
2787 pIn1 = &aMem[pOp->p1];
2788 while( (cAff = *(zAffinity++))!=0 ){
2789 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
2790 assert( memIsValid(pIn1) );
2791 applyAffinity(pIn1, cAff, encoding);
2792 pIn1++;
2793 }
2794 break;
2795 }
2796
2797 /* Opcode: MakeRecord P1 P2 P3 P4 *
2798 ** Synopsis: r[P3]=mkrec(r[P1@P2])
2799 **
2800 ** Convert P2 registers beginning with P1 into the [record format]
2801 ** use as a data record in a database table or as a key
2802 ** in an index. The OP_Column opcode can decode the record later.
2803 **
2804 ** P4 may be a string that is P2 characters long. The nth character of the
2805 ** string indicates the column affinity that should be used for the nth
2806 ** field of the index key.
2807 **
2808 ** The mapping from character to affinity is given by the SQLITE_AFF_
2809 ** macros defined in sqliteInt.h.
2810 **
2811 ** If P4 is NULL then all index fields have the affinity BLOB.
2812 */
2813 case OP_MakeRecord: {
2814 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2815 Mem *pRec; /* The new record */
2816 u64 nData; /* Number of bytes of data space */
2817 int nHdr; /* Number of bytes of header space */
2818 i64 nByte; /* Data space required for this record */
2819 i64 nZero; /* Number of zero bytes at the end of the record */
2820 int nVarint; /* Number of bytes in a varint */
2821 u32 serial_type; /* Type field */
2822 Mem *pData0; /* First field to be combined into the record */
2823 Mem *pLast; /* Last field of the record */
2824 int nField; /* Number of fields in the record */
2825 char *zAffinity; /* The affinity string for the record */
2826 int file_format; /* File format to use for encoding */
2827 int i; /* Space used in zNewRecord[] header */
2828 int j; /* Space used in zNewRecord[] content */
2829 u32 len; /* Length of a field */
2830
2831 /* Assuming the record contains N fields, the record format looks
2832 ** like this:
2833 **
2834 ** ------------------------------------------------------------------------
2835 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2836 ** ------------------------------------------------------------------------
2837 **
2838 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2839 ** and so forth.
2840 **
2841 ** Each type field is a varint representing the serial type of the
2842 ** corresponding data element (see sqlite3VdbeSerialType()). The
2843 ** hdr-size field is also a varint which is the offset from the beginning
2844 ** of the record to data0.
2845 */
2846 nData = 0; /* Number of bytes of data space */
2847 nHdr = 0; /* Number of bytes of header space */
2848 nZero = 0; /* Number of zero bytes at the end of the record */
2849 nField = pOp->p1;
2850 zAffinity = pOp->p4.z;
2851 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
2852 pData0 = &aMem[nField];
2853 nField = pOp->p2;
2854 pLast = &pData0[nField-1];
2855 file_format = p->minWriteFileFormat;
2856
2857 /* Identify the output register */
2858 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2859 pOut = &aMem[pOp->p3];
2860 memAboutToChange(p, pOut);
2861
2862 /* Apply the requested affinity to all inputs
2863 */
2864 assert( pData0<=pLast );
2865 if( zAffinity ){
2866 pRec = pData0;
2867 do{
2868 applyAffinity(pRec++, *(zAffinity++), encoding);
2869 assert( zAffinity[0]==0 || pRec<=pLast );
2870 }while( zAffinity[0] );
2871 }
2872
2873 #ifdef SQLITE_ENABLE_NULL_TRIM
2874 /* NULLs can be safely trimmed from the end of the record, as long as
2875 ** as the schema format is 2 or more and none of the omitted columns
2876 ** have a non-NULL default value. Also, the record must be left with
2877 ** at least one field. If P5>0 then it will be one more than the
2878 ** index of the right-most column with a non-NULL default value */
2879 if( pOp->p5 ){
2880 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2881 pLast--;
2882 nField--;
2883 }
2884 }
2885 #endif
2886
2887 /* Loop through the elements that will make up the record to figure
2888 ** out how much space is required for the new record.
2889 */
2890 pRec = pLast;
2891 do{
2892 assert( memIsValid(pRec) );
2893 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
2894 if( pRec->flags & MEM_Zero ){
2895 if( nData ){
2896 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
2897 }else{
2898 nZero += pRec->u.nZero;
2899 len -= pRec->u.nZero;
2900 }
2901 }
2902 nData += len;
2903 testcase( serial_type==127 );
2904 testcase( serial_type==128 );
2905 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
2906 if( pRec==pData0 ) break;
2907 pRec--;
2908 }while(1);
2909
2910 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2911 ** which determines the total number of bytes in the header. The varint
2912 ** value is the size of the header in bytes including the size varint
2913 ** itself. */
2914 testcase( nHdr==126 );
2915 testcase( nHdr==127 );
2916 if( nHdr<=126 ){
2917 /* The common case */
2918 nHdr += 1;
2919 }else{
2920 /* Rare case of a really large header */
2921 nVarint = sqlite3VarintLen(nHdr);
2922 nHdr += nVarint;
2923 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
2924 }
2925 nByte = nHdr+nData;
2926 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
2927 goto too_big;
2928 }
2929
2930 /* Make sure the output register has a buffer large enough to store
2931 ** the new record. The output register (pOp->p3) is not allowed to
2932 ** be one of the input registers (because the following call to
2933 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
2934 */
2935 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
2936 goto no_mem;
2937 }
2938 zNewRecord = (u8 *)pOut->z;
2939
2940 /* Write the record */
2941 i = putVarint32(zNewRecord, nHdr);
2942 j = nHdr;
2943 assert( pData0<=pLast );
2944 pRec = pData0;
2945 do{
2946 serial_type = pRec->uTemp;
2947 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2948 ** additional varints, one per column. */
2949 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
2950 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2951 ** immediately follow the header. */
2952 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
2953 }while( (++pRec)<=pLast );
2954 assert( i==nHdr );
2955 assert( j==nByte );
2956
2957 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
2958 pOut->n = (int)nByte;
2959 pOut->flags = MEM_Blob;
2960 if( nZero ){
2961 pOut->u.nZero = nZero;
2962 pOut->flags |= MEM_Zero;
2963 }
2964 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
2965 REGISTER_TRACE(pOp->p3, pOut);
2966 UPDATE_MAX_BLOBSIZE(pOut);
2967 break;
2968 }
2969
2970 /* Opcode: Count P1 P2 * * *
2971 ** Synopsis: r[P2]=count()
2972 **
2973 ** Store the number of entries (an integer value) in the table or index
2974 ** opened by cursor P1 in register P2
2975 */
2976 #ifndef SQLITE_OMIT_BTREECOUNT
2977 case OP_Count: { /* out2 */
2978 i64 nEntry;
2979 BtCursor *pCrsr;
2980
2981 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
2982 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
2983 assert( pCrsr );
2984 nEntry = 0; /* Not needed. Only used to silence a warning. */
2985 rc = sqlite3BtreeCount(pCrsr, &nEntry);
2986 if( rc ) goto abort_due_to_error;
2987 pOut = out2Prerelease(p, pOp);
2988 pOut->u.i = nEntry;
2989 break;
2990 }
2991 #endif
2992
2993 /* Opcode: Savepoint P1 * * P4 *
2994 **
2995 ** Open, release or rollback the savepoint named by parameter P4, depending
2996 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2997 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2998 */
2999 case OP_Savepoint: {
3000 int p1; /* Value of P1 operand */
3001 char *zName; /* Name of savepoint */
3002 int nName;
3003 Savepoint *pNew;
3004 Savepoint *pSavepoint;
3005 Savepoint *pTmp;
3006 int iSavepoint;
3007 int ii;
3008
3009 p1 = pOp->p1;
3010 zName = pOp->p4.z;
3011
3012 /* Assert that the p1 parameter is valid. Also that if there is no open
3013 ** transaction, then there cannot be any savepoints.
3014 */
3015 assert( db->pSavepoint==0 || db->autoCommit==0 );
3016 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
3017 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
3018 assert( checkSavepointCount(db) );
3019 assert( p->bIsReader );
3020
3021 if( p1==SAVEPOINT_BEGIN ){
3022 if( db->nVdbeWrite>0 ){
3023 /* A new savepoint cannot be created if there are active write
3024 ** statements (i.e. open read/write incremental blob handles).
3025 */
3026 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
3027 rc = SQLITE_BUSY;
3028 }else{
3029 nName = sqlite3Strlen30(zName);
3030
3031 #ifndef SQLITE_OMIT_VIRTUALTABLE
3032 /* This call is Ok even if this savepoint is actually a transaction
3033 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3034 ** If this is a transaction savepoint being opened, it is guaranteed
3035 ** that the db->aVTrans[] array is empty. */
3036 assert( db->autoCommit==0 || db->nVTrans==0 );
3037 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3038 db->nStatement+db->nSavepoint);
3039 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3040 #endif
3041
3042 /* Create a new savepoint structure. */
3043 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
3044 if( pNew ){
3045 pNew->zName = (char *)&pNew[1];
3046 memcpy(pNew->zName, zName, nName+1);
3047
3048 /* If there is no open transaction, then mark this as a special
3049 ** "transaction savepoint". */
3050 if( db->autoCommit ){
3051 db->autoCommit = 0;
3052 db->isTransactionSavepoint = 1;
3053 }else{
3054 db->nSavepoint++;
3055 }
3056
3057 /* Link the new savepoint into the database handle's list. */
3058 pNew->pNext = db->pSavepoint;
3059 db->pSavepoint = pNew;
3060 pNew->nDeferredCons = db->nDeferredCons;
3061 pNew->nDeferredImmCons = db->nDeferredImmCons;
3062 }
3063 }
3064 }else{
3065 iSavepoint = 0;
3066
3067 /* Find the named savepoint. If there is no such savepoint, then an
3068 ** an error is returned to the user. */
3069 for(
3070 pSavepoint = db->pSavepoint;
3071 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
3072 pSavepoint = pSavepoint->pNext
3073 ){
3074 iSavepoint++;
3075 }
3076 if( !pSavepoint ){
3077 sqlite3VdbeError(p, "no such savepoint: %s", zName);
3078 rc = SQLITE_ERROR;
3079 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
3080 /* It is not possible to release (commit) a savepoint if there are
3081 ** active write statements.
3082 */
3083 sqlite3VdbeError(p, "cannot release savepoint - "
3084 "SQL statements in progress");
3085 rc = SQLITE_BUSY;
3086 }else{
3087
3088 /* Determine whether or not this is a transaction savepoint. If so,
3089 ** and this is a RELEASE command, then the current transaction
3090 ** is committed.
3091 */
3092 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3093 if( isTransaction && p1==SAVEPOINT_RELEASE ){
3094 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
3095 goto vdbe_return;
3096 }
3097 db->autoCommit = 1;
3098 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3099 p->pc = (int)(pOp - aOp);
3100 db->autoCommit = 0;
3101 p->rc = rc = SQLITE_BUSY;
3102 goto vdbe_return;
3103 }
3104 db->isTransactionSavepoint = 0;
3105 rc = p->rc;
3106 }else{
3107 int isSchemaChange;
3108 iSavepoint = db->nSavepoint - iSavepoint - 1;
3109 if( p1==SAVEPOINT_ROLLBACK ){
3110 isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
3111 for(ii=0; ii<db->nDb; ii++){
3112 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3113 SQLITE_ABORT_ROLLBACK,
3114 isSchemaChange==0);
3115 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3116 }
3117 }else{
3118 isSchemaChange = 0;
3119 }
3120 for(ii=0; ii<db->nDb; ii++){
3121 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3122 if( rc!=SQLITE_OK ){
3123 goto abort_due_to_error;
3124 }
3125 }
3126 if( isSchemaChange ){
3127 sqlite3ExpirePreparedStatements(db);
3128 sqlite3ResetAllSchemasOfConnection(db);
3129 db->flags = (db->flags | SQLITE_InternChanges);
3130 }
3131 }
3132
3133 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3134 ** savepoints nested inside of the savepoint being operated on. */
3135 while( db->pSavepoint!=pSavepoint ){
3136 pTmp = db->pSavepoint;
3137 db->pSavepoint = pTmp->pNext;
3138 sqlite3DbFree(db, pTmp);
3139 db->nSavepoint--;
3140 }
3141
3142 /* If it is a RELEASE, then destroy the savepoint being operated on
3143 ** too. If it is a ROLLBACK TO, then set the number of deferred
3144 ** constraint violations present in the database to the value stored
3145 ** when the savepoint was created. */
3146 if( p1==SAVEPOINT_RELEASE ){
3147 assert( pSavepoint==db->pSavepoint );
3148 db->pSavepoint = pSavepoint->pNext;
3149 sqlite3DbFree(db, pSavepoint);
3150 if( !isTransaction ){
3151 db->nSavepoint--;
3152 }
3153 }else{
3154 db->nDeferredCons = pSavepoint->nDeferredCons;
3155 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
3156 }
3157
3158 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
3159 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3160 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3161 }
3162 }
3163 }
3164 if( rc ) goto abort_due_to_error;
3165
3166 break;
3167 }
3168
3169 /* Opcode: AutoCommit P1 P2 * * *
3170 **
3171 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
3172 ** back any currently active btree transactions. If there are any active
3173 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3174 ** there are active writing VMs or active VMs that use shared cache.
3175 **
3176 ** This instruction causes the VM to halt.
3177 */
3178 case OP_AutoCommit: {
3179 int desiredAutoCommit;
3180 int iRollback;
3181
3182 desiredAutoCommit = pOp->p1;
3183 iRollback = pOp->p2;
3184 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
3185 assert( desiredAutoCommit==1 || iRollback==0 );
3186 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
3187 assert( p->bIsReader );
3188
3189 if( desiredAutoCommit!=db->autoCommit ){
3190 if( iRollback ){
3191 assert( desiredAutoCommit==1 );
3192 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
3193 db->autoCommit = 1;
3194 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3195 /* If this instruction implements a COMMIT and other VMs are writing
3196 ** return an error indicating that the other VMs must complete first.
3197 */
3198 sqlite3VdbeError(p, "cannot commit transaction - "
3199 "SQL statements in progress");
3200 rc = SQLITE_BUSY;
3201 goto abort_due_to_error;
3202 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
3203 goto vdbe_return;
3204 }else{
3205 db->autoCommit = (u8)desiredAutoCommit;
3206 }
3207 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3208 p->pc = (int)(pOp - aOp);
3209 db->autoCommit = (u8)(1-desiredAutoCommit);
3210 p->rc = rc = SQLITE_BUSY;
3211 goto vdbe_return;
3212 }
3213 assert( db->nStatement==0 );
3214 sqlite3CloseSavepoints(db);
3215 if( p->rc==SQLITE_OK ){
3216 rc = SQLITE_DONE;
3217 }else{
3218 rc = SQLITE_ERROR;
3219 }
3220 goto vdbe_return;
3221 }else{
3222 sqlite3VdbeError(p,
3223 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
3224 (iRollback)?"cannot rollback - no transaction is active":
3225 "cannot commit - no transaction is active"));
3226
3227 rc = SQLITE_ERROR;
3228 goto abort_due_to_error;
3229 }
3230 break;
3231 }
3232
3233 /* Opcode: Transaction P1 P2 P3 P4 P5
3234 **
3235 ** Begin a transaction on database P1 if a transaction is not already
3236 ** active.
3237 ** If P2 is non-zero, then a write-transaction is started, or if a
3238 ** read-transaction is already active, it is upgraded to a write-transaction.
3239 ** If P2 is zero, then a read-transaction is started.
3240 **
3241 ** P1 is the index of the database file on which the transaction is
3242 ** started. Index 0 is the main database file and index 1 is the
3243 ** file used for temporary tables. Indices of 2 or more are used for
3244 ** attached databases.
3245 **
3246 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3247 ** true (this flag is set if the Vdbe may modify more than one row and may
3248 ** throw an ABORT exception), a statement transaction may also be opened.
3249 ** More specifically, a statement transaction is opened iff the database
3250 ** connection is currently not in autocommit mode, or if there are other
3251 ** active statements. A statement transaction allows the changes made by this
3252 ** VDBE to be rolled back after an error without having to roll back the
3253 ** entire transaction. If no error is encountered, the statement transaction
3254 ** will automatically commit when the VDBE halts.
3255 **
3256 ** If P5!=0 then this opcode also checks the schema cookie against P3
3257 ** and the schema generation counter against P4.
3258 ** The cookie changes its value whenever the database schema changes.
3259 ** This operation is used to detect when that the cookie has changed
3260 ** and that the current process needs to reread the schema. If the schema
3261 ** cookie in P3 differs from the schema cookie in the database header or
3262 ** if the schema generation counter in P4 differs from the current
3263 ** generation counter, then an SQLITE_SCHEMA error is raised and execution
3264 ** halts. The sqlite3_step() wrapper function might then reprepare the
3265 ** statement and rerun it from the beginning.
3266 */
3267 case OP_Transaction: {
3268 Btree *pBt;
3269 int iMeta;
3270 int iGen;
3271
3272 assert( p->bIsReader );
3273 assert( p->readOnly==0 || pOp->p2==0 );
3274 assert( pOp->p1>=0 && pOp->p1<db->nDb );
3275 assert( DbMaskTest(p->btreeMask, pOp->p1) );
3276 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3277 rc = SQLITE_READONLY;
3278 goto abort_due_to_error;
3279 }
3280 pBt = db->aDb[pOp->p1].pBt;
3281
3282 if( pBt ){
3283 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
3284 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3285 testcase( rc==SQLITE_BUSY_RECOVERY );
3286 if( rc!=SQLITE_OK ){
3287 if( (rc&0xff)==SQLITE_BUSY ){
3288 p->pc = (int)(pOp - aOp);
3289 p->rc = rc;
3290 goto vdbe_return;
3291 }
3292 goto abort_due_to_error;
3293 }
3294
3295 if( pOp->p2 && p->usesStmtJournal
3296 && (db->autoCommit==0 || db->nVdbeRead>1)
3297 ){
3298 assert( sqlite3BtreeIsInTrans(pBt) );
3299 if( p->iStatement==0 ){
3300 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3301 db->nStatement++;
3302 p->iStatement = db->nSavepoint + db->nStatement;
3303 }
3304
3305 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
3306 if( rc==SQLITE_OK ){
3307 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3308 }
3309
3310 /* Store the current value of the database handles deferred constraint
3311 ** counter. If the statement transaction needs to be rolled back,
3312 ** the value of this counter needs to be restored too. */
3313 p->nStmtDefCons = db->nDeferredCons;
3314 p->nStmtDefImmCons = db->nDeferredImmCons;
3315 }
3316
3317 /* Gather the schema version number for checking:
3318 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3319 ** version is checked to ensure that the schema has not changed since the
3320 ** SQL statement was prepared.
3321 */
3322 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
3323 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
3324 }else{
3325 iGen = iMeta = 0;
3326 }
3327 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3328 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
3329 sqlite3DbFree(db, p->zErrMsg);
3330 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3331 /* If the schema-cookie from the database file matches the cookie
3332 ** stored with the in-memory representation of the schema, do
3333 ** not reload the schema from the database file.
3334 **
3335 ** If virtual-tables are in use, this is not just an optimization.
3336 ** Often, v-tables store their data in other SQLite tables, which
3337 ** are queried from within xNext() and other v-table methods using
3338 ** prepared queries. If such a query is out-of-date, we do not want to
3339 ** discard the database schema, as the user code implementing the
3340 ** v-table would have to be ready for the sqlite3_vtab structure itself
3341 ** to be invalidated whenever sqlite3_step() is called from within
3342 ** a v-table method.
3343 */
3344 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3345 sqlite3ResetOneSchema(db, pOp->p1);
3346 }
3347 p->expired = 1;
3348 rc = SQLITE_SCHEMA;
3349 }
3350 if( rc ) goto abort_due_to_error;
3351 break;
3352 }
3353
3354 /* Opcode: ReadCookie P1 P2 P3 * *
3355 **
3356 ** Read cookie number P3 from database P1 and write it into register P2.
3357 ** P3==1 is the schema version. P3==2 is the database format.
3358 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is
3359 ** the main database file and P1==1 is the database file used to store
3360 ** temporary tables.
3361 **
3362 ** There must be a read-lock on the database (either a transaction
3363 ** must be started or there must be an open cursor) before
3364 ** executing this instruction.
3365 */
3366 case OP_ReadCookie: { /* out2 */
3367 int iMeta;
3368 int iDb;
3369 int iCookie;
3370
3371 assert( p->bIsReader );
3372 iDb = pOp->p1;
3373 iCookie = pOp->p3;
3374 assert( pOp->p3<SQLITE_N_BTREE_META );
3375 assert( iDb>=0 && iDb<db->nDb );
3376 assert( db->aDb[iDb].pBt!=0 );
3377 assert( DbMaskTest(p->btreeMask, iDb) );
3378
3379 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
3380 pOut = out2Prerelease(p, pOp);
3381 pOut->u.i = iMeta;
3382 break;
3383 }
3384
3385 /* Opcode: SetCookie P1 P2 P3 * *
3386 **
3387 ** Write the integer value P3 into cookie number P2 of database P1.
3388 ** P2==1 is the schema version. P2==2 is the database format.
3389 ** P2==3 is the recommended pager cache
3390 ** size, and so forth. P1==0 is the main database file and P1==1 is the
3391 ** database file used to store temporary tables.
3392 **
3393 ** A transaction must be started before executing this opcode.
3394 */
3395 case OP_SetCookie: {
3396 Db *pDb;
3397 assert( pOp->p2<SQLITE_N_BTREE_META );
3398 assert( pOp->p1>=0 && pOp->p1<db->nDb );
3399 assert( DbMaskTest(p->btreeMask, pOp->p1) );
3400 assert( p->readOnly==0 );
3401 pDb = &db->aDb[pOp->p1];
3402 assert( pDb->pBt!=0 );
3403 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
3404 /* See note about index shifting on OP_ReadCookie */
3405 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
3406 if( pOp->p2==BTREE_SCHEMA_VERSION ){
3407 /* When the schema cookie changes, record the new cookie internally */
3408 pDb->pSchema->schema_cookie = pOp->p3;
3409 db->flags |= SQLITE_InternChanges;
3410 }else if( pOp->p2==BTREE_FILE_FORMAT ){
3411 /* Record changes in the file format */
3412 pDb->pSchema->file_format = pOp->p3;
3413 }
3414 if( pOp->p1==1 ){
3415 /* Invalidate all prepared statements whenever the TEMP database
3416 ** schema is changed. Ticket #1644 */
3417 sqlite3ExpirePreparedStatements(db);
3418 p->expired = 0;
3419 }
3420 if( rc ) goto abort_due_to_error;
3421 break;
3422 }
3423
3424 /* Opcode: OpenRead P1 P2 P3 P4 P5
3425 ** Synopsis: root=P2 iDb=P3
3426 **
3427 ** Open a read-only cursor for the database table whose root page is
3428 ** P2 in a database file. The database file is determined by P3.
3429 ** P3==0 means the main database, P3==1 means the database used for
3430 ** temporary tables, and P3>1 means used the corresponding attached
3431 ** database. Give the new cursor an identifier of P1. The P1
3432 ** values need not be contiguous but all P1 values should be small integers.
3433 ** It is an error for P1 to be negative.
3434 **
3435 ** If P5!=0 then use the content of register P2 as the root page, not
3436 ** the value of P2 itself.
3437 **
3438 ** There will be a read lock on the database whenever there is an
3439 ** open cursor. If the database was unlocked prior to this instruction
3440 ** then a read lock is acquired as part of this instruction. A read
3441 ** lock allows other processes to read the database but prohibits
3442 ** any other process from modifying the database. The read lock is
3443 ** released when all cursors are closed. If this instruction attempts
3444 ** to get a read lock but fails, the script terminates with an
3445 ** SQLITE_BUSY error code.
3446 **
3447 ** The P4 value may be either an integer (P4_INT32) or a pointer to
3448 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3449 ** structure, then said structure defines the content and collating
3450 ** sequence of the index being opened. Otherwise, if P4 is an integer
3451 ** value, it is set to the number of columns in the table.
3452 **
3453 ** See also: OpenWrite, ReopenIdx
3454 */
3455 /* Opcode: ReopenIdx P1 P2 P3 P4 P5
3456 ** Synopsis: root=P2 iDb=P3
3457 **
3458 ** The ReopenIdx opcode works exactly like ReadOpen except that it first
3459 ** checks to see if the cursor on P1 is already open with a root page
3460 ** number of P2 and if it is this opcode becomes a no-op. In other words,
3461 ** if the cursor is already open, do not reopen it.
3462 **
3463 ** The ReopenIdx opcode may only be used with P5==0 and with P4 being
3464 ** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
3465 ** every other ReopenIdx or OpenRead for the same cursor number.
3466 **
3467 ** See the OpenRead opcode documentation for additional information.
3468 */
3469 /* Opcode: OpenWrite P1 P2 P3 P4 P5
3470 ** Synopsis: root=P2 iDb=P3
3471 **
3472 ** Open a read/write cursor named P1 on the table or index whose root
3473 ** page is P2. Or if P5!=0 use the content of register P2 to find the
3474 ** root page.
3475 **
3476 ** The P4 value may be either an integer (P4_INT32) or a pointer to
3477 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3478 ** structure, then said structure defines the content and collating
3479 ** sequence of the index being opened. Otherwise, if P4 is an integer
3480 ** value, it is set to the number of columns in the table, or to the
3481 ** largest index of any column of the table that is actually used.
3482 **
3483 ** This instruction works just like OpenRead except that it opens the cursor
3484 ** in read/write mode. For a given table, there can be one or more read-only
3485 ** cursors or a single read/write cursor but not both.
3486 **
3487 ** See also OpenRead.
3488 */
3489 case OP_ReopenIdx: {
3490 int nField;
3491 KeyInfo *pKeyInfo;
3492 int p2;
3493 int iDb;
3494 int wrFlag;
3495 Btree *pX;
3496 VdbeCursor *pCur;
3497 Db *pDb;
3498
3499 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
3500 assert( pOp->p4type==P4_KEYINFO );
3501 pCur = p->apCsr[pOp->p1];
3502 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
3503 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
3504 goto open_cursor_set_hints;
3505 }
3506 /* If the cursor is not currently open or is open on a different
3507 ** index, then fall through into OP_OpenRead to force a reopen */
3508 case OP_OpenRead:
3509 case OP_OpenWrite:
3510
3511 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
3512 assert( p->bIsReader );
3513 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3514 || p->readOnly==0 );
3515
3516 if( p->expired ){
3517 rc = SQLITE_ABORT_ROLLBACK;
3518 goto abort_due_to_error;
3519 }
3520
3521 nField = 0;
3522 pKeyInfo = 0;
3523 p2 = pOp->p2;
3524 iDb = pOp->p3;
3525 assert( iDb>=0 && iDb<db->nDb );
3526 assert( DbMaskTest(p->btreeMask, iDb) );
3527 pDb = &db->aDb[iDb];
3528 pX = pDb->pBt;
3529 assert( pX!=0 );
3530 if( pOp->opcode==OP_OpenWrite ){
3531 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3532 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
3533 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
3534 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3535 p->minWriteFileFormat = pDb->pSchema->file_format;
3536 }
3537 }else{
3538 wrFlag = 0;
3539 }
3540 if( pOp->p5 & OPFLAG_P2ISREG ){
3541 assert( p2>0 );
3542 assert( p2<=(p->nMem+1 - p->nCursor) );
3543 pIn2 = &aMem[p2];
3544 assert( memIsValid(pIn2) );
3545 assert( (pIn2->flags & MEM_Int)!=0 );
3546 sqlite3VdbeMemIntegerify(pIn2);
3547 p2 = (int)pIn2->u.i;
3548 /* The p2 value always comes from a prior OP_CreateTable opcode and
3549 ** that opcode will always set the p2 value to 2 or more or else fail.
3550 ** If there were a failure, the prepared statement would have halted
3551 ** before reaching this instruction. */
3552 assert( p2>=2 );
3553 }
3554 if( pOp->p4type==P4_KEYINFO ){
3555 pKeyInfo = pOp->p4.pKeyInfo;
3556 assert( pKeyInfo->enc==ENC(db) );
3557 assert( pKeyInfo->db==db );
3558 nField = pKeyInfo->nField+pKeyInfo->nXField;
3559 }else if( pOp->p4type==P4_INT32 ){
3560 nField = pOp->p4.i;
3561 }
3562 assert( pOp->p1>=0 );
3563 assert( nField>=0 );
3564 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
3565 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
3566 if( pCur==0 ) goto no_mem;
3567 pCur->nullRow = 1;
3568 pCur->isOrdered = 1;
3569 pCur->pgnoRoot = p2;
3570 #ifdef SQLITE_DEBUG
3571 pCur->wrFlag = wrFlag;
3572 #endif
3573 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
3574 pCur->pKeyInfo = pKeyInfo;
3575 /* Set the VdbeCursor.isTable variable. Previous versions of
3576 ** SQLite used to check if the root-page flags were sane at this point
3577 ** and report database corruption if they were not, but this check has
3578 ** since moved into the btree layer. */
3579 pCur->isTable = pOp->p4type!=P4_KEYINFO;
3580
3581 open_cursor_set_hints:
3582 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3583 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
3584 testcase( pOp->p5 & OPFLAG_BULKCSR );
3585 #ifdef SQLITE_ENABLE_CURSOR_HINTS
3586 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3587 #endif
3588 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
3589 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
3590 if( rc ) goto abort_due_to_error;
3591 break;
3592 }
3593
3594 /* Opcode: OpenEphemeral P1 P2 * P4 P5
3595 ** Synopsis: nColumn=P2
3596 **
3597 ** Open a new cursor P1 to a transient table.
3598 ** The cursor is always opened read/write even if
3599 ** the main database is read-only. The ephemeral
3600 ** table is deleted automatically when the cursor is closed.
3601 **
3602 ** P2 is the number of columns in the ephemeral table.
3603 ** The cursor points to a BTree table if P4==0 and to a BTree index
3604 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
3605 ** that defines the format of keys in the index.
3606 **
3607 ** The P5 parameter can be a mask of the BTREE_* flags defined
3608 ** in btree.h. These flags control aspects of the operation of
3609 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3610 ** added automatically.
3611 */
3612 /* Opcode: OpenAutoindex P1 P2 * P4 *
3613 ** Synopsis: nColumn=P2
3614 **
3615 ** This opcode works the same as OP_OpenEphemeral. It has a
3616 ** different name to distinguish its use. Tables created using
3617 ** by this opcode will be used for automatically created transient
3618 ** indices in joins.
3619 */
3620 case OP_OpenAutoindex:
3621 case OP_OpenEphemeral: {
3622 VdbeCursor *pCx;
3623 KeyInfo *pKeyInfo;
3624
3625 static const int vfsFlags =
3626 SQLITE_OPEN_READWRITE |
3627 SQLITE_OPEN_CREATE |
3628 SQLITE_OPEN_EXCLUSIVE |
3629 SQLITE_OPEN_DELETEONCLOSE |
3630 SQLITE_OPEN_TRANSIENT_DB;
3631 assert( pOp->p1>=0 );
3632 assert( pOp->p2>=0 );
3633 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
3634 if( pCx==0 ) goto no_mem;
3635 pCx->nullRow = 1;
3636 pCx->isEphemeral = 1;
3637 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
3638 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
3639 if( rc==SQLITE_OK ){
3640 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1);
3641 }
3642 if( rc==SQLITE_OK ){
3643 /* If a transient index is required, create it by calling
3644 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
3645 ** opening it. If a transient table is required, just use the
3646 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
3647 */
3648 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
3649 int pgno;
3650 assert( pOp->p4type==P4_KEYINFO );
3651 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
3652 if( rc==SQLITE_OK ){
3653 assert( pgno==MASTER_ROOT+1 );
3654 assert( pKeyInfo->db==db );
3655 assert( pKeyInfo->enc==ENC(db) );
3656 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
3657 pKeyInfo, pCx->uc.pCursor);
3658 }
3659 pCx->isTable = 0;
3660 }else{
3661 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
3662 0, pCx->uc.pCursor);
3663 pCx->isTable = 1;
3664 }
3665 }
3666 if( rc ) goto abort_due_to_error;
3667 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
3668 break;
3669 }
3670
3671 /* Opcode: SorterOpen P1 P2 P3 P4 *
3672 **
3673 ** This opcode works like OP_OpenEphemeral except that it opens
3674 ** a transient index that is specifically designed to sort large
3675 ** tables using an external merge-sort algorithm.
3676 **
3677 ** If argument P3 is non-zero, then it indicates that the sorter may
3678 ** assume that a stable sort considering the first P3 fields of each
3679 ** key is sufficient to produce the required results.
3680 */
3681 case OP_SorterOpen: {
3682 VdbeCursor *pCx;
3683
3684 assert( pOp->p1>=0 );
3685 assert( pOp->p2>=0 );
3686 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
3687 if( pCx==0 ) goto no_mem;
3688 pCx->pKeyInfo = pOp->p4.pKeyInfo;
3689 assert( pCx->pKeyInfo->db==db );
3690 assert( pCx->pKeyInfo->enc==ENC(db) );
3691 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
3692 if( rc ) goto abort_due_to_error;
3693 break;
3694 }
3695
3696 /* Opcode: SequenceTest P1 P2 * * *
3697 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3698 **
3699 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3700 ** to P2. Regardless of whether or not the jump is taken, increment the
3701 ** the sequence value.
3702 */
3703 case OP_SequenceTest: {
3704 VdbeCursor *pC;
3705 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3706 pC = p->apCsr[pOp->p1];
3707 assert( isSorter(pC) );
3708 if( (pC->seqCount++)==0 ){
3709 goto jump_to_p2;
3710 }
3711 break;
3712 }
3713
3714 /* Opcode: OpenPseudo P1 P2 P3 * *
3715 ** Synopsis: P3 columns in r[P2]
3716 **
3717 ** Open a new cursor that points to a fake table that contains a single
3718 ** row of data. The content of that one row is the content of memory
3719 ** register P2. In other words, cursor P1 becomes an alias for the
3720 ** MEM_Blob content contained in register P2.
3721 **
3722 ** A pseudo-table created by this opcode is used to hold a single
3723 ** row output from the sorter so that the row can be decomposed into
3724 ** individual columns using the OP_Column opcode. The OP_Column opcode
3725 ** is the only cursor opcode that works with a pseudo-table.
3726 **
3727 ** P3 is the number of fields in the records that will be stored by
3728 ** the pseudo-table.
3729 */
3730 case OP_OpenPseudo: {
3731 VdbeCursor *pCx;
3732
3733 assert( pOp->p1>=0 );
3734 assert( pOp->p3>=0 );
3735 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
3736 if( pCx==0 ) goto no_mem;
3737 pCx->nullRow = 1;
3738 pCx->uc.pseudoTableReg = pOp->p2;
3739 pCx->isTable = 1;
3740 assert( pOp->p5==0 );
3741 break;
3742 }
3743
3744 /* Opcode: Close P1 * * * *
3745 **
3746 ** Close a cursor previously opened as P1. If P1 is not
3747 ** currently open, this instruction is a no-op.
3748 */
3749 case OP_Close: {
3750 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3751 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3752 p->apCsr[pOp->p1] = 0;
3753 break;
3754 }
3755
3756 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3757 /* Opcode: ColumnsUsed P1 * * P4 *
3758 **
3759 ** This opcode (which only exists if SQLite was compiled with
3760 ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
3761 ** table or index for cursor P1 are used. P4 is a 64-bit integer
3762 ** (P4_INT64) in which the first 63 bits are one for each of the
3763 ** first 63 columns of the table or index that are actually used
3764 ** by the cursor. The high-order bit is set if any column after
3765 ** the 64th is used.
3766 */
3767 case OP_ColumnsUsed: {
3768 VdbeCursor *pC;
3769 pC = p->apCsr[pOp->p1];
3770 assert( pC->eCurType==CURTYPE_BTREE );
3771 pC->maskUsed = *(u64*)pOp->p4.pI64;
3772 break;
3773 }
3774 #endif
3775
3776 /* Opcode: SeekGE P1 P2 P3 P4 *
3777 ** Synopsis: key=r[P3@P4]
3778 **
3779 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
3780 ** use the value in register P3 as the key. If cursor P1 refers
3781 ** to an SQL index, then P3 is the first in an array of P4 registers
3782 ** that are used as an unpacked index key.
3783 **
3784 ** Reposition cursor P1 so that it points to the smallest entry that
3785 ** is greater than or equal to the key value. If there are no records
3786 ** greater than or equal to the key and P2 is not zero, then jump to P2.
3787 **
3788 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3789 ** opcode will always land on a record that equally equals the key, or
3790 ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3791 ** opcode must be followed by an IdxLE opcode with the same arguments.
3792 ** The IdxLE opcode will be skipped if this opcode succeeds, but the
3793 ** IdxLE opcode will be used on subsequent loop iterations.
3794 **
3795 ** This opcode leaves the cursor configured to move in forward order,
3796 ** from the beginning toward the end. In other words, the cursor is
3797 ** configured to use Next, not Prev.
3798 **
3799 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
3800 */
3801 /* Opcode: SeekGT P1 P2 P3 P4 *
3802 ** Synopsis: key=r[P3@P4]
3803 **
3804 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
3805 ** use the value in register P3 as a key. If cursor P1 refers
3806 ** to an SQL index, then P3 is the first in an array of P4 registers
3807 ** that are used as an unpacked index key.
3808 **
3809 ** Reposition cursor P1 so that it points to the smallest entry that
3810 ** is greater than the key value. If there are no records greater than
3811 ** the key and P2 is not zero, then jump to P2.
3812 **
3813 ** This opcode leaves the cursor configured to move in forward order,
3814 ** from the beginning toward the end. In other words, the cursor is
3815 ** configured to use Next, not Prev.
3816 **
3817 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
3818 */
3819 /* Opcode: SeekLT P1 P2 P3 P4 *
3820 ** Synopsis: key=r[P3@P4]
3821 **
3822 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
3823 ** use the value in register P3 as a key. If cursor P1 refers
3824 ** to an SQL index, then P3 is the first in an array of P4 registers
3825 ** that are used as an unpacked index key.
3826 **
3827 ** Reposition cursor P1 so that it points to the largest entry that
3828 ** is less than the key value. If there are no records less than
3829 ** the key and P2 is not zero, then jump to P2.
3830 **
3831 ** This opcode leaves the cursor configured to move in reverse order,
3832 ** from the end toward the beginning. In other words, the cursor is
3833 ** configured to use Prev, not Next.
3834 **
3835 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
3836 */
3837 /* Opcode: SeekLE P1 P2 P3 P4 *
3838 ** Synopsis: key=r[P3@P4]
3839 **
3840 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
3841 ** use the value in register P3 as a key. If cursor P1 refers
3842 ** to an SQL index, then P3 is the first in an array of P4 registers
3843 ** that are used as an unpacked index key.
3844 **
3845 ** Reposition cursor P1 so that it points to the largest entry that
3846 ** is less than or equal to the key value. If there are no records
3847 ** less than or equal to the key and P2 is not zero, then jump to P2.
3848 **
3849 ** This opcode leaves the cursor configured to move in reverse order,
3850 ** from the end toward the beginning. In other words, the cursor is
3851 ** configured to use Prev, not Next.
3852 **
3853 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3854 ** opcode will always land on a record that equally equals the key, or
3855 ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3856 ** opcode must be followed by an IdxGE opcode with the same arguments.
3857 ** The IdxGE opcode will be skipped if this opcode succeeds, but the
3858 ** IdxGE opcode will be used on subsequent loop iterations.
3859 **
3860 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
3861 */
3862 case OP_SeekLT: /* jump, in3 */
3863 case OP_SeekLE: /* jump, in3 */
3864 case OP_SeekGE: /* jump, in3 */
3865 case OP_SeekGT: { /* jump, in3 */
3866 int res; /* Comparison result */
3867 int oc; /* Opcode */
3868 VdbeCursor *pC; /* The cursor to seek */
3869 UnpackedRecord r; /* The key to seek for */
3870 int nField; /* Number of columns or fields in the key */
3871 i64 iKey; /* The rowid we are to seek to */
3872 int eqOnly; /* Only interested in == results */
3873
3874 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3875 assert( pOp->p2!=0 );
3876 pC = p->apCsr[pOp->p1];
3877 assert( pC!=0 );
3878 assert( pC->eCurType==CURTYPE_BTREE );
3879 assert( OP_SeekLE == OP_SeekLT+1 );
3880 assert( OP_SeekGE == OP_SeekLT+2 );
3881 assert( OP_SeekGT == OP_SeekLT+3 );
3882 assert( pC->isOrdered );
3883 assert( pC->uc.pCursor!=0 );
3884 oc = pOp->opcode;
3885 eqOnly = 0;
3886 pC->nullRow = 0;
3887 #ifdef SQLITE_DEBUG
3888 pC->seekOp = pOp->opcode;
3889 #endif
3890
3891 if( pC->isTable ){
3892 /* The BTREE_SEEK_EQ flag is only set on index cursors */
3893 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
3894 || CORRUPT_DB );
3895
3896 /* The input value in P3 might be of any type: integer, real, string,
3897 ** blob, or NULL. But it needs to be an integer before we can do
3898 ** the seek, so convert it. */
3899 pIn3 = &aMem[pOp->p3];
3900 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
3901 applyNumericAffinity(pIn3, 0);
3902 }
3903 iKey = sqlite3VdbeIntValue(pIn3);
3904
3905 /* If the P3 value could not be converted into an integer without
3906 ** loss of information, then special processing is required... */
3907 if( (pIn3->flags & MEM_Int)==0 ){
3908 if( (pIn3->flags & MEM_Real)==0 ){
3909 /* If the P3 value cannot be converted into any kind of a number,
3910 ** then the seek is not possible, so jump to P2 */
3911 VdbeBranchTaken(1,2); goto jump_to_p2;
3912 break;
3913 }
3914
3915 /* If the approximation iKey is larger than the actual real search
3916 ** term, substitute >= for > and < for <=. e.g. if the search term
3917 ** is 4.9 and the integer approximation 5:
3918 **
3919 ** (x > 4.9) -> (x >= 5)
3920 ** (x <= 4.9) -> (x < 5)
3921 */
3922 if( pIn3->u.r<(double)iKey ){
3923 assert( OP_SeekGE==(OP_SeekGT-1) );
3924 assert( OP_SeekLT==(OP_SeekLE-1) );
3925 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3926 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
3927 }
3928
3929 /* If the approximation iKey is smaller than the actual real search
3930 ** term, substitute <= for < and > for >=. */
3931 else if( pIn3->u.r>(double)iKey ){
3932 assert( OP_SeekLE==(OP_SeekLT+1) );
3933 assert( OP_SeekGT==(OP_SeekGE+1) );
3934 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3935 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
3936 }
3937 }
3938 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
3939 pC->movetoTarget = iKey; /* Used by OP_Delete */
3940 if( rc!=SQLITE_OK ){
3941 goto abort_due_to_error;
3942 }
3943 }else{
3944 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
3945 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
3946 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
3947 */
3948 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
3949 eqOnly = 1;
3950 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
3951 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3952 assert( pOp[1].p1==pOp[0].p1 );
3953 assert( pOp[1].p2==pOp[0].p2 );
3954 assert( pOp[1].p3==pOp[0].p3 );
3955 assert( pOp[1].p4.i==pOp[0].p4.i );
3956 }
3957
3958 nField = pOp->p4.i;
3959 assert( pOp->p4type==P4_INT32 );
3960 assert( nField>0 );
3961 r.pKeyInfo = pC->pKeyInfo;
3962 r.nField = (u16)nField;
3963
3964 /* The next line of code computes as follows, only faster:
3965 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
3966 ** r.default_rc = -1;
3967 ** }else{
3968 ** r.default_rc = +1;
3969 ** }
3970 */
3971 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
3972 assert( oc!=OP_SeekGT || r.default_rc==-1 );
3973 assert( oc!=OP_SeekLE || r.default_rc==-1 );
3974 assert( oc!=OP_SeekGE || r.default_rc==+1 );
3975 assert( oc!=OP_SeekLT || r.default_rc==+1 );
3976
3977 r.aMem = &aMem[pOp->p3];
3978 #ifdef SQLITE_DEBUG
3979 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3980 #endif
3981 r.eqSeen = 0;
3982 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
3983 if( rc!=SQLITE_OK ){
3984 goto abort_due_to_error;
3985 }
3986 if( eqOnly && r.eqSeen==0 ){
3987 assert( res!=0 );
3988 goto seek_not_found;
3989 }
3990 }
3991 pC->deferredMoveto = 0;
3992 pC->cacheStatus = CACHE_STALE;
3993 #ifdef SQLITE_TEST
3994 sqlite3_search_count++;
3995 #endif
3996 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
3997 if( res<0 || (res==0 && oc==OP_SeekGT) ){
3998 res = 0;
3999 rc = sqlite3BtreeNext(pC->uc.pCursor, &res);
4000 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4001 }else{
4002 res = 0;
4003 }
4004 }else{
4005 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4006 if( res>0 || (res==0 && oc==OP_SeekLT) ){
4007 res = 0;
4008 rc = sqlite3BtreePrevious(pC->uc.pCursor, &res);
4009 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4010 }else{
4011 /* res might be negative because the table is empty. Check to
4012 ** see if this is the case.
4013 */
4014 res = sqlite3BtreeEof(pC->uc.pCursor);
4015 }
4016 }
4017 seek_not_found:
4018 assert( pOp->p2>0 );
4019 VdbeBranchTaken(res!=0,2);
4020 if( res ){
4021 goto jump_to_p2;
4022 }else if( eqOnly ){
4023 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4024 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
4025 }
4026 break;
4027 }
4028
4029 /* Opcode: Found P1 P2 P3 P4 *
4030 ** Synopsis: key=r[P3@P4]
4031 **
4032 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4033 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
4034 ** record.
4035 **
4036 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
4037 ** is a prefix of any entry in P1 then a jump is made to P2 and
4038 ** P1 is left pointing at the matching entry.
4039 **
4040 ** This operation leaves the cursor in a state where it can be
4041 ** advanced in the forward direction. The Next instruction will work,
4042 ** but not the Prev instruction.
4043 **
4044 ** See also: NotFound, NoConflict, NotExists. SeekGe
4045 */
4046 /* Opcode: NotFound P1 P2 P3 P4 *
4047 ** Synopsis: key=r[P3@P4]
4048 **
4049 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4050 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
4051 ** record.
4052 **
4053 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
4054 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4055 ** does contain an entry whose prefix matches the P3/P4 record then control
4056 ** falls through to the next instruction and P1 is left pointing at the
4057 ** matching entry.
4058 **
4059 ** This operation leaves the cursor in a state where it cannot be
4060 ** advanced in either direction. In other words, the Next and Prev
4061 ** opcodes do not work after this operation.
4062 **
4063 ** See also: Found, NotExists, NoConflict
4064 */
4065 /* Opcode: NoConflict P1 P2 P3 P4 *
4066 ** Synopsis: key=r[P3@P4]
4067 **
4068 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4069 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
4070 ** record.
4071 **
4072 ** Cursor P1 is on an index btree. If the record identified by P3 and P4
4073 ** contains any NULL value, jump immediately to P2. If all terms of the
4074 ** record are not-NULL then a check is done to determine if any row in the
4075 ** P1 index btree has a matching key prefix. If there are no matches, jump
4076 ** immediately to P2. If there is a match, fall through and leave the P1
4077 ** cursor pointing to the matching row.
4078 **
4079 ** This opcode is similar to OP_NotFound with the exceptions that the
4080 ** branch is always taken if any part of the search key input is NULL.
4081 **
4082 ** This operation leaves the cursor in a state where it cannot be
4083 ** advanced in either direction. In other words, the Next and Prev
4084 ** opcodes do not work after this operation.
4085 **
4086 ** See also: NotFound, Found, NotExists
4087 */
4088 case OP_NoConflict: /* jump, in3 */
4089 case OP_NotFound: /* jump, in3 */
4090 case OP_Found: { /* jump, in3 */
4091 int alreadyExists;
4092 int takeJump;
4093 int ii;
4094 VdbeCursor *pC;
4095 int res;
4096 UnpackedRecord *pFree;
4097 UnpackedRecord *pIdxKey;
4098 UnpackedRecord r;
4099
4100 #ifdef SQLITE_TEST
4101 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
4102 #endif
4103
4104 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4105 assert( pOp->p4type==P4_INT32 );
4106 pC = p->apCsr[pOp->p1];
4107 assert( pC!=0 );
4108 #ifdef SQLITE_DEBUG
4109 pC->seekOp = pOp->opcode;
4110 #endif
4111 pIn3 = &aMem[pOp->p3];
4112 assert( pC->eCurType==CURTYPE_BTREE );
4113 assert( pC->uc.pCursor!=0 );
4114 assert( pC->isTable==0 );
4115 if( pOp->p4.i>0 ){
4116 r.pKeyInfo = pC->pKeyInfo;
4117 r.nField = (u16)pOp->p4.i;
4118 r.aMem = pIn3;
4119 #ifdef SQLITE_DEBUG
4120 for(ii=0; ii<r.nField; ii++){
4121 assert( memIsValid(&r.aMem[ii]) );
4122 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
4123 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
4124 }
4125 #endif
4126 pIdxKey = &r;
4127 pFree = 0;
4128 }else{
4129 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
4130 if( pIdxKey==0 ) goto no_mem;
4131 assert( pIn3->flags & MEM_Blob );
4132 (void)ExpandBlob(pIn3);
4133 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
4134 }
4135 pIdxKey->default_rc = 0;
4136 takeJump = 0;
4137 if( pOp->opcode==OP_NoConflict ){
4138 /* For the OP_NoConflict opcode, take the jump if any of the
4139 ** input fields are NULL, since any key with a NULL will not
4140 ** conflict */
4141 for(ii=0; ii<pIdxKey->nField; ii++){
4142 if( pIdxKey->aMem[ii].flags & MEM_Null ){
4143 takeJump = 1;
4144 break;
4145 }
4146 }
4147 }
4148 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
4149 if( pFree ) sqlite3DbFree(db, pFree);
4150 if( rc!=SQLITE_OK ){
4151 goto abort_due_to_error;
4152 }
4153 pC->seekResult = res;
4154 alreadyExists = (res==0);
4155 pC->nullRow = 1-alreadyExists;
4156 pC->deferredMoveto = 0;
4157 pC->cacheStatus = CACHE_STALE;
4158 if( pOp->opcode==OP_Found ){
4159 VdbeBranchTaken(alreadyExists!=0,2);
4160 if( alreadyExists ) goto jump_to_p2;
4161 }else{
4162 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4163 if( takeJump || !alreadyExists ) goto jump_to_p2;
4164 }
4165 break;
4166 }
4167
4168 /* Opcode: SeekRowid P1 P2 P3 * *
4169 ** Synopsis: intkey=r[P3]
4170 **
4171 ** P1 is the index of a cursor open on an SQL table btree (with integer
4172 ** keys). If register P3 does not contain an integer or if P1 does not
4173 ** contain a record with rowid P3 then jump immediately to P2.
4174 ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4175 ** a record with rowid P3 then
4176 ** leave the cursor pointing at that record and fall through to the next
4177 ** instruction.
4178 **
4179 ** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4180 ** the P3 register must be guaranteed to contain an integer value. With this
4181 ** opcode, register P3 might not contain an integer.
4182 **
4183 ** The OP_NotFound opcode performs the same operation on index btrees
4184 ** (with arbitrary multi-value keys).
4185 **
4186 ** This opcode leaves the cursor in a state where it cannot be advanced
4187 ** in either direction. In other words, the Next and Prev opcodes will
4188 ** not work following this opcode.
4189 **
4190 ** See also: Found, NotFound, NoConflict, SeekRowid
4191 */
4192 /* Opcode: NotExists P1 P2 P3 * *
4193 ** Synopsis: intkey=r[P3]
4194 **
4195 ** P1 is the index of a cursor open on an SQL table btree (with integer
4196 ** keys). P3 is an integer rowid. If P1 does not contain a record with
4197 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4198 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4199 ** leave the cursor pointing at that record and fall through to the next
4200 ** instruction.
4201 **
4202 ** The OP_SeekRowid opcode performs the same operation but also allows the
4203 ** P3 register to contain a non-integer value, in which case the jump is
4204 ** always taken. This opcode requires that P3 always contain an integer.
4205 **
4206 ** The OP_NotFound opcode performs the same operation on index btrees
4207 ** (with arbitrary multi-value keys).
4208 **
4209 ** This opcode leaves the cursor in a state where it cannot be advanced
4210 ** in either direction. In other words, the Next and Prev opcodes will
4211 ** not work following this opcode.
4212 **
4213 ** See also: Found, NotFound, NoConflict, SeekRowid
4214 */
4215 case OP_SeekRowid: { /* jump, in3 */
4216 VdbeCursor *pC;
4217 BtCursor *pCrsr;
4218 int res;
4219 u64 iKey;
4220
4221 pIn3 = &aMem[pOp->p3];
4222 if( (pIn3->flags & MEM_Int)==0 ){
4223 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
4224 if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2;
4225 }
4226 /* Fall through into OP_NotExists */
4227 case OP_NotExists: /* jump, in3 */
4228 pIn3 = &aMem[pOp->p3];
4229 assert( pIn3->flags & MEM_Int );
4230 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4231 pC = p->apCsr[pOp->p1];
4232 assert( pC!=0 );
4233 #ifdef SQLITE_DEBUG
4234 pC->seekOp = 0;
4235 #endif
4236 assert( pC->isTable );
4237 assert( pC->eCurType==CURTYPE_BTREE );
4238 pCrsr = pC->uc.pCursor;
4239 assert( pCrsr!=0 );
4240 res = 0;
4241 iKey = pIn3->u.i;
4242 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
4243 assert( rc==SQLITE_OK || res==0 );
4244 pC->movetoTarget = iKey; /* Used by OP_Delete */
4245 pC->nullRow = 0;
4246 pC->cacheStatus = CACHE_STALE;
4247 pC->deferredMoveto = 0;
4248 VdbeBranchTaken(res!=0,2);
4249 pC->seekResult = res;
4250 if( res!=0 ){
4251 assert( rc==SQLITE_OK );
4252 if( pOp->p2==0 ){
4253 rc = SQLITE_CORRUPT_BKPT;
4254 }else{
4255 goto jump_to_p2;
4256 }
4257 }
4258 if( rc ) goto abort_due_to_error;
4259 break;
4260 }
4261
4262 /* Opcode: Sequence P1 P2 * * *
4263 ** Synopsis: r[P2]=cursor[P1].ctr++
4264 **
4265 ** Find the next available sequence number for cursor P1.
4266 ** Write the sequence number into register P2.
4267 ** The sequence number on the cursor is incremented after this
4268 ** instruction.
4269 */
4270 case OP_Sequence: { /* out2 */
4271 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4272 assert( p->apCsr[pOp->p1]!=0 );
4273 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
4274 pOut = out2Prerelease(p, pOp);
4275 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
4276 break;
4277 }
4278
4279
4280 /* Opcode: NewRowid P1 P2 P3 * *
4281 ** Synopsis: r[P2]=rowid
4282 **
4283 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
4284 ** The record number is not previously used as a key in the database
4285 ** table that cursor P1 points to. The new record number is written
4286 ** written to register P2.
4287 **
4288 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4289 ** the largest previously generated record number. No new record numbers are
4290 ** allowed to be less than this value. When this value reaches its maximum,
4291 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
4292 ** generated record number. This P3 mechanism is used to help implement the
4293 ** AUTOINCREMENT feature.
4294 */
4295 case OP_NewRowid: { /* out2 */
4296 i64 v; /* The new rowid */
4297 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4298 int res; /* Result of an sqlite3BtreeLast() */
4299 int cnt; /* Counter to limit the number of searches */
4300 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
4301 VdbeFrame *pFrame; /* Root frame of VDBE */
4302
4303 v = 0;
4304 res = 0;
4305 pOut = out2Prerelease(p, pOp);
4306 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4307 pC = p->apCsr[pOp->p1];
4308 assert( pC!=0 );
4309 assert( pC->eCurType==CURTYPE_BTREE );
4310 assert( pC->uc.pCursor!=0 );
4311 {
4312 /* The next rowid or record number (different terms for the same
4313 ** thing) is obtained in a two-step algorithm.
4314 **
4315 ** First we attempt to find the largest existing rowid and add one
4316 ** to that. But if the largest existing rowid is already the maximum
4317 ** positive integer, we have to fall through to the second
4318 ** probabilistic algorithm
4319 **
4320 ** The second algorithm is to select a rowid at random and see if
4321 ** it already exists in the table. If it does not exist, we have
4322 ** succeeded. If the random rowid does exist, we select a new one
4323 ** and try again, up to 100 times.
4324 */
4325 assert( pC->isTable );
4326
4327 #ifdef SQLITE_32BIT_ROWID
4328 # define MAX_ROWID 0x7fffffff
4329 #else
4330 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4331 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4332 ** to provide the constant while making all compilers happy.
4333 */
4334 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
4335 #endif
4336
4337 if( !pC->useRandomRowid ){
4338 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
4339 if( rc!=SQLITE_OK ){
4340 goto abort_due_to_error;
4341 }
4342 if( res ){
4343 v = 1; /* IMP: R-61914-48074 */
4344 }else{
4345 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
4346 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
4347 if( v>=MAX_ROWID ){
4348 pC->useRandomRowid = 1;
4349 }else{
4350 v++; /* IMP: R-29538-34987 */
4351 }
4352 }
4353 }
4354
4355 #ifndef SQLITE_OMIT_AUTOINCREMENT
4356 if( pOp->p3 ){
4357 /* Assert that P3 is a valid memory cell. */
4358 assert( pOp->p3>0 );
4359 if( p->pFrame ){
4360 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
4361 /* Assert that P3 is a valid memory cell. */
4362 assert( pOp->p3<=pFrame->nMem );
4363 pMem = &pFrame->aMem[pOp->p3];
4364 }else{
4365 /* Assert that P3 is a valid memory cell. */
4366 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
4367 pMem = &aMem[pOp->p3];
4368 memAboutToChange(p, pMem);
4369 }
4370 assert( memIsValid(pMem) );
4371
4372 REGISTER_TRACE(pOp->p3, pMem);
4373 sqlite3VdbeMemIntegerify(pMem);
4374 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4375 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
4376 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
4377 goto abort_due_to_error;
4378 }
4379 if( v<pMem->u.i+1 ){
4380 v = pMem->u.i + 1;
4381 }
4382 pMem->u.i = v;
4383 }
4384 #endif
4385 if( pC->useRandomRowid ){
4386 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
4387 ** largest possible integer (9223372036854775807) then the database
4388 ** engine starts picking positive candidate ROWIDs at random until
4389 ** it finds one that is not previously used. */
4390 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4391 ** an AUTOINCREMENT table. */
4392 cnt = 0;
4393 do{
4394 sqlite3_randomness(sizeof(v), &v);
4395 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
4396 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
4397 0, &res))==SQLITE_OK)
4398 && (res==0)
4399 && (++cnt<100));
4400 if( rc ) goto abort_due_to_error;
4401 if( res==0 ){
4402 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
4403 goto abort_due_to_error;
4404 }
4405 assert( v>0 ); /* EV: R-40812-03570 */
4406 }
4407 pC->deferredMoveto = 0;
4408 pC->cacheStatus = CACHE_STALE;
4409 }
4410 pOut->u.i = v;
4411 break;
4412 }
4413
4414 /* Opcode: Insert P1 P2 P3 P4 P5
4415 ** Synopsis: intkey=r[P3] data=r[P2]
4416 **
4417 ** Write an entry into the table of cursor P1. A new entry is
4418 ** created if it doesn't already exist or the data for an existing
4419 ** entry is overwritten. The data is the value MEM_Blob stored in register
4420 ** number P2. The key is stored in register P3. The key must
4421 ** be a MEM_Int.
4422 **
4423 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4424 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
4425 ** then rowid is stored for subsequent return by the
4426 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
4427 **
4428 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4429 ** run faster by avoiding an unnecessary seek on cursor P1. However,
4430 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
4431 ** seeks on the cursor or if the most recent seek used a key equal to P3.
4432 **
4433 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4434 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4435 ** is part of an INSERT operation. The difference is only important to
4436 ** the update hook.
4437 **
4438 ** Parameter P4 may point to a Table structure, or may be NULL. If it is
4439 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
4440 ** following a successful insert.
4441 **
4442 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4443 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
4444 ** and register P2 becomes ephemeral. If the cursor is changed, the
4445 ** value of register P2 will then change. Make sure this does not
4446 ** cause any problems.)
4447 **
4448 ** This instruction only works on tables. The equivalent instruction
4449 ** for indices is OP_IdxInsert.
4450 */
4451 /* Opcode: InsertInt P1 P2 P3 P4 P5
4452 ** Synopsis: intkey=P3 data=r[P2]
4453 **
4454 ** This works exactly like OP_Insert except that the key is the
4455 ** integer value P3, not the value of the integer stored in register P3.
4456 */
4457 case OP_Insert:
4458 case OP_InsertInt: {
4459 Mem *pData; /* MEM cell holding data for the record to be inserted */
4460 Mem *pKey; /* MEM cell holding key for the record */
4461 VdbeCursor *pC; /* Cursor to table into which insert is written */
4462 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4463 const char *zDb; /* database name - used by the update hook */
4464 Table *pTab; /* Table structure - used by update and pre-update hooks */
4465 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
4466 BtreePayload x; /* Payload to be inserted */
4467
4468 op = 0;
4469 pData = &aMem[pOp->p2];
4470 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4471 assert( memIsValid(pData) );
4472 pC = p->apCsr[pOp->p1];
4473 assert( pC!=0 );
4474 assert( pC->eCurType==CURTYPE_BTREE );
4475 assert( pC->uc.pCursor!=0 );
4476 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
4477 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
4478 REGISTER_TRACE(pOp->p2, pData);
4479
4480 if( pOp->opcode==OP_Insert ){
4481 pKey = &aMem[pOp->p3];
4482 assert( pKey->flags & MEM_Int );
4483 assert( memIsValid(pKey) );
4484 REGISTER_TRACE(pOp->p3, pKey);
4485 x.nKey = pKey->u.i;
4486 }else{
4487 assert( pOp->opcode==OP_InsertInt );
4488 x.nKey = pOp->p3;
4489 }
4490
4491 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
4492 assert( pC->iDb>=0 );
4493 zDb = db->aDb[pC->iDb].zDbSName;
4494 pTab = pOp->p4.pTab;
4495 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
4496 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
4497 }else{
4498 pTab = 0; /* Not needed. Silence a compiler warning. */
4499 zDb = 0; /* Not needed. Silence a compiler warning. */
4500 }
4501
4502 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
4503 /* Invoke the pre-update hook, if any */
4504 if( db->xPreUpdateCallback
4505 && pOp->p4type==P4_TABLE
4506 && !(pOp->p5 & OPFLAG_ISUPDATE)
4507 ){
4508 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
4509 }
4510 if( pOp->p5 & OPFLAG_ISNOOP ) break;
4511 #endif
4512
4513 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
4514 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
4515 if( pData->flags & MEM_Null ){
4516 x.pData = 0;
4517 x.nData = 0;
4518 }else{
4519 assert( pData->flags & (MEM_Blob|MEM_Str) );
4520 x.pData = pData->z;
4521 x.nData = pData->n;
4522 }
4523 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4524 if( pData->flags & MEM_Zero ){
4525 x.nZero = pData->u.nZero;
4526 }else{
4527 x.nZero = 0;
4528 }
4529 x.pKey = 0;
4530 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
4531 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
4532 );
4533 pC->deferredMoveto = 0;
4534 pC->cacheStatus = CACHE_STALE;
4535
4536 /* Invoke the update-hook if required. */
4537 if( rc ) goto abort_due_to_error;
4538 if( db->xUpdateCallback && op ){
4539 db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey);
4540 }
4541 break;
4542 }
4543
4544 /* Opcode: Delete P1 P2 P3 P4 P5
4545 **
4546 ** Delete the record at which the P1 cursor is currently pointing.
4547 **
4548 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4549 ** the cursor will be left pointing at either the next or the previous
4550 ** record in the table. If it is left pointing at the next record, then
4551 ** the next Next instruction will be a no-op. As a result, in this case
4552 ** it is ok to delete a record from within a Next loop. If
4553 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4554 ** left in an undefined state.
4555 **
4556 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4557 ** delete one of several associated with deleting a table row and all its
4558 ** associated index entries. Exactly one of those deletes is the "primary"
4559 ** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4560 ** marked with the AUXDELETE flag.
4561 **
4562 ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4563 ** change count is incremented (otherwise not).
4564 **
4565 ** P1 must not be pseudo-table. It has to be a real table with
4566 ** multiple rows.
4567 **
4568 ** If P4 is not NULL then it points to a Table object. In this case either
4569 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
4570 ** have been positioned using OP_NotFound prior to invoking this opcode in
4571 ** this case. Specifically, if one is configured, the pre-update hook is
4572 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
4573 ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
4574 **
4575 ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
4576 ** of the memory cell that contains the value that the rowid of the row will
4577 ** be set to by the update.
4578 */
4579 case OP_Delete: {
4580 VdbeCursor *pC;
4581 const char *zDb;
4582 Table *pTab;
4583 int opflags;
4584
4585 opflags = pOp->p2;
4586 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4587 pC = p->apCsr[pOp->p1];
4588 assert( pC!=0 );
4589 assert( pC->eCurType==CURTYPE_BTREE );
4590 assert( pC->uc.pCursor!=0 );
4591 assert( pC->deferredMoveto==0 );
4592
4593 #ifdef SQLITE_DEBUG
4594 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
4595 /* If p5 is zero, the seek operation that positioned the cursor prior to
4596 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
4597 ** the row that is being deleted */
4598 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
4599 assert( pC->movetoTarget==iKey );
4600 }
4601 #endif
4602
4603 /* If the update-hook or pre-update-hook will be invoked, set zDb to
4604 ** the name of the db to pass as to it. Also set local pTab to a copy
4605 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
4606 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
4607 ** VdbeCursor.movetoTarget to the current rowid. */
4608 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
4609 assert( pC->iDb>=0 );
4610 assert( pOp->p4.pTab!=0 );
4611 zDb = db->aDb[pC->iDb].zDbSName;
4612 pTab = pOp->p4.pTab;
4613 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
4614 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
4615 }
4616 }else{
4617 zDb = 0; /* Not needed. Silence a compiler warning. */
4618 pTab = 0; /* Not needed. Silence a compiler warning. */
4619 }
4620
4621 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
4622 /* Invoke the pre-update-hook if required. */
4623 if( db->xPreUpdateCallback && pOp->p4.pTab ){
4624 assert( !(opflags & OPFLAG_ISUPDATE)
4625 || HasRowid(pTab)==0
4626 || (aMem[pOp->p3].flags & MEM_Int)
4627 );
4628 sqlite3VdbePreUpdateHook(p, pC,
4629 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
4630 zDb, pTab, pC->movetoTarget,
4631 pOp->p3
4632 );
4633 }
4634 if( opflags & OPFLAG_ISNOOP ) break;
4635 #endif
4636
4637 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
4638 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
4639 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
4640 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
4641
4642 #ifdef SQLITE_DEBUG
4643 if( p->pFrame==0 ){
4644 if( pC->isEphemeral==0
4645 && (pOp->p5 & OPFLAG_AUXDELETE)==0
4646 && (pC->wrFlag & OPFLAG_FORDELETE)==0
4647 ){
4648 nExtraDelete++;
4649 }
4650 if( pOp->p2 & OPFLAG_NCHANGE ){
4651 nExtraDelete--;
4652 }
4653 }
4654 #endif
4655
4656 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
4657 pC->cacheStatus = CACHE_STALE;
4658 pC->seekResult = 0;
4659 if( rc ) goto abort_due_to_error;
4660
4661 /* Invoke the update-hook if required. */
4662 if( opflags & OPFLAG_NCHANGE ){
4663 p->nChange++;
4664 if( db->xUpdateCallback && HasRowid(pTab) ){
4665 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
4666 pC->movetoTarget);
4667 assert( pC->iDb>=0 );
4668 }
4669 }
4670
4671 break;
4672 }
4673 /* Opcode: ResetCount * * * * *
4674 **
4675 ** The value of the change counter is copied to the database handle
4676 ** change counter (returned by subsequent calls to sqlite3_changes()).
4677 ** Then the VMs internal change counter resets to 0.
4678 ** This is used by trigger programs.
4679 */
4680 case OP_ResetCount: {
4681 sqlite3VdbeSetChanges(db, p->nChange);
4682 p->nChange = 0;
4683 break;
4684 }
4685
4686 /* Opcode: SorterCompare P1 P2 P3 P4
4687 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
4688 **
4689 ** P1 is a sorter cursor. This instruction compares a prefix of the
4690 ** record blob in register P3 against a prefix of the entry that
4691 ** the sorter cursor currently points to. Only the first P4 fields
4692 ** of r[P3] and the sorter record are compared.
4693 **
4694 ** If either P3 or the sorter contains a NULL in one of their significant
4695 ** fields (not counting the P4 fields at the end which are ignored) then
4696 ** the comparison is assumed to be equal.
4697 **
4698 ** Fall through to next instruction if the two records compare equal to
4699 ** each other. Jump to P2 if they are different.
4700 */
4701 case OP_SorterCompare: {
4702 VdbeCursor *pC;
4703 int res;
4704 int nKeyCol;
4705
4706 pC = p->apCsr[pOp->p1];
4707 assert( isSorter(pC) );
4708 assert( pOp->p4type==P4_INT32 );
4709 pIn3 = &aMem[pOp->p3];
4710 nKeyCol = pOp->p4.i;
4711 res = 0;
4712 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
4713 VdbeBranchTaken(res!=0,2);
4714 if( rc ) goto abort_due_to_error;
4715 if( res ) goto jump_to_p2;
4716 break;
4717 };
4718
4719 /* Opcode: SorterData P1 P2 P3 * *
4720 ** Synopsis: r[P2]=data
4721 **
4722 ** Write into register P2 the current sorter data for sorter cursor P1.
4723 ** Then clear the column header cache on cursor P3.
4724 **
4725 ** This opcode is normally use to move a record out of the sorter and into
4726 ** a register that is the source for a pseudo-table cursor created using
4727 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
4728 ** parameter P3. Clearing the P3 column cache as part of this opcode saves
4729 ** us from having to issue a separate NullRow instruction to clear that cache.
4730 */
4731 case OP_SorterData: {
4732 VdbeCursor *pC;
4733
4734 pOut = &aMem[pOp->p2];
4735 pC = p->apCsr[pOp->p1];
4736 assert( isSorter(pC) );
4737 rc = sqlite3VdbeSorterRowkey(pC, pOut);
4738 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
4739 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4740 if( rc ) goto abort_due_to_error;
4741 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
4742 break;
4743 }
4744
4745 /* Opcode: RowData P1 P2 P3 * *
4746 ** Synopsis: r[P2]=data
4747 **
4748 ** Write into register P2 the complete row content for the row at
4749 ** which cursor P1 is currently pointing.
4750 ** There is no interpretation of the data.
4751 ** It is just copied onto the P2 register exactly as
4752 ** it is found in the database file.
4753 **
4754 ** If cursor P1 is an index, then the content is the key of the row.
4755 ** If cursor P2 is a table, then the content extracted is the data.
4756 **
4757 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
4758 ** of a real table, not a pseudo-table.
4759 **
4760 ** If P3!=0 then this opcode is allowed to make an ephermeral pointer
4761 ** into the database page. That means that the content of the output
4762 ** register will be invalidated as soon as the cursor moves - including
4763 ** moves caused by other cursors that "save" the the current cursors
4764 ** position in order that they can write to the same table. If P3==0
4765 ** then a copy of the data is made into memory. P3!=0 is faster, but
4766 ** P3==0 is safer.
4767 **
4768 ** If P3!=0 then the content of the P2 register is unsuitable for use
4769 ** in OP_Result and any OP_Result will invalidate the P2 register content.
4770 ** The P2 register content is invalidated by opcodes like OP_Function or
4771 ** by any use of another cursor pointing to the same table.
4772 */
4773 case OP_RowData: {
4774 VdbeCursor *pC;
4775 BtCursor *pCrsr;
4776 u32 n;
4777
4778 pOut = out2Prerelease(p, pOp);
4779
4780 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4781 pC = p->apCsr[pOp->p1];
4782 assert( pC!=0 );
4783 assert( pC->eCurType==CURTYPE_BTREE );
4784 assert( isSorter(pC)==0 );
4785 assert( pC->nullRow==0 );
4786 assert( pC->uc.pCursor!=0 );
4787 pCrsr = pC->uc.pCursor;
4788
4789 /* The OP_RowData opcodes always follow OP_NotExists or
4790 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
4791 ** that might invalidate the cursor.
4792 ** If this where not the case, on of the following assert()s
4793 ** would fail. Should this ever change (because of changes in the code
4794 ** generator) then the fix would be to insert a call to
4795 ** sqlite3VdbeCursorMoveto().
4796 */
4797 assert( pC->deferredMoveto==0 );
4798 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4799 #if 0 /* Not required due to the previous to assert() statements */
4800 rc = sqlite3VdbeCursorMoveto(pC);
4801 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4802 #endif
4803
4804 n = sqlite3BtreePayloadSize(pCrsr);
4805 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
4806 goto too_big;
4807 }
4808 testcase( n==0 );
4809 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
4810 if( rc ) goto abort_due_to_error;
4811 if( !pOp->p3 ) Deephemeralize(pOut);
4812 UPDATE_MAX_BLOBSIZE(pOut);
4813 REGISTER_TRACE(pOp->p2, pOut);
4814 break;
4815 }
4816
4817 /* Opcode: Rowid P1 P2 * * *
4818 ** Synopsis: r[P2]=rowid
4819 **
4820 ** Store in register P2 an integer which is the key of the table entry that
4821 ** P1 is currently point to.
4822 **
4823 ** P1 can be either an ordinary table or a virtual table. There used to
4824 ** be a separate OP_VRowid opcode for use with virtual tables, but this
4825 ** one opcode now works for both table types.
4826 */
4827 case OP_Rowid: { /* out2 */
4828 VdbeCursor *pC;
4829 i64 v;
4830 sqlite3_vtab *pVtab;
4831 const sqlite3_module *pModule;
4832
4833 pOut = out2Prerelease(p, pOp);
4834 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4835 pC = p->apCsr[pOp->p1];
4836 assert( pC!=0 );
4837 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
4838 if( pC->nullRow ){
4839 pOut->flags = MEM_Null;
4840 break;
4841 }else if( pC->deferredMoveto ){
4842 v = pC->movetoTarget;
4843 #ifndef SQLITE_OMIT_VIRTUALTABLE
4844 }else if( pC->eCurType==CURTYPE_VTAB ){
4845 assert( pC->uc.pVCur!=0 );
4846 pVtab = pC->uc.pVCur->pVtab;
4847 pModule = pVtab->pModule;
4848 assert( pModule->xRowid );
4849 rc = pModule->xRowid(pC->uc.pVCur, &v);
4850 sqlite3VtabImportErrmsg(p, pVtab);
4851 if( rc ) goto abort_due_to_error;
4852 #endif /* SQLITE_OMIT_VIRTUALTABLE */
4853 }else{
4854 assert( pC->eCurType==CURTYPE_BTREE );
4855 assert( pC->uc.pCursor!=0 );
4856 rc = sqlite3VdbeCursorRestore(pC);
4857 if( rc ) goto abort_due_to_error;
4858 if( pC->nullRow ){
4859 pOut->flags = MEM_Null;
4860 break;
4861 }
4862 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
4863 }
4864 pOut->u.i = v;
4865 break;
4866 }
4867
4868 /* Opcode: NullRow P1 * * * *
4869 **
4870 ** Move the cursor P1 to a null row. Any OP_Column operations
4871 ** that occur while the cursor is on the null row will always
4872 ** write a NULL.
4873 */
4874 case OP_NullRow: {
4875 VdbeCursor *pC;
4876
4877 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4878 pC = p->apCsr[pOp->p1];
4879 assert( pC!=0 );
4880 pC->nullRow = 1;
4881 pC->cacheStatus = CACHE_STALE;
4882 if( pC->eCurType==CURTYPE_BTREE ){
4883 assert( pC->uc.pCursor!=0 );
4884 sqlite3BtreeClearCursor(pC->uc.pCursor);
4885 }
4886 break;
4887 }
4888
4889 /* Opcode: Last P1 P2 P3 * *
4890 **
4891 ** The next use of the Rowid or Column or Prev instruction for P1
4892 ** will refer to the last entry in the database table or index.
4893 ** If the table or index is empty and P2>0, then jump immediately to P2.
4894 ** If P2 is 0 or if the table or index is not empty, fall through
4895 ** to the following instruction.
4896 **
4897 ** This opcode leaves the cursor configured to move in reverse order,
4898 ** from the end toward the beginning. In other words, the cursor is
4899 ** configured to use Prev, not Next.
4900 **
4901 ** If P3 is -1, then the cursor is positioned at the end of the btree
4902 ** for the purpose of appending a new entry onto the btree. In that
4903 ** case P2 must be 0. It is assumed that the cursor is used only for
4904 ** appending and so if the cursor is valid, then the cursor must already
4905 ** be pointing at the end of the btree and so no changes are made to
4906 ** the cursor.
4907 */
4908 case OP_Last: { /* jump */
4909 VdbeCursor *pC;
4910 BtCursor *pCrsr;
4911 int res;
4912
4913 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4914 pC = p->apCsr[pOp->p1];
4915 assert( pC!=0 );
4916 assert( pC->eCurType==CURTYPE_BTREE );
4917 pCrsr = pC->uc.pCursor;
4918 res = 0;
4919 assert( pCrsr!=0 );
4920 pC->seekResult = pOp->p3;
4921 #ifdef SQLITE_DEBUG
4922 pC->seekOp = OP_Last;
4923 #endif
4924 if( pOp->p3==0 || !sqlite3BtreeCursorIsValidNN(pCrsr) ){
4925 rc = sqlite3BtreeLast(pCrsr, &res);
4926 pC->nullRow = (u8)res;
4927 pC->deferredMoveto = 0;
4928 pC->cacheStatus = CACHE_STALE;
4929 if( rc ) goto abort_due_to_error;
4930 if( pOp->p2>0 ){
4931 VdbeBranchTaken(res!=0,2);
4932 if( res ) goto jump_to_p2;
4933 }
4934 }else{
4935 assert( pOp->p2==0 );
4936 }
4937 break;
4938 }
4939
4940
4941 /* Opcode: SorterSort P1 P2 * * *
4942 **
4943 ** After all records have been inserted into the Sorter object
4944 ** identified by P1, invoke this opcode to actually do the sorting.
4945 ** Jump to P2 if there are no records to be sorted.
4946 **
4947 ** This opcode is an alias for OP_Sort and OP_Rewind that is used
4948 ** for Sorter objects.
4949 */
4950 /* Opcode: Sort P1 P2 * * *
4951 **
4952 ** This opcode does exactly the same thing as OP_Rewind except that
4953 ** it increments an undocumented global variable used for testing.
4954 **
4955 ** Sorting is accomplished by writing records into a sorting index,
4956 ** then rewinding that index and playing it back from beginning to
4957 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4958 ** rewinding so that the global variable will be incremented and
4959 ** regression tests can determine whether or not the optimizer is
4960 ** correctly optimizing out sorts.
4961 */
4962 case OP_SorterSort: /* jump */
4963 case OP_Sort: { /* jump */
4964 #ifdef SQLITE_TEST
4965 sqlite3_sort_count++;
4966 sqlite3_search_count--;
4967 #endif
4968 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
4969 /* Fall through into OP_Rewind */
4970 }
4971 /* Opcode: Rewind P1 P2 * * *
4972 **
4973 ** The next use of the Rowid or Column or Next instruction for P1
4974 ** will refer to the first entry in the database table or index.
4975 ** If the table or index is empty, jump immediately to P2.
4976 ** If the table or index is not empty, fall through to the following
4977 ** instruction.
4978 **
4979 ** This opcode leaves the cursor configured to move in forward order,
4980 ** from the beginning toward the end. In other words, the cursor is
4981 ** configured to use Next, not Prev.
4982 */
4983 case OP_Rewind: { /* jump */
4984 VdbeCursor *pC;
4985 BtCursor *pCrsr;
4986 int res;
4987
4988 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4989 pC = p->apCsr[pOp->p1];
4990 assert( pC!=0 );
4991 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
4992 res = 1;
4993 #ifdef SQLITE_DEBUG
4994 pC->seekOp = OP_Rewind;
4995 #endif
4996 if( isSorter(pC) ){
4997 rc = sqlite3VdbeSorterRewind(pC, &res);
4998 }else{
4999 assert( pC->eCurType==CURTYPE_BTREE );
5000 pCrsr = pC->uc.pCursor;
5001 assert( pCrsr );
5002 rc = sqlite3BtreeFirst(pCrsr, &res);
5003 pC->deferredMoveto = 0;
5004 pC->cacheStatus = CACHE_STALE;
5005 }
5006 if( rc ) goto abort_due_to_error;
5007 pC->nullRow = (u8)res;
5008 assert( pOp->p2>0 && pOp->p2<p->nOp );
5009 VdbeBranchTaken(res!=0,2);
5010 if( res ) goto jump_to_p2;
5011 break;
5012 }
5013
5014 /* Opcode: Next P1 P2 P3 P4 P5
5015 **
5016 ** Advance cursor P1 so that it points to the next key/data pair in its
5017 ** table or index. If there are no more key/value pairs then fall through
5018 ** to the following instruction. But if the cursor advance was successful,
5019 ** jump immediately to P2.
5020 **
5021 ** The Next opcode is only valid following an SeekGT, SeekGE, or
5022 ** OP_Rewind opcode used to position the cursor. Next is not allowed
5023 ** to follow SeekLT, SeekLE, or OP_Last.
5024 **
5025 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
5026 ** been opened prior to this opcode or the program will segfault.
5027 **
5028 ** The P3 value is a hint to the btree implementation. If P3==1, that
5029 ** means P1 is an SQL index and that this instruction could have been
5030 ** omitted if that index had been unique. P3 is usually 0. P3 is
5031 ** always either 0 or 1.
5032 **
5033 ** P4 is always of type P4_ADVANCE. The function pointer points to
5034 ** sqlite3BtreeNext().
5035 **
5036 ** If P5 is positive and the jump is taken, then event counter
5037 ** number P5-1 in the prepared statement is incremented.
5038 **
5039 ** See also: Prev, NextIfOpen
5040 */
5041 /* Opcode: NextIfOpen P1 P2 P3 P4 P5
5042 **
5043 ** This opcode works just like Next except that if cursor P1 is not
5044 ** open it behaves a no-op.
5045 */
5046 /* Opcode: Prev P1 P2 P3 P4 P5
5047 **
5048 ** Back up cursor P1 so that it points to the previous key/data pair in its
5049 ** table or index. If there is no previous key/value pairs then fall through
5050 ** to the following instruction. But if the cursor backup was successful,
5051 ** jump immediately to P2.
5052 **
5053 **
5054 ** The Prev opcode is only valid following an SeekLT, SeekLE, or
5055 ** OP_Last opcode used to position the cursor. Prev is not allowed
5056 ** to follow SeekGT, SeekGE, or OP_Rewind.
5057 **
5058 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5059 ** not open then the behavior is undefined.
5060 **
5061 ** The P3 value is a hint to the btree implementation. If P3==1, that
5062 ** means P1 is an SQL index and that this instruction could have been
5063 ** omitted if that index had been unique. P3 is usually 0. P3 is
5064 ** always either 0 or 1.
5065 **
5066 ** P4 is always of type P4_ADVANCE. The function pointer points to
5067 ** sqlite3BtreePrevious().
5068 **
5069 ** If P5 is positive and the jump is taken, then event counter
5070 ** number P5-1 in the prepared statement is incremented.
5071 */
5072 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5
5073 **
5074 ** This opcode works just like Prev except that if cursor P1 is not
5075 ** open it behaves a no-op.
5076 */
5077 /* Opcode: SorterNext P1 P2 * * P5
5078 **
5079 ** This opcode works just like OP_Next except that P1 must be a
5080 ** sorter object for which the OP_SorterSort opcode has been
5081 ** invoked. This opcode advances the cursor to the next sorted
5082 ** record, or jumps to P2 if there are no more sorted records.
5083 */
5084 case OP_SorterNext: { /* jump */
5085 VdbeCursor *pC;
5086 int res;
5087
5088 pC = p->apCsr[pOp->p1];
5089 assert( isSorter(pC) );
5090 res = 0;
5091 rc = sqlite3VdbeSorterNext(db, pC, &res);
5092 goto next_tail;
5093 case OP_PrevIfOpen: /* jump */
5094 case OP_NextIfOpen: /* jump */
5095 if( p->apCsr[pOp->p1]==0 ) break;
5096 /* Fall through */
5097 case OP_Prev: /* jump */
5098 case OP_Next: /* jump */
5099 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5100 assert( pOp->p5<ArraySize(p->aCounter) );
5101 pC = p->apCsr[pOp->p1];
5102 res = pOp->p3;
5103 assert( pC!=0 );
5104 assert( pC->deferredMoveto==0 );
5105 assert( pC->eCurType==CURTYPE_BTREE );
5106 assert( res==0 || (res==1 && pC->isTable==0) );
5107 testcase( res==1 );
5108 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5109 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
5110 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
5111 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
5112
5113 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
5114 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
5115 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
5116 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
5117 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
5118 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
5119 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
5120 || pC->seekOp==OP_Last );
5121
5122 rc = pOp->p4.xAdvance(pC->uc.pCursor, &res);
5123 next_tail:
5124 pC->cacheStatus = CACHE_STALE;
5125 VdbeBranchTaken(res==0,2);
5126 if( rc ) goto abort_due_to_error;
5127 if( res==0 ){
5128 pC->nullRow = 0;
5129 p->aCounter[pOp->p5]++;
5130 #ifdef SQLITE_TEST
5131 sqlite3_search_count++;
5132 #endif
5133 goto jump_to_p2_and_check_for_interrupt;
5134 }else{
5135 pC->nullRow = 1;
5136 }
5137 goto check_for_interrupt;
5138 }
5139
5140 /* Opcode: IdxInsert P1 P2 P3 P4 P5
5141 ** Synopsis: key=r[P2]
5142 **
5143 ** Register P2 holds an SQL index key made using the
5144 ** MakeRecord instructions. This opcode writes that key
5145 ** into the index P1. Data for the entry is nil.
5146 **
5147 ** If P4 is not zero, then it is the number of values in the unpacked
5148 ** key of reg(P2). In that case, P3 is the index of the first register
5149 ** for the unpacked key. The availability of the unpacked key can sometimes
5150 ** be an optimization.
5151 **
5152 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5153 ** that this insert is likely to be an append.
5154 **
5155 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5156 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5157 ** then the change counter is unchanged.
5158 **
5159 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5160 ** run faster by avoiding an unnecessary seek on cursor P1. However,
5161 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5162 ** seeks on the cursor or if the most recent seek used a key equivalent
5163 ** to P2.
5164 **
5165 ** This instruction only works for indices. The equivalent instruction
5166 ** for tables is OP_Insert.
5167 */
5168 /* Opcode: SorterInsert P1 P2 * * *
5169 ** Synopsis: key=r[P2]
5170 **
5171 ** Register P2 holds an SQL index key made using the
5172 ** MakeRecord instructions. This opcode writes that key
5173 ** into the sorter P1. Data for the entry is nil.
5174 */
5175 case OP_SorterInsert: /* in2 */
5176 case OP_IdxInsert: { /* in2 */
5177 VdbeCursor *pC;
5178 BtreePayload x;
5179
5180 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5181 pC = p->apCsr[pOp->p1];
5182 assert( pC!=0 );
5183 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
5184 pIn2 = &aMem[pOp->p2];
5185 assert( pIn2->flags & MEM_Blob );
5186 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
5187 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
5188 assert( pC->isTable==0 );
5189 rc = ExpandBlob(pIn2);
5190 if( rc ) goto abort_due_to_error;
5191 if( pOp->opcode==OP_SorterInsert ){
5192 rc = sqlite3VdbeSorterWrite(pC, pIn2);
5193 }else{
5194 x.nKey = pIn2->n;
5195 x.pKey = pIn2->z;
5196 x.aMem = aMem + pOp->p3;
5197 x.nMem = (u16)pOp->p4.i;
5198 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
5199 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
5200 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5201 );
5202 assert( pC->deferredMoveto==0 );
5203 pC->cacheStatus = CACHE_STALE;
5204 }
5205 if( rc) goto abort_due_to_error;
5206 break;
5207 }
5208
5209 /* Opcode: IdxDelete P1 P2 P3 * *
5210 ** Synopsis: key=r[P2@P3]
5211 **
5212 ** The content of P3 registers starting at register P2 form
5213 ** an unpacked index key. This opcode removes that entry from the
5214 ** index opened by cursor P1.
5215 */
5216 case OP_IdxDelete: {
5217 VdbeCursor *pC;
5218 BtCursor *pCrsr;
5219 int res;
5220 UnpackedRecord r;
5221
5222 assert( pOp->p3>0 );
5223 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
5224 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5225 pC = p->apCsr[pOp->p1];
5226 assert( pC!=0 );
5227 assert( pC->eCurType==CURTYPE_BTREE );
5228 pCrsr = pC->uc.pCursor;
5229 assert( pCrsr!=0 );
5230 assert( pOp->p5==0 );
5231 r.pKeyInfo = pC->pKeyInfo;
5232 r.nField = (u16)pOp->p3;
5233 r.default_rc = 0;
5234 r.aMem = &aMem[pOp->p2];
5235 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
5236 if( rc ) goto abort_due_to_error;
5237 if( res==0 ){
5238 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
5239 if( rc ) goto abort_due_to_error;
5240 }
5241 assert( pC->deferredMoveto==0 );
5242 pC->cacheStatus = CACHE_STALE;
5243 pC->seekResult = 0;
5244 break;
5245 }
5246
5247 /* Opcode: Seek P1 * P3 P4 *
5248 ** Synopsis: Move P3 to P1.rowid
5249 **
5250 ** P1 is an open index cursor and P3 is a cursor on the corresponding
5251 ** table. This opcode does a deferred seek of the P3 table cursor
5252 ** to the row that corresponds to the current row of P1.
5253 **
5254 ** This is a deferred seek. Nothing actually happens until
5255 ** the cursor is used to read a record. That way, if no reads
5256 ** occur, no unnecessary I/O happens.
5257 **
5258 ** P4 may be an array of integers (type P4_INTARRAY) containing
5259 ** one entry for each column in the P3 table. If array entry a(i)
5260 ** is non-zero, then reading column a(i)-1 from cursor P3 is
5261 ** equivalent to performing the deferred seek and then reading column i
5262 ** from P1. This information is stored in P3 and used to redirect
5263 ** reads against P3 over to P1, thus possibly avoiding the need to
5264 ** seek and read cursor P3.
5265 */
5266 /* Opcode: IdxRowid P1 P2 * * *
5267 ** Synopsis: r[P2]=rowid
5268 **
5269 ** Write into register P2 an integer which is the last entry in the record at
5270 ** the end of the index key pointed to by cursor P1. This integer should be
5271 ** the rowid of the table entry to which this index entry points.
5272 **
5273 ** See also: Rowid, MakeRecord.
5274 */
5275 case OP_Seek:
5276 case OP_IdxRowid: { /* out2 */
5277 VdbeCursor *pC; /* The P1 index cursor */
5278 VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */
5279 i64 rowid; /* Rowid that P1 current points to */
5280
5281 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5282 pC = p->apCsr[pOp->p1];
5283 assert( pC!=0 );
5284 assert( pC->eCurType==CURTYPE_BTREE );
5285 assert( pC->uc.pCursor!=0 );
5286 assert( pC->isTable==0 );
5287 assert( pC->deferredMoveto==0 );
5288 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5289
5290 /* The IdxRowid and Seek opcodes are combined because of the commonality
5291 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5292 rc = sqlite3VdbeCursorRestore(pC);
5293
5294 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
5295 ** out from under the cursor. That will never happens for an IdxRowid
5296 ** or Seek opcode */
5297 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5298
5299 if( !pC->nullRow ){
5300 rowid = 0; /* Not needed. Only used to silence a warning. */
5301 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
5302 if( rc!=SQLITE_OK ){
5303 goto abort_due_to_error;
5304 }
5305 if( pOp->opcode==OP_Seek ){
5306 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5307 pTabCur = p->apCsr[pOp->p3];
5308 assert( pTabCur!=0 );
5309 assert( pTabCur->eCurType==CURTYPE_BTREE );
5310 assert( pTabCur->uc.pCursor!=0 );
5311 assert( pTabCur->isTable );
5312 pTabCur->nullRow = 0;
5313 pTabCur->movetoTarget = rowid;
5314 pTabCur->deferredMoveto = 1;
5315 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5316 pTabCur->aAltMap = pOp->p4.ai;
5317 pTabCur->pAltCursor = pC;
5318 }else{
5319 pOut = out2Prerelease(p, pOp);
5320 pOut->u.i = rowid;
5321 }
5322 }else{
5323 assert( pOp->opcode==OP_IdxRowid );
5324 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
5325 }
5326 break;
5327 }
5328
5329 /* Opcode: IdxGE P1 P2 P3 P4 P5
5330 ** Synopsis: key=r[P3@P4]
5331 **
5332 ** The P4 register values beginning with P3 form an unpacked index
5333 ** key that omits the PRIMARY KEY. Compare this key value against the index
5334 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5335 ** fields at the end.
5336 **
5337 ** If the P1 index entry is greater than or equal to the key value
5338 ** then jump to P2. Otherwise fall through to the next instruction.
5339 */
5340 /* Opcode: IdxGT P1 P2 P3 P4 P5
5341 ** Synopsis: key=r[P3@P4]
5342 **
5343 ** The P4 register values beginning with P3 form an unpacked index
5344 ** key that omits the PRIMARY KEY. Compare this key value against the index
5345 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5346 ** fields at the end.
5347 **
5348 ** If the P1 index entry is greater than the key value
5349 ** then jump to P2. Otherwise fall through to the next instruction.
5350 */
5351 /* Opcode: IdxLT P1 P2 P3 P4 P5
5352 ** Synopsis: key=r[P3@P4]
5353 **
5354 ** The P4 register values beginning with P3 form an unpacked index
5355 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5356 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5357 ** ROWID on the P1 index.
5358 **
5359 ** If the P1 index entry is less than the key value then jump to P2.
5360 ** Otherwise fall through to the next instruction.
5361 */
5362 /* Opcode: IdxLE P1 P2 P3 P4 P5
5363 ** Synopsis: key=r[P3@P4]
5364 **
5365 ** The P4 register values beginning with P3 form an unpacked index
5366 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5367 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5368 ** ROWID on the P1 index.
5369 **
5370 ** If the P1 index entry is less than or equal to the key value then jump
5371 ** to P2. Otherwise fall through to the next instruction.
5372 */
5373 case OP_IdxLE: /* jump */
5374 case OP_IdxGT: /* jump */
5375 case OP_IdxLT: /* jump */
5376 case OP_IdxGE: { /* jump */
5377 VdbeCursor *pC;
5378 int res;
5379 UnpackedRecord r;
5380
5381 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5382 pC = p->apCsr[pOp->p1];
5383 assert( pC!=0 );
5384 assert( pC->isOrdered );
5385 assert( pC->eCurType==CURTYPE_BTREE );
5386 assert( pC->uc.pCursor!=0);
5387 assert( pC->deferredMoveto==0 );
5388 assert( pOp->p5==0 || pOp->p5==1 );
5389 assert( pOp->p4type==P4_INT32 );
5390 r.pKeyInfo = pC->pKeyInfo;
5391 r.nField = (u16)pOp->p4.i;
5392 if( pOp->opcode<OP_IdxLT ){
5393 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
5394 r.default_rc = -1;
5395 }else{
5396 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
5397 r.default_rc = 0;
5398 }
5399 r.aMem = &aMem[pOp->p3];
5400 #ifdef SQLITE_DEBUG
5401 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
5402 #endif
5403 res = 0; /* Not needed. Only used to silence a warning. */
5404 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
5405 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5406 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5407 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
5408 res = -res;
5409 }else{
5410 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
5411 res++;
5412 }
5413 VdbeBranchTaken(res>0,2);
5414 if( rc ) goto abort_due_to_error;
5415 if( res>0 ) goto jump_to_p2;
5416 break;
5417 }
5418
5419 /* Opcode: Destroy P1 P2 P3 * *
5420 **
5421 ** Delete an entire database table or index whose root page in the database
5422 ** file is given by P1.
5423 **
5424 ** The table being destroyed is in the main database file if P3==0. If
5425 ** P3==1 then the table to be clear is in the auxiliary database file
5426 ** that is used to store tables create using CREATE TEMPORARY TABLE.
5427 **
5428 ** If AUTOVACUUM is enabled then it is possible that another root page
5429 ** might be moved into the newly deleted root page in order to keep all
5430 ** root pages contiguous at the beginning of the database. The former
5431 ** value of the root page that moved - its value before the move occurred -
5432 ** is stored in register P2. If no page
5433 ** movement was required (because the table being dropped was already
5434 ** the last one in the database) then a zero is stored in register P2.
5435 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
5436 **
5437 ** See also: Clear
5438 */
5439 case OP_Destroy: { /* out2 */
5440 int iMoved;
5441 int iDb;
5442
5443 assert( p->readOnly==0 );
5444 assert( pOp->p1>1 );
5445 pOut = out2Prerelease(p, pOp);
5446 pOut->flags = MEM_Null;
5447 if( db->nVdbeRead > db->nVDestroy+1 ){
5448 rc = SQLITE_LOCKED;
5449 p->errorAction = OE_Abort;
5450 goto abort_due_to_error;
5451 }else{
5452 iDb = pOp->p3;
5453 assert( DbMaskTest(p->btreeMask, iDb) );
5454 iMoved = 0; /* Not needed. Only to silence a warning. */
5455 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
5456 pOut->flags = MEM_Int;
5457 pOut->u.i = iMoved;
5458 if( rc ) goto abort_due_to_error;
5459 #ifndef SQLITE_OMIT_AUTOVACUUM
5460 if( iMoved!=0 ){
5461 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5462 /* All OP_Destroy operations occur on the same btree */
5463 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5464 resetSchemaOnFault = iDb+1;
5465 }
5466 #endif
5467 }
5468 break;
5469 }
5470
5471 /* Opcode: Clear P1 P2 P3
5472 **
5473 ** Delete all contents of the database table or index whose root page
5474 ** in the database file is given by P1. But, unlike Destroy, do not
5475 ** remove the table or index from the database file.
5476 **
5477 ** The table being clear is in the main database file if P2==0. If
5478 ** P2==1 then the table to be clear is in the auxiliary database file
5479 ** that is used to store tables create using CREATE TEMPORARY TABLE.
5480 **
5481 ** If the P3 value is non-zero, then the table referred to must be an
5482 ** intkey table (an SQL table, not an index). In this case the row change
5483 ** count is incremented by the number of rows in the table being cleared.
5484 ** If P3 is greater than zero, then the value stored in register P3 is
5485 ** also incremented by the number of rows in the table being cleared.
5486 **
5487 ** See also: Destroy
5488 */
5489 case OP_Clear: {
5490 int nChange;
5491
5492 nChange = 0;
5493 assert( p->readOnly==0 );
5494 assert( DbMaskTest(p->btreeMask, pOp->p2) );
5495 rc = sqlite3BtreeClearTable(
5496 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5497 );
5498 if( pOp->p3 ){
5499 p->nChange += nChange;
5500 if( pOp->p3>0 ){
5501 assert( memIsValid(&aMem[pOp->p3]) );
5502 memAboutToChange(p, &aMem[pOp->p3]);
5503 aMem[pOp->p3].u.i += nChange;
5504 }
5505 }
5506 if( rc ) goto abort_due_to_error;
5507 break;
5508 }
5509
5510 /* Opcode: ResetSorter P1 * * * *
5511 **
5512 ** Delete all contents from the ephemeral table or sorter
5513 ** that is open on cursor P1.
5514 **
5515 ** This opcode only works for cursors used for sorting and
5516 ** opened with OP_OpenEphemeral or OP_SorterOpen.
5517 */
5518 case OP_ResetSorter: {
5519 VdbeCursor *pC;
5520
5521 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5522 pC = p->apCsr[pOp->p1];
5523 assert( pC!=0 );
5524 if( isSorter(pC) ){
5525 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
5526 }else{
5527 assert( pC->eCurType==CURTYPE_BTREE );
5528 assert( pC->isEphemeral );
5529 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
5530 if( rc ) goto abort_due_to_error;
5531 }
5532 break;
5533 }
5534
5535 /* Opcode: CreateTable P1 P2 * * *
5536 ** Synopsis: r[P2]=root iDb=P1
5537 **
5538 ** Allocate a new table in the main database file if P1==0 or in the
5539 ** auxiliary database file if P1==1 or in an attached database if
5540 ** P1>1. Write the root page number of the new table into
5541 ** register P2
5542 **
5543 ** The difference between a table and an index is this: A table must
5544 ** have a 4-byte integer key and can have arbitrary data. An index
5545 ** has an arbitrary key but no data.
5546 **
5547 ** See also: CreateIndex
5548 */
5549 /* Opcode: CreateIndex P1 P2 * * *
5550 ** Synopsis: r[P2]=root iDb=P1
5551 **
5552 ** Allocate a new index in the main database file if P1==0 or in the
5553 ** auxiliary database file if P1==1 or in an attached database if
5554 ** P1>1. Write the root page number of the new table into
5555 ** register P2.
5556 **
5557 ** See documentation on OP_CreateTable for additional information.
5558 */
5559 case OP_CreateIndex: /* out2 */
5560 case OP_CreateTable: { /* out2 */
5561 int pgno;
5562 int flags;
5563 Db *pDb;
5564
5565 pOut = out2Prerelease(p, pOp);
5566 pgno = 0;
5567 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5568 assert( DbMaskTest(p->btreeMask, pOp->p1) );
5569 assert( p->readOnly==0 );
5570 pDb = &db->aDb[pOp->p1];
5571 assert( pDb->pBt!=0 );
5572 if( pOp->opcode==OP_CreateTable ){
5573 /* flags = BTREE_INTKEY; */
5574 flags = BTREE_INTKEY;
5575 }else{
5576 flags = BTREE_BLOBKEY;
5577 }
5578 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
5579 if( rc ) goto abort_due_to_error;
5580 pOut->u.i = pgno;
5581 break;
5582 }
5583
5584 /* Opcode: ParseSchema P1 * * P4 *
5585 **
5586 ** Read and parse all entries from the SQLITE_MASTER table of database P1
5587 ** that match the WHERE clause P4.
5588 **
5589 ** This opcode invokes the parser to create a new virtual machine,
5590 ** then runs the new virtual machine. It is thus a re-entrant opcode.
5591 */
5592 case OP_ParseSchema: {
5593 int iDb;
5594 const char *zMaster;
5595 char *zSql;
5596 InitData initData;
5597
5598 /* Any prepared statement that invokes this opcode will hold mutexes
5599 ** on every btree. This is a prerequisite for invoking
5600 ** sqlite3InitCallback().
5601 */
5602 #ifdef SQLITE_DEBUG
5603 for(iDb=0; iDb<db->nDb; iDb++){
5604 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5605 }
5606 #endif
5607
5608 iDb = pOp->p1;
5609 assert( iDb>=0 && iDb<db->nDb );
5610 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
5611 /* Used to be a conditional */ {
5612 zMaster = MASTER_NAME;
5613 initData.db = db;
5614 initData.iDb = pOp->p1;
5615 initData.pzErrMsg = &p->zErrMsg;
5616 zSql = sqlite3MPrintf(db,
5617 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
5618 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
5619 if( zSql==0 ){
5620 rc = SQLITE_NOMEM_BKPT;
5621 }else{
5622 assert( db->init.busy==0 );
5623 db->init.busy = 1;
5624 initData.rc = SQLITE_OK;
5625 assert( !db->mallocFailed );
5626 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5627 if( rc==SQLITE_OK ) rc = initData.rc;
5628 sqlite3DbFree(db, zSql);
5629 db->init.busy = 0;
5630 }
5631 }
5632 if( rc ){
5633 sqlite3ResetAllSchemasOfConnection(db);
5634 if( rc==SQLITE_NOMEM ){
5635 goto no_mem;
5636 }
5637 goto abort_due_to_error;
5638 }
5639 break;
5640 }
5641
5642 #if !defined(SQLITE_OMIT_ANALYZE)
5643 /* Opcode: LoadAnalysis P1 * * * *
5644 **
5645 ** Read the sqlite_stat1 table for database P1 and load the content
5646 ** of that table into the internal index hash table. This will cause
5647 ** the analysis to be used when preparing all subsequent queries.
5648 */
5649 case OP_LoadAnalysis: {
5650 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5651 rc = sqlite3AnalysisLoad(db, pOp->p1);
5652 if( rc ) goto abort_due_to_error;
5653 break;
5654 }
5655 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
5656
5657 /* Opcode: DropTable P1 * * P4 *
5658 **
5659 ** Remove the internal (in-memory) data structures that describe
5660 ** the table named P4 in database P1. This is called after a table
5661 ** is dropped from disk (using the Destroy opcode) in order to keep
5662 ** the internal representation of the
5663 ** schema consistent with what is on disk.
5664 */
5665 case OP_DropTable: {
5666 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
5667 break;
5668 }
5669
5670 /* Opcode: DropIndex P1 * * P4 *
5671 **
5672 ** Remove the internal (in-memory) data structures that describe
5673 ** the index named P4 in database P1. This is called after an index
5674 ** is dropped from disk (using the Destroy opcode)
5675 ** in order to keep the internal representation of the
5676 ** schema consistent with what is on disk.
5677 */
5678 case OP_DropIndex: {
5679 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
5680 break;
5681 }
5682
5683 /* Opcode: DropTrigger P1 * * P4 *
5684 **
5685 ** Remove the internal (in-memory) data structures that describe
5686 ** the trigger named P4 in database P1. This is called after a trigger
5687 ** is dropped from disk (using the Destroy opcode) in order to keep
5688 ** the internal representation of the
5689 ** schema consistent with what is on disk.
5690 */
5691 case OP_DropTrigger: {
5692 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
5693 break;
5694 }
5695
5696
5697 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
5698 /* Opcode: IntegrityCk P1 P2 P3 P4 P5
5699 **
5700 ** Do an analysis of the currently open database. Store in
5701 ** register P1 the text of an error message describing any problems.
5702 ** If no problems are found, store a NULL in register P1.
5703 **
5704 ** The register P3 contains the maximum number of allowed errors.
5705 ** At most reg(P3) errors will be reported.
5706 ** In other words, the analysis stops as soon as reg(P1) errors are
5707 ** seen. Reg(P1) is updated with the number of errors remaining.
5708 **
5709 ** The root page numbers of all tables in the database are integers
5710 ** stored in P4_INTARRAY argument.
5711 **
5712 ** If P5 is not zero, the check is done on the auxiliary database
5713 ** file, not the main database file.
5714 **
5715 ** This opcode is used to implement the integrity_check pragma.
5716 */
5717 case OP_IntegrityCk: {
5718 int nRoot; /* Number of tables to check. (Number of root pages.) */
5719 int *aRoot; /* Array of rootpage numbers for tables to be checked */
5720 int nErr; /* Number of errors reported */
5721 char *z; /* Text of the error report */
5722 Mem *pnErr; /* Register keeping track of errors remaining */
5723
5724 assert( p->bIsReader );
5725 nRoot = pOp->p2;
5726 aRoot = pOp->p4.ai;
5727 assert( nRoot>0 );
5728 assert( aRoot[nRoot]==0 );
5729 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
5730 pnErr = &aMem[pOp->p3];
5731 assert( (pnErr->flags & MEM_Int)!=0 );
5732 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
5733 pIn1 = &aMem[pOp->p1];
5734 assert( pOp->p5<db->nDb );
5735 assert( DbMaskTest(p->btreeMask, pOp->p5) );
5736 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
5737 (int)pnErr->u.i, &nErr);
5738 pnErr->u.i -= nErr;
5739 sqlite3VdbeMemSetNull(pIn1);
5740 if( nErr==0 ){
5741 assert( z==0 );
5742 }else if( z==0 ){
5743 goto no_mem;
5744 }else{
5745 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
5746 }
5747 UPDATE_MAX_BLOBSIZE(pIn1);
5748 sqlite3VdbeChangeEncoding(pIn1, encoding);
5749 break;
5750 }
5751 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
5752
5753 /* Opcode: RowSetAdd P1 P2 * * *
5754 ** Synopsis: rowset(P1)=r[P2]
5755 **
5756 ** Insert the integer value held by register P2 into a boolean index
5757 ** held in register P1.
5758 **
5759 ** An assertion fails if P2 is not an integer.
5760 */
5761 case OP_RowSetAdd: { /* in1, in2 */
5762 pIn1 = &aMem[pOp->p1];
5763 pIn2 = &aMem[pOp->p2];
5764 assert( (pIn2->flags & MEM_Int)!=0 );
5765 if( (pIn1->flags & MEM_RowSet)==0 ){
5766 sqlite3VdbeMemSetRowSet(pIn1);
5767 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
5768 }
5769 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
5770 break;
5771 }
5772
5773 /* Opcode: RowSetRead P1 P2 P3 * *
5774 ** Synopsis: r[P3]=rowset(P1)
5775 **
5776 ** Extract the smallest value from boolean index P1 and put that value into
5777 ** register P3. Or, if boolean index P1 is initially empty, leave P3
5778 ** unchanged and jump to instruction P2.
5779 */
5780 case OP_RowSetRead: { /* jump, in1, out3 */
5781 i64 val;
5782
5783 pIn1 = &aMem[pOp->p1];
5784 if( (pIn1->flags & MEM_RowSet)==0
5785 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
5786 ){
5787 /* The boolean index is empty */
5788 sqlite3VdbeMemSetNull(pIn1);
5789 VdbeBranchTaken(1,2);
5790 goto jump_to_p2_and_check_for_interrupt;
5791 }else{
5792 /* A value was pulled from the index */
5793 VdbeBranchTaken(0,2);
5794 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
5795 }
5796 goto check_for_interrupt;
5797 }
5798
5799 /* Opcode: RowSetTest P1 P2 P3 P4
5800 ** Synopsis: if r[P3] in rowset(P1) goto P2
5801 **
5802 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
5803 ** contains a RowSet object and that RowSet object contains
5804 ** the value held in P3, jump to register P2. Otherwise, insert the
5805 ** integer in P3 into the RowSet and continue on to the
5806 ** next opcode.
5807 **
5808 ** The RowSet object is optimized for the case where successive sets
5809 ** of integers, where each set contains no duplicates. Each set
5810 ** of values is identified by a unique P4 value. The first set
5811 ** must have P4==0, the final set P4=-1. P4 must be either -1 or
5812 ** non-negative. For non-negative values of P4 only the lower 4
5813 ** bits are significant.
5814 **
5815 ** This allows optimizations: (a) when P4==0 there is no need to test
5816 ** the rowset object for P3, as it is guaranteed not to contain it,
5817 ** (b) when P4==-1 there is no need to insert the value, as it will
5818 ** never be tested for, and (c) when a value that is part of set X is
5819 ** inserted, there is no need to search to see if the same value was
5820 ** previously inserted as part of set X (only if it was previously
5821 ** inserted as part of some other set).
5822 */
5823 case OP_RowSetTest: { /* jump, in1, in3 */
5824 int iSet;
5825 int exists;
5826
5827 pIn1 = &aMem[pOp->p1];
5828 pIn3 = &aMem[pOp->p3];
5829 iSet = pOp->p4.i;
5830 assert( pIn3->flags&MEM_Int );
5831
5832 /* If there is anything other than a rowset object in memory cell P1,
5833 ** delete it now and initialize P1 with an empty rowset
5834 */
5835 if( (pIn1->flags & MEM_RowSet)==0 ){
5836 sqlite3VdbeMemSetRowSet(pIn1);
5837 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
5838 }
5839
5840 assert( pOp->p4type==P4_INT32 );
5841 assert( iSet==-1 || iSet>=0 );
5842 if( iSet ){
5843 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
5844 VdbeBranchTaken(exists!=0,2);
5845 if( exists ) goto jump_to_p2;
5846 }
5847 if( iSet>=0 ){
5848 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
5849 }
5850 break;
5851 }
5852
5853
5854 #ifndef SQLITE_OMIT_TRIGGER
5855
5856 /* Opcode: Program P1 P2 P3 P4 P5
5857 **
5858 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
5859 **
5860 ** P1 contains the address of the memory cell that contains the first memory
5861 ** cell in an array of values used as arguments to the sub-program. P2
5862 ** contains the address to jump to if the sub-program throws an IGNORE
5863 ** exception using the RAISE() function. Register P3 contains the address
5864 ** of a memory cell in this (the parent) VM that is used to allocate the
5865 ** memory required by the sub-vdbe at runtime.
5866 **
5867 ** P4 is a pointer to the VM containing the trigger program.
5868 **
5869 ** If P5 is non-zero, then recursive program invocation is enabled.
5870 */
5871 case OP_Program: { /* jump */
5872 int nMem; /* Number of memory registers for sub-program */
5873 int nByte; /* Bytes of runtime space required for sub-program */
5874 Mem *pRt; /* Register to allocate runtime space */
5875 Mem *pMem; /* Used to iterate through memory cells */
5876 Mem *pEnd; /* Last memory cell in new array */
5877 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5878 SubProgram *pProgram; /* Sub-program to execute */
5879 void *t; /* Token identifying trigger */
5880
5881 pProgram = pOp->p4.pProgram;
5882 pRt = &aMem[pOp->p3];
5883 assert( pProgram->nOp>0 );
5884
5885 /* If the p5 flag is clear, then recursive invocation of triggers is
5886 ** disabled for backwards compatibility (p5 is set if this sub-program
5887 ** is really a trigger, not a foreign key action, and the flag set
5888 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
5889 **
5890 ** It is recursive invocation of triggers, at the SQL level, that is
5891 ** disabled. In some cases a single trigger may generate more than one
5892 ** SubProgram (if the trigger may be executed with more than one different
5893 ** ON CONFLICT algorithm). SubProgram structures associated with a
5894 ** single trigger all have the same value for the SubProgram.token
5895 ** variable. */
5896 if( pOp->p5 ){
5897 t = pProgram->token;
5898 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5899 if( pFrame ) break;
5900 }
5901
5902 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
5903 rc = SQLITE_ERROR;
5904 sqlite3VdbeError(p, "too many levels of trigger recursion");
5905 goto abort_due_to_error;
5906 }
5907
5908 /* Register pRt is used to store the memory required to save the state
5909 ** of the current program, and the memory required at runtime to execute
5910 ** the trigger program. If this trigger has been fired before, then pRt
5911 ** is already allocated. Otherwise, it must be initialized. */
5912 if( (pRt->flags&MEM_Frame)==0 ){
5913 /* SubProgram.nMem is set to the number of memory cells used by the
5914 ** program stored in SubProgram.aOp. As well as these, one memory
5915 ** cell is required for each cursor used by the program. Set local
5916 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5917 */
5918 nMem = pProgram->nMem + pProgram->nCsr;
5919 assert( nMem>0 );
5920 if( pProgram->nCsr==0 ) nMem++;
5921 nByte = ROUND8(sizeof(VdbeFrame))
5922 + nMem * sizeof(Mem)
5923 + pProgram->nCsr * sizeof(VdbeCursor *);
5924 pFrame = sqlite3DbMallocZero(db, nByte);
5925 if( !pFrame ){
5926 goto no_mem;
5927 }
5928 sqlite3VdbeMemRelease(pRt);
5929 pRt->flags = MEM_Frame;
5930 pRt->u.pFrame = pFrame;
5931
5932 pFrame->v = p;
5933 pFrame->nChildMem = nMem;
5934 pFrame->nChildCsr = pProgram->nCsr;
5935 pFrame->pc = (int)(pOp - aOp);
5936 pFrame->aMem = p->aMem;
5937 pFrame->nMem = p->nMem;
5938 pFrame->apCsr = p->apCsr;
5939 pFrame->nCursor = p->nCursor;
5940 pFrame->aOp = p->aOp;
5941 pFrame->nOp = p->nOp;
5942 pFrame->token = pProgram->token;
5943 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
5944 pFrame->anExec = p->anExec;
5945 #endif
5946
5947 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5948 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
5949 pMem->flags = MEM_Undefined;
5950 pMem->db = db;
5951 }
5952 }else{
5953 pFrame = pRt->u.pFrame;
5954 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
5955 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
5956 assert( pProgram->nCsr==pFrame->nChildCsr );
5957 assert( (int)(pOp - aOp)==pFrame->pc );
5958 }
5959
5960 p->nFrame++;
5961 pFrame->pParent = p->pFrame;
5962 pFrame->lastRowid = db->lastRowid;
5963 pFrame->nChange = p->nChange;
5964 pFrame->nDbChange = p->db->nChange;
5965 assert( pFrame->pAuxData==0 );
5966 pFrame->pAuxData = p->pAuxData;
5967 p->pAuxData = 0;
5968 p->nChange = 0;
5969 p->pFrame = pFrame;
5970 p->aMem = aMem = VdbeFrameMem(pFrame);
5971 p->nMem = pFrame->nChildMem;
5972 p->nCursor = (u16)pFrame->nChildCsr;
5973 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
5974 p->aOp = aOp = pProgram->aOp;
5975 p->nOp = pProgram->nOp;
5976 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
5977 p->anExec = 0;
5978 #endif
5979 pOp = &aOp[-1];
5980
5981 break;
5982 }
5983
5984 /* Opcode: Param P1 P2 * * *
5985 **
5986 ** This opcode is only ever present in sub-programs called via the
5987 ** OP_Program instruction. Copy a value currently stored in a memory
5988 ** cell of the calling (parent) frame to cell P2 in the current frames
5989 ** address space. This is used by trigger programs to access the new.*
5990 ** and old.* values.
5991 **
5992 ** The address of the cell in the parent frame is determined by adding
5993 ** the value of the P1 argument to the value of the P1 argument to the
5994 ** calling OP_Program instruction.
5995 */
5996 case OP_Param: { /* out2 */
5997 VdbeFrame *pFrame;
5998 Mem *pIn;
5999 pOut = out2Prerelease(p, pOp);
6000 pFrame = p->pFrame;
6001 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
6002 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
6003 break;
6004 }
6005
6006 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
6007
6008 #ifndef SQLITE_OMIT_FOREIGN_KEY
6009 /* Opcode: FkCounter P1 P2 * * *
6010 ** Synopsis: fkctr[P1]+=P2
6011 **
6012 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
6013 ** If P1 is non-zero, the database constraint counter is incremented
6014 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
6015 ** statement counter is incremented (immediate foreign key constraints).
6016 */
6017 case OP_FkCounter: {
6018 if( db->flags & SQLITE_DeferFKs ){
6019 db->nDeferredImmCons += pOp->p2;
6020 }else if( pOp->p1 ){
6021 db->nDeferredCons += pOp->p2;
6022 }else{
6023 p->nFkConstraint += pOp->p2;
6024 }
6025 break;
6026 }
6027
6028 /* Opcode: FkIfZero P1 P2 * * *
6029 ** Synopsis: if fkctr[P1]==0 goto P2
6030 **
6031 ** This opcode tests if a foreign key constraint-counter is currently zero.
6032 ** If so, jump to instruction P2. Otherwise, fall through to the next
6033 ** instruction.
6034 **
6035 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
6036 ** is zero (the one that counts deferred constraint violations). If P1 is
6037 ** zero, the jump is taken if the statement constraint-counter is zero
6038 ** (immediate foreign key constraint violations).
6039 */
6040 case OP_FkIfZero: { /* jump */
6041 if( pOp->p1 ){
6042 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
6043 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
6044 }else{
6045 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
6046 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
6047 }
6048 break;
6049 }
6050 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6051
6052 #ifndef SQLITE_OMIT_AUTOINCREMENT
6053 /* Opcode: MemMax P1 P2 * * *
6054 ** Synopsis: r[P1]=max(r[P1],r[P2])
6055 **
6056 ** P1 is a register in the root frame of this VM (the root frame is
6057 ** different from the current frame if this instruction is being executed
6058 ** within a sub-program). Set the value of register P1 to the maximum of
6059 ** its current value and the value in register P2.
6060 **
6061 ** This instruction throws an error if the memory cell is not initially
6062 ** an integer.
6063 */
6064 case OP_MemMax: { /* in2 */
6065 VdbeFrame *pFrame;
6066 if( p->pFrame ){
6067 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6068 pIn1 = &pFrame->aMem[pOp->p1];
6069 }else{
6070 pIn1 = &aMem[pOp->p1];
6071 }
6072 assert( memIsValid(pIn1) );
6073 sqlite3VdbeMemIntegerify(pIn1);
6074 pIn2 = &aMem[pOp->p2];
6075 sqlite3VdbeMemIntegerify(pIn2);
6076 if( pIn1->u.i<pIn2->u.i){
6077 pIn1->u.i = pIn2->u.i;
6078 }
6079 break;
6080 }
6081 #endif /* SQLITE_OMIT_AUTOINCREMENT */
6082
6083 /* Opcode: IfPos P1 P2 P3 * *
6084 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
6085 **
6086 ** Register P1 must contain an integer.
6087 ** If the value of register P1 is 1 or greater, subtract P3 from the
6088 ** value in P1 and jump to P2.
6089 **
6090 ** If the initial value of register P1 is less than 1, then the
6091 ** value is unchanged and control passes through to the next instruction.
6092 */
6093 case OP_IfPos: { /* jump, in1 */
6094 pIn1 = &aMem[pOp->p1];
6095 assert( pIn1->flags&MEM_Int );
6096 VdbeBranchTaken( pIn1->u.i>0, 2);
6097 if( pIn1->u.i>0 ){
6098 pIn1->u.i -= pOp->p3;
6099 goto jump_to_p2;
6100 }
6101 break;
6102 }
6103
6104 /* Opcode: OffsetLimit P1 P2 P3 * *
6105 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
6106 **
6107 ** This opcode performs a commonly used computation associated with
6108 ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6109 ** holds the offset counter. The opcode computes the combined value
6110 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6111 ** value computed is the total number of rows that will need to be
6112 ** visited in order to complete the query.
6113 **
6114 ** If r[P3] is zero or negative, that means there is no OFFSET
6115 ** and r[P2] is set to be the value of the LIMIT, r[P1].
6116 **
6117 ** if r[P1] is zero or negative, that means there is no LIMIT
6118 ** and r[P2] is set to -1.
6119 **
6120 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
6121 */
6122 case OP_OffsetLimit: { /* in1, out2, in3 */
6123 i64 x;
6124 pIn1 = &aMem[pOp->p1];
6125 pIn3 = &aMem[pOp->p3];
6126 pOut = out2Prerelease(p, pOp);
6127 assert( pIn1->flags & MEM_Int );
6128 assert( pIn3->flags & MEM_Int );
6129 x = pIn1->u.i;
6130 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6131 /* If the LIMIT is less than or equal to zero, loop forever. This
6132 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6133 ** also loop forever. This is undocumented. In fact, one could argue
6134 ** that the loop should terminate. But assuming 1 billion iterations
6135 ** per second (far exceeding the capabilities of any current hardware)
6136 ** it would take nearly 300 years to actually reach the limit. So
6137 ** looping forever is a reasonable approximation. */
6138 pOut->u.i = -1;
6139 }else{
6140 pOut->u.i = x;
6141 }
6142 break;
6143 }
6144
6145 /* Opcode: IfNotZero P1 P2 * * *
6146 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
6147 **
6148 ** Register P1 must contain an integer. If the content of register P1 is
6149 ** initially greater than zero, then decrement the value in register P1.
6150 ** If it is non-zero (negative or positive) and then also jump to P2.
6151 ** If register P1 is initially zero, leave it unchanged and fall through.
6152 */
6153 case OP_IfNotZero: { /* jump, in1 */
6154 pIn1 = &aMem[pOp->p1];
6155 assert( pIn1->flags&MEM_Int );
6156 VdbeBranchTaken(pIn1->u.i<0, 2);
6157 if( pIn1->u.i ){
6158 if( pIn1->u.i>0 ) pIn1->u.i--;
6159 goto jump_to_p2;
6160 }
6161 break;
6162 }
6163
6164 /* Opcode: DecrJumpZero P1 P2 * * *
6165 ** Synopsis: if (--r[P1])==0 goto P2
6166 **
6167 ** Register P1 must hold an integer. Decrement the value in P1
6168 ** and jump to P2 if the new value is exactly zero.
6169 */
6170 case OP_DecrJumpZero: { /* jump, in1 */
6171 pIn1 = &aMem[pOp->p1];
6172 assert( pIn1->flags&MEM_Int );
6173 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6174 VdbeBranchTaken(pIn1->u.i==0, 2);
6175 if( pIn1->u.i==0 ) goto jump_to_p2;
6176 break;
6177 }
6178
6179
6180 /* Opcode: AggStep0 * P2 P3 P4 P5
6181 ** Synopsis: accum=r[P3] step(r[P2@P5])
6182 **
6183 ** Execute the step function for an aggregate. The
6184 ** function has P5 arguments. P4 is a pointer to the FuncDef
6185 ** structure that specifies the function. Register P3 is the
6186 ** accumulator.
6187 **
6188 ** The P5 arguments are taken from register P2 and its
6189 ** successors.
6190 */
6191 /* Opcode: AggStep * P2 P3 P4 P5
6192 ** Synopsis: accum=r[P3] step(r[P2@P5])
6193 **
6194 ** Execute the step function for an aggregate. The
6195 ** function has P5 arguments. P4 is a pointer to an sqlite3_context
6196 ** object that is used to run the function. Register P3 is
6197 ** as the accumulator.
6198 **
6199 ** The P5 arguments are taken from register P2 and its
6200 ** successors.
6201 **
6202 ** This opcode is initially coded as OP_AggStep0. On first evaluation,
6203 ** the FuncDef stored in P4 is converted into an sqlite3_context and
6204 ** the opcode is changed. In this way, the initialization of the
6205 ** sqlite3_context only happens once, instead of on each call to the
6206 ** step function.
6207 */
6208 case OP_AggStep0: {
6209 int n;
6210 sqlite3_context *pCtx;
6211
6212 assert( pOp->p4type==P4_FUNCDEF );
6213 n = pOp->p5;
6214 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6215 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
6216 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
6217 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
6218 if( pCtx==0 ) goto no_mem;
6219 pCtx->pMem = 0;
6220 pCtx->pFunc = pOp->p4.pFunc;
6221 pCtx->iOp = (int)(pOp - aOp);
6222 pCtx->pVdbe = p;
6223 pCtx->argc = n;
6224 pOp->p4type = P4_FUNCCTX;
6225 pOp->p4.pCtx = pCtx;
6226 pOp->opcode = OP_AggStep;
6227 /* Fall through into OP_AggStep */
6228 }
6229 case OP_AggStep: {
6230 int i;
6231 sqlite3_context *pCtx;
6232 Mem *pMem;
6233 Mem t;
6234
6235 assert( pOp->p4type==P4_FUNCCTX );
6236 pCtx = pOp->p4.pCtx;
6237 pMem = &aMem[pOp->p3];
6238
6239 /* If this function is inside of a trigger, the register array in aMem[]
6240 ** might change from one evaluation to the next. The next block of code
6241 ** checks to see if the register array has changed, and if so it
6242 ** reinitializes the relavant parts of the sqlite3_context object */
6243 if( pCtx->pMem != pMem ){
6244 pCtx->pMem = pMem;
6245 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
6246 }
6247
6248 #ifdef SQLITE_DEBUG
6249 for(i=0; i<pCtx->argc; i++){
6250 assert( memIsValid(pCtx->argv[i]) );
6251 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
6252 }
6253 #endif
6254
6255 pMem->n++;
6256 sqlite3VdbeMemInit(&t, db, MEM_Null);
6257 pCtx->pOut = &t;
6258 pCtx->fErrorOrAux = 0;
6259 pCtx->skipFlag = 0;
6260 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
6261 if( pCtx->fErrorOrAux ){
6262 if( pCtx->isError ){
6263 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
6264 rc = pCtx->isError;
6265 }
6266 sqlite3VdbeMemRelease(&t);
6267 if( rc ) goto abort_due_to_error;
6268 }else{
6269 assert( t.flags==MEM_Null );
6270 }
6271 if( pCtx->skipFlag ){
6272 assert( pOp[-1].opcode==OP_CollSeq );
6273 i = pOp[-1].p1;
6274 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6275 }
6276 break;
6277 }
6278
6279 /* Opcode: AggFinal P1 P2 * P4 *
6280 ** Synopsis: accum=r[P1] N=P2
6281 **
6282 ** Execute the finalizer function for an aggregate. P1 is
6283 ** the memory location that is the accumulator for the aggregate.
6284 **
6285 ** P2 is the number of arguments that the step function takes and
6286 ** P4 is a pointer to the FuncDef for this function. The P2
6287 ** argument is not used by this opcode. It is only there to disambiguate
6288 ** functions that can take varying numbers of arguments. The
6289 ** P4 argument is only needed for the degenerate case where
6290 ** the step function was not previously called.
6291 */
6292 case OP_AggFinal: {
6293 Mem *pMem;
6294 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
6295 pMem = &aMem[pOp->p1];
6296 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
6297 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
6298 if( rc ){
6299 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
6300 goto abort_due_to_error;
6301 }
6302 sqlite3VdbeChangeEncoding(pMem, encoding);
6303 UPDATE_MAX_BLOBSIZE(pMem);
6304 if( sqlite3VdbeMemTooBig(pMem) ){
6305 goto too_big;
6306 }
6307 break;
6308 }
6309
6310 #ifndef SQLITE_OMIT_WAL
6311 /* Opcode: Checkpoint P1 P2 P3 * *
6312 **
6313 ** Checkpoint database P1. This is a no-op if P1 is not currently in
6314 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6315 ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
6316 ** SQLITE_BUSY or not, respectively. Write the number of pages in the
6317 ** WAL after the checkpoint into mem[P3+1] and the number of pages
6318 ** in the WAL that have been checkpointed after the checkpoint
6319 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
6320 ** mem[P3+2] are initialized to -1.
6321 */
6322 case OP_Checkpoint: {
6323 int i; /* Loop counter */
6324 int aRes[3]; /* Results */
6325 Mem *pMem; /* Write results here */
6326
6327 assert( p->readOnly==0 );
6328 aRes[0] = 0;
6329 aRes[1] = aRes[2] = -1;
6330 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6331 || pOp->p2==SQLITE_CHECKPOINT_FULL
6332 || pOp->p2==SQLITE_CHECKPOINT_RESTART
6333 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
6334 );
6335 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
6336 if( rc ){
6337 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
6338 rc = SQLITE_OK;
6339 aRes[0] = 1;
6340 }
6341 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6342 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6343 }
6344 break;
6345 };
6346 #endif
6347
6348 #ifndef SQLITE_OMIT_PRAGMA
6349 /* Opcode: JournalMode P1 P2 P3 * *
6350 **
6351 ** Change the journal mode of database P1 to P3. P3 must be one of the
6352 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6353 ** modes (delete, truncate, persist, off and memory), this is a simple
6354 ** operation. No IO is required.
6355 **
6356 ** If changing into or out of WAL mode the procedure is more complicated.
6357 **
6358 ** Write a string containing the final journal-mode to register P2.
6359 */
6360 case OP_JournalMode: { /* out2 */
6361 Btree *pBt; /* Btree to change journal mode of */
6362 Pager *pPager; /* Pager associated with pBt */
6363 int eNew; /* New journal mode */
6364 int eOld; /* The old journal mode */
6365 #ifndef SQLITE_OMIT_WAL
6366 const char *zFilename; /* Name of database file for pPager */
6367 #endif
6368
6369 pOut = out2Prerelease(p, pOp);
6370 eNew = pOp->p3;
6371 assert( eNew==PAGER_JOURNALMODE_DELETE
6372 || eNew==PAGER_JOURNALMODE_TRUNCATE
6373 || eNew==PAGER_JOURNALMODE_PERSIST
6374 || eNew==PAGER_JOURNALMODE_OFF
6375 || eNew==PAGER_JOURNALMODE_MEMORY
6376 || eNew==PAGER_JOURNALMODE_WAL
6377 || eNew==PAGER_JOURNALMODE_QUERY
6378 );
6379 assert( pOp->p1>=0 && pOp->p1<db->nDb );
6380 assert( p->readOnly==0 );
6381
6382 pBt = db->aDb[pOp->p1].pBt;
6383 pPager = sqlite3BtreePager(pBt);
6384 eOld = sqlite3PagerGetJournalMode(pPager);
6385 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6386 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
6387
6388 #ifndef SQLITE_OMIT_WAL
6389 zFilename = sqlite3PagerFilename(pPager, 1);
6390
6391 /* Do not allow a transition to journal_mode=WAL for a database
6392 ** in temporary storage or if the VFS does not support shared memory
6393 */
6394 if( eNew==PAGER_JOURNALMODE_WAL
6395 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
6396 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
6397 ){
6398 eNew = eOld;
6399 }
6400
6401 if( (eNew!=eOld)
6402 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6403 ){
6404 if( !db->autoCommit || db->nVdbeRead>1 ){
6405 rc = SQLITE_ERROR;
6406 sqlite3VdbeError(p,
6407 "cannot change %s wal mode from within a transaction",
6408 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6409 );
6410 goto abort_due_to_error;
6411 }else{
6412
6413 if( eOld==PAGER_JOURNALMODE_WAL ){
6414 /* If leaving WAL mode, close the log file. If successful, the call
6415 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6416 ** file. An EXCLUSIVE lock may still be held on the database file
6417 ** after a successful return.
6418 */
6419 rc = sqlite3PagerCloseWal(pPager, db);
6420 if( rc==SQLITE_OK ){
6421 sqlite3PagerSetJournalMode(pPager, eNew);
6422 }
6423 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6424 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6425 ** as an intermediate */
6426 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
6427 }
6428
6429 /* Open a transaction on the database file. Regardless of the journal
6430 ** mode, this transaction always uses a rollback journal.
6431 */
6432 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6433 if( rc==SQLITE_OK ){
6434 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
6435 }
6436 }
6437 }
6438 #endif /* ifndef SQLITE_OMIT_WAL */
6439
6440 if( rc ) eNew = eOld;
6441 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
6442
6443 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
6444 pOut->z = (char *)sqlite3JournalModename(eNew);
6445 pOut->n = sqlite3Strlen30(pOut->z);
6446 pOut->enc = SQLITE_UTF8;
6447 sqlite3VdbeChangeEncoding(pOut, encoding);
6448 if( rc ) goto abort_due_to_error;
6449 break;
6450 };
6451 #endif /* SQLITE_OMIT_PRAGMA */
6452
6453 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
6454 /* Opcode: Vacuum P1 * * * *
6455 **
6456 ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
6457 ** for an attached database. The "temp" database may not be vacuumed.
6458 */
6459 case OP_Vacuum: {
6460 assert( p->readOnly==0 );
6461 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
6462 if( rc ) goto abort_due_to_error;
6463 break;
6464 }
6465 #endif
6466
6467 #if !defined(SQLITE_OMIT_AUTOVACUUM)
6468 /* Opcode: IncrVacuum P1 P2 * * *
6469 **
6470 ** Perform a single step of the incremental vacuum procedure on
6471 ** the P1 database. If the vacuum has finished, jump to instruction
6472 ** P2. Otherwise, fall through to the next instruction.
6473 */
6474 case OP_IncrVacuum: { /* jump */
6475 Btree *pBt;
6476
6477 assert( pOp->p1>=0 && pOp->p1<db->nDb );
6478 assert( DbMaskTest(p->btreeMask, pOp->p1) );
6479 assert( p->readOnly==0 );
6480 pBt = db->aDb[pOp->p1].pBt;
6481 rc = sqlite3BtreeIncrVacuum(pBt);
6482 VdbeBranchTaken(rc==SQLITE_DONE,2);
6483 if( rc ){
6484 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
6485 rc = SQLITE_OK;
6486 goto jump_to_p2;
6487 }
6488 break;
6489 }
6490 #endif
6491
6492 /* Opcode: Expire P1 * * * *
6493 **
6494 ** Cause precompiled statements to expire. When an expired statement
6495 ** is executed using sqlite3_step() it will either automatically
6496 ** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6497 ** or it will fail with SQLITE_SCHEMA.
6498 **
6499 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
6500 ** then only the currently executing statement is expired.
6501 */
6502 case OP_Expire: {
6503 if( !pOp->p1 ){
6504 sqlite3ExpirePreparedStatements(db);
6505 }else{
6506 p->expired = 1;
6507 }
6508 break;
6509 }
6510
6511 #ifndef SQLITE_OMIT_SHARED_CACHE
6512 /* Opcode: TableLock P1 P2 P3 P4 *
6513 ** Synopsis: iDb=P1 root=P2 write=P3
6514 **
6515 ** Obtain a lock on a particular table. This instruction is only used when
6516 ** the shared-cache feature is enabled.
6517 **
6518 ** P1 is the index of the database in sqlite3.aDb[] of the database
6519 ** on which the lock is acquired. A readlock is obtained if P3==0 or
6520 ** a write lock if P3==1.
6521 **
6522 ** P2 contains the root-page of the table to lock.
6523 **
6524 ** P4 contains a pointer to the name of the table being locked. This is only
6525 ** used to generate an error message if the lock cannot be obtained.
6526 */
6527 case OP_TableLock: {
6528 u8 isWriteLock = (u8)pOp->p3;
6529 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
6530 int p1 = pOp->p1;
6531 assert( p1>=0 && p1<db->nDb );
6532 assert( DbMaskTest(p->btreeMask, p1) );
6533 assert( isWriteLock==0 || isWriteLock==1 );
6534 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
6535 if( rc ){
6536 if( (rc&0xFF)==SQLITE_LOCKED ){
6537 const char *z = pOp->p4.z;
6538 sqlite3VdbeError(p, "database table is locked: %s", z);
6539 }
6540 goto abort_due_to_error;
6541 }
6542 }
6543 break;
6544 }
6545 #endif /* SQLITE_OMIT_SHARED_CACHE */
6546
6547 #ifndef SQLITE_OMIT_VIRTUALTABLE
6548 /* Opcode: VBegin * * * P4 *
6549 **
6550 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6551 ** xBegin method for that table.
6552 **
6553 ** Also, whether or not P4 is set, check that this is not being called from
6554 ** within a callback to a virtual table xSync() method. If it is, the error
6555 ** code will be set to SQLITE_LOCKED.
6556 */
6557 case OP_VBegin: {
6558 VTable *pVTab;
6559 pVTab = pOp->p4.pVtab;
6560 rc = sqlite3VtabBegin(db, pVTab);
6561 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
6562 if( rc ) goto abort_due_to_error;
6563 break;
6564 }
6565 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6566
6567 #ifndef SQLITE_OMIT_VIRTUALTABLE
6568 /* Opcode: VCreate P1 P2 * * *
6569 **
6570 ** P2 is a register that holds the name of a virtual table in database
6571 ** P1. Call the xCreate method for that table.
6572 */
6573 case OP_VCreate: {
6574 Mem sMem; /* For storing the record being decoded */
6575 const char *zTab; /* Name of the virtual table */
6576
6577 memset(&sMem, 0, sizeof(sMem));
6578 sMem.db = db;
6579 /* Because P2 is always a static string, it is impossible for the
6580 ** sqlite3VdbeMemCopy() to fail */
6581 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6582 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
6583 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
6584 assert( rc==SQLITE_OK );
6585 zTab = (const char*)sqlite3_value_text(&sMem);
6586 assert( zTab || db->mallocFailed );
6587 if( zTab ){
6588 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
6589 }
6590 sqlite3VdbeMemRelease(&sMem);
6591 if( rc ) goto abort_due_to_error;
6592 break;
6593 }
6594 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6595
6596 #ifndef SQLITE_OMIT_VIRTUALTABLE
6597 /* Opcode: VDestroy P1 * * P4 *
6598 **
6599 ** P4 is the name of a virtual table in database P1. Call the xDestroy method
6600 ** of that table.
6601 */
6602 case OP_VDestroy: {
6603 db->nVDestroy++;
6604 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
6605 db->nVDestroy--;
6606 if( rc ) goto abort_due_to_error;
6607 break;
6608 }
6609 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6610
6611 #ifndef SQLITE_OMIT_VIRTUALTABLE
6612 /* Opcode: VOpen P1 * * P4 *
6613 **
6614 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6615 ** P1 is a cursor number. This opcode opens a cursor to the virtual
6616 ** table and stores that cursor in P1.
6617 */
6618 case OP_VOpen: {
6619 VdbeCursor *pCur;
6620 sqlite3_vtab_cursor *pVCur;
6621 sqlite3_vtab *pVtab;
6622 const sqlite3_module *pModule;
6623
6624 assert( p->bIsReader );
6625 pCur = 0;
6626 pVCur = 0;
6627 pVtab = pOp->p4.pVtab->pVtab;
6628 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6629 rc = SQLITE_LOCKED;
6630 goto abort_due_to_error;
6631 }
6632 pModule = pVtab->pModule;
6633 rc = pModule->xOpen(pVtab, &pVCur);
6634 sqlite3VtabImportErrmsg(p, pVtab);
6635 if( rc ) goto abort_due_to_error;
6636
6637 /* Initialize sqlite3_vtab_cursor base class */
6638 pVCur->pVtab = pVtab;
6639
6640 /* Initialize vdbe cursor object */
6641 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
6642 if( pCur ){
6643 pCur->uc.pVCur = pVCur;
6644 pVtab->nRef++;
6645 }else{
6646 assert( db->mallocFailed );
6647 pModule->xClose(pVCur);
6648 goto no_mem;
6649 }
6650 break;
6651 }
6652 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6653
6654 #ifndef SQLITE_OMIT_VIRTUALTABLE
6655 /* Opcode: VFilter P1 P2 P3 P4 *
6656 ** Synopsis: iplan=r[P3] zplan='P4'
6657 **
6658 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6659 ** the filtered result set is empty.
6660 **
6661 ** P4 is either NULL or a string that was generated by the xBestIndex
6662 ** method of the module. The interpretation of the P4 string is left
6663 ** to the module implementation.
6664 **
6665 ** This opcode invokes the xFilter method on the virtual table specified
6666 ** by P1. The integer query plan parameter to xFilter is stored in register
6667 ** P3. Register P3+1 stores the argc parameter to be passed to the
6668 ** xFilter method. Registers P3+2..P3+1+argc are the argc
6669 ** additional parameters which are passed to
6670 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
6671 **
6672 ** A jump is made to P2 if the result set after filtering would be empty.
6673 */
6674 case OP_VFilter: { /* jump */
6675 int nArg;
6676 int iQuery;
6677 const sqlite3_module *pModule;
6678 Mem *pQuery;
6679 Mem *pArgc;
6680 sqlite3_vtab_cursor *pVCur;
6681 sqlite3_vtab *pVtab;
6682 VdbeCursor *pCur;
6683 int res;
6684 int i;
6685 Mem **apArg;
6686
6687 pQuery = &aMem[pOp->p3];
6688 pArgc = &pQuery[1];
6689 pCur = p->apCsr[pOp->p1];
6690 assert( memIsValid(pQuery) );
6691 REGISTER_TRACE(pOp->p3, pQuery);
6692 assert( pCur->eCurType==CURTYPE_VTAB );
6693 pVCur = pCur->uc.pVCur;
6694 pVtab = pVCur->pVtab;
6695 pModule = pVtab->pModule;
6696
6697 /* Grab the index number and argc parameters */
6698 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
6699 nArg = (int)pArgc->u.i;
6700 iQuery = (int)pQuery->u.i;
6701
6702 /* Invoke the xFilter method */
6703 res = 0;
6704 apArg = p->apArg;
6705 for(i = 0; i<nArg; i++){
6706 apArg[i] = &pArgc[i+1];
6707 }
6708 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
6709 sqlite3VtabImportErrmsg(p, pVtab);
6710 if( rc ) goto abort_due_to_error;
6711 res = pModule->xEof(pVCur);
6712 pCur->nullRow = 0;
6713 VdbeBranchTaken(res!=0,2);
6714 if( res ) goto jump_to_p2;
6715 break;
6716 }
6717 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6718
6719 #ifndef SQLITE_OMIT_VIRTUALTABLE
6720 /* Opcode: VColumn P1 P2 P3 * *
6721 ** Synopsis: r[P3]=vcolumn(P2)
6722 **
6723 ** Store the value of the P2-th column of
6724 ** the row of the virtual-table that the
6725 ** P1 cursor is pointing to into register P3.
6726 */
6727 case OP_VColumn: {
6728 sqlite3_vtab *pVtab;
6729 const sqlite3_module *pModule;
6730 Mem *pDest;
6731 sqlite3_context sContext;
6732
6733 VdbeCursor *pCur = p->apCsr[pOp->p1];
6734 assert( pCur->eCurType==CURTYPE_VTAB );
6735 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6736 pDest = &aMem[pOp->p3];
6737 memAboutToChange(p, pDest);
6738 if( pCur->nullRow ){
6739 sqlite3VdbeMemSetNull(pDest);
6740 break;
6741 }
6742 pVtab = pCur->uc.pVCur->pVtab;
6743 pModule = pVtab->pModule;
6744 assert( pModule->xColumn );
6745 memset(&sContext, 0, sizeof(sContext));
6746 sContext.pOut = pDest;
6747 MemSetTypeFlag(pDest, MEM_Null);
6748 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
6749 sqlite3VtabImportErrmsg(p, pVtab);
6750 if( sContext.isError ){
6751 rc = sContext.isError;
6752 }
6753 sqlite3VdbeChangeEncoding(pDest, encoding);
6754 REGISTER_TRACE(pOp->p3, pDest);
6755 UPDATE_MAX_BLOBSIZE(pDest);
6756
6757 if( sqlite3VdbeMemTooBig(pDest) ){
6758 goto too_big;
6759 }
6760 if( rc ) goto abort_due_to_error;
6761 break;
6762 }
6763 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6764
6765 #ifndef SQLITE_OMIT_VIRTUALTABLE
6766 /* Opcode: VNext P1 P2 * * *
6767 **
6768 ** Advance virtual table P1 to the next row in its result set and
6769 ** jump to instruction P2. Or, if the virtual table has reached
6770 ** the end of its result set, then fall through to the next instruction.
6771 */
6772 case OP_VNext: { /* jump */
6773 sqlite3_vtab *pVtab;
6774 const sqlite3_module *pModule;
6775 int res;
6776 VdbeCursor *pCur;
6777
6778 res = 0;
6779 pCur = p->apCsr[pOp->p1];
6780 assert( pCur->eCurType==CURTYPE_VTAB );
6781 if( pCur->nullRow ){
6782 break;
6783 }
6784 pVtab = pCur->uc.pVCur->pVtab;
6785 pModule = pVtab->pModule;
6786 assert( pModule->xNext );
6787
6788 /* Invoke the xNext() method of the module. There is no way for the
6789 ** underlying implementation to return an error if one occurs during
6790 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6791 ** data is available) and the error code returned when xColumn or
6792 ** some other method is next invoked on the save virtual table cursor.
6793 */
6794 rc = pModule->xNext(pCur->uc.pVCur);
6795 sqlite3VtabImportErrmsg(p, pVtab);
6796 if( rc ) goto abort_due_to_error;
6797 res = pModule->xEof(pCur->uc.pVCur);
6798 VdbeBranchTaken(!res,2);
6799 if( !res ){
6800 /* If there is data, jump to P2 */
6801 goto jump_to_p2_and_check_for_interrupt;
6802 }
6803 goto check_for_interrupt;
6804 }
6805 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6806
6807 #ifndef SQLITE_OMIT_VIRTUALTABLE
6808 /* Opcode: VRename P1 * * P4 *
6809 **
6810 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6811 ** This opcode invokes the corresponding xRename method. The value
6812 ** in register P1 is passed as the zName argument to the xRename method.
6813 */
6814 case OP_VRename: {
6815 sqlite3_vtab *pVtab;
6816 Mem *pName;
6817
6818 pVtab = pOp->p4.pVtab->pVtab;
6819 pName = &aMem[pOp->p1];
6820 assert( pVtab->pModule->xRename );
6821 assert( memIsValid(pName) );
6822 assert( p->readOnly==0 );
6823 REGISTER_TRACE(pOp->p1, pName);
6824 assert( pName->flags & MEM_Str );
6825 testcase( pName->enc==SQLITE_UTF8 );
6826 testcase( pName->enc==SQLITE_UTF16BE );
6827 testcase( pName->enc==SQLITE_UTF16LE );
6828 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
6829 if( rc ) goto abort_due_to_error;
6830 rc = pVtab->pModule->xRename(pVtab, pName->z);
6831 sqlite3VtabImportErrmsg(p, pVtab);
6832 p->expired = 0;
6833 if( rc ) goto abort_due_to_error;
6834 break;
6835 }
6836 #endif
6837
6838 #ifndef SQLITE_OMIT_VIRTUALTABLE
6839 /* Opcode: VUpdate P1 P2 P3 P4 P5
6840 ** Synopsis: data=r[P3@P2]
6841 **
6842 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6843 ** This opcode invokes the corresponding xUpdate method. P2 values
6844 ** are contiguous memory cells starting at P3 to pass to the xUpdate
6845 ** invocation. The value in register (P3+P2-1) corresponds to the
6846 ** p2th element of the argv array passed to xUpdate.
6847 **
6848 ** The xUpdate method will do a DELETE or an INSERT or both.
6849 ** The argv[0] element (which corresponds to memory cell P3)
6850 ** is the rowid of a row to delete. If argv[0] is NULL then no
6851 ** deletion occurs. The argv[1] element is the rowid of the new
6852 ** row. This can be NULL to have the virtual table select the new
6853 ** rowid for itself. The subsequent elements in the array are
6854 ** the values of columns in the new row.
6855 **
6856 ** If P2==1 then no insert is performed. argv[0] is the rowid of
6857 ** a row to delete.
6858 **
6859 ** P1 is a boolean flag. If it is set to true and the xUpdate call
6860 ** is successful, then the value returned by sqlite3_last_insert_rowid()
6861 ** is set to the value of the rowid for the row just inserted.
6862 **
6863 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
6864 ** apply in the case of a constraint failure on an insert or update.
6865 */
6866 case OP_VUpdate: {
6867 sqlite3_vtab *pVtab;
6868 const sqlite3_module *pModule;
6869 int nArg;
6870 int i;
6871 sqlite_int64 rowid;
6872 Mem **apArg;
6873 Mem *pX;
6874
6875 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6876 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6877 );
6878 assert( p->readOnly==0 );
6879 pVtab = pOp->p4.pVtab->pVtab;
6880 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6881 rc = SQLITE_LOCKED;
6882 goto abort_due_to_error;
6883 }
6884 pModule = pVtab->pModule;
6885 nArg = pOp->p2;
6886 assert( pOp->p4type==P4_VTAB );
6887 if( ALWAYS(pModule->xUpdate) ){
6888 u8 vtabOnConflict = db->vtabOnConflict;
6889 apArg = p->apArg;
6890 pX = &aMem[pOp->p3];
6891 for(i=0; i<nArg; i++){
6892 assert( memIsValid(pX) );
6893 memAboutToChange(p, pX);
6894 apArg[i] = pX;
6895 pX++;
6896 }
6897 db->vtabOnConflict = pOp->p5;
6898 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
6899 db->vtabOnConflict = vtabOnConflict;
6900 sqlite3VtabImportErrmsg(p, pVtab);
6901 if( rc==SQLITE_OK && pOp->p1 ){
6902 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
6903 db->lastRowid = rowid;
6904 }
6905 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
6906 if( pOp->p5==OE_Ignore ){
6907 rc = SQLITE_OK;
6908 }else{
6909 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6910 }
6911 }else{
6912 p->nChange++;
6913 }
6914 if( rc ) goto abort_due_to_error;
6915 }
6916 break;
6917 }
6918 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6919
6920 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
6921 /* Opcode: Pagecount P1 P2 * * *
6922 **
6923 ** Write the current number of pages in database P1 to memory cell P2.
6924 */
6925 case OP_Pagecount: { /* out2 */
6926 pOut = out2Prerelease(p, pOp);
6927 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
6928 break;
6929 }
6930 #endif
6931
6932
6933 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
6934 /* Opcode: MaxPgcnt P1 P2 P3 * *
6935 **
6936 ** Try to set the maximum page count for database P1 to the value in P3.
6937 ** Do not let the maximum page count fall below the current page count and
6938 ** do not change the maximum page count value if P3==0.
6939 **
6940 ** Store the maximum page count after the change in register P2.
6941 */
6942 case OP_MaxPgcnt: { /* out2 */
6943 unsigned int newMax;
6944 Btree *pBt;
6945
6946 pOut = out2Prerelease(p, pOp);
6947 pBt = db->aDb[pOp->p1].pBt;
6948 newMax = 0;
6949 if( pOp->p3 ){
6950 newMax = sqlite3BtreeLastPage(pBt);
6951 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
6952 }
6953 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
6954 break;
6955 }
6956 #endif
6957
6958
6959 /* Opcode: Init P1 P2 * P4 *
6960 ** Synopsis: Start at P2
6961 **
6962 ** Programs contain a single instance of this opcode as the very first
6963 ** opcode.
6964 **
6965 ** If tracing is enabled (by the sqlite3_trace()) interface, then
6966 ** the UTF-8 string contained in P4 is emitted on the trace callback.
6967 ** Or if P4 is blank, use the string returned by sqlite3_sql().
6968 **
6969 ** If P2 is not zero, jump to instruction P2.
6970 **
6971 ** Increment the value of P1 so that OP_Once opcodes will jump the
6972 ** first time they are evaluated for this run.
6973 */
6974 case OP_Init: { /* jump */
6975 char *zTrace;
6976 int i;
6977
6978 /* If the P4 argument is not NULL, then it must be an SQL comment string.
6979 ** The "--" string is broken up to prevent false-positives with srcck1.c.
6980 **
6981 ** This assert() provides evidence for:
6982 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
6983 ** would have been returned by the legacy sqlite3_trace() interface by
6984 ** using the X argument when X begins with "--" and invoking
6985 ** sqlite3_expanded_sql(P) otherwise.
6986 */
6987 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
6988 assert( pOp==p->aOp ); /* Always instruction 0 */
6989
6990 #ifndef SQLITE_OMIT_TRACE
6991 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
6992 && !p->doingRerun
6993 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6994 ){
6995 #ifndef SQLITE_OMIT_DEPRECATED
6996 if( db->mTrace & SQLITE_TRACE_LEGACY ){
6997 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
6998 char *z = sqlite3VdbeExpandSql(p, zTrace);
6999 x(db->pTraceArg, z);
7000 sqlite3_free(z);
7001 }else
7002 #endif
7003 {
7004 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
7005 }
7006 }
7007 #ifdef SQLITE_USE_FCNTL_TRACE
7008 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7009 if( zTrace ){
7010 int j;
7011 for(j=0; j<db->nDb; j++){
7012 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
7013 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
7014 }
7015 }
7016 #endif /* SQLITE_USE_FCNTL_TRACE */
7017 #ifdef SQLITE_DEBUG
7018 if( (db->flags & SQLITE_SqlTrace)!=0
7019 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7020 ){
7021 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
7022 }
7023 #endif /* SQLITE_DEBUG */
7024 #endif /* SQLITE_OMIT_TRACE */
7025 assert( pOp->p2>0 );
7026 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
7027 for(i=1; i<p->nOp; i++){
7028 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
7029 }
7030 pOp->p1 = 0;
7031 }
7032 pOp->p1++;
7033 goto jump_to_p2;
7034 }
7035
7036 #ifdef SQLITE_ENABLE_CURSOR_HINTS
7037 /* Opcode: CursorHint P1 * * P4 *
7038 **
7039 ** Provide a hint to cursor P1 that it only needs to return rows that
7040 ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
7041 ** to values currently held in registers. TK_COLUMN terms in the P4
7042 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
7043 */
7044 case OP_CursorHint: {
7045 VdbeCursor *pC;
7046
7047 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7048 assert( pOp->p4type==P4_EXPR );
7049 pC = p->apCsr[pOp->p1];
7050 if( pC ){
7051 assert( pC->eCurType==CURTYPE_BTREE );
7052 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
7053 pOp->p4.pExpr, aMem);
7054 }
7055 break;
7056 }
7057 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
7058
7059 /* Opcode: Noop * * * * *
7060 **
7061 ** Do nothing. This instruction is often useful as a jump
7062 ** destination.
7063 */
7064 /*
7065 ** The magic Explain opcode are only inserted when explain==2 (which
7066 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
7067 ** This opcode records information from the optimizer. It is the
7068 ** the same as a no-op. This opcodesnever appears in a real VM program.
7069 */
7070 default: { /* This is really OP_Noop and OP_Explain */
7071 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
7072 break;
7073 }
7074
7075 /*****************************************************************************
7076 ** The cases of the switch statement above this line should all be indented
7077 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
7078 ** readability. From this point on down, the normal indentation rules are
7079 ** restored.
7080 *****************************************************************************/
7081 }
7082
7083 #ifdef VDBE_PROFILE
7084 {
7085 u64 endTime = sqlite3Hwtime();
7086 if( endTime>start ) pOrigOp->cycles += endTime - start;
7087 pOrigOp->cnt++;
7088 }
7089 #endif
7090
7091 /* The following code adds nothing to the actual functionality
7092 ** of the program. It is only here for testing and debugging.
7093 ** On the other hand, it does burn CPU cycles every time through
7094 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
7095 */
7096 #ifndef NDEBUG
7097 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
7098
7099 #ifdef SQLITE_DEBUG
7100 if( db->flags & SQLITE_VdbeTrace ){
7101 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
7102 if( rc!=0 ) printf("rc=%d\n",rc);
7103 if( opProperty & (OPFLG_OUT2) ){
7104 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
7105 }
7106 if( opProperty & OPFLG_OUT3 ){
7107 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
7108 }
7109 }
7110 #endif /* SQLITE_DEBUG */
7111 #endif /* NDEBUG */
7112 } /* The end of the for(;;) loop the loops through opcodes */
7113
7114 /* If we reach this point, it means that execution is finished with
7115 ** an error of some kind.
7116 */
7117 abort_due_to_error:
7118 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
7119 assert( rc );
7120 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7121 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7122 }
7123 p->rc = rc;
7124 sqlite3SystemError(db, rc);
7125 testcase( sqlite3GlobalConfig.xLog!=0 );
7126 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
7127 (int)(pOp - aOp), p->zSql, p->zErrMsg);
7128 sqlite3VdbeHalt(p);
7129 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
7130 rc = SQLITE_ERROR;
7131 if( resetSchemaOnFault>0 ){
7132 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
7133 }
7134
7135 /* This is the only way out of this procedure. We have to
7136 ** release the mutexes on btrees that were acquired at the
7137 ** top. */
7138 vdbe_return:
7139 testcase( nVmStep>0 );
7140 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
7141 sqlite3VdbeLeave(p);
7142 assert( rc!=SQLITE_OK || nExtraDelete==0
7143 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7144 );
7145 return rc;
7146
7147 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7148 ** is encountered.
7149 */
7150 too_big:
7151 sqlite3VdbeError(p, "string or blob too big");
7152 rc = SQLITE_TOOBIG;
7153 goto abort_due_to_error;
7154
7155 /* Jump to here if a malloc() fails.
7156 */
7157 no_mem:
7158 sqlite3OomFault(db);
7159 sqlite3VdbeError(p, "out of memory");
7160 rc = SQLITE_NOMEM_BKPT;
7161 goto abort_due_to_error;
7162
7163 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
7164 ** flag.
7165 */
7166 abort_due_to_interrupt:
7167 assert( db->u1.isInterrupted );
7168 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
7169 p->rc = rc;
7170 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7171 goto abort_due_to_error;
7172 }
7173
7174
7175 /************** End of vdbe.c ************************************************/
7176 /************** Begin file vdbeblob.c ****************************************/
7177 /*
7178 ** 2007 May 1
7179 **
7180 ** The author disclaims copyright to this source code. In place of
7181 ** a legal notice, here is a blessing:
7182 **
7183 ** May you do good and not evil.
7184 ** May you find forgiveness for yourself and forgive others.
7185 ** May you share freely, never taking more than you give.
7186 **
7187 *************************************************************************
7188 **
7189 ** This file contains code used to implement incremental BLOB I/O.
7190 */
7191
7192 /* #include "sqliteInt.h" */
7193 /* #include "vdbeInt.h" */
7194
7195 #ifndef SQLITE_OMIT_INCRBLOB
7196
7197 /*
7198 ** Valid sqlite3_blob* handles point to Incrblob structures.
7199 */
7200 typedef struct Incrblob Incrblob;
7201 struct Incrblob {
7202 int nByte; /* Size of open blob, in bytes */
7203 int iOffset; /* Byte offset of blob in cursor data */
7204 u16 iCol; /* Table column this handle is open on */
7205 BtCursor *pCsr; /* Cursor pointing at blob row */
7206 sqlite3_stmt *pStmt; /* Statement holding cursor open */
7207 sqlite3 *db; /* The associated database */
7208 char *zDb; /* Database name */
7209 Table *pTab; /* Table object */
7210 };
7211
7212
7213 /*
7214 ** This function is used by both blob_open() and blob_reopen(). It seeks
7215 ** the b-tree cursor associated with blob handle p to point to row iRow.
7216 ** If successful, SQLITE_OK is returned and subsequent calls to
7217 ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
7218 **
7219 ** If an error occurs, or if the specified row does not exist or does not
7220 ** contain a value of type TEXT or BLOB in the column nominated when the
7221 ** blob handle was opened, then an error code is returned and *pzErr may
7222 ** be set to point to a buffer containing an error message. It is the
7223 ** responsibility of the caller to free the error message buffer using
7224 ** sqlite3DbFree().
7225 **
7226 ** If an error does occur, then the b-tree cursor is closed. All subsequent
7227 ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
7228 ** immediately return SQLITE_ABORT.
7229 */
7230 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
7231 int rc; /* Error code */
7232 char *zErr = 0; /* Error message */
7233 Vdbe *v = (Vdbe *)p->pStmt;
7234
7235 /* Set the value of register r[1] in the SQL statement to integer iRow.
7236 ** This is done directly as a performance optimization
7237 */
7238 v->aMem[1].flags = MEM_Int;
7239 v->aMem[1].u.i = iRow;
7240
7241 /* If the statement has been run before (and is paused at the OP_ResultRow)
7242 ** then back it up to the point where it does the OP_SeekRowid. This could
7243 ** have been down with an extra OP_Goto, but simply setting the program
7244 ** counter is faster. */
7245 if( v->pc>3 ){
7246 v->pc = 3;
7247 rc = sqlite3VdbeExec(v);
7248 }else{
7249 rc = sqlite3_step(p->pStmt);
7250 }
7251 if( rc==SQLITE_ROW ){
7252 VdbeCursor *pC = v->apCsr[0];
7253 u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
7254 testcase( pC->nHdrParsed==p->iCol );
7255 testcase( pC->nHdrParsed==p->iCol+1 );
7256 if( type<12 ){
7257 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
7258 type==0?"null": type==7?"real": "integer"
7259 );
7260 rc = SQLITE_ERROR;
7261 sqlite3_finalize(p->pStmt);
7262 p->pStmt = 0;
7263 }else{
7264 p->iOffset = pC->aType[p->iCol + pC->nField];
7265 p->nByte = sqlite3VdbeSerialTypeLen(type);
7266 p->pCsr = pC->uc.pCursor;
7267 sqlite3BtreeIncrblobCursor(p->pCsr);
7268 }
7269 }
7270
7271 if( rc==SQLITE_ROW ){
7272 rc = SQLITE_OK;
7273 }else if( p->pStmt ){
7274 rc = sqlite3_finalize(p->pStmt);
7275 p->pStmt = 0;
7276 if( rc==SQLITE_OK ){
7277 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
7278 rc = SQLITE_ERROR;
7279 }else{
7280 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
7281 }
7282 }
7283
7284 assert( rc!=SQLITE_OK || zErr==0 );
7285 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
7286
7287 *pzErr = zErr;
7288 return rc;
7289 }
7290
7291 /*
7292 ** Open a blob handle.
7293 */
7294 SQLITE_API int sqlite3_blob_open(
7295 sqlite3* db, /* The database connection */
7296 const char *zDb, /* The attached database containing the blob */
7297 const char *zTable, /* The table containing the blob */
7298 const char *zColumn, /* The column containing the blob */
7299 sqlite_int64 iRow, /* The row containing the glob */
7300 int wrFlag, /* True -> read/write access, false -> read-only */
7301 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
7302 ){
7303 int nAttempt = 0;
7304 int iCol; /* Index of zColumn in row-record */
7305 int rc = SQLITE_OK;
7306 char *zErr = 0;
7307 Table *pTab;
7308 Parse *pParse = 0;
7309 Incrblob *pBlob = 0;
7310
7311 #ifdef SQLITE_ENABLE_API_ARMOR
7312 if( ppBlob==0 ){
7313 return SQLITE_MISUSE_BKPT;
7314 }
7315 #endif
7316 *ppBlob = 0;
7317 #ifdef SQLITE_ENABLE_API_ARMOR
7318 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
7319 return SQLITE_MISUSE_BKPT;
7320 }
7321 #endif
7322 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
7323
7324 sqlite3_mutex_enter(db->mutex);
7325
7326 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
7327 if( !pBlob ) goto blob_open_out;
7328 pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
7329 if( !pParse ) goto blob_open_out;
7330
7331 do {
7332 memset(pParse, 0, sizeof(Parse));
7333 pParse->db = db;
7334 sqlite3DbFree(db, zErr);
7335 zErr = 0;
7336
7337 sqlite3BtreeEnterAll(db);
7338 pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
7339 if( pTab && IsVirtual(pTab) ){
7340 pTab = 0;
7341 sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
7342 }
7343 if( pTab && !HasRowid(pTab) ){
7344 pTab = 0;
7345 sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
7346 }
7347 #ifndef SQLITE_OMIT_VIEW
7348 if( pTab && pTab->pSelect ){
7349 pTab = 0;
7350 sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
7351 }
7352 #endif
7353 if( !pTab ){
7354 if( pParse->zErrMsg ){
7355 sqlite3DbFree(db, zErr);
7356 zErr = pParse->zErrMsg;
7357 pParse->zErrMsg = 0;
7358 }
7359 rc = SQLITE_ERROR;
7360 sqlite3BtreeLeaveAll(db);
7361 goto blob_open_out;
7362 }
7363 pBlob->pTab = pTab;
7364 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
7365
7366 /* Now search pTab for the exact column. */
7367 for(iCol=0; iCol<pTab->nCol; iCol++) {
7368 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
7369 break;
7370 }
7371 }
7372 if( iCol==pTab->nCol ){
7373 sqlite3DbFree(db, zErr);
7374 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
7375 rc = SQLITE_ERROR;
7376 sqlite3BtreeLeaveAll(db);
7377 goto blob_open_out;
7378 }
7379
7380 /* If the value is being opened for writing, check that the
7381 ** column is not indexed, and that it is not part of a foreign key.
7382 */
7383 if( wrFlag ){
7384 const char *zFault = 0;
7385 Index *pIdx;
7386 #ifndef SQLITE_OMIT_FOREIGN_KEY
7387 if( db->flags&SQLITE_ForeignKeys ){
7388 /* Check that the column is not part of an FK child key definition. It
7389 ** is not necessary to check if it is part of a parent key, as parent
7390 ** key columns must be indexed. The check below will pick up this
7391 ** case. */
7392 FKey *pFKey;
7393 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
7394 int j;
7395 for(j=0; j<pFKey->nCol; j++){
7396 if( pFKey->aCol[j].iFrom==iCol ){
7397 zFault = "foreign key";
7398 }
7399 }
7400 }
7401 }
7402 #endif
7403 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
7404 int j;
7405 for(j=0; j<pIdx->nKeyCol; j++){
7406 /* FIXME: Be smarter about indexes that use expressions */
7407 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
7408 zFault = "indexed";
7409 }
7410 }
7411 }
7412 if( zFault ){
7413 sqlite3DbFree(db, zErr);
7414 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
7415 rc = SQLITE_ERROR;
7416 sqlite3BtreeLeaveAll(db);
7417 goto blob_open_out;
7418 }
7419 }
7420
7421 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
7422 assert( pBlob->pStmt || db->mallocFailed );
7423 if( pBlob->pStmt ){
7424
7425 /* This VDBE program seeks a btree cursor to the identified
7426 ** db/table/row entry. The reason for using a vdbe program instead
7427 ** of writing code to use the b-tree layer directly is that the
7428 ** vdbe program will take advantage of the various transaction,
7429 ** locking and error handling infrastructure built into the vdbe.
7430 **
7431 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
7432 ** Code external to the Vdbe then "borrows" the b-tree cursor and
7433 ** uses it to implement the blob_read(), blob_write() and
7434 ** blob_bytes() functions.
7435 **
7436 ** The sqlite3_blob_close() function finalizes the vdbe program,
7437 ** which closes the b-tree cursor and (possibly) commits the
7438 ** transaction.
7439 */
7440 static const int iLn = VDBE_OFFSET_LINENO(2);
7441 static const VdbeOpList openBlob[] = {
7442 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
7443 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
7444 /* blobSeekToRow() will initialize r[1] to the desired rowid */
7445 {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
7446 {OP_Column, 0, 0, 1}, /* 3 */
7447 {OP_ResultRow, 1, 0, 0}, /* 4 */
7448 {OP_Halt, 0, 0, 0}, /* 5 */
7449 };
7450 Vdbe *v = (Vdbe *)pBlob->pStmt;
7451 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
7452 VdbeOp *aOp;
7453
7454 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
7455 pTab->pSchema->schema_cookie,
7456 pTab->pSchema->iGeneration);
7457 sqlite3VdbeChangeP5(v, 1);
7458 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
7459
7460 /* Make sure a mutex is held on the table to be accessed */
7461 sqlite3VdbeUsesBtree(v, iDb);
7462
7463 if( db->mallocFailed==0 ){
7464 assert( aOp!=0 );
7465 /* Configure the OP_TableLock instruction */
7466 #ifdef SQLITE_OMIT_SHARED_CACHE
7467 aOp[0].opcode = OP_Noop;
7468 #else
7469 aOp[0].p1 = iDb;
7470 aOp[0].p2 = pTab->tnum;
7471 aOp[0].p3 = wrFlag;
7472 sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
7473 }
7474 if( db->mallocFailed==0 ){
7475 #endif
7476
7477 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
7478 ** parameter of the other to pTab->tnum. */
7479 if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
7480 aOp[1].p2 = pTab->tnum;
7481 aOp[1].p3 = iDb;
7482
7483 /* Configure the number of columns. Configure the cursor to
7484 ** think that the table has one more column than it really
7485 ** does. An OP_Column to retrieve this imaginary column will
7486 ** always return an SQL NULL. This is useful because it means
7487 ** we can invoke OP_Column to fill in the vdbe cursors type
7488 ** and offset cache without causing any IO.
7489 */
7490 aOp[1].p4type = P4_INT32;
7491 aOp[1].p4.i = pTab->nCol+1;
7492 aOp[3].p2 = pTab->nCol;
7493
7494 pParse->nVar = 0;
7495 pParse->nMem = 1;
7496 pParse->nTab = 1;
7497 sqlite3VdbeMakeReady(v, pParse);
7498 }
7499 }
7500
7501 pBlob->iCol = iCol;
7502 pBlob->db = db;
7503 sqlite3BtreeLeaveAll(db);
7504 if( db->mallocFailed ){
7505 goto blob_open_out;
7506 }
7507 rc = blobSeekToRow(pBlob, iRow, &zErr);
7508 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
7509
7510 blob_open_out:
7511 if( rc==SQLITE_OK && db->mallocFailed==0 ){
7512 *ppBlob = (sqlite3_blob *)pBlob;
7513 }else{
7514 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
7515 sqlite3DbFree(db, pBlob);
7516 }
7517 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
7518 sqlite3DbFree(db, zErr);
7519 sqlite3ParserReset(pParse);
7520 sqlite3StackFree(db, pParse);
7521 rc = sqlite3ApiExit(db, rc);
7522 sqlite3_mutex_leave(db->mutex);
7523 return rc;
7524 }
7525
7526 /*
7527 ** Close a blob handle that was previously created using
7528 ** sqlite3_blob_open().
7529 */
7530 SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
7531 Incrblob *p = (Incrblob *)pBlob;
7532 int rc;
7533 sqlite3 *db;
7534
7535 if( p ){
7536 db = p->db;
7537 sqlite3_mutex_enter(db->mutex);
7538 rc = sqlite3_finalize(p->pStmt);
7539 sqlite3DbFree(db, p);
7540 sqlite3_mutex_leave(db->mutex);
7541 }else{
7542 rc = SQLITE_OK;
7543 }
7544 return rc;
7545 }
7546
7547 /*
7548 ** Perform a read or write operation on a blob
7549 */
7550 static int blobReadWrite(
7551 sqlite3_blob *pBlob,
7552 void *z,
7553 int n,
7554 int iOffset,
7555 int (*xCall)(BtCursor*, u32, u32, void*)
7556 ){
7557 int rc;
7558 Incrblob *p = (Incrblob *)pBlob;
7559 Vdbe *v;
7560 sqlite3 *db;
7561
7562 if( p==0 ) return SQLITE_MISUSE_BKPT;
7563 db = p->db;
7564 sqlite3_mutex_enter(db->mutex);
7565 v = (Vdbe*)p->pStmt;
7566
7567 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
7568 /* Request is out of range. Return a transient error. */
7569 rc = SQLITE_ERROR;
7570 }else if( v==0 ){
7571 /* If there is no statement handle, then the blob-handle has
7572 ** already been invalidated. Return SQLITE_ABORT in this case.
7573 */
7574 rc = SQLITE_ABORT;
7575 }else{
7576 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
7577 ** returned, clean-up the statement handle.
7578 */
7579 assert( db == v->db );
7580 sqlite3BtreeEnterCursor(p->pCsr);
7581
7582 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
7583 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
7584 /* If a pre-update hook is registered and this is a write cursor,
7585 ** invoke it here.
7586 **
7587 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
7588 ** operation should really be an SQLITE_UPDATE. This is probably
7589 ** incorrect, but is convenient because at this point the new.* values
7590 ** are not easily obtainable. And for the sessions module, an
7591 ** SQLITE_UPDATE where the PK columns do not change is handled in the
7592 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
7593 ** slightly more efficient). Since you cannot write to a PK column
7594 ** using the incremental-blob API, this works. For the sessions module
7595 ** anyhow.
7596 */
7597 sqlite3_int64 iKey;
7598 iKey = sqlite3BtreeIntegerKey(p->pCsr);
7599 sqlite3VdbePreUpdateHook(
7600 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
7601 );
7602 }
7603 #endif
7604
7605 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
7606 sqlite3BtreeLeaveCursor(p->pCsr);
7607 if( rc==SQLITE_ABORT ){
7608 sqlite3VdbeFinalize(v);
7609 p->pStmt = 0;
7610 }else{
7611 v->rc = rc;
7612 }
7613 }
7614 sqlite3Error(db, rc);
7615 rc = sqlite3ApiExit(db, rc);
7616 sqlite3_mutex_leave(db->mutex);
7617 return rc;
7618 }
7619
7620 /*
7621 ** Read data from a blob handle.
7622 */
7623 SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffse t){
7624 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
7625 }
7626
7627 /*
7628 ** Write data to a blob handle.
7629 */
7630 SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
7631 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
7632 }
7633
7634 /*
7635 ** Query a blob handle for the size of the data.
7636 **
7637 ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
7638 ** so no mutex is required for access.
7639 */
7640 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
7641 Incrblob *p = (Incrblob *)pBlob;
7642 return (p && p->pStmt) ? p->nByte : 0;
7643 }
7644
7645 /*
7646 ** Move an existing blob handle to point to a different row of the same
7647 ** database table.
7648 **
7649 ** If an error occurs, or if the specified row does not exist or does not
7650 ** contain a blob or text value, then an error code is returned and the
7651 ** database handle error code and message set. If this happens, then all
7652 ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
7653 ** immediately return SQLITE_ABORT.
7654 */
7655 SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
7656 int rc;
7657 Incrblob *p = (Incrblob *)pBlob;
7658 sqlite3 *db;
7659
7660 if( p==0 ) return SQLITE_MISUSE_BKPT;
7661 db = p->db;
7662 sqlite3_mutex_enter(db->mutex);
7663
7664 if( p->pStmt==0 ){
7665 /* If there is no statement handle, then the blob-handle has
7666 ** already been invalidated. Return SQLITE_ABORT in this case.
7667 */
7668 rc = SQLITE_ABORT;
7669 }else{
7670 char *zErr;
7671 rc = blobSeekToRow(p, iRow, &zErr);
7672 if( rc!=SQLITE_OK ){
7673 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
7674 sqlite3DbFree(db, zErr);
7675 }
7676 assert( rc!=SQLITE_SCHEMA );
7677 }
7678
7679 rc = sqlite3ApiExit(db, rc);
7680 assert( rc==SQLITE_OK || p->pStmt==0 );
7681 sqlite3_mutex_leave(db->mutex);
7682 return rc;
7683 }
7684
7685 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
7686
7687 /************** End of vdbeblob.c ********************************************/
7688 /************** Begin file vdbesort.c ****************************************/
7689 /*
7690 ** 2011-07-09
7691 **
7692 ** The author disclaims copyright to this source code. In place of
7693 ** a legal notice, here is a blessing:
7694 **
7695 ** May you do good and not evil.
7696 ** May you find forgiveness for yourself and forgive others.
7697 ** May you share freely, never taking more than you give.
7698 **
7699 *************************************************************************
7700 ** This file contains code for the VdbeSorter object, used in concert with
7701 ** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
7702 ** or by SELECT statements with ORDER BY clauses that cannot be satisfied
7703 ** using indexes and without LIMIT clauses.
7704 **
7705 ** The VdbeSorter object implements a multi-threaded external merge sort
7706 ** algorithm that is efficient even if the number of elements being sorted
7707 ** exceeds the available memory.
7708 **
7709 ** Here is the (internal, non-API) interface between this module and the
7710 ** rest of the SQLite system:
7711 **
7712 ** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
7713 **
7714 ** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
7715 ** object. The row is a binary blob in the
7716 ** OP_MakeRecord format that contains both
7717 ** the ORDER BY key columns and result columns
7718 ** in the case of a SELECT w/ ORDER BY, or
7719 ** the complete record for an index entry
7720 ** in the case of a CREATE INDEX.
7721 **
7722 ** sqlite3VdbeSorterRewind() Sort all content previously added.
7723 ** Position the read cursor on the
7724 ** first sorted element.
7725 **
7726 ** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
7727 ** element.
7728 **
7729 ** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
7730 ** row currently under the read cursor.
7731 **
7732 ** sqlite3VdbeSorterCompare() Compare the binary blob for the row
7733 ** currently under the read cursor against
7734 ** another binary blob X and report if
7735 ** X is strictly less than the read cursor.
7736 ** Used to enforce uniqueness in a
7737 ** CREATE UNIQUE INDEX statement.
7738 **
7739 ** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
7740 ** all resources.
7741 **
7742 ** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
7743 ** is like Close() followed by Init() only
7744 ** much faster.
7745 **
7746 ** The interfaces above must be called in a particular order. Write() can
7747 ** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
7748 ** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
7749 **
7750 ** Init()
7751 ** for each record: Write()
7752 ** Rewind()
7753 ** Rowkey()/Compare()
7754 ** Next()
7755 ** Close()
7756 **
7757 ** Algorithm:
7758 **
7759 ** Records passed to the sorter via calls to Write() are initially held
7760 ** unsorted in main memory. Assuming the amount of memory used never exceeds
7761 ** a threshold, when Rewind() is called the set of records is sorted using
7762 ** an in-memory merge sort. In this case, no temporary files are required
7763 ** and subsequent calls to Rowkey(), Next() and Compare() read records
7764 ** directly from main memory.
7765 **
7766 ** If the amount of space used to store records in main memory exceeds the
7767 ** threshold, then the set of records currently in memory are sorted and
7768 ** written to a temporary file in "Packed Memory Array" (PMA) format.
7769 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
7770 ** of PMAs may be created by merging existing PMAs together - for example
7771 ** merging two or more level-0 PMAs together creates a level-1 PMA.
7772 **
7773 ** The threshold for the amount of main memory to use before flushing
7774 ** records to a PMA is roughly the same as the limit configured for the
7775 ** page-cache of the main database. Specifically, the threshold is set to
7776 ** the value returned by "PRAGMA main.page_size" multipled by
7777 ** that returned by "PRAGMA main.cache_size", in bytes.
7778 **
7779 ** If the sorter is running in single-threaded mode, then all PMAs generated
7780 ** are appended to a single temporary file. Or, if the sorter is running in
7781 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
7782 ** N is the configured number of worker threads. In this case, instead of
7783 ** sorting the records and writing the PMA to a temporary file itself, the
7784 ** calling thread usually launches a worker thread to do so. Except, if
7785 ** there are already N worker threads running, the main thread does the work
7786 ** itself.
7787 **
7788 ** The sorter is running in multi-threaded mode if (a) the library was built
7789 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
7790 ** than zero, and (b) worker threads have been enabled at runtime by calling
7791 ** "PRAGMA threads=N" with some value of N greater than 0.
7792 **
7793 ** When Rewind() is called, any data remaining in memory is flushed to a
7794 ** final PMA. So at this point the data is stored in some number of sorted
7795 ** PMAs within temporary files on disk.
7796 **
7797 ** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
7798 ** sorter is running in single-threaded mode, then these PMAs are merged
7799 ** incrementally as keys are retreived from the sorter by the VDBE. The
7800 ** MergeEngine object, described in further detail below, performs this
7801 ** merge.
7802 **
7803 ** Or, if running in multi-threaded mode, then a background thread is
7804 ** launched to merge the existing PMAs. Once the background thread has
7805 ** merged T bytes of data into a single sorted PMA, the main thread
7806 ** begins reading keys from that PMA while the background thread proceeds
7807 ** with merging the next T bytes of data. And so on.
7808 **
7809 ** Parameter T is set to half the value of the memory threshold used
7810 ** by Write() above to determine when to create a new PMA.
7811 **
7812 ** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
7813 ** Rewind() is called, then a hierarchy of incremental-merges is used.
7814 ** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
7815 ** disk are merged together. Then T bytes of data from the second set, and
7816 ** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
7817 ** PMAs at a time. This done is to improve locality.
7818 **
7819 ** If running in multi-threaded mode and there are more than
7820 ** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
7821 ** than one background thread may be created. Specifically, there may be
7822 ** one background thread for each temporary file on disk, and one background
7823 ** thread to merge the output of each of the others to a single PMA for
7824 ** the main thread to read from.
7825 */
7826 /* #include "sqliteInt.h" */
7827 /* #include "vdbeInt.h" */
7828
7829 /*
7830 ** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
7831 ** messages to stderr that may be helpful in understanding the performance
7832 ** characteristics of the sorter in multi-threaded mode.
7833 */
7834 #if 0
7835 # define SQLITE_DEBUG_SORTER_THREADS 1
7836 #endif
7837
7838 /*
7839 ** Hard-coded maximum amount of data to accumulate in memory before flushing
7840 ** to a level 0 PMA. The purpose of this limit is to prevent various integer
7841 ** overflows. 512MiB.
7842 */
7843 #define SQLITE_MAX_PMASZ (1<<29)
7844
7845 /*
7846 ** Private objects used by the sorter
7847 */
7848 typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
7849 typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
7850 typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
7851 typedef struct SorterRecord SorterRecord; /* A record being sorted */
7852 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
7853 typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
7854 typedef struct SorterList SorterList; /* In-memory list of records */
7855 typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
7856
7857 /*
7858 ** A container for a temp file handle and the current amount of data
7859 ** stored in the file.
7860 */
7861 struct SorterFile {
7862 sqlite3_file *pFd; /* File handle */
7863 i64 iEof; /* Bytes of data stored in pFd */
7864 };
7865
7866 /*
7867 ** An in-memory list of objects to be sorted.
7868 **
7869 ** If aMemory==0 then each object is allocated separately and the objects
7870 ** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
7871 ** are stored in the aMemory[] bulk memory, one right after the other, and
7872 ** are connected using SorterRecord.u.iNext.
7873 */
7874 struct SorterList {
7875 SorterRecord *pList; /* Linked list of records */
7876 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
7877 int szPMA; /* Size of pList as PMA in bytes */
7878 };
7879
7880 /*
7881 ** The MergeEngine object is used to combine two or more smaller PMAs into
7882 ** one big PMA using a merge operation. Separate PMAs all need to be
7883 ** combined into one big PMA in order to be able to step through the sorted
7884 ** records in order.
7885 **
7886 ** The aReadr[] array contains a PmaReader object for each of the PMAs being
7887 ** merged. An aReadr[] object either points to a valid key or else is at EOF.
7888 ** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
7889 ** For the purposes of the paragraphs below, we assume that the array is
7890 ** actually N elements in size, where N is the smallest power of 2 greater
7891 ** to or equal to the number of PMAs being merged. The extra aReadr[] elements
7892 ** are treated as if they are empty (always at EOF).
7893 **
7894 ** The aTree[] array is also N elements in size. The value of N is stored in
7895 ** the MergeEngine.nTree variable.
7896 **
7897 ** The final (N/2) elements of aTree[] contain the results of comparing
7898 ** pairs of PMA keys together. Element i contains the result of
7899 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
7900 ** aTree element is set to the index of it.
7901 **
7902 ** For the purposes of this comparison, EOF is considered greater than any
7903 ** other key value. If the keys are equal (only possible with two EOF
7904 ** values), it doesn't matter which index is stored.
7905 **
7906 ** The (N/4) elements of aTree[] that precede the final (N/2) described
7907 ** above contains the index of the smallest of each block of 4 PmaReaders
7908 ** And so on. So that aTree[1] contains the index of the PmaReader that
7909 ** currently points to the smallest key value. aTree[0] is unused.
7910 **
7911 ** Example:
7912 **
7913 ** aReadr[0] -> Banana
7914 ** aReadr[1] -> Feijoa
7915 ** aReadr[2] -> Elderberry
7916 ** aReadr[3] -> Currant
7917 ** aReadr[4] -> Grapefruit
7918 ** aReadr[5] -> Apple
7919 ** aReadr[6] -> Durian
7920 ** aReadr[7] -> EOF
7921 **
7922 ** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
7923 **
7924 ** The current element is "Apple" (the value of the key indicated by
7925 ** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
7926 ** be advanced to the next key in its segment. Say the next key is
7927 ** "Eggplant":
7928 **
7929 ** aReadr[5] -> Eggplant
7930 **
7931 ** The contents of aTree[] are updated first by comparing the new PmaReader
7932 ** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
7933 ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
7934 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
7935 ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
7936 ** so the value written into element 1 of the array is 0. As follows:
7937 **
7938 ** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
7939 **
7940 ** In other words, each time we advance to the next sorter element, log2(N)
7941 ** key comparison operations are required, where N is the number of segments
7942 ** being merged (rounded up to the next power of 2).
7943 */
7944 struct MergeEngine {
7945 int nTree; /* Used size of aTree/aReadr (power of 2) */
7946 SortSubtask *pTask; /* Used by this thread only */
7947 int *aTree; /* Current state of incremental merge */
7948 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
7949 };
7950
7951 /*
7952 ** This object represents a single thread of control in a sort operation.
7953 ** Exactly VdbeSorter.nTask instances of this object are allocated
7954 ** as part of each VdbeSorter object. Instances are never allocated any
7955 ** other way. VdbeSorter.nTask is set to the number of worker threads allowed
7956 ** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
7957 ** single-threaded operation, there is exactly one instance of this object
7958 ** and for multi-threaded operation there are two or more instances.
7959 **
7960 ** Essentially, this structure contains all those fields of the VdbeSorter
7961 ** structure for which each thread requires a separate instance. For example,
7962 ** each thread requries its own UnpackedRecord object to unpack records in
7963 ** as part of comparison operations.
7964 **
7965 ** Before a background thread is launched, variable bDone is set to 0. Then,
7966 ** right before it exits, the thread itself sets bDone to 1. This is used for
7967 ** two purposes:
7968 **
7969 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
7970 ** attempt to select a SortSubtask for which there is not already an
7971 ** active background thread (since doing so causes the main thread
7972 ** to block until it finishes).
7973 **
7974 ** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
7975 ** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
7976 ** block provoke debugging output.
7977 **
7978 ** In both cases, the effects of the main thread seeing (bDone==0) even
7979 ** after the thread has finished are not dire. So we don't worry about
7980 ** memory barriers and such here.
7981 */
7982 typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
7983 struct SortSubtask {
7984 SQLiteThread *pThread; /* Background thread, if any */
7985 int bDone; /* Set if thread is finished but not joined */
7986 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
7987 UnpackedRecord *pUnpacked; /* Space to unpack a record */
7988 SorterList list; /* List for thread to write to a PMA */
7989 int nPMA; /* Number of PMAs currently in file */
7990 SorterCompare xCompare; /* Compare function to use */
7991 SorterFile file; /* Temp file for level-0 PMAs */
7992 SorterFile file2; /* Space for other PMAs */
7993 };
7994
7995
7996 /*
7997 ** Main sorter structure. A single instance of this is allocated for each
7998 ** sorter cursor created by the VDBE.
7999 **
8000 ** mxKeysize:
8001 ** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
8002 ** this variable is updated so as to be set to the size on disk of the
8003 ** largest record in the sorter.
8004 */
8005 struct VdbeSorter {
8006 int mnPmaSize; /* Minimum PMA size, in bytes */
8007 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
8008 int mxKeysize; /* Largest serialized key seen so far */
8009 int pgsz; /* Main database page size */
8010 PmaReader *pReader; /* Readr data from here after Rewind() */
8011 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
8012 sqlite3 *db; /* Database connection */
8013 KeyInfo *pKeyInfo; /* How to compare records */
8014 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
8015 SorterList list; /* List of in-memory records */
8016 int iMemory; /* Offset of free space in list.aMemory */
8017 int nMemory; /* Size of list.aMemory allocation in bytes */
8018 u8 bUsePMA; /* True if one or more PMAs created */
8019 u8 bUseThreads; /* True to use background threads */
8020 u8 iPrev; /* Previous thread used to flush PMA */
8021 u8 nTask; /* Size of aTask[] array */
8022 u8 typeMask;
8023 SortSubtask aTask[1]; /* One or more subtasks */
8024 };
8025
8026 #define SORTER_TYPE_INTEGER 0x01
8027 #define SORTER_TYPE_TEXT 0x02
8028
8029 /*
8030 ** An instance of the following object is used to read records out of a
8031 ** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
8032 ** aKey might point into aMap or into aBuffer. If neither of those locations
8033 ** contain a contiguous representation of the key, then aAlloc is allocated
8034 ** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
8035 **
8036 ** pFd==0 at EOF.
8037 */
8038 struct PmaReader {
8039 i64 iReadOff; /* Current read offset */
8040 i64 iEof; /* 1 byte past EOF for this PmaReader */
8041 int nAlloc; /* Bytes of space at aAlloc */
8042 int nKey; /* Number of bytes in key */
8043 sqlite3_file *pFd; /* File handle we are reading from */
8044 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
8045 u8 *aKey; /* Pointer to current key */
8046 u8 *aBuffer; /* Current read buffer */
8047 int nBuffer; /* Size of read buffer in bytes */
8048 u8 *aMap; /* Pointer to mapping of entire file */
8049 IncrMerger *pIncr; /* Incremental merger */
8050 };
8051
8052 /*
8053 ** Normally, a PmaReader object iterates through an existing PMA stored
8054 ** within a temp file. However, if the PmaReader.pIncr variable points to
8055 ** an object of the following type, it may be used to iterate/merge through
8056 ** multiple PMAs simultaneously.
8057 **
8058 ** There are two types of IncrMerger object - single (bUseThread==0) and
8059 ** multi-threaded (bUseThread==1).
8060 **
8061 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
8062 ** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
8063 ** size. When the IncrMerger is initialized, it reads enough data from
8064 ** pMerger to populate aFile[0]. It then sets variables within the
8065 ** corresponding PmaReader object to read from that file and kicks off
8066 ** a background thread to populate aFile[1] with the next mxSz bytes of
8067 ** sorted record data from pMerger.
8068 **
8069 ** When the PmaReader reaches the end of aFile[0], it blocks until the
8070 ** background thread has finished populating aFile[1]. It then exchanges
8071 ** the contents of the aFile[0] and aFile[1] variables within this structure,
8072 ** sets the PmaReader fields to read from the new aFile[0] and kicks off
8073 ** another background thread to populate the new aFile[1]. And so on, until
8074 ** the contents of pMerger are exhausted.
8075 **
8076 ** A single-threaded IncrMerger does not open any temporary files of its
8077 ** own. Instead, it has exclusive access to mxSz bytes of space beginning
8078 ** at offset iStartOff of file pTask->file2. And instead of using a
8079 ** background thread to prepare data for the PmaReader, with a single
8080 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
8081 ** keys from pMerger by the calling thread whenever the PmaReader runs out
8082 ** of data.
8083 */
8084 struct IncrMerger {
8085 SortSubtask *pTask; /* Task that owns this merger */
8086 MergeEngine *pMerger; /* Merge engine thread reads data from */
8087 i64 iStartOff; /* Offset to start writing file at */
8088 int mxSz; /* Maximum bytes of data to store */
8089 int bEof; /* Set to true when merge is finished */
8090 int bUseThread; /* True to use a bg thread for this object */
8091 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
8092 };
8093
8094 /*
8095 ** An instance of this object is used for writing a PMA.
8096 **
8097 ** The PMA is written one record at a time. Each record is of an arbitrary
8098 ** size. But I/O is more efficient if it occurs in page-sized blocks where
8099 ** each block is aligned on a page boundary. This object caches writes to
8100 ** the PMA so that aligned, page-size blocks are written.
8101 */
8102 struct PmaWriter {
8103 int eFWErr; /* Non-zero if in an error state */
8104 u8 *aBuffer; /* Pointer to write buffer */
8105 int nBuffer; /* Size of write buffer in bytes */
8106 int iBufStart; /* First byte of buffer to write */
8107 int iBufEnd; /* Last byte of buffer to write */
8108 i64 iWriteOff; /* Offset of start of buffer in file */
8109 sqlite3_file *pFd; /* File handle to write to */
8110 };
8111
8112 /*
8113 ** This object is the header on a single record while that record is being
8114 ** held in memory and prior to being written out as part of a PMA.
8115 **
8116 ** How the linked list is connected depends on how memory is being managed
8117 ** by this module. If using a separate allocation for each in-memory record
8118 ** (VdbeSorter.list.aMemory==0), then the list is always connected using the
8119 ** SorterRecord.u.pNext pointers.
8120 **
8121 ** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
8122 ** then while records are being accumulated the list is linked using the
8123 ** SorterRecord.u.iNext offset. This is because the aMemory[] array may
8124 ** be sqlite3Realloc()ed while records are being accumulated. Once the VM
8125 ** has finished passing records to the sorter, or when the in-memory buffer
8126 ** is full, the list is sorted. As part of the sorting process, it is
8127 ** converted to use the SorterRecord.u.pNext pointers. See function
8128 ** vdbeSorterSort() for details.
8129 */
8130 struct SorterRecord {
8131 int nVal; /* Size of the record in bytes */
8132 union {
8133 SorterRecord *pNext; /* Pointer to next record in list */
8134 int iNext; /* Offset within aMemory of next record */
8135 } u;
8136 /* The data for the record immediately follows this header */
8137 };
8138
8139 /* Return a pointer to the buffer containing the record data for SorterRecord
8140 ** object p. Should be used as if:
8141 **
8142 ** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
8143 */
8144 #define SRVAL(p) ((void*)((SorterRecord*)(p) + 1))
8145
8146
8147 /* Maximum number of PMAs that a single MergeEngine can merge */
8148 #define SORTER_MAX_MERGE_COUNT 16
8149
8150 static int vdbeIncrSwap(IncrMerger*);
8151 static void vdbeIncrFree(IncrMerger *);
8152
8153 /*
8154 ** Free all memory belonging to the PmaReader object passed as the
8155 ** argument. All structure fields are set to zero before returning.
8156 */
8157 static void vdbePmaReaderClear(PmaReader *pReadr){
8158 sqlite3_free(pReadr->aAlloc);
8159 sqlite3_free(pReadr->aBuffer);
8160 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
8161 vdbeIncrFree(pReadr->pIncr);
8162 memset(pReadr, 0, sizeof(PmaReader));
8163 }
8164
8165 /*
8166 ** Read the next nByte bytes of data from the PMA p.
8167 ** If successful, set *ppOut to point to a buffer containing the data
8168 ** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
8169 ** error code.
8170 **
8171 ** The buffer returned in *ppOut is only valid until the
8172 ** next call to this function.
8173 */
8174 static int vdbePmaReadBlob(
8175 PmaReader *p, /* PmaReader from which to take the blob */
8176 int nByte, /* Bytes of data to read */
8177 u8 **ppOut /* OUT: Pointer to buffer containing data */
8178 ){
8179 int iBuf; /* Offset within buffer to read from */
8180 int nAvail; /* Bytes of data available in buffer */
8181
8182 if( p->aMap ){
8183 *ppOut = &p->aMap[p->iReadOff];
8184 p->iReadOff += nByte;
8185 return SQLITE_OK;
8186 }
8187
8188 assert( p->aBuffer );
8189
8190 /* If there is no more data to be read from the buffer, read the next
8191 ** p->nBuffer bytes of data from the file into it. Or, if there are less
8192 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
8193 iBuf = p->iReadOff % p->nBuffer;
8194 if( iBuf==0 ){
8195 int nRead; /* Bytes to read from disk */
8196 int rc; /* sqlite3OsRead() return code */
8197
8198 /* Determine how many bytes of data to read. */
8199 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
8200 nRead = p->nBuffer;
8201 }else{
8202 nRead = (int)(p->iEof - p->iReadOff);
8203 }
8204 assert( nRead>0 );
8205
8206 /* Readr data from the file. Return early if an error occurs. */
8207 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
8208 assert( rc!=SQLITE_IOERR_SHORT_READ );
8209 if( rc!=SQLITE_OK ) return rc;
8210 }
8211 nAvail = p->nBuffer - iBuf;
8212
8213 if( nByte<=nAvail ){
8214 /* The requested data is available in the in-memory buffer. In this
8215 ** case there is no need to make a copy of the data, just return a
8216 ** pointer into the buffer to the caller. */
8217 *ppOut = &p->aBuffer[iBuf];
8218 p->iReadOff += nByte;
8219 }else{
8220 /* The requested data is not all available in the in-memory buffer.
8221 ** In this case, allocate space at p->aAlloc[] to copy the requested
8222 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
8223 int nRem; /* Bytes remaining to copy */
8224
8225 /* Extend the p->aAlloc[] allocation if required. */
8226 if( p->nAlloc<nByte ){
8227 u8 *aNew;
8228 int nNew = MAX(128, p->nAlloc*2);
8229 while( nByte>nNew ) nNew = nNew*2;
8230 aNew = sqlite3Realloc(p->aAlloc, nNew);
8231 if( !aNew ) return SQLITE_NOMEM_BKPT;
8232 p->nAlloc = nNew;
8233 p->aAlloc = aNew;
8234 }
8235
8236 /* Copy as much data as is available in the buffer into the start of
8237 ** p->aAlloc[]. */
8238 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
8239 p->iReadOff += nAvail;
8240 nRem = nByte - nAvail;
8241
8242 /* The following loop copies up to p->nBuffer bytes per iteration into
8243 ** the p->aAlloc[] buffer. */
8244 while( nRem>0 ){
8245 int rc; /* vdbePmaReadBlob() return code */
8246 int nCopy; /* Number of bytes to copy */
8247 u8 *aNext; /* Pointer to buffer to copy data from */
8248
8249 nCopy = nRem;
8250 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
8251 rc = vdbePmaReadBlob(p, nCopy, &aNext);
8252 if( rc!=SQLITE_OK ) return rc;
8253 assert( aNext!=p->aAlloc );
8254 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
8255 nRem -= nCopy;
8256 }
8257
8258 *ppOut = p->aAlloc;
8259 }
8260
8261 return SQLITE_OK;
8262 }
8263
8264 /*
8265 ** Read a varint from the stream of data accessed by p. Set *pnOut to
8266 ** the value read.
8267 */
8268 static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
8269 int iBuf;
8270
8271 if( p->aMap ){
8272 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
8273 }else{
8274 iBuf = p->iReadOff % p->nBuffer;
8275 if( iBuf && (p->nBuffer-iBuf)>=9 ){
8276 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
8277 }else{
8278 u8 aVarint[16], *a;
8279 int i = 0, rc;
8280 do{
8281 rc = vdbePmaReadBlob(p, 1, &a);
8282 if( rc ) return rc;
8283 aVarint[(i++)&0xf] = a[0];
8284 }while( (a[0]&0x80)!=0 );
8285 sqlite3GetVarint(aVarint, pnOut);
8286 }
8287 }
8288
8289 return SQLITE_OK;
8290 }
8291
8292 /*
8293 ** Attempt to memory map file pFile. If successful, set *pp to point to the
8294 ** new mapping and return SQLITE_OK. If the mapping is not attempted
8295 ** (because the file is too large or the VFS layer is configured not to use
8296 ** mmap), return SQLITE_OK and set *pp to NULL.
8297 **
8298 ** Or, if an error occurs, return an SQLite error code. The final value of
8299 ** *pp is undefined in this case.
8300 */
8301 static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
8302 int rc = SQLITE_OK;
8303 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
8304 sqlite3_file *pFd = pFile->pFd;
8305 if( pFd->pMethods->iVersion>=3 ){
8306 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
8307 testcase( rc!=SQLITE_OK );
8308 }
8309 }
8310 return rc;
8311 }
8312
8313 /*
8314 ** Attach PmaReader pReadr to file pFile (if it is not already attached to
8315 ** that file) and seek it to offset iOff within the file. Return SQLITE_OK
8316 ** if successful, or an SQLite error code if an error occurs.
8317 */
8318 static int vdbePmaReaderSeek(
8319 SortSubtask *pTask, /* Task context */
8320 PmaReader *pReadr, /* Reader whose cursor is to be moved */
8321 SorterFile *pFile, /* Sorter file to read from */
8322 i64 iOff /* Offset in pFile */
8323 ){
8324 int rc = SQLITE_OK;
8325
8326 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
8327
8328 if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ;
8329 if( pReadr->aMap ){
8330 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
8331 pReadr->aMap = 0;
8332 }
8333 pReadr->iReadOff = iOff;
8334 pReadr->iEof = pFile->iEof;
8335 pReadr->pFd = pFile->pFd;
8336
8337 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
8338 if( rc==SQLITE_OK && pReadr->aMap==0 ){
8339 int pgsz = pTask->pSorter->pgsz;
8340 int iBuf = pReadr->iReadOff % pgsz;
8341 if( pReadr->aBuffer==0 ){
8342 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
8343 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
8344 pReadr->nBuffer = pgsz;
8345 }
8346 if( rc==SQLITE_OK && iBuf ){
8347 int nRead = pgsz - iBuf;
8348 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
8349 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
8350 }
8351 rc = sqlite3OsRead(
8352 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
8353 );
8354 testcase( rc!=SQLITE_OK );
8355 }
8356 }
8357
8358 return rc;
8359 }
8360
8361 /*
8362 ** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
8363 ** no error occurs, or an SQLite error code if one does.
8364 */
8365 static int vdbePmaReaderNext(PmaReader *pReadr){
8366 int rc = SQLITE_OK; /* Return Code */
8367 u64 nRec = 0; /* Size of record in bytes */
8368
8369
8370 if( pReadr->iReadOff>=pReadr->iEof ){
8371 IncrMerger *pIncr = pReadr->pIncr;
8372 int bEof = 1;
8373 if( pIncr ){
8374 rc = vdbeIncrSwap(pIncr);
8375 if( rc==SQLITE_OK && pIncr->bEof==0 ){
8376 rc = vdbePmaReaderSeek(
8377 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
8378 );
8379 bEof = 0;
8380 }
8381 }
8382
8383 if( bEof ){
8384 /* This is an EOF condition */
8385 vdbePmaReaderClear(pReadr);
8386 testcase( rc!=SQLITE_OK );
8387 return rc;
8388 }
8389 }
8390
8391 if( rc==SQLITE_OK ){
8392 rc = vdbePmaReadVarint(pReadr, &nRec);
8393 }
8394 if( rc==SQLITE_OK ){
8395 pReadr->nKey = (int)nRec;
8396 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
8397 testcase( rc!=SQLITE_OK );
8398 }
8399
8400 return rc;
8401 }
8402
8403 /*
8404 ** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
8405 ** starting at offset iStart and ending at offset iEof-1. This function
8406 ** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
8407 ** PMA is empty).
8408 **
8409 ** If the pnByte parameter is NULL, then it is assumed that the file
8410 ** contains a single PMA, and that that PMA omits the initial length varint.
8411 */
8412 static int vdbePmaReaderInit(
8413 SortSubtask *pTask, /* Task context */
8414 SorterFile *pFile, /* Sorter file to read from */
8415 i64 iStart, /* Start offset in pFile */
8416 PmaReader *pReadr, /* PmaReader to populate */
8417 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
8418 ){
8419 int rc;
8420
8421 assert( pFile->iEof>iStart );
8422 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
8423 assert( pReadr->aBuffer==0 );
8424 assert( pReadr->aMap==0 );
8425
8426 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
8427 if( rc==SQLITE_OK ){
8428 u64 nByte = 0; /* Size of PMA in bytes */
8429 rc = vdbePmaReadVarint(pReadr, &nByte);
8430 pReadr->iEof = pReadr->iReadOff + nByte;
8431 *pnByte += nByte;
8432 }
8433
8434 if( rc==SQLITE_OK ){
8435 rc = vdbePmaReaderNext(pReadr);
8436 }
8437 return rc;
8438 }
8439
8440 /*
8441 ** A version of vdbeSorterCompare() that assumes that it has already been
8442 ** determined that the first field of key1 is equal to the first field of
8443 ** key2.
8444 */
8445 static int vdbeSorterCompareTail(
8446 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
8447 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
8448 const void *pKey1, int nKey1, /* Left side of comparison */
8449 const void *pKey2, int nKey2 /* Right side of comparison */
8450 ){
8451 UnpackedRecord *r2 = pTask->pUnpacked;
8452 if( *pbKey2Cached==0 ){
8453 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
8454 *pbKey2Cached = 1;
8455 }
8456 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
8457 }
8458
8459 /*
8460 ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
8461 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
8462 ** used by the comparison. Return the result of the comparison.
8463 **
8464 ** If IN/OUT parameter *pbKey2Cached is true when this function is called,
8465 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
8466 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
8467 ** version of key2 and *pbKey2Cached set to true before returning.
8468 **
8469 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
8470 ** to SQLITE_NOMEM.
8471 */
8472 static int vdbeSorterCompare(
8473 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
8474 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
8475 const void *pKey1, int nKey1, /* Left side of comparison */
8476 const void *pKey2, int nKey2 /* Right side of comparison */
8477 ){
8478 UnpackedRecord *r2 = pTask->pUnpacked;
8479 if( !*pbKey2Cached ){
8480 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
8481 *pbKey2Cached = 1;
8482 }
8483 return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
8484 }
8485
8486 /*
8487 ** A specially optimized version of vdbeSorterCompare() that assumes that
8488 ** the first field of each key is a TEXT value and that the collation
8489 ** sequence to compare them with is BINARY.
8490 */
8491 static int vdbeSorterCompareText(
8492 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
8493 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
8494 const void *pKey1, int nKey1, /* Left side of comparison */
8495 const void *pKey2, int nKey2 /* Right side of comparison */
8496 ){
8497 const u8 * const p1 = (const u8 * const)pKey1;
8498 const u8 * const p2 = (const u8 * const)pKey2;
8499 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
8500 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
8501
8502 int n1;
8503 int n2;
8504 int res;
8505
8506 getVarint32(&p1[1], n1); n1 = (n1 - 13) / 2;
8507 getVarint32(&p2[1], n2); n2 = (n2 - 13) / 2;
8508 res = memcmp(v1, v2, MIN(n1, n2));
8509 if( res==0 ){
8510 res = n1 - n2;
8511 }
8512
8513 if( res==0 ){
8514 if( pTask->pSorter->pKeyInfo->nField>1 ){
8515 res = vdbeSorterCompareTail(
8516 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
8517 );
8518 }
8519 }else{
8520 if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
8521 res = res * -1;
8522 }
8523 }
8524
8525 return res;
8526 }
8527
8528 /*
8529 ** A specially optimized version of vdbeSorterCompare() that assumes that
8530 ** the first field of each key is an INTEGER value.
8531 */
8532 static int vdbeSorterCompareInt(
8533 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
8534 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
8535 const void *pKey1, int nKey1, /* Left side of comparison */
8536 const void *pKey2, int nKey2 /* Right side of comparison */
8537 ){
8538 const u8 * const p1 = (const u8 * const)pKey1;
8539 const u8 * const p2 = (const u8 * const)pKey2;
8540 const int s1 = p1[1]; /* Left hand serial type */
8541 const int s2 = p2[1]; /* Right hand serial type */
8542 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
8543 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
8544 int res; /* Return value */
8545
8546 assert( (s1>0 && s1<7) || s1==8 || s1==9 );
8547 assert( (s2>0 && s2<7) || s2==8 || s2==9 );
8548
8549 if( s1>7 && s2>7 ){
8550 res = s1 - s2;
8551 }else{
8552 if( s1==s2 ){
8553 if( (*v1 ^ *v2) & 0x80 ){
8554 /* The two values have different signs */
8555 res = (*v1 & 0x80) ? -1 : +1;
8556 }else{
8557 /* The two values have the same sign. Compare using memcmp(). */
8558 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 };
8559 int i;
8560 res = 0;
8561 for(i=0; i<aLen[s1]; i++){
8562 if( (res = v1[i] - v2[i]) ) break;
8563 }
8564 }
8565 }else{
8566 if( s2>7 ){
8567 res = +1;
8568 }else if( s1>7 ){
8569 res = -1;
8570 }else{
8571 res = s1 - s2;
8572 }
8573 assert( res!=0 );
8574
8575 if( res>0 ){
8576 if( *v1 & 0x80 ) res = -1;
8577 }else{
8578 if( *v2 & 0x80 ) res = +1;
8579 }
8580 }
8581 }
8582
8583 if( res==0 ){
8584 if( pTask->pSorter->pKeyInfo->nField>1 ){
8585 res = vdbeSorterCompareTail(
8586 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
8587 );
8588 }
8589 }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
8590 res = res * -1;
8591 }
8592
8593 return res;
8594 }
8595
8596 /*
8597 ** Initialize the temporary index cursor just opened as a sorter cursor.
8598 **
8599 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField)
8600 ** to determine the number of fields that should be compared from the
8601 ** records being sorted. However, if the value passed as argument nField
8602 ** is non-zero and the sorter is able to guarantee a stable sort, nField
8603 ** is used instead. This is used when sorting records for a CREATE INDEX
8604 ** statement. In this case, keys are always delivered to the sorter in
8605 ** order of the primary key, which happens to be make up the final part
8606 ** of the records being sorted. So if the sort is stable, there is never
8607 ** any reason to compare PK fields and they can be ignored for a small
8608 ** performance boost.
8609 **
8610 ** The sorter can guarantee a stable sort when running in single-threaded
8611 ** mode, but not in multi-threaded mode.
8612 **
8613 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
8614 */
8615 SQLITE_PRIVATE int sqlite3VdbeSorterInit(
8616 sqlite3 *db, /* Database connection (for malloc()) */
8617 int nField, /* Number of key fields in each record */
8618 VdbeCursor *pCsr /* Cursor that holds the new sorter */
8619 ){
8620 int pgsz; /* Page size of main database */
8621 int i; /* Used to iterate through aTask[] */
8622 VdbeSorter *pSorter; /* The new sorter */
8623 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
8624 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
8625 int sz; /* Size of pSorter in bytes */
8626 int rc = SQLITE_OK;
8627 #if SQLITE_MAX_WORKER_THREADS==0
8628 # define nWorker 0
8629 #else
8630 int nWorker;
8631 #endif
8632
8633 /* Initialize the upper limit on the number of worker threads */
8634 #if SQLITE_MAX_WORKER_THREADS>0
8635 if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){
8636 nWorker = 0;
8637 }else{
8638 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
8639 }
8640 #endif
8641
8642 /* Do not allow the total number of threads (main thread + all workers)
8643 ** to exceed the maximum merge count */
8644 #if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
8645 if( nWorker>=SORTER_MAX_MERGE_COUNT ){
8646 nWorker = SORTER_MAX_MERGE_COUNT-1;
8647 }
8648 #endif
8649
8650 assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
8651 assert( pCsr->eCurType==CURTYPE_SORTER );
8652 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*);
8653 sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
8654
8655 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
8656 pCsr->uc.pSorter = pSorter;
8657 if( pSorter==0 ){
8658 rc = SQLITE_NOMEM_BKPT;
8659 }else{
8660 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
8661 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
8662 pKeyInfo->db = 0;
8663 if( nField && nWorker==0 ){
8664 pKeyInfo->nXField += (pKeyInfo->nField - nField);
8665 pKeyInfo->nField = nField;
8666 }
8667 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
8668 pSorter->nTask = nWorker + 1;
8669 pSorter->iPrev = (u8)(nWorker - 1);
8670 pSorter->bUseThreads = (pSorter->nTask>1);
8671 pSorter->db = db;
8672 for(i=0; i<pSorter->nTask; i++){
8673 SortSubtask *pTask = &pSorter->aTask[i];
8674 pTask->pSorter = pSorter;
8675 }
8676
8677 if( !sqlite3TempInMemory(db) ){
8678 i64 mxCache; /* Cache size in bytes*/
8679 u32 szPma = sqlite3GlobalConfig.szPma;
8680 pSorter->mnPmaSize = szPma * pgsz;
8681
8682 mxCache = db->aDb[0].pSchema->cache_size;
8683 if( mxCache<0 ){
8684 /* A negative cache-size value C indicates that the cache is abs(C)
8685 ** KiB in size. */
8686 mxCache = mxCache * -1024;
8687 }else{
8688 mxCache = mxCache * pgsz;
8689 }
8690 mxCache = MIN(mxCache, SQLITE_MAX_PMASZ);
8691 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
8692
8693 /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of
8694 ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary
8695 ** large heap allocations.
8696 */
8697 if( sqlite3GlobalConfig.pScratch==0 ){
8698 assert( pSorter->iMemory==0 );
8699 pSorter->nMemory = pgsz;
8700 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
8701 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
8702 }
8703 }
8704
8705 if( (pKeyInfo->nField+pKeyInfo->nXField)<13
8706 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
8707 ){
8708 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
8709 }
8710 }
8711
8712 return rc;
8713 }
8714 #undef nWorker /* Defined at the top of this function */
8715
8716 /*
8717 ** Free the list of sorted records starting at pRecord.
8718 */
8719 static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
8720 SorterRecord *p;
8721 SorterRecord *pNext;
8722 for(p=pRecord; p; p=pNext){
8723 pNext = p->u.pNext;
8724 sqlite3DbFree(db, p);
8725 }
8726 }
8727
8728 /*
8729 ** Free all resources owned by the object indicated by argument pTask. All
8730 ** fields of *pTask are zeroed before returning.
8731 */
8732 static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
8733 sqlite3DbFree(db, pTask->pUnpacked);
8734 #if SQLITE_MAX_WORKER_THREADS>0
8735 /* pTask->list.aMemory can only be non-zero if it was handed memory
8736 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
8737 if( pTask->list.aMemory ){
8738 sqlite3_free(pTask->list.aMemory);
8739 }else
8740 #endif
8741 {
8742 assert( pTask->list.aMemory==0 );
8743 vdbeSorterRecordFree(0, pTask->list.pList);
8744 }
8745 if( pTask->file.pFd ){
8746 sqlite3OsCloseFree(pTask->file.pFd);
8747 }
8748 if( pTask->file2.pFd ){
8749 sqlite3OsCloseFree(pTask->file2.pFd);
8750 }
8751 memset(pTask, 0, sizeof(SortSubtask));
8752 }
8753
8754 #ifdef SQLITE_DEBUG_SORTER_THREADS
8755 static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
8756 i64 t;
8757 int iTask = (pTask - pTask->pSorter->aTask);
8758 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
8759 fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent);
8760 }
8761 static void vdbeSorterRewindDebug(const char *zEvent){
8762 i64 t;
8763 sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t);
8764 fprintf(stderr, "%lld:X %s\n", t, zEvent);
8765 }
8766 static void vdbeSorterPopulateDebug(
8767 SortSubtask *pTask,
8768 const char *zEvent
8769 ){
8770 i64 t;
8771 int iTask = (pTask - pTask->pSorter->aTask);
8772 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
8773 fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent);
8774 }
8775 static void vdbeSorterBlockDebug(
8776 SortSubtask *pTask,
8777 int bBlocked,
8778 const char *zEvent
8779 ){
8780 if( bBlocked ){
8781 i64 t;
8782 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
8783 fprintf(stderr, "%lld:main %s\n", t, zEvent);
8784 }
8785 }
8786 #else
8787 # define vdbeSorterWorkDebug(x,y)
8788 # define vdbeSorterRewindDebug(y)
8789 # define vdbeSorterPopulateDebug(x,y)
8790 # define vdbeSorterBlockDebug(x,y,z)
8791 #endif
8792
8793 #if SQLITE_MAX_WORKER_THREADS>0
8794 /*
8795 ** Join thread pTask->thread.
8796 */
8797 static int vdbeSorterJoinThread(SortSubtask *pTask){
8798 int rc = SQLITE_OK;
8799 if( pTask->pThread ){
8800 #ifdef SQLITE_DEBUG_SORTER_THREADS
8801 int bDone = pTask->bDone;
8802 #endif
8803 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR);
8804 vdbeSorterBlockDebug(pTask, !bDone, "enter");
8805 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
8806 vdbeSorterBlockDebug(pTask, !bDone, "exit");
8807 rc = SQLITE_PTR_TO_INT(pRet);
8808 assert( pTask->bDone==1 );
8809 pTask->bDone = 0;
8810 pTask->pThread = 0;
8811 }
8812 return rc;
8813 }
8814
8815 /*
8816 ** Launch a background thread to run xTask(pIn).
8817 */
8818 static int vdbeSorterCreateThread(
8819 SortSubtask *pTask, /* Thread will use this task object */
8820 void *(*xTask)(void*), /* Routine to run in a separate thread */
8821 void *pIn /* Argument passed into xTask() */
8822 ){
8823 assert( pTask->pThread==0 && pTask->bDone==0 );
8824 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
8825 }
8826
8827 /*
8828 ** Join all outstanding threads launched by SorterWrite() to create
8829 ** level-0 PMAs.
8830 */
8831 static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
8832 int rc = rcin;
8833 int i;
8834
8835 /* This function is always called by the main user thread.
8836 **
8837 ** If this function is being called after SorterRewind() has been called,
8838 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
8839 ** is currently attempt to join one of the other threads. To avoid a race
8840 ** condition where this thread also attempts to join the same object, join
8841 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
8842 for(i=pSorter->nTask-1; i>=0; i--){
8843 SortSubtask *pTask = &pSorter->aTask[i];
8844 int rc2 = vdbeSorterJoinThread(pTask);
8845 if( rc==SQLITE_OK ) rc = rc2;
8846 }
8847 return rc;
8848 }
8849 #else
8850 # define vdbeSorterJoinAll(x,rcin) (rcin)
8851 # define vdbeSorterJoinThread(pTask) SQLITE_OK
8852 #endif
8853
8854 /*
8855 ** Allocate a new MergeEngine object capable of handling up to
8856 ** nReader PmaReader inputs.
8857 **
8858 ** nReader is automatically rounded up to the next power of two.
8859 ** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
8860 */
8861 static MergeEngine *vdbeMergeEngineNew(int nReader){
8862 int N = 2; /* Smallest power of two >= nReader */
8863 int nByte; /* Total bytes of space to allocate */
8864 MergeEngine *pNew; /* Pointer to allocated object to return */
8865
8866 assert( nReader<=SORTER_MAX_MERGE_COUNT );
8867
8868 while( N<nReader ) N += N;
8869 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
8870
8871 pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
8872 if( pNew ){
8873 pNew->nTree = N;
8874 pNew->pTask = 0;
8875 pNew->aReadr = (PmaReader*)&pNew[1];
8876 pNew->aTree = (int*)&pNew->aReadr[N];
8877 }
8878 return pNew;
8879 }
8880
8881 /*
8882 ** Free the MergeEngine object passed as the only argument.
8883 */
8884 static void vdbeMergeEngineFree(MergeEngine *pMerger){
8885 int i;
8886 if( pMerger ){
8887 for(i=0; i<pMerger->nTree; i++){
8888 vdbePmaReaderClear(&pMerger->aReadr[i]);
8889 }
8890 }
8891 sqlite3_free(pMerger);
8892 }
8893
8894 /*
8895 ** Free all resources associated with the IncrMerger object indicated by
8896 ** the first argument.
8897 */
8898 static void vdbeIncrFree(IncrMerger *pIncr){
8899 if( pIncr ){
8900 #if SQLITE_MAX_WORKER_THREADS>0
8901 if( pIncr->bUseThread ){
8902 vdbeSorterJoinThread(pIncr->pTask);
8903 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
8904 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
8905 }
8906 #endif
8907 vdbeMergeEngineFree(pIncr->pMerger);
8908 sqlite3_free(pIncr);
8909 }
8910 }
8911
8912 /*
8913 ** Reset a sorting cursor back to its original empty state.
8914 */
8915 SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
8916 int i;
8917 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK);
8918 assert( pSorter->bUseThreads || pSorter->pReader==0 );
8919 #if SQLITE_MAX_WORKER_THREADS>0
8920 if( pSorter->pReader ){
8921 vdbePmaReaderClear(pSorter->pReader);
8922 sqlite3DbFree(db, pSorter->pReader);
8923 pSorter->pReader = 0;
8924 }
8925 #endif
8926 vdbeMergeEngineFree(pSorter->pMerger);
8927 pSorter->pMerger = 0;
8928 for(i=0; i<pSorter->nTask; i++){
8929 SortSubtask *pTask = &pSorter->aTask[i];
8930 vdbeSortSubtaskCleanup(db, pTask);
8931 pTask->pSorter = pSorter;
8932 }
8933 if( pSorter->list.aMemory==0 ){
8934 vdbeSorterRecordFree(0, pSorter->list.pList);
8935 }
8936 pSorter->list.pList = 0;
8937 pSorter->list.szPMA = 0;
8938 pSorter->bUsePMA = 0;
8939 pSorter->iMemory = 0;
8940 pSorter->mxKeysize = 0;
8941 sqlite3DbFree(db, pSorter->pUnpacked);
8942 pSorter->pUnpacked = 0;
8943 }
8944
8945 /*
8946 ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
8947 */
8948 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
8949 VdbeSorter *pSorter;
8950 assert( pCsr->eCurType==CURTYPE_SORTER );
8951 pSorter = pCsr->uc.pSorter;
8952 if( pSorter ){
8953 sqlite3VdbeSorterReset(db, pSorter);
8954 sqlite3_free(pSorter->list.aMemory);
8955 sqlite3DbFree(db, pSorter);
8956 pCsr->uc.pSorter = 0;
8957 }
8958 }
8959
8960 #if SQLITE_MAX_MMAP_SIZE>0
8961 /*
8962 ** The first argument is a file-handle open on a temporary file. The file
8963 ** is guaranteed to be nByte bytes or smaller in size. This function
8964 ** attempts to extend the file to nByte bytes in size and to ensure that
8965 ** the VFS has memory mapped it.
8966 **
8967 ** Whether or not the file does end up memory mapped of course depends on
8968 ** the specific VFS implementation.
8969 */
8970 static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
8971 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
8972 void *p = 0;
8973 int chunksize = 4*1024;
8974 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize);
8975 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte);
8976 sqlite3OsFetch(pFd, 0, (int)nByte, &p);
8977 sqlite3OsUnfetch(pFd, 0, p);
8978 }
8979 }
8980 #else
8981 # define vdbeSorterExtendFile(x,y,z)
8982 #endif
8983
8984 /*
8985 ** Allocate space for a file-handle and open a temporary file. If successful,
8986 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
8987 ** Otherwise, set *ppFd to 0 and return an SQLite error code.
8988 */
8989 static int vdbeSorterOpenTempFile(
8990 sqlite3 *db, /* Database handle doing sort */
8991 i64 nExtend, /* Attempt to extend file to this size */
8992 sqlite3_file **ppFd
8993 ){
8994 int rc;
8995 if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS;
8996 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
8997 SQLITE_OPEN_TEMP_JOURNAL |
8998 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
8999 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc
9000 );
9001 if( rc==SQLITE_OK ){
9002 i64 max = SQLITE_MAX_MMAP_SIZE;
9003 sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE, (void*)&max);
9004 if( nExtend>0 ){
9005 vdbeSorterExtendFile(db, *ppFd, nExtend);
9006 }
9007 }
9008 return rc;
9009 }
9010
9011 /*
9012 ** If it has not already been allocated, allocate the UnpackedRecord
9013 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
9014 ** if no allocation was required), or SQLITE_NOMEM otherwise.
9015 */
9016 static int vdbeSortAllocUnpacked(SortSubtask *pTask){
9017 if( pTask->pUnpacked==0 ){
9018 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
9019 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
9020 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nField;
9021 pTask->pUnpacked->errCode = 0;
9022 }
9023 return SQLITE_OK;
9024 }
9025
9026
9027 /*
9028 ** Merge the two sorted lists p1 and p2 into a single list.
9029 */
9030 static SorterRecord *vdbeSorterMerge(
9031 SortSubtask *pTask, /* Calling thread context */
9032 SorterRecord *p1, /* First list to merge */
9033 SorterRecord *p2 /* Second list to merge */
9034 ){
9035 SorterRecord *pFinal = 0;
9036 SorterRecord **pp = &pFinal;
9037 int bCached = 0;
9038
9039 assert( p1!=0 && p2!=0 );
9040 for(;;){
9041 int res;
9042 res = pTask->xCompare(
9043 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
9044 );
9045
9046 if( res<=0 ){
9047 *pp = p1;
9048 pp = &p1->u.pNext;
9049 p1 = p1->u.pNext;
9050 if( p1==0 ){
9051 *pp = p2;
9052 break;
9053 }
9054 }else{
9055 *pp = p2;
9056 pp = &p2->u.pNext;
9057 p2 = p2->u.pNext;
9058 bCached = 0;
9059 if( p2==0 ){
9060 *pp = p1;
9061 break;
9062 }
9063 }
9064 }
9065 return pFinal;
9066 }
9067
9068 /*
9069 ** Return the SorterCompare function to compare values collected by the
9070 ** sorter object passed as the only argument.
9071 */
9072 static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
9073 if( p->typeMask==SORTER_TYPE_INTEGER ){
9074 return vdbeSorterCompareInt;
9075 }else if( p->typeMask==SORTER_TYPE_TEXT ){
9076 return vdbeSorterCompareText;
9077 }
9078 return vdbeSorterCompare;
9079 }
9080
9081 /*
9082 ** Sort the linked list of records headed at pTask->pList. Return
9083 ** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
9084 ** an error occurs.
9085 */
9086 static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
9087 int i;
9088 SorterRecord **aSlot;
9089 SorterRecord *p;
9090 int rc;
9091
9092 rc = vdbeSortAllocUnpacked(pTask);
9093 if( rc!=SQLITE_OK ) return rc;
9094
9095 p = pList->pList;
9096 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
9097
9098 aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
9099 if( !aSlot ){
9100 return SQLITE_NOMEM_BKPT;
9101 }
9102
9103 while( p ){
9104 SorterRecord *pNext;
9105 if( pList->aMemory ){
9106 if( (u8*)p==pList->aMemory ){
9107 pNext = 0;
9108 }else{
9109 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
9110 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
9111 }
9112 }else{
9113 pNext = p->u.pNext;
9114 }
9115
9116 p->u.pNext = 0;
9117 for(i=0; aSlot[i]; i++){
9118 p = vdbeSorterMerge(pTask, p, aSlot[i]);
9119 aSlot[i] = 0;
9120 }
9121 aSlot[i] = p;
9122 p = pNext;
9123 }
9124
9125 p = 0;
9126 for(i=0; i<64; i++){
9127 if( aSlot[i]==0 ) continue;
9128 p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
9129 }
9130 pList->pList = p;
9131
9132 sqlite3_free(aSlot);
9133 assert( pTask->pUnpacked->errCode==SQLITE_OK
9134 || pTask->pUnpacked->errCode==SQLITE_NOMEM
9135 );
9136 return pTask->pUnpacked->errCode;
9137 }
9138
9139 /*
9140 ** Initialize a PMA-writer object.
9141 */
9142 static void vdbePmaWriterInit(
9143 sqlite3_file *pFd, /* File handle to write to */
9144 PmaWriter *p, /* Object to populate */
9145 int nBuf, /* Buffer size */
9146 i64 iStart /* Offset of pFd to begin writing at */
9147 ){
9148 memset(p, 0, sizeof(PmaWriter));
9149 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
9150 if( !p->aBuffer ){
9151 p->eFWErr = SQLITE_NOMEM_BKPT;
9152 }else{
9153 p->iBufEnd = p->iBufStart = (iStart % nBuf);
9154 p->iWriteOff = iStart - p->iBufStart;
9155 p->nBuffer = nBuf;
9156 p->pFd = pFd;
9157 }
9158 }
9159
9160 /*
9161 ** Write nData bytes of data to the PMA. Return SQLITE_OK
9162 ** if successful, or an SQLite error code if an error occurs.
9163 */
9164 static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
9165 int nRem = nData;
9166 while( nRem>0 && p->eFWErr==0 ){
9167 int nCopy = nRem;
9168 if( nCopy>(p->nBuffer - p->iBufEnd) ){
9169 nCopy = p->nBuffer - p->iBufEnd;
9170 }
9171
9172 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
9173 p->iBufEnd += nCopy;
9174 if( p->iBufEnd==p->nBuffer ){
9175 p->eFWErr = sqlite3OsWrite(p->pFd,
9176 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
9177 p->iWriteOff + p->iBufStart
9178 );
9179 p->iBufStart = p->iBufEnd = 0;
9180 p->iWriteOff += p->nBuffer;
9181 }
9182 assert( p->iBufEnd<p->nBuffer );
9183
9184 nRem -= nCopy;
9185 }
9186 }
9187
9188 /*
9189 ** Flush any buffered data to disk and clean up the PMA-writer object.
9190 ** The results of using the PMA-writer after this call are undefined.
9191 ** Return SQLITE_OK if flushing the buffered data succeeds or is not
9192 ** required. Otherwise, return an SQLite error code.
9193 **
9194 ** Before returning, set *piEof to the offset immediately following the
9195 ** last byte written to the file.
9196 */
9197 static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
9198 int rc;
9199 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
9200 p->eFWErr = sqlite3OsWrite(p->pFd,
9201 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
9202 p->iWriteOff + p->iBufStart
9203 );
9204 }
9205 *piEof = (p->iWriteOff + p->iBufEnd);
9206 sqlite3_free(p->aBuffer);
9207 rc = p->eFWErr;
9208 memset(p, 0, sizeof(PmaWriter));
9209 return rc;
9210 }
9211
9212 /*
9213 ** Write value iVal encoded as a varint to the PMA. Return
9214 ** SQLITE_OK if successful, or an SQLite error code if an error occurs.
9215 */
9216 static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
9217 int nByte;
9218 u8 aByte[10];
9219 nByte = sqlite3PutVarint(aByte, iVal);
9220 vdbePmaWriteBlob(p, aByte, nByte);
9221 }
9222
9223 /*
9224 ** Write the current contents of in-memory linked-list pList to a level-0
9225 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
9226 ** successful, or an SQLite error code otherwise.
9227 **
9228 ** The format of a PMA is:
9229 **
9230 ** * A varint. This varint contains the total number of bytes of content
9231 ** in the PMA (not including the varint itself).
9232 **
9233 ** * One or more records packed end-to-end in order of ascending keys.
9234 ** Each record consists of a varint followed by a blob of data (the
9235 ** key). The varint is the number of bytes in the blob of data.
9236 */
9237 static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
9238 sqlite3 *db = pTask->pSorter->db;
9239 int rc = SQLITE_OK; /* Return code */
9240 PmaWriter writer; /* Object used to write to the file */
9241
9242 #ifdef SQLITE_DEBUG
9243 /* Set iSz to the expected size of file pTask->file after writing the PMA.
9244 ** This is used by an assert() statement at the end of this function. */
9245 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
9246 #endif
9247
9248 vdbeSorterWorkDebug(pTask, "enter");
9249 memset(&writer, 0, sizeof(PmaWriter));
9250 assert( pList->szPMA>0 );
9251
9252 /* If the first temporary PMA file has not been opened, open it now. */
9253 if( pTask->file.pFd==0 ){
9254 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
9255 assert( rc!=SQLITE_OK || pTask->file.pFd );
9256 assert( pTask->file.iEof==0 );
9257 assert( pTask->nPMA==0 );
9258 }
9259
9260 /* Try to get the file to memory map */
9261 if( rc==SQLITE_OK ){
9262 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
9263 }
9264
9265 /* Sort the list */
9266 if( rc==SQLITE_OK ){
9267 rc = vdbeSorterSort(pTask, pList);
9268 }
9269
9270 if( rc==SQLITE_OK ){
9271 SorterRecord *p;
9272 SorterRecord *pNext = 0;
9273
9274 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
9275 pTask->file.iEof);
9276 pTask->nPMA++;
9277 vdbePmaWriteVarint(&writer, pList->szPMA);
9278 for(p=pList->pList; p; p=pNext){
9279 pNext = p->u.pNext;
9280 vdbePmaWriteVarint(&writer, p->nVal);
9281 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
9282 if( pList->aMemory==0 ) sqlite3_free(p);
9283 }
9284 pList->pList = p;
9285 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
9286 }
9287
9288 vdbeSorterWorkDebug(pTask, "exit");
9289 assert( rc!=SQLITE_OK || pList->pList==0 );
9290 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
9291 return rc;
9292 }
9293
9294 /*
9295 ** Advance the MergeEngine to its next entry.
9296 ** Set *pbEof to true there is no next entry because
9297 ** the MergeEngine has reached the end of all its inputs.
9298 **
9299 ** Return SQLITE_OK if successful or an error code if an error occurs.
9300 */
9301 static int vdbeMergeEngineStep(
9302 MergeEngine *pMerger, /* The merge engine to advance to the next row */
9303 int *pbEof /* Set TRUE at EOF. Set false for more content */
9304 ){
9305 int rc;
9306 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
9307 SortSubtask *pTask = pMerger->pTask;
9308
9309 /* Advance the current PmaReader */
9310 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
9311
9312 /* Update contents of aTree[] */
9313 if( rc==SQLITE_OK ){
9314 int i; /* Index of aTree[] to recalculate */
9315 PmaReader *pReadr1; /* First PmaReader to compare */
9316 PmaReader *pReadr2; /* Second PmaReader to compare */
9317 int bCached = 0;
9318
9319 /* Find the first two PmaReaders to compare. The one that was just
9320 ** advanced (iPrev) and the one next to it in the array. */
9321 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
9322 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
9323
9324 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
9325 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
9326 int iRes;
9327 if( pReadr1->pFd==0 ){
9328 iRes = +1;
9329 }else if( pReadr2->pFd==0 ){
9330 iRes = -1;
9331 }else{
9332 iRes = pTask->xCompare(pTask, &bCached,
9333 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
9334 );
9335 }
9336
9337 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
9338 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
9339 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
9340 ** pKey2 to point to the record belonging to pReadr2.
9341 **
9342 ** Alternatively, if pReadr2 contains the smaller of the two values,
9343 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
9344 ** was actually called above, then pTask->pUnpacked now contains
9345 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
9346 ** vdbeSorterCompare() from decoding pReadr2 again.
9347 **
9348 ** If the two values were equal, then the value from the oldest
9349 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
9350 ** is sorted from oldest to newest, so pReadr1 contains older values
9351 ** than pReadr2 iff (pReadr1<pReadr2). */
9352 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
9353 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
9354 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
9355 bCached = 0;
9356 }else{
9357 if( pReadr1->pFd ) bCached = 0;
9358 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
9359 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
9360 }
9361 }
9362 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
9363 }
9364
9365 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
9366 }
9367
9368 #if SQLITE_MAX_WORKER_THREADS>0
9369 /*
9370 ** The main routine for background threads that write level-0 PMAs.
9371 */
9372 static void *vdbeSorterFlushThread(void *pCtx){
9373 SortSubtask *pTask = (SortSubtask*)pCtx;
9374 int rc; /* Return code */
9375 assert( pTask->bDone==0 );
9376 rc = vdbeSorterListToPMA(pTask, &pTask->list);
9377 pTask->bDone = 1;
9378 return SQLITE_INT_TO_PTR(rc);
9379 }
9380 #endif /* SQLITE_MAX_WORKER_THREADS>0 */
9381
9382 /*
9383 ** Flush the current contents of VdbeSorter.list to a new PMA, possibly
9384 ** using a background thread.
9385 */
9386 static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
9387 #if SQLITE_MAX_WORKER_THREADS==0
9388 pSorter->bUsePMA = 1;
9389 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
9390 #else
9391 int rc = SQLITE_OK;
9392 int i;
9393 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
9394 int nWorker = (pSorter->nTask-1);
9395
9396 /* Set the flag to indicate that at least one PMA has been written.
9397 ** Or will be, anyhow. */
9398 pSorter->bUsePMA = 1;
9399
9400 /* Select a sub-task to sort and flush the current list of in-memory
9401 ** records to disk. If the sorter is running in multi-threaded mode,
9402 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
9403 ** the background thread from a sub-tasks previous turn is still running,
9404 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
9405 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
9406 ** sub-tasks are prefered as they use background threads - the final
9407 ** sub-task uses the main thread. */
9408 for(i=0; i<nWorker; i++){
9409 int iTest = (pSorter->iPrev + i + 1) % nWorker;
9410 pTask = &pSorter->aTask[iTest];
9411 if( pTask->bDone ){
9412 rc = vdbeSorterJoinThread(pTask);
9413 }
9414 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
9415 }
9416
9417 if( rc==SQLITE_OK ){
9418 if( i==nWorker ){
9419 /* Use the foreground thread for this operation */
9420 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
9421 }else{
9422 /* Launch a background thread for this operation */
9423 u8 *aMem = pTask->list.aMemory;
9424 void *pCtx = (void*)pTask;
9425
9426 assert( pTask->pThread==0 && pTask->bDone==0 );
9427 assert( pTask->list.pList==0 );
9428 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
9429
9430 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
9431 pTask->list = pSorter->list;
9432 pSorter->list.pList = 0;
9433 pSorter->list.szPMA = 0;
9434 if( aMem ){
9435 pSorter->list.aMemory = aMem;
9436 pSorter->nMemory = sqlite3MallocSize(aMem);
9437 }else if( pSorter->list.aMemory ){
9438 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
9439 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
9440 }
9441
9442 rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
9443 }
9444 }
9445
9446 return rc;
9447 #endif /* SQLITE_MAX_WORKER_THREADS!=0 */
9448 }
9449
9450 /*
9451 ** Add a record to the sorter.
9452 */
9453 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
9454 const VdbeCursor *pCsr, /* Sorter cursor */
9455 Mem *pVal /* Memory cell containing record */
9456 ){
9457 VdbeSorter *pSorter;
9458 int rc = SQLITE_OK; /* Return Code */
9459 SorterRecord *pNew; /* New list element */
9460 int bFlush; /* True to flush contents of memory to PMA */
9461 int nReq; /* Bytes of memory required */
9462 int nPMA; /* Bytes of PMA space required */
9463 int t; /* serial type of first record field */
9464
9465 assert( pCsr->eCurType==CURTYPE_SORTER );
9466 pSorter = pCsr->uc.pSorter;
9467 getVarint32((const u8*)&pVal->z[1], t);
9468 if( t>0 && t<10 && t!=7 ){
9469 pSorter->typeMask &= SORTER_TYPE_INTEGER;
9470 }else if( t>10 && (t & 0x01) ){
9471 pSorter->typeMask &= SORTER_TYPE_TEXT;
9472 }else{
9473 pSorter->typeMask = 0;
9474 }
9475
9476 assert( pSorter );
9477
9478 /* Figure out whether or not the current contents of memory should be
9479 ** flushed to a PMA before continuing. If so, do so.
9480 **
9481 ** If using the single large allocation mode (pSorter->aMemory!=0), then
9482 ** flush the contents of memory to a new PMA if (a) at least one value is
9483 ** already in memory and (b) the new value will not fit in memory.
9484 **
9485 ** Or, if using separate allocations for each record, flush the contents
9486 ** of memory to a PMA if either of the following are true:
9487 **
9488 ** * The total memory allocated for the in-memory list is greater
9489 ** than (page-size * cache-size), or
9490 **
9491 ** * The total memory allocated for the in-memory list is greater
9492 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
9493 */
9494 nReq = pVal->n + sizeof(SorterRecord);
9495 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
9496 if( pSorter->mxPmaSize ){
9497 if( pSorter->list.aMemory ){
9498 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
9499 }else{
9500 bFlush = (
9501 (pSorter->list.szPMA > pSorter->mxPmaSize)
9502 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
9503 );
9504 }
9505 if( bFlush ){
9506 rc = vdbeSorterFlushPMA(pSorter);
9507 pSorter->list.szPMA = 0;
9508 pSorter->iMemory = 0;
9509 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
9510 }
9511 }
9512
9513 pSorter->list.szPMA += nPMA;
9514 if( nPMA>pSorter->mxKeysize ){
9515 pSorter->mxKeysize = nPMA;
9516 }
9517
9518 if( pSorter->list.aMemory ){
9519 int nMin = pSorter->iMemory + nReq;
9520
9521 if( nMin>pSorter->nMemory ){
9522 u8 *aNew;
9523 int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
9524 int nNew = pSorter->nMemory * 2;
9525 while( nNew < nMin ) nNew = nNew*2;
9526 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
9527 if( nNew < nMin ) nNew = nMin;
9528
9529 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
9530 if( !aNew ) return SQLITE_NOMEM_BKPT;
9531 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
9532 pSorter->list.aMemory = aNew;
9533 pSorter->nMemory = nNew;
9534 }
9535
9536 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
9537 pSorter->iMemory += ROUND8(nReq);
9538 if( pSorter->list.pList ){
9539 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
9540 }
9541 }else{
9542 pNew = (SorterRecord *)sqlite3Malloc(nReq);
9543 if( pNew==0 ){
9544 return SQLITE_NOMEM_BKPT;
9545 }
9546 pNew->u.pNext = pSorter->list.pList;
9547 }
9548
9549 memcpy(SRVAL(pNew), pVal->z, pVal->n);
9550 pNew->nVal = pVal->n;
9551 pSorter->list.pList = pNew;
9552
9553 return rc;
9554 }
9555
9556 /*
9557 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
9558 ** of the data stored in aFile[1] is the same as that used by regular PMAs,
9559 ** except that the number-of-bytes varint is omitted from the start.
9560 */
9561 static int vdbeIncrPopulate(IncrMerger *pIncr){
9562 int rc = SQLITE_OK;
9563 int rc2;
9564 i64 iStart = pIncr->iStartOff;
9565 SorterFile *pOut = &pIncr->aFile[1];
9566 SortSubtask *pTask = pIncr->pTask;
9567 MergeEngine *pMerger = pIncr->pMerger;
9568 PmaWriter writer;
9569 assert( pIncr->bEof==0 );
9570
9571 vdbeSorterPopulateDebug(pTask, "enter");
9572
9573 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
9574 while( rc==SQLITE_OK ){
9575 int dummy;
9576 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
9577 int nKey = pReader->nKey;
9578 i64 iEof = writer.iWriteOff + writer.iBufEnd;
9579
9580 /* Check if the output file is full or if the input has been exhausted.
9581 ** In either case exit the loop. */
9582 if( pReader->pFd==0 ) break;
9583 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
9584
9585 /* Write the next key to the output. */
9586 vdbePmaWriteVarint(&writer, nKey);
9587 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
9588 assert( pIncr->pMerger->pTask==pTask );
9589 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
9590 }
9591
9592 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
9593 if( rc==SQLITE_OK ) rc = rc2;
9594 vdbeSorterPopulateDebug(pTask, "exit");
9595 return rc;
9596 }
9597
9598 #if SQLITE_MAX_WORKER_THREADS>0
9599 /*
9600 ** The main routine for background threads that populate aFile[1] of
9601 ** multi-threaded IncrMerger objects.
9602 */
9603 static void *vdbeIncrPopulateThread(void *pCtx){
9604 IncrMerger *pIncr = (IncrMerger*)pCtx;
9605 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) );
9606 pIncr->pTask->bDone = 1;
9607 return pRet;
9608 }
9609
9610 /*
9611 ** Launch a background thread to populate aFile[1] of pIncr.
9612 */
9613 static int vdbeIncrBgPopulate(IncrMerger *pIncr){
9614 void *p = (void*)pIncr;
9615 assert( pIncr->bUseThread );
9616 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
9617 }
9618 #endif
9619
9620 /*
9621 ** This function is called when the PmaReader corresponding to pIncr has
9622 ** finished reading the contents of aFile[0]. Its purpose is to "refill"
9623 ** aFile[0] such that the PmaReader should start rereading it from the
9624 ** beginning.
9625 **
9626 ** For single-threaded objects, this is accomplished by literally reading
9627 ** keys from pIncr->pMerger and repopulating aFile[0].
9628 **
9629 ** For multi-threaded objects, all that is required is to wait until the
9630 ** background thread is finished (if it is not already) and then swap
9631 ** aFile[0] and aFile[1] in place. If the contents of pMerger have not
9632 ** been exhausted, this function also launches a new background thread
9633 ** to populate the new aFile[1].
9634 **
9635 ** SQLITE_OK is returned on success, or an SQLite error code otherwise.
9636 */
9637 static int vdbeIncrSwap(IncrMerger *pIncr){
9638 int rc = SQLITE_OK;
9639
9640 #if SQLITE_MAX_WORKER_THREADS>0
9641 if( pIncr->bUseThread ){
9642 rc = vdbeSorterJoinThread(pIncr->pTask);
9643
9644 if( rc==SQLITE_OK ){
9645 SorterFile f0 = pIncr->aFile[0];
9646 pIncr->aFile[0] = pIncr->aFile[1];
9647 pIncr->aFile[1] = f0;
9648 }
9649
9650 if( rc==SQLITE_OK ){
9651 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
9652 pIncr->bEof = 1;
9653 }else{
9654 rc = vdbeIncrBgPopulate(pIncr);
9655 }
9656 }
9657 }else
9658 #endif
9659 {
9660 rc = vdbeIncrPopulate(pIncr);
9661 pIncr->aFile[0] = pIncr->aFile[1];
9662 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
9663 pIncr->bEof = 1;
9664 }
9665 }
9666
9667 return rc;
9668 }
9669
9670 /*
9671 ** Allocate and return a new IncrMerger object to read data from pMerger.
9672 **
9673 ** If an OOM condition is encountered, return NULL. In this case free the
9674 ** pMerger argument before returning.
9675 */
9676 static int vdbeIncrMergerNew(
9677 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
9678 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
9679 IncrMerger **ppOut /* Write the new IncrMerger here */
9680 ){
9681 int rc = SQLITE_OK;
9682 IncrMerger *pIncr = *ppOut = (IncrMerger*)
9683 (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
9684 if( pIncr ){
9685 pIncr->pMerger = pMerger;
9686 pIncr->pTask = pTask;
9687 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
9688 pTask->file2.iEof += pIncr->mxSz;
9689 }else{
9690 vdbeMergeEngineFree(pMerger);
9691 rc = SQLITE_NOMEM_BKPT;
9692 }
9693 return rc;
9694 }
9695
9696 #if SQLITE_MAX_WORKER_THREADS>0
9697 /*
9698 ** Set the "use-threads" flag on object pIncr.
9699 */
9700 static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
9701 pIncr->bUseThread = 1;
9702 pIncr->pTask->file2.iEof -= pIncr->mxSz;
9703 }
9704 #endif /* SQLITE_MAX_WORKER_THREADS>0 */
9705
9706
9707
9708 /*
9709 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
9710 ** two PmaReaders that feed that entry. Neither of the PmaReaders
9711 ** are advanced. This routine merely does the comparison.
9712 */
9713 static void vdbeMergeEngineCompare(
9714 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
9715 int iOut /* Store the result in pMerger->aTree[iOut] */
9716 ){
9717 int i1;
9718 int i2;
9719 int iRes;
9720 PmaReader *p1;
9721 PmaReader *p2;
9722
9723 assert( iOut<pMerger->nTree && iOut>0 );
9724
9725 if( iOut>=(pMerger->nTree/2) ){
9726 i1 = (iOut - pMerger->nTree/2) * 2;
9727 i2 = i1 + 1;
9728 }else{
9729 i1 = pMerger->aTree[iOut*2];
9730 i2 = pMerger->aTree[iOut*2+1];
9731 }
9732
9733 p1 = &pMerger->aReadr[i1];
9734 p2 = &pMerger->aReadr[i2];
9735
9736 if( p1->pFd==0 ){
9737 iRes = i2;
9738 }else if( p2->pFd==0 ){
9739 iRes = i1;
9740 }else{
9741 SortSubtask *pTask = pMerger->pTask;
9742 int bCached = 0;
9743 int res;
9744 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
9745 res = pTask->xCompare(
9746 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
9747 );
9748 if( res<=0 ){
9749 iRes = i1;
9750 }else{
9751 iRes = i2;
9752 }
9753 }
9754
9755 pMerger->aTree[iOut] = iRes;
9756 }
9757
9758 /*
9759 ** Allowed values for the eMode parameter to vdbeMergeEngineInit()
9760 ** and vdbePmaReaderIncrMergeInit().
9761 **
9762 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
9763 ** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
9764 ** when there exists one or more separate worker threads.
9765 */
9766 #define INCRINIT_NORMAL 0
9767 #define INCRINIT_TASK 1
9768 #define INCRINIT_ROOT 2
9769
9770 /*
9771 ** Forward reference required as the vdbeIncrMergeInit() and
9772 ** vdbePmaReaderIncrInit() routines are called mutually recursively when
9773 ** building a merge tree.
9774 */
9775 static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
9776
9777 /*
9778 ** Initialize the MergeEngine object passed as the second argument. Once this
9779 ** function returns, the first key of merged data may be read from the
9780 ** MergeEngine object in the usual fashion.
9781 **
9782 ** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
9783 ** objects attached to the PmaReader objects that the merger reads from have
9784 ** already been populated, but that they have not yet populated aFile[0] and
9785 ** set the PmaReader objects up to read from it. In this case all that is
9786 ** required is to call vdbePmaReaderNext() on each PmaReader to point it at
9787 ** its first key.
9788 **
9789 ** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
9790 ** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
9791 ** to pMerger.
9792 **
9793 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
9794 */
9795 static int vdbeMergeEngineInit(
9796 SortSubtask *pTask, /* Thread that will run pMerger */
9797 MergeEngine *pMerger, /* MergeEngine to initialize */
9798 int eMode /* One of the INCRINIT_XXX constants */
9799 ){
9800 int rc = SQLITE_OK; /* Return code */
9801 int i; /* For looping over PmaReader objects */
9802 int nTree = pMerger->nTree;
9803
9804 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
9805 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
9806
9807 /* Verify that the MergeEngine is assigned to a single thread */
9808 assert( pMerger->pTask==0 );
9809 pMerger->pTask = pTask;
9810
9811 for(i=0; i<nTree; i++){
9812 if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){
9813 /* PmaReaders should be normally initialized in order, as if they are
9814 ** reading from the same temp file this makes for more linear file IO.
9815 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
9816 ** in use it will block the vdbePmaReaderNext() call while it uses
9817 ** the main thread to fill its buffer. So calling PmaReaderNext()
9818 ** on this PmaReader before any of the multi-threaded PmaReaders takes
9819 ** better advantage of multi-processor hardware. */
9820 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
9821 }else{
9822 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
9823 }
9824 if( rc!=SQLITE_OK ) return rc;
9825 }
9826
9827 for(i=pMerger->nTree-1; i>0; i--){
9828 vdbeMergeEngineCompare(pMerger, i);
9829 }
9830 return pTask->pUnpacked->errCode;
9831 }
9832
9833 /*
9834 ** The PmaReader passed as the first argument is guaranteed to be an
9835 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
9836 ** and/or initialize the temp file related fields of the IncrMerge
9837 ** object at (pReadr->pIncr).
9838 **
9839 ** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
9840 ** in the sub-tree headed by pReadr are also initialized. Data is then
9841 ** loaded into the buffers belonging to pReadr and it is set to point to
9842 ** the first key in its range.
9843 **
9844 ** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
9845 ** to be a multi-threaded PmaReader and this function is being called in a
9846 ** background thread. In this case all PmaReaders in the sub-tree are
9847 ** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
9848 ** pReadr is populated. However, pReadr itself is not set up to point
9849 ** to its first key. A call to vdbePmaReaderNext() is still required to do
9850 ** that.
9851 **
9852 ** The reason this function does not call vdbePmaReaderNext() immediately
9853 ** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
9854 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
9855 ** this entire function is being run by thread (pTask->thread), that will
9856 ** lead to the current background thread attempting to join itself.
9857 **
9858 ** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
9859 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
9860 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
9861 ** In this case vdbePmaReaderNext() is called on all child PmaReaders and
9862 ** the current PmaReader set to point to the first key in its range.
9863 **
9864 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
9865 */
9866 static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
9867 int rc = SQLITE_OK;
9868 IncrMerger *pIncr = pReadr->pIncr;
9869 SortSubtask *pTask = pIncr->pTask;
9870 sqlite3 *db = pTask->pSorter->db;
9871
9872 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
9873 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
9874
9875 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
9876
9877 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
9878 ** requires two temp files to itself, whereas a single-threaded object
9879 ** only requires a region of pTask->file2. */
9880 if( rc==SQLITE_OK ){
9881 int mxSz = pIncr->mxSz;
9882 #if SQLITE_MAX_WORKER_THREADS>0
9883 if( pIncr->bUseThread ){
9884 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
9885 if( rc==SQLITE_OK ){
9886 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
9887 }
9888 }else
9889 #endif
9890 /*if( !pIncr->bUseThread )*/{
9891 if( pTask->file2.pFd==0 ){
9892 assert( pTask->file2.iEof>0 );
9893 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
9894 pTask->file2.iEof = 0;
9895 }
9896 if( rc==SQLITE_OK ){
9897 pIncr->aFile[1].pFd = pTask->file2.pFd;
9898 pIncr->iStartOff = pTask->file2.iEof;
9899 pTask->file2.iEof += mxSz;
9900 }
9901 }
9902 }
9903
9904 #if SQLITE_MAX_WORKER_THREADS>0
9905 if( rc==SQLITE_OK && pIncr->bUseThread ){
9906 /* Use the current thread to populate aFile[1], even though this
9907 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
9908 ** then this function is already running in background thread
9909 ** pIncr->pTask->thread.
9910 **
9911 ** If this is the INCRINIT_ROOT object, then it is running in the
9912 ** main VDBE thread. But that is Ok, as that thread cannot return
9913 ** control to the VDBE or proceed with anything useful until the
9914 ** first results are ready from this merger object anyway.
9915 */
9916 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
9917 rc = vdbeIncrPopulate(pIncr);
9918 }
9919 #endif
9920
9921 if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
9922 rc = vdbePmaReaderNext(pReadr);
9923 }
9924
9925 return rc;
9926 }
9927
9928 #if SQLITE_MAX_WORKER_THREADS>0
9929 /*
9930 ** The main routine for vdbePmaReaderIncrMergeInit() operations run in
9931 ** background threads.
9932 */
9933 static void *vdbePmaReaderBgIncrInit(void *pCtx){
9934 PmaReader *pReader = (PmaReader*)pCtx;
9935 void *pRet = SQLITE_INT_TO_PTR(
9936 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
9937 );
9938 pReader->pIncr->pTask->bDone = 1;
9939 return pRet;
9940 }
9941 #endif
9942
9943 /*
9944 ** If the PmaReader passed as the first argument is not an incremental-reader
9945 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
9946 ** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
9947 ** this routine to initialize the incremental merge.
9948 **
9949 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
9950 ** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
9951 ** Or, if the IncrMerger is single threaded, the same function is called
9952 ** using the current thread.
9953 */
9954 static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
9955 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
9956 int rc = SQLITE_OK; /* Return code */
9957 if( pIncr ){
9958 #if SQLITE_MAX_WORKER_THREADS>0
9959 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
9960 if( pIncr->bUseThread ){
9961 void *pCtx = (void*)pReadr;
9962 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
9963 }else
9964 #endif
9965 {
9966 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
9967 }
9968 }
9969 return rc;
9970 }
9971
9972 /*
9973 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
9974 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
9975 ** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
9976 ** to NULL and return an SQLite error code.
9977 **
9978 ** When this function is called, *piOffset is set to the offset of the
9979 ** first PMA to read from pTask->file. Assuming no error occurs, it is
9980 ** set to the offset immediately following the last byte of the last
9981 ** PMA before returning. If an error does occur, then the final value of
9982 ** *piOffset is undefined.
9983 */
9984 static int vdbeMergeEngineLevel0(
9985 SortSubtask *pTask, /* Sorter task to read from */
9986 int nPMA, /* Number of PMAs to read */
9987 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
9988 MergeEngine **ppOut /* OUT: New merge-engine */
9989 ){
9990 MergeEngine *pNew; /* Merge engine to return */
9991 i64 iOff = *piOffset;
9992 int i;
9993 int rc = SQLITE_OK;
9994
9995 *ppOut = pNew = vdbeMergeEngineNew(nPMA);
9996 if( pNew==0 ) rc = SQLITE_NOMEM_BKPT;
9997
9998 for(i=0; i<nPMA && rc==SQLITE_OK; i++){
9999 i64 nDummy = 0;
10000 PmaReader *pReadr = &pNew->aReadr[i];
10001 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
10002 iOff = pReadr->iEof;
10003 }
10004
10005 if( rc!=SQLITE_OK ){
10006 vdbeMergeEngineFree(pNew);
10007 *ppOut = 0;
10008 }
10009 *piOffset = iOff;
10010 return rc;
10011 }
10012
10013 /*
10014 ** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
10015 ** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
10016 **
10017 ** i.e.
10018 **
10019 ** nPMA<=16 -> TreeDepth() == 0
10020 ** nPMA<=256 -> TreeDepth() == 1
10021 ** nPMA<=65536 -> TreeDepth() == 2
10022 */
10023 static int vdbeSorterTreeDepth(int nPMA){
10024 int nDepth = 0;
10025 i64 nDiv = SORTER_MAX_MERGE_COUNT;
10026 while( nDiv < (i64)nPMA ){
10027 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
10028 nDepth++;
10029 }
10030 return nDepth;
10031 }
10032
10033 /*
10034 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
10035 ** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
10036 ** tree, counting from zero. This function adds pLeaf to the tree.
10037 **
10038 ** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
10039 ** code is returned and pLeaf is freed.
10040 */
10041 static int vdbeSorterAddToTree(
10042 SortSubtask *pTask, /* Task context */
10043 int nDepth, /* Depth of tree according to TreeDepth() */
10044 int iSeq, /* Sequence number of leaf within tree */
10045 MergeEngine *pRoot, /* Root of tree */
10046 MergeEngine *pLeaf /* Leaf to add to tree */
10047 ){
10048 int rc = SQLITE_OK;
10049 int nDiv = 1;
10050 int i;
10051 MergeEngine *p = pRoot;
10052 IncrMerger *pIncr;
10053
10054 rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
10055
10056 for(i=1; i<nDepth; i++){
10057 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
10058 }
10059
10060 for(i=1; i<nDepth && rc==SQLITE_OK; i++){
10061 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
10062 PmaReader *pReadr = &p->aReadr[iIter];
10063
10064 if( pReadr->pIncr==0 ){
10065 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
10066 if( pNew==0 ){
10067 rc = SQLITE_NOMEM_BKPT;
10068 }else{
10069 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
10070 }
10071 }
10072 if( rc==SQLITE_OK ){
10073 p = pReadr->pIncr->pMerger;
10074 nDiv = nDiv / SORTER_MAX_MERGE_COUNT;
10075 }
10076 }
10077
10078 if( rc==SQLITE_OK ){
10079 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
10080 }else{
10081 vdbeIncrFree(pIncr);
10082 }
10083 return rc;
10084 }
10085
10086 /*
10087 ** This function is called as part of a SorterRewind() operation on a sorter
10088 ** that has already written two or more level-0 PMAs to one or more temp
10089 ** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
10090 ** can be used to incrementally merge all PMAs on disk.
10091 **
10092 ** If successful, SQLITE_OK is returned and *ppOut set to point to the
10093 ** MergeEngine object at the root of the tree before returning. Or, if an
10094 ** error occurs, an SQLite error code is returned and the final value
10095 ** of *ppOut is undefined.
10096 */
10097 static int vdbeSorterMergeTreeBuild(
10098 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
10099 MergeEngine **ppOut /* Write the MergeEngine here */
10100 ){
10101 MergeEngine *pMain = 0;
10102 int rc = SQLITE_OK;
10103 int iTask;
10104
10105 #if SQLITE_MAX_WORKER_THREADS>0
10106 /* If the sorter uses more than one task, then create the top-level
10107 ** MergeEngine here. This MergeEngine will read data from exactly
10108 ** one PmaReader per sub-task. */
10109 assert( pSorter->bUseThreads || pSorter->nTask==1 );
10110 if( pSorter->nTask>1 ){
10111 pMain = vdbeMergeEngineNew(pSorter->nTask);
10112 if( pMain==0 ) rc = SQLITE_NOMEM_BKPT;
10113 }
10114 #endif
10115
10116 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
10117 SortSubtask *pTask = &pSorter->aTask[iTask];
10118 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
10119 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
10120 MergeEngine *pRoot = 0; /* Root node of tree for this task */
10121 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
10122 i64 iReadOff = 0;
10123
10124 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
10125 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
10126 }else{
10127 int i;
10128 int iSeq = 0;
10129 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
10130 if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT;
10131 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
10132 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
10133 int nReader; /* Number of level-0 PMAs to merge */
10134
10135 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
10136 rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
10137 if( rc==SQLITE_OK ){
10138 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
10139 }
10140 }
10141 }
10142
10143 if( rc==SQLITE_OK ){
10144 #if SQLITE_MAX_WORKER_THREADS>0
10145 if( pMain!=0 ){
10146 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
10147 }else
10148 #endif
10149 {
10150 assert( pMain==0 );
10151 pMain = pRoot;
10152 }
10153 }else{
10154 vdbeMergeEngineFree(pRoot);
10155 }
10156 }
10157 }
10158
10159 if( rc!=SQLITE_OK ){
10160 vdbeMergeEngineFree(pMain);
10161 pMain = 0;
10162 }
10163 *ppOut = pMain;
10164 return rc;
10165 }
10166
10167 /*
10168 ** This function is called as part of an sqlite3VdbeSorterRewind() operation
10169 ** on a sorter that has written two or more PMAs to temporary files. It sets
10170 ** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
10171 ** (for multi-threaded sorters) so that it can be used to iterate through
10172 ** all records stored in the sorter.
10173 **
10174 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
10175 */
10176 static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
10177 int rc; /* Return code */
10178 SortSubtask *pTask0 = &pSorter->aTask[0];
10179 MergeEngine *pMain = 0;
10180 #if SQLITE_MAX_WORKER_THREADS
10181 sqlite3 *db = pTask0->pSorter->db;
10182 int i;
10183 SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
10184 for(i=0; i<pSorter->nTask; i++){
10185 pSorter->aTask[i].xCompare = xCompare;
10186 }
10187 #endif
10188
10189 rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
10190 if( rc==SQLITE_OK ){
10191 #if SQLITE_MAX_WORKER_THREADS
10192 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
10193 if( pSorter->bUseThreads ){
10194 int iTask;
10195 PmaReader *pReadr = 0;
10196 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
10197 rc = vdbeSortAllocUnpacked(pLast);
10198 if( rc==SQLITE_OK ){
10199 pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
10200 pSorter->pReader = pReadr;
10201 if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT;
10202 }
10203 if( rc==SQLITE_OK ){
10204 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
10205 if( rc==SQLITE_OK ){
10206 vdbeIncrMergerSetThreads(pReadr->pIncr);
10207 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
10208 IncrMerger *pIncr;
10209 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
10210 vdbeIncrMergerSetThreads(pIncr);
10211 assert( pIncr->pTask!=pLast );
10212 }
10213 }
10214 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
10215 /* Check that:
10216 **
10217 ** a) The incremental merge object is configured to use the
10218 ** right task, and
10219 ** b) If it is using task (nTask-1), it is configured to run
10220 ** in single-threaded mode. This is important, as the
10221 ** root merge (INCRINIT_ROOT) will be using the same task
10222 ** object.
10223 */
10224 PmaReader *p = &pMain->aReadr[iTask];
10225 assert( p->pIncr==0 || (
10226 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
10227 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
10228 ));
10229 rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK);
10230 }
10231 }
10232 pMain = 0;
10233 }
10234 if( rc==SQLITE_OK ){
10235 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
10236 }
10237 }else
10238 #endif
10239 {
10240 rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL);
10241 pSorter->pMerger = pMain;
10242 pMain = 0;
10243 }
10244 }
10245
10246 if( rc!=SQLITE_OK ){
10247 vdbeMergeEngineFree(pMain);
10248 }
10249 return rc;
10250 }
10251
10252
10253 /*
10254 ** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
10255 ** this function is called to prepare for iterating through the records
10256 ** in sorted order.
10257 */
10258 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
10259 VdbeSorter *pSorter;
10260 int rc = SQLITE_OK; /* Return code */
10261
10262 assert( pCsr->eCurType==CURTYPE_SORTER );
10263 pSorter = pCsr->uc.pSorter;
10264 assert( pSorter );
10265
10266 /* If no data has been written to disk, then do not do so now. Instead,
10267 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
10268 ** from the in-memory list. */
10269 if( pSorter->bUsePMA==0 ){
10270 if( pSorter->list.pList ){
10271 *pbEof = 0;
10272 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
10273 }else{
10274 *pbEof = 1;
10275 }
10276 return rc;
10277 }
10278
10279 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
10280 ** function flushes the contents of memory to disk, it immediately always
10281 ** creates a new list consisting of a single key immediately afterwards.
10282 ** So the list is never empty at this point. */
10283 assert( pSorter->list.pList );
10284 rc = vdbeSorterFlushPMA(pSorter);
10285
10286 /* Join all threads */
10287 rc = vdbeSorterJoinAll(pSorter, rc);
10288
10289 vdbeSorterRewindDebug("rewind");
10290
10291 /* Assuming no errors have occurred, set up a merger structure to
10292 ** incrementally read and merge all remaining PMAs. */
10293 assert( pSorter->pReader==0 );
10294 if( rc==SQLITE_OK ){
10295 rc = vdbeSorterSetupMerge(pSorter);
10296 *pbEof = 0;
10297 }
10298
10299 vdbeSorterRewindDebug("rewinddone");
10300 return rc;
10301 }
10302
10303 /*
10304 ** Advance to the next element in the sorter.
10305 */
10306 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, in t *pbEof){
10307 VdbeSorter *pSorter;
10308 int rc; /* Return code */
10309
10310 assert( pCsr->eCurType==CURTYPE_SORTER );
10311 pSorter = pCsr->uc.pSorter;
10312 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
10313 if( pSorter->bUsePMA ){
10314 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
10315 assert( pSorter->bUseThreads==0 || pSorter->pReader );
10316 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
10317 #if SQLITE_MAX_WORKER_THREADS>0
10318 if( pSorter->bUseThreads ){
10319 rc = vdbePmaReaderNext(pSorter->pReader);
10320 *pbEof = (pSorter->pReader->pFd==0);
10321 }else
10322 #endif
10323 /*if( !pSorter->bUseThreads )*/ {
10324 assert( pSorter->pMerger!=0 );
10325 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
10326 rc = vdbeMergeEngineStep(pSorter->pMerger, pbEof);
10327 }
10328 }else{
10329 SorterRecord *pFree = pSorter->list.pList;
10330 pSorter->list.pList = pFree->u.pNext;
10331 pFree->u.pNext = 0;
10332 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
10333 *pbEof = !pSorter->list.pList;
10334 rc = SQLITE_OK;
10335 }
10336 return rc;
10337 }
10338
10339 /*
10340 ** Return a pointer to a buffer owned by the sorter that contains the
10341 ** current key.
10342 */
10343 static void *vdbeSorterRowkey(
10344 const VdbeSorter *pSorter, /* Sorter object */
10345 int *pnKey /* OUT: Size of current key in bytes */
10346 ){
10347 void *pKey;
10348 if( pSorter->bUsePMA ){
10349 PmaReader *pReader;
10350 #if SQLITE_MAX_WORKER_THREADS>0
10351 if( pSorter->bUseThreads ){
10352 pReader = pSorter->pReader;
10353 }else
10354 #endif
10355 /*if( !pSorter->bUseThreads )*/{
10356 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
10357 }
10358 *pnKey = pReader->nKey;
10359 pKey = pReader->aKey;
10360 }else{
10361 *pnKey = pSorter->list.pList->nVal;
10362 pKey = SRVAL(pSorter->list.pList);
10363 }
10364 return pKey;
10365 }
10366
10367 /*
10368 ** Copy the current sorter key into the memory cell pOut.
10369 */
10370 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
10371 VdbeSorter *pSorter;
10372 void *pKey; int nKey; /* Sorter key to copy into pOut */
10373
10374 assert( pCsr->eCurType==CURTYPE_SORTER );
10375 pSorter = pCsr->uc.pSorter;
10376 pKey = vdbeSorterRowkey(pSorter, &nKey);
10377 if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
10378 return SQLITE_NOMEM_BKPT;
10379 }
10380 pOut->n = nKey;
10381 MemSetTypeFlag(pOut, MEM_Blob);
10382 memcpy(pOut->z, pKey, nKey);
10383
10384 return SQLITE_OK;
10385 }
10386
10387 /*
10388 ** Compare the key in memory cell pVal with the key that the sorter cursor
10389 ** passed as the first argument currently points to. For the purposes of
10390 ** the comparison, ignore the rowid field at the end of each record.
10391 **
10392 ** If the sorter cursor key contains any NULL values, consider it to be
10393 ** less than pVal. Even if pVal also contains NULL values.
10394 **
10395 ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
10396 ** Otherwise, set *pRes to a negative, zero or positive value if the
10397 ** key in pVal is smaller than, equal to or larger than the current sorter
10398 ** key.
10399 **
10400 ** This routine forms the core of the OP_SorterCompare opcode, which in
10401 ** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
10402 */
10403 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
10404 const VdbeCursor *pCsr, /* Sorter cursor */
10405 Mem *pVal, /* Value to compare to current sorter key */
10406 int nKeyCol, /* Compare this many columns */
10407 int *pRes /* OUT: Result of comparison */
10408 ){
10409 VdbeSorter *pSorter;
10410 UnpackedRecord *r2;
10411 KeyInfo *pKeyInfo;
10412 int i;
10413 void *pKey; int nKey; /* Sorter key to compare pVal with */
10414
10415 assert( pCsr->eCurType==CURTYPE_SORTER );
10416 pSorter = pCsr->uc.pSorter;
10417 r2 = pSorter->pUnpacked;
10418 pKeyInfo = pCsr->pKeyInfo;
10419 if( r2==0 ){
10420 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
10421 if( r2==0 ) return SQLITE_NOMEM_BKPT;
10422 r2->nField = nKeyCol;
10423 }
10424 assert( r2->nField==nKeyCol );
10425
10426 pKey = vdbeSorterRowkey(pSorter, &nKey);
10427 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
10428 for(i=0; i<nKeyCol; i++){
10429 if( r2->aMem[i].flags & MEM_Null ){
10430 *pRes = -1;
10431 return SQLITE_OK;
10432 }
10433 }
10434
10435 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
10436 return SQLITE_OK;
10437 }
10438
10439 /************** End of vdbesort.c ********************************************/
10440 /************** Begin file memjournal.c **************************************/
10441 /*
10442 ** 2008 October 7
10443 **
10444 ** The author disclaims copyright to this source code. In place of
10445 ** a legal notice, here is a blessing:
10446 **
10447 ** May you do good and not evil.
10448 ** May you find forgiveness for yourself and forgive others.
10449 ** May you share freely, never taking more than you give.
10450 **
10451 *************************************************************************
10452 **
10453 ** This file contains code use to implement an in-memory rollback journal.
10454 ** The in-memory rollback journal is used to journal transactions for
10455 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
10456 **
10457 ** Update: The in-memory journal is also used to temporarily cache
10458 ** smaller journals that are not critical for power-loss recovery.
10459 ** For example, statement journals that are not too big will be held
10460 ** entirely in memory, thus reducing the number of file I/O calls, and
10461 ** more importantly, reducing temporary file creation events. If these
10462 ** journals become too large for memory, they are spilled to disk. But
10463 ** in the common case, they are usually small and no file I/O needs to
10464 ** occur.
10465 */
10466 /* #include "sqliteInt.h" */
10467
10468 /* Forward references to internal structures */
10469 typedef struct MemJournal MemJournal;
10470 typedef struct FilePoint FilePoint;
10471 typedef struct FileChunk FileChunk;
10472
10473 /*
10474 ** The rollback journal is composed of a linked list of these structures.
10475 **
10476 ** The zChunk array is always at least 8 bytes in size - usually much more.
10477 ** Its actual size is stored in the MemJournal.nChunkSize variable.
10478 */
10479 struct FileChunk {
10480 FileChunk *pNext; /* Next chunk in the journal */
10481 u8 zChunk[8]; /* Content of this chunk */
10482 };
10483
10484 /*
10485 ** By default, allocate this many bytes of memory for each FileChunk object.
10486 */
10487 #define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024
10488
10489 /*
10490 ** For chunk size nChunkSize, return the number of bytes that should
10491 ** be allocated for each FileChunk structure.
10492 */
10493 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
10494
10495 /*
10496 ** An instance of this object serves as a cursor into the rollback journal.
10497 ** The cursor can be either for reading or writing.
10498 */
10499 struct FilePoint {
10500 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
10501 FileChunk *pChunk; /* Specific chunk into which cursor points */
10502 };
10503
10504 /*
10505 ** This structure is a subclass of sqlite3_file. Each open memory-journal
10506 ** is an instance of this class.
10507 */
10508 struct MemJournal {
10509 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
10510 int nChunkSize; /* In-memory chunk-size */
10511
10512 int nSpill; /* Bytes of data before flushing */
10513 int nSize; /* Bytes of data currently in memory */
10514 FileChunk *pFirst; /* Head of in-memory chunk-list */
10515 FilePoint endpoint; /* Pointer to the end of the file */
10516 FilePoint readpoint; /* Pointer to the end of the last xRead() */
10517
10518 int flags; /* xOpen flags */
10519 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
10520 const char *zJournal; /* Name of the journal file */
10521 };
10522
10523 /*
10524 ** Read data from the in-memory journal file. This is the implementation
10525 ** of the sqlite3_vfs.xRead method.
10526 */
10527 static int memjrnlRead(
10528 sqlite3_file *pJfd, /* The journal file from which to read */
10529 void *zBuf, /* Put the results here */
10530 int iAmt, /* Number of bytes to read */
10531 sqlite_int64 iOfst /* Begin reading at this offset */
10532 ){
10533 MemJournal *p = (MemJournal *)pJfd;
10534 u8 *zOut = zBuf;
10535 int nRead = iAmt;
10536 int iChunkOffset;
10537 FileChunk *pChunk;
10538
10539 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
10540 if( (iAmt+iOfst)>p->endpoint.iOffset ){
10541 return SQLITE_IOERR_SHORT_READ;
10542 }
10543 #endif
10544
10545 assert( (iAmt+iOfst)<=p->endpoint.iOffset );
10546 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
10547 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
10548 sqlite3_int64 iOff = 0;
10549 for(pChunk=p->pFirst;
10550 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
10551 pChunk=pChunk->pNext
10552 ){
10553 iOff += p->nChunkSize;
10554 }
10555 }else{
10556 pChunk = p->readpoint.pChunk;
10557 assert( pChunk!=0 );
10558 }
10559
10560 iChunkOffset = (int)(iOfst%p->nChunkSize);
10561 do {
10562 int iSpace = p->nChunkSize - iChunkOffset;
10563 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
10564 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
10565 zOut += nCopy;
10566 nRead -= iSpace;
10567 iChunkOffset = 0;
10568 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
10569 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
10570 p->readpoint.pChunk = pChunk;
10571
10572 return SQLITE_OK;
10573 }
10574
10575 /*
10576 ** Free the list of FileChunk structures headed at MemJournal.pFirst.
10577 */
10578 static void memjrnlFreeChunks(MemJournal *p){
10579 FileChunk *pIter;
10580 FileChunk *pNext;
10581 for(pIter=p->pFirst; pIter; pIter=pNext){
10582 pNext = pIter->pNext;
10583 sqlite3_free(pIter);
10584 }
10585 p->pFirst = 0;
10586 }
10587
10588 /*
10589 ** Flush the contents of memory to a real file on disk.
10590 */
10591 static int memjrnlCreateFile(MemJournal *p){
10592 int rc;
10593 sqlite3_file *pReal = (sqlite3_file*)p;
10594 MemJournal copy = *p;
10595
10596 memset(p, 0, sizeof(MemJournal));
10597 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
10598 if( rc==SQLITE_OK ){
10599 int nChunk = copy.nChunkSize;
10600 i64 iOff = 0;
10601 FileChunk *pIter;
10602 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
10603 if( iOff + nChunk > copy.endpoint.iOffset ){
10604 nChunk = copy.endpoint.iOffset - iOff;
10605 }
10606 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
10607 if( rc ) break;
10608 iOff += nChunk;
10609 }
10610 if( rc==SQLITE_OK ){
10611 /* No error has occurred. Free the in-memory buffers. */
10612 memjrnlFreeChunks(&copy);
10613 }
10614 }
10615 if( rc!=SQLITE_OK ){
10616 /* If an error occurred while creating or writing to the file, restore
10617 ** the original before returning. This way, SQLite uses the in-memory
10618 ** journal data to roll back changes made to the internal page-cache
10619 ** before this function was called. */
10620 sqlite3OsClose(pReal);
10621 *p = copy;
10622 }
10623 return rc;
10624 }
10625
10626
10627 /*
10628 ** Write data to the file.
10629 */
10630 static int memjrnlWrite(
10631 sqlite3_file *pJfd, /* The journal file into which to write */
10632 const void *zBuf, /* Take data to be written from here */
10633 int iAmt, /* Number of bytes to write */
10634 sqlite_int64 iOfst /* Begin writing at this offset into the file */
10635 ){
10636 MemJournal *p = (MemJournal *)pJfd;
10637 int nWrite = iAmt;
10638 u8 *zWrite = (u8 *)zBuf;
10639
10640 /* If the file should be created now, create it and write the new data
10641 ** into the file on disk. */
10642 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
10643 int rc = memjrnlCreateFile(p);
10644 if( rc==SQLITE_OK ){
10645 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
10646 }
10647 return rc;
10648 }
10649
10650 /* If the contents of this write should be stored in memory */
10651 else{
10652 /* An in-memory journal file should only ever be appended to. Random
10653 ** access writes are not required. The only exception to this is when
10654 ** the in-memory journal is being used by a connection using the
10655 ** atomic-write optimization. In this case the first 28 bytes of the
10656 ** journal file may be written as part of committing the transaction. */
10657 assert( iOfst==p->endpoint.iOffset || iOfst==0 );
10658 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
10659 if( iOfst==0 && p->pFirst ){
10660 assert( p->nChunkSize>iAmt );
10661 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
10662 }else
10663 #else
10664 assert( iOfst>0 || p->pFirst==0 );
10665 #endif
10666 {
10667 while( nWrite>0 ){
10668 FileChunk *pChunk = p->endpoint.pChunk;
10669 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
10670 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
10671
10672 if( iChunkOffset==0 ){
10673 /* New chunk is required to extend the file. */
10674 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
10675 if( !pNew ){
10676 return SQLITE_IOERR_NOMEM_BKPT;
10677 }
10678 pNew->pNext = 0;
10679 if( pChunk ){
10680 assert( p->pFirst );
10681 pChunk->pNext = pNew;
10682 }else{
10683 assert( !p->pFirst );
10684 p->pFirst = pNew;
10685 }
10686 p->endpoint.pChunk = pNew;
10687 }
10688
10689 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
10690 zWrite += iSpace;
10691 nWrite -= iSpace;
10692 p->endpoint.iOffset += iSpace;
10693 }
10694 p->nSize = iAmt + iOfst;
10695 }
10696 }
10697
10698 return SQLITE_OK;
10699 }
10700
10701 /*
10702 ** Truncate the file.
10703 **
10704 ** If the journal file is already on disk, truncate it there. Or, if it
10705 ** is still in main memory but is being truncated to zero bytes in size,
10706 ** ignore
10707 */
10708 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
10709 MemJournal *p = (MemJournal *)pJfd;
10710 if( ALWAYS(size==0) ){
10711 memjrnlFreeChunks(p);
10712 p->nSize = 0;
10713 p->endpoint.pChunk = 0;
10714 p->endpoint.iOffset = 0;
10715 p->readpoint.pChunk = 0;
10716 p->readpoint.iOffset = 0;
10717 }
10718 return SQLITE_OK;
10719 }
10720
10721 /*
10722 ** Close the file.
10723 */
10724 static int memjrnlClose(sqlite3_file *pJfd){
10725 MemJournal *p = (MemJournal *)pJfd;
10726 memjrnlFreeChunks(p);
10727 return SQLITE_OK;
10728 }
10729
10730 /*
10731 ** Sync the file.
10732 **
10733 ** If the real file has been created, call its xSync method. Otherwise,
10734 ** syncing an in-memory journal is a no-op.
10735 */
10736 static int memjrnlSync(sqlite3_file *pJfd, int flags){
10737 UNUSED_PARAMETER2(pJfd, flags);
10738 return SQLITE_OK;
10739 }
10740
10741 /*
10742 ** Query the size of the file in bytes.
10743 */
10744 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
10745 MemJournal *p = (MemJournal *)pJfd;
10746 *pSize = (sqlite_int64) p->endpoint.iOffset;
10747 return SQLITE_OK;
10748 }
10749
10750 /*
10751 ** Table of methods for MemJournal sqlite3_file object.
10752 */
10753 static const struct sqlite3_io_methods MemJournalMethods = {
10754 1, /* iVersion */
10755 memjrnlClose, /* xClose */
10756 memjrnlRead, /* xRead */
10757 memjrnlWrite, /* xWrite */
10758 memjrnlTruncate, /* xTruncate */
10759 memjrnlSync, /* xSync */
10760 memjrnlFileSize, /* xFileSize */
10761 0, /* xLock */
10762 0, /* xUnlock */
10763 0, /* xCheckReservedLock */
10764 0, /* xFileControl */
10765 0, /* xSectorSize */
10766 0, /* xDeviceCharacteristics */
10767 0, /* xShmMap */
10768 0, /* xShmLock */
10769 0, /* xShmBarrier */
10770 0, /* xShmUnmap */
10771 0, /* xFetch */
10772 0 /* xUnfetch */
10773 };
10774
10775 /*
10776 ** Open a journal file.
10777 **
10778 ** The behaviour of the journal file depends on the value of parameter
10779 ** nSpill. If nSpill is 0, then the journal file is always create and
10780 ** accessed using the underlying VFS. If nSpill is less than zero, then
10781 ** all content is always stored in main-memory. Finally, if nSpill is a
10782 ** positive value, then the journal file is initially created in-memory
10783 ** but may be flushed to disk later on. In this case the journal file is
10784 ** flushed to disk either when it grows larger than nSpill bytes in size,
10785 ** or when sqlite3JournalCreate() is called.
10786 */
10787 SQLITE_PRIVATE int sqlite3JournalOpen(
10788 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
10789 const char *zName, /* Name of the journal file */
10790 sqlite3_file *pJfd, /* Preallocated, blank file handle */
10791 int flags, /* Opening flags */
10792 int nSpill /* Bytes buffered before opening the file */
10793 ){
10794 MemJournal *p = (MemJournal*)pJfd;
10795
10796 /* Zero the file-handle object. If nSpill was passed zero, initialize
10797 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
10798 ** case none of the code in this module is executed as a result of calls
10799 ** made on the journal file-handle. */
10800 memset(p, 0, sizeof(MemJournal));
10801 if( nSpill==0 ){
10802 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
10803 }
10804
10805 if( nSpill>0 ){
10806 p->nChunkSize = nSpill;
10807 }else{
10808 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
10809 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
10810 }
10811
10812 p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods;
10813 p->nSpill = nSpill;
10814 p->flags = flags;
10815 p->zJournal = zName;
10816 p->pVfs = pVfs;
10817 return SQLITE_OK;
10818 }
10819
10820 /*
10821 ** Open an in-memory journal file.
10822 */
10823 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
10824 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
10825 }
10826
10827 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
10828 /*
10829 ** If the argument p points to a MemJournal structure that is not an
10830 ** in-memory-only journal file (i.e. is one that was opened with a +ve
10831 ** nSpill parameter), and the underlying file has not yet been created,
10832 ** create it now.
10833 */
10834 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
10835 int rc = SQLITE_OK;
10836 if( p->pMethods==&MemJournalMethods && ((MemJournal*)p)->nSpill>0 ){
10837 rc = memjrnlCreateFile((MemJournal*)p);
10838 }
10839 return rc;
10840 }
10841 #endif
10842
10843 /*
10844 ** The file-handle passed as the only argument is open on a journal file.
10845 ** Return true if this "journal file" is currently stored in heap memory,
10846 ** or false otherwise.
10847 */
10848 SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){
10849 return p->pMethods==&MemJournalMethods;
10850 }
10851
10852 /*
10853 ** Return the number of bytes required to store a JournalFile that uses vfs
10854 ** pVfs to create the underlying on-disk files.
10855 */
10856 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
10857 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
10858 }
10859
10860 /************** End of memjournal.c ******************************************/
10861 /************** Begin file walker.c ******************************************/
10862 /*
10863 ** 2008 August 16
10864 **
10865 ** The author disclaims copyright to this source code. In place of
10866 ** a legal notice, here is a blessing:
10867 **
10868 ** May you do good and not evil.
10869 ** May you find forgiveness for yourself and forgive others.
10870 ** May you share freely, never taking more than you give.
10871 **
10872 *************************************************************************
10873 ** This file contains routines used for walking the parser tree for
10874 ** an SQL statement.
10875 */
10876 /* #include "sqliteInt.h" */
10877 /* #include <stdlib.h> */
10878 /* #include <string.h> */
10879
10880
10881 /*
10882 ** Walk an expression tree. Invoke the callback once for each node
10883 ** of the expression, while descending. (In other words, the callback
10884 ** is invoked before visiting children.)
10885 **
10886 ** The return value from the callback should be one of the WRC_*
10887 ** constants to specify how to proceed with the walk.
10888 **
10889 ** WRC_Continue Continue descending down the tree.
10890 **
10891 ** WRC_Prune Do not descend into child nodes. But allow
10892 ** the walk to continue with sibling nodes.
10893 **
10894 ** WRC_Abort Do no more callbacks. Unwind the stack and
10895 ** return the top-level walk call.
10896 **
10897 ** The return value from this routine is WRC_Abort to abandon the tree walk
10898 ** and WRC_Continue to continue.
10899 */
10900 static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
10901 int rc;
10902 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
10903 testcase( ExprHasProperty(pExpr, EP_Reduced) );
10904 rc = pWalker->xExprCallback(pWalker, pExpr);
10905 if( rc || ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
10906 return rc & WRC_Abort;
10907 }
10908 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
10909 if( pExpr->pRight && walkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
10910 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
10911 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
10912 }else if( pExpr->x.pList ){
10913 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
10914 }
10915 return WRC_Continue;
10916 }
10917 SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
10918 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
10919 }
10920
10921 /*
10922 ** Call sqlite3WalkExpr() for every expression in list p or until
10923 ** an abort request is seen.
10924 */
10925 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
10926 int i;
10927 struct ExprList_item *pItem;
10928 if( p ){
10929 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
10930 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
10931 }
10932 }
10933 return WRC_Continue;
10934 }
10935
10936 /*
10937 ** Walk all expressions associated with SELECT statement p. Do
10938 ** not invoke the SELECT callback on p, but do (of course) invoke
10939 ** any expr callbacks and SELECT callbacks that come from subqueries.
10940 ** Return WRC_Abort or WRC_Continue.
10941 */
10942 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
10943 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
10944 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
10945 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
10946 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
10947 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
10948 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
10949 if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
10950 return WRC_Continue;
10951 }
10952
10953 /*
10954 ** Walk the parse trees associated with all subqueries in the
10955 ** FROM clause of SELECT statement p. Do not invoke the select
10956 ** callback on p, but do invoke it on each FROM clause subquery
10957 ** and on any subqueries further down in the tree. Return
10958 ** WRC_Abort or WRC_Continue;
10959 */
10960 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
10961 SrcList *pSrc;
10962 int i;
10963 struct SrcList_item *pItem;
10964
10965 pSrc = p->pSrc;
10966 if( ALWAYS(pSrc) ){
10967 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
10968 if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
10969 return WRC_Abort;
10970 }
10971 if( pItem->fg.isTabFunc
10972 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
10973 ){
10974 return WRC_Abort;
10975 }
10976 }
10977 }
10978 return WRC_Continue;
10979 }
10980
10981 /*
10982 ** Call sqlite3WalkExpr() for every expression in Select statement p.
10983 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
10984 ** on the compound select chain, p->pPrior.
10985 **
10986 ** If it is not NULL, the xSelectCallback() callback is invoked before
10987 ** the walk of the expressions and FROM clause. The xSelectCallback2()
10988 ** method, if it is not NULL, is invoked following the walk of the
10989 ** expressions and FROM clause.
10990 **
10991 ** Return WRC_Continue under normal conditions. Return WRC_Abort if
10992 ** there is an abort request.
10993 **
10994 ** If the Walker does not have an xSelectCallback() then this routine
10995 ** is a no-op returning WRC_Continue.
10996 */
10997 SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
10998 int rc;
10999 if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){
11000 return WRC_Continue;
11001 }
11002 rc = WRC_Continue;
11003 pWalker->walkerDepth++;
11004 while( p ){
11005 if( pWalker->xSelectCallback ){
11006 rc = pWalker->xSelectCallback(pWalker, p);
11007 if( rc ) break;
11008 }
11009 if( sqlite3WalkSelectExpr(pWalker, p)
11010 || sqlite3WalkSelectFrom(pWalker, p)
11011 ){
11012 pWalker->walkerDepth--;
11013 return WRC_Abort;
11014 }
11015 if( pWalker->xSelectCallback2 ){
11016 pWalker->xSelectCallback2(pWalker, p);
11017 }
11018 p = p->pPrior;
11019 }
11020 pWalker->walkerDepth--;
11021 return rc & WRC_Abort;
11022 }
11023
11024 /************** End of walker.c **********************************************/
11025 /************** Begin file resolve.c *****************************************/
11026 /*
11027 ** 2008 August 18
11028 **
11029 ** The author disclaims copyright to this source code. In place of
11030 ** a legal notice, here is a blessing:
11031 **
11032 ** May you do good and not evil.
11033 ** May you find forgiveness for yourself and forgive others.
11034 ** May you share freely, never taking more than you give.
11035 **
11036 *************************************************************************
11037 **
11038 ** This file contains routines used for walking the parser tree and
11039 ** resolve all identifiers by associating them with a particular
11040 ** table and column.
11041 */
11042 /* #include "sqliteInt.h" */
11043
11044 /*
11045 ** Walk the expression tree pExpr and increase the aggregate function
11046 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
11047 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
11048 ** outer query into an inner subquery.
11049 **
11050 ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
11051 ** is a helper function - a callback for the tree walker.
11052 */
11053 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
11054 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
11055 return WRC_Continue;
11056 }
11057 static void incrAggFunctionDepth(Expr *pExpr, int N){
11058 if( N>0 ){
11059 Walker w;
11060 memset(&w, 0, sizeof(w));
11061 w.xExprCallback = incrAggDepth;
11062 w.u.n = N;
11063 sqlite3WalkExpr(&w, pExpr);
11064 }
11065 }
11066
11067 /*
11068 ** Turn the pExpr expression into an alias for the iCol-th column of the
11069 ** result set in pEList.
11070 **
11071 ** If the reference is followed by a COLLATE operator, then make sure
11072 ** the COLLATE operator is preserved. For example:
11073 **
11074 ** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
11075 **
11076 ** Should be transformed into:
11077 **
11078 ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
11079 **
11080 ** The nSubquery parameter specifies how many levels of subquery the
11081 ** alias is removed from the original expression. The usual value is
11082 ** zero but it might be more if the alias is contained within a subquery
11083 ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
11084 ** structures must be increased by the nSubquery amount.
11085 */
11086 static void resolveAlias(
11087 Parse *pParse, /* Parsing context */
11088 ExprList *pEList, /* A result set */
11089 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
11090 Expr *pExpr, /* Transform this into an alias to the result set */
11091 const char *zType, /* "GROUP" or "ORDER" or "" */
11092 int nSubquery /* Number of subqueries that the label is moving */
11093 ){
11094 Expr *pOrig; /* The iCol-th column of the result set */
11095 Expr *pDup; /* Copy of pOrig */
11096 sqlite3 *db; /* The database connection */
11097
11098 assert( iCol>=0 && iCol<pEList->nExpr );
11099 pOrig = pEList->a[iCol].pExpr;
11100 assert( pOrig!=0 );
11101 db = pParse->db;
11102 pDup = sqlite3ExprDup(db, pOrig, 0);
11103 if( pDup==0 ) return;
11104 if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
11105 if( pExpr->op==TK_COLLATE ){
11106 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
11107 }
11108 ExprSetProperty(pDup, EP_Alias);
11109
11110 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
11111 ** prevents ExprDelete() from deleting the Expr structure itself,
11112 ** allowing it to be repopulated by the memcpy() on the following line.
11113 ** The pExpr->u.zToken might point into memory that will be freed by the
11114 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
11115 ** make a copy of the token before doing the sqlite3DbFree().
11116 */
11117 ExprSetProperty(pExpr, EP_Static);
11118 sqlite3ExprDelete(db, pExpr);
11119 memcpy(pExpr, pDup, sizeof(*pExpr));
11120 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
11121 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
11122 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
11123 pExpr->flags |= EP_MemToken;
11124 }
11125 sqlite3DbFree(db, pDup);
11126 }
11127
11128
11129 /*
11130 ** Return TRUE if the name zCol occurs anywhere in the USING clause.
11131 **
11132 ** Return FALSE if the USING clause is NULL or if it does not contain
11133 ** zCol.
11134 */
11135 static int nameInUsingClause(IdList *pUsing, const char *zCol){
11136 if( pUsing ){
11137 int k;
11138 for(k=0; k<pUsing->nId; k++){
11139 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
11140 }
11141 }
11142 return 0;
11143 }
11144
11145 /*
11146 ** Subqueries stores the original database, table and column names for their
11147 ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
11148 ** Check to see if the zSpan given to this routine matches the zDb, zTab,
11149 ** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
11150 ** match anything.
11151 */
11152 SQLITE_PRIVATE int sqlite3MatchSpanName(
11153 const char *zSpan,
11154 const char *zCol,
11155 const char *zTab,
11156 const char *zDb
11157 ){
11158 int n;
11159 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
11160 if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
11161 return 0;
11162 }
11163 zSpan += n+1;
11164 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
11165 if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
11166 return 0;
11167 }
11168 zSpan += n+1;
11169 if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
11170 return 0;
11171 }
11172 return 1;
11173 }
11174
11175 /*
11176 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
11177 ** that name in the set of source tables in pSrcList and make the pExpr
11178 ** expression node refer back to that source column. The following changes
11179 ** are made to pExpr:
11180 **
11181 ** pExpr->iDb Set the index in db->aDb[] of the database X
11182 ** (even if X is implied).
11183 ** pExpr->iTable Set to the cursor number for the table obtained
11184 ** from pSrcList.
11185 ** pExpr->pTab Points to the Table structure of X.Y (even if
11186 ** X and/or Y are implied.)
11187 ** pExpr->iColumn Set to the column number within the table.
11188 ** pExpr->op Set to TK_COLUMN.
11189 ** pExpr->pLeft Any expression this points to is deleted
11190 ** pExpr->pRight Any expression this points to is deleted.
11191 **
11192 ** The zDb variable is the name of the database (the "X"). This value may be
11193 ** NULL meaning that name is of the form Y.Z or Z. Any available database
11194 ** can be used. The zTable variable is the name of the table (the "Y"). This
11195 ** value can be NULL if zDb is also NULL. If zTable is NULL it
11196 ** means that the form of the name is Z and that columns from any table
11197 ** can be used.
11198 **
11199 ** If the name cannot be resolved unambiguously, leave an error message
11200 ** in pParse and return WRC_Abort. Return WRC_Prune on success.
11201 */
11202 static int lookupName(
11203 Parse *pParse, /* The parsing context */
11204 const char *zDb, /* Name of the database containing table, or NULL */
11205 const char *zTab, /* Name of table containing column, or NULL */
11206 const char *zCol, /* Name of the column. */
11207 NameContext *pNC, /* The name context used to resolve the name */
11208 Expr *pExpr /* Make this EXPR node point to the selected column */
11209 ){
11210 int i, j; /* Loop counters */
11211 int cnt = 0; /* Number of matching column names */
11212 int cntTab = 0; /* Number of matching table names */
11213 int nSubquery = 0; /* How many levels of subquery */
11214 sqlite3 *db = pParse->db; /* The database connection */
11215 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
11216 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
11217 NameContext *pTopNC = pNC; /* First namecontext in the list */
11218 Schema *pSchema = 0; /* Schema of the expression */
11219 int isTrigger = 0; /* True if resolved to a trigger column */
11220 Table *pTab = 0; /* Table hold the row */
11221 Column *pCol; /* A column of pTab */
11222
11223 assert( pNC ); /* the name context cannot be NULL. */
11224 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
11225 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
11226
11227 /* Initialize the node to no-match */
11228 pExpr->iTable = -1;
11229 pExpr->pTab = 0;
11230 ExprSetVVAProperty(pExpr, EP_NoReduce);
11231
11232 /* Translate the schema name in zDb into a pointer to the corresponding
11233 ** schema. If not found, pSchema will remain NULL and nothing will match
11234 ** resulting in an appropriate error message toward the end of this routine
11235 */
11236 if( zDb ){
11237 testcase( pNC->ncFlags & NC_PartIdx );
11238 testcase( pNC->ncFlags & NC_IsCheck );
11239 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
11240 /* Silently ignore database qualifiers inside CHECK constraints and
11241 ** partial indices. Do not raise errors because that might break
11242 ** legacy and because it does not hurt anything to just ignore the
11243 ** database name. */
11244 zDb = 0;
11245 }else{
11246 for(i=0; i<db->nDb; i++){
11247 assert( db->aDb[i].zDbSName );
11248 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
11249 pSchema = db->aDb[i].pSchema;
11250 break;
11251 }
11252 }
11253 }
11254 }
11255
11256 /* Start at the inner-most context and move outward until a match is found */
11257 while( pNC && cnt==0 ){
11258 ExprList *pEList;
11259 SrcList *pSrcList = pNC->pSrcList;
11260
11261 if( pSrcList ){
11262 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
11263 pTab = pItem->pTab;
11264 assert( pTab!=0 && pTab->zName!=0 );
11265 assert( pTab->nCol>0 );
11266 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
11267 int hit = 0;
11268 pEList = pItem->pSelect->pEList;
11269 for(j=0; j<pEList->nExpr; j++){
11270 if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
11271 cnt++;
11272 cntTab = 2;
11273 pMatch = pItem;
11274 pExpr->iColumn = j;
11275 hit = 1;
11276 }
11277 }
11278 if( hit || zTab==0 ) continue;
11279 }
11280 if( zDb && pTab->pSchema!=pSchema ){
11281 continue;
11282 }
11283 if( zTab ){
11284 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
11285 assert( zTabName!=0 );
11286 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
11287 continue;
11288 }
11289 }
11290 if( 0==(cntTab++) ){
11291 pMatch = pItem;
11292 }
11293 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
11294 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
11295 /* If there has been exactly one prior match and this match
11296 ** is for the right-hand table of a NATURAL JOIN or is in a
11297 ** USING clause, then skip this match.
11298 */
11299 if( cnt==1 ){
11300 if( pItem->fg.jointype & JT_NATURAL ) continue;
11301 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
11302 }
11303 cnt++;
11304 pMatch = pItem;
11305 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
11306 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
11307 break;
11308 }
11309 }
11310 }
11311 if( pMatch ){
11312 pExpr->iTable = pMatch->iCursor;
11313 pExpr->pTab = pMatch->pTab;
11314 /* RIGHT JOIN not (yet) supported */
11315 assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
11316 if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
11317 ExprSetProperty(pExpr, EP_CanBeNull);
11318 }
11319 pSchema = pExpr->pTab->pSchema;
11320 }
11321 } /* if( pSrcList ) */
11322
11323 #ifndef SQLITE_OMIT_TRIGGER
11324 /* If we have not already resolved the name, then maybe
11325 ** it is a new.* or old.* trigger argument reference
11326 */
11327 if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
11328 int op = pParse->eTriggerOp;
11329 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
11330 if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
11331 pExpr->iTable = 1;
11332 pTab = pParse->pTriggerTab;
11333 }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
11334 pExpr->iTable = 0;
11335 pTab = pParse->pTriggerTab;
11336 }else{
11337 pTab = 0;
11338 }
11339
11340 if( pTab ){
11341 int iCol;
11342 pSchema = pTab->pSchema;
11343 cntTab++;
11344 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
11345 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
11346 if( iCol==pTab->iPKey ){
11347 iCol = -1;
11348 }
11349 break;
11350 }
11351 }
11352 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
11353 /* IMP: R-51414-32910 */
11354 iCol = -1;
11355 }
11356 if( iCol<pTab->nCol ){
11357 cnt++;
11358 if( iCol<0 ){
11359 pExpr->affinity = SQLITE_AFF_INTEGER;
11360 }else if( pExpr->iTable==0 ){
11361 testcase( iCol==31 );
11362 testcase( iCol==32 );
11363 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
11364 }else{
11365 testcase( iCol==31 );
11366 testcase( iCol==32 );
11367 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
11368 }
11369 pExpr->iColumn = (i16)iCol;
11370 pExpr->pTab = pTab;
11371 isTrigger = 1;
11372 }
11373 }
11374 }
11375 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
11376
11377 /*
11378 ** Perhaps the name is a reference to the ROWID
11379 */
11380 if( cnt==0
11381 && cntTab==1
11382 && pMatch
11383 && (pNC->ncFlags & NC_IdxExpr)==0
11384 && sqlite3IsRowid(zCol)
11385 && VisibleRowid(pMatch->pTab)
11386 ){
11387 cnt = 1;
11388 pExpr->iColumn = -1;
11389 pExpr->affinity = SQLITE_AFF_INTEGER;
11390 }
11391
11392 /*
11393 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
11394 ** might refer to an result-set alias. This happens, for example, when
11395 ** we are resolving names in the WHERE clause of the following command:
11396 **
11397 ** SELECT a+b AS x FROM table WHERE x<10;
11398 **
11399 ** In cases like this, replace pExpr with a copy of the expression that
11400 ** forms the result set entry ("a+b" in the example) and return immediately.
11401 ** Note that the expression in the result set should have already been
11402 ** resolved by the time the WHERE clause is resolved.
11403 **
11404 ** The ability to use an output result-set column in the WHERE, GROUP BY,
11405 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
11406 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
11407 ** is supported for backwards compatibility only. Hence, we issue a warning
11408 ** on sqlite3_log() whenever the capability is used.
11409 */
11410 if( (pEList = pNC->pEList)!=0
11411 && zTab==0
11412 && cnt==0
11413 ){
11414 for(j=0; j<pEList->nExpr; j++){
11415 char *zAs = pEList->a[j].zName;
11416 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
11417 Expr *pOrig;
11418 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
11419 assert( pExpr->x.pList==0 );
11420 assert( pExpr->x.pSelect==0 );
11421 pOrig = pEList->a[j].pExpr;
11422 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
11423 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
11424 return WRC_Abort;
11425 }
11426 if( sqlite3ExprVectorSize(pOrig)!=1 ){
11427 sqlite3ErrorMsg(pParse, "row value misused");
11428 return WRC_Abort;
11429 }
11430 resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
11431 cnt = 1;
11432 pMatch = 0;
11433 assert( zTab==0 && zDb==0 );
11434 goto lookupname_end;
11435 }
11436 }
11437 }
11438
11439 /* Advance to the next name context. The loop will exit when either
11440 ** we have a match (cnt>0) or when we run out of name contexts.
11441 */
11442 if( cnt==0 ){
11443 pNC = pNC->pNext;
11444 nSubquery++;
11445 }
11446 }
11447
11448 /*
11449 ** If X and Y are NULL (in other words if only the column name Z is
11450 ** supplied) and the value of Z is enclosed in double-quotes, then
11451 ** Z is a string literal if it doesn't match any column names. In that
11452 ** case, we need to return right away and not make any changes to
11453 ** pExpr.
11454 **
11455 ** Because no reference was made to outer contexts, the pNC->nRef
11456 ** fields are not changed in any context.
11457 */
11458 if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
11459 pExpr->op = TK_STRING;
11460 pExpr->pTab = 0;
11461 return WRC_Prune;
11462 }
11463
11464 /*
11465 ** cnt==0 means there was not match. cnt>1 means there were two or
11466 ** more matches. Either way, we have an error.
11467 */
11468 if( cnt!=1 ){
11469 const char *zErr;
11470 zErr = cnt==0 ? "no such column" : "ambiguous column name";
11471 if( zDb ){
11472 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
11473 }else if( zTab ){
11474 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
11475 }else{
11476 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
11477 }
11478 pParse->checkSchema = 1;
11479 pTopNC->nErr++;
11480 }
11481
11482 /* If a column from a table in pSrcList is referenced, then record
11483 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
11484 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
11485 ** column number is greater than the number of bits in the bitmask
11486 ** then set the high-order bit of the bitmask.
11487 */
11488 if( pExpr->iColumn>=0 && pMatch!=0 ){
11489 int n = pExpr->iColumn;
11490 testcase( n==BMS-1 );
11491 if( n>=BMS ){
11492 n = BMS-1;
11493 }
11494 assert( pMatch->iCursor==pExpr->iTable );
11495 pMatch->colUsed |= ((Bitmask)1)<<n;
11496 }
11497
11498 /* Clean up and return
11499 */
11500 sqlite3ExprDelete(db, pExpr->pLeft);
11501 pExpr->pLeft = 0;
11502 sqlite3ExprDelete(db, pExpr->pRight);
11503 pExpr->pRight = 0;
11504 pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
11505 lookupname_end:
11506 if( cnt==1 ){
11507 assert( pNC!=0 );
11508 if( !ExprHasProperty(pExpr, EP_Alias) ){
11509 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
11510 }
11511 /* Increment the nRef value on all name contexts from TopNC up to
11512 ** the point where the name matched. */
11513 for(;;){
11514 assert( pTopNC!=0 );
11515 pTopNC->nRef++;
11516 if( pTopNC==pNC ) break;
11517 pTopNC = pTopNC->pNext;
11518 }
11519 return WRC_Prune;
11520 } else {
11521 return WRC_Abort;
11522 }
11523 }
11524
11525 /*
11526 ** Allocate and return a pointer to an expression to load the column iCol
11527 ** from datasource iSrc in SrcList pSrc.
11528 */
11529 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSr c, int iCol){
11530 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
11531 if( p ){
11532 struct SrcList_item *pItem = &pSrc->a[iSrc];
11533 p->pTab = pItem->pTab;
11534 p->iTable = pItem->iCursor;
11535 if( p->pTab->iPKey==iCol ){
11536 p->iColumn = -1;
11537 }else{
11538 p->iColumn = (ynVar)iCol;
11539 testcase( iCol==BMS );
11540 testcase( iCol==BMS-1 );
11541 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
11542 }
11543 ExprSetProperty(p, EP_Resolved);
11544 }
11545 return p;
11546 }
11547
11548 /*
11549 ** Report an error that an expression is not valid for some set of
11550 ** pNC->ncFlags values determined by validMask.
11551 */
11552 static void notValid(
11553 Parse *pParse, /* Leave error message here */
11554 NameContext *pNC, /* The name context */
11555 const char *zMsg, /* Type of error */
11556 int validMask /* Set of contexts for which prohibited */
11557 ){
11558 assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 );
11559 if( (pNC->ncFlags & validMask)!=0 ){
11560 const char *zIn = "partial index WHERE clauses";
11561 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
11562 #ifndef SQLITE_OMIT_CHECK
11563 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
11564 #endif
11565 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
11566 }
11567 }
11568
11569 /*
11570 ** Expression p should encode a floating point value between 1.0 and 0.0.
11571 ** Return 1024 times this value. Or return -1 if p is not a floating point
11572 ** value between 1.0 and 0.0.
11573 */
11574 static int exprProbability(Expr *p){
11575 double r = -1.0;
11576 if( p->op!=TK_FLOAT ) return -1;
11577 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
11578 assert( r>=0.0 );
11579 if( r>1.0 ) return -1;
11580 return (int)(r*134217728.0);
11581 }
11582
11583 /*
11584 ** This routine is callback for sqlite3WalkExpr().
11585 **
11586 ** Resolve symbolic names into TK_COLUMN operators for the current
11587 ** node in the expression tree. Return 0 to continue the search down
11588 ** the tree or 2 to abort the tree walk.
11589 **
11590 ** This routine also does error checking and name resolution for
11591 ** function names. The operator for aggregate functions is changed
11592 ** to TK_AGG_FUNCTION.
11593 */
11594 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
11595 NameContext *pNC;
11596 Parse *pParse;
11597
11598 pNC = pWalker->u.pNC;
11599 assert( pNC!=0 );
11600 pParse = pNC->pParse;
11601 assert( pParse==pWalker->pParse );
11602
11603 if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune;
11604 ExprSetProperty(pExpr, EP_Resolved);
11605 #ifndef NDEBUG
11606 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
11607 SrcList *pSrcList = pNC->pSrcList;
11608 int i;
11609 for(i=0; i<pNC->pSrcList->nSrc; i++){
11610 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
11611 }
11612 }
11613 #endif
11614 switch( pExpr->op ){
11615
11616 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
11617 /* The special operator TK_ROW means use the rowid for the first
11618 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
11619 ** clause processing on UPDATE and DELETE statements.
11620 */
11621 case TK_ROW: {
11622 SrcList *pSrcList = pNC->pSrcList;
11623 struct SrcList_item *pItem;
11624 assert( pSrcList && pSrcList->nSrc==1 );
11625 pItem = pSrcList->a;
11626 pExpr->op = TK_COLUMN;
11627 pExpr->pTab = pItem->pTab;
11628 pExpr->iTable = pItem->iCursor;
11629 pExpr->iColumn = -1;
11630 pExpr->affinity = SQLITE_AFF_INTEGER;
11631 break;
11632 }
11633 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
11634 && !defined(SQLITE_OMIT_SUBQUERY) */
11635
11636 /* A lone identifier is the name of a column.
11637 */
11638 case TK_ID: {
11639 return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
11640 }
11641
11642 /* A table name and column name: ID.ID
11643 ** Or a database, table and column: ID.ID.ID
11644 */
11645 case TK_DOT: {
11646 const char *zColumn;
11647 const char *zTable;
11648 const char *zDb;
11649 Expr *pRight;
11650
11651 /* if( pSrcList==0 ) break; */
11652 notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
11653 pRight = pExpr->pRight;
11654 if( pRight->op==TK_ID ){
11655 zDb = 0;
11656 zTable = pExpr->pLeft->u.zToken;
11657 zColumn = pRight->u.zToken;
11658 }else{
11659 assert( pRight->op==TK_DOT );
11660 zDb = pExpr->pLeft->u.zToken;
11661 zTable = pRight->pLeft->u.zToken;
11662 zColumn = pRight->pRight->u.zToken;
11663 }
11664 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
11665 }
11666
11667 /* Resolve function names
11668 */
11669 case TK_FUNCTION: {
11670 ExprList *pList = pExpr->x.pList; /* The argument list */
11671 int n = pList ? pList->nExpr : 0; /* Number of arguments */
11672 int no_such_func = 0; /* True if no such function exists */
11673 int wrong_num_args = 0; /* True if wrong number of arguments */
11674 int is_agg = 0; /* True if is an aggregate function */
11675 int nId; /* Number of characters in function name */
11676 const char *zId; /* The function name. */
11677 FuncDef *pDef; /* Information about the function */
11678 u8 enc = ENC(pParse->db); /* The database encoding */
11679
11680 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
11681 zId = pExpr->u.zToken;
11682 nId = sqlite3Strlen30(zId);
11683 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
11684 if( pDef==0 ){
11685 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
11686 if( pDef==0 ){
11687 no_such_func = 1;
11688 }else{
11689 wrong_num_args = 1;
11690 }
11691 }else{
11692 is_agg = pDef->xFinalize!=0;
11693 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
11694 ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
11695 if( n==2 ){
11696 pExpr->iTable = exprProbability(pList->a[1].pExpr);
11697 if( pExpr->iTable<0 ){
11698 sqlite3ErrorMsg(pParse,
11699 "second argument to likelihood() must be a "
11700 "constant between 0.0 and 1.0");
11701 pNC->nErr++;
11702 }
11703 }else{
11704 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
11705 ** equivalent to likelihood(X, 0.0625).
11706 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
11707 ** short-hand for likelihood(X,0.0625).
11708 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
11709 ** for likelihood(X,0.9375).
11710 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
11711 ** to likelihood(X,0.9375). */
11712 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
11713 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
11714 }
11715 }
11716 #ifndef SQLITE_OMIT_AUTHORIZATION
11717 {
11718 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
11719 if( auth!=SQLITE_OK ){
11720 if( auth==SQLITE_DENY ){
11721 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
11722 pDef->zName);
11723 pNC->nErr++;
11724 }
11725 pExpr->op = TK_NULL;
11726 return WRC_Prune;
11727 }
11728 }
11729 #endif
11730 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
11731 /* For the purposes of the EP_ConstFunc flag, date and time
11732 ** functions and other functions that change slowly are considered
11733 ** constant because they are constant for the duration of one query */
11734 ExprSetProperty(pExpr,EP_ConstFunc);
11735 }
11736 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
11737 /* Date/time functions that use 'now', and other functions like
11738 ** sqlite_version() that might change over time cannot be used
11739 ** in an index. */
11740 notValid(pParse, pNC, "non-deterministic functions",
11741 NC_IdxExpr|NC_PartIdx);
11742 }
11743 }
11744 if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
11745 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
11746 pNC->nErr++;
11747 is_agg = 0;
11748 }else if( no_such_func && pParse->db->init.busy==0
11749 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
11750 && pParse->explain==0
11751 #endif
11752 ){
11753 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
11754 pNC->nErr++;
11755 }else if( wrong_num_args ){
11756 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
11757 nId, zId);
11758 pNC->nErr++;
11759 }
11760 if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
11761 sqlite3WalkExprList(pWalker, pList);
11762 if( is_agg ){
11763 NameContext *pNC2 = pNC;
11764 pExpr->op = TK_AGG_FUNCTION;
11765 pExpr->op2 = 0;
11766 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
11767 pExpr->op2++;
11768 pNC2 = pNC2->pNext;
11769 }
11770 assert( pDef!=0 );
11771 if( pNC2 ){
11772 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
11773 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
11774 pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
11775
11776 }
11777 pNC->ncFlags |= NC_AllowAgg;
11778 }
11779 /* FIX ME: Compute pExpr->affinity based on the expected return
11780 ** type of the function
11781 */
11782 return WRC_Prune;
11783 }
11784 #ifndef SQLITE_OMIT_SUBQUERY
11785 case TK_SELECT:
11786 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
11787 #endif
11788 case TK_IN: {
11789 testcase( pExpr->op==TK_IN );
11790 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
11791 int nRef = pNC->nRef;
11792 notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
11793 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
11794 assert( pNC->nRef>=nRef );
11795 if( nRef!=pNC->nRef ){
11796 ExprSetProperty(pExpr, EP_VarSelect);
11797 pNC->ncFlags |= NC_VarSelect;
11798 }
11799 }
11800 break;
11801 }
11802 case TK_VARIABLE: {
11803 notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
11804 break;
11805 }
11806 case TK_BETWEEN:
11807 case TK_EQ:
11808 case TK_NE:
11809 case TK_LT:
11810 case TK_LE:
11811 case TK_GT:
11812 case TK_GE:
11813 case TK_IS:
11814 case TK_ISNOT: {
11815 int nLeft, nRight;
11816 if( pParse->db->mallocFailed ) break;
11817 assert( pExpr->pLeft!=0 );
11818 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
11819 if( pExpr->op==TK_BETWEEN ){
11820 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
11821 if( nRight==nLeft ){
11822 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
11823 }
11824 }else{
11825 assert( pExpr->pRight!=0 );
11826 nRight = sqlite3ExprVectorSize(pExpr->pRight);
11827 }
11828 if( nLeft!=nRight ){
11829 testcase( pExpr->op==TK_EQ );
11830 testcase( pExpr->op==TK_NE );
11831 testcase( pExpr->op==TK_LT );
11832 testcase( pExpr->op==TK_LE );
11833 testcase( pExpr->op==TK_GT );
11834 testcase( pExpr->op==TK_GE );
11835 testcase( pExpr->op==TK_IS );
11836 testcase( pExpr->op==TK_ISNOT );
11837 testcase( pExpr->op==TK_BETWEEN );
11838 sqlite3ErrorMsg(pParse, "row value misused");
11839 }
11840 break;
11841 }
11842 }
11843 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
11844 }
11845
11846 /*
11847 ** pEList is a list of expressions which are really the result set of the
11848 ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
11849 ** This routine checks to see if pE is a simple identifier which corresponds
11850 ** to the AS-name of one of the terms of the expression list. If it is,
11851 ** this routine return an integer between 1 and N where N is the number of
11852 ** elements in pEList, corresponding to the matching entry. If there is
11853 ** no match, or if pE is not a simple identifier, then this routine
11854 ** return 0.
11855 **
11856 ** pEList has been resolved. pE has not.
11857 */
11858 static int resolveAsName(
11859 Parse *pParse, /* Parsing context for error messages */
11860 ExprList *pEList, /* List of expressions to scan */
11861 Expr *pE /* Expression we are trying to match */
11862 ){
11863 int i; /* Loop counter */
11864
11865 UNUSED_PARAMETER(pParse);
11866
11867 if( pE->op==TK_ID ){
11868 char *zCol = pE->u.zToken;
11869 for(i=0; i<pEList->nExpr; i++){
11870 char *zAs = pEList->a[i].zName;
11871 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
11872 return i+1;
11873 }
11874 }
11875 }
11876 return 0;
11877 }
11878
11879 /*
11880 ** pE is a pointer to an expression which is a single term in the
11881 ** ORDER BY of a compound SELECT. The expression has not been
11882 ** name resolved.
11883 **
11884 ** At the point this routine is called, we already know that the
11885 ** ORDER BY term is not an integer index into the result set. That
11886 ** case is handled by the calling routine.
11887 **
11888 ** Attempt to match pE against result set columns in the left-most
11889 ** SELECT statement. Return the index i of the matching column,
11890 ** as an indication to the caller that it should sort by the i-th column.
11891 ** The left-most column is 1. In other words, the value returned is the
11892 ** same integer value that would be used in the SQL statement to indicate
11893 ** the column.
11894 **
11895 ** If there is no match, return 0. Return -1 if an error occurs.
11896 */
11897 static int resolveOrderByTermToExprList(
11898 Parse *pParse, /* Parsing context for error messages */
11899 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
11900 Expr *pE /* The specific ORDER BY term */
11901 ){
11902 int i; /* Loop counter */
11903 ExprList *pEList; /* The columns of the result set */
11904 NameContext nc; /* Name context for resolving pE */
11905 sqlite3 *db; /* Database connection */
11906 int rc; /* Return code from subprocedures */
11907 u8 savedSuppErr; /* Saved value of db->suppressErr */
11908
11909 assert( sqlite3ExprIsInteger(pE, &i)==0 );
11910 pEList = pSelect->pEList;
11911
11912 /* Resolve all names in the ORDER BY term expression
11913 */
11914 memset(&nc, 0, sizeof(nc));
11915 nc.pParse = pParse;
11916 nc.pSrcList = pSelect->pSrc;
11917 nc.pEList = pEList;
11918 nc.ncFlags = NC_AllowAgg;
11919 nc.nErr = 0;
11920 db = pParse->db;
11921 savedSuppErr = db->suppressErr;
11922 db->suppressErr = 1;
11923 rc = sqlite3ResolveExprNames(&nc, pE);
11924 db->suppressErr = savedSuppErr;
11925 if( rc ) return 0;
11926
11927 /* Try to match the ORDER BY expression against an expression
11928 ** in the result set. Return an 1-based index of the matching
11929 ** result-set entry.
11930 */
11931 for(i=0; i<pEList->nExpr; i++){
11932 if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
11933 return i+1;
11934 }
11935 }
11936
11937 /* If no match, return 0. */
11938 return 0;
11939 }
11940
11941 /*
11942 ** Generate an ORDER BY or GROUP BY term out-of-range error.
11943 */
11944 static void resolveOutOfRangeError(
11945 Parse *pParse, /* The error context into which to write the error */
11946 const char *zType, /* "ORDER" or "GROUP" */
11947 int i, /* The index (1-based) of the term out of range */
11948 int mx /* Largest permissible value of i */
11949 ){
11950 sqlite3ErrorMsg(pParse,
11951 "%r %s BY term out of range - should be "
11952 "between 1 and %d", i, zType, mx);
11953 }
11954
11955 /*
11956 ** Analyze the ORDER BY clause in a compound SELECT statement. Modify
11957 ** each term of the ORDER BY clause is a constant integer between 1
11958 ** and N where N is the number of columns in the compound SELECT.
11959 **
11960 ** ORDER BY terms that are already an integer between 1 and N are
11961 ** unmodified. ORDER BY terms that are integers outside the range of
11962 ** 1 through N generate an error. ORDER BY terms that are expressions
11963 ** are matched against result set expressions of compound SELECT
11964 ** beginning with the left-most SELECT and working toward the right.
11965 ** At the first match, the ORDER BY expression is transformed into
11966 ** the integer column number.
11967 **
11968 ** Return the number of errors seen.
11969 */
11970 static int resolveCompoundOrderBy(
11971 Parse *pParse, /* Parsing context. Leave error messages here */
11972 Select *pSelect /* The SELECT statement containing the ORDER BY */
11973 ){
11974 int i;
11975 ExprList *pOrderBy;
11976 ExprList *pEList;
11977 sqlite3 *db;
11978 int moreToDo = 1;
11979
11980 pOrderBy = pSelect->pOrderBy;
11981 if( pOrderBy==0 ) return 0;
11982 db = pParse->db;
11983 #if SQLITE_MAX_COLUMN
11984 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
11985 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
11986 return 1;
11987 }
11988 #endif
11989 for(i=0; i<pOrderBy->nExpr; i++){
11990 pOrderBy->a[i].done = 0;
11991 }
11992 pSelect->pNext = 0;
11993 while( pSelect->pPrior ){
11994 pSelect->pPrior->pNext = pSelect;
11995 pSelect = pSelect->pPrior;
11996 }
11997 while( pSelect && moreToDo ){
11998 struct ExprList_item *pItem;
11999 moreToDo = 0;
12000 pEList = pSelect->pEList;
12001 assert( pEList!=0 );
12002 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
12003 int iCol = -1;
12004 Expr *pE, *pDup;
12005 if( pItem->done ) continue;
12006 pE = sqlite3ExprSkipCollate(pItem->pExpr);
12007 if( sqlite3ExprIsInteger(pE, &iCol) ){
12008 if( iCol<=0 || iCol>pEList->nExpr ){
12009 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
12010 return 1;
12011 }
12012 }else{
12013 iCol = resolveAsName(pParse, pEList, pE);
12014 if( iCol==0 ){
12015 pDup = sqlite3ExprDup(db, pE, 0);
12016 if( !db->mallocFailed ){
12017 assert(pDup);
12018 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
12019 }
12020 sqlite3ExprDelete(db, pDup);
12021 }
12022 }
12023 if( iCol>0 ){
12024 /* Convert the ORDER BY term into an integer column number iCol,
12025 ** taking care to preserve the COLLATE clause if it exists */
12026 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
12027 if( pNew==0 ) return 1;
12028 pNew->flags |= EP_IntValue;
12029 pNew->u.iValue = iCol;
12030 if( pItem->pExpr==pE ){
12031 pItem->pExpr = pNew;
12032 }else{
12033 Expr *pParent = pItem->pExpr;
12034 assert( pParent->op==TK_COLLATE );
12035 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
12036 assert( pParent->pLeft==pE );
12037 pParent->pLeft = pNew;
12038 }
12039 sqlite3ExprDelete(db, pE);
12040 pItem->u.x.iOrderByCol = (u16)iCol;
12041 pItem->done = 1;
12042 }else{
12043 moreToDo = 1;
12044 }
12045 }
12046 pSelect = pSelect->pNext;
12047 }
12048 for(i=0; i<pOrderBy->nExpr; i++){
12049 if( pOrderBy->a[i].done==0 ){
12050 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
12051 "column in the result set", i+1);
12052 return 1;
12053 }
12054 }
12055 return 0;
12056 }
12057
12058 /*
12059 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
12060 ** the SELECT statement pSelect. If any term is reference to a
12061 ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
12062 ** field) then convert that term into a copy of the corresponding result set
12063 ** column.
12064 **
12065 ** If any errors are detected, add an error message to pParse and
12066 ** return non-zero. Return zero if no errors are seen.
12067 */
12068 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
12069 Parse *pParse, /* Parsing context. Leave error messages here */
12070 Select *pSelect, /* The SELECT statement containing the clause */
12071 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
12072 const char *zType /* "ORDER" or "GROUP" */
12073 ){
12074 int i;
12075 sqlite3 *db = pParse->db;
12076 ExprList *pEList;
12077 struct ExprList_item *pItem;
12078
12079 if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
12080 #if SQLITE_MAX_COLUMN
12081 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
12082 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
12083 return 1;
12084 }
12085 #endif
12086 pEList = pSelect->pEList;
12087 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
12088 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
12089 if( pItem->u.x.iOrderByCol ){
12090 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
12091 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
12092 return 1;
12093 }
12094 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
12095 zType,0);
12096 }
12097 }
12098 return 0;
12099 }
12100
12101 /*
12102 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
12103 ** The Name context of the SELECT statement is pNC. zType is either
12104 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
12105 **
12106 ** This routine resolves each term of the clause into an expression.
12107 ** If the order-by term is an integer I between 1 and N (where N is the
12108 ** number of columns in the result set of the SELECT) then the expression
12109 ** in the resolution is a copy of the I-th result-set expression. If
12110 ** the order-by term is an identifier that corresponds to the AS-name of
12111 ** a result-set expression, then the term resolves to a copy of the
12112 ** result-set expression. Otherwise, the expression is resolved in
12113 ** the usual way - using sqlite3ResolveExprNames().
12114 **
12115 ** This routine returns the number of errors. If errors occur, then
12116 ** an appropriate error message might be left in pParse. (OOM errors
12117 ** excepted.)
12118 */
12119 static int resolveOrderGroupBy(
12120 NameContext *pNC, /* The name context of the SELECT statement */
12121 Select *pSelect, /* The SELECT statement holding pOrderBy */
12122 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
12123 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
12124 ){
12125 int i, j; /* Loop counters */
12126 int iCol; /* Column number */
12127 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
12128 Parse *pParse; /* Parsing context */
12129 int nResult; /* Number of terms in the result set */
12130
12131 if( pOrderBy==0 ) return 0;
12132 nResult = pSelect->pEList->nExpr;
12133 pParse = pNC->pParse;
12134 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
12135 Expr *pE = pItem->pExpr;
12136 Expr *pE2 = sqlite3ExprSkipCollate(pE);
12137 if( zType[0]!='G' ){
12138 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
12139 if( iCol>0 ){
12140 /* If an AS-name match is found, mark this ORDER BY column as being
12141 ** a copy of the iCol-th result-set column. The subsequent call to
12142 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
12143 ** copy of the iCol-th result-set expression. */
12144 pItem->u.x.iOrderByCol = (u16)iCol;
12145 continue;
12146 }
12147 }
12148 if( sqlite3ExprIsInteger(pE2, &iCol) ){
12149 /* The ORDER BY term is an integer constant. Again, set the column
12150 ** number so that sqlite3ResolveOrderGroupBy() will convert the
12151 ** order-by term to a copy of the result-set expression */
12152 if( iCol<1 || iCol>0xffff ){
12153 resolveOutOfRangeError(pParse, zType, i+1, nResult);
12154 return 1;
12155 }
12156 pItem->u.x.iOrderByCol = (u16)iCol;
12157 continue;
12158 }
12159
12160 /* Otherwise, treat the ORDER BY term as an ordinary expression */
12161 pItem->u.x.iOrderByCol = 0;
12162 if( sqlite3ResolveExprNames(pNC, pE) ){
12163 return 1;
12164 }
12165 for(j=0; j<pSelect->pEList->nExpr; j++){
12166 if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
12167 pItem->u.x.iOrderByCol = j+1;
12168 }
12169 }
12170 }
12171 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
12172 }
12173
12174 /*
12175 ** Resolve names in the SELECT statement p and all of its descendants.
12176 */
12177 static int resolveSelectStep(Walker *pWalker, Select *p){
12178 NameContext *pOuterNC; /* Context that contains this SELECT */
12179 NameContext sNC; /* Name context of this SELECT */
12180 int isCompound; /* True if p is a compound select */
12181 int nCompound; /* Number of compound terms processed so far */
12182 Parse *pParse; /* Parsing context */
12183 int i; /* Loop counter */
12184 ExprList *pGroupBy; /* The GROUP BY clause */
12185 Select *pLeftmost; /* Left-most of SELECT of a compound */
12186 sqlite3 *db; /* Database connection */
12187
12188
12189 assert( p!=0 );
12190 if( p->selFlags & SF_Resolved ){
12191 return WRC_Prune;
12192 }
12193 pOuterNC = pWalker->u.pNC;
12194 pParse = pWalker->pParse;
12195 db = pParse->db;
12196
12197 /* Normally sqlite3SelectExpand() will be called first and will have
12198 ** already expanded this SELECT. However, if this is a subquery within
12199 ** an expression, sqlite3ResolveExprNames() will be called without a
12200 ** prior call to sqlite3SelectExpand(). When that happens, let
12201 ** sqlite3SelectPrep() do all of the processing for this SELECT.
12202 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
12203 ** this routine in the correct order.
12204 */
12205 if( (p->selFlags & SF_Expanded)==0 ){
12206 sqlite3SelectPrep(pParse, p, pOuterNC);
12207 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
12208 }
12209
12210 isCompound = p->pPrior!=0;
12211 nCompound = 0;
12212 pLeftmost = p;
12213 while( p ){
12214 assert( (p->selFlags & SF_Expanded)!=0 );
12215 assert( (p->selFlags & SF_Resolved)==0 );
12216 p->selFlags |= SF_Resolved;
12217
12218 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
12219 ** are not allowed to refer to any names, so pass an empty NameContext.
12220 */
12221 memset(&sNC, 0, sizeof(sNC));
12222 sNC.pParse = pParse;
12223 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
12224 sqlite3ResolveExprNames(&sNC, p->pOffset) ){
12225 return WRC_Abort;
12226 }
12227
12228 /* If the SF_Converted flags is set, then this Select object was
12229 ** was created by the convertCompoundSelectToSubquery() function.
12230 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
12231 ** as if it were part of the sub-query, not the parent. This block
12232 ** moves the pOrderBy down to the sub-query. It will be moved back
12233 ** after the names have been resolved. */
12234 if( p->selFlags & SF_Converted ){
12235 Select *pSub = p->pSrc->a[0].pSelect;
12236 assert( p->pSrc->nSrc==1 && p->pOrderBy );
12237 assert( pSub->pPrior && pSub->pOrderBy==0 );
12238 pSub->pOrderBy = p->pOrderBy;
12239 p->pOrderBy = 0;
12240 }
12241
12242 /* Recursively resolve names in all subqueries
12243 */
12244 for(i=0; i<p->pSrc->nSrc; i++){
12245 struct SrcList_item *pItem = &p->pSrc->a[i];
12246 if( pItem->pSelect ){
12247 NameContext *pNC; /* Used to iterate name contexts */
12248 int nRef = 0; /* Refcount for pOuterNC and outer contexts */
12249 const char *zSavedContext = pParse->zAuthContext;
12250
12251 /* Count the total number of references to pOuterNC and all of its
12252 ** parent contexts. After resolving references to expressions in
12253 ** pItem->pSelect, check if this value has changed. If so, then
12254 ** SELECT statement pItem->pSelect must be correlated. Set the
12255 ** pItem->fg.isCorrelated flag if this is the case. */
12256 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
12257
12258 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
12259 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
12260 pParse->zAuthContext = zSavedContext;
12261 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
12262
12263 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
12264 assert( pItem->fg.isCorrelated==0 && nRef<=0 );
12265 pItem->fg.isCorrelated = (nRef!=0);
12266 }
12267 }
12268
12269 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
12270 ** resolve the result-set expression list.
12271 */
12272 sNC.ncFlags = NC_AllowAgg;
12273 sNC.pSrcList = p->pSrc;
12274 sNC.pNext = pOuterNC;
12275
12276 /* Resolve names in the result set. */
12277 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
12278
12279 /* If there are no aggregate functions in the result-set, and no GROUP BY
12280 ** expression, do not allow aggregates in any of the other expressions.
12281 */
12282 assert( (p->selFlags & SF_Aggregate)==0 );
12283 pGroupBy = p->pGroupBy;
12284 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
12285 assert( NC_MinMaxAgg==SF_MinMaxAgg );
12286 p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
12287 }else{
12288 sNC.ncFlags &= ~NC_AllowAgg;
12289 }
12290
12291 /* If a HAVING clause is present, then there must be a GROUP BY clause.
12292 */
12293 if( p->pHaving && !pGroupBy ){
12294 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
12295 return WRC_Abort;
12296 }
12297
12298 /* Add the output column list to the name-context before parsing the
12299 ** other expressions in the SELECT statement. This is so that
12300 ** expressions in the WHERE clause (etc.) can refer to expressions by
12301 ** aliases in the result set.
12302 **
12303 ** Minor point: If this is the case, then the expression will be
12304 ** re-evaluated for each reference to it.
12305 */
12306 sNC.pEList = p->pEList;
12307 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
12308 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
12309
12310 /* Resolve names in table-valued-function arguments */
12311 for(i=0; i<p->pSrc->nSrc; i++){
12312 struct SrcList_item *pItem = &p->pSrc->a[i];
12313 if( pItem->fg.isTabFunc
12314 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
12315 ){
12316 return WRC_Abort;
12317 }
12318 }
12319
12320 /* The ORDER BY and GROUP BY clauses may not refer to terms in
12321 ** outer queries
12322 */
12323 sNC.pNext = 0;
12324 sNC.ncFlags |= NC_AllowAgg;
12325
12326 /* If this is a converted compound query, move the ORDER BY clause from
12327 ** the sub-query back to the parent query. At this point each term
12328 ** within the ORDER BY clause has been transformed to an integer value.
12329 ** These integers will be replaced by copies of the corresponding result
12330 ** set expressions by the call to resolveOrderGroupBy() below. */
12331 if( p->selFlags & SF_Converted ){
12332 Select *pSub = p->pSrc->a[0].pSelect;
12333 p->pOrderBy = pSub->pOrderBy;
12334 pSub->pOrderBy = 0;
12335 }
12336
12337 /* Process the ORDER BY clause for singleton SELECT statements.
12338 ** The ORDER BY clause for compounds SELECT statements is handled
12339 ** below, after all of the result-sets for all of the elements of
12340 ** the compound have been resolved.
12341 **
12342 ** If there is an ORDER BY clause on a term of a compound-select other
12343 ** than the right-most term, then that is a syntax error. But the error
12344 ** is not detected until much later, and so we need to go ahead and
12345 ** resolve those symbols on the incorrect ORDER BY for consistency.
12346 */
12347 if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
12348 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
12349 ){
12350 return WRC_Abort;
12351 }
12352 if( db->mallocFailed ){
12353 return WRC_Abort;
12354 }
12355
12356 /* Resolve the GROUP BY clause. At the same time, make sure
12357 ** the GROUP BY clause does not contain aggregate functions.
12358 */
12359 if( pGroupBy ){
12360 struct ExprList_item *pItem;
12361
12362 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
12363 return WRC_Abort;
12364 }
12365 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
12366 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
12367 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
12368 "the GROUP BY clause");
12369 return WRC_Abort;
12370 }
12371 }
12372 }
12373
12374 /* If this is part of a compound SELECT, check that it has the right
12375 ** number of expressions in the select list. */
12376 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
12377 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
12378 return WRC_Abort;
12379 }
12380
12381 /* Advance to the next term of the compound
12382 */
12383 p = p->pPrior;
12384 nCompound++;
12385 }
12386
12387 /* Resolve the ORDER BY on a compound SELECT after all terms of
12388 ** the compound have been resolved.
12389 */
12390 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
12391 return WRC_Abort;
12392 }
12393
12394 return WRC_Prune;
12395 }
12396
12397 /*
12398 ** This routine walks an expression tree and resolves references to
12399 ** table columns and result-set columns. At the same time, do error
12400 ** checking on function usage and set a flag if any aggregate functions
12401 ** are seen.
12402 **
12403 ** To resolve table columns references we look for nodes (or subtrees) of the
12404 ** form X.Y.Z or Y.Z or just Z where
12405 **
12406 ** X: The name of a database. Ex: "main" or "temp" or
12407 ** the symbolic name assigned to an ATTACH-ed database.
12408 **
12409 ** Y: The name of a table in a FROM clause. Or in a trigger
12410 ** one of the special names "old" or "new".
12411 **
12412 ** Z: The name of a column in table Y.
12413 **
12414 ** The node at the root of the subtree is modified as follows:
12415 **
12416 ** Expr.op Changed to TK_COLUMN
12417 ** Expr.pTab Points to the Table object for X.Y
12418 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
12419 ** Expr.iTable The VDBE cursor number for X.Y
12420 **
12421 **
12422 ** To resolve result-set references, look for expression nodes of the
12423 ** form Z (with no X and Y prefix) where the Z matches the right-hand
12424 ** size of an AS clause in the result-set of a SELECT. The Z expression
12425 ** is replaced by a copy of the left-hand side of the result-set expression.
12426 ** Table-name and function resolution occurs on the substituted expression
12427 ** tree. For example, in:
12428 **
12429 ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
12430 **
12431 ** The "x" term of the order by is replaced by "a+b" to render:
12432 **
12433 ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
12434 **
12435 ** Function calls are checked to make sure that the function is
12436 ** defined and that the correct number of arguments are specified.
12437 ** If the function is an aggregate function, then the NC_HasAgg flag is
12438 ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
12439 ** If an expression contains aggregate functions then the EP_Agg
12440 ** property on the expression is set.
12441 **
12442 ** An error message is left in pParse if anything is amiss. The number
12443 ** if errors is returned.
12444 */
12445 SQLITE_PRIVATE int sqlite3ResolveExprNames(
12446 NameContext *pNC, /* Namespace to resolve expressions in. */
12447 Expr *pExpr /* The expression to be analyzed. */
12448 ){
12449 u16 savedHasAgg;
12450 Walker w;
12451
12452 if( pExpr==0 ) return 0;
12453 #if SQLITE_MAX_EXPR_DEPTH>0
12454 {
12455 Parse *pParse = pNC->pParse;
12456 if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
12457 return 1;
12458 }
12459 pParse->nHeight += pExpr->nHeight;
12460 }
12461 #endif
12462 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
12463 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
12464 w.pParse = pNC->pParse;
12465 w.xExprCallback = resolveExprStep;
12466 w.xSelectCallback = resolveSelectStep;
12467 w.xSelectCallback2 = 0;
12468 w.walkerDepth = 0;
12469 w.eCode = 0;
12470 w.u.pNC = pNC;
12471 sqlite3WalkExpr(&w, pExpr);
12472 #if SQLITE_MAX_EXPR_DEPTH>0
12473 pNC->pParse->nHeight -= pExpr->nHeight;
12474 #endif
12475 if( pNC->nErr>0 || w.pParse->nErr>0 ){
12476 ExprSetProperty(pExpr, EP_Error);
12477 }
12478 if( pNC->ncFlags & NC_HasAgg ){
12479 ExprSetProperty(pExpr, EP_Agg);
12480 }
12481 pNC->ncFlags |= savedHasAgg;
12482 return ExprHasProperty(pExpr, EP_Error);
12483 }
12484
12485 /*
12486 ** Resolve all names for all expression in an expression list. This is
12487 ** just like sqlite3ResolveExprNames() except that it works for an expression
12488 ** list rather than a single expression.
12489 */
12490 SQLITE_PRIVATE int sqlite3ResolveExprListNames(
12491 NameContext *pNC, /* Namespace to resolve expressions in. */
12492 ExprList *pList /* The expression list to be analyzed. */
12493 ){
12494 int i;
12495 if( pList ){
12496 for(i=0; i<pList->nExpr; i++){
12497 if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
12498 }
12499 }
12500 return WRC_Continue;
12501 }
12502
12503 /*
12504 ** Resolve all names in all expressions of a SELECT and in all
12505 ** decendents of the SELECT, including compounds off of p->pPrior,
12506 ** subqueries in expressions, and subqueries used as FROM clause
12507 ** terms.
12508 **
12509 ** See sqlite3ResolveExprNames() for a description of the kinds of
12510 ** transformations that occur.
12511 **
12512 ** All SELECT statements should have been expanded using
12513 ** sqlite3SelectExpand() prior to invoking this routine.
12514 */
12515 SQLITE_PRIVATE void sqlite3ResolveSelectNames(
12516 Parse *pParse, /* The parser context */
12517 Select *p, /* The SELECT statement being coded. */
12518 NameContext *pOuterNC /* Name context for parent SELECT statement */
12519 ){
12520 Walker w;
12521
12522 assert( p!=0 );
12523 memset(&w, 0, sizeof(w));
12524 w.xExprCallback = resolveExprStep;
12525 w.xSelectCallback = resolveSelectStep;
12526 w.pParse = pParse;
12527 w.u.pNC = pOuterNC;
12528 sqlite3WalkSelect(&w, p);
12529 }
12530
12531 /*
12532 ** Resolve names in expressions that can only reference a single table:
12533 **
12534 ** * CHECK constraints
12535 ** * WHERE clauses on partial indices
12536 **
12537 ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
12538 ** is set to -1 and the Expr.iColumn value is set to the column number.
12539 **
12540 ** Any errors cause an error message to be set in pParse.
12541 */
12542 SQLITE_PRIVATE void sqlite3ResolveSelfReference(
12543 Parse *pParse, /* Parsing context */
12544 Table *pTab, /* The table being referenced */
12545 int type, /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */
12546 Expr *pExpr, /* Expression to resolve. May be NULL. */
12547 ExprList *pList /* Expression list to resolve. May be NUL. */
12548 ){
12549 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
12550 NameContext sNC; /* Name context for pParse->pNewTable */
12551
12552 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr );
12553 memset(&sNC, 0, sizeof(sNC));
12554 memset(&sSrc, 0, sizeof(sSrc));
12555 sSrc.nSrc = 1;
12556 sSrc.a[0].zName = pTab->zName;
12557 sSrc.a[0].pTab = pTab;
12558 sSrc.a[0].iCursor = -1;
12559 sNC.pParse = pParse;
12560 sNC.pSrcList = &sSrc;
12561 sNC.ncFlags = type;
12562 if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
12563 if( pList ) sqlite3ResolveExprListNames(&sNC, pList);
12564 }
12565
12566 /************** End of resolve.c *********************************************/
12567 /************** Begin file expr.c ********************************************/
12568 /*
12569 ** 2001 September 15
12570 **
12571 ** The author disclaims copyright to this source code. In place of
12572 ** a legal notice, here is a blessing:
12573 **
12574 ** May you do good and not evil.
12575 ** May you find forgiveness for yourself and forgive others.
12576 ** May you share freely, never taking more than you give.
12577 **
12578 *************************************************************************
12579 ** This file contains routines used for analyzing expressions and
12580 ** for generating VDBE code that evaluates expressions in SQLite.
12581 */
12582 /* #include "sqliteInt.h" */
12583
12584 /* Forward declarations */
12585 static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
12586 static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
12587
12588 /*
12589 ** Return the affinity character for a single column of a table.
12590 */
12591 SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table *pTab, int iCol){
12592 assert( iCol<pTab->nCol );
12593 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
12594 }
12595
12596 /*
12597 ** Return the 'affinity' of the expression pExpr if any.
12598 **
12599 ** If pExpr is a column, a reference to a column via an 'AS' alias,
12600 ** or a sub-select with a column as the return value, then the
12601 ** affinity of that column is returned. Otherwise, 0x00 is returned,
12602 ** indicating no affinity for the expression.
12603 **
12604 ** i.e. the WHERE clause expressions in the following statements all
12605 ** have an affinity:
12606 **
12607 ** CREATE TABLE t1(a);
12608 ** SELECT * FROM t1 WHERE a;
12609 ** SELECT a AS b FROM t1 WHERE b;
12610 ** SELECT * FROM t1 WHERE (select a from t1);
12611 */
12612 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
12613 int op;
12614 pExpr = sqlite3ExprSkipCollate(pExpr);
12615 if( pExpr->flags & EP_Generic ) return 0;
12616 op = pExpr->op;
12617 if( op==TK_SELECT ){
12618 assert( pExpr->flags&EP_xIsSelect );
12619 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
12620 }
12621 if( op==TK_REGISTER ) op = pExpr->op2;
12622 #ifndef SQLITE_OMIT_CAST
12623 if( op==TK_CAST ){
12624 assert( !ExprHasProperty(pExpr, EP_IntValue) );
12625 return sqlite3AffinityType(pExpr->u.zToken, 0);
12626 }
12627 #endif
12628 if( op==TK_AGG_COLUMN || op==TK_COLUMN ){
12629 return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
12630 }
12631 if( op==TK_SELECT_COLUMN ){
12632 assert( pExpr->pLeft->flags&EP_xIsSelect );
12633 return sqlite3ExprAffinity(
12634 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
12635 );
12636 }
12637 return pExpr->affinity;
12638 }
12639
12640 /*
12641 ** Set the collating sequence for expression pExpr to be the collating
12642 ** sequence named by pToken. Return a pointer to a new Expr node that
12643 ** implements the COLLATE operator.
12644 **
12645 ** If a memory allocation error occurs, that fact is recorded in pParse->db
12646 ** and the pExpr parameter is returned unchanged.
12647 */
12648 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
12649 Parse *pParse, /* Parsing context */
12650 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
12651 const Token *pCollName, /* Name of collating sequence */
12652 int dequote /* True to dequote pCollName */
12653 ){
12654 if( pCollName->n>0 ){
12655 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
12656 if( pNew ){
12657 pNew->pLeft = pExpr;
12658 pNew->flags |= EP_Collate|EP_Skip;
12659 pExpr = pNew;
12660 }
12661 }
12662 return pExpr;
12663 }
12664 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, con st char *zC){
12665 Token s;
12666 assert( zC!=0 );
12667 sqlite3TokenInit(&s, (char*)zC);
12668 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
12669 }
12670
12671 /*
12672 ** Skip over any TK_COLLATE operators and any unlikely()
12673 ** or likelihood() function at the root of an expression.
12674 */
12675 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
12676 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
12677 if( ExprHasProperty(pExpr, EP_Unlikely) ){
12678 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
12679 assert( pExpr->x.pList->nExpr>0 );
12680 assert( pExpr->op==TK_FUNCTION );
12681 pExpr = pExpr->x.pList->a[0].pExpr;
12682 }else{
12683 assert( pExpr->op==TK_COLLATE );
12684 pExpr = pExpr->pLeft;
12685 }
12686 }
12687 return pExpr;
12688 }
12689
12690 /*
12691 ** Return the collation sequence for the expression pExpr. If
12692 ** there is no defined collating sequence, return NULL.
12693 **
12694 ** The collating sequence might be determined by a COLLATE operator
12695 ** or by the presence of a column with a defined collating sequence.
12696 ** COLLATE operators take first precedence. Left operands take
12697 ** precedence over right operands.
12698 */
12699 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
12700 sqlite3 *db = pParse->db;
12701 CollSeq *pColl = 0;
12702 Expr *p = pExpr;
12703 while( p ){
12704 int op = p->op;
12705 if( p->flags & EP_Generic ) break;
12706 if( op==TK_CAST || op==TK_UPLUS ){
12707 p = p->pLeft;
12708 continue;
12709 }
12710 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
12711 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
12712 break;
12713 }
12714 if( (op==TK_AGG_COLUMN || op==TK_COLUMN
12715 || op==TK_REGISTER || op==TK_TRIGGER)
12716 && p->pTab!=0
12717 ){
12718 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
12719 ** a TK_COLUMN but was previously evaluated and cached in a register */
12720 int j = p->iColumn;
12721 if( j>=0 ){
12722 const char *zColl = p->pTab->aCol[j].zColl;
12723 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
12724 }
12725 break;
12726 }
12727 if( p->flags & EP_Collate ){
12728 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
12729 p = p->pLeft;
12730 }else{
12731 Expr *pNext = p->pRight;
12732 /* The Expr.x union is never used at the same time as Expr.pRight */
12733 assert( p->x.pList==0 || p->pRight==0 );
12734 /* p->flags holds EP_Collate and p->pLeft->flags does not. And
12735 ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at
12736 ** least one EP_Collate. Thus the following two ALWAYS. */
12737 if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){
12738 int i;
12739 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
12740 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
12741 pNext = p->x.pList->a[i].pExpr;
12742 break;
12743 }
12744 }
12745 }
12746 p = pNext;
12747 }
12748 }else{
12749 break;
12750 }
12751 }
12752 if( sqlite3CheckCollSeq(pParse, pColl) ){
12753 pColl = 0;
12754 }
12755 return pColl;
12756 }
12757
12758 /*
12759 ** pExpr is an operand of a comparison operator. aff2 is the
12760 ** type affinity of the other operand. This routine returns the
12761 ** type affinity that should be used for the comparison operator.
12762 */
12763 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
12764 char aff1 = sqlite3ExprAffinity(pExpr);
12765 if( aff1 && aff2 ){
12766 /* Both sides of the comparison are columns. If one has numeric
12767 ** affinity, use that. Otherwise use no affinity.
12768 */
12769 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
12770 return SQLITE_AFF_NUMERIC;
12771 }else{
12772 return SQLITE_AFF_BLOB;
12773 }
12774 }else if( !aff1 && !aff2 ){
12775 /* Neither side of the comparison is a column. Compare the
12776 ** results directly.
12777 */
12778 return SQLITE_AFF_BLOB;
12779 }else{
12780 /* One side is a column, the other is not. Use the columns affinity. */
12781 assert( aff1==0 || aff2==0 );
12782 return (aff1 + aff2);
12783 }
12784 }
12785
12786 /*
12787 ** pExpr is a comparison operator. Return the type affinity that should
12788 ** be applied to both operands prior to doing the comparison.
12789 */
12790 static char comparisonAffinity(Expr *pExpr){
12791 char aff;
12792 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
12793 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
12794 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
12795 assert( pExpr->pLeft );
12796 aff = sqlite3ExprAffinity(pExpr->pLeft);
12797 if( pExpr->pRight ){
12798 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
12799 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
12800 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
12801 }else if( aff==0 ){
12802 aff = SQLITE_AFF_BLOB;
12803 }
12804 return aff;
12805 }
12806
12807 /*
12808 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
12809 ** idx_affinity is the affinity of an indexed column. Return true
12810 ** if the index with affinity idx_affinity may be used to implement
12811 ** the comparison in pExpr.
12812 */
12813 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
12814 char aff = comparisonAffinity(pExpr);
12815 switch( aff ){
12816 case SQLITE_AFF_BLOB:
12817 return 1;
12818 case SQLITE_AFF_TEXT:
12819 return idx_affinity==SQLITE_AFF_TEXT;
12820 default:
12821 return sqlite3IsNumericAffinity(idx_affinity);
12822 }
12823 }
12824
12825 /*
12826 ** Return the P5 value that should be used for a binary comparison
12827 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
12828 */
12829 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
12830 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
12831 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
12832 return aff;
12833 }
12834
12835 /*
12836 ** Return a pointer to the collation sequence that should be used by
12837 ** a binary comparison operator comparing pLeft and pRight.
12838 **
12839 ** If the left hand expression has a collating sequence type, then it is
12840 ** used. Otherwise the collation sequence for the right hand expression
12841 ** is used, or the default (BINARY) if neither expression has a collating
12842 ** type.
12843 **
12844 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
12845 ** it is not considered.
12846 */
12847 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
12848 Parse *pParse,
12849 Expr *pLeft,
12850 Expr *pRight
12851 ){
12852 CollSeq *pColl;
12853 assert( pLeft );
12854 if( pLeft->flags & EP_Collate ){
12855 pColl = sqlite3ExprCollSeq(pParse, pLeft);
12856 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
12857 pColl = sqlite3ExprCollSeq(pParse, pRight);
12858 }else{
12859 pColl = sqlite3ExprCollSeq(pParse, pLeft);
12860 if( !pColl ){
12861 pColl = sqlite3ExprCollSeq(pParse, pRight);
12862 }
12863 }
12864 return pColl;
12865 }
12866
12867 /*
12868 ** Generate code for a comparison operator.
12869 */
12870 static int codeCompare(
12871 Parse *pParse, /* The parsing (and code generating) context */
12872 Expr *pLeft, /* The left operand */
12873 Expr *pRight, /* The right operand */
12874 int opcode, /* The comparison opcode */
12875 int in1, int in2, /* Register holding operands */
12876 int dest, /* Jump here if true. */
12877 int jumpIfNull /* If true, jump if either operand is NULL */
12878 ){
12879 int p5;
12880 int addr;
12881 CollSeq *p4;
12882
12883 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
12884 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
12885 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
12886 (void*)p4, P4_COLLSEQ);
12887 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
12888 return addr;
12889 }
12890
12891 /*
12892 ** Return true if expression pExpr is a vector, or false otherwise.
12893 **
12894 ** A vector is defined as any expression that results in two or more
12895 ** columns of result. Every TK_VECTOR node is an vector because the
12896 ** parser will not generate a TK_VECTOR with fewer than two entries.
12897 ** But a TK_SELECT might be either a vector or a scalar. It is only
12898 ** considered a vector if it has two or more result columns.
12899 */
12900 SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr){
12901 return sqlite3ExprVectorSize(pExpr)>1;
12902 }
12903
12904 /*
12905 ** If the expression passed as the only argument is of type TK_VECTOR
12906 ** return the number of expressions in the vector. Or, if the expression
12907 ** is a sub-select, return the number of columns in the sub-select. For
12908 ** any other type of expression, return 1.
12909 */
12910 SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr){
12911 u8 op = pExpr->op;
12912 if( op==TK_REGISTER ) op = pExpr->op2;
12913 if( op==TK_VECTOR ){
12914 return pExpr->x.pList->nExpr;
12915 }else if( op==TK_SELECT ){
12916 return pExpr->x.pSelect->pEList->nExpr;
12917 }else{
12918 return 1;
12919 }
12920 }
12921
12922 #ifndef SQLITE_OMIT_SUBQUERY
12923 /*
12924 ** Return a pointer to a subexpression of pVector that is the i-th
12925 ** column of the vector (numbered starting with 0). The caller must
12926 ** ensure that i is within range.
12927 **
12928 ** If pVector is really a scalar (and "scalar" here includes subqueries
12929 ** that return a single column!) then return pVector unmodified.
12930 **
12931 ** pVector retains ownership of the returned subexpression.
12932 **
12933 ** If the vector is a (SELECT ...) then the expression returned is
12934 ** just the expression for the i-th term of the result set, and may
12935 ** not be ready for evaluation because the table cursor has not yet
12936 ** been positioned.
12937 */
12938 SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
12939 assert( i<sqlite3ExprVectorSize(pVector) );
12940 if( sqlite3ExprIsVector(pVector) ){
12941 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
12942 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
12943 return pVector->x.pSelect->pEList->a[i].pExpr;
12944 }else{
12945 return pVector->x.pList->a[i].pExpr;
12946 }
12947 }
12948 return pVector;
12949 }
12950 #endif /* !defined(SQLITE_OMIT_SUBQUERY) */
12951
12952 #ifndef SQLITE_OMIT_SUBQUERY
12953 /*
12954 ** Compute and return a new Expr object which when passed to
12955 ** sqlite3ExprCode() will generate all necessary code to compute
12956 ** the iField-th column of the vector expression pVector.
12957 **
12958 ** It is ok for pVector to be a scalar (as long as iField==0).
12959 ** In that case, this routine works like sqlite3ExprDup().
12960 **
12961 ** The caller owns the returned Expr object and is responsible for
12962 ** ensuring that the returned value eventually gets freed.
12963 **
12964 ** The caller retains ownership of pVector. If pVector is a TK_SELECT,
12965 ** then the returned object will reference pVector and so pVector must remain
12966 ** valid for the life of the returned object. If pVector is a TK_VECTOR
12967 ** or a scalar expression, then it can be deleted as soon as this routine
12968 ** returns.
12969 **
12970 ** A trick to cause a TK_SELECT pVector to be deleted together with
12971 ** the returned Expr object is to attach the pVector to the pRight field
12972 ** of the returned TK_SELECT_COLUMN Expr object.
12973 */
12974 SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(
12975 Parse *pParse, /* Parsing context */
12976 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
12977 int iField /* Which column of the vector to return */
12978 ){
12979 Expr *pRet;
12980 if( pVector->op==TK_SELECT ){
12981 assert( pVector->flags & EP_xIsSelect );
12982 /* The TK_SELECT_COLUMN Expr node:
12983 **
12984 ** pLeft: pVector containing TK_SELECT. Not deleted.
12985 ** pRight: not used. But recursively deleted.
12986 ** iColumn: Index of a column in pVector
12987 ** iTable: 0 or the number of columns on the LHS of an assignment
12988 ** pLeft->iTable: First in an array of register holding result, or 0
12989 ** if the result is not yet computed.
12990 **
12991 ** sqlite3ExprDelete() specifically skips the recursive delete of
12992 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
12993 ** can be attached to pRight to cause this node to take ownership of
12994 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
12995 ** with the same pLeft pointer to the pVector, but only one of them
12996 ** will own the pVector.
12997 */
12998 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0);
12999 if( pRet ){
13000 pRet->iColumn = iField;
13001 pRet->pLeft = pVector;
13002 }
13003 assert( pRet==0 || pRet->iTable==0 );
13004 }else{
13005 if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
13006 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
13007 }
13008 return pRet;
13009 }
13010 #endif /* !define(SQLITE_OMIT_SUBQUERY) */
13011
13012 /*
13013 ** If expression pExpr is of type TK_SELECT, generate code to evaluate
13014 ** it. Return the register in which the result is stored (or, if the
13015 ** sub-select returns more than one column, the first in an array
13016 ** of registers in which the result is stored).
13017 **
13018 ** If pExpr is not a TK_SELECT expression, return 0.
13019 */
13020 static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
13021 int reg = 0;
13022 #ifndef SQLITE_OMIT_SUBQUERY
13023 if( pExpr->op==TK_SELECT ){
13024 reg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
13025 }
13026 #endif
13027 return reg;
13028 }
13029
13030 /*
13031 ** Argument pVector points to a vector expression - either a TK_VECTOR
13032 ** or TK_SELECT that returns more than one column. This function returns
13033 ** the register number of a register that contains the value of
13034 ** element iField of the vector.
13035 **
13036 ** If pVector is a TK_SELECT expression, then code for it must have
13037 ** already been generated using the exprCodeSubselect() routine. In this
13038 ** case parameter regSelect should be the first in an array of registers
13039 ** containing the results of the sub-select.
13040 **
13041 ** If pVector is of type TK_VECTOR, then code for the requested field
13042 ** is generated. In this case (*pRegFree) may be set to the number of
13043 ** a temporary register to be freed by the caller before returning.
13044 **
13045 ** Before returning, output parameter (*ppExpr) is set to point to the
13046 ** Expr object corresponding to element iElem of the vector.
13047 */
13048 static int exprVectorRegister(
13049 Parse *pParse, /* Parse context */
13050 Expr *pVector, /* Vector to extract element from */
13051 int iField, /* Field to extract from pVector */
13052 int regSelect, /* First in array of registers */
13053 Expr **ppExpr, /* OUT: Expression element */
13054 int *pRegFree /* OUT: Temp register to free */
13055 ){
13056 u8 op = pVector->op;
13057 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT );
13058 if( op==TK_REGISTER ){
13059 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
13060 return pVector->iTable+iField;
13061 }
13062 if( op==TK_SELECT ){
13063 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
13064 return regSelect+iField;
13065 }
13066 *ppExpr = pVector->x.pList->a[iField].pExpr;
13067 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
13068 }
13069
13070 /*
13071 ** Expression pExpr is a comparison between two vector values. Compute
13072 ** the result of the comparison (1, 0, or NULL) and write that
13073 ** result into register dest.
13074 **
13075 ** The caller must satisfy the following preconditions:
13076 **
13077 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
13078 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
13079 ** otherwise: op==pExpr->op and p5==0
13080 */
13081 static void codeVectorCompare(
13082 Parse *pParse, /* Code generator context */
13083 Expr *pExpr, /* The comparison operation */
13084 int dest, /* Write results into this register */
13085 u8 op, /* Comparison operator */
13086 u8 p5 /* SQLITE_NULLEQ or zero */
13087 ){
13088 Vdbe *v = pParse->pVdbe;
13089 Expr *pLeft = pExpr->pLeft;
13090 Expr *pRight = pExpr->pRight;
13091 int nLeft = sqlite3ExprVectorSize(pLeft);
13092 int i;
13093 int regLeft = 0;
13094 int regRight = 0;
13095 u8 opx = op;
13096 int addrDone = sqlite3VdbeMakeLabel(v);
13097
13098 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
13099 sqlite3ErrorMsg(pParse, "row value misused");
13100 return;
13101 }
13102 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
13103 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
13104 || pExpr->op==TK_LT || pExpr->op==TK_GT
13105 || pExpr->op==TK_LE || pExpr->op==TK_GE
13106 );
13107 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
13108 || (pExpr->op==TK_ISNOT && op==TK_NE) );
13109 assert( p5==0 || pExpr->op!=op );
13110 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
13111
13112 p5 |= SQLITE_STOREP2;
13113 if( opx==TK_LE ) opx = TK_LT;
13114 if( opx==TK_GE ) opx = TK_GT;
13115
13116 regLeft = exprCodeSubselect(pParse, pLeft);
13117 regRight = exprCodeSubselect(pParse, pRight);
13118
13119 for(i=0; 1 /*Loop exits by "break"*/; i++){
13120 int regFree1 = 0, regFree2 = 0;
13121 Expr *pL, *pR;
13122 int r1, r2;
13123 assert( i>=0 && i<nLeft );
13124 if( i>0 ) sqlite3ExprCachePush(pParse);
13125 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
13126 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
13127 codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5);
13128 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
13129 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
13130 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
13131 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
13132 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
13133 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
13134 sqlite3ReleaseTempReg(pParse, regFree1);
13135 sqlite3ReleaseTempReg(pParse, regFree2);
13136 if( i>0 ) sqlite3ExprCachePop(pParse);
13137 if( i==nLeft-1 ){
13138 break;
13139 }
13140 if( opx==TK_EQ ){
13141 sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
13142 p5 |= SQLITE_KEEPNULL;
13143 }else if( opx==TK_NE ){
13144 sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v);
13145 p5 |= SQLITE_KEEPNULL;
13146 }else{
13147 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
13148 sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone);
13149 VdbeCoverageIf(v, op==TK_LT);
13150 VdbeCoverageIf(v, op==TK_GT);
13151 VdbeCoverageIf(v, op==TK_LE);
13152 VdbeCoverageIf(v, op==TK_GE);
13153 if( i==nLeft-2 ) opx = op;
13154 }
13155 }
13156 sqlite3VdbeResolveLabel(v, addrDone);
13157 }
13158
13159 #if SQLITE_MAX_EXPR_DEPTH>0
13160 /*
13161 ** Check that argument nHeight is less than or equal to the maximum
13162 ** expression depth allowed. If it is not, leave an error message in
13163 ** pParse.
13164 */
13165 SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
13166 int rc = SQLITE_OK;
13167 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
13168 if( nHeight>mxHeight ){
13169 sqlite3ErrorMsg(pParse,
13170 "Expression tree is too large (maximum depth %d)", mxHeight
13171 );
13172 rc = SQLITE_ERROR;
13173 }
13174 return rc;
13175 }
13176
13177 /* The following three functions, heightOfExpr(), heightOfExprList()
13178 ** and heightOfSelect(), are used to determine the maximum height
13179 ** of any expression tree referenced by the structure passed as the
13180 ** first argument.
13181 **
13182 ** If this maximum height is greater than the current value pointed
13183 ** to by pnHeight, the second parameter, then set *pnHeight to that
13184 ** value.
13185 */
13186 static void heightOfExpr(Expr *p, int *pnHeight){
13187 if( p ){
13188 if( p->nHeight>*pnHeight ){
13189 *pnHeight = p->nHeight;
13190 }
13191 }
13192 }
13193 static void heightOfExprList(ExprList *p, int *pnHeight){
13194 if( p ){
13195 int i;
13196 for(i=0; i<p->nExpr; i++){
13197 heightOfExpr(p->a[i].pExpr, pnHeight);
13198 }
13199 }
13200 }
13201 static void heightOfSelect(Select *p, int *pnHeight){
13202 if( p ){
13203 heightOfExpr(p->pWhere, pnHeight);
13204 heightOfExpr(p->pHaving, pnHeight);
13205 heightOfExpr(p->pLimit, pnHeight);
13206 heightOfExpr(p->pOffset, pnHeight);
13207 heightOfExprList(p->pEList, pnHeight);
13208 heightOfExprList(p->pGroupBy, pnHeight);
13209 heightOfExprList(p->pOrderBy, pnHeight);
13210 heightOfSelect(p->pPrior, pnHeight);
13211 }
13212 }
13213
13214 /*
13215 ** Set the Expr.nHeight variable in the structure passed as an
13216 ** argument. An expression with no children, Expr.pList or
13217 ** Expr.pSelect member has a height of 1. Any other expression
13218 ** has a height equal to the maximum height of any other
13219 ** referenced Expr plus one.
13220 **
13221 ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
13222 ** if appropriate.
13223 */
13224 static void exprSetHeight(Expr *p){
13225 int nHeight = 0;
13226 heightOfExpr(p->pLeft, &nHeight);
13227 heightOfExpr(p->pRight, &nHeight);
13228 if( ExprHasProperty(p, EP_xIsSelect) ){
13229 heightOfSelect(p->x.pSelect, &nHeight);
13230 }else if( p->x.pList ){
13231 heightOfExprList(p->x.pList, &nHeight);
13232 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
13233 }
13234 p->nHeight = nHeight + 1;
13235 }
13236
13237 /*
13238 ** Set the Expr.nHeight variable using the exprSetHeight() function. If
13239 ** the height is greater than the maximum allowed expression depth,
13240 ** leave an error in pParse.
13241 **
13242 ** Also propagate all EP_Propagate flags from the Expr.x.pList into
13243 ** Expr.flags.
13244 */
13245 SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
13246 if( pParse->nErr ) return;
13247 exprSetHeight(p);
13248 sqlite3ExprCheckHeight(pParse, p->nHeight);
13249 }
13250
13251 /*
13252 ** Return the maximum height of any expression tree referenced
13253 ** by the select statement passed as an argument.
13254 */
13255 SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
13256 int nHeight = 0;
13257 heightOfSelect(p, &nHeight);
13258 return nHeight;
13259 }
13260 #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
13261 /*
13262 ** Propagate all EP_Propagate flags from the Expr.x.pList into
13263 ** Expr.flags.
13264 */
13265 SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
13266 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
13267 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
13268 }
13269 }
13270 #define exprSetHeight(y)
13271 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
13272
13273 /*
13274 ** This routine is the core allocator for Expr nodes.
13275 **
13276 ** Construct a new expression node and return a pointer to it. Memory
13277 ** for this node and for the pToken argument is a single allocation
13278 ** obtained from sqlite3DbMalloc(). The calling function
13279 ** is responsible for making sure the node eventually gets freed.
13280 **
13281 ** If dequote is true, then the token (if it exists) is dequoted.
13282 ** If dequote is false, no dequoting is performed. The deQuote
13283 ** parameter is ignored if pToken is NULL or if the token does not
13284 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
13285 ** then the EP_DblQuoted flag is set on the expression node.
13286 **
13287 ** Special case: If op==TK_INTEGER and pToken points to a string that
13288 ** can be translated into a 32-bit integer, then the token is not
13289 ** stored in u.zToken. Instead, the integer values is written
13290 ** into u.iValue and the EP_IntValue flag is set. No extra storage
13291 ** is allocated to hold the integer text and the dequote flag is ignored.
13292 */
13293 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
13294 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
13295 int op, /* Expression opcode */
13296 const Token *pToken, /* Token argument. Might be NULL */
13297 int dequote /* True to dequote */
13298 ){
13299 Expr *pNew;
13300 int nExtra = 0;
13301 int iValue = 0;
13302
13303 assert( db!=0 );
13304 if( pToken ){
13305 if( op!=TK_INTEGER || pToken->z==0
13306 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
13307 nExtra = pToken->n+1;
13308 assert( iValue>=0 );
13309 }
13310 }
13311 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
13312 if( pNew ){
13313 memset(pNew, 0, sizeof(Expr));
13314 pNew->op = (u8)op;
13315 pNew->iAgg = -1;
13316 if( pToken ){
13317 if( nExtra==0 ){
13318 pNew->flags |= EP_IntValue;
13319 pNew->u.iValue = iValue;
13320 }else{
13321 pNew->u.zToken = (char*)&pNew[1];
13322 assert( pToken->z!=0 || pToken->n==0 );
13323 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
13324 pNew->u.zToken[pToken->n] = 0;
13325 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
13326 if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted;
13327 sqlite3Dequote(pNew->u.zToken);
13328 }
13329 }
13330 }
13331 #if SQLITE_MAX_EXPR_DEPTH>0
13332 pNew->nHeight = 1;
13333 #endif
13334 }
13335 return pNew;
13336 }
13337
13338 /*
13339 ** Allocate a new expression node from a zero-terminated token that has
13340 ** already been dequoted.
13341 */
13342 SQLITE_PRIVATE Expr *sqlite3Expr(
13343 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
13344 int op, /* Expression opcode */
13345 const char *zToken /* Token argument. Might be NULL */
13346 ){
13347 Token x;
13348 x.z = zToken;
13349 x.n = zToken ? sqlite3Strlen30(zToken) : 0;
13350 return sqlite3ExprAlloc(db, op, &x, 0);
13351 }
13352
13353 /*
13354 ** Attach subtrees pLeft and pRight to the Expr node pRoot.
13355 **
13356 ** If pRoot==NULL that means that a memory allocation error has occurred.
13357 ** In that case, delete the subtrees pLeft and pRight.
13358 */
13359 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
13360 sqlite3 *db,
13361 Expr *pRoot,
13362 Expr *pLeft,
13363 Expr *pRight
13364 ){
13365 if( pRoot==0 ){
13366 assert( db->mallocFailed );
13367 sqlite3ExprDelete(db, pLeft);
13368 sqlite3ExprDelete(db, pRight);
13369 }else{
13370 if( pRight ){
13371 pRoot->pRight = pRight;
13372 pRoot->flags |= EP_Propagate & pRight->flags;
13373 }
13374 if( pLeft ){
13375 pRoot->pLeft = pLeft;
13376 pRoot->flags |= EP_Propagate & pLeft->flags;
13377 }
13378 exprSetHeight(pRoot);
13379 }
13380 }
13381
13382 /*
13383 ** Allocate an Expr node which joins as many as two subtrees.
13384 **
13385 ** One or both of the subtrees can be NULL. Return a pointer to the new
13386 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
13387 ** free the subtrees and return NULL.
13388 */
13389 SQLITE_PRIVATE Expr *sqlite3PExpr(
13390 Parse *pParse, /* Parsing context */
13391 int op, /* Expression opcode */
13392 Expr *pLeft, /* Left operand */
13393 Expr *pRight /* Right operand */
13394 ){
13395 Expr *p;
13396 if( op==TK_AND && pParse->nErr==0 ){
13397 /* Take advantage of short-circuit false optimization for AND */
13398 p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
13399 }else{
13400 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
13401 if( p ){
13402 memset(p, 0, sizeof(Expr));
13403 p->op = op & TKFLG_MASK;
13404 p->iAgg = -1;
13405 }
13406 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
13407 }
13408 if( p ) {
13409 sqlite3ExprCheckHeight(pParse, p->nHeight);
13410 }
13411 return p;
13412 }
13413
13414 /*
13415 ** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
13416 ** do a memory allocation failure) then delete the pSelect object.
13417 */
13418 SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pS elect){
13419 if( pExpr ){
13420 pExpr->x.pSelect = pSelect;
13421 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
13422 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
13423 }else{
13424 assert( pParse->db->mallocFailed );
13425 sqlite3SelectDelete(pParse->db, pSelect);
13426 }
13427 }
13428
13429
13430 /*
13431 ** If the expression is always either TRUE or FALSE (respectively),
13432 ** then return 1. If one cannot determine the truth value of the
13433 ** expression at compile-time return 0.
13434 **
13435 ** This is an optimization. If is OK to return 0 here even if
13436 ** the expression really is always false or false (a false negative).
13437 ** But it is a bug to return 1 if the expression might have different
13438 ** boolean values in different circumstances (a false positive.)
13439 **
13440 ** Note that if the expression is part of conditional for a
13441 ** LEFT JOIN, then we cannot determine at compile-time whether or not
13442 ** is it true or false, so always return 0.
13443 */
13444 static int exprAlwaysTrue(Expr *p){
13445 int v = 0;
13446 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
13447 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
13448 return v!=0;
13449 }
13450 static int exprAlwaysFalse(Expr *p){
13451 int v = 0;
13452 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
13453 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
13454 return v==0;
13455 }
13456
13457 /*
13458 ** Join two expressions using an AND operator. If either expression is
13459 ** NULL, then just return the other expression.
13460 **
13461 ** If one side or the other of the AND is known to be false, then instead
13462 ** of returning an AND expression, just return a constant expression with
13463 ** a value of false.
13464 */
13465 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
13466 if( pLeft==0 ){
13467 return pRight;
13468 }else if( pRight==0 ){
13469 return pLeft;
13470 }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
13471 sqlite3ExprDelete(db, pLeft);
13472 sqlite3ExprDelete(db, pRight);
13473 return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
13474 }else{
13475 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
13476 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
13477 return pNew;
13478 }
13479 }
13480
13481 /*
13482 ** Construct a new expression node for a function with multiple
13483 ** arguments.
13484 */
13485 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token * pToken){
13486 Expr *pNew;
13487 sqlite3 *db = pParse->db;
13488 assert( pToken );
13489 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
13490 if( pNew==0 ){
13491 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
13492 return 0;
13493 }
13494 pNew->x.pList = pList;
13495 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
13496 sqlite3ExprSetHeightAndFlags(pParse, pNew);
13497 return pNew;
13498 }
13499
13500 /*
13501 ** Assign a variable number to an expression that encodes a wildcard
13502 ** in the original SQL statement.
13503 **
13504 ** Wildcards consisting of a single "?" are assigned the next sequential
13505 ** variable number.
13506 **
13507 ** Wildcards of the form "?nnn" are assigned the number "nnn". We make
13508 ** sure "nnn" is not too big to avoid a denial of service attack when
13509 ** the SQL statement comes from an external source.
13510 **
13511 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
13512 ** as the previous instance of the same wildcard. Or if this is the first
13513 ** instance of the wildcard, the next sequential variable number is
13514 ** assigned.
13515 */
13516 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n ){
13517 sqlite3 *db = pParse->db;
13518 const char *z;
13519 ynVar x;
13520
13521 if( pExpr==0 ) return;
13522 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
13523 z = pExpr->u.zToken;
13524 assert( z!=0 );
13525 assert( z[0]!=0 );
13526 assert( n==sqlite3Strlen30(z) );
13527 if( z[1]==0 ){
13528 /* Wildcard of the form "?". Assign the next variable number */
13529 assert( z[0]=='?' );
13530 x = (ynVar)(++pParse->nVar);
13531 }else{
13532 int doAdd = 0;
13533 if( z[0]=='?' ){
13534 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
13535 ** use it as the variable number */
13536 i64 i;
13537 int bOk;
13538 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
13539 i = z[1]-'0'; /* The common case of ?N for a single digit N */
13540 bOk = 1;
13541 }else{
13542 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
13543 }
13544 testcase( i==0 );
13545 testcase( i==1 );
13546 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
13547 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
13548 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
13549 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
13550 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
13551 return;
13552 }
13553 x = (ynVar)i;
13554 if( x>pParse->nVar ){
13555 pParse->nVar = (int)x;
13556 doAdd = 1;
13557 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
13558 doAdd = 1;
13559 }
13560 }else{
13561 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
13562 ** number as the prior appearance of the same name, or if the name
13563 ** has never appeared before, reuse the same variable number
13564 */
13565 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
13566 if( x==0 ){
13567 x = (ynVar)(++pParse->nVar);
13568 doAdd = 1;
13569 }
13570 }
13571 if( doAdd ){
13572 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
13573 }
13574 }
13575 pExpr->iColumn = x;
13576 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
13577 sqlite3ErrorMsg(pParse, "too many SQL variables");
13578 }
13579 }
13580
13581 /*
13582 ** Recursively delete an expression tree.
13583 */
13584 static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
13585 assert( p!=0 );
13586 /* Sanity check: Assert that the IntValue is non-negative if it exists */
13587 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
13588 #ifdef SQLITE_DEBUG
13589 if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
13590 assert( p->pLeft==0 );
13591 assert( p->pRight==0 );
13592 assert( p->x.pSelect==0 );
13593 }
13594 #endif
13595 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
13596 /* The Expr.x union is never used at the same time as Expr.pRight */
13597 assert( p->x.pList==0 || p->pRight==0 );
13598 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
13599 sqlite3ExprDelete(db, p->pRight);
13600 if( ExprHasProperty(p, EP_xIsSelect) ){
13601 sqlite3SelectDelete(db, p->x.pSelect);
13602 }else{
13603 sqlite3ExprListDelete(db, p->x.pList);
13604 }
13605 }
13606 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
13607 if( !ExprHasProperty(p, EP_Static) ){
13608 sqlite3DbFree(db, p);
13609 }
13610 }
13611 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
13612 if( p ) sqlite3ExprDeleteNN(db, p);
13613 }
13614
13615 /*
13616 ** Return the number of bytes allocated for the expression structure
13617 ** passed as the first argument. This is always one of EXPR_FULLSIZE,
13618 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
13619 */
13620 static int exprStructSize(Expr *p){
13621 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
13622 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
13623 return EXPR_FULLSIZE;
13624 }
13625
13626 /*
13627 ** The dupedExpr*Size() routines each return the number of bytes required
13628 ** to store a copy of an expression or expression tree. They differ in
13629 ** how much of the tree is measured.
13630 **
13631 ** dupedExprStructSize() Size of only the Expr structure
13632 ** dupedExprNodeSize() Size of Expr + space for token
13633 ** dupedExprSize() Expr + token + subtree components
13634 **
13635 ***************************************************************************
13636 **
13637 ** The dupedExprStructSize() function returns two values OR-ed together:
13638 ** (1) the space required for a copy of the Expr structure only and
13639 ** (2) the EP_xxx flags that indicate what the structure size should be.
13640 ** The return values is always one of:
13641 **
13642 ** EXPR_FULLSIZE
13643 ** EXPR_REDUCEDSIZE | EP_Reduced
13644 ** EXPR_TOKENONLYSIZE | EP_TokenOnly
13645 **
13646 ** The size of the structure can be found by masking the return value
13647 ** of this routine with 0xfff. The flags can be found by masking the
13648 ** return value with EP_Reduced|EP_TokenOnly.
13649 **
13650 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
13651 ** (unreduced) Expr objects as they or originally constructed by the parser.
13652 ** During expression analysis, extra information is computed and moved into
13653 ** later parts of teh Expr object and that extra information might get chopped
13654 ** off if the expression is reduced. Note also that it does not work to
13655 ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
13656 ** to reduce a pristine expression tree from the parser. The implementation
13657 ** of dupedExprStructSize() contain multiple assert() statements that attempt
13658 ** to enforce this constraint.
13659 */
13660 static int dupedExprStructSize(Expr *p, int flags){
13661 int nSize;
13662 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
13663 assert( EXPR_FULLSIZE<=0xfff );
13664 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
13665 if( 0==flags || p->op==TK_SELECT_COLUMN ){
13666 nSize = EXPR_FULLSIZE;
13667 }else{
13668 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
13669 assert( !ExprHasProperty(p, EP_FromJoin) );
13670 assert( !ExprHasProperty(p, EP_MemToken) );
13671 assert( !ExprHasProperty(p, EP_NoReduce) );
13672 if( p->pLeft || p->x.pList ){
13673 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
13674 }else{
13675 assert( p->pRight==0 );
13676 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
13677 }
13678 }
13679 return nSize;
13680 }
13681
13682 /*
13683 ** This function returns the space in bytes required to store the copy
13684 ** of the Expr structure and a copy of the Expr.u.zToken string (if that
13685 ** string is defined.)
13686 */
13687 static int dupedExprNodeSize(Expr *p, int flags){
13688 int nByte = dupedExprStructSize(p, flags) & 0xfff;
13689 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
13690 nByte += sqlite3Strlen30(p->u.zToken)+1;
13691 }
13692 return ROUND8(nByte);
13693 }
13694
13695 /*
13696 ** Return the number of bytes required to create a duplicate of the
13697 ** expression passed as the first argument. The second argument is a
13698 ** mask containing EXPRDUP_XXX flags.
13699 **
13700 ** The value returned includes space to create a copy of the Expr struct
13701 ** itself and the buffer referred to by Expr.u.zToken, if any.
13702 **
13703 ** If the EXPRDUP_REDUCE flag is set, then the return value includes
13704 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
13705 ** and Expr.pRight variables (but not for any structures pointed to or
13706 ** descended from the Expr.x.pList or Expr.x.pSelect variables).
13707 */
13708 static int dupedExprSize(Expr *p, int flags){
13709 int nByte = 0;
13710 if( p ){
13711 nByte = dupedExprNodeSize(p, flags);
13712 if( flags&EXPRDUP_REDUCE ){
13713 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
13714 }
13715 }
13716 return nByte;
13717 }
13718
13719 /*
13720 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer
13721 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough
13722 ** to store the copy of expression p, the copies of p->u.zToken
13723 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
13724 ** if any. Before returning, *pzBuffer is set to the first byte past the
13725 ** portion of the buffer copied into by this function.
13726 */
13727 static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
13728 Expr *pNew; /* Value to return */
13729 u8 *zAlloc; /* Memory space from which to build Expr object */
13730 u32 staticFlag; /* EP_Static if space not obtained from malloc */
13731
13732 assert( db!=0 );
13733 assert( p );
13734 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
13735 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
13736
13737 /* Figure out where to write the new Expr structure. */
13738 if( pzBuffer ){
13739 zAlloc = *pzBuffer;
13740 staticFlag = EP_Static;
13741 }else{
13742 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
13743 staticFlag = 0;
13744 }
13745 pNew = (Expr *)zAlloc;
13746
13747 if( pNew ){
13748 /* Set nNewSize to the size allocated for the structure pointed to
13749 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
13750 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
13751 ** by the copy of the p->u.zToken string (if any).
13752 */
13753 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
13754 const int nNewSize = nStructSize & 0xfff;
13755 int nToken;
13756 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
13757 nToken = sqlite3Strlen30(p->u.zToken) + 1;
13758 }else{
13759 nToken = 0;
13760 }
13761 if( dupFlags ){
13762 assert( ExprHasProperty(p, EP_Reduced)==0 );
13763 memcpy(zAlloc, p, nNewSize);
13764 }else{
13765 u32 nSize = (u32)exprStructSize(p);
13766 memcpy(zAlloc, p, nSize);
13767 if( nSize<EXPR_FULLSIZE ){
13768 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
13769 }
13770 }
13771
13772 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
13773 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
13774 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
13775 pNew->flags |= staticFlag;
13776
13777 /* Copy the p->u.zToken string, if any. */
13778 if( nToken ){
13779 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
13780 memcpy(zToken, p->u.zToken, nToken);
13781 }
13782
13783 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
13784 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
13785 if( ExprHasProperty(p, EP_xIsSelect) ){
13786 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
13787 }else{
13788 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
13789 }
13790 }
13791
13792 /* Fill in pNew->pLeft and pNew->pRight. */
13793 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
13794 zAlloc += dupedExprNodeSize(p, dupFlags);
13795 if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
13796 pNew->pLeft = p->pLeft ?
13797 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
13798 pNew->pRight = p->pRight ?
13799 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
13800 }
13801 if( pzBuffer ){
13802 *pzBuffer = zAlloc;
13803 }
13804 }else{
13805 if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
13806 if( pNew->op==TK_SELECT_COLUMN ){
13807 pNew->pLeft = p->pLeft;
13808 assert( p->iColumn==0 || p->pRight==0 );
13809 assert( p->pRight==0 || p->pRight==p->pLeft );
13810 }else{
13811 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
13812 }
13813 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
13814 }
13815 }
13816 }
13817 return pNew;
13818 }
13819
13820 /*
13821 ** Create and return a deep copy of the object passed as the second
13822 ** argument. If an OOM condition is encountered, NULL is returned
13823 ** and the db->mallocFailed flag set.
13824 */
13825 #ifndef SQLITE_OMIT_CTE
13826 static With *withDup(sqlite3 *db, With *p){
13827 With *pRet = 0;
13828 if( p ){
13829 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
13830 pRet = sqlite3DbMallocZero(db, nByte);
13831 if( pRet ){
13832 int i;
13833 pRet->nCte = p->nCte;
13834 for(i=0; i<p->nCte; i++){
13835 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
13836 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
13837 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
13838 }
13839 }
13840 }
13841 return pRet;
13842 }
13843 #else
13844 # define withDup(x,y) 0
13845 #endif
13846
13847 /*
13848 ** The following group of routines make deep copies of expressions,
13849 ** expression lists, ID lists, and select statements. The copies can
13850 ** be deleted (by being passed to their respective ...Delete() routines)
13851 ** without effecting the originals.
13852 **
13853 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
13854 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
13855 ** by subsequent calls to sqlite*ListAppend() routines.
13856 **
13857 ** Any tables that the SrcList might point to are not duplicated.
13858 **
13859 ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
13860 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
13861 ** truncated version of the usual Expr structure that will be stored as
13862 ** part of the in-memory representation of the database schema.
13863 */
13864 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
13865 assert( flags==0 || flags==EXPRDUP_REDUCE );
13866 return p ? exprDup(db, p, flags, 0) : 0;
13867 }
13868 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags) {
13869 ExprList *pNew;
13870 struct ExprList_item *pItem, *pOldItem;
13871 int i;
13872 Expr *pPriorSelectCol = 0;
13873 assert( db!=0 );
13874 if( p==0 ) return 0;
13875 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
13876 if( pNew==0 ) return 0;
13877 pNew->nExpr = i = p->nExpr;
13878 if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
13879 pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) );
13880 if( pItem==0 ){
13881 sqlite3DbFree(db, pNew);
13882 return 0;
13883 }
13884 pOldItem = p->a;
13885 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
13886 Expr *pOldExpr = pOldItem->pExpr;
13887 Expr *pNewExpr;
13888 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
13889 if( pOldExpr
13890 && pOldExpr->op==TK_SELECT_COLUMN
13891 && (pNewExpr = pItem->pExpr)!=0
13892 ){
13893 assert( pNewExpr->iColumn==0 || i>0 );
13894 if( pNewExpr->iColumn==0 ){
13895 assert( pOldExpr->pLeft==pOldExpr->pRight );
13896 pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
13897 }else{
13898 assert( i>0 );
13899 assert( pItem[-1].pExpr!=0 );
13900 assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
13901 assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
13902 pNewExpr->pLeft = pPriorSelectCol;
13903 }
13904 }
13905 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
13906 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
13907 pItem->sortOrder = pOldItem->sortOrder;
13908 pItem->done = 0;
13909 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
13910 pItem->u = pOldItem->u;
13911 }
13912 return pNew;
13913 }
13914
13915 /*
13916 ** If cursors, triggers, views and subqueries are all omitted from
13917 ** the build, then none of the following routines, except for
13918 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
13919 ** called with a NULL argument.
13920 */
13921 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
13922 || !defined(SQLITE_OMIT_SUBQUERY)
13923 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
13924 SrcList *pNew;
13925 int i;
13926 int nByte;
13927 assert( db!=0 );
13928 if( p==0 ) return 0;
13929 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
13930 pNew = sqlite3DbMallocRawNN(db, nByte );
13931 if( pNew==0 ) return 0;
13932 pNew->nSrc = pNew->nAlloc = p->nSrc;
13933 for(i=0; i<p->nSrc; i++){
13934 struct SrcList_item *pNewItem = &pNew->a[i];
13935 struct SrcList_item *pOldItem = &p->a[i];
13936 Table *pTab;
13937 pNewItem->pSchema = pOldItem->pSchema;
13938 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
13939 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
13940 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
13941 pNewItem->fg = pOldItem->fg;
13942 pNewItem->iCursor = pOldItem->iCursor;
13943 pNewItem->addrFillSub = pOldItem->addrFillSub;
13944 pNewItem->regReturn = pOldItem->regReturn;
13945 if( pNewItem->fg.isIndexedBy ){
13946 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
13947 }
13948 pNewItem->pIBIndex = pOldItem->pIBIndex;
13949 if( pNewItem->fg.isTabFunc ){
13950 pNewItem->u1.pFuncArg =
13951 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
13952 }
13953 pTab = pNewItem->pTab = pOldItem->pTab;
13954 if( pTab ){
13955 pTab->nTabRef++;
13956 }
13957 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
13958 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
13959 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
13960 pNewItem->colUsed = pOldItem->colUsed;
13961 }
13962 return pNew;
13963 }
13964 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
13965 IdList *pNew;
13966 int i;
13967 assert( db!=0 );
13968 if( p==0 ) return 0;
13969 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
13970 if( pNew==0 ) return 0;
13971 pNew->nId = p->nId;
13972 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
13973 if( pNew->a==0 ){
13974 sqlite3DbFree(db, pNew);
13975 return 0;
13976 }
13977 /* Note that because the size of the allocation for p->a[] is not
13978 ** necessarily a power of two, sqlite3IdListAppend() may not be called
13979 ** on the duplicate created by this function. */
13980 for(i=0; i<p->nId; i++){
13981 struct IdList_item *pNewItem = &pNew->a[i];
13982 struct IdList_item *pOldItem = &p->a[i];
13983 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
13984 pNewItem->idx = pOldItem->idx;
13985 }
13986 return pNew;
13987 }
13988 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
13989 Select *pRet = 0;
13990 Select *pNext = 0;
13991 Select **pp = &pRet;
13992 Select *p;
13993
13994 assert( db!=0 );
13995 for(p=pDup; p; p=p->pPrior){
13996 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
13997 if( pNew==0 ) break;
13998 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
13999 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
14000 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
14001 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
14002 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
14003 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
14004 pNew->op = p->op;
14005 pNew->pNext = pNext;
14006 pNew->pPrior = 0;
14007 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
14008 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
14009 pNew->iLimit = 0;
14010 pNew->iOffset = 0;
14011 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
14012 pNew->addrOpenEphm[0] = -1;
14013 pNew->addrOpenEphm[1] = -1;
14014 pNew->nSelectRow = p->nSelectRow;
14015 pNew->pWith = withDup(db, p->pWith);
14016 sqlite3SelectSetName(pNew, p->zSelName);
14017 *pp = pNew;
14018 pp = &pNew->pPrior;
14019 pNext = pNew;
14020 }
14021
14022 return pRet;
14023 }
14024 #else
14025 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
14026 assert( p==0 );
14027 return 0;
14028 }
14029 #endif
14030
14031
14032 /*
14033 ** Add a new element to the end of an expression list. If pList is
14034 ** initially NULL, then create a new expression list.
14035 **
14036 ** If a memory allocation error occurs, the entire list is freed and
14037 ** NULL is returned. If non-NULL is returned, then it is guaranteed
14038 ** that the new entry was successfully appended.
14039 */
14040 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
14041 Parse *pParse, /* Parsing context */
14042 ExprList *pList, /* List to which to append. Might be NULL */
14043 Expr *pExpr /* Expression to be appended. Might be NULL */
14044 ){
14045 sqlite3 *db = pParse->db;
14046 assert( db!=0 );
14047 if( pList==0 ){
14048 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
14049 if( pList==0 ){
14050 goto no_mem;
14051 }
14052 pList->nExpr = 0;
14053 pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0]));
14054 if( pList->a==0 ) goto no_mem;
14055 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
14056 struct ExprList_item *a;
14057 assert( pList->nExpr>0 );
14058 a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
14059 if( a==0 ){
14060 goto no_mem;
14061 }
14062 pList->a = a;
14063 }
14064 assert( pList->a!=0 );
14065 if( 1 ){
14066 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
14067 memset(pItem, 0, sizeof(*pItem));
14068 pItem->pExpr = pExpr;
14069 }
14070 return pList;
14071
14072 no_mem:
14073 /* Avoid leaking memory if malloc has failed. */
14074 sqlite3ExprDelete(db, pExpr);
14075 sqlite3ExprListDelete(db, pList);
14076 return 0;
14077 }
14078
14079 /*
14080 ** pColumns and pExpr form a vector assignment which is part of the SET
14081 ** clause of an UPDATE statement. Like this:
14082 **
14083 ** (a,b,c) = (expr1,expr2,expr3)
14084 ** Or: (a,b,c) = (SELECT x,y,z FROM ....)
14085 **
14086 ** For each term of the vector assignment, append new entries to the
14087 ** expression list pList. In the case of a subquery on the RHS, append
14088 ** TK_SELECT_COLUMN expressions.
14089 */
14090 SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
14091 Parse *pParse, /* Parsing context */
14092 ExprList *pList, /* List to which to append. Might be NULL */
14093 IdList *pColumns, /* List of names of LHS of the assignment */
14094 Expr *pExpr /* Vector expression to be appended. Might be NULL */
14095 ){
14096 sqlite3 *db = pParse->db;
14097 int n;
14098 int i;
14099 int iFirst = pList ? pList->nExpr : 0;
14100 /* pColumns can only be NULL due to an OOM but an OOM will cause an
14101 ** exit prior to this routine being invoked */
14102 if( NEVER(pColumns==0) ) goto vector_append_error;
14103 if( pExpr==0 ) goto vector_append_error;
14104
14105 /* If the RHS is a vector, then we can immediately check to see that
14106 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
14107 ** wildcards ("*") in the result set of the SELECT must be expanded before
14108 ** we can do the size check, so defer the size check until code generation.
14109 */
14110 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
14111 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
14112 pColumns->nId, n);
14113 goto vector_append_error;
14114 }
14115
14116 for(i=0; i<pColumns->nId; i++){
14117 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
14118 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
14119 if( pList ){
14120 assert( pList->nExpr==iFirst+i+1 );
14121 pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
14122 pColumns->a[i].zName = 0;
14123 }
14124 }
14125
14126 if( pExpr->op==TK_SELECT ){
14127 if( pList && pList->a[iFirst].pExpr ){
14128 Expr *pFirst = pList->a[iFirst].pExpr;
14129 assert( pFirst->op==TK_SELECT_COLUMN );
14130
14131 /* Store the SELECT statement in pRight so it will be deleted when
14132 ** sqlite3ExprListDelete() is called */
14133 pFirst->pRight = pExpr;
14134 pExpr = 0;
14135
14136 /* Remember the size of the LHS in iTable so that we can check that
14137 ** the RHS and LHS sizes match during code generation. */
14138 pFirst->iTable = pColumns->nId;
14139 }
14140 }
14141
14142 vector_append_error:
14143 sqlite3ExprDelete(db, pExpr);
14144 sqlite3IdListDelete(db, pColumns);
14145 return pList;
14146 }
14147
14148 /*
14149 ** Set the sort order for the last element on the given ExprList.
14150 */
14151 SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
14152 if( p==0 ) return;
14153 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
14154 assert( p->nExpr>0 );
14155 if( iSortOrder<0 ){
14156 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
14157 return;
14158 }
14159 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
14160 }
14161
14162 /*
14163 ** Set the ExprList.a[].zName element of the most recently added item
14164 ** on the expression list.
14165 **
14166 ** pList might be NULL following an OOM error. But pName should never be
14167 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
14168 ** is set.
14169 */
14170 SQLITE_PRIVATE void sqlite3ExprListSetName(
14171 Parse *pParse, /* Parsing context */
14172 ExprList *pList, /* List to which to add the span. */
14173 Token *pName, /* Name to be added */
14174 int dequote /* True to cause the name to be dequoted */
14175 ){
14176 assert( pList!=0 || pParse->db->mallocFailed!=0 );
14177 if( pList ){
14178 struct ExprList_item *pItem;
14179 assert( pList->nExpr>0 );
14180 pItem = &pList->a[pList->nExpr-1];
14181 assert( pItem->zName==0 );
14182 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
14183 if( dequote ) sqlite3Dequote(pItem->zName);
14184 }
14185 }
14186
14187 /*
14188 ** Set the ExprList.a[].zSpan element of the most recently added item
14189 ** on the expression list.
14190 **
14191 ** pList might be NULL following an OOM error. But pSpan should never be
14192 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
14193 ** is set.
14194 */
14195 SQLITE_PRIVATE void sqlite3ExprListSetSpan(
14196 Parse *pParse, /* Parsing context */
14197 ExprList *pList, /* List to which to add the span. */
14198 ExprSpan *pSpan /* The span to be added */
14199 ){
14200 sqlite3 *db = pParse->db;
14201 assert( pList!=0 || db->mallocFailed!=0 );
14202 if( pList ){
14203 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
14204 assert( pList->nExpr>0 );
14205 assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
14206 sqlite3DbFree(db, pItem->zSpan);
14207 pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
14208 (int)(pSpan->zEnd - pSpan->zStart));
14209 }
14210 }
14211
14212 /*
14213 ** If the expression list pEList contains more than iLimit elements,
14214 ** leave an error message in pParse.
14215 */
14216 SQLITE_PRIVATE void sqlite3ExprListCheckLength(
14217 Parse *pParse,
14218 ExprList *pEList,
14219 const char *zObject
14220 ){
14221 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
14222 testcase( pEList && pEList->nExpr==mx );
14223 testcase( pEList && pEList->nExpr==mx+1 );
14224 if( pEList && pEList->nExpr>mx ){
14225 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
14226 }
14227 }
14228
14229 /*
14230 ** Delete an entire expression list.
14231 */
14232 static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
14233 int i;
14234 struct ExprList_item *pItem;
14235 assert( pList->a!=0 || pList->nExpr==0 );
14236 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
14237 sqlite3ExprDelete(db, pItem->pExpr);
14238 sqlite3DbFree(db, pItem->zName);
14239 sqlite3DbFree(db, pItem->zSpan);
14240 }
14241 sqlite3DbFree(db, pList->a);
14242 sqlite3DbFree(db, pList);
14243 }
14244 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
14245 if( pList ) exprListDeleteNN(db, pList);
14246 }
14247
14248 /*
14249 ** Return the bitwise-OR of all Expr.flags fields in the given
14250 ** ExprList.
14251 */
14252 SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
14253 int i;
14254 u32 m = 0;
14255 if( pList ){
14256 for(i=0; i<pList->nExpr; i++){
14257 Expr *pExpr = pList->a[i].pExpr;
14258 assert( pExpr!=0 );
14259 m |= pExpr->flags;
14260 }
14261 }
14262 return m;
14263 }
14264
14265 /*
14266 ** These routines are Walker callbacks used to check expressions to
14267 ** see if they are "constant" for some definition of constant. The
14268 ** Walker.eCode value determines the type of "constant" we are looking
14269 ** for.
14270 **
14271 ** These callback routines are used to implement the following:
14272 **
14273 ** sqlite3ExprIsConstant() pWalker->eCode==1
14274 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
14275 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
14276 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
14277 **
14278 ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
14279 ** is found to not be a constant.
14280 **
14281 ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
14282 ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
14283 ** an existing schema and 4 when processing a new statement. A bound
14284 ** parameter raises an error for new statements, but is silently converted
14285 ** to NULL for existing schemas. This allows sqlite_master tables that
14286 ** contain a bound parameter because they were generated by older versions
14287 ** of SQLite to be parsed by newer versions of SQLite without raising a
14288 ** malformed schema error.
14289 */
14290 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
14291
14292 /* If pWalker->eCode is 2 then any term of the expression that comes from
14293 ** the ON or USING clauses of a left join disqualifies the expression
14294 ** from being considered constant. */
14295 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
14296 pWalker->eCode = 0;
14297 return WRC_Abort;
14298 }
14299
14300 switch( pExpr->op ){
14301 /* Consider functions to be constant if all their arguments are constant
14302 ** and either pWalker->eCode==4 or 5 or the function has the
14303 ** SQLITE_FUNC_CONST flag. */
14304 case TK_FUNCTION:
14305 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
14306 return WRC_Continue;
14307 }else{
14308 pWalker->eCode = 0;
14309 return WRC_Abort;
14310 }
14311 case TK_ID:
14312 case TK_COLUMN:
14313 case TK_AGG_FUNCTION:
14314 case TK_AGG_COLUMN:
14315 testcase( pExpr->op==TK_ID );
14316 testcase( pExpr->op==TK_COLUMN );
14317 testcase( pExpr->op==TK_AGG_FUNCTION );
14318 testcase( pExpr->op==TK_AGG_COLUMN );
14319 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
14320 return WRC_Continue;
14321 }else{
14322 pWalker->eCode = 0;
14323 return WRC_Abort;
14324 }
14325 case TK_VARIABLE:
14326 if( pWalker->eCode==5 ){
14327 /* Silently convert bound parameters that appear inside of CREATE
14328 ** statements into a NULL when parsing the CREATE statement text out
14329 ** of the sqlite_master table */
14330 pExpr->op = TK_NULL;
14331 }else if( pWalker->eCode==4 ){
14332 /* A bound parameter in a CREATE statement that originates from
14333 ** sqlite3_prepare() causes an error */
14334 pWalker->eCode = 0;
14335 return WRC_Abort;
14336 }
14337 /* Fall through */
14338 default:
14339 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
14340 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
14341 return WRC_Continue;
14342 }
14343 }
14344 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
14345 UNUSED_PARAMETER(NotUsed);
14346 pWalker->eCode = 0;
14347 return WRC_Abort;
14348 }
14349 static int exprIsConst(Expr *p, int initFlag, int iCur){
14350 Walker w;
14351 memset(&w, 0, sizeof(w));
14352 w.eCode = initFlag;
14353 w.xExprCallback = exprNodeIsConstant;
14354 w.xSelectCallback = selectNodeIsConstant;
14355 w.u.iCur = iCur;
14356 sqlite3WalkExpr(&w, p);
14357 return w.eCode;
14358 }
14359
14360 /*
14361 ** Walk an expression tree. Return non-zero if the expression is constant
14362 ** and 0 if it involves variables or function calls.
14363 **
14364 ** For the purposes of this function, a double-quoted string (ex: "abc")
14365 ** is considered a variable but a single-quoted string (ex: 'abc') is
14366 ** a constant.
14367 */
14368 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
14369 return exprIsConst(p, 1, 0);
14370 }
14371
14372 /*
14373 ** Walk an expression tree. Return non-zero if the expression is constant
14374 ** that does no originate from the ON or USING clauses of a join.
14375 ** Return 0 if it involves variables or function calls or terms from
14376 ** an ON or USING clause.
14377 */
14378 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
14379 return exprIsConst(p, 2, 0);
14380 }
14381
14382 /*
14383 ** Walk an expression tree. Return non-zero if the expression is constant
14384 ** for any single row of the table with cursor iCur. In other words, the
14385 ** expression must not refer to any non-deterministic function nor any
14386 ** table other than iCur.
14387 */
14388 SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
14389 return exprIsConst(p, 3, iCur);
14390 }
14391
14392 /*
14393 ** Walk an expression tree. Return non-zero if the expression is constant
14394 ** or a function call with constant arguments. Return and 0 if there
14395 ** are any variables.
14396 **
14397 ** For the purposes of this function, a double-quoted string (ex: "abc")
14398 ** is considered a variable but a single-quoted string (ex: 'abc') is
14399 ** a constant.
14400 */
14401 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
14402 assert( isInit==0 || isInit==1 );
14403 return exprIsConst(p, 4+isInit, 0);
14404 }
14405
14406 #ifdef SQLITE_ENABLE_CURSOR_HINTS
14407 /*
14408 ** Walk an expression tree. Return 1 if the expression contains a
14409 ** subquery of some kind. Return 0 if there are no subqueries.
14410 */
14411 SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
14412 Walker w;
14413 memset(&w, 0, sizeof(w));
14414 w.eCode = 1;
14415 w.xExprCallback = sqlite3ExprWalkNoop;
14416 w.xSelectCallback = selectNodeIsConstant;
14417 sqlite3WalkExpr(&w, p);
14418 return w.eCode==0;
14419 }
14420 #endif
14421
14422 /*
14423 ** If the expression p codes a constant integer that is small enough
14424 ** to fit in a 32-bit integer, return 1 and put the value of the integer
14425 ** in *pValue. If the expression is not an integer or if it is too big
14426 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
14427 */
14428 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
14429 int rc = 0;
14430
14431 /* If an expression is an integer literal that fits in a signed 32-bit
14432 ** integer, then the EP_IntValue flag will have already been set */
14433 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
14434 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
14435
14436 if( p->flags & EP_IntValue ){
14437 *pValue = p->u.iValue;
14438 return 1;
14439 }
14440 switch( p->op ){
14441 case TK_UPLUS: {
14442 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
14443 break;
14444 }
14445 case TK_UMINUS: {
14446 int v;
14447 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
14448 assert( v!=(-2147483647-1) );
14449 *pValue = -v;
14450 rc = 1;
14451 }
14452 break;
14453 }
14454 default: break;
14455 }
14456 return rc;
14457 }
14458
14459 /*
14460 ** Return FALSE if there is no chance that the expression can be NULL.
14461 **
14462 ** If the expression might be NULL or if the expression is too complex
14463 ** to tell return TRUE.
14464 **
14465 ** This routine is used as an optimization, to skip OP_IsNull opcodes
14466 ** when we know that a value cannot be NULL. Hence, a false positive
14467 ** (returning TRUE when in fact the expression can never be NULL) might
14468 ** be a small performance hit but is otherwise harmless. On the other
14469 ** hand, a false negative (returning FALSE when the result could be NULL)
14470 ** will likely result in an incorrect answer. So when in doubt, return
14471 ** TRUE.
14472 */
14473 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
14474 u8 op;
14475 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
14476 op = p->op;
14477 if( op==TK_REGISTER ) op = p->op2;
14478 switch( op ){
14479 case TK_INTEGER:
14480 case TK_STRING:
14481 case TK_FLOAT:
14482 case TK_BLOB:
14483 return 0;
14484 case TK_COLUMN:
14485 assert( p->pTab!=0 );
14486 return ExprHasProperty(p, EP_CanBeNull) ||
14487 (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
14488 default:
14489 return 1;
14490 }
14491 }
14492
14493 /*
14494 ** Return TRUE if the given expression is a constant which would be
14495 ** unchanged by OP_Affinity with the affinity given in the second
14496 ** argument.
14497 **
14498 ** This routine is used to determine if the OP_Affinity operation
14499 ** can be omitted. When in doubt return FALSE. A false negative
14500 ** is harmless. A false positive, however, can result in the wrong
14501 ** answer.
14502 */
14503 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
14504 u8 op;
14505 if( aff==SQLITE_AFF_BLOB ) return 1;
14506 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
14507 op = p->op;
14508 if( op==TK_REGISTER ) op = p->op2;
14509 switch( op ){
14510 case TK_INTEGER: {
14511 return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
14512 }
14513 case TK_FLOAT: {
14514 return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
14515 }
14516 case TK_STRING: {
14517 return aff==SQLITE_AFF_TEXT;
14518 }
14519 case TK_BLOB: {
14520 return 1;
14521 }
14522 case TK_COLUMN: {
14523 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
14524 return p->iColumn<0
14525 && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
14526 }
14527 default: {
14528 return 0;
14529 }
14530 }
14531 }
14532
14533 /*
14534 ** Return TRUE if the given string is a row-id column name.
14535 */
14536 SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
14537 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
14538 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
14539 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
14540 return 0;
14541 }
14542
14543 /*
14544 ** pX is the RHS of an IN operator. If pX is a SELECT statement
14545 ** that can be simplified to a direct table access, then return
14546 ** a pointer to the SELECT statement. If pX is not a SELECT statement,
14547 ** or if the SELECT statement needs to be manifested into a transient
14548 ** table, then return NULL.
14549 */
14550 #ifndef SQLITE_OMIT_SUBQUERY
14551 static Select *isCandidateForInOpt(Expr *pX){
14552 Select *p;
14553 SrcList *pSrc;
14554 ExprList *pEList;
14555 Table *pTab;
14556 int i;
14557 if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */
14558 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */
14559 p = pX->x.pSelect;
14560 if( p->pPrior ) return 0; /* Not a compound SELECT */
14561 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
14562 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
14563 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
14564 return 0; /* No DISTINCT keyword and no aggregate functions */
14565 }
14566 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
14567 if( p->pLimit ) return 0; /* Has no LIMIT clause */
14568 assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */
14569 if( p->pWhere ) return 0; /* Has no WHERE clause */
14570 pSrc = p->pSrc;
14571 assert( pSrc!=0 );
14572 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
14573 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
14574 pTab = pSrc->a[0].pTab;
14575 assert( pTab!=0 );
14576 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
14577 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
14578 pEList = p->pEList;
14579 assert( pEList!=0 );
14580 /* All SELECT results must be columns. */
14581 for(i=0; i<pEList->nExpr; i++){
14582 Expr *pRes = pEList->a[i].pExpr;
14583 if( pRes->op!=TK_COLUMN ) return 0;
14584 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
14585 }
14586 return p;
14587 }
14588 #endif /* SQLITE_OMIT_SUBQUERY */
14589
14590 #ifndef SQLITE_OMIT_SUBQUERY
14591 /*
14592 ** Generate code that checks the left-most column of index table iCur to see if
14593 ** it contains any NULL entries. Cause the register at regHasNull to be set
14594 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
14595 ** to be set to NULL if iCur contains one or more NULL values.
14596 */
14597 static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
14598 int addr1;
14599 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
14600 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
14601 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
14602 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
14603 VdbeComment((v, "first_entry_in(%d)", iCur));
14604 sqlite3VdbeJumpHere(v, addr1);
14605 }
14606 #endif
14607
14608
14609 #ifndef SQLITE_OMIT_SUBQUERY
14610 /*
14611 ** The argument is an IN operator with a list (not a subquery) on the
14612 ** right-hand side. Return TRUE if that list is constant.
14613 */
14614 static int sqlite3InRhsIsConstant(Expr *pIn){
14615 Expr *pLHS;
14616 int res;
14617 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
14618 pLHS = pIn->pLeft;
14619 pIn->pLeft = 0;
14620 res = sqlite3ExprIsConstant(pIn);
14621 pIn->pLeft = pLHS;
14622 return res;
14623 }
14624 #endif
14625
14626 /*
14627 ** This function is used by the implementation of the IN (...) operator.
14628 ** The pX parameter is the expression on the RHS of the IN operator, which
14629 ** might be either a list of expressions or a subquery.
14630 **
14631 ** The job of this routine is to find or create a b-tree object that can
14632 ** be used either to test for membership in the RHS set or to iterate through
14633 ** all members of the RHS set, skipping duplicates.
14634 **
14635 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
14636 ** and pX->iTable is set to the index of that cursor.
14637 **
14638 ** The returned value of this function indicates the b-tree type, as follows:
14639 **
14640 ** IN_INDEX_ROWID - The cursor was opened on a database table.
14641 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
14642 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
14643 ** IN_INDEX_EPH - The cursor was opened on a specially created and
14644 ** populated epheremal table.
14645 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
14646 ** implemented as a sequence of comparisons.
14647 **
14648 ** An existing b-tree might be used if the RHS expression pX is a simple
14649 ** subquery such as:
14650 **
14651 ** SELECT <column1>, <column2>... FROM <table>
14652 **
14653 ** If the RHS of the IN operator is a list or a more complex subquery, then
14654 ** an ephemeral table might need to be generated from the RHS and then
14655 ** pX->iTable made to point to the ephemeral table instead of an
14656 ** existing table.
14657 **
14658 ** The inFlags parameter must contain exactly one of the bits
14659 ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains
14660 ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
14661 ** fast membership test. When the IN_INDEX_LOOP bit is set, the
14662 ** IN index will be used to loop over all values of the RHS of the
14663 ** IN operator.
14664 **
14665 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
14666 ** through the set members) then the b-tree must not contain duplicates.
14667 ** An epheremal table must be used unless the selected columns are guaranteed
14668 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
14669 ** a UNIQUE constraint or index.
14670 **
14671 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
14672 ** for fast set membership tests) then an epheremal table must
14673 ** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
14674 ** index can be found with the specified <columns> as its left-most.
14675 **
14676 ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
14677 ** if the RHS of the IN operator is a list (not a subquery) then this
14678 ** routine might decide that creating an ephemeral b-tree for membership
14679 ** testing is too expensive and return IN_INDEX_NOOP. In that case, the
14680 ** calling routine should implement the IN operator using a sequence
14681 ** of Eq or Ne comparison operations.
14682 **
14683 ** When the b-tree is being used for membership tests, the calling function
14684 ** might need to know whether or not the RHS side of the IN operator
14685 ** contains a NULL. If prRhsHasNull is not a NULL pointer and
14686 ** if there is any chance that the (...) might contain a NULL value at
14687 ** runtime, then a register is allocated and the register number written
14688 ** to *prRhsHasNull. If there is no chance that the (...) contains a
14689 ** NULL value, then *prRhsHasNull is left unchanged.
14690 **
14691 ** If a register is allocated and its location stored in *prRhsHasNull, then
14692 ** the value in that register will be NULL if the b-tree contains one or more
14693 ** NULL values, and it will be some non-NULL value if the b-tree contains no
14694 ** NULL values.
14695 **
14696 ** If the aiMap parameter is not NULL, it must point to an array containing
14697 ** one element for each column returned by the SELECT statement on the RHS
14698 ** of the IN(...) operator. The i'th entry of the array is populated with the
14699 ** offset of the index column that matches the i'th column returned by the
14700 ** SELECT. For example, if the expression and selected index are:
14701 **
14702 ** (?,?,?) IN (SELECT a, b, c FROM t1)
14703 ** CREATE INDEX i1 ON t1(b, c, a);
14704 **
14705 ** then aiMap[] is populated with {2, 0, 1}.
14706 */
14707 #ifndef SQLITE_OMIT_SUBQUERY
14708 SQLITE_PRIVATE int sqlite3FindInIndex(
14709 Parse *pParse, /* Parsing context */
14710 Expr *pX, /* The right-hand side (RHS) of the IN operator */
14711 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
14712 int *prRhsHasNull, /* Register holding NULL status. See notes */
14713 int *aiMap /* Mapping from Index fields to RHS fields */
14714 ){
14715 Select *p; /* SELECT to the right of IN operator */
14716 int eType = 0; /* Type of RHS table. IN_INDEX_* */
14717 int iTab = pParse->nTab++; /* Cursor of the RHS table */
14718 int mustBeUnique; /* True if RHS must be unique */
14719 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
14720
14721 assert( pX->op==TK_IN );
14722 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
14723
14724 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
14725 ** whether or not the SELECT result contains NULL values, check whether
14726 ** or not NULL is actually possible (it may not be, for example, due
14727 ** to NOT NULL constraints in the schema). If no NULL values are possible,
14728 ** set prRhsHasNull to 0 before continuing. */
14729 if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
14730 int i;
14731 ExprList *pEList = pX->x.pSelect->pEList;
14732 for(i=0; i<pEList->nExpr; i++){
14733 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
14734 }
14735 if( i==pEList->nExpr ){
14736 prRhsHasNull = 0;
14737 }
14738 }
14739
14740 /* Check to see if an existing table or index can be used to
14741 ** satisfy the query. This is preferable to generating a new
14742 ** ephemeral table. */
14743 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
14744 sqlite3 *db = pParse->db; /* Database connection */
14745 Table *pTab; /* Table <table>. */
14746 i16 iDb; /* Database idx for pTab */
14747 ExprList *pEList = p->pEList;
14748 int nExpr = pEList->nExpr;
14749
14750 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
14751 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
14752 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
14753 pTab = p->pSrc->a[0].pTab;
14754
14755 /* Code an OP_Transaction and OP_TableLock for <table>. */
14756 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
14757 sqlite3CodeVerifySchema(pParse, iDb);
14758 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
14759
14760 assert(v); /* sqlite3GetVdbe() has always been previously called */
14761 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
14762 /* The "x IN (SELECT rowid FROM table)" case */
14763 int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
14764 VdbeCoverage(v);
14765
14766 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
14767 eType = IN_INDEX_ROWID;
14768
14769 sqlite3VdbeJumpHere(v, iAddr);
14770 }else{
14771 Index *pIdx; /* Iterator variable */
14772 int affinity_ok = 1;
14773 int i;
14774
14775 /* Check that the affinity that will be used to perform each
14776 ** comparison is the same as the affinity of each column in table
14777 ** on the RHS of the IN operator. If it not, it is not possible to
14778 ** use any index of the RHS table. */
14779 for(i=0; i<nExpr && affinity_ok; i++){
14780 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
14781 int iCol = pEList->a[i].pExpr->iColumn;
14782 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
14783 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
14784 testcase( cmpaff==SQLITE_AFF_BLOB );
14785 testcase( cmpaff==SQLITE_AFF_TEXT );
14786 switch( cmpaff ){
14787 case SQLITE_AFF_BLOB:
14788 break;
14789 case SQLITE_AFF_TEXT:
14790 /* sqlite3CompareAffinity() only returns TEXT if one side or the
14791 ** other has no affinity and the other side is TEXT. Hence,
14792 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
14793 ** and for the term on the LHS of the IN to have no affinity. */
14794 assert( idxaff==SQLITE_AFF_TEXT );
14795 break;
14796 default:
14797 affinity_ok = sqlite3IsNumericAffinity(idxaff);
14798 }
14799 }
14800
14801 if( affinity_ok ){
14802 /* Search for an existing index that will work for this IN operator */
14803 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
14804 Bitmask colUsed; /* Columns of the index used */
14805 Bitmask mCol; /* Mask for the current column */
14806 if( pIdx->nColumn<nExpr ) continue;
14807 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
14808 ** BITMASK(nExpr) without overflowing */
14809 testcase( pIdx->nColumn==BMS-2 );
14810 testcase( pIdx->nColumn==BMS-1 );
14811 if( pIdx->nColumn>=BMS-1 ) continue;
14812 if( mustBeUnique ){
14813 if( pIdx->nKeyCol>nExpr
14814 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
14815 ){
14816 continue; /* This index is not unique over the IN RHS columns */
14817 }
14818 }
14819
14820 colUsed = 0; /* Columns of index used so far */
14821 for(i=0; i<nExpr; i++){
14822 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
14823 Expr *pRhs = pEList->a[i].pExpr;
14824 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
14825 int j;
14826
14827 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
14828 for(j=0; j<nExpr; j++){
14829 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
14830 assert( pIdx->azColl[j] );
14831 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
14832 continue;
14833 }
14834 break;
14835 }
14836 if( j==nExpr ) break;
14837 mCol = MASKBIT(j);
14838 if( mCol & colUsed ) break; /* Each column used only once */
14839 colUsed |= mCol;
14840 if( aiMap ) aiMap[i] = j;
14841 }
14842
14843 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
14844 if( colUsed==(MASKBIT(nExpr)-1) ){
14845 /* If we reach this point, that means the index pIdx is usable */
14846 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
14847 #ifndef SQLITE_OMIT_EXPLAIN
14848 sqlite3VdbeAddOp4(v, OP_Explain, 0, 0, 0,
14849 sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName),
14850 P4_DYNAMIC);
14851 #endif
14852 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
14853 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
14854 VdbeComment((v, "%s", pIdx->zName));
14855 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
14856 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
14857
14858 if( prRhsHasNull ){
14859 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
14860 i64 mask = (1<<nExpr)-1;
14861 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed,
14862 iTab, 0, 0, (u8*)&mask, P4_INT64);
14863 #endif
14864 *prRhsHasNull = ++pParse->nMem;
14865 if( nExpr==1 ){
14866 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
14867 }
14868 }
14869 sqlite3VdbeJumpHere(v, iAddr);
14870 }
14871 } /* End loop over indexes */
14872 } /* End if( affinity_ok ) */
14873 } /* End if not an rowid index */
14874 } /* End attempt to optimize using an index */
14875
14876 /* If no preexisting index is available for the IN clause
14877 ** and IN_INDEX_NOOP is an allowed reply
14878 ** and the RHS of the IN operator is a list, not a subquery
14879 ** and the RHS is not constant or has two or fewer terms,
14880 ** then it is not worth creating an ephemeral table to evaluate
14881 ** the IN operator so return IN_INDEX_NOOP.
14882 */
14883 if( eType==0
14884 && (inFlags & IN_INDEX_NOOP_OK)
14885 && !ExprHasProperty(pX, EP_xIsSelect)
14886 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
14887 ){
14888 eType = IN_INDEX_NOOP;
14889 }
14890
14891 if( eType==0 ){
14892 /* Could not find an existing table or index to use as the RHS b-tree.
14893 ** We will have to generate an ephemeral table to do the job.
14894 */
14895 u32 savedNQueryLoop = pParse->nQueryLoop;
14896 int rMayHaveNull = 0;
14897 eType = IN_INDEX_EPH;
14898 if( inFlags & IN_INDEX_LOOP ){
14899 pParse->nQueryLoop = 0;
14900 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
14901 eType = IN_INDEX_ROWID;
14902 }
14903 }else if( prRhsHasNull ){
14904 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
14905 }
14906 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
14907 pParse->nQueryLoop = savedNQueryLoop;
14908 }else{
14909 pX->iTable = iTab;
14910 }
14911
14912 if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
14913 int i, n;
14914 n = sqlite3ExprVectorSize(pX->pLeft);
14915 for(i=0; i<n; i++) aiMap[i] = i;
14916 }
14917 return eType;
14918 }
14919 #endif
14920
14921 #ifndef SQLITE_OMIT_SUBQUERY
14922 /*
14923 ** Argument pExpr is an (?, ?...) IN(...) expression. This
14924 ** function allocates and returns a nul-terminated string containing
14925 ** the affinities to be used for each column of the comparison.
14926 **
14927 ** It is the responsibility of the caller to ensure that the returned
14928 ** string is eventually freed using sqlite3DbFree().
14929 */
14930 static char *exprINAffinity(Parse *pParse, Expr *pExpr){
14931 Expr *pLeft = pExpr->pLeft;
14932 int nVal = sqlite3ExprVectorSize(pLeft);
14933 Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
14934 char *zRet;
14935
14936 assert( pExpr->op==TK_IN );
14937 zRet = sqlite3DbMallocZero(pParse->db, nVal+1);
14938 if( zRet ){
14939 int i;
14940 for(i=0; i<nVal; i++){
14941 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
14942 char a = sqlite3ExprAffinity(pA);
14943 if( pSelect ){
14944 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
14945 }else{
14946 zRet[i] = a;
14947 }
14948 }
14949 zRet[nVal] = '\0';
14950 }
14951 return zRet;
14952 }
14953 #endif
14954
14955 #ifndef SQLITE_OMIT_SUBQUERY
14956 /*
14957 ** Load the Parse object passed as the first argument with an error
14958 ** message of the form:
14959 **
14960 ** "sub-select returns N columns - expected M"
14961 */
14962 SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpec t){
14963 const char *zFmt = "sub-select returns %d columns - expected %d";
14964 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
14965 }
14966 #endif
14967
14968 /*
14969 ** Expression pExpr is a vector that has been used in a context where
14970 ** it is not permitted. If pExpr is a sub-select vector, this routine
14971 ** loads the Parse object with a message of the form:
14972 **
14973 ** "sub-select returns N columns - expected 1"
14974 **
14975 ** Or, if it is a regular scalar vector:
14976 **
14977 ** "row value misused"
14978 */
14979 SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
14980 #ifndef SQLITE_OMIT_SUBQUERY
14981 if( pExpr->flags & EP_xIsSelect ){
14982 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
14983 }else
14984 #endif
14985 {
14986 sqlite3ErrorMsg(pParse, "row value misused");
14987 }
14988 }
14989
14990 /*
14991 ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
14992 ** or IN operators. Examples:
14993 **
14994 ** (SELECT a FROM b) -- subquery
14995 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
14996 ** x IN (4,5,11) -- IN operator with list on right-hand side
14997 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
14998 **
14999 ** The pExpr parameter describes the expression that contains the IN
15000 ** operator or subquery.
15001 **
15002 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
15003 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
15004 ** to some integer key column of a table B-Tree. In this case, use an
15005 ** intkey B-Tree to store the set of IN(...) values instead of the usual
15006 ** (slower) variable length keys B-Tree.
15007 **
15008 ** If rMayHaveNull is non-zero, that means that the operation is an IN
15009 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
15010 ** All this routine does is initialize the register given by rMayHaveNull
15011 ** to NULL. Calling routines will take care of changing this register
15012 ** value to non-NULL if the RHS is NULL-free.
15013 **
15014 ** For a SELECT or EXISTS operator, return the register that holds the
15015 ** result. For a multi-column SELECT, the result is stored in a contiguous
15016 ** array of registers and the return value is the register of the left-most
15017 ** result column. Return 0 for IN operators or if an error occurs.
15018 */
15019 #ifndef SQLITE_OMIT_SUBQUERY
15020 SQLITE_PRIVATE int sqlite3CodeSubselect(
15021 Parse *pParse, /* Parsing context */
15022 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
15023 int rHasNullFlag, /* Register that records whether NULLs exist in RHS */
15024 int isRowid /* If true, LHS of IN operator is a rowid */
15025 ){
15026 int jmpIfDynamic = -1; /* One-time test address */
15027 int rReg = 0; /* Register storing resulting */
15028 Vdbe *v = sqlite3GetVdbe(pParse);
15029 if( NEVER(v==0) ) return 0;
15030 sqlite3ExprCachePush(pParse);
15031
15032 /* The evaluation of the IN/EXISTS/SELECT must be repeated every time it
15033 ** is encountered if any of the following is true:
15034 **
15035 ** * The right-hand side is a correlated subquery
15036 ** * The right-hand side is an expression list containing variables
15037 ** * We are inside a trigger
15038 **
15039 ** If all of the above are false, then we can run this code just once
15040 ** save the results, and reuse the same result on subsequent invocations.
15041 */
15042 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
15043 jmpIfDynamic = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
15044 }
15045
15046 #ifndef SQLITE_OMIT_EXPLAIN
15047 if( pParse->explain==2 ){
15048 char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
15049 jmpIfDynamic>=0?"":"CORRELATED ",
15050 pExpr->op==TK_IN?"LIST":"SCALAR",
15051 pParse->iNextSelectId
15052 );
15053 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
15054 }
15055 #endif
15056
15057 switch( pExpr->op ){
15058 case TK_IN: {
15059 int addr; /* Address of OP_OpenEphemeral instruction */
15060 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
15061 KeyInfo *pKeyInfo = 0; /* Key information */
15062 int nVal; /* Size of vector pLeft */
15063
15064 nVal = sqlite3ExprVectorSize(pLeft);
15065 assert( !isRowid || nVal==1 );
15066
15067 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
15068 ** expression it is handled the same way. An ephemeral table is
15069 ** filled with index keys representing the results from the
15070 ** SELECT or the <exprlist>.
15071 **
15072 ** If the 'x' expression is a column value, or the SELECT...
15073 ** statement returns a column value, then the affinity of that
15074 ** column is used to build the index keys. If both 'x' and the
15075 ** SELECT... statement are columns, then numeric affinity is used
15076 ** if either column has NUMERIC or INTEGER affinity. If neither
15077 ** 'x' nor the SELECT... statement are columns, then numeric affinity
15078 ** is used.
15079 */
15080 pExpr->iTable = pParse->nTab++;
15081 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral,
15082 pExpr->iTable, (isRowid?0:nVal));
15083 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
15084
15085 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
15086 /* Case 1: expr IN (SELECT ...)
15087 **
15088 ** Generate code to write the results of the select into the temporary
15089 ** table allocated and opened above.
15090 */
15091 Select *pSelect = pExpr->x.pSelect;
15092 ExprList *pEList = pSelect->pEList;
15093
15094 assert( !isRowid );
15095 /* If the LHS and RHS of the IN operator do not match, that
15096 ** error will have been caught long before we reach this point. */
15097 if( ALWAYS(pEList->nExpr==nVal) ){
15098 SelectDest dest;
15099 int i;
15100 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
15101 dest.zAffSdst = exprINAffinity(pParse, pExpr);
15102 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
15103 pSelect->iLimit = 0;
15104 testcase( pSelect->selFlags & SF_Distinct );
15105 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
15106 if( sqlite3Select(pParse, pSelect, &dest) ){
15107 sqlite3DbFree(pParse->db, dest.zAffSdst);
15108 sqlite3KeyInfoUnref(pKeyInfo);
15109 return 0;
15110 }
15111 sqlite3DbFree(pParse->db, dest.zAffSdst);
15112 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
15113 assert( pEList!=0 );
15114 assert( pEList->nExpr>0 );
15115 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
15116 for(i=0; i<nVal; i++){
15117 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
15118 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
15119 pParse, p, pEList->a[i].pExpr
15120 );
15121 }
15122 }
15123 }else if( ALWAYS(pExpr->x.pList!=0) ){
15124 /* Case 2: expr IN (exprlist)
15125 **
15126 ** For each expression, build an index key from the evaluation and
15127 ** store it in the temporary table. If <expr> is a column, then use
15128 ** that columns affinity when building index keys. If <expr> is not
15129 ** a column, use numeric affinity.
15130 */
15131 char affinity; /* Affinity of the LHS of the IN */
15132 int i;
15133 ExprList *pList = pExpr->x.pList;
15134 struct ExprList_item *pItem;
15135 int r1, r2, r3;
15136
15137 affinity = sqlite3ExprAffinity(pLeft);
15138 if( !affinity ){
15139 affinity = SQLITE_AFF_BLOB;
15140 }
15141 if( pKeyInfo ){
15142 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
15143 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
15144 }
15145
15146 /* Loop through each expression in <exprlist>. */
15147 r1 = sqlite3GetTempReg(pParse);
15148 r2 = sqlite3GetTempReg(pParse);
15149 if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
15150 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
15151 Expr *pE2 = pItem->pExpr;
15152 int iValToIns;
15153
15154 /* If the expression is not constant then we will need to
15155 ** disable the test that was generated above that makes sure
15156 ** this code only executes once. Because for a non-constant
15157 ** expression we need to rerun this code each time.
15158 */
15159 if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
15160 sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
15161 jmpIfDynamic = -1;
15162 }
15163
15164 /* Evaluate the expression and insert it into the temp table */
15165 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
15166 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
15167 }else{
15168 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
15169 if( isRowid ){
15170 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
15171 sqlite3VdbeCurrentAddr(v)+2);
15172 VdbeCoverage(v);
15173 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
15174 }else{
15175 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
15176 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
15177 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1);
15178 }
15179 }
15180 }
15181 sqlite3ReleaseTempReg(pParse, r1);
15182 sqlite3ReleaseTempReg(pParse, r2);
15183 }
15184 if( pKeyInfo ){
15185 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
15186 }
15187 break;
15188 }
15189
15190 case TK_EXISTS:
15191 case TK_SELECT:
15192 default: {
15193 /* Case 3: (SELECT ... FROM ...)
15194 ** or: EXISTS(SELECT ... FROM ...)
15195 **
15196 ** For a SELECT, generate code to put the values for all columns of
15197 ** the first row into an array of registers and return the index of
15198 ** the first register.
15199 **
15200 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
15201 ** into a register and return that register number.
15202 **
15203 ** In both cases, the query is augmented with "LIMIT 1". Any
15204 ** preexisting limit is discarded in place of the new LIMIT 1.
15205 */
15206 Select *pSel; /* SELECT statement to encode */
15207 SelectDest dest; /* How to deal with SELECT result */
15208 int nReg; /* Registers to allocate */
15209
15210 testcase( pExpr->op==TK_EXISTS );
15211 testcase( pExpr->op==TK_SELECT );
15212 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
15213 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
15214
15215 pSel = pExpr->x.pSelect;
15216 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
15217 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
15218 pParse->nMem += nReg;
15219 if( pExpr->op==TK_SELECT ){
15220 dest.eDest = SRT_Mem;
15221 dest.iSdst = dest.iSDParm;
15222 dest.nSdst = nReg;
15223 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
15224 VdbeComment((v, "Init subquery result"));
15225 }else{
15226 dest.eDest = SRT_Exists;
15227 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
15228 VdbeComment((v, "Init EXISTS result"));
15229 }
15230 sqlite3ExprDelete(pParse->db, pSel->pLimit);
15231 pSel->pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,
15232 &sqlite3IntTokens[1], 0);
15233 pSel->iLimit = 0;
15234 pSel->selFlags &= ~SF_MultiValue;
15235 if( sqlite3Select(pParse, pSel, &dest) ){
15236 return 0;
15237 }
15238 rReg = dest.iSDParm;
15239 ExprSetVVAProperty(pExpr, EP_NoReduce);
15240 break;
15241 }
15242 }
15243
15244 if( rHasNullFlag ){
15245 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
15246 }
15247
15248 if( jmpIfDynamic>=0 ){
15249 sqlite3VdbeJumpHere(v, jmpIfDynamic);
15250 }
15251 sqlite3ExprCachePop(pParse);
15252
15253 return rReg;
15254 }
15255 #endif /* SQLITE_OMIT_SUBQUERY */
15256
15257 #ifndef SQLITE_OMIT_SUBQUERY
15258 /*
15259 ** Expr pIn is an IN(...) expression. This function checks that the
15260 ** sub-select on the RHS of the IN() operator has the same number of
15261 ** columns as the vector on the LHS. Or, if the RHS of the IN() is not
15262 ** a sub-query, that the LHS is a vector of size 1.
15263 */
15264 SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
15265 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
15266 if( (pIn->flags & EP_xIsSelect) ){
15267 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
15268 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
15269 return 1;
15270 }
15271 }else if( nVector!=1 ){
15272 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
15273 return 1;
15274 }
15275 return 0;
15276 }
15277 #endif
15278
15279 #ifndef SQLITE_OMIT_SUBQUERY
15280 /*
15281 ** Generate code for an IN expression.
15282 **
15283 ** x IN (SELECT ...)
15284 ** x IN (value, value, ...)
15285 **
15286 ** The left-hand side (LHS) is a scalar or vector expression. The
15287 ** right-hand side (RHS) is an array of zero or more scalar values, or a
15288 ** subquery. If the RHS is a subquery, the number of result columns must
15289 ** match the number of columns in the vector on the LHS. If the RHS is
15290 ** a list of values, the LHS must be a scalar.
15291 **
15292 ** The IN operator is true if the LHS value is contained within the RHS.
15293 ** The result is false if the LHS is definitely not in the RHS. The
15294 ** result is NULL if the presence of the LHS in the RHS cannot be
15295 ** determined due to NULLs.
15296 **
15297 ** This routine generates code that jumps to destIfFalse if the LHS is not
15298 ** contained within the RHS. If due to NULLs we cannot determine if the LHS
15299 ** is contained in the RHS then jump to destIfNull. If the LHS is contained
15300 ** within the RHS then fall through.
15301 **
15302 ** See the separate in-operator.md documentation file in the canonical
15303 ** SQLite source tree for additional information.
15304 */
15305 static void sqlite3ExprCodeIN(
15306 Parse *pParse, /* Parsing and code generating context */
15307 Expr *pExpr, /* The IN expression */
15308 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
15309 int destIfNull /* Jump here if the results are unknown due to NULLs */
15310 ){
15311 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
15312 int eType; /* Type of the RHS */
15313 int rLhs; /* Register(s) holding the LHS values */
15314 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
15315 Vdbe *v; /* Statement under construction */
15316 int *aiMap = 0; /* Map from vector field to index column */
15317 char *zAff = 0; /* Affinity string for comparisons */
15318 int nVector; /* Size of vectors for this IN operator */
15319 int iDummy; /* Dummy parameter to exprCodeVector() */
15320 Expr *pLeft; /* The LHS of the IN operator */
15321 int i; /* loop counter */
15322 int destStep2; /* Where to jump when NULLs seen in step 2 */
15323 int destStep6 = 0; /* Start of code for Step 6 */
15324 int addrTruthOp; /* Address of opcode that determines the IN is true */
15325 int destNotNull; /* Jump here if a comparison is not true in step 6 */
15326 int addrTop; /* Top of the step-6 loop */
15327
15328 pLeft = pExpr->pLeft;
15329 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
15330 zAff = exprINAffinity(pParse, pExpr);
15331 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
15332 aiMap = (int*)sqlite3DbMallocZero(
15333 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
15334 );
15335 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
15336
15337 /* Attempt to compute the RHS. After this step, if anything other than
15338 ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable
15339 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
15340 ** the RHS has not yet been coded. */
15341 v = pParse->pVdbe;
15342 assert( v!=0 ); /* OOM detected prior to this routine */
15343 VdbeNoopComment((v, "begin IN expr"));
15344 eType = sqlite3FindInIndex(pParse, pExpr,
15345 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
15346 destIfFalse==destIfNull ? 0 : &rRhsHasNull, aiMap);
15347
15348 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
15349 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC
15350 );
15351 #ifdef SQLITE_DEBUG
15352 /* Confirm that aiMap[] contains nVector integer values between 0 and
15353 ** nVector-1. */
15354 for(i=0; i<nVector; i++){
15355 int j, cnt;
15356 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
15357 assert( cnt==1 );
15358 }
15359 #endif
15360
15361 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
15362 ** vector, then it is stored in an array of nVector registers starting
15363 ** at r1.
15364 **
15365 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
15366 ** so that the fields are in the same order as an existing index. The
15367 ** aiMap[] array contains a mapping from the original LHS field order to
15368 ** the field order that matches the RHS index.
15369 */
15370 sqlite3ExprCachePush(pParse);
15371 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
15372 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
15373 if( i==nVector ){
15374 /* LHS fields are not reordered */
15375 rLhs = rLhsOrig;
15376 }else{
15377 /* Need to reorder the LHS fields according to aiMap */
15378 rLhs = sqlite3GetTempRange(pParse, nVector);
15379 for(i=0; i<nVector; i++){
15380 sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0);
15381 }
15382 }
15383
15384 /* If sqlite3FindInIndex() did not find or create an index that is
15385 ** suitable for evaluating the IN operator, then evaluate using a
15386 ** sequence of comparisons.
15387 **
15388 ** This is step (1) in the in-operator.md optimized algorithm.
15389 */
15390 if( eType==IN_INDEX_NOOP ){
15391 ExprList *pList = pExpr->x.pList;
15392 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
15393 int labelOk = sqlite3VdbeMakeLabel(v);
15394 int r2, regToFree;
15395 int regCkNull = 0;
15396 int ii;
15397 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
15398 if( destIfNull!=destIfFalse ){
15399 regCkNull = sqlite3GetTempReg(pParse);
15400 sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
15401 }
15402 for(ii=0; ii<pList->nExpr; ii++){
15403 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
15404 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
15405 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
15406 }
15407 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
15408 sqlite3VdbeAddOp4(v, OP_Eq, rLhs, labelOk, r2,
15409 (void*)pColl, P4_COLLSEQ);
15410 VdbeCoverageIf(v, ii<pList->nExpr-1);
15411 VdbeCoverageIf(v, ii==pList->nExpr-1);
15412 sqlite3VdbeChangeP5(v, zAff[0]);
15413 }else{
15414 assert( destIfNull==destIfFalse );
15415 sqlite3VdbeAddOp4(v, OP_Ne, rLhs, destIfFalse, r2,
15416 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
15417 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL);
15418 }
15419 sqlite3ReleaseTempReg(pParse, regToFree);
15420 }
15421 if( regCkNull ){
15422 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
15423 sqlite3VdbeGoto(v, destIfFalse);
15424 }
15425 sqlite3VdbeResolveLabel(v, labelOk);
15426 sqlite3ReleaseTempReg(pParse, regCkNull);
15427 goto sqlite3ExprCodeIN_finished;
15428 }
15429
15430 /* Step 2: Check to see if the LHS contains any NULL columns. If the
15431 ** LHS does contain NULLs then the result must be either FALSE or NULL.
15432 ** We will then skip the binary search of the RHS.
15433 */
15434 if( destIfNull==destIfFalse ){
15435 destStep2 = destIfFalse;
15436 }else{
15437 destStep2 = destStep6 = sqlite3VdbeMakeLabel(v);
15438 }
15439 for(i=0; i<nVector; i++){
15440 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
15441 if( sqlite3ExprCanBeNull(p) ){
15442 sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2);
15443 VdbeCoverage(v);
15444 }
15445 }
15446
15447 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
15448 ** of the RHS using the LHS as a probe. If found, the result is
15449 ** true.
15450 */
15451 if( eType==IN_INDEX_ROWID ){
15452 /* In this case, the RHS is the ROWID of table b-tree and so we also
15453 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
15454 ** into a single opcode. */
15455 sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, rLhs);
15456 VdbeCoverage(v);
15457 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto); /* Return True */
15458 }else{
15459 sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector);
15460 if( destIfFalse==destIfNull ){
15461 /* Combine Step 3 and Step 5 into a single opcode */
15462 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse,
15463 rLhs, nVector); VdbeCoverage(v);
15464 goto sqlite3ExprCodeIN_finished;
15465 }
15466 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
15467 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0,
15468 rLhs, nVector); VdbeCoverage(v);
15469 }
15470
15471 /* Step 4. If the RHS is known to be non-NULL and we did not find
15472 ** an match on the search above, then the result must be FALSE.
15473 */
15474 if( rRhsHasNull && nVector==1 ){
15475 sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse);
15476 VdbeCoverage(v);
15477 }
15478
15479 /* Step 5. If we do not care about the difference between NULL and
15480 ** FALSE, then just return false.
15481 */
15482 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
15483
15484 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
15485 ** If any comparison is NULL, then the result is NULL. If all
15486 ** comparisons are FALSE then the final result is FALSE.
15487 **
15488 ** For a scalar LHS, it is sufficient to check just the first row
15489 ** of the RHS.
15490 */
15491 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
15492 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
15493 VdbeCoverage(v);
15494 if( nVector>1 ){
15495 destNotNull = sqlite3VdbeMakeLabel(v);
15496 }else{
15497 /* For nVector==1, combine steps 6 and 7 by immediately returning
15498 ** FALSE if the first comparison is not NULL */
15499 destNotNull = destIfFalse;
15500 }
15501 for(i=0; i<nVector; i++){
15502 Expr *p;
15503 CollSeq *pColl;
15504 int r3 = sqlite3GetTempReg(pParse);
15505 p = sqlite3VectorFieldSubexpr(pLeft, i);
15506 pColl = sqlite3ExprCollSeq(pParse, p);
15507 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r3);
15508 sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3,
15509 (void*)pColl, P4_COLLSEQ);
15510 VdbeCoverage(v);
15511 sqlite3ReleaseTempReg(pParse, r3);
15512 }
15513 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
15514 if( nVector>1 ){
15515 sqlite3VdbeResolveLabel(v, destNotNull);
15516 sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrTop+1);
15517 VdbeCoverage(v);
15518
15519 /* Step 7: If we reach this point, we know that the result must
15520 ** be false. */
15521 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
15522 }
15523
15524 /* Jumps here in order to return true. */
15525 sqlite3VdbeJumpHere(v, addrTruthOp);
15526
15527 sqlite3ExprCodeIN_finished:
15528 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
15529 sqlite3ExprCachePop(pParse);
15530 VdbeComment((v, "end IN expr"));
15531 sqlite3ExprCodeIN_oom_error:
15532 sqlite3DbFree(pParse->db, aiMap);
15533 sqlite3DbFree(pParse->db, zAff);
15534 }
15535 #endif /* SQLITE_OMIT_SUBQUERY */
15536
15537 #ifndef SQLITE_OMIT_FLOATING_POINT
15538 /*
15539 ** Generate an instruction that will put the floating point
15540 ** value described by z[0..n-1] into register iMem.
15541 **
15542 ** The z[] string will probably not be zero-terminated. But the
15543 ** z[n] character is guaranteed to be something that does not look
15544 ** like the continuation of the number.
15545 */
15546 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
15547 if( ALWAYS(z!=0) ){
15548 double value;
15549 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
15550 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
15551 if( negateFlag ) value = -value;
15552 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
15553 }
15554 }
15555 #endif
15556
15557
15558 /*
15559 ** Generate an instruction that will put the integer describe by
15560 ** text z[0..n-1] into register iMem.
15561 **
15562 ** Expr.u.zToken is always UTF8 and zero-terminated.
15563 */
15564 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
15565 Vdbe *v = pParse->pVdbe;
15566 if( pExpr->flags & EP_IntValue ){
15567 int i = pExpr->u.iValue;
15568 assert( i>=0 );
15569 if( negFlag ) i = -i;
15570 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
15571 }else{
15572 int c;
15573 i64 value;
15574 const char *z = pExpr->u.zToken;
15575 assert( z!=0 );
15576 c = sqlite3DecOrHexToI64(z, &value);
15577 if( c==1 || (c==2 && !negFlag) || (negFlag && value==SMALLEST_INT64)){
15578 #ifdef SQLITE_OMIT_FLOATING_POINT
15579 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
15580 #else
15581 #ifndef SQLITE_OMIT_HEX_INTEGER
15582 if( sqlite3_strnicmp(z,"0x",2)==0 ){
15583 sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
15584 }else
15585 #endif
15586 {
15587 codeReal(v, z, negFlag, iMem);
15588 }
15589 #endif
15590 }else{
15591 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
15592 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
15593 }
15594 }
15595 }
15596
15597 /*
15598 ** Erase column-cache entry number i
15599 */
15600 static void cacheEntryClear(Parse *pParse, int i){
15601 if( pParse->aColCache[i].tempReg ){
15602 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
15603 pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
15604 }
15605 }
15606 pParse->nColCache--;
15607 if( i<pParse->nColCache ){
15608 pParse->aColCache[i] = pParse->aColCache[pParse->nColCache];
15609 }
15610 }
15611
15612
15613 /*
15614 ** Record in the column cache that a particular column from a
15615 ** particular table is stored in a particular register.
15616 */
15617 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
15618 int i;
15619 int minLru;
15620 int idxLru;
15621 struct yColCache *p;
15622
15623 /* Unless an error has occurred, register numbers are always positive. */
15624 assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
15625 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */
15626
15627 /* The SQLITE_ColumnCache flag disables the column cache. This is used
15628 ** for testing only - to verify that SQLite always gets the same answer
15629 ** with and without the column cache.
15630 */
15631 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
15632
15633 /* First replace any existing entry.
15634 **
15635 ** Actually, the way the column cache is currently used, we are guaranteed
15636 ** that the object will never already be in cache. Verify this guarantee.
15637 */
15638 #ifndef NDEBUG
15639 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
15640 assert( p->iTable!=iTab || p->iColumn!=iCol );
15641 }
15642 #endif
15643
15644 /* If the cache is already full, delete the least recently used entry */
15645 if( pParse->nColCache>=SQLITE_N_COLCACHE ){
15646 minLru = 0x7fffffff;
15647 idxLru = -1;
15648 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
15649 if( p->lru<minLru ){
15650 idxLru = i;
15651 minLru = p->lru;
15652 }
15653 }
15654 p = &pParse->aColCache[idxLru];
15655 }else{
15656 p = &pParse->aColCache[pParse->nColCache++];
15657 }
15658
15659 /* Add the new entry to the end of the cache */
15660 p->iLevel = pParse->iCacheLevel;
15661 p->iTable = iTab;
15662 p->iColumn = iCol;
15663 p->iReg = iReg;
15664 p->tempReg = 0;
15665 p->lru = pParse->iCacheCnt++;
15666 }
15667
15668 /*
15669 ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
15670 ** Purge the range of registers from the column cache.
15671 */
15672 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
15673 int i = 0;
15674 while( i<pParse->nColCache ){
15675 struct yColCache *p = &pParse->aColCache[i];
15676 if( p->iReg >= iReg && p->iReg < iReg+nReg ){
15677 cacheEntryClear(pParse, i);
15678 }else{
15679 i++;
15680 }
15681 }
15682 }
15683
15684 /*
15685 ** Remember the current column cache context. Any new entries added
15686 ** added to the column cache after this call are removed when the
15687 ** corresponding pop occurs.
15688 */
15689 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
15690 pParse->iCacheLevel++;
15691 #ifdef SQLITE_DEBUG
15692 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
15693 printf("PUSH to %d\n", pParse->iCacheLevel);
15694 }
15695 #endif
15696 }
15697
15698 /*
15699 ** Remove from the column cache any entries that were added since the
15700 ** the previous sqlite3ExprCachePush operation. In other words, restore
15701 ** the cache to the state it was in prior the most recent Push.
15702 */
15703 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse){
15704 int i = 0;
15705 assert( pParse->iCacheLevel>=1 );
15706 pParse->iCacheLevel--;
15707 #ifdef SQLITE_DEBUG
15708 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
15709 printf("POP to %d\n", pParse->iCacheLevel);
15710 }
15711 #endif
15712 while( i<pParse->nColCache ){
15713 if( pParse->aColCache[i].iLevel>pParse->iCacheLevel ){
15714 cacheEntryClear(pParse, i);
15715 }else{
15716 i++;
15717 }
15718 }
15719 }
15720
15721 /*
15722 ** When a cached column is reused, make sure that its register is
15723 ** no longer available as a temp register. ticket #3879: that same
15724 ** register might be in the cache in multiple places, so be sure to
15725 ** get them all.
15726 */
15727 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
15728 int i;
15729 struct yColCache *p;
15730 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
15731 if( p->iReg==iReg ){
15732 p->tempReg = 0;
15733 }
15734 }
15735 }
15736
15737 /* Generate code that will load into register regOut a value that is
15738 ** appropriate for the iIdxCol-th column of index pIdx.
15739 */
15740 SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(
15741 Parse *pParse, /* The parsing context */
15742 Index *pIdx, /* The index whose column is to be loaded */
15743 int iTabCur, /* Cursor pointing to a table row */
15744 int iIdxCol, /* The column of the index to be loaded */
15745 int regOut /* Store the index column value in this register */
15746 ){
15747 i16 iTabCol = pIdx->aiColumn[iIdxCol];
15748 if( iTabCol==XN_EXPR ){
15749 assert( pIdx->aColExpr );
15750 assert( pIdx->aColExpr->nExpr>iIdxCol );
15751 pParse->iSelfTab = iTabCur;
15752 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
15753 }else{
15754 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
15755 iTabCol, regOut);
15756 }
15757 }
15758
15759 /*
15760 ** Generate code to extract the value of the iCol-th column of a table.
15761 */
15762 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
15763 Vdbe *v, /* The VDBE under construction */
15764 Table *pTab, /* The table containing the value */
15765 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
15766 int iCol, /* Index of the column to extract */
15767 int regOut /* Extract the value into this register */
15768 ){
15769 if( iCol<0 || iCol==pTab->iPKey ){
15770 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
15771 }else{
15772 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
15773 int x = iCol;
15774 if( !HasRowid(pTab) && !IsVirtual(pTab) ){
15775 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
15776 }
15777 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
15778 }
15779 if( iCol>=0 ){
15780 sqlite3ColumnDefault(v, pTab, iCol, regOut);
15781 }
15782 }
15783
15784 /*
15785 ** Generate code that will extract the iColumn-th column from
15786 ** table pTab and store the column value in a register.
15787 **
15788 ** An effort is made to store the column value in register iReg. This
15789 ** is not garanteeed for GetColumn() - the result can be stored in
15790 ** any register. But the result is guaranteed to land in register iReg
15791 ** for GetColumnToReg().
15792 **
15793 ** There must be an open cursor to pTab in iTable when this routine
15794 ** is called. If iColumn<0 then code is generated that extracts the rowid.
15795 */
15796 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
15797 Parse *pParse, /* Parsing and code generating context */
15798 Table *pTab, /* Description of the table we are reading from */
15799 int iColumn, /* Index of the table column */
15800 int iTable, /* The cursor pointing to the table */
15801 int iReg, /* Store results here */
15802 u8 p5 /* P5 value for OP_Column + FLAGS */
15803 ){
15804 Vdbe *v = pParse->pVdbe;
15805 int i;
15806 struct yColCache *p;
15807
15808 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
15809 if( p->iTable==iTable && p->iColumn==iColumn ){
15810 p->lru = pParse->iCacheCnt++;
15811 sqlite3ExprCachePinRegister(pParse, p->iReg);
15812 return p->iReg;
15813 }
15814 }
15815 assert( v!=0 );
15816 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
15817 if( p5 ){
15818 sqlite3VdbeChangeP5(v, p5);
15819 }else{
15820 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
15821 }
15822 return iReg;
15823 }
15824 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnToReg(
15825 Parse *pParse, /* Parsing and code generating context */
15826 Table *pTab, /* Description of the table we are reading from */
15827 int iColumn, /* Index of the table column */
15828 int iTable, /* The cursor pointing to the table */
15829 int iReg /* Store results here */
15830 ){
15831 int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0);
15832 if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg);
15833 }
15834
15835
15836 /*
15837 ** Clear all column cache entries.
15838 */
15839 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
15840 int i;
15841
15842 #if SQLITE_DEBUG
15843 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
15844 printf("CLEAR\n");
15845 }
15846 #endif
15847 for(i=0; i<pParse->nColCache; i++){
15848 if( pParse->aColCache[i].tempReg
15849 && pParse->nTempReg<ArraySize(pParse->aTempReg)
15850 ){
15851 pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
15852 }
15853 }
15854 pParse->nColCache = 0;
15855 }
15856
15857 /*
15858 ** Record the fact that an affinity change has occurred on iCount
15859 ** registers starting with iStart.
15860 */
15861 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, in t iCount){
15862 sqlite3ExprCacheRemove(pParse, iStart, iCount);
15863 }
15864
15865 /*
15866 ** Generate code to move content from registers iFrom...iFrom+nReg-1
15867 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
15868 */
15869 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int n Reg){
15870 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
15871 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
15872 sqlite3ExprCacheRemove(pParse, iFrom, nReg);
15873 }
15874
15875 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
15876 /*
15877 ** Return true if any register in the range iFrom..iTo (inclusive)
15878 ** is used as part of the column cache.
15879 **
15880 ** This routine is used within assert() and testcase() macros only
15881 ** and does not appear in a normal build.
15882 */
15883 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
15884 int i;
15885 struct yColCache *p;
15886 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
15887 int r = p->iReg;
15888 if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/
15889 }
15890 return 0;
15891 }
15892 #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
15893
15894
15895 /*
15896 ** Convert a scalar expression node to a TK_REGISTER referencing
15897 ** register iReg. The caller must ensure that iReg already contains
15898 ** the correct value for the expression.
15899 */
15900 static void exprToRegister(Expr *p, int iReg){
15901 p->op2 = p->op;
15902 p->op = TK_REGISTER;
15903 p->iTable = iReg;
15904 ExprClearProperty(p, EP_Skip);
15905 }
15906
15907 /*
15908 ** Evaluate an expression (either a vector or a scalar expression) and store
15909 ** the result in continguous temporary registers. Return the index of
15910 ** the first register used to store the result.
15911 **
15912 ** If the returned result register is a temporary scalar, then also write
15913 ** that register number into *piFreeable. If the returned result register
15914 ** is not a temporary or if the expression is a vector set *piFreeable
15915 ** to 0.
15916 */
15917 static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
15918 int iResult;
15919 int nResult = sqlite3ExprVectorSize(p);
15920 if( nResult==1 ){
15921 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
15922 }else{
15923 *piFreeable = 0;
15924 if( p->op==TK_SELECT ){
15925 iResult = sqlite3CodeSubselect(pParse, p, 0, 0);
15926 }else{
15927 int i;
15928 iResult = pParse->nMem+1;
15929 pParse->nMem += nResult;
15930 for(i=0; i<nResult; i++){
15931 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
15932 }
15933 }
15934 }
15935 return iResult;
15936 }
15937
15938
15939 /*
15940 ** Generate code into the current Vdbe to evaluate the given
15941 ** expression. Attempt to store the results in register "target".
15942 ** Return the register where results are stored.
15943 **
15944 ** With this routine, there is no guarantee that results will
15945 ** be stored in target. The result might be stored in some other
15946 ** register if it is convenient to do so. The calling function
15947 ** must check the return code and move the results to the desired
15948 ** register.
15949 */
15950 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target) {
15951 Vdbe *v = pParse->pVdbe; /* The VM under construction */
15952 int op; /* The opcode being coded */
15953 int inReg = target; /* Results stored in register inReg */
15954 int regFree1 = 0; /* If non-zero free this temporary register */
15955 int regFree2 = 0; /* If non-zero free this temporary register */
15956 int r1, r2; /* Various register numbers */
15957 Expr tempX; /* Temporary expression node */
15958 int p5 = 0;
15959
15960 assert( target>0 && target<=pParse->nMem );
15961 if( v==0 ){
15962 assert( pParse->db->mallocFailed );
15963 return 0;
15964 }
15965
15966 if( pExpr==0 ){
15967 op = TK_NULL;
15968 }else{
15969 op = pExpr->op;
15970 }
15971 switch( op ){
15972 case TK_AGG_COLUMN: {
15973 AggInfo *pAggInfo = pExpr->pAggInfo;
15974 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
15975 if( !pAggInfo->directMode ){
15976 assert( pCol->iMem>0 );
15977 return pCol->iMem;
15978 }else if( pAggInfo->useSortingIdx ){
15979 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
15980 pCol->iSorterColumn, target);
15981 return target;
15982 }
15983 /* Otherwise, fall thru into the TK_COLUMN case */
15984 }
15985 case TK_COLUMN: {
15986 int iTab = pExpr->iTable;
15987 if( iTab<0 ){
15988 if( pParse->ckBase>0 ){
15989 /* Generating CHECK constraints or inserting into partial index */
15990 return pExpr->iColumn + pParse->ckBase;
15991 }else{
15992 /* Coding an expression that is part of an index where column names
15993 ** in the index refer to the table to which the index belongs */
15994 iTab = pParse->iSelfTab;
15995 }
15996 }
15997 return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
15998 pExpr->iColumn, iTab, target,
15999 pExpr->op2);
16000 }
16001 case TK_INTEGER: {
16002 codeInteger(pParse, pExpr, 0, target);
16003 return target;
16004 }
16005 #ifndef SQLITE_OMIT_FLOATING_POINT
16006 case TK_FLOAT: {
16007 assert( !ExprHasProperty(pExpr, EP_IntValue) );
16008 codeReal(v, pExpr->u.zToken, 0, target);
16009 return target;
16010 }
16011 #endif
16012 case TK_STRING: {
16013 assert( !ExprHasProperty(pExpr, EP_IntValue) );
16014 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
16015 return target;
16016 }
16017 case TK_NULL: {
16018 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
16019 return target;
16020 }
16021 #ifndef SQLITE_OMIT_BLOB_LITERAL
16022 case TK_BLOB: {
16023 int n;
16024 const char *z;
16025 char *zBlob;
16026 assert( !ExprHasProperty(pExpr, EP_IntValue) );
16027 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
16028 assert( pExpr->u.zToken[1]=='\'' );
16029 z = &pExpr->u.zToken[2];
16030 n = sqlite3Strlen30(z) - 1;
16031 assert( z[n]=='\'' );
16032 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
16033 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
16034 return target;
16035 }
16036 #endif
16037 case TK_VARIABLE: {
16038 assert( !ExprHasProperty(pExpr, EP_IntValue) );
16039 assert( pExpr->u.zToken!=0 );
16040 assert( pExpr->u.zToken[0]!=0 );
16041 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
16042 if( pExpr->u.zToken[1]!=0 ){
16043 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
16044 assert( pExpr->u.zToken[0]=='?' || strcmp(pExpr->u.zToken, z)==0 );
16045 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
16046 sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC);
16047 }
16048 return target;
16049 }
16050 case TK_REGISTER: {
16051 return pExpr->iTable;
16052 }
16053 #ifndef SQLITE_OMIT_CAST
16054 case TK_CAST: {
16055 /* Expressions of the form: CAST(pLeft AS token) */
16056 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
16057 if( inReg!=target ){
16058 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
16059 inReg = target;
16060 }
16061 sqlite3VdbeAddOp2(v, OP_Cast, target,
16062 sqlite3AffinityType(pExpr->u.zToken, 0));
16063 testcase( usedAsColumnCache(pParse, inReg, inReg) );
16064 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
16065 return inReg;
16066 }
16067 #endif /* SQLITE_OMIT_CAST */
16068 case TK_IS:
16069 case TK_ISNOT:
16070 op = (op==TK_IS) ? TK_EQ : TK_NE;
16071 p5 = SQLITE_NULLEQ;
16072 /* fall-through */
16073 case TK_LT:
16074 case TK_LE:
16075 case TK_GT:
16076 case TK_GE:
16077 case TK_NE:
16078 case TK_EQ: {
16079 Expr *pLeft = pExpr->pLeft;
16080 if( sqlite3ExprIsVector(pLeft) ){
16081 codeVectorCompare(pParse, pExpr, target, op, p5);
16082 }else{
16083 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
16084 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
16085 codeCompare(pParse, pLeft, pExpr->pRight, op,
16086 r1, r2, inReg, SQLITE_STOREP2 | p5);
16087 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
16088 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
16089 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
16090 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
16091 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
16092 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
16093 testcase( regFree1==0 );
16094 testcase( regFree2==0 );
16095 }
16096 break;
16097 }
16098 case TK_AND:
16099 case TK_OR:
16100 case TK_PLUS:
16101 case TK_STAR:
16102 case TK_MINUS:
16103 case TK_REM:
16104 case TK_BITAND:
16105 case TK_BITOR:
16106 case TK_SLASH:
16107 case TK_LSHIFT:
16108 case TK_RSHIFT:
16109 case TK_CONCAT: {
16110 assert( TK_AND==OP_And ); testcase( op==TK_AND );
16111 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
16112 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
16113 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
16114 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
16115 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
16116 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
16117 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
16118 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
16119 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
16120 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
16121 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
16122 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
16123 sqlite3VdbeAddOp3(v, op, r2, r1, target);
16124 testcase( regFree1==0 );
16125 testcase( regFree2==0 );
16126 break;
16127 }
16128 case TK_UMINUS: {
16129 Expr *pLeft = pExpr->pLeft;
16130 assert( pLeft );
16131 if( pLeft->op==TK_INTEGER ){
16132 codeInteger(pParse, pLeft, 1, target);
16133 return target;
16134 #ifndef SQLITE_OMIT_FLOATING_POINT
16135 }else if( pLeft->op==TK_FLOAT ){
16136 assert( !ExprHasProperty(pExpr, EP_IntValue) );
16137 codeReal(v, pLeft->u.zToken, 1, target);
16138 return target;
16139 #endif
16140 }else{
16141 tempX.op = TK_INTEGER;
16142 tempX.flags = EP_IntValue|EP_TokenOnly;
16143 tempX.u.iValue = 0;
16144 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
16145 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
16146 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
16147 testcase( regFree2==0 );
16148 }
16149 break;
16150 }
16151 case TK_BITNOT:
16152 case TK_NOT: {
16153 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
16154 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
16155 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
16156 testcase( regFree1==0 );
16157 sqlite3VdbeAddOp2(v, op, r1, inReg);
16158 break;
16159 }
16160 case TK_ISNULL:
16161 case TK_NOTNULL: {
16162 int addr;
16163 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
16164 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
16165 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
16166 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
16167 testcase( regFree1==0 );
16168 addr = sqlite3VdbeAddOp1(v, op, r1);
16169 VdbeCoverageIf(v, op==TK_ISNULL);
16170 VdbeCoverageIf(v, op==TK_NOTNULL);
16171 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
16172 sqlite3VdbeJumpHere(v, addr);
16173 break;
16174 }
16175 case TK_AGG_FUNCTION: {
16176 AggInfo *pInfo = pExpr->pAggInfo;
16177 if( pInfo==0 ){
16178 assert( !ExprHasProperty(pExpr, EP_IntValue) );
16179 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
16180 }else{
16181 return pInfo->aFunc[pExpr->iAgg].iMem;
16182 }
16183 break;
16184 }
16185 case TK_FUNCTION: {
16186 ExprList *pFarg; /* List of function arguments */
16187 int nFarg; /* Number of function arguments */
16188 FuncDef *pDef; /* The function definition object */
16189 const char *zId; /* The function name */
16190 u32 constMask = 0; /* Mask of function arguments that are constant */
16191 int i; /* Loop counter */
16192 sqlite3 *db = pParse->db; /* The database connection */
16193 u8 enc = ENC(db); /* The text encoding used by this database */
16194 CollSeq *pColl = 0; /* A collating sequence */
16195
16196 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
16197 /* SQL functions can be expensive. So try to move constant functions
16198 ** out of the inner loop, even if that means an extra OP_Copy. */
16199 return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
16200 }
16201 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
16202 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
16203 pFarg = 0;
16204 }else{
16205 pFarg = pExpr->x.pList;
16206 }
16207 nFarg = pFarg ? pFarg->nExpr : 0;
16208 assert( !ExprHasProperty(pExpr, EP_IntValue) );
16209 zId = pExpr->u.zToken;
16210 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
16211 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
16212 if( pDef==0 && pParse->explain ){
16213 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
16214 }
16215 #endif
16216 if( pDef==0 || pDef->xFinalize!=0 ){
16217 sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
16218 break;
16219 }
16220
16221 /* Attempt a direct implementation of the built-in COALESCE() and
16222 ** IFNULL() functions. This avoids unnecessary evaluation of
16223 ** arguments past the first non-NULL argument.
16224 */
16225 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
16226 int endCoalesce = sqlite3VdbeMakeLabel(v);
16227 assert( nFarg>=2 );
16228 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
16229 for(i=1; i<nFarg; i++){
16230 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
16231 VdbeCoverage(v);
16232 sqlite3ExprCacheRemove(pParse, target, 1);
16233 sqlite3ExprCachePush(pParse);
16234 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
16235 sqlite3ExprCachePop(pParse);
16236 }
16237 sqlite3VdbeResolveLabel(v, endCoalesce);
16238 break;
16239 }
16240
16241 /* The UNLIKELY() function is a no-op. The result is the value
16242 ** of the first argument.
16243 */
16244 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
16245 assert( nFarg>=1 );
16246 return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
16247 }
16248
16249 #ifdef SQLITE_DEBUG
16250 /* The AFFINITY() function evaluates to a string that describes
16251 ** the type affinity of the argument. This is used for testing of
16252 ** the SQLite type logic.
16253 */
16254 if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
16255 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
16256 char aff;
16257 assert( nFarg==1 );
16258 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
16259 sqlite3VdbeLoadString(v, target,
16260 aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
16261 return target;
16262 }
16263 #endif
16264
16265 for(i=0; i<nFarg; i++){
16266 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
16267 testcase( i==31 );
16268 constMask |= MASKBIT32(i);
16269 }
16270 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
16271 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
16272 }
16273 }
16274 if( pFarg ){
16275 if( constMask ){
16276 r1 = pParse->nMem+1;
16277 pParse->nMem += nFarg;
16278 }else{
16279 r1 = sqlite3GetTempRange(pParse, nFarg);
16280 }
16281
16282 /* For length() and typeof() functions with a column argument,
16283 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
16284 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
16285 ** loading.
16286 */
16287 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
16288 u8 exprOp;
16289 assert( nFarg==1 );
16290 assert( pFarg->a[0].pExpr!=0 );
16291 exprOp = pFarg->a[0].pExpr->op;
16292 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
16293 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
16294 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
16295 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
16296 pFarg->a[0].pExpr->op2 =
16297 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
16298 }
16299 }
16300
16301 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */
16302 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
16303 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
16304 sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */
16305 }else{
16306 r1 = 0;
16307 }
16308 #ifndef SQLITE_OMIT_VIRTUALTABLE
16309 /* Possibly overload the function if the first argument is
16310 ** a virtual table column.
16311 **
16312 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
16313 ** second argument, not the first, as the argument to test to
16314 ** see if it is a column in a virtual table. This is done because
16315 ** the left operand of infix functions (the operand we want to
16316 ** control overloading) ends up as the second argument to the
16317 ** function. The expression "A glob B" is equivalent to
16318 ** "glob(B,A). We want to use the A in "A glob B" to test
16319 ** for function overloading. But we use the B term in "glob(B,A)".
16320 */
16321 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
16322 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
16323 }else if( nFarg>0 ){
16324 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
16325 }
16326 #endif
16327 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
16328 if( !pColl ) pColl = db->pDfltColl;
16329 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
16330 }
16331 sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
16332 (char*)pDef, P4_FUNCDEF);
16333 sqlite3VdbeChangeP5(v, (u8)nFarg);
16334 if( nFarg && constMask==0 ){
16335 sqlite3ReleaseTempRange(pParse, r1, nFarg);
16336 }
16337 return target;
16338 }
16339 #ifndef SQLITE_OMIT_SUBQUERY
16340 case TK_EXISTS:
16341 case TK_SELECT: {
16342 int nCol;
16343 testcase( op==TK_EXISTS );
16344 testcase( op==TK_SELECT );
16345 if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
16346 sqlite3SubselectError(pParse, nCol, 1);
16347 }else{
16348 return sqlite3CodeSubselect(pParse, pExpr, 0, 0);
16349 }
16350 break;
16351 }
16352 case TK_SELECT_COLUMN: {
16353 int n;
16354 if( pExpr->pLeft->iTable==0 ){
16355 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft, 0, 0);
16356 }
16357 assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
16358 if( pExpr->iTable
16359 && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
16360 ){
16361 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
16362 pExpr->iTable, n);
16363 }
16364 return pExpr->pLeft->iTable + pExpr->iColumn;
16365 }
16366 case TK_IN: {
16367 int destIfFalse = sqlite3VdbeMakeLabel(v);
16368 int destIfNull = sqlite3VdbeMakeLabel(v);
16369 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
16370 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
16371 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
16372 sqlite3VdbeResolveLabel(v, destIfFalse);
16373 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
16374 sqlite3VdbeResolveLabel(v, destIfNull);
16375 return target;
16376 }
16377 #endif /* SQLITE_OMIT_SUBQUERY */
16378
16379
16380 /*
16381 ** x BETWEEN y AND z
16382 **
16383 ** This is equivalent to
16384 **
16385 ** x>=y AND x<=z
16386 **
16387 ** X is stored in pExpr->pLeft.
16388 ** Y is stored in pExpr->pList->a[0].pExpr.
16389 ** Z is stored in pExpr->pList->a[1].pExpr.
16390 */
16391 case TK_BETWEEN: {
16392 exprCodeBetween(pParse, pExpr, target, 0, 0);
16393 return target;
16394 }
16395 case TK_SPAN:
16396 case TK_COLLATE:
16397 case TK_UPLUS: {
16398 return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
16399 }
16400
16401 case TK_TRIGGER: {
16402 /* If the opcode is TK_TRIGGER, then the expression is a reference
16403 ** to a column in the new.* or old.* pseudo-tables available to
16404 ** trigger programs. In this case Expr.iTable is set to 1 for the
16405 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
16406 ** is set to the column of the pseudo-table to read, or to -1 to
16407 ** read the rowid field.
16408 **
16409 ** The expression is implemented using an OP_Param opcode. The p1
16410 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
16411 ** to reference another column of the old.* pseudo-table, where
16412 ** i is the index of the column. For a new.rowid reference, p1 is
16413 ** set to (n+1), where n is the number of columns in each pseudo-table.
16414 ** For a reference to any other column in the new.* pseudo-table, p1
16415 ** is set to (n+2+i), where n and i are as defined previously. For
16416 ** example, if the table on which triggers are being fired is
16417 ** declared as:
16418 **
16419 ** CREATE TABLE t1(a, b);
16420 **
16421 ** Then p1 is interpreted as follows:
16422 **
16423 ** p1==0 -> old.rowid p1==3 -> new.rowid
16424 ** p1==1 -> old.a p1==4 -> new.a
16425 ** p1==2 -> old.b p1==5 -> new.b
16426 */
16427 Table *pTab = pExpr->pTab;
16428 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
16429
16430 assert( pExpr->iTable==0 || pExpr->iTable==1 );
16431 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
16432 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
16433 assert( p1>=0 && p1<(pTab->nCol*2+2) );
16434
16435 sqlite3VdbeAddOp2(v, OP_Param, p1, target);
16436 VdbeComment((v, "%s.%s -> $%d",
16437 (pExpr->iTable ? "new" : "old"),
16438 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
16439 target
16440 ));
16441
16442 #ifndef SQLITE_OMIT_FLOATING_POINT
16443 /* If the column has REAL affinity, it may currently be stored as an
16444 ** integer. Use OP_RealAffinity to make sure it is really real.
16445 **
16446 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
16447 ** floating point when extracting it from the record. */
16448 if( pExpr->iColumn>=0
16449 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
16450 ){
16451 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
16452 }
16453 #endif
16454 break;
16455 }
16456
16457 case TK_VECTOR: {
16458 sqlite3ErrorMsg(pParse, "row value misused");
16459 break;
16460 }
16461
16462 /*
16463 ** Form A:
16464 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
16465 **
16466 ** Form B:
16467 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
16468 **
16469 ** Form A is can be transformed into the equivalent form B as follows:
16470 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
16471 ** WHEN x=eN THEN rN ELSE y END
16472 **
16473 ** X (if it exists) is in pExpr->pLeft.
16474 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
16475 ** odd. The Y is also optional. If the number of elements in x.pList
16476 ** is even, then Y is omitted and the "otherwise" result is NULL.
16477 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
16478 **
16479 ** The result of the expression is the Ri for the first matching Ei,
16480 ** or if there is no matching Ei, the ELSE term Y, or if there is
16481 ** no ELSE term, NULL.
16482 */
16483 default: assert( op==TK_CASE ); {
16484 int endLabel; /* GOTO label for end of CASE stmt */
16485 int nextCase; /* GOTO label for next WHEN clause */
16486 int nExpr; /* 2x number of WHEN terms */
16487 int i; /* Loop counter */
16488 ExprList *pEList; /* List of WHEN terms */
16489 struct ExprList_item *aListelem; /* Array of WHEN terms */
16490 Expr opCompare; /* The X==Ei expression */
16491 Expr *pX; /* The X expression */
16492 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
16493 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
16494
16495 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
16496 assert(pExpr->x.pList->nExpr > 0);
16497 pEList = pExpr->x.pList;
16498 aListelem = pEList->a;
16499 nExpr = pEList->nExpr;
16500 endLabel = sqlite3VdbeMakeLabel(v);
16501 if( (pX = pExpr->pLeft)!=0 ){
16502 tempX = *pX;
16503 testcase( pX->op==TK_COLUMN );
16504 exprToRegister(&tempX, exprCodeVector(pParse, &tempX, &regFree1));
16505 testcase( regFree1==0 );
16506 memset(&opCompare, 0, sizeof(opCompare));
16507 opCompare.op = TK_EQ;
16508 opCompare.pLeft = &tempX;
16509 pTest = &opCompare;
16510 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
16511 ** The value in regFree1 might get SCopy-ed into the file result.
16512 ** So make sure that the regFree1 register is not reused for other
16513 ** purposes and possibly overwritten. */
16514 regFree1 = 0;
16515 }
16516 for(i=0; i<nExpr-1; i=i+2){
16517 sqlite3ExprCachePush(pParse);
16518 if( pX ){
16519 assert( pTest!=0 );
16520 opCompare.pRight = aListelem[i].pExpr;
16521 }else{
16522 pTest = aListelem[i].pExpr;
16523 }
16524 nextCase = sqlite3VdbeMakeLabel(v);
16525 testcase( pTest->op==TK_COLUMN );
16526 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
16527 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
16528 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
16529 sqlite3VdbeGoto(v, endLabel);
16530 sqlite3ExprCachePop(pParse);
16531 sqlite3VdbeResolveLabel(v, nextCase);
16532 }
16533 if( (nExpr&1)!=0 ){
16534 sqlite3ExprCachePush(pParse);
16535 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
16536 sqlite3ExprCachePop(pParse);
16537 }else{
16538 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
16539 }
16540 assert( pParse->db->mallocFailed || pParse->nErr>0
16541 || pParse->iCacheLevel==iCacheLevel );
16542 sqlite3VdbeResolveLabel(v, endLabel);
16543 break;
16544 }
16545 #ifndef SQLITE_OMIT_TRIGGER
16546 case TK_RAISE: {
16547 assert( pExpr->affinity==OE_Rollback
16548 || pExpr->affinity==OE_Abort
16549 || pExpr->affinity==OE_Fail
16550 || pExpr->affinity==OE_Ignore
16551 );
16552 if( !pParse->pTriggerTab ){
16553 sqlite3ErrorMsg(pParse,
16554 "RAISE() may only be used within a trigger-program");
16555 return 0;
16556 }
16557 if( pExpr->affinity==OE_Abort ){
16558 sqlite3MayAbort(pParse);
16559 }
16560 assert( !ExprHasProperty(pExpr, EP_IntValue) );
16561 if( pExpr->affinity==OE_Ignore ){
16562 sqlite3VdbeAddOp4(
16563 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
16564 VdbeCoverage(v);
16565 }else{
16566 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
16567 pExpr->affinity, pExpr->u.zToken, 0, 0);
16568 }
16569
16570 break;
16571 }
16572 #endif
16573 }
16574 sqlite3ReleaseTempReg(pParse, regFree1);
16575 sqlite3ReleaseTempReg(pParse, regFree2);
16576 return inReg;
16577 }
16578
16579 /*
16580 ** Factor out the code of the given expression to initialization time.
16581 **
16582 ** If regDest>=0 then the result is always stored in that register and the
16583 ** result is not reusable. If regDest<0 then this routine is free to
16584 ** store the value whereever it wants. The register where the expression
16585 ** is stored is returned. When regDest<0, two identical expressions will
16586 ** code to the same register.
16587 */
16588 SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
16589 Parse *pParse, /* Parsing context */
16590 Expr *pExpr, /* The expression to code when the VDBE initializes */
16591 int regDest /* Store the value in this register */
16592 ){
16593 ExprList *p;
16594 assert( ConstFactorOk(pParse) );
16595 p = pParse->pConstExpr;
16596 if( regDest<0 && p ){
16597 struct ExprList_item *pItem;
16598 int i;
16599 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
16600 if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
16601 return pItem->u.iConstExprReg;
16602 }
16603 }
16604 }
16605 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
16606 p = sqlite3ExprListAppend(pParse, p, pExpr);
16607 if( p ){
16608 struct ExprList_item *pItem = &p->a[p->nExpr-1];
16609 pItem->reusable = regDest<0;
16610 if( regDest<0 ) regDest = ++pParse->nMem;
16611 pItem->u.iConstExprReg = regDest;
16612 }
16613 pParse->pConstExpr = p;
16614 return regDest;
16615 }
16616
16617 /*
16618 ** Generate code to evaluate an expression and store the results
16619 ** into a register. Return the register number where the results
16620 ** are stored.
16621 **
16622 ** If the register is a temporary register that can be deallocated,
16623 ** then write its number into *pReg. If the result register is not
16624 ** a temporary, then set *pReg to zero.
16625 **
16626 ** If pExpr is a constant, then this routine might generate this
16627 ** code to fill the register in the initialization section of the
16628 ** VDBE program, in order to factor it out of the evaluation loop.
16629 */
16630 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
16631 int r2;
16632 pExpr = sqlite3ExprSkipCollate(pExpr);
16633 if( ConstFactorOk(pParse)
16634 && pExpr->op!=TK_REGISTER
16635 && sqlite3ExprIsConstantNotJoin(pExpr)
16636 ){
16637 *pReg = 0;
16638 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
16639 }else{
16640 int r1 = sqlite3GetTempReg(pParse);
16641 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
16642 if( r2==r1 ){
16643 *pReg = r1;
16644 }else{
16645 sqlite3ReleaseTempReg(pParse, r1);
16646 *pReg = 0;
16647 }
16648 }
16649 return r2;
16650 }
16651
16652 /*
16653 ** Generate code that will evaluate expression pExpr and store the
16654 ** results in register target. The results are guaranteed to appear
16655 ** in register target.
16656 */
16657 SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
16658 int inReg;
16659
16660 assert( target>0 && target<=pParse->nMem );
16661 if( pExpr && pExpr->op==TK_REGISTER ){
16662 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
16663 }else{
16664 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
16665 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
16666 if( inReg!=target && pParse->pVdbe ){
16667 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
16668 }
16669 }
16670 }
16671
16672 /*
16673 ** Make a transient copy of expression pExpr and then code it using
16674 ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
16675 ** except that the input expression is guaranteed to be unchanged.
16676 */
16677 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
16678 sqlite3 *db = pParse->db;
16679 pExpr = sqlite3ExprDup(db, pExpr, 0);
16680 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
16681 sqlite3ExprDelete(db, pExpr);
16682 }
16683
16684 /*
16685 ** Generate code that will evaluate expression pExpr and store the
16686 ** results in register target. The results are guaranteed to appear
16687 ** in register target. If the expression is constant, then this routine
16688 ** might choose to code the expression at initialization time.
16689 */
16690 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int ta rget){
16691 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
16692 sqlite3ExprCodeAtInit(pParse, pExpr, target);
16693 }else{
16694 sqlite3ExprCode(pParse, pExpr, target);
16695 }
16696 }
16697
16698 /*
16699 ** Generate code that evaluates the given expression and puts the result
16700 ** in register target.
16701 **
16702 ** Also make a copy of the expression results into another "cache" register
16703 ** and modify the expression so that the next time it is evaluated,
16704 ** the result is a copy of the cache register.
16705 **
16706 ** This routine is used for expressions that are used multiple
16707 ** times. They are evaluated once and the results of the expression
16708 ** are reused.
16709 */
16710 SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int targ et){
16711 Vdbe *v = pParse->pVdbe;
16712 int iMem;
16713
16714 assert( target>0 );
16715 assert( pExpr->op!=TK_REGISTER );
16716 sqlite3ExprCode(pParse, pExpr, target);
16717 iMem = ++pParse->nMem;
16718 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
16719 exprToRegister(pExpr, iMem);
16720 }
16721
16722 /*
16723 ** Generate code that pushes the value of every element of the given
16724 ** expression list into a sequence of registers beginning at target.
16725 **
16726 ** Return the number of elements evaluated.
16727 **
16728 ** The SQLITE_ECEL_DUP flag prevents the arguments from being
16729 ** filled using OP_SCopy. OP_Copy must be used instead.
16730 **
16731 ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
16732 ** factored out into initialization code.
16733 **
16734 ** The SQLITE_ECEL_REF flag means that expressions in the list with
16735 ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
16736 ** in registers at srcReg, and so the value can be copied from there.
16737 */
16738 SQLITE_PRIVATE int sqlite3ExprCodeExprList(
16739 Parse *pParse, /* Parsing context */
16740 ExprList *pList, /* The expression list to be coded */
16741 int target, /* Where to write results */
16742 int srcReg, /* Source registers if SQLITE_ECEL_REF */
16743 u8 flags /* SQLITE_ECEL_* flags */
16744 ){
16745 struct ExprList_item *pItem;
16746 int i, j, n;
16747 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
16748 Vdbe *v = pParse->pVdbe;
16749 assert( pList!=0 );
16750 assert( target>0 );
16751 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
16752 n = pList->nExpr;
16753 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
16754 for(pItem=pList->a, i=0; i<n; i++, pItem++){
16755 Expr *pExpr = pItem->pExpr;
16756 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
16757 if( flags & SQLITE_ECEL_OMITREF ){
16758 i--;
16759 n--;
16760 }else{
16761 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
16762 }
16763 }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
16764 sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
16765 }else{
16766 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
16767 if( inReg!=target+i ){
16768 VdbeOp *pOp;
16769 if( copyOp==OP_Copy
16770 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
16771 && pOp->p1+pOp->p3+1==inReg
16772 && pOp->p2+pOp->p3+1==target+i
16773 ){
16774 pOp->p3++;
16775 }else{
16776 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
16777 }
16778 }
16779 }
16780 }
16781 return n;
16782 }
16783
16784 /*
16785 ** Generate code for a BETWEEN operator.
16786 **
16787 ** x BETWEEN y AND z
16788 **
16789 ** The above is equivalent to
16790 **
16791 ** x>=y AND x<=z
16792 **
16793 ** Code it as such, taking care to do the common subexpression
16794 ** elimination of x.
16795 **
16796 ** The xJumpIf parameter determines details:
16797 **
16798 ** NULL: Store the boolean result in reg[dest]
16799 ** sqlite3ExprIfTrue: Jump to dest if true
16800 ** sqlite3ExprIfFalse: Jump to dest if false
16801 **
16802 ** The jumpIfNull parameter is ignored if xJumpIf is NULL.
16803 */
16804 static void exprCodeBetween(
16805 Parse *pParse, /* Parsing and code generating context */
16806 Expr *pExpr, /* The BETWEEN expression */
16807 int dest, /* Jump destination or storage location */
16808 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
16809 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
16810 ){
16811 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
16812 Expr compLeft; /* The x>=y term */
16813 Expr compRight; /* The x<=z term */
16814 Expr exprX; /* The x subexpression */
16815 int regFree1 = 0; /* Temporary use register */
16816
16817
16818 memset(&compLeft, 0, sizeof(Expr));
16819 memset(&compRight, 0, sizeof(Expr));
16820 memset(&exprAnd, 0, sizeof(Expr));
16821
16822 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
16823 exprX = *pExpr->pLeft;
16824 exprAnd.op = TK_AND;
16825 exprAnd.pLeft = &compLeft;
16826 exprAnd.pRight = &compRight;
16827 compLeft.op = TK_GE;
16828 compLeft.pLeft = &exprX;
16829 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
16830 compRight.op = TK_LE;
16831 compRight.pLeft = &exprX;
16832 compRight.pRight = pExpr->x.pList->a[1].pExpr;
16833 exprToRegister(&exprX, exprCodeVector(pParse, &exprX, &regFree1));
16834 if( xJump ){
16835 xJump(pParse, &exprAnd, dest, jumpIfNull);
16836 }else{
16837 /* Mark the expression is being from the ON or USING clause of a join
16838 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
16839 ** it into the Parse.pConstExpr list. We should use a new bit for this,
16840 ** for clarity, but we are out of bits in the Expr.flags field so we
16841 ** have to reuse the EP_FromJoin bit. Bummer. */
16842 exprX.flags |= EP_FromJoin;
16843 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
16844 }
16845 sqlite3ReleaseTempReg(pParse, regFree1);
16846
16847 /* Ensure adequate test coverage */
16848 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
16849 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
16850 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
16851 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
16852 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
16853 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
16854 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
16855 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
16856 testcase( xJump==0 );
16857 }
16858
16859 /*
16860 ** Generate code for a boolean expression such that a jump is made
16861 ** to the label "dest" if the expression is true but execution
16862 ** continues straight thru if the expression is false.
16863 **
16864 ** If the expression evaluates to NULL (neither true nor false), then
16865 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
16866 **
16867 ** This code depends on the fact that certain token values (ex: TK_EQ)
16868 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
16869 ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
16870 ** the make process cause these values to align. Assert()s in the code
16871 ** below verify that the numbers are aligned correctly.
16872 */
16873 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
16874 Vdbe *v = pParse->pVdbe;
16875 int op = 0;
16876 int regFree1 = 0;
16877 int regFree2 = 0;
16878 int r1, r2;
16879
16880 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
16881 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
16882 if( NEVER(pExpr==0) ) return; /* No way this can happen */
16883 op = pExpr->op;
16884 switch( op ){
16885 case TK_AND: {
16886 int d2 = sqlite3VdbeMakeLabel(v);
16887 testcase( jumpIfNull==0 );
16888 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
16889 sqlite3ExprCachePush(pParse);
16890 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
16891 sqlite3VdbeResolveLabel(v, d2);
16892 sqlite3ExprCachePop(pParse);
16893 break;
16894 }
16895 case TK_OR: {
16896 testcase( jumpIfNull==0 );
16897 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
16898 sqlite3ExprCachePush(pParse);
16899 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
16900 sqlite3ExprCachePop(pParse);
16901 break;
16902 }
16903 case TK_NOT: {
16904 testcase( jumpIfNull==0 );
16905 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
16906 break;
16907 }
16908 case TK_IS:
16909 case TK_ISNOT:
16910 testcase( op==TK_IS );
16911 testcase( op==TK_ISNOT );
16912 op = (op==TK_IS) ? TK_EQ : TK_NE;
16913 jumpIfNull = SQLITE_NULLEQ;
16914 /* Fall thru */
16915 case TK_LT:
16916 case TK_LE:
16917 case TK_GT:
16918 case TK_GE:
16919 case TK_NE:
16920 case TK_EQ: {
16921 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
16922 testcase( jumpIfNull==0 );
16923 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
16924 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
16925 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
16926 r1, r2, dest, jumpIfNull);
16927 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
16928 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
16929 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
16930 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
16931 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
16932 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
16933 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
16934 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
16935 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
16936 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
16937 testcase( regFree1==0 );
16938 testcase( regFree2==0 );
16939 break;
16940 }
16941 case TK_ISNULL:
16942 case TK_NOTNULL: {
16943 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
16944 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
16945 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
16946 sqlite3VdbeAddOp2(v, op, r1, dest);
16947 VdbeCoverageIf(v, op==TK_ISNULL);
16948 VdbeCoverageIf(v, op==TK_NOTNULL);
16949 testcase( regFree1==0 );
16950 break;
16951 }
16952 case TK_BETWEEN: {
16953 testcase( jumpIfNull==0 );
16954 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
16955 break;
16956 }
16957 #ifndef SQLITE_OMIT_SUBQUERY
16958 case TK_IN: {
16959 int destIfFalse = sqlite3VdbeMakeLabel(v);
16960 int destIfNull = jumpIfNull ? dest : destIfFalse;
16961 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
16962 sqlite3VdbeGoto(v, dest);
16963 sqlite3VdbeResolveLabel(v, destIfFalse);
16964 break;
16965 }
16966 #endif
16967 default: {
16968 default_expr:
16969 if( exprAlwaysTrue(pExpr) ){
16970 sqlite3VdbeGoto(v, dest);
16971 }else if( exprAlwaysFalse(pExpr) ){
16972 /* No-op */
16973 }else{
16974 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
16975 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
16976 VdbeCoverage(v);
16977 testcase( regFree1==0 );
16978 testcase( jumpIfNull==0 );
16979 }
16980 break;
16981 }
16982 }
16983 sqlite3ReleaseTempReg(pParse, regFree1);
16984 sqlite3ReleaseTempReg(pParse, regFree2);
16985 }
16986
16987 /*
16988 ** Generate code for a boolean expression such that a jump is made
16989 ** to the label "dest" if the expression is false but execution
16990 ** continues straight thru if the expression is true.
16991 **
16992 ** If the expression evaluates to NULL (neither true nor false) then
16993 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
16994 ** is 0.
16995 */
16996 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
16997 Vdbe *v = pParse->pVdbe;
16998 int op = 0;
16999 int regFree1 = 0;
17000 int regFree2 = 0;
17001 int r1, r2;
17002
17003 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
17004 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
17005 if( pExpr==0 ) return;
17006
17007 /* The value of pExpr->op and op are related as follows:
17008 **
17009 ** pExpr->op op
17010 ** --------- ----------
17011 ** TK_ISNULL OP_NotNull
17012 ** TK_NOTNULL OP_IsNull
17013 ** TK_NE OP_Eq
17014 ** TK_EQ OP_Ne
17015 ** TK_GT OP_Le
17016 ** TK_LE OP_Gt
17017 ** TK_GE OP_Lt
17018 ** TK_LT OP_Ge
17019 **
17020 ** For other values of pExpr->op, op is undefined and unused.
17021 ** The value of TK_ and OP_ constants are arranged such that we
17022 ** can compute the mapping above using the following expression.
17023 ** Assert()s verify that the computation is correct.
17024 */
17025 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
17026
17027 /* Verify correct alignment of TK_ and OP_ constants
17028 */
17029 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
17030 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
17031 assert( pExpr->op!=TK_NE || op==OP_Eq );
17032 assert( pExpr->op!=TK_EQ || op==OP_Ne );
17033 assert( pExpr->op!=TK_LT || op==OP_Ge );
17034 assert( pExpr->op!=TK_LE || op==OP_Gt );
17035 assert( pExpr->op!=TK_GT || op==OP_Le );
17036 assert( pExpr->op!=TK_GE || op==OP_Lt );
17037
17038 switch( pExpr->op ){
17039 case TK_AND: {
17040 testcase( jumpIfNull==0 );
17041 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
17042 sqlite3ExprCachePush(pParse);
17043 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
17044 sqlite3ExprCachePop(pParse);
17045 break;
17046 }
17047 case TK_OR: {
17048 int d2 = sqlite3VdbeMakeLabel(v);
17049 testcase( jumpIfNull==0 );
17050 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
17051 sqlite3ExprCachePush(pParse);
17052 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
17053 sqlite3VdbeResolveLabel(v, d2);
17054 sqlite3ExprCachePop(pParse);
17055 break;
17056 }
17057 case TK_NOT: {
17058 testcase( jumpIfNull==0 );
17059 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
17060 break;
17061 }
17062 case TK_IS:
17063 case TK_ISNOT:
17064 testcase( pExpr->op==TK_IS );
17065 testcase( pExpr->op==TK_ISNOT );
17066 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
17067 jumpIfNull = SQLITE_NULLEQ;
17068 /* Fall thru */
17069 case TK_LT:
17070 case TK_LE:
17071 case TK_GT:
17072 case TK_GE:
17073 case TK_NE:
17074 case TK_EQ: {
17075 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
17076 testcase( jumpIfNull==0 );
17077 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
17078 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
17079 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
17080 r1, r2, dest, jumpIfNull);
17081 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
17082 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
17083 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
17084 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
17085 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
17086 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
17087 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
17088 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
17089 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
17090 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
17091 testcase( regFree1==0 );
17092 testcase( regFree2==0 );
17093 break;
17094 }
17095 case TK_ISNULL:
17096 case TK_NOTNULL: {
17097 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
17098 sqlite3VdbeAddOp2(v, op, r1, dest);
17099 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
17100 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
17101 testcase( regFree1==0 );
17102 break;
17103 }
17104 case TK_BETWEEN: {
17105 testcase( jumpIfNull==0 );
17106 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
17107 break;
17108 }
17109 #ifndef SQLITE_OMIT_SUBQUERY
17110 case TK_IN: {
17111 if( jumpIfNull ){
17112 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
17113 }else{
17114 int destIfNull = sqlite3VdbeMakeLabel(v);
17115 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
17116 sqlite3VdbeResolveLabel(v, destIfNull);
17117 }
17118 break;
17119 }
17120 #endif
17121 default: {
17122 default_expr:
17123 if( exprAlwaysFalse(pExpr) ){
17124 sqlite3VdbeGoto(v, dest);
17125 }else if( exprAlwaysTrue(pExpr) ){
17126 /* no-op */
17127 }else{
17128 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
17129 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
17130 VdbeCoverage(v);
17131 testcase( regFree1==0 );
17132 testcase( jumpIfNull==0 );
17133 }
17134 break;
17135 }
17136 }
17137 sqlite3ReleaseTempReg(pParse, regFree1);
17138 sqlite3ReleaseTempReg(pParse, regFree2);
17139 }
17140
17141 /*
17142 ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
17143 ** code generation, and that copy is deleted after code generation. This
17144 ** ensures that the original pExpr is unchanged.
17145 */
17146 SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,i nt jumpIfNull){
17147 sqlite3 *db = pParse->db;
17148 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
17149 if( db->mallocFailed==0 ){
17150 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
17151 }
17152 sqlite3ExprDelete(db, pCopy);
17153 }
17154
17155
17156 /*
17157 ** Do a deep comparison of two expression trees. Return 0 if the two
17158 ** expressions are completely identical. Return 1 if they differ only
17159 ** by a COLLATE operator at the top level. Return 2 if there are differences
17160 ** other than the top-level COLLATE operator.
17161 **
17162 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
17163 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
17164 **
17165 ** The pA side might be using TK_REGISTER. If that is the case and pB is
17166 ** not using TK_REGISTER but is otherwise equivalent, then still return 0.
17167 **
17168 ** Sometimes this routine will return 2 even if the two expressions
17169 ** really are equivalent. If we cannot prove that the expressions are
17170 ** identical, we return 2 just to be safe. So if this routine
17171 ** returns 2, then you do not really know for certain if the two
17172 ** expressions are the same. But if you get a 0 or 1 return, then you
17173 ** can be sure the expressions are the same. In the places where
17174 ** this routine is used, it does not hurt to get an extra 2 - that
17175 ** just might result in some slightly slower code. But returning
17176 ** an incorrect 0 or 1 could lead to a malfunction.
17177 */
17178 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
17179 u32 combinedFlags;
17180 if( pA==0 || pB==0 ){
17181 return pB==pA ? 0 : 2;
17182 }
17183 combinedFlags = pA->flags | pB->flags;
17184 if( combinedFlags & EP_IntValue ){
17185 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
17186 return 0;
17187 }
17188 return 2;
17189 }
17190 if( pA->op!=pB->op ){
17191 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
17192 return 1;
17193 }
17194 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
17195 return 1;
17196 }
17197 return 2;
17198 }
17199 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
17200 if( pA->op==TK_FUNCTION ){
17201 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
17202 }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
17203 return pA->op==TK_COLLATE ? 1 : 2;
17204 }
17205 }
17206 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
17207 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
17208 if( combinedFlags & EP_xIsSelect ) return 2;
17209 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
17210 if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
17211 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
17212 if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
17213 if( pA->iColumn!=pB->iColumn ) return 2;
17214 if( pA->iTable!=pB->iTable
17215 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
17216 }
17217 }
17218 return 0;
17219 }
17220
17221 /*
17222 ** Compare two ExprList objects. Return 0 if they are identical and
17223 ** non-zero if they differ in any way.
17224 **
17225 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
17226 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
17227 **
17228 ** This routine might return non-zero for equivalent ExprLists. The
17229 ** only consequence will be disabled optimizations. But this routine
17230 ** must never return 0 if the two ExprList objects are different, or
17231 ** a malfunction will result.
17232 **
17233 ** Two NULL pointers are considered to be the same. But a NULL pointer
17234 ** always differs from a non-NULL pointer.
17235 */
17236 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
17237 int i;
17238 if( pA==0 && pB==0 ) return 0;
17239 if( pA==0 || pB==0 ) return 1;
17240 if( pA->nExpr!=pB->nExpr ) return 1;
17241 for(i=0; i<pA->nExpr; i++){
17242 Expr *pExprA = pA->a[i].pExpr;
17243 Expr *pExprB = pB->a[i].pExpr;
17244 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
17245 if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
17246 }
17247 return 0;
17248 }
17249
17250 /*
17251 ** Return true if we can prove the pE2 will always be true if pE1 is
17252 ** true. Return false if we cannot complete the proof or if pE2 might
17253 ** be false. Examples:
17254 **
17255 ** pE1: x==5 pE2: x==5 Result: true
17256 ** pE1: x>0 pE2: x==5 Result: false
17257 ** pE1: x=21 pE2: x=21 OR y=43 Result: true
17258 ** pE1: x!=123 pE2: x IS NOT NULL Result: true
17259 ** pE1: x!=?1 pE2: x IS NOT NULL Result: true
17260 ** pE1: x IS NULL pE2: x IS NOT NULL Result: false
17261 ** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
17262 **
17263 ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
17264 ** Expr.iTable<0 then assume a table number given by iTab.
17265 **
17266 ** When in doubt, return false. Returning true might give a performance
17267 ** improvement. Returning false might cause a performance reduction, but
17268 ** it will always give the correct answer and is hence always safe.
17269 */
17270 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
17271 if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
17272 return 1;
17273 }
17274 if( pE2->op==TK_OR
17275 && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
17276 || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
17277 ){
17278 return 1;
17279 }
17280 if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
17281 Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
17282 testcase( pX!=pE1->pLeft );
17283 if( sqlite3ExprCompare(pX, pE2->pLeft, iTab)==0 ) return 1;
17284 }
17285 return 0;
17286 }
17287
17288 /*
17289 ** An instance of the following structure is used by the tree walker
17290 ** to determine if an expression can be evaluated by reference to the
17291 ** index only, without having to do a search for the corresponding
17292 ** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
17293 ** is the cursor for the table.
17294 */
17295 struct IdxCover {
17296 Index *pIdx; /* The index to be tested for coverage */
17297 int iCur; /* Cursor number for the table corresponding to the index */
17298 };
17299
17300 /*
17301 ** Check to see if there are references to columns in table
17302 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
17303 ** pWalker->u.pIdxCover->pIdx.
17304 */
17305 static int exprIdxCover(Walker *pWalker, Expr *pExpr){
17306 if( pExpr->op==TK_COLUMN
17307 && pExpr->iTable==pWalker->u.pIdxCover->iCur
17308 && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
17309 ){
17310 pWalker->eCode = 1;
17311 return WRC_Abort;
17312 }
17313 return WRC_Continue;
17314 }
17315
17316 /*
17317 ** Determine if an index pIdx on table with cursor iCur contains will
17318 ** the expression pExpr. Return true if the index does cover the
17319 ** expression and false if the pExpr expression references table columns
17320 ** that are not found in the index pIdx.
17321 **
17322 ** An index covering an expression means that the expression can be
17323 ** evaluated using only the index and without having to lookup the
17324 ** corresponding table entry.
17325 */
17326 SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(
17327 Expr *pExpr, /* The index to be tested */
17328 int iCur, /* The cursor number for the corresponding table */
17329 Index *pIdx /* The index that might be used for coverage */
17330 ){
17331 Walker w;
17332 struct IdxCover xcov;
17333 memset(&w, 0, sizeof(w));
17334 xcov.iCur = iCur;
17335 xcov.pIdx = pIdx;
17336 w.xExprCallback = exprIdxCover;
17337 w.u.pIdxCover = &xcov;
17338 sqlite3WalkExpr(&w, pExpr);
17339 return !w.eCode;
17340 }
17341
17342
17343 /*
17344 ** An instance of the following structure is used by the tree walker
17345 ** to count references to table columns in the arguments of an
17346 ** aggregate function, in order to implement the
17347 ** sqlite3FunctionThisSrc() routine.
17348 */
17349 struct SrcCount {
17350 SrcList *pSrc; /* One particular FROM clause in a nested query */
17351 int nThis; /* Number of references to columns in pSrcList */
17352 int nOther; /* Number of references to columns in other FROM clauses */
17353 };
17354
17355 /*
17356 ** Count the number of references to columns.
17357 */
17358 static int exprSrcCount(Walker *pWalker, Expr *pExpr){
17359 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
17360 ** is always called before sqlite3ExprAnalyzeAggregates() and so the
17361 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If
17362 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
17363 ** NEVER() will need to be removed. */
17364 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
17365 int i;
17366 struct SrcCount *p = pWalker->u.pSrcCount;
17367 SrcList *pSrc = p->pSrc;
17368 int nSrc = pSrc ? pSrc->nSrc : 0;
17369 for(i=0; i<nSrc; i++){
17370 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
17371 }
17372 if( i<nSrc ){
17373 p->nThis++;
17374 }else{
17375 p->nOther++;
17376 }
17377 }
17378 return WRC_Continue;
17379 }
17380
17381 /*
17382 ** Determine if any of the arguments to the pExpr Function reference
17383 ** pSrcList. Return true if they do. Also return true if the function
17384 ** has no arguments or has only constant arguments. Return false if pExpr
17385 ** references columns but not columns of tables found in pSrcList.
17386 */
17387 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
17388 Walker w;
17389 struct SrcCount cnt;
17390 assert( pExpr->op==TK_AGG_FUNCTION );
17391 memset(&w, 0, sizeof(w));
17392 w.xExprCallback = exprSrcCount;
17393 w.u.pSrcCount = &cnt;
17394 cnt.pSrc = pSrcList;
17395 cnt.nThis = 0;
17396 cnt.nOther = 0;
17397 sqlite3WalkExprList(&w, pExpr->x.pList);
17398 return cnt.nThis>0 || cnt.nOther==0;
17399 }
17400
17401 /*
17402 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
17403 ** the new element. Return a negative number if malloc fails.
17404 */
17405 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
17406 int i;
17407 pInfo->aCol = sqlite3ArrayAllocate(
17408 db,
17409 pInfo->aCol,
17410 sizeof(pInfo->aCol[0]),
17411 &pInfo->nColumn,
17412 &i
17413 );
17414 return i;
17415 }
17416
17417 /*
17418 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
17419 ** the new element. Return a negative number if malloc fails.
17420 */
17421 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
17422 int i;
17423 pInfo->aFunc = sqlite3ArrayAllocate(
17424 db,
17425 pInfo->aFunc,
17426 sizeof(pInfo->aFunc[0]),
17427 &pInfo->nFunc,
17428 &i
17429 );
17430 return i;
17431 }
17432
17433 /*
17434 ** This is the xExprCallback for a tree walker. It is used to
17435 ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
17436 ** for additional information.
17437 */
17438 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
17439 int i;
17440 NameContext *pNC = pWalker->u.pNC;
17441 Parse *pParse = pNC->pParse;
17442 SrcList *pSrcList = pNC->pSrcList;
17443 AggInfo *pAggInfo = pNC->pAggInfo;
17444
17445 switch( pExpr->op ){
17446 case TK_AGG_COLUMN:
17447 case TK_COLUMN: {
17448 testcase( pExpr->op==TK_AGG_COLUMN );
17449 testcase( pExpr->op==TK_COLUMN );
17450 /* Check to see if the column is in one of the tables in the FROM
17451 ** clause of the aggregate query */
17452 if( ALWAYS(pSrcList!=0) ){
17453 struct SrcList_item *pItem = pSrcList->a;
17454 for(i=0; i<pSrcList->nSrc; i++, pItem++){
17455 struct AggInfo_col *pCol;
17456 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
17457 if( pExpr->iTable==pItem->iCursor ){
17458 /* If we reach this point, it means that pExpr refers to a table
17459 ** that is in the FROM clause of the aggregate query.
17460 **
17461 ** Make an entry for the column in pAggInfo->aCol[] if there
17462 ** is not an entry there already.
17463 */
17464 int k;
17465 pCol = pAggInfo->aCol;
17466 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
17467 if( pCol->iTable==pExpr->iTable &&
17468 pCol->iColumn==pExpr->iColumn ){
17469 break;
17470 }
17471 }
17472 if( (k>=pAggInfo->nColumn)
17473 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
17474 ){
17475 pCol = &pAggInfo->aCol[k];
17476 pCol->pTab = pExpr->pTab;
17477 pCol->iTable = pExpr->iTable;
17478 pCol->iColumn = pExpr->iColumn;
17479 pCol->iMem = ++pParse->nMem;
17480 pCol->iSorterColumn = -1;
17481 pCol->pExpr = pExpr;
17482 if( pAggInfo->pGroupBy ){
17483 int j, n;
17484 ExprList *pGB = pAggInfo->pGroupBy;
17485 struct ExprList_item *pTerm = pGB->a;
17486 n = pGB->nExpr;
17487 for(j=0; j<n; j++, pTerm++){
17488 Expr *pE = pTerm->pExpr;
17489 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
17490 pE->iColumn==pExpr->iColumn ){
17491 pCol->iSorterColumn = j;
17492 break;
17493 }
17494 }
17495 }
17496 if( pCol->iSorterColumn<0 ){
17497 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
17498 }
17499 }
17500 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
17501 ** because it was there before or because we just created it).
17502 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
17503 ** pAggInfo->aCol[] entry.
17504 */
17505 ExprSetVVAProperty(pExpr, EP_NoReduce);
17506 pExpr->pAggInfo = pAggInfo;
17507 pExpr->op = TK_AGG_COLUMN;
17508 pExpr->iAgg = (i16)k;
17509 break;
17510 } /* endif pExpr->iTable==pItem->iCursor */
17511 } /* end loop over pSrcList */
17512 }
17513 return WRC_Prune;
17514 }
17515 case TK_AGG_FUNCTION: {
17516 if( (pNC->ncFlags & NC_InAggFunc)==0
17517 && pWalker->walkerDepth==pExpr->op2
17518 ){
17519 /* Check to see if pExpr is a duplicate of another aggregate
17520 ** function that is already in the pAggInfo structure
17521 */
17522 struct AggInfo_func *pItem = pAggInfo->aFunc;
17523 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
17524 if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
17525 break;
17526 }
17527 }
17528 if( i>=pAggInfo->nFunc ){
17529 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
17530 */
17531 u8 enc = ENC(pParse->db);
17532 i = addAggInfoFunc(pParse->db, pAggInfo);
17533 if( i>=0 ){
17534 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
17535 pItem = &pAggInfo->aFunc[i];
17536 pItem->pExpr = pExpr;
17537 pItem->iMem = ++pParse->nMem;
17538 assert( !ExprHasProperty(pExpr, EP_IntValue) );
17539 pItem->pFunc = sqlite3FindFunction(pParse->db,
17540 pExpr->u.zToken,
17541 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
17542 if( pExpr->flags & EP_Distinct ){
17543 pItem->iDistinct = pParse->nTab++;
17544 }else{
17545 pItem->iDistinct = -1;
17546 }
17547 }
17548 }
17549 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
17550 */
17551 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
17552 ExprSetVVAProperty(pExpr, EP_NoReduce);
17553 pExpr->iAgg = (i16)i;
17554 pExpr->pAggInfo = pAggInfo;
17555 return WRC_Prune;
17556 }else{
17557 return WRC_Continue;
17558 }
17559 }
17560 }
17561 return WRC_Continue;
17562 }
17563 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
17564 UNUSED_PARAMETER(pWalker);
17565 UNUSED_PARAMETER(pSelect);
17566 return WRC_Continue;
17567 }
17568
17569 /*
17570 ** Analyze the pExpr expression looking for aggregate functions and
17571 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
17572 ** points to. Additional entries are made on the AggInfo object as
17573 ** necessary.
17574 **
17575 ** This routine should only be called after the expression has been
17576 ** analyzed by sqlite3ResolveExprNames().
17577 */
17578 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
17579 Walker w;
17580 memset(&w, 0, sizeof(w));
17581 w.xExprCallback = analyzeAggregate;
17582 w.xSelectCallback = analyzeAggregatesInSelect;
17583 w.u.pNC = pNC;
17584 assert( pNC->pSrcList!=0 );
17585 sqlite3WalkExpr(&w, pExpr);
17586 }
17587
17588 /*
17589 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
17590 ** expression list. Return the number of errors.
17591 **
17592 ** If an error is found, the analysis is cut short.
17593 */
17594 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList) {
17595 struct ExprList_item *pItem;
17596 int i;
17597 if( pList ){
17598 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
17599 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
17600 }
17601 }
17602 }
17603
17604 /*
17605 ** Allocate a single new register for use to hold some intermediate result.
17606 */
17607 SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
17608 if( pParse->nTempReg==0 ){
17609 return ++pParse->nMem;
17610 }
17611 return pParse->aTempReg[--pParse->nTempReg];
17612 }
17613
17614 /*
17615 ** Deallocate a register, making available for reuse for some other
17616 ** purpose.
17617 **
17618 ** If a register is currently being used by the column cache, then
17619 ** the deallocation is deferred until the column cache line that uses
17620 ** the register becomes stale.
17621 */
17622 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
17623 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
17624 int i;
17625 struct yColCache *p;
17626 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
17627 if( p->iReg==iReg ){
17628 p->tempReg = 1;
17629 return;
17630 }
17631 }
17632 pParse->aTempReg[pParse->nTempReg++] = iReg;
17633 }
17634 }
17635
17636 /*
17637 ** Allocate or deallocate a block of nReg consecutive registers.
17638 */
17639 SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
17640 int i, n;
17641 if( nReg==1 ) return sqlite3GetTempReg(pParse);
17642 i = pParse->iRangeReg;
17643 n = pParse->nRangeReg;
17644 if( nReg<=n ){
17645 assert( !usedAsColumnCache(pParse, i, i+n-1) );
17646 pParse->iRangeReg += nReg;
17647 pParse->nRangeReg -= nReg;
17648 }else{
17649 i = pParse->nMem+1;
17650 pParse->nMem += nReg;
17651 }
17652 return i;
17653 }
17654 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
17655 if( nReg==1 ){
17656 sqlite3ReleaseTempReg(pParse, iReg);
17657 return;
17658 }
17659 sqlite3ExprCacheRemove(pParse, iReg, nReg);
17660 if( nReg>pParse->nRangeReg ){
17661 pParse->nRangeReg = nReg;
17662 pParse->iRangeReg = iReg;
17663 }
17664 }
17665
17666 /*
17667 ** Mark all temporary registers as being unavailable for reuse.
17668 */
17669 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
17670 pParse->nTempReg = 0;
17671 pParse->nRangeReg = 0;
17672 }
17673
17674 /*
17675 ** Validate that no temporary register falls within the range of
17676 ** iFirst..iLast, inclusive. This routine is only call from within assert()
17677 ** statements.
17678 */
17679 #ifdef SQLITE_DEBUG
17680 SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
17681 int i;
17682 if( pParse->nRangeReg>0
17683 && pParse->iRangeReg+pParse->nRangeReg<iLast
17684 && pParse->iRangeReg>=iFirst
17685 ){
17686 return 0;
17687 }
17688 for(i=0; i<pParse->nTempReg; i++){
17689 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
17690 return 0;
17691 }
17692 }
17693 return 1;
17694 }
17695 #endif /* SQLITE_DEBUG */
17696
17697 /************** End of expr.c ************************************************/
17698 /************** Begin file alter.c *******************************************/
17699 /*
17700 ** 2005 February 15
17701 **
17702 ** The author disclaims copyright to this source code. In place of
17703 ** a legal notice, here is a blessing:
17704 **
17705 ** May you do good and not evil.
17706 ** May you find forgiveness for yourself and forgive others.
17707 ** May you share freely, never taking more than you give.
17708 **
17709 *************************************************************************
17710 ** This file contains C code routines that used to generate VDBE code
17711 ** that implements the ALTER TABLE command.
17712 */
17713 /* #include "sqliteInt.h" */
17714
17715 /*
17716 ** The code in this file only exists if we are not omitting the
17717 ** ALTER TABLE logic from the build.
17718 */
17719 #ifndef SQLITE_OMIT_ALTERTABLE
17720
17721
17722 /*
17723 ** This function is used by SQL generated to implement the
17724 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
17725 ** CREATE INDEX command. The second is a table name. The table name in
17726 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
17727 ** argument and the result returned. Examples:
17728 **
17729 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
17730 ** -> 'CREATE TABLE def(a, b, c)'
17731 **
17732 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
17733 ** -> 'CREATE INDEX i ON def(a, b, c)'
17734 */
17735 static void renameTableFunc(
17736 sqlite3_context *context,
17737 int NotUsed,
17738 sqlite3_value **argv
17739 ){
17740 unsigned char const *zSql = sqlite3_value_text(argv[0]);
17741 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
17742
17743 int token;
17744 Token tname;
17745 unsigned char const *zCsr = zSql;
17746 int len = 0;
17747 char *zRet;
17748
17749 sqlite3 *db = sqlite3_context_db_handle(context);
17750
17751 UNUSED_PARAMETER(NotUsed);
17752
17753 /* The principle used to locate the table name in the CREATE TABLE
17754 ** statement is that the table name is the first non-space token that
17755 ** is immediately followed by a TK_LP or TK_USING token.
17756 */
17757 if( zSql ){
17758 do {
17759 if( !*zCsr ){
17760 /* Ran out of input before finding an opening bracket. Return NULL. */
17761 return;
17762 }
17763
17764 /* Store the token that zCsr points to in tname. */
17765 tname.z = (char*)zCsr;
17766 tname.n = len;
17767
17768 /* Advance zCsr to the next token. Store that token type in 'token',
17769 ** and its length in 'len' (to be used next iteration of this loop).
17770 */
17771 do {
17772 zCsr += len;
17773 len = sqlite3GetToken(zCsr, &token);
17774 } while( token==TK_SPACE );
17775 assert( len>0 );
17776 } while( token!=TK_LP && token!=TK_USING );
17777
17778 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
17779 zSql, zTableName, tname.z+tname.n);
17780 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
17781 }
17782 }
17783
17784 /*
17785 ** This C function implements an SQL user function that is used by SQL code
17786 ** generated by the ALTER TABLE ... RENAME command to modify the definition
17787 ** of any foreign key constraints that use the table being renamed as the
17788 ** parent table. It is passed three arguments:
17789 **
17790 ** 1) The complete text of the CREATE TABLE statement being modified,
17791 ** 2) The old name of the table being renamed, and
17792 ** 3) The new name of the table being renamed.
17793 **
17794 ** It returns the new CREATE TABLE statement. For example:
17795 **
17796 ** sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
17797 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
17798 */
17799 #ifndef SQLITE_OMIT_FOREIGN_KEY
17800 static void renameParentFunc(
17801 sqlite3_context *context,
17802 int NotUsed,
17803 sqlite3_value **argv
17804 ){
17805 sqlite3 *db = sqlite3_context_db_handle(context);
17806 char *zOutput = 0;
17807 char *zResult;
17808 unsigned char const *zInput = sqlite3_value_text(argv[0]);
17809 unsigned char const *zOld = sqlite3_value_text(argv[1]);
17810 unsigned char const *zNew = sqlite3_value_text(argv[2]);
17811
17812 unsigned const char *z; /* Pointer to token */
17813 int n; /* Length of token z */
17814 int token; /* Type of token */
17815
17816 UNUSED_PARAMETER(NotUsed);
17817 if( zInput==0 || zOld==0 ) return;
17818 for(z=zInput; *z; z=z+n){
17819 n = sqlite3GetToken(z, &token);
17820 if( token==TK_REFERENCES ){
17821 char *zParent;
17822 do {
17823 z += n;
17824 n = sqlite3GetToken(z, &token);
17825 }while( token==TK_SPACE );
17826
17827 if( token==TK_ILLEGAL ) break;
17828 zParent = sqlite3DbStrNDup(db, (const char *)z, n);
17829 if( zParent==0 ) break;
17830 sqlite3Dequote(zParent);
17831 if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
17832 char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"",
17833 (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
17834 );
17835 sqlite3DbFree(db, zOutput);
17836 zOutput = zOut;
17837 zInput = &z[n];
17838 }
17839 sqlite3DbFree(db, zParent);
17840 }
17841 }
17842
17843 zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput),
17844 sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
17845 sqlite3DbFree(db, zOutput);
17846 }
17847 #endif
17848
17849 #ifndef SQLITE_OMIT_TRIGGER
17850 /* This function is used by SQL generated to implement the
17851 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
17852 ** statement. The second is a table name. The table name in the CREATE
17853 ** TRIGGER statement is replaced with the third argument and the result
17854 ** returned. This is analagous to renameTableFunc() above, except for CREATE
17855 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
17856 */
17857 static void renameTriggerFunc(
17858 sqlite3_context *context,
17859 int NotUsed,
17860 sqlite3_value **argv
17861 ){
17862 unsigned char const *zSql = sqlite3_value_text(argv[0]);
17863 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
17864
17865 int token;
17866 Token tname;
17867 int dist = 3;
17868 unsigned char const *zCsr = zSql;
17869 int len = 0;
17870 char *zRet;
17871 sqlite3 *db = sqlite3_context_db_handle(context);
17872
17873 UNUSED_PARAMETER(NotUsed);
17874
17875 /* The principle used to locate the table name in the CREATE TRIGGER
17876 ** statement is that the table name is the first token that is immediately
17877 ** preceded by either TK_ON or TK_DOT and immediately followed by one
17878 ** of TK_WHEN, TK_BEGIN or TK_FOR.
17879 */
17880 if( zSql ){
17881 do {
17882
17883 if( !*zCsr ){
17884 /* Ran out of input before finding the table name. Return NULL. */
17885 return;
17886 }
17887
17888 /* Store the token that zCsr points to in tname. */
17889 tname.z = (char*)zCsr;
17890 tname.n = len;
17891
17892 /* Advance zCsr to the next token. Store that token type in 'token',
17893 ** and its length in 'len' (to be used next iteration of this loop).
17894 */
17895 do {
17896 zCsr += len;
17897 len = sqlite3GetToken(zCsr, &token);
17898 }while( token==TK_SPACE );
17899 assert( len>0 );
17900
17901 /* Variable 'dist' stores the number of tokens read since the most
17902 ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
17903 ** token is read and 'dist' equals 2, the condition stated above
17904 ** to be met.
17905 **
17906 ** Note that ON cannot be a database, table or column name, so
17907 ** there is no need to worry about syntax like
17908 ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
17909 */
17910 dist++;
17911 if( token==TK_DOT || token==TK_ON ){
17912 dist = 0;
17913 }
17914 } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
17915
17916 /* Variable tname now contains the token that is the old table-name
17917 ** in the CREATE TRIGGER statement.
17918 */
17919 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
17920 zSql, zTableName, tname.z+tname.n);
17921 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
17922 }
17923 }
17924 #endif /* !SQLITE_OMIT_TRIGGER */
17925
17926 /*
17927 ** Register built-in functions used to help implement ALTER TABLE
17928 */
17929 SQLITE_PRIVATE void sqlite3AlterFunctions(void){
17930 static FuncDef aAlterTableFuncs[] = {
17931 FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc),
17932 #ifndef SQLITE_OMIT_TRIGGER
17933 FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
17934 #endif
17935 #ifndef SQLITE_OMIT_FOREIGN_KEY
17936 FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc),
17937 #endif
17938 };
17939 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
17940 }
17941
17942 /*
17943 ** This function is used to create the text of expressions of the form:
17944 **
17945 ** name=<constant1> OR name=<constant2> OR ...
17946 **
17947 ** If argument zWhere is NULL, then a pointer string containing the text
17948 ** "name=<constant>" is returned, where <constant> is the quoted version
17949 ** of the string passed as argument zConstant. The returned buffer is
17950 ** allocated using sqlite3DbMalloc(). It is the responsibility of the
17951 ** caller to ensure that it is eventually freed.
17952 **
17953 ** If argument zWhere is not NULL, then the string returned is
17954 ** "<where> OR name=<constant>", where <where> is the contents of zWhere.
17955 ** In this case zWhere is passed to sqlite3DbFree() before returning.
17956 **
17957 */
17958 static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
17959 char *zNew;
17960 if( !zWhere ){
17961 zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
17962 }else{
17963 zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
17964 sqlite3DbFree(db, zWhere);
17965 }
17966 return zNew;
17967 }
17968
17969 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
17970 /*
17971 ** Generate the text of a WHERE expression which can be used to select all
17972 ** tables that have foreign key constraints that refer to table pTab (i.e.
17973 ** constraints for which pTab is the parent table) from the sqlite_master
17974 ** table.
17975 */
17976 static char *whereForeignKeys(Parse *pParse, Table *pTab){
17977 FKey *p;
17978 char *zWhere = 0;
17979 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
17980 zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
17981 }
17982 return zWhere;
17983 }
17984 #endif
17985
17986 /*
17987 ** Generate the text of a WHERE expression which can be used to select all
17988 ** temporary triggers on table pTab from the sqlite_temp_master table. If
17989 ** table pTab has no temporary triggers, or is itself stored in the
17990 ** temporary database, NULL is returned.
17991 */
17992 static char *whereTempTriggers(Parse *pParse, Table *pTab){
17993 Trigger *pTrig;
17994 char *zWhere = 0;
17995 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
17996
17997 /* If the table is not located in the temp-db (in which case NULL is
17998 ** returned, loop through the tables list of triggers. For each trigger
17999 ** that is not part of the temp-db schema, add a clause to the WHERE
18000 ** expression being built up in zWhere.
18001 */
18002 if( pTab->pSchema!=pTempSchema ){
18003 sqlite3 *db = pParse->db;
18004 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
18005 if( pTrig->pSchema==pTempSchema ){
18006 zWhere = whereOrName(db, zWhere, pTrig->zName);
18007 }
18008 }
18009 }
18010 if( zWhere ){
18011 char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
18012 sqlite3DbFree(pParse->db, zWhere);
18013 zWhere = zNew;
18014 }
18015 return zWhere;
18016 }
18017
18018 /*
18019 ** Generate code to drop and reload the internal representation of table
18020 ** pTab from the database, including triggers and temporary triggers.
18021 ** Argument zName is the name of the table in the database schema at
18022 ** the time the generated code is executed. This can be different from
18023 ** pTab->zName if this function is being called to code part of an
18024 ** "ALTER TABLE RENAME TO" statement.
18025 */
18026 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
18027 Vdbe *v;
18028 char *zWhere;
18029 int iDb; /* Index of database containing pTab */
18030 #ifndef SQLITE_OMIT_TRIGGER
18031 Trigger *pTrig;
18032 #endif
18033
18034 v = sqlite3GetVdbe(pParse);
18035 if( NEVER(v==0) ) return;
18036 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
18037 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
18038 assert( iDb>=0 );
18039
18040 #ifndef SQLITE_OMIT_TRIGGER
18041 /* Drop any table triggers from the internal schema. */
18042 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
18043 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
18044 assert( iTrigDb==iDb || iTrigDb==1 );
18045 sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
18046 }
18047 #endif
18048
18049 /* Drop the table and index from the internal schema. */
18050 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
18051
18052 /* Reload the table, index and permanent trigger schemas. */
18053 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
18054 if( !zWhere ) return;
18055 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
18056
18057 #ifndef SQLITE_OMIT_TRIGGER
18058 /* Now, if the table is not stored in the temp database, reload any temp
18059 ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
18060 */
18061 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
18062 sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
18063 }
18064 #endif
18065 }
18066
18067 /*
18068 ** Parameter zName is the name of a table that is about to be altered
18069 ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
18070 ** If the table is a system table, this function leaves an error message
18071 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
18072 **
18073 ** Or, if zName is not a system table, zero is returned.
18074 */
18075 static int isSystemTable(Parse *pParse, const char *zName){
18076 if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
18077 sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
18078 return 1;
18079 }
18080 return 0;
18081 }
18082
18083 /*
18084 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
18085 ** command.
18086 */
18087 SQLITE_PRIVATE void sqlite3AlterRenameTable(
18088 Parse *pParse, /* Parser context. */
18089 SrcList *pSrc, /* The table to rename. */
18090 Token *pName /* The new table name. */
18091 ){
18092 int iDb; /* Database that contains the table */
18093 char *zDb; /* Name of database iDb */
18094 Table *pTab; /* Table being renamed */
18095 char *zName = 0; /* NULL-terminated version of pName */
18096 sqlite3 *db = pParse->db; /* Database connection */
18097 int nTabName; /* Number of UTF-8 characters in zTabName */
18098 const char *zTabName; /* Original name of the table */
18099 Vdbe *v;
18100 #ifndef SQLITE_OMIT_TRIGGER
18101 char *zWhere = 0; /* Where clause to locate temp triggers */
18102 #endif
18103 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
18104 int savedDbFlags; /* Saved value of db->flags */
18105
18106 savedDbFlags = db->flags;
18107 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
18108 assert( pSrc->nSrc==1 );
18109 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
18110
18111 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
18112 if( !pTab ) goto exit_rename_table;
18113 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
18114 zDb = db->aDb[iDb].zDbSName;
18115 db->flags |= SQLITE_PreferBuiltin;
18116
18117 /* Get a NULL terminated version of the new table name. */
18118 zName = sqlite3NameFromToken(db, pName);
18119 if( !zName ) goto exit_rename_table;
18120
18121 /* Check that a table or index named 'zName' does not already exist
18122 ** in database iDb. If so, this is an error.
18123 */
18124 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
18125 sqlite3ErrorMsg(pParse,
18126 "there is already another table or index with this name: %s", zName);
18127 goto exit_rename_table;
18128 }
18129
18130 /* Make sure it is not a system table being altered, or a reserved name
18131 ** that the table is being renamed to.
18132 */
18133 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
18134 goto exit_rename_table;
18135 }
18136 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
18137 exit_rename_table;
18138 }
18139
18140 #ifndef SQLITE_OMIT_VIEW
18141 if( pTab->pSelect ){
18142 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
18143 goto exit_rename_table;
18144 }
18145 #endif
18146
18147 #ifndef SQLITE_OMIT_AUTHORIZATION
18148 /* Invoke the authorization callback. */
18149 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
18150 goto exit_rename_table;
18151 }
18152 #endif
18153
18154 #ifndef SQLITE_OMIT_VIRTUALTABLE
18155 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
18156 goto exit_rename_table;
18157 }
18158 if( IsVirtual(pTab) ){
18159 pVTab = sqlite3GetVTable(db, pTab);
18160 if( pVTab->pVtab->pModule->xRename==0 ){
18161 pVTab = 0;
18162 }
18163 }
18164 #endif
18165
18166 /* Begin a transaction for database iDb.
18167 ** Then modify the schema cookie (since the ALTER TABLE modifies the
18168 ** schema). Open a statement transaction if the table is a virtual
18169 ** table.
18170 */
18171 v = sqlite3GetVdbe(pParse);
18172 if( v==0 ){
18173 goto exit_rename_table;
18174 }
18175 sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
18176 sqlite3ChangeCookie(pParse, iDb);
18177
18178 /* If this is a virtual table, invoke the xRename() function if
18179 ** one is defined. The xRename() callback will modify the names
18180 ** of any resources used by the v-table implementation (including other
18181 ** SQLite tables) that are identified by the name of the virtual table.
18182 */
18183 #ifndef SQLITE_OMIT_VIRTUALTABLE
18184 if( pVTab ){
18185 int i = ++pParse->nMem;
18186 sqlite3VdbeLoadString(v, i, zName);
18187 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
18188 sqlite3MayAbort(pParse);
18189 }
18190 #endif
18191
18192 /* figure out how many UTF-8 characters are in zName */
18193 zTabName = pTab->zName;
18194 nTabName = sqlite3Utf8CharLen(zTabName, -1);
18195
18196 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
18197 if( db->flags&SQLITE_ForeignKeys ){
18198 /* If foreign-key support is enabled, rewrite the CREATE TABLE
18199 ** statements corresponding to all child tables of foreign key constraints
18200 ** for which the renamed table is the parent table. */
18201 if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
18202 sqlite3NestedParse(pParse,
18203 "UPDATE \"%w\".%s SET "
18204 "sql = sqlite_rename_parent(sql, %Q, %Q) "
18205 "WHERE %s;", zDb, MASTER_NAME, zTabName, zName, zWhere);
18206 sqlite3DbFree(db, zWhere);
18207 }
18208 }
18209 #endif
18210
18211 /* Modify the sqlite_master table to use the new table name. */
18212 sqlite3NestedParse(pParse,
18213 "UPDATE %Q.%s SET "
18214 #ifdef SQLITE_OMIT_TRIGGER
18215 "sql = sqlite_rename_table(sql, %Q), "
18216 #else
18217 "sql = CASE "
18218 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
18219 "ELSE sqlite_rename_table(sql, %Q) END, "
18220 #endif
18221 "tbl_name = %Q, "
18222 "name = CASE "
18223 "WHEN type='table' THEN %Q "
18224 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
18225 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
18226 "ELSE name END "
18227 "WHERE tbl_name=%Q COLLATE nocase AND "
18228 "(type='table' OR type='index' OR type='trigger');",
18229 zDb, MASTER_NAME, zName, zName, zName,
18230 #ifndef SQLITE_OMIT_TRIGGER
18231 zName,
18232 #endif
18233 zName, nTabName, zTabName
18234 );
18235
18236 #ifndef SQLITE_OMIT_AUTOINCREMENT
18237 /* If the sqlite_sequence table exists in this database, then update
18238 ** it with the new table name.
18239 */
18240 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
18241 sqlite3NestedParse(pParse,
18242 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
18243 zDb, zName, pTab->zName);
18244 }
18245 #endif
18246
18247 #ifndef SQLITE_OMIT_TRIGGER
18248 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
18249 ** table. Don't do this if the table being ALTERed is itself located in
18250 ** the temp database.
18251 */
18252 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
18253 sqlite3NestedParse(pParse,
18254 "UPDATE sqlite_temp_master SET "
18255 "sql = sqlite_rename_trigger(sql, %Q), "
18256 "tbl_name = %Q "
18257 "WHERE %s;", zName, zName, zWhere);
18258 sqlite3DbFree(db, zWhere);
18259 }
18260 #endif
18261
18262 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
18263 if( db->flags&SQLITE_ForeignKeys ){
18264 FKey *p;
18265 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
18266 Table *pFrom = p->pFrom;
18267 if( pFrom!=pTab ){
18268 reloadTableSchema(pParse, p->pFrom, pFrom->zName);
18269 }
18270 }
18271 }
18272 #endif
18273
18274 /* Drop and reload the internal table schema. */
18275 reloadTableSchema(pParse, pTab, zName);
18276
18277 exit_rename_table:
18278 sqlite3SrcListDelete(db, pSrc);
18279 sqlite3DbFree(db, zName);
18280 db->flags = savedDbFlags;
18281 }
18282
18283 /*
18284 ** This function is called after an "ALTER TABLE ... ADD" statement
18285 ** has been parsed. Argument pColDef contains the text of the new
18286 ** column definition.
18287 **
18288 ** The Table structure pParse->pNewTable was extended to include
18289 ** the new column during parsing.
18290 */
18291 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
18292 Table *pNew; /* Copy of pParse->pNewTable */
18293 Table *pTab; /* Table being altered */
18294 int iDb; /* Database number */
18295 const char *zDb; /* Database name */
18296 const char *zTab; /* Table name */
18297 char *zCol; /* Null-terminated column definition */
18298 Column *pCol; /* The new column */
18299 Expr *pDflt; /* Default value for the new column */
18300 sqlite3 *db; /* The database connection; */
18301 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
18302 int r1; /* Temporary registers */
18303
18304 db = pParse->db;
18305 if( pParse->nErr || db->mallocFailed ) return;
18306 assert( v!=0 );
18307 pNew = pParse->pNewTable;
18308 assert( pNew );
18309
18310 assert( sqlite3BtreeHoldsAllMutexes(db) );
18311 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
18312 zDb = db->aDb[iDb].zDbSName;
18313 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
18314 pCol = &pNew->aCol[pNew->nCol-1];
18315 pDflt = pCol->pDflt;
18316 pTab = sqlite3FindTable(db, zTab, zDb);
18317 assert( pTab );
18318
18319 #ifndef SQLITE_OMIT_AUTHORIZATION
18320 /* Invoke the authorization callback. */
18321 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
18322 return;
18323 }
18324 #endif
18325
18326 /* If the default value for the new column was specified with a
18327 ** literal NULL, then set pDflt to 0. This simplifies checking
18328 ** for an SQL NULL default below.
18329 */
18330 assert( pDflt==0 || pDflt->op==TK_SPAN );
18331 if( pDflt && pDflt->pLeft->op==TK_NULL ){
18332 pDflt = 0;
18333 }
18334
18335 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
18336 ** If there is a NOT NULL constraint, then the default value for the
18337 ** column must not be NULL.
18338 */
18339 if( pCol->colFlags & COLFLAG_PRIMKEY ){
18340 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
18341 return;
18342 }
18343 if( pNew->pIndex ){
18344 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
18345 return;
18346 }
18347 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
18348 sqlite3ErrorMsg(pParse,
18349 "Cannot add a REFERENCES column with non-NULL default value");
18350 return;
18351 }
18352 if( pCol->notNull && !pDflt ){
18353 sqlite3ErrorMsg(pParse,
18354 "Cannot add a NOT NULL column with default value NULL");
18355 return;
18356 }
18357
18358 /* Ensure the default expression is something that sqlite3ValueFromExpr()
18359 ** can handle (i.e. not CURRENT_TIME etc.)
18360 */
18361 if( pDflt ){
18362 sqlite3_value *pVal = 0;
18363 int rc;
18364 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
18365 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
18366 if( rc!=SQLITE_OK ){
18367 assert( db->mallocFailed == 1 );
18368 return;
18369 }
18370 if( !pVal ){
18371 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
18372 return;
18373 }
18374 sqlite3ValueFree(pVal);
18375 }
18376
18377 /* Modify the CREATE TABLE statement. */
18378 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
18379 if( zCol ){
18380 char *zEnd = &zCol[pColDef->n-1];
18381 int savedDbFlags = db->flags;
18382 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
18383 *zEnd-- = '\0';
18384 }
18385 db->flags |= SQLITE_PreferBuiltin;
18386 sqlite3NestedParse(pParse,
18387 "UPDATE \"%w\".%s SET "
18388 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
18389 "WHERE type = 'table' AND name = %Q",
18390 zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
18391 zTab
18392 );
18393 sqlite3DbFree(db, zCol);
18394 db->flags = savedDbFlags;
18395 }
18396
18397 /* Make sure the schema version is at least 3. But do not upgrade
18398 ** from less than 3 to 4, as that will corrupt any preexisting DESC
18399 ** index.
18400 */
18401 r1 = sqlite3GetTempReg(pParse);
18402 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
18403 sqlite3VdbeUsesBtree(v, iDb);
18404 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
18405 sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
18406 VdbeCoverage(v);
18407 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
18408 sqlite3ReleaseTempReg(pParse, r1);
18409
18410 /* Reload the schema of the modified table. */
18411 reloadTableSchema(pParse, pTab, pTab->zName);
18412 }
18413
18414 /*
18415 ** This function is called by the parser after the table-name in
18416 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
18417 ** pSrc is the full-name of the table being altered.
18418 **
18419 ** This routine makes a (partial) copy of the Table structure
18420 ** for the table being altered and sets Parse.pNewTable to point
18421 ** to it. Routines called by the parser as the column definition
18422 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
18423 ** the copy. The copy of the Table structure is deleted by tokenize.c
18424 ** after parsing is finished.
18425 **
18426 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
18427 ** coding the "ALTER TABLE ... ADD" statement.
18428 */
18429 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
18430 Table *pNew;
18431 Table *pTab;
18432 Vdbe *v;
18433 int iDb;
18434 int i;
18435 int nAlloc;
18436 sqlite3 *db = pParse->db;
18437
18438 /* Look up the table being altered. */
18439 assert( pParse->pNewTable==0 );
18440 assert( sqlite3BtreeHoldsAllMutexes(db) );
18441 if( db->mallocFailed ) goto exit_begin_add_column;
18442 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
18443 if( !pTab ) goto exit_begin_add_column;
18444
18445 #ifndef SQLITE_OMIT_VIRTUALTABLE
18446 if( IsVirtual(pTab) ){
18447 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
18448 goto exit_begin_add_column;
18449 }
18450 #endif
18451
18452 /* Make sure this is not an attempt to ALTER a view. */
18453 if( pTab->pSelect ){
18454 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
18455 goto exit_begin_add_column;
18456 }
18457 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
18458 goto exit_begin_add_column;
18459 }
18460
18461 assert( pTab->addColOffset>0 );
18462 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
18463
18464 /* Put a copy of the Table struct in Parse.pNewTable for the
18465 ** sqlite3AddColumn() function and friends to modify. But modify
18466 ** the name by adding an "sqlite_altertab_" prefix. By adding this
18467 ** prefix, we insure that the name will not collide with an existing
18468 ** table because user table are not allowed to have the "sqlite_"
18469 ** prefix on their name.
18470 */
18471 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
18472 if( !pNew ) goto exit_begin_add_column;
18473 pParse->pNewTable = pNew;
18474 pNew->nTabRef = 1;
18475 pNew->nCol = pTab->nCol;
18476 assert( pNew->nCol>0 );
18477 nAlloc = (((pNew->nCol-1)/8)*8)+8;
18478 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
18479 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
18480 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
18481 if( !pNew->aCol || !pNew->zName ){
18482 assert( db->mallocFailed );
18483 goto exit_begin_add_column;
18484 }
18485 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
18486 for(i=0; i<pNew->nCol; i++){
18487 Column *pCol = &pNew->aCol[i];
18488 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
18489 pCol->zColl = 0;
18490 pCol->pDflt = 0;
18491 }
18492 pNew->pSchema = db->aDb[iDb].pSchema;
18493 pNew->addColOffset = pTab->addColOffset;
18494 pNew->nTabRef = 1;
18495
18496 /* Begin a transaction and increment the schema cookie. */
18497 sqlite3BeginWriteOperation(pParse, 0, iDb);
18498 v = sqlite3GetVdbe(pParse);
18499 if( !v ) goto exit_begin_add_column;
18500 sqlite3ChangeCookie(pParse, iDb);
18501
18502 exit_begin_add_column:
18503 sqlite3SrcListDelete(db, pSrc);
18504 return;
18505 }
18506 #endif /* SQLITE_ALTER_TABLE */
18507
18508 /************** End of alter.c ***********************************************/
18509 /************** Begin file analyze.c *****************************************/
18510 /*
18511 ** 2005-07-08
18512 **
18513 ** The author disclaims copyright to this source code. In place of
18514 ** a legal notice, here is a blessing:
18515 **
18516 ** May you do good and not evil.
18517 ** May you find forgiveness for yourself and forgive others.
18518 ** May you share freely, never taking more than you give.
18519 **
18520 *************************************************************************
18521 ** This file contains code associated with the ANALYZE command.
18522 **
18523 ** The ANALYZE command gather statistics about the content of tables
18524 ** and indices. These statistics are made available to the query planner
18525 ** to help it make better decisions about how to perform queries.
18526 **
18527 ** The following system tables are or have been supported:
18528 **
18529 ** CREATE TABLE sqlite_stat1(tbl, idx, stat);
18530 ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
18531 ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
18532 ** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
18533 **
18534 ** Additional tables might be added in future releases of SQLite.
18535 ** The sqlite_stat2 table is not created or used unless the SQLite version
18536 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
18537 ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
18538 ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
18539 ** created and used by SQLite versions 3.7.9 and later and with
18540 ** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
18541 ** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
18542 ** version of sqlite_stat3 and is only available when compiled with
18543 ** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later. It is
18544 ** not possible to enable both STAT3 and STAT4 at the same time. If they
18545 ** are both enabled, then STAT4 takes precedence.
18546 **
18547 ** For most applications, sqlite_stat1 provides all the statistics required
18548 ** for the query planner to make good choices.
18549 **
18550 ** Format of sqlite_stat1:
18551 **
18552 ** There is normally one row per index, with the index identified by the
18553 ** name in the idx column. The tbl column is the name of the table to
18554 ** which the index belongs. In each such row, the stat column will be
18555 ** a string consisting of a list of integers. The first integer in this
18556 ** list is the number of rows in the index. (This is the same as the
18557 ** number of rows in the table, except for partial indices.) The second
18558 ** integer is the average number of rows in the index that have the same
18559 ** value in the first column of the index. The third integer is the average
18560 ** number of rows in the index that have the same value for the first two
18561 ** columns. The N-th integer (for N>1) is the average number of rows in
18562 ** the index which have the same value for the first N-1 columns. For
18563 ** a K-column index, there will be K+1 integers in the stat column. If
18564 ** the index is unique, then the last integer will be 1.
18565 **
18566 ** The list of integers in the stat column can optionally be followed
18567 ** by the keyword "unordered". The "unordered" keyword, if it is present,
18568 ** must be separated from the last integer by a single space. If the
18569 ** "unordered" keyword is present, then the query planner assumes that
18570 ** the index is unordered and will not use the index for a range query.
18571 **
18572 ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
18573 ** column contains a single integer which is the (estimated) number of
18574 ** rows in the table identified by sqlite_stat1.tbl.
18575 **
18576 ** Format of sqlite_stat2:
18577 **
18578 ** The sqlite_stat2 is only created and is only used if SQLite is compiled
18579 ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
18580 ** 3.6.18 and 3.7.8. The "stat2" table contains additional information
18581 ** about the distribution of keys within an index. The index is identified by
18582 ** the "idx" column and the "tbl" column is the name of the table to which
18583 ** the index belongs. There are usually 10 rows in the sqlite_stat2
18584 ** table for each index.
18585 **
18586 ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
18587 ** inclusive are samples of the left-most key value in the index taken at
18588 ** evenly spaced points along the index. Let the number of samples be S
18589 ** (10 in the standard build) and let C be the number of rows in the index.
18590 ** Then the sampled rows are given by:
18591 **
18592 ** rownumber = (i*C*2 + C)/(S*2)
18593 **
18594 ** For i between 0 and S-1. Conceptually, the index space is divided into
18595 ** S uniform buckets and the samples are the middle row from each bucket.
18596 **
18597 ** The format for sqlite_stat2 is recorded here for legacy reference. This
18598 ** version of SQLite does not support sqlite_stat2. It neither reads nor
18599 ** writes the sqlite_stat2 table. This version of SQLite only supports
18600 ** sqlite_stat3.
18601 **
18602 ** Format for sqlite_stat3:
18603 **
18604 ** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
18605 ** sqlite_stat4 format will be described first. Further information
18606 ** about sqlite_stat3 follows the sqlite_stat4 description.
18607 **
18608 ** Format for sqlite_stat4:
18609 **
18610 ** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
18611 ** to aid the query planner in choosing good indices based on the values
18612 ** that indexed columns are compared against in the WHERE clauses of
18613 ** queries.
18614 **
18615 ** The sqlite_stat4 table contains multiple entries for each index.
18616 ** The idx column names the index and the tbl column is the table of the
18617 ** index. If the idx and tbl columns are the same, then the sample is
18618 ** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
18619 ** binary encoding of a key from the index. The nEq column is a
18620 ** list of integers. The first integer is the approximate number
18621 ** of entries in the index whose left-most column exactly matches
18622 ** the left-most column of the sample. The second integer in nEq
18623 ** is the approximate number of entries in the index where the
18624 ** first two columns match the first two columns of the sample.
18625 ** And so forth. nLt is another list of integers that show the approximate
18626 ** number of entries that are strictly less than the sample. The first
18627 ** integer in nLt contains the number of entries in the index where the
18628 ** left-most column is less than the left-most column of the sample.
18629 ** The K-th integer in the nLt entry is the number of index entries
18630 ** where the first K columns are less than the first K columns of the
18631 ** sample. The nDLt column is like nLt except that it contains the
18632 ** number of distinct entries in the index that are less than the
18633 ** sample.
18634 **
18635 ** There can be an arbitrary number of sqlite_stat4 entries per index.
18636 ** The ANALYZE command will typically generate sqlite_stat4 tables
18637 ** that contain between 10 and 40 samples which are distributed across
18638 ** the key space, though not uniformly, and which include samples with
18639 ** large nEq values.
18640 **
18641 ** Format for sqlite_stat3 redux:
18642 **
18643 ** The sqlite_stat3 table is like sqlite_stat4 except that it only
18644 ** looks at the left-most column of the index. The sqlite_stat3.sample
18645 ** column contains the actual value of the left-most column instead
18646 ** of a blob encoding of the complete index key as is found in
18647 ** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
18648 ** all contain just a single integer which is the same as the first
18649 ** integer in the equivalent columns in sqlite_stat4.
18650 */
18651 #ifndef SQLITE_OMIT_ANALYZE
18652 /* #include "sqliteInt.h" */
18653
18654 #if defined(SQLITE_ENABLE_STAT4)
18655 # define IsStat4 1
18656 # define IsStat3 0
18657 #elif defined(SQLITE_ENABLE_STAT3)
18658 # define IsStat4 0
18659 # define IsStat3 1
18660 #else
18661 # define IsStat4 0
18662 # define IsStat3 0
18663 # undef SQLITE_STAT4_SAMPLES
18664 # define SQLITE_STAT4_SAMPLES 1
18665 #endif
18666 #define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */
18667
18668 /*
18669 ** This routine generates code that opens the sqlite_statN tables.
18670 ** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
18671 ** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
18672 ** appropriate compile-time options are provided.
18673 **
18674 ** If the sqlite_statN tables do not previously exist, it is created.
18675 **
18676 ** Argument zWhere may be a pointer to a buffer containing a table name,
18677 ** or it may be a NULL pointer. If it is not NULL, then all entries in
18678 ** the sqlite_statN tables associated with the named table are deleted.
18679 ** If zWhere==0, then code is generated to delete all stat table entries.
18680 */
18681 static void openStatTable(
18682 Parse *pParse, /* Parsing context */
18683 int iDb, /* The database we are looking in */
18684 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
18685 const char *zWhere, /* Delete entries for this table or index */
18686 const char *zWhereType /* Either "tbl" or "idx" */
18687 ){
18688 static const struct {
18689 const char *zName;
18690 const char *zCols;
18691 } aTable[] = {
18692 { "sqlite_stat1", "tbl,idx,stat" },
18693 #if defined(SQLITE_ENABLE_STAT4)
18694 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
18695 { "sqlite_stat3", 0 },
18696 #elif defined(SQLITE_ENABLE_STAT3)
18697 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
18698 { "sqlite_stat4", 0 },
18699 #else
18700 { "sqlite_stat3", 0 },
18701 { "sqlite_stat4", 0 },
18702 #endif
18703 };
18704 int i;
18705 sqlite3 *db = pParse->db;
18706 Db *pDb;
18707 Vdbe *v = sqlite3GetVdbe(pParse);
18708 int aRoot[ArraySize(aTable)];
18709 u8 aCreateTbl[ArraySize(aTable)];
18710
18711 if( v==0 ) return;
18712 assert( sqlite3BtreeHoldsAllMutexes(db) );
18713 assert( sqlite3VdbeDb(v)==db );
18714 pDb = &db->aDb[iDb];
18715
18716 /* Create new statistic tables if they do not exist, or clear them
18717 ** if they do already exist.
18718 */
18719 for(i=0; i<ArraySize(aTable); i++){
18720 const char *zTab = aTable[i].zName;
18721 Table *pStat;
18722 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
18723 if( aTable[i].zCols ){
18724 /* The sqlite_statN table does not exist. Create it. Note that a
18725 ** side-effect of the CREATE TABLE statement is to leave the rootpage
18726 ** of the new table in register pParse->regRoot. This is important
18727 ** because the OpenWrite opcode below will be needing it. */
18728 sqlite3NestedParse(pParse,
18729 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
18730 );
18731 aRoot[i] = pParse->regRoot;
18732 aCreateTbl[i] = OPFLAG_P2ISREG;
18733 }
18734 }else{
18735 /* The table already exists. If zWhere is not NULL, delete all entries
18736 ** associated with the table zWhere. If zWhere is NULL, delete the
18737 ** entire contents of the table. */
18738 aRoot[i] = pStat->tnum;
18739 aCreateTbl[i] = 0;
18740 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
18741 if( zWhere ){
18742 sqlite3NestedParse(pParse,
18743 "DELETE FROM %Q.%s WHERE %s=%Q",
18744 pDb->zDbSName, zTab, zWhereType, zWhere
18745 );
18746 }else{
18747 /* The sqlite_stat[134] table already exists. Delete all rows. */
18748 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
18749 }
18750 }
18751 }
18752
18753 /* Open the sqlite_stat[134] tables for writing. */
18754 for(i=0; aTable[i].zCols; i++){
18755 assert( i<ArraySize(aTable) );
18756 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
18757 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
18758 VdbeComment((v, aTable[i].zName));
18759 }
18760 }
18761
18762 /*
18763 ** Recommended number of samples for sqlite_stat4
18764 */
18765 #ifndef SQLITE_STAT4_SAMPLES
18766 # define SQLITE_STAT4_SAMPLES 24
18767 #endif
18768
18769 /*
18770 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
18771 ** share an instance of the following structure to hold their state
18772 ** information.
18773 */
18774 typedef struct Stat4Accum Stat4Accum;
18775 typedef struct Stat4Sample Stat4Sample;
18776 struct Stat4Sample {
18777 tRowcnt *anEq; /* sqlite_stat4.nEq */
18778 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
18779 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
18780 tRowcnt *anLt; /* sqlite_stat4.nLt */
18781 union {
18782 i64 iRowid; /* Rowid in main table of the key */
18783 u8 *aRowid; /* Key for WITHOUT ROWID tables */
18784 } u;
18785 u32 nRowid; /* Sizeof aRowid[] */
18786 u8 isPSample; /* True if a periodic sample */
18787 int iCol; /* If !isPSample, the reason for inclusion */
18788 u32 iHash; /* Tiebreaker hash */
18789 #endif
18790 };
18791 struct Stat4Accum {
18792 tRowcnt nRow; /* Number of rows in the entire table */
18793 tRowcnt nPSample; /* How often to do a periodic sample */
18794 int nCol; /* Number of columns in index + pk/rowid */
18795 int nKeyCol; /* Number of index columns w/o the pk/rowid */
18796 int mxSample; /* Maximum number of samples to accumulate */
18797 Stat4Sample current; /* Current row as a Stat4Sample */
18798 u32 iPrn; /* Pseudo-random number used for sampling */
18799 Stat4Sample *aBest; /* Array of nCol best samples */
18800 int iMin; /* Index in a[] of entry with minimum score */
18801 int nSample; /* Current number of samples */
18802 int iGet; /* Index of current sample accessed by stat_get() */
18803 Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
18804 sqlite3 *db; /* Database connection, for malloc() */
18805 };
18806
18807 /* Reclaim memory used by a Stat4Sample
18808 */
18809 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
18810 static void sampleClear(sqlite3 *db, Stat4Sample *p){
18811 assert( db!=0 );
18812 if( p->nRowid ){
18813 sqlite3DbFree(db, p->u.aRowid);
18814 p->nRowid = 0;
18815 }
18816 }
18817 #endif
18818
18819 /* Initialize the BLOB value of a ROWID
18820 */
18821 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
18822 static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
18823 assert( db!=0 );
18824 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
18825 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
18826 if( p->u.aRowid ){
18827 p->nRowid = n;
18828 memcpy(p->u.aRowid, pData, n);
18829 }else{
18830 p->nRowid = 0;
18831 }
18832 }
18833 #endif
18834
18835 /* Initialize the INTEGER value of a ROWID.
18836 */
18837 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
18838 static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
18839 assert( db!=0 );
18840 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
18841 p->nRowid = 0;
18842 p->u.iRowid = iRowid;
18843 }
18844 #endif
18845
18846
18847 /*
18848 ** Copy the contents of object (*pFrom) into (*pTo).
18849 */
18850 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
18851 static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
18852 pTo->isPSample = pFrom->isPSample;
18853 pTo->iCol = pFrom->iCol;
18854 pTo->iHash = pFrom->iHash;
18855 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
18856 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
18857 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
18858 if( pFrom->nRowid ){
18859 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
18860 }else{
18861 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
18862 }
18863 }
18864 #endif
18865
18866 /*
18867 ** Reclaim all memory of a Stat4Accum structure.
18868 */
18869 static void stat4Destructor(void *pOld){
18870 Stat4Accum *p = (Stat4Accum*)pOld;
18871 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
18872 int i;
18873 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
18874 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
18875 sampleClear(p->db, &p->current);
18876 #endif
18877 sqlite3DbFree(p->db, p);
18878 }
18879
18880 /*
18881 ** Implementation of the stat_init(N,K,C) SQL function. The three parameters
18882 ** are:
18883 ** N: The number of columns in the index including the rowid/pk (note 1)
18884 ** K: The number of columns in the index excluding the rowid/pk.
18885 ** C: The number of rows in the index (note 2)
18886 **
18887 ** Note 1: In the special case of the covering index that implements a
18888 ** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
18889 ** total number of columns in the table.
18890 **
18891 ** Note 2: C is only used for STAT3 and STAT4.
18892 **
18893 ** For indexes on ordinary rowid tables, N==K+1. But for indexes on
18894 ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
18895 ** PRIMARY KEY of the table. The covering index that implements the
18896 ** original WITHOUT ROWID table as N==K as a special case.
18897 **
18898 ** This routine allocates the Stat4Accum object in heap memory. The return
18899 ** value is a pointer to the Stat4Accum object. The datatype of the
18900 ** return value is BLOB, but it is really just a pointer to the Stat4Accum
18901 ** object.
18902 */
18903 static void statInit(
18904 sqlite3_context *context,
18905 int argc,
18906 sqlite3_value **argv
18907 ){
18908 Stat4Accum *p;
18909 int nCol; /* Number of columns in index being sampled */
18910 int nKeyCol; /* Number of key columns */
18911 int nColUp; /* nCol rounded up for alignment */
18912 int n; /* Bytes of space to allocate */
18913 sqlite3 *db; /* Database connection */
18914 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
18915 int mxSample = SQLITE_STAT4_SAMPLES;
18916 #endif
18917
18918 /* Decode the three function arguments */
18919 UNUSED_PARAMETER(argc);
18920 nCol = sqlite3_value_int(argv[0]);
18921 assert( nCol>0 );
18922 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
18923 nKeyCol = sqlite3_value_int(argv[1]);
18924 assert( nKeyCol<=nCol );
18925 assert( nKeyCol>0 );
18926
18927 /* Allocate the space required for the Stat4Accum object */
18928 n = sizeof(*p)
18929 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
18930 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
18931 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
18932 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
18933 + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
18934 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
18935 #endif
18936 ;
18937 db = sqlite3_context_db_handle(context);
18938 p = sqlite3DbMallocZero(db, n);
18939 if( p==0 ){
18940 sqlite3_result_error_nomem(context);
18941 return;
18942 }
18943
18944 p->db = db;
18945 p->nRow = 0;
18946 p->nCol = nCol;
18947 p->nKeyCol = nKeyCol;
18948 p->current.anDLt = (tRowcnt*)&p[1];
18949 p->current.anEq = &p->current.anDLt[nColUp];
18950
18951 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
18952 {
18953 u8 *pSpace; /* Allocated space not yet assigned */
18954 int i; /* Used to iterate through p->aSample[] */
18955
18956 p->iGet = -1;
18957 p->mxSample = mxSample;
18958 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
18959 p->current.anLt = &p->current.anEq[nColUp];
18960 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
18961
18962 /* Set up the Stat4Accum.a[] and aBest[] arrays */
18963 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
18964 p->aBest = &p->a[mxSample];
18965 pSpace = (u8*)(&p->a[mxSample+nCol]);
18966 for(i=0; i<(mxSample+nCol); i++){
18967 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
18968 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
18969 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
18970 }
18971 assert( (pSpace - (u8*)p)==n );
18972
18973 for(i=0; i<nCol; i++){
18974 p->aBest[i].iCol = i;
18975 }
18976 }
18977 #endif
18978
18979 /* Return a pointer to the allocated object to the caller. Note that
18980 ** only the pointer (the 2nd parameter) matters. The size of the object
18981 ** (given by the 3rd parameter) is never used and can be any positive
18982 ** value. */
18983 sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
18984 }
18985 static const FuncDef statInitFuncdef = {
18986 2+IsStat34, /* nArg */
18987 SQLITE_UTF8, /* funcFlags */
18988 0, /* pUserData */
18989 0, /* pNext */
18990 statInit, /* xSFunc */
18991 0, /* xFinalize */
18992 "stat_init", /* zName */
18993 {0}
18994 };
18995
18996 #ifdef SQLITE_ENABLE_STAT4
18997 /*
18998 ** pNew and pOld are both candidate non-periodic samples selected for
18999 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
19000 ** considering only any trailing columns and the sample hash value, this
19001 ** function returns true if sample pNew is to be preferred over pOld.
19002 ** In other words, if we assume that the cardinalities of the selected
19003 ** column for pNew and pOld are equal, is pNew to be preferred over pOld.
19004 **
19005 ** This function assumes that for each argument sample, the contents of
19006 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
19007 */
19008 static int sampleIsBetterPost(
19009 Stat4Accum *pAccum,
19010 Stat4Sample *pNew,
19011 Stat4Sample *pOld
19012 ){
19013 int nCol = pAccum->nCol;
19014 int i;
19015 assert( pNew->iCol==pOld->iCol );
19016 for(i=pNew->iCol+1; i<nCol; i++){
19017 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
19018 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
19019 }
19020 if( pNew->iHash>pOld->iHash ) return 1;
19021 return 0;
19022 }
19023 #endif
19024
19025 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19026 /*
19027 ** Return true if pNew is to be preferred over pOld.
19028 **
19029 ** This function assumes that for each argument sample, the contents of
19030 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
19031 */
19032 static int sampleIsBetter(
19033 Stat4Accum *pAccum,
19034 Stat4Sample *pNew,
19035 Stat4Sample *pOld
19036 ){
19037 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
19038 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
19039
19040 assert( pOld->isPSample==0 && pNew->isPSample==0 );
19041 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
19042
19043 if( (nEqNew>nEqOld) ) return 1;
19044 #ifdef SQLITE_ENABLE_STAT4
19045 if( nEqNew==nEqOld ){
19046 if( pNew->iCol<pOld->iCol ) return 1;
19047 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
19048 }
19049 return 0;
19050 #else
19051 return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
19052 #endif
19053 }
19054
19055 /*
19056 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
19057 ** remove the least desirable sample from p->a[] to make room.
19058 */
19059 static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
19060 Stat4Sample *pSample = 0;
19061 int i;
19062
19063 assert( IsStat4 || nEqZero==0 );
19064
19065 #ifdef SQLITE_ENABLE_STAT4
19066 if( pNew->isPSample==0 ){
19067 Stat4Sample *pUpgrade = 0;
19068 assert( pNew->anEq[pNew->iCol]>0 );
19069
19070 /* This sample is being added because the prefix that ends in column
19071 ** iCol occurs many times in the table. However, if we have already
19072 ** added a sample that shares this prefix, there is no need to add
19073 ** this one. Instead, upgrade the priority of the highest priority
19074 ** existing sample that shares this prefix. */
19075 for(i=p->nSample-1; i>=0; i--){
19076 Stat4Sample *pOld = &p->a[i];
19077 if( pOld->anEq[pNew->iCol]==0 ){
19078 if( pOld->isPSample ) return;
19079 assert( pOld->iCol>pNew->iCol );
19080 assert( sampleIsBetter(p, pNew, pOld) );
19081 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
19082 pUpgrade = pOld;
19083 }
19084 }
19085 }
19086 if( pUpgrade ){
19087 pUpgrade->iCol = pNew->iCol;
19088 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
19089 goto find_new_min;
19090 }
19091 }
19092 #endif
19093
19094 /* If necessary, remove sample iMin to make room for the new sample. */
19095 if( p->nSample>=p->mxSample ){
19096 Stat4Sample *pMin = &p->a[p->iMin];
19097 tRowcnt *anEq = pMin->anEq;
19098 tRowcnt *anLt = pMin->anLt;
19099 tRowcnt *anDLt = pMin->anDLt;
19100 sampleClear(p->db, pMin);
19101 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
19102 pSample = &p->a[p->nSample-1];
19103 pSample->nRowid = 0;
19104 pSample->anEq = anEq;
19105 pSample->anDLt = anDLt;
19106 pSample->anLt = anLt;
19107 p->nSample = p->mxSample-1;
19108 }
19109
19110 /* The "rows less-than" for the rowid column must be greater than that
19111 ** for the last sample in the p->a[] array. Otherwise, the samples would
19112 ** be out of order. */
19113 #ifdef SQLITE_ENABLE_STAT4
19114 assert( p->nSample==0
19115 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
19116 #endif
19117
19118 /* Insert the new sample */
19119 pSample = &p->a[p->nSample];
19120 sampleCopy(p, pSample, pNew);
19121 p->nSample++;
19122
19123 /* Zero the first nEqZero entries in the anEq[] array. */
19124 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
19125
19126 #ifdef SQLITE_ENABLE_STAT4
19127 find_new_min:
19128 #endif
19129 if( p->nSample>=p->mxSample ){
19130 int iMin = -1;
19131 for(i=0; i<p->mxSample; i++){
19132 if( p->a[i].isPSample ) continue;
19133 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
19134 iMin = i;
19135 }
19136 }
19137 assert( iMin>=0 );
19138 p->iMin = iMin;
19139 }
19140 }
19141 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
19142
19143 /*
19144 ** Field iChng of the index being scanned has changed. So at this point
19145 ** p->current contains a sample that reflects the previous row of the
19146 ** index. The value of anEq[iChng] and subsequent anEq[] elements are
19147 ** correct at this point.
19148 */
19149 static void samplePushPrevious(Stat4Accum *p, int iChng){
19150 #ifdef SQLITE_ENABLE_STAT4
19151 int i;
19152
19153 /* Check if any samples from the aBest[] array should be pushed
19154 ** into IndexSample.a[] at this point. */
19155 for(i=(p->nCol-2); i>=iChng; i--){
19156 Stat4Sample *pBest = &p->aBest[i];
19157 pBest->anEq[i] = p->current.anEq[i];
19158 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
19159 sampleInsert(p, pBest, i);
19160 }
19161 }
19162
19163 /* Update the anEq[] fields of any samples already collected. */
19164 for(i=p->nSample-1; i>=0; i--){
19165 int j;
19166 for(j=iChng; j<p->nCol; j++){
19167 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
19168 }
19169 }
19170 #endif
19171
19172 #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
19173 if( iChng==0 ){
19174 tRowcnt nLt = p->current.anLt[0];
19175 tRowcnt nEq = p->current.anEq[0];
19176
19177 /* Check if this is to be a periodic sample. If so, add it. */
19178 if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
19179 p->current.isPSample = 1;
19180 sampleInsert(p, &p->current, 0);
19181 p->current.isPSample = 0;
19182 }else
19183
19184 /* Or if it is a non-periodic sample. Add it in this case too. */
19185 if( p->nSample<p->mxSample
19186 || sampleIsBetter(p, &p->current, &p->a[p->iMin])
19187 ){
19188 sampleInsert(p, &p->current, 0);
19189 }
19190 }
19191 #endif
19192
19193 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
19194 UNUSED_PARAMETER( p );
19195 UNUSED_PARAMETER( iChng );
19196 #endif
19197 }
19198
19199 /*
19200 ** Implementation of the stat_push SQL function: stat_push(P,C,R)
19201 ** Arguments:
19202 **
19203 ** P Pointer to the Stat4Accum object created by stat_init()
19204 ** C Index of left-most column to differ from previous row
19205 ** R Rowid for the current row. Might be a key record for
19206 ** WITHOUT ROWID tables.
19207 **
19208 ** This SQL function always returns NULL. It's purpose it to accumulate
19209 ** statistical data and/or samples in the Stat4Accum object about the
19210 ** index being analyzed. The stat_get() SQL function will later be used to
19211 ** extract relevant information for constructing the sqlite_statN tables.
19212 **
19213 ** The R parameter is only used for STAT3 and STAT4
19214 */
19215 static void statPush(
19216 sqlite3_context *context,
19217 int argc,
19218 sqlite3_value **argv
19219 ){
19220 int i;
19221
19222 /* The three function arguments */
19223 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
19224 int iChng = sqlite3_value_int(argv[1]);
19225
19226 UNUSED_PARAMETER( argc );
19227 UNUSED_PARAMETER( context );
19228 assert( p->nCol>0 );
19229 assert( iChng<p->nCol );
19230
19231 if( p->nRow==0 ){
19232 /* This is the first call to this function. Do initialization. */
19233 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
19234 }else{
19235 /* Second and subsequent calls get processed here */
19236 samplePushPrevious(p, iChng);
19237
19238 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
19239 ** to the current row of the index. */
19240 for(i=0; i<iChng; i++){
19241 p->current.anEq[i]++;
19242 }
19243 for(i=iChng; i<p->nCol; i++){
19244 p->current.anDLt[i]++;
19245 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19246 p->current.anLt[i] += p->current.anEq[i];
19247 #endif
19248 p->current.anEq[i] = 1;
19249 }
19250 }
19251 p->nRow++;
19252 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19253 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
19254 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
19255 }else{
19256 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
19257 sqlite3_value_blob(argv[2]));
19258 }
19259 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
19260 #endif
19261
19262 #ifdef SQLITE_ENABLE_STAT4
19263 {
19264 tRowcnt nLt = p->current.anLt[p->nCol-1];
19265
19266 /* Check if this is to be a periodic sample. If so, add it. */
19267 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
19268 p->current.isPSample = 1;
19269 p->current.iCol = 0;
19270 sampleInsert(p, &p->current, p->nCol-1);
19271 p->current.isPSample = 0;
19272 }
19273
19274 /* Update the aBest[] array. */
19275 for(i=0; i<(p->nCol-1); i++){
19276 p->current.iCol = i;
19277 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
19278 sampleCopy(p, &p->aBest[i], &p->current);
19279 }
19280 }
19281 }
19282 #endif
19283 }
19284 static const FuncDef statPushFuncdef = {
19285 2+IsStat34, /* nArg */
19286 SQLITE_UTF8, /* funcFlags */
19287 0, /* pUserData */
19288 0, /* pNext */
19289 statPush, /* xSFunc */
19290 0, /* xFinalize */
19291 "stat_push", /* zName */
19292 {0}
19293 };
19294
19295 #define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
19296 #define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
19297 #define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
19298 #define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
19299 #define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
19300
19301 /*
19302 ** Implementation of the stat_get(P,J) SQL function. This routine is
19303 ** used to query statistical information that has been gathered into
19304 ** the Stat4Accum object by prior calls to stat_push(). The P parameter
19305 ** has type BLOB but it is really just a pointer to the Stat4Accum object.
19306 ** The content to returned is determined by the parameter J
19307 ** which is one of the STAT_GET_xxxx values defined above.
19308 **
19309 ** The stat_get(P,J) function is not available to generic SQL. It is
19310 ** inserted as part of a manually constructed bytecode program. (See
19311 ** the callStatGet() routine below.) It is guaranteed that the P
19312 ** parameter will always be a poiner to a Stat4Accum object, never a
19313 ** NULL.
19314 **
19315 ** If neither STAT3 nor STAT4 are enabled, then J is always
19316 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
19317 ** a one-parameter function, stat_get(P), that always returns the
19318 ** stat1 table entry information.
19319 */
19320 static void statGet(
19321 sqlite3_context *context,
19322 int argc,
19323 sqlite3_value **argv
19324 ){
19325 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
19326 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19327 /* STAT3 and STAT4 have a parameter on this routine. */
19328 int eCall = sqlite3_value_int(argv[1]);
19329 assert( argc==2 );
19330 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
19331 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
19332 || eCall==STAT_GET_NDLT
19333 );
19334 if( eCall==STAT_GET_STAT1 )
19335 #else
19336 assert( argc==1 );
19337 #endif
19338 {
19339 /* Return the value to store in the "stat" column of the sqlite_stat1
19340 ** table for this index.
19341 **
19342 ** The value is a string composed of a list of integers describing
19343 ** the index. The first integer in the list is the total number of
19344 ** entries in the index. There is one additional integer in the list
19345 ** for each indexed column. This additional integer is an estimate of
19346 ** the number of rows matched by a stabbing query on the index using
19347 ** a key with the corresponding number of fields. In other words,
19348 ** if the index is on columns (a,b) and the sqlite_stat1 value is
19349 ** "100 10 2", then SQLite estimates that:
19350 **
19351 ** * the index contains 100 rows,
19352 ** * "WHERE a=?" matches 10 rows, and
19353 ** * "WHERE a=? AND b=?" matches 2 rows.
19354 **
19355 ** If D is the count of distinct values and K is the total number of
19356 ** rows, then each estimate is computed as:
19357 **
19358 ** I = (K+D-1)/D
19359 */
19360 char *z;
19361 int i;
19362
19363 char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
19364 if( zRet==0 ){
19365 sqlite3_result_error_nomem(context);
19366 return;
19367 }
19368
19369 sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
19370 z = zRet + sqlite3Strlen30(zRet);
19371 for(i=0; i<p->nKeyCol; i++){
19372 u64 nDistinct = p->current.anDLt[i] + 1;
19373 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
19374 sqlite3_snprintf(24, z, " %llu", iVal);
19375 z += sqlite3Strlen30(z);
19376 assert( p->current.anEq[i] );
19377 }
19378 assert( z[0]=='\0' && z>zRet );
19379
19380 sqlite3_result_text(context, zRet, -1, sqlite3_free);
19381 }
19382 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19383 else if( eCall==STAT_GET_ROWID ){
19384 if( p->iGet<0 ){
19385 samplePushPrevious(p, 0);
19386 p->iGet = 0;
19387 }
19388 if( p->iGet<p->nSample ){
19389 Stat4Sample *pS = p->a + p->iGet;
19390 if( pS->nRowid==0 ){
19391 sqlite3_result_int64(context, pS->u.iRowid);
19392 }else{
19393 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
19394 SQLITE_TRANSIENT);
19395 }
19396 }
19397 }else{
19398 tRowcnt *aCnt = 0;
19399
19400 assert( p->iGet<p->nSample );
19401 switch( eCall ){
19402 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
19403 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
19404 default: {
19405 aCnt = p->a[p->iGet].anDLt;
19406 p->iGet++;
19407 break;
19408 }
19409 }
19410
19411 if( IsStat3 ){
19412 sqlite3_result_int64(context, (i64)aCnt[0]);
19413 }else{
19414 char *zRet = sqlite3MallocZero(p->nCol * 25);
19415 if( zRet==0 ){
19416 sqlite3_result_error_nomem(context);
19417 }else{
19418 int i;
19419 char *z = zRet;
19420 for(i=0; i<p->nCol; i++){
19421 sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
19422 z += sqlite3Strlen30(z);
19423 }
19424 assert( z[0]=='\0' && z>zRet );
19425 z[-1] = '\0';
19426 sqlite3_result_text(context, zRet, -1, sqlite3_free);
19427 }
19428 }
19429 }
19430 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
19431 #ifndef SQLITE_DEBUG
19432 UNUSED_PARAMETER( argc );
19433 #endif
19434 }
19435 static const FuncDef statGetFuncdef = {
19436 1+IsStat34, /* nArg */
19437 SQLITE_UTF8, /* funcFlags */
19438 0, /* pUserData */
19439 0, /* pNext */
19440 statGet, /* xSFunc */
19441 0, /* xFinalize */
19442 "stat_get", /* zName */
19443 {0}
19444 };
19445
19446 static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
19447 assert( regOut!=regStat4 && regOut!=regStat4+1 );
19448 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19449 sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
19450 #elif SQLITE_DEBUG
19451 assert( iParam==STAT_GET_STAT1 );
19452 #else
19453 UNUSED_PARAMETER( iParam );
19454 #endif
19455 sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut,
19456 (char*)&statGetFuncdef, P4_FUNCDEF);
19457 sqlite3VdbeChangeP5(v, 1 + IsStat34);
19458 }
19459
19460 /*
19461 ** Generate code to do an analysis of all indices associated with
19462 ** a single table.
19463 */
19464 static void analyzeOneTable(
19465 Parse *pParse, /* Parser context */
19466 Table *pTab, /* Table whose indices are to be analyzed */
19467 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
19468 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
19469 int iMem, /* Available memory locations begin here */
19470 int iTab /* Next available cursor */
19471 ){
19472 sqlite3 *db = pParse->db; /* Database handle */
19473 Index *pIdx; /* An index to being analyzed */
19474 int iIdxCur; /* Cursor open on index being analyzed */
19475 int iTabCur; /* Table cursor */
19476 Vdbe *v; /* The virtual machine being built up */
19477 int i; /* Loop counter */
19478 int jZeroRows = -1; /* Jump from here if number of rows is zero */
19479 int iDb; /* Index of database containing pTab */
19480 u8 needTableCnt = 1; /* True to count the table */
19481 int regNewRowid = iMem++; /* Rowid for the inserted record */
19482 int regStat4 = iMem++; /* Register to hold Stat4Accum object */
19483 int regChng = iMem++; /* Index of changed index field */
19484 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19485 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
19486 #endif
19487 int regTemp = iMem++; /* Temporary use register */
19488 int regTabname = iMem++; /* Register containing table name */
19489 int regIdxname = iMem++; /* Register containing index name */
19490 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
19491 int regPrev = iMem; /* MUST BE LAST (see below) */
19492
19493 pParse->nMem = MAX(pParse->nMem, iMem);
19494 v = sqlite3GetVdbe(pParse);
19495 if( v==0 || NEVER(pTab==0) ){
19496 return;
19497 }
19498 if( pTab->tnum==0 ){
19499 /* Do not gather statistics on views or virtual tables */
19500 return;
19501 }
19502 if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){
19503 /* Do not gather statistics on system tables */
19504 return;
19505 }
19506 assert( sqlite3BtreeHoldsAllMutexes(db) );
19507 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
19508 assert( iDb>=0 );
19509 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
19510 #ifndef SQLITE_OMIT_AUTHORIZATION
19511 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
19512 db->aDb[iDb].zDbSName ) ){
19513 return;
19514 }
19515 #endif
19516
19517 /* Establish a read-lock on the table at the shared-cache level.
19518 ** Open a read-only cursor on the table. Also allocate a cursor number
19519 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
19520 ** this time though. */
19521 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
19522 iTabCur = iTab++;
19523 iIdxCur = iTab++;
19524 pParse->nTab = MAX(pParse->nTab, iTab);
19525 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
19526 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
19527
19528 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
19529 int nCol; /* Number of columns in pIdx. "N" */
19530 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
19531 int addrNextRow; /* Address of "next_row:" */
19532 const char *zIdxName; /* Name of the index */
19533 int nColTest; /* Number of columns to test for changes */
19534
19535 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
19536 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
19537 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
19538 nCol = pIdx->nKeyCol;
19539 zIdxName = pTab->zName;
19540 nColTest = nCol - 1;
19541 }else{
19542 nCol = pIdx->nColumn;
19543 zIdxName = pIdx->zName;
19544 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
19545 }
19546
19547 /* Populate the register containing the index name. */
19548 sqlite3VdbeLoadString(v, regIdxname, zIdxName);
19549 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
19550
19551 /*
19552 ** Pseudo-code for loop that calls stat_push():
19553 **
19554 ** Rewind csr
19555 ** if eof(csr) goto end_of_scan;
19556 ** regChng = 0
19557 ** goto chng_addr_0;
19558 **
19559 ** next_row:
19560 ** regChng = 0
19561 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
19562 ** regChng = 1
19563 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
19564 ** ...
19565 ** regChng = N
19566 ** goto chng_addr_N
19567 **
19568 ** chng_addr_0:
19569 ** regPrev(0) = idx(0)
19570 ** chng_addr_1:
19571 ** regPrev(1) = idx(1)
19572 ** ...
19573 **
19574 ** endDistinctTest:
19575 ** regRowid = idx(rowid)
19576 ** stat_push(P, regChng, regRowid)
19577 ** Next csr
19578 ** if !eof(csr) goto next_row;
19579 **
19580 ** end_of_scan:
19581 */
19582
19583 /* Make sure there are enough memory cells allocated to accommodate
19584 ** the regPrev array and a trailing rowid (the rowid slot is required
19585 ** when building a record to insert into the sample column of
19586 ** the sqlite_stat4 table. */
19587 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
19588
19589 /* Open a read-only cursor on the index being analyzed. */
19590 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
19591 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
19592 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
19593 VdbeComment((v, "%s", pIdx->zName));
19594
19595 /* Invoke the stat_init() function. The arguments are:
19596 **
19597 ** (1) the number of columns in the index including the rowid
19598 ** (or for a WITHOUT ROWID table, the number of PK columns),
19599 ** (2) the number of columns in the key without the rowid/pk
19600 ** (3) the number of rows in the index,
19601 **
19602 **
19603 ** The third argument is only used for STAT3 and STAT4
19604 */
19605 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19606 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
19607 #endif
19608 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
19609 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
19610 sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4,
19611 (char*)&statInitFuncdef, P4_FUNCDEF);
19612 sqlite3VdbeChangeP5(v, 2+IsStat34);
19613
19614 /* Implementation of the following:
19615 **
19616 ** Rewind csr
19617 ** if eof(csr) goto end_of_scan;
19618 ** regChng = 0
19619 ** goto next_push_0;
19620 **
19621 */
19622 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
19623 VdbeCoverage(v);
19624 sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
19625 addrNextRow = sqlite3VdbeCurrentAddr(v);
19626
19627 if( nColTest>0 ){
19628 int endDistinctTest = sqlite3VdbeMakeLabel(v);
19629 int *aGotoChng; /* Array of jump instruction addresses */
19630 aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
19631 if( aGotoChng==0 ) continue;
19632
19633 /*
19634 ** next_row:
19635 ** regChng = 0
19636 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
19637 ** regChng = 1
19638 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
19639 ** ...
19640 ** regChng = N
19641 ** goto endDistinctTest
19642 */
19643 sqlite3VdbeAddOp0(v, OP_Goto);
19644 addrNextRow = sqlite3VdbeCurrentAddr(v);
19645 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
19646 /* For a single-column UNIQUE index, once we have found a non-NULL
19647 ** row, we know that all the rest will be distinct, so skip
19648 ** subsequent distinctness tests. */
19649 sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
19650 VdbeCoverage(v);
19651 }
19652 for(i=0; i<nColTest; i++){
19653 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
19654 sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
19655 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
19656 aGotoChng[i] =
19657 sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
19658 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
19659 VdbeCoverage(v);
19660 }
19661 sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
19662 sqlite3VdbeGoto(v, endDistinctTest);
19663
19664
19665 /*
19666 ** chng_addr_0:
19667 ** regPrev(0) = idx(0)
19668 ** chng_addr_1:
19669 ** regPrev(1) = idx(1)
19670 ** ...
19671 */
19672 sqlite3VdbeJumpHere(v, addrNextRow-1);
19673 for(i=0; i<nColTest; i++){
19674 sqlite3VdbeJumpHere(v, aGotoChng[i]);
19675 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
19676 }
19677 sqlite3VdbeResolveLabel(v, endDistinctTest);
19678 sqlite3DbFree(db, aGotoChng);
19679 }
19680
19681 /*
19682 ** chng_addr_N:
19683 ** regRowid = idx(rowid) // STAT34 only
19684 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
19685 ** Next csr
19686 ** if !eof(csr) goto next_row;
19687 */
19688 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19689 assert( regRowid==(regStat4+2) );
19690 if( HasRowid(pTab) ){
19691 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
19692 }else{
19693 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
19694 int j, k, regKey;
19695 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
19696 for(j=0; j<pPk->nKeyCol; j++){
19697 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
19698 assert( k>=0 && k<pTab->nCol );
19699 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
19700 VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
19701 }
19702 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
19703 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
19704 }
19705 #endif
19706 assert( regChng==(regStat4+1) );
19707 sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp,
19708 (char*)&statPushFuncdef, P4_FUNCDEF);
19709 sqlite3VdbeChangeP5(v, 2+IsStat34);
19710 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
19711
19712 /* Add the entry to the stat1 table. */
19713 callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
19714 assert( "BBB"[0]==SQLITE_AFF_TEXT );
19715 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
19716 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
19717 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
19718 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
19719
19720 /* Add the entries to the stat3 or stat4 table. */
19721 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19722 {
19723 int regEq = regStat1;
19724 int regLt = regStat1+1;
19725 int regDLt = regStat1+2;
19726 int regSample = regStat1+3;
19727 int regCol = regStat1+4;
19728 int regSampleRowid = regCol + nCol;
19729 int addrNext;
19730 int addrIsNull;
19731 u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
19732
19733 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
19734
19735 addrNext = sqlite3VdbeCurrentAddr(v);
19736 callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
19737 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
19738 VdbeCoverage(v);
19739 callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
19740 callStatGet(v, regStat4, STAT_GET_NLT, regLt);
19741 callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
19742 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
19743 /* We know that the regSampleRowid row exists because it was read by
19744 ** the previous loop. Thus the not-found jump of seekOp will never
19745 ** be taken */
19746 VdbeCoverageNeverTaken(v);
19747 #ifdef SQLITE_ENABLE_STAT3
19748 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample);
19749 #else
19750 for(i=0; i<nCol; i++){
19751 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
19752 }
19753 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
19754 #endif
19755 sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
19756 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
19757 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
19758 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
19759 sqlite3VdbeJumpHere(v, addrIsNull);
19760 }
19761 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
19762
19763 /* End of analysis */
19764 sqlite3VdbeJumpHere(v, addrRewind);
19765 }
19766
19767
19768 /* Create a single sqlite_stat1 entry containing NULL as the index
19769 ** name and the row count as the content.
19770 */
19771 if( pOnlyIdx==0 && needTableCnt ){
19772 VdbeComment((v, "%s", pTab->zName));
19773 sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
19774 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
19775 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
19776 assert( "BBB"[0]==SQLITE_AFF_TEXT );
19777 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
19778 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
19779 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
19780 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
19781 sqlite3VdbeJumpHere(v, jZeroRows);
19782 }
19783 }
19784
19785
19786 /*
19787 ** Generate code that will cause the most recent index analysis to
19788 ** be loaded into internal hash tables where is can be used.
19789 */
19790 static void loadAnalysis(Parse *pParse, int iDb){
19791 Vdbe *v = sqlite3GetVdbe(pParse);
19792 if( v ){
19793 sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
19794 }
19795 }
19796
19797 /*
19798 ** Generate code that will do an analysis of an entire database
19799 */
19800 static void analyzeDatabase(Parse *pParse, int iDb){
19801 sqlite3 *db = pParse->db;
19802 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
19803 HashElem *k;
19804 int iStatCur;
19805 int iMem;
19806 int iTab;
19807
19808 sqlite3BeginWriteOperation(pParse, 0, iDb);
19809 iStatCur = pParse->nTab;
19810 pParse->nTab += 3;
19811 openStatTable(pParse, iDb, iStatCur, 0, 0);
19812 iMem = pParse->nMem+1;
19813 iTab = pParse->nTab;
19814 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
19815 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
19816 Table *pTab = (Table*)sqliteHashData(k);
19817 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
19818 }
19819 loadAnalysis(pParse, iDb);
19820 }
19821
19822 /*
19823 ** Generate code that will do an analysis of a single table in
19824 ** a database. If pOnlyIdx is not NULL then it is a single index
19825 ** in pTab that should be analyzed.
19826 */
19827 static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
19828 int iDb;
19829 int iStatCur;
19830
19831 assert( pTab!=0 );
19832 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
19833 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
19834 sqlite3BeginWriteOperation(pParse, 0, iDb);
19835 iStatCur = pParse->nTab;
19836 pParse->nTab += 3;
19837 if( pOnlyIdx ){
19838 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
19839 }else{
19840 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
19841 }
19842 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
19843 loadAnalysis(pParse, iDb);
19844 }
19845
19846 /*
19847 ** Generate code for the ANALYZE command. The parser calls this routine
19848 ** when it recognizes an ANALYZE command.
19849 **
19850 ** ANALYZE -- 1
19851 ** ANALYZE <database> -- 2
19852 ** ANALYZE ?<database>.?<tablename> -- 3
19853 **
19854 ** Form 1 causes all indices in all attached databases to be analyzed.
19855 ** Form 2 analyzes all indices the single database named.
19856 ** Form 3 analyzes all indices associated with the named table.
19857 */
19858 SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
19859 sqlite3 *db = pParse->db;
19860 int iDb;
19861 int i;
19862 char *z, *zDb;
19863 Table *pTab;
19864 Index *pIdx;
19865 Token *pTableName;
19866 Vdbe *v;
19867
19868 /* Read the database schema. If an error occurs, leave an error message
19869 ** and code in pParse and return NULL. */
19870 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
19871 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
19872 return;
19873 }
19874
19875 assert( pName2!=0 || pName1==0 );
19876 if( pName1==0 ){
19877 /* Form 1: Analyze everything */
19878 for(i=0; i<db->nDb; i++){
19879 if( i==1 ) continue; /* Do not analyze the TEMP database */
19880 analyzeDatabase(pParse, i);
19881 }
19882 }else if( pName2->n==0 ){
19883 /* Form 2: Analyze the database or table named */
19884 iDb = sqlite3FindDb(db, pName1);
19885 if( iDb>=0 ){
19886 analyzeDatabase(pParse, iDb);
19887 }else{
19888 z = sqlite3NameFromToken(db, pName1);
19889 if( z ){
19890 if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
19891 analyzeTable(pParse, pIdx->pTable, pIdx);
19892 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
19893 analyzeTable(pParse, pTab, 0);
19894 }
19895 sqlite3DbFree(db, z);
19896 }
19897 }
19898 }else{
19899 /* Form 3: Analyze the fully qualified table name */
19900 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
19901 if( iDb>=0 ){
19902 zDb = db->aDb[iDb].zDbSName;
19903 z = sqlite3NameFromToken(db, pTableName);
19904 if( z ){
19905 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
19906 analyzeTable(pParse, pIdx->pTable, pIdx);
19907 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
19908 analyzeTable(pParse, pTab, 0);
19909 }
19910 sqlite3DbFree(db, z);
19911 }
19912 }
19913 }
19914 v = sqlite3GetVdbe(pParse);
19915 if( v ) sqlite3VdbeAddOp0(v, OP_Expire);
19916 }
19917
19918 /*
19919 ** Used to pass information from the analyzer reader through to the
19920 ** callback routine.
19921 */
19922 typedef struct analysisInfo analysisInfo;
19923 struct analysisInfo {
19924 sqlite3 *db;
19925 const char *zDatabase;
19926 };
19927
19928 /*
19929 ** The first argument points to a nul-terminated string containing a
19930 ** list of space separated integers. Read the first nOut of these into
19931 ** the array aOut[].
19932 */
19933 static void decodeIntArray(
19934 char *zIntArray, /* String containing int array to decode */
19935 int nOut, /* Number of slots in aOut[] */
19936 tRowcnt *aOut, /* Store integers here */
19937 LogEst *aLog, /* Or, if aOut==0, here */
19938 Index *pIndex /* Handle extra flags for this index, if not NULL */
19939 ){
19940 char *z = zIntArray;
19941 int c;
19942 int i;
19943 tRowcnt v;
19944
19945 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19946 if( z==0 ) z = "";
19947 #else
19948 assert( z!=0 );
19949 #endif
19950 for(i=0; *z && i<nOut; i++){
19951 v = 0;
19952 while( (c=z[0])>='0' && c<='9' ){
19953 v = v*10 + c - '0';
19954 z++;
19955 }
19956 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19957 if( aOut ) aOut[i] = v;
19958 if( aLog ) aLog[i] = sqlite3LogEst(v);
19959 #else
19960 assert( aOut==0 );
19961 UNUSED_PARAMETER(aOut);
19962 assert( aLog!=0 );
19963 aLog[i] = sqlite3LogEst(v);
19964 #endif
19965 if( *z==' ' ) z++;
19966 }
19967 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
19968 assert( pIndex!=0 ); {
19969 #else
19970 if( pIndex ){
19971 #endif
19972 pIndex->bUnordered = 0;
19973 pIndex->noSkipScan = 0;
19974 while( z[0] ){
19975 if( sqlite3_strglob("unordered*", z)==0 ){
19976 pIndex->bUnordered = 1;
19977 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
19978 pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
19979 }else if( sqlite3_strglob("noskipscan*", z)==0 ){
19980 pIndex->noSkipScan = 1;
19981 }
19982 #ifdef SQLITE_ENABLE_COSTMULT
19983 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
19984 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
19985 }
19986 #endif
19987 while( z[0]!=0 && z[0]!=' ' ) z++;
19988 while( z[0]==' ' ) z++;
19989 }
19990 }
19991 }
19992
19993 /*
19994 ** This callback is invoked once for each index when reading the
19995 ** sqlite_stat1 table.
19996 **
19997 ** argv[0] = name of the table
19998 ** argv[1] = name of the index (might be NULL)
19999 ** argv[2] = results of analysis - on integer for each column
20000 **
20001 ** Entries for which argv[1]==NULL simply record the number of rows in
20002 ** the table.
20003 */
20004 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
20005 analysisInfo *pInfo = (analysisInfo*)pData;
20006 Index *pIndex;
20007 Table *pTable;
20008 const char *z;
20009
20010 assert( argc==3 );
20011 UNUSED_PARAMETER2(NotUsed, argc);
20012
20013 if( argv==0 || argv[0]==0 || argv[2]==0 ){
20014 return 0;
20015 }
20016 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
20017 if( pTable==0 ){
20018 return 0;
20019 }
20020 if( argv[1]==0 ){
20021 pIndex = 0;
20022 }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
20023 pIndex = sqlite3PrimaryKeyIndex(pTable);
20024 }else{
20025 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
20026 }
20027 z = argv[2];
20028
20029 if( pIndex ){
20030 tRowcnt *aiRowEst = 0;
20031 int nCol = pIndex->nKeyCol+1;
20032 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
20033 /* Index.aiRowEst may already be set here if there are duplicate
20034 ** sqlite_stat1 entries for this index. In that case just clobber
20035 ** the old data with the new instead of allocating a new array. */
20036 if( pIndex->aiRowEst==0 ){
20037 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
20038 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
20039 }
20040 aiRowEst = pIndex->aiRowEst;
20041 #endif
20042 pIndex->bUnordered = 0;
20043 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
20044 if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
20045 }else{
20046 Index fakeIdx;
20047 fakeIdx.szIdxRow = pTable->szTabRow;
20048 #ifdef SQLITE_ENABLE_COSTMULT
20049 fakeIdx.pTable = pTable;
20050 #endif
20051 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
20052 pTable->szTabRow = fakeIdx.szIdxRow;
20053 }
20054
20055 return 0;
20056 }
20057
20058 /*
20059 ** If the Index.aSample variable is not NULL, delete the aSample[] array
20060 ** and its contents.
20061 */
20062 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
20063 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
20064 if( pIdx->aSample ){
20065 int j;
20066 for(j=0; j<pIdx->nSample; j++){
20067 IndexSample *p = &pIdx->aSample[j];
20068 sqlite3DbFree(db, p->p);
20069 }
20070 sqlite3DbFree(db, pIdx->aSample);
20071 }
20072 if( db && db->pnBytesFreed==0 ){
20073 pIdx->nSample = 0;
20074 pIdx->aSample = 0;
20075 }
20076 #else
20077 UNUSED_PARAMETER(db);
20078 UNUSED_PARAMETER(pIdx);
20079 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
20080 }
20081
20082 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
20083 /*
20084 ** Populate the pIdx->aAvgEq[] array based on the samples currently
20085 ** stored in pIdx->aSample[].
20086 */
20087 static void initAvgEq(Index *pIdx){
20088 if( pIdx ){
20089 IndexSample *aSample = pIdx->aSample;
20090 IndexSample *pFinal = &aSample[pIdx->nSample-1];
20091 int iCol;
20092 int nCol = 1;
20093 if( pIdx->nSampleCol>1 ){
20094 /* If this is stat4 data, then calculate aAvgEq[] values for all
20095 ** sample columns except the last. The last is always set to 1, as
20096 ** once the trailing PK fields are considered all index keys are
20097 ** unique. */
20098 nCol = pIdx->nSampleCol-1;
20099 pIdx->aAvgEq[nCol] = 1;
20100 }
20101 for(iCol=0; iCol<nCol; iCol++){
20102 int nSample = pIdx->nSample;
20103 int i; /* Used to iterate through samples */
20104 tRowcnt sumEq = 0; /* Sum of the nEq values */
20105 tRowcnt avgEq = 0;
20106 tRowcnt nRow; /* Number of rows in index */
20107 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
20108 i64 nDist100; /* Number of distinct values in index */
20109
20110 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
20111 nRow = pFinal->anLt[iCol];
20112 nDist100 = (i64)100 * pFinal->anDLt[iCol];
20113 nSample--;
20114 }else{
20115 nRow = pIdx->aiRowEst[0];
20116 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
20117 }
20118 pIdx->nRowEst0 = nRow;
20119
20120 /* Set nSum to the number of distinct (iCol+1) field prefixes that
20121 ** occur in the stat4 table for this index. Set sumEq to the sum of
20122 ** the nEq values for column iCol for the same set (adding the value
20123 ** only once where there exist duplicate prefixes). */
20124 for(i=0; i<nSample; i++){
20125 if( i==(pIdx->nSample-1)
20126 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
20127 ){
20128 sumEq += aSample[i].anEq[iCol];
20129 nSum100 += 100;
20130 }
20131 }
20132
20133 if( nDist100>nSum100 && sumEq<nRow ){
20134 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
20135 }
20136 if( avgEq==0 ) avgEq = 1;
20137 pIdx->aAvgEq[iCol] = avgEq;
20138 }
20139 }
20140 }
20141
20142 /*
20143 ** Look up an index by name. Or, if the name of a WITHOUT ROWID table
20144 ** is supplied instead, find the PRIMARY KEY index for that table.
20145 */
20146 static Index *findIndexOrPrimaryKey(
20147 sqlite3 *db,
20148 const char *zName,
20149 const char *zDb
20150 ){
20151 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
20152 if( pIdx==0 ){
20153 Table *pTab = sqlite3FindTable(db, zName, zDb);
20154 if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
20155 }
20156 return pIdx;
20157 }
20158
20159 /*
20160 ** Load the content from either the sqlite_stat4 or sqlite_stat3 table
20161 ** into the relevant Index.aSample[] arrays.
20162 **
20163 ** Arguments zSql1 and zSql2 must point to SQL statements that return
20164 ** data equivalent to the following (statements are different for stat3,
20165 ** see the caller of this function for details):
20166 **
20167 ** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
20168 ** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
20169 **
20170 ** where %Q is replaced with the database name before the SQL is executed.
20171 */
20172 static int loadStatTbl(
20173 sqlite3 *db, /* Database handle */
20174 int bStat3, /* Assume single column records only */
20175 const char *zSql1, /* SQL statement 1 (see above) */
20176 const char *zSql2, /* SQL statement 2 (see above) */
20177 const char *zDb /* Database name (e.g. "main") */
20178 ){
20179 int rc; /* Result codes from subroutines */
20180 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
20181 char *zSql; /* Text of the SQL statement */
20182 Index *pPrevIdx = 0; /* Previous index in the loop */
20183 IndexSample *pSample; /* A slot in pIdx->aSample[] */
20184
20185 assert( db->lookaside.bDisable );
20186 zSql = sqlite3MPrintf(db, zSql1, zDb);
20187 if( !zSql ){
20188 return SQLITE_NOMEM_BKPT;
20189 }
20190 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
20191 sqlite3DbFree(db, zSql);
20192 if( rc ) return rc;
20193
20194 while( sqlite3_step(pStmt)==SQLITE_ROW ){
20195 int nIdxCol = 1; /* Number of columns in stat4 records */
20196
20197 char *zIndex; /* Index name */
20198 Index *pIdx; /* Pointer to the index object */
20199 int nSample; /* Number of samples */
20200 int nByte; /* Bytes of space required */
20201 int i; /* Bytes of space required */
20202 tRowcnt *pSpace;
20203
20204 zIndex = (char *)sqlite3_column_text(pStmt, 0);
20205 if( zIndex==0 ) continue;
20206 nSample = sqlite3_column_int(pStmt, 1);
20207 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
20208 assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
20209 /* Index.nSample is non-zero at this point if data has already been
20210 ** loaded from the stat4 table. In this case ignore stat3 data. */
20211 if( pIdx==0 || pIdx->nSample ) continue;
20212 if( bStat3==0 ){
20213 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
20214 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
20215 nIdxCol = pIdx->nKeyCol;
20216 }else{
20217 nIdxCol = pIdx->nColumn;
20218 }
20219 }
20220 pIdx->nSampleCol = nIdxCol;
20221 nByte = sizeof(IndexSample) * nSample;
20222 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
20223 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
20224
20225 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
20226 if( pIdx->aSample==0 ){
20227 sqlite3_finalize(pStmt);
20228 return SQLITE_NOMEM_BKPT;
20229 }
20230 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
20231 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
20232 for(i=0; i<nSample; i++){
20233 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
20234 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
20235 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
20236 }
20237 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
20238 }
20239 rc = sqlite3_finalize(pStmt);
20240 if( rc ) return rc;
20241
20242 zSql = sqlite3MPrintf(db, zSql2, zDb);
20243 if( !zSql ){
20244 return SQLITE_NOMEM_BKPT;
20245 }
20246 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
20247 sqlite3DbFree(db, zSql);
20248 if( rc ) return rc;
20249
20250 while( sqlite3_step(pStmt)==SQLITE_ROW ){
20251 char *zIndex; /* Index name */
20252 Index *pIdx; /* Pointer to the index object */
20253 int nCol = 1; /* Number of columns in index */
20254
20255 zIndex = (char *)sqlite3_column_text(pStmt, 0);
20256 if( zIndex==0 ) continue;
20257 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
20258 if( pIdx==0 ) continue;
20259 /* This next condition is true if data has already been loaded from
20260 ** the sqlite_stat4 table. In this case ignore stat3 data. */
20261 nCol = pIdx->nSampleCol;
20262 if( bStat3 && nCol>1 ) continue;
20263 if( pIdx!=pPrevIdx ){
20264 initAvgEq(pPrevIdx);
20265 pPrevIdx = pIdx;
20266 }
20267 pSample = &pIdx->aSample[pIdx->nSample];
20268 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
20269 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
20270 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
20271
20272 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
20273 ** This is in case the sample record is corrupted. In that case, the
20274 ** sqlite3VdbeRecordCompare() may read up to two varints past the
20275 ** end of the allocated buffer before it realizes it is dealing with
20276 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
20277 ** a buffer overread. */
20278 pSample->n = sqlite3_column_bytes(pStmt, 4);
20279 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
20280 if( pSample->p==0 ){
20281 sqlite3_finalize(pStmt);
20282 return SQLITE_NOMEM_BKPT;
20283 }
20284 if( pSample->n ){
20285 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
20286 }
20287 pIdx->nSample++;
20288 }
20289 rc = sqlite3_finalize(pStmt);
20290 if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
20291 return rc;
20292 }
20293
20294 /*
20295 ** Load content from the sqlite_stat4 and sqlite_stat3 tables into
20296 ** the Index.aSample[] arrays of all indices.
20297 */
20298 static int loadStat4(sqlite3 *db, const char *zDb){
20299 int rc = SQLITE_OK; /* Result codes from subroutines */
20300
20301 assert( db->lookaside.bDisable );
20302 if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
20303 rc = loadStatTbl(db, 0,
20304 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
20305 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
20306 zDb
20307 );
20308 }
20309
20310 if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
20311 rc = loadStatTbl(db, 1,
20312 "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
20313 "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
20314 zDb
20315 );
20316 }
20317
20318 return rc;
20319 }
20320 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
20321
20322 /*
20323 ** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
20324 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
20325 ** arrays. The contents of sqlite_stat3/4 are used to populate the
20326 ** Index.aSample[] arrays.
20327 **
20328 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
20329 ** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
20330 ** during compilation and the sqlite_stat3/4 table is present, no data is
20331 ** read from it.
20332 **
20333 ** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
20334 ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
20335 ** returned. However, in this case, data is read from the sqlite_stat1
20336 ** table (if it is present) before returning.
20337 **
20338 ** If an OOM error occurs, this function always sets db->mallocFailed.
20339 ** This means if the caller does not care about other errors, the return
20340 ** code may be ignored.
20341 */
20342 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
20343 analysisInfo sInfo;
20344 HashElem *i;
20345 char *zSql;
20346 int rc = SQLITE_OK;
20347
20348 assert( iDb>=0 && iDb<db->nDb );
20349 assert( db->aDb[iDb].pBt!=0 );
20350
20351 /* Clear any prior statistics */
20352 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
20353 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
20354 Index *pIdx = sqliteHashData(i);
20355 pIdx->aiRowLogEst[0] = 0;
20356 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
20357 sqlite3DeleteIndexSamples(db, pIdx);
20358 pIdx->aSample = 0;
20359 #endif
20360 }
20361
20362 /* Load new statistics out of the sqlite_stat1 table */
20363 sInfo.db = db;
20364 sInfo.zDatabase = db->aDb[iDb].zDbSName;
20365 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
20366 zSql = sqlite3MPrintf(db,
20367 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
20368 if( zSql==0 ){
20369 rc = SQLITE_NOMEM_BKPT;
20370 }else{
20371 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
20372 sqlite3DbFree(db, zSql);
20373 }
20374 }
20375
20376 /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
20377 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
20378 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
20379 Index *pIdx = sqliteHashData(i);
20380 if( pIdx->aiRowLogEst[0]==0 ) sqlite3DefaultRowEst(pIdx);
20381 }
20382
20383 /* Load the statistics from the sqlite_stat4 table. */
20384 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
20385 if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
20386 db->lookaside.bDisable++;
20387 rc = loadStat4(db, sInfo.zDatabase);
20388 db->lookaside.bDisable--;
20389 }
20390 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
20391 Index *pIdx = sqliteHashData(i);
20392 sqlite3_free(pIdx->aiRowEst);
20393 pIdx->aiRowEst = 0;
20394 }
20395 #endif
20396
20397 if( rc==SQLITE_NOMEM ){
20398 sqlite3OomFault(db);
20399 }
20400 return rc;
20401 }
20402
20403
20404 #endif /* SQLITE_OMIT_ANALYZE */
20405
20406 /************** End of analyze.c *********************************************/
20407 /************** Begin file attach.c ******************************************/
20408 /*
20409 ** 2003 April 6
20410 **
20411 ** The author disclaims copyright to this source code. In place of
20412 ** a legal notice, here is a blessing:
20413 **
20414 ** May you do good and not evil.
20415 ** May you find forgiveness for yourself and forgive others.
20416 ** May you share freely, never taking more than you give.
20417 **
20418 *************************************************************************
20419 ** This file contains code used to implement the ATTACH and DETACH commands.
20420 */
20421 /* #include "sqliteInt.h" */
20422
20423 #ifndef SQLITE_OMIT_ATTACH
20424 /*
20425 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
20426 ** is slightly different from resolving a normal SQL expression, because simple
20427 ** identifiers are treated as strings, not possible column names or aliases.
20428 **
20429 ** i.e. if the parser sees:
20430 **
20431 ** ATTACH DATABASE abc AS def
20432 **
20433 ** it treats the two expressions as literal strings 'abc' and 'def' instead of
20434 ** looking for columns of the same name.
20435 **
20436 ** This only applies to the root node of pExpr, so the statement:
20437 **
20438 ** ATTACH DATABASE abc||def AS 'db2'
20439 **
20440 ** will fail because neither abc or def can be resolved.
20441 */
20442 static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
20443 {
20444 int rc = SQLITE_OK;
20445 if( pExpr ){
20446 if( pExpr->op!=TK_ID ){
20447 rc = sqlite3ResolveExprNames(pName, pExpr);
20448 }else{
20449 pExpr->op = TK_STRING;
20450 }
20451 }
20452 return rc;
20453 }
20454
20455 /*
20456 ** An SQL user-function registered to do the work of an ATTACH statement. The
20457 ** three arguments to the function come directly from an attach statement:
20458 **
20459 ** ATTACH DATABASE x AS y KEY z
20460 **
20461 ** SELECT sqlite_attach(x, y, z)
20462 **
20463 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
20464 ** third argument.
20465 */
20466 static void attachFunc(
20467 sqlite3_context *context,
20468 int NotUsed,
20469 sqlite3_value **argv
20470 ){
20471 int i;
20472 int rc = 0;
20473 sqlite3 *db = sqlite3_context_db_handle(context);
20474 const char *zName;
20475 const char *zFile;
20476 char *zPath = 0;
20477 char *zErr = 0;
20478 unsigned int flags;
20479 Db *aNew;
20480 char *zErrDyn = 0;
20481 sqlite3_vfs *pVfs;
20482
20483 UNUSED_PARAMETER(NotUsed);
20484
20485 zFile = (const char *)sqlite3_value_text(argv[0]);
20486 zName = (const char *)sqlite3_value_text(argv[1]);
20487 if( zFile==0 ) zFile = "";
20488 if( zName==0 ) zName = "";
20489
20490 /* Check for the following errors:
20491 **
20492 ** * Too many attached databases,
20493 ** * Transaction currently open
20494 ** * Specified database name already being used.
20495 */
20496 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
20497 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
20498 db->aLimit[SQLITE_LIMIT_ATTACHED]
20499 );
20500 goto attach_error;
20501 }
20502 if( !db->autoCommit ){
20503 zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
20504 goto attach_error;
20505 }
20506 for(i=0; i<db->nDb; i++){
20507 char *z = db->aDb[i].zDbSName;
20508 assert( z && zName );
20509 if( sqlite3StrICmp(z, zName)==0 ){
20510 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
20511 goto attach_error;
20512 }
20513 }
20514
20515 /* Allocate the new entry in the db->aDb[] array and initialize the schema
20516 ** hash tables.
20517 */
20518 if( db->aDb==db->aDbStatic ){
20519 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
20520 if( aNew==0 ) return;
20521 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
20522 }else{
20523 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
20524 if( aNew==0 ) return;
20525 }
20526 db->aDb = aNew;
20527 aNew = &db->aDb[db->nDb];
20528 memset(aNew, 0, sizeof(*aNew));
20529
20530 /* Open the database file. If the btree is successfully opened, use
20531 ** it to obtain the database schema. At this point the schema may
20532 ** or may not be initialized.
20533 */
20534 flags = db->openFlags;
20535 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
20536 if( rc!=SQLITE_OK ){
20537 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
20538 sqlite3_result_error(context, zErr, -1);
20539 sqlite3_free(zErr);
20540 return;
20541 }
20542 assert( pVfs );
20543 flags |= SQLITE_OPEN_MAIN_DB;
20544 rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
20545 sqlite3_free( zPath );
20546 db->nDb++;
20547 db->skipBtreeMutex = 0;
20548 if( rc==SQLITE_CONSTRAINT ){
20549 rc = SQLITE_ERROR;
20550 zErrDyn = sqlite3MPrintf(db, "database is already attached");
20551 }else if( rc==SQLITE_OK ){
20552 Pager *pPager;
20553 aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
20554 if( !aNew->pSchema ){
20555 rc = SQLITE_NOMEM_BKPT;
20556 }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
20557 zErrDyn = sqlite3MPrintf(db,
20558 "attached databases must use the same text encoding as main database");
20559 rc = SQLITE_ERROR;
20560 }
20561 sqlite3BtreeEnter(aNew->pBt);
20562 pPager = sqlite3BtreePager(aNew->pBt);
20563 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
20564 sqlite3BtreeSecureDelete(aNew->pBt,
20565 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
20566 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
20567 sqlite3BtreeSetPagerFlags(aNew->pBt,
20568 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
20569 #endif
20570 sqlite3BtreeLeave(aNew->pBt);
20571 }
20572 aNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
20573 aNew->zDbSName = sqlite3DbStrDup(db, zName);
20574 if( rc==SQLITE_OK && aNew->zDbSName==0 ){
20575 rc = SQLITE_NOMEM_BKPT;
20576 }
20577
20578
20579 #ifdef SQLITE_HAS_CODEC
20580 if( rc==SQLITE_OK ){
20581 extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
20582 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
20583 int nKey;
20584 char *zKey;
20585 int t = sqlite3_value_type(argv[2]);
20586 switch( t ){
20587 case SQLITE_INTEGER:
20588 case SQLITE_FLOAT:
20589 zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
20590 rc = SQLITE_ERROR;
20591 break;
20592
20593 case SQLITE_TEXT:
20594 case SQLITE_BLOB:
20595 nKey = sqlite3_value_bytes(argv[2]);
20596 zKey = (char *)sqlite3_value_blob(argv[2]);
20597 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
20598 break;
20599
20600 case SQLITE_NULL:
20601 /* No key specified. Use the key from the main database */
20602 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
20603 if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
20604 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
20605 }
20606 break;
20607 }
20608 }
20609 #endif
20610
20611 /* If the file was opened successfully, read the schema for the new database.
20612 ** If this fails, or if opening the file failed, then close the file and
20613 ** remove the entry from the db->aDb[] array. i.e. put everything back the way
20614 ** we found it.
20615 */
20616 if( rc==SQLITE_OK ){
20617 sqlite3BtreeEnterAll(db);
20618 rc = sqlite3Init(db, &zErrDyn);
20619 sqlite3BtreeLeaveAll(db);
20620 }
20621 #ifdef SQLITE_USER_AUTHENTICATION
20622 if( rc==SQLITE_OK ){
20623 u8 newAuth = 0;
20624 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
20625 if( newAuth<db->auth.authLevel ){
20626 rc = SQLITE_AUTH_USER;
20627 }
20628 }
20629 #endif
20630 if( rc ){
20631 int iDb = db->nDb - 1;
20632 assert( iDb>=2 );
20633 if( db->aDb[iDb].pBt ){
20634 sqlite3BtreeClose(db->aDb[iDb].pBt);
20635 db->aDb[iDb].pBt = 0;
20636 db->aDb[iDb].pSchema = 0;
20637 }
20638 sqlite3ResetAllSchemasOfConnection(db);
20639 db->nDb = iDb;
20640 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
20641 sqlite3OomFault(db);
20642 sqlite3DbFree(db, zErrDyn);
20643 zErrDyn = sqlite3MPrintf(db, "out of memory");
20644 }else if( zErrDyn==0 ){
20645 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
20646 }
20647 goto attach_error;
20648 }
20649
20650 return;
20651
20652 attach_error:
20653 /* Return an error if we get here */
20654 if( zErrDyn ){
20655 sqlite3_result_error(context, zErrDyn, -1);
20656 sqlite3DbFree(db, zErrDyn);
20657 }
20658 if( rc ) sqlite3_result_error_code(context, rc);
20659 }
20660
20661 /*
20662 ** An SQL user-function registered to do the work of an DETACH statement. The
20663 ** three arguments to the function come directly from a detach statement:
20664 **
20665 ** DETACH DATABASE x
20666 **
20667 ** SELECT sqlite_detach(x)
20668 */
20669 static void detachFunc(
20670 sqlite3_context *context,
20671 int NotUsed,
20672 sqlite3_value **argv
20673 ){
20674 const char *zName = (const char *)sqlite3_value_text(argv[0]);
20675 sqlite3 *db = sqlite3_context_db_handle(context);
20676 int i;
20677 Db *pDb = 0;
20678 char zErr[128];
20679
20680 UNUSED_PARAMETER(NotUsed);
20681
20682 if( zName==0 ) zName = "";
20683 for(i=0; i<db->nDb; i++){
20684 pDb = &db->aDb[i];
20685 if( pDb->pBt==0 ) continue;
20686 if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
20687 }
20688
20689 if( i>=db->nDb ){
20690 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
20691 goto detach_error;
20692 }
20693 if( i<2 ){
20694 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
20695 goto detach_error;
20696 }
20697 if( !db->autoCommit ){
20698 sqlite3_snprintf(sizeof(zErr), zErr,
20699 "cannot DETACH database within transaction");
20700 goto detach_error;
20701 }
20702 if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
20703 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
20704 goto detach_error;
20705 }
20706
20707 sqlite3BtreeClose(pDb->pBt);
20708 pDb->pBt = 0;
20709 pDb->pSchema = 0;
20710 sqlite3CollapseDatabaseArray(db);
20711 return;
20712
20713 detach_error:
20714 sqlite3_result_error(context, zErr, -1);
20715 }
20716
20717 /*
20718 ** This procedure generates VDBE code for a single invocation of either the
20719 ** sqlite_detach() or sqlite_attach() SQL user functions.
20720 */
20721 static void codeAttach(
20722 Parse *pParse, /* The parser context */
20723 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
20724 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
20725 Expr *pAuthArg, /* Expression to pass to authorization callback */
20726 Expr *pFilename, /* Name of database file */
20727 Expr *pDbname, /* Name of the database to use internally */
20728 Expr *pKey /* Database key for encryption extension */
20729 ){
20730 int rc;
20731 NameContext sName;
20732 Vdbe *v;
20733 sqlite3* db = pParse->db;
20734 int regArgs;
20735
20736 if( pParse->nErr ) goto attach_end;
20737 memset(&sName, 0, sizeof(NameContext));
20738 sName.pParse = pParse;
20739
20740 if(
20741 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
20742 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
20743 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
20744 ){
20745 goto attach_end;
20746 }
20747
20748 #ifndef SQLITE_OMIT_AUTHORIZATION
20749 if( pAuthArg ){
20750 char *zAuthArg;
20751 if( pAuthArg->op==TK_STRING ){
20752 zAuthArg = pAuthArg->u.zToken;
20753 }else{
20754 zAuthArg = 0;
20755 }
20756 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
20757 if(rc!=SQLITE_OK ){
20758 goto attach_end;
20759 }
20760 }
20761 #endif /* SQLITE_OMIT_AUTHORIZATION */
20762
20763
20764 v = sqlite3GetVdbe(pParse);
20765 regArgs = sqlite3GetTempRange(pParse, 4);
20766 sqlite3ExprCode(pParse, pFilename, regArgs);
20767 sqlite3ExprCode(pParse, pDbname, regArgs+1);
20768 sqlite3ExprCode(pParse, pKey, regArgs+2);
20769
20770 assert( v || db->mallocFailed );
20771 if( v ){
20772 sqlite3VdbeAddOp4(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3,
20773 (char *)pFunc, P4_FUNCDEF);
20774 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
20775 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
20776
20777 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
20778 ** statement only). For DETACH, set it to false (expire all existing
20779 ** statements).
20780 */
20781 sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
20782 }
20783
20784 attach_end:
20785 sqlite3ExprDelete(db, pFilename);
20786 sqlite3ExprDelete(db, pDbname);
20787 sqlite3ExprDelete(db, pKey);
20788 }
20789
20790 /*
20791 ** Called by the parser to compile a DETACH statement.
20792 **
20793 ** DETACH pDbname
20794 */
20795 SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
20796 static const FuncDef detach_func = {
20797 1, /* nArg */
20798 SQLITE_UTF8, /* funcFlags */
20799 0, /* pUserData */
20800 0, /* pNext */
20801 detachFunc, /* xSFunc */
20802 0, /* xFinalize */
20803 "sqlite_detach", /* zName */
20804 {0}
20805 };
20806 codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
20807 }
20808
20809 /*
20810 ** Called by the parser to compile an ATTACH statement.
20811 **
20812 ** ATTACH p AS pDbname KEY pKey
20813 */
20814 SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *p Key){
20815 static const FuncDef attach_func = {
20816 3, /* nArg */
20817 SQLITE_UTF8, /* funcFlags */
20818 0, /* pUserData */
20819 0, /* pNext */
20820 attachFunc, /* xSFunc */
20821 0, /* xFinalize */
20822 "sqlite_attach", /* zName */
20823 {0}
20824 };
20825 codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
20826 }
20827 #endif /* SQLITE_OMIT_ATTACH */
20828
20829 /*
20830 ** Initialize a DbFixer structure. This routine must be called prior
20831 ** to passing the structure to one of the sqliteFixAAAA() routines below.
20832 */
20833 SQLITE_PRIVATE void sqlite3FixInit(
20834 DbFixer *pFix, /* The fixer to be initialized */
20835 Parse *pParse, /* Error messages will be written here */
20836 int iDb, /* This is the database that must be used */
20837 const char *zType, /* "view", "trigger", or "index" */
20838 const Token *pName /* Name of the view, trigger, or index */
20839 ){
20840 sqlite3 *db;
20841
20842 db = pParse->db;
20843 assert( db->nDb>iDb );
20844 pFix->pParse = pParse;
20845 pFix->zDb = db->aDb[iDb].zDbSName;
20846 pFix->pSchema = db->aDb[iDb].pSchema;
20847 pFix->zType = zType;
20848 pFix->pName = pName;
20849 pFix->bVarOnly = (iDb==1);
20850 }
20851
20852 /*
20853 ** The following set of routines walk through the parse tree and assign
20854 ** a specific database to all table references where the database name
20855 ** was left unspecified in the original SQL statement. The pFix structure
20856 ** must have been initialized by a prior call to sqlite3FixInit().
20857 **
20858 ** These routines are used to make sure that an index, trigger, or
20859 ** view in one database does not refer to objects in a different database.
20860 ** (Exception: indices, triggers, and views in the TEMP database are
20861 ** allowed to refer to anything.) If a reference is explicitly made
20862 ** to an object in a different database, an error message is added to
20863 ** pParse->zErrMsg and these routines return non-zero. If everything
20864 ** checks out, these routines return 0.
20865 */
20866 SQLITE_PRIVATE int sqlite3FixSrcList(
20867 DbFixer *pFix, /* Context of the fixation */
20868 SrcList *pList /* The Source list to check and modify */
20869 ){
20870 int i;
20871 const char *zDb;
20872 struct SrcList_item *pItem;
20873
20874 if( NEVER(pList==0) ) return 0;
20875 zDb = pFix->zDb;
20876 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
20877 if( pFix->bVarOnly==0 ){
20878 if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
20879 sqlite3ErrorMsg(pFix->pParse,
20880 "%s %T cannot reference objects in database %s",
20881 pFix->zType, pFix->pName, pItem->zDatabase);
20882 return 1;
20883 }
20884 sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
20885 pItem->zDatabase = 0;
20886 pItem->pSchema = pFix->pSchema;
20887 }
20888 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
20889 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
20890 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
20891 #endif
20892 }
20893 return 0;
20894 }
20895 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
20896 SQLITE_PRIVATE int sqlite3FixSelect(
20897 DbFixer *pFix, /* Context of the fixation */
20898 Select *pSelect /* The SELECT statement to be fixed to one database */
20899 ){
20900 while( pSelect ){
20901 if( sqlite3FixExprList(pFix, pSelect->pEList) ){
20902 return 1;
20903 }
20904 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
20905 return 1;
20906 }
20907 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
20908 return 1;
20909 }
20910 if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
20911 return 1;
20912 }
20913 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
20914 return 1;
20915 }
20916 if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
20917 return 1;
20918 }
20919 if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
20920 return 1;
20921 }
20922 if( sqlite3FixExpr(pFix, pSelect->pOffset) ){
20923 return 1;
20924 }
20925 pSelect = pSelect->pPrior;
20926 }
20927 return 0;
20928 }
20929 SQLITE_PRIVATE int sqlite3FixExpr(
20930 DbFixer *pFix, /* Context of the fixation */
20931 Expr *pExpr /* The expression to be fixed to one database */
20932 ){
20933 while( pExpr ){
20934 if( pExpr->op==TK_VARIABLE ){
20935 if( pFix->pParse->db->init.busy ){
20936 pExpr->op = TK_NULL;
20937 }else{
20938 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
20939 return 1;
20940 }
20941 }
20942 if( ExprHasProperty(pExpr, EP_TokenOnly|EP_Leaf) ) break;
20943 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
20944 if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
20945 }else{
20946 if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
20947 }
20948 if( sqlite3FixExpr(pFix, pExpr->pRight) ){
20949 return 1;
20950 }
20951 pExpr = pExpr->pLeft;
20952 }
20953 return 0;
20954 }
20955 SQLITE_PRIVATE int sqlite3FixExprList(
20956 DbFixer *pFix, /* Context of the fixation */
20957 ExprList *pList /* The expression to be fixed to one database */
20958 ){
20959 int i;
20960 struct ExprList_item *pItem;
20961 if( pList==0 ) return 0;
20962 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
20963 if( sqlite3FixExpr(pFix, pItem->pExpr) ){
20964 return 1;
20965 }
20966 }
20967 return 0;
20968 }
20969 #endif
20970
20971 #ifndef SQLITE_OMIT_TRIGGER
20972 SQLITE_PRIVATE int sqlite3FixTriggerStep(
20973 DbFixer *pFix, /* Context of the fixation */
20974 TriggerStep *pStep /* The trigger step be fixed to one database */
20975 ){
20976 while( pStep ){
20977 if( sqlite3FixSelect(pFix, pStep->pSelect) ){
20978 return 1;
20979 }
20980 if( sqlite3FixExpr(pFix, pStep->pWhere) ){
20981 return 1;
20982 }
20983 if( sqlite3FixExprList(pFix, pStep->pExprList) ){
20984 return 1;
20985 }
20986 pStep = pStep->pNext;
20987 }
20988 return 0;
20989 }
20990 #endif
20991
20992 /************** End of attach.c **********************************************/
20993 /************** Begin file auth.c ********************************************/
20994 /*
20995 ** 2003 January 11
20996 **
20997 ** The author disclaims copyright to this source code. In place of
20998 ** a legal notice, here is a blessing:
20999 **
21000 ** May you do good and not evil.
21001 ** May you find forgiveness for yourself and forgive others.
21002 ** May you share freely, never taking more than you give.
21003 **
21004 *************************************************************************
21005 ** This file contains code used to implement the sqlite3_set_authorizer()
21006 ** API. This facility is an optional feature of the library. Embedded
21007 ** systems that do not need this facility may omit it by recompiling
21008 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
21009 */
21010 /* #include "sqliteInt.h" */
21011
21012 /*
21013 ** All of the code in this file may be omitted by defining a single
21014 ** macro.
21015 */
21016 #ifndef SQLITE_OMIT_AUTHORIZATION
21017
21018 /*
21019 ** Set or clear the access authorization function.
21020 **
21021 ** The access authorization function is be called during the compilation
21022 ** phase to verify that the user has read and/or write access permission on
21023 ** various fields of the database. The first argument to the auth function
21024 ** is a copy of the 3rd argument to this routine. The second argument
21025 ** to the auth function is one of these constants:
21026 **
21027 ** SQLITE_CREATE_INDEX
21028 ** SQLITE_CREATE_TABLE
21029 ** SQLITE_CREATE_TEMP_INDEX
21030 ** SQLITE_CREATE_TEMP_TABLE
21031 ** SQLITE_CREATE_TEMP_TRIGGER
21032 ** SQLITE_CREATE_TEMP_VIEW
21033 ** SQLITE_CREATE_TRIGGER
21034 ** SQLITE_CREATE_VIEW
21035 ** SQLITE_DELETE
21036 ** SQLITE_DROP_INDEX
21037 ** SQLITE_DROP_TABLE
21038 ** SQLITE_DROP_TEMP_INDEX
21039 ** SQLITE_DROP_TEMP_TABLE
21040 ** SQLITE_DROP_TEMP_TRIGGER
21041 ** SQLITE_DROP_TEMP_VIEW
21042 ** SQLITE_DROP_TRIGGER
21043 ** SQLITE_DROP_VIEW
21044 ** SQLITE_INSERT
21045 ** SQLITE_PRAGMA
21046 ** SQLITE_READ
21047 ** SQLITE_SELECT
21048 ** SQLITE_TRANSACTION
21049 ** SQLITE_UPDATE
21050 **
21051 ** The third and fourth arguments to the auth function are the name of
21052 ** the table and the column that are being accessed. The auth function
21053 ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
21054 ** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
21055 ** means that the SQL statement will never-run - the sqlite3_exec() call
21056 ** will return with an error. SQLITE_IGNORE means that the SQL statement
21057 ** should run but attempts to read the specified column will return NULL
21058 ** and attempts to write the column will be ignored.
21059 **
21060 ** Setting the auth function to NULL disables this hook. The default
21061 ** setting of the auth function is NULL.
21062 */
21063 SQLITE_API int sqlite3_set_authorizer(
21064 sqlite3 *db,
21065 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
21066 void *pArg
21067 ){
21068 #ifdef SQLITE_ENABLE_API_ARMOR
21069 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
21070 #endif
21071 sqlite3_mutex_enter(db->mutex);
21072 db->xAuth = (sqlite3_xauth)xAuth;
21073 db->pAuthArg = pArg;
21074 sqlite3ExpirePreparedStatements(db);
21075 sqlite3_mutex_leave(db->mutex);
21076 return SQLITE_OK;
21077 }
21078
21079 /*
21080 ** Write an error message into pParse->zErrMsg that explains that the
21081 ** user-supplied authorization function returned an illegal value.
21082 */
21083 static void sqliteAuthBadReturnCode(Parse *pParse){
21084 sqlite3ErrorMsg(pParse, "authorizer malfunction");
21085 pParse->rc = SQLITE_ERROR;
21086 }
21087
21088 /*
21089 ** Invoke the authorization callback for permission to read column zCol from
21090 ** table zTab in database zDb. This function assumes that an authorization
21091 ** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
21092 **
21093 ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
21094 ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
21095 ** is treated as SQLITE_DENY. In this case an error is left in pParse.
21096 */
21097 SQLITE_PRIVATE int sqlite3AuthReadCol(
21098 Parse *pParse, /* The parser context */
21099 const char *zTab, /* Table name */
21100 const char *zCol, /* Column name */
21101 int iDb /* Index of containing database. */
21102 ){
21103 sqlite3 *db = pParse->db; /* Database handle */
21104 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
21105 int rc; /* Auth callback return code */
21106
21107 if( db->init.busy ) return SQLITE_OK;
21108 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
21109 #ifdef SQLITE_USER_AUTHENTICATION
21110 ,db->auth.zAuthUser
21111 #endif
21112 );
21113 if( rc==SQLITE_DENY ){
21114 if( db->nDb>2 || iDb!=0 ){
21115 sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
21116 }else{
21117 sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
21118 }
21119 pParse->rc = SQLITE_AUTH;
21120 }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
21121 sqliteAuthBadReturnCode(pParse);
21122 }
21123 return rc;
21124 }
21125
21126 /*
21127 ** The pExpr should be a TK_COLUMN expression. The table referred to
21128 ** is in pTabList or else it is the NEW or OLD table of a trigger.
21129 ** Check to see if it is OK to read this particular column.
21130 **
21131 ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
21132 ** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
21133 ** then generate an error.
21134 */
21135 SQLITE_PRIVATE void sqlite3AuthRead(
21136 Parse *pParse, /* The parser context */
21137 Expr *pExpr, /* The expression to check authorization on */
21138 Schema *pSchema, /* The schema of the expression */
21139 SrcList *pTabList /* All table that pExpr might refer to */
21140 ){
21141 sqlite3 *db = pParse->db;
21142 Table *pTab = 0; /* The table being read */
21143 const char *zCol; /* Name of the column of the table */
21144 int iSrc; /* Index in pTabList->a[] of table being read */
21145 int iDb; /* The index of the database the expression refers to */
21146 int iCol; /* Index of column in table */
21147
21148 if( db->xAuth==0 ) return;
21149 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
21150 if( iDb<0 ){
21151 /* An attempt to read a column out of a subquery or other
21152 ** temporary table. */
21153 return;
21154 }
21155
21156 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
21157 if( pExpr->op==TK_TRIGGER ){
21158 pTab = pParse->pTriggerTab;
21159 }else{
21160 assert( pTabList );
21161 for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
21162 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
21163 pTab = pTabList->a[iSrc].pTab;
21164 break;
21165 }
21166 }
21167 }
21168 iCol = pExpr->iColumn;
21169 if( NEVER(pTab==0) ) return;
21170
21171 if( iCol>=0 ){
21172 assert( iCol<pTab->nCol );
21173 zCol = pTab->aCol[iCol].zName;
21174 }else if( pTab->iPKey>=0 ){
21175 assert( pTab->iPKey<pTab->nCol );
21176 zCol = pTab->aCol[pTab->iPKey].zName;
21177 }else{
21178 zCol = "ROWID";
21179 }
21180 assert( iDb>=0 && iDb<db->nDb );
21181 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
21182 pExpr->op = TK_NULL;
21183 }
21184 }
21185
21186 /*
21187 ** Do an authorization check using the code and arguments given. Return
21188 ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
21189 ** is returned, then the error count and error message in pParse are
21190 ** modified appropriately.
21191 */
21192 SQLITE_PRIVATE int sqlite3AuthCheck(
21193 Parse *pParse,
21194 int code,
21195 const char *zArg1,
21196 const char *zArg2,
21197 const char *zArg3
21198 ){
21199 sqlite3 *db = pParse->db;
21200 int rc;
21201
21202 /* Don't do any authorization checks if the database is initialising
21203 ** or if the parser is being invoked from within sqlite3_declare_vtab.
21204 */
21205 if( db->init.busy || IN_DECLARE_VTAB ){
21206 return SQLITE_OK;
21207 }
21208
21209 if( db->xAuth==0 ){
21210 return SQLITE_OK;
21211 }
21212 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
21213 #ifdef SQLITE_USER_AUTHENTICATION
21214 ,db->auth.zAuthUser
21215 #endif
21216 );
21217 if( rc==SQLITE_DENY ){
21218 sqlite3ErrorMsg(pParse, "not authorized");
21219 pParse->rc = SQLITE_AUTH;
21220 }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
21221 rc = SQLITE_DENY;
21222 sqliteAuthBadReturnCode(pParse);
21223 }
21224 return rc;
21225 }
21226
21227 /*
21228 ** Push an authorization context. After this routine is called, the
21229 ** zArg3 argument to authorization callbacks will be zContext until
21230 ** popped. Or if pParse==0, this routine is a no-op.
21231 */
21232 SQLITE_PRIVATE void sqlite3AuthContextPush(
21233 Parse *pParse,
21234 AuthContext *pContext,
21235 const char *zContext
21236 ){
21237 assert( pParse );
21238 pContext->pParse = pParse;
21239 pContext->zAuthContext = pParse->zAuthContext;
21240 pParse->zAuthContext = zContext;
21241 }
21242
21243 /*
21244 ** Pop an authorization context that was previously pushed
21245 ** by sqlite3AuthContextPush
21246 */
21247 SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
21248 if( pContext->pParse ){
21249 pContext->pParse->zAuthContext = pContext->zAuthContext;
21250 pContext->pParse = 0;
21251 }
21252 }
21253
21254 #endif /* SQLITE_OMIT_AUTHORIZATION */
21255
21256 /************** End of auth.c ************************************************/
21257 /************** Begin file build.c *******************************************/
21258 /*
21259 ** 2001 September 15
21260 **
21261 ** The author disclaims copyright to this source code. In place of
21262 ** a legal notice, here is a blessing:
21263 **
21264 ** May you do good and not evil.
21265 ** May you find forgiveness for yourself and forgive others.
21266 ** May you share freely, never taking more than you give.
21267 **
21268 *************************************************************************
21269 ** This file contains C code routines that are called by the SQLite parser
21270 ** when syntax rules are reduced. The routines in this file handle the
21271 ** following kinds of SQL syntax:
21272 **
21273 ** CREATE TABLE
21274 ** DROP TABLE
21275 ** CREATE INDEX
21276 ** DROP INDEX
21277 ** creating ID lists
21278 ** BEGIN TRANSACTION
21279 ** COMMIT
21280 ** ROLLBACK
21281 */
21282 /* #include "sqliteInt.h" */
21283
21284 #ifndef SQLITE_OMIT_SHARED_CACHE
21285 /*
21286 ** The TableLock structure is only used by the sqlite3TableLock() and
21287 ** codeTableLocks() functions.
21288 */
21289 struct TableLock {
21290 int iDb; /* The database containing the table to be locked */
21291 int iTab; /* The root page of the table to be locked */
21292 u8 isWriteLock; /* True for write lock. False for a read lock */
21293 const char *zLockName; /* Name of the table */
21294 };
21295
21296 /*
21297 ** Record the fact that we want to lock a table at run-time.
21298 **
21299 ** The table to be locked has root page iTab and is found in database iDb.
21300 ** A read or a write lock can be taken depending on isWritelock.
21301 **
21302 ** This routine just records the fact that the lock is desired. The
21303 ** code to make the lock occur is generated by a later call to
21304 ** codeTableLocks() which occurs during sqlite3FinishCoding().
21305 */
21306 SQLITE_PRIVATE void sqlite3TableLock(
21307 Parse *pParse, /* Parsing context */
21308 int iDb, /* Index of the database containing the table to lock */
21309 int iTab, /* Root page number of the table to be locked */
21310 u8 isWriteLock, /* True for a write lock */
21311 const char *zName /* Name of the table to be locked */
21312 ){
21313 Parse *pToplevel = sqlite3ParseToplevel(pParse);
21314 int i;
21315 int nBytes;
21316 TableLock *p;
21317 assert( iDb>=0 );
21318
21319 if( iDb==1 ) return;
21320 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
21321 for(i=0; i<pToplevel->nTableLock; i++){
21322 p = &pToplevel->aTableLock[i];
21323 if( p->iDb==iDb && p->iTab==iTab ){
21324 p->isWriteLock = (p->isWriteLock || isWriteLock);
21325 return;
21326 }
21327 }
21328
21329 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
21330 pToplevel->aTableLock =
21331 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
21332 if( pToplevel->aTableLock ){
21333 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
21334 p->iDb = iDb;
21335 p->iTab = iTab;
21336 p->isWriteLock = isWriteLock;
21337 p->zLockName = zName;
21338 }else{
21339 pToplevel->nTableLock = 0;
21340 sqlite3OomFault(pToplevel->db);
21341 }
21342 }
21343
21344 /*
21345 ** Code an OP_TableLock instruction for each table locked by the
21346 ** statement (configured by calls to sqlite3TableLock()).
21347 */
21348 static void codeTableLocks(Parse *pParse){
21349 int i;
21350 Vdbe *pVdbe;
21351
21352 pVdbe = sqlite3GetVdbe(pParse);
21353 assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
21354
21355 for(i=0; i<pParse->nTableLock; i++){
21356 TableLock *p = &pParse->aTableLock[i];
21357 int p1 = p->iDb;
21358 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
21359 p->zLockName, P4_STATIC);
21360 }
21361 }
21362 #else
21363 #define codeTableLocks(x)
21364 #endif
21365
21366 /*
21367 ** Return TRUE if the given yDbMask object is empty - if it contains no
21368 ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
21369 ** macros when SQLITE_MAX_ATTACHED is greater than 30.
21370 */
21371 #if SQLITE_MAX_ATTACHED>30
21372 SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
21373 int i;
21374 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
21375 return 1;
21376 }
21377 #endif
21378
21379 /*
21380 ** This routine is called after a single SQL statement has been
21381 ** parsed and a VDBE program to execute that statement has been
21382 ** prepared. This routine puts the finishing touches on the
21383 ** VDBE program and resets the pParse structure for the next
21384 ** parse.
21385 **
21386 ** Note that if an error occurred, it might be the case that
21387 ** no VDBE code was generated.
21388 */
21389 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
21390 sqlite3 *db;
21391 Vdbe *v;
21392
21393 assert( pParse->pToplevel==0 );
21394 db = pParse->db;
21395 if( pParse->nested ) return;
21396 if( db->mallocFailed || pParse->nErr ){
21397 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
21398 return;
21399 }
21400
21401 /* Begin by generating some termination code at the end of the
21402 ** vdbe program
21403 */
21404 v = sqlite3GetVdbe(pParse);
21405 assert( !pParse->isMultiWrite
21406 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
21407 if( v ){
21408 sqlite3VdbeAddOp0(v, OP_Halt);
21409
21410 #if SQLITE_USER_AUTHENTICATION
21411 if( pParse->nTableLock>0 && db->init.busy==0 ){
21412 sqlite3UserAuthInit(db);
21413 if( db->auth.authLevel<UAUTH_User ){
21414 sqlite3ErrorMsg(pParse, "user not authenticated");
21415 pParse->rc = SQLITE_AUTH_USER;
21416 return;
21417 }
21418 }
21419 #endif
21420
21421 /* The cookie mask contains one bit for each database file open.
21422 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
21423 ** set for each database that is used. Generate code to start a
21424 ** transaction on each used database and to verify the schema cookie
21425 ** on each used database.
21426 */
21427 if( db->mallocFailed==0
21428 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
21429 ){
21430 int iDb, i;
21431 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
21432 sqlite3VdbeJumpHere(v, 0);
21433 for(iDb=0; iDb<db->nDb; iDb++){
21434 Schema *pSchema;
21435 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
21436 sqlite3VdbeUsesBtree(v, iDb);
21437 pSchema = db->aDb[iDb].pSchema;
21438 sqlite3VdbeAddOp4Int(v,
21439 OP_Transaction, /* Opcode */
21440 iDb, /* P1 */
21441 DbMaskTest(pParse->writeMask,iDb), /* P2 */
21442 pSchema->schema_cookie, /* P3 */
21443 pSchema->iGeneration /* P4 */
21444 );
21445 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
21446 VdbeComment((v,
21447 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
21448 }
21449 #ifndef SQLITE_OMIT_VIRTUALTABLE
21450 for(i=0; i<pParse->nVtabLock; i++){
21451 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
21452 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
21453 }
21454 pParse->nVtabLock = 0;
21455 #endif
21456
21457 /* Once all the cookies have been verified and transactions opened,
21458 ** obtain the required table-locks. This is a no-op unless the
21459 ** shared-cache feature is enabled.
21460 */
21461 codeTableLocks(pParse);
21462
21463 /* Initialize any AUTOINCREMENT data structures required.
21464 */
21465 sqlite3AutoincrementBegin(pParse);
21466
21467 /* Code constant expressions that where factored out of inner loops */
21468 if( pParse->pConstExpr ){
21469 ExprList *pEL = pParse->pConstExpr;
21470 pParse->okConstFactor = 0;
21471 for(i=0; i<pEL->nExpr; i++){
21472 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
21473 }
21474 }
21475
21476 /* Finally, jump back to the beginning of the executable code. */
21477 sqlite3VdbeGoto(v, 1);
21478 }
21479 }
21480
21481
21482 /* Get the VDBE program ready for execution
21483 */
21484 if( v && pParse->nErr==0 && !db->mallocFailed ){
21485 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */
21486 /* A minimum of one cursor is required if autoincrement is used
21487 * See ticket [a696379c1f08866] */
21488 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
21489 sqlite3VdbeMakeReady(v, pParse);
21490 pParse->rc = SQLITE_DONE;
21491 }else{
21492 pParse->rc = SQLITE_ERROR;
21493 }
21494 }
21495
21496 /*
21497 ** Run the parser and code generator recursively in order to generate
21498 ** code for the SQL statement given onto the end of the pParse context
21499 ** currently under construction. When the parser is run recursively
21500 ** this way, the final OP_Halt is not appended and other initialization
21501 ** and finalization steps are omitted because those are handling by the
21502 ** outermost parser.
21503 **
21504 ** Not everything is nestable. This facility is designed to permit
21505 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
21506 ** care if you decide to try to use this routine for some other purposes.
21507 */
21508 SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
21509 va_list ap;
21510 char *zSql;
21511 char *zErrMsg = 0;
21512 sqlite3 *db = pParse->db;
21513 char saveBuf[PARSE_TAIL_SZ];
21514
21515 if( pParse->nErr ) return;
21516 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
21517 va_start(ap, zFormat);
21518 zSql = sqlite3VMPrintf(db, zFormat, ap);
21519 va_end(ap);
21520 if( zSql==0 ){
21521 return; /* A malloc must have failed */
21522 }
21523 pParse->nested++;
21524 memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
21525 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
21526 sqlite3RunParser(pParse, zSql, &zErrMsg);
21527 sqlite3DbFree(db, zErrMsg);
21528 sqlite3DbFree(db, zSql);
21529 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
21530 pParse->nested--;
21531 }
21532
21533 #if SQLITE_USER_AUTHENTICATION
21534 /*
21535 ** Return TRUE if zTable is the name of the system table that stores the
21536 ** list of users and their access credentials.
21537 */
21538 SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
21539 return sqlite3_stricmp(zTable, "sqlite_user")==0;
21540 }
21541 #endif
21542
21543 /*
21544 ** Locate the in-memory structure that describes a particular database
21545 ** table given the name of that table and (optionally) the name of the
21546 ** database containing the table. Return NULL if not found.
21547 **
21548 ** If zDatabase is 0, all databases are searched for the table and the
21549 ** first matching table is returned. (No checking for duplicate table
21550 ** names is done.) The search order is TEMP first, then MAIN, then any
21551 ** auxiliary databases added using the ATTACH command.
21552 **
21553 ** See also sqlite3LocateTable().
21554 */
21555 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const cha r *zDatabase){
21556 Table *p = 0;
21557 int i;
21558
21559 /* All mutexes are required for schema access. Make sure we hold them. */
21560 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
21561 #if SQLITE_USER_AUTHENTICATION
21562 /* Only the admin user is allowed to know that the sqlite_user table
21563 ** exists */
21564 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
21565 return 0;
21566 }
21567 #endif
21568 while(1){
21569 for(i=OMIT_TEMPDB; i<db->nDb; i++){
21570 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
21571 if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
21572 assert( sqlite3SchemaMutexHeld(db, j, 0) );
21573 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
21574 if( p ) return p;
21575 }
21576 }
21577 /* Not found. If the name we were looking for was temp.sqlite_master
21578 ** then change the name to sqlite_temp_master and try again. */
21579 if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break;
21580 if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break;
21581 zName = TEMP_MASTER_NAME;
21582 }
21583 return 0;
21584 }
21585
21586 /*
21587 ** Locate the in-memory structure that describes a particular database
21588 ** table given the name of that table and (optionally) the name of the
21589 ** database containing the table. Return NULL if not found. Also leave an
21590 ** error message in pParse->zErrMsg.
21591 **
21592 ** The difference between this routine and sqlite3FindTable() is that this
21593 ** routine leaves an error message in pParse->zErrMsg where
21594 ** sqlite3FindTable() does not.
21595 */
21596 SQLITE_PRIVATE Table *sqlite3LocateTable(
21597 Parse *pParse, /* context in which to report errors */
21598 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
21599 const char *zName, /* Name of the table we are looking for */
21600 const char *zDbase /* Name of the database. Might be NULL */
21601 ){
21602 Table *p;
21603
21604 /* Read the database schema. If an error occurs, leave an error message
21605 ** and code in pParse and return NULL. */
21606 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
21607 return 0;
21608 }
21609
21610 p = sqlite3FindTable(pParse->db, zName, zDbase);
21611 if( p==0 ){
21612 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
21613 #ifndef SQLITE_OMIT_VIRTUALTABLE
21614 if( sqlite3FindDbName(pParse->db, zDbase)<1 ){
21615 /* If zName is the not the name of a table in the schema created using
21616 ** CREATE, then check to see if it is the name of an virtual table that
21617 ** can be an eponymous virtual table. */
21618 Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName);
21619 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
21620 pMod = sqlite3PragmaVtabRegister(pParse->db, zName);
21621 }
21622 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
21623 return pMod->pEpoTab;
21624 }
21625 }
21626 #endif
21627 if( (flags & LOCATE_NOERR)==0 ){
21628 if( zDbase ){
21629 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
21630 }else{
21631 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
21632 }
21633 pParse->checkSchema = 1;
21634 }
21635 }
21636
21637 return p;
21638 }
21639
21640 /*
21641 ** Locate the table identified by *p.
21642 **
21643 ** This is a wrapper around sqlite3LocateTable(). The difference between
21644 ** sqlite3LocateTable() and this function is that this function restricts
21645 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
21646 ** non-NULL if it is part of a view or trigger program definition. See
21647 ** sqlite3FixSrcList() for details.
21648 */
21649 SQLITE_PRIVATE Table *sqlite3LocateTableItem(
21650 Parse *pParse,
21651 u32 flags,
21652 struct SrcList_item *p
21653 ){
21654 const char *zDb;
21655 assert( p->pSchema==0 || p->zDatabase==0 );
21656 if( p->pSchema ){
21657 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
21658 zDb = pParse->db->aDb[iDb].zDbSName;
21659 }else{
21660 zDb = p->zDatabase;
21661 }
21662 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
21663 }
21664
21665 /*
21666 ** Locate the in-memory structure that describes
21667 ** a particular index given the name of that index
21668 ** and the name of the database that contains the index.
21669 ** Return NULL if not found.
21670 **
21671 ** If zDatabase is 0, all databases are searched for the
21672 ** table and the first matching index is returned. (No checking
21673 ** for duplicate index names is done.) The search order is
21674 ** TEMP first, then MAIN, then any auxiliary databases added
21675 ** using the ATTACH command.
21676 */
21677 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const cha r *zDb){
21678 Index *p = 0;
21679 int i;
21680 /* All mutexes are required for schema access. Make sure we hold them. */
21681 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
21682 for(i=OMIT_TEMPDB; i<db->nDb; i++){
21683 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
21684 Schema *pSchema = db->aDb[j].pSchema;
21685 assert( pSchema );
21686 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
21687 assert( sqlite3SchemaMutexHeld(db, j, 0) );
21688 p = sqlite3HashFind(&pSchema->idxHash, zName);
21689 if( p ) break;
21690 }
21691 return p;
21692 }
21693
21694 /*
21695 ** Reclaim the memory used by an index
21696 */
21697 static void freeIndex(sqlite3 *db, Index *p){
21698 #ifndef SQLITE_OMIT_ANALYZE
21699 sqlite3DeleteIndexSamples(db, p);
21700 #endif
21701 sqlite3ExprDelete(db, p->pPartIdxWhere);
21702 sqlite3ExprListDelete(db, p->aColExpr);
21703 sqlite3DbFree(db, p->zColAff);
21704 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
21705 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
21706 sqlite3_free(p->aiRowEst);
21707 #endif
21708 sqlite3DbFree(db, p);
21709 }
21710
21711 /*
21712 ** For the index called zIdxName which is found in the database iDb,
21713 ** unlike that index from its Table then remove the index from
21714 ** the index hash table and free all memory structures associated
21715 ** with the index.
21716 */
21717 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
21718 Index *pIndex;
21719 Hash *pHash;
21720
21721 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
21722 pHash = &db->aDb[iDb].pSchema->idxHash;
21723 pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
21724 if( ALWAYS(pIndex) ){
21725 if( pIndex->pTable->pIndex==pIndex ){
21726 pIndex->pTable->pIndex = pIndex->pNext;
21727 }else{
21728 Index *p;
21729 /* Justification of ALWAYS(); The index must be on the list of
21730 ** indices. */
21731 p = pIndex->pTable->pIndex;
21732 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
21733 if( ALWAYS(p && p->pNext==pIndex) ){
21734 p->pNext = pIndex->pNext;
21735 }
21736 }
21737 freeIndex(db, pIndex);
21738 }
21739 db->flags |= SQLITE_InternChanges;
21740 }
21741
21742 /*
21743 ** Look through the list of open database files in db->aDb[] and if
21744 ** any have been closed, remove them from the list. Reallocate the
21745 ** db->aDb[] structure to a smaller size, if possible.
21746 **
21747 ** Entry 0 (the "main" database) and entry 1 (the "temp" database)
21748 ** are never candidates for being collapsed.
21749 */
21750 SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
21751 int i, j;
21752 for(i=j=2; i<db->nDb; i++){
21753 struct Db *pDb = &db->aDb[i];
21754 if( pDb->pBt==0 ){
21755 sqlite3DbFree(db, pDb->zDbSName);
21756 pDb->zDbSName = 0;
21757 continue;
21758 }
21759 if( j<i ){
21760 db->aDb[j] = db->aDb[i];
21761 }
21762 j++;
21763 }
21764 db->nDb = j;
21765 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
21766 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
21767 sqlite3DbFree(db, db->aDb);
21768 db->aDb = db->aDbStatic;
21769 }
21770 }
21771
21772 /*
21773 ** Reset the schema for the database at index iDb. Also reset the
21774 ** TEMP schema.
21775 */
21776 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
21777 Db *pDb;
21778 assert( iDb<db->nDb );
21779
21780 /* Case 1: Reset the single schema identified by iDb */
21781 pDb = &db->aDb[iDb];
21782 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
21783 assert( pDb->pSchema!=0 );
21784 sqlite3SchemaClear(pDb->pSchema);
21785
21786 /* If any database other than TEMP is reset, then also reset TEMP
21787 ** since TEMP might be holding triggers that reference tables in the
21788 ** other database.
21789 */
21790 if( iDb!=1 ){
21791 pDb = &db->aDb[1];
21792 assert( pDb->pSchema!=0 );
21793 sqlite3SchemaClear(pDb->pSchema);
21794 }
21795 return;
21796 }
21797
21798 /*
21799 ** Erase all schema information from all attached databases (including
21800 ** "main" and "temp") for a single database connection.
21801 */
21802 SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
21803 int i;
21804 sqlite3BtreeEnterAll(db);
21805 for(i=0; i<db->nDb; i++){
21806 Db *pDb = &db->aDb[i];
21807 if( pDb->pSchema ){
21808 sqlite3SchemaClear(pDb->pSchema);
21809 }
21810 }
21811 db->flags &= ~SQLITE_InternChanges;
21812 sqlite3VtabUnlockList(db);
21813 sqlite3BtreeLeaveAll(db);
21814 sqlite3CollapseDatabaseArray(db);
21815 }
21816
21817 /*
21818 ** This routine is called when a commit occurs.
21819 */
21820 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
21821 db->flags &= ~SQLITE_InternChanges;
21822 }
21823
21824 /*
21825 ** Delete memory allocated for the column names of a table or view (the
21826 ** Table.aCol[] array).
21827 */
21828 SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
21829 int i;
21830 Column *pCol;
21831 assert( pTable!=0 );
21832 if( (pCol = pTable->aCol)!=0 ){
21833 for(i=0; i<pTable->nCol; i++, pCol++){
21834 sqlite3DbFree(db, pCol->zName);
21835 sqlite3ExprDelete(db, pCol->pDflt);
21836 sqlite3DbFree(db, pCol->zColl);
21837 }
21838 sqlite3DbFree(db, pTable->aCol);
21839 }
21840 }
21841
21842 /*
21843 ** Remove the memory data structures associated with the given
21844 ** Table. No changes are made to disk by this routine.
21845 **
21846 ** This routine just deletes the data structure. It does not unlink
21847 ** the table data structure from the hash table. But it does destroy
21848 ** memory structures of the indices and foreign keys associated with
21849 ** the table.
21850 **
21851 ** The db parameter is optional. It is needed if the Table object
21852 ** contains lookaside memory. (Table objects in the schema do not use
21853 ** lookaside memory, but some ephemeral Table objects do.) Or the
21854 ** db parameter can be used with db->pnBytesFreed to measure the memory
21855 ** used by the Table object.
21856 */
21857 static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
21858 Index *pIndex, *pNext;
21859 TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
21860
21861 /* Record the number of outstanding lookaside allocations in schema Tables
21862 ** prior to doing any free() operations. Since schema Tables do not use
21863 ** lookaside, this number should not change. */
21864 TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
21865 db->lookaside.nOut : 0 );
21866
21867 /* Delete all indices associated with this table. */
21868 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
21869 pNext = pIndex->pNext;
21870 assert( pIndex->pSchema==pTable->pSchema
21871 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
21872 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
21873 char *zName = pIndex->zName;
21874 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
21875 &pIndex->pSchema->idxHash, zName, 0
21876 );
21877 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
21878 assert( pOld==pIndex || pOld==0 );
21879 }
21880 freeIndex(db, pIndex);
21881 }
21882
21883 /* Delete any foreign keys attached to this table. */
21884 sqlite3FkDelete(db, pTable);
21885
21886 /* Delete the Table structure itself.
21887 */
21888 sqlite3DeleteColumnNames(db, pTable);
21889 sqlite3DbFree(db, pTable->zName);
21890 sqlite3DbFree(db, pTable->zColAff);
21891 sqlite3SelectDelete(db, pTable->pSelect);
21892 sqlite3ExprListDelete(db, pTable->pCheck);
21893 #ifndef SQLITE_OMIT_VIRTUALTABLE
21894 sqlite3VtabClear(db, pTable);
21895 #endif
21896 sqlite3DbFree(db, pTable);
21897
21898 /* Verify that no lookaside memory was used by schema tables */
21899 assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
21900 }
21901 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
21902 /* Do not delete the table until the reference count reaches zero. */
21903 if( !pTable ) return;
21904 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
21905 deleteTable(db, pTable);
21906 }
21907
21908
21909 /*
21910 ** Unlink the given table from the hash tables and the delete the
21911 ** table structure with all its indices and foreign keys.
21912 */
21913 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
21914 Table *p;
21915 Db *pDb;
21916
21917 assert( db!=0 );
21918 assert( iDb>=0 && iDb<db->nDb );
21919 assert( zTabName );
21920 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
21921 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
21922 pDb = &db->aDb[iDb];
21923 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
21924 sqlite3DeleteTable(db, p);
21925 db->flags |= SQLITE_InternChanges;
21926 }
21927
21928 /*
21929 ** Given a token, return a string that consists of the text of that
21930 ** token. Space to hold the returned string
21931 ** is obtained from sqliteMalloc() and must be freed by the calling
21932 ** function.
21933 **
21934 ** Any quotation marks (ex: "name", 'name', [name], or `name`) that
21935 ** surround the body of the token are removed.
21936 **
21937 ** Tokens are often just pointers into the original SQL text and so
21938 ** are not \000 terminated and are not persistent. The returned string
21939 ** is \000 terminated and is persistent.
21940 */
21941 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
21942 char *zName;
21943 if( pName ){
21944 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
21945 sqlite3Dequote(zName);
21946 }else{
21947 zName = 0;
21948 }
21949 return zName;
21950 }
21951
21952 /*
21953 ** Open the sqlite_master table stored in database number iDb for
21954 ** writing. The table is opened using cursor 0.
21955 */
21956 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
21957 Vdbe *v = sqlite3GetVdbe(p);
21958 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, MASTER_NAME);
21959 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
21960 if( p->nTab==0 ){
21961 p->nTab = 1;
21962 }
21963 }
21964
21965 /*
21966 ** Parameter zName points to a nul-terminated buffer containing the name
21967 ** of a database ("main", "temp" or the name of an attached db). This
21968 ** function returns the index of the named database in db->aDb[], or
21969 ** -1 if the named db cannot be found.
21970 */
21971 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
21972 int i = -1; /* Database number */
21973 if( zName ){
21974 Db *pDb;
21975 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
21976 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
21977 /* "main" is always an acceptable alias for the primary database
21978 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
21979 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
21980 }
21981 }
21982 return i;
21983 }
21984
21985 /*
21986 ** The token *pName contains the name of a database (either "main" or
21987 ** "temp" or the name of an attached db). This routine returns the
21988 ** index of the named database in db->aDb[], or -1 if the named db
21989 ** does not exist.
21990 */
21991 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
21992 int i; /* Database number */
21993 char *zName; /* Name we are searching for */
21994 zName = sqlite3NameFromToken(db, pName);
21995 i = sqlite3FindDbName(db, zName);
21996 sqlite3DbFree(db, zName);
21997 return i;
21998 }
21999
22000 /* The table or view or trigger name is passed to this routine via tokens
22001 ** pName1 and pName2. If the table name was fully qualified, for example:
22002 **
22003 ** CREATE TABLE xxx.yyy (...);
22004 **
22005 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
22006 ** the table name is not fully qualified, i.e.:
22007 **
22008 ** CREATE TABLE yyy(...);
22009 **
22010 ** Then pName1 is set to "yyy" and pName2 is "".
22011 **
22012 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
22013 ** pName2) that stores the unqualified table name. The index of the
22014 ** database "xxx" is returned.
22015 */
22016 SQLITE_PRIVATE int sqlite3TwoPartName(
22017 Parse *pParse, /* Parsing and code generating context */
22018 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
22019 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
22020 Token **pUnqual /* Write the unqualified object name here */
22021 ){
22022 int iDb; /* Database holding the object */
22023 sqlite3 *db = pParse->db;
22024
22025 assert( pName2!=0 );
22026 if( pName2->n>0 ){
22027 if( db->init.busy ) {
22028 sqlite3ErrorMsg(pParse, "corrupt database");
22029 return -1;
22030 }
22031 *pUnqual = pName2;
22032 iDb = sqlite3FindDb(db, pName1);
22033 if( iDb<0 ){
22034 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
22035 return -1;
22036 }
22037 }else{
22038 assert( db->init.iDb==0 || db->init.busy || (db->flags & SQLITE_Vacuum)!=0);
22039 iDb = db->init.iDb;
22040 *pUnqual = pName1;
22041 }
22042 return iDb;
22043 }
22044
22045 /*
22046 ** This routine is used to check if the UTF-8 string zName is a legal
22047 ** unqualified name for a new schema object (table, index, view or
22048 ** trigger). All names are legal except those that begin with the string
22049 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
22050 ** is reserved for internal use.
22051 */
22052 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
22053 if( !pParse->db->init.busy && pParse->nested==0
22054 && (pParse->db->flags & SQLITE_WriteSchema)==0
22055 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
22056 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
22057 return SQLITE_ERROR;
22058 }
22059 return SQLITE_OK;
22060 }
22061
22062 /*
22063 ** Return the PRIMARY KEY index of a table
22064 */
22065 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
22066 Index *p;
22067 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
22068 return p;
22069 }
22070
22071 /*
22072 ** Return the column of index pIdx that corresponds to table
22073 ** column iCol. Return -1 if not found.
22074 */
22075 SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
22076 int i;
22077 for(i=0; i<pIdx->nColumn; i++){
22078 if( iCol==pIdx->aiColumn[i] ) return i;
22079 }
22080 return -1;
22081 }
22082
22083 /*
22084 ** Begin constructing a new table representation in memory. This is
22085 ** the first of several action routines that get called in response
22086 ** to a CREATE TABLE statement. In particular, this routine is called
22087 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
22088 ** flag is true if the table should be stored in the auxiliary database
22089 ** file instead of in the main database file. This is normally the case
22090 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
22091 ** CREATE and TABLE.
22092 **
22093 ** The new table record is initialized and put in pParse->pNewTable.
22094 ** As more of the CREATE TABLE statement is parsed, additional action
22095 ** routines will be called to add more information to this record.
22096 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
22097 ** is called to complete the construction of the new table record.
22098 */
22099 SQLITE_PRIVATE void sqlite3StartTable(
22100 Parse *pParse, /* Parser context */
22101 Token *pName1, /* First part of the name of the table or view */
22102 Token *pName2, /* Second part of the name of the table or view */
22103 int isTemp, /* True if this is a TEMP table */
22104 int isView, /* True if this is a VIEW */
22105 int isVirtual, /* True if this is a VIRTUAL table */
22106 int noErr /* Do nothing if table already exists */
22107 ){
22108 Table *pTable;
22109 char *zName = 0; /* The name of the new table */
22110 sqlite3 *db = pParse->db;
22111 Vdbe *v;
22112 int iDb; /* Database number to create the table in */
22113 Token *pName; /* Unqualified name of the table to create */
22114
22115 if( db->init.busy && db->init.newTnum==1 ){
22116 /* Special case: Parsing the sqlite_master or sqlite_temp_master schema */
22117 iDb = db->init.iDb;
22118 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
22119 pName = pName1;
22120 }else{
22121 /* The common case */
22122 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
22123 if( iDb<0 ) return;
22124 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
22125 /* If creating a temp table, the name may not be qualified. Unless
22126 ** the database name is "temp" anyway. */
22127 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
22128 return;
22129 }
22130 if( !OMIT_TEMPDB && isTemp ) iDb = 1;
22131 zName = sqlite3NameFromToken(db, pName);
22132 }
22133 pParse->sNameToken = *pName;
22134 if( zName==0 ) return;
22135 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
22136 goto begin_table_error;
22137 }
22138 if( db->init.iDb==1 ) isTemp = 1;
22139 #ifndef SQLITE_OMIT_AUTHORIZATION
22140 assert( isTemp==0 || isTemp==1 );
22141 assert( isView==0 || isView==1 );
22142 {
22143 static const u8 aCode[] = {
22144 SQLITE_CREATE_TABLE,
22145 SQLITE_CREATE_TEMP_TABLE,
22146 SQLITE_CREATE_VIEW,
22147 SQLITE_CREATE_TEMP_VIEW
22148 };
22149 char *zDb = db->aDb[iDb].zDbSName;
22150 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
22151 goto begin_table_error;
22152 }
22153 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
22154 zName, 0, zDb) ){
22155 goto begin_table_error;
22156 }
22157 }
22158 #endif
22159
22160 /* Make sure the new table name does not collide with an existing
22161 ** index or table name in the same database. Issue an error message if
22162 ** it does. The exception is if the statement being parsed was passed
22163 ** to an sqlite3_declare_vtab() call. In that case only the column names
22164 ** and types will be used, so there is no need to test for namespace
22165 ** collisions.
22166 */
22167 if( !IN_DECLARE_VTAB ){
22168 char *zDb = db->aDb[iDb].zDbSName;
22169 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
22170 goto begin_table_error;
22171 }
22172 pTable = sqlite3FindTable(db, zName, zDb);
22173 if( pTable ){
22174 if( !noErr ){
22175 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
22176 }else{
22177 assert( !db->init.busy || CORRUPT_DB );
22178 sqlite3CodeVerifySchema(pParse, iDb);
22179 }
22180 goto begin_table_error;
22181 }
22182 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
22183 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
22184 goto begin_table_error;
22185 }
22186 }
22187
22188 pTable = sqlite3DbMallocZero(db, sizeof(Table));
22189 if( pTable==0 ){
22190 assert( db->mallocFailed );
22191 pParse->rc = SQLITE_NOMEM_BKPT;
22192 pParse->nErr++;
22193 goto begin_table_error;
22194 }
22195 pTable->zName = zName;
22196 pTable->iPKey = -1;
22197 pTable->pSchema = db->aDb[iDb].pSchema;
22198 pTable->nTabRef = 1;
22199 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
22200 assert( pParse->pNewTable==0 );
22201 pParse->pNewTable = pTable;
22202
22203 /* If this is the magic sqlite_sequence table used by autoincrement,
22204 ** then record a pointer to this table in the main database structure
22205 ** so that INSERT can find the table easily.
22206 */
22207 #ifndef SQLITE_OMIT_AUTOINCREMENT
22208 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
22209 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
22210 pTable->pSchema->pSeqTab = pTable;
22211 }
22212 #endif
22213
22214 /* Begin generating the code that will insert the table record into
22215 ** the SQLITE_MASTER table. Note in particular that we must go ahead
22216 ** and allocate the record number for the table entry now. Before any
22217 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
22218 ** indices to be created and the table record must come before the
22219 ** indices. Hence, the record number for the table must be allocated
22220 ** now.
22221 */
22222 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
22223 int addr1;
22224 int fileFormat;
22225 int reg1, reg2, reg3;
22226 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
22227 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
22228 sqlite3BeginWriteOperation(pParse, 1, iDb);
22229
22230 #ifndef SQLITE_OMIT_VIRTUALTABLE
22231 if( isVirtual ){
22232 sqlite3VdbeAddOp0(v, OP_VBegin);
22233 }
22234 #endif
22235
22236 /* If the file format and encoding in the database have not been set,
22237 ** set them now.
22238 */
22239 reg1 = pParse->regRowid = ++pParse->nMem;
22240 reg2 = pParse->regRoot = ++pParse->nMem;
22241 reg3 = ++pParse->nMem;
22242 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
22243 sqlite3VdbeUsesBtree(v, iDb);
22244 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
22245 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
22246 1 : SQLITE_MAX_FILE_FORMAT;
22247 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
22248 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
22249 sqlite3VdbeJumpHere(v, addr1);
22250
22251 /* This just creates a place-holder record in the sqlite_master table.
22252 ** The record created does not contain anything yet. It will be replaced
22253 ** by the real entry in code generated at sqlite3EndTable().
22254 **
22255 ** The rowid for the new entry is left in register pParse->regRowid.
22256 ** The root page number of the new table is left in reg pParse->regRoot.
22257 ** The rowid and root page number values are needed by the code that
22258 ** sqlite3EndTable will generate.
22259 */
22260 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
22261 if( isView || isVirtual ){
22262 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
22263 }else
22264 #endif
22265 {
22266 pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
22267 }
22268 sqlite3OpenMasterTable(pParse, iDb);
22269 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
22270 sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
22271 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
22272 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
22273 sqlite3VdbeAddOp0(v, OP_Close);
22274 }
22275
22276 /* Normal (non-error) return. */
22277 return;
22278
22279 /* If an error occurs, we jump here */
22280 begin_table_error:
22281 sqlite3DbFree(db, zName);
22282 return;
22283 }
22284
22285 /* Set properties of a table column based on the (magical)
22286 ** name of the column.
22287 */
22288 #if SQLITE_ENABLE_HIDDEN_COLUMNS
22289 SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
22290 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
22291 pCol->colFlags |= COLFLAG_HIDDEN;
22292 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
22293 pTab->tabFlags |= TF_OOOHidden;
22294 }
22295 }
22296 #endif
22297
22298
22299 /*
22300 ** Add a new column to the table currently being constructed.
22301 **
22302 ** The parser calls this routine once for each column declaration
22303 ** in a CREATE TABLE statement. sqlite3StartTable() gets called
22304 ** first to get things going. Then this routine is called for each
22305 ** column.
22306 */
22307 SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){
22308 Table *p;
22309 int i;
22310 char *z;
22311 char *zType;
22312 Column *pCol;
22313 sqlite3 *db = pParse->db;
22314 if( (p = pParse->pNewTable)==0 ) return;
22315 #if SQLITE_MAX_COLUMN
22316 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
22317 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
22318 return;
22319 }
22320 #endif
22321 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
22322 if( z==0 ) return;
22323 memcpy(z, pName->z, pName->n);
22324 z[pName->n] = 0;
22325 sqlite3Dequote(z);
22326 for(i=0; i<p->nCol; i++){
22327 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
22328 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
22329 sqlite3DbFree(db, z);
22330 return;
22331 }
22332 }
22333 if( (p->nCol & 0x7)==0 ){
22334 Column *aNew;
22335 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
22336 if( aNew==0 ){
22337 sqlite3DbFree(db, z);
22338 return;
22339 }
22340 p->aCol = aNew;
22341 }
22342 pCol = &p->aCol[p->nCol];
22343 memset(pCol, 0, sizeof(p->aCol[0]));
22344 pCol->zName = z;
22345 sqlite3ColumnPropertiesFromName(p, pCol);
22346
22347 if( pType->n==0 ){
22348 /* If there is no type specified, columns have the default affinity
22349 ** 'BLOB'. */
22350 pCol->affinity = SQLITE_AFF_BLOB;
22351 pCol->szEst = 1;
22352 }else{
22353 zType = z + sqlite3Strlen30(z) + 1;
22354 memcpy(zType, pType->z, pType->n);
22355 zType[pType->n] = 0;
22356 sqlite3Dequote(zType);
22357 pCol->affinity = sqlite3AffinityType(zType, &pCol->szEst);
22358 pCol->colFlags |= COLFLAG_HASTYPE;
22359 }
22360 p->nCol++;
22361 pParse->constraintName.n = 0;
22362 }
22363
22364 /*
22365 ** This routine is called by the parser while in the middle of
22366 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
22367 ** been seen on a column. This routine sets the notNull flag on
22368 ** the column currently under construction.
22369 */
22370 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
22371 Table *p;
22372 p = pParse->pNewTable;
22373 if( p==0 || NEVER(p->nCol<1) ) return;
22374 p->aCol[p->nCol-1].notNull = (u8)onError;
22375 }
22376
22377 /*
22378 ** Scan the column type name zType (length nType) and return the
22379 ** associated affinity type.
22380 **
22381 ** This routine does a case-independent search of zType for the
22382 ** substrings in the following table. If one of the substrings is
22383 ** found, the corresponding affinity is returned. If zType contains
22384 ** more than one of the substrings, entries toward the top of
22385 ** the table take priority. For example, if zType is 'BLOBINT',
22386 ** SQLITE_AFF_INTEGER is returned.
22387 **
22388 ** Substring | Affinity
22389 ** --------------------------------
22390 ** 'INT' | SQLITE_AFF_INTEGER
22391 ** 'CHAR' | SQLITE_AFF_TEXT
22392 ** 'CLOB' | SQLITE_AFF_TEXT
22393 ** 'TEXT' | SQLITE_AFF_TEXT
22394 ** 'BLOB' | SQLITE_AFF_BLOB
22395 ** 'REAL' | SQLITE_AFF_REAL
22396 ** 'FLOA' | SQLITE_AFF_REAL
22397 ** 'DOUB' | SQLITE_AFF_REAL
22398 **
22399 ** If none of the substrings in the above table are found,
22400 ** SQLITE_AFF_NUMERIC is returned.
22401 */
22402 SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
22403 u32 h = 0;
22404 char aff = SQLITE_AFF_NUMERIC;
22405 const char *zChar = 0;
22406
22407 assert( zIn!=0 );
22408 while( zIn[0] ){
22409 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
22410 zIn++;
22411 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
22412 aff = SQLITE_AFF_TEXT;
22413 zChar = zIn;
22414 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
22415 aff = SQLITE_AFF_TEXT;
22416 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
22417 aff = SQLITE_AFF_TEXT;
22418 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
22419 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
22420 aff = SQLITE_AFF_BLOB;
22421 if( zIn[0]=='(' ) zChar = zIn;
22422 #ifndef SQLITE_OMIT_FLOATING_POINT
22423 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
22424 && aff==SQLITE_AFF_NUMERIC ){
22425 aff = SQLITE_AFF_REAL;
22426 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
22427 && aff==SQLITE_AFF_NUMERIC ){
22428 aff = SQLITE_AFF_REAL;
22429 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
22430 && aff==SQLITE_AFF_NUMERIC ){
22431 aff = SQLITE_AFF_REAL;
22432 #endif
22433 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
22434 aff = SQLITE_AFF_INTEGER;
22435 break;
22436 }
22437 }
22438
22439 /* If pszEst is not NULL, store an estimate of the field size. The
22440 ** estimate is scaled so that the size of an integer is 1. */
22441 if( pszEst ){
22442 *pszEst = 1; /* default size is approx 4 bytes */
22443 if( aff<SQLITE_AFF_NUMERIC ){
22444 if( zChar ){
22445 while( zChar[0] ){
22446 if( sqlite3Isdigit(zChar[0]) ){
22447 int v = 0;
22448 sqlite3GetInt32(zChar, &v);
22449 v = v/4 + 1;
22450 if( v>255 ) v = 255;
22451 *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
22452 break;
22453 }
22454 zChar++;
22455 }
22456 }else{
22457 *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
22458 }
22459 }
22460 }
22461 return aff;
22462 }
22463
22464 /*
22465 ** The expression is the default value for the most recently added column
22466 ** of the table currently under construction.
22467 **
22468 ** Default value expressions must be constant. Raise an exception if this
22469 ** is not the case.
22470 **
22471 ** This routine is called by the parser while in the middle of
22472 ** parsing a CREATE TABLE statement.
22473 */
22474 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
22475 Table *p;
22476 Column *pCol;
22477 sqlite3 *db = pParse->db;
22478 p = pParse->pNewTable;
22479 if( p!=0 ){
22480 pCol = &(p->aCol[p->nCol-1]);
22481 if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){
22482 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
22483 pCol->zName);
22484 }else{
22485 /* A copy of pExpr is used instead of the original, as pExpr contains
22486 ** tokens that point to volatile memory. The 'span' of the expression
22487 ** is required by pragma table_info.
22488 */
22489 Expr x;
22490 sqlite3ExprDelete(db, pCol->pDflt);
22491 memset(&x, 0, sizeof(x));
22492 x.op = TK_SPAN;
22493 x.u.zToken = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
22494 (int)(pSpan->zEnd - pSpan->zStart));
22495 x.pLeft = pSpan->pExpr;
22496 x.flags = EP_Skip;
22497 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
22498 sqlite3DbFree(db, x.u.zToken);
22499 }
22500 }
22501 sqlite3ExprDelete(db, pSpan->pExpr);
22502 }
22503
22504 /*
22505 ** Backwards Compatibility Hack:
22506 **
22507 ** Historical versions of SQLite accepted strings as column names in
22508 ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
22509 **
22510 ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
22511 ** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
22512 **
22513 ** This is goofy. But to preserve backwards compatibility we continue to
22514 ** accept it. This routine does the necessary conversion. It converts
22515 ** the expression given in its argument from a TK_STRING into a TK_ID
22516 ** if the expression is just a TK_STRING with an optional COLLATE clause.
22517 ** If the epxression is anything other than TK_STRING, the expression is
22518 ** unchanged.
22519 */
22520 static void sqlite3StringToId(Expr *p){
22521 if( p->op==TK_STRING ){
22522 p->op = TK_ID;
22523 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
22524 p->pLeft->op = TK_ID;
22525 }
22526 }
22527
22528 /*
22529 ** Designate the PRIMARY KEY for the table. pList is a list of names
22530 ** of columns that form the primary key. If pList is NULL, then the
22531 ** most recently added column of the table is the primary key.
22532 **
22533 ** A table can have at most one primary key. If the table already has
22534 ** a primary key (and this is the second primary key) then create an
22535 ** error.
22536 **
22537 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
22538 ** then we will try to use that column as the rowid. Set the Table.iPKey
22539 ** field of the table under construction to be the index of the
22540 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
22541 ** no INTEGER PRIMARY KEY.
22542 **
22543 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
22544 ** index for the key. No index is created for INTEGER PRIMARY KEYs.
22545 */
22546 SQLITE_PRIVATE void sqlite3AddPrimaryKey(
22547 Parse *pParse, /* Parsing context */
22548 ExprList *pList, /* List of field names to be indexed */
22549 int onError, /* What to do with a uniqueness conflict */
22550 int autoInc, /* True if the AUTOINCREMENT keyword is present */
22551 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
22552 ){
22553 Table *pTab = pParse->pNewTable;
22554 Column *pCol = 0;
22555 int iCol = -1, i;
22556 int nTerm;
22557 if( pTab==0 ) goto primary_key_exit;
22558 if( pTab->tabFlags & TF_HasPrimaryKey ){
22559 sqlite3ErrorMsg(pParse,
22560 "table \"%s\" has more than one primary key", pTab->zName);
22561 goto primary_key_exit;
22562 }
22563 pTab->tabFlags |= TF_HasPrimaryKey;
22564 if( pList==0 ){
22565 iCol = pTab->nCol - 1;
22566 pCol = &pTab->aCol[iCol];
22567 pCol->colFlags |= COLFLAG_PRIMKEY;
22568 nTerm = 1;
22569 }else{
22570 nTerm = pList->nExpr;
22571 for(i=0; i<nTerm; i++){
22572 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
22573 assert( pCExpr!=0 );
22574 sqlite3StringToId(pCExpr);
22575 if( pCExpr->op==TK_ID ){
22576 const char *zCName = pCExpr->u.zToken;
22577 for(iCol=0; iCol<pTab->nCol; iCol++){
22578 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
22579 pCol = &pTab->aCol[iCol];
22580 pCol->colFlags |= COLFLAG_PRIMKEY;
22581 break;
22582 }
22583 }
22584 }
22585 }
22586 }
22587 if( nTerm==1
22588 && pCol
22589 && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
22590 && sortOrder!=SQLITE_SO_DESC
22591 ){
22592 pTab->iPKey = iCol;
22593 pTab->keyConf = (u8)onError;
22594 assert( autoInc==0 || autoInc==1 );
22595 pTab->tabFlags |= autoInc*TF_Autoincrement;
22596 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
22597 }else if( autoInc ){
22598 #ifndef SQLITE_OMIT_AUTOINCREMENT
22599 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
22600 "INTEGER PRIMARY KEY");
22601 #endif
22602 }else{
22603 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
22604 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
22605 pList = 0;
22606 }
22607
22608 primary_key_exit:
22609 sqlite3ExprListDelete(pParse->db, pList);
22610 return;
22611 }
22612
22613 /*
22614 ** Add a new CHECK constraint to the table currently under construction.
22615 */
22616 SQLITE_PRIVATE void sqlite3AddCheckConstraint(
22617 Parse *pParse, /* Parsing context */
22618 Expr *pCheckExpr /* The check expression */
22619 ){
22620 #ifndef SQLITE_OMIT_CHECK
22621 Table *pTab = pParse->pNewTable;
22622 sqlite3 *db = pParse->db;
22623 if( pTab && !IN_DECLARE_VTAB
22624 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
22625 ){
22626 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
22627 if( pParse->constraintName.n ){
22628 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
22629 }
22630 }else
22631 #endif
22632 {
22633 sqlite3ExprDelete(pParse->db, pCheckExpr);
22634 }
22635 }
22636
22637 /*
22638 ** Set the collation function of the most recently parsed table column
22639 ** to the CollSeq given.
22640 */
22641 SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
22642 Table *p;
22643 int i;
22644 char *zColl; /* Dequoted name of collation sequence */
22645 sqlite3 *db;
22646
22647 if( (p = pParse->pNewTable)==0 ) return;
22648 i = p->nCol-1;
22649 db = pParse->db;
22650 zColl = sqlite3NameFromToken(db, pToken);
22651 if( !zColl ) return;
22652
22653 if( sqlite3LocateCollSeq(pParse, zColl) ){
22654 Index *pIdx;
22655 sqlite3DbFree(db, p->aCol[i].zColl);
22656 p->aCol[i].zColl = zColl;
22657
22658 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
22659 ** then an index may have been created on this column before the
22660 ** collation type was added. Correct this if it is the case.
22661 */
22662 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
22663 assert( pIdx->nKeyCol==1 );
22664 if( pIdx->aiColumn[0]==i ){
22665 pIdx->azColl[0] = p->aCol[i].zColl;
22666 }
22667 }
22668 }else{
22669 sqlite3DbFree(db, zColl);
22670 }
22671 }
22672
22673 /*
22674 ** This function returns the collation sequence for database native text
22675 ** encoding identified by the string zName, length nName.
22676 **
22677 ** If the requested collation sequence is not available, or not available
22678 ** in the database native encoding, the collation factory is invoked to
22679 ** request it. If the collation factory does not supply such a sequence,
22680 ** and the sequence is available in another text encoding, then that is
22681 ** returned instead.
22682 **
22683 ** If no versions of the requested collations sequence are available, or
22684 ** another error occurs, NULL is returned and an error message written into
22685 ** pParse.
22686 **
22687 ** This routine is a wrapper around sqlite3FindCollSeq(). This routine
22688 ** invokes the collation factory if the named collation cannot be found
22689 ** and generates an error message.
22690 **
22691 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
22692 */
22693 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
22694 sqlite3 *db = pParse->db;
22695 u8 enc = ENC(db);
22696 u8 initbusy = db->init.busy;
22697 CollSeq *pColl;
22698
22699 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
22700 if( !initbusy && (!pColl || !pColl->xCmp) ){
22701 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
22702 }
22703
22704 return pColl;
22705 }
22706
22707
22708 /*
22709 ** Generate code that will increment the schema cookie.
22710 **
22711 ** The schema cookie is used to determine when the schema for the
22712 ** database changes. After each schema change, the cookie value
22713 ** changes. When a process first reads the schema it records the
22714 ** cookie. Thereafter, whenever it goes to access the database,
22715 ** it checks the cookie to make sure the schema has not changed
22716 ** since it was last read.
22717 **
22718 ** This plan is not completely bullet-proof. It is possible for
22719 ** the schema to change multiple times and for the cookie to be
22720 ** set back to prior value. But schema changes are infrequent
22721 ** and the probability of hitting the same cookie value is only
22722 ** 1 chance in 2^32. So we're safe enough.
22723 **
22724 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
22725 ** the schema-version whenever the schema changes.
22726 */
22727 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
22728 sqlite3 *db = pParse->db;
22729 Vdbe *v = pParse->pVdbe;
22730 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
22731 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION,
22732 db->aDb[iDb].pSchema->schema_cookie+1);
22733 }
22734
22735 /*
22736 ** Measure the number of characters needed to output the given
22737 ** identifier. The number returned includes any quotes used
22738 ** but does not include the null terminator.
22739 **
22740 ** The estimate is conservative. It might be larger that what is
22741 ** really needed.
22742 */
22743 static int identLength(const char *z){
22744 int n;
22745 for(n=0; *z; n++, z++){
22746 if( *z=='"' ){ n++; }
22747 }
22748 return n + 2;
22749 }
22750
22751 /*
22752 ** The first parameter is a pointer to an output buffer. The second
22753 ** parameter is a pointer to an integer that contains the offset at
22754 ** which to write into the output buffer. This function copies the
22755 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
22756 ** to the specified offset in the buffer and updates *pIdx to refer
22757 ** to the first byte after the last byte written before returning.
22758 **
22759 ** If the string zSignedIdent consists entirely of alpha-numeric
22760 ** characters, does not begin with a digit and is not an SQL keyword,
22761 ** then it is copied to the output buffer exactly as it is. Otherwise,
22762 ** it is quoted using double-quotes.
22763 */
22764 static void identPut(char *z, int *pIdx, char *zSignedIdent){
22765 unsigned char *zIdent = (unsigned char*)zSignedIdent;
22766 int i, j, needQuote;
22767 i = *pIdx;
22768
22769 for(j=0; zIdent[j]; j++){
22770 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
22771 }
22772 needQuote = sqlite3Isdigit(zIdent[0])
22773 || sqlite3KeywordCode(zIdent, j)!=TK_ID
22774 || zIdent[j]!=0
22775 || j==0;
22776
22777 if( needQuote ) z[i++] = '"';
22778 for(j=0; zIdent[j]; j++){
22779 z[i++] = zIdent[j];
22780 if( zIdent[j]=='"' ) z[i++] = '"';
22781 }
22782 if( needQuote ) z[i++] = '"';
22783 z[i] = 0;
22784 *pIdx = i;
22785 }
22786
22787 /*
22788 ** Generate a CREATE TABLE statement appropriate for the given
22789 ** table. Memory to hold the text of the statement is obtained
22790 ** from sqliteMalloc() and must be freed by the calling function.
22791 */
22792 static char *createTableStmt(sqlite3 *db, Table *p){
22793 int i, k, n;
22794 char *zStmt;
22795 char *zSep, *zSep2, *zEnd;
22796 Column *pCol;
22797 n = 0;
22798 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
22799 n += identLength(pCol->zName) + 5;
22800 }
22801 n += identLength(p->zName);
22802 if( n<50 ){
22803 zSep = "";
22804 zSep2 = ",";
22805 zEnd = ")";
22806 }else{
22807 zSep = "\n ";
22808 zSep2 = ",\n ";
22809 zEnd = "\n)";
22810 }
22811 n += 35 + 6*p->nCol;
22812 zStmt = sqlite3DbMallocRaw(0, n);
22813 if( zStmt==0 ){
22814 sqlite3OomFault(db);
22815 return 0;
22816 }
22817 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
22818 k = sqlite3Strlen30(zStmt);
22819 identPut(zStmt, &k, p->zName);
22820 zStmt[k++] = '(';
22821 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
22822 static const char * const azType[] = {
22823 /* SQLITE_AFF_BLOB */ "",
22824 /* SQLITE_AFF_TEXT */ " TEXT",
22825 /* SQLITE_AFF_NUMERIC */ " NUM",
22826 /* SQLITE_AFF_INTEGER */ " INT",
22827 /* SQLITE_AFF_REAL */ " REAL"
22828 };
22829 int len;
22830 const char *zType;
22831
22832 sqlite3_snprintf(n-k, &zStmt[k], zSep);
22833 k += sqlite3Strlen30(&zStmt[k]);
22834 zSep = zSep2;
22835 identPut(zStmt, &k, pCol->zName);
22836 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
22837 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
22838 testcase( pCol->affinity==SQLITE_AFF_BLOB );
22839 testcase( pCol->affinity==SQLITE_AFF_TEXT );
22840 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
22841 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
22842 testcase( pCol->affinity==SQLITE_AFF_REAL );
22843
22844 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
22845 len = sqlite3Strlen30(zType);
22846 assert( pCol->affinity==SQLITE_AFF_BLOB
22847 || pCol->affinity==sqlite3AffinityType(zType, 0) );
22848 memcpy(&zStmt[k], zType, len);
22849 k += len;
22850 assert( k<=n );
22851 }
22852 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
22853 return zStmt;
22854 }
22855
22856 /*
22857 ** Resize an Index object to hold N columns total. Return SQLITE_OK
22858 ** on success and SQLITE_NOMEM on an OOM error.
22859 */
22860 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
22861 char *zExtra;
22862 int nByte;
22863 if( pIdx->nColumn>=N ) return SQLITE_OK;
22864 assert( pIdx->isResized==0 );
22865 nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
22866 zExtra = sqlite3DbMallocZero(db, nByte);
22867 if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
22868 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
22869 pIdx->azColl = (const char**)zExtra;
22870 zExtra += sizeof(char*)*N;
22871 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
22872 pIdx->aiColumn = (i16*)zExtra;
22873 zExtra += sizeof(i16)*N;
22874 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
22875 pIdx->aSortOrder = (u8*)zExtra;
22876 pIdx->nColumn = N;
22877 pIdx->isResized = 1;
22878 return SQLITE_OK;
22879 }
22880
22881 /*
22882 ** Estimate the total row width for a table.
22883 */
22884 static void estimateTableWidth(Table *pTab){
22885 unsigned wTable = 0;
22886 const Column *pTabCol;
22887 int i;
22888 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
22889 wTable += pTabCol->szEst;
22890 }
22891 if( pTab->iPKey<0 ) wTable++;
22892 pTab->szTabRow = sqlite3LogEst(wTable*4);
22893 }
22894
22895 /*
22896 ** Estimate the average size of a row for an index.
22897 */
22898 static void estimateIndexWidth(Index *pIdx){
22899 unsigned wIndex = 0;
22900 int i;
22901 const Column *aCol = pIdx->pTable->aCol;
22902 for(i=0; i<pIdx->nColumn; i++){
22903 i16 x = pIdx->aiColumn[i];
22904 assert( x<pIdx->pTable->nCol );
22905 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
22906 }
22907 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
22908 }
22909
22910 /* Return true if value x is found any of the first nCol entries of aiCol[]
22911 */
22912 static int hasColumn(const i16 *aiCol, int nCol, int x){
22913 while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
22914 return 0;
22915 }
22916
22917 /*
22918 ** This routine runs at the end of parsing a CREATE TABLE statement that
22919 ** has a WITHOUT ROWID clause. The job of this routine is to convert both
22920 ** internal schema data structures and the generated VDBE code so that they
22921 ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
22922 ** Changes include:
22923 **
22924 ** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
22925 ** (2) Convert the OP_CreateTable into an OP_CreateIndex. There is
22926 ** no rowid btree for a WITHOUT ROWID. Instead, the canonical
22927 ** data storage is a covering index btree.
22928 ** (3) Bypass the creation of the sqlite_master table entry
22929 ** for the PRIMARY KEY as the primary key index is now
22930 ** identified by the sqlite_master table entry of the table itself.
22931 ** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
22932 ** schema to the rootpage from the main table.
22933 ** (5) Add all table columns to the PRIMARY KEY Index object
22934 ** so that the PRIMARY KEY is a covering index. The surplus
22935 ** columns are part of KeyInfo.nXField and are not used for
22936 ** sorting or lookup or uniqueness checks.
22937 ** (6) Replace the rowid tail on all automatically generated UNIQUE
22938 ** indices with the PRIMARY KEY columns.
22939 **
22940 ** For virtual tables, only (1) is performed.
22941 */
22942 static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
22943 Index *pIdx;
22944 Index *pPk;
22945 int nPk;
22946 int i, j;
22947 sqlite3 *db = pParse->db;
22948 Vdbe *v = pParse->pVdbe;
22949
22950 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
22951 */
22952 if( !db->init.imposterTable ){
22953 for(i=0; i<pTab->nCol; i++){
22954 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
22955 pTab->aCol[i].notNull = OE_Abort;
22956 }
22957 }
22958 }
22959
22960 /* The remaining transformations only apply to b-tree tables, not to
22961 ** virtual tables */
22962 if( IN_DECLARE_VTAB ) return;
22963
22964 /* Convert the OP_CreateTable opcode that would normally create the
22965 ** root-page for the table into an OP_CreateIndex opcode. The index
22966 ** created will become the PRIMARY KEY index.
22967 */
22968 if( pParse->addrCrTab ){
22969 assert( v );
22970 sqlite3VdbeChangeOpcode(v, pParse->addrCrTab, OP_CreateIndex);
22971 }
22972
22973 /* Locate the PRIMARY KEY index. Or, if this table was originally
22974 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
22975 */
22976 if( pTab->iPKey>=0 ){
22977 ExprList *pList;
22978 Token ipkToken;
22979 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
22980 pList = sqlite3ExprListAppend(pParse, 0,
22981 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
22982 if( pList==0 ) return;
22983 pList->a[0].sortOrder = pParse->iPkSortOrder;
22984 assert( pParse->pNewTable==pTab );
22985 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
22986 SQLITE_IDXTYPE_PRIMARYKEY);
22987 if( db->mallocFailed ) return;
22988 pPk = sqlite3PrimaryKeyIndex(pTab);
22989 pTab->iPKey = -1;
22990 }else{
22991 pPk = sqlite3PrimaryKeyIndex(pTab);
22992
22993 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
22994 ** table entry. This is only required if currently generating VDBE
22995 ** code for a CREATE TABLE (not when parsing one as part of reading
22996 ** a database schema). */
22997 if( v ){
22998 assert( db->init.busy==0 );
22999 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto);
23000 }
23001
23002 /*
23003 ** Remove all redundant columns from the PRIMARY KEY. For example, change
23004 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
23005 ** code assumes the PRIMARY KEY contains no repeated columns.
23006 */
23007 for(i=j=1; i<pPk->nKeyCol; i++){
23008 if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){
23009 pPk->nColumn--;
23010 }else{
23011 pPk->aiColumn[j++] = pPk->aiColumn[i];
23012 }
23013 }
23014 pPk->nKeyCol = j;
23015 }
23016 assert( pPk!=0 );
23017 pPk->isCovering = 1;
23018 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
23019 nPk = pPk->nKeyCol;
23020
23021 /* The root page of the PRIMARY KEY is the table root page */
23022 pPk->tnum = pTab->tnum;
23023
23024 /* Update the in-memory representation of all UNIQUE indices by converting
23025 ** the final rowid column into one or more columns of the PRIMARY KEY.
23026 */
23027 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
23028 int n;
23029 if( IsPrimaryKeyIndex(pIdx) ) continue;
23030 for(i=n=0; i<nPk; i++){
23031 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
23032 }
23033 if( n==0 ){
23034 /* This index is a superset of the primary key */
23035 pIdx->nColumn = pIdx->nKeyCol;
23036 continue;
23037 }
23038 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
23039 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
23040 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
23041 pIdx->aiColumn[j] = pPk->aiColumn[i];
23042 pIdx->azColl[j] = pPk->azColl[i];
23043 j++;
23044 }
23045 }
23046 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
23047 assert( pIdx->nColumn>=j );
23048 }
23049
23050 /* Add all table columns to the PRIMARY KEY index
23051 */
23052 if( nPk<pTab->nCol ){
23053 if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
23054 for(i=0, j=nPk; i<pTab->nCol; i++){
23055 if( !hasColumn(pPk->aiColumn, j, i) ){
23056 assert( j<pPk->nColumn );
23057 pPk->aiColumn[j] = i;
23058 pPk->azColl[j] = sqlite3StrBINARY;
23059 j++;
23060 }
23061 }
23062 assert( pPk->nColumn==j );
23063 assert( pTab->nCol==j );
23064 }else{
23065 pPk->nColumn = pTab->nCol;
23066 }
23067 }
23068
23069 /*
23070 ** This routine is called to report the final ")" that terminates
23071 ** a CREATE TABLE statement.
23072 **
23073 ** The table structure that other action routines have been building
23074 ** is added to the internal hash tables, assuming no errors have
23075 ** occurred.
23076 **
23077 ** An entry for the table is made in the master table on disk, unless
23078 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
23079 ** it means we are reading the sqlite_master table because we just
23080 ** connected to the database or because the sqlite_master table has
23081 ** recently changed, so the entry for this table already exists in
23082 ** the sqlite_master table. We do not want to create it again.
23083 **
23084 ** If the pSelect argument is not NULL, it means that this routine
23085 ** was called to create a table generated from a
23086 ** "CREATE TABLE ... AS SELECT ..." statement. The column names of
23087 ** the new table will match the result set of the SELECT.
23088 */
23089 SQLITE_PRIVATE void sqlite3EndTable(
23090 Parse *pParse, /* Parse context */
23091 Token *pCons, /* The ',' token after the last column defn. */
23092 Token *pEnd, /* The ')' before options in the CREATE TABLE */
23093 u8 tabOpts, /* Extra table options. Usually 0. */
23094 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
23095 ){
23096 Table *p; /* The new table */
23097 sqlite3 *db = pParse->db; /* The database connection */
23098 int iDb; /* Database in which the table lives */
23099 Index *pIdx; /* An implied index of the table */
23100
23101 if( pEnd==0 && pSelect==0 ){
23102 return;
23103 }
23104 assert( !db->mallocFailed );
23105 p = pParse->pNewTable;
23106 if( p==0 ) return;
23107
23108 assert( !db->init.busy || !pSelect );
23109
23110 /* If the db->init.busy is 1 it means we are reading the SQL off the
23111 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
23112 ** So do not write to the disk again. Extract the root page number
23113 ** for the table from the db->init.newTnum field. (The page number
23114 ** should have been put there by the sqliteOpenCb routine.)
23115 **
23116 ** If the root page number is 1, that means this is the sqlite_master
23117 ** table itself. So mark it read-only.
23118 */
23119 if( db->init.busy ){
23120 p->tnum = db->init.newTnum;
23121 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
23122 }
23123
23124 /* Special processing for WITHOUT ROWID Tables */
23125 if( tabOpts & TF_WithoutRowid ){
23126 if( (p->tabFlags & TF_Autoincrement) ){
23127 sqlite3ErrorMsg(pParse,
23128 "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
23129 return;
23130 }
23131 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
23132 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
23133 }else{
23134 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
23135 convertToWithoutRowidTable(pParse, p);
23136 }
23137 }
23138
23139 iDb = sqlite3SchemaToIndex(db, p->pSchema);
23140
23141 #ifndef SQLITE_OMIT_CHECK
23142 /* Resolve names in all CHECK constraint expressions.
23143 */
23144 if( p->pCheck ){
23145 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
23146 }
23147 #endif /* !defined(SQLITE_OMIT_CHECK) */
23148
23149 /* Estimate the average row size for the table and for all implied indices */
23150 estimateTableWidth(p);
23151 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
23152 estimateIndexWidth(pIdx);
23153 }
23154
23155 /* If not initializing, then create a record for the new table
23156 ** in the SQLITE_MASTER table of the database.
23157 **
23158 ** If this is a TEMPORARY table, write the entry into the auxiliary
23159 ** file instead of into the main database file.
23160 */
23161 if( !db->init.busy ){
23162 int n;
23163 Vdbe *v;
23164 char *zType; /* "view" or "table" */
23165 char *zType2; /* "VIEW" or "TABLE" */
23166 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
23167
23168 v = sqlite3GetVdbe(pParse);
23169 if( NEVER(v==0) ) return;
23170
23171 sqlite3VdbeAddOp1(v, OP_Close, 0);
23172
23173 /*
23174 ** Initialize zType for the new view or table.
23175 */
23176 if( p->pSelect==0 ){
23177 /* A regular table */
23178 zType = "table";
23179 zType2 = "TABLE";
23180 #ifndef SQLITE_OMIT_VIEW
23181 }else{
23182 /* A view */
23183 zType = "view";
23184 zType2 = "VIEW";
23185 #endif
23186 }
23187
23188 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
23189 ** statement to populate the new table. The root-page number for the
23190 ** new table is in register pParse->regRoot.
23191 **
23192 ** Once the SELECT has been coded by sqlite3Select(), it is in a
23193 ** suitable state to query for the column names and types to be used
23194 ** by the new table.
23195 **
23196 ** A shared-cache write-lock is not required to write to the new table,
23197 ** as a schema-lock must have already been obtained to create it. Since
23198 ** a schema-lock excludes all other database users, the write-lock would
23199 ** be redundant.
23200 */
23201 if( pSelect ){
23202 SelectDest dest; /* Where the SELECT should store results */
23203 int regYield; /* Register holding co-routine entry-point */
23204 int addrTop; /* Top of the co-routine */
23205 int regRec; /* A record to be insert into the new table */
23206 int regRowid; /* Rowid of the next row to insert */
23207 int addrInsLoop; /* Top of the loop for inserting rows */
23208 Table *pSelTab; /* A table that describes the SELECT results */
23209
23210 regYield = ++pParse->nMem;
23211 regRec = ++pParse->nMem;
23212 regRowid = ++pParse->nMem;
23213 assert(pParse->nTab==1);
23214 sqlite3MayAbort(pParse);
23215 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
23216 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
23217 pParse->nTab = 2;
23218 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
23219 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
23220 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
23221 sqlite3Select(pParse, pSelect, &dest);
23222 sqlite3VdbeEndCoroutine(v, regYield);
23223 sqlite3VdbeJumpHere(v, addrTop - 1);
23224 if( pParse->nErr ) return;
23225 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
23226 if( pSelTab==0 ) return;
23227 assert( p->aCol==0 );
23228 p->nCol = pSelTab->nCol;
23229 p->aCol = pSelTab->aCol;
23230 pSelTab->nCol = 0;
23231 pSelTab->aCol = 0;
23232 sqlite3DeleteTable(db, pSelTab);
23233 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
23234 VdbeCoverage(v);
23235 sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
23236 sqlite3TableAffinity(v, p, 0);
23237 sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
23238 sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
23239 sqlite3VdbeGoto(v, addrInsLoop);
23240 sqlite3VdbeJumpHere(v, addrInsLoop);
23241 sqlite3VdbeAddOp1(v, OP_Close, 1);
23242 }
23243
23244 /* Compute the complete text of the CREATE statement */
23245 if( pSelect ){
23246 zStmt = createTableStmt(db, p);
23247 }else{
23248 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
23249 n = (int)(pEnd2->z - pParse->sNameToken.z);
23250 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
23251 zStmt = sqlite3MPrintf(db,
23252 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
23253 );
23254 }
23255
23256 /* A slot for the record has already been allocated in the
23257 ** SQLITE_MASTER table. We just need to update that slot with all
23258 ** the information we've collected.
23259 */
23260 sqlite3NestedParse(pParse,
23261 "UPDATE %Q.%s "
23262 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
23263 "WHERE rowid=#%d",
23264 db->aDb[iDb].zDbSName, MASTER_NAME,
23265 zType,
23266 p->zName,
23267 p->zName,
23268 pParse->regRoot,
23269 zStmt,
23270 pParse->regRowid
23271 );
23272 sqlite3DbFree(db, zStmt);
23273 sqlite3ChangeCookie(pParse, iDb);
23274
23275 #ifndef SQLITE_OMIT_AUTOINCREMENT
23276 /* Check to see if we need to create an sqlite_sequence table for
23277 ** keeping track of autoincrement keys.
23278 */
23279 if( (p->tabFlags & TF_Autoincrement)!=0 ){
23280 Db *pDb = &db->aDb[iDb];
23281 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
23282 if( pDb->pSchema->pSeqTab==0 ){
23283 sqlite3NestedParse(pParse,
23284 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
23285 pDb->zDbSName
23286 );
23287 }
23288 }
23289 #endif
23290
23291 /* Reparse everything to update our internal data structures */
23292 sqlite3VdbeAddParseSchemaOp(v, iDb,
23293 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
23294 }
23295
23296
23297 /* Add the table to the in-memory representation of the database.
23298 */
23299 if( db->init.busy ){
23300 Table *pOld;
23301 Schema *pSchema = p->pSchema;
23302 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
23303 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
23304 if( pOld ){
23305 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
23306 sqlite3OomFault(db);
23307 return;
23308 }
23309 pParse->pNewTable = 0;
23310 db->flags |= SQLITE_InternChanges;
23311
23312 #ifndef SQLITE_OMIT_ALTERTABLE
23313 if( !p->pSelect ){
23314 const char *zName = (const char *)pParse->sNameToken.z;
23315 int nName;
23316 assert( !pSelect && pCons && pEnd );
23317 if( pCons->z==0 ){
23318 pCons = pEnd;
23319 }
23320 nName = (int)((const char *)pCons->z - zName);
23321 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
23322 }
23323 #endif
23324 }
23325 }
23326
23327 #ifndef SQLITE_OMIT_VIEW
23328 /*
23329 ** The parser calls this routine in order to create a new VIEW
23330 */
23331 SQLITE_PRIVATE void sqlite3CreateView(
23332 Parse *pParse, /* The parsing context */
23333 Token *pBegin, /* The CREATE token that begins the statement */
23334 Token *pName1, /* The token that holds the name of the view */
23335 Token *pName2, /* The token that holds the name of the view */
23336 ExprList *pCNames, /* Optional list of view column names */
23337 Select *pSelect, /* A SELECT statement that will become the new view */
23338 int isTemp, /* TRUE for a TEMPORARY view */
23339 int noErr /* Suppress error messages if VIEW already exists */
23340 ){
23341 Table *p;
23342 int n;
23343 const char *z;
23344 Token sEnd;
23345 DbFixer sFix;
23346 Token *pName = 0;
23347 int iDb;
23348 sqlite3 *db = pParse->db;
23349
23350 if( pParse->nVar>0 ){
23351 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
23352 goto create_view_fail;
23353 }
23354 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
23355 p = pParse->pNewTable;
23356 if( p==0 || pParse->nErr ) goto create_view_fail;
23357 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
23358 iDb = sqlite3SchemaToIndex(db, p->pSchema);
23359 sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
23360 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
23361
23362 /* Make a copy of the entire SELECT statement that defines the view.
23363 ** This will force all the Expr.token.z values to be dynamically
23364 ** allocated rather than point to the input string - which means that
23365 ** they will persist after the current sqlite3_exec() call returns.
23366 */
23367 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
23368 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
23369 if( db->mallocFailed ) goto create_view_fail;
23370
23371 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
23372 ** the end.
23373 */
23374 sEnd = pParse->sLastToken;
23375 assert( sEnd.z[0]!=0 );
23376 if( sEnd.z[0]!=';' ){
23377 sEnd.z += sEnd.n;
23378 }
23379 sEnd.n = 0;
23380 n = (int)(sEnd.z - pBegin->z);
23381 assert( n>0 );
23382 z = pBegin->z;
23383 while( sqlite3Isspace(z[n-1]) ){ n--; }
23384 sEnd.z = &z[n-1];
23385 sEnd.n = 1;
23386
23387 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
23388 sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
23389
23390 create_view_fail:
23391 sqlite3SelectDelete(db, pSelect);
23392 sqlite3ExprListDelete(db, pCNames);
23393 return;
23394 }
23395 #endif /* SQLITE_OMIT_VIEW */
23396
23397 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
23398 /*
23399 ** The Table structure pTable is really a VIEW. Fill in the names of
23400 ** the columns of the view in the pTable structure. Return the number
23401 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
23402 */
23403 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
23404 Table *pSelTab; /* A fake table from which we get the result set */
23405 Select *pSel; /* Copy of the SELECT that implements the view */
23406 int nErr = 0; /* Number of errors encountered */
23407 int n; /* Temporarily holds the number of cursors assigned */
23408 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
23409 #ifndef SQLITE_OMIT_AUTHORIZATION
23410 sqlite3_xauth xAuth; /* Saved xAuth pointer */
23411 #endif
23412
23413 assert( pTable );
23414
23415 #ifndef SQLITE_OMIT_VIRTUALTABLE
23416 if( sqlite3VtabCallConnect(pParse, pTable) ){
23417 return SQLITE_ERROR;
23418 }
23419 if( IsVirtual(pTable) ) return 0;
23420 #endif
23421
23422 #ifndef SQLITE_OMIT_VIEW
23423 /* A positive nCol means the columns names for this view are
23424 ** already known.
23425 */
23426 if( pTable->nCol>0 ) return 0;
23427
23428 /* A negative nCol is a special marker meaning that we are currently
23429 ** trying to compute the column names. If we enter this routine with
23430 ** a negative nCol, it means two or more views form a loop, like this:
23431 **
23432 ** CREATE VIEW one AS SELECT * FROM two;
23433 ** CREATE VIEW two AS SELECT * FROM one;
23434 **
23435 ** Actually, the error above is now caught prior to reaching this point.
23436 ** But the following test is still important as it does come up
23437 ** in the following:
23438 **
23439 ** CREATE TABLE main.ex1(a);
23440 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
23441 ** SELECT * FROM temp.ex1;
23442 */
23443 if( pTable->nCol<0 ){
23444 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
23445 return 1;
23446 }
23447 assert( pTable->nCol>=0 );
23448
23449 /* If we get this far, it means we need to compute the table names.
23450 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
23451 ** "*" elements in the results set of the view and will assign cursors
23452 ** to the elements of the FROM clause. But we do not want these changes
23453 ** to be permanent. So the computation is done on a copy of the SELECT
23454 ** statement that defines the view.
23455 */
23456 assert( pTable->pSelect );
23457 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
23458 if( pSel ){
23459 n = pParse->nTab;
23460 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
23461 pTable->nCol = -1;
23462 db->lookaside.bDisable++;
23463 #ifndef SQLITE_OMIT_AUTHORIZATION
23464 xAuth = db->xAuth;
23465 db->xAuth = 0;
23466 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
23467 db->xAuth = xAuth;
23468 #else
23469 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
23470 #endif
23471 pParse->nTab = n;
23472 if( pTable->pCheck ){
23473 /* CREATE VIEW name(arglist) AS ...
23474 ** The names of the columns in the table are taken from
23475 ** arglist which is stored in pTable->pCheck. The pCheck field
23476 ** normally holds CHECK constraints on an ordinary table, but for
23477 ** a VIEW it holds the list of column names.
23478 */
23479 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
23480 &pTable->nCol, &pTable->aCol);
23481 if( db->mallocFailed==0
23482 && pParse->nErr==0
23483 && pTable->nCol==pSel->pEList->nExpr
23484 ){
23485 sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel);
23486 }
23487 }else if( pSelTab ){
23488 /* CREATE VIEW name AS... without an argument list. Construct
23489 ** the column names from the SELECT statement that defines the view.
23490 */
23491 assert( pTable->aCol==0 );
23492 pTable->nCol = pSelTab->nCol;
23493 pTable->aCol = pSelTab->aCol;
23494 pSelTab->nCol = 0;
23495 pSelTab->aCol = 0;
23496 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
23497 }else{
23498 pTable->nCol = 0;
23499 nErr++;
23500 }
23501 sqlite3DeleteTable(db, pSelTab);
23502 sqlite3SelectDelete(db, pSel);
23503 db->lookaside.bDisable--;
23504 } else {
23505 nErr++;
23506 }
23507 pTable->pSchema->schemaFlags |= DB_UnresetViews;
23508 #endif /* SQLITE_OMIT_VIEW */
23509 return nErr;
23510 }
23511 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
23512
23513 #ifndef SQLITE_OMIT_VIEW
23514 /*
23515 ** Clear the column names from every VIEW in database idx.
23516 */
23517 static void sqliteViewResetAll(sqlite3 *db, int idx){
23518 HashElem *i;
23519 assert( sqlite3SchemaMutexHeld(db, idx, 0) );
23520 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
23521 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
23522 Table *pTab = sqliteHashData(i);
23523 if( pTab->pSelect ){
23524 sqlite3DeleteColumnNames(db, pTab);
23525 pTab->aCol = 0;
23526 pTab->nCol = 0;
23527 }
23528 }
23529 DbClearProperty(db, idx, DB_UnresetViews);
23530 }
23531 #else
23532 # define sqliteViewResetAll(A,B)
23533 #endif /* SQLITE_OMIT_VIEW */
23534
23535 /*
23536 ** This function is called by the VDBE to adjust the internal schema
23537 ** used by SQLite when the btree layer moves a table root page. The
23538 ** root-page of a table or index in database iDb has changed from iFrom
23539 ** to iTo.
23540 **
23541 ** Ticket #1728: The symbol table might still contain information
23542 ** on tables and/or indices that are the process of being deleted.
23543 ** If you are unlucky, one of those deleted indices or tables might
23544 ** have the same rootpage number as the real table or index that is
23545 ** being moved. So we cannot stop searching after the first match
23546 ** because the first match might be for one of the deleted indices
23547 ** or tables and not the table/index that is actually being moved.
23548 ** We must continue looping until all tables and indices with
23549 ** rootpage==iFrom have been converted to have a rootpage of iTo
23550 ** in order to be certain that we got the right one.
23551 */
23552 #ifndef SQLITE_OMIT_AUTOVACUUM
23553 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iT o){
23554 HashElem *pElem;
23555 Hash *pHash;
23556 Db *pDb;
23557
23558 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
23559 pDb = &db->aDb[iDb];
23560 pHash = &pDb->pSchema->tblHash;
23561 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
23562 Table *pTab = sqliteHashData(pElem);
23563 if( pTab->tnum==iFrom ){
23564 pTab->tnum = iTo;
23565 }
23566 }
23567 pHash = &pDb->pSchema->idxHash;
23568 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
23569 Index *pIdx = sqliteHashData(pElem);
23570 if( pIdx->tnum==iFrom ){
23571 pIdx->tnum = iTo;
23572 }
23573 }
23574 }
23575 #endif
23576
23577 /*
23578 ** Write code to erase the table with root-page iTable from database iDb.
23579 ** Also write code to modify the sqlite_master table and internal schema
23580 ** if a root-page of another table is moved by the btree-layer whilst
23581 ** erasing iTable (this can happen with an auto-vacuum database).
23582 */
23583 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
23584 Vdbe *v = sqlite3GetVdbe(pParse);
23585 int r1 = sqlite3GetTempReg(pParse);
23586 assert( iTable>1 );
23587 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
23588 sqlite3MayAbort(pParse);
23589 #ifndef SQLITE_OMIT_AUTOVACUUM
23590 /* OP_Destroy stores an in integer r1. If this integer
23591 ** is non-zero, then it is the root page number of a table moved to
23592 ** location iTable. The following code modifies the sqlite_master table to
23593 ** reflect this.
23594 **
23595 ** The "#NNN" in the SQL is a special constant that means whatever value
23596 ** is in register NNN. See grammar rules associated with the TK_REGISTER
23597 ** token for additional information.
23598 */
23599 sqlite3NestedParse(pParse,
23600 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
23601 pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1);
23602 #endif
23603 sqlite3ReleaseTempReg(pParse, r1);
23604 }
23605
23606 /*
23607 ** Write VDBE code to erase table pTab and all associated indices on disk.
23608 ** Code to update the sqlite_master tables and internal schema definitions
23609 ** in case a root-page belonging to another table is moved by the btree layer
23610 ** is also added (this can happen with an auto-vacuum database).
23611 */
23612 static void destroyTable(Parse *pParse, Table *pTab){
23613 #ifdef SQLITE_OMIT_AUTOVACUUM
23614 Index *pIdx;
23615 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
23616 destroyRootPage(pParse, pTab->tnum, iDb);
23617 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
23618 destroyRootPage(pParse, pIdx->tnum, iDb);
23619 }
23620 #else
23621 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
23622 ** is not defined), then it is important to call OP_Destroy on the
23623 ** table and index root-pages in order, starting with the numerically
23624 ** largest root-page number. This guarantees that none of the root-pages
23625 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
23626 ** following were coded:
23627 **
23628 ** OP_Destroy 4 0
23629 ** ...
23630 ** OP_Destroy 5 0
23631 **
23632 ** and root page 5 happened to be the largest root-page number in the
23633 ** database, then root page 5 would be moved to page 4 by the
23634 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
23635 ** a free-list page.
23636 */
23637 int iTab = pTab->tnum;
23638 int iDestroyed = 0;
23639
23640 while( 1 ){
23641 Index *pIdx;
23642 int iLargest = 0;
23643
23644 if( iDestroyed==0 || iTab<iDestroyed ){
23645 iLargest = iTab;
23646 }
23647 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
23648 int iIdx = pIdx->tnum;
23649 assert( pIdx->pSchema==pTab->pSchema );
23650 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
23651 iLargest = iIdx;
23652 }
23653 }
23654 if( iLargest==0 ){
23655 return;
23656 }else{
23657 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
23658 assert( iDb>=0 && iDb<pParse->db->nDb );
23659 destroyRootPage(pParse, iLargest, iDb);
23660 iDestroyed = iLargest;
23661 }
23662 }
23663 #endif
23664 }
23665
23666 /*
23667 ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
23668 ** after a DROP INDEX or DROP TABLE command.
23669 */
23670 static void sqlite3ClearStatTables(
23671 Parse *pParse, /* The parsing context */
23672 int iDb, /* The database number */
23673 const char *zType, /* "idx" or "tbl" */
23674 const char *zName /* Name of index or table */
23675 ){
23676 int i;
23677 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
23678 for(i=1; i<=4; i++){
23679 char zTab[24];
23680 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
23681 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
23682 sqlite3NestedParse(pParse,
23683 "DELETE FROM %Q.%s WHERE %s=%Q",
23684 zDbName, zTab, zType, zName
23685 );
23686 }
23687 }
23688 }
23689
23690 /*
23691 ** Generate code to drop a table.
23692 */
23693 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, in t isView){
23694 Vdbe *v;
23695 sqlite3 *db = pParse->db;
23696 Trigger *pTrigger;
23697 Db *pDb = &db->aDb[iDb];
23698
23699 v = sqlite3GetVdbe(pParse);
23700 assert( v!=0 );
23701 sqlite3BeginWriteOperation(pParse, 1, iDb);
23702
23703 #ifndef SQLITE_OMIT_VIRTUALTABLE
23704 if( IsVirtual(pTab) ){
23705 sqlite3VdbeAddOp0(v, OP_VBegin);
23706 }
23707 #endif
23708
23709 /* Drop all triggers associated with the table being dropped. Code
23710 ** is generated to remove entries from sqlite_master and/or
23711 ** sqlite_temp_master if required.
23712 */
23713 pTrigger = sqlite3TriggerList(pParse, pTab);
23714 while( pTrigger ){
23715 assert( pTrigger->pSchema==pTab->pSchema ||
23716 pTrigger->pSchema==db->aDb[1].pSchema );
23717 sqlite3DropTriggerPtr(pParse, pTrigger);
23718 pTrigger = pTrigger->pNext;
23719 }
23720
23721 #ifndef SQLITE_OMIT_AUTOINCREMENT
23722 /* Remove any entries of the sqlite_sequence table associated with
23723 ** the table being dropped. This is done before the table is dropped
23724 ** at the btree level, in case the sqlite_sequence table needs to
23725 ** move as a result of the drop (can happen in auto-vacuum mode).
23726 */
23727 if( pTab->tabFlags & TF_Autoincrement ){
23728 sqlite3NestedParse(pParse,
23729 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
23730 pDb->zDbSName, pTab->zName
23731 );
23732 }
23733 #endif
23734
23735 /* Drop all SQLITE_MASTER table and index entries that refer to the
23736 ** table. The program name loops through the master table and deletes
23737 ** every row that refers to a table of the same name as the one being
23738 ** dropped. Triggers are handled separately because a trigger can be
23739 ** created in the temp database that refers to a table in another
23740 ** database.
23741 */
23742 sqlite3NestedParse(pParse,
23743 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
23744 pDb->zDbSName, MASTER_NAME, pTab->zName);
23745 if( !isView && !IsVirtual(pTab) ){
23746 destroyTable(pParse, pTab);
23747 }
23748
23749 /* Remove the table entry from SQLite's internal schema and modify
23750 ** the schema cookie.
23751 */
23752 if( IsVirtual(pTab) ){
23753 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
23754 }
23755 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
23756 sqlite3ChangeCookie(pParse, iDb);
23757 sqliteViewResetAll(db, iDb);
23758 }
23759
23760 /*
23761 ** This routine is called to do the work of a DROP TABLE statement.
23762 ** pName is the name of the table to be dropped.
23763 */
23764 SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
23765 Table *pTab;
23766 Vdbe *v;
23767 sqlite3 *db = pParse->db;
23768 int iDb;
23769
23770 if( db->mallocFailed ){
23771 goto exit_drop_table;
23772 }
23773 assert( pParse->nErr==0 );
23774 assert( pName->nSrc==1 );
23775 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
23776 if( noErr ) db->suppressErr++;
23777 assert( isView==0 || isView==LOCATE_VIEW );
23778 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
23779 if( noErr ) db->suppressErr--;
23780
23781 if( pTab==0 ){
23782 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
23783 goto exit_drop_table;
23784 }
23785 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
23786 assert( iDb>=0 && iDb<db->nDb );
23787
23788 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
23789 ** it is initialized.
23790 */
23791 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
23792 goto exit_drop_table;
23793 }
23794 #ifndef SQLITE_OMIT_AUTHORIZATION
23795 {
23796 int code;
23797 const char *zTab = SCHEMA_TABLE(iDb);
23798 const char *zDb = db->aDb[iDb].zDbSName;
23799 const char *zArg2 = 0;
23800 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
23801 goto exit_drop_table;
23802 }
23803 if( isView ){
23804 if( !OMIT_TEMPDB && iDb==1 ){
23805 code = SQLITE_DROP_TEMP_VIEW;
23806 }else{
23807 code = SQLITE_DROP_VIEW;
23808 }
23809 #ifndef SQLITE_OMIT_VIRTUALTABLE
23810 }else if( IsVirtual(pTab) ){
23811 code = SQLITE_DROP_VTABLE;
23812 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
23813 #endif
23814 }else{
23815 if( !OMIT_TEMPDB && iDb==1 ){
23816 code = SQLITE_DROP_TEMP_TABLE;
23817 }else{
23818 code = SQLITE_DROP_TABLE;
23819 }
23820 }
23821 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
23822 goto exit_drop_table;
23823 }
23824 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
23825 goto exit_drop_table;
23826 }
23827 }
23828 #endif
23829 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
23830 && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
23831 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
23832 goto exit_drop_table;
23833 }
23834
23835 #ifndef SQLITE_OMIT_VIEW
23836 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
23837 ** on a table.
23838 */
23839 if( isView && pTab->pSelect==0 ){
23840 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
23841 goto exit_drop_table;
23842 }
23843 if( !isView && pTab->pSelect ){
23844 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
23845 goto exit_drop_table;
23846 }
23847 #endif
23848
23849 /* Generate code to remove the table from the master table
23850 ** on disk.
23851 */
23852 v = sqlite3GetVdbe(pParse);
23853 if( v ){
23854 sqlite3BeginWriteOperation(pParse, 1, iDb);
23855 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
23856 sqlite3FkDropTable(pParse, pName, pTab);
23857 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
23858 }
23859
23860 exit_drop_table:
23861 sqlite3SrcListDelete(db, pName);
23862 }
23863
23864 /*
23865 ** This routine is called to create a new foreign key on the table
23866 ** currently under construction. pFromCol determines which columns
23867 ** in the current table point to the foreign key. If pFromCol==0 then
23868 ** connect the key to the last column inserted. pTo is the name of
23869 ** the table referred to (a.k.a the "parent" table). pToCol is a list
23870 ** of tables in the parent pTo table. flags contains all
23871 ** information about the conflict resolution algorithms specified
23872 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
23873 **
23874 ** An FKey structure is created and added to the table currently
23875 ** under construction in the pParse->pNewTable field.
23876 **
23877 ** The foreign key is set for IMMEDIATE processing. A subsequent call
23878 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
23879 */
23880 SQLITE_PRIVATE void sqlite3CreateForeignKey(
23881 Parse *pParse, /* Parsing context */
23882 ExprList *pFromCol, /* Columns in this table that point to other table */
23883 Token *pTo, /* Name of the other table */
23884 ExprList *pToCol, /* Columns in the other table */
23885 int flags /* Conflict resolution algorithms. */
23886 ){
23887 sqlite3 *db = pParse->db;
23888 #ifndef SQLITE_OMIT_FOREIGN_KEY
23889 FKey *pFKey = 0;
23890 FKey *pNextTo;
23891 Table *p = pParse->pNewTable;
23892 int nByte;
23893 int i;
23894 int nCol;
23895 char *z;
23896
23897 assert( pTo!=0 );
23898 if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
23899 if( pFromCol==0 ){
23900 int iCol = p->nCol-1;
23901 if( NEVER(iCol<0) ) goto fk_end;
23902 if( pToCol && pToCol->nExpr!=1 ){
23903 sqlite3ErrorMsg(pParse, "foreign key on %s"
23904 " should reference only one column of table %T",
23905 p->aCol[iCol].zName, pTo);
23906 goto fk_end;
23907 }
23908 nCol = 1;
23909 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
23910 sqlite3ErrorMsg(pParse,
23911 "number of columns in foreign key does not match the number of "
23912 "columns in the referenced table");
23913 goto fk_end;
23914 }else{
23915 nCol = pFromCol->nExpr;
23916 }
23917 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
23918 if( pToCol ){
23919 for(i=0; i<pToCol->nExpr; i++){
23920 nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
23921 }
23922 }
23923 pFKey = sqlite3DbMallocZero(db, nByte );
23924 if( pFKey==0 ){
23925 goto fk_end;
23926 }
23927 pFKey->pFrom = p;
23928 pFKey->pNextFrom = p->pFKey;
23929 z = (char*)&pFKey->aCol[nCol];
23930 pFKey->zTo = z;
23931 memcpy(z, pTo->z, pTo->n);
23932 z[pTo->n] = 0;
23933 sqlite3Dequote(z);
23934 z += pTo->n+1;
23935 pFKey->nCol = nCol;
23936 if( pFromCol==0 ){
23937 pFKey->aCol[0].iFrom = p->nCol-1;
23938 }else{
23939 for(i=0; i<nCol; i++){
23940 int j;
23941 for(j=0; j<p->nCol; j++){
23942 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
23943 pFKey->aCol[i].iFrom = j;
23944 break;
23945 }
23946 }
23947 if( j>=p->nCol ){
23948 sqlite3ErrorMsg(pParse,
23949 "unknown column \"%s\" in foreign key definition",
23950 pFromCol->a[i].zName);
23951 goto fk_end;
23952 }
23953 }
23954 }
23955 if( pToCol ){
23956 for(i=0; i<nCol; i++){
23957 int n = sqlite3Strlen30(pToCol->a[i].zName);
23958 pFKey->aCol[i].zCol = z;
23959 memcpy(z, pToCol->a[i].zName, n);
23960 z[n] = 0;
23961 z += n+1;
23962 }
23963 }
23964 pFKey->isDeferred = 0;
23965 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
23966 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
23967
23968 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
23969 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
23970 pFKey->zTo, (void *)pFKey
23971 );
23972 if( pNextTo==pFKey ){
23973 sqlite3OomFault(db);
23974 goto fk_end;
23975 }
23976 if( pNextTo ){
23977 assert( pNextTo->pPrevTo==0 );
23978 pFKey->pNextTo = pNextTo;
23979 pNextTo->pPrevTo = pFKey;
23980 }
23981
23982 /* Link the foreign key to the table as the last step.
23983 */
23984 p->pFKey = pFKey;
23985 pFKey = 0;
23986
23987 fk_end:
23988 sqlite3DbFree(db, pFKey);
23989 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
23990 sqlite3ExprListDelete(db, pFromCol);
23991 sqlite3ExprListDelete(db, pToCol);
23992 }
23993
23994 /*
23995 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
23996 ** clause is seen as part of a foreign key definition. The isDeferred
23997 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
23998 ** The behavior of the most recently created foreign key is adjusted
23999 ** accordingly.
24000 */
24001 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
24002 #ifndef SQLITE_OMIT_FOREIGN_KEY
24003 Table *pTab;
24004 FKey *pFKey;
24005 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
24006 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
24007 pFKey->isDeferred = (u8)isDeferred;
24008 #endif
24009 }
24010
24011 /*
24012 ** Generate code that will erase and refill index *pIdx. This is
24013 ** used to initialize a newly created index or to recompute the
24014 ** content of an index in response to a REINDEX command.
24015 **
24016 ** if memRootPage is not negative, it means that the index is newly
24017 ** created. The register specified by memRootPage contains the
24018 ** root page number of the index. If memRootPage is negative, then
24019 ** the index already exists and must be cleared before being refilled and
24020 ** the root page number of the index is taken from pIndex->tnum.
24021 */
24022 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
24023 Table *pTab = pIndex->pTable; /* The table that is indexed */
24024 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
24025 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
24026 int iSorter; /* Cursor opened by OpenSorter (if in use) */
24027 int addr1; /* Address of top of loop */
24028 int addr2; /* Address to jump to for next iteration */
24029 int tnum; /* Root page of index */
24030 int iPartIdxLabel; /* Jump to this label to skip a row */
24031 Vdbe *v; /* Generate code into this virtual machine */
24032 KeyInfo *pKey; /* KeyInfo for index */
24033 int regRecord; /* Register holding assembled index record */
24034 sqlite3 *db = pParse->db; /* The database connection */
24035 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
24036
24037 #ifndef SQLITE_OMIT_AUTHORIZATION
24038 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
24039 db->aDb[iDb].zDbSName ) ){
24040 return;
24041 }
24042 #endif
24043
24044 /* Require a write-lock on the table to perform this operation */
24045 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
24046
24047 v = sqlite3GetVdbe(pParse);
24048 if( v==0 ) return;
24049 if( memRootPage>=0 ){
24050 tnum = memRootPage;
24051 }else{
24052 tnum = pIndex->tnum;
24053 }
24054 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
24055 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
24056
24057 /* Open the sorter cursor if we are to use one. */
24058 iSorter = pParse->nTab++;
24059 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
24060 sqlite3KeyInfoRef(pKey), P4_KEYINFO);
24061
24062 /* Open the table. Loop through all rows of the table, inserting index
24063 ** records into the sorter. */
24064 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
24065 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
24066 regRecord = sqlite3GetTempReg(pParse);
24067
24068 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
24069 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
24070 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
24071 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
24072 sqlite3VdbeJumpHere(v, addr1);
24073 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
24074 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
24075 (char *)pKey, P4_KEYINFO);
24076 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
24077
24078 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
24079 if( IsUniqueIndex(pIndex) ){
24080 int j2 = sqlite3VdbeCurrentAddr(v) + 3;
24081 sqlite3VdbeGoto(v, j2);
24082 addr2 = sqlite3VdbeCurrentAddr(v);
24083 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
24084 pIndex->nKeyCol); VdbeCoverage(v);
24085 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
24086 }else{
24087 addr2 = sqlite3VdbeCurrentAddr(v);
24088 }
24089 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
24090 sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1);
24091 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
24092 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
24093 sqlite3ReleaseTempReg(pParse, regRecord);
24094 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
24095 sqlite3VdbeJumpHere(v, addr1);
24096
24097 sqlite3VdbeAddOp1(v, OP_Close, iTab);
24098 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
24099 sqlite3VdbeAddOp1(v, OP_Close, iSorter);
24100 }
24101
24102 /*
24103 ** Allocate heap space to hold an Index object with nCol columns.
24104 **
24105 ** Increase the allocation size to provide an extra nExtra bytes
24106 ** of 8-byte aligned space after the Index object and return a
24107 ** pointer to this extra space in *ppExtra.
24108 */
24109 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
24110 sqlite3 *db, /* Database connection */
24111 i16 nCol, /* Total number of columns in the index */
24112 int nExtra, /* Number of bytes of extra space to alloc */
24113 char **ppExtra /* Pointer to the "extra" space */
24114 ){
24115 Index *p; /* Allocated index object */
24116 int nByte; /* Bytes of space for Index object + arrays */
24117
24118 nByte = ROUND8(sizeof(Index)) + /* Index structure */
24119 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
24120 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
24121 sizeof(i16)*nCol + /* Index.aiColumn */
24122 sizeof(u8)*nCol); /* Index.aSortOrder */
24123 p = sqlite3DbMallocZero(db, nByte + nExtra);
24124 if( p ){
24125 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
24126 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
24127 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
24128 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
24129 p->aSortOrder = (u8*)pExtra;
24130 p->nColumn = nCol;
24131 p->nKeyCol = nCol - 1;
24132 *ppExtra = ((char*)p) + nByte;
24133 }
24134 return p;
24135 }
24136
24137 /*
24138 ** Create a new index for an SQL table. pName1.pName2 is the name of the index
24139 ** and pTblList is the name of the table that is to be indexed. Both will
24140 ** be NULL for a primary key or an index that is created to satisfy a
24141 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
24142 ** as the table to be indexed. pParse->pNewTable is a table that is
24143 ** currently being constructed by a CREATE TABLE statement.
24144 **
24145 ** pList is a list of columns to be indexed. pList will be NULL if this
24146 ** is a primary key or unique-constraint on the most recent column added
24147 ** to the table currently under construction.
24148 */
24149 SQLITE_PRIVATE void sqlite3CreateIndex(
24150 Parse *pParse, /* All information about this parse */
24151 Token *pName1, /* First part of index name. May be NULL */
24152 Token *pName2, /* Second part of index name. May be NULL */
24153 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
24154 ExprList *pList, /* A list of columns to be indexed */
24155 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
24156 Token *pStart, /* The CREATE token that begins this statement */
24157 Expr *pPIWhere, /* WHERE clause for partial indices */
24158 int sortOrder, /* Sort order of primary key when pList==NULL */
24159 int ifNotExist, /* Omit error if index already exists */
24160 u8 idxType /* The index type */
24161 ){
24162 Table *pTab = 0; /* Table to be indexed */
24163 Index *pIndex = 0; /* The index to be created */
24164 char *zName = 0; /* Name of the index */
24165 int nName; /* Number of characters in zName */
24166 int i, j;
24167 DbFixer sFix; /* For assigning database names to pTable */
24168 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
24169 sqlite3 *db = pParse->db;
24170 Db *pDb; /* The specific table containing the indexed database */
24171 int iDb; /* Index of the database that is being written */
24172 Token *pName = 0; /* Unqualified name of the index to create */
24173 struct ExprList_item *pListItem; /* For looping over pList */
24174 int nExtra = 0; /* Space allocated for zExtra[] */
24175 int nExtraCol; /* Number of extra columns needed */
24176 char *zExtra = 0; /* Extra space after the Index object */
24177 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
24178
24179 if( db->mallocFailed || pParse->nErr>0 ){
24180 goto exit_create_index;
24181 }
24182 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
24183 goto exit_create_index;
24184 }
24185 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
24186 goto exit_create_index;
24187 }
24188
24189 /*
24190 ** Find the table that is to be indexed. Return early if not found.
24191 */
24192 if( pTblName!=0 ){
24193
24194 /* Use the two-part index name to determine the database
24195 ** to search for the table. 'Fix' the table name to this db
24196 ** before looking up the table.
24197 */
24198 assert( pName1 && pName2 );
24199 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
24200 if( iDb<0 ) goto exit_create_index;
24201 assert( pName && pName->z );
24202
24203 #ifndef SQLITE_OMIT_TEMPDB
24204 /* If the index name was unqualified, check if the table
24205 ** is a temp table. If so, set the database to 1. Do not do this
24206 ** if initialising a database schema.
24207 */
24208 if( !db->init.busy ){
24209 pTab = sqlite3SrcListLookup(pParse, pTblName);
24210 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
24211 iDb = 1;
24212 }
24213 }
24214 #endif
24215
24216 sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
24217 if( sqlite3FixSrcList(&sFix, pTblName) ){
24218 /* Because the parser constructs pTblName from a single identifier,
24219 ** sqlite3FixSrcList can never fail. */
24220 assert(0);
24221 }
24222 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
24223 assert( db->mallocFailed==0 || pTab==0 );
24224 if( pTab==0 ) goto exit_create_index;
24225 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
24226 sqlite3ErrorMsg(pParse,
24227 "cannot create a TEMP index on non-TEMP table \"%s\"",
24228 pTab->zName);
24229 goto exit_create_index;
24230 }
24231 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
24232 }else{
24233 assert( pName==0 );
24234 assert( pStart==0 );
24235 pTab = pParse->pNewTable;
24236 if( !pTab ) goto exit_create_index;
24237 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
24238 }
24239 pDb = &db->aDb[iDb];
24240
24241 assert( pTab!=0 );
24242 assert( pParse->nErr==0 );
24243 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
24244 && db->init.busy==0
24245 #if SQLITE_USER_AUTHENTICATION
24246 && sqlite3UserAuthTable(pTab->zName)==0
24247 #endif
24248 && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
24249 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
24250 goto exit_create_index;
24251 }
24252 #ifndef SQLITE_OMIT_VIEW
24253 if( pTab->pSelect ){
24254 sqlite3ErrorMsg(pParse, "views may not be indexed");
24255 goto exit_create_index;
24256 }
24257 #endif
24258 #ifndef SQLITE_OMIT_VIRTUALTABLE
24259 if( IsVirtual(pTab) ){
24260 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
24261 goto exit_create_index;
24262 }
24263 #endif
24264
24265 /*
24266 ** Find the name of the index. Make sure there is not already another
24267 ** index or table with the same name.
24268 **
24269 ** Exception: If we are reading the names of permanent indices from the
24270 ** sqlite_master table (because some other process changed the schema) and
24271 ** one of the index names collides with the name of a temporary table or
24272 ** index, then we will continue to process this index.
24273 **
24274 ** If pName==0 it means that we are
24275 ** dealing with a primary key or UNIQUE constraint. We have to invent our
24276 ** own name.
24277 */
24278 if( pName ){
24279 zName = sqlite3NameFromToken(db, pName);
24280 if( zName==0 ) goto exit_create_index;
24281 assert( pName->z!=0 );
24282 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
24283 goto exit_create_index;
24284 }
24285 if( !db->init.busy ){
24286 if( sqlite3FindTable(db, zName, 0)!=0 ){
24287 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
24288 goto exit_create_index;
24289 }
24290 }
24291 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
24292 if( !ifNotExist ){
24293 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
24294 }else{
24295 assert( !db->init.busy );
24296 sqlite3CodeVerifySchema(pParse, iDb);
24297 }
24298 goto exit_create_index;
24299 }
24300 }else{
24301 int n;
24302 Index *pLoop;
24303 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
24304 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
24305 if( zName==0 ){
24306 goto exit_create_index;
24307 }
24308
24309 /* Automatic index names generated from within sqlite3_declare_vtab()
24310 ** must have names that are distinct from normal automatic index names.
24311 ** The following statement converts "sqlite3_autoindex..." into
24312 ** "sqlite3_butoindex..." in order to make the names distinct.
24313 ** The "vtab_err.test" test demonstrates the need of this statement. */
24314 if( IN_DECLARE_VTAB ) zName[7]++;
24315 }
24316
24317 /* Check for authorization to create an index.
24318 */
24319 #ifndef SQLITE_OMIT_AUTHORIZATION
24320 {
24321 const char *zDb = pDb->zDbSName;
24322 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
24323 goto exit_create_index;
24324 }
24325 i = SQLITE_CREATE_INDEX;
24326 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
24327 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
24328 goto exit_create_index;
24329 }
24330 }
24331 #endif
24332
24333 /* If pList==0, it means this routine was called to make a primary
24334 ** key out of the last column added to the table under construction.
24335 ** So create a fake list to simulate this.
24336 */
24337 if( pList==0 ){
24338 Token prevCol;
24339 sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
24340 pList = sqlite3ExprListAppend(pParse, 0,
24341 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
24342 if( pList==0 ) goto exit_create_index;
24343 assert( pList->nExpr==1 );
24344 sqlite3ExprListSetSortOrder(pList, sortOrder);
24345 }else{
24346 sqlite3ExprListCheckLength(pParse, pList, "index");
24347 }
24348
24349 /* Figure out how many bytes of space are required to store explicitly
24350 ** specified collation sequence names.
24351 */
24352 for(i=0; i<pList->nExpr; i++){
24353 Expr *pExpr = pList->a[i].pExpr;
24354 assert( pExpr!=0 );
24355 if( pExpr->op==TK_COLLATE ){
24356 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
24357 }
24358 }
24359
24360 /*
24361 ** Allocate the index structure.
24362 */
24363 nName = sqlite3Strlen30(zName);
24364 nExtraCol = pPk ? pPk->nKeyCol : 1;
24365 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
24366 nName + nExtra + 1, &zExtra);
24367 if( db->mallocFailed ){
24368 goto exit_create_index;
24369 }
24370 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
24371 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
24372 pIndex->zName = zExtra;
24373 zExtra += nName + 1;
24374 memcpy(pIndex->zName, zName, nName+1);
24375 pIndex->pTable = pTab;
24376 pIndex->onError = (u8)onError;
24377 pIndex->uniqNotNull = onError!=OE_None;
24378 pIndex->idxType = idxType;
24379 pIndex->pSchema = db->aDb[iDb].pSchema;
24380 pIndex->nKeyCol = pList->nExpr;
24381 if( pPIWhere ){
24382 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
24383 pIndex->pPartIdxWhere = pPIWhere;
24384 pPIWhere = 0;
24385 }
24386 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
24387
24388 /* Check to see if we should honor DESC requests on index columns
24389 */
24390 if( pDb->pSchema->file_format>=4 ){
24391 sortOrderMask = -1; /* Honor DESC */
24392 }else{
24393 sortOrderMask = 0; /* Ignore DESC */
24394 }
24395
24396 /* Analyze the list of expressions that form the terms of the index and
24397 ** report any errors. In the common case where the expression is exactly
24398 ** a table column, store that column in aiColumn[]. For general expressions,
24399 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
24400 **
24401 ** TODO: Issue a warning if two or more columns of the index are identical.
24402 ** TODO: Issue a warning if the table primary key is used as part of the
24403 ** index key.
24404 */
24405 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
24406 Expr *pCExpr; /* The i-th index expression */
24407 int requestedSortOrder; /* ASC or DESC on the i-th expression */
24408 const char *zColl; /* Collation sequence name */
24409
24410 sqlite3StringToId(pListItem->pExpr);
24411 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
24412 if( pParse->nErr ) goto exit_create_index;
24413 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
24414 if( pCExpr->op!=TK_COLUMN ){
24415 if( pTab==pParse->pNewTable ){
24416 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
24417 "UNIQUE constraints");
24418 goto exit_create_index;
24419 }
24420 if( pIndex->aColExpr==0 ){
24421 ExprList *pCopy = sqlite3ExprListDup(db, pList, 0);
24422 pIndex->aColExpr = pCopy;
24423 if( !db->mallocFailed ){
24424 assert( pCopy!=0 );
24425 pListItem = &pCopy->a[i];
24426 }
24427 }
24428 j = XN_EXPR;
24429 pIndex->aiColumn[i] = XN_EXPR;
24430 pIndex->uniqNotNull = 0;
24431 }else{
24432 j = pCExpr->iColumn;
24433 assert( j<=0x7fff );
24434 if( j<0 ){
24435 j = pTab->iPKey;
24436 }else if( pTab->aCol[j].notNull==0 ){
24437 pIndex->uniqNotNull = 0;
24438 }
24439 pIndex->aiColumn[i] = (i16)j;
24440 }
24441 zColl = 0;
24442 if( pListItem->pExpr->op==TK_COLLATE ){
24443 int nColl;
24444 zColl = pListItem->pExpr->u.zToken;
24445 nColl = sqlite3Strlen30(zColl) + 1;
24446 assert( nExtra>=nColl );
24447 memcpy(zExtra, zColl, nColl);
24448 zColl = zExtra;
24449 zExtra += nColl;
24450 nExtra -= nColl;
24451 }else if( j>=0 ){
24452 zColl = pTab->aCol[j].zColl;
24453 }
24454 if( !zColl ) zColl = sqlite3StrBINARY;
24455 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
24456 goto exit_create_index;
24457 }
24458 pIndex->azColl[i] = zColl;
24459 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
24460 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
24461 }
24462
24463 /* Append the table key to the end of the index. For WITHOUT ROWID
24464 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
24465 ** normal tables (when pPk==0) this will be the rowid.
24466 */
24467 if( pPk ){
24468 for(j=0; j<pPk->nKeyCol; j++){
24469 int x = pPk->aiColumn[j];
24470 assert( x>=0 );
24471 if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
24472 pIndex->nColumn--;
24473 }else{
24474 pIndex->aiColumn[i] = x;
24475 pIndex->azColl[i] = pPk->azColl[j];
24476 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
24477 i++;
24478 }
24479 }
24480 assert( i==pIndex->nColumn );
24481 }else{
24482 pIndex->aiColumn[i] = XN_ROWID;
24483 pIndex->azColl[i] = sqlite3StrBINARY;
24484 }
24485 sqlite3DefaultRowEst(pIndex);
24486 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
24487
24488 /* If this index contains every column of its table, then mark
24489 ** it as a covering index */
24490 assert( HasRowid(pTab)
24491 || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 );
24492 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
24493 pIndex->isCovering = 1;
24494 for(j=0; j<pTab->nCol; j++){
24495 if( j==pTab->iPKey ) continue;
24496 if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue;
24497 pIndex->isCovering = 0;
24498 break;
24499 }
24500 }
24501
24502 if( pTab==pParse->pNewTable ){
24503 /* This routine has been called to create an automatic index as a
24504 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
24505 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
24506 ** i.e. one of:
24507 **
24508 ** CREATE TABLE t(x PRIMARY KEY, y);
24509 ** CREATE TABLE t(x, y, UNIQUE(x, y));
24510 **
24511 ** Either way, check to see if the table already has such an index. If
24512 ** so, don't bother creating this one. This only applies to
24513 ** automatically created indices. Users can do as they wish with
24514 ** explicit indices.
24515 **
24516 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
24517 ** (and thus suppressing the second one) even if they have different
24518 ** sort orders.
24519 **
24520 ** If there are different collating sequences or if the columns of
24521 ** the constraint occur in different orders, then the constraints are
24522 ** considered distinct and both result in separate indices.
24523 */
24524 Index *pIdx;
24525 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
24526 int k;
24527 assert( IsUniqueIndex(pIdx) );
24528 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
24529 assert( IsUniqueIndex(pIndex) );
24530
24531 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
24532 for(k=0; k<pIdx->nKeyCol; k++){
24533 const char *z1;
24534 const char *z2;
24535 assert( pIdx->aiColumn[k]>=0 );
24536 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
24537 z1 = pIdx->azColl[k];
24538 z2 = pIndex->azColl[k];
24539 if( sqlite3StrICmp(z1, z2) ) break;
24540 }
24541 if( k==pIdx->nKeyCol ){
24542 if( pIdx->onError!=pIndex->onError ){
24543 /* This constraint creates the same index as a previous
24544 ** constraint specified somewhere in the CREATE TABLE statement.
24545 ** However the ON CONFLICT clauses are different. If both this
24546 ** constraint and the previous equivalent constraint have explicit
24547 ** ON CONFLICT clauses this is an error. Otherwise, use the
24548 ** explicitly specified behavior for the index.
24549 */
24550 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
24551 sqlite3ErrorMsg(pParse,
24552 "conflicting ON CONFLICT clauses specified", 0);
24553 }
24554 if( pIdx->onError==OE_Default ){
24555 pIdx->onError = pIndex->onError;
24556 }
24557 }
24558 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
24559 goto exit_create_index;
24560 }
24561 }
24562 }
24563
24564 /* Link the new Index structure to its table and to the other
24565 ** in-memory database structures.
24566 */
24567 assert( pParse->nErr==0 );
24568 if( db->init.busy ){
24569 Index *p;
24570 assert( !IN_DECLARE_VTAB );
24571 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
24572 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
24573 pIndex->zName, pIndex);
24574 if( p ){
24575 assert( p==pIndex ); /* Malloc must have failed */
24576 sqlite3OomFault(db);
24577 goto exit_create_index;
24578 }
24579 db->flags |= SQLITE_InternChanges;
24580 if( pTblName!=0 ){
24581 pIndex->tnum = db->init.newTnum;
24582 }
24583 }
24584
24585 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
24586 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
24587 ** emit code to allocate the index rootpage on disk and make an entry for
24588 ** the index in the sqlite_master table and populate the index with
24589 ** content. But, do not do this if we are simply reading the sqlite_master
24590 ** table to parse the schema, or if this index is the PRIMARY KEY index
24591 ** of a WITHOUT ROWID table.
24592 **
24593 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
24594 ** or UNIQUE index in a CREATE TABLE statement. Since the table
24595 ** has just been created, it contains no data and the index initialization
24596 ** step can be skipped.
24597 */
24598 else if( HasRowid(pTab) || pTblName!=0 ){
24599 Vdbe *v;
24600 char *zStmt;
24601 int iMem = ++pParse->nMem;
24602
24603 v = sqlite3GetVdbe(pParse);
24604 if( v==0 ) goto exit_create_index;
24605
24606 sqlite3BeginWriteOperation(pParse, 1, iDb);
24607
24608 /* Create the rootpage for the index using CreateIndex. But before
24609 ** doing so, code a Noop instruction and store its address in
24610 ** Index.tnum. This is required in case this index is actually a
24611 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
24612 ** that case the convertToWithoutRowidTable() routine will replace
24613 ** the Noop with a Goto to jump over the VDBE code generated below. */
24614 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
24615 sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
24616
24617 /* Gather the complete text of the CREATE INDEX statement into
24618 ** the zStmt variable
24619 */
24620 if( pStart ){
24621 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
24622 if( pName->z[n-1]==';' ) n--;
24623 /* A named index with an explicit CREATE INDEX statement */
24624 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
24625 onError==OE_None ? "" : " UNIQUE", n, pName->z);
24626 }else{
24627 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
24628 /* zStmt = sqlite3MPrintf(""); */
24629 zStmt = 0;
24630 }
24631
24632 /* Add an entry in sqlite_master for this index
24633 */
24634 sqlite3NestedParse(pParse,
24635 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
24636 db->aDb[iDb].zDbSName, MASTER_NAME,
24637 pIndex->zName,
24638 pTab->zName,
24639 iMem,
24640 zStmt
24641 );
24642 sqlite3DbFree(db, zStmt);
24643
24644 /* Fill the index with data and reparse the schema. Code an OP_Expire
24645 ** to invalidate all pre-compiled statements.
24646 */
24647 if( pTblName ){
24648 sqlite3RefillIndex(pParse, pIndex, iMem);
24649 sqlite3ChangeCookie(pParse, iDb);
24650 sqlite3VdbeAddParseSchemaOp(v, iDb,
24651 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
24652 sqlite3VdbeAddOp0(v, OP_Expire);
24653 }
24654
24655 sqlite3VdbeJumpHere(v, pIndex->tnum);
24656 }
24657
24658 /* When adding an index to the list of indices for a table, make
24659 ** sure all indices labeled OE_Replace come after all those labeled
24660 ** OE_Ignore. This is necessary for the correct constraint check
24661 ** processing (in sqlite3GenerateConstraintChecks()) as part of
24662 ** UPDATE and INSERT statements.
24663 */
24664 if( db->init.busy || pTblName==0 ){
24665 if( onError!=OE_Replace || pTab->pIndex==0
24666 || pTab->pIndex->onError==OE_Replace){
24667 pIndex->pNext = pTab->pIndex;
24668 pTab->pIndex = pIndex;
24669 }else{
24670 Index *pOther = pTab->pIndex;
24671 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
24672 pOther = pOther->pNext;
24673 }
24674 pIndex->pNext = pOther->pNext;
24675 pOther->pNext = pIndex;
24676 }
24677 pIndex = 0;
24678 }
24679
24680 /* Clean up before exiting */
24681 exit_create_index:
24682 if( pIndex ) freeIndex(db, pIndex);
24683 sqlite3ExprDelete(db, pPIWhere);
24684 sqlite3ExprListDelete(db, pList);
24685 sqlite3SrcListDelete(db, pTblName);
24686 sqlite3DbFree(db, zName);
24687 }
24688
24689 /*
24690 ** Fill the Index.aiRowEst[] array with default information - information
24691 ** to be used when we have not run the ANALYZE command.
24692 **
24693 ** aiRowEst[0] is supposed to contain the number of elements in the index.
24694 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
24695 ** number of rows in the table that match any particular value of the
24696 ** first column of the index. aiRowEst[2] is an estimate of the number
24697 ** of rows that match any particular combination of the first 2 columns
24698 ** of the index. And so forth. It must always be the case that
24699 *
24700 ** aiRowEst[N]<=aiRowEst[N-1]
24701 ** aiRowEst[N]>=1
24702 **
24703 ** Apart from that, we have little to go on besides intuition as to
24704 ** how aiRowEst[] should be initialized. The numbers generated here
24705 ** are based on typical values found in actual indices.
24706 */
24707 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
24708 /* 10, 9, 8, 7, 6 */
24709 LogEst aVal[] = { 33, 32, 30, 28, 26 };
24710 LogEst *a = pIdx->aiRowLogEst;
24711 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
24712 int i;
24713
24714 /* Set the first entry (number of rows in the index) to the estimated
24715 ** number of rows in the table, or half the number of rows in the table
24716 ** for a partial index. But do not let the estimate drop below 10. */
24717 a[0] = pIdx->pTable->nRowLogEst;
24718 if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) );
24719 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) );
24720
24721 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
24722 ** 6 and each subsequent value (if any) is 5. */
24723 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
24724 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
24725 a[i] = 23; assert( 23==sqlite3LogEst(5) );
24726 }
24727
24728 assert( 0==sqlite3LogEst(1) );
24729 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
24730 }
24731
24732 /*
24733 ** This routine will drop an existing named index. This routine
24734 ** implements the DROP INDEX statement.
24735 */
24736 SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists ){
24737 Index *pIndex;
24738 Vdbe *v;
24739 sqlite3 *db = pParse->db;
24740 int iDb;
24741
24742 assert( pParse->nErr==0 ); /* Never called with prior errors */
24743 if( db->mallocFailed ){
24744 goto exit_drop_index;
24745 }
24746 assert( pName->nSrc==1 );
24747 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
24748 goto exit_drop_index;
24749 }
24750 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
24751 if( pIndex==0 ){
24752 if( !ifExists ){
24753 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
24754 }else{
24755 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
24756 }
24757 pParse->checkSchema = 1;
24758 goto exit_drop_index;
24759 }
24760 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
24761 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
24762 "or PRIMARY KEY constraint cannot be dropped", 0);
24763 goto exit_drop_index;
24764 }
24765 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
24766 #ifndef SQLITE_OMIT_AUTHORIZATION
24767 {
24768 int code = SQLITE_DROP_INDEX;
24769 Table *pTab = pIndex->pTable;
24770 const char *zDb = db->aDb[iDb].zDbSName;
24771 const char *zTab = SCHEMA_TABLE(iDb);
24772 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
24773 goto exit_drop_index;
24774 }
24775 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
24776 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
24777 goto exit_drop_index;
24778 }
24779 }
24780 #endif
24781
24782 /* Generate code to remove the index and from the master table */
24783 v = sqlite3GetVdbe(pParse);
24784 if( v ){
24785 sqlite3BeginWriteOperation(pParse, 1, iDb);
24786 sqlite3NestedParse(pParse,
24787 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
24788 db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName
24789 );
24790 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
24791 sqlite3ChangeCookie(pParse, iDb);
24792 destroyRootPage(pParse, pIndex->tnum, iDb);
24793 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
24794 }
24795
24796 exit_drop_index:
24797 sqlite3SrcListDelete(db, pName);
24798 }
24799
24800 /*
24801 ** pArray is a pointer to an array of objects. Each object in the
24802 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
24803 ** to extend the array so that there is space for a new object at the end.
24804 **
24805 ** When this function is called, *pnEntry contains the current size of
24806 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
24807 ** in total).
24808 **
24809 ** If the realloc() is successful (i.e. if no OOM condition occurs), the
24810 ** space allocated for the new object is zeroed, *pnEntry updated to
24811 ** reflect the new size of the array and a pointer to the new allocation
24812 ** returned. *pIdx is set to the index of the new array entry in this case.
24813 **
24814 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
24815 ** unchanged and a copy of pArray returned.
24816 */
24817 SQLITE_PRIVATE void *sqlite3ArrayAllocate(
24818 sqlite3 *db, /* Connection to notify of malloc failures */
24819 void *pArray, /* Array of objects. Might be reallocated */
24820 int szEntry, /* Size of each object in the array */
24821 int *pnEntry, /* Number of objects currently in use */
24822 int *pIdx /* Write the index of a new slot here */
24823 ){
24824 char *z;
24825 int n = *pnEntry;
24826 if( (n & (n-1))==0 ){
24827 int sz = (n==0) ? 1 : 2*n;
24828 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
24829 if( pNew==0 ){
24830 *pIdx = -1;
24831 return pArray;
24832 }
24833 pArray = pNew;
24834 }
24835 z = (char*)pArray;
24836 memset(&z[n * szEntry], 0, szEntry);
24837 *pIdx = n;
24838 ++*pnEntry;
24839 return pArray;
24840 }
24841
24842 /*
24843 ** Append a new element to the given IdList. Create a new IdList if
24844 ** need be.
24845 **
24846 ** A new IdList is returned, or NULL if malloc() fails.
24847 */
24848 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pT oken){
24849 int i;
24850 if( pList==0 ){
24851 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
24852 if( pList==0 ) return 0;
24853 }
24854 pList->a = sqlite3ArrayAllocate(
24855 db,
24856 pList->a,
24857 sizeof(pList->a[0]),
24858 &pList->nId,
24859 &i
24860 );
24861 if( i<0 ){
24862 sqlite3IdListDelete(db, pList);
24863 return 0;
24864 }
24865 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
24866 return pList;
24867 }
24868
24869 /*
24870 ** Delete an IdList.
24871 */
24872 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
24873 int i;
24874 if( pList==0 ) return;
24875 for(i=0; i<pList->nId; i++){
24876 sqlite3DbFree(db, pList->a[i].zName);
24877 }
24878 sqlite3DbFree(db, pList->a);
24879 sqlite3DbFree(db, pList);
24880 }
24881
24882 /*
24883 ** Return the index in pList of the identifier named zId. Return -1
24884 ** if not found.
24885 */
24886 SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
24887 int i;
24888 if( pList==0 ) return -1;
24889 for(i=0; i<pList->nId; i++){
24890 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
24891 }
24892 return -1;
24893 }
24894
24895 /*
24896 ** Expand the space allocated for the given SrcList object by
24897 ** creating nExtra new slots beginning at iStart. iStart is zero based.
24898 ** New slots are zeroed.
24899 **
24900 ** For example, suppose a SrcList initially contains two entries: A,B.
24901 ** To append 3 new entries onto the end, do this:
24902 **
24903 ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
24904 **
24905 ** After the call above it would contain: A, B, nil, nil, nil.
24906 ** If the iStart argument had been 1 instead of 2, then the result
24907 ** would have been: A, nil, nil, nil, B. To prepend the new slots,
24908 ** the iStart value would be 0. The result then would
24909 ** be: nil, nil, nil, A, B.
24910 **
24911 ** If a memory allocation fails the SrcList is unchanged. The
24912 ** db->mallocFailed flag will be set to true.
24913 */
24914 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
24915 sqlite3 *db, /* Database connection to notify of OOM errors */
24916 SrcList *pSrc, /* The SrcList to be enlarged */
24917 int nExtra, /* Number of new slots to add to pSrc->a[] */
24918 int iStart /* Index in pSrc->a[] of first new slot */
24919 ){
24920 int i;
24921
24922 /* Sanity checking on calling parameters */
24923 assert( iStart>=0 );
24924 assert( nExtra>=1 );
24925 assert( pSrc!=0 );
24926 assert( iStart<=pSrc->nSrc );
24927
24928 /* Allocate additional space if needed */
24929 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
24930 SrcList *pNew;
24931 int nAlloc = pSrc->nSrc*2+nExtra;
24932 int nGot;
24933 pNew = sqlite3DbRealloc(db, pSrc,
24934 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
24935 if( pNew==0 ){
24936 assert( db->mallocFailed );
24937 return pSrc;
24938 }
24939 pSrc = pNew;
24940 nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
24941 pSrc->nAlloc = nGot;
24942 }
24943
24944 /* Move existing slots that come after the newly inserted slots
24945 ** out of the way */
24946 for(i=pSrc->nSrc-1; i>=iStart; i--){
24947 pSrc->a[i+nExtra] = pSrc->a[i];
24948 }
24949 pSrc->nSrc += nExtra;
24950
24951 /* Zero the newly allocated slots */
24952 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
24953 for(i=iStart; i<iStart+nExtra; i++){
24954 pSrc->a[i].iCursor = -1;
24955 }
24956
24957 /* Return a pointer to the enlarged SrcList */
24958 return pSrc;
24959 }
24960
24961
24962 /*
24963 ** Append a new table name to the given SrcList. Create a new SrcList if
24964 ** need be. A new entry is created in the SrcList even if pTable is NULL.
24965 **
24966 ** A SrcList is returned, or NULL if there is an OOM error. The returned
24967 ** SrcList might be the same as the SrcList that was input or it might be
24968 ** a new one. If an OOM error does occurs, then the prior value of pList
24969 ** that is input to this routine is automatically freed.
24970 **
24971 ** If pDatabase is not null, it means that the table has an optional
24972 ** database name prefix. Like this: "database.table". The pDatabase
24973 ** points to the table name and the pTable points to the database name.
24974 ** The SrcList.a[].zName field is filled with the table name which might
24975 ** come from pTable (if pDatabase is NULL) or from pDatabase.
24976 ** SrcList.a[].zDatabase is filled with the database name from pTable,
24977 ** or with NULL if no database is specified.
24978 **
24979 ** In other words, if call like this:
24980 **
24981 ** sqlite3SrcListAppend(D,A,B,0);
24982 **
24983 ** Then B is a table name and the database name is unspecified. If called
24984 ** like this:
24985 **
24986 ** sqlite3SrcListAppend(D,A,B,C);
24987 **
24988 ** Then C is the table name and B is the database name. If C is defined
24989 ** then so is B. In other words, we never have a case where:
24990 **
24991 ** sqlite3SrcListAppend(D,A,0,C);
24992 **
24993 ** Both pTable and pDatabase are assumed to be quoted. They are dequoted
24994 ** before being added to the SrcList.
24995 */
24996 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
24997 sqlite3 *db, /* Connection to notify of malloc failures */
24998 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
24999 Token *pTable, /* Table to append */
25000 Token *pDatabase /* Database of the table */
25001 ){
25002 struct SrcList_item *pItem;
25003 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
25004 assert( db!=0 );
25005 if( pList==0 ){
25006 pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) );
25007 if( pList==0 ) return 0;
25008 pList->nAlloc = 1;
25009 pList->nSrc = 1;
25010 memset(&pList->a[0], 0, sizeof(pList->a[0]));
25011 pList->a[0].iCursor = -1;
25012 }else{
25013 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
25014 }
25015 if( db->mallocFailed ){
25016 sqlite3SrcListDelete(db, pList);
25017 return 0;
25018 }
25019 pItem = &pList->a[pList->nSrc-1];
25020 if( pDatabase && pDatabase->z==0 ){
25021 pDatabase = 0;
25022 }
25023 if( pDatabase ){
25024 Token *pTemp = pDatabase;
25025 pDatabase = pTable;
25026 pTable = pTemp;
25027 }
25028 pItem->zName = sqlite3NameFromToken(db, pTable);
25029 pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
25030 return pList;
25031 }
25032
25033 /*
25034 ** Assign VdbeCursor index numbers to all tables in a SrcList
25035 */
25036 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
25037 int i;
25038 struct SrcList_item *pItem;
25039 assert(pList || pParse->db->mallocFailed );
25040 if( pList ){
25041 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
25042 if( pItem->iCursor>=0 ) break;
25043 pItem->iCursor = pParse->nTab++;
25044 if( pItem->pSelect ){
25045 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
25046 }
25047 }
25048 }
25049 }
25050
25051 /*
25052 ** Delete an entire SrcList including all its substructure.
25053 */
25054 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
25055 int i;
25056 struct SrcList_item *pItem;
25057 if( pList==0 ) return;
25058 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
25059 sqlite3DbFree(db, pItem->zDatabase);
25060 sqlite3DbFree(db, pItem->zName);
25061 sqlite3DbFree(db, pItem->zAlias);
25062 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
25063 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
25064 sqlite3DeleteTable(db, pItem->pTab);
25065 sqlite3SelectDelete(db, pItem->pSelect);
25066 sqlite3ExprDelete(db, pItem->pOn);
25067 sqlite3IdListDelete(db, pItem->pUsing);
25068 }
25069 sqlite3DbFree(db, pList);
25070 }
25071
25072 /*
25073 ** This routine is called by the parser to add a new term to the
25074 ** end of a growing FROM clause. The "p" parameter is the part of
25075 ** the FROM clause that has already been constructed. "p" is NULL
25076 ** if this is the first term of the FROM clause. pTable and pDatabase
25077 ** are the name of the table and database named in the FROM clause term.
25078 ** pDatabase is NULL if the database name qualifier is missing - the
25079 ** usual case. If the term has an alias, then pAlias points to the
25080 ** alias token. If the term is a subquery, then pSubquery is the
25081 ** SELECT statement that the subquery encodes. The pTable and
25082 ** pDatabase parameters are NULL for subqueries. The pOn and pUsing
25083 ** parameters are the content of the ON and USING clauses.
25084 **
25085 ** Return a new SrcList which encodes is the FROM with the new
25086 ** term added.
25087 */
25088 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
25089 Parse *pParse, /* Parsing context */
25090 SrcList *p, /* The left part of the FROM clause already seen */
25091 Token *pTable, /* Name of the table to add to the FROM clause */
25092 Token *pDatabase, /* Name of the database containing pTable */
25093 Token *pAlias, /* The right-hand side of the AS subexpression */
25094 Select *pSubquery, /* A subquery used in place of a table name */
25095 Expr *pOn, /* The ON clause of a join */
25096 IdList *pUsing /* The USING clause of a join */
25097 ){
25098 struct SrcList_item *pItem;
25099 sqlite3 *db = pParse->db;
25100 if( !p && (pOn || pUsing) ){
25101 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
25102 (pOn ? "ON" : "USING")
25103 );
25104 goto append_from_error;
25105 }
25106 p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
25107 if( p==0 || NEVER(p->nSrc==0) ){
25108 goto append_from_error;
25109 }
25110 pItem = &p->a[p->nSrc-1];
25111 assert( pAlias!=0 );
25112 if( pAlias->n ){
25113 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
25114 }
25115 pItem->pSelect = pSubquery;
25116 pItem->pOn = pOn;
25117 pItem->pUsing = pUsing;
25118 return p;
25119
25120 append_from_error:
25121 assert( p==0 );
25122 sqlite3ExprDelete(db, pOn);
25123 sqlite3IdListDelete(db, pUsing);
25124 sqlite3SelectDelete(db, pSubquery);
25125 return 0;
25126 }
25127
25128 /*
25129 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added
25130 ** element of the source-list passed as the second argument.
25131 */
25132 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pI ndexedBy){
25133 assert( pIndexedBy!=0 );
25134 if( p && ALWAYS(p->nSrc>0) ){
25135 struct SrcList_item *pItem = &p->a[p->nSrc-1];
25136 assert( pItem->fg.notIndexed==0 );
25137 assert( pItem->fg.isIndexedBy==0 );
25138 assert( pItem->fg.isTabFunc==0 );
25139 if( pIndexedBy->n==1 && !pIndexedBy->z ){
25140 /* A "NOT INDEXED" clause was supplied. See parse.y
25141 ** construct "indexed_opt" for details. */
25142 pItem->fg.notIndexed = 1;
25143 }else{
25144 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
25145 pItem->fg.isIndexedBy = (pItem->u1.zIndexedBy!=0);
25146 }
25147 }
25148 }
25149
25150 /*
25151 ** Add the list of function arguments to the SrcList entry for a
25152 ** table-valued-function.
25153 */
25154 SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList * pList){
25155 if( p ){
25156 struct SrcList_item *pItem = &p->a[p->nSrc-1];
25157 assert( pItem->fg.notIndexed==0 );
25158 assert( pItem->fg.isIndexedBy==0 );
25159 assert( pItem->fg.isTabFunc==0 );
25160 pItem->u1.pFuncArg = pList;
25161 pItem->fg.isTabFunc = 1;
25162 }else{
25163 sqlite3ExprListDelete(pParse->db, pList);
25164 }
25165 }
25166
25167 /*
25168 ** When building up a FROM clause in the parser, the join operator
25169 ** is initially attached to the left operand. But the code generator
25170 ** expects the join operator to be on the right operand. This routine
25171 ** Shifts all join operators from left to right for an entire FROM
25172 ** clause.
25173 **
25174 ** Example: Suppose the join is like this:
25175 **
25176 ** A natural cross join B
25177 **
25178 ** The operator is "natural cross join". The A and B operands are stored
25179 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
25180 ** operator with A. This routine shifts that operator over to B.
25181 */
25182 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
25183 if( p ){
25184 int i;
25185 for(i=p->nSrc-1; i>0; i--){
25186 p->a[i].fg.jointype = p->a[i-1].fg.jointype;
25187 }
25188 p->a[0].fg.jointype = 0;
25189 }
25190 }
25191
25192 /*
25193 ** Generate VDBE code for a BEGIN statement.
25194 */
25195 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
25196 sqlite3 *db;
25197 Vdbe *v;
25198 int i;
25199
25200 assert( pParse!=0 );
25201 db = pParse->db;
25202 assert( db!=0 );
25203 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
25204 return;
25205 }
25206 v = sqlite3GetVdbe(pParse);
25207 if( !v ) return;
25208 if( type!=TK_DEFERRED ){
25209 for(i=0; i<db->nDb; i++){
25210 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
25211 sqlite3VdbeUsesBtree(v, i);
25212 }
25213 }
25214 sqlite3VdbeAddOp0(v, OP_AutoCommit);
25215 }
25216
25217 /*
25218 ** Generate VDBE code for a COMMIT statement.
25219 */
25220 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
25221 Vdbe *v;
25222
25223 assert( pParse!=0 );
25224 assert( pParse->db!=0 );
25225 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
25226 return;
25227 }
25228 v = sqlite3GetVdbe(pParse);
25229 if( v ){
25230 sqlite3VdbeAddOp1(v, OP_AutoCommit, 1);
25231 }
25232 }
25233
25234 /*
25235 ** Generate VDBE code for a ROLLBACK statement.
25236 */
25237 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
25238 Vdbe *v;
25239
25240 assert( pParse!=0 );
25241 assert( pParse->db!=0 );
25242 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
25243 return;
25244 }
25245 v = sqlite3GetVdbe(pParse);
25246 if( v ){
25247 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
25248 }
25249 }
25250
25251 /*
25252 ** This function is called by the parser when it parses a command to create,
25253 ** release or rollback an SQL savepoint.
25254 */
25255 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
25256 char *zName = sqlite3NameFromToken(pParse->db, pName);
25257 if( zName ){
25258 Vdbe *v = sqlite3GetVdbe(pParse);
25259 #ifndef SQLITE_OMIT_AUTHORIZATION
25260 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
25261 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
25262 #endif
25263 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
25264 sqlite3DbFree(pParse->db, zName);
25265 return;
25266 }
25267 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
25268 }
25269 }
25270
25271 /*
25272 ** Make sure the TEMP database is open and available for use. Return
25273 ** the number of errors. Leave any error messages in the pParse structure.
25274 */
25275 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
25276 sqlite3 *db = pParse->db;
25277 if( db->aDb[1].pBt==0 && !pParse->explain ){
25278 int rc;
25279 Btree *pBt;
25280 static const int flags =
25281 SQLITE_OPEN_READWRITE |
25282 SQLITE_OPEN_CREATE |
25283 SQLITE_OPEN_EXCLUSIVE |
25284 SQLITE_OPEN_DELETEONCLOSE |
25285 SQLITE_OPEN_TEMP_DB;
25286
25287 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
25288 if( rc!=SQLITE_OK ){
25289 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
25290 "file for storing temporary tables");
25291 pParse->rc = rc;
25292 return 1;
25293 }
25294 db->aDb[1].pBt = pBt;
25295 assert( db->aDb[1].pSchema );
25296 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
25297 sqlite3OomFault(db);
25298 return 1;
25299 }
25300 }
25301 return 0;
25302 }
25303
25304 /*
25305 ** Record the fact that the schema cookie will need to be verified
25306 ** for database iDb. The code to actually verify the schema cookie
25307 ** will occur at the end of the top-level VDBE and will be generated
25308 ** later, by sqlite3FinishCoding().
25309 */
25310 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
25311 Parse *pToplevel = sqlite3ParseToplevel(pParse);
25312
25313 assert( iDb>=0 && iDb<pParse->db->nDb );
25314 assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 );
25315 assert( iDb<SQLITE_MAX_ATTACHED+2 );
25316 assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) );
25317 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
25318 DbMaskSet(pToplevel->cookieMask, iDb);
25319 if( !OMIT_TEMPDB && iDb==1 ){
25320 sqlite3OpenTempDatabase(pToplevel);
25321 }
25322 }
25323 }
25324
25325 /*
25326 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
25327 ** attached database. Otherwise, invoke it for the database named zDb only.
25328 */
25329 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb) {
25330 sqlite3 *db = pParse->db;
25331 int i;
25332 for(i=0; i<db->nDb; i++){
25333 Db *pDb = &db->aDb[i];
25334 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
25335 sqlite3CodeVerifySchema(pParse, i);
25336 }
25337 }
25338 }
25339
25340 /*
25341 ** Generate VDBE code that prepares for doing an operation that
25342 ** might change the database.
25343 **
25344 ** This routine starts a new transaction if we are not already within
25345 ** a transaction. If we are already within a transaction, then a checkpoint
25346 ** is set if the setStatement parameter is true. A checkpoint should
25347 ** be set for operations that might fail (due to a constraint) part of
25348 ** the way through and which will need to undo some writes without having to
25349 ** rollback the whole transaction. For operations where all constraints
25350 ** can be checked before any changes are made to the database, it is never
25351 ** necessary to undo a write and the checkpoint should not be set.
25352 */
25353 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
25354 Parse *pToplevel = sqlite3ParseToplevel(pParse);
25355 sqlite3CodeVerifySchema(pParse, iDb);
25356 DbMaskSet(pToplevel->writeMask, iDb);
25357 pToplevel->isMultiWrite |= setStatement;
25358 }
25359
25360 /*
25361 ** Indicate that the statement currently under construction might write
25362 ** more than one entry (example: deleting one row then inserting another,
25363 ** inserting multiple rows in a table, or inserting a row and index entries.)
25364 ** If an abort occurs after some of these writes have completed, then it will
25365 ** be necessary to undo the completed writes.
25366 */
25367 SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
25368 Parse *pToplevel = sqlite3ParseToplevel(pParse);
25369 pToplevel->isMultiWrite = 1;
25370 }
25371
25372 /*
25373 ** The code generator calls this routine if is discovers that it is
25374 ** possible to abort a statement prior to completion. In order to
25375 ** perform this abort without corrupting the database, we need to make
25376 ** sure that the statement is protected by a statement transaction.
25377 **
25378 ** Technically, we only need to set the mayAbort flag if the
25379 ** isMultiWrite flag was previously set. There is a time dependency
25380 ** such that the abort must occur after the multiwrite. This makes
25381 ** some statements involving the REPLACE conflict resolution algorithm
25382 ** go a little faster. But taking advantage of this time dependency
25383 ** makes it more difficult to prove that the code is correct (in
25384 ** particular, it prevents us from writing an effective
25385 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
25386 ** to take the safe route and skip the optimization.
25387 */
25388 SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
25389 Parse *pToplevel = sqlite3ParseToplevel(pParse);
25390 pToplevel->mayAbort = 1;
25391 }
25392
25393 /*
25394 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
25395 ** error. The onError parameter determines which (if any) of the statement
25396 ** and/or current transaction is rolled back.
25397 */
25398 SQLITE_PRIVATE void sqlite3HaltConstraint(
25399 Parse *pParse, /* Parsing context */
25400 int errCode, /* extended error code */
25401 int onError, /* Constraint type */
25402 char *p4, /* Error message */
25403 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
25404 u8 p5Errmsg /* P5_ErrMsg type */
25405 ){
25406 Vdbe *v = sqlite3GetVdbe(pParse);
25407 assert( (errCode&0xff)==SQLITE_CONSTRAINT );
25408 if( onError==OE_Abort ){
25409 sqlite3MayAbort(pParse);
25410 }
25411 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
25412 sqlite3VdbeChangeP5(v, p5Errmsg);
25413 }
25414
25415 /*
25416 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
25417 */
25418 SQLITE_PRIVATE void sqlite3UniqueConstraint(
25419 Parse *pParse, /* Parsing context */
25420 int onError, /* Constraint type */
25421 Index *pIdx /* The index that triggers the constraint */
25422 ){
25423 char *zErr;
25424 int j;
25425 StrAccum errMsg;
25426 Table *pTab = pIdx->pTable;
25427
25428 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
25429 if( pIdx->aColExpr ){
25430 sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName);
25431 }else{
25432 for(j=0; j<pIdx->nKeyCol; j++){
25433 char *zCol;
25434 assert( pIdx->aiColumn[j]>=0 );
25435 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
25436 if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
25437 sqlite3XPrintf(&errMsg, "%s.%s", pTab->zName, zCol);
25438 }
25439 }
25440 zErr = sqlite3StrAccumFinish(&errMsg);
25441 sqlite3HaltConstraint(pParse,
25442 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
25443 : SQLITE_CONSTRAINT_UNIQUE,
25444 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
25445 }
25446
25447
25448 /*
25449 ** Code an OP_Halt due to non-unique rowid.
25450 */
25451 SQLITE_PRIVATE void sqlite3RowidConstraint(
25452 Parse *pParse, /* Parsing context */
25453 int onError, /* Conflict resolution algorithm */
25454 Table *pTab /* The table with the non-unique rowid */
25455 ){
25456 char *zMsg;
25457 int rc;
25458 if( pTab->iPKey>=0 ){
25459 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
25460 pTab->aCol[pTab->iPKey].zName);
25461 rc = SQLITE_CONSTRAINT_PRIMARYKEY;
25462 }else{
25463 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
25464 rc = SQLITE_CONSTRAINT_ROWID;
25465 }
25466 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
25467 P5_ConstraintUnique);
25468 }
25469
25470 /*
25471 ** Check to see if pIndex uses the collating sequence pColl. Return
25472 ** true if it does and false if it does not.
25473 */
25474 #ifndef SQLITE_OMIT_REINDEX
25475 static int collationMatch(const char *zColl, Index *pIndex){
25476 int i;
25477 assert( zColl!=0 );
25478 for(i=0; i<pIndex->nColumn; i++){
25479 const char *z = pIndex->azColl[i];
25480 assert( z!=0 || pIndex->aiColumn[i]<0 );
25481 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
25482 return 1;
25483 }
25484 }
25485 return 0;
25486 }
25487 #endif
25488
25489 /*
25490 ** Recompute all indices of pTab that use the collating sequence pColl.
25491 ** If pColl==0 then recompute all indices of pTab.
25492 */
25493 #ifndef SQLITE_OMIT_REINDEX
25494 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
25495 Index *pIndex; /* An index associated with pTab */
25496
25497 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
25498 if( zColl==0 || collationMatch(zColl, pIndex) ){
25499 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
25500 sqlite3BeginWriteOperation(pParse, 0, iDb);
25501 sqlite3RefillIndex(pParse, pIndex, -1);
25502 }
25503 }
25504 }
25505 #endif
25506
25507 /*
25508 ** Recompute all indices of all tables in all databases where the
25509 ** indices use the collating sequence pColl. If pColl==0 then recompute
25510 ** all indices everywhere.
25511 */
25512 #ifndef SQLITE_OMIT_REINDEX
25513 static void reindexDatabases(Parse *pParse, char const *zColl){
25514 Db *pDb; /* A single database */
25515 int iDb; /* The database index number */
25516 sqlite3 *db = pParse->db; /* The database connection */
25517 HashElem *k; /* For looping over tables in pDb */
25518 Table *pTab; /* A table in the database */
25519
25520 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */
25521 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
25522 assert( pDb!=0 );
25523 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
25524 pTab = (Table*)sqliteHashData(k);
25525 reindexTable(pParse, pTab, zColl);
25526 }
25527 }
25528 }
25529 #endif
25530
25531 /*
25532 ** Generate code for the REINDEX command.
25533 **
25534 ** REINDEX -- 1
25535 ** REINDEX <collation> -- 2
25536 ** REINDEX ?<database>.?<tablename> -- 3
25537 ** REINDEX ?<database>.?<indexname> -- 4
25538 **
25539 ** Form 1 causes all indices in all attached databases to be rebuilt.
25540 ** Form 2 rebuilds all indices in all databases that use the named
25541 ** collating function. Forms 3 and 4 rebuild the named index or all
25542 ** indices associated with the named table.
25543 */
25544 #ifndef SQLITE_OMIT_REINDEX
25545 SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
25546 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
25547 char *z; /* Name of a table or index */
25548 const char *zDb; /* Name of the database */
25549 Table *pTab; /* A table in the database */
25550 Index *pIndex; /* An index associated with pTab */
25551 int iDb; /* The database index number */
25552 sqlite3 *db = pParse->db; /* The database connection */
25553 Token *pObjName; /* Name of the table or index to be reindexed */
25554
25555 /* Read the database schema. If an error occurs, leave an error message
25556 ** and code in pParse and return NULL. */
25557 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
25558 return;
25559 }
25560
25561 if( pName1==0 ){
25562 reindexDatabases(pParse, 0);
25563 return;
25564 }else if( NEVER(pName2==0) || pName2->z==0 ){
25565 char *zColl;
25566 assert( pName1->z );
25567 zColl = sqlite3NameFromToken(pParse->db, pName1);
25568 if( !zColl ) return;
25569 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
25570 if( pColl ){
25571 reindexDatabases(pParse, zColl);
25572 sqlite3DbFree(db, zColl);
25573 return;
25574 }
25575 sqlite3DbFree(db, zColl);
25576 }
25577 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
25578 if( iDb<0 ) return;
25579 z = sqlite3NameFromToken(db, pObjName);
25580 if( z==0 ) return;
25581 zDb = db->aDb[iDb].zDbSName;
25582 pTab = sqlite3FindTable(db, z, zDb);
25583 if( pTab ){
25584 reindexTable(pParse, pTab, 0);
25585 sqlite3DbFree(db, z);
25586 return;
25587 }
25588 pIndex = sqlite3FindIndex(db, z, zDb);
25589 sqlite3DbFree(db, z);
25590 if( pIndex ){
25591 sqlite3BeginWriteOperation(pParse, 0, iDb);
25592 sqlite3RefillIndex(pParse, pIndex, -1);
25593 return;
25594 }
25595 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
25596 }
25597 #endif
25598
25599 /*
25600 ** Return a KeyInfo structure that is appropriate for the given Index.
25601 **
25602 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object
25603 ** when it has finished using it.
25604 */
25605 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
25606 int i;
25607 int nCol = pIdx->nColumn;
25608 int nKey = pIdx->nKeyCol;
25609 KeyInfo *pKey;
25610 if( pParse->nErr ) return 0;
25611 if( pIdx->uniqNotNull ){
25612 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
25613 }else{
25614 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
25615 }
25616 if( pKey ){
25617 assert( sqlite3KeyInfoIsWriteable(pKey) );
25618 for(i=0; i<nCol; i++){
25619 const char *zColl = pIdx->azColl[i];
25620 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
25621 sqlite3LocateCollSeq(pParse, zColl);
25622 pKey->aSortOrder[i] = pIdx->aSortOrder[i];
25623 }
25624 if( pParse->nErr ){
25625 sqlite3KeyInfoUnref(pKey);
25626 pKey = 0;
25627 }
25628 }
25629 return pKey;
25630 }
25631
25632 #ifndef SQLITE_OMIT_CTE
25633 /*
25634 ** This routine is invoked once per CTE by the parser while parsing a
25635 ** WITH clause.
25636 */
25637 SQLITE_PRIVATE With *sqlite3WithAdd(
25638 Parse *pParse, /* Parsing context */
25639 With *pWith, /* Existing WITH clause, or NULL */
25640 Token *pName, /* Name of the common-table */
25641 ExprList *pArglist, /* Optional column name list for the table */
25642 Select *pQuery /* Query used to initialize the table */
25643 ){
25644 sqlite3 *db = pParse->db;
25645 With *pNew;
25646 char *zName;
25647
25648 /* Check that the CTE name is unique within this WITH clause. If
25649 ** not, store an error in the Parse structure. */
25650 zName = sqlite3NameFromToken(pParse->db, pName);
25651 if( zName && pWith ){
25652 int i;
25653 for(i=0; i<pWith->nCte; i++){
25654 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
25655 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
25656 }
25657 }
25658 }
25659
25660 if( pWith ){
25661 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
25662 pNew = sqlite3DbRealloc(db, pWith, nByte);
25663 }else{
25664 pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
25665 }
25666 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
25667
25668 if( db->mallocFailed ){
25669 sqlite3ExprListDelete(db, pArglist);
25670 sqlite3SelectDelete(db, pQuery);
25671 sqlite3DbFree(db, zName);
25672 pNew = pWith;
25673 }else{
25674 pNew->a[pNew->nCte].pSelect = pQuery;
25675 pNew->a[pNew->nCte].pCols = pArglist;
25676 pNew->a[pNew->nCte].zName = zName;
25677 pNew->a[pNew->nCte].zCteErr = 0;
25678 pNew->nCte++;
25679 }
25680
25681 return pNew;
25682 }
25683
25684 /*
25685 ** Free the contents of the With object passed as the second argument.
25686 */
25687 SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
25688 if( pWith ){
25689 int i;
25690 for(i=0; i<pWith->nCte; i++){
25691 struct Cte *pCte = &pWith->a[i];
25692 sqlite3ExprListDelete(db, pCte->pCols);
25693 sqlite3SelectDelete(db, pCte->pSelect);
25694 sqlite3DbFree(db, pCte->zName);
25695 }
25696 sqlite3DbFree(db, pWith);
25697 }
25698 }
25699 #endif /* !defined(SQLITE_OMIT_CTE) */
25700
25701 /************** End of build.c ***********************************************/
25702
25703 /* Chain include. */
25704 #include "sqlite3.05.c"
OLDNEW
« no previous file with comments | « third_party/sqlite/amalgamation/sqlite3.03.c ('k') | third_party/sqlite/amalgamation/sqlite3.05.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698