]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add [/info/cec6a06508239c09e363?ln=5525-5531|restriction (5)] to the
authordrh <>
Thu, 11 May 2023 21:15:55 +0000 (21:15 +0000)
committerdrh <>
Thu, 11 May 2023 21:15:55 +0000 (21:15 +0000)
omit-noop-join optimization.  Fix for the issue reported by
[forum/forumpost/49f2c7f690|forum post 49f2c7f690].

FossilOrigin-Name: 3e9c9bbdb59b9d500ff218db538c047c83da7ac18ebb95c3ee7629ab15e0b43a

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

index aada83d7c6e23f1597fc96fbeab2b2c8d27bb144..d6238b11426bc5bc38fc84bb4a420a2e9c1f5a3a 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\sminor\stypo\sin\sa\scomment.\s\sNo\scode\schanges.
-D 2023-05-11T18:03:11.662
+C Add\s[/info/cec6a06508239c09e363?ln=5525-5531|restriction\s(5)]\sto\sthe\nomit-noop-join\soptimization.\s\sFix\sfor\sthe\sissue\sreported\sby\n[forum/forumpost/49f2c7f690|forum\spost\s49f2c7f690].
+D 2023-05-11T21:15:55.618
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -720,7 +720,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
 F src/wal.c 7a65f64bfe4a783c5e2df73ffb0efc383dec934dee9e3ac706b2eeb3631d17ac
 F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a
 F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b
-F src/where.c 324445b046167c86c6fb4c94d4be816edcbbbf6df17fd94f1def2c3dd27520d5
+F src/where.c cec6a06508239c09e3637b52a12484fc9a84dc4302d45b4a311cce2ea6e4fd47
 F src/whereInt.h e25203e5bfee149f5f1225ae0166cfb4f1e65490c998a024249e98bb0647377c
 F src/wherecode.c b300db0bcd84ad6c2642bf3f509f92fad7b7d697b9856b64dd66d692d184d054
 F src/whereexpr.c 22cf19b0ececeaf838daed1039c5231a8778784eba5ad67b991442a23473fd3f
@@ -1230,7 +1230,7 @@ F test/ioerr4.test f130fe9e71008577b342b8874d52984bd04ede2c
 F test/ioerr5.test 2edfa4fb0f896f733071303b42224df8bedd9da4
 F test/ioerr6.test a395a6ab144b26a9e3e21059a1ab6a7149cca65b
 F test/istrue.test e7f285bb70282625c258e866ce6337d4c762922f5a300e1b50f958aef6e7d9c9
-F test/join.test aea7a4f55b2d9eb8ef3434ea78f55b15bd688ab6136a11105c9c52f77424f199
+F test/join.test f7abfef3faeaf2800308872e33a57e5b6e4a2b44fb8c6b90c6068412e71a6cf4
 F test/join2.test 8561fe82ce434ac96de91544072e578dc2cadddf2d9bc9cd802f866a9b92502e
 F test/join3.test 6f0c774ff1ba0489e6c88a3e77b9d3528fb4fda0
 F test/join4.test 1a352e4e267114444c29266ce79e941af5885916
@@ -2068,8 +2068,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P caca67d30670913914335aa3edff784e1c3d26aed70ae32efb5607ccf31ad142
-R fd8d4680e1e19432e6f44d2411716e16
+P 607cfb5bc5c0fb8b789944c2326cfdebf0629e45fbf0a61dd5f667ed685a1bbe
+R 8116201afc21cb8eec37acb602fb93db
 U drh
-Z e79912786f3f45d1ec22559d3cd92d52
+Z 3170c3485800599271837b45b05d7827
 # Remove this line to create a well-formed Fossil manifest.
index 9649ad4ed39a61e9735f910b842a763c2f096718..a8d9fe99eca9d7fee021be99915706c5ad10da24 100644 (file)
@@ -1 +1 @@
-607cfb5bc5c0fb8b789944c2326cfdebf0629e45fbf0a61dd5f667ed685a1bbe
\ No newline at end of file
+3e9c9bbdb59b9d500ff218db538c047c83da7ac18ebb95c3ee7629ab15e0b43a
\ No newline at end of file
index 527e1a3a67548b5ed4d7a964a9661dd588b4a48f..cb41f0f3a3b27b2d72461eeff3e0c77da3a27528 100644 (file)
@@ -5522,6 +5522,13 @@ static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){
 **      at most a single row.
 **   4) The table must not be referenced by any part of the query apart
 **      from its own USING or ON clause.
+**   5) The table must not have an inner-join ON or USING clause if there is
+**      a RIGHT JOIN anywhere in the query.  Otherwise the ON/USING clause
+**      might move from the right side to the left side of the RIGHT JOIN.
+**      Note: Due to (2), this condition can only arise if the table is
+**      the right-most table of a subquery that was flattened into the
+**      main query and that subquery was the right-hand operand of an
+**      inner join that held an ON or USING clause.
 **
 ** For example, given:
 **
@@ -5547,6 +5554,7 @@ static SQLITE_NOINLINE Bitmask whereOmitNoopJoin(
 ){
   int i;
   Bitmask tabUsed;
+  int hasRightJoin;
 
   /* Preconditions checked by the caller */
   assert( pWInfo->nLevel>=2 );
@@ -5561,6 +5569,7 @@ static SQLITE_NOINLINE Bitmask whereOmitNoopJoin(
   if( pWInfo->pOrderBy ){
     tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
   }
+  hasRightJoin = (pWInfo->pTabList->a[0].fg.jointype & JT_LTORJ)!=0;
   for(i=pWInfo->nLevel-1; i>=1; i--){
     WhereTerm *pTerm, *pEnd;
     SrcItem *pItem;
@@ -5583,6 +5592,12 @@ static SQLITE_NOINLINE Bitmask whereOmitNoopJoin(
           break;
         }
       }
+      if( hasRightJoin
+       && ExprHasProperty(pTerm->pExpr, EP_InnerON)
+       && pTerm->pExpr->w.iJoin==pItem->iCursor
+      ){
+        break;  /* restriction (5) */
+      }
     }
     if( pTerm<pEnd ) continue;
     WHERETRACE(0xffffffff, ("-> drop loop %c not used\n", pLoop->cId));
index 44bfb3bef2593fa1c916c7b6f1e0d7641782977e..aa526aeb2996ced87091231a557971c2432368c6 100644 (file)
@@ -1263,4 +1263,30 @@ do_execsql_test join-29.3 {
   SELECT * FROM t1 JOIN v2 ON false FULL OUTER JOIN t0 ON true;
 } {NULL NULL 1}
 
+# 2023-05-11 https://sqlite.org/forum/forumpost/49f2c7f690
+# Verify that omit-noop-join optimization does not apply if the table
+# to be omitted has an inner-join constraint and there is a RIGHT JOIN
+# anywhere in the query.
+#
+reset_db
+db null NULL
+do_execsql_test join-30.1 {
+  CREATE TABLE t0(z INT);         INSERT INTO t0 VALUES(1),(2);
+  CREATE TABLE t1(a INT);         INSERT INTO t1 VALUES(1);
+  CREATE TABLE t2(b INT);         INSERT INTO t2 VALUES(2);
+  CREATE TABLE t3(c INT, d INT);  INSERT INTO t3 VALUES(3,4);
+  CREATE TABLE t4(e INT);         INSERT INTO t4 VALUES(5);
+  CREATE VIEW v5(x,y) AS SELECT c, d FROM t3 LEFT JOIN t4 ON false;
+}
+do_execsql_test join-30.2 {
+  SELECT DISTINCT a, b
+    FROM t1 RIGHT JOIN t2 ON a=b LEFT JOIN v5 ON false
+   WHERE x <= y;
+} {}
+do_execsql_test join-30.3 {
+  SELECT DISTINCT a, b
+    FROM t0 JOIN t1 ON z=a RIGHT JOIN t2 ON a=b LEFT JOIN v5 ON false
+   WHERE x <= y;
+} {}
+
 finish_test