1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains C code routines that are called by the parser
13 ** to handle INSERT statements in SQLite.
14 */
15 #include "sqliteInt.h"
16 
17 /*
18 ** Generate code that will
19 **
20 **   (1) acquire a lock for table pTab then
21 **   (2) open pTab as cursor iCur.
22 **
23 ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
24 ** for that table that is actually opened.
25 */
26 void sqlite3OpenTable(
27   Parse *pParse,  /* Generate code into this VDBE */
28   int iCur,       /* The cursor number of the table */
29   int iDb,        /* The database index in sqlite3.aDb[] */
30   Table *pTab,    /* The table to be opened */
31   int opcode      /* OP_OpenRead or OP_OpenWrite */
32 ){
33   Vdbe *v;
34   assert( !IsVirtual(pTab) );
35   assert( pParse->pVdbe!=0 );
36   v = pParse->pVdbe;
37   assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
38   sqlite3TableLock(pParse, iDb, pTab->tnum,
39                    (opcode==OP_OpenWrite)?1:0, pTab->zName);
40   if( HasRowid(pTab) ){
41     sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
42     VdbeComment((v, "%s", pTab->zName));
43   }else{
44     Index *pPk = sqlite3PrimaryKeyIndex(pTab);
45     assert( pPk!=0 );
46     assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
47     sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
48     sqlite3VdbeSetP4KeyInfo(pParse, pPk);
49     VdbeComment((v, "%s", pTab->zName));
50   }
51 }
52 
53 /*
54 ** Return a pointer to the column affinity string associated with index
55 ** pIdx. A column affinity string has one character for each column in
56 ** the table, according to the affinity of the column:
57 **
58 **  Character      Column affinity
59 **  ------------------------------
60 **  'A'            BLOB
61 **  'B'            TEXT
62 **  'C'            NUMERIC
63 **  'D'            INTEGER
64 **  'F'            REAL
65 **
66 ** An extra 'D' is appended to the end of the string to cover the
67 ** rowid that appears as the last column in every index.
68 **
69 ** Memory for the buffer containing the column index affinity string
70 ** is managed along with the rest of the Index structure. It will be
71 ** released when sqlite3DeleteIndex() is called.
72 */
73 const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
74   if( !pIdx->zColAff ){
75     /* The first time a column affinity string for a particular index is
76     ** required, it is allocated and populated here. It is then stored as
77     ** a member of the Index structure for subsequent use.
78     **
79     ** The column affinity string will eventually be deleted by
80     ** sqliteDeleteIndex() when the Index structure itself is cleaned
81     ** up.
82     */
83     int n;
84     Table *pTab = pIdx->pTable;
85     pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
86     if( !pIdx->zColAff ){
87       sqlite3OomFault(db);
88       return 0;
89     }
90     for(n=0; n<pIdx->nColumn; n++){
91       i16 x = pIdx->aiColumn[n];
92       char aff;
93       if( x>=0 ){
94         aff = pTab->aCol[x].affinity;
95       }else if( x==XN_ROWID ){
96         aff = SQLITE_AFF_INTEGER;
97       }else{
98         assert( x==XN_EXPR );
99         assert( pIdx->bHasExpr );
100         assert( pIdx->aColExpr!=0 );
101         aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
102       }
103       if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
104       if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
105       pIdx->zColAff[n] = aff;
106     }
107     pIdx->zColAff[n] = 0;
108   }
109 
110   return pIdx->zColAff;
111 }
112 
113 /*
114 ** Compute an affinity string for a table.   Space is obtained
115 ** from sqlite3DbMalloc().  The caller is responsible for freeing
116 ** the space when done.
117 */
118 char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){
119   char *zColAff;
120   zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
121   if( zColAff ){
122     int i, j;
123     for(i=j=0; i<pTab->nCol; i++){
124       if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
125         zColAff[j++] = pTab->aCol[i].affinity;
126       }
127     }
128     do{
129       zColAff[j--] = 0;
130     }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
131   }
132   return zColAff;
133 }
134 
135 /*
136 ** Make changes to the evolving bytecode to do affinity transformations
137 ** of values that are about to be gathered into a row for table pTab.
138 **
139 ** For ordinary (legacy, non-strict) tables:
140 ** -----------------------------------------
141 **
142 ** Compute the affinity string for table pTab, if it has not already been
143 ** computed.  As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
144 **
145 ** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries
146 ** which were then optimized out) then this routine becomes a no-op.
147 **
148 ** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the
149 ** affinities for register iReg and following.  Or if iReg==0,
150 ** then just set the P4 operand of the previous opcode (which should  be
151 ** an OP_MakeRecord) to the affinity string.
152 **
153 ** A column affinity string has one character per column:
154 **
155 **    Character      Column affinity
156 **    ---------      ---------------
157 **    'A'            BLOB
158 **    'B'            TEXT
159 **    'C'            NUMERIC
160 **    'D'            INTEGER
161 **    'E'            REAL
162 **
163 ** For STRICT tables:
164 ** ------------------
165 **
166 ** Generate an appropropriate OP_TypeCheck opcode that will verify the
167 ** datatypes against the column definitions in pTab.  If iReg==0, that
168 ** means an OP_MakeRecord opcode has already been generated and should be
169 ** the last opcode generated.  The new OP_TypeCheck needs to be inserted
170 ** before the OP_MakeRecord.  The new OP_TypeCheck should use the same
171 ** register set as the OP_MakeRecord.  If iReg>0 then register iReg is
172 ** the first of a series of registers that will form the new record.
173 ** Apply the type checking to that array of registers.
174 */
175 void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
176   int i;
177   char *zColAff;
178   if( pTab->tabFlags & TF_Strict ){
179     if( iReg==0 ){
180       /* Move the previous opcode (which should be OP_MakeRecord) forward
181       ** by one slot and insert a new OP_TypeCheck where the current
182       ** OP_MakeRecord is found */
183       VdbeOp *pPrev;
184       sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
185       pPrev = sqlite3VdbeGetLastOp(v);
186       assert( pPrev!=0 );
187       assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
188       pPrev->opcode = OP_TypeCheck;
189       sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
190     }else{
191       /* Insert an isolated OP_Typecheck */
192       sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
193       sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
194     }
195     return;
196   }
197   zColAff = pTab->zColAff;
198   if( zColAff==0 ){
199     zColAff = sqlite3TableAffinityStr(0, pTab);
200     if( !zColAff ){
201       sqlite3OomFault(sqlite3VdbeDb(v));
202       return;
203     }
204     pTab->zColAff = zColAff;
205   }
206   assert( zColAff!=0 );
207   i = sqlite3Strlen30NN(zColAff);
208   if( i ){
209     if( iReg ){
210       sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
211     }else{
212       assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
213               || sqlite3VdbeDb(v)->mallocFailed );
214       sqlite3VdbeChangeP4(v, -1, zColAff, i);
215     }
216   }
217 }
218 
219 /*
220 ** Return non-zero if the table pTab in database iDb or any of its indices
221 ** have been opened at any point in the VDBE program. This is used to see if
222 ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can
223 ** run without using a temporary table for the results of the SELECT.
224 */
225 static int readsTable(Parse *p, int iDb, Table *pTab){
226   Vdbe *v = sqlite3GetVdbe(p);
227   int i;
228   int iEnd = sqlite3VdbeCurrentAddr(v);
229 #ifndef SQLITE_OMIT_VIRTUALTABLE
230   VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
231 #endif
232 
233   for(i=1; i<iEnd; i++){
234     VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
235     assert( pOp!=0 );
236     if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
237       Index *pIndex;
238       Pgno tnum = pOp->p2;
239       if( tnum==pTab->tnum ){
240         return 1;
241       }
242       for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
243         if( tnum==pIndex->tnum ){
244           return 1;
245         }
246       }
247     }
248 #ifndef SQLITE_OMIT_VIRTUALTABLE
249     if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
250       assert( pOp->p4.pVtab!=0 );
251       assert( pOp->p4type==P4_VTAB );
252       return 1;
253     }
254 #endif
255   }
256   return 0;
257 }
258 
259 /* This walker callback will compute the union of colFlags flags for all
260 ** referenced columns in a CHECK constraint or generated column expression.
261 */
262 static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){
263   if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
264     assert( pExpr->iColumn < pWalker->u.pTab->nCol );
265     pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
266   }
267   return WRC_Continue;
268 }
269 
270 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
271 /*
272 ** All regular columns for table pTab have been puts into registers
273 ** starting with iRegStore.  The registers that correspond to STORED
274 ** or VIRTUAL columns have not yet been initialized.  This routine goes
275 ** back and computes the values for those columns based on the previously
276 ** computed normal columns.
277 */
278 void sqlite3ComputeGeneratedColumns(
279   Parse *pParse,    /* Parsing context */
280   int iRegStore,    /* Register holding the first column */
281   Table *pTab       /* The table */
282 ){
283   int i;
284   Walker w;
285   Column *pRedo;
286   int eProgress;
287   VdbeOp *pOp;
288 
289   assert( pTab->tabFlags & TF_HasGenerated );
290   testcase( pTab->tabFlags & TF_HasVirtual );
291   testcase( pTab->tabFlags & TF_HasStored );
292 
293   /* Before computing generated columns, first go through and make sure
294   ** that appropriate affinity has been applied to the regular columns
295   */
296   sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
297   if( (pTab->tabFlags & TF_HasStored)!=0 ){
298     pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
299     if( pOp->opcode==OP_Affinity ){
300       /* Change the OP_Affinity argument to '@' (NONE) for all stored
301       ** columns.  '@' is the no-op affinity and those columns have not
302       ** yet been computed. */
303       int ii, jj;
304       char *zP4 = pOp->p4.z;
305       assert( zP4!=0 );
306       assert( pOp->p4type==P4_DYNAMIC );
307       for(ii=jj=0; zP4[jj]; ii++){
308         if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
309           continue;
310         }
311         if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
312           zP4[jj] = SQLITE_AFF_NONE;
313         }
314         jj++;
315       }
316     }else if( pOp->opcode==OP_TypeCheck ){
317       /* If an OP_TypeCheck was generated because the table is STRICT,
318       ** then set the P3 operand to indicate that generated columns should
319       ** not be checked */
320       pOp->p3 = 1;
321     }
322   }
323 
324   /* Because there can be multiple generated columns that refer to one another,
325   ** this is a two-pass algorithm.  On the first pass, mark all generated
326   ** columns as "not available".
327   */
328   for(i=0; i<pTab->nCol; i++){
329     if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
330       testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
331       testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
332       pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
333     }
334   }
335 
336   w.u.pTab = pTab;
337   w.xExprCallback = exprColumnFlagUnion;
338   w.xSelectCallback = 0;
339   w.xSelectCallback2 = 0;
340 
341   /* On the second pass, compute the value of each NOT-AVAILABLE column.
342   ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
343   ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
344   ** they are needed.
345   */
346   pParse->iSelfTab = -iRegStore;
347   do{
348     eProgress = 0;
349     pRedo = 0;
350     for(i=0; i<pTab->nCol; i++){
351       Column *pCol = pTab->aCol + i;
352       if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
353         int x;
354         pCol->colFlags |= COLFLAG_BUSY;
355         w.eCode = 0;
356         sqlite3WalkExpr(&w, sqlite3ColumnExpr(pTab, pCol));
357         pCol->colFlags &= ~COLFLAG_BUSY;
358         if( w.eCode & COLFLAG_NOTAVAIL ){
359           pRedo = pCol;
360           continue;
361         }
362         eProgress = 1;
363         assert( pCol->colFlags & COLFLAG_GENERATED );
364         x = sqlite3TableColumnToStorage(pTab, i) + iRegStore;
365         sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, x);
366         pCol->colFlags &= ~COLFLAG_NOTAVAIL;
367       }
368     }
369   }while( pRedo && eProgress );
370   if( pRedo ){
371     sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
372   }
373   pParse->iSelfTab = 0;
374 }
375 #endif /* SQLITE_OMIT_GENERATED_COLUMNS */
376 
377 
378 #ifndef SQLITE_OMIT_AUTOINCREMENT
379 /*
380 ** Locate or create an AutoincInfo structure associated with table pTab
381 ** which is in database iDb.  Return the register number for the register
382 ** that holds the maximum rowid.  Return zero if pTab is not an AUTOINCREMENT
383 ** table.  (Also return zero when doing a VACUUM since we do not want to
384 ** update the AUTOINCREMENT counters during a VACUUM.)
385 **
386 ** There is at most one AutoincInfo structure per table even if the
387 ** same table is autoincremented multiple times due to inserts within
388 ** triggers.  A new AutoincInfo structure is created if this is the
389 ** first use of table pTab.  On 2nd and subsequent uses, the original
390 ** AutoincInfo structure is used.
391 **
392 ** Four consecutive registers are allocated:
393 **
394 **   (1)  The name of the pTab table.
395 **   (2)  The maximum ROWID of pTab.
396 **   (3)  The rowid in sqlite_sequence of pTab
397 **   (4)  The original value of the max ROWID in pTab, or NULL if none
398 **
399 ** The 2nd register is the one that is returned.  That is all the
400 ** insert routine needs to know about.
401 */
402 static int autoIncBegin(
403   Parse *pParse,      /* Parsing context */
404   int iDb,            /* Index of the database holding pTab */
405   Table *pTab         /* The table we are writing to */
406 ){
407   int memId = 0;      /* Register holding maximum rowid */
408   assert( pParse->db->aDb[iDb].pSchema!=0 );
409   if( (pTab->tabFlags & TF_Autoincrement)!=0
410    && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
411   ){
412     Parse *pToplevel = sqlite3ParseToplevel(pParse);
413     AutoincInfo *pInfo;
414     Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
415 
416     /* Verify that the sqlite_sequence table exists and is an ordinary
417     ** rowid table with exactly two columns.
418     ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
419     if( pSeqTab==0
420      || !HasRowid(pSeqTab)
421      || NEVER(IsVirtual(pSeqTab))
422      || pSeqTab->nCol!=2
423     ){
424       pParse->nErr++;
425       pParse->rc = SQLITE_CORRUPT_SEQUENCE;
426       return 0;
427     }
428 
429     pInfo = pToplevel->pAinc;
430     while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
431     if( pInfo==0 ){
432       pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
433       sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo);
434       testcase( pParse->earlyCleanup );
435       if( pParse->db->mallocFailed ) return 0;
436       pInfo->pNext = pToplevel->pAinc;
437       pToplevel->pAinc = pInfo;
438       pInfo->pTab = pTab;
439       pInfo->iDb = iDb;
440       pToplevel->nMem++;                  /* Register to hold name of table */
441       pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
442       pToplevel->nMem +=2;       /* Rowid in sqlite_sequence + orig max val */
443     }
444     memId = pInfo->regCtr;
445   }
446   return memId;
447 }
448 
449 /*
450 ** This routine generates code that will initialize all of the
451 ** register used by the autoincrement tracker.
452 */
453 void sqlite3AutoincrementBegin(Parse *pParse){
454   AutoincInfo *p;            /* Information about an AUTOINCREMENT */
455   sqlite3 *db = pParse->db;  /* The database connection */
456   Db *pDb;                   /* Database only autoinc table */
457   int memId;                 /* Register holding max rowid */
458   Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
459 
460   /* This routine is never called during trigger-generation.  It is
461   ** only called from the top-level */
462   assert( pParse->pTriggerTab==0 );
463   assert( sqlite3IsToplevel(pParse) );
464 
465   assert( v );   /* We failed long ago if this is not so */
466   for(p = pParse->pAinc; p; p = p->pNext){
467     static const int iLn = VDBE_OFFSET_LINENO(2);
468     static const VdbeOpList autoInc[] = {
469       /* 0  */ {OP_Null,    0,  0, 0},
470       /* 1  */ {OP_Rewind,  0, 10, 0},
471       /* 2  */ {OP_Column,  0,  0, 0},
472       /* 3  */ {OP_Ne,      0,  9, 0},
473       /* 4  */ {OP_Rowid,   0,  0, 0},
474       /* 5  */ {OP_Column,  0,  1, 0},
475       /* 6  */ {OP_AddImm,  0,  0, 0},
476       /* 7  */ {OP_Copy,    0,  0, 0},
477       /* 8  */ {OP_Goto,    0, 11, 0},
478       /* 9  */ {OP_Next,    0,  2, 0},
479       /* 10 */ {OP_Integer, 0,  0, 0},
480       /* 11 */ {OP_Close,   0,  0, 0}
481     };
482     VdbeOp *aOp;
483     pDb = &db->aDb[p->iDb];
484     memId = p->regCtr;
485     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
486     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
487     sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
488     aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
489     if( aOp==0 ) break;
490     aOp[0].p2 = memId;
491     aOp[0].p3 = memId+2;
492     aOp[2].p3 = memId;
493     aOp[3].p1 = memId-1;
494     aOp[3].p3 = memId;
495     aOp[3].p5 = SQLITE_JUMPIFNULL;
496     aOp[4].p2 = memId+1;
497     aOp[5].p3 = memId;
498     aOp[6].p1 = memId;
499     aOp[7].p2 = memId+2;
500     aOp[7].p1 = memId;
501     aOp[10].p2 = memId;
502     if( pParse->nTab==0 ) pParse->nTab = 1;
503   }
504 }
505 
506 /*
507 ** Update the maximum rowid for an autoincrement calculation.
508 **
509 ** This routine should be called when the regRowid register holds a
510 ** new rowid that is about to be inserted.  If that new rowid is
511 ** larger than the maximum rowid in the memId memory cell, then the
512 ** memory cell is updated.
513 */
514 static void autoIncStep(Parse *pParse, int memId, int regRowid){
515   if( memId>0 ){
516     sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
517   }
518 }
519 
520 /*
521 ** This routine generates the code needed to write autoincrement
522 ** maximum rowid values back into the sqlite_sequence register.
523 ** Every statement that might do an INSERT into an autoincrement
524 ** table (either directly or through triggers) needs to call this
525 ** routine just before the "exit" code.
526 */
527 static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
528   AutoincInfo *p;
529   Vdbe *v = pParse->pVdbe;
530   sqlite3 *db = pParse->db;
531 
532   assert( v );
533   for(p = pParse->pAinc; p; p = p->pNext){
534     static const int iLn = VDBE_OFFSET_LINENO(2);
535     static const VdbeOpList autoIncEnd[] = {
536       /* 0 */ {OP_NotNull,     0, 2, 0},
537       /* 1 */ {OP_NewRowid,    0, 0, 0},
538       /* 2 */ {OP_MakeRecord,  0, 2, 0},
539       /* 3 */ {OP_Insert,      0, 0, 0},
540       /* 4 */ {OP_Close,       0, 0, 0}
541     };
542     VdbeOp *aOp;
543     Db *pDb = &db->aDb[p->iDb];
544     int iRec;
545     int memId = p->regCtr;
546 
547     iRec = sqlite3GetTempReg(pParse);
548     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
549     sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
550     VdbeCoverage(v);
551     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
552     aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
553     if( aOp==0 ) break;
554     aOp[0].p1 = memId+1;
555     aOp[1].p2 = memId+1;
556     aOp[2].p1 = memId-1;
557     aOp[2].p3 = iRec;
558     aOp[3].p2 = iRec;
559     aOp[3].p3 = memId+1;
560     aOp[3].p5 = OPFLAG_APPEND;
561     sqlite3ReleaseTempReg(pParse, iRec);
562   }
563 }
564 void sqlite3AutoincrementEnd(Parse *pParse){
565   if( pParse->pAinc ) autoIncrementEnd(pParse);
566 }
567 #else
568 /*
569 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
570 ** above are all no-ops
571 */
572 # define autoIncBegin(A,B,C) (0)
573 # define autoIncStep(A,B,C)
574 #endif /* SQLITE_OMIT_AUTOINCREMENT */
575 
576 
577 /* Forward declaration */
578 static int xferOptimization(
579   Parse *pParse,        /* Parser context */
580   Table *pDest,         /* The table we are inserting into */
581   Select *pSelect,      /* A SELECT statement to use as the data source */
582   int onError,          /* How to handle constraint errors */
583   int iDbDest           /* The database of pDest */
584 );
585 
586 /*
587 ** This routine is called to handle SQL of the following forms:
588 **
589 **    insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
590 **    insert into TABLE (IDLIST) select
591 **    insert into TABLE (IDLIST) default values
592 **
593 ** The IDLIST following the table name is always optional.  If omitted,
594 ** then a list of all (non-hidden) columns for the table is substituted.
595 ** The IDLIST appears in the pColumn parameter.  pColumn is NULL if IDLIST
596 ** is omitted.
597 **
598 ** For the pSelect parameter holds the values to be inserted for the
599 ** first two forms shown above.  A VALUES clause is really just short-hand
600 ** for a SELECT statement that omits the FROM clause and everything else
601 ** that follows.  If the pSelect parameter is NULL, that means that the
602 ** DEFAULT VALUES form of the INSERT statement is intended.
603 **
604 ** The code generated follows one of four templates.  For a simple
605 ** insert with data coming from a single-row VALUES clause, the code executes
606 ** once straight down through.  Pseudo-code follows (we call this
607 ** the "1st template"):
608 **
609 **         open write cursor to <table> and its indices
610 **         put VALUES clause expressions into registers
611 **         write the resulting record into <table>
612 **         cleanup
613 **
614 ** The three remaining templates assume the statement is of the form
615 **
616 **   INSERT INTO <table> SELECT ...
617 **
618 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
619 ** in other words if the SELECT pulls all columns from a single table
620 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
621 ** if <table2> and <table1> are distinct tables but have identical
622 ** schemas, including all the same indices, then a special optimization
623 ** is invoked that copies raw records from <table2> over to <table1>.
624 ** See the xferOptimization() function for the implementation of this
625 ** template.  This is the 2nd template.
626 **
627 **         open a write cursor to <table>
628 **         open read cursor on <table2>
629 **         transfer all records in <table2> over to <table>
630 **         close cursors
631 **         foreach index on <table>
632 **           open a write cursor on the <table> index
633 **           open a read cursor on the corresponding <table2> index
634 **           transfer all records from the read to the write cursors
635 **           close cursors
636 **         end foreach
637 **
638 ** The 3rd template is for when the second template does not apply
639 ** and the SELECT clause does not read from <table> at any time.
640 ** The generated code follows this template:
641 **
642 **         X <- A
643 **         goto B
644 **      A: setup for the SELECT
645 **         loop over the rows in the SELECT
646 **           load values into registers R..R+n
647 **           yield X
648 **         end loop
649 **         cleanup after the SELECT
650 **         end-coroutine X
651 **      B: open write cursor to <table> and its indices
652 **      C: yield X, at EOF goto D
653 **         insert the select result into <table> from R..R+n
654 **         goto C
655 **      D: cleanup
656 **
657 ** The 4th template is used if the insert statement takes its
658 ** values from a SELECT but the data is being inserted into a table
659 ** that is also read as part of the SELECT.  In the third form,
660 ** we have to use an intermediate table to store the results of
661 ** the select.  The template is like this:
662 **
663 **         X <- A
664 **         goto B
665 **      A: setup for the SELECT
666 **         loop over the tables in the SELECT
667 **           load value into register R..R+n
668 **           yield X
669 **         end loop
670 **         cleanup after the SELECT
671 **         end co-routine R
672 **      B: open temp table
673 **      L: yield X, at EOF goto M
674 **         insert row from R..R+n into temp table
675 **         goto L
676 **      M: open write cursor to <table> and its indices
677 **         rewind temp table
678 **      C: loop over rows of intermediate table
679 **           transfer values form intermediate table into <table>
680 **         end loop
681 **      D: cleanup
682 */
683 void sqlite3Insert(
684   Parse *pParse,        /* Parser context */
685   SrcList *pTabList,    /* Name of table into which we are inserting */
686   Select *pSelect,      /* A SELECT statement to use as the data source */
687   IdList *pColumn,      /* Column names corresponding to IDLIST, or NULL. */
688   int onError,          /* How to handle constraint errors */
689   Upsert *pUpsert       /* ON CONFLICT clauses for upsert, or NULL */
690 ){
691   sqlite3 *db;          /* The main database structure */
692   Table *pTab;          /* The table to insert into.  aka TABLE */
693   int i, j;             /* Loop counters */
694   Vdbe *v;              /* Generate code into this virtual machine */
695   Index *pIdx;          /* For looping over indices of the table */
696   int nColumn;          /* Number of columns in the data */
697   int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
698   int iDataCur = 0;     /* VDBE cursor that is the main data repository */
699   int iIdxCur = 0;      /* First index cursor */
700   int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
701   int endOfLoop;        /* Label for the end of the insertion loop */
702   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
703   int addrInsTop = 0;   /* Jump to label "D" */
704   int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
705   SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
706   int iDb;              /* Index of database holding TABLE */
707   u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
708   u8 appendFlag = 0;    /* True if the insert is likely to be an append */
709   u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
710   u8 bIdListInOrder;    /* True if IDLIST is in table order */
711   ExprList *pList = 0;  /* List of VALUES() to be inserted  */
712   int iRegStore;        /* Register in which to store next column */
713 
714   /* Register allocations */
715   int regFromSelect = 0;/* Base register for data coming from SELECT */
716   int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
717   int regRowCount = 0;  /* Memory cell used for the row counter */
718   int regIns;           /* Block of regs holding rowid+data being inserted */
719   int regRowid;         /* registers holding insert rowid */
720   int regData;          /* register holding first column to insert */
721   int *aRegIdx = 0;     /* One register allocated to each index */
722 
723 #ifndef SQLITE_OMIT_TRIGGER
724   int isView;                 /* True if attempting to insert into a view */
725   Trigger *pTrigger;          /* List of triggers on pTab, if required */
726   int tmask;                  /* Mask of trigger times */
727 #endif
728 
729   db = pParse->db;
730   assert( db->pParse==pParse );
731   if( pParse->nErr ){
732     goto insert_cleanup;
733   }
734   assert( db->mallocFailed==0 );
735   dest.iSDParm = 0;  /* Suppress a harmless compiler warning */
736 
737   /* If the Select object is really just a simple VALUES() list with a
738   ** single row (the common case) then keep that one row of values
739   ** and discard the other (unused) parts of the pSelect object
740   */
741   if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
742     pList = pSelect->pEList;
743     pSelect->pEList = 0;
744     sqlite3SelectDelete(db, pSelect);
745     pSelect = 0;
746   }
747 
748   /* Locate the table into which we will be inserting new information.
749   */
750   assert( pTabList->nSrc==1 );
751   pTab = sqlite3SrcListLookup(pParse, pTabList);
752   if( pTab==0 ){
753     goto insert_cleanup;
754   }
755   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
756   assert( iDb<db->nDb );
757   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
758                        db->aDb[iDb].zDbSName) ){
759     goto insert_cleanup;
760   }
761   withoutRowid = !HasRowid(pTab);
762 
763   /* Figure out if we have any triggers and if the table being
764   ** inserted into is a view
765   */
766 #ifndef SQLITE_OMIT_TRIGGER
767   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
768   isView = IsView(pTab);
769 #else
770 # define pTrigger 0
771 # define tmask 0
772 # define isView 0
773 #endif
774 #ifdef SQLITE_OMIT_VIEW
775 # undef isView
776 # define isView 0
777 #endif
778   assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
779 
780 #if TREETRACE_ENABLED
781   if( sqlite3TreeTrace & 0x10000 ){
782     sqlite3TreeViewLine(0, "In sqlite3Insert() at %s:%d", __FILE__, __LINE__);
783     sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
784                           onError, pUpsert, pTrigger);
785   }
786 #endif
787 
788   /* If pTab is really a view, make sure it has been initialized.
789   ** ViewGetColumnNames() is a no-op if pTab is not a view.
790   */
791   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
792     goto insert_cleanup;
793   }
794 
795   /* Cannot insert into a read-only table.
796   */
797   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
798     goto insert_cleanup;
799   }
800 
801   /* Allocate a VDBE
802   */
803   v = sqlite3GetVdbe(pParse);
804   if( v==0 ) goto insert_cleanup;
805   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
806   sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
807 
808 #ifndef SQLITE_OMIT_XFER_OPT
809   /* If the statement is of the form
810   **
811   **       INSERT INTO <table1> SELECT * FROM <table2>;
812   **
813   ** Then special optimizations can be applied that make the transfer
814   ** very fast and which reduce fragmentation of indices.
815   **
816   ** This is the 2nd template.
817   */
818   if( pColumn==0
819    && pSelect!=0
820    && pTrigger==0
821    && xferOptimization(pParse, pTab, pSelect, onError, iDb)
822   ){
823     assert( !pTrigger );
824     assert( pList==0 );
825     goto insert_end;
826   }
827 #endif /* SQLITE_OMIT_XFER_OPT */
828 
829   /* If this is an AUTOINCREMENT table, look up the sequence number in the
830   ** sqlite_sequence table and store it in memory cell regAutoinc.
831   */
832   regAutoinc = autoIncBegin(pParse, iDb, pTab);
833 
834   /* Allocate a block registers to hold the rowid and the values
835   ** for all columns of the new row.
836   */
837   regRowid = regIns = pParse->nMem+1;
838   pParse->nMem += pTab->nCol + 1;
839   if( IsVirtual(pTab) ){
840     regRowid++;
841     pParse->nMem++;
842   }
843   regData = regRowid+1;
844 
845   /* If the INSERT statement included an IDLIST term, then make sure
846   ** all elements of the IDLIST really are columns of the table and
847   ** remember the column indices.
848   **
849   ** If the table has an INTEGER PRIMARY KEY column and that column
850   ** is named in the IDLIST, then record in the ipkColumn variable
851   ** the index into IDLIST of the primary key column.  ipkColumn is
852   ** the index of the primary key as it appears in IDLIST, not as
853   ** is appears in the original table.  (The index of the INTEGER
854   ** PRIMARY KEY in the original table is pTab->iPKey.)  After this
855   ** loop, if ipkColumn==(-1), that means that integer primary key
856   ** is unspecified, and hence the table is either WITHOUT ROWID or
857   ** it will automatically generated an integer primary key.
858   **
859   ** bIdListInOrder is true if the columns in IDLIST are in storage
860   ** order.  This enables an optimization that avoids shuffling the
861   ** columns into storage order.  False negatives are harmless,
862   ** but false positives will cause database corruption.
863   */
864   bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
865   if( pColumn ){
866     assert( pColumn->eU4!=EU4_EXPR );
867     pColumn->eU4 = EU4_IDX;
868     for(i=0; i<pColumn->nId; i++){
869       pColumn->a[i].u4.idx = -1;
870     }
871     for(i=0; i<pColumn->nId; i++){
872       for(j=0; j<pTab->nCol; j++){
873         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
874           pColumn->a[i].u4.idx = j;
875           if( i!=j ) bIdListInOrder = 0;
876           if( j==pTab->iPKey ){
877             ipkColumn = i;  assert( !withoutRowid );
878           }
879 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
880           if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
881             sqlite3ErrorMsg(pParse,
882                "cannot INSERT into generated column \"%s\"",
883                pTab->aCol[j].zCnName);
884             goto insert_cleanup;
885           }
886 #endif
887           break;
888         }
889       }
890       if( j>=pTab->nCol ){
891         if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
892           ipkColumn = i;
893           bIdListInOrder = 0;
894         }else{
895           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
896               pTabList->a, pColumn->a[i].zName);
897           pParse->checkSchema = 1;
898           goto insert_cleanup;
899         }
900       }
901     }
902   }
903 
904   /* Figure out how many columns of data are supplied.  If the data
905   ** is coming from a SELECT statement, then generate a co-routine that
906   ** produces a single row of the SELECT on each invocation.  The
907   ** co-routine is the common header to the 3rd and 4th templates.
908   */
909   if( pSelect ){
910     /* Data is coming from a SELECT or from a multi-row VALUES clause.
911     ** Generate a co-routine to run the SELECT. */
912     int regYield;       /* Register holding co-routine entry-point */
913     int addrTop;        /* Top of the co-routine */
914     int rc;             /* Result code */
915 
916     regYield = ++pParse->nMem;
917     addrTop = sqlite3VdbeCurrentAddr(v) + 1;
918     sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
919     sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
920     dest.iSdst = bIdListInOrder ? regData : 0;
921     dest.nSdst = pTab->nCol;
922     rc = sqlite3Select(pParse, pSelect, &dest);
923     regFromSelect = dest.iSdst;
924     assert( db->pParse==pParse );
925     if( rc || pParse->nErr ) goto insert_cleanup;
926     assert( db->mallocFailed==0 );
927     sqlite3VdbeEndCoroutine(v, regYield);
928     sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
929     assert( pSelect->pEList );
930     nColumn = pSelect->pEList->nExpr;
931 
932     /* Set useTempTable to TRUE if the result of the SELECT statement
933     ** should be written into a temporary table (template 4).  Set to
934     ** FALSE if each output row of the SELECT can be written directly into
935     ** the destination table (template 3).
936     **
937     ** A temp table must be used if the table being updated is also one
938     ** of the tables being read by the SELECT statement.  Also use a
939     ** temp table in the case of row triggers.
940     */
941     if( pTrigger || readsTable(pParse, iDb, pTab) ){
942       useTempTable = 1;
943     }
944 
945     if( useTempTable ){
946       /* Invoke the coroutine to extract information from the SELECT
947       ** and add it to a transient table srcTab.  The code generated
948       ** here is from the 4th template:
949       **
950       **      B: open temp table
951       **      L: yield X, goto M at EOF
952       **         insert row from R..R+n into temp table
953       **         goto L
954       **      M: ...
955       */
956       int regRec;          /* Register to hold packed record */
957       int regTempRowid;    /* Register to hold temp table ROWID */
958       int addrL;           /* Label "L" */
959 
960       srcTab = pParse->nTab++;
961       regRec = sqlite3GetTempReg(pParse);
962       regTempRowid = sqlite3GetTempReg(pParse);
963       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
964       addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
965       sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
966       sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
967       sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
968       sqlite3VdbeGoto(v, addrL);
969       sqlite3VdbeJumpHere(v, addrL);
970       sqlite3ReleaseTempReg(pParse, regRec);
971       sqlite3ReleaseTempReg(pParse, regTempRowid);
972     }
973   }else{
974     /* This is the case if the data for the INSERT is coming from a
975     ** single-row VALUES clause
976     */
977     NameContext sNC;
978     memset(&sNC, 0, sizeof(sNC));
979     sNC.pParse = pParse;
980     srcTab = -1;
981     assert( useTempTable==0 );
982     if( pList ){
983       nColumn = pList->nExpr;
984       if( sqlite3ResolveExprListNames(&sNC, pList) ){
985         goto insert_cleanup;
986       }
987     }else{
988       nColumn = 0;
989     }
990   }
991 
992   /* If there is no IDLIST term but the table has an integer primary
993   ** key, the set the ipkColumn variable to the integer primary key
994   ** column index in the original table definition.
995   */
996   if( pColumn==0 && nColumn>0 ){
997     ipkColumn = pTab->iPKey;
998 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
999     if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
1000       testcase( pTab->tabFlags & TF_HasVirtual );
1001       testcase( pTab->tabFlags & TF_HasStored );
1002       for(i=ipkColumn-1; i>=0; i--){
1003         if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
1004           testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
1005           testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
1006           ipkColumn--;
1007         }
1008       }
1009     }
1010 #endif
1011 
1012     /* Make sure the number of columns in the source data matches the number
1013     ** of columns to be inserted into the table.
1014     */
1015     assert( TF_HasHidden==COLFLAG_HIDDEN );
1016     assert( TF_HasGenerated==COLFLAG_GENERATED );
1017     assert( COLFLAG_NOINSERT==(COLFLAG_GENERATED|COLFLAG_HIDDEN) );
1018     if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
1019       for(i=0; i<pTab->nCol; i++){
1020         if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
1021       }
1022     }
1023     if( nColumn!=(pTab->nCol-nHidden) ){
1024       sqlite3ErrorMsg(pParse,
1025          "table %S has %d columns but %d values were supplied",
1026          pTabList->a, pTab->nCol-nHidden, nColumn);
1027      goto insert_cleanup;
1028     }
1029   }
1030   if( pColumn!=0 && nColumn!=pColumn->nId ){
1031     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
1032     goto insert_cleanup;
1033   }
1034 
1035   /* Initialize the count of rows to be inserted
1036   */
1037   if( (db->flags & SQLITE_CountRows)!=0
1038    && !pParse->nested
1039    && !pParse->pTriggerTab
1040    && !pParse->bReturning
1041   ){
1042     regRowCount = ++pParse->nMem;
1043     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
1044   }
1045 
1046   /* If this is not a view, open the table and and all indices */
1047   if( !isView ){
1048     int nIdx;
1049     nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
1050                                       &iDataCur, &iIdxCur);
1051     aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
1052     if( aRegIdx==0 ){
1053       goto insert_cleanup;
1054     }
1055     for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
1056       assert( pIdx );
1057       aRegIdx[i] = ++pParse->nMem;
1058       pParse->nMem += pIdx->nColumn;
1059     }
1060     aRegIdx[i] = ++pParse->nMem;  /* Register to store the table record */
1061   }
1062 #ifndef SQLITE_OMIT_UPSERT
1063   if( pUpsert ){
1064     Upsert *pNx;
1065     if( IsVirtual(pTab) ){
1066       sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
1067               pTab->zName);
1068       goto insert_cleanup;
1069     }
1070     if( IsView(pTab) ){
1071       sqlite3ErrorMsg(pParse, "cannot UPSERT a view");
1072       goto insert_cleanup;
1073     }
1074     if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
1075       goto insert_cleanup;
1076     }
1077     pTabList->a[0].iCursor = iDataCur;
1078     pNx = pUpsert;
1079     do{
1080       pNx->pUpsertSrc = pTabList;
1081       pNx->regData = regData;
1082       pNx->iDataCur = iDataCur;
1083       pNx->iIdxCur = iIdxCur;
1084       if( pNx->pUpsertTarget ){
1085         if( sqlite3UpsertAnalyzeTarget(pParse, pTabList, pNx) ){
1086           goto insert_cleanup;
1087         }
1088       }
1089       pNx = pNx->pNextUpsert;
1090     }while( pNx!=0 );
1091   }
1092 #endif
1093 
1094 
1095   /* This is the top of the main insertion loop */
1096   if( useTempTable ){
1097     /* This block codes the top of loop only.  The complete loop is the
1098     ** following pseudocode (template 4):
1099     **
1100     **         rewind temp table, if empty goto D
1101     **      C: loop over rows of intermediate table
1102     **           transfer values form intermediate table into <table>
1103     **         end loop
1104     **      D: ...
1105     */
1106     addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
1107     addrCont = sqlite3VdbeCurrentAddr(v);
1108   }else if( pSelect ){
1109     /* This block codes the top of loop only.  The complete loop is the
1110     ** following pseudocode (template 3):
1111     **
1112     **      C: yield X, at EOF goto D
1113     **         insert the select result into <table> from R..R+n
1114     **         goto C
1115     **      D: ...
1116     */
1117     sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
1118     addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
1119     VdbeCoverage(v);
1120     if( ipkColumn>=0 ){
1121       /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
1122       ** SELECT, go ahead and copy the value into the rowid slot now, so that
1123       ** the value does not get overwritten by a NULL at tag-20191021-002. */
1124       sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
1125     }
1126   }
1127 
1128   /* Compute data for ordinary columns of the new entry.  Values
1129   ** are written in storage order into registers starting with regData.
1130   ** Only ordinary columns are computed in this loop. The rowid
1131   ** (if there is one) is computed later and generated columns are
1132   ** computed after the rowid since they might depend on the value
1133   ** of the rowid.
1134   */
1135   nHidden = 0;
1136   iRegStore = regData;  assert( regData==regRowid+1 );
1137   for(i=0; i<pTab->nCol; i++, iRegStore++){
1138     int k;
1139     u32 colFlags;
1140     assert( i>=nHidden );
1141     if( i==pTab->iPKey ){
1142       /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
1143       ** using the rowid. So put a NULL in the IPK slot of the record to avoid
1144       ** using excess space.  The file format definition requires this extra
1145       ** NULL - we cannot optimize further by skipping the column completely */
1146       sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
1147       continue;
1148     }
1149     if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
1150       nHidden++;
1151       if( (colFlags & COLFLAG_VIRTUAL)!=0 ){
1152         /* Virtual columns do not participate in OP_MakeRecord.  So back up
1153         ** iRegStore by one slot to compensate for the iRegStore++ in the
1154         ** outer for() loop */
1155         iRegStore--;
1156         continue;
1157       }else if( (colFlags & COLFLAG_STORED)!=0 ){
1158         /* Stored columns are computed later.  But if there are BEFORE
1159         ** triggers, the slots used for stored columns will be OP_Copy-ed
1160         ** to a second block of registers, so the register needs to be
1161         ** initialized to NULL to avoid an uninitialized register read */
1162         if( tmask & TRIGGER_BEFORE ){
1163           sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
1164         }
1165         continue;
1166       }else if( pColumn==0 ){
1167         /* Hidden columns that are not explicitly named in the INSERT
1168         ** get there default value */
1169         sqlite3ExprCodeFactorable(pParse,
1170             sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
1171             iRegStore);
1172         continue;
1173       }
1174     }
1175     if( pColumn ){
1176       assert( pColumn->eU4==EU4_IDX );
1177       for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
1178       if( j>=pColumn->nId ){
1179         /* A column not named in the insert column list gets its
1180         ** default value */
1181         sqlite3ExprCodeFactorable(pParse,
1182             sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
1183             iRegStore);
1184         continue;
1185       }
1186       k = j;
1187     }else if( nColumn==0 ){
1188       /* This is INSERT INTO ... DEFAULT VALUES.  Load the default value. */
1189       sqlite3ExprCodeFactorable(pParse,
1190           sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
1191           iRegStore);
1192       continue;
1193     }else{
1194       k = i - nHidden;
1195     }
1196 
1197     if( useTempTable ){
1198       sqlite3VdbeAddOp3(v, OP_Column, srcTab, k, iRegStore);
1199     }else if( pSelect ){
1200       if( regFromSelect!=regData ){
1201         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore);
1202       }
1203     }else{
1204       Expr *pX = pList->a[k].pExpr;
1205       int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore);
1206       if( y!=iRegStore ){
1207         sqlite3VdbeAddOp2(v,
1208           ExprHasProperty(pX, EP_Subquery) ? OP_Copy : OP_SCopy, y, iRegStore);
1209       }
1210     }
1211   }
1212 
1213 
1214   /* Run the BEFORE and INSTEAD OF triggers, if there are any
1215   */
1216   endOfLoop = sqlite3VdbeMakeLabel(pParse);
1217   if( tmask & TRIGGER_BEFORE ){
1218     int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
1219 
1220     /* build the NEW.* reference row.  Note that if there is an INTEGER
1221     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
1222     ** translated into a unique ID for the row.  But on a BEFORE trigger,
1223     ** we do not know what the unique ID will be (because the insert has
1224     ** not happened yet) so we substitute a rowid of -1
1225     */
1226     if( ipkColumn<0 ){
1227       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
1228     }else{
1229       int addr1;
1230       assert( !withoutRowid );
1231       if( useTempTable ){
1232         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
1233       }else{
1234         assert( pSelect==0 );  /* Otherwise useTempTable is true */
1235         sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
1236       }
1237       addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
1238       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
1239       sqlite3VdbeJumpHere(v, addr1);
1240       sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
1241     }
1242 
1243     /* Copy the new data already generated. */
1244     assert( pTab->nNVCol>0 );
1245     sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
1246 
1247 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1248     /* Compute the new value for generated columns after all other
1249     ** columns have already been computed.  This must be done after
1250     ** computing the ROWID in case one of the generated columns
1251     ** refers to the ROWID. */
1252     if( pTab->tabFlags & TF_HasGenerated ){
1253       testcase( pTab->tabFlags & TF_HasVirtual );
1254       testcase( pTab->tabFlags & TF_HasStored );
1255       sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab);
1256     }
1257 #endif
1258 
1259     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
1260     ** do not attempt any conversions before assembling the record.
1261     ** If this is a real table, attempt conversions as required by the
1262     ** table column affinities.
1263     */
1264     if( !isView ){
1265       sqlite3TableAffinity(v, pTab, regCols+1);
1266     }
1267 
1268     /* Fire BEFORE or INSTEAD OF triggers */
1269     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
1270         pTab, regCols-pTab->nCol-1, onError, endOfLoop);
1271 
1272     sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
1273   }
1274 
1275   if( !isView ){
1276     if( IsVirtual(pTab) ){
1277       /* The row that the VUpdate opcode will delete: none */
1278       sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
1279     }
1280     if( ipkColumn>=0 ){
1281       /* Compute the new rowid */
1282       if( useTempTable ){
1283         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
1284       }else if( pSelect ){
1285         /* Rowid already initialized at tag-20191021-001 */
1286       }else{
1287         Expr *pIpk = pList->a[ipkColumn].pExpr;
1288         if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
1289           sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
1290           appendFlag = 1;
1291         }else{
1292           sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
1293         }
1294       }
1295       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
1296       ** to generate a unique primary key value.
1297       */
1298       if( !appendFlag ){
1299         int addr1;
1300         if( !IsVirtual(pTab) ){
1301           addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
1302           sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
1303           sqlite3VdbeJumpHere(v, addr1);
1304         }else{
1305           addr1 = sqlite3VdbeCurrentAddr(v);
1306           sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v);
1307         }
1308         sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
1309       }
1310     }else if( IsVirtual(pTab) || withoutRowid ){
1311       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
1312     }else{
1313       sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
1314       appendFlag = 1;
1315     }
1316     autoIncStep(pParse, regAutoinc, regRowid);
1317 
1318 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1319     /* Compute the new value for generated columns after all other
1320     ** columns have already been computed.  This must be done after
1321     ** computing the ROWID in case one of the generated columns
1322     ** is derived from the INTEGER PRIMARY KEY. */
1323     if( pTab->tabFlags & TF_HasGenerated ){
1324       sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab);
1325     }
1326 #endif
1327 
1328     /* Generate code to check constraints and generate index keys and
1329     ** do the insertion.
1330     */
1331 #ifndef SQLITE_OMIT_VIRTUALTABLE
1332     if( IsVirtual(pTab) ){
1333       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
1334       sqlite3VtabMakeWritable(pParse, pTab);
1335       sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
1336       sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
1337       sqlite3MayAbort(pParse);
1338     }else
1339 #endif
1340     {
1341       int isReplace = 0;/* Set to true if constraints may cause a replace */
1342       int bUseSeek;     /* True to use OPFLAG_SEEKRESULT */
1343       sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
1344           regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
1345       );
1346       if( db->flags & SQLITE_ForeignKeys ){
1347         sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
1348       }
1349 
1350       /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
1351       ** constraints or (b) there are no triggers and this table is not a
1352       ** parent table in a foreign key constraint. It is safe to set the
1353       ** flag in the second case as if any REPLACE constraint is hit, an
1354       ** OP_Delete or OP_IdxDelete instruction will be executed on each
1355       ** cursor that is disturbed. And these instructions both clear the
1356       ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
1357       ** functionality.  */
1358       bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(v));
1359       sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
1360           regIns, aRegIdx, 0, appendFlag, bUseSeek
1361       );
1362     }
1363 #ifdef SQLITE_ALLOW_ROWID_IN_VIEW
1364   }else if( pParse->bReturning ){
1365     /* If there is a RETURNING clause, populate the rowid register with
1366     ** constant value -1, in case one or more of the returned expressions
1367     ** refer to the "rowid" of the view.  */
1368     sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
1369 #endif
1370   }
1371 
1372   /* Update the count of rows that are inserted
1373   */
1374   if( regRowCount ){
1375     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
1376   }
1377 
1378   if( pTrigger ){
1379     /* Code AFTER triggers */
1380     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
1381         pTab, regData-2-pTab->nCol, onError, endOfLoop);
1382   }
1383 
1384   /* The bottom of the main insertion loop, if the data source
1385   ** is a SELECT statement.
1386   */
1387   sqlite3VdbeResolveLabel(v, endOfLoop);
1388   if( useTempTable ){
1389     sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
1390     sqlite3VdbeJumpHere(v, addrInsTop);
1391     sqlite3VdbeAddOp1(v, OP_Close, srcTab);
1392   }else if( pSelect ){
1393     sqlite3VdbeGoto(v, addrCont);
1394 #ifdef SQLITE_DEBUG
1395     /* If we are jumping back to an OP_Yield that is preceded by an
1396     ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
1397     ** OP_ReleaseReg will be included in the loop. */
1398     if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
1399       assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
1400       sqlite3VdbeChangeP5(v, 1);
1401     }
1402 #endif
1403     sqlite3VdbeJumpHere(v, addrInsTop);
1404   }
1405 
1406 #ifndef SQLITE_OMIT_XFER_OPT
1407 insert_end:
1408 #endif /* SQLITE_OMIT_XFER_OPT */
1409   /* Update the sqlite_sequence table by storing the content of the
1410   ** maximum rowid counter values recorded while inserting into
1411   ** autoincrement tables.
1412   */
1413   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
1414     sqlite3AutoincrementEnd(pParse);
1415   }
1416 
1417   /*
1418   ** Return the number of rows inserted. If this routine is
1419   ** generating code because of a call to sqlite3NestedParse(), do not
1420   ** invoke the callback function.
1421   */
1422   if( regRowCount ){
1423     sqlite3CodeChangeCount(v, regRowCount, "rows inserted");
1424   }
1425 
1426 insert_cleanup:
1427   sqlite3SrcListDelete(db, pTabList);
1428   sqlite3ExprListDelete(db, pList);
1429   sqlite3UpsertDelete(db, pUpsert);
1430   sqlite3SelectDelete(db, pSelect);
1431   sqlite3IdListDelete(db, pColumn);
1432   if( aRegIdx ) sqlite3DbNNFreeNN(db, aRegIdx);
1433 }
1434 
1435 /* Make sure "isView" and other macros defined above are undefined. Otherwise
1436 ** they may interfere with compilation of other functions in this file
1437 ** (or in another file, if this file becomes part of the amalgamation).  */
1438 #ifdef isView
1439  #undef isView
1440 #endif
1441 #ifdef pTrigger
1442  #undef pTrigger
1443 #endif
1444 #ifdef tmask
1445  #undef tmask
1446 #endif
1447 
1448 /*
1449 ** Meanings of bits in of pWalker->eCode for
1450 ** sqlite3ExprReferencesUpdatedColumn()
1451 */
1452 #define CKCNSTRNT_COLUMN   0x01    /* CHECK constraint uses a changing column */
1453 #define CKCNSTRNT_ROWID    0x02    /* CHECK constraint references the ROWID */
1454 
1455 /* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
1456 *  Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
1457 ** expression node references any of the
1458 ** columns that are being modifed by an UPDATE statement.
1459 */
1460 static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
1461   if( pExpr->op==TK_COLUMN ){
1462     assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
1463     if( pExpr->iColumn>=0 ){
1464       if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
1465         pWalker->eCode |= CKCNSTRNT_COLUMN;
1466       }
1467     }else{
1468       pWalker->eCode |= CKCNSTRNT_ROWID;
1469     }
1470   }
1471   return WRC_Continue;
1472 }
1473 
1474 /*
1475 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed.  The
1476 ** only columns that are modified by the UPDATE are those for which
1477 ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
1478 **
1479 ** Return true if CHECK constraint pExpr uses any of the
1480 ** changing columns (or the rowid if it is changing).  In other words,
1481 ** return true if this CHECK constraint must be validated for
1482 ** the new row in the UPDATE statement.
1483 **
1484 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
1485 ** The operation of this routine is the same - return true if an only if
1486 ** the expression uses one or more of columns identified by the second and
1487 ** third arguments.
1488 */
1489 int sqlite3ExprReferencesUpdatedColumn(
1490   Expr *pExpr,    /* The expression to be checked */
1491   int *aiChng,    /* aiChng[x]>=0 if column x changed by the UPDATE */
1492   int chngRowid   /* True if UPDATE changes the rowid */
1493 ){
1494   Walker w;
1495   memset(&w, 0, sizeof(w));
1496   w.eCode = 0;
1497   w.xExprCallback = checkConstraintExprNode;
1498   w.u.aiCol = aiChng;
1499   sqlite3WalkExpr(&w, pExpr);
1500   if( !chngRowid ){
1501     testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
1502     w.eCode &= ~CKCNSTRNT_ROWID;
1503   }
1504   testcase( w.eCode==0 );
1505   testcase( w.eCode==CKCNSTRNT_COLUMN );
1506   testcase( w.eCode==CKCNSTRNT_ROWID );
1507   testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
1508   return w.eCode!=0;
1509 }
1510 
1511 /*
1512 ** The sqlite3GenerateConstraintChecks() routine usually wants to visit
1513 ** the indexes of a table in the order provided in the Table->pIndex list.
1514 ** However, sometimes (rarely - when there is an upsert) it wants to visit
1515 ** the indexes in a different order.  The following data structures accomplish
1516 ** this.
1517 **
1518 ** The IndexIterator object is used to walk through all of the indexes
1519 ** of a table in either Index.pNext order, or in some other order established
1520 ** by an array of IndexListTerm objects.
1521 */
1522 typedef struct IndexListTerm IndexListTerm;
1523 typedef struct IndexIterator IndexIterator;
1524 struct IndexIterator {
1525   int eType;    /* 0 for Index.pNext list.  1 for an array of IndexListTerm */
1526   int i;        /* Index of the current item from the list */
1527   union {
1528     struct {    /* Use this object for eType==0: A Index.pNext list */
1529       Index *pIdx;   /* The current Index */
1530     } lx;
1531     struct {    /* Use this object for eType==1; Array of IndexListTerm */
1532       int nIdx;               /* Size of the array */
1533       IndexListTerm *aIdx;    /* Array of IndexListTerms */
1534     } ax;
1535   } u;
1536 };
1537 
1538 /* When IndexIterator.eType==1, then each index is an array of instances
1539 ** of the following object
1540 */
1541 struct IndexListTerm {
1542   Index *p;  /* The index */
1543   int ix;    /* Which entry in the original Table.pIndex list is this index*/
1544 };
1545 
1546 /* Return the first index on the list */
1547 static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){
1548   assert( pIter->i==0 );
1549   if( pIter->eType ){
1550     *pIx = pIter->u.ax.aIdx[0].ix;
1551     return pIter->u.ax.aIdx[0].p;
1552   }else{
1553     *pIx = 0;
1554     return pIter->u.lx.pIdx;
1555   }
1556 }
1557 
1558 /* Return the next index from the list.  Return NULL when out of indexes */
1559 static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){
1560   if( pIter->eType ){
1561     int i = ++pIter->i;
1562     if( i>=pIter->u.ax.nIdx ){
1563       *pIx = i;
1564       return 0;
1565     }
1566     *pIx = pIter->u.ax.aIdx[i].ix;
1567     return pIter->u.ax.aIdx[i].p;
1568   }else{
1569     ++(*pIx);
1570     pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
1571     return pIter->u.lx.pIdx;
1572   }
1573 }
1574 
1575 /*
1576 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
1577 ** on table pTab.
1578 **
1579 ** The regNewData parameter is the first register in a range that contains
1580 ** the data to be inserted or the data after the update.  There will be
1581 ** pTab->nCol+1 registers in this range.  The first register (the one
1582 ** that regNewData points to) will contain the new rowid, or NULL in the
1583 ** case of a WITHOUT ROWID table.  The second register in the range will
1584 ** contain the content of the first table column.  The third register will
1585 ** contain the content of the second table column.  And so forth.
1586 **
1587 ** The regOldData parameter is similar to regNewData except that it contains
1588 ** the data prior to an UPDATE rather than afterwards.  regOldData is zero
1589 ** for an INSERT.  This routine can distinguish between UPDATE and INSERT by
1590 ** checking regOldData for zero.
1591 **
1592 ** For an UPDATE, the pkChng boolean is true if the true primary key (the
1593 ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
1594 ** might be modified by the UPDATE.  If pkChng is false, then the key of
1595 ** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
1596 **
1597 ** For an INSERT, the pkChng boolean indicates whether or not the rowid
1598 ** was explicitly specified as part of the INSERT statement.  If pkChng
1599 ** is zero, it means that the either rowid is computed automatically or
1600 ** that the table is a WITHOUT ROWID table and has no rowid.  On an INSERT,
1601 ** pkChng will only be true if the INSERT statement provides an integer
1602 ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
1603 **
1604 ** The code generated by this routine will store new index entries into
1605 ** registers identified by aRegIdx[].  No index entry is created for
1606 ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
1607 ** the same as the order of indices on the linked list of indices
1608 ** at pTab->pIndex.
1609 **
1610 ** (2019-05-07) The generated code also creates a new record for the
1611 ** main table, if pTab is a rowid table, and stores that record in the
1612 ** register identified by aRegIdx[nIdx] - in other words in the first
1613 ** entry of aRegIdx[] past the last index.  It is important that the
1614 ** record be generated during constraint checks to avoid affinity changes
1615 ** to the register content that occur after constraint checks but before
1616 ** the new record is inserted.
1617 **
1618 ** The caller must have already opened writeable cursors on the main
1619 ** table and all applicable indices (that is to say, all indices for which
1620 ** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
1621 ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
1622 ** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
1623 ** for the first index in the pTab->pIndex list.  Cursors for other indices
1624 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
1625 **
1626 ** This routine also generates code to check constraints.  NOT NULL,
1627 ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
1628 ** then the appropriate action is performed.  There are five possible
1629 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
1630 **
1631 **  Constraint type  Action       What Happens
1632 **  ---------------  ----------   ----------------------------------------
1633 **  any              ROLLBACK     The current transaction is rolled back and
1634 **                                sqlite3_step() returns immediately with a
1635 **                                return code of SQLITE_CONSTRAINT.
1636 **
1637 **  any              ABORT        Back out changes from the current command
1638 **                                only (do not do a complete rollback) then
1639 **                                cause sqlite3_step() to return immediately
1640 **                                with SQLITE_CONSTRAINT.
1641 **
1642 **  any              FAIL         Sqlite3_step() returns immediately with a
1643 **                                return code of SQLITE_CONSTRAINT.  The
1644 **                                transaction is not rolled back and any
1645 **                                changes to prior rows are retained.
1646 **
1647 **  any              IGNORE       The attempt in insert or update the current
1648 **                                row is skipped, without throwing an error.
1649 **                                Processing continues with the next row.
1650 **                                (There is an immediate jump to ignoreDest.)
1651 **
1652 **  NOT NULL         REPLACE      The NULL value is replace by the default
1653 **                                value for that column.  If the default value
1654 **                                is NULL, the action is the same as ABORT.
1655 **
1656 **  UNIQUE           REPLACE      The other row that conflicts with the row
1657 **                                being inserted is removed.
1658 **
1659 **  CHECK            REPLACE      Illegal.  The results in an exception.
1660 **
1661 ** Which action to take is determined by the overrideError parameter.
1662 ** Or if overrideError==OE_Default, then the pParse->onError parameter
1663 ** is used.  Or if pParse->onError==OE_Default then the onError value
1664 ** for the constraint is used.
1665 */
1666 void sqlite3GenerateConstraintChecks(
1667   Parse *pParse,       /* The parser context */
1668   Table *pTab,         /* The table being inserted or updated */
1669   int *aRegIdx,        /* Use register aRegIdx[i] for index i.  0 for unused */
1670   int iDataCur,        /* Canonical data cursor (main table or PK index) */
1671   int iIdxCur,         /* First index cursor */
1672   int regNewData,      /* First register in a range holding values to insert */
1673   int regOldData,      /* Previous content.  0 for INSERTs */
1674   u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
1675   u8 overrideError,    /* Override onError to this if not OE_Default */
1676   int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
1677   int *pbMayReplace,   /* OUT: Set to true if constraint may cause a replace */
1678   int *aiChng,         /* column i is unchanged if aiChng[i]<0 */
1679   Upsert *pUpsert      /* ON CONFLICT clauses, if any.  NULL otherwise */
1680 ){
1681   Vdbe *v;             /* VDBE under constrution */
1682   Index *pIdx;         /* Pointer to one of the indices */
1683   Index *pPk = 0;      /* The PRIMARY KEY index for WITHOUT ROWID tables */
1684   sqlite3 *db;         /* Database connection */
1685   int i;               /* loop counter */
1686   int ix;              /* Index loop counter */
1687   int nCol;            /* Number of columns */
1688   int onError;         /* Conflict resolution strategy */
1689   int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
1690   int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
1691   Upsert *pUpsertClause = 0;  /* The specific ON CONFLICT clause for pIdx */
1692   u8 isUpdate;           /* True if this is an UPDATE operation */
1693   u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
1694   int upsertIpkReturn = 0; /* Address of Goto at end of IPK uniqueness check */
1695   int upsertIpkDelay = 0;  /* Address of Goto to bypass initial IPK check */
1696   int ipkTop = 0;        /* Top of the IPK uniqueness check */
1697   int ipkBottom = 0;     /* OP_Goto at the end of the IPK uniqueness check */
1698   /* Variables associated with retesting uniqueness constraints after
1699   ** replace triggers fire have run */
1700   int regTrigCnt;       /* Register used to count replace trigger invocations */
1701   int addrRecheck = 0;  /* Jump here to recheck all uniqueness constraints */
1702   int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
1703   Trigger *pTrigger;    /* List of DELETE triggers on the table pTab */
1704   int nReplaceTrig = 0; /* Number of replace triggers coded */
1705   IndexIterator sIdxIter;  /* Index iterator */
1706 
1707   isUpdate = regOldData!=0;
1708   db = pParse->db;
1709   v = pParse->pVdbe;
1710   assert( v!=0 );
1711   assert( !IsView(pTab) );  /* This table is not a VIEW */
1712   nCol = pTab->nCol;
1713 
1714   /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
1715   ** normal rowid tables.  nPkField is the number of key fields in the
1716   ** pPk index or 1 for a rowid table.  In other words, nPkField is the
1717   ** number of fields in the true primary key of the table. */
1718   if( HasRowid(pTab) ){
1719     pPk = 0;
1720     nPkField = 1;
1721   }else{
1722     pPk = sqlite3PrimaryKeyIndex(pTab);
1723     nPkField = pPk->nKeyCol;
1724   }
1725 
1726   /* Record that this module has started */
1727   VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
1728                      iDataCur, iIdxCur, regNewData, regOldData, pkChng));
1729 
1730   /* Test all NOT NULL constraints.
1731   */
1732   if( pTab->tabFlags & TF_HasNotNull ){
1733     int b2ndPass = 0;         /* True if currently running 2nd pass */
1734     int nSeenReplace = 0;     /* Number of ON CONFLICT REPLACE operations */
1735     int nGenerated = 0;       /* Number of generated columns with NOT NULL */
1736     while(1){  /* Make 2 passes over columns. Exit loop via "break" */
1737       for(i=0; i<nCol; i++){
1738         int iReg;                        /* Register holding column value */
1739         Column *pCol = &pTab->aCol[i];   /* The column to check for NOT NULL */
1740         int isGenerated;                 /* non-zero if column is generated */
1741         onError = pCol->notNull;
1742         if( onError==OE_None ) continue; /* No NOT NULL on this column */
1743         if( i==pTab->iPKey ){
1744           continue;        /* ROWID is never NULL */
1745         }
1746         isGenerated = pCol->colFlags & COLFLAG_GENERATED;
1747         if( isGenerated && !b2ndPass ){
1748           nGenerated++;
1749           continue;        /* Generated columns processed on 2nd pass */
1750         }
1751         if( aiChng && aiChng[i]<0 && !isGenerated ){
1752           /* Do not check NOT NULL on columns that do not change */
1753           continue;
1754         }
1755         if( overrideError!=OE_Default ){
1756           onError = overrideError;
1757         }else if( onError==OE_Default ){
1758           onError = OE_Abort;
1759         }
1760         if( onError==OE_Replace ){
1761           if( b2ndPass        /* REPLACE becomes ABORT on the 2nd pass */
1762            || pCol->iDflt==0  /* REPLACE is ABORT if no DEFAULT value */
1763           ){
1764             testcase( pCol->colFlags & COLFLAG_VIRTUAL );
1765             testcase( pCol->colFlags & COLFLAG_STORED );
1766             testcase( pCol->colFlags & COLFLAG_GENERATED );
1767             onError = OE_Abort;
1768           }else{
1769             assert( !isGenerated );
1770           }
1771         }else if( b2ndPass && !isGenerated ){
1772           continue;
1773         }
1774         assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1775             || onError==OE_Ignore || onError==OE_Replace );
1776         testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
1777         iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1;
1778         switch( onError ){
1779           case OE_Replace: {
1780             int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg);
1781             VdbeCoverage(v);
1782             assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
1783             nSeenReplace++;
1784             sqlite3ExprCodeCopy(pParse,
1785                sqlite3ColumnExpr(pTab, pCol), iReg);
1786             sqlite3VdbeJumpHere(v, addr1);
1787             break;
1788           }
1789           case OE_Abort:
1790             sqlite3MayAbort(pParse);
1791             /* no break */ deliberate_fall_through
1792           case OE_Rollback:
1793           case OE_Fail: {
1794             char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
1795                                         pCol->zCnName);
1796             sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL,
1797                               onError, iReg);
1798             sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
1799             sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
1800             VdbeCoverage(v);
1801             break;
1802           }
1803           default: {
1804             assert( onError==OE_Ignore );
1805             sqlite3VdbeAddOp2(v, OP_IsNull, iReg, ignoreDest);
1806             VdbeCoverage(v);
1807             break;
1808           }
1809         } /* end switch(onError) */
1810       } /* end loop i over columns */
1811       if( nGenerated==0 && nSeenReplace==0 ){
1812         /* If there are no generated columns with NOT NULL constraints
1813         ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
1814         ** pass is sufficient */
1815         break;
1816       }
1817       if( b2ndPass ) break;  /* Never need more than 2 passes */
1818       b2ndPass = 1;
1819 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1820       if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
1821         /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
1822         ** first pass, recomputed values for all generated columns, as
1823         ** those values might depend on columns affected by the REPLACE.
1824         */
1825         sqlite3ComputeGeneratedColumns(pParse, regNewData+1, pTab);
1826       }
1827 #endif
1828     } /* end of 2-pass loop */
1829   } /* end if( has-not-null-constraints ) */
1830 
1831   /* Test all CHECK constraints
1832   */
1833 #ifndef SQLITE_OMIT_CHECK
1834   if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
1835     ExprList *pCheck = pTab->pCheck;
1836     pParse->iSelfTab = -(regNewData+1);
1837     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
1838     for(i=0; i<pCheck->nExpr; i++){
1839       int allOk;
1840       Expr *pCopy;
1841       Expr *pExpr = pCheck->a[i].pExpr;
1842       if( aiChng
1843        && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
1844       ){
1845         /* The check constraints do not reference any of the columns being
1846         ** updated so there is no point it verifying the check constraint */
1847         continue;
1848       }
1849       if( bAffinityDone==0 ){
1850         sqlite3TableAffinity(v, pTab, regNewData+1);
1851         bAffinityDone = 1;
1852       }
1853       allOk = sqlite3VdbeMakeLabel(pParse);
1854       sqlite3VdbeVerifyAbortable(v, onError);
1855       pCopy = sqlite3ExprDup(db, pExpr, 0);
1856       if( !db->mallocFailed ){
1857         sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL);
1858       }
1859       sqlite3ExprDelete(db, pCopy);
1860       if( onError==OE_Ignore ){
1861         sqlite3VdbeGoto(v, ignoreDest);
1862       }else{
1863         char *zName = pCheck->a[i].zEName;
1864         assert( zName!=0 || pParse->db->mallocFailed );
1865         if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
1866         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
1867                               onError, zName, P4_TRANSIENT,
1868                               P5_ConstraintCheck);
1869       }
1870       sqlite3VdbeResolveLabel(v, allOk);
1871     }
1872     pParse->iSelfTab = 0;
1873   }
1874 #endif /* !defined(SQLITE_OMIT_CHECK) */
1875 
1876   /* UNIQUE and PRIMARY KEY constraints should be handled in the following
1877   ** order:
1878   **
1879   **   (1)  OE_Update
1880   **   (2)  OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
1881   **   (3)  OE_Replace
1882   **
1883   ** OE_Fail and OE_Ignore must happen before any changes are made.
1884   ** OE_Update guarantees that only a single row will change, so it
1885   ** must happen before OE_Replace.  Technically, OE_Abort and OE_Rollback
1886   ** could happen in any order, but they are grouped up front for
1887   ** convenience.
1888   **
1889   ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
1890   ** The order of constraints used to have OE_Update as (2) and OE_Abort
1891   ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
1892   ** constraint before any others, so it had to be moved.
1893   **
1894   ** Constraint checking code is generated in this order:
1895   **   (A)  The rowid constraint
1896   **   (B)  Unique index constraints that do not have OE_Replace as their
1897   **        default conflict resolution strategy
1898   **   (C)  Unique index that do use OE_Replace by default.
1899   **
1900   ** The ordering of (2) and (3) is accomplished by making sure the linked
1901   ** list of indexes attached to a table puts all OE_Replace indexes last
1902   ** in the list.  See sqlite3CreateIndex() for where that happens.
1903   */
1904   sIdxIter.eType = 0;
1905   sIdxIter.i = 0;
1906   sIdxIter.u.ax.aIdx = 0;  /* Silence harmless compiler warning */
1907   sIdxIter.u.lx.pIdx = pTab->pIndex;
1908   if( pUpsert ){
1909     if( pUpsert->pUpsertTarget==0 ){
1910       /* There is just on ON CONFLICT clause and it has no constraint-target */
1911       assert( pUpsert->pNextUpsert==0 );
1912       if( pUpsert->isDoUpdate==0 ){
1913         /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
1914         ** Make all unique constraint resolution be OE_Ignore */
1915         overrideError = OE_Ignore;
1916         pUpsert = 0;
1917       }else{
1918         /* A single ON CONFLICT DO UPDATE.  Make all resolutions OE_Update */
1919         overrideError = OE_Update;
1920       }
1921     }else if( pTab->pIndex!=0 ){
1922       /* Otherwise, we'll need to run the IndexListTerm array version of the
1923       ** iterator to ensure that all of the ON CONFLICT conditions are
1924       ** checked first and in order. */
1925       int nIdx, jj;
1926       u64 nByte;
1927       Upsert *pTerm;
1928       u8 *bUsed;
1929       for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
1930          assert( aRegIdx[nIdx]>0 );
1931       }
1932       sIdxIter.eType = 1;
1933       sIdxIter.u.ax.nIdx = nIdx;
1934       nByte = (sizeof(IndexListTerm)+1)*nIdx + nIdx;
1935       sIdxIter.u.ax.aIdx = sqlite3DbMallocZero(db, nByte);
1936       if( sIdxIter.u.ax.aIdx==0 ) return; /* OOM */
1937       bUsed = (u8*)&sIdxIter.u.ax.aIdx[nIdx];
1938       pUpsert->pToFree = sIdxIter.u.ax.aIdx;
1939       for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
1940         if( pTerm->pUpsertTarget==0 ) break;
1941         if( pTerm->pUpsertIdx==0 ) continue;  /* Skip ON CONFLICT for the IPK */
1942         jj = 0;
1943         pIdx = pTab->pIndex;
1944         while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
1945            pIdx = pIdx->pNext;
1946            jj++;
1947         }
1948         if( bUsed[jj] ) continue; /* Duplicate ON CONFLICT clause ignored */
1949         bUsed[jj] = 1;
1950         sIdxIter.u.ax.aIdx[i].p = pIdx;
1951         sIdxIter.u.ax.aIdx[i].ix = jj;
1952         i++;
1953       }
1954       for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
1955         if( bUsed[jj] ) continue;
1956         sIdxIter.u.ax.aIdx[i].p = pIdx;
1957         sIdxIter.u.ax.aIdx[i].ix = jj;
1958         i++;
1959       }
1960       assert( i==nIdx );
1961     }
1962   }
1963 
1964   /* Determine if it is possible that triggers (either explicitly coded
1965   ** triggers or FK resolution actions) might run as a result of deletes
1966   ** that happen when OE_Replace conflict resolution occurs. (Call these
1967   ** "replace triggers".)  If any replace triggers run, we will need to
1968   ** recheck all of the uniqueness constraints after they have all run.
1969   ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
1970   **
1971   ** If replace triggers are a possibility, then
1972   **
1973   **   (1) Allocate register regTrigCnt and initialize it to zero.
1974   **       That register will count the number of replace triggers that
1975   **       fire.  Constraint recheck only occurs if the number is positive.
1976   **   (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
1977   **   (3) Initialize addrRecheck and lblRecheckOk
1978   **
1979   ** The uniqueness rechecking code will create a series of tests to run
1980   ** in a second pass.  The addrRecheck and lblRecheckOk variables are
1981   ** used to link together these tests which are separated from each other
1982   ** in the generate bytecode.
1983   */
1984   if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
1985     /* There are not DELETE triggers nor FK constraints.  No constraint
1986     ** rechecks are needed. */
1987     pTrigger = 0;
1988     regTrigCnt = 0;
1989   }else{
1990     if( db->flags&SQLITE_RecTriggers ){
1991       pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
1992       regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0);
1993     }else{
1994       pTrigger = 0;
1995       regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0);
1996     }
1997     if( regTrigCnt ){
1998       /* Replace triggers might exist.  Allocate the counter and
1999       ** initialize it to zero. */
2000       regTrigCnt = ++pParse->nMem;
2001       sqlite3VdbeAddOp2(v, OP_Integer, 0, regTrigCnt);
2002       VdbeComment((v, "trigger count"));
2003       lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
2004       addrRecheck = lblRecheckOk;
2005     }
2006   }
2007 
2008   /* If rowid is changing, make sure the new rowid does not previously
2009   ** exist in the table.
2010   */
2011   if( pkChng && pPk==0 ){
2012     int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
2013 
2014     /* Figure out what action to take in case of a rowid collision */
2015     onError = pTab->keyConf;
2016     if( overrideError!=OE_Default ){
2017       onError = overrideError;
2018     }else if( onError==OE_Default ){
2019       onError = OE_Abort;
2020     }
2021 
2022     /* figure out whether or not upsert applies in this case */
2023     if( pUpsert ){
2024       pUpsertClause = sqlite3UpsertOfIndex(pUpsert,0);
2025       if( pUpsertClause!=0 ){
2026         if( pUpsertClause->isDoUpdate==0 ){
2027           onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
2028         }else{
2029           onError = OE_Update;  /* DO UPDATE */
2030         }
2031       }
2032       if( pUpsertClause!=pUpsert ){
2033         /* The first ON CONFLICT clause has a conflict target other than
2034         ** the IPK.  We have to jump ahead to that first ON CONFLICT clause
2035         ** and then come back here and deal with the IPK afterwards */
2036         upsertIpkDelay = sqlite3VdbeAddOp0(v, OP_Goto);
2037       }
2038     }
2039 
2040     /* If the response to a rowid conflict is REPLACE but the response
2041     ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
2042     ** to defer the running of the rowid conflict checking until after
2043     ** the UNIQUE constraints have run.
2044     */
2045     if( onError==OE_Replace      /* IPK rule is REPLACE */
2046      && onError!=overrideError   /* Rules for other constraints are different */
2047      && pTab->pIndex             /* There exist other constraints */
2048      && !upsertIpkDelay          /* IPK check already deferred by UPSERT */
2049     ){
2050       ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
2051       VdbeComment((v, "defer IPK REPLACE until last"));
2052     }
2053 
2054     if( isUpdate ){
2055       /* pkChng!=0 does not mean that the rowid has changed, only that
2056       ** it might have changed.  Skip the conflict logic below if the rowid
2057       ** is unchanged. */
2058       sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
2059       sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
2060       VdbeCoverage(v);
2061     }
2062 
2063     /* Check to see if the new rowid already exists in the table.  Skip
2064     ** the following conflict logic if it does not. */
2065     VdbeNoopComment((v, "uniqueness check for ROWID"));
2066     sqlite3VdbeVerifyAbortable(v, onError);
2067     sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
2068     VdbeCoverage(v);
2069 
2070     switch( onError ){
2071       default: {
2072         onError = OE_Abort;
2073         /* no break */ deliberate_fall_through
2074       }
2075       case OE_Rollback:
2076       case OE_Abort:
2077       case OE_Fail: {
2078         testcase( onError==OE_Rollback );
2079         testcase( onError==OE_Abort );
2080         testcase( onError==OE_Fail );
2081         sqlite3RowidConstraint(pParse, onError, pTab);
2082         break;
2083       }
2084       case OE_Replace: {
2085         /* If there are DELETE triggers on this table and the
2086         ** recursive-triggers flag is set, call GenerateRowDelete() to
2087         ** remove the conflicting row from the table. This will fire
2088         ** the triggers and remove both the table and index b-tree entries.
2089         **
2090         ** Otherwise, if there are no triggers or the recursive-triggers
2091         ** flag is not set, but the table has one or more indexes, call
2092         ** GenerateRowIndexDelete(). This removes the index b-tree entries
2093         ** only. The table b-tree entry will be replaced by the new entry
2094         ** when it is inserted.
2095         **
2096         ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
2097         ** also invoke MultiWrite() to indicate that this VDBE may require
2098         ** statement rollback (if the statement is aborted after the delete
2099         ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
2100         ** but being more selective here allows statements like:
2101         **
2102         **   REPLACE INTO t(rowid) VALUES($newrowid)
2103         **
2104         ** to run without a statement journal if there are no indexes on the
2105         ** table.
2106         */
2107         if( regTrigCnt ){
2108           sqlite3MultiWrite(pParse);
2109           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
2110                                    regNewData, 1, 0, OE_Replace, 1, -1);
2111           sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */
2112           nReplaceTrig++;
2113         }else{
2114 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2115           assert( HasRowid(pTab) );
2116           /* This OP_Delete opcode fires the pre-update-hook only. It does
2117           ** not modify the b-tree. It is more efficient to let the coming
2118           ** OP_Insert replace the existing entry than it is to delete the
2119           ** existing entry and then insert a new one. */
2120           sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
2121           sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
2122 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
2123           if( pTab->pIndex ){
2124             sqlite3MultiWrite(pParse);
2125             sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
2126           }
2127         }
2128         seenReplace = 1;
2129         break;
2130       }
2131 #ifndef SQLITE_OMIT_UPSERT
2132       case OE_Update: {
2133         sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
2134         /* no break */ deliberate_fall_through
2135       }
2136 #endif
2137       case OE_Ignore: {
2138         testcase( onError==OE_Ignore );
2139         sqlite3VdbeGoto(v, ignoreDest);
2140         break;
2141       }
2142     }
2143     sqlite3VdbeResolveLabel(v, addrRowidOk);
2144     if( pUpsert && pUpsertClause!=pUpsert ){
2145       upsertIpkReturn = sqlite3VdbeAddOp0(v, OP_Goto);
2146     }else if( ipkTop ){
2147       ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
2148       sqlite3VdbeJumpHere(v, ipkTop-1);
2149     }
2150   }
2151 
2152   /* Test all UNIQUE constraints by creating entries for each UNIQUE
2153   ** index and making sure that duplicate entries do not already exist.
2154   ** Compute the revised record entries for indices as we go.
2155   **
2156   ** This loop also handles the case of the PRIMARY KEY index for a
2157   ** WITHOUT ROWID table.
2158   */
2159   for(pIdx = indexIteratorFirst(&sIdxIter, &ix);
2160       pIdx;
2161       pIdx = indexIteratorNext(&sIdxIter, &ix)
2162   ){
2163     int regIdx;          /* Range of registers hold conent for pIdx */
2164     int regR;            /* Range of registers holding conflicting PK */
2165     int iThisCur;        /* Cursor for this UNIQUE index */
2166     int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
2167     int addrConflictCk;  /* First opcode in the conflict check logic */
2168 
2169     if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
2170     if( pUpsert ){
2171       pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx);
2172       if( upsertIpkDelay && pUpsertClause==pUpsert ){
2173         sqlite3VdbeJumpHere(v, upsertIpkDelay);
2174       }
2175     }
2176     addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
2177     if( bAffinityDone==0 ){
2178       sqlite3TableAffinity(v, pTab, regNewData+1);
2179       bAffinityDone = 1;
2180     }
2181     VdbeNoopComment((v, "prep index %s", pIdx->zName));
2182     iThisCur = iIdxCur+ix;
2183 
2184 
2185     /* Skip partial indices for which the WHERE clause is not true */
2186     if( pIdx->pPartIdxWhere ){
2187       sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
2188       pParse->iSelfTab = -(regNewData+1);
2189       sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
2190                             SQLITE_JUMPIFNULL);
2191       pParse->iSelfTab = 0;
2192     }
2193 
2194     /* Create a record for this index entry as it should appear after
2195     ** the insert or update.  Store that record in the aRegIdx[ix] register
2196     */
2197     regIdx = aRegIdx[ix]+1;
2198     for(i=0; i<pIdx->nColumn; i++){
2199       int iField = pIdx->aiColumn[i];
2200       int x;
2201       if( iField==XN_EXPR ){
2202         pParse->iSelfTab = -(regNewData+1);
2203         sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
2204         pParse->iSelfTab = 0;
2205         VdbeComment((v, "%s column %d", pIdx->zName, i));
2206       }else if( iField==XN_ROWID || iField==pTab->iPKey ){
2207         x = regNewData;
2208         sqlite3VdbeAddOp2(v, OP_IntCopy, x, regIdx+i);
2209         VdbeComment((v, "rowid"));
2210       }else{
2211         testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField );
2212         x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1;
2213         sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
2214         VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
2215       }
2216     }
2217     sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
2218     VdbeComment((v, "for %s", pIdx->zName));
2219 #ifdef SQLITE_ENABLE_NULL_TRIM
2220     if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
2221       sqlite3SetMakeRecordP5(v, pIdx->pTable);
2222     }
2223 #endif
2224     sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
2225 
2226     /* In an UPDATE operation, if this index is the PRIMARY KEY index
2227     ** of a WITHOUT ROWID table and there has been no change the
2228     ** primary key, then no collision is possible.  The collision detection
2229     ** logic below can all be skipped. */
2230     if( isUpdate && pPk==pIdx && pkChng==0 ){
2231       sqlite3VdbeResolveLabel(v, addrUniqueOk);
2232       continue;
2233     }
2234 
2235     /* Find out what action to take in case there is a uniqueness conflict */
2236     onError = pIdx->onError;
2237     if( onError==OE_None ){
2238       sqlite3VdbeResolveLabel(v, addrUniqueOk);
2239       continue;  /* pIdx is not a UNIQUE index */
2240     }
2241     if( overrideError!=OE_Default ){
2242       onError = overrideError;
2243     }else if( onError==OE_Default ){
2244       onError = OE_Abort;
2245     }
2246 
2247     /* Figure out if the upsert clause applies to this index */
2248     if( pUpsertClause ){
2249       if( pUpsertClause->isDoUpdate==0 ){
2250         onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
2251       }else{
2252         onError = OE_Update;  /* DO UPDATE */
2253       }
2254     }
2255 
2256     /* Collision detection may be omitted if all of the following are true:
2257     **   (1) The conflict resolution algorithm is REPLACE
2258     **   (2) The table is a WITHOUT ROWID table
2259     **   (3) There are no secondary indexes on the table
2260     **   (4) No delete triggers need to be fired if there is a conflict
2261     **   (5) No FK constraint counters need to be updated if a conflict occurs.
2262     **
2263     ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
2264     ** must be explicitly deleted in order to ensure any pre-update hook
2265     ** is invoked.  */
2266     assert( IsOrdinaryTable(pTab) );
2267 #ifndef SQLITE_ENABLE_PREUPDATE_HOOK
2268     if( (ix==0 && pIdx->pNext==0)                   /* Condition 3 */
2269      && pPk==pIdx                                   /* Condition 2 */
2270      && onError==OE_Replace                         /* Condition 1 */
2271      && ( 0==(db->flags&SQLITE_RecTriggers) ||      /* Condition 4 */
2272           0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
2273      && ( 0==(db->flags&SQLITE_ForeignKeys) ||      /* Condition 5 */
2274          (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
2275     ){
2276       sqlite3VdbeResolveLabel(v, addrUniqueOk);
2277       continue;
2278     }
2279 #endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
2280 
2281     /* Check to see if the new index entry will be unique */
2282     sqlite3VdbeVerifyAbortable(v, onError);
2283     addrConflictCk =
2284       sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
2285                            regIdx, pIdx->nKeyCol); VdbeCoverage(v);
2286 
2287     /* Generate code to handle collisions */
2288     regR = pIdx==pPk ? regIdx : sqlite3GetTempRange(pParse, nPkField);
2289     if( isUpdate || onError==OE_Replace ){
2290       if( HasRowid(pTab) ){
2291         sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
2292         /* Conflict only if the rowid of the existing index entry
2293         ** is different from old-rowid */
2294         if( isUpdate ){
2295           sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
2296           sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
2297           VdbeCoverage(v);
2298         }
2299       }else{
2300         int x;
2301         /* Extract the PRIMARY KEY from the end of the index entry and
2302         ** store it in registers regR..regR+nPk-1 */
2303         if( pIdx!=pPk ){
2304           for(i=0; i<pPk->nKeyCol; i++){
2305             assert( pPk->aiColumn[i]>=0 );
2306             x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
2307             sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
2308             VdbeComment((v, "%s.%s", pTab->zName,
2309                          pTab->aCol[pPk->aiColumn[i]].zCnName));
2310           }
2311         }
2312         if( isUpdate ){
2313           /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
2314           ** table, only conflict if the new PRIMARY KEY values are actually
2315           ** different from the old.  See TH3 withoutrowid04.test.
2316           **
2317           ** For a UNIQUE index, only conflict if the PRIMARY KEY values
2318           ** of the matched index row are different from the original PRIMARY
2319           ** KEY values of this row before the update.  */
2320           int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
2321           int op = OP_Ne;
2322           int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
2323 
2324           for(i=0; i<pPk->nKeyCol; i++){
2325             char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
2326             x = pPk->aiColumn[i];
2327             assert( x>=0 );
2328             if( i==(pPk->nKeyCol-1) ){
2329               addrJump = addrUniqueOk;
2330               op = OP_Eq;
2331             }
2332             x = sqlite3TableColumnToStorage(pTab, x);
2333             sqlite3VdbeAddOp4(v, op,
2334                 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
2335             );
2336             sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
2337             VdbeCoverageIf(v, op==OP_Eq);
2338             VdbeCoverageIf(v, op==OP_Ne);
2339           }
2340         }
2341       }
2342     }
2343 
2344     /* Generate code that executes if the new index entry is not unique */
2345     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
2346         || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
2347     switch( onError ){
2348       case OE_Rollback:
2349       case OE_Abort:
2350       case OE_Fail: {
2351         testcase( onError==OE_Rollback );
2352         testcase( onError==OE_Abort );
2353         testcase( onError==OE_Fail );
2354         sqlite3UniqueConstraint(pParse, onError, pIdx);
2355         break;
2356       }
2357 #ifndef SQLITE_OMIT_UPSERT
2358       case OE_Update: {
2359         sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
2360         /* no break */ deliberate_fall_through
2361       }
2362 #endif
2363       case OE_Ignore: {
2364         testcase( onError==OE_Ignore );
2365         sqlite3VdbeGoto(v, ignoreDest);
2366         break;
2367       }
2368       default: {
2369         int nConflictCk;   /* Number of opcodes in conflict check logic */
2370 
2371         assert( onError==OE_Replace );
2372         nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
2373         assert( nConflictCk>0 || db->mallocFailed );
2374         testcase( nConflictCk<=0 );
2375         testcase( nConflictCk>1 );
2376         if( regTrigCnt ){
2377           sqlite3MultiWrite(pParse);
2378           nReplaceTrig++;
2379         }
2380         if( pTrigger && isUpdate ){
2381           sqlite3VdbeAddOp1(v, OP_CursorLock, iDataCur);
2382         }
2383         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
2384             regR, nPkField, 0, OE_Replace,
2385             (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
2386         if( pTrigger && isUpdate ){
2387           sqlite3VdbeAddOp1(v, OP_CursorUnlock, iDataCur);
2388         }
2389         if( regTrigCnt ){
2390           int addrBypass;  /* Jump destination to bypass recheck logic */
2391 
2392           sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */
2393           addrBypass = sqlite3VdbeAddOp0(v, OP_Goto);  /* Bypass recheck */
2394           VdbeComment((v, "bypass recheck"));
2395 
2396           /* Here we insert code that will be invoked after all constraint
2397           ** checks have run, if and only if one or more replace triggers
2398           ** fired. */
2399           sqlite3VdbeResolveLabel(v, lblRecheckOk);
2400           lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
2401           if( pIdx->pPartIdxWhere ){
2402             /* Bypass the recheck if this partial index is not defined
2403             ** for the current row */
2404             sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
2405             VdbeCoverage(v);
2406           }
2407           /* Copy the constraint check code from above, except change
2408           ** the constraint-ok jump destination to be the address of
2409           ** the next retest block */
2410           while( nConflictCk>0 ){
2411             VdbeOp x;    /* Conflict check opcode to copy */
2412             /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
2413             ** Hence, make a complete copy of the opcode, rather than using
2414             ** a pointer to the opcode. */
2415             x = *sqlite3VdbeGetOp(v, addrConflictCk);
2416             if( x.opcode!=OP_IdxRowid ){
2417               int p2;      /* New P2 value for copied conflict check opcode */
2418               const char *zP4;
2419               if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){
2420                 p2 = lblRecheckOk;
2421               }else{
2422                 p2 = x.p2;
2423               }
2424               zP4 = x.p4type==P4_INT32 ? SQLITE_INT_TO_PTR(x.p4.i) : x.p4.z;
2425               sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type);
2426               sqlite3VdbeChangeP5(v, x.p5);
2427               VdbeCoverageIf(v, p2!=x.p2);
2428             }
2429             nConflictCk--;
2430             addrConflictCk++;
2431           }
2432           /* If the retest fails, issue an abort */
2433           sqlite3UniqueConstraint(pParse, OE_Abort, pIdx);
2434 
2435           sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */
2436         }
2437         seenReplace = 1;
2438         break;
2439       }
2440     }
2441     sqlite3VdbeResolveLabel(v, addrUniqueOk);
2442     if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
2443     if( pUpsertClause
2444      && upsertIpkReturn
2445      && sqlite3UpsertNextIsIPK(pUpsertClause)
2446     ){
2447       sqlite3VdbeGoto(v, upsertIpkDelay+1);
2448       sqlite3VdbeJumpHere(v, upsertIpkReturn);
2449       upsertIpkReturn = 0;
2450     }
2451   }
2452 
2453   /* If the IPK constraint is a REPLACE, run it last */
2454   if( ipkTop ){
2455     sqlite3VdbeGoto(v, ipkTop);
2456     VdbeComment((v, "Do IPK REPLACE"));
2457     assert( ipkBottom>0 );
2458     sqlite3VdbeJumpHere(v, ipkBottom);
2459   }
2460 
2461   /* Recheck all uniqueness constraints after replace triggers have run */
2462   testcase( regTrigCnt!=0 && nReplaceTrig==0 );
2463   assert( regTrigCnt!=0 || nReplaceTrig==0 );
2464   if( nReplaceTrig ){
2465     sqlite3VdbeAddOp2(v, OP_IfNot, regTrigCnt, lblRecheckOk);VdbeCoverage(v);
2466     if( !pPk ){
2467       if( isUpdate ){
2468         sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRecheck, regOldData);
2469         sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
2470         VdbeCoverage(v);
2471       }
2472       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRecheck, regNewData);
2473       VdbeCoverage(v);
2474       sqlite3RowidConstraint(pParse, OE_Abort, pTab);
2475     }else{
2476       sqlite3VdbeGoto(v, addrRecheck);
2477     }
2478     sqlite3VdbeResolveLabel(v, lblRecheckOk);
2479   }
2480 
2481   /* Generate the table record */
2482   if( HasRowid(pTab) ){
2483     int regRec = aRegIdx[ix];
2484     sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
2485     sqlite3SetMakeRecordP5(v, pTab);
2486     if( !bAffinityDone ){
2487       sqlite3TableAffinity(v, pTab, 0);
2488     }
2489   }
2490 
2491   *pbMayReplace = seenReplace;
2492   VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
2493 }
2494 
2495 #ifdef SQLITE_ENABLE_NULL_TRIM
2496 /*
2497 ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
2498 ** to be the number of columns in table pTab that must not be NULL-trimmed.
2499 **
2500 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
2501 */
2502 void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
2503   u16 i;
2504 
2505   /* Records with omitted columns are only allowed for schema format
2506   ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
2507   if( pTab->pSchema->file_format<2 ) return;
2508 
2509   for(i=pTab->nCol-1; i>0; i--){
2510     if( pTab->aCol[i].iDflt!=0 ) break;
2511     if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
2512   }
2513   sqlite3VdbeChangeP5(v, i+1);
2514 }
2515 #endif
2516 
2517 /*
2518 ** Table pTab is a WITHOUT ROWID table that is being written to. The cursor
2519 ** number is iCur, and register regData contains the new record for the
2520 ** PK index. This function adds code to invoke the pre-update hook,
2521 ** if one is registered.
2522 */
2523 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2524 static void codeWithoutRowidPreupdate(
2525   Parse *pParse,                  /* Parse context */
2526   Table *pTab,                    /* Table being updated */
2527   int iCur,                       /* Cursor number for table */
2528   int regData                     /* Data containing new record */
2529 ){
2530   Vdbe *v = pParse->pVdbe;
2531   int r = sqlite3GetTempReg(pParse);
2532   assert( !HasRowid(pTab) );
2533   assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
2534   sqlite3VdbeAddOp2(v, OP_Integer, 0, r);
2535   sqlite3VdbeAddOp4(v, OP_Insert, iCur, regData, r, (char*)pTab, P4_TABLE);
2536   sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
2537   sqlite3ReleaseTempReg(pParse, r);
2538 }
2539 #else
2540 # define codeWithoutRowidPreupdate(a,b,c,d)
2541 #endif
2542 
2543 /*
2544 ** This routine generates code to finish the INSERT or UPDATE operation
2545 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
2546 ** A consecutive range of registers starting at regNewData contains the
2547 ** rowid and the content to be inserted.
2548 **
2549 ** The arguments to this routine should be the same as the first six
2550 ** arguments to sqlite3GenerateConstraintChecks.
2551 */
2552 void sqlite3CompleteInsertion(
2553   Parse *pParse,      /* The parser context */
2554   Table *pTab,        /* the table into which we are inserting */
2555   int iDataCur,       /* Cursor of the canonical data source */
2556   int iIdxCur,        /* First index cursor */
2557   int regNewData,     /* Range of content */
2558   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
2559   int update_flags,   /* True for UPDATE, False for INSERT */
2560   int appendBias,     /* True if this is likely to be an append */
2561   int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
2562 ){
2563   Vdbe *v;            /* Prepared statements under construction */
2564   Index *pIdx;        /* An index being inserted or updated */
2565   u8 pik_flags;       /* flag values passed to the btree insert */
2566   int i;              /* Loop counter */
2567 
2568   assert( update_flags==0
2569        || update_flags==OPFLAG_ISUPDATE
2570        || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
2571   );
2572 
2573   v = pParse->pVdbe;
2574   assert( v!=0 );
2575   assert( !IsView(pTab) );  /* This table is not a VIEW */
2576   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
2577     /* All REPLACE indexes are at the end of the list */
2578     assert( pIdx->onError!=OE_Replace
2579          || pIdx->pNext==0
2580          || pIdx->pNext->onError==OE_Replace );
2581     if( aRegIdx[i]==0 ) continue;
2582     if( pIdx->pPartIdxWhere ){
2583       sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
2584       VdbeCoverage(v);
2585     }
2586     pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
2587     if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
2588       pik_flags |= OPFLAG_NCHANGE;
2589       pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
2590       if( update_flags==0 ){
2591         codeWithoutRowidPreupdate(pParse, pTab, iIdxCur+i, aRegIdx[i]);
2592       }
2593     }
2594     sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
2595                          aRegIdx[i]+1,
2596                          pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
2597     sqlite3VdbeChangeP5(v, pik_flags);
2598   }
2599   if( !HasRowid(pTab) ) return;
2600   if( pParse->nested ){
2601     pik_flags = 0;
2602   }else{
2603     pik_flags = OPFLAG_NCHANGE;
2604     pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
2605   }
2606   if( appendBias ){
2607     pik_flags |= OPFLAG_APPEND;
2608   }
2609   if( useSeekResult ){
2610     pik_flags |= OPFLAG_USESEEKRESULT;
2611   }
2612   sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, aRegIdx[i], regNewData);
2613   if( !pParse->nested ){
2614     sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
2615   }
2616   sqlite3VdbeChangeP5(v, pik_flags);
2617 }
2618 
2619 /*
2620 ** Allocate cursors for the pTab table and all its indices and generate
2621 ** code to open and initialized those cursors.
2622 **
2623 ** The cursor for the object that contains the complete data (normally
2624 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
2625 ** ROWID table) is returned in *piDataCur.  The first index cursor is
2626 ** returned in *piIdxCur.  The number of indices is returned.
2627 **
2628 ** Use iBase as the first cursor (either the *piDataCur for rowid tables
2629 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
2630 ** If iBase is negative, then allocate the next available cursor.
2631 **
2632 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
2633 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
2634 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
2635 ** pTab->pIndex list.
2636 **
2637 ** If pTab is a virtual table, then this routine is a no-op and the
2638 ** *piDataCur and *piIdxCur values are left uninitialized.
2639 */
2640 int sqlite3OpenTableAndIndices(
2641   Parse *pParse,   /* Parsing context */
2642   Table *pTab,     /* Table to be opened */
2643   int op,          /* OP_OpenRead or OP_OpenWrite */
2644   u8 p5,           /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
2645   int iBase,       /* Use this for the table cursor, if there is one */
2646   u8 *aToOpen,     /* If not NULL: boolean for each table and index */
2647   int *piDataCur,  /* Write the database source cursor number here */
2648   int *piIdxCur    /* Write the first index cursor number here */
2649 ){
2650   int i;
2651   int iDb;
2652   int iDataCur;
2653   Index *pIdx;
2654   Vdbe *v;
2655 
2656   assert( op==OP_OpenRead || op==OP_OpenWrite );
2657   assert( op==OP_OpenWrite || p5==0 );
2658   if( IsVirtual(pTab) ){
2659     /* This routine is a no-op for virtual tables. Leave the output
2660     ** variables *piDataCur and *piIdxCur set to illegal cursor numbers
2661     ** for improved error detection. */
2662     *piDataCur = *piIdxCur = -999;
2663     return 0;
2664   }
2665   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
2666   v = pParse->pVdbe;
2667   assert( v!=0 );
2668   if( iBase<0 ) iBase = pParse->nTab;
2669   iDataCur = iBase++;
2670   if( piDataCur ) *piDataCur = iDataCur;
2671   if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
2672     sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
2673   }else{
2674     sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
2675   }
2676   if( piIdxCur ) *piIdxCur = iBase;
2677   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
2678     int iIdxCur = iBase++;
2679     assert( pIdx->pSchema==pTab->pSchema );
2680     if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
2681       if( piDataCur ) *piDataCur = iIdxCur;
2682       p5 = 0;
2683     }
2684     if( aToOpen==0 || aToOpen[i+1] ){
2685       sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
2686       sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
2687       sqlite3VdbeChangeP5(v, p5);
2688       VdbeComment((v, "%s", pIdx->zName));
2689     }
2690   }
2691   if( iBase>pParse->nTab ) pParse->nTab = iBase;
2692   return i;
2693 }
2694 
2695 
2696 #ifdef SQLITE_TEST
2697 /*
2698 ** The following global variable is incremented whenever the
2699 ** transfer optimization is used.  This is used for testing
2700 ** purposes only - to make sure the transfer optimization really
2701 ** is happening when it is supposed to.
2702 */
2703 int sqlite3_xferopt_count;
2704 #endif /* SQLITE_TEST */
2705 
2706 
2707 #ifndef SQLITE_OMIT_XFER_OPT
2708 /*
2709 ** Check to see if index pSrc is compatible as a source of data
2710 ** for index pDest in an insert transfer optimization.  The rules
2711 ** for a compatible index:
2712 **
2713 **    *   The index is over the same set of columns
2714 **    *   The same DESC and ASC markings occurs on all columns
2715 **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
2716 **    *   The same collating sequence on each column
2717 **    *   The index has the exact same WHERE clause
2718 */
2719 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
2720   int i;
2721   assert( pDest && pSrc );
2722   assert( pDest->pTable!=pSrc->pTable );
2723   if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
2724     return 0;   /* Different number of columns */
2725   }
2726   if( pDest->onError!=pSrc->onError ){
2727     return 0;   /* Different conflict resolution strategies */
2728   }
2729   for(i=0; i<pSrc->nKeyCol; i++){
2730     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
2731       return 0;   /* Different columns indexed */
2732     }
2733     if( pSrc->aiColumn[i]==XN_EXPR ){
2734       assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
2735       if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
2736                              pDest->aColExpr->a[i].pExpr, -1)!=0 ){
2737         return 0;   /* Different expressions in the index */
2738       }
2739     }
2740     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
2741       return 0;   /* Different sort orders */
2742     }
2743     if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
2744       return 0;   /* Different collating sequences */
2745     }
2746   }
2747   if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
2748     return 0;     /* Different WHERE clauses */
2749   }
2750 
2751   /* If no test above fails then the indices must be compatible */
2752   return 1;
2753 }
2754 
2755 /*
2756 ** Attempt the transfer optimization on INSERTs of the form
2757 **
2758 **     INSERT INTO tab1 SELECT * FROM tab2;
2759 **
2760 ** The xfer optimization transfers raw records from tab2 over to tab1.
2761 ** Columns are not decoded and reassembled, which greatly improves
2762 ** performance.  Raw index records are transferred in the same way.
2763 **
2764 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
2765 ** There are lots of rules for determining compatibility - see comments
2766 ** embedded in the code for details.
2767 **
2768 ** This routine returns TRUE if the optimization is guaranteed to be used.
2769 ** Sometimes the xfer optimization will only work if the destination table
2770 ** is empty - a factor that can only be determined at run-time.  In that
2771 ** case, this routine generates code for the xfer optimization but also
2772 ** does a test to see if the destination table is empty and jumps over the
2773 ** xfer optimization code if the test fails.  In that case, this routine
2774 ** returns FALSE so that the caller will know to go ahead and generate
2775 ** an unoptimized transfer.  This routine also returns FALSE if there
2776 ** is no chance that the xfer optimization can be applied.
2777 **
2778 ** This optimization is particularly useful at making VACUUM run faster.
2779 */
2780 static int xferOptimization(
2781   Parse *pParse,        /* Parser context */
2782   Table *pDest,         /* The table we are inserting into */
2783   Select *pSelect,      /* A SELECT statement to use as the data source */
2784   int onError,          /* How to handle constraint errors */
2785   int iDbDest           /* The database of pDest */
2786 ){
2787   sqlite3 *db = pParse->db;
2788   ExprList *pEList;                /* The result set of the SELECT */
2789   Table *pSrc;                     /* The table in the FROM clause of SELECT */
2790   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
2791   SrcItem *pItem;                  /* An element of pSelect->pSrc */
2792   int i;                           /* Loop counter */
2793   int iDbSrc;                      /* The database of pSrc */
2794   int iSrc, iDest;                 /* Cursors from source and destination */
2795   int addr1, addr2;                /* Loop addresses */
2796   int emptyDestTest = 0;           /* Address of test for empty pDest */
2797   int emptySrcTest = 0;            /* Address of test for empty pSrc */
2798   Vdbe *v;                         /* The VDBE we are building */
2799   int regAutoinc;                  /* Memory register used by AUTOINC */
2800   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
2801   int regData, regRowid;           /* Registers holding data and rowid */
2802 
2803   assert( pSelect!=0 );
2804   if( pParse->pWith || pSelect->pWith ){
2805     /* Do not attempt to process this query if there are an WITH clauses
2806     ** attached to it. Proceeding may generate a false "no such table: xxx"
2807     ** error if pSelect reads from a CTE named "xxx".  */
2808     return 0;
2809   }
2810 #ifndef SQLITE_OMIT_VIRTUALTABLE
2811   if( IsVirtual(pDest) ){
2812     return 0;   /* tab1 must not be a virtual table */
2813   }
2814 #endif
2815   if( onError==OE_Default ){
2816     if( pDest->iPKey>=0 ) onError = pDest->keyConf;
2817     if( onError==OE_Default ) onError = OE_Abort;
2818   }
2819   assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
2820   if( pSelect->pSrc->nSrc!=1 ){
2821     return 0;   /* FROM clause must have exactly one term */
2822   }
2823   if( pSelect->pSrc->a[0].pSelect ){
2824     return 0;   /* FROM clause cannot contain a subquery */
2825   }
2826   if( pSelect->pWhere ){
2827     return 0;   /* SELECT may not have a WHERE clause */
2828   }
2829   if( pSelect->pOrderBy ){
2830     return 0;   /* SELECT may not have an ORDER BY clause */
2831   }
2832   /* Do not need to test for a HAVING clause.  If HAVING is present but
2833   ** there is no ORDER BY, we will get an error. */
2834   if( pSelect->pGroupBy ){
2835     return 0;   /* SELECT may not have a GROUP BY clause */
2836   }
2837   if( pSelect->pLimit ){
2838     return 0;   /* SELECT may not have a LIMIT clause */
2839   }
2840   if( pSelect->pPrior ){
2841     return 0;   /* SELECT may not be a compound query */
2842   }
2843   if( pSelect->selFlags & SF_Distinct ){
2844     return 0;   /* SELECT may not be DISTINCT */
2845   }
2846   pEList = pSelect->pEList;
2847   assert( pEList!=0 );
2848   if( pEList->nExpr!=1 ){
2849     return 0;   /* The result set must have exactly one column */
2850   }
2851   assert( pEList->a[0].pExpr );
2852   if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
2853     return 0;   /* The result set must be the special operator "*" */
2854   }
2855 
2856   /* At this point we have established that the statement is of the
2857   ** correct syntactic form to participate in this optimization.  Now
2858   ** we have to check the semantics.
2859   */
2860   pItem = pSelect->pSrc->a;
2861   pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
2862   if( pSrc==0 ){
2863     return 0;   /* FROM clause does not contain a real table */
2864   }
2865   if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
2866     testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */
2867     return 0;   /* tab1 and tab2 may not be the same table */
2868   }
2869   if( HasRowid(pDest)!=HasRowid(pSrc) ){
2870     return 0;   /* source and destination must both be WITHOUT ROWID or not */
2871   }
2872   if( !IsOrdinaryTable(pSrc) ){
2873     return 0;   /* tab2 may not be a view or virtual table */
2874   }
2875   if( pDest->nCol!=pSrc->nCol ){
2876     return 0;   /* Number of columns must be the same in tab1 and tab2 */
2877   }
2878   if( pDest->iPKey!=pSrc->iPKey ){
2879     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
2880   }
2881   if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
2882     return 0;   /* Cannot feed from a non-strict into a strict table */
2883   }
2884   for(i=0; i<pDest->nCol; i++){
2885     Column *pDestCol = &pDest->aCol[i];
2886     Column *pSrcCol = &pSrc->aCol[i];
2887 #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
2888     if( (db->mDbFlags & DBFLAG_Vacuum)==0
2889      && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
2890     ){
2891       return 0;    /* Neither table may have __hidden__ columns */
2892     }
2893 #endif
2894 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
2895     /* Even if tables t1 and t2 have identical schemas, if they contain
2896     ** generated columns, then this statement is semantically incorrect:
2897     **
2898     **     INSERT INTO t2 SELECT * FROM t1;
2899     **
2900     ** The reason is that generated column values are returned by the
2901     ** the SELECT statement on the right but the INSERT statement on the
2902     ** left wants them to be omitted.
2903     **
2904     ** Nevertheless, this is a useful notational shorthand to tell SQLite
2905     ** to do a bulk transfer all of the content from t1 over to t2.
2906     **
2907     ** We could, in theory, disable this (except for internal use by the
2908     ** VACUUM command where it is actually needed).  But why do that?  It
2909     ** seems harmless enough, and provides a useful service.
2910     */
2911     if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
2912         (pSrcCol->colFlags & COLFLAG_GENERATED) ){
2913       return 0;    /* Both columns have the same generated-column type */
2914     }
2915     /* But the transfer is only allowed if both the source and destination
2916     ** tables have the exact same expressions for generated columns.
2917     ** This requirement could be relaxed for VIRTUAL columns, I suppose.
2918     */
2919     if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
2920       if( sqlite3ExprCompare(0,
2921              sqlite3ColumnExpr(pSrc, pSrcCol),
2922              sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
2923         testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
2924         testcase( pDestCol->colFlags & COLFLAG_STORED );
2925         return 0;  /* Different generator expressions */
2926       }
2927     }
2928 #endif
2929     if( pDestCol->affinity!=pSrcCol->affinity ){
2930       return 0;    /* Affinity must be the same on all columns */
2931     }
2932     if( sqlite3_stricmp(sqlite3ColumnColl(pDestCol),
2933                         sqlite3ColumnColl(pSrcCol))!=0 ){
2934       return 0;    /* Collating sequence must be the same on all columns */
2935     }
2936     if( pDestCol->notNull && !pSrcCol->notNull ){
2937       return 0;    /* tab2 must be NOT NULL if tab1 is */
2938     }
2939     /* Default values for second and subsequent columns need to match. */
2940     if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
2941       Expr *pDestExpr = sqlite3ColumnExpr(pDest, pDestCol);
2942       Expr *pSrcExpr = sqlite3ColumnExpr(pSrc, pSrcCol);
2943       assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
2944       assert( pDestExpr==0 || !ExprHasProperty(pDestExpr, EP_IntValue) );
2945       assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
2946       assert( pSrcExpr==0 || !ExprHasProperty(pSrcExpr, EP_IntValue) );
2947       if( (pDestExpr==0)!=(pSrcExpr==0)
2948        || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
2949                                        pSrcExpr->u.zToken)!=0)
2950       ){
2951         return 0;    /* Default values must be the same for all columns */
2952       }
2953     }
2954   }
2955   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
2956     if( IsUniqueIndex(pDestIdx) ){
2957       destHasUniqueIdx = 1;
2958     }
2959     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
2960       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
2961     }
2962     if( pSrcIdx==0 ){
2963       return 0;    /* pDestIdx has no corresponding index in pSrc */
2964     }
2965     if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
2966          && sqlite3FaultSim(411)==SQLITE_OK ){
2967       /* The sqlite3FaultSim() call allows this corruption test to be
2968       ** bypassed during testing, in order to exercise other corruption tests
2969       ** further downstream. */
2970       return 0;   /* Corrupt schema - two indexes on the same btree */
2971     }
2972   }
2973 #ifndef SQLITE_OMIT_CHECK
2974   if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
2975     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
2976   }
2977 #endif
2978 #ifndef SQLITE_OMIT_FOREIGN_KEY
2979   /* Disallow the transfer optimization if the destination table constains
2980   ** any foreign key constraints.  This is more restrictive than necessary.
2981   ** But the main beneficiary of the transfer optimization is the VACUUM
2982   ** command, and the VACUUM command disables foreign key constraints.  So
2983   ** the extra complication to make this rule less restrictive is probably
2984   ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
2985   */
2986   assert( IsOrdinaryTable(pDest) );
2987   if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
2988     return 0;
2989   }
2990 #endif
2991   if( (db->flags & SQLITE_CountRows)!=0 ){
2992     return 0;  /* xfer opt does not play well with PRAGMA count_changes */
2993   }
2994 
2995   /* If we get this far, it means that the xfer optimization is at
2996   ** least a possibility, though it might only work if the destination
2997   ** table (tab1) is initially empty.
2998   */
2999 #ifdef SQLITE_TEST
3000   sqlite3_xferopt_count++;
3001 #endif
3002   iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
3003   v = sqlite3GetVdbe(pParse);
3004   sqlite3CodeVerifySchema(pParse, iDbSrc);
3005   iSrc = pParse->nTab++;
3006   iDest = pParse->nTab++;
3007   regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
3008   regData = sqlite3GetTempReg(pParse);
3009   sqlite3VdbeAddOp2(v, OP_Null, 0, regData);
3010   regRowid = sqlite3GetTempReg(pParse);
3011   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
3012   assert( HasRowid(pDest) || destHasUniqueIdx );
3013   if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
3014       (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
3015    || destHasUniqueIdx                              /* (2) */
3016    || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
3017   )){
3018     /* In some circumstances, we are able to run the xfer optimization
3019     ** only if the destination table is initially empty. Unless the
3020     ** DBFLAG_Vacuum flag is set, this block generates code to make
3021     ** that determination. If DBFLAG_Vacuum is set, then the destination
3022     ** table is always empty.
3023     **
3024     ** Conditions under which the destination must be empty:
3025     **
3026     ** (1) There is no INTEGER PRIMARY KEY but there are indices.
3027     **     (If the destination is not initially empty, the rowid fields
3028     **     of index entries might need to change.)
3029     **
3030     ** (2) The destination has a unique index.  (The xfer optimization
3031     **     is unable to test uniqueness.)
3032     **
3033     ** (3) onError is something other than OE_Abort and OE_Rollback.
3034     */
3035     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
3036     emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
3037     sqlite3VdbeJumpHere(v, addr1);
3038   }
3039   if( HasRowid(pSrc) ){
3040     u8 insFlags;
3041     sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
3042     emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
3043     if( pDest->iPKey>=0 ){
3044       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
3045       if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
3046         sqlite3VdbeVerifyAbortable(v, onError);
3047         addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
3048         VdbeCoverage(v);
3049         sqlite3RowidConstraint(pParse, onError, pDest);
3050         sqlite3VdbeJumpHere(v, addr2);
3051       }
3052       autoIncStep(pParse, regAutoinc, regRowid);
3053     }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
3054       addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
3055     }else{
3056       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
3057       assert( (pDest->tabFlags & TF_Autoincrement)==0 );
3058     }
3059 
3060     if( db->mDbFlags & DBFLAG_Vacuum ){
3061       sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
3062       insFlags = OPFLAG_APPEND|OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
3063     }else{
3064       insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND|OPFLAG_PREFORMAT;
3065     }
3066 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
3067     if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
3068       sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
3069       insFlags &= ~OPFLAG_PREFORMAT;
3070     }else
3071 #endif
3072     {
3073       sqlite3VdbeAddOp3(v, OP_RowCell, iDest, iSrc, regRowid);
3074     }
3075     sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
3076     if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
3077       sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
3078     }
3079     sqlite3VdbeChangeP5(v, insFlags);
3080 
3081     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
3082     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
3083     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
3084   }else{
3085     sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
3086     sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
3087   }
3088   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
3089     u8 idxInsFlags = 0;
3090     for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
3091       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
3092     }
3093     assert( pSrcIdx );
3094     sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
3095     sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
3096     VdbeComment((v, "%s", pSrcIdx->zName));
3097     sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
3098     sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
3099     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
3100     VdbeComment((v, "%s", pDestIdx->zName));
3101     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
3102     if( db->mDbFlags & DBFLAG_Vacuum ){
3103       /* This INSERT command is part of a VACUUM operation, which guarantees
3104       ** that the destination table is empty. If all indexed columns use
3105       ** collation sequence BINARY, then it can also be assumed that the
3106       ** index will be populated by inserting keys in strictly sorted
3107       ** order. In this case, instead of seeking within the b-tree as part
3108       ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
3109       ** OP_IdxInsert to seek to the point within the b-tree where each key
3110       ** should be inserted. This is faster.
3111       **
3112       ** If any of the indexed columns use a collation sequence other than
3113       ** BINARY, this optimization is disabled. This is because the user
3114       ** might change the definition of a collation sequence and then run
3115       ** a VACUUM command. In that case keys may not be written in strictly
3116       ** sorted order.  */
3117       for(i=0; i<pSrcIdx->nColumn; i++){
3118         const char *zColl = pSrcIdx->azColl[i];
3119         if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
3120       }
3121       if( i==pSrcIdx->nColumn ){
3122         idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT;
3123         sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
3124         sqlite3VdbeAddOp2(v, OP_RowCell, iDest, iSrc);
3125       }
3126     }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
3127       idxInsFlags |= OPFLAG_NCHANGE;
3128     }
3129     if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){
3130       sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
3131       if( (db->mDbFlags & DBFLAG_Vacuum)==0
3132        && !HasRowid(pDest)
3133        && IsPrimaryKeyIndex(pDestIdx)
3134       ){
3135         codeWithoutRowidPreupdate(pParse, pDest, iDest, regData);
3136       }
3137     }
3138     sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
3139     sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
3140     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
3141     sqlite3VdbeJumpHere(v, addr1);
3142     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
3143     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
3144   }
3145   if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
3146   sqlite3ReleaseTempReg(pParse, regRowid);
3147   sqlite3ReleaseTempReg(pParse, regData);
3148   if( emptyDestTest ){
3149     sqlite3AutoincrementEnd(pParse);
3150     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
3151     sqlite3VdbeJumpHere(v, emptyDestTest);
3152     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
3153     return 0;
3154   }else{
3155     return 1;
3156   }
3157 }
3158 #endif /* SQLITE_OMIT_XFER_OPT */
3159