Yuriy Kaminskiy wrote:
> When WHERE condition is constant, there are no need to evaluate and check it 
> for
> each row. It works, but only partially:
...
> [In fact, you can move out out loop not only *whole* constant WHERE, but also
> all constant AND terms of WHERE, like this:
> SELECT * FROM t WHERE const1 AND notconst AND const2 ->
> SELECT * FROM (SELECT * FROM t WHERE notconst) WHERE const1 AND const2
> I'll take a shot on that later.]

Here it goes.

Prerequisite: previous patch.
Passes quick regression test (make test).
Possible problem: short-circuits evaluation. Should not be problem, IMO, as only
constants references? Please verify.

The author or authors of this code dedicate any and all copyright interest
in this code to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and successors.
We intend this dedication to be an overt act of relinquishment in perpetuity
of all present and future rights to this code under copyright law.

Signed-off-by: Yuriy M. Kaminskiy <yum...@gmail.com>

Index: sqlite3-3.7.8/src/where.c
===================================================================
--- sqlite3-3.7.8.orig/src/where.c      2011-10-23 20:41:44.000000000 +0400
+++ sqlite3-3.7.8/src/where.c   2011-10-23 20:48:33.000000000 +0400
@@ -4636,6 +4636,21 @@ WhereInfo *sqlite3WhereBegin(

   whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */

+  {
+    /* Move const in "WHERE const AND notconst" out of internal loop */
+    int j;
+    for(j=i=0; i<pWC->nTerm; i++){
+      if( nTabList==0 || sqlite3ExprIsConstantNotJoin(pWC->a[i].pExpr) ){
+        sqlite3ExprIfFalse(pParse, pWC->a[i].pExpr, pWInfo->iBreak,
SQLITE_JUMPIFNULL);
+        continue;
+      }
+      if( j!=i )
+        pWC->a[j]=pWC->a[i];
+      j++;
+    }
+    pWC->nTerm -= i-j;
+  }
+
   /* Assign a bit from the bitmask to every term in the FROM clause.
   **
   ** When assigning bitmask values to FROM clause cursors, it must be
========================================================================================

Part 2: Remove redundant sqlite3ExprIsConstantNotJoin call. Result should be
equivalent. Feel free to squash with above patch on apply.

Index: sqlite3-3.7.8/src/where.c
===================================================================
--- sqlite3-3.7.8.orig/src/where.c      2011-10-23 21:23:00.000000000 +0400
+++ sqlite3-3.7.8/src/where.c   2011-10-23 21:25:06.000000000 +0400
@@ -4626,18 +4626,12 @@ WhereInfo *sqlite3WhereBegin(
   whereClauseInit(pWC, pParse, pMaskSet);
   sqlite3ExprCodeConstants(pParse, pWhere);

-  /* Special case: a WHERE clause that is constant.  Evaluate the
-  ** expression and either jump over all of the code or fall thru.
-  */
-  if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
-    sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
-    pWhere = 0;
-  }
-
   whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */

   {
-    /* Move const in "WHERE const AND notconst" out of internal loop */
+    /* Special case: AND subterm of WHERE clause that is constant. Evaluate the
+    ** expression and either jump over all of the code or fall thru.
+    */
     int j;
     for(j=i=0; i<pWC->nTerm; i++){
       if( nTabList==0 || sqlite3ExprIsConstantNotJoin(pWC->a[i].pExpr) ){

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to