From: drh Date: Fri, 18 Oct 2019 02:19:18 +0000 (+0000) Subject: STORED columns can now reference other STORED columns, in any order, as long X-Git-Tag: version-3.31.0~376^2~23 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9942ef0d95c5a34bec24b669f454fc157bbf8eee;p=thirdparty%2Fsqlite.git STORED columns can now reference other STORED columns, in any order, as long as there are not loops. FossilOrigin-Name: 0d236698e64b2a4b46f91a25279c406e0bf392fe66116678456f0a034c11d7b4 --- diff --git a/manifest b/manifest index 69da213089..9e844296d9 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C In\sthe\sTable\sobject,\schange\sthe\snVCol\sfield\sto\snNVCol\s-\sthe\snumber\sof\nnon-virtual\scolumns,\sas\sthat\sis\sthe\squantity\sthat\swe\sneed\smost. -D 2019-10-17T18:35:57.978 +C STORED\scolumns\scan\snow\sreference\sother\sSTORED\scolumns,\sin\sany\sorder,\sas\slong\nas\sthere\sare\snot\sloops. +D 2019-10-18T02:19:18.134 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -477,7 +477,7 @@ F src/date.c e1d8ac7102f3f283e63e13867acb0efa33861cf34f0faf4cdbaf9fa7a1eb7041 F src/dbpage.c 135eb3b5e74f9ef74bde5cec2571192c90c86984fa534c88bf4a055076fa19b7 F src/dbstat.c c12833de69cb655751487d2c5a59607e36be1c58ba1f4bd536609909ad47b319 F src/delete.c fd10ac325c73bdf4d558530745d70df01fe6f7a0763553045adf42a62c303dc9 -F src/expr.c 61d9043b1b9b82d72c919bcf5801a885a0ee7e41bf3e5ce3021b206a5706a949 +F src/expr.c e492fffa17f9b89ba01790715d8f6b3a14b1af6b486163a6690b3fee4b3e4529 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 96e3cd32ddc5cfc75b2a03ced36f49a31105cfbda3939f1e239109bdd12227e8 +F src/insert.c 3115663711fabc6a79b96f4c5f1a4cd64f79778c51a1f18b4180c2959785f636 F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa F src/loadext.c 4ddc65ae13c0d93db0ceedc8b14a28c8c260513448b0eb8c5a2ac375e3b6a85d F src/main.c 3e01f6a1c96643381b5f9d79e4ff7f2520bc5712197746fb0852283e78cccf66 @@ -530,7 +530,7 @@ F src/shell.c.in d70bcf630c4073eaa994fa74be98886c781918e794cb8b562be8df10f018e27 F src/sqlite.h.in 5725a6b20190a1e8d662077a1c1c8ea889ad7be90dd803f914c2de226f5fe6ab F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3ext.h cef696ce3293242c67b2339763608427bf72ee66f1f3a05389ac2a7b46001c31 -F src/sqliteInt.h 67d8d7f2d734823a978e50764563046759c23c6fa752c91e4f33b9815539e3e1 +F src/sqliteInt.h aa16ccbbe83b06807da56079398412a995c904e0c0bb5a18eb5c659eb5c96898 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 61b4459ae6a6cc182c11abbc8b1dd629f77beb088d7ccad5e563d8d4769056df -R 3d476f0303fa6cd5641db0923ba30a4b +P 4ad66af04a654d92711d2d056ce8f35cd21eac4b99fb8d78be1a314620b6d29e +R 9578e8b1b10efcdb33ad67045419d288 U drh -Z 39ac0138a046fe5fcbf36ab2cfc09aa2 +Z f6acd0d3a8cdd8b86a336efd8f5324bf diff --git a/manifest.uuid b/manifest.uuid index 480b2fbc94..c8abb502de 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -4ad66af04a654d92711d2d056ce8f35cd21eac4b99fb8d78be1a314620b6d29e \ No newline at end of file +0d236698e64b2a4b46f91a25279c406e0bf392fe66116678456f0a034c11d7b4 \ No newline at end of file diff --git a/src/expr.c b/src/expr.c index c21006b115..c026c9583b 100644 --- a/src/expr.c +++ b/src/expr.c @@ -3577,19 +3577,49 @@ expr_code_doover: } if( iTab<0 ){ if( pParse->iSelfTab<0 ){ - /* Generating CHECK constraints or inserting into partial index */ - assert( pExpr->y.pTab!=0 ); + /* Other columns in the same row for CHECK constraints or + ** generated columns or for inserting into partial index. + ** The row is unpacked into registers beginning at + ** 0-(pParse->iSelfTab). The rowid (if any) is in a register + ** immediately prior to the first column. + */ + Column *pCol; + Table *pTab = pExpr->y.pTab; + int iSrc; + assert( pTab!=0 ); assert( pExpr->iColumn>=XN_ROWID ); assert( pExpr->iColumny.pTab->nCol ); - if( pExpr->iColumn>=0 - && pExpr->y.pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL - ){ - sqlite3VdbeAddOp2(v, OP_SCopy, pExpr->iColumn - pParse->iSelfTab, - target); + if( pExpr->iColumn<0 ){ + return -1-pParse->iSelfTab; + } + pCol = pTab->aCol + pExpr->iColumn; + iSrc = sqlite3ColumnOfTable(pTab, pExpr->iColumn) - pParse->iSelfTab; +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + if( pCol->colFlags & COLFLAG_GENERATED ){ + if( pCol->colFlags & COLFLAG_BUSY ){ + sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", + pCol->zName); + return 0; + } + pCol->colFlags |= COLFLAG_BUSY; + if( pCol->colFlags & COLFLAG_VIRTUAL ){ + target = sqlite3ExprCodeTarget(pParse, pCol->pDflt, target); + }else{ + target = iSrc; + if( pCol->colFlags & COLFLAG_NOTAVAIL ){ + sqlite3ExprCode(pParse, pCol->pDflt, iSrc); + } + } + pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL); + return target; + }else +#endif /* SQLITE_OMIT_GENERATED_COLUMNS */ + if( pCol->affinity==SQLITE_AFF_REAL ){ + sqlite3VdbeAddOp2(v, OP_SCopy, iSrc, target); sqlite3VdbeAddOp1(v, OP_RealAffinity, target); return target; }else{ - return pExpr->iColumn - pParse->iSelfTab; + return iSrc; } }else{ /* Coding an expression that is part of an index where column names diff --git a/src/insert.c b/src/insert.c index cd3eaf293d..d53db3dd3e 100644 --- a/src/insert.c +++ b/src/insert.c @@ -215,15 +215,30 @@ void sqlite3ComputeStoredColumns( Table *pTab /* The table */ ){ int i; + /* Because there can be multiple STORED columns that refer to one another, + ** either directly or through VIRTUAL columns, this is a two pass + ** algorithm. On the first pass, mark all STORED columns as NOT-AVAILABLE. + */ + for(i=0; inCol; i++){ + if( pTab->aCol[i].colFlags & COLFLAG_STORED ){ + pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL; + } + } + /* On the second pass, compute the value of each NOT-AVAILABLE column. + ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will + ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as + ** they are needed. + */ pParse->iSelfTab = -iRegStore; for(i=0; inCol; i++, iRegStore++){ u32 colFlags = pTab->aCol[i].colFlags; if( (colFlags & COLFLAG_VIRTUAL)!=0 ){ /* Virtual columns are not stored */ iRegStore--; - }else if( (colFlags & COLFLAG_STORED)!=0 ){ + }else if( (colFlags & COLFLAG_NOTAVAIL)!=0 ){ /* Stored columns are handled on the second pass */ sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore); + colFlags &= ~COLFLAG_NOTAVAIL; } } pParse->iSelfTab = 0; diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 2726a0ac4e..f6646e4b82 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -1828,7 +1828,7 @@ struct Column { u8 notNull; /* An OE_ code for handling a NOT NULL constraint */ char affinity; /* One of the SQLITE_AFF_... values */ u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */ - u8 colFlags; /* Boolean properties. See COLFLAG_ defines below */ + u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */ }; /* Allowed values for Column.colFlags: @@ -1840,7 +1840,8 @@ struct Column { #define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */ #define COLFLAG_VIRTUAL 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */ #define COLFLAG_STORED 0x0040 /* GENERATED ALWAYS AS ... STORED */ -#define COLFLAG_BUSY 0x0080 /* Blocks recursion on VIRTUAL columns */ +#define COLFLAG_BUSY 0x0080 /* Blocks recursion on GENERATED columns */ +#define COLFLAG_NOTAVAIL 0x0100 /* STORED column not yet calculated */ #define COLFLAG_GENERATED 0x0060 /* Combo: _STORED, _VIRTUAL */ #define COLFLAG_NOINSERT 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */