]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Work toward getting generated columns to work with triggers. Still more
authordrh <drh@noemail.net>
Sat, 19 Oct 2019 18:47:27 +0000 (18:47 +0000)
committerdrh <drh@noemail.net>
Sat, 19 Oct 2019 18:47:27 +0000 (18:47 +0000)
work to do in this area.

FossilOrigin-Name: 932a37275d7e932f8237d32c8fc6087ed8cd342fe01ef2f7a43c7237ab84c9ac

manifest
manifest.uuid
src/build.c
src/expr.c
src/insert.c
src/sqliteInt.h
src/update.c

index 729ea94862bb97168f5d441994048d3a2093d6a9..2d2a9b70e5a4f0daaac8504b711b0461a58a32fb 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\stestcase\smacros.
-D 2019-10-19T15:01:52.518
+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
 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 fb6464e8ae5364a7a31915432f349d209a1a3fd287edbce651600bf46ccacd34
+F src/build.c c6421066f1ce6ce789a8346818f7ce00fec47f469004fa6b6d31643d793d2b2d
 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 254252a94890f2493a5e214971342bfdf228857483ea7a263ae75048ff67f5b5
+F src/expr.c ffa1cda4277078c0572055f46f83d06dc2e0a89b843e601bef6f9dc8fd430f41
 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 2f1b93da19a9a65eab197534dcc3fb569708b1a7035da5b229063b7f13e71253
+F src/insert.c fd8ba5c85b6e96770957f3f23b66fb619adb287dcd53e6f19fb5e8ddaf9b792a
 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 7fa9357635e6c32c61d103054a4d4040392d81a798cfe982c09d4ed5568aa724
+F src/sqliteInt.h 1929d5f85ce9e4aba60540c70f88ef6fcd2bb272ee52b7dfee96126775dd12b9
 F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b
 F src/status.c 46e7aec11f79dad50965a5ca5fa9de009f7d6bde08be2156f1538a0a296d4d0e
 F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34
@@ -592,7 +592,7 @@ F src/threads.c 4ae07fa022a3dc7c5beb373cf744a85d3c5c6c3c
 F src/tokenize.c 7b17f6e2f20f6cbcb0b215025a86b7457c38451fc7622f705e553d7a488c572d
 F src/treeview.c fddeb413159c3eeeaea3f496172f121cf3695606c461dc4e6dcee51417952df5
 F src/trigger.c 845ccc08f60716c58aa28fe6470385c18ef8c4e1d88c93dcf449bc13d464eb2e
-F src/update.c ca754bbcb5662184bf5face4b67686bed8f53a36cf3def87cbb238deb1086b9b
+F src/update.c e5c432097191e6e6c73e48364daa58b257b30a8b0d54b018fd0bd9415cd7b553
 F src/upsert.c b445315c8958d8f17ec3297d06842e61dacaad0633ccaec1e4e160de7e562212
 F src/utf.c 2f0fac345c7660d5c5bd3df9e9d8d33d4c27f366bcfb09e07443064d751a0507
 F src/util.c 10d910e04a4f3842042485e0df01a484f57f912c10b60b3a09ccddd5019bd138
@@ -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 27ab41c9102e7801ff829488fc123a8040da008bef373d6704efbe2f93e1da90
-R 72df5515bebd9dc031bf51cf11383344
+P fb9c9bb284d441bd7aa34c87b6fd8ce57d036c17623d82354154fc6286bdd134
+R 3288eb40d3c7dad01a2cfbfeeeb4e1e7
 U drh
-Z dca30fedc0b8883c88ac3723c3316cd7
+Z dcf9ae594cbfb14d7391aa7ed9f45058
index 7c08b6a36cbd124f869f162372c7e7e27f4aac0b..2f83dfbcb3a1da07c433a29f74d09ff45699a159 100644 (file)
@@ -1 +1 @@
-fb9c9bb284d441bd7aa34c87b6fd8ce57d036c17623d82354154fc6286bdd134
\ No newline at end of file
+932a37275d7e932f8237d32c8fc6087ed8cd342fe01ef2f7a43c7237ab84c9ac
\ No newline at end of file
index 48f3a1c1ea0833f0d9811881021a6d9cd732d87e..c688a68e0fd5966612fa7963816fd7564b90bba6 100644 (file)
@@ -901,8 +901,6 @@ i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
 ** and only there are VIRTUAL columns to the left.
 **
 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
-**
-** This function is the inverse of sqlite3TableColumnToStorage().
 */
 i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
   if( pTab->tabFlags & TF_HasVirtual ){
@@ -919,12 +917,36 @@ i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
 /* Convert a table column number into a storage column number.
 **
 ** The storage column number (0,1,2,....) is the index of the value
-** as it appears in the record on disk.  The true column number
-** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
+** as it appears in the record on disk.  Or, if the input column is
+** the N-th virtual column (zero-based) then the storage number is
+** the number of non-virtual columns in the table plus N.  
 **
-** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
+** The true column number is the index (0,1,2,...) of the column in
+** the CREATE TABLE statement.
+**
+** If the input column is a VIRTUAL column, then it should not appear
+** in storage.  But the value sometimes is cached in registers that
+** follow the range of registers used to construct storage.  This
+** avoids computing the same VIRTUAL column multiple times, and provides
+** values for use by OP_Param opcodes in triggers.  Hence, if the
+** input column is a VIRTUAL table, put it after all the other columns.
+**
+** In the following, N means "normal column", S means STORED, and
+** V means VIRTUAL.  Suppose the CREATE TABLE has columns like this:
 **
-** This function is the inverse of sqlite3StorageColumnToTable().
+**        CREATE TABLE ex(N,S,V,N,S,V,N,S,V);
+**                     -- 0 1 2 3 4 5 6 7 8
+**
+** Then the mapping from this function is as follows:
+**
+**    INPUTS:     0 1 2 3 4 5 6 7 8
+**    OUTPUTS:    0 1 6 2 3 7 4 5 8
+**
+** So, in other words, this routine shifts all the virtual columns to
+** the end.
+**
+** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
+** this routine is a no-op macro.
 */
 i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
   int i;
@@ -934,7 +956,13 @@ i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
   for(i=0, n=0; i<iCol; i++){
     if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
   }
-  return n;    
+  if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
+    /* iCol is a virtual column itself */
+    return pTab->nNVCol + i - n;
+  }else{
+    /* iCol is a normal or stored column */
+    return n;
+  }
 }
 #endif
 
index 01e99428f32f09bc007dc84390b72c0e8714b5de..b868ed969a4588314f1723846b2e355b6862f604 100644 (file)
@@ -3604,16 +3604,11 @@ expr_code_doover:
               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);
-              }
+            if( pCol->colFlags & COLFLAG_NOTAVAIL ){
+              sqlite3ExprCode(pParse, pCol->pDflt, iSrc);
             }
             pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
-            return target;
+            return iSrc;
           }else
 #endif /* SQLITE_OMIT_GENERATED_COLUMNS */
           if( pCol->affinity==SQLITE_AFF_REAL ){
@@ -4088,17 +4083,19 @@ expr_code_doover:
       **   p1==2   ->    old.b         p1==5   ->    new.b       
       */
       Table *pTab = pExpr->y.pTab;
-      int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
+      int iCol = pExpr->iColumn;
+      int p1 = pExpr->iTable * (pTab->nCol+1) + 1 
+                     + (iCol>=0 ? sqlite3TableColumnToStorage(pTab, iCol) : -1);
 
       assert( pExpr->iTable==0 || pExpr->iTable==1 );
-      assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
-      assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
+      assert( iCol>=-1 && iCol<pTab->nCol );
+      assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
       assert( p1>=0 && p1<(pTab->nCol*2+2) );
 
       sqlite3VdbeAddOp2(v, OP_Param, p1, target);
       VdbeComment((v, "r[%d]=%s.%s", target,
         (pExpr->iTable ? "new" : "old"),
-        (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[pExpr->iColumn].zName)
+        (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zName)
       ));
 
 #ifndef SQLITE_OMIT_FLOATING_POINT
@@ -4107,9 +4104,7 @@ expr_code_doover:
       **
       ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
       ** floating point when extracting it from the record.  */
-      if( pExpr->iColumn>=0 
-       && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
-      ){
+      if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
         sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
       }
 #endif
index e15e9feed2d8d9d40739fe6c3dd07bc2e4c726cb..7eba08e95574fabdeba22c8073f5304270d3824a 100644 (file)
@@ -205,22 +205,23 @@ static int readsTable(Parse *p, int iDb, Table *pTab){
 /*
 ** All regular columns for table pTab have been puts into registers
 ** starting with iRegStore.  The registers that correspond to STORED
-** columns have not been initialized.  This routine goes back and computes
-** the values for STORED columns based on the previously computed normal
-** columns.
+** or VIRTUAL columns have not yet been initialized.  This routine goes
+** back and computes the values for those columns based on the previously
+** computed normal columns.
 */
-void sqlite3ComputeStoredColumns(
+void sqlite3ComputeGeneratedColumns(
   Parse *pParse,    /* Parsing context */
   int iRegStore,    /* Register holding the first column */
   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.
+  int nv;
+  /* Because there can be multiple generated columns that refer to one another,
+  ** this is a two-pass algorithm.  On the first pass, mark all generated
+  ** columns as "not available".
   */
   for(i=0; i<pTab->nCol; i++){
-    if( pTab->aCol[i].colFlags & COLFLAG_STORED ){
+    if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
       pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
     }
   }
@@ -230,16 +231,23 @@ void sqlite3ComputeStoredColumns(
   ** they are needed.
   */
   pParse->iSelfTab = -iRegStore;
-  for(i=0; i<pTab->nCol; i++, iRegStore++){
+  for(i=nv=0; i<pTab->nCol; i++){
     u32 colFlags = pTab->aCol[i].colFlags;
-    if( (colFlags & COLFLAG_VIRTUAL)!=0 ){
-      /* Virtual columns are not stored */
-      iRegStore--;
-    }else if( (colFlags & COLFLAG_NOTAVAIL)!=0 ){
-      /* Stored columns are handled on the second pass */
-      sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
+    if( (colFlags & COLFLAG_NOTAVAIL)!=0 ){
+      assert( colFlags & COLFLAG_GENERATED );
+      if( colFlags & COLFLAG_VIRTUAL ){
+        /* Virtual columns go at the end */
+        assert( pTab->nNVCol+nv == sqlite3TableColumnToStorage(pTab,i) );
+        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt,
+                        iRegStore+pTab->nNVCol+nv);
+      }else{
+        /* Stored columns go in column order */
+        assert( i-nv == sqlite3TableColumnToStorage(pTab,i) );
+        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore+i-nv);
+      }
       colFlags &= ~COLFLAG_NOTAVAIL;
     }
+    if( (colFlags & COLFLAG_VIRTUAL)!=0 ) nv++;
   }
   pParse->iSelfTab = 0;
 }
@@ -1055,7 +1063,6 @@ void sqlite3Insert(
       int k;
       u32 colFlags;
       assert( i>=nHidden );
-      assert( iRegStore==sqlite3TableColumnToStorage(pTab,i)+regRowid+1 );
       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
@@ -1108,10 +1115,10 @@ void sqlite3Insert(
     }
 
 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
-    /* Compute the new value for STORED columns after all other
+    /* Compute the new value for generated columns after all other
     ** columns have already been computed */
-    if( pTab->tabFlags & TF_HasStored ){
-      sqlite3ComputeStoredColumns(pParse, regRowid+1, pTab);
+    if( pTab->tabFlags & (TF_HasStored|TF_HasVirtual) ){
+      sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab);
     }
 #endif
 
@@ -1454,19 +1461,11 @@ void sqlite3GenerateConstraintChecks(
     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
         || onError==OE_Ignore || onError==OE_Replace );
     addr1 = 0;
-    if( (pTab->tabFlags & TF_HasVirtual)==0 ){
-      iReg = regNewData+1+i;
-    }else if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
-      iReg = ++pParse->nMem;
-      assert( pParse->iSelfTab==0 );
-      pParse->iSelfTab = -regNewData;
-      sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iReg);
-      pParse->iSelfTab = 0;
-      if( onError==OE_Replace ) onError = OE_Abort;
-    }else{
-      testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
-      iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1;
-    }
+    testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
+    testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
+    testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
+    testcase( pTab->aCol[i].colFlags & COLFLAG_GENERATED );
+    iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1;
     switch( onError ){
       case OE_Replace: {
         assert( onError==OE_Replace );
index 10b2eeaf3f5f1e4fcbfc20dec6a8d7f70f181c37..00013bd17236b30e4edacf8c309da3307d59a023 100644 (file)
@@ -4044,7 +4044,7 @@ void sqlite3FreeIndex(sqlite3*, Index*);
 #endif
 void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
-  void sqlite3ComputeStoredColumns(Parse*, int, Table*);
+  void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
 #endif
 void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
 IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
index 08502cf5e30335567fc86e6b94fb2c0c49b5224e..79b6fbd2a5cb41d202956061b4e86fbbfd9ab26a 100644 (file)
@@ -693,8 +693,8 @@ void sqlite3Update(
     }
   }
 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
-  if( pTab->tabFlags & TF_HasStored ){
-    sqlite3ComputeStoredColumns(pParse, regNew, pTab);
+  if( pTab->tabFlags & (TF_HasStored|TF_HasVirtual) ){
+    sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
   }
 #endif
 
@@ -737,8 +737,8 @@ void sqlite3Update(
       }
     }
 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
-    if( pTab->tabFlags & TF_HasStored ){
-      sqlite3ComputeStoredColumns(pParse, regNew, pTab);
+    if( pTab->tabFlags & (TF_HasStored|TF_HasVirtual) ){
+      sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
     }
 #endif 
   }