]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add the ability to disable constant factoring using sqlite3_test_control().
authordrh <drh@noemail.net>
Mon, 6 Dec 2010 21:06:09 +0000 (21:06 +0000)
committerdrh <drh@noemail.net>
Mon, 6 Dec 2010 21:06:09 +0000 (21:06 +0000)
Add a TCL interface to this new capability and add tests cases to the TCL
test scripts to actually use the new capability.

FossilOrigin-Name: ad8bc68197f2b47435149c3dbc035f4e7210fc76

manifest
manifest.uuid
src/expr.c
src/sqliteInt.h
src/test1.c
test/tkt-80ba201079.test

index 6b49f17704ed1548e0b78f25db233f8ac7c32bd1..497fc3d1da08080c2249459ea5246520b73eeb62 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,8 +1,8 @@
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
-C Back\sout\spart\sof\sthe\sprevious\schange\sthat\swas\snot\sreally\snecessary\sin\sorder\nto\sfix\s[80ba201079ea60],\sand\swhich\sin\sfact\sserves\sno\suseful\spurpose.
-D 2010-12-06T18:59:14
+C Add\sthe\sability\sto\sdisable\sconstant\sfactoring\susing\ssqlite3_test_control().\nAdd\sa\sTCL\sinterface\sto\sthis\snew\scapability\sand\sadd\stests\scases\sto\sthe\sTCL\ntest\sscripts\sto\sactually\suse\sthe\snew\scapability.
+D 2010-12-06T21:06:09
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 4547616ad2286053af6ccccefa242dc925e49bf0
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -131,7 +131,7 @@ F src/complete.c dc1d136c0feee03c2f7550bafc0d29075e36deac
 F src/ctime.c 4f3aadad62c6c9f0d4e5a96718516ac4e3c598df
 F src/date.c 1548fdac51377e4e7833251de878b4058c148e1b
 F src/delete.c 7ed8a8c8b5f748ece92df173d7e0f7810c899ebd
-F src/expr.c 325f49c99d982fdea3867d9fff8bec0c3e5d19aa
+F src/expr.c 1810f3056b11de99cc10e24629edf00e5fbd3a75
 F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
 F src/fkey.c 17950a28f28b23e8ad3feaac5fc88c324d2f600a
 F src/func.c 2b7cf54d2569c2eba42fe81165d1932b546681a3
@@ -181,12 +181,12 @@ F src/select.c 8a7ba246b0b4bb45df7fbc52681728a0e3deaaa7
 F src/shell.c 8517fc1f9c59ae4007e6cc8b9af91ab231ea2056
 F src/sqlite.h.in b7e8a4e4749cfae9c99e1fbe3f9f87763cbbf7a2
 F src/sqlite3ext.h c90bd5507099f62043832d73f6425d8d5c5da754
-F src/sqliteInt.h 5cfee93885789f174df186409b81812ee189e44f
+F src/sqliteInt.h b96d5ddb8b419a2ed7cf69a7778b53872d73e8a7
 F src/sqliteLimit.h a17dcd3fb775d63b64a43a55c54cb282f9726f44
 F src/status.c 496913d4e8441195f6f2a75b1c95993a45b9b30b
 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
 F src/tclsqlite.c 77c5c4b8ac7b2d94ee480e1ad626fbd921d948e4
-F src/test1.c c2aa29d0fd6db7506fb7f0de7bff1386078296df
+F src/test1.c 397809a3069c171a507a49b6f6edfc4255b0afbc
 F src/test2.c 80d323d11e909cf0eb1b6fbb4ac22276483bcf31
 F src/test3.c 056093cfef69ff4227a6bdb9108564dc7f45e4bc
 F src/test4.c 0528360b5025688002a5feb6be906ddce52eaaee
@@ -689,7 +689,7 @@ F test/tkt-4a03edc4c8.test 2865e4edbc075b954daa82f8da7cc973033ec76e
 F test/tkt-5e10420e8d.test 904d1687b3c06d43e5b3555bbcf6802e7c0ffd84
 F test/tkt-5ee23731f.test 3581260f2a71e51db94e1506ba6b0f7311d002a9
 F test/tkt-78e04e52ea.test ab52f0c1e2de6e46c910f4cc16b086bba05952b7
-F test/tkt-80ba201079.test 29357d3008cbd9c7d031884a9e8ed609ed0b022c
+F test/tkt-80ba201079.test 5618c9ae2dee98215b2e4b9eee0d08228c05f1c7
 F test/tkt-80e031a00f.test 9a154173461a4dbe2de49cda73963e04842d52f7
 F test/tkt-8454a207b9.test c583a9f814a82a2b5ba95207f55001c9f0cd816c
 F test/tkt-94c04eaadb.test be5ea61cb04dfdc047d19b5c5a9e75fa3da67a7f
@@ -897,14 +897,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P c5c53152d68218bb5e7f922271dd7c50da2361c1
-R 1dcb2efdd5f0c7709490525224087ff2
+P fa9eef865f2f399870305bef82296db25e5b3e90
+R adc371a70378ff8785a66a8184809cd8
 U drh
-Z 1d03f4cb6f394055496ff40462427480
+Z 8093631df4d65f06b173b4129fbe870c
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.6 (GNU/Linux)
 
-iD8DBQFM/TKFoxKgR168RlERAs0NAJ9DYT7vr59wTUtfXYJEEy+jTlAYTQCgjZ0A
-Sk/Mz8OxzQFizI7ulUt5ljU=
-=Pk08
+iD8DBQFM/VBEoxKgR168RlERAqlBAJ9ypWriiT3TGhHuFN/JhWSodYBScwCeObWR
+24IrqP1lO8pMhydpHueuRoA=
+=C+pp
 -----END PGP SIGNATURE-----
index b953841ca8ea54f6dea05e188d302da653faf801..b4291676b42076cd5ece9e436171ba5812853601 100644 (file)
@@ -1 +1 @@
-fa9eef865f2f399870305bef82296db25e5b3e90
\ No newline at end of file
+ad8bc68197f2b47435149c3dbc035f4e7210fc76
\ No newline at end of file
index 3a6ba9f3ee326fcc98232c039d5353244d5a3068..b902f451c4e2993b480eb1e2dc9c27c28b961ee1 100644 (file)
@@ -3040,10 +3040,22 @@ static int evalConstExpr(Walker *pWalker, Expr *pExpr){
 ** Preevaluate constant subexpressions within pExpr and store the
 ** results in registers.  Modify pExpr so that the constant subexpresions
 ** are TK_REGISTER opcodes that refer to the precomputed values.
+**
+** This routine is a no-op if the jump to the cookie-check code has
+** already occur.  Since the cookie-check jump is generated prior to
+** any other serious processing, this check ensures that there is no
+** way to accidently bypass the constant initializations.
+**
+** This routine is also a no-op if the SQLITE_FactorOutConst optimization
+** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
+** interface.  This allows test logic to verify that the same answer is
+** obtained for queries regardless of whether or not constants are
+** precomputed into registers or if they are inserted in-line.
 */
 void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
   Walker w;
   if( pParse->cookieGoto ) return;
+  if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
   w.xExprCallback = evalConstExpr;
   w.xSelectCallback = 0;
   w.pParse = pParse;
index 6e8b768d573e3a7a770918d82bfbb5582c0fa5c1..4f0e08ea76dc2c436b2805e00313017afbb1b871 100644 (file)
@@ -934,6 +934,7 @@ struct sqlite3 {
 #define SQLITE_IndexSearch    0x08        /* Disable indexes for searching */
 #define SQLITE_IndexCover     0x10        /* Disable index covering table */
 #define SQLITE_GroupByOrder   0x20        /* Disable GROUPBY cover of ORDERBY */
+#define SQLITE_FactorOutConst 0x40        /* Disable factoring out constants */
 #define SQLITE_OptMask        0xff        /* Mask of all disablable opts */
 
 /*
index 27834f36e1b083f7a5e9692d73ce3af09d9f9e97..bab78451fb72e8bd50fdf10aee0cd01445175f45 100644 (file)
@@ -5317,6 +5317,64 @@ static int test_print_eqp(
 }
 #endif /* SQLITE_OMIT_EXPLAIN */
 
+/*
+**      optimization_control DB OPT BOOLEAN
+**
+** Enable or disable query optimizations using the sqlite3_test_control()
+** interface.  Disable if BOOLEAN is false and enable if BOOLEAN is true.
+** OPT is the name of the optimization to be disabled.
+*/
+static int optimization_control(
+  void * clientData,
+  Tcl_Interp *interp,
+  int objc,
+  Tcl_Obj *CONST objv[]
+){
+  int i;
+  sqlite3 *db;
+  const char *zOpt;
+  int onoff;
+  int mask;
+  static const struct {
+    const char *zOptName;
+    int mask;
+  } aOpt[] = {
+    { "all",              SQLITE_OptMask        },
+    { "query-flattener",  SQLITE_QueryFlattener },
+    { "column-cache",     SQLITE_ColumnCache    },
+    { "index-sort",       SQLITE_IndexSort      },
+    { "index-search",     SQLITE_IndexSearch    },
+    { "index-cover",      SQLITE_IndexCover     },
+    { "groupby-order",    SQLITE_GroupByOrder   },
+    { "factor-constants", SQLITE_FactorOutConst },
+  };
+
+  if( objc!=4 ){
+    Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
+    return TCL_ERROR;
+  }
+  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
+  if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR;
+  zOpt = Tcl_GetString(objv[2]);
+  for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
+    if( strcmp(zOpt, aOpt[i].zOptName)==0 ){
+      mask = aOpt[i].mask;
+      break;
+    }
+  }
+  if( onoff ) mask = ~mask;
+  if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){
+    Tcl_AppendResult(interp, "unknown optimization - should be one of:",
+                     (char*)0);
+    for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
+      Tcl_AppendResult(interp, " ", aOpt[i].zOptName);
+    }
+    return TCL_ERROR;
+  }
+  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask);
+  return TCL_OK;
+}
+
 /*
 ** Register commands with the TCL interpreter.
 */
@@ -5434,6 +5492,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp){
      { "save_prng_state",               save_prng_state,    0 },
      { "restore_prng_state",            restore_prng_state, 0 },
      { "reset_prng_state",              reset_prng_state,   0 },
+     { "optimization_control",          optimization_control,0},
      { "tcl_objproc",                   runAsObjProc,       0 },
 
      /* sqlite3_column_*() API */
index bd22acb0ca88beafe46f0a07e2c822ff05fec9f7..7abeeb649509f70124a50eee8c53ab6ccd9fb889 100644 (file)
@@ -39,6 +39,35 @@ do_test tkt-80ba2-101 {
         OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));
   }
 } {A B}
+do_test tkt-80ba2-102 {
+  optimization_control db factor-constants 0
+  db cache flush
+  db eval {
+    SELECT * FROM t1, t2
+     WHERE (a='A' AND b='X')
+        OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));
+  }
+} {A B}
+optimization_control db all 1
+
+# Verify that the optimization_control command is actually working
+#
+do_test tkt-80ba2-150 {
+  optimization_control db factor-constants 1
+  db cache flush
+  set x1 [db eval {EXPLAIN 
+    SELECT * FROM t1, t2
+     WHERE (a='A' AND b='X')
+        OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));}]
+  optimization_control db factor-constants 0
+  db cache flush
+  set x2 [db eval {EXPLAIN 
+    SELECT * FROM t1, t2
+     WHERE (a='A' AND b='X')
+        OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));}]
+
+  expr {$x1==$x2}
+} {0}
 
 do_test tkt-80ba2-200 {
   db eval {
@@ -92,5 +121,20 @@ do_test tkt-80ba2-201 {
                                WHERE obj_context = 'exported_pools'));
   }
 } {300 object_change 2048}
+do_test tkt-80ba2-202 {
+  optimization_control db factor-constants 0
+  db cache flush
+  db eval {
+    SELECT entry_type,
+           entry_types.name,
+           entry_id
+      FROM timeline JOIN entry_types ON entry_type = entry_types.id
+     WHERE (entry_types.name = 'cli_command' AND entry_id=2114)
+        OR (entry_types.name = 'object_change'
+             AND entry_id IN (SELECT change_id
+                              FROM object_changes
+                               WHERE obj_context = 'exported_pools'));
+  }
+} {300 object_change 2048}
 
 finish_test