xref: /sqlite-3.40.0/src/insert.c (revision af94adf0)
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   v = sqlite3GetVdbe(pParse);
36   assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
37   sqlite3TableLock(pParse, iDb, pTab->tnum,
38                    (opcode==OP_OpenWrite)?1:0, pTab->zName);
39   if( HasRowid(pTab) ){
40     sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
41     VdbeComment((v, "%s", pTab->zName));
42   }else{
43     Index *pPk = sqlite3PrimaryKeyIndex(pTab);
44     assert( pPk!=0 );
45     assert( pPk->tnum==pTab->tnum );
46     sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
47     sqlite3VdbeSetP4KeyInfo(pParse, pPk);
48     VdbeComment((v, "%s", pTab->zName));
49   }
50 }
51 
52 /*
53 ** Return a pointer to the column affinity string associated with index
54 ** pIdx. A column affinity string has one character for each column in
55 ** the table, according to the affinity of the column:
56 **
57 **  Character      Column affinity
58 **  ------------------------------
59 **  'A'            BLOB
60 **  'B'            TEXT
61 **  'C'            NUMERIC
62 **  'D'            INTEGER
63 **  'F'            REAL
64 **
65 ** An extra 'D' is appended to the end of the string to cover the
66 ** rowid that appears as the last column in every index.
67 **
68 ** Memory for the buffer containing the column index affinity string
69 ** is managed along with the rest of the Index structure. It will be
70 ** released when sqlite3DeleteIndex() is called.
71 */
72 const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
73   if( !pIdx->zColAff ){
74     /* The first time a column affinity string for a particular index is
75     ** required, it is allocated and populated here. It is then stored as
76     ** a member of the Index structure for subsequent use.
77     **
78     ** The column affinity string will eventually be deleted by
79     ** sqliteDeleteIndex() when the Index structure itself is cleaned
80     ** up.
81     */
82     int n;
83     Table *pTab = pIdx->pTable;
84     pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
85     if( !pIdx->zColAff ){
86       sqlite3OomFault(db);
87       return 0;
88     }
89     for(n=0; n<pIdx->nColumn; n++){
90       i16 x = pIdx->aiColumn[n];
91       char aff;
92       if( x>=0 ){
93         aff = pTab->aCol[x].affinity;
94       }else if( x==XN_ROWID ){
95         aff = SQLITE_AFF_INTEGER;
96       }else{
97         assert( x==XN_EXPR );
98         assert( pIdx->aColExpr!=0 );
99         aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
100       }
101       if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
102       if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
103       pIdx->zColAff[n] = aff;
104     }
105     pIdx->zColAff[n] = 0;
106   }
107 
108   return pIdx->zColAff;
109 }
110 
111 /*
112 ** Compute the affinity string for table pTab, if it has not already been
113 ** computed.  As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
114 **
115 ** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
116 ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
117 ** for register iReg and following.  Or if affinities exists and iReg==0,
118 ** then just set the P4 operand of the previous opcode (which should  be
119 ** an OP_MakeRecord) to the affinity string.
120 **
121 ** A column affinity string has one character per column:
122 **
123 **  Character      Column affinity
124 **  ------------------------------
125 **  'A'            BLOB
126 **  'B'            TEXT
127 **  'C'            NUMERIC
128 **  'D'            INTEGER
129 **  'E'            REAL
130 */
131 void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
132   int i;
133   char *zColAff = pTab->zColAff;
134   if( zColAff==0 ){
135     sqlite3 *db = sqlite3VdbeDb(v);
136     zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
137     if( !zColAff ){
138       sqlite3OomFault(db);
139       return;
140     }
141 
142     for(i=0; i<pTab->nCol; i++){
143       assert( pTab->aCol[i].affinity!=0 );
144       zColAff[i] = pTab->aCol[i].affinity;
145     }
146     do{
147       zColAff[i--] = 0;
148     }while( i>=0 && zColAff[i]<=SQLITE_AFF_BLOB );
149     pTab->zColAff = zColAff;
150   }
151   assert( zColAff!=0 );
152   i = sqlite3Strlen30NN(zColAff);
153   if( i ){
154     if( iReg ){
155       sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
156     }else{
157       sqlite3VdbeChangeP4(v, -1, zColAff, i);
158     }
159   }
160 }
161 
162 /*
163 ** Return non-zero if the table pTab in database iDb or any of its indices
164 ** have been opened at any point in the VDBE program. This is used to see if
165 ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can
166 ** run without using a temporary table for the results of the SELECT.
167 */
168 static int readsTable(Parse *p, int iDb, Table *pTab){
169   Vdbe *v = sqlite3GetVdbe(p);
170   int i;
171   int iEnd = sqlite3VdbeCurrentAddr(v);
172 #ifndef SQLITE_OMIT_VIRTUALTABLE
173   VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
174 #endif
175 
176   for(i=1; i<iEnd; i++){
177     VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
178     assert( pOp!=0 );
179     if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
180       Index *pIndex;
181       int tnum = pOp->p2;
182       if( tnum==pTab->tnum ){
183         return 1;
184       }
185       for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
186         if( tnum==pIndex->tnum ){
187           return 1;
188         }
189       }
190     }
191 #ifndef SQLITE_OMIT_VIRTUALTABLE
192     if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
193       assert( pOp->p4.pVtab!=0 );
194       assert( pOp->p4type==P4_VTAB );
195       return 1;
196     }
197 #endif
198   }
199   return 0;
200 }
201 
202 #ifndef SQLITE_OMIT_AUTOINCREMENT
203 /*
204 ** Locate or create an AutoincInfo structure associated with table pTab
205 ** which is in database iDb.  Return the register number for the register
206 ** that holds the maximum rowid.  Return zero if pTab is not an AUTOINCREMENT
207 ** table.  (Also return zero when doing a VACUUM since we do not want to
208 ** update the AUTOINCREMENT counters during a VACUUM.)
209 **
210 ** There is at most one AutoincInfo structure per table even if the
211 ** same table is autoincremented multiple times due to inserts within
212 ** triggers.  A new AutoincInfo structure is created if this is the
213 ** first use of table pTab.  On 2nd and subsequent uses, the original
214 ** AutoincInfo structure is used.
215 **
216 ** Four consecutive registers are allocated:
217 **
218 **   (1)  The name of the pTab table.
219 **   (2)  The maximum ROWID of pTab.
220 **   (3)  The rowid in sqlite_sequence of pTab
221 **   (4)  The original value of the max ROWID in pTab, or NULL if none
222 **
223 ** The 2nd register is the one that is returned.  That is all the
224 ** insert routine needs to know about.
225 */
226 static int autoIncBegin(
227   Parse *pParse,      /* Parsing context */
228   int iDb,            /* Index of the database holding pTab */
229   Table *pTab         /* The table we are writing to */
230 ){
231   int memId = 0;      /* Register holding maximum rowid */
232   assert( pParse->db->aDb[iDb].pSchema!=0 );
233   if( (pTab->tabFlags & TF_Autoincrement)!=0
234    && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
235   ){
236     Parse *pToplevel = sqlite3ParseToplevel(pParse);
237     AutoincInfo *pInfo;
238     Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
239 
240     /* Verify that the sqlite_sequence table exists and is an ordinary
241     ** rowid table with exactly two columns.
242     ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
243     if( pSeqTab==0
244      || !HasRowid(pSeqTab)
245      || IsVirtual(pSeqTab)
246      || pSeqTab->nCol!=2
247     ){
248       pParse->nErr++;
249       pParse->rc = SQLITE_CORRUPT_SEQUENCE;
250       return 0;
251     }
252 
253     pInfo = pToplevel->pAinc;
254     while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
255     if( pInfo==0 ){
256       pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
257       if( pInfo==0 ) return 0;
258       pInfo->pNext = pToplevel->pAinc;
259       pToplevel->pAinc = pInfo;
260       pInfo->pTab = pTab;
261       pInfo->iDb = iDb;
262       pToplevel->nMem++;                  /* Register to hold name of table */
263       pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
264       pToplevel->nMem +=2;       /* Rowid in sqlite_sequence + orig max val */
265     }
266     memId = pInfo->regCtr;
267   }
268   return memId;
269 }
270 
271 /*
272 ** This routine generates code that will initialize all of the
273 ** register used by the autoincrement tracker.
274 */
275 void sqlite3AutoincrementBegin(Parse *pParse){
276   AutoincInfo *p;            /* Information about an AUTOINCREMENT */
277   sqlite3 *db = pParse->db;  /* The database connection */
278   Db *pDb;                   /* Database only autoinc table */
279   int memId;                 /* Register holding max rowid */
280   Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
281 
282   /* This routine is never called during trigger-generation.  It is
283   ** only called from the top-level */
284   assert( pParse->pTriggerTab==0 );
285   assert( sqlite3IsToplevel(pParse) );
286 
287   assert( v );   /* We failed long ago if this is not so */
288   for(p = pParse->pAinc; p; p = p->pNext){
289     static const int iLn = VDBE_OFFSET_LINENO(2);
290     static const VdbeOpList autoInc[] = {
291       /* 0  */ {OP_Null,    0,  0, 0},
292       /* 1  */ {OP_Rewind,  0, 10, 0},
293       /* 2  */ {OP_Column,  0,  0, 0},
294       /* 3  */ {OP_Ne,      0,  9, 0},
295       /* 4  */ {OP_Rowid,   0,  0, 0},
296       /* 5  */ {OP_Column,  0,  1, 0},
297       /* 6  */ {OP_AddImm,  0,  0, 0},
298       /* 7  */ {OP_Copy,    0,  0, 0},
299       /* 8  */ {OP_Goto,    0, 11, 0},
300       /* 9  */ {OP_Next,    0,  2, 0},
301       /* 10 */ {OP_Integer, 0,  0, 0},
302       /* 11 */ {OP_Close,   0,  0, 0}
303     };
304     VdbeOp *aOp;
305     pDb = &db->aDb[p->iDb];
306     memId = p->regCtr;
307     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
308     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
309     sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
310     aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
311     if( aOp==0 ) break;
312     aOp[0].p2 = memId;
313     aOp[0].p3 = memId+2;
314     aOp[2].p3 = memId;
315     aOp[3].p1 = memId-1;
316     aOp[3].p3 = memId;
317     aOp[3].p5 = SQLITE_JUMPIFNULL;
318     aOp[4].p2 = memId+1;
319     aOp[5].p3 = memId;
320     aOp[6].p1 = memId;
321     aOp[7].p2 = memId+2;
322     aOp[7].p1 = memId;
323     aOp[10].p2 = memId;
324     if( pParse->nTab==0 ) pParse->nTab = 1;
325   }
326 }
327 
328 /*
329 ** Update the maximum rowid for an autoincrement calculation.
330 **
331 ** This routine should be called when the regRowid register holds a
332 ** new rowid that is about to be inserted.  If that new rowid is
333 ** larger than the maximum rowid in the memId memory cell, then the
334 ** memory cell is updated.
335 */
336 static void autoIncStep(Parse *pParse, int memId, int regRowid){
337   if( memId>0 ){
338     sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
339   }
340 }
341 
342 /*
343 ** This routine generates the code needed to write autoincrement
344 ** maximum rowid values back into the sqlite_sequence register.
345 ** Every statement that might do an INSERT into an autoincrement
346 ** table (either directly or through triggers) needs to call this
347 ** routine just before the "exit" code.
348 */
349 static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
350   AutoincInfo *p;
351   Vdbe *v = pParse->pVdbe;
352   sqlite3 *db = pParse->db;
353 
354   assert( v );
355   for(p = pParse->pAinc; p; p = p->pNext){
356     static const int iLn = VDBE_OFFSET_LINENO(2);
357     static const VdbeOpList autoIncEnd[] = {
358       /* 0 */ {OP_NotNull,     0, 2, 0},
359       /* 1 */ {OP_NewRowid,    0, 0, 0},
360       /* 2 */ {OP_MakeRecord,  0, 2, 0},
361       /* 3 */ {OP_Insert,      0, 0, 0},
362       /* 4 */ {OP_Close,       0, 0, 0}
363     };
364     VdbeOp *aOp;
365     Db *pDb = &db->aDb[p->iDb];
366     int iRec;
367     int memId = p->regCtr;
368 
369     iRec = sqlite3GetTempReg(pParse);
370     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
371     sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
372     VdbeCoverage(v);
373     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
374     aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
375     if( aOp==0 ) break;
376     aOp[0].p1 = memId+1;
377     aOp[1].p2 = memId+1;
378     aOp[2].p1 = memId-1;
379     aOp[2].p3 = iRec;
380     aOp[3].p2 = iRec;
381     aOp[3].p3 = memId+1;
382     aOp[3].p5 = OPFLAG_APPEND;
383     sqlite3ReleaseTempReg(pParse, iRec);
384   }
385 }
386 void sqlite3AutoincrementEnd(Parse *pParse){
387   if( pParse->pAinc ) autoIncrementEnd(pParse);
388 }
389 #else
390 /*
391 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
392 ** above are all no-ops
393 */
394 # define autoIncBegin(A,B,C) (0)
395 # define autoIncStep(A,B,C)
396 #endif /* SQLITE_OMIT_AUTOINCREMENT */
397 
398 
399 /* Forward declaration */
400 static int xferOptimization(
401   Parse *pParse,        /* Parser context */
402   Table *pDest,         /* The table we are inserting into */
403   Select *pSelect,      /* A SELECT statement to use as the data source */
404   int onError,          /* How to handle constraint errors */
405   int iDbDest           /* The database of pDest */
406 );
407 
408 /*
409 ** This routine is called to handle SQL of the following forms:
410 **
411 **    insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
412 **    insert into TABLE (IDLIST) select
413 **    insert into TABLE (IDLIST) default values
414 **
415 ** The IDLIST following the table name is always optional.  If omitted,
416 ** then a list of all (non-hidden) columns for the table is substituted.
417 ** The IDLIST appears in the pColumn parameter.  pColumn is NULL if IDLIST
418 ** is omitted.
419 **
420 ** For the pSelect parameter holds the values to be inserted for the
421 ** first two forms shown above.  A VALUES clause is really just short-hand
422 ** for a SELECT statement that omits the FROM clause and everything else
423 ** that follows.  If the pSelect parameter is NULL, that means that the
424 ** DEFAULT VALUES form of the INSERT statement is intended.
425 **
426 ** The code generated follows one of four templates.  For a simple
427 ** insert with data coming from a single-row VALUES clause, the code executes
428 ** once straight down through.  Pseudo-code follows (we call this
429 ** the "1st template"):
430 **
431 **         open write cursor to <table> and its indices
432 **         put VALUES clause expressions into registers
433 **         write the resulting record into <table>
434 **         cleanup
435 **
436 ** The three remaining templates assume the statement is of the form
437 **
438 **   INSERT INTO <table> SELECT ...
439 **
440 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
441 ** in other words if the SELECT pulls all columns from a single table
442 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
443 ** if <table2> and <table1> are distinct tables but have identical
444 ** schemas, including all the same indices, then a special optimization
445 ** is invoked that copies raw records from <table2> over to <table1>.
446 ** See the xferOptimization() function for the implementation of this
447 ** template.  This is the 2nd template.
448 **
449 **         open a write cursor to <table>
450 **         open read cursor on <table2>
451 **         transfer all records in <table2> over to <table>
452 **         close cursors
453 **         foreach index on <table>
454 **           open a write cursor on the <table> index
455 **           open a read cursor on the corresponding <table2> index
456 **           transfer all records from the read to the write cursors
457 **           close cursors
458 **         end foreach
459 **
460 ** The 3rd template is for when the second template does not apply
461 ** and the SELECT clause does not read from <table> at any time.
462 ** The generated code follows this template:
463 **
464 **         X <- A
465 **         goto B
466 **      A: setup for the SELECT
467 **         loop over the rows in the SELECT
468 **           load values into registers R..R+n
469 **           yield X
470 **         end loop
471 **         cleanup after the SELECT
472 **         end-coroutine X
473 **      B: open write cursor to <table> and its indices
474 **      C: yield X, at EOF goto D
475 **         insert the select result into <table> from R..R+n
476 **         goto C
477 **      D: cleanup
478 **
479 ** The 4th template is used if the insert statement takes its
480 ** values from a SELECT but the data is being inserted into a table
481 ** that is also read as part of the SELECT.  In the third form,
482 ** we have to use an intermediate table to store the results of
483 ** the select.  The template is like this:
484 **
485 **         X <- A
486 **         goto B
487 **      A: setup for the SELECT
488 **         loop over the tables in the SELECT
489 **           load value into register R..R+n
490 **           yield X
491 **         end loop
492 **         cleanup after the SELECT
493 **         end co-routine R
494 **      B: open temp table
495 **      L: yield X, at EOF goto M
496 **         insert row from R..R+n into temp table
497 **         goto L
498 **      M: open write cursor to <table> and its indices
499 **         rewind temp table
500 **      C: loop over rows of intermediate table
501 **           transfer values form intermediate table into <table>
502 **         end loop
503 **      D: cleanup
504 */
505 void sqlite3Insert(
506   Parse *pParse,        /* Parser context */
507   SrcList *pTabList,    /* Name of table into which we are inserting */
508   Select *pSelect,      /* A SELECT statement to use as the data source */
509   IdList *pColumn,      /* Column names corresponding to IDLIST. */
510   int onError,          /* How to handle constraint errors */
511   Upsert *pUpsert       /* ON CONFLICT clauses for upsert, or NULL */
512 ){
513   sqlite3 *db;          /* The main database structure */
514   Table *pTab;          /* The table to insert into.  aka TABLE */
515   int i, j;             /* Loop counters */
516   Vdbe *v;              /* Generate code into this virtual machine */
517   Index *pIdx;          /* For looping over indices of the table */
518   int nColumn;          /* Number of columns in the data */
519   int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
520   int iDataCur = 0;     /* VDBE cursor that is the main data repository */
521   int iIdxCur = 0;      /* First index cursor */
522   int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
523   int endOfLoop;        /* Label for the end of the insertion loop */
524   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
525   int addrInsTop = 0;   /* Jump to label "D" */
526   int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
527   SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
528   int iDb;              /* Index of database holding TABLE */
529   u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
530   u8 appendFlag = 0;    /* True if the insert is likely to be an append */
531   u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
532   u8 bIdListInOrder;    /* True if IDLIST is in table order */
533   ExprList *pList = 0;  /* List of VALUES() to be inserted  */
534 
535   /* Register allocations */
536   int regFromSelect = 0;/* Base register for data coming from SELECT */
537   int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
538   int regRowCount = 0;  /* Memory cell used for the row counter */
539   int regIns;           /* Block of regs holding rowid+data being inserted */
540   int regRowid;         /* registers holding insert rowid */
541   int regData;          /* register holding first column to insert */
542   int *aRegIdx = 0;     /* One register allocated to each index */
543 
544 #ifndef SQLITE_OMIT_TRIGGER
545   int isView;                 /* True if attempting to insert into a view */
546   Trigger *pTrigger;          /* List of triggers on pTab, if required */
547   int tmask;                  /* Mask of trigger times */
548 #endif
549 
550   db = pParse->db;
551   if( pParse->nErr || db->mallocFailed ){
552     goto insert_cleanup;
553   }
554   dest.iSDParm = 0;  /* Suppress a harmless compiler warning */
555 
556   /* If the Select object is really just a simple VALUES() list with a
557   ** single row (the common case) then keep that one row of values
558   ** and discard the other (unused) parts of the pSelect object
559   */
560   if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
561     pList = pSelect->pEList;
562     pSelect->pEList = 0;
563     sqlite3SelectDelete(db, pSelect);
564     pSelect = 0;
565   }
566 
567   /* Locate the table into which we will be inserting new information.
568   */
569   assert( pTabList->nSrc==1 );
570   pTab = sqlite3SrcListLookup(pParse, pTabList);
571   if( pTab==0 ){
572     goto insert_cleanup;
573   }
574   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
575   assert( iDb<db->nDb );
576   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
577                        db->aDb[iDb].zDbSName) ){
578     goto insert_cleanup;
579   }
580   withoutRowid = !HasRowid(pTab);
581 
582   /* Figure out if we have any triggers and if the table being
583   ** inserted into is a view
584   */
585 #ifndef SQLITE_OMIT_TRIGGER
586   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
587   isView = pTab->pSelect!=0;
588 #else
589 # define pTrigger 0
590 # define tmask 0
591 # define isView 0
592 #endif
593 #ifdef SQLITE_OMIT_VIEW
594 # undef isView
595 # define isView 0
596 #endif
597   assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
598 
599   /* If pTab is really a view, make sure it has been initialized.
600   ** ViewGetColumnNames() is a no-op if pTab is not a view.
601   */
602   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
603     goto insert_cleanup;
604   }
605 
606   /* Cannot insert into a read-only table.
607   */
608   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
609     goto insert_cleanup;
610   }
611 
612   /* Allocate a VDBE
613   */
614   v = sqlite3GetVdbe(pParse);
615   if( v==0 ) goto insert_cleanup;
616   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
617   sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
618 
619 #ifndef SQLITE_OMIT_XFER_OPT
620   /* If the statement is of the form
621   **
622   **       INSERT INTO <table1> SELECT * FROM <table2>;
623   **
624   ** Then special optimizations can be applied that make the transfer
625   ** very fast and which reduce fragmentation of indices.
626   **
627   ** This is the 2nd template.
628   */
629   if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
630     assert( !pTrigger );
631     assert( pList==0 );
632     goto insert_end;
633   }
634 #endif /* SQLITE_OMIT_XFER_OPT */
635 
636   /* If this is an AUTOINCREMENT table, look up the sequence number in the
637   ** sqlite_sequence table and store it in memory cell regAutoinc.
638   */
639   regAutoinc = autoIncBegin(pParse, iDb, pTab);
640 
641   /* Allocate registers for holding the rowid of the new row,
642   ** the content of the new row, and the assembled row record.
643   */
644   regRowid = regIns = pParse->nMem+1;
645   pParse->nMem += pTab->nCol + 1;
646   if( IsVirtual(pTab) ){
647     regRowid++;
648     pParse->nMem++;
649   }
650   regData = regRowid+1;
651 
652   /* If the INSERT statement included an IDLIST term, then make sure
653   ** all elements of the IDLIST really are columns of the table and
654   ** remember the column indices.
655   **
656   ** If the table has an INTEGER PRIMARY KEY column and that column
657   ** is named in the IDLIST, then record in the ipkColumn variable
658   ** the index into IDLIST of the primary key column.  ipkColumn is
659   ** the index of the primary key as it appears in IDLIST, not as
660   ** is appears in the original table.  (The index of the INTEGER
661   ** PRIMARY KEY in the original table is pTab->iPKey.)
662   */
663   bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0;
664   if( pColumn ){
665     for(i=0; i<pColumn->nId; i++){
666       pColumn->a[i].idx = -1;
667     }
668     for(i=0; i<pColumn->nId; i++){
669       for(j=0; j<pTab->nCol; j++){
670         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
671           pColumn->a[i].idx = j;
672           if( i!=j ) bIdListInOrder = 0;
673           if( j==pTab->iPKey ){
674             ipkColumn = i;  assert( !withoutRowid );
675           }
676           break;
677         }
678       }
679       if( j>=pTab->nCol ){
680         if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
681           ipkColumn = i;
682           bIdListInOrder = 0;
683         }else{
684           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
685               pTabList, 0, pColumn->a[i].zName);
686           pParse->checkSchema = 1;
687           goto insert_cleanup;
688         }
689       }
690     }
691   }
692 
693   /* Figure out how many columns of data are supplied.  If the data
694   ** is coming from a SELECT statement, then generate a co-routine that
695   ** produces a single row of the SELECT on each invocation.  The
696   ** co-routine is the common header to the 3rd and 4th templates.
697   */
698   if( pSelect ){
699     /* Data is coming from a SELECT or from a multi-row VALUES clause.
700     ** Generate a co-routine to run the SELECT. */
701     int regYield;       /* Register holding co-routine entry-point */
702     int addrTop;        /* Top of the co-routine */
703     int rc;             /* Result code */
704 
705     regYield = ++pParse->nMem;
706     addrTop = sqlite3VdbeCurrentAddr(v) + 1;
707     sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
708     sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
709     dest.iSdst = bIdListInOrder ? regData : 0;
710     dest.nSdst = pTab->nCol;
711     rc = sqlite3Select(pParse, pSelect, &dest);
712     regFromSelect = dest.iSdst;
713     if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
714     sqlite3VdbeEndCoroutine(v, regYield);
715     sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
716     assert( pSelect->pEList );
717     nColumn = pSelect->pEList->nExpr;
718 
719     /* Set useTempTable to TRUE if the result of the SELECT statement
720     ** should be written into a temporary table (template 4).  Set to
721     ** FALSE if each output row of the SELECT can be written directly into
722     ** the destination table (template 3).
723     **
724     ** A temp table must be used if the table being updated is also one
725     ** of the tables being read by the SELECT statement.  Also use a
726     ** temp table in the case of row triggers.
727     */
728     if( pTrigger || readsTable(pParse, iDb, pTab) ){
729       useTempTable = 1;
730     }
731 
732     if( useTempTable ){
733       /* Invoke the coroutine to extract information from the SELECT
734       ** and add it to a transient table srcTab.  The code generated
735       ** here is from the 4th template:
736       **
737       **      B: open temp table
738       **      L: yield X, goto M at EOF
739       **         insert row from R..R+n into temp table
740       **         goto L
741       **      M: ...
742       */
743       int regRec;          /* Register to hold packed record */
744       int regTempRowid;    /* Register to hold temp table ROWID */
745       int addrL;           /* Label "L" */
746 
747       srcTab = pParse->nTab++;
748       regRec = sqlite3GetTempReg(pParse);
749       regTempRowid = sqlite3GetTempReg(pParse);
750       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
751       addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
752       sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
753       sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
754       sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
755       sqlite3VdbeGoto(v, addrL);
756       sqlite3VdbeJumpHere(v, addrL);
757       sqlite3ReleaseTempReg(pParse, regRec);
758       sqlite3ReleaseTempReg(pParse, regTempRowid);
759     }
760   }else{
761     /* This is the case if the data for the INSERT is coming from a
762     ** single-row VALUES clause
763     */
764     NameContext sNC;
765     memset(&sNC, 0, sizeof(sNC));
766     sNC.pParse = pParse;
767     srcTab = -1;
768     assert( useTempTable==0 );
769     if( pList ){
770       nColumn = pList->nExpr;
771       if( sqlite3ResolveExprListNames(&sNC, pList) ){
772         goto insert_cleanup;
773       }
774     }else{
775       nColumn = 0;
776     }
777   }
778 
779   /* If there is no IDLIST term but the table has an integer primary
780   ** key, the set the ipkColumn variable to the integer primary key
781   ** column index in the original table definition.
782   */
783   if( pColumn==0 && nColumn>0 ){
784     ipkColumn = pTab->iPKey;
785   }
786 
787   /* Make sure the number of columns in the source data matches the number
788   ** of columns to be inserted into the table.
789   */
790   for(i=0; i<pTab->nCol; i++){
791     nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
792   }
793   if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
794     sqlite3ErrorMsg(pParse,
795        "table %S has %d columns but %d values were supplied",
796        pTabList, 0, pTab->nCol-nHidden, nColumn);
797     goto insert_cleanup;
798   }
799   if( pColumn!=0 && nColumn!=pColumn->nId ){
800     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
801     goto insert_cleanup;
802   }
803 
804   /* Initialize the count of rows to be inserted
805   */
806   if( (db->flags & SQLITE_CountRows)!=0
807    && !pParse->nested
808    && !pParse->pTriggerTab
809   ){
810     regRowCount = ++pParse->nMem;
811     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
812   }
813 
814   /* If this is not a view, open the table and and all indices */
815   if( !isView ){
816     int nIdx;
817     nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
818                                       &iDataCur, &iIdxCur);
819     aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
820     if( aRegIdx==0 ){
821       goto insert_cleanup;
822     }
823     for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
824       assert( pIdx );
825       aRegIdx[i] = ++pParse->nMem;
826       pParse->nMem += pIdx->nColumn;
827     }
828     aRegIdx[i] = ++pParse->nMem;  /* Register to store the table record */
829   }
830 #ifndef SQLITE_OMIT_UPSERT
831   if( pUpsert ){
832     if( IsVirtual(pTab) ){
833       sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
834               pTab->zName);
835       goto insert_cleanup;
836     }
837     if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
838       goto insert_cleanup;
839     }
840     pTabList->a[0].iCursor = iDataCur;
841     pUpsert->pUpsertSrc = pTabList;
842     pUpsert->regData = regData;
843     pUpsert->iDataCur = iDataCur;
844     pUpsert->iIdxCur = iIdxCur;
845     if( pUpsert->pUpsertTarget ){
846       sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
847     }
848   }
849 #endif
850 
851 
852   /* This is the top of the main insertion loop */
853   if( useTempTable ){
854     /* This block codes the top of loop only.  The complete loop is the
855     ** following pseudocode (template 4):
856     **
857     **         rewind temp table, if empty goto D
858     **      C: loop over rows of intermediate table
859     **           transfer values form intermediate table into <table>
860     **         end loop
861     **      D: ...
862     */
863     addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
864     addrCont = sqlite3VdbeCurrentAddr(v);
865   }else if( pSelect ){
866     /* This block codes the top of loop only.  The complete loop is the
867     ** following pseudocode (template 3):
868     **
869     **      C: yield X, at EOF goto D
870     **         insert the select result into <table> from R..R+n
871     **         goto C
872     **      D: ...
873     */
874     addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
875     VdbeCoverage(v);
876   }
877 
878   /* Run the BEFORE and INSTEAD OF triggers, if there are any
879   */
880   endOfLoop = sqlite3VdbeMakeLabel(pParse);
881   if( tmask & TRIGGER_BEFORE ){
882     int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
883 
884     /* build the NEW.* reference row.  Note that if there is an INTEGER
885     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
886     ** translated into a unique ID for the row.  But on a BEFORE trigger,
887     ** we do not know what the unique ID will be (because the insert has
888     ** not happened yet) so we substitute a rowid of -1
889     */
890     if( ipkColumn<0 ){
891       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
892     }else{
893       int addr1;
894       assert( !withoutRowid );
895       if( useTempTable ){
896         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
897       }else{
898         assert( pSelect==0 );  /* Otherwise useTempTable is true */
899         sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
900       }
901       addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
902       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
903       sqlite3VdbeJumpHere(v, addr1);
904       sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
905     }
906 
907     /* Cannot have triggers on a virtual table. If it were possible,
908     ** this block would have to account for hidden column.
909     */
910     assert( !IsVirtual(pTab) );
911 
912     /* Create the new column data
913     */
914     for(i=j=0; i<pTab->nCol; i++){
915       if( pColumn ){
916         for(j=0; j<pColumn->nId; j++){
917           if( pColumn->a[j].idx==i ) break;
918         }
919       }
920       if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
921             || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){
922         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
923       }else if( useTempTable ){
924         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
925       }else{
926         assert( pSelect==0 ); /* Otherwise useTempTable is true */
927         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
928       }
929       if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++;
930     }
931 
932     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
933     ** do not attempt any conversions before assembling the record.
934     ** If this is a real table, attempt conversions as required by the
935     ** table column affinities.
936     */
937     if( !isView ){
938       sqlite3TableAffinity(v, pTab, regCols+1);
939     }
940 
941     /* Fire BEFORE or INSTEAD OF triggers */
942     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
943         pTab, regCols-pTab->nCol-1, onError, endOfLoop);
944 
945     sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
946   }
947 
948   /* Compute the content of the next row to insert into a range of
949   ** registers beginning at regIns.
950   */
951   if( !isView ){
952     if( IsVirtual(pTab) ){
953       /* The row that the VUpdate opcode will delete: none */
954       sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
955     }
956     if( ipkColumn>=0 ){
957       if( useTempTable ){
958         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
959       }else if( pSelect ){
960         sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
961       }else{
962         Expr *pIpk = pList->a[ipkColumn].pExpr;
963         if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
964           sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
965           appendFlag = 1;
966         }else{
967           sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
968         }
969       }
970       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
971       ** to generate a unique primary key value.
972       */
973       if( !appendFlag ){
974         int addr1;
975         if( !IsVirtual(pTab) ){
976           addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
977           sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
978           sqlite3VdbeJumpHere(v, addr1);
979         }else{
980           addr1 = sqlite3VdbeCurrentAddr(v);
981           sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v);
982         }
983         sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
984       }
985     }else if( IsVirtual(pTab) || withoutRowid ){
986       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
987     }else{
988       sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
989       appendFlag = 1;
990     }
991     autoIncStep(pParse, regAutoinc, regRowid);
992 
993     /* Compute data for all columns of the new entry, beginning
994     ** with the first column.
995     */
996     nHidden = 0;
997     for(i=0; i<pTab->nCol; i++){
998       int iRegStore = regRowid+1+i;
999       if( i==pTab->iPKey ){
1000         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
1001         ** Whenever this column is read, the rowid will be substituted
1002         ** in its place.  Hence, fill this column with a NULL to avoid
1003         ** taking up data space with information that will never be used.
1004         ** As there may be shallow copies of this value, make it a soft-NULL */
1005         sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
1006         continue;
1007       }
1008       if( pColumn==0 ){
1009         if( IsHiddenColumn(&pTab->aCol[i]) ){
1010           j = -1;
1011           nHidden++;
1012         }else{
1013           j = i - nHidden;
1014         }
1015       }else{
1016         for(j=0; j<pColumn->nId; j++){
1017           if( pColumn->a[j].idx==i ) break;
1018         }
1019       }
1020       if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
1021         sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
1022       }else if( useTempTable ){
1023         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
1024       }else if( pSelect ){
1025         if( regFromSelect!=regData ){
1026           sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
1027         }
1028       }else{
1029         sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
1030       }
1031     }
1032 
1033     /* Generate code to check constraints and generate index keys and
1034     ** do the insertion.
1035     */
1036 #ifndef SQLITE_OMIT_VIRTUALTABLE
1037     if( IsVirtual(pTab) ){
1038       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
1039       sqlite3VtabMakeWritable(pParse, pTab);
1040       sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
1041       sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
1042       sqlite3MayAbort(pParse);
1043     }else
1044 #endif
1045     {
1046       int isReplace;    /* Set to true if constraints may cause a replace */
1047       int bUseSeek;     /* True to use OPFLAG_SEEKRESULT */
1048       sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
1049           regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
1050       );
1051       sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
1052 
1053       /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
1054       ** constraints or (b) there are no triggers and this table is not a
1055       ** parent table in a foreign key constraint. It is safe to set the
1056       ** flag in the second case as if any REPLACE constraint is hit, an
1057       ** OP_Delete or OP_IdxDelete instruction will be executed on each
1058       ** cursor that is disturbed. And these instructions both clear the
1059       ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
1060       ** functionality.  */
1061       bUseSeek = (isReplace==0 || (pTrigger==0 &&
1062           ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0)
1063       ));
1064       sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
1065           regIns, aRegIdx, 0, appendFlag, bUseSeek
1066       );
1067     }
1068   }
1069 
1070   /* Update the count of rows that are inserted
1071   */
1072   if( regRowCount ){
1073     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
1074   }
1075 
1076   if( pTrigger ){
1077     /* Code AFTER triggers */
1078     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
1079         pTab, regData-2-pTab->nCol, onError, endOfLoop);
1080   }
1081 
1082   /* The bottom of the main insertion loop, if the data source
1083   ** is a SELECT statement.
1084   */
1085   sqlite3VdbeResolveLabel(v, endOfLoop);
1086   if( useTempTable ){
1087     sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
1088     sqlite3VdbeJumpHere(v, addrInsTop);
1089     sqlite3VdbeAddOp1(v, OP_Close, srcTab);
1090   }else if( pSelect ){
1091     sqlite3VdbeGoto(v, addrCont);
1092     sqlite3VdbeJumpHere(v, addrInsTop);
1093   }
1094 
1095 insert_end:
1096   /* Update the sqlite_sequence table by storing the content of the
1097   ** maximum rowid counter values recorded while inserting into
1098   ** autoincrement tables.
1099   */
1100   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
1101     sqlite3AutoincrementEnd(pParse);
1102   }
1103 
1104   /*
1105   ** Return the number of rows inserted. If this routine is
1106   ** generating code because of a call to sqlite3NestedParse(), do not
1107   ** invoke the callback function.
1108   */
1109   if( regRowCount ){
1110     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
1111     sqlite3VdbeSetNumCols(v, 1);
1112     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
1113   }
1114 
1115 insert_cleanup:
1116   sqlite3SrcListDelete(db, pTabList);
1117   sqlite3ExprListDelete(db, pList);
1118   sqlite3UpsertDelete(db, pUpsert);
1119   sqlite3SelectDelete(db, pSelect);
1120   sqlite3IdListDelete(db, pColumn);
1121   sqlite3DbFree(db, aRegIdx);
1122 }
1123 
1124 /* Make sure "isView" and other macros defined above are undefined. Otherwise
1125 ** they may interfere with compilation of other functions in this file
1126 ** (or in another file, if this file becomes part of the amalgamation).  */
1127 #ifdef isView
1128  #undef isView
1129 #endif
1130 #ifdef pTrigger
1131  #undef pTrigger
1132 #endif
1133 #ifdef tmask
1134  #undef tmask
1135 #endif
1136 
1137 /*
1138 ** Meanings of bits in of pWalker->eCode for
1139 ** sqlite3ExprReferencesUpdatedColumn()
1140 */
1141 #define CKCNSTRNT_COLUMN   0x01    /* CHECK constraint uses a changing column */
1142 #define CKCNSTRNT_ROWID    0x02    /* CHECK constraint references the ROWID */
1143 
1144 /* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
1145 *  Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
1146 ** expression node references any of the
1147 ** columns that are being modifed by an UPDATE statement.
1148 */
1149 static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
1150   if( pExpr->op==TK_COLUMN ){
1151     assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
1152     if( pExpr->iColumn>=0 ){
1153       if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
1154         pWalker->eCode |= CKCNSTRNT_COLUMN;
1155       }
1156     }else{
1157       pWalker->eCode |= CKCNSTRNT_ROWID;
1158     }
1159   }
1160   return WRC_Continue;
1161 }
1162 
1163 /*
1164 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed.  The
1165 ** only columns that are modified by the UPDATE are those for which
1166 ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
1167 **
1168 ** Return true if CHECK constraint pExpr uses any of the
1169 ** changing columns (or the rowid if it is changing).  In other words,
1170 ** return true if this CHECK constraint must be validated for
1171 ** the new row in the UPDATE statement.
1172 **
1173 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
1174 ** The operation of this routine is the same - return true if an only if
1175 ** the expression uses one or more of columns identified by the second and
1176 ** third arguments.
1177 */
1178 int sqlite3ExprReferencesUpdatedColumn(
1179   Expr *pExpr,    /* The expression to be checked */
1180   int *aiChng,    /* aiChng[x]>=0 if column x changed by the UPDATE */
1181   int chngRowid   /* True if UPDATE changes the rowid */
1182 ){
1183   Walker w;
1184   memset(&w, 0, sizeof(w));
1185   w.eCode = 0;
1186   w.xExprCallback = checkConstraintExprNode;
1187   w.u.aiCol = aiChng;
1188   sqlite3WalkExpr(&w, pExpr);
1189   if( !chngRowid ){
1190     testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
1191     w.eCode &= ~CKCNSTRNT_ROWID;
1192   }
1193   testcase( w.eCode==0 );
1194   testcase( w.eCode==CKCNSTRNT_COLUMN );
1195   testcase( w.eCode==CKCNSTRNT_ROWID );
1196   testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
1197   return w.eCode!=0;
1198 }
1199 
1200 /*
1201 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
1202 ** on table pTab.
1203 **
1204 ** The regNewData parameter is the first register in a range that contains
1205 ** the data to be inserted or the data after the update.  There will be
1206 ** pTab->nCol+1 registers in this range.  The first register (the one
1207 ** that regNewData points to) will contain the new rowid, or NULL in the
1208 ** case of a WITHOUT ROWID table.  The second register in the range will
1209 ** contain the content of the first table column.  The third register will
1210 ** contain the content of the second table column.  And so forth.
1211 **
1212 ** The regOldData parameter is similar to regNewData except that it contains
1213 ** the data prior to an UPDATE rather than afterwards.  regOldData is zero
1214 ** for an INSERT.  This routine can distinguish between UPDATE and INSERT by
1215 ** checking regOldData for zero.
1216 **
1217 ** For an UPDATE, the pkChng boolean is true if the true primary key (the
1218 ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
1219 ** might be modified by the UPDATE.  If pkChng is false, then the key of
1220 ** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
1221 **
1222 ** For an INSERT, the pkChng boolean indicates whether or not the rowid
1223 ** was explicitly specified as part of the INSERT statement.  If pkChng
1224 ** is zero, it means that the either rowid is computed automatically or
1225 ** that the table is a WITHOUT ROWID table and has no rowid.  On an INSERT,
1226 ** pkChng will only be true if the INSERT statement provides an integer
1227 ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
1228 **
1229 ** The code generated by this routine will store new index entries into
1230 ** registers identified by aRegIdx[].  No index entry is created for
1231 ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
1232 ** the same as the order of indices on the linked list of indices
1233 ** at pTab->pIndex.
1234 **
1235 ** (2019-05-07) The generated code also creates a new record for the
1236 ** main table, if pTab is a rowid table, and stores that record in the
1237 ** register identified by aRegIdx[nIdx] - in other words in the first
1238 ** entry of aRegIdx[] past the last index.  It is important that the
1239 ** record be generated during constraint checks to avoid affinity changes
1240 ** to the register content that occur after constraint checks but before
1241 ** the new record is inserted.
1242 **
1243 ** The caller must have already opened writeable cursors on the main
1244 ** table and all applicable indices (that is to say, all indices for which
1245 ** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
1246 ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
1247 ** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
1248 ** for the first index in the pTab->pIndex list.  Cursors for other indices
1249 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
1250 **
1251 ** This routine also generates code to check constraints.  NOT NULL,
1252 ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
1253 ** then the appropriate action is performed.  There are five possible
1254 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
1255 **
1256 **  Constraint type  Action       What Happens
1257 **  ---------------  ----------   ----------------------------------------
1258 **  any              ROLLBACK     The current transaction is rolled back and
1259 **                                sqlite3_step() returns immediately with a
1260 **                                return code of SQLITE_CONSTRAINT.
1261 **
1262 **  any              ABORT        Back out changes from the current command
1263 **                                only (do not do a complete rollback) then
1264 **                                cause sqlite3_step() to return immediately
1265 **                                with SQLITE_CONSTRAINT.
1266 **
1267 **  any              FAIL         Sqlite3_step() returns immediately with a
1268 **                                return code of SQLITE_CONSTRAINT.  The
1269 **                                transaction is not rolled back and any
1270 **                                changes to prior rows are retained.
1271 **
1272 **  any              IGNORE       The attempt in insert or update the current
1273 **                                row is skipped, without throwing an error.
1274 **                                Processing continues with the next row.
1275 **                                (There is an immediate jump to ignoreDest.)
1276 **
1277 **  NOT NULL         REPLACE      The NULL value is replace by the default
1278 **                                value for that column.  If the default value
1279 **                                is NULL, the action is the same as ABORT.
1280 **
1281 **  UNIQUE           REPLACE      The other row that conflicts with the row
1282 **                                being inserted is removed.
1283 **
1284 **  CHECK            REPLACE      Illegal.  The results in an exception.
1285 **
1286 ** Which action to take is determined by the overrideError parameter.
1287 ** Or if overrideError==OE_Default, then the pParse->onError parameter
1288 ** is used.  Or if pParse->onError==OE_Default then the onError value
1289 ** for the constraint is used.
1290 */
1291 void sqlite3GenerateConstraintChecks(
1292   Parse *pParse,       /* The parser context */
1293   Table *pTab,         /* The table being inserted or updated */
1294   int *aRegIdx,        /* Use register aRegIdx[i] for index i.  0 for unused */
1295   int iDataCur,        /* Canonical data cursor (main table or PK index) */
1296   int iIdxCur,         /* First index cursor */
1297   int regNewData,      /* First register in a range holding values to insert */
1298   int regOldData,      /* Previous content.  0 for INSERTs */
1299   u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
1300   u8 overrideError,    /* Override onError to this if not OE_Default */
1301   int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
1302   int *pbMayReplace,   /* OUT: Set to true if constraint may cause a replace */
1303   int *aiChng,         /* column i is unchanged if aiChng[i]<0 */
1304   Upsert *pUpsert      /* ON CONFLICT clauses, if any.  NULL otherwise */
1305 ){
1306   Vdbe *v;             /* VDBE under constrution */
1307   Index *pIdx;         /* Pointer to one of the indices */
1308   Index *pPk = 0;      /* The PRIMARY KEY index */
1309   sqlite3 *db;         /* Database connection */
1310   int i;               /* loop counter */
1311   int ix;              /* Index loop counter */
1312   int nCol;            /* Number of columns */
1313   int onError;         /* Conflict resolution strategy */
1314   int addr1;           /* Address of jump instruction */
1315   int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
1316   int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
1317   Index *pUpIdx = 0;   /* Index to which to apply the upsert */
1318   u8 isUpdate;         /* True if this is an UPDATE operation */
1319   u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
1320   int upsertBypass = 0;  /* Address of Goto to bypass upsert subroutine */
1321   int upsertJump = 0;    /* Address of Goto that jumps into upsert subroutine */
1322   int ipkTop = 0;        /* Top of the IPK uniqueness check */
1323   int ipkBottom = 0;     /* OP_Goto at the end of the IPK uniqueness check */
1324 
1325   isUpdate = regOldData!=0;
1326   db = pParse->db;
1327   v = sqlite3GetVdbe(pParse);
1328   assert( v!=0 );
1329   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
1330   nCol = pTab->nCol;
1331 
1332   /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
1333   ** normal rowid tables.  nPkField is the number of key fields in the
1334   ** pPk index or 1 for a rowid table.  In other words, nPkField is the
1335   ** number of fields in the true primary key of the table. */
1336   if( HasRowid(pTab) ){
1337     pPk = 0;
1338     nPkField = 1;
1339   }else{
1340     pPk = sqlite3PrimaryKeyIndex(pTab);
1341     nPkField = pPk->nKeyCol;
1342   }
1343 
1344   /* Record that this module has started */
1345   VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
1346                      iDataCur, iIdxCur, regNewData, regOldData, pkChng));
1347 
1348   /* Test all NOT NULL constraints.
1349   */
1350   for(i=0; i<nCol; i++){
1351     if( i==pTab->iPKey ){
1352       continue;        /* ROWID is never NULL */
1353     }
1354     if( aiChng && aiChng[i]<0 ){
1355       /* Don't bother checking for NOT NULL on columns that do not change */
1356       continue;
1357     }
1358     onError = pTab->aCol[i].notNull;
1359     if( onError==OE_None ) continue;  /* This column is allowed to be NULL */
1360     if( overrideError!=OE_Default ){
1361       onError = overrideError;
1362     }else if( onError==OE_Default ){
1363       onError = OE_Abort;
1364     }
1365     if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
1366       onError = OE_Abort;
1367     }
1368     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1369         || onError==OE_Ignore || onError==OE_Replace );
1370     addr1 = 0;
1371     switch( onError ){
1372       case OE_Replace: {
1373         assert( onError==OE_Replace );
1374         addr1 = sqlite3VdbeMakeLabel(pParse);
1375         sqlite3VdbeAddOp2(v, OP_NotNull, regNewData+1+i, addr1);
1376           VdbeCoverage(v);
1377         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
1378         sqlite3VdbeAddOp2(v, OP_NotNull, regNewData+1+i, addr1);
1379           VdbeCoverage(v);
1380         onError = OE_Abort;
1381         /* Fall through into the OE_Abort case to generate code that runs
1382         ** if both the input and the default value are NULL */
1383       }
1384       case OE_Abort:
1385         sqlite3MayAbort(pParse);
1386         /* Fall through */
1387       case OE_Rollback:
1388       case OE_Fail: {
1389         char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
1390                                     pTab->aCol[i].zName);
1391         sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
1392                           regNewData+1+i);
1393         sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
1394         sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
1395         VdbeCoverage(v);
1396         if( addr1 ) sqlite3VdbeResolveLabel(v, addr1);
1397         break;
1398       }
1399       default: {
1400         assert( onError==OE_Ignore );
1401         sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
1402         VdbeCoverage(v);
1403         break;
1404       }
1405     }
1406   }
1407 
1408   /* Test all CHECK constraints
1409   */
1410 #ifndef SQLITE_OMIT_CHECK
1411   if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
1412     ExprList *pCheck = pTab->pCheck;
1413     pParse->iSelfTab = -(regNewData+1);
1414     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
1415     for(i=0; i<pCheck->nExpr; i++){
1416       int allOk;
1417       Expr *pExpr = pCheck->a[i].pExpr;
1418       if( aiChng
1419        && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
1420       ){
1421         /* The check constraints do not reference any of the columns being
1422         ** updated so there is no point it verifying the check constraint */
1423         continue;
1424       }
1425       allOk = sqlite3VdbeMakeLabel(pParse);
1426       sqlite3VdbeVerifyAbortable(v, onError);
1427       sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
1428       if( onError==OE_Ignore ){
1429         sqlite3VdbeGoto(v, ignoreDest);
1430       }else{
1431         char *zName = pCheck->a[i].zName;
1432         if( zName==0 ) zName = pTab->zName;
1433         if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
1434         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
1435                               onError, zName, P4_TRANSIENT,
1436                               P5_ConstraintCheck);
1437       }
1438       sqlite3VdbeResolveLabel(v, allOk);
1439     }
1440     pParse->iSelfTab = 0;
1441   }
1442 #endif /* !defined(SQLITE_OMIT_CHECK) */
1443 
1444   /* UNIQUE and PRIMARY KEY constraints should be handled in the following
1445   ** order:
1446   **
1447   **   (1)  OE_Update
1448   **   (2)  OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
1449   **   (3)  OE_Replace
1450   **
1451   ** OE_Fail and OE_Ignore must happen before any changes are made.
1452   ** OE_Update guarantees that only a single row will change, so it
1453   ** must happen before OE_Replace.  Technically, OE_Abort and OE_Rollback
1454   ** could happen in any order, but they are grouped up front for
1455   ** convenience.
1456   **
1457   ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
1458   ** The order of constraints used to have OE_Update as (2) and OE_Abort
1459   ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
1460   ** constraint before any others, so it had to be moved.
1461   **
1462   ** Constraint checking code is generated in this order:
1463   **   (A)  The rowid constraint
1464   **   (B)  Unique index constraints that do not have OE_Replace as their
1465   **        default conflict resolution strategy
1466   **   (C)  Unique index that do use OE_Replace by default.
1467   **
1468   ** The ordering of (2) and (3) is accomplished by making sure the linked
1469   ** list of indexes attached to a table puts all OE_Replace indexes last
1470   ** in the list.  See sqlite3CreateIndex() for where that happens.
1471   */
1472 
1473   if( pUpsert ){
1474     if( pUpsert->pUpsertTarget==0 ){
1475       /* An ON CONFLICT DO NOTHING clause, without a constraint-target.
1476       ** Make all unique constraint resolution be OE_Ignore */
1477       assert( pUpsert->pUpsertSet==0 );
1478       overrideError = OE_Ignore;
1479       pUpsert = 0;
1480     }else if( (pUpIdx = pUpsert->pUpsertIdx)!=0 ){
1481       /* If the constraint-target uniqueness check must be run first.
1482       ** Jump to that uniqueness check now */
1483       upsertJump = sqlite3VdbeAddOp0(v, OP_Goto);
1484       VdbeComment((v, "UPSERT constraint goes first"));
1485     }
1486   }
1487 
1488   /* If rowid is changing, make sure the new rowid does not previously
1489   ** exist in the table.
1490   */
1491   if( pkChng && pPk==0 ){
1492     int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
1493 
1494     /* Figure out what action to take in case of a rowid collision */
1495     onError = pTab->keyConf;
1496     if( overrideError!=OE_Default ){
1497       onError = overrideError;
1498     }else if( onError==OE_Default ){
1499       onError = OE_Abort;
1500     }
1501 
1502     /* figure out whether or not upsert applies in this case */
1503     if( pUpsert && pUpsert->pUpsertIdx==0 ){
1504       if( pUpsert->pUpsertSet==0 ){
1505         onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
1506       }else{
1507         onError = OE_Update;  /* DO UPDATE */
1508       }
1509     }
1510 
1511     /* If the response to a rowid conflict is REPLACE but the response
1512     ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
1513     ** to defer the running of the rowid conflict checking until after
1514     ** the UNIQUE constraints have run.
1515     */
1516     if( onError==OE_Replace      /* IPK rule is REPLACE */
1517      && onError!=overrideError   /* Rules for other contraints are different */
1518      && pTab->pIndex             /* There exist other constraints */
1519     ){
1520       ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
1521       VdbeComment((v, "defer IPK REPLACE until last"));
1522     }
1523 
1524     if( isUpdate ){
1525       /* pkChng!=0 does not mean that the rowid has changed, only that
1526       ** it might have changed.  Skip the conflict logic below if the rowid
1527       ** is unchanged. */
1528       sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
1529       sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
1530       VdbeCoverage(v);
1531     }
1532 
1533     /* Check to see if the new rowid already exists in the table.  Skip
1534     ** the following conflict logic if it does not. */
1535     VdbeNoopComment((v, "uniqueness check for ROWID"));
1536     sqlite3VdbeVerifyAbortable(v, onError);
1537     sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
1538     VdbeCoverage(v);
1539 
1540     switch( onError ){
1541       default: {
1542         onError = OE_Abort;
1543         /* Fall thru into the next case */
1544       }
1545       case OE_Rollback:
1546       case OE_Abort:
1547       case OE_Fail: {
1548         testcase( onError==OE_Rollback );
1549         testcase( onError==OE_Abort );
1550         testcase( onError==OE_Fail );
1551         sqlite3RowidConstraint(pParse, onError, pTab);
1552         break;
1553       }
1554       case OE_Replace: {
1555         /* If there are DELETE triggers on this table and the
1556         ** recursive-triggers flag is set, call GenerateRowDelete() to
1557         ** remove the conflicting row from the table. This will fire
1558         ** the triggers and remove both the table and index b-tree entries.
1559         **
1560         ** Otherwise, if there are no triggers or the recursive-triggers
1561         ** flag is not set, but the table has one or more indexes, call
1562         ** GenerateRowIndexDelete(). This removes the index b-tree entries
1563         ** only. The table b-tree entry will be replaced by the new entry
1564         ** when it is inserted.
1565         **
1566         ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
1567         ** also invoke MultiWrite() to indicate that this VDBE may require
1568         ** statement rollback (if the statement is aborted after the delete
1569         ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
1570         ** but being more selective here allows statements like:
1571         **
1572         **   REPLACE INTO t(rowid) VALUES($newrowid)
1573         **
1574         ** to run without a statement journal if there are no indexes on the
1575         ** table.
1576         */
1577         Trigger *pTrigger = 0;
1578         if( db->flags&SQLITE_RecTriggers ){
1579           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
1580         }
1581         if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
1582           sqlite3MultiWrite(pParse);
1583           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
1584                                    regNewData, 1, 0, OE_Replace, 1, -1);
1585         }else{
1586 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1587           assert( HasRowid(pTab) );
1588           /* This OP_Delete opcode fires the pre-update-hook only. It does
1589           ** not modify the b-tree. It is more efficient to let the coming
1590           ** OP_Insert replace the existing entry than it is to delete the
1591           ** existing entry and then insert a new one. */
1592           sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
1593           sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
1594 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
1595           if( pTab->pIndex ){
1596             sqlite3MultiWrite(pParse);
1597             sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
1598           }
1599         }
1600         seenReplace = 1;
1601         break;
1602       }
1603 #ifndef SQLITE_OMIT_UPSERT
1604       case OE_Update: {
1605         sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
1606         /* Fall through */
1607       }
1608 #endif
1609       case OE_Ignore: {
1610         testcase( onError==OE_Ignore );
1611         sqlite3VdbeGoto(v, ignoreDest);
1612         break;
1613       }
1614     }
1615     sqlite3VdbeResolveLabel(v, addrRowidOk);
1616     if( ipkTop ){
1617       ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
1618       sqlite3VdbeJumpHere(v, ipkTop-1);
1619     }
1620   }
1621 
1622   /* Test all UNIQUE constraints by creating entries for each UNIQUE
1623   ** index and making sure that duplicate entries do not already exist.
1624   ** Compute the revised record entries for indices as we go.
1625   **
1626   ** This loop also handles the case of the PRIMARY KEY index for a
1627   ** WITHOUT ROWID table.
1628   */
1629   for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
1630     int regIdx;          /* Range of registers hold conent for pIdx */
1631     int regR;            /* Range of registers holding conflicting PK */
1632     int iThisCur;        /* Cursor for this UNIQUE index */
1633     int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
1634 
1635     if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
1636     if( pUpIdx==pIdx ){
1637       addrUniqueOk = upsertJump+1;
1638       upsertBypass = sqlite3VdbeGoto(v, 0);
1639       VdbeComment((v, "Skip upsert subroutine"));
1640       sqlite3VdbeJumpHere(v, upsertJump);
1641     }else{
1642       addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
1643     }
1644     if( bAffinityDone==0 && (pUpIdx==0 || pUpIdx==pIdx) ){
1645       sqlite3TableAffinity(v, pTab, regNewData+1);
1646       bAffinityDone = 1;
1647     }
1648     VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName));
1649     iThisCur = iIdxCur+ix;
1650 
1651 
1652     /* Skip partial indices for which the WHERE clause is not true */
1653     if( pIdx->pPartIdxWhere ){
1654       sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
1655       pParse->iSelfTab = -(regNewData+1);
1656       sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
1657                             SQLITE_JUMPIFNULL);
1658       pParse->iSelfTab = 0;
1659     }
1660 
1661     /* Create a record for this index entry as it should appear after
1662     ** the insert or update.  Store that record in the aRegIdx[ix] register
1663     */
1664     regIdx = aRegIdx[ix]+1;
1665     for(i=0; i<pIdx->nColumn; i++){
1666       int iField = pIdx->aiColumn[i];
1667       int x;
1668       if( iField==XN_EXPR ){
1669         pParse->iSelfTab = -(regNewData+1);
1670         sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
1671         pParse->iSelfTab = 0;
1672         VdbeComment((v, "%s column %d", pIdx->zName, i));
1673       }else{
1674         if( iField==XN_ROWID || iField==pTab->iPKey ){
1675           x = regNewData;
1676         }else{
1677           x = iField + regNewData + 1;
1678         }
1679         sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
1680         VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
1681       }
1682     }
1683     sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
1684     VdbeComment((v, "for %s", pIdx->zName));
1685 #ifdef SQLITE_ENABLE_NULL_TRIM
1686     if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
1687       sqlite3SetMakeRecordP5(v, pIdx->pTable);
1688     }
1689 #endif
1690 
1691     /* In an UPDATE operation, if this index is the PRIMARY KEY index
1692     ** of a WITHOUT ROWID table and there has been no change the
1693     ** primary key, then no collision is possible.  The collision detection
1694     ** logic below can all be skipped. */
1695     if( isUpdate && pPk==pIdx && pkChng==0 ){
1696       sqlite3VdbeResolveLabel(v, addrUniqueOk);
1697       continue;
1698     }
1699 
1700     /* Find out what action to take in case there is a uniqueness conflict */
1701     onError = pIdx->onError;
1702     if( onError==OE_None ){
1703       sqlite3VdbeResolveLabel(v, addrUniqueOk);
1704       continue;  /* pIdx is not a UNIQUE index */
1705     }
1706     if( overrideError!=OE_Default ){
1707       onError = overrideError;
1708     }else if( onError==OE_Default ){
1709       onError = OE_Abort;
1710     }
1711 
1712     /* Figure out if the upsert clause applies to this index */
1713     if( pUpIdx==pIdx ){
1714       if( pUpsert->pUpsertSet==0 ){
1715         onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
1716       }else{
1717         onError = OE_Update;  /* DO UPDATE */
1718       }
1719     }
1720 
1721     /* Collision detection may be omitted if all of the following are true:
1722     **   (1) The conflict resolution algorithm is REPLACE
1723     **   (2) The table is a WITHOUT ROWID table
1724     **   (3) There are no secondary indexes on the table
1725     **   (4) No delete triggers need to be fired if there is a conflict
1726     **   (5) No FK constraint counters need to be updated if a conflict occurs.
1727     **
1728     ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
1729     ** must be explicitly deleted in order to ensure any pre-update hook
1730     ** is invoked.  */
1731 #ifndef SQLITE_ENABLE_PREUPDATE_HOOK
1732     if( (ix==0 && pIdx->pNext==0)                   /* Condition 3 */
1733      && pPk==pIdx                                   /* Condition 2 */
1734      && onError==OE_Replace                         /* Condition 1 */
1735      && ( 0==(db->flags&SQLITE_RecTriggers) ||      /* Condition 4 */
1736           0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
1737      && ( 0==(db->flags&SQLITE_ForeignKeys) ||      /* Condition 5 */
1738          (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
1739     ){
1740       sqlite3VdbeResolveLabel(v, addrUniqueOk);
1741       continue;
1742     }
1743 #endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
1744 
1745     /* Check to see if the new index entry will be unique */
1746     sqlite3VdbeVerifyAbortable(v, onError);
1747     sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
1748                          regIdx, pIdx->nKeyCol); VdbeCoverage(v);
1749 
1750     /* Generate code to handle collisions */
1751     regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
1752     if( isUpdate || onError==OE_Replace ){
1753       if( HasRowid(pTab) ){
1754         sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
1755         /* Conflict only if the rowid of the existing index entry
1756         ** is different from old-rowid */
1757         if( isUpdate ){
1758           sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
1759           sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
1760           VdbeCoverage(v);
1761         }
1762       }else{
1763         int x;
1764         /* Extract the PRIMARY KEY from the end of the index entry and
1765         ** store it in registers regR..regR+nPk-1 */
1766         if( pIdx!=pPk ){
1767           for(i=0; i<pPk->nKeyCol; i++){
1768             assert( pPk->aiColumn[i]>=0 );
1769             x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
1770             sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
1771             VdbeComment((v, "%s.%s", pTab->zName,
1772                          pTab->aCol[pPk->aiColumn[i]].zName));
1773           }
1774         }
1775         if( isUpdate ){
1776           /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
1777           ** table, only conflict if the new PRIMARY KEY values are actually
1778           ** different from the old.
1779           **
1780           ** For a UNIQUE index, only conflict if the PRIMARY KEY values
1781           ** of the matched index row are different from the original PRIMARY
1782           ** KEY values of this row before the update.  */
1783           int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
1784           int op = OP_Ne;
1785           int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
1786 
1787           for(i=0; i<pPk->nKeyCol; i++){
1788             char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
1789             x = pPk->aiColumn[i];
1790             assert( x>=0 );
1791             if( i==(pPk->nKeyCol-1) ){
1792               addrJump = addrUniqueOk;
1793               op = OP_Eq;
1794             }
1795             sqlite3VdbeAddOp4(v, op,
1796                 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
1797             );
1798             sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
1799             VdbeCoverageIf(v, op==OP_Eq);
1800             VdbeCoverageIf(v, op==OP_Ne);
1801           }
1802         }
1803       }
1804     }
1805 
1806     /* Generate code that executes if the new index entry is not unique */
1807     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1808         || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
1809     switch( onError ){
1810       case OE_Rollback:
1811       case OE_Abort:
1812       case OE_Fail: {
1813         testcase( onError==OE_Rollback );
1814         testcase( onError==OE_Abort );
1815         testcase( onError==OE_Fail );
1816         sqlite3UniqueConstraint(pParse, onError, pIdx);
1817         break;
1818       }
1819 #ifndef SQLITE_OMIT_UPSERT
1820       case OE_Update: {
1821         sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
1822         /* Fall through */
1823       }
1824 #endif
1825       case OE_Ignore: {
1826         testcase( onError==OE_Ignore );
1827         sqlite3VdbeGoto(v, ignoreDest);
1828         break;
1829       }
1830       default: {
1831         Trigger *pTrigger = 0;
1832         assert( onError==OE_Replace );
1833         if( db->flags&SQLITE_RecTriggers ){
1834           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
1835         }
1836         if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
1837           sqlite3MultiWrite(pParse);
1838         }
1839         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
1840             regR, nPkField, 0, OE_Replace,
1841             (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
1842         seenReplace = 1;
1843         break;
1844       }
1845     }
1846     if( pUpIdx==pIdx ){
1847       sqlite3VdbeGoto(v, upsertJump+1);
1848       sqlite3VdbeJumpHere(v, upsertBypass);
1849     }else{
1850       sqlite3VdbeResolveLabel(v, addrUniqueOk);
1851     }
1852     if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
1853   }
1854 
1855   /* If the IPK constraint is a REPLACE, run it last */
1856   if( ipkTop ){
1857     sqlite3VdbeGoto(v, ipkTop);
1858     VdbeComment((v, "Do IPK REPLACE"));
1859     sqlite3VdbeJumpHere(v, ipkBottom);
1860   }
1861 
1862   /* Generate the table record */
1863   if( HasRowid(pTab) ){
1864     int regRec = aRegIdx[ix];
1865     sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nCol, regRec);
1866     sqlite3SetMakeRecordP5(v, pTab);
1867     if( !bAffinityDone ){
1868       sqlite3TableAffinity(v, pTab, 0);
1869     }
1870   }
1871 
1872   *pbMayReplace = seenReplace;
1873   VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
1874 }
1875 
1876 #ifdef SQLITE_ENABLE_NULL_TRIM
1877 /*
1878 ** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
1879 ** to be the number of columns in table pTab that must not be NULL-trimmed.
1880 **
1881 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
1882 */
1883 void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
1884   u16 i;
1885 
1886   /* Records with omitted columns are only allowed for schema format
1887   ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
1888   if( pTab->pSchema->file_format<2 ) return;
1889 
1890   for(i=pTab->nCol-1; i>0; i--){
1891     if( pTab->aCol[i].pDflt!=0 ) break;
1892     if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
1893   }
1894   sqlite3VdbeChangeP5(v, i+1);
1895 }
1896 #endif
1897 
1898 /*
1899 ** This routine generates code to finish the INSERT or UPDATE operation
1900 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
1901 ** A consecutive range of registers starting at regNewData contains the
1902 ** rowid and the content to be inserted.
1903 **
1904 ** The arguments to this routine should be the same as the first six
1905 ** arguments to sqlite3GenerateConstraintChecks.
1906 */
1907 void sqlite3CompleteInsertion(
1908   Parse *pParse,      /* The parser context */
1909   Table *pTab,        /* the table into which we are inserting */
1910   int iDataCur,       /* Cursor of the canonical data source */
1911   int iIdxCur,        /* First index cursor */
1912   int regNewData,     /* Range of content */
1913   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
1914   int update_flags,   /* True for UPDATE, False for INSERT */
1915   int appendBias,     /* True if this is likely to be an append */
1916   int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
1917 ){
1918   Vdbe *v;            /* Prepared statements under construction */
1919   Index *pIdx;        /* An index being inserted or updated */
1920   u8 pik_flags;       /* flag values passed to the btree insert */
1921   int i;              /* Loop counter */
1922 
1923   assert( update_flags==0
1924        || update_flags==OPFLAG_ISUPDATE
1925        || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
1926   );
1927 
1928   v = sqlite3GetVdbe(pParse);
1929   assert( v!=0 );
1930   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
1931   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1932     if( aRegIdx[i]==0 ) continue;
1933     if( pIdx->pPartIdxWhere ){
1934       sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
1935       VdbeCoverage(v);
1936     }
1937     pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
1938     if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
1939       assert( pParse->nested==0 );
1940       pik_flags |= OPFLAG_NCHANGE;
1941       pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
1942 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
1943       if( update_flags==0 ){
1944         int r = sqlite3GetTempReg(pParse);
1945         sqlite3VdbeAddOp2(v, OP_Integer, 0, r);
1946         sqlite3VdbeAddOp4(v, OP_Insert,
1947             iIdxCur+i, aRegIdx[i], r, (char*)pTab, P4_TABLE
1948         );
1949         sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
1950         sqlite3ReleaseTempReg(pParse, r);
1951       }
1952 #endif
1953     }
1954     sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
1955                          aRegIdx[i]+1,
1956                          pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
1957     sqlite3VdbeChangeP5(v, pik_flags);
1958   }
1959   if( !HasRowid(pTab) ) return;
1960   if( pParse->nested ){
1961     pik_flags = 0;
1962   }else{
1963     pik_flags = OPFLAG_NCHANGE;
1964     pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
1965   }
1966   if( appendBias ){
1967     pik_flags |= OPFLAG_APPEND;
1968   }
1969   if( useSeekResult ){
1970     pik_flags |= OPFLAG_USESEEKRESULT;
1971   }
1972   sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, aRegIdx[i], regNewData);
1973   if( !pParse->nested ){
1974     sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
1975   }
1976   sqlite3VdbeChangeP5(v, pik_flags);
1977 }
1978 
1979 /*
1980 ** Allocate cursors for the pTab table and all its indices and generate
1981 ** code to open and initialized those cursors.
1982 **
1983 ** The cursor for the object that contains the complete data (normally
1984 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
1985 ** ROWID table) is returned in *piDataCur.  The first index cursor is
1986 ** returned in *piIdxCur.  The number of indices is returned.
1987 **
1988 ** Use iBase as the first cursor (either the *piDataCur for rowid tables
1989 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
1990 ** If iBase is negative, then allocate the next available cursor.
1991 **
1992 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
1993 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
1994 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
1995 ** pTab->pIndex list.
1996 **
1997 ** If pTab is a virtual table, then this routine is a no-op and the
1998 ** *piDataCur and *piIdxCur values are left uninitialized.
1999 */
2000 int sqlite3OpenTableAndIndices(
2001   Parse *pParse,   /* Parsing context */
2002   Table *pTab,     /* Table to be opened */
2003   int op,          /* OP_OpenRead or OP_OpenWrite */
2004   u8 p5,           /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
2005   int iBase,       /* Use this for the table cursor, if there is one */
2006   u8 *aToOpen,     /* If not NULL: boolean for each table and index */
2007   int *piDataCur,  /* Write the database source cursor number here */
2008   int *piIdxCur    /* Write the first index cursor number here */
2009 ){
2010   int i;
2011   int iDb;
2012   int iDataCur;
2013   Index *pIdx;
2014   Vdbe *v;
2015 
2016   assert( op==OP_OpenRead || op==OP_OpenWrite );
2017   assert( op==OP_OpenWrite || p5==0 );
2018   if( IsVirtual(pTab) ){
2019     /* This routine is a no-op for virtual tables. Leave the output
2020     ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
2021     ** can detect if they are used by mistake in the caller. */
2022     return 0;
2023   }
2024   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
2025   v = sqlite3GetVdbe(pParse);
2026   assert( v!=0 );
2027   if( iBase<0 ) iBase = pParse->nTab;
2028   iDataCur = iBase++;
2029   if( piDataCur ) *piDataCur = iDataCur;
2030   if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
2031     sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
2032   }else{
2033     sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
2034   }
2035   if( piIdxCur ) *piIdxCur = iBase;
2036   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
2037     int iIdxCur = iBase++;
2038     assert( pIdx->pSchema==pTab->pSchema );
2039     if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
2040       if( piDataCur ) *piDataCur = iIdxCur;
2041       p5 = 0;
2042     }
2043     if( aToOpen==0 || aToOpen[i+1] ){
2044       sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
2045       sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
2046       sqlite3VdbeChangeP5(v, p5);
2047       VdbeComment((v, "%s", pIdx->zName));
2048     }
2049   }
2050   if( iBase>pParse->nTab ) pParse->nTab = iBase;
2051   return i;
2052 }
2053 
2054 
2055 #ifdef SQLITE_TEST
2056 /*
2057 ** The following global variable is incremented whenever the
2058 ** transfer optimization is used.  This is used for testing
2059 ** purposes only - to make sure the transfer optimization really
2060 ** is happening when it is supposed to.
2061 */
2062 int sqlite3_xferopt_count;
2063 #endif /* SQLITE_TEST */
2064 
2065 
2066 #ifndef SQLITE_OMIT_XFER_OPT
2067 /*
2068 ** Check to see if index pSrc is compatible as a source of data
2069 ** for index pDest in an insert transfer optimization.  The rules
2070 ** for a compatible index:
2071 **
2072 **    *   The index is over the same set of columns
2073 **    *   The same DESC and ASC markings occurs on all columns
2074 **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
2075 **    *   The same collating sequence on each column
2076 **    *   The index has the exact same WHERE clause
2077 */
2078 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
2079   int i;
2080   assert( pDest && pSrc );
2081   assert( pDest->pTable!=pSrc->pTable );
2082   if( pDest->nKeyCol!=pSrc->nKeyCol ){
2083     return 0;   /* Different number of columns */
2084   }
2085   if( pDest->onError!=pSrc->onError ){
2086     return 0;   /* Different conflict resolution strategies */
2087   }
2088   for(i=0; i<pSrc->nKeyCol; i++){
2089     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
2090       return 0;   /* Different columns indexed */
2091     }
2092     if( pSrc->aiColumn[i]==XN_EXPR ){
2093       assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
2094       if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
2095                              pDest->aColExpr->a[i].pExpr, -1)!=0 ){
2096         return 0;   /* Different expressions in the index */
2097       }
2098     }
2099     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
2100       return 0;   /* Different sort orders */
2101     }
2102     if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
2103       return 0;   /* Different collating sequences */
2104     }
2105   }
2106   if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
2107     return 0;     /* Different WHERE clauses */
2108   }
2109 
2110   /* If no test above fails then the indices must be compatible */
2111   return 1;
2112 }
2113 
2114 /*
2115 ** Attempt the transfer optimization on INSERTs of the form
2116 **
2117 **     INSERT INTO tab1 SELECT * FROM tab2;
2118 **
2119 ** The xfer optimization transfers raw records from tab2 over to tab1.
2120 ** Columns are not decoded and reassembled, which greatly improves
2121 ** performance.  Raw index records are transferred in the same way.
2122 **
2123 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
2124 ** There are lots of rules for determining compatibility - see comments
2125 ** embedded in the code for details.
2126 **
2127 ** This routine returns TRUE if the optimization is guaranteed to be used.
2128 ** Sometimes the xfer optimization will only work if the destination table
2129 ** is empty - a factor that can only be determined at run-time.  In that
2130 ** case, this routine generates code for the xfer optimization but also
2131 ** does a test to see if the destination table is empty and jumps over the
2132 ** xfer optimization code if the test fails.  In that case, this routine
2133 ** returns FALSE so that the caller will know to go ahead and generate
2134 ** an unoptimized transfer.  This routine also returns FALSE if there
2135 ** is no chance that the xfer optimization can be applied.
2136 **
2137 ** This optimization is particularly useful at making VACUUM run faster.
2138 */
2139 static int xferOptimization(
2140   Parse *pParse,        /* Parser context */
2141   Table *pDest,         /* The table we are inserting into */
2142   Select *pSelect,      /* A SELECT statement to use as the data source */
2143   int onError,          /* How to handle constraint errors */
2144   int iDbDest           /* The database of pDest */
2145 ){
2146   sqlite3 *db = pParse->db;
2147   ExprList *pEList;                /* The result set of the SELECT */
2148   Table *pSrc;                     /* The table in the FROM clause of SELECT */
2149   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
2150   struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
2151   int i;                           /* Loop counter */
2152   int iDbSrc;                      /* The database of pSrc */
2153   int iSrc, iDest;                 /* Cursors from source and destination */
2154   int addr1, addr2;                /* Loop addresses */
2155   int emptyDestTest = 0;           /* Address of test for empty pDest */
2156   int emptySrcTest = 0;            /* Address of test for empty pSrc */
2157   Vdbe *v;                         /* The VDBE we are building */
2158   int regAutoinc;                  /* Memory register used by AUTOINC */
2159   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
2160   int regData, regRowid;           /* Registers holding data and rowid */
2161 
2162   if( pSelect==0 ){
2163     return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
2164   }
2165   if( pParse->pWith || pSelect->pWith ){
2166     /* Do not attempt to process this query if there are an WITH clauses
2167     ** attached to it. Proceeding may generate a false "no such table: xxx"
2168     ** error if pSelect reads from a CTE named "xxx".  */
2169     return 0;
2170   }
2171   if( sqlite3TriggerList(pParse, pDest) ){
2172     return 0;   /* tab1 must not have triggers */
2173   }
2174 #ifndef SQLITE_OMIT_VIRTUALTABLE
2175   if( IsVirtual(pDest) ){
2176     return 0;   /* tab1 must not be a virtual table */
2177   }
2178 #endif
2179   if( onError==OE_Default ){
2180     if( pDest->iPKey>=0 ) onError = pDest->keyConf;
2181     if( onError==OE_Default ) onError = OE_Abort;
2182   }
2183   assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
2184   if( pSelect->pSrc->nSrc!=1 ){
2185     return 0;   /* FROM clause must have exactly one term */
2186   }
2187   if( pSelect->pSrc->a[0].pSelect ){
2188     return 0;   /* FROM clause cannot contain a subquery */
2189   }
2190   if( pSelect->pWhere ){
2191     return 0;   /* SELECT may not have a WHERE clause */
2192   }
2193   if( pSelect->pOrderBy ){
2194     return 0;   /* SELECT may not have an ORDER BY clause */
2195   }
2196   /* Do not need to test for a HAVING clause.  If HAVING is present but
2197   ** there is no ORDER BY, we will get an error. */
2198   if( pSelect->pGroupBy ){
2199     return 0;   /* SELECT may not have a GROUP BY clause */
2200   }
2201   if( pSelect->pLimit ){
2202     return 0;   /* SELECT may not have a LIMIT clause */
2203   }
2204   if( pSelect->pPrior ){
2205     return 0;   /* SELECT may not be a compound query */
2206   }
2207   if( pSelect->selFlags & SF_Distinct ){
2208     return 0;   /* SELECT may not be DISTINCT */
2209   }
2210   pEList = pSelect->pEList;
2211   assert( pEList!=0 );
2212   if( pEList->nExpr!=1 ){
2213     return 0;   /* The result set must have exactly one column */
2214   }
2215   assert( pEList->a[0].pExpr );
2216   if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
2217     return 0;   /* The result set must be the special operator "*" */
2218   }
2219 
2220   /* At this point we have established that the statement is of the
2221   ** correct syntactic form to participate in this optimization.  Now
2222   ** we have to check the semantics.
2223   */
2224   pItem = pSelect->pSrc->a;
2225   pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
2226   if( pSrc==0 ){
2227     return 0;   /* FROM clause does not contain a real table */
2228   }
2229   if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
2230     testcase( pSrc!=pDest ); /* Possible due to bad sqlite_master.rootpage */
2231     return 0;   /* tab1 and tab2 may not be the same table */
2232   }
2233   if( HasRowid(pDest)!=HasRowid(pSrc) ){
2234     return 0;   /* source and destination must both be WITHOUT ROWID or not */
2235   }
2236 #ifndef SQLITE_OMIT_VIRTUALTABLE
2237   if( IsVirtual(pSrc) ){
2238     return 0;   /* tab2 must not be a virtual table */
2239   }
2240 #endif
2241   if( pSrc->pSelect ){
2242     return 0;   /* tab2 may not be a view */
2243   }
2244   if( pDest->nCol!=pSrc->nCol ){
2245     return 0;   /* Number of columns must be the same in tab1 and tab2 */
2246   }
2247   if( pDest->iPKey!=pSrc->iPKey ){
2248     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
2249   }
2250   for(i=0; i<pDest->nCol; i++){
2251     Column *pDestCol = &pDest->aCol[i];
2252     Column *pSrcCol = &pSrc->aCol[i];
2253 #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
2254     if( (db->mDbFlags & DBFLAG_Vacuum)==0
2255      && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
2256     ){
2257       return 0;    /* Neither table may have __hidden__ columns */
2258     }
2259 #endif
2260     if( pDestCol->affinity!=pSrcCol->affinity ){
2261       return 0;    /* Affinity must be the same on all columns */
2262     }
2263     if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
2264       return 0;    /* Collating sequence must be the same on all columns */
2265     }
2266     if( pDestCol->notNull && !pSrcCol->notNull ){
2267       return 0;    /* tab2 must be NOT NULL if tab1 is */
2268     }
2269     /* Default values for second and subsequent columns need to match. */
2270     if( i>0 ){
2271       assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
2272       assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
2273       if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
2274        || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
2275                                        pSrcCol->pDflt->u.zToken)!=0)
2276       ){
2277         return 0;    /* Default values must be the same for all columns */
2278       }
2279     }
2280   }
2281   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
2282     if( IsUniqueIndex(pDestIdx) ){
2283       destHasUniqueIdx = 1;
2284     }
2285     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
2286       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
2287     }
2288     if( pSrcIdx==0 ){
2289       return 0;    /* pDestIdx has no corresponding index in pSrc */
2290     }
2291     if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
2292          && sqlite3FaultSim(411)==SQLITE_OK ){
2293       /* The sqlite3FaultSim() call allows this corruption test to be
2294       ** bypassed during testing, in order to exercise other corruption tests
2295       ** further downstream. */
2296       return 0;   /* Corrupt schema - two indexes on the same btree */
2297     }
2298   }
2299 #ifndef SQLITE_OMIT_CHECK
2300   if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
2301     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
2302   }
2303 #endif
2304 #ifndef SQLITE_OMIT_FOREIGN_KEY
2305   /* Disallow the transfer optimization if the destination table constains
2306   ** any foreign key constraints.  This is more restrictive than necessary.
2307   ** But the main beneficiary of the transfer optimization is the VACUUM
2308   ** command, and the VACUUM command disables foreign key constraints.  So
2309   ** the extra complication to make this rule less restrictive is probably
2310   ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
2311   */
2312   if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
2313     return 0;
2314   }
2315 #endif
2316   if( (db->flags & SQLITE_CountRows)!=0 ){
2317     return 0;  /* xfer opt does not play well with PRAGMA count_changes */
2318   }
2319 
2320   /* If we get this far, it means that the xfer optimization is at
2321   ** least a possibility, though it might only work if the destination
2322   ** table (tab1) is initially empty.
2323   */
2324 #ifdef SQLITE_TEST
2325   sqlite3_xferopt_count++;
2326 #endif
2327   iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
2328   v = sqlite3GetVdbe(pParse);
2329   sqlite3CodeVerifySchema(pParse, iDbSrc);
2330   iSrc = pParse->nTab++;
2331   iDest = pParse->nTab++;
2332   regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
2333   regData = sqlite3GetTempReg(pParse);
2334   regRowid = sqlite3GetTempReg(pParse);
2335   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
2336   assert( HasRowid(pDest) || destHasUniqueIdx );
2337   if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
2338       (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
2339    || destHasUniqueIdx                              /* (2) */
2340    || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
2341   )){
2342     /* In some circumstances, we are able to run the xfer optimization
2343     ** only if the destination table is initially empty. Unless the
2344     ** DBFLAG_Vacuum flag is set, this block generates code to make
2345     ** that determination. If DBFLAG_Vacuum is set, then the destination
2346     ** table is always empty.
2347     **
2348     ** Conditions under which the destination must be empty:
2349     **
2350     ** (1) There is no INTEGER PRIMARY KEY but there are indices.
2351     **     (If the destination is not initially empty, the rowid fields
2352     **     of index entries might need to change.)
2353     **
2354     ** (2) The destination has a unique index.  (The xfer optimization
2355     **     is unable to test uniqueness.)
2356     **
2357     ** (3) onError is something other than OE_Abort and OE_Rollback.
2358     */
2359     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
2360     emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
2361     sqlite3VdbeJumpHere(v, addr1);
2362   }
2363   if( HasRowid(pSrc) ){
2364     u8 insFlags;
2365     sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
2366     emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
2367     if( pDest->iPKey>=0 ){
2368       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
2369       sqlite3VdbeVerifyAbortable(v, onError);
2370       addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
2371       VdbeCoverage(v);
2372       sqlite3RowidConstraint(pParse, onError, pDest);
2373       sqlite3VdbeJumpHere(v, addr2);
2374       autoIncStep(pParse, regAutoinc, regRowid);
2375     }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
2376       addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
2377     }else{
2378       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
2379       assert( (pDest->tabFlags & TF_Autoincrement)==0 );
2380     }
2381     sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
2382     if( db->mDbFlags & DBFLAG_Vacuum ){
2383       sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
2384       insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
2385                            OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
2386     }else{
2387       insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
2388     }
2389     sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid,
2390                       (char*)pDest, P4_TABLE);
2391     sqlite3VdbeChangeP5(v, insFlags);
2392     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
2393     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
2394     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
2395   }else{
2396     sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
2397     sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
2398   }
2399   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
2400     u8 idxInsFlags = 0;
2401     for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
2402       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
2403     }
2404     assert( pSrcIdx );
2405     sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
2406     sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
2407     VdbeComment((v, "%s", pSrcIdx->zName));
2408     sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
2409     sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
2410     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
2411     VdbeComment((v, "%s", pDestIdx->zName));
2412     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
2413     sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
2414     if( db->mDbFlags & DBFLAG_Vacuum ){
2415       /* This INSERT command is part of a VACUUM operation, which guarantees
2416       ** that the destination table is empty. If all indexed columns use
2417       ** collation sequence BINARY, then it can also be assumed that the
2418       ** index will be populated by inserting keys in strictly sorted
2419       ** order. In this case, instead of seeking within the b-tree as part
2420       ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
2421       ** OP_IdxInsert to seek to the point within the b-tree where each key
2422       ** should be inserted. This is faster.
2423       **
2424       ** If any of the indexed columns use a collation sequence other than
2425       ** BINARY, this optimization is disabled. This is because the user
2426       ** might change the definition of a collation sequence and then run
2427       ** a VACUUM command. In that case keys may not be written in strictly
2428       ** sorted order.  */
2429       for(i=0; i<pSrcIdx->nColumn; i++){
2430         const char *zColl = pSrcIdx->azColl[i];
2431         if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
2432       }
2433       if( i==pSrcIdx->nColumn ){
2434         idxInsFlags = OPFLAG_USESEEKRESULT;
2435         sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
2436       }
2437     }
2438     if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
2439       idxInsFlags |= OPFLAG_NCHANGE;
2440     }
2441     sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
2442     sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
2443     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
2444     sqlite3VdbeJumpHere(v, addr1);
2445     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
2446     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
2447   }
2448   if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
2449   sqlite3ReleaseTempReg(pParse, regRowid);
2450   sqlite3ReleaseTempReg(pParse, regData);
2451   if( emptyDestTest ){
2452     sqlite3AutoincrementEnd(pParse);
2453     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
2454     sqlite3VdbeJumpHere(v, emptyDestTest);
2455     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
2456     return 0;
2457   }else{
2458     return 1;
2459   }
2460 }
2461 #endif /* SQLITE_OMIT_XFER_OPT */
2462