]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Fix the truncate optimization so that PRAGMA count_changes and changes()
authordrh <>
Thu, 3 Jun 2021 18:51:51 +0000 (18:51 +0000)
committerdrh <>
Thu, 3 Jun 2021 18:51:51 +0000 (18:51 +0000)
return correct values even for a WITHOUT ROWID table.
[forum:/forumpost/07dedbf9a1|Forum post 07dedbf9a1].

FossilOrigin-Name: 820ae3b117c2d8c117c0c5a4e440a7d06886772516c7fc0a84606388f91be195

manifest
manifest.uuid
src/btree.c
src/delete.c
src/vdbe.c
test/delete.test
test/delete4.test

index 874a463b7ac30c5bf2149a518a6c43c1e01366ff..a320c5acd64a98a89584de4db9fe529b271d8796 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sthe\sregexp()\sand\sregexpi()\sextension\sfunctions\sto\sthe\sCLI.
-D 2021-06-03T13:51:31.070
+C Fix\sthe\struncate\soptimization\sso\sthat\sPRAGMA\scount_changes\sand\schanges()\nreturn\scorrect\svalues\seven\sfor\sa\sWITHOUT\sROWID\stable.\n[forum:/forumpost/07dedbf9a1|Forum\spost\s07dedbf9a1].
+D 2021-06-03T18:51:51.135
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -485,7 +485,7 @@ F src/auth.c 08954fdc4cc2da5264ba5b75cfd90b67a6fc7d1710a02ccf917c38eadec77853
 F src/backup.c 3014889fa06e20e6adfa0d07b60097eec1f6e5b06671625f476a714d2356513d
 F src/bitvec.c 17ea48eff8ba979f1f5b04cc484c7bb2be632f33
 F src/btmutex.c 8acc2f464ee76324bf13310df5692a262b801808984c1b79defb2503bbafadb6
-F src/btree.c 51ba86095316fceb3e25bc61617d673d84627e79f4ace83f8722910f33eedef3
+F src/btree.c 6b24d93490ea4450a6800eef046fedf29e5e5ba00efc896eef2da5bb2869ea2e
 F src/btree.h 096cc53baa58be22b02c896d1cf933c38cfc6d65f9253c1367ece8cc88a24de5
 F src/btreeInt.h 7bc15a24a02662409ebcd6aeaa1065522d14b7fda71573a2b0568b458f514ae0
 F src/build.c d766f04ddd88822c2c10cf3c0dfb34b068c45f103f442222038b9eeff02312ca
@@ -495,7 +495,7 @@ F src/ctime.c 3052b6b05ed9ef547a3dd66b8e01bfa9d582e78752ad6ed327da84652641e038
 F src/date.c e0632f335952b32401482d099321bbf12716b29d6e72836b53ae49683ebae4bf
 F src/dbpage.c 8a01e865bf8bc6d7b1844b4314443a6436c07c3efe1d488ed89e81719047833a
 F src/dbstat.c 3aa79fc3aed7ce906e4ea6c10e85d657299e304f6049861fe300053ac57de36c
-F src/delete.c 73f57a9a183532c344a3135cf8f2a5589376e39183e0b5f562d6b61b2af0f4d8
+F src/delete.c 930e63df768dcb197242b1efa0ed9fcdcc9a7b7556c236ee01a9959b61cf527b
 F src/expr.c 09f8ae1421d09802b4ac4b05c507761ecd142fa32cc0b1e33b9bd07fba10d34a
 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
 F src/fkey.c e9063648396c58778f77583a678342fe4a9bc82436bf23c5f9f444f2df0fdaa4
@@ -616,7 +616,7 @@ F src/upsert.c df8f1727d62b5987c4fd302cd4d7c0c84ae57cd65683c5a34a740dfe24039235
 F src/utf.c ee39565f0843775cc2c81135751ddd93eceb91a673ea2c57f61c76f288b041a0
 F src/util.c 41c7a72da1df47864faa378a1c720b38adb288c6838cb6be5594511b6287a048
 F src/vacuum.c 492422c1463c076473bae1858799c7a0a5fe87a133d1223239447c422cd26286
-F src/vdbe.c 73e5a8b64a77359f4287d76f168e89d12ba967cbb72a3d7619650568e9c1d66c
+F src/vdbe.c c42799bcd6cbeaf33a1767dde3ba926370afec4f82ba7ca92327a099251f62ec
 F src/vdbe.h 25dabb25c7e157b84e59260cfb5b466c3ac103ede9f36f4db371332c47601abe
 F src/vdbeInt.h 58980223a32495ad059d10581b83e133abdc77248b1bab85c080cab8a13bd819
 F src/vdbeapi.c d9e99daf59fec928986838b3389a7337e82fec6b3b5de30206cb99fb4661b94e
@@ -836,10 +836,10 @@ F test/dbstatus.test 4a4221a883025ffd39696b3d1b3910b928fb097d77e671351acb35f3aed
 F test/dbstatus2.test f5fe0afed3fa45e57cfa70d1147606c20d2ba23feac78e9a172f2fe8ab5b78ef
 F test/decimal.test fcf403fd5585f47342234e153c4a4338cd737b8e0884ac66fc484df47dbcf1a7
 F test/default.test 9687cfb16717e4b8238c191697c98be88c0b16e568dd5368cd9284154097ef50
-F test/delete.test 31832b0c45ecb51a54348c68db173be462985901e6ed7f403d6d7a8f70ab4ef0
+F test/delete.test 438c14fdf2f2a520b63f892ea645896acf8832e2a2308578593d8e05274314b1
 F test/delete2.test 3a03f2cca1f9a67ec469915cb8babd6485db43fa
 F test/delete3.test 555e84a00a99230b7d049d477a324a631126a6ab
-F test/delete4.test 6aa279f459f4aa792cc251435c3809415c1ecaf9f27dce91675e26f05b503db3
+F test/delete4.test 51fafebe9503a40796d1aae1565c60524cada720e50eecac01b7fd0419d9ea0b
 F test/delete_db.test 096d828493c7907f9ea11a7098ea6a0f73edba89406487d5d6cc2228dc4ab8b0
 F test/descidx1.test edc8adee58d491b06c7157c50364eaf1c3605c9c19f8093cb1ea2b6184f3ac13
 F test/descidx2.test a0ba347037ff3b811f4c6ceca5fd0f9d5d72e74e59f2d9de346a9d2f6ad78298
@@ -1918,7 +1918,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 fa23108b299fc70e80540c449500e50ac6344a152f4f6b9c1b1f568bbda8f6e1
-R 10e18e5c8351d48b79324cb92f1ac65f
+P 5d4535bfb603d7c8229ef60f99666459f2997e02e186bc1e52b7ec1320251d67
+R 44321a6657926f9575a85818259f7e27
 U drh
-Z 8ae1da0c921582126dbed75196d61b62
+Z 587f0cd29c08ce7be2dd885b5d005a8e
index 846f991b3064c6c971c6ed2b4f50648a442c8762..3e93a1a2133507c3bba2af364109a88347f2b7d1 100644 (file)
@@ -1 +1 @@
-5d4535bfb603d7c8229ef60f99666459f2997e02e186bc1e52b7ec1320251d67
\ No newline at end of file
+820ae3b117c2d8c117c0c5a4e440a7d06886772516c7fc0a84606388f91be195
\ No newline at end of file
index 300bb361e0ab0751e5bc6dd54edf33fca8140d31..371f8d15c11a8d79bcaba5abe065c76929412bd4 100644 (file)
@@ -9494,8 +9494,8 @@ static int clearDatabasePage(
   if( !pPage->leaf ){
     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
     if( rc ) goto cleardatabasepage_out;
-  }else if( pnChange ){
-    assert( pPage->intKey || CORRUPT_DB );
+  }
+  if( pnChange ){
     testcase( !pPage->intKey );
     *pnChange += pPage->nCell;
   }
@@ -9520,9 +9520,8 @@ cleardatabasepage_out:
 ** read cursors on the table.  Open write cursors are moved to the
 ** root of the table.
 **
-** If pnChange is not NULL, then table iTable must be an intkey table. The
-** integer value pointed to by pnChange is incremented by the number of
-** entries in the table.
+** If pnChange is not NULL, then the integer value pointed to by pnChange
+** is incremented by the number of entries in the table.
 */
 int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
   int rc;
index 0c9c7bc8d0f68cfb3cfb2c34bef6d558c6b7b771..0a3b2dc16f6f2d4c6333bbcddc6c98e6ce8312c7 100644 (file)
@@ -428,6 +428,9 @@ void sqlite3DeleteFrom(
     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
       assert( pIdx->pSchema==pTab->pSchema );
       sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
+      if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
+        sqlite3VdbeChangeP3(v, -1, memCnt ? memCnt : -1);
+      }
     }
   }else
 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
index ad3008669aae3ff1d8b8e68d6ab04d589fd9c178..f9be7893c5ba879d4db4ee9d1f53a736a62a38d9 100644 (file)
@@ -6242,11 +6242,10 @@ case OP_Destroy: {     /* out2 */
 ** P2==1 then the table to be clear is in the auxiliary database file
 ** that is used to store tables create using CREATE TEMPORARY TABLE.
 **
-** If the P3 value is non-zero, then the table referred to must be an
-** intkey table (an SQL table, not an index). In this case the row change 
-** count is incremented by the number of rows in the table being cleared. 
-** If P3 is greater than zero, then the value stored in register P3 is
-** also incremented by the number of rows in the table being cleared.
+** If the P3 value is non-zero, then the row change count is incremented
+** by the number of rows in the table being cleared. If P3 is greater
+** than zero, then the value stored in register P3 is also incremented
+** by the number of rows in the table being cleared.
 **
 ** See also: Destroy
 */
@@ -6257,9 +6256,7 @@ case OP_Clear: {
   nChange = 0;
   assert( p->readOnly==0 );
   assert( DbMaskTest(p->btreeMask, pOp->p2) );
-  rc = sqlite3BtreeClearTable(
-      db->aDb[pOp->p2].pBt, (u32)pOp->p1, (pOp->p3 ? &nChange : 0)
-  );
+  rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
   if( pOp->p3 ){
     p->nChange += nChange;
     if( pOp->p3>0 ){
index a448e52dd285847fb7241aba9bded79e920b7bf2..c15ac53c63a01a78aae238273331f852dd59d0cd 100644 (file)
@@ -100,7 +100,7 @@ do_test delete-5.2.1 {
 } {200}
 do_test delete-5.2.2 {
   execsql {DELETE FROM table1}
-} {200}
+} {202}
 do_test delete-5.2.3 {
   execsql {BEGIN TRANSACTION}
   for {set i 1} {$i<=200} {incr i} {
index f64778a1670159c2b308d1345bbd0db9f49861ee..8d6a1b8c7cacf6189d49f0d473c176053c0f845a 100644 (file)
@@ -59,14 +59,22 @@ do_execsql_test 2.3 {
 #-------------------------------------------------------------------------
 #
 reset_db
-do_execsql_test 3.1 {
+do_execsql_test 3.0.1 {
   CREATE TABLE t1(a, b, PRIMARY KEY(a, b)) WITHOUT ROWID;
   INSERT INTO t1 VALUES(1, 2);
   INSERT INTO t1 VALUES(2, 4);
   INSERT INTO t1 VALUES(1, 5);
   DELETE FROM t1 WHERE a=1;
+  SELECT printf('(%d)',changes());
   SELECT * FROM t1;
-} {2 4}
+} {(2) 2 4}
+do_execsql_test 3.0.2 {
+  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
+     INSERT INTO t1(a,b) SELECT x, x+1 FROM c;
+  SELECT printf('(%d)',changes());
+  DELETE FROM t1;
+  SELECT printf('(%d)',changes());
+} {(100) (101)}
 
 #-------------------------------------------------------------------------
 # DELETE statement that uses the OR optimization