Changes to the INSERT logic to make it simpler and faster and so that

it works with generated columns and BEFORE triggers.

FossilOrigin-Name: bc368cb090376d33d3844e3689c4f6bd19eed758e39b878ee67fef93b1c839ea
This commit is contained in:
drh 2019-10-21 01:04:11 +00:00
parent dd6cc9b52a
commit f5f1915d59
6 changed files with 125 additions and 132 deletions

View File

@ -1,5 +1,5 @@
C Work\stoward\sgetting\sgenerated\scolumns\sto\swork\swith\striggers.\s\sStill\smore\nwork\sto\sdo\sin\sthis\sarea.
D 2019-10-19T18:47:27.679
C Changes\sto\sthe\sINSERT\slogic\sto\smake\sit\ssimpler\sand\sfaster\sand\sso\sthat\nit\sworks\swith\sgenerated\scolumns\sand\sBEFORE\striggers.
D 2019-10-21T01:04:11.302
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -469,7 +469,7 @@ F src/btmutex.c 8acc2f464ee76324bf13310df5692a262b801808984c1b79defb2503bbafadb6
F src/btree.c a8a9c2ce62bdf54c8cf9795143d7cb10b7473a1230a0572f702d061ffcceefe5
F src/btree.h f27a33c49280209a93385e218306c4ee5f46ba8d7649d2f81a7166b282232484
F src/btreeInt.h 91806f01fd1145a9a86ba3042f25c38d8faf6002701bf5e780742cf88bcff437
F src/build.c c6421066f1ce6ce789a8346818f7ce00fec47f469004fa6b6d31643d793d2b2d
F src/build.c b5eefc5b29788ff43062aa15f08e17df843fcc3dedbf6ad2e8a5ecfdd71c83a8
F src/callback.c 25dda5e1c2334a367b94a64077b1d06b2553369f616261ca6783c48bcb6bda73
F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e
F src/ctime.c 1b0724e66f95f33b160b1af85caaf9cceb325d22abf39bd24df4f54a73982251
@ -477,7 +477,7 @@ F src/date.c e1d8ac7102f3f283e63e13867acb0efa33861cf34f0faf4cdbaf9fa7a1eb7041
F src/dbpage.c 135eb3b5e74f9ef74bde5cec2571192c90c86984fa534c88bf4a055076fa19b7
F src/dbstat.c c12833de69cb655751487d2c5a59607e36be1c58ba1f4bd536609909ad47b319
F src/delete.c d08c9e01a2664afd12edcfa3a9c6578517e8ff8735f35509582693adbe0edeaf
F src/expr.c ffa1cda4277078c0572055f46f83d06dc2e0a89b843e601bef6f9dc8fd430f41
F src/expr.c e00d483f68fcaa2bfb334ba21550f949ca92b239f86ca22824577988efc53010
F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
F src/fkey.c 6271fda51794b569d736eba4097d28f13080cd0c9eb66d5fcecb4b77336fae50
F src/func.c ed33e38cd642058182a31a3f518f2e34f4bbe53aa483335705c153c4d3e50b12
@ -486,7 +486,7 @@ F src/hash.c 8d7dda241d0ebdafb6ffdeda3149a412d7df75102cecfc1021c98d6219823b19
F src/hash.h 9d56a9079d523b648774c1784b74b89bd93fac7b365210157482e4319a468f38
F src/hwtime.h 747c1bbe9df21a92e9c50f3bbec1de841dc5e5da
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
F src/insert.c fd8ba5c85b6e96770957f3f23b66fb619adb287dcd53e6f19fb5e8ddaf9b792a
F src/insert.c db411a28a6c5bf159c0ea9f9b7dde1b41b4be6f393a77b574e8086dd4bea0153
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c 4ddc65ae13c0d93db0ceedc8b14a28c8c260513448b0eb8c5a2ac375e3b6a85d
F src/main.c 3e01f6a1c96643381b5f9d79e4ff7f2520bc5712197746fb0852283e78cccf66
@ -530,7 +530,7 @@ F src/shell.c.in 3093bdf5eedd91da08f0268f1442aa510a60798c9441868149ddbecdf8bcaa7
F src/sqlite.h.in 5725a6b20190a1e8d662077a1c1c8ea889ad7be90dd803f914c2de226f5fe6ab
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h cef696ce3293242c67b2339763608427bf72ee66f1f3a05389ac2a7b46001c31
F src/sqliteInt.h 1929d5f85ce9e4aba60540c70f88ef6fcd2bb272ee52b7dfee96126775dd12b9
F src/sqliteInt.h 49ade8ae4322263c4bfb46ededd566f6351a82380294bb11efb91d2ae100ca24
F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b
F src/status.c 46e7aec11f79dad50965a5ca5fa9de009f7d6bde08be2156f1538a0a296d4d0e
F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34
@ -1847,7 +1847,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P fb9c9bb284d441bd7aa34c87b6fd8ce57d036c17623d82354154fc6286bdd134
R 3288eb40d3c7dad01a2cfbfeeeb4e1e7
P 932a37275d7e932f8237d32c8fc6087ed8cd342fe01ef2f7a43c7237ab84c9ac
R 47a4a5169a9bdb2c3c1d9653909f73fd
U drh
Z dcf9ae594cbfb14d7391aa7ed9f45058
Z f477eaf49473bce8390a1ef42b7a8a95

View File

@ -1 +1 @@
932a37275d7e932f8237d32c8fc6087ed8cd342fe01ef2f7a43c7237ab84c9ac
bc368cb090376d33d3844e3689c4f6bd19eed758e39b878ee67fef93b1c839ea

View File

@ -2329,7 +2329,7 @@ void sqlite3EndTable(
pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB);
if( pSelTab==0 ) return;
assert( p->aCol==0 );
p->nCol = pSelTab->nCol;
p->nCol = p->nNVCol = pSelTab->nCol;
p->aCol = pSelTab->aCol;
pSelTab->nCol = 0;
pSelTab->aCol = 0;
@ -2617,7 +2617,7 @@ int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
** the column names from the SELECT statement that defines the view.
*/
assert( pTable->aCol==0 );
pTable->nCol = pSelTab->nCol;
pTable->nCol = pTable->nNVCol = pSelTab->nCol;
pTable->aCol = pSelTab->aCol;
pSelTab->nCol = 0;
pSelTab->aCol = 0;

View File

@ -4337,15 +4337,11 @@ void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
int inReg;
assert( target>0 && target<=pParse->nMem );
if( pExpr && pExpr->op==TK_REGISTER ){
sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
}else{
inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
if( inReg!=target && pParse->pVdbe ){
sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
}
}
}
/*
@ -4374,30 +4370,6 @@ void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
}
}
/*
** Generate code that evaluates the given expression and puts the result
** in register target.
**
** Also make a copy of the expression results into another "cache" register
** and modify the expression so that the next time it is evaluated,
** the result is a copy of the cache register.
**
** This routine is used for expressions that are used multiple
** times. They are evaluated once and the results of the expression
** are reused.
*/
void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
Vdbe *v = pParse->pVdbe;
int iMem;
assert( target>0 );
assert( pExpr->op!=TK_REGISTER );
sqlite3ExprCode(pParse, pExpr, target);
iMem = ++pParse->nMem;
sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
exprToRegister(pExpr, iMem);
}
/*
** Generate code that pushes the value of every element of the given
** expression list into a sequence of registers beginning at target.

View File

@ -561,7 +561,7 @@ void sqlite3Insert(
Parse *pParse, /* Parser context */
SrcList *pTabList, /* Name of table into which we are inserting */
Select *pSelect, /* A SELECT statement to use as the data source */
IdList *pColumn, /* Column names corresponding to IDLIST. */
IdList *pColumn, /* Column names corresponding to IDLIST, or NULL. */
int onError, /* How to handle constraint errors */
Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */
){
@ -694,8 +694,8 @@ void sqlite3Insert(
*/
regAutoinc = autoIncBegin(pParse, iDb, pTab);
/* Allocate registers for holding the rowid of the new row,
** the content of the new row, and the assembled row record.
/* Allocate a block registers to hold the rowid and the values
** for all columns of the new row.
*/
regRowid = regIns = pParse->nMem+1;
pParse->nMem += pTab->nCol + 1;
@ -714,7 +714,15 @@ void sqlite3Insert(
** the index into IDLIST of the primary key column. ipkColumn is
** the index of the primary key as it appears in IDLIST, not as
** is appears in the original table. (The index of the INTEGER
** PRIMARY KEY in the original table is pTab->iPKey.)
** PRIMARY KEY in the original table is pTab->iPKey.) After this
** loop, if ipkColumn==(-1), that means that integer primary key
** is unspecified, and hence the table is either WITHOUT ROWID or
** it will automatically generated an integer primary key.
**
** bIdListInOrder is true if the columns in IDLIST are in storage
** order. This enables an optimization that avoids shuffling the
** columns into storage order. False negatives are harmless,
** but false positives will cause database corruption.
*/
bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
if( pColumn ){
@ -937,7 +945,87 @@ void sqlite3Insert(
*/
addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
VdbeCoverage(v);
if( ipkColumn>=0 ){
/* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
** SELECT, go ahead and copy the value into the rowid slot now, so that
** the value does not get overwritten by a NULL at tag-20191021-002. */
sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
}
}
/* Compute data for ordinary columns of the new entry. Values
** are written in storage order into registers starting with regData.
** Only ordinary columns are computed in this loop. The rowid
** (if there is one) is computed later and generated columns are
** computed after the rowid since they might depend on the value
** of the rowid.
*/
nHidden = 0;
iRegStore = regData; assert( regData==regRowid+1 );
for(i=0; i<pTab->nCol; i++, iRegStore++){
int k;
u32 colFlags;
assert( i>=nHidden );
if( i==pTab->iPKey ){
/* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
** using the rowid. So put a NULL in the IPK slot of the record to avoid
** using excess space. The file format definition requires this extra
** NULL - we cannot optimize further by skipping the column completely */
sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
continue;
}
if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
nHidden++;
if( (colFlags & COLFLAG_VIRTUAL)!=0 ){
/* Virtual columns do not participate in OP_MakeRecord. So back up
** iRegStore by one slot to compensate for the iRegStore++ in the
** outer for() loop */
iRegStore--;
continue;
}else if( (colFlags & COLFLAG_STORED)!=0 ){
/* Stored columns are computed later. But if there are BEFORE
** triggers, the slots used for stored columns will be OP_Copy-ed
** to a second block of registers, so the register needs to be
** initialized to NULL to avoid an uninitialized register read */
if( tmask & TRIGGER_BEFORE ){
sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
}
continue;
}else if( pColumn==0 ){
/* Hidden columns that are not explicitly named in the INSERT
** get there default value */
sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
continue;
}
}
if( pColumn ){
for(j=0; j<pColumn->nId && pColumn->a[j].idx!=i; j++){}
if( j>=pColumn->nId ){
/* A column not named in the insert column list gets its
** default value */
sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
continue;
}
k = j;
}else if( nColumn==0 ){
/* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */
sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
continue;
}else{
k = i - nHidden;
}
if( useTempTable ){
sqlite3VdbeAddOp3(v, OP_Column, srcTab, k, iRegStore);
}else if( pSelect ){
if( regFromSelect!=regData ){
sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore);
}
}else{
sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore);
}
}
/* Run the BEFORE and INSTEAD OF triggers, if there are any
*/
@ -973,25 +1061,19 @@ void sqlite3Insert(
*/
assert( !IsVirtual(pTab) );
/* Create the new column data
*/
for(i=j=0; i<pTab->nCol; i++){
if( pColumn ){
for(j=0; j<pColumn->nId; j++){
if( pColumn->a[j].idx==i ) break;
}
}
if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
|| (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){
sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
}else if( useTempTable ){
sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
}else{
assert( pSelect==0 ); /* Otherwise useTempTable is true */
sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
}
if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++;
/* Copy the new data already generated. */
assert( pTab->nNVCol>0 );
sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
#ifndef SQLITE_OMIT_GENERATED_COLUMNS
/* Compute the new value for generated columns after all other
** columns have already been computed. This must be done after
** computing the ROWID in case one of the generated columns
** refers to the ROWID. */
if( pTab->tabFlags & (TF_HasStored|TF_HasVirtual) ){
sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab);
}
#endif
/* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
** do not attempt any conversions before assembling the record.
@ -1009,19 +1091,17 @@ void sqlite3Insert(
sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
}
/* Compute the content of the next row to insert into a range of
** registers beginning at regIns.
*/
if( !isView ){
if( IsVirtual(pTab) ){
/* The row that the VUpdate opcode will delete: none */
sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
}
if( ipkColumn>=0 ){
/* Compute the new rowid */
if( useTempTable ){
sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
}else if( pSelect ){
sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
/* Rowid already initialized at tag-20191021-001 */
}else{
Expr *pIpk = pList->a[ipkColumn].pExpr;
if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
@ -1054,69 +1134,11 @@ void sqlite3Insert(
}
autoIncStep(pParse, regAutoinc, regRowid);
/* Compute data for all columns of the new entry, beginning
** with the first column.
*/
nHidden = 0;
iRegStore = regRowid+1;
for(i=0; i<pTab->nCol; i++, iRegStore++){
int k;
u32 colFlags;
assert( i>=nHidden );
if( i==pTab->iPKey ){
/* The value of the INTEGER PRIMARY KEY column is always a NULL.
** Whenever this column is read, the rowid will be substituted
** in its place. Hence, fill this column with a NULL to avoid
** taking up data space with information that will never be used.
** As there may be shallow copies of this value, make it a soft-NULL */
sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
continue;
}
if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
nHidden++;
if( (colFlags & COLFLAG_VIRTUAL)!=0 ){
/* Virtual columns are no stored */
iRegStore--;
continue;
}else if( (colFlags & COLFLAG_STORED)!=0 ){
/* Stored columns are handled on the second pass */
continue;
}else if( pColumn==0 ){
/* Hidden columns that are not explicitly named in the INSERT */
sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
continue;
}
}
if( pColumn ){
for(j=0; j<pColumn->nId && pColumn->a[j].idx!=i; j++){}
if( j>=pColumn->nId ){
/* A column not named in the insert column list gets its
** default value */
sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
continue;
}
k = j;
}else if( nColumn==0 ){
sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
continue;
}else{
k = i - nHidden;
}
if( useTempTable ){
sqlite3VdbeAddOp3(v, OP_Column, srcTab, k, iRegStore);
}else if( pSelect ){
if( regFromSelect!=regData ){
sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore);
}
}else{
sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore);
}
}
#ifndef SQLITE_OMIT_GENERATED_COLUMNS
/* Compute the new value for generated columns after all other
** columns have already been computed */
** columns have already been computed. This must be done after
** computing the ROWID in case one of the generated columns
** refers to the ROWID. */
if( pTab->tabFlags & (TF_HasStored|TF_HasVirtual) ){
sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab);
}

View File

@ -4100,7 +4100,6 @@ void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
int sqlite3ExprCodeTarget(Parse*, Expr*, int);
void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */