]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
In the automatic index generator logic, be more precise about when a
authordrh <>
Tue, 30 Nov 2021 14:07:58 +0000 (14:07 +0000)
committerdrh <>
Tue, 30 Nov 2021 14:07:58 +0000 (14:07 +0000)
partial automatic index is allowed in order to capture more cases where it
is legal to use a partial automatic index.

FossilOrigin-Name: 664b461bb5063d98047fc2e51a3827235cd9f55ca2e23cb66e719eac53fb5437

manifest
manifest.uuid
src/where.c
test/autoindex4.test

index 3577caddb5a442f881eedc2f84a78998b7e6b8e5..1799086100a0f22ea4a9a8edabe3b2fac3278698 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\sin\sthe\sproposed\sdate/time\sfunction\senhancements:\s\s(1)\sAdd\sthe\nunixepoch()\sfunction,\s(2)\sthe\s'auto'\smodifier\sand\s(3)\sthe\s'julianday'\smodifier.
-D 2021-11-29T18:09:54.447
+C In\sthe\sautomatic\sindex\sgenerator\slogic,\sbe\smore\sprecise\sabout\swhen\sa\npartial\sautomatic\sindex\sis\sallowed\sin\sorder\sto\scapture\smore\scases\swhere\sit\nis\slegal\sto\suse\sa\spartial\sautomatic\sindex.
+D 2021-11-30T14:07:58.372
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -637,7 +637,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
 F src/wal.c ed0398a7adf02c31e34aada42cc86c58f413a7afe5f741a5d373ad087abde028
 F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a
 F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b
-F src/where.c de0d4ff409c7b62a8803f9f267cc2c7fedddbc00de9ab7b5382c507383c18665
+F src/where.c 4b276017881185d0e1dc984df66c835cde6faf03834ef9ff34f48653f9144dec
 F src/whereInt.h 83877a75a1bce056ea44aff02f1dfa958ad1d6038c213ddadb8652003b45151d
 F src/wherecode.c 1f5b62f46d284c8886945eb7438415bc27e23e87bb60b9ee468fa6bd31268f33
 F src/whereexpr.c 17bdbf4f5b490e70a18635498f0b910a558f953a9bf80af7f19cbde6e60e6825
@@ -705,7 +705,7 @@ F test/autoinc.test 997d6f185f138229dc4251583a1d04816423dddc2fc034871a01aeb1d728
 F test/autoindex1.test fe27af92eaf884bd9c38f94be3e8afa04ec494e5eefb189902026181a6175f5e
 F test/autoindex2.test 12ef578928102baaa0dc23ad397601a2f4ecb0df
 F test/autoindex3.test 2d13958a5617e987624a428d7aed91bf51f322b49b476e3573fadec697ce6da5
-F test/autoindex4.test 49d3cd791a9baa16fb461d7ea3de80d019a819cf
+F test/autoindex4.test 75cb1191a552b8201351f5a50d160fcb9387a0fbbfb820c77798bfee7da3f8cf
 F test/autoindex5.test 2ee94f033b87ca0160e08d81034c507aff8e230df2627f0304fa309b2fee19a3
 F test/autovacuum.test 00671369bbf96c6a49989a9425f5b78b94075d6a4b031e5e00000c2c32f365df
 F test/autovacuum2.test 76f7eb4fe6a6bf6d33a196a7141dba98886d2fb53a268d7feca285d5da4759d7
@@ -1933,8 +1933,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 8ad1fcaa1b734e32d8d67c4cca7a23d2b9717ae5c7c2c5ad5a3411a503ee23ae 559fdc0aa76f4c207f99f7b0cee42043b402dc388165817529d9963b115a798c
-R ff09119d5c0ee6b5a0429abffd206856
-T +closed 559fdc0aa76f4c207f99f7b0cee42043b402dc388165817529d9963b115a798c
+P 19c51b46e4095ee28badb10f4e08bbd330bda320c9a8806e93b8fc60ba211a2e
+R 0fbfb4a13255ae0bad405a99d2bdf9ec
 U drh
-Z 24b943d8efa351054aa299f0bcbeffcd
+Z 511f59b4656ae7d0825d672340080bad
index d0851eaaea868e4cec55076c5f9f1125d41e207e..8a15f64b8a2c2d01320d16cf850915a60d8b8e8d 100644 (file)
@@ -1 +1 @@
-19c51b46e4095ee28badb10f4e08bbd330bda320c9a8806e93b8fc60ba211a2e
\ No newline at end of file
+664b461bb5063d98047fc2e51a3827235cd9f55ca2e23cb66e719eac53fb5437
\ No newline at end of file
index 7a8342675d7f6a104b9ae5f9367ebe971eea4a53..54e4ea81963e91f024c3f2f90d2504b3e8fbd323 100644 (file)
@@ -791,13 +791,13 @@ static void constructAutomaticIndex(
   idxCols = 0;
   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
     Expr *pExpr = pTerm->pExpr;
-    assert( !ExprHasProperty(pExpr, EP_FromJoin)    /* prereq always non-zero */
-         || pExpr->iRightJoinTable!=pSrc->iCursor   /*   for the right-hand   */
-         || pLoop->prereq!=0 );                     /*   table of a LEFT JOIN */
-    if( pLoop->prereq==0
-     && (pTerm->wtFlags & TERM_VIRTUAL)==0
-     && !ExprHasProperty(pExpr, EP_FromJoin)
-     && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
+    /* Make the automatic index a partial index if there are terms in the
+    ** WHERE clause (or the ON clause of a LEFT join) that constrain which
+    ** rows of the target table (pSrc) that can be used. */
+    if( (pTerm->wtFlags & TERM_VIRTUAL)==0
+     && ((pSrc->fg.jointype&JT_LEFT)==0 || ExprHasProperty(pExpr,EP_FromJoin))
+     && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor)
+    ){
       pPartial = sqlite3ExprAnd(pParse, pPartial,
                                 sqlite3ExprDup(pParse->db, pExpr, 0));
     }
index 0e7a80df216d9ef41537ba2c4e3d5dc790f9dc5a..24604af588282520a54de8b7a236e99694359c37 100644 (file)
@@ -79,5 +79,98 @@ do_execsql_test autoindex4-3.1 {
     ORDER BY Items.ItemName;
 } {Item1 Item2}
 
+# 2021-11-30 - Enhancement to help the automatic index mechanism to
+# create a partial index more often.
+#
+unset -nocomplain id data1 data2 jointype onclause whereclause answer
+foreach {id data1 data2 jointype onclause whereclause answer} {
+  1
+  VALUES(1,2),(3,4)
+  VALUES(1,2),(3,4)
+  {LEFT JOIN}
+  a=x
+  {y=4 OR y IS NULL}
+  {3 4 3 4}
+
+  2
+  VALUES(1,2),(3,4)
+  VALUES(1,2),(3,4)
+  {LEFT JOIN}
+  {a=x AND y=4}
+  {coalesce(y,4)==4}
+  {1 2 {} {} 3 4 3 4}
+
+  3
+  VALUES(1,2),(3,4)
+  VALUES(1,2),(3,4)
+  {JOIN}
+  {a=x}
+  {y=4 OR y IS NULL}
+  {3 4 3 4}
+
+  4
+  VALUES(1,2),(3,4)
+  VALUES(1,2),(3,4)
+  {JOIN}
+  {a=x AND y=4}
+  {coalesce(y,4)==4}
+  {3 4 3 4}
+
+  5
+  VALUES(1,2),(3,4),(NULL,4)
+  VALUES(1,2),(3,4)
+  {LEFT JOIN}
+  a=x
+  {y=4 OR y IS NULL}
+  {3 4 3 4 {} 4 {} {}}
+
+  6
+  VALUES(1,2),(3,4)
+  VALUES(1,2),(3,4),(NULL,4)
+  {LEFT JOIN}
+  {a=x AND y=4}
+  {coalesce(y,4)==4}
+  {1 2 {} {} 3 4 3 4}
+
+  7
+  VALUES(1,2),(3,4),(NULL,4)
+  VALUES(1,2),(3,4),(NULL,4)
+  {JOIN}
+  {a=x}
+  {y=4 OR y IS NULL}
+  {3 4 3 4}
+
+  8
+  VALUES(1,2),(3,4)
+  VALUES(1,2),(3,4)
+  {JOIN}
+  {a=x AND y=4}
+  {coalesce(y,4)==4}
+  {3 4 3 4}
+} {
+  do_test autoindex4-4.$id.0 {
+    db eval {
+       DROP TABLE IF EXISTS t1;
+       CREATE TABLE t1(a INT, b INT);
+       DROP TABLE IF EXISTS t2;
+       CREATE TABLE t2(x INT, y INT);
+    }
+    db eval "INSERT INTO t1(a,b) $data1;"
+    db eval "INSERT INTO t2(x,y) $data2;"
+  } {}
+  set sql "SELECT * FROM t1 $jointype t2 ON $onclause WHERE $whereclause"
+  # puts "sql = $sql"
+  do_test autoindex4-4.$id.1 {
+    db eval {PRAGMA automatic_index=ON;}
+    db eval $sql
+  } $answer
+  do_test autoindex4-4.$id.2 {
+    db eval {PRAGMA automatic_index=OFF;}
+    db eval $sql
+  } $answer
+}
+
+
+
 
 finish_test