]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Generalize the constant propagation optimization so that it applies on every
authordrh <drh@noemail.net>
Thu, 26 Jul 2018 23:47:11 +0000 (23:47 +0000)
committerdrh <drh@noemail.net>
Thu, 26 Jul 2018 23:47:11 +0000 (23:47 +0000)
WHERE close, not just those that contain a subquery.  This then demonstrates
that the current implementation is inadequate since it does not take into
account collating sequences.

FossilOrigin-Name: 57eb2abd5b270d65be5e0f138f0d46899fa6091df3ba20b0ea7ef04244a15d48

manifest
manifest.uuid
src/expr.c
src/select.c
test/whereL.test

index 7fe18e364a1aed23a8de962d1e8b9c5b285be374..4a1a680ce5261ffbdbc1194debf0b41497e8729b 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Initial\simplementation\sof\sthe\sWHERE-clause\sconstant\spropagation\soptimization.
-D 2018-07-26T21:16:53.742
+C Generalize\sthe\sconstant\spropagation\soptimization\sso\sthat\sit\sapplies\son\severy\nWHERE\sclose,\snot\sjust\sthose\sthat\scontain\sa\ssubquery.\s\sThis\sthen\sdemonstrates\nthat\sthe\scurrent\simplementation\sis\sinadequate\ssince\sit\sdoes\snot\stake\sinto\naccount\scollating\ssequences.
+D 2018-07-26T23:47:11.108
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F Makefile.in 0a3a6c81e6fcb969ff9106e882f0a08547014ba463cb6beca4c4efaecc924ee6
@@ -450,7 +450,7 @@ F src/date.c ebe1dc7c8a347117bb02570f1a931c62dd78f4a2b1b516f4837d45b7d6426957
 F src/dbpage.c 4aa7f26198934dbd002e69418220eae3dbc71b010bbac32bd78faf86b52ce6c3
 F src/dbstat.c edabb82611143727511a45ca0859b8cd037851ebe756ae3db289859dd18b6f91
 F src/delete.c 4c8c7604277a2041647f96b78f4b9a47858e9217e4fb333d35e7b5ab32c5b57f
-F src/expr.c bb148f4c45b6e53b5b58a912974253547e8862685cb001fbdfaef0ab24812e6f
+F src/expr.c 8187c1be1003230f027bfc5f5a9830aa43d55beb67d87f4a2589d125749fbcfd
 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
 F src/fkey.c b1da9ef8dc834603bb0d28972378a7ce65897847f9a1e89ab800bbdf24c788ee
 F src/func.c 7c288b4ce309b5a8b8473514b88e1f8e69a80134509a8c0db8e39c858e367e7f
@@ -498,7 +498,7 @@ F src/printf.c 7f6f3cba8e0c49c19e30a1ff4e9aeda6e06814dcbad4b664a69e1b6cb6e7e365
 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
 F src/resolve.c 797088662ed61102485e3070ba3b3f7828bd5ef6a588223ba6865d77d52f6cea
 F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
-F src/select.c cd3993844ab1186e80069f9de2304e730732cfea1f718e603293792d4401d037
+F src/select.c 81c6517a8d7b6aae805aaa4171187390f72988d1ceec343ad2f51be09513b289
 F src/shell.c.in f6ebd05c461805a7c708333cd645e74e0a93560d2118f5adb73a75d8c9cf6b01
 F src/sqlite.h.in c6451bb876adced3aba5b1682c6317d215c5eceaba21a6ce979e71a0b8d0bf95
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
@@ -1617,7 +1617,7 @@ F test/whereH.test e4b07f7a3c2f5d31195cd33710054c78667573b2
 F test/whereI.test b7769ee8dbefd987fb266715fee887f05f9ff180016b06fca7fa402df739193b
 F test/whereJ.test 88287550f6ee604422403b053455b1ad894eeaa5c35d348532dfa1439286cb9a
 F test/whereK.test f8e3cf26a8513ecc7f514f54df9f0572c046c42b
-F test/whereL.test 46492cf4b3b2bfa5bb8adb8d6565f31c010bf8f23baf66bcba4e38cec89117a2
+F test/whereL.test eca38f3f9e056bec962c3cf9abb8825a1701311a3fdc281471460cc127618714
 F test/wherefault.test 1374c3aa198388925246475f84ad4cd5f9528864
 F test/wherelfault.test 9012e4ef5259058b771606616bd007af5d154e64cc25fa9fd4170f6411db44e3
 F test/wherelimit.test 592081800806d297dd7449b1030c863d2883d6d42901837ccd2e5a9bd962edb0
@@ -1753,10 +1753,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 0e3de8abbb0c7ae64e637776cb055ce79736f99a103e00e44d17a6b091b98c81
-R 83557e363f35588e2f481ff35d0d8054
-T *branch * propagate-const-opt
-T *sym-propagate-const-opt *
-T -sym-trunk *
+P 2fb82ad8ebb6434438c0d235b1239444fb08c8711cea2c5a9ed955fedd0acdec
+R aac189787e29efcfb7af237e9604f0a0
 U drh
-Z 8488ca3ac81dbe859bc19508bb265e68
+Z b571d91896795a40b7aa465c4c8e4005
index 8c60a8812f24b7ae6a8b6bb80c71be5d6be8b766..db9ef1c737f78db225f895f34b45a020ff9a6334 100644 (file)
@@ -1 +1 @@
-2fb82ad8ebb6434438c0d235b1239444fb08c8711cea2c5a9ed955fedd0acdec
\ No newline at end of file
+57eb2abd5b270d65be5e0f138f0d46899fa6091df3ba20b0ea7ef04244a15d48
\ No newline at end of file
index 019bf17b994843452c14e9d0fb4edb002a06bd4e..6e5100a224cb97166ade0ad31b763025ee898be7 100644 (file)
@@ -4935,7 +4935,7 @@ int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){
       if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
     }else if( pA->op==TK_COLLATE ){
       if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
-    }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
+    }else if( pA->op!=TK_UPLUS && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
       return 2;
     }
   }
index a0f140ef926012eceb08181430aec79454a9852a..2060524c495ec84db28abee2625d6849d4d2bc66 100644 (file)
@@ -4100,6 +4100,7 @@ static void constInsert(
   if( pConst->apExpr==0 ){
     pConst->nConst = 0;
   }else{
+    while( pValue->op==TK_UPLUS ) pValue = pValue->pLeft;
     pConst->apExpr[pConst->nConst*2-2] = pColumn;
     pConst->apExpr[pConst->nConst*2-1] = pValue;
   }
@@ -5726,6 +5727,20 @@ int sqlite3Select(
   }
 #endif
 
+  /* Do the constant propagation optimization */
+  if( OptimizationEnabled(db, SQLITE_PropagateConst)
+   && propagateConstants(pParse, p)
+  ){
+#if SELECTTRACE_ENABLED
+    if( sqlite3SelectTrace & 0x100 ){
+      SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
+      sqlite3TreeViewSelect(0, p, 0);
+    }
+#endif
+  }else{
+    SELECTTRACE(0x100,pParse,p,("Constant propagation not possible\n"));
+  }
+
   /* For each term in the FROM clause, do two things:
   ** (1) Authorized unreferenced tables
   ** (2) Generate code for all sub-queries
@@ -5790,20 +5805,6 @@ int sqlite3Select(
     */
     pParse->nHeight += sqlite3SelectExprHeight(p);
 
-    /* Do the constant propagation optimization */
-    if( OptimizationEnabled(db, SQLITE_PropagateConst)
-     && propagateConstants(pParse, p)
-    ){
-#if SELECTTRACE_ENABLED
-      if( sqlite3SelectTrace & 0x100 ){
-        SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
-        sqlite3TreeViewSelect(0, p, 0);
-      }
-#endif
-    }else{
-      SELECTTRACE(0x100,pParse,p,("Constant propagation not possible\n"));
-    }
-
     /* Make copies of constant WHERE-clause terms in the outer query down
     ** inside the subquery.  This can help the subquery to run more efficiently.
     */
index 04c8f066ccc8abee054d481212fd23f91e462e8b..33e612b8550d186e49c315440311dd3471f0c5e8 100644 (file)
@@ -36,4 +36,19 @@ do_eqp_test 110 {
   `--SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (a=?)
 }
 
+# The scan of the t1 table goes first since that enables the ORDER BY
+# sort to be omitted.  This would not be possible without constant
+# propagation because without it the t1 table would depend on t3.
+#
+do_eqp_test 120 {
+  SELECT * FROM t1, t2, t3
+   WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=5
+  ORDER BY t1.a;
+} {
+  QUERY PLAN
+  |--SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (a=?)
+  |--SEARCH TABLE t2 USING INDEX sqlite_autoindex_t2_1 (a=?)
+  `--SCAN TABLE t3
+}
+
 finish_test