From: drh <> Date: Fri, 15 Apr 2022 17:08:40 +0000 (+0000) Subject: The sqlite3ProcessJoin() routine converts a NATURAL JOIN into a JOIN USING so X-Git-Tag: version-3.39.0~206^2~49 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=22c4bc8991e45118d22a8bf818aad0191ee5979d;p=thirdparty%2Fsqlite.git The sqlite3ProcessJoin() routine converts a NATURAL JOIN into a JOIN USING so that henceforth the NATURAL keyword can be ignored. FossilOrigin-Name: 8378e1e0d289627fb294ccd3f5865ef49df3a42b8a5aa211e21be1b42d9da753 --- diff --git a/manifest b/manifest index c8c5ea8505..fc69bf60d0 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Enhance\sthe\sIdList\sobject\sto\sexist\sin\sa\ssingle\smemory\sallocation\s(rather\sthan\na\sseparate\sallocate\sfor\sthe\sbase\sobject\sand\sthe\sarray\sof\sIDs).\s\sAlso\spermit\nan\sIdList\sobject\sto\sstore\san\sExpr\spointer\stogether\swith\seach\sname. -D 2022-04-15T15:47:14.312 +C The\ssqlite3ProcessJoin()\sroutine\sconverts\sa\sNATURAL\sJOIN\sinto\sa\sJOIN\sUSING\sso\nthat\shenceforth\sthe\sNATURAL\skeyword\scan\sbe\signored. +D 2022-04-15T17:08:40.527 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -550,9 +550,9 @@ F src/pragma.h e690a356c18e98414d2e870ea791c1be1545a714ba623719deb63f7f226d8bb7 F src/prepare.c fd940149c691684e7c1073c3787a7170e44852b02d1275d2e30a5b58e89cfcaf F src/printf.c 05d8dfd2018bc4fc3ddb8b37eb97ccef7abf985643fa1caebdcf2916ca90fa32 F src/random.c 097dc8b31b8fba5a9aca1697aeb9fd82078ec91be734c16bffda620ced7ab83c -F src/resolve.c 7110fc3b5a4dec5d11559141c1906c4a125349fb602f541b05db3a3d448d4b95 +F src/resolve.c 2d0f29eadbc87154399b2794490a5bfd104daf818d76e59845ab5afb9bf66824 F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92 -F src/select.c b785c8d0dc9b00d3ddac164f57d586f893c259a5ccc8c18284fec06451109278 +F src/select.c 820941d6c857e2cfdb2e56565bacd2b31b4adacc468550d44f8f9089888405e2 F src/shell.c.in eb7f10d5e2c47bd014d92ec5db1def21fcc1ed56ffaaa4ee715b6c37c370b47f F src/sqlite.h.in 2a35f62185eb5e7ecc64a2f68442b538ce9be74f80f28a00abc24837edcf1c17 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 @@ -1947,8 +1947,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 b925f72b6f679c61b0d6be16fabe64dc7605550b7bd86f35c586dcecd8217673 -R b11d83f7cd569b4ace13c0dab33f163b +P 40f3c95871e6f40f287ef2756abafb8fc56dffdd0af69436e5c7d8e95022d94e +R a4dbe6f876199f457625a48f56e40a96 U drh -Z a062e1ee126eb5e46a77952af86658af +Z 67a6ae70c20850bccd3ed1b730f595c0 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 33815810f5..a6a2850870 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -40f3c95871e6f40f287ef2756abafb8fc56dffdd0af69436e5c7d8e95022d94e \ No newline at end of file +8378e1e0d289627fb294ccd3f5865ef49df3a42b8a5aa211e21be1b42d9da753 \ No newline at end of file diff --git a/src/resolve.c b/src/resolve.c index ac00564daa..ecc07b296e 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -344,7 +344,6 @@ static int lookupName( ** USING clause, then skip this match. */ if( cnt==1 ){ - if( pItem->fg.jointype & JT_NATURAL ) continue; if( pItem->fg.isUsing && nameInUsingClause(pItem->u3.pUsing, zCol) ){ diff --git a/src/select.c b/src/select.c index d7d289ef53..9c60e209e7 100644 --- a/src/select.c +++ b/src/select.c @@ -320,8 +320,11 @@ int sqlite3ColumnIndex(Table *pTab, const char *zCol){ } /* -** Search the first N tables in pSrc, from left to right, looking for a -** table that has a column named zCol. +** Search the first N tables in pSrc, looking for a +** table that has a column named zCol. +** +** Search left-to-right if bRightmost is false. Search right-to-left +** if bRightmost is true. ** ** When found, set *piTab and *piCol to the table index and column index ** of the matching column and return TRUE. @@ -334,11 +337,15 @@ static int tableAndColumnIndex( const char *zCol, /* Name of the column we are looking for */ int *piTab, /* Write index of pSrc->a[] here */ int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */ - int bIgnoreHidden /* True to ignore hidden columns */ + int bIgnoreHidden, /* True to ignore hidden columns */ + int bRightmost /* Return the right-most match */ ){ int i; /* For looping over tables in pSrc */ int iCol; /* Index of column matching zCol */ + int rc = 0; + assert( N<=pSrc->nSrc ); + assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */ for(i=0; ia[i].pTab, zCol); @@ -349,10 +356,11 @@ static int tableAndColumnIndex( *piTab = i; *piCol = iCol; } - return 1; + rc = 1; + if( !bRightmost ) break; } } - return 0; + return rc; } /* @@ -509,10 +517,11 @@ static int sqlite3ProcessJoin(Parse *pParse, Select *p){ if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue; joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_FromJoin : EP_InnerJoin; - /* When the NATURAL keyword is present, add WHERE clause terms for - ** every column that the two tables have in common. + /* If this is a NATURAL join, synthesize an approprate USING clause + ** to specify which columns should be joined. */ if( pRight->fg.jointype & JT_NATURAL ){ + IdList *pUsing = 0; if( pRight->fg.isUsing || pRight->u3.pOn ){ sqlite3ErrorMsg(pParse, "a NATURAL join may not have " "an ON or USING clause", 0); @@ -520,16 +529,20 @@ static int sqlite3ProcessJoin(Parse *pParse, Select *p){ } for(j=0; jnCol; j++){ char *zName; /* Name of column in the right table */ - int iLeft; /* Matching left table */ - int iLeftCol; /* Matching column in the left table */ if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue; zName = pRightTab->aCol[j].zCnName; - if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol, 1) ){ - addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j, - joinType, &p->pWhere); + if( tableAndColumnIndex(pSrc, i+1, zName, 0, 0, 1, 0) ){ + Token x; + x.z = zName; + x.n = sqlite3Strlen30(zName); + pUsing = sqlite3IdListAppend(pParse, pUsing, &x); } } + if( pUsing ){ + pRight->fg.isUsing = 1; + pRight->u3.pUsing = pUsing; + } } /* Create extra terms on the WHERE clause for each column named @@ -541,6 +554,7 @@ static int sqlite3ProcessJoin(Parse *pParse, Select *p){ */ if( pRight->fg.isUsing ){ IdList *pList = pRight->u3.pUsing; + int bRight = (pRight->fg.jointype & JT_RIGHT)!=0; assert( pList!=0 ); for(j=0; jnId; j++){ char *zName; /* Name of the term in the USING clause */ @@ -551,7 +565,7 @@ static int sqlite3ProcessJoin(Parse *pParse, Select *p){ zName = pList->a[j].zName; iRightCol = sqlite3ColumnIndex(pRightTab, zName); if( iRightCol<0 - || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol, 0) + || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol,0,bRight) ){ sqlite3ErrorMsg(pParse, "cannot join using column %s - column " "not present in both tables", zName); @@ -5772,13 +5786,6 @@ static int selectExpander(Walker *pWalker, Select *p){ tableSeen = 1; if( i>0 && zTName==0 ){ - if( (pFrom->fg.jointype & JT_NATURAL)!=0 - && tableAndColumnIndex(pTabList, i, zName, 0, 0, 1) - ){ - /* In a NATURAL join, omit the join columns from the - ** table to the right of the join */ - continue; - } if( pFrom->fg.isUsing && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0 ){