]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Basic functionality for descending indices is in place. Lots more testing
authordrh <drh@noemail.net>
Wed, 21 Dec 2005 14:43:11 +0000 (14:43 +0000)
committerdrh <drh@noemail.net>
Wed, 21 Dec 2005 14:43:11 +0000 (14:43 +0000)
needed. (CVS 2840)

FossilOrigin-Name: 7064433e5b06a4f858f39ce57650fba99fd72ffd

manifest
manifest.uuid
src/alter.c
src/build.c
src/vdbe.c
test/descidx1.test [new file with mode: 0644]

index 898f81d356128ef49e3b233d7350a1c65a1184b0..18924fb9903b4e86e63e52debae6fc83f30c0740 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Progress\stoward\sdecending\sindices.\s(CVS\s2839)
-D 2005-12-21T03:16:43
+C Basic\sfunctionality\sfor\sdescending\sindices\sis\sin\splace.\s\sLots\smore\stesting\nneeded.\s(CVS\s2840)
+D 2005-12-21T14:43:12
 F Makefile.in e3c6b3a38d734d41574c04f2fc90d18de2b87102
 F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092
 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -30,13 +30,13 @@ F sqlite.pc.in 30552343140c53304c2a658c080fbe810cd09ca2
 F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc
 F sqlite3.def c413e514217736884254739a105c8c942fdf0c2f
 F sqlite3.pc.in 985b9bf34192a549d7d370e0f0b6b34a4f61369a
-F src/alter.c 7bc737c689044fa00ff2f445139c61f5c9ee0ad8
+F src/alter.c 5905a3372379daa4f860199452b4de5a836e53f3
 F src/analyze.c ea42005eed52c382fcc7ef66969e7f1858597633
 F src/attach.c ee70131f128d31a9c6dcb8824e8471c91b18601a
 F src/auth.c 31e2304bef67f44d635655f44234387ea7d21454
 F src/btree.c 300fd8342f4d9f75829a8a96d60578592f3dee79
 F src/btree.h 8ff86378bb5af0cde282614c16bf0c0190b6d216
-F src/build.c 502479f9b9b5cc0f7f629289f82e98c54c1789d5
+F src/build.c fd4417648d8e06d7a67ecfc2128752e8dad8909a
 F src/callback.c 62066afd516f220575e81b1a1239ab92a2eae252
 F src/complete.c df1681cef40dec33a286006981845f87b194e7a4
 F src/date.c bb079317bff6a2b78aba5c0d2ddae5f6f03acfb7
@@ -84,7 +84,7 @@ F src/update.c ec8e540617b116725b5a55c8d6b4db8bc67fdd7d
 F src/utf.c b7bffac4260177ae7f83c01d025fe0f5ed70ce71
 F src/util.c 8e0e3b1ad4f7c70706a9e76881882a409ccdb2e8
 F src/vacuum.c fbfdd3967fd34e2f260fafed88dcbf3c10856b94
-F src/vdbe.c 09aaed71f076bfd4286607ee4845100b910a492f
+F src/vdbe.c ac5789b1d90538a578c0898e53bcc78876ed5a99
 F src/vdbe.h 8729a4ee16ff9aeab2af9667df3cf300ff978e13
 F src/vdbeInt.h 7b8b8c5dcb203243e4a9a4414c9e488473f67741
 F src/vdbeapi.c b270b680cbc5d20b5a1abfdb08339667985df94e
@@ -145,6 +145,7 @@ F test/default.test 252298e42a680146b1dd64f563b95bdf088d94fb
 F test/delete.test e08ffb452f581d58703d0294242c9e2b8d091916
 F test/delete2.test e382b6a97787197eb8b93dd4ccd37797c3725ea3
 F test/delete3.test 555e84a00a99230b7d049d477a324a631126a6ab
+F test/descidx1.test c1e71467faea9757136c5e6189c86c08cdbd61e8
 F test/diskfull.test d828d72adfc9e2d1a194d25996718c1989152cf9
 F test/distinctagg.test 2b89d1c5220d966a30ba4b40430338669301188b
 F test/enc.test 7a03417a1051fe8bc6c7641cf4c8c3f7e0066d52
@@ -329,7 +330,7 @@ F www/tclsqlite.tcl ddcf912ea48695603c8ed7efb29f0812ef8d1b49
 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
 F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
-P 77a37ceca7792e6cda6810e3387e6dda14a5c7ec
-R 72d096b7221b434491091a1e91cf3acd
+P 112a34b8dcceb39540cb0cd629e264a867400bfb
+R f37dcce5af7edd562296885925a69bf6
 U drh
-Z db01996f6f52833d4c472a9960d2c37e
+Z 916338110e2662cd6d96522a66ddbd1b
index f358883775062d85b6ea1926807f5717fcd8a228..1a1d963a9125a358beeb13a70f29179d1af6eca1 100644 (file)
@@ -1 +1 @@
-112a34b8dcceb39540cb0cd629e264a867400bfb
\ No newline at end of file
+7064433e5b06a4f858f39ce57650fba99fd72ffd
\ No newline at end of file
index 6758315d1beeda10bc9c527311312a965248f4c1..77109c78352a4c08f130d6f0da8fe01ca942e001 100644 (file)
@@ -12,7 +12,7 @@
 ** This file contains C code routines that used to generate VDBE code
 ** that implements the ALTER TABLE command.
 **
-** $Id: alter.c,v 1.12 2005/12/16 01:06:17 drh Exp $
+** $Id: alter.c,v 1.13 2005/12/21 14:43:12 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -385,7 +385,6 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
   char *zCol;               /* Null-terminated column definition */
   Column *pCol;             /* The new column */
   Expr *pDflt;              /* Default value for the new column */
-  Vdbe *v;
 
   if( pParse->nErr ) return;
   pNew = pParse->pNewTable;
@@ -468,23 +467,6 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
   reloadTableSchema(pParse, pTab, pTab->zName);
 }
 
-/*
-** Generate code to make sure the file format number is at least minFormat.
-** The generated code will increase the file format number if necessary.
-*/
-void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
-  Vdbe *v;
-  v = sqlite3GetVdbe(pParse);
-  if( v ){
-    sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1);
-    sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0);
-    sqlite3VdbeAddOp(v, OP_Ge, 0, sqlite3VdbeCurrentAddr(v)+3);
-    sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0);
-    sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
-  }
-}
-
-
 /*
 ** This function is called by the parser after the table-name in
 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
index f771fb689c49346ccae8ba501709fa9bb0f41707..297478197654d53ce771729b9d736c313dc429c5 100644 (file)
@@ -22,7 +22,7 @@
 **     COMMIT
 **     ROLLBACK
 **
-** $Id: build.c,v 1.359 2005/12/21 03:16:43 drh Exp $
+** $Id: build.c,v 1.360 2005/12/21 14:43:12 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -2259,12 +2259,8 @@ void sqlite3CreateIndex(
 
   /* Check to see if we should honor DESC requests on index columns
   */
-  if( pDb->file_format>=4 || (pDb->descIndex && db->init.busy) ){
-#if 0
+  if( pDb->file_format>=4 || (!pDb->descIndex && !db->init.busy) ){
     sortOrderMask = -1;   /* Honor DESC */
-#else
-    sortOrderMask = 0;
-#endif
   }else{
     sortOrderMask = 0;    /* Ignore DESC */
   }
@@ -2396,10 +2392,6 @@ void sqlite3CreateIndex(
     v = sqlite3GetVdbe(pParse);
     if( v==0 ) goto exit_create_index;
 
-    /* Make sure the file_format is at least 4 if we have DESC indices. */
-    if( descSeen ){
-      sqlite3MinimumFileFormat(pParse, iDb, 4);
-    }
 
     /* Create the rootpage for the index
     */
@@ -2407,6 +2399,11 @@ void sqlite3CreateIndex(
     sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0);
     sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
 
+    /* Make sure the file_format is at least 4 if we have DESC indices. */
+    if( descSeen ){
+      sqlite3MinimumFileFormat(pParse, iDb, 4);
+    }
+
     /* Gather the complete text of the CREATE INDEX statement into
     ** the zStmt variable
     */
@@ -2478,6 +2475,22 @@ exit_create_index:
   return;
 }
 
+/*
+** Generate code to make sure the file format number is at least minFormat.
+** The generated code will increase the file format number if necessary.
+*/
+void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
+  Vdbe *v;
+  v = sqlite3GetVdbe(pParse);
+  if( v ){
+    sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1);
+    sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0);
+    sqlite3VdbeAddOp(v, OP_Ge, 0, sqlite3VdbeCurrentAddr(v)+3);
+    sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0);
+    sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
+  }
+}
+
 /*
 ** Fill the Index.aiRowEst[] array with default information - information
 ** to be used when we have not run the ANALYZE command.
index 4cedcd5c2a412dd5611b94f049734910273e45b9..111c8ee15c4e43ca07d5efe7e24df941373ea1be 100644 (file)
@@ -43,7 +43,7 @@
 ** in this file for details.  If in doubt, do not deviate from existing
 ** commenting and indentation practices when changing or adding code.
 **
-** $Id: vdbe.c,v 1.506 2005/12/16 15:24:29 danielk1977 Exp $
+** $Id: vdbe.c,v 1.507 2005/12/21 14:43:12 drh Exp $
 */
 #include "sqliteInt.h"
 #include "os.h"
@@ -2422,6 +2422,9 @@ case OP_SetCookie: {       /* no-push */
     /* When the schema cookie changes, record the new cookie internally */
     pDb->schema_cookie = pTos->i;
     db->flags |= SQLITE_InternChanges;
+  }else if( pOp->p2==1 ){
+    /* Record changes in the file format */
+    pDb->file_format = pTos->i;
   }
   assert( (pTos->flags & MEM_Dyn)==0 );
   pTos--;
diff --git a/test/descidx1.test b/test/descidx1.test
new file mode 100644 (file)
index 0000000..32ca815
--- /dev/null
@@ -0,0 +1,171 @@
+# 2005 December 21
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#*************************************************************************
+# This file implements regression tests for SQLite library.  The
+# focus of this script is descending indices.
+#
+# $Id: descidx1.test,v 1.1 2005/12/21 14:43:12 drh Exp $
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+# This procedure sets the value of the file-format in file 'test.db'
+# to $newval. Also, the schema cookie is incremented.
+# 
+proc set_file_format {newval} {
+  set bt [btree_open test.db 10 0]
+  btree_begin_transaction $bt
+  set meta [btree_get_meta $bt]
+  lset meta 2 $newval                    ;# File format
+  lset meta 1 [expr [lindex $meta 1]+1]  ;# Schema cookie
+  eval "btree_update_meta $bt $meta"
+  btree_commit $bt
+  btree_close $bt
+}
+
+# This procedure returns the value of the file-format in file 'test.db'.
+# 
+proc get_file_format {{fname test.db}} {
+  set bt [btree_open $fname 10 0]
+  set meta [btree_get_meta $bt]
+  btree_close $bt
+  lindex $meta 2
+}
+
+# Verify that the file format jumps to (at least) 4 as soon as a
+# descending index is created.
+#
+do_test descidx1-1.1 {
+  execsql {
+    CREATE TABLE t1(a,b);
+    CREATE INDEX i1 ON t1(b ASC);
+  }
+  get_file_format
+} {1}
+do_test descidx1-1.2 {
+  execsql {
+    CREATE INDEX i2 ON t1(a DESC);
+  }
+  get_file_format
+} {4}
+
+# Put some information in the table and verify that the descending
+# index actually works.
+#
+do_test descidx1-2.1 {
+  execsql {
+    INSERT INTO t1 VALUES(1,1);
+    INSERT INTO t1 VALUES(2,2);
+    INSERT INTO t1 SELECT a+2, a+2 FROM t1;
+    INSERT INTO t1 SELECT a+4, a+4 FROM t1;
+    SELECT b FROM t1 WHERE a>3 AND a<7;
+  }
+} {6 5 4}
+do_test descidx1-2.2 {
+  execsql {
+    SELECT a FROM t1 WHERE b>3 AND b<7;
+  }
+} {4 5 6}
+do_test descidx1-2.3 {
+  execsql {
+    SELECT b FROM t1 WHERE a>=3 AND a<7;
+  }
+} {6 5 4 3}
+do_test descidx1-2.4 {
+  execsql {
+    SELECT b FROM t1 WHERE a>3 AND a<=7;
+  }
+} {7 6 5 4}
+do_test descidx1-2.5 {
+  execsql {
+    SELECT b FROM t1 WHERE a>=3 AND a<=7;
+  }
+} {7 6 5 4 3}
+do_test descidx1-2.6 {
+  execsql {
+    SELECT a FROM t1 WHERE b>=3 AND b<=7;
+  }
+} {3 4 5 6 7}
+
+# This procedure executes the SQL.  Then it checks to see if the OP_Sort
+# opcode was executed.  If an OP_Sort did occur, then "sort" is appended
+# to the result.  If no OP_Sort happened, then "nosort" is appended.
+#
+# This procedure is used to check to make sure sorting is or is not
+# occurring as expected.
+#
+proc cksort {sql} {
+  set ::sqlite_sort_count 0
+  set data [execsql $sql]
+  if {$::sqlite_sort_count} {set x sort} {set x nosort}
+  lappend data $x
+  return $data
+}
+
+# Test sorting using a descending index.
+#
+do_test descidx1-3.1 {
+  cksort {SELECT a FROM t1 ORDER BY a}
+} {1 2 3 4 5 6 7 8 nosort}
+do_test descidx1-3.2 {
+  cksort {SELECT a FROM t1 ORDER BY a ASC}
+} {1 2 3 4 5 6 7 8 nosort}
+do_test descidx1-3.3 {
+  cksort {SELECT a FROM t1 ORDER BY a DESC}
+} {8 7 6 5 4 3 2 1 nosort}
+do_test descidx1-3.4 {
+  cksort {SELECT b FROM t1 ORDER BY a}
+} {1 2 3 4 5 6 7 8 nosort}
+do_test descidx1-3.5 {
+  cksort {SELECT b FROM t1 ORDER BY a ASC}
+} {1 2 3 4 5 6 7 8 nosort}
+do_test descidx1-3.6 {
+  cksort {SELECT b FROM t1 ORDER BY a DESC}
+} {8 7 6 5 4 3 2 1 nosort}
+do_test descidx1-3.7 {
+  cksort {SELECT a FROM t1 ORDER BY b}
+} {1 2 3 4 5 6 7 8 nosort}
+do_test descidx1-3.8 {
+  cksort {SELECT a FROM t1 ORDER BY b ASC}
+} {1 2 3 4 5 6 7 8 nosort}
+do_test descidx1-3.9 {
+  cksort {SELECT a FROM t1 ORDER BY b DESC}
+} {8 7 6 5 4 3 2 1 nosort}
+do_test descidx1-3.10 {
+  cksort {SELECT b FROM t1 ORDER BY b}
+} {1 2 3 4 5 6 7 8 nosort}
+do_test descidx1-3.11 {
+  cksort {SELECT b FROM t1 ORDER BY b ASC}
+} {1 2 3 4 5 6 7 8 nosort}
+do_test descidx1-3.12 {
+  cksort {SELECT b FROM t1 ORDER BY b DESC}
+} {8 7 6 5 4 3 2 1 nosort}
+
+do_test descidx1-3.21 {
+  cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a}
+} {4 5 6 7 nosort}
+do_test descidx1-3.22 {
+  cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
+} {4 5 6 7 nosort}
+do_test descidx1-3.23 {
+  cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
+} {7 6 5 4 nosort}
+do_test descidx1-3.24 {
+  cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a}
+} {4 5 6 7 nosort}
+do_test descidx1-3.25 {
+  cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
+} {4 5 6 7 nosort}
+do_test descidx1-3.26 {
+  cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
+} {7 6 5 4 nosort}
+
+finish_test