1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-11 01:42:22 +03:00

Simplified resolution of CTE names. Slightly faster and about 100 bytes

of code smaller.

FossilOrigin-Name: 0d2c992f3622a6272ca4a3caff6b21f619fe976b8df8b34eff066086f8df2202
This commit is contained in:
drh
2021-02-21 01:19:42 +00:00
parent 1862271f7c
commit 5abe1d3d53
3 changed files with 56 additions and 50 deletions

View File

@@ -1,5 +1,5 @@
C Performance\simprovement\sin\sresolving\sthe\sINDEXED\sBY\sindex\sname\sfor\sthe\scommon\ncase\swhere\sthere\sis\sno\sINDEXED\sBY\sclause. C Simplified\sresolution\sof\sCTE\snames.\s\sSlightly\sfaster\sand\sabout\s100\sbytes\nof\scode\ssmaller.
D 2021-02-20T21:20:54.553 D 2021-02-21T01:19:42.925
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -542,7 +542,7 @@ F src/printf.c 30e92b638fac71dcd85cdea1d12ecfae354c9adee2c71e8e1ae4727cde7c91ed
F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
F src/resolve.c 52f81603cc40f78449f5b6aed96dbea9484b194771ecb1937e8c0f6547c186a0 F src/resolve.c 52f81603cc40f78449f5b6aed96dbea9484b194771ecb1937e8c0f6547c186a0
F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92 F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
F src/select.c fc2ed90b5e99399ad5cd040d1ccc969127c332275bbb82726b66e5eadf4cb4c0 F src/select.c 94031cbbcae233326b90ce6a598494987ec4838a88c08ce57ffdcdab465dc3ca
F src/shell.c.in 844417f84df1f6c4fce1c815629a888cfdcf219e86513e9c332bbcc38832f477 F src/shell.c.in 844417f84df1f6c4fce1c815629a888cfdcf219e86513e9c332bbcc38832f477
F src/sqlite.h.in 8855a19f37ade8dad189a9e48233a2ebe1b46faf469c7eb0906a654e252dcc57 F src/sqlite.h.in 8855a19f37ade8dad189a9e48233a2ebe1b46faf469c7eb0906a654e252dcc57
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
@@ -1905,7 +1905,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 68bb541a39833d7d4bf41aa91cb6cd9c98757d1fc8236299d09db7e9b14d8ec9 P 554b286ac208168bde91c6c6034cc7087410def76fce650b519661b2ee2c61b7
R 5e60a181334dd40bb8d4205e0cb8c33a R e20e20e562861619a6969efc4756acf8
U drh U drh
Z 063342d2568b33a8aa12ee70783f45a5 Z 4bcb1bbda8b09f531893d946c969ef3a

View File

@@ -1 +1 @@
554b286ac208168bde91c6c6034cc7087410def76fce650b519661b2ee2c61b7 0d2c992f3622a6272ca4a3caff6b21f619fe976b8df8b34eff066086f8df2202

View File

@@ -4840,16 +4840,16 @@ static struct Cte *searchWith(
struct SrcList_item *pItem, /* FROM clause element to resolve */ struct SrcList_item *pItem, /* FROM clause element to resolve */
With **ppContext /* OUT: WITH clause return value belongs to */ With **ppContext /* OUT: WITH clause return value belongs to */
){ ){
const char *zName; const char *zName = pItem->zName;
if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){ With *p;
With *p; assert( pItem->zDatabase==0 );
for(p=pWith; p; p=p->pOuter){ assert( zName!=0 );
int i; for(p=pWith; p; p=p->pOuter){
for(i=0; i<p->nCte; i++){ int i;
if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){ for(i=0; i<p->nCte; i++){
*ppContext = p; if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
return &p->a[i]; *ppContext = p;
} return &p->a[i];
} }
} }
} }
@@ -4882,35 +4882,39 @@ void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
/* /*
** This function checks if argument pFrom refers to a CTE declared by ** This function checks if argument pFrom refers to a CTE declared by
** a WITH clause on the stack currently maintained by the parser. And, ** a WITH clause on the stack currently maintained by the parser (on the
** if currently processing a CTE expression, if it is a recursive ** pParse->pWith linked list). And if currently processing a CTE
** reference to the current CTE. ** CTE expression, through routine checks to see if the reference is
** a recursive reference to the CTE.
** **
** If pFrom falls into either of the two categories above, pFrom->pTab ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
** and other fields are populated accordingly. The caller should check ** and other fields are populated accordingly.
** (pFrom->pTab!=0) to determine whether or not a successful match
** was found.
** **
** Whether or not a match is found, SQLITE_OK is returned if no error ** Return 0 if no match is found.
** occurs. If an error does occur, an error message is stored in the ** Return 1 if a match is found.
** parser and some error code other than SQLITE_OK returned. ** Return 2 if an error condition is detected.
*/ */
static int withExpand( static int resolveFromTermToCte(
Walker *pWalker, Parse *pParse, /* The parsing context */
struct SrcList_item *pFrom Walker *pWalker, /* Current tree walker */
struct SrcList_item *pFrom /* The FROM clause term to check */
){ ){
Parse *pParse = pWalker->pParse; Cte *pCte; /* Matched CTE (or NULL if no match) */
sqlite3 *db = pParse->db; With *pWith; /* The matching WITH */
struct Cte *pCte; /* Matched CTE (or NULL if no match) */
With *pWith; /* WITH clause that pCte belongs to */
assert( pFrom->pTab==0 ); assert( pFrom->pTab==0 );
if( pParse->nErr ){ if( pParse->pWith==0 ){
return SQLITE_ERROR; /* There are no WITH clauses in the stack. No match is possible */
return 0;
}
if( pFrom->zDatabase!=0 ){
/* The FROM term contains a schema qualifier (ex: main.t1) and so
** it cannot possibly be a CTE reference. */
return 0;
} }
pCte = searchWith(pParse->pWith, pFrom, &pWith); pCte = searchWith(pParse->pWith, pFrom, &pWith);
if( pCte ){ if( pCte ){
sqlite3 *db = pParse->db;
Table *pTab; Table *pTab;
ExprList *pEList; ExprList *pEList;
Select *pSel; Select *pSel;
@@ -4926,20 +4930,20 @@ static int withExpand(
** In this case, proceed. */ ** In this case, proceed. */
if( pCte->zCteErr ){ if( pCte->zCteErr ){
sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName); sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
return SQLITE_ERROR; return 2;
} }
if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR; if( cannotBeFunction(pParse, pFrom) ) return 2;
assert( pFrom->pTab==0 ); assert( pFrom->pTab==0 );
pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
if( pTab==0 ) return WRC_Abort; if( pTab==0 ) return 2;
pTab->nTabRef = 1; pTab->nTabRef = 1;
pTab->zName = sqlite3DbStrDup(db, pCte->zName); pTab->zName = sqlite3DbStrDup(db, pCte->zName);
pTab->iPKey = -1; pTab->iPKey = -1;
pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid; pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0); pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
if( db->mallocFailed ) return SQLITE_NOMEM_BKPT; if( db->mallocFailed ) return 2;
assert( pFrom->pSelect ); assert( pFrom->pSelect );
/* Check if this is a recursive CTE. */ /* Check if this is a recursive CTE. */
@@ -4962,7 +4966,7 @@ static int withExpand(
sqlite3ErrorMsg(pParse, sqlite3ErrorMsg(pParse,
"multiple references to recursive table: %s", pCte->zName "multiple references to recursive table: %s", pCte->zName
); );
return SQLITE_ERROR; return 2;
} }
pRecTerm->selFlags |= SF_Recursive; pRecTerm->selFlags |= SF_Recursive;
if( iRecTab<0 ) iRecTab = pParse->nTab++; if( iRecTab<0 ) iRecTab = pParse->nTab++;
@@ -4998,7 +5002,7 @@ static int withExpand(
pCte->zName, pEList->nExpr, pCte->pCols->nExpr pCte->zName, pEList->nExpr, pCte->pCols->nExpr
); );
pParse->pWith = pSavedWith; pParse->pWith = pSavedWith;
return SQLITE_ERROR; return 2;
} }
pEList = pCte->pCols; pEList = pCte->pCols;
} }
@@ -5014,9 +5018,9 @@ static int withExpand(
} }
pCte->zCteErr = 0; pCte->zCteErr = 0;
pParse->pWith = pSavedWith; pParse->pWith = pSavedWith;
return 1; /* Success */
} }
return 0; /* No match */
return SQLITE_OK;
} }
#endif #endif
@@ -5098,7 +5102,7 @@ int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
*/ */
static int selectExpander(Walker *pWalker, Select *p){ static int selectExpander(Walker *pWalker, Select *p){
Parse *pParse = pWalker->pParse; Parse *pParse = pWalker->pParse;
int i, j, k; int i, j, k, rc;
SrcList *pTabList; SrcList *pTabList;
ExprList *pEList; ExprList *pEList;
struct SrcList_item *pFrom; struct SrcList_item *pFrom;
@@ -5137,10 +5141,6 @@ static int selectExpander(Walker *pWalker, Select *p){
assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 ); assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
if( pFrom->pTab ) continue; if( pFrom->pTab ) continue;
assert( pFrom->fg.isRecursive==0 ); assert( pFrom->fg.isRecursive==0 );
#ifndef SQLITE_OMIT_CTE
if( withExpand(pWalker, pFrom) ) return WRC_Abort;
if( pFrom->pTab ) {} else
#endif
if( pFrom->zName==0 ){ if( pFrom->zName==0 ){
#ifndef SQLITE_OMIT_SUBQUERY #ifndef SQLITE_OMIT_SUBQUERY
Select *pSel = pFrom->pSelect; Select *pSel = pFrom->pSelect;
@@ -5149,6 +5149,12 @@ static int selectExpander(Walker *pWalker, Select *p){
assert( pFrom->pTab==0 ); assert( pFrom->pTab==0 );
if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort; if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort; if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
#endif
#ifndef SQLITE_OMIT_CTE
}else if( (rc = resolveFromTermToCte(pParse, pWalker, pFrom))!=0 ){
if( rc>1 ) return WRC_Abort;
pTab = pFrom->pTab;
assert( pTab!=0 );
#endif #endif
}else{ }else{
/* An ordinary table or view name in the FROM clause */ /* An ordinary table or view name in the FROM clause */