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:
12
manifest
12
manifest
@@ -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.
|
||||
D 2021-02-20T21:20:54.553
|
||||
C Simplified\sresolution\sof\sCTE\snames.\s\sSlightly\sfaster\sand\sabout\s100\sbytes\nof\scode\ssmaller.
|
||||
D 2021-02-21T01:19:42.925
|
||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
|
||||
@@ -542,7 +542,7 @@ F src/printf.c 30e92b638fac71dcd85cdea1d12ecfae354c9adee2c71e8e1ae4727cde7c91ed
|
||||
F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
|
||||
F src/resolve.c 52f81603cc40f78449f5b6aed96dbea9484b194771ecb1937e8c0f6547c186a0
|
||||
F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
|
||||
F src/select.c fc2ed90b5e99399ad5cd040d1ccc969127c332275bbb82726b66e5eadf4cb4c0
|
||||
F src/select.c 94031cbbcae233326b90ce6a598494987ec4838a88c08ce57ffdcdab465dc3ca
|
||||
F src/shell.c.in 844417f84df1f6c4fce1c815629a888cfdcf219e86513e9c332bbcc38832f477
|
||||
F src/sqlite.h.in 8855a19f37ade8dad189a9e48233a2ebe1b46faf469c7eb0906a654e252dcc57
|
||||
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
|
||||
@@ -1905,7 +1905,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 68bb541a39833d7d4bf41aa91cb6cd9c98757d1fc8236299d09db7e9b14d8ec9
|
||||
R 5e60a181334dd40bb8d4205e0cb8c33a
|
||||
P 554b286ac208168bde91c6c6034cc7087410def76fce650b519661b2ee2c61b7
|
||||
R e20e20e562861619a6969efc4756acf8
|
||||
U drh
|
||||
Z 063342d2568b33a8aa12ee70783f45a5
|
||||
Z 4bcb1bbda8b09f531893d946c969ef3a
|
||||
|
||||
@@ -1 +1 @@
|
||||
554b286ac208168bde91c6c6034cc7087410def76fce650b519661b2ee2c61b7
|
||||
0d2c992f3622a6272ca4a3caff6b21f619fe976b8df8b34eff066086f8df2202
|
||||
92
src/select.c
92
src/select.c
@@ -4840,16 +4840,16 @@ static struct Cte *searchWith(
|
||||
struct SrcList_item *pItem, /* FROM clause element to resolve */
|
||||
With **ppContext /* OUT: WITH clause return value belongs to */
|
||||
){
|
||||
const char *zName;
|
||||
if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
|
||||
With *p;
|
||||
for(p=pWith; p; p=p->pOuter){
|
||||
int i;
|
||||
for(i=0; i<p->nCte; i++){
|
||||
if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
|
||||
*ppContext = p;
|
||||
return &p->a[i];
|
||||
}
|
||||
const char *zName = pItem->zName;
|
||||
With *p;
|
||||
assert( pItem->zDatabase==0 );
|
||||
assert( zName!=0 );
|
||||
for(p=pWith; p; p=p->pOuter){
|
||||
int i;
|
||||
for(i=0; i<p->nCte; i++){
|
||||
if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
|
||||
*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
|
||||
** a WITH clause on the stack currently maintained by the parser. And,
|
||||
** if currently processing a CTE expression, if it is a recursive
|
||||
** reference to the current CTE.
|
||||
** a WITH clause on the stack currently maintained by the parser (on the
|
||||
** pParse->pWith linked list). And if currently processing a 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
|
||||
** and other fields are populated accordingly. The caller should check
|
||||
** (pFrom->pTab!=0) to determine whether or not a successful match
|
||||
** was found.
|
||||
** If pFrom matches a CTE according to either of these two above, pFrom->pTab
|
||||
** and other fields are populated accordingly.
|
||||
**
|
||||
** Whether or not a match is found, SQLITE_OK is returned if no error
|
||||
** occurs. If an error does occur, an error message is stored in the
|
||||
** parser and some error code other than SQLITE_OK returned.
|
||||
** Return 0 if no match is found.
|
||||
** Return 1 if a match is found.
|
||||
** Return 2 if an error condition is detected.
|
||||
*/
|
||||
static int withExpand(
|
||||
Walker *pWalker,
|
||||
struct SrcList_item *pFrom
|
||||
static int resolveFromTermToCte(
|
||||
Parse *pParse, /* The parsing context */
|
||||
Walker *pWalker, /* Current tree walker */
|
||||
struct SrcList_item *pFrom /* The FROM clause term to check */
|
||||
){
|
||||
Parse *pParse = pWalker->pParse;
|
||||
sqlite3 *db = pParse->db;
|
||||
struct Cte *pCte; /* Matched CTE (or NULL if no match) */
|
||||
With *pWith; /* WITH clause that pCte belongs to */
|
||||
Cte *pCte; /* Matched CTE (or NULL if no match) */
|
||||
With *pWith; /* The matching WITH */
|
||||
|
||||
assert( pFrom->pTab==0 );
|
||||
if( pParse->nErr ){
|
||||
return SQLITE_ERROR;
|
||||
if( pParse->pWith==0 ){
|
||||
/* 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);
|
||||
if( pCte ){
|
||||
sqlite3 *db = pParse->db;
|
||||
Table *pTab;
|
||||
ExprList *pEList;
|
||||
Select *pSel;
|
||||
@@ -4926,20 +4930,20 @@ static int withExpand(
|
||||
** In this case, proceed. */
|
||||
if( pCte->zCteErr ){
|
||||
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 );
|
||||
pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
|
||||
if( pTab==0 ) return WRC_Abort;
|
||||
if( pTab==0 ) return 2;
|
||||
pTab->nTabRef = 1;
|
||||
pTab->zName = sqlite3DbStrDup(db, pCte->zName);
|
||||
pTab->iPKey = -1;
|
||||
pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
|
||||
pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
|
||||
pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
|
||||
if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
|
||||
if( db->mallocFailed ) return 2;
|
||||
assert( pFrom->pSelect );
|
||||
|
||||
/* Check if this is a recursive CTE. */
|
||||
@@ -4962,7 +4966,7 @@ static int withExpand(
|
||||
sqlite3ErrorMsg(pParse,
|
||||
"multiple references to recursive table: %s", pCte->zName
|
||||
);
|
||||
return SQLITE_ERROR;
|
||||
return 2;
|
||||
}
|
||||
pRecTerm->selFlags |= SF_Recursive;
|
||||
if( iRecTab<0 ) iRecTab = pParse->nTab++;
|
||||
@@ -4998,7 +5002,7 @@ static int withExpand(
|
||||
pCte->zName, pEList->nExpr, pCte->pCols->nExpr
|
||||
);
|
||||
pParse->pWith = pSavedWith;
|
||||
return SQLITE_ERROR;
|
||||
return 2;
|
||||
}
|
||||
pEList = pCte->pCols;
|
||||
}
|
||||
@@ -5014,9 +5018,9 @@ static int withExpand(
|
||||
}
|
||||
pCte->zCteErr = 0;
|
||||
pParse->pWith = pSavedWith;
|
||||
return 1; /* Success */
|
||||
}
|
||||
|
||||
return SQLITE_OK;
|
||||
return 0; /* No match */
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -5098,7 +5102,7 @@ int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
|
||||
*/
|
||||
static int selectExpander(Walker *pWalker, Select *p){
|
||||
Parse *pParse = pWalker->pParse;
|
||||
int i, j, k;
|
||||
int i, j, k, rc;
|
||||
SrcList *pTabList;
|
||||
ExprList *pEList;
|
||||
struct SrcList_item *pFrom;
|
||||
@@ -5137,10 +5141,6 @@ static int selectExpander(Walker *pWalker, Select *p){
|
||||
assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
|
||||
if( pFrom->pTab ) continue;
|
||||
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 ){
|
||||
#ifndef SQLITE_OMIT_SUBQUERY
|
||||
Select *pSel = pFrom->pSelect;
|
||||
@@ -5149,6 +5149,12 @@ static int selectExpander(Walker *pWalker, Select *p){
|
||||
assert( pFrom->pTab==0 );
|
||||
if( sqlite3WalkSelect(pWalker, pSel) ) 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
|
||||
}else{
|
||||
/* An ordinary table or view name in the FROM clause */
|
||||
|
||||
Reference in New Issue
Block a user