000001  /*
000002  ** 2008 August 18
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  **
000013  ** This file contains routines used for walking the parser tree and
000014  ** resolve all identifiers by associating them with a particular
000015  ** table and column.
000016  */
000017  #include "sqliteInt.h"
000018  #include <stdlib.h>
000019  #include <string.h>
000020  
000021  /*
000022  ** Walk the expression tree pExpr and increase the aggregate function
000023  ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
000024  ** This needs to occur when copying a TK_AGG_FUNCTION node from an
000025  ** outer query into an inner subquery.
000026  **
000027  ** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
000028  ** is a helper function - a callback for the tree walker.
000029  */
000030  static int incrAggDepth(Walker *pWalker, Expr *pExpr){
000031    if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
000032    return WRC_Continue;
000033  }
000034  static void incrAggFunctionDepth(Expr *pExpr, int N){
000035    if( N>0 ){
000036      Walker w;
000037      memset(&w, 0, sizeof(w));
000038      w.xExprCallback = incrAggDepth;
000039      w.u.n = N;
000040      sqlite3WalkExpr(&w, pExpr);
000041    }
000042  }
000043  
000044  /*
000045  ** Turn the pExpr expression into an alias for the iCol-th column of the
000046  ** result set in pEList.
000047  **
000048  ** If the reference is followed by a COLLATE operator, then make sure
000049  ** the COLLATE operator is preserved.  For example:
000050  **
000051  **     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
000052  **
000053  ** Should be transformed into:
000054  **
000055  **     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
000056  **
000057  ** The nSubquery parameter specifies how many levels of subquery the
000058  ** alias is removed from the original expression.  The usual value is
000059  ** zero but it might be more if the alias is contained within a subquery
000060  ** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
000061  ** structures must be increased by the nSubquery amount.
000062  */
000063  static void resolveAlias(
000064    Parse *pParse,         /* Parsing context */
000065    ExprList *pEList,      /* A result set */
000066    int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
000067    Expr *pExpr,           /* Transform this into an alias to the result set */
000068    const char *zType,     /* "GROUP" or "ORDER" or "" */
000069    int nSubquery          /* Number of subqueries that the label is moving */
000070  ){
000071    Expr *pOrig;           /* The iCol-th column of the result set */
000072    Expr *pDup;            /* Copy of pOrig */
000073    sqlite3 *db;           /* The database connection */
000074  
000075    assert( iCol>=0 && iCol<pEList->nExpr );
000076    pOrig = pEList->a[iCol].pExpr;
000077    assert( pOrig!=0 );
000078    db = pParse->db;
000079    pDup = sqlite3ExprDup(db, pOrig, 0);
000080    if( pDup==0 ) return;
000081    if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
000082    if( pExpr->op==TK_COLLATE ){
000083      pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
000084    }
000085    ExprSetProperty(pDup, EP_Alias);
000086  
000087    /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
000088    ** prevents ExprDelete() from deleting the Expr structure itself,
000089    ** allowing it to be repopulated by the memcpy() on the following line.
000090    ** The pExpr->u.zToken might point into memory that will be freed by the
000091    ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
000092    ** make a copy of the token before doing the sqlite3DbFree().
000093    */
000094    ExprSetProperty(pExpr, EP_Static);
000095    sqlite3ExprDelete(db, pExpr);
000096    memcpy(pExpr, pDup, sizeof(*pExpr));
000097    if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
000098      assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
000099      pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
000100      pExpr->flags |= EP_MemToken;
000101    }
000102    sqlite3DbFree(db, pDup);
000103  }
000104  
000105  
000106  /*
000107  ** Return TRUE if the name zCol occurs anywhere in the USING clause.
000108  **
000109  ** Return FALSE if the USING clause is NULL or if it does not contain
000110  ** zCol.
000111  */
000112  static int nameInUsingClause(IdList *pUsing, const char *zCol){
000113    if( pUsing ){
000114      int k;
000115      for(k=0; k<pUsing->nId; k++){
000116        if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
000117      }
000118    }
000119    return 0;
000120  }
000121  
000122  /*
000123  ** Subqueries stores the original database, table and column names for their
000124  ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
000125  ** Check to see if the zSpan given to this routine matches the zDb, zTab,
000126  ** and zCol.  If any of zDb, zTab, and zCol are NULL then those fields will
000127  ** match anything.
000128  */
000129  int sqlite3MatchSpanName(
000130    const char *zSpan,
000131    const char *zCol,
000132    const char *zTab,
000133    const char *zDb
000134  ){
000135    int n;
000136    for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
000137    if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
000138      return 0;
000139    }
000140    zSpan += n+1;
000141    for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
000142    if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
000143      return 0;
000144    }
000145    zSpan += n+1;
000146    if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
000147      return 0;
000148    }
000149    return 1;
000150  }
000151  
000152  /*
000153  ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
000154  ** that name in the set of source tables in pSrcList and make the pExpr 
000155  ** expression node refer back to that source column.  The following changes
000156  ** are made to pExpr:
000157  **
000158  **    pExpr->iDb           Set the index in db->aDb[] of the database X
000159  **                         (even if X is implied).
000160  **    pExpr->iTable        Set to the cursor number for the table obtained
000161  **                         from pSrcList.
000162  **    pExpr->pTab          Points to the Table structure of X.Y (even if
000163  **                         X and/or Y are implied.)
000164  **    pExpr->iColumn       Set to the column number within the table.
000165  **    pExpr->op            Set to TK_COLUMN.
000166  **    pExpr->pLeft         Any expression this points to is deleted
000167  **    pExpr->pRight        Any expression this points to is deleted.
000168  **
000169  ** The zDb variable is the name of the database (the "X").  This value may be
000170  ** NULL meaning that name is of the form Y.Z or Z.  Any available database
000171  ** can be used.  The zTable variable is the name of the table (the "Y").  This
000172  ** value can be NULL if zDb is also NULL.  If zTable is NULL it
000173  ** means that the form of the name is Z and that columns from any table
000174  ** can be used.
000175  **
000176  ** If the name cannot be resolved unambiguously, leave an error message
000177  ** in pParse and return WRC_Abort.  Return WRC_Prune on success.
000178  */
000179  static int lookupName(
000180    Parse *pParse,       /* The parsing context */
000181    const char *zDb,     /* Name of the database containing table, or NULL */
000182    const char *zTab,    /* Name of table containing column, or NULL */
000183    const char *zCol,    /* Name of the column. */
000184    NameContext *pNC,    /* The name context used to resolve the name */
000185    Expr *pExpr          /* Make this EXPR node point to the selected column */
000186  ){
000187    int i, j;                         /* Loop counters */
000188    int cnt = 0;                      /* Number of matching column names */
000189    int cntTab = 0;                   /* Number of matching table names */
000190    int nSubquery = 0;                /* How many levels of subquery */
000191    sqlite3 *db = pParse->db;         /* The database connection */
000192    struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
000193    struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
000194    NameContext *pTopNC = pNC;        /* First namecontext in the list */
000195    Schema *pSchema = 0;              /* Schema of the expression */
000196    int isTrigger = 0;                /* True if resolved to a trigger column */
000197    Table *pTab = 0;                  /* Table hold the row */
000198    Column *pCol;                     /* A column of pTab */
000199  
000200    assert( pNC );     /* the name context cannot be NULL. */
000201    assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
000202    assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
000203  
000204    /* Initialize the node to no-match */
000205    pExpr->iTable = -1;
000206    pExpr->pTab = 0;
000207    ExprSetVVAProperty(pExpr, EP_NoReduce);
000208  
000209    /* Translate the schema name in zDb into a pointer to the corresponding
000210    ** schema.  If not found, pSchema will remain NULL and nothing will match
000211    ** resulting in an appropriate error message toward the end of this routine
000212    */
000213    if( zDb ){
000214      testcase( pNC->ncFlags & NC_PartIdx );
000215      testcase( pNC->ncFlags & NC_IsCheck );
000216      if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
000217        /* Silently ignore database qualifiers inside CHECK constraints and
000218        ** partial indices.  Do not raise errors because that might break
000219        ** legacy and because it does not hurt anything to just ignore the
000220        ** database name. */
000221        zDb = 0;
000222      }else{
000223        for(i=0; i<db->nDb; i++){
000224          assert( db->aDb[i].zDbSName );
000225          if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
000226            pSchema = db->aDb[i].pSchema;
000227            break;
000228          }
000229        }
000230      }
000231    }
000232  
000233    /* Start at the inner-most context and move outward until a match is found */
000234    while( pNC && cnt==0 ){
000235      ExprList *pEList;
000236      SrcList *pSrcList = pNC->pSrcList;
000237  
000238      if( pSrcList ){
000239        for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
000240          pTab = pItem->pTab;
000241          assert( pTab!=0 && pTab->zName!=0 );
000242          assert( pTab->nCol>0 );
000243          if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
000244            int hit = 0;
000245            pEList = pItem->pSelect->pEList;
000246            for(j=0; j<pEList->nExpr; j++){
000247              if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
000248                cnt++;
000249                cntTab = 2;
000250                pMatch = pItem;
000251                pExpr->iColumn = j;
000252                hit = 1;
000253              }
000254            }
000255            if( hit || zTab==0 ) continue;
000256          }
000257          if( zDb && pTab->pSchema!=pSchema ){
000258            continue;
000259          }
000260          if( zTab ){
000261            const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
000262            assert( zTabName!=0 );
000263            if( sqlite3StrICmp(zTabName, zTab)!=0 ){
000264              continue;
000265            }
000266          }
000267          if( 0==(cntTab++) ){
000268            pMatch = pItem;
000269          }
000270          for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
000271            if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
000272              /* If there has been exactly one prior match and this match
000273              ** is for the right-hand table of a NATURAL JOIN or is in a 
000274              ** USING clause, then skip this match.
000275              */
000276              if( cnt==1 ){
000277                if( pItem->fg.jointype & JT_NATURAL ) continue;
000278                if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
000279              }
000280              cnt++;
000281              pMatch = pItem;
000282              /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
000283              pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
000284              break;
000285            }
000286          }
000287        }
000288        if( pMatch ){
000289          pExpr->iTable = pMatch->iCursor;
000290          pExpr->pTab = pMatch->pTab;
000291          /* RIGHT JOIN not (yet) supported */
000292          assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
000293          if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
000294            ExprSetProperty(pExpr, EP_CanBeNull);
000295          }
000296          pSchema = pExpr->pTab->pSchema;
000297        }
000298      } /* if( pSrcList ) */
000299  
000300  #ifndef SQLITE_OMIT_TRIGGER
000301      /* If we have not already resolved the name, then maybe 
000302      ** it is a new.* or old.* trigger argument reference
000303      */
000304      if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
000305        int op = pParse->eTriggerOp;
000306        assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
000307        if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
000308          pExpr->iTable = 1;
000309          pTab = pParse->pTriggerTab;
000310        }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
000311          pExpr->iTable = 0;
000312          pTab = pParse->pTriggerTab;
000313        }else{
000314          pTab = 0;
000315        }
000316  
000317        if( pTab ){ 
000318          int iCol;
000319          pSchema = pTab->pSchema;
000320          cntTab++;
000321          for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
000322            if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
000323              if( iCol==pTab->iPKey ){
000324                iCol = -1;
000325              }
000326              break;
000327            }
000328          }
000329          if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
000330            /* IMP: R-51414-32910 */
000331            iCol = -1;
000332          }
000333          if( iCol<pTab->nCol ){
000334            cnt++;
000335            if( iCol<0 ){
000336              pExpr->affinity = SQLITE_AFF_INTEGER;
000337            }else if( pExpr->iTable==0 ){
000338              testcase( iCol==31 );
000339              testcase( iCol==32 );
000340              pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
000341            }else{
000342              testcase( iCol==31 );
000343              testcase( iCol==32 );
000344              pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
000345            }
000346            pExpr->iColumn = (i16)iCol;
000347            pExpr->pTab = pTab;
000348            isTrigger = 1;
000349          }
000350        }
000351      }
000352  #endif /* !defined(SQLITE_OMIT_TRIGGER) */
000353  
000354      /*
000355      ** Perhaps the name is a reference to the ROWID
000356      */
000357      if( cnt==0
000358       && cntTab==1
000359       && pMatch
000360       && (pNC->ncFlags & NC_IdxExpr)==0
000361       && sqlite3IsRowid(zCol)
000362       && VisibleRowid(pMatch->pTab)
000363      ){
000364        cnt = 1;
000365        pExpr->iColumn = -1;
000366        pExpr->affinity = SQLITE_AFF_INTEGER;
000367      }
000368  
000369      /*
000370      ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
000371      ** might refer to an result-set alias.  This happens, for example, when
000372      ** we are resolving names in the WHERE clause of the following command:
000373      **
000374      **     SELECT a+b AS x FROM table WHERE x<10;
000375      **
000376      ** In cases like this, replace pExpr with a copy of the expression that
000377      ** forms the result set entry ("a+b" in the example) and return immediately.
000378      ** Note that the expression in the result set should have already been
000379      ** resolved by the time the WHERE clause is resolved.
000380      **
000381      ** The ability to use an output result-set column in the WHERE, GROUP BY,
000382      ** or HAVING clauses, or as part of a larger expression in the ORDER BY
000383      ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
000384      ** is supported for backwards compatibility only. Hence, we issue a warning
000385      ** on sqlite3_log() whenever the capability is used.
000386      */
000387      if( (pEList = pNC->pEList)!=0
000388       && zTab==0
000389       && cnt==0
000390      ){
000391        for(j=0; j<pEList->nExpr; j++){
000392          char *zAs = pEList->a[j].zName;
000393          if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
000394            Expr *pOrig;
000395            assert( pExpr->pLeft==0 && pExpr->pRight==0 );
000396            assert( pExpr->x.pList==0 );
000397            assert( pExpr->x.pSelect==0 );
000398            pOrig = pEList->a[j].pExpr;
000399            if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
000400              sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
000401              return WRC_Abort;
000402            }
000403            if( sqlite3ExprVectorSize(pOrig)!=1 ){
000404              sqlite3ErrorMsg(pParse, "row value misused");
000405              return WRC_Abort;
000406            }
000407            resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
000408            cnt = 1;
000409            pMatch = 0;
000410            assert( zTab==0 && zDb==0 );
000411            goto lookupname_end;
000412          }
000413        } 
000414      }
000415  
000416      /* Advance to the next name context.  The loop will exit when either
000417      ** we have a match (cnt>0) or when we run out of name contexts.
000418      */
000419      if( cnt==0 ){
000420        pNC = pNC->pNext;
000421        nSubquery++;
000422      }
000423    }
000424  
000425    /*
000426    ** If X and Y are NULL (in other words if only the column name Z is
000427    ** supplied) and the value of Z is enclosed in double-quotes, then
000428    ** Z is a string literal if it doesn't match any column names.  In that
000429    ** case, we need to return right away and not make any changes to
000430    ** pExpr.
000431    **
000432    ** Because no reference was made to outer contexts, the pNC->nRef
000433    ** fields are not changed in any context.
000434    */
000435    if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
000436      pExpr->op = TK_STRING;
000437      pExpr->pTab = 0;
000438      return WRC_Prune;
000439    }
000440  
000441    /*
000442    ** cnt==0 means there was not match.  cnt>1 means there were two or
000443    ** more matches.  Either way, we have an error.
000444    */
000445    if( cnt!=1 ){
000446      const char *zErr;
000447      zErr = cnt==0 ? "no such column" : "ambiguous column name";
000448      if( zDb ){
000449        sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
000450      }else if( zTab ){
000451        sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
000452      }else{
000453        sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
000454      }
000455      pParse->checkSchema = 1;
000456      pTopNC->nErr++;
000457    }
000458  
000459    /* If a column from a table in pSrcList is referenced, then record
000460    ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
000461    ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
000462    ** column number is greater than the number of bits in the bitmask
000463    ** then set the high-order bit of the bitmask.
000464    */
000465    if( pExpr->iColumn>=0 && pMatch!=0 ){
000466      int n = pExpr->iColumn;
000467      testcase( n==BMS-1 );
000468      if( n>=BMS ){
000469        n = BMS-1;
000470      }
000471      assert( pMatch->iCursor==pExpr->iTable );
000472      pMatch->colUsed |= ((Bitmask)1)<<n;
000473    }
000474  
000475    /* Clean up and return
000476    */
000477    sqlite3ExprDelete(db, pExpr->pLeft);
000478    pExpr->pLeft = 0;
000479    sqlite3ExprDelete(db, pExpr->pRight);
000480    pExpr->pRight = 0;
000481    pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
000482  lookupname_end:
000483    if( cnt==1 ){
000484      assert( pNC!=0 );
000485      if( !ExprHasProperty(pExpr, EP_Alias) ){
000486        sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
000487      }
000488      /* Increment the nRef value on all name contexts from TopNC up to
000489      ** the point where the name matched. */
000490      for(;;){
000491        assert( pTopNC!=0 );
000492        pTopNC->nRef++;
000493        if( pTopNC==pNC ) break;
000494        pTopNC = pTopNC->pNext;
000495      }
000496      return WRC_Prune;
000497    } else {
000498      return WRC_Abort;
000499    }
000500  }
000501  
000502  /*
000503  ** Allocate and return a pointer to an expression to load the column iCol
000504  ** from datasource iSrc in SrcList pSrc.
000505  */
000506  Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
000507    Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
000508    if( p ){
000509      struct SrcList_item *pItem = &pSrc->a[iSrc];
000510      p->pTab = pItem->pTab;
000511      p->iTable = pItem->iCursor;
000512      if( p->pTab->iPKey==iCol ){
000513        p->iColumn = -1;
000514      }else{
000515        p->iColumn = (ynVar)iCol;
000516        testcase( iCol==BMS );
000517        testcase( iCol==BMS-1 );
000518        pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
000519      }
000520      ExprSetProperty(p, EP_Resolved);
000521    }
000522    return p;
000523  }
000524  
000525  /*
000526  ** Report an error that an expression is not valid for some set of
000527  ** pNC->ncFlags values determined by validMask.
000528  */
000529  static void notValid(
000530    Parse *pParse,       /* Leave error message here */
000531    NameContext *pNC,    /* The name context */
000532    const char *zMsg,    /* Type of error */
000533    int validMask        /* Set of contexts for which prohibited */
000534  ){
000535    assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 );
000536    if( (pNC->ncFlags & validMask)!=0 ){
000537      const char *zIn = "partial index WHERE clauses";
000538      if( pNC->ncFlags & NC_IdxExpr )      zIn = "index expressions";
000539  #ifndef SQLITE_OMIT_CHECK
000540      else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
000541  #endif
000542      sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
000543    }
000544  }
000545  
000546  /*
000547  ** Expression p should encode a floating point value between 1.0 and 0.0.
000548  ** Return 1024 times this value.  Or return -1 if p is not a floating point
000549  ** value between 1.0 and 0.0.
000550  */
000551  static int exprProbability(Expr *p){
000552    double r = -1.0;
000553    if( p->op!=TK_FLOAT ) return -1;
000554    sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
000555    assert( r>=0.0 );
000556    if( r>1.0 ) return -1;
000557    return (int)(r*134217728.0);
000558  }
000559  
000560  /*
000561  ** This routine is callback for sqlite3WalkExpr().
000562  **
000563  ** Resolve symbolic names into TK_COLUMN operators for the current
000564  ** node in the expression tree.  Return 0 to continue the search down
000565  ** the tree or 2 to abort the tree walk.
000566  **
000567  ** This routine also does error checking and name resolution for
000568  ** function names.  The operator for aggregate functions is changed
000569  ** to TK_AGG_FUNCTION.
000570  */
000571  static int resolveExprStep(Walker *pWalker, Expr *pExpr){
000572    NameContext *pNC;
000573    Parse *pParse;
000574  
000575    pNC = pWalker->u.pNC;
000576    assert( pNC!=0 );
000577    pParse = pNC->pParse;
000578    assert( pParse==pWalker->pParse );
000579  
000580    if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune;
000581    ExprSetProperty(pExpr, EP_Resolved);
000582  #ifndef NDEBUG
000583    if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
000584      SrcList *pSrcList = pNC->pSrcList;
000585      int i;
000586      for(i=0; i<pNC->pSrcList->nSrc; i++){
000587        assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
000588      }
000589    }
000590  #endif
000591    switch( pExpr->op ){
000592  
000593  #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
000594      /* The special operator TK_ROW means use the rowid for the first
000595      ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
000596      ** clause processing on UPDATE and DELETE statements.
000597      */
000598      case TK_ROW: {
000599        SrcList *pSrcList = pNC->pSrcList;
000600        struct SrcList_item *pItem;
000601        assert( pSrcList && pSrcList->nSrc==1 );
000602        pItem = pSrcList->a; 
000603        pExpr->op = TK_COLUMN;
000604        pExpr->pTab = pItem->pTab;
000605        pExpr->iTable = pItem->iCursor;
000606        pExpr->iColumn = -1;
000607        pExpr->affinity = SQLITE_AFF_INTEGER;
000608        break;
000609      }
000610  #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
000611            && !defined(SQLITE_OMIT_SUBQUERY) */
000612  
000613      /* A lone identifier is the name of a column.
000614      */
000615      case TK_ID: {
000616        return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
000617      }
000618    
000619      /* A table name and column name:     ID.ID
000620      ** Or a database, table and column:  ID.ID.ID
000621      */
000622      case TK_DOT: {
000623        const char *zColumn;
000624        const char *zTable;
000625        const char *zDb;
000626        Expr *pRight;
000627  
000628        /* if( pSrcList==0 ) break; */
000629        notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
000630        pRight = pExpr->pRight;
000631        if( pRight->op==TK_ID ){
000632          zDb = 0;
000633          zTable = pExpr->pLeft->u.zToken;
000634          zColumn = pRight->u.zToken;
000635        }else{
000636          assert( pRight->op==TK_DOT );
000637          zDb = pExpr->pLeft->u.zToken;
000638          zTable = pRight->pLeft->u.zToken;
000639          zColumn = pRight->pRight->u.zToken;
000640        }
000641        return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
000642      }
000643  
000644      /* Resolve function names
000645      */
000646      case TK_FUNCTION: {
000647        ExprList *pList = pExpr->x.pList;    /* The argument list */
000648        int n = pList ? pList->nExpr : 0;    /* Number of arguments */
000649        int no_such_func = 0;       /* True if no such function exists */
000650        int wrong_num_args = 0;     /* True if wrong number of arguments */
000651        int is_agg = 0;             /* True if is an aggregate function */
000652        int nId;                    /* Number of characters in function name */
000653        const char *zId;            /* The function name. */
000654        FuncDef *pDef;              /* Information about the function */
000655        u8 enc = ENC(pParse->db);   /* The database encoding */
000656  
000657        assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
000658        zId = pExpr->u.zToken;
000659        nId = sqlite3Strlen30(zId);
000660        pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
000661        if( pDef==0 ){
000662          pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
000663          if( pDef==0 ){
000664            no_such_func = 1;
000665          }else{
000666            wrong_num_args = 1;
000667          }
000668        }else{
000669          is_agg = pDef->xFinalize!=0;
000670          if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
000671            ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
000672            if( n==2 ){
000673              pExpr->iTable = exprProbability(pList->a[1].pExpr);
000674              if( pExpr->iTable<0 ){
000675                sqlite3ErrorMsg(pParse,
000676                  "second argument to likelihood() must be a "
000677                  "constant between 0.0 and 1.0");
000678                pNC->nErr++;
000679              }
000680            }else{
000681              /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
000682              ** equivalent to likelihood(X, 0.0625).
000683              ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
000684              ** short-hand for likelihood(X,0.0625).
000685              ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
000686              ** for likelihood(X,0.9375).
000687              ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
000688              ** to likelihood(X,0.9375). */
000689              /* TUNING: unlikely() probability is 0.0625.  likely() is 0.9375 */
000690              pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
000691            }             
000692          }
000693  #ifndef SQLITE_OMIT_AUTHORIZATION
000694          {
000695            int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
000696            if( auth!=SQLITE_OK ){
000697              if( auth==SQLITE_DENY ){
000698                sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
000699                                        pDef->zName);
000700                pNC->nErr++;
000701              }
000702              pExpr->op = TK_NULL;
000703              return WRC_Prune;
000704            }
000705          }
000706  #endif
000707          if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
000708            /* For the purposes of the EP_ConstFunc flag, date and time
000709            ** functions and other functions that change slowly are considered
000710            ** constant because they are constant for the duration of one query */
000711            ExprSetProperty(pExpr,EP_ConstFunc);
000712          }
000713          if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
000714            /* Date/time functions that use 'now', and other functions like
000715            ** sqlite_version() that might change over time cannot be used
000716            ** in an index. */
000717            notValid(pParse, pNC, "non-deterministic functions",
000718                     NC_IdxExpr|NC_PartIdx);
000719          }
000720        }
000721        if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
000722          sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
000723          pNC->nErr++;
000724          is_agg = 0;
000725        }else if( no_such_func && pParse->db->init.busy==0
000726  #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
000727                  && pParse->explain==0
000728  #endif
000729        ){
000730          sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
000731          pNC->nErr++;
000732        }else if( wrong_num_args ){
000733          sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
000734               nId, zId);
000735          pNC->nErr++;
000736        }
000737        if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
000738        sqlite3WalkExprList(pWalker, pList);
000739        if( is_agg ){
000740          NameContext *pNC2 = pNC;
000741          pExpr->op = TK_AGG_FUNCTION;
000742          pExpr->op2 = 0;
000743          while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
000744            pExpr->op2++;
000745            pNC2 = pNC2->pNext;
000746          }
000747          assert( pDef!=0 );
000748          if( pNC2 ){
000749            assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
000750            testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
000751            pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
000752  
000753          }
000754          pNC->ncFlags |= NC_AllowAgg;
000755        }
000756        /* FIX ME:  Compute pExpr->affinity based on the expected return
000757        ** type of the function 
000758        */
000759        return WRC_Prune;
000760      }
000761  #ifndef SQLITE_OMIT_SUBQUERY
000762      case TK_SELECT:
000763      case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
000764  #endif
000765      case TK_IN: {
000766        testcase( pExpr->op==TK_IN );
000767        if( ExprHasProperty(pExpr, EP_xIsSelect) ){
000768          int nRef = pNC->nRef;
000769          notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
000770          sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
000771          assert( pNC->nRef>=nRef );
000772          if( nRef!=pNC->nRef ){
000773            ExprSetProperty(pExpr, EP_VarSelect);
000774            pNC->ncFlags |= NC_VarSelect;
000775          }
000776        }
000777        break;
000778      }
000779      case TK_VARIABLE: {
000780        notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
000781        break;
000782      }
000783      case TK_BETWEEN:
000784      case TK_EQ:
000785      case TK_NE:
000786      case TK_LT:
000787      case TK_LE:
000788      case TK_GT:
000789      case TK_GE:
000790      case TK_IS:
000791      case TK_ISNOT: {
000792        int nLeft, nRight;
000793        if( pParse->db->mallocFailed ) break;
000794        assert( pExpr->pLeft!=0 );
000795        nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
000796        if( pExpr->op==TK_BETWEEN ){
000797          nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
000798          if( nRight==nLeft ){
000799            nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
000800          }
000801        }else{
000802          assert( pExpr->pRight!=0 );
000803          nRight = sqlite3ExprVectorSize(pExpr->pRight);
000804        }
000805        if( nLeft!=nRight ){
000806          testcase( pExpr->op==TK_EQ );
000807          testcase( pExpr->op==TK_NE );
000808          testcase( pExpr->op==TK_LT );
000809          testcase( pExpr->op==TK_LE );
000810          testcase( pExpr->op==TK_GT );
000811          testcase( pExpr->op==TK_GE );
000812          testcase( pExpr->op==TK_IS );
000813          testcase( pExpr->op==TK_ISNOT );
000814          testcase( pExpr->op==TK_BETWEEN );
000815          sqlite3ErrorMsg(pParse, "row value misused");
000816        }
000817        break; 
000818      }
000819    }
000820    return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
000821  }
000822  
000823  /*
000824  ** pEList is a list of expressions which are really the result set of the
000825  ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
000826  ** This routine checks to see if pE is a simple identifier which corresponds
000827  ** to the AS-name of one of the terms of the expression list.  If it is,
000828  ** this routine return an integer between 1 and N where N is the number of
000829  ** elements in pEList, corresponding to the matching entry.  If there is
000830  ** no match, or if pE is not a simple identifier, then this routine
000831  ** return 0.
000832  **
000833  ** pEList has been resolved.  pE has not.
000834  */
000835  static int resolveAsName(
000836    Parse *pParse,     /* Parsing context for error messages */
000837    ExprList *pEList,  /* List of expressions to scan */
000838    Expr *pE           /* Expression we are trying to match */
000839  ){
000840    int i;             /* Loop counter */
000841  
000842    UNUSED_PARAMETER(pParse);
000843  
000844    if( pE->op==TK_ID ){
000845      char *zCol = pE->u.zToken;
000846      for(i=0; i<pEList->nExpr; i++){
000847        char *zAs = pEList->a[i].zName;
000848        if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
000849          return i+1;
000850        }
000851      }
000852    }
000853    return 0;
000854  }
000855  
000856  /*
000857  ** pE is a pointer to an expression which is a single term in the
000858  ** ORDER BY of a compound SELECT.  The expression has not been
000859  ** name resolved.
000860  **
000861  ** At the point this routine is called, we already know that the
000862  ** ORDER BY term is not an integer index into the result set.  That
000863  ** case is handled by the calling routine.
000864  **
000865  ** Attempt to match pE against result set columns in the left-most
000866  ** SELECT statement.  Return the index i of the matching column,
000867  ** as an indication to the caller that it should sort by the i-th column.
000868  ** The left-most column is 1.  In other words, the value returned is the
000869  ** same integer value that would be used in the SQL statement to indicate
000870  ** the column.
000871  **
000872  ** If there is no match, return 0.  Return -1 if an error occurs.
000873  */
000874  static int resolveOrderByTermToExprList(
000875    Parse *pParse,     /* Parsing context for error messages */
000876    Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
000877    Expr *pE           /* The specific ORDER BY term */
000878  ){
000879    int i;             /* Loop counter */
000880    ExprList *pEList;  /* The columns of the result set */
000881    NameContext nc;    /* Name context for resolving pE */
000882    sqlite3 *db;       /* Database connection */
000883    int rc;            /* Return code from subprocedures */
000884    u8 savedSuppErr;   /* Saved value of db->suppressErr */
000885  
000886    assert( sqlite3ExprIsInteger(pE, &i)==0 );
000887    pEList = pSelect->pEList;
000888  
000889    /* Resolve all names in the ORDER BY term expression
000890    */
000891    memset(&nc, 0, sizeof(nc));
000892    nc.pParse = pParse;
000893    nc.pSrcList = pSelect->pSrc;
000894    nc.pEList = pEList;
000895    nc.ncFlags = NC_AllowAgg;
000896    nc.nErr = 0;
000897    db = pParse->db;
000898    savedSuppErr = db->suppressErr;
000899    db->suppressErr = 1;
000900    rc = sqlite3ResolveExprNames(&nc, pE);
000901    db->suppressErr = savedSuppErr;
000902    if( rc ) return 0;
000903  
000904    /* Try to match the ORDER BY expression against an expression
000905    ** in the result set.  Return an 1-based index of the matching
000906    ** result-set entry.
000907    */
000908    for(i=0; i<pEList->nExpr; i++){
000909      if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
000910        return i+1;
000911      }
000912    }
000913  
000914    /* If no match, return 0. */
000915    return 0;
000916  }
000917  
000918  /*
000919  ** Generate an ORDER BY or GROUP BY term out-of-range error.
000920  */
000921  static void resolveOutOfRangeError(
000922    Parse *pParse,         /* The error context into which to write the error */
000923    const char *zType,     /* "ORDER" or "GROUP" */
000924    int i,                 /* The index (1-based) of the term out of range */
000925    int mx                 /* Largest permissible value of i */
000926  ){
000927    sqlite3ErrorMsg(pParse, 
000928      "%r %s BY term out of range - should be "
000929      "between 1 and %d", i, zType, mx);
000930  }
000931  
000932  /*
000933  ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
000934  ** each term of the ORDER BY clause is a constant integer between 1
000935  ** and N where N is the number of columns in the compound SELECT.
000936  **
000937  ** ORDER BY terms that are already an integer between 1 and N are
000938  ** unmodified.  ORDER BY terms that are integers outside the range of
000939  ** 1 through N generate an error.  ORDER BY terms that are expressions
000940  ** are matched against result set expressions of compound SELECT
000941  ** beginning with the left-most SELECT and working toward the right.
000942  ** At the first match, the ORDER BY expression is transformed into
000943  ** the integer column number.
000944  **
000945  ** Return the number of errors seen.
000946  */
000947  static int resolveCompoundOrderBy(
000948    Parse *pParse,        /* Parsing context.  Leave error messages here */
000949    Select *pSelect       /* The SELECT statement containing the ORDER BY */
000950  ){
000951    int i;
000952    ExprList *pOrderBy;
000953    ExprList *pEList;
000954    sqlite3 *db;
000955    int moreToDo = 1;
000956  
000957    pOrderBy = pSelect->pOrderBy;
000958    if( pOrderBy==0 ) return 0;
000959    db = pParse->db;
000960  #if SQLITE_MAX_COLUMN
000961    if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
000962      sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
000963      return 1;
000964    }
000965  #endif
000966    for(i=0; i<pOrderBy->nExpr; i++){
000967      pOrderBy->a[i].done = 0;
000968    }
000969    pSelect->pNext = 0;
000970    while( pSelect->pPrior ){
000971      pSelect->pPrior->pNext = pSelect;
000972      pSelect = pSelect->pPrior;
000973    }
000974    while( pSelect && moreToDo ){
000975      struct ExprList_item *pItem;
000976      moreToDo = 0;
000977      pEList = pSelect->pEList;
000978      assert( pEList!=0 );
000979      for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
000980        int iCol = -1;
000981        Expr *pE, *pDup;
000982        if( pItem->done ) continue;
000983        pE = sqlite3ExprSkipCollate(pItem->pExpr);
000984        if( sqlite3ExprIsInteger(pE, &iCol) ){
000985          if( iCol<=0 || iCol>pEList->nExpr ){
000986            resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
000987            return 1;
000988          }
000989        }else{
000990          iCol = resolveAsName(pParse, pEList, pE);
000991          if( iCol==0 ){
000992            pDup = sqlite3ExprDup(db, pE, 0);
000993            if( !db->mallocFailed ){
000994              assert(pDup);
000995              iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
000996            }
000997            sqlite3ExprDelete(db, pDup);
000998          }
000999        }
001000        if( iCol>0 ){
001001          /* Convert the ORDER BY term into an integer column number iCol,
001002          ** taking care to preserve the COLLATE clause if it exists */
001003          Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
001004          if( pNew==0 ) return 1;
001005          pNew->flags |= EP_IntValue;
001006          pNew->u.iValue = iCol;
001007          if( pItem->pExpr==pE ){
001008            pItem->pExpr = pNew;
001009          }else{
001010            Expr *pParent = pItem->pExpr;
001011            assert( pParent->op==TK_COLLATE );
001012            while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
001013            assert( pParent->pLeft==pE );
001014            pParent->pLeft = pNew;
001015          }
001016          sqlite3ExprDelete(db, pE);
001017          pItem->u.x.iOrderByCol = (u16)iCol;
001018          pItem->done = 1;
001019        }else{
001020          moreToDo = 1;
001021        }
001022      }
001023      pSelect = pSelect->pNext;
001024    }
001025    for(i=0; i<pOrderBy->nExpr; i++){
001026      if( pOrderBy->a[i].done==0 ){
001027        sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
001028              "column in the result set", i+1);
001029        return 1;
001030      }
001031    }
001032    return 0;
001033  }
001034  
001035  /*
001036  ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
001037  ** the SELECT statement pSelect.  If any term is reference to a
001038  ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
001039  ** field) then convert that term into a copy of the corresponding result set
001040  ** column.
001041  **
001042  ** If any errors are detected, add an error message to pParse and
001043  ** return non-zero.  Return zero if no errors are seen.
001044  */
001045  int sqlite3ResolveOrderGroupBy(
001046    Parse *pParse,        /* Parsing context.  Leave error messages here */
001047    Select *pSelect,      /* The SELECT statement containing the clause */
001048    ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
001049    const char *zType     /* "ORDER" or "GROUP" */
001050  ){
001051    int i;
001052    sqlite3 *db = pParse->db;
001053    ExprList *pEList;
001054    struct ExprList_item *pItem;
001055  
001056    if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
001057  #if SQLITE_MAX_COLUMN
001058    if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
001059      sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
001060      return 1;
001061    }
001062  #endif
001063    pEList = pSelect->pEList;
001064    assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
001065    for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
001066      if( pItem->u.x.iOrderByCol ){
001067        if( pItem->u.x.iOrderByCol>pEList->nExpr ){
001068          resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
001069          return 1;
001070        }
001071        resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
001072                     zType,0);
001073      }
001074    }
001075    return 0;
001076  }
001077  
001078  /*
001079  ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
001080  ** The Name context of the SELECT statement is pNC.  zType is either
001081  ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
001082  **
001083  ** This routine resolves each term of the clause into an expression.
001084  ** If the order-by term is an integer I between 1 and N (where N is the
001085  ** number of columns in the result set of the SELECT) then the expression
001086  ** in the resolution is a copy of the I-th result-set expression.  If
001087  ** the order-by term is an identifier that corresponds to the AS-name of
001088  ** a result-set expression, then the term resolves to a copy of the
001089  ** result-set expression.  Otherwise, the expression is resolved in
001090  ** the usual way - using sqlite3ResolveExprNames().
001091  **
001092  ** This routine returns the number of errors.  If errors occur, then
001093  ** an appropriate error message might be left in pParse.  (OOM errors
001094  ** excepted.)
001095  */
001096  static int resolveOrderGroupBy(
001097    NameContext *pNC,     /* The name context of the SELECT statement */
001098    Select *pSelect,      /* The SELECT statement holding pOrderBy */
001099    ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
001100    const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
001101  ){
001102    int i, j;                      /* Loop counters */
001103    int iCol;                      /* Column number */
001104    struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
001105    Parse *pParse;                 /* Parsing context */
001106    int nResult;                   /* Number of terms in the result set */
001107  
001108    if( pOrderBy==0 ) return 0;
001109    nResult = pSelect->pEList->nExpr;
001110    pParse = pNC->pParse;
001111    for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
001112      Expr *pE = pItem->pExpr;
001113      Expr *pE2 = sqlite3ExprSkipCollate(pE);
001114      if( zType[0]!='G' ){
001115        iCol = resolveAsName(pParse, pSelect->pEList, pE2);
001116        if( iCol>0 ){
001117          /* If an AS-name match is found, mark this ORDER BY column as being
001118          ** a copy of the iCol-th result-set column.  The subsequent call to
001119          ** sqlite3ResolveOrderGroupBy() will convert the expression to a
001120          ** copy of the iCol-th result-set expression. */
001121          pItem->u.x.iOrderByCol = (u16)iCol;
001122          continue;
001123        }
001124      }
001125      if( sqlite3ExprIsInteger(pE2, &iCol) ){
001126        /* The ORDER BY term is an integer constant.  Again, set the column
001127        ** number so that sqlite3ResolveOrderGroupBy() will convert the
001128        ** order-by term to a copy of the result-set expression */
001129        if( iCol<1 || iCol>0xffff ){
001130          resolveOutOfRangeError(pParse, zType, i+1, nResult);
001131          return 1;
001132        }
001133        pItem->u.x.iOrderByCol = (u16)iCol;
001134        continue;
001135      }
001136  
001137      /* Otherwise, treat the ORDER BY term as an ordinary expression */
001138      pItem->u.x.iOrderByCol = 0;
001139      if( sqlite3ResolveExprNames(pNC, pE) ){
001140        return 1;
001141      }
001142      for(j=0; j<pSelect->pEList->nExpr; j++){
001143        if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
001144          pItem->u.x.iOrderByCol = j+1;
001145        }
001146      }
001147    }
001148    return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
001149  }
001150  
001151  /*
001152  ** Resolve names in the SELECT statement p and all of its descendants.
001153  */
001154  static int resolveSelectStep(Walker *pWalker, Select *p){
001155    NameContext *pOuterNC;  /* Context that contains this SELECT */
001156    NameContext sNC;        /* Name context of this SELECT */
001157    int isCompound;         /* True if p is a compound select */
001158    int nCompound;          /* Number of compound terms processed so far */
001159    Parse *pParse;          /* Parsing context */
001160    int i;                  /* Loop counter */
001161    ExprList *pGroupBy;     /* The GROUP BY clause */
001162    Select *pLeftmost;      /* Left-most of SELECT of a compound */
001163    sqlite3 *db;            /* Database connection */
001164    
001165  
001166    assert( p!=0 );
001167    if( p->selFlags & SF_Resolved ){
001168      return WRC_Prune;
001169    }
001170    pOuterNC = pWalker->u.pNC;
001171    pParse = pWalker->pParse;
001172    db = pParse->db;
001173  
001174    /* Normally sqlite3SelectExpand() will be called first and will have
001175    ** already expanded this SELECT.  However, if this is a subquery within
001176    ** an expression, sqlite3ResolveExprNames() will be called without a
001177    ** prior call to sqlite3SelectExpand().  When that happens, let
001178    ** sqlite3SelectPrep() do all of the processing for this SELECT.
001179    ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
001180    ** this routine in the correct order.
001181    */
001182    if( (p->selFlags & SF_Expanded)==0 ){
001183      sqlite3SelectPrep(pParse, p, pOuterNC);
001184      return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
001185    }
001186  
001187    isCompound = p->pPrior!=0;
001188    nCompound = 0;
001189    pLeftmost = p;
001190    while( p ){
001191      assert( (p->selFlags & SF_Expanded)!=0 );
001192      assert( (p->selFlags & SF_Resolved)==0 );
001193      p->selFlags |= SF_Resolved;
001194  
001195      /* Resolve the expressions in the LIMIT and OFFSET clauses. These
001196      ** are not allowed to refer to any names, so pass an empty NameContext.
001197      */
001198      memset(&sNC, 0, sizeof(sNC));
001199      sNC.pParse = pParse;
001200      if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
001201          sqlite3ResolveExprNames(&sNC, p->pOffset) ){
001202        return WRC_Abort;
001203      }
001204  
001205      /* If the SF_Converted flags is set, then this Select object was
001206      ** was created by the convertCompoundSelectToSubquery() function.
001207      ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
001208      ** as if it were part of the sub-query, not the parent. This block
001209      ** moves the pOrderBy down to the sub-query. It will be moved back
001210      ** after the names have been resolved.  */
001211      if( p->selFlags & SF_Converted ){
001212        Select *pSub = p->pSrc->a[0].pSelect;
001213        assert( p->pSrc->nSrc==1 && p->pOrderBy );
001214        assert( pSub->pPrior && pSub->pOrderBy==0 );
001215        pSub->pOrderBy = p->pOrderBy;
001216        p->pOrderBy = 0;
001217      }
001218    
001219      /* Recursively resolve names in all subqueries
001220      */
001221      for(i=0; i<p->pSrc->nSrc; i++){
001222        struct SrcList_item *pItem = &p->pSrc->a[i];
001223        if( pItem->pSelect ){
001224          NameContext *pNC;         /* Used to iterate name contexts */
001225          int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
001226          const char *zSavedContext = pParse->zAuthContext;
001227  
001228          /* Count the total number of references to pOuterNC and all of its
001229          ** parent contexts. After resolving references to expressions in
001230          ** pItem->pSelect, check if this value has changed. If so, then
001231          ** SELECT statement pItem->pSelect must be correlated. Set the
001232          ** pItem->fg.isCorrelated flag if this is the case. */
001233          for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
001234  
001235          if( pItem->zName ) pParse->zAuthContext = pItem->zName;
001236          sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
001237          pParse->zAuthContext = zSavedContext;
001238          if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
001239  
001240          for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
001241          assert( pItem->fg.isCorrelated==0 && nRef<=0 );
001242          pItem->fg.isCorrelated = (nRef!=0);
001243        }
001244      }
001245    
001246      /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
001247      ** resolve the result-set expression list.
001248      */
001249      sNC.ncFlags = NC_AllowAgg;
001250      sNC.pSrcList = p->pSrc;
001251      sNC.pNext = pOuterNC;
001252    
001253      /* Resolve names in the result set. */
001254      if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
001255    
001256      /* If there are no aggregate functions in the result-set, and no GROUP BY 
001257      ** expression, do not allow aggregates in any of the other expressions.
001258      */
001259      assert( (p->selFlags & SF_Aggregate)==0 );
001260      pGroupBy = p->pGroupBy;
001261      if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
001262        assert( NC_MinMaxAgg==SF_MinMaxAgg );
001263        p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
001264      }else{
001265        sNC.ncFlags &= ~NC_AllowAgg;
001266      }
001267    
001268      /* If a HAVING clause is present, then there must be a GROUP BY clause.
001269      */
001270      if( p->pHaving && !pGroupBy ){
001271        sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
001272        return WRC_Abort;
001273      }
001274    
001275      /* Add the output column list to the name-context before parsing the
001276      ** other expressions in the SELECT statement. This is so that
001277      ** expressions in the WHERE clause (etc.) can refer to expressions by
001278      ** aliases in the result set.
001279      **
001280      ** Minor point: If this is the case, then the expression will be
001281      ** re-evaluated for each reference to it.
001282      */
001283      sNC.pEList = p->pEList;
001284      if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
001285      if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
001286  
001287      /* Resolve names in table-valued-function arguments */
001288      for(i=0; i<p->pSrc->nSrc; i++){
001289        struct SrcList_item *pItem = &p->pSrc->a[i];
001290        if( pItem->fg.isTabFunc
001291         && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg) 
001292        ){
001293          return WRC_Abort;
001294        }
001295      }
001296  
001297      /* The ORDER BY and GROUP BY clauses may not refer to terms in
001298      ** outer queries 
001299      */
001300      sNC.pNext = 0;
001301      sNC.ncFlags |= NC_AllowAgg;
001302  
001303      /* If this is a converted compound query, move the ORDER BY clause from 
001304      ** the sub-query back to the parent query. At this point each term
001305      ** within the ORDER BY clause has been transformed to an integer value.
001306      ** These integers will be replaced by copies of the corresponding result
001307      ** set expressions by the call to resolveOrderGroupBy() below.  */
001308      if( p->selFlags & SF_Converted ){
001309        Select *pSub = p->pSrc->a[0].pSelect;
001310        p->pOrderBy = pSub->pOrderBy;
001311        pSub->pOrderBy = 0;
001312      }
001313  
001314      /* Process the ORDER BY clause for singleton SELECT statements.
001315      ** The ORDER BY clause for compounds SELECT statements is handled
001316      ** below, after all of the result-sets for all of the elements of
001317      ** the compound have been resolved.
001318      **
001319      ** If there is an ORDER BY clause on a term of a compound-select other
001320      ** than the right-most term, then that is a syntax error.  But the error
001321      ** is not detected until much later, and so we need to go ahead and
001322      ** resolve those symbols on the incorrect ORDER BY for consistency.
001323      */
001324      if( isCompound<=nCompound  /* Defer right-most ORDER BY of a compound */
001325       && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
001326      ){
001327        return WRC_Abort;
001328      }
001329      if( db->mallocFailed ){
001330        return WRC_Abort;
001331      }
001332    
001333      /* Resolve the GROUP BY clause.  At the same time, make sure 
001334      ** the GROUP BY clause does not contain aggregate functions.
001335      */
001336      if( pGroupBy ){
001337        struct ExprList_item *pItem;
001338      
001339        if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
001340          return WRC_Abort;
001341        }
001342        for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
001343          if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
001344            sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
001345                "the GROUP BY clause");
001346            return WRC_Abort;
001347          }
001348        }
001349      }
001350  
001351      /* If this is part of a compound SELECT, check that it has the right
001352      ** number of expressions in the select list. */
001353      if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
001354        sqlite3SelectWrongNumTermsError(pParse, p->pNext);
001355        return WRC_Abort;
001356      }
001357  
001358      /* Advance to the next term of the compound
001359      */
001360      p = p->pPrior;
001361      nCompound++;
001362    }
001363  
001364    /* Resolve the ORDER BY on a compound SELECT after all terms of
001365    ** the compound have been resolved.
001366    */
001367    if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
001368      return WRC_Abort;
001369    }
001370  
001371    return WRC_Prune;
001372  }
001373  
001374  /*
001375  ** This routine walks an expression tree and resolves references to
001376  ** table columns and result-set columns.  At the same time, do error
001377  ** checking on function usage and set a flag if any aggregate functions
001378  ** are seen.
001379  **
001380  ** To resolve table columns references we look for nodes (or subtrees) of the 
001381  ** form X.Y.Z or Y.Z or just Z where
001382  **
001383  **      X:   The name of a database.  Ex:  "main" or "temp" or
001384  **           the symbolic name assigned to an ATTACH-ed database.
001385  **
001386  **      Y:   The name of a table in a FROM clause.  Or in a trigger
001387  **           one of the special names "old" or "new".
001388  **
001389  **      Z:   The name of a column in table Y.
001390  **
001391  ** The node at the root of the subtree is modified as follows:
001392  **
001393  **    Expr.op        Changed to TK_COLUMN
001394  **    Expr.pTab      Points to the Table object for X.Y
001395  **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
001396  **    Expr.iTable    The VDBE cursor number for X.Y
001397  **
001398  **
001399  ** To resolve result-set references, look for expression nodes of the
001400  ** form Z (with no X and Y prefix) where the Z matches the right-hand
001401  ** size of an AS clause in the result-set of a SELECT.  The Z expression
001402  ** is replaced by a copy of the left-hand side of the result-set expression.
001403  ** Table-name and function resolution occurs on the substituted expression
001404  ** tree.  For example, in:
001405  **
001406  **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
001407  **
001408  ** The "x" term of the order by is replaced by "a+b" to render:
001409  **
001410  **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
001411  **
001412  ** Function calls are checked to make sure that the function is 
001413  ** defined and that the correct number of arguments are specified.
001414  ** If the function is an aggregate function, then the NC_HasAgg flag is
001415  ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
001416  ** If an expression contains aggregate functions then the EP_Agg
001417  ** property on the expression is set.
001418  **
001419  ** An error message is left in pParse if anything is amiss.  The number
001420  ** if errors is returned.
001421  */
001422  int sqlite3ResolveExprNames( 
001423    NameContext *pNC,       /* Namespace to resolve expressions in. */
001424    Expr *pExpr             /* The expression to be analyzed. */
001425  ){
001426    u16 savedHasAgg;
001427    Walker w;
001428  
001429    if( pExpr==0 ) return 0;
001430  #if SQLITE_MAX_EXPR_DEPTH>0
001431    {
001432      Parse *pParse = pNC->pParse;
001433      if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
001434        return 1;
001435      }
001436      pParse->nHeight += pExpr->nHeight;
001437    }
001438  #endif
001439    savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
001440    pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
001441    w.pParse = pNC->pParse;
001442    w.xExprCallback = resolveExprStep;
001443    w.xSelectCallback = resolveSelectStep;
001444    w.xSelectCallback2 = 0;
001445    w.walkerDepth = 0;
001446    w.eCode = 0;
001447    w.u.pNC = pNC;
001448    sqlite3WalkExpr(&w, pExpr);
001449  #if SQLITE_MAX_EXPR_DEPTH>0
001450    pNC->pParse->nHeight -= pExpr->nHeight;
001451  #endif
001452    if( pNC->nErr>0 || w.pParse->nErr>0 ){
001453      ExprSetProperty(pExpr, EP_Error);
001454    }
001455    if( pNC->ncFlags & NC_HasAgg ){
001456      ExprSetProperty(pExpr, EP_Agg);
001457    }
001458    pNC->ncFlags |= savedHasAgg;
001459    return ExprHasProperty(pExpr, EP_Error);
001460  }
001461  
001462  /*
001463  ** Resolve all names for all expression in an expression list.  This is
001464  ** just like sqlite3ResolveExprNames() except that it works for an expression
001465  ** list rather than a single expression.
001466  */
001467  int sqlite3ResolveExprListNames( 
001468    NameContext *pNC,       /* Namespace to resolve expressions in. */
001469    ExprList *pList         /* The expression list to be analyzed. */
001470  ){
001471    int i;
001472    if( pList ){
001473      for(i=0; i<pList->nExpr; i++){
001474        if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
001475      }
001476    }
001477    return WRC_Continue;
001478  }
001479  
001480  /*
001481  ** Resolve all names in all expressions of a SELECT and in all
001482  ** decendents of the SELECT, including compounds off of p->pPrior,
001483  ** subqueries in expressions, and subqueries used as FROM clause
001484  ** terms.
001485  **
001486  ** See sqlite3ResolveExprNames() for a description of the kinds of
001487  ** transformations that occur.
001488  **
001489  ** All SELECT statements should have been expanded using
001490  ** sqlite3SelectExpand() prior to invoking this routine.
001491  */
001492  void sqlite3ResolveSelectNames(
001493    Parse *pParse,         /* The parser context */
001494    Select *p,             /* The SELECT statement being coded. */
001495    NameContext *pOuterNC  /* Name context for parent SELECT statement */
001496  ){
001497    Walker w;
001498  
001499    assert( p!=0 );
001500    memset(&w, 0, sizeof(w));
001501    w.xExprCallback = resolveExprStep;
001502    w.xSelectCallback = resolveSelectStep;
001503    w.pParse = pParse;
001504    w.u.pNC = pOuterNC;
001505    sqlite3WalkSelect(&w, p);
001506  }
001507  
001508  /*
001509  ** Resolve names in expressions that can only reference a single table:
001510  **
001511  **    *   CHECK constraints
001512  **    *   WHERE clauses on partial indices
001513  **
001514  ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
001515  ** is set to -1 and the Expr.iColumn value is set to the column number.
001516  **
001517  ** Any errors cause an error message to be set in pParse.
001518  */
001519  void sqlite3ResolveSelfReference(
001520    Parse *pParse,      /* Parsing context */
001521    Table *pTab,        /* The table being referenced */
001522    int type,           /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */
001523    Expr *pExpr,        /* Expression to resolve.  May be NULL. */
001524    ExprList *pList     /* Expression list to resolve.  May be NUL. */
001525  ){
001526    SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
001527    NameContext sNC;                /* Name context for pParse->pNewTable */
001528  
001529    assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr );
001530    memset(&sNC, 0, sizeof(sNC));
001531    memset(&sSrc, 0, sizeof(sSrc));
001532    sSrc.nSrc = 1;
001533    sSrc.a[0].zName = pTab->zName;
001534    sSrc.a[0].pTab = pTab;
001535    sSrc.a[0].iCursor = -1;
001536    sNC.pParse = pParse;
001537    sNC.pSrcList = &sSrc;
001538    sNC.ncFlags = type;
001539    if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
001540    if( pList ) sqlite3ResolveExprListNames(&sNC, pList);
001541  }