/ Check-in [afe96a11]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Remove an incorrect assert() statement (ticket [beba9cae6345a]). Fix other minor problems in the name resolution logic.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | name-resolution-fix
Files: files | file ages | folders
SHA1: afe96a118c8a9627819ba5960aa83a607e734087
User & Date: drh 2013-01-03 16:54:20
Context
2013-01-03
17:34
Further corner-case fixes to the name resolution logic. Closed-Leaf check-in: 20730bad user: drh tags: name-resolution-fix
16:54
Remove an incorrect assert() statement (ticket [beba9cae6345a]). Fix other minor problems in the name resolution logic. check-in: afe96a11 user: drh tags: name-resolution-fix
00:45
Now supports result sets of the form "TABLE.*" with nested FROM clauses. check-in: 4cf5ed7e user: drh tags: name-resolution-fix
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/resolve.c.

   258    258         for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
   259    259           Table *pTab;
   260    260           Column *pCol;
   261    261     
   262    262           pTab = pItem->pTab;
   263    263           assert( pTab!=0 && pTab->zName!=0 );
   264    264           assert( pTab->nCol>0 );
   265         -        if( zDb && pTab->pSchema!=pSchema ){
   266         -          continue;
   267         -        }
   268    265           if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
   269    266             ExprList *pEList = pItem->pSelect->pEList;
   270    267             int hit = 0;
   271    268             for(j=0; j<pEList->nExpr; j++){
   272    269               if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
   273    270                 cnt++;
   274    271                 cntTab = 2;
   275    272                 pMatch = pItem;
   276    273                 pExpr->iColumn = j;
   277    274               }
   278    275             }
   279    276             if( hit || zTab==0 ) continue;
          277  +        }
          278  +        if( zDb && pTab->pSchema!=pSchema ){
          279  +          continue;
   280    280           }
   281    281           if( zTab ){
   282    282             const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
   283    283             assert( zTabName!=0 );
   284    284             if( sqlite3StrICmp(zTabName, zTab)!=0 ){
   285    285               continue;
   286    286             }

Changes to src/select.c.

  1332   1332     *pnCol = nCol;
  1333   1333     *paCol = aCol;
  1334   1334   
  1335   1335     for(i=0, pCol=aCol; i<nCol; i++, pCol++){
  1336   1336       /* Get an appropriate name for the column
  1337   1337       */
  1338   1338       p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
  1339         -    assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
  1340         -               || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
  1341   1339       if( (zName = pEList->a[i].zName)!=0 ){
  1342   1340         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1343   1341         zName = sqlite3DbStrDup(db, zName);
  1344   1342       }else{
  1345   1343         Expr *pColExpr = p;  /* The expression that is the result column name */
  1346   1344         Table *pTab;         /* Table associated with this expression */
  1347   1345         while( pColExpr->op==TK_DOT ){
................................................................................
  3425   3423             zTName = pE->pLeft->u.zToken;
  3426   3424           }
  3427   3425           for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  3428   3426             Table *pTab = pFrom->pTab;
  3429   3427             Select *pSub = pFrom->pSelect;
  3430   3428             char *zTabName = pFrom->zAlias;
  3431   3429             const char *zSchemaName = 0;
         3430  +          int iDb;
  3432   3431             if( zTabName==0 ){
  3433   3432               zTabName = pTab->zName;
  3434   3433             }
  3435   3434             if( db->mallocFailed ) break;
  3436   3435             if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
  3437         -            int iDb;
  3438   3436               pSub = 0;
  3439   3437               if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
  3440   3438                 continue;
  3441   3439               }
  3442   3440               iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  3443         -            zSchemaName = iDb>=0 ? db->aDb[i].zName : "*";
         3441  +            zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*";
  3444   3442             }
  3445   3443             for(j=0; j<pTab->nCol; j++){
  3446   3444               char *zName = pTab->aCol[j].zName;
  3447   3445               char *zColname;  /* The computed column name */
  3448   3446               char *zToFree;   /* Malloced string that needs to be freed */
  3449   3447               Token sColname;  /* Computed column name as a token */
  3450   3448   
         3449  +            assert( zName );
  3451   3450               if( zTName && pSub
  3452   3451                && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
  3453   3452               ){
  3454   3453                 continue;
  3455   3454               }
  3456   3455   
  3457   3456               /* If a column is marked as 'hidden' (currently only possible
................................................................................
  3481   3480               pRight = sqlite3Expr(db, TK_ID, zName);
  3482   3481               zColname = zName;
  3483   3482               zToFree = 0;
  3484   3483               if( longNames || pTabList->nSrc>1 ){
  3485   3484                 Expr *pLeft;
  3486   3485                 pLeft = sqlite3Expr(db, TK_ID, zTabName);
  3487   3486                 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
         3487  +              if( zSchemaName && iDb>0 ){
         3488  +                pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
         3489  +                pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0);
         3490  +              }
  3488   3491                 if( longNames ){
  3489   3492                   zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
  3490   3493                   zToFree = zColname;
  3491   3494                 }
  3492   3495               }else{
  3493   3496                 pExpr = pRight;
  3494   3497               }
................................................................................
  3496   3499               sColname.z = zColname;
  3497   3500               sColname.n = sqlite3Strlen30(zColname);
  3498   3501               sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
  3499   3502               if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
  3500   3503                 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
  3501   3504                 if( pSub ){
  3502   3505                   pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
         3506  +                testcase( pX->zSpan==0 );
  3503   3507                 }else{
  3504   3508                   pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
  3505   3509                                              zSchemaName, zTabName, zColname);
         3510  +                testcase( pX->zSpan==0 );
  3506   3511                 }
  3507   3512                 pX->bSpanIsTab = 1;
  3508   3513               }
  3509   3514               sqlite3DbFree(db, zToFree);
  3510   3515             }
  3511   3516           }
  3512   3517           if( !tableSeen ){

Changes to test/selectD.test.

    21     21     forcedelete test$i.db
    22     22     sqlite3 db test$i.db
    23     23     if {$i==2} {
    24     24       optimization_control db query-flattener off
    25     25     }
    26     26     do_test selectD-$i.0 {
    27     27       db eval {
           28  +      ATTACH ':memory:' AS aux1;
    28     29         CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(111,'x1');
    29     30         CREATE TABLE t2(a,b); INSERT INTO t2 VALUES(222,'x2');
    30     31         CREATE TEMP TABLE t3(a,b); INSERT INTO t3 VALUES(333,'x3');
    31         -      CREATE TABLE t4(a,b); INSERT INTO t4 VALUES(444,'x4');
           32  +      CREATE TABLE main.t4(a,b); INSERT INTO main.t4 VALUES(444,'x4');
           33  +      CREATE TABLE aux1.t4(a,b); INSERT INTO aux1.t4 VALUES(555,'x5');
    32     34       }
    33     35     } {}
    34     36     do_test selectD-$i.1 {
    35     37       db eval {
    36     38         SELECT *
    37     39           FROM (t1), (t2), (t3), (t4)
    38     40          WHERE t4.a=t3.a+111 
................................................................................
    68     70       db eval {
    69     71         SELECT t3.*, t2.*
    70     72           FROM t1 JOIN (t2 JOIN (t3 JOIN t4 ON t4.a=t3.a+111)
    71     73                                 ON t3.a=t2.a+111)
    72     74                        ON t2.a=t1.a+111;
    73     75       }
    74     76     } {333 x3 222 x2}
           77  +  do_test selectD-$i.2.4 {
           78  +    db eval {
           79  +      SELECT *
           80  +        FROM t1 JOIN (t2 JOIN (main.t4 JOIN aux1.t4 ON aux1.t4.a=main.t4.a+111)
           81  +                              ON main.t4.a=t2.a+222)
           82  +                     ON t2.a=t1.a+111;
           83  +    }
           84  +  } {111 x1 222 x2 444 x4 555 x5}
           85  +  do_test selectD-$i.2.5 {
           86  +    db eval {
           87  +      SELECT *
           88  +        FROM t1 JOIN (t2 JOIN (main.t4 AS x JOIN aux1.t4 ON aux1.t4.a=x.a+111)
           89  +                              ON x.a=t2.a+222)
           90  +                     ON t2.a=t1.a+111;
           91  +    }
           92  +  } {111 x1 222 x2 444 x4 555 x5}
           93  +  do_test selectD-$i.2.6 {
           94  +    catchsql {
           95  +      SELECT *
           96  +        FROM t1 JOIN (t2 JOIN (main.t4 JOIN aux.t4 ON aux.t4.a=main.t4.a+111)
           97  +                              ON main.t4.a=t2.a+222)
           98  +                     ON t2.a=t1.a+111;
           99  +    }
          100  +  } {1 {no such table: aux.t4}}
          101  +  do_test selectD-$i.2.7 {
          102  +    db eval {
          103  +      SELECT x.a, y.b
          104  +        FROM t1 JOIN (t2 JOIN (main.t4 x JOIN aux1.t4 y ON y.a=x.a+111)
          105  +                              ON x.a=t2.a+222)
          106  +                     ON t2.a=t1.a+111;
          107  +    }
          108  +  } {444 x5}
    75    109     do_test selectD-$i.3 {
    76    110       db eval {
    77    111         UPDATE t2 SET a=111;
    78    112         UPDATE t3 SET a=111;
    79    113         UPDATE t4 SET a=111;
    80    114         SELECT *
    81    115           FROM t1 JOIN (t2 JOIN (t3 JOIN t4 USING(a)) USING (a)) USING (a);