From: drh <> Date: Mon, 20 Jun 2022 18:26:14 +0000 (+0000) Subject: Do not allow FROM-clause terms on the left side of a RIGHT or FULL JOIN to X-Git-Tag: version-3.39.0~16 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=436c28a265044008ae18ba55b040358b52b2c531;p=thirdparty%2Fsqlite.git Do not allow FROM-clause terms on the left side of a RIGHT or FULL JOIN to be reordered. [forum:/forumpost/6650cd40b5634f35|forum post 6650cd40b5634f35]. This is probably more strict that necessary to get correct behavior, but for the first release that supports RIGHT/FULL JOIN it is perhaps better to be correct than fast. A less strict constraint might be to prohibit FROM-clause terms that originate on the left side of a RIGHT JOIN from crossing from the right side to the left side of a LEFT JOIN. Revisit this later. FossilOrigin-Name: 238453ffab0ba1bdddb529be35da82d5e8fb312a9574003a5441f455e601a909 --- diff --git a/manifest b/manifest index a9812cacf6..a63676b224 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Do\snot\sallow\san\sON\sclause\sto\sreferences\stables\sto\sits\sright\sif\sthere\sis\sa\nRIGHT\sor\sLEFT\sjoin\sanywhere\sin\sthe\squery.\s\sOther\sRDBMSes\sprohibit\sthis\salways,\nbut\sSQLite\smust\sallow\sON\sclauses\sto\sreference\stables\sto\stheir\sright\sfor\slegacy\ncompatibility,\sunless\sthere\sis\sa\sRIGHT\sor\sLEFT\sjoin\ssomeplace\sin\sthe\squery,\nin\swhich\scase\sthere\sis\sno\slegacy\sto\ssupport. -D 2022-06-20T17:04:44.141 +C Do\snot\sallow\sFROM-clause\sterms\son\sthe\sleft\sside\sof\sa\sRIGHT\sor\sFULL\sJOIN\sto\nbe\sreordered.\s[forum:/forumpost/6650cd40b5634f35|forum\spost\s6650cd40b5634f35].\nThis\sis\sprobably\smore\sstrict\sthat\snecessary\sto\sget\scorrect\sbehavior,\nbut\sfor\sthe\sfirst\srelease\sthat\ssupports\sRIGHT/FULL\sJOIN\sit\sis\sperhaps\sbetter\nto\sbe\scorrect\sthan\sfast.\s\sA\sless\sstrict\sconstraint\smight\sbe\sto\sprohibit\nFROM-clause\sterms\sthat\soriginate\son\sthe\sleft\sside\sof\sa\sRIGHT\sJOIN\sfrom\ncrossing\sfrom\sthe\sright\sside\sto\sthe\sleft\sside\sof\sa\sLEFT\sJOIN.\s\sRevisit\sthis\nlater. +D 2022-06-20T18:26:14.716 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -657,7 +657,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 F src/wal.c b9df133a705093da8977da5eb202eaadb844839f1c7297c08d33471f5491843d F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b -F src/where.c 2db3d41a1fd3b93d86680b4f33530d1f96ce9941771ebee80effba071d9d1e38 +F src/where.c ce4cb0246b6a21bbc728405ade013e1ea2d8d7662e720bf3601cdf57877738fd F src/whereInt.h b48ca529ffe293c18cbfa8326af18a09e39910de66fb3e96ef788c7cbf8ef3a7 F src/wherecode.c 0b09abfcb88c61c6a6984a3e065786631ff35495e9bdf865e6b74ab0a1299c5b F src/whereexpr.c 3b5f9f6f9fc07039b242ecf7a66e87392e9c49d62923b3c593c904090bd9345c @@ -1168,7 +1168,7 @@ F test/join4.test 1a352e4e267114444c29266ce79e941af5885916 F test/join5.test d22b6cba8fb59ab3f1c82701434c360705eb12d4ce200c449f37b018fc47681a F test/join6.test f809c025fa253f9e150c0e9afd4cef8813257bceeb6f46e04041228c9403cc2c F test/join7.test 2268dcbb54b724391dda3748ea95c60d960607ffeed67885675998e7117697f6 -F test/join8.test 6d063ef3fa580ff9e50015107b0e6caf51e7055731749ba6391b8f11c1e00e3f +F test/join8.test d0e75bf880a1b1ee8fda57d8187170c5b2ff0c0cd0e1d6d474faa99fe1283d8d F test/join9.test 9056ddd3b0c0f4f9d658f4521038d9a37dc23ead8ca9a505d0b0db2b6a471e05 F test/joinA.test 7eab225dc1c1ab258a5e62513a4ed7cabbd3db971d59d5d92f4fb6fa14c12f6a F test/joinB.test 1b2ba3fc8568b49411787fccbf540570c148e9b6a53a30f80691cb6268098ded @@ -1978,8 +1978,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 fb0a23b6789da8e934562ce9ebd9d58ea13a10fd10dee5cbfc7ac8f394e1aeec -R 9a31acf363f55129767709f03c1bfdcf +P e615dbe02ca949252d1526ed5c48f8ce08159773ea2008ce666484379d0d9854 +R 48589c176c5ab1c40074a760cfbc3f01 U drh -Z ae2f170f0ee7cf50c320a853890db816 +Z 3000f0f04f5b94504d5251d15c1dcd1e # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index bd8414421c..7f5c636bac 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e615dbe02ca949252d1526ed5c48f8ce08159773ea2008ce666484379d0d9854 \ No newline at end of file +238453ffab0ba1bdddb529be35da82d5e8fb312a9574003a5441f455e601a909 \ No newline at end of file diff --git a/src/where.c b/src/where.c index ed2a2d2218..196c508b0d 100644 --- a/src/where.c +++ b/src/where.c @@ -4180,11 +4180,21 @@ static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ pNew->iTab = iTab; pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR; pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); - if( bFirstPastRJ || (pItem->fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){ + if( bFirstPastRJ + || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0 + ){ /* Add prerequisites to prevent reordering of FROM clause terms ** across CROSS joins and outer joins. The bFirstPastRJ boolean ** prevents the right operand of a RIGHT JOIN from being swapped with - ** other elements even further to the right. */ + ** other elements even further to the right. + ** + ** The JT_LTORJ term prevents any FROM-clause term reordering for terms + ** to the left of a RIGHT JOIN. This is conservative. Relaxing this + ** constraint somewhat to prevent terms from crossing from the right + ** side of a LEFT JOIN over to the left side when they are on the + ** left side of a RIGHT JOIN would be sufficient for all known failure + ** cases. FIX ME: Implement this optimization. + */ mPrereq |= mPrior; bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0; } diff --git a/test/join8.test b/test/join8.test index 46bf38aa6d..97b0fe13df 100644 --- a/test/join8.test +++ b/test/join8.test @@ -764,5 +764,26 @@ do_execsql_test join8-24000 { SELECT * FROM t4 JOIN t7 ON true RIGHT JOIN (SELECT y AS z FROM t7) AS t6 ON (x=z); } {- - - - 200} +# 2022-06-20 +# forum/forumpost/6650cd40b5634f35 +# +reset_db +do_execsql_test join8-25000 { + CREATE TABLE t1(a1 INT); + CREATE TABLE t2(b2 INT); + CREATE TABLE t3(c3 INT, d3 INT UNIQUE); + CREATE TABLE t4(e4 INT, f4 TEXT); + INSERT INTO t3(c3, d3) VALUES (2, 1); + INSERT INTO t4(f4) VALUES ('x'); + CREATE INDEX i0 ON t3(c3) WHERE d3 ISNULL; + ANALYZE main; +} +db null - +do_execsql_test join8-25010 { + SELECT * FROM t1 LEFT JOIN t2 ON true JOIN t3 ON (b2 IN (a1)) FULL JOIN t4 ON true; +} {- - - - - x} +do_execsql_test join8-25020 { + SELECT 1 FROM t1 LEFT JOIN t2 ON true JOIN t3 ON (b2 IN (a1)) FULL JOIN t4 ON true; +} {1} finish_test