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