Better resolution of table and column names in joins where some of the

terms of the FROM clause are parenthesized.

FossilOrigin-Name: 7344e791b9456286ecdca6d45f2f5260fb3f10e2
This commit is contained in:
drh
2012-12-19 02:36:45 +00:00
parent 832ee3d4cc
commit 8f25d18b21
4 changed files with 57 additions and 29 deletions
+10 -7
View File
@@ -1,5 +1,5 @@
C Optimize\sthe\sdegenerate\scase\sof\sa\sFROM\sclause\stable\sname\senclosed\sall\sby\nitself\sinside\sparentheses.\s\sGenerate\scode\sas\sif\sthe\sparentheses\sdid\snot\nexist,\srather\sthan\sthe\sold\sbehavior\sof\smanifesting\sthe\sparenthesized\stable\ninto\sa\stransient\stable.\s\sAlso,\stag\severy\sFROM-clause\sSELECT\ssubquery\sthat\sis\ngenerated\sby\sa\sparenthesized\sFROM-clause\sexpression\susing\sthe\sSF_NestedFrom\nflag.\s\sThe\snew\sSF_NestedFrom\sflag\sis\snot\syet\sused\sfor\sanything.
D 2012-12-18T19:36:11.944
C Better\sresolution\sof\stable\sand\scolumn\snames\sin\sjoins\swhere\ssome\sof\sthe\nterms\sof\sthe\sFROM\sclause\sare\sparenthesized.
D 2012-12-19T02:36:45.854
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 690d441a758cbffd13e814dc2724a721a6ebd400
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -172,9 +172,9 @@ F src/pragma.c 015723c48072781d2740e310ab04dc92956b76d1
F src/prepare.c 931ad0d852a0df48f79adcba6ce79ca5f475625c
F src/printf.c 4a9f882f1c1787a8b494a2987765acf9d97ac21f
F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
F src/resolve.c 52331299f4095397d6d00715b70cd153baa11931
F src/resolve.c 3104a5e4c2cb6e3a813b7266cbd541dc91e26fad
F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0
F src/select.c 5eab6941c0ac97355817f846b77cd20bfdf5a82e
F src/select.c 3cce5b09cae6eaf23276bb2e5551a9d22cd7ae3d
F src/shell.c e392dd1ccbb77cc1d75a8367a89b473c24bea019
F src/sqlite.h.in 39cc33bb08897c748fe3383c29ccf56585704177
F src/sqlite3.rc fea433eb0a59f4c9393c8e6d76a6e2596b1fe0c0
@@ -1026,7 +1026,10 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
P 9b67c633d932f3e566f521ee6a9cf3be193436fa
R d9a19320be87db9daf2926b8e71a7df8
P 7fecced466d86a66b0b751c5b5608141e134fe2d
R edfa9398c5c900d8f6d5f20d3c0d802e
T *branch * name-resolution-fix
T *sym-name-resolution-fix *
T -sym-trunk *
U drh
Z bc761a01222fad0c80ad013442fb2ec8
Z 3b6c2dbf5da1f286c0c463d165d85b47
+1 -1
View File
@@ -1 +1 @@
7fecced466d86a66b0b751c5b5608141e134fe2d
7344e791b9456286ecdca6d45f2f5260fb3f10e2
+41 -20
View File
@@ -206,6 +206,20 @@ static int lookupName(
pExpr->pTab = 0;
ExprSetIrreducible(pExpr);
/* Translate the schema name in zDb into a pointer to the corresponding
** schema. If not found, pSchema will remain NULL and nothing will match
** resulting in an appropriate error message toward the end of this routine
*/
if( zDb ){
for(i=0; i<db->nDb; i++){
assert( db->aDb[i].zName );
if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
pSchema = db->aDb[i].pSchema;
break;
}
}
}
/* Start at the inner-most context and move outward until a match is found */
while( pNC && cnt==0 ){
ExprList *pEList;
@@ -214,31 +228,36 @@ static int lookupName(
if( pSrcList ){
for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
Table *pTab;
int iDb;
Column *pCol;
pTab = pItem->pTab;
assert( pTab!=0 && pTab->zName!=0 );
iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
assert( pTab->nCol>0 );
if( zDb && pTab->pSchema!=pSchema ){
continue;
}
if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
ExprList *pEList = pItem->pSelect->pEList;
int hit = 0;
for(j=0; j<pEList->nExpr; j++){
if( zTab && sqlite3StrICmp(pEList->a[j].zSpan, zTab)!=0 ) continue;
if( sqlite3StrICmp(pEList->a[j].zName, zCol)==0 ){
cnt++;
cntTab = 2;
pMatch = pItem;
pExpr->iColumn = j;
}
}
if( hit || zTab==0 ) continue;
}
if( zTab ){
if( pItem->zAlias ){
char *zTabName = pItem->zAlias;
if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
}else{
char *zTabName = pTab->zName;
if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
continue;
}
if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
continue;
}
const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
assert( zTabName!=0 );
if( sqlite3StrICmp(zTabName, zTab)!=0 ){
continue;
}
}
if( 0==(cntTab++) ){
pExpr->iTable = pItem->iCursor;
pExpr->pTab = pTab;
pSchema = pTab->pSchema;
pMatch = pItem;
}
for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
@@ -252,17 +271,19 @@ static int lookupName(
if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
}
cnt++;
pExpr->iTable = pItem->iCursor;
pExpr->pTab = pTab;
pMatch = pItem;
pSchema = pTab->pSchema;
/* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
break;
}
}
}
}
if( pMatch ){
pExpr->iTable = pMatch->iCursor;
pExpr->pTab = pMatch->pTab;
pSchema = pExpr->pTab->pSchema;
}
} /* if( pSrcList ) */
#ifndef SQLITE_OMIT_TRIGGER
/* If we have not already resolved the name, then maybe
+5 -1
View File
@@ -3392,7 +3392,8 @@ static int selectExpander(Walker *pWalker, Select *p){
ExprList *pNew = 0;
int flags = pParse->db->flags;
int longNames = (flags & SQLITE_FullColNames)!=0
&& (flags & SQLITE_ShortColNames)==0;
&& (flags & SQLITE_ShortColNames)==0
&& (p->selFlags & SF_NestedFrom)==0;
for(k=0; k<pEList->nExpr; k++){
Expr *pE = a[k].pExpr;
@@ -3479,6 +3480,9 @@ static int selectExpander(Walker *pWalker, Select *p){
sColname.z = zColname;
sColname.n = sqlite3Strlen30(zColname);
sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
pNew->a[pNew->nExpr-1].zSpan = sqlite3DbStrDup(db, zTabName);
}
sqlite3DbFree(db, zToFree);
}
}