]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Fix a problem in the code added by [707ea170b3] causing vdbesort.c to sort unstably.
authordan <dan@noemail.net>
Tue, 25 Mar 2014 17:07:48 +0000 (17:07 +0000)
committerdan <dan@noemail.net>
Tue, 25 Mar 2014 17:07:48 +0000 (17:07 +0000)
FossilOrigin-Name: d3e640afe611b6ae0b7f2cff5b00900d7e4d5ee3

manifest
manifest.uuid
src/vdbesort.c
test/sort.test

index 2690ecef90195bac6bef27f10dc59ec4f7174ae9..eb635d79a892ade0ec678aa0693c7b26cb56ac6c 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Remove\sthe\ssequence\svalues\sfrom\ssorter\srecords\sused\sby\sORDER\sBY\sas\swell.
-D 2014-03-25T15:04:07.777
+C Fix\sa\sproblem\sin\sthe\scode\sadded\sby\s[707ea170b3]\scausing\svdbesort.c\sto\ssort\sunstably.
+D 2014-03-25T17:07:48.821
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 2ef13430cd359f7b361bb863504e227b25cc7f81
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -285,7 +285,7 @@ F src/vdbeapi.c 0ed6053f947edd0b30f64ce5aeb811872a3450a4
 F src/vdbeaux.c 5078ca7de4fd5ba4535bd17fe44d5b56c2d3294c
 F src/vdbeblob.c 15377abfb59251bccedd5a9c7d014a895f0c04aa
 F src/vdbemem.c 6fc77594c60f6155404f3f8d71bf36d1fdeb4447
-F src/vdbesort.c 46e50c6bc9300625cff144f8948381a2c53116bf
+F src/vdbesort.c 691f2186ae0943cd746ea7f5498cc9abebb7a7cc
 F src/vdbetrace.c 6f52bc0c51e144b7efdcfb2a8f771167a8816767
 F src/vtab.c 21b932841e51ebd7d075e2d0ad1415dce8d2d5fd
 F src/wal.c 76e7fc6de229bea8b30bb2539110f03a494dc3a8
@@ -816,7 +816,7 @@ F test/skipscan1.test bed8cbe9d554c8c27afb6c88500f704c86a9196f
 F test/skipscan2.test 5a4db0799c338ddbacb154aaa5589c0254b36a8d
 F test/soak.test 0b5b6375c9f4110c828070b826b3b4b0bb65cd5f
 F test/softheap1.test 40562fe6cac6d9827b7b42b86d45aedf12c15e24
-F test/sort.test 0e4456e729e5a92a625907c63dcdedfbe72c5dc5
+F test/sort.test cb76a6e9db897b6871ef4dbc206ebc6dbc033bf4
 F test/speed1.test f2974a91d79f58507ada01864c0e323093065452
 F test/speed1p.explain d841e650a04728b39e6740296b852dccdca9b2cb
 F test/speed1p.test b180e98609c7677382cf618c0ec9b69f789033a8
@@ -1157,7 +1157,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01
 F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff
-P 3f90abddc31ac20739778c235a834c33f7057997
-R 743d88a284437e5e0babc8e33ec37e61
+P c3ae3697832a00d4d5758988a8766bdbb691e6b8
+R eb749164f2115a0b3ef6dbb32fd74b6a
 U dan
-Z 5024d9c780855d9abccbd4c6c23f3054
+Z ecf1eb93a5e3757595a49ddb23f9c671
index b187241ae4772e2534e07f31c4de83351cb6154e..bb3b1adce9a3d2f4e5f1f5aa5604e31fc5d09575 100644 (file)
@@ -1 +1 @@
-c3ae3697832a00d4d5758988a8766bdbb691e6b8
\ No newline at end of file
+d3e640afe611b6ae0b7f2cff5b00900d7e4d5ee3
\ No newline at end of file
index d55156a2c150751bfbb154aba3103560e4f8f3d8..7b435542823a2c839be0ba774dd522ef06f9ec00 100644 (file)
@@ -562,6 +562,9 @@ static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
 /*
 ** Merge the two sorted lists p1 and p2 into a single list.
 ** Set *ppOut to the head of the new list.
+**
+** In cases where key values are equal, keys from list p1 are considered
+** to be smaller than list p2.
 */
 static void vdbeSorterMerge(
   const VdbeCursor *pCsr,         /* For pKeyInfo */
@@ -597,6 +600,11 @@ static void vdbeSorterMerge(
 ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
 ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
 ** occurs.
+**
+** The sort is required to be stable - if two elements compare as equal
+** then the one added to the sorter first is considered the smaller.
+** Currently, the list is sorted from newest to oldest - pSorter->pRecord
+** points to the most recently added sort key.
 */
 static int vdbeSorterSort(const VdbeCursor *pCsr){
   int i;
@@ -837,7 +845,7 @@ static int vdbeSorterInitMerge(
   int i;                          /* Used to iterator through aIter[] */
   i64 nByte = 0;                  /* Total bytes in all opened PMAs */
 
-  /* Initialize the iterators. */
+  /* Initialize the iterators. Iterator 0 contains the oldest data. */
   for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
     VdbeSorterIter *pIter = &pSorter->aIter[i];
     rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
@@ -1009,8 +1017,13 @@ int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
         ** set aTree[i] to its index and update pIter1. If vdbeSorterCompare()
         ** was actually called above, then pSorter->pUnpacked now contains
         ** a value equivalent to pIter2. So set pKey2 to NULL to prevent
-        ** vdbeSorterCompare() from decoding pIter2 again.  */
-        if( iRes<=0 ){
+        ** vdbeSorterCompare() from decoding pIter2 again.  
+        **
+        ** If the two values were equal, then the value from the oldest
+        ** PMA should be considered smaller. The VdbeSorter.aIter[] array
+        ** is sorted from oldest to newest, so pIter1 contains older values
+        ** than pIter2 iff (pIter1<pIter2).  */
+        if( iRes<0 || (iRes==0 && pIter1<pIter2) ){
           pSorter->aTree[i] = (int)(pIter1 - pSorter->aIter);
           pIter2 = &pSorter->aIter[ pSorter->aTree[i ^ 0x0001] ];
           pKey2 = pIter2->aKey;
@@ -1019,7 +1032,6 @@ int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
           pSorter->aTree[i] = (int)(pIter2 - pSorter->aIter);
           pIter1 = &pSorter->aIter[ pSorter->aTree[i ^ 0x0001] ];
         }
-
       }
       *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
     }
index 08d496b2596a0380455034930d974f4e20a816c2..b543ffad241cb64d02e168ff528a81822574a098 100644 (file)
@@ -464,4 +464,27 @@ do_test sort-12.1 {
   }
 } {1 2 xxx 1 3 yyy 1 1 zzz}
 
+
+#-------------------------------------------------------------------------
+# Check that the sorter in vdbesort.c sorts in a stable fashion.
+#
+do_execsql_test sort-13.0 {
+  CREATE TABLE t10(a, b);
+}
+do_test sort-13.1 {
+  db transaction {
+    for {set i 0} {$i < 100000} {incr i} {
+      execsql { INSERT INTO t10 VALUES( $i/10, $i%10 ) }
+    }
+  }
+} {}
+do_execsql_test sort-13.2 {
+  SELECT a, b FROM t10 ORDER BY a;
+} [db eval {SELECT a, b FROM t10 ORDER BY a, b}]
+do_execsql_test sort-13.3 {
+  PRAGMA cache_size = 5;
+  SELECT a, b FROM t10 ORDER BY a;
+} [db eval {SELECT a, b FROM t10 ORDER BY a, b}]
+
+
 finish_test