ds@1952: /* ds@1952: ** 2001 September 15 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This file contains routines used for analyzing expressions and ds@1952: ** for generating VDBE code that evaluates expressions in SQLite. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: ds@1952: /* ds@1952: ** Return the 'affinity' of the expression pExpr if any. ds@1952: ** ds@1952: ** If pExpr is a column, a reference to a column via an 'AS' alias, ds@1952: ** or a sub-select with a column as the return value, then the ds@1952: ** affinity of that column is returned. Otherwise, 0x00 is returned, ds@1952: ** indicating no affinity for the expression. ds@1952: ** ds@1952: ** i.e. the WHERE clause expresssions in the following statements all ds@1952: ** have an affinity: ds@1952: ** ds@1952: ** CREATE TABLE t1(a); ds@1952: ** SELECT * FROM t1 WHERE a; ds@1952: ** SELECT a AS b FROM t1 WHERE b; ds@1952: ** SELECT * FROM t1 WHERE (select a from t1); ds@1952: */ ds@1952: char sqlite3ExprAffinity(Expr *pExpr){ ds@1952: int op = pExpr->op; ds@1952: if( op==TK_SELECT ){ ds@1952: return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_CAST ds@1952: if( op==TK_CAST ){ ds@1952: return sqlite3AffinityType(&pExpr->token); ds@1952: } ds@1952: #endif ds@1952: return pExpr->affinity; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Set the collating sequence for expression pExpr to be the collating ds@1952: ** sequence named by pToken. Return a pointer to the revised expression. ds@1952: ** The collating sequence is marked as "explicit" using the EP_ExpCollate ds@1952: ** flag. An explicit collating sequence will override implicit ds@1952: ** collating sequences. ds@1952: */ ds@1952: Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){ ds@1952: CollSeq *pColl; ds@1952: if( pExpr==0 ) return 0; ds@1952: pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n); ds@1952: if( pColl ){ ds@1952: pExpr->pColl = pColl; ds@1952: pExpr->flags |= EP_ExpCollate; ds@1952: } ds@1952: return pExpr; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the default collation sequence for the expression pExpr. If ds@1952: ** there is no default collation type, return 0. ds@1952: */ ds@1952: CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ ds@1952: CollSeq *pColl = 0; ds@1952: if( pExpr ){ ds@1952: int op; ds@1952: pColl = pExpr->pColl; ds@1952: op = pExpr->op; ds@1952: if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){ ds@1952: return sqlite3ExprCollSeq(pParse, pExpr->pLeft); ds@1952: } ds@1952: } ds@1952: if( sqlite3CheckCollSeq(pParse, pColl) ){ ds@1952: pColl = 0; ds@1952: } ds@1952: return pColl; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** pExpr is an operand of a comparison operator. aff2 is the ds@1952: ** type affinity of the other operand. This routine returns the ds@1952: ** type affinity that should be used for the comparison operator. ds@1952: */ ds@1952: char sqlite3CompareAffinity(Expr *pExpr, char aff2){ ds@1952: char aff1 = sqlite3ExprAffinity(pExpr); ds@1952: if( aff1 && aff2 ){ ds@1952: /* Both sides of the comparison are columns. If one has numeric ds@1952: ** affinity, use that. Otherwise use no affinity. ds@1952: */ ds@1952: if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ ds@1952: return SQLITE_AFF_NUMERIC; ds@1952: }else{ ds@1952: return SQLITE_AFF_NONE; ds@1952: } ds@1952: }else if( !aff1 && !aff2 ){ ds@1952: /* Neither side of the comparison is a column. Compare the ds@1952: ** results directly. ds@1952: */ ds@1952: return SQLITE_AFF_NONE; ds@1952: }else{ ds@1952: /* One side is a column, the other is not. Use the columns affinity. */ ds@1952: assert( aff1==0 || aff2==0 ); ds@1952: return (aff1 + aff2); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** pExpr is a comparison operator. Return the type affinity that should ds@1952: ** be applied to both operands prior to doing the comparison. ds@1952: */ ds@1952: static char comparisonAffinity(Expr *pExpr){ ds@1952: char aff; ds@1952: assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || ds@1952: pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || ds@1952: pExpr->op==TK_NE ); ds@1952: assert( pExpr->pLeft ); ds@1952: aff = sqlite3ExprAffinity(pExpr->pLeft); ds@1952: if( pExpr->pRight ){ ds@1952: aff = sqlite3CompareAffinity(pExpr->pRight, aff); ds@1952: } ds@1952: else if( pExpr->pSelect ){ ds@1952: aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); ds@1952: } ds@1952: else if( !aff ){ ds@1952: aff = SQLITE_AFF_NONE; ds@1952: } ds@1952: return aff; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. ds@1952: ** idx_affinity is the affinity of an indexed column. Return true ds@1952: ** if the index with affinity idx_affinity may be used to implement ds@1952: ** the comparison in pExpr. ds@1952: */ ds@1952: int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ ds@1952: char aff = comparisonAffinity(pExpr); ds@1952: switch( aff ){ ds@1952: case SQLITE_AFF_NONE: ds@1952: return 1; ds@1952: case SQLITE_AFF_TEXT: ds@1952: return idx_affinity==SQLITE_AFF_TEXT; ds@1952: default: ds@1952: return sqlite3IsNumericAffinity(idx_affinity); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the P1 value that should be used for a binary comparison ds@1952: ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. ds@1952: ** If jumpIfNull is true, then set the low byte of the returned ds@1952: ** P1 value to tell the opcode to jump if either expression ds@1952: ** evaluates to NULL. ds@1952: */ ds@1952: static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ ds@1952: char aff = sqlite3ExprAffinity(pExpr2); ds@1952: return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return a pointer to the collation sequence that should be used by ds@1952: ** a binary comparison operator comparing pLeft and pRight. ds@1952: ** ds@1952: ** If the left hand expression has a collating sequence type, then it is ds@1952: ** used. Otherwise the collation sequence for the right hand expression ds@1952: ** is used, or the default (BINARY) if neither expression has a collating ds@1952: ** type. ds@1952: ** ds@1952: ** Argument pRight (but not pLeft) may be a null pointer. In this case, ds@1952: ** it is not considered. ds@1952: */ ds@1952: CollSeq *sqlite3BinaryCompareCollSeq( ds@1952: Parse *pParse, ds@1952: Expr *pLeft, ds@1952: Expr *pRight ds@1952: ){ ds@1952: CollSeq *pColl; ds@1952: assert( pLeft ); ds@1952: if( pLeft->flags & EP_ExpCollate ){ ds@1952: assert( pLeft->pColl ); ds@1952: pColl = pLeft->pColl; ds@1952: }else if( pRight && pRight->flags & EP_ExpCollate ){ ds@1952: assert( pRight->pColl ); ds@1952: pColl = pRight->pColl; ds@1952: }else{ ds@1952: pColl = sqlite3ExprCollSeq(pParse, pLeft); ds@1952: if( !pColl ){ ds@1952: pColl = sqlite3ExprCollSeq(pParse, pRight); ds@1952: } ds@1952: } ds@1952: return pColl; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Generate code for a comparison operator. ds@1952: */ ds@1952: static int codeCompare( ds@1952: Parse *pParse, /* The parsing (and code generating) context */ ds@1952: Expr *pLeft, /* The left operand */ ds@1952: Expr *pRight, /* The right operand */ ds@1952: int opcode, /* The comparison opcode */ ds@1952: int dest, /* Jump here if true. */ ds@1952: int jumpIfNull /* If true, jump if either operand is NULL */ ds@1952: ){ ds@1952: int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); ds@1952: CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); ds@1952: return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Construct a new expression node and return a pointer to it. Memory ds@1952: ** for this node is obtained from sqlite3_malloc(). The calling function ds@1952: ** is responsible for making sure the node eventually gets freed. ds@1952: */ ds@1952: Expr *sqlite3Expr( ds@1952: sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ ds@1952: int op, /* Expression opcode */ ds@1952: Expr *pLeft, /* Left operand */ ds@1952: Expr *pRight, /* Right operand */ ds@1952: const Token *pToken /* Argument token */ ds@1952: ){ ds@1952: Expr *pNew; ds@1952: pNew = sqlite3DbMallocZero(db, sizeof(Expr)); ds@1952: if( pNew==0 ){ ds@1952: /* When malloc fails, delete pLeft and pRight. Expressions passed to ds@1952: ** this function must always be allocated with sqlite3Expr() for this ds@1952: ** reason. ds@1952: */ ds@1952: sqlite3ExprDelete(pLeft); ds@1952: sqlite3ExprDelete(pRight); ds@1952: return 0; ds@1952: } ds@1952: pNew->op = op; ds@1952: pNew->pLeft = pLeft; ds@1952: pNew->pRight = pRight; ds@1952: pNew->iAgg = -1; ds@1952: if( pToken ){ ds@1952: assert( pToken->dyn==0 ); ds@1952: pNew->span = pNew->token = *pToken; ds@1952: }else if( pLeft ){ ds@1952: if( pRight ){ ds@1952: sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); ds@1952: if( pRight->flags & EP_ExpCollate ){ ds@1952: pNew->flags |= EP_ExpCollate; ds@1952: pNew->pColl = pRight->pColl; ds@1952: } ds@1952: } ds@1952: if( pLeft->flags & EP_ExpCollate ){ ds@1952: pNew->flags |= EP_ExpCollate; ds@1952: pNew->pColl = pLeft->pColl; ds@1952: } ds@1952: } ds@1952: ds@1952: sqlite3ExprSetHeight(pNew); ds@1952: return pNew; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Works like sqlite3Expr() except that it takes an extra Parse* ds@1952: ** argument and notifies the associated connection object if malloc fails. ds@1952: */ ds@1952: Expr *sqlite3PExpr( ds@1952: Parse *pParse, /* Parsing context */ ds@1952: int op, /* Expression opcode */ ds@1952: Expr *pLeft, /* Left operand */ ds@1952: Expr *pRight, /* Right operand */ ds@1952: const Token *pToken /* Argument token */ ds@1952: ){ ds@1952: return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** When doing a nested parse, you can include terms in an expression ds@1952: ** that look like this: #0 #1 #2 ... These terms refer to elements ds@1952: ** on the stack. "#0" means the top of the stack. ds@1952: ** "#1" means the next down on the stack. And so forth. ds@1952: ** ds@1952: ** This routine is called by the parser to deal with on of those terms. ds@1952: ** It immediately generates code to store the value in a memory location. ds@1952: ** The returns an expression that will code to extract the value from ds@1952: ** that memory location as needed. ds@1952: */ ds@1952: Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ ds@1952: Vdbe *v = pParse->pVdbe; ds@1952: Expr *p; ds@1952: int depth; ds@1952: if( pParse->nested==0 ){ ds@1952: sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); ds@1952: return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); ds@1952: } ds@1952: if( v==0 ) return 0; ds@1952: p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken); ds@1952: if( p==0 ){ ds@1952: return 0; /* Malloc failed */ ds@1952: } ds@1952: depth = atoi((char*)&pToken->z[1]); ds@1952: p->iTable = pParse->nMem++; ds@1952: sqlite3VdbeAddOp(v, OP_Dup, depth, 0); ds@1952: sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1); ds@1952: return p; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Join two expressions using an AND operator. If either expression is ds@1952: ** NULL, then just return the other expression. ds@1952: */ ds@1952: Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ ds@1952: if( pLeft==0 ){ ds@1952: return pRight; ds@1952: }else if( pRight==0 ){ ds@1952: return pLeft; ds@1952: }else{ ds@1952: return sqlite3Expr(db, TK_AND, pLeft, pRight, 0); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Set the Expr.span field of the given expression to span all ds@1952: ** text between the two given tokens. ds@1952: */ ds@1952: void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ ds@1952: assert( pRight!=0 ); ds@1952: assert( pLeft!=0 ); ds@1952: if( pExpr && pRight->z && pLeft->z ){ ds@1952: assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); ds@1952: if( pLeft->dyn==0 && pRight->dyn==0 ){ ds@1952: pExpr->span.z = pLeft->z; ds@1952: pExpr->span.n = pRight->n + (pRight->z - pLeft->z); ds@1952: }else{ ds@1952: pExpr->span.z = 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Construct a new expression node for a function with multiple ds@1952: ** arguments. ds@1952: */ ds@1952: Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ ds@1952: Expr *pNew; ds@1952: assert( pToken ); ds@1952: pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) ); ds@1952: if( pNew==0 ){ ds@1952: sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */ ds@1952: return 0; ds@1952: } ds@1952: pNew->op = TK_FUNCTION; ds@1952: pNew->pList = pList; ds@1952: assert( pToken->dyn==0 ); ds@1952: pNew->token = *pToken; ds@1952: pNew->span = pNew->token; ds@1952: ds@1952: sqlite3ExprSetHeight(pNew); ds@1952: return pNew; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Assign a variable number to an expression that encodes a wildcard ds@1952: ** in the original SQL statement. ds@1952: ** ds@1952: ** Wildcards consisting of a single "?" are assigned the next sequential ds@1952: ** variable number. ds@1952: ** ds@1952: ** Wildcards of the form "?nnn" are assigned the number "nnn". We make ds@1952: ** sure "nnn" is not too be to avoid a denial of service attack when ds@1952: ** the SQL statement comes from an external source. ds@1952: ** ds@1952: ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number ds@1952: ** as the previous instance of the same wildcard. Or if this is the first ds@1952: ** instance of the wildcard, the next sequenial variable number is ds@1952: ** assigned. ds@1952: */ ds@1952: void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ ds@1952: Token *pToken; ds@1952: sqlite3 *db = pParse->db; ds@1952: ds@1952: if( pExpr==0 ) return; ds@1952: pToken = &pExpr->token; ds@1952: assert( pToken->n>=1 ); ds@1952: assert( pToken->z!=0 ); ds@1952: assert( pToken->z[0]!=0 ); ds@1952: if( pToken->n==1 ){ ds@1952: /* Wildcard of the form "?". Assign the next variable number */ ds@1952: pExpr->iTable = ++pParse->nVar; ds@1952: }else if( pToken->z[0]=='?' ){ ds@1952: /* Wildcard of the form "?nnn". Convert "nnn" to an integer and ds@1952: ** use it as the variable number */ ds@1952: int i; ds@1952: pExpr->iTable = i = atoi((char*)&pToken->z[1]); ds@1952: if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){ ds@1952: sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", ds@1952: SQLITE_MAX_VARIABLE_NUMBER); ds@1952: } ds@1952: if( i>pParse->nVar ){ ds@1952: pParse->nVar = i; ds@1952: } ds@1952: }else{ ds@1952: /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable ds@1952: ** number as the prior appearance of the same name, or if the name ds@1952: ** has never appeared before, reuse the same variable number ds@1952: */ ds@1952: int i, n; ds@1952: n = pToken->n; ds@1952: for(i=0; inVarExpr; i++){ ds@1952: Expr *pE; ds@1952: if( (pE = pParse->apVarExpr[i])!=0 ds@1952: && pE->token.n==n ds@1952: && memcmp(pE->token.z, pToken->z, n)==0 ){ ds@1952: pExpr->iTable = pE->iTable; ds@1952: break; ds@1952: } ds@1952: } ds@1952: if( i>=pParse->nVarExpr ){ ds@1952: pExpr->iTable = ++pParse->nVar; ds@1952: if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ ds@1952: pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; ds@1952: pParse->apVarExpr = ds@1952: sqlite3DbReallocOrFree( ds@1952: db, ds@1952: pParse->apVarExpr, ds@1952: pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) ds@1952: ); ds@1952: } ds@1952: if( !db->mallocFailed ){ ds@1952: assert( pParse->apVarExpr!=0 ); ds@1952: pParse->apVarExpr[pParse->nVarExpr++] = pExpr; ds@1952: } ds@1952: } ds@1952: } ds@1952: if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){ ds@1952: sqlite3ErrorMsg(pParse, "too many SQL variables"); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Recursively delete an expression tree. ds@1952: */ ds@1952: void sqlite3ExprDelete(Expr *p){ ds@1952: if( p==0 ) return; ds@1952: if( p->span.dyn ) sqlite3_free((char*)p->span.z); ds@1952: if( p->token.dyn ) sqlite3_free((char*)p->token.z); ds@1952: sqlite3ExprDelete(p->pLeft); ds@1952: sqlite3ExprDelete(p->pRight); ds@1952: sqlite3ExprListDelete(p->pList); ds@1952: sqlite3SelectDelete(p->pSelect); ds@1952: sqlite3_free(p); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The Expr.token field might be a string literal that is quoted. ds@1952: ** If so, remove the quotation marks. ds@1952: */ ds@1952: void sqlite3DequoteExpr(sqlite3 *db, Expr *p){ ds@1952: if( ExprHasAnyProperty(p, EP_Dequoted) ){ ds@1952: return; ds@1952: } ds@1952: ExprSetProperty(p, EP_Dequoted); ds@1952: if( p->token.dyn==0 ){ ds@1952: sqlite3TokenCopy(db, &p->token, &p->token); ds@1952: } ds@1952: sqlite3Dequote((char*)p->token.z); ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** The following group of routines make deep copies of expressions, ds@1952: ** expression lists, ID lists, and select statements. The copies can ds@1952: ** be deleted (by being passed to their respective ...Delete() routines) ds@1952: ** without effecting the originals. ds@1952: ** ds@1952: ** The expression list, ID, and source lists return by sqlite3ExprListDup(), ds@1952: ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded ds@1952: ** by subsequent calls to sqlite*ListAppend() routines. ds@1952: ** ds@1952: ** Any tables that the SrcList might point to are not duplicated. ds@1952: */ ds@1952: Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){ ds@1952: Expr *pNew; ds@1952: if( p==0 ) return 0; ds@1952: pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); ds@1952: if( pNew==0 ) return 0; ds@1952: memcpy(pNew, p, sizeof(*pNew)); ds@1952: if( p->token.z!=0 ){ ds@1952: pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n); ds@1952: pNew->token.dyn = 1; ds@1952: }else{ ds@1952: assert( pNew->token.z==0 ); ds@1952: } ds@1952: pNew->span.z = 0; ds@1952: pNew->pLeft = sqlite3ExprDup(db, p->pLeft); ds@1952: pNew->pRight = sqlite3ExprDup(db, p->pRight); ds@1952: pNew->pList = sqlite3ExprListDup(db, p->pList); ds@1952: pNew->pSelect = sqlite3SelectDup(db, p->pSelect); ds@1952: return pNew; ds@1952: } ds@1952: void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){ ds@1952: if( pTo->dyn ) sqlite3_free((char*)pTo->z); ds@1952: if( pFrom->z ){ ds@1952: pTo->n = pFrom->n; ds@1952: pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n); ds@1952: pTo->dyn = 1; ds@1952: }else{ ds@1952: pTo->z = 0; ds@1952: } ds@1952: } ds@1952: ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){ ds@1952: ExprList *pNew; ds@1952: struct ExprList_item *pItem, *pOldItem; ds@1952: int i; ds@1952: if( p==0 ) return 0; ds@1952: pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); ds@1952: if( pNew==0 ) return 0; ds@1952: pNew->iECursor = 0; ds@1952: pNew->nExpr = pNew->nAlloc = p->nExpr; ds@1952: pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) ); ds@1952: if( pItem==0 ){ ds@1952: sqlite3_free(pNew); ds@1952: return 0; ds@1952: } ds@1952: pOldItem = p->a; ds@1952: for(i=0; inExpr; i++, pItem++, pOldItem++){ ds@1952: Expr *pNewExpr, *pOldExpr; ds@1952: pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr); ds@1952: if( pOldExpr->span.z!=0 && pNewExpr ){ ds@1952: /* Always make a copy of the span for top-level expressions in the ds@1952: ** expression list. The logic in SELECT processing that determines ds@1952: ** the names of columns in the result set needs this information */ ds@1952: sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span); ds@1952: } ds@1952: assert( pNewExpr==0 || pNewExpr->span.z!=0 ds@1952: || pOldExpr->span.z==0 ds@1952: || db->mallocFailed ); ds@1952: pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); ds@1952: pItem->sortOrder = pOldItem->sortOrder; ds@1952: pItem->isAgg = pOldItem->isAgg; ds@1952: pItem->done = 0; ds@1952: } ds@1952: return pNew; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** If cursors, triggers, views and subqueries are all omitted from ds@1952: ** the build, then none of the following routines, except for ds@1952: ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes ds@1952: ** called with a NULL argument. ds@1952: */ ds@1952: #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ ds@1952: || !defined(SQLITE_OMIT_SUBQUERY) ds@1952: SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){ ds@1952: SrcList *pNew; ds@1952: int i; ds@1952: int nByte; ds@1952: if( p==0 ) return 0; ds@1952: nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); ds@1952: pNew = sqlite3DbMallocRaw(db, nByte ); ds@1952: if( pNew==0 ) return 0; ds@1952: pNew->nSrc = pNew->nAlloc = p->nSrc; ds@1952: for(i=0; inSrc; i++){ ds@1952: struct SrcList_item *pNewItem = &pNew->a[i]; ds@1952: struct SrcList_item *pOldItem = &p->a[i]; ds@1952: Table *pTab; ds@1952: pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); ds@1952: pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); ds@1952: pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); ds@1952: pNewItem->jointype = pOldItem->jointype; ds@1952: pNewItem->iCursor = pOldItem->iCursor; ds@1952: pNewItem->isPopulated = pOldItem->isPopulated; ds@1952: pTab = pNewItem->pTab = pOldItem->pTab; ds@1952: if( pTab ){ ds@1952: pTab->nRef++; ds@1952: } ds@1952: pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect); ds@1952: pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn); ds@1952: pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); ds@1952: pNewItem->colUsed = pOldItem->colUsed; ds@1952: } ds@1952: return pNew; ds@1952: } ds@1952: IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ ds@1952: IdList *pNew; ds@1952: int i; ds@1952: if( p==0 ) return 0; ds@1952: pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); ds@1952: if( pNew==0 ) return 0; ds@1952: pNew->nId = pNew->nAlloc = p->nId; ds@1952: pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); ds@1952: if( pNew->a==0 ){ ds@1952: sqlite3_free(pNew); ds@1952: return 0; ds@1952: } ds@1952: for(i=0; inId; i++){ ds@1952: struct IdList_item *pNewItem = &pNew->a[i]; ds@1952: struct IdList_item *pOldItem = &p->a[i]; ds@1952: pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); ds@1952: pNewItem->idx = pOldItem->idx; ds@1952: } ds@1952: return pNew; ds@1952: } ds@1952: Select *sqlite3SelectDup(sqlite3 *db, Select *p){ ds@1952: Select *pNew; ds@1952: if( p==0 ) return 0; ds@1952: pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); ds@1952: if( pNew==0 ) return 0; ds@1952: pNew->isDistinct = p->isDistinct; ds@1952: pNew->pEList = sqlite3ExprListDup(db, p->pEList); ds@1952: pNew->pSrc = sqlite3SrcListDup(db, p->pSrc); ds@1952: pNew->pWhere = sqlite3ExprDup(db, p->pWhere); ds@1952: pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy); ds@1952: pNew->pHaving = sqlite3ExprDup(db, p->pHaving); ds@1952: pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy); ds@1952: pNew->op = p->op; ds@1952: pNew->pPrior = sqlite3SelectDup(db, p->pPrior); ds@1952: pNew->pLimit = sqlite3ExprDup(db, p->pLimit); ds@1952: pNew->pOffset = sqlite3ExprDup(db, p->pOffset); ds@1952: pNew->iLimit = -1; ds@1952: pNew->iOffset = -1; ds@1952: pNew->isResolved = p->isResolved; ds@1952: pNew->isAgg = p->isAgg; ds@1952: pNew->usesEphm = 0; ds@1952: pNew->disallowOrderBy = 0; ds@1952: pNew->pRightmost = 0; ds@1952: pNew->addrOpenEphm[0] = -1; ds@1952: pNew->addrOpenEphm[1] = -1; ds@1952: pNew->addrOpenEphm[2] = -1; ds@1952: return pNew; ds@1952: } ds@1952: #else ds@1952: Select *sqlite3SelectDup(sqlite3 *db, Select *p){ ds@1952: assert( p==0 ); ds@1952: return 0; ds@1952: } ds@1952: #endif ds@1952: ds@1952: ds@1952: /* ds@1952: ** Add a new element to the end of an expression list. If pList is ds@1952: ** initially NULL, then create a new expression list. ds@1952: */ ds@1952: ExprList *sqlite3ExprListAppend( ds@1952: Parse *pParse, /* Parsing context */ ds@1952: ExprList *pList, /* List to which to append. Might be NULL */ ds@1952: Expr *pExpr, /* Expression to be appended */ ds@1952: Token *pName /* AS keyword for the expression */ ds@1952: ){ ds@1952: sqlite3 *db = pParse->db; ds@1952: if( pList==0 ){ ds@1952: pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); ds@1952: if( pList==0 ){ ds@1952: goto no_mem; ds@1952: } ds@1952: assert( pList->nAlloc==0 ); ds@1952: } ds@1952: if( pList->nAlloc<=pList->nExpr ){ ds@1952: struct ExprList_item *a; ds@1952: int n = pList->nAlloc*2 + 4; ds@1952: a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0])); ds@1952: if( a==0 ){ ds@1952: goto no_mem; ds@1952: } ds@1952: pList->a = a; ds@1952: pList->nAlloc = n; ds@1952: } ds@1952: assert( pList->a!=0 ); ds@1952: if( pExpr || pName ){ ds@1952: struct ExprList_item *pItem = &pList->a[pList->nExpr++]; ds@1952: memset(pItem, 0, sizeof(*pItem)); ds@1952: pItem->zName = sqlite3NameFromToken(db, pName); ds@1952: pItem->pExpr = pExpr; ds@1952: } ds@1952: return pList; ds@1952: ds@1952: no_mem: ds@1952: /* Avoid leaking memory if malloc has failed. */ ds@1952: sqlite3ExprDelete(pExpr); ds@1952: sqlite3ExprListDelete(pList); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** If the expression list pEList contains more than iLimit elements, ds@1952: ** leave an error message in pParse. ds@1952: */ ds@1952: void sqlite3ExprListCheckLength( ds@1952: Parse *pParse, ds@1952: ExprList *pEList, ds@1952: int iLimit, ds@1952: const char *zObject ds@1952: ){ ds@1952: if( pEList && pEList->nExpr>iLimit ){ ds@1952: sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); ds@1952: } ds@1952: } ds@1952: ds@1952: ds@1952: #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0 ds@1952: /* The following three functions, heightOfExpr(), heightOfExprList() ds@1952: ** and heightOfSelect(), are used to determine the maximum height ds@1952: ** of any expression tree referenced by the structure passed as the ds@1952: ** first argument. ds@1952: ** ds@1952: ** If this maximum height is greater than the current value pointed ds@1952: ** to by pnHeight, the second parameter, then set *pnHeight to that ds@1952: ** value. ds@1952: */ ds@1952: static void heightOfExpr(Expr *p, int *pnHeight){ ds@1952: if( p ){ ds@1952: if( p->nHeight>*pnHeight ){ ds@1952: *pnHeight = p->nHeight; ds@1952: } ds@1952: } ds@1952: } ds@1952: static void heightOfExprList(ExprList *p, int *pnHeight){ ds@1952: if( p ){ ds@1952: int i; ds@1952: for(i=0; inExpr; i++){ ds@1952: heightOfExpr(p->a[i].pExpr, pnHeight); ds@1952: } ds@1952: } ds@1952: } ds@1952: static void heightOfSelect(Select *p, int *pnHeight){ ds@1952: if( p ){ ds@1952: heightOfExpr(p->pWhere, pnHeight); ds@1952: heightOfExpr(p->pHaving, pnHeight); ds@1952: heightOfExpr(p->pLimit, pnHeight); ds@1952: heightOfExpr(p->pOffset, pnHeight); ds@1952: heightOfExprList(p->pEList, pnHeight); ds@1952: heightOfExprList(p->pGroupBy, pnHeight); ds@1952: heightOfExprList(p->pOrderBy, pnHeight); ds@1952: heightOfSelect(p->pPrior, pnHeight); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Set the Expr.nHeight variable in the structure passed as an ds@1952: ** argument. An expression with no children, Expr.pList or ds@1952: ** Expr.pSelect member has a height of 1. Any other expression ds@1952: ** has a height equal to the maximum height of any other ds@1952: ** referenced Expr plus one. ds@1952: */ ds@1952: void sqlite3ExprSetHeight(Expr *p){ ds@1952: int nHeight = 0; ds@1952: heightOfExpr(p->pLeft, &nHeight); ds@1952: heightOfExpr(p->pRight, &nHeight); ds@1952: heightOfExprList(p->pList, &nHeight); ds@1952: heightOfSelect(p->pSelect, &nHeight); ds@1952: p->nHeight = nHeight + 1; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the maximum height of any expression tree referenced ds@1952: ** by the select statement passed as an argument. ds@1952: */ ds@1952: int sqlite3SelectExprHeight(Select *p){ ds@1952: int nHeight = 0; ds@1952: heightOfSelect(p, &nHeight); ds@1952: return nHeight; ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Delete an entire expression list. ds@1952: */ ds@1952: void sqlite3ExprListDelete(ExprList *pList){ ds@1952: int i; ds@1952: struct ExprList_item *pItem; ds@1952: if( pList==0 ) return; ds@1952: assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); ds@1952: assert( pList->nExpr<=pList->nAlloc ); ds@1952: for(pItem=pList->a, i=0; inExpr; i++, pItem++){ ds@1952: sqlite3ExprDelete(pItem->pExpr); ds@1952: sqlite3_free(pItem->zName); ds@1952: } ds@1952: sqlite3_free(pList->a); ds@1952: sqlite3_free(pList); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Walk an expression tree. Call xFunc for each node visited. ds@1952: ** ds@1952: ** The return value from xFunc determines whether the tree walk continues. ds@1952: ** 0 means continue walking the tree. 1 means do not walk children ds@1952: ** of the current node but continue with siblings. 2 means abandon ds@1952: ** the tree walk completely. ds@1952: ** ds@1952: ** The return value from this routine is 1 to abandon the tree walk ds@1952: ** and 0 to continue. ds@1952: ** ds@1952: ** NOTICE: This routine does *not* descend into subqueries. ds@1952: */ ds@1952: static int walkExprList(ExprList *, int (*)(void *, Expr*), void *); ds@1952: static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ ds@1952: int rc; ds@1952: if( pExpr==0 ) return 0; ds@1952: rc = (*xFunc)(pArg, pExpr); ds@1952: if( rc==0 ){ ds@1952: if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; ds@1952: if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; ds@1952: if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1; ds@1952: } ds@1952: return rc>1; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Call walkExprTree() for every expression in list p. ds@1952: */ ds@1952: static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){ ds@1952: int i; ds@1952: struct ExprList_item *pItem; ds@1952: if( !p ) return 0; ds@1952: for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ ds@1952: if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Call walkExprTree() for every expression in Select p, not including ds@1952: ** expressions that are part of sub-selects in any FROM clause or the LIMIT ds@1952: ** or OFFSET expressions.. ds@1952: */ ds@1952: static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){ ds@1952: walkExprList(p->pEList, xFunc, pArg); ds@1952: walkExprTree(p->pWhere, xFunc, pArg); ds@1952: walkExprList(p->pGroupBy, xFunc, pArg); ds@1952: walkExprTree(p->pHaving, xFunc, pArg); ds@1952: walkExprList(p->pOrderBy, xFunc, pArg); ds@1952: if( p->pPrior ){ ds@1952: walkSelectExpr(p->pPrior, xFunc, pArg); ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** This routine is designed as an xFunc for walkExprTree(). ds@1952: ** ds@1952: ** pArg is really a pointer to an integer. If we can tell by looking ds@1952: ** at pExpr that the expression that contains pExpr is not a constant ds@1952: ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. ds@1952: ** If pExpr does does not disqualify the expression from being a constant ds@1952: ** then do nothing. ds@1952: ** ds@1952: ** After walking the whole tree, if no nodes are found that disqualify ds@1952: ** the expression as constant, then we assume the whole expression ds@1952: ** is constant. See sqlite3ExprIsConstant() for additional information. ds@1952: */ ds@1952: static int exprNodeIsConstant(void *pArg, Expr *pExpr){ ds@1952: int *pN = (int*)pArg; ds@1952: ds@1952: /* If *pArg is 3 then any term of the expression that comes from ds@1952: ** the ON or USING clauses of a join disqualifies the expression ds@1952: ** from being considered constant. */ ds@1952: if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){ ds@1952: *pN = 0; ds@1952: return 2; ds@1952: } ds@1952: ds@1952: switch( pExpr->op ){ ds@1952: /* Consider functions to be constant if all their arguments are constant ds@1952: ** and *pArg==2 */ ds@1952: case TK_FUNCTION: ds@1952: if( (*pN)==2 ) return 0; ds@1952: /* Fall through */ ds@1952: case TK_ID: ds@1952: case TK_COLUMN: ds@1952: case TK_DOT: ds@1952: case TK_AGG_FUNCTION: ds@1952: case TK_AGG_COLUMN: ds@1952: #ifndef SQLITE_OMIT_SUBQUERY ds@1952: case TK_SELECT: ds@1952: case TK_EXISTS: ds@1952: #endif ds@1952: *pN = 0; ds@1952: return 2; ds@1952: case TK_IN: ds@1952: if( pExpr->pSelect ){ ds@1952: *pN = 0; ds@1952: return 2; ds@1952: } ds@1952: default: ds@1952: return 0; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Walk an expression tree. Return 1 if the expression is constant ds@1952: ** and 0 if it involves variables or function calls. ds@1952: ** ds@1952: ** For the purposes of this function, a double-quoted string (ex: "abc") ds@1952: ** is considered a variable but a single-quoted string (ex: 'abc') is ds@1952: ** a constant. ds@1952: */ ds@1952: int sqlite3ExprIsConstant(Expr *p){ ds@1952: int isConst = 1; ds@1952: walkExprTree(p, exprNodeIsConstant, &isConst); ds@1952: return isConst; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Walk an expression tree. Return 1 if the expression is constant ds@1952: ** that does no originate from the ON or USING clauses of a join. ds@1952: ** Return 0 if it involves variables or function calls or terms from ds@1952: ** an ON or USING clause. ds@1952: */ ds@1952: int sqlite3ExprIsConstantNotJoin(Expr *p){ ds@1952: int isConst = 3; ds@1952: walkExprTree(p, exprNodeIsConstant, &isConst); ds@1952: return isConst!=0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Walk an expression tree. Return 1 if the expression is constant ds@1952: ** or a function call with constant arguments. Return and 0 if there ds@1952: ** are any variables. ds@1952: ** ds@1952: ** For the purposes of this function, a double-quoted string (ex: "abc") ds@1952: ** is considered a variable but a single-quoted string (ex: 'abc') is ds@1952: ** a constant. ds@1952: */ ds@1952: int sqlite3ExprIsConstantOrFunction(Expr *p){ ds@1952: int isConst = 2; ds@1952: walkExprTree(p, exprNodeIsConstant, &isConst); ds@1952: return isConst!=0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** If the expression p codes a constant integer that is small enough ds@1952: ** to fit in a 32-bit integer, return 1 and put the value of the integer ds@1952: ** in *pValue. If the expression is not an integer or if it is too big ds@1952: ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. ds@1952: */ ds@1952: int sqlite3ExprIsInteger(Expr *p, int *pValue){ ds@1952: switch( p->op ){ ds@1952: case TK_INTEGER: { ds@1952: if( sqlite3GetInt32((char*)p->token.z, pValue) ){ ds@1952: return 1; ds@1952: } ds@1952: break; ds@1952: } ds@1952: case TK_UPLUS: { ds@1952: return sqlite3ExprIsInteger(p->pLeft, pValue); ds@1952: } ds@1952: case TK_UMINUS: { ds@1952: int v; ds@1952: if( sqlite3ExprIsInteger(p->pLeft, &v) ){ ds@1952: *pValue = -v; ds@1952: return 1; ds@1952: } ds@1952: break; ds@1952: } ds@1952: default: break; ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return TRUE if the given string is a row-id column name. ds@1952: */ ds@1952: int sqlite3IsRowid(const char *z){ ds@1952: if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; ds@1952: if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; ds@1952: if( sqlite3StrICmp(z, "OID")==0 ) return 1; ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up ds@1952: ** that name in the set of source tables in pSrcList and make the pExpr ds@1952: ** expression node refer back to that source column. The following changes ds@1952: ** are made to pExpr: ds@1952: ** ds@1952: ** pExpr->iDb Set the index in db->aDb[] of the database holding ds@1952: ** the table. ds@1952: ** pExpr->iTable Set to the cursor number for the table obtained ds@1952: ** from pSrcList. ds@1952: ** pExpr->iColumn Set to the column number within the table. ds@1952: ** pExpr->op Set to TK_COLUMN. ds@1952: ** pExpr->pLeft Any expression this points to is deleted ds@1952: ** pExpr->pRight Any expression this points to is deleted. ds@1952: ** ds@1952: ** The pDbToken is the name of the database (the "X"). This value may be ds@1952: ** NULL meaning that name is of the form Y.Z or Z. Any available database ds@1952: ** can be used. The pTableToken is the name of the table (the "Y"). This ds@1952: ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it ds@1952: ** means that the form of the name is Z and that columns from any table ds@1952: ** can be used. ds@1952: ** ds@1952: ** If the name cannot be resolved unambiguously, leave an error message ds@1952: ** in pParse and return non-zero. Return zero on success. ds@1952: */ ds@1952: static int lookupName( ds@1952: Parse *pParse, /* The parsing context */ ds@1952: Token *pDbToken, /* Name of the database containing table, or NULL */ ds@1952: Token *pTableToken, /* Name of table containing column, or NULL */ ds@1952: Token *pColumnToken, /* Name of the column. */ ds@1952: NameContext *pNC, /* The name context used to resolve the name */ ds@1952: Expr *pExpr /* Make this EXPR node point to the selected column */ ds@1952: ){ ds@1952: char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ ds@1952: char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ ds@1952: char *zCol = 0; /* Name of the column. The "Z" */ ds@1952: int i, j; /* Loop counters */ ds@1952: int cnt = 0; /* Number of matching column names */ ds@1952: int cntTab = 0; /* Number of matching table names */ ds@1952: sqlite3 *db = pParse->db; /* The database */ ds@1952: struct SrcList_item *pItem; /* Use for looping over pSrcList items */ ds@1952: struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ ds@1952: NameContext *pTopNC = pNC; /* First namecontext in the list */ ds@1952: Schema *pSchema = 0; /* Schema of the expression */ ds@1952: ds@1952: assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ ds@1952: zDb = sqlite3NameFromToken(db, pDbToken); ds@1952: zTab = sqlite3NameFromToken(db, pTableToken); ds@1952: zCol = sqlite3NameFromToken(db, pColumnToken); ds@1952: if( db->mallocFailed ){ ds@1952: goto lookupname_end; ds@1952: } ds@1952: ds@1952: pExpr->iTable = -1; ds@1952: while( pNC && cnt==0 ){ ds@1952: ExprList *pEList; ds@1952: SrcList *pSrcList = pNC->pSrcList; ds@1952: ds@1952: if( pSrcList ){ ds@1952: for(i=0, pItem=pSrcList->a; inSrc; i++, pItem++){ ds@1952: Table *pTab; ds@1952: int iDb; ds@1952: Column *pCol; ds@1952: ds@1952: pTab = pItem->pTab; ds@1952: assert( pTab!=0 ); ds@1952: iDb = sqlite3SchemaToIndex(db, pTab->pSchema); ds@1952: assert( pTab->nCol>0 ); ds@1952: if( zTab ){ ds@1952: if( pItem->zAlias ){ ds@1952: char *zTabName = pItem->zAlias; ds@1952: if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; ds@1952: }else{ ds@1952: char *zTabName = pTab->zName; ds@1952: if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; ds@1952: if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){ ds@1952: continue; ds@1952: } ds@1952: } ds@1952: } ds@1952: if( 0==(cntTab++) ){ ds@1952: pExpr->iTable = pItem->iCursor; ds@1952: pSchema = pTab->pSchema; ds@1952: pMatch = pItem; ds@1952: } ds@1952: for(j=0, pCol=pTab->aCol; jnCol; j++, pCol++){ ds@1952: if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ ds@1952: const char *zColl = pTab->aCol[j].zColl; ds@1952: IdList *pUsing; ds@1952: cnt++; ds@1952: pExpr->iTable = pItem->iCursor; ds@1952: pMatch = pItem; ds@1952: pSchema = pTab->pSchema; ds@1952: /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ ds@1952: pExpr->iColumn = j==pTab->iPKey ? -1 : j; ds@1952: pExpr->affinity = pTab->aCol[j].affinity; ds@1952: if( (pExpr->flags & EP_ExpCollate)==0 ){ ds@1952: pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); ds@1952: } ds@1952: if( inSrc-1 ){ ds@1952: if( pItem[1].jointype & JT_NATURAL ){ ds@1952: /* If this match occurred in the left table of a natural join, ds@1952: ** then skip the right table to avoid a duplicate match */ ds@1952: pItem++; ds@1952: i++; ds@1952: }else if( (pUsing = pItem[1].pUsing)!=0 ){ ds@1952: /* If this match occurs on a column that is in the USING clause ds@1952: ** of a join, skip the search of the right table of the join ds@1952: ** to avoid a duplicate match there. */ ds@1952: int k; ds@1952: for(k=0; knId; k++){ ds@1952: if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ ds@1952: pItem++; ds@1952: i++; ds@1952: break; ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: break; ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_TRIGGER ds@1952: /* If we have not already resolved the name, then maybe ds@1952: ** it is a new.* or old.* trigger argument reference ds@1952: */ ds@1952: if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ ds@1952: TriggerStack *pTriggerStack = pParse->trigStack; ds@1952: Table *pTab = 0; ds@1952: if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ ds@1952: pExpr->iTable = pTriggerStack->newIdx; ds@1952: assert( pTriggerStack->pTab ); ds@1952: pTab = pTriggerStack->pTab; ds@1952: }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ ds@1952: pExpr->iTable = pTriggerStack->oldIdx; ds@1952: assert( pTriggerStack->pTab ); ds@1952: pTab = pTriggerStack->pTab; ds@1952: } ds@1952: ds@1952: if( pTab ){ ds@1952: int iCol; ds@1952: Column *pCol = pTab->aCol; ds@1952: ds@1952: pSchema = pTab->pSchema; ds@1952: cntTab++; ds@1952: for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) { ds@1952: if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ ds@1952: const char *zColl = pTab->aCol[iCol].zColl; ds@1952: cnt++; ds@1952: pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol; ds@1952: pExpr->affinity = pTab->aCol[iCol].affinity; ds@1952: if( (pExpr->flags & EP_ExpCollate)==0 ){ ds@1952: pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); ds@1952: } ds@1952: pExpr->pTab = pTab; ds@1952: break; ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: #endif /* !defined(SQLITE_OMIT_TRIGGER) */ ds@1952: ds@1952: /* ds@1952: ** Perhaps the name is a reference to the ROWID ds@1952: */ ds@1952: if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ ds@1952: cnt = 1; ds@1952: pExpr->iColumn = -1; ds@1952: pExpr->affinity = SQLITE_AFF_INTEGER; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z ds@1952: ** might refer to an result-set alias. This happens, for example, when ds@1952: ** we are resolving names in the WHERE clause of the following command: ds@1952: ** ds@1952: ** SELECT a+b AS x FROM table WHERE x<10; ds@1952: ** ds@1952: ** In cases like this, replace pExpr with a copy of the expression that ds@1952: ** forms the result set entry ("a+b" in the example) and return immediately. ds@1952: ** Note that the expression in the result set should have already been ds@1952: ** resolved by the time the WHERE clause is resolved. ds@1952: */ ds@1952: if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){ ds@1952: for(j=0; jnExpr; j++){ ds@1952: char *zAs = pEList->a[j].zName; ds@1952: if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ ds@1952: Expr *pDup, *pOrig; ds@1952: assert( pExpr->pLeft==0 && pExpr->pRight==0 ); ds@1952: assert( pExpr->pList==0 ); ds@1952: assert( pExpr->pSelect==0 ); ds@1952: pOrig = pEList->a[j].pExpr; ds@1952: if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){ ds@1952: sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); ds@1952: sqlite3_free(zCol); ds@1952: return 2; ds@1952: } ds@1952: pDup = sqlite3ExprDup(db, pOrig); ds@1952: if( pExpr->flags & EP_ExpCollate ){ ds@1952: pDup->pColl = pExpr->pColl; ds@1952: pDup->flags |= EP_ExpCollate; ds@1952: } ds@1952: if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z); ds@1952: if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z); ds@1952: memcpy(pExpr, pDup, sizeof(*pExpr)); ds@1952: sqlite3_free(pDup); ds@1952: cnt = 1; ds@1952: pMatch = 0; ds@1952: assert( zTab==0 && zDb==0 ); ds@1952: goto lookupname_end_2; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* Advance to the next name context. The loop will exit when either ds@1952: ** we have a match (cnt>0) or when we run out of name contexts. ds@1952: */ ds@1952: if( cnt==0 ){ ds@1952: pNC = pNC->pNext; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** If X and Y are NULL (in other words if only the column name Z is ds@1952: ** supplied) and the value of Z is enclosed in double-quotes, then ds@1952: ** Z is a string literal if it doesn't match any column names. In that ds@1952: ** case, we need to return right away and not make any changes to ds@1952: ** pExpr. ds@1952: ** ds@1952: ** Because no reference was made to outer contexts, the pNC->nRef ds@1952: ** fields are not changed in any context. ds@1952: */ ds@1952: if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ ds@1952: sqlite3_free(zCol); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** cnt==0 means there was not match. cnt>1 means there were two or ds@1952: ** more matches. Either way, we have an error. ds@1952: */ ds@1952: if( cnt!=1 ){ ds@1952: char *z = 0; ds@1952: char *zErr; ds@1952: zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; ds@1952: if( zDb ){ ds@1952: sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0); ds@1952: }else if( zTab ){ ds@1952: sqlite3SetString(&z, zTab, ".", zCol, (char*)0); ds@1952: }else{ ds@1952: z = sqlite3StrDup(zCol); ds@1952: } ds@1952: if( z ){ ds@1952: sqlite3ErrorMsg(pParse, zErr, z); ds@1952: sqlite3_free(z); ds@1952: pTopNC->nErr++; ds@1952: }else{ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: } ds@1952: ds@1952: /* If a column from a table in pSrcList is referenced, then record ds@1952: ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes ds@1952: ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the ds@1952: ** column number is greater than the number of bits in the bitmask ds@1952: ** then set the high-order bit of the bitmask. ds@1952: */ ds@1952: if( pExpr->iColumn>=0 && pMatch!=0 ){ ds@1952: int n = pExpr->iColumn; ds@1952: if( n>=sizeof(Bitmask)*8 ){ ds@1952: n = sizeof(Bitmask)*8-1; ds@1952: } ds@1952: assert( pMatch->iCursor==pExpr->iTable ); ds@1952: pMatch->colUsed |= ((Bitmask)1)<pLeft); ds@1952: pExpr->pLeft = 0; ds@1952: sqlite3ExprDelete(pExpr->pRight); ds@1952: pExpr->pRight = 0; ds@1952: pExpr->op = TK_COLUMN; ds@1952: lookupname_end_2: ds@1952: sqlite3_free(zCol); ds@1952: if( cnt==1 ){ ds@1952: assert( pNC!=0 ); ds@1952: sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); ds@1952: if( pMatch && !pMatch->pSelect ){ ds@1952: pExpr->pTab = pMatch->pTab; ds@1952: } ds@1952: /* Increment the nRef value on all name contexts from TopNC up to ds@1952: ** the point where the name matched. */ ds@1952: for(;;){ ds@1952: assert( pTopNC!=0 ); ds@1952: pTopNC->nRef++; ds@1952: if( pTopNC==pNC ) break; ds@1952: pTopNC = pTopNC->pNext; ds@1952: } ds@1952: return 0; ds@1952: } else { ds@1952: return 1; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine is designed as an xFunc for walkExprTree(). ds@1952: ** ds@1952: ** Resolve symbolic names into TK_COLUMN operators for the current ds@1952: ** node in the expression tree. Return 0 to continue the search down ds@1952: ** the tree or 2 to abort the tree walk. ds@1952: ** ds@1952: ** This routine also does error checking and name resolution for ds@1952: ** function names. The operator for aggregate functions is changed ds@1952: ** to TK_AGG_FUNCTION. ds@1952: */ ds@1952: static int nameResolverStep(void *pArg, Expr *pExpr){ ds@1952: NameContext *pNC = (NameContext*)pArg; ds@1952: Parse *pParse; ds@1952: ds@1952: if( pExpr==0 ) return 1; ds@1952: assert( pNC!=0 ); ds@1952: pParse = pNC->pParse; ds@1952: ds@1952: if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; ds@1952: ExprSetProperty(pExpr, EP_Resolved); ds@1952: #ifndef NDEBUG ds@1952: if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ ds@1952: SrcList *pSrcList = pNC->pSrcList; ds@1952: int i; ds@1952: for(i=0; ipSrcList->nSrc; i++){ ds@1952: assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursornTab); ds@1952: } ds@1952: } ds@1952: #endif ds@1952: switch( pExpr->op ){ ds@1952: /* Double-quoted strings (ex: "abc") are used as identifiers if ds@1952: ** possible. Otherwise they remain as strings. Single-quoted ds@1952: ** strings (ex: 'abc') are always string literals. ds@1952: */ ds@1952: case TK_STRING: { ds@1952: if( pExpr->token.z[0]=='\'' ) break; ds@1952: /* Fall thru into the TK_ID case if this is a double-quoted string */ ds@1952: } ds@1952: /* A lone identifier is the name of a column. ds@1952: */ ds@1952: case TK_ID: { ds@1952: lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); ds@1952: return 1; ds@1952: } ds@1952: ds@1952: /* A table name and column name: ID.ID ds@1952: ** Or a database, table and column: ID.ID.ID ds@1952: */ ds@1952: case TK_DOT: { ds@1952: Token *pColumn; ds@1952: Token *pTable; ds@1952: Token *pDb; ds@1952: Expr *pRight; ds@1952: ds@1952: /* if( pSrcList==0 ) break; */ ds@1952: pRight = pExpr->pRight; ds@1952: if( pRight->op==TK_ID ){ ds@1952: pDb = 0; ds@1952: pTable = &pExpr->pLeft->token; ds@1952: pColumn = &pRight->token; ds@1952: }else{ ds@1952: assert( pRight->op==TK_DOT ); ds@1952: pDb = &pExpr->pLeft->token; ds@1952: pTable = &pRight->pLeft->token; ds@1952: pColumn = &pRight->pRight->token; ds@1952: } ds@1952: lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); ds@1952: return 1; ds@1952: } ds@1952: ds@1952: /* Resolve function names ds@1952: */ ds@1952: case TK_CONST_FUNC: ds@1952: case TK_FUNCTION: { ds@1952: ExprList *pList = pExpr->pList; /* The argument list */ ds@1952: int n = pList ? pList->nExpr : 0; /* Number of arguments */ ds@1952: int no_such_func = 0; /* True if no such function exists */ ds@1952: int wrong_num_args = 0; /* True if wrong number of arguments */ ds@1952: int is_agg = 0; /* True if is an aggregate function */ ds@1952: int i; ds@1952: int auth; /* Authorization to use the function */ ds@1952: int nId; /* Number of characters in function name */ ds@1952: const char *zId; /* The function name. */ ds@1952: FuncDef *pDef; /* Information about the function */ ds@1952: int enc = ENC(pParse->db); /* The database encoding */ ds@1952: ds@1952: zId = (char*)pExpr->token.z; ds@1952: nId = pExpr->token.n; ds@1952: pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); ds@1952: if( pDef==0 ){ ds@1952: pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); ds@1952: if( pDef==0 ){ ds@1952: no_such_func = 1; ds@1952: }else{ ds@1952: wrong_num_args = 1; ds@1952: } ds@1952: }else{ ds@1952: is_agg = pDef->xFunc==0; ds@1952: } ds@1952: #ifndef SQLITE_OMIT_AUTHORIZATION ds@1952: if( pDef ){ ds@1952: auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); ds@1952: if( auth!=SQLITE_OK ){ ds@1952: if( auth==SQLITE_DENY ){ ds@1952: sqlite3ErrorMsg(pParse, "not authorized to use function: %s", ds@1952: pDef->zName); ds@1952: pNC->nErr++; ds@1952: } ds@1952: pExpr->op = TK_NULL; ds@1952: return 1; ds@1952: } ds@1952: } ds@1952: #endif ds@1952: if( is_agg && !pNC->allowAgg ){ ds@1952: sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); ds@1952: pNC->nErr++; ds@1952: is_agg = 0; ds@1952: }else if( no_such_func ){ ds@1952: sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); ds@1952: pNC->nErr++; ds@1952: }else if( wrong_num_args ){ ds@1952: sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", ds@1952: nId, zId); ds@1952: pNC->nErr++; ds@1952: } ds@1952: if( is_agg ){ ds@1952: pExpr->op = TK_AGG_FUNCTION; ds@1952: pNC->hasAgg = 1; ds@1952: } ds@1952: if( is_agg ) pNC->allowAgg = 0; ds@1952: for(i=0; pNC->nErr==0 && ia[i].pExpr, nameResolverStep, pNC); ds@1952: } ds@1952: if( is_agg ) pNC->allowAgg = 1; ds@1952: /* FIX ME: Compute pExpr->affinity based on the expected return ds@1952: ** type of the function ds@1952: */ ds@1952: return is_agg; ds@1952: } ds@1952: #ifndef SQLITE_OMIT_SUBQUERY ds@1952: case TK_SELECT: ds@1952: case TK_EXISTS: ds@1952: #endif ds@1952: case TK_IN: { ds@1952: if( pExpr->pSelect ){ ds@1952: int nRef = pNC->nRef; ds@1952: #ifndef SQLITE_OMIT_CHECK ds@1952: if( pNC->isCheck ){ ds@1952: sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints"); ds@1952: } ds@1952: #endif ds@1952: sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); ds@1952: assert( pNC->nRef>=nRef ); ds@1952: if( nRef!=pNC->nRef ){ ds@1952: ExprSetProperty(pExpr, EP_VarSelect); ds@1952: } ds@1952: } ds@1952: break; ds@1952: } ds@1952: #ifndef SQLITE_OMIT_CHECK ds@1952: case TK_VARIABLE: { ds@1952: if( pNC->isCheck ){ ds@1952: sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints"); ds@1952: } ds@1952: break; ds@1952: } ds@1952: #endif ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine walks an expression tree and resolves references to ds@1952: ** table columns. Nodes of the form ID.ID or ID resolve into an ds@1952: ** index to the table in the table list and a column offset. The ds@1952: ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable ds@1952: ** value is changed to the index of the referenced table in pTabList ds@1952: ** plus the "base" value. The base value will ultimately become the ds@1952: ** VDBE cursor number for a cursor that is pointing into the referenced ds@1952: ** table. The Expr.iColumn value is changed to the index of the column ds@1952: ** of the referenced table. The Expr.iColumn value for the special ds@1952: ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an ds@1952: ** alias for ROWID. ds@1952: ** ds@1952: ** Also resolve function names and check the functions for proper ds@1952: ** usage. Make sure all function names are recognized and all functions ds@1952: ** have the correct number of arguments. Leave an error message ds@1952: ** in pParse->zErrMsg if anything is amiss. Return the number of errors. ds@1952: ** ds@1952: ** If the expression contains aggregate functions then set the EP_Agg ds@1952: ** property on the expression. ds@1952: */ ds@1952: int sqlite3ExprResolveNames( ds@1952: NameContext *pNC, /* Namespace to resolve expressions in. */ ds@1952: Expr *pExpr /* The expression to be analyzed. */ ds@1952: ){ ds@1952: int savedHasAgg; ds@1952: if( pExpr==0 ) return 0; ds@1952: #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0 ds@1952: if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){ ds@1952: sqlite3ErrorMsg(pNC->pParse, ds@1952: "Expression tree is too large (maximum depth %d)", ds@1952: SQLITE_MAX_EXPR_DEPTH ds@1952: ); ds@1952: return 1; ds@1952: } ds@1952: pNC->pParse->nHeight += pExpr->nHeight; ds@1952: #endif ds@1952: savedHasAgg = pNC->hasAgg; ds@1952: pNC->hasAgg = 0; ds@1952: walkExprTree(pExpr, nameResolverStep, pNC); ds@1952: #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0 ds@1952: pNC->pParse->nHeight -= pExpr->nHeight; ds@1952: #endif ds@1952: if( pNC->nErr>0 ){ ds@1952: ExprSetProperty(pExpr, EP_Error); ds@1952: } ds@1952: if( pNC->hasAgg ){ ds@1952: ExprSetProperty(pExpr, EP_Agg); ds@1952: }else if( savedHasAgg ){ ds@1952: pNC->hasAgg = 1; ds@1952: } ds@1952: return ExprHasProperty(pExpr, EP_Error); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** A pointer instance of this structure is used to pass information ds@1952: ** through walkExprTree into codeSubqueryStep(). ds@1952: */ ds@1952: typedef struct QueryCoder QueryCoder; ds@1952: struct QueryCoder { ds@1952: Parse *pParse; /* The parsing context */ ds@1952: NameContext *pNC; /* Namespace of first enclosing query */ ds@1952: }; ds@1952: ds@1952: ds@1952: /* ds@1952: ** Generate code for scalar subqueries used as an expression ds@1952: ** and IN operators. Examples: ds@1952: ** ds@1952: ** (SELECT a FROM b) -- subquery ds@1952: ** EXISTS (SELECT a FROM b) -- EXISTS subquery ds@1952: ** x IN (4,5,11) -- IN operator with list on right-hand side ds@1952: ** x IN (SELECT a FROM b) -- IN operator with subquery on the right ds@1952: ** ds@1952: ** The pExpr parameter describes the expression that contains the IN ds@1952: ** operator or subquery. ds@1952: */ ds@1952: #ifndef SQLITE_OMIT_SUBQUERY ds@1952: void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ ds@1952: int testAddr = 0; /* One-time test address */ ds@1952: Vdbe *v = sqlite3GetVdbe(pParse); ds@1952: if( v==0 ) return; ds@1952: ds@1952: ds@1952: /* This code must be run in its entirety every time it is encountered ds@1952: ** if any of the following is true: ds@1952: ** ds@1952: ** * The right-hand side is a correlated subquery ds@1952: ** * The right-hand side is an expression list containing variables ds@1952: ** * We are inside a trigger ds@1952: ** ds@1952: ** If all of the above are false, then we can run this code just once ds@1952: ** save the results, and reuse the same result on subsequent invocations. ds@1952: */ ds@1952: if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ ds@1952: int mem = pParse->nMem++; ds@1952: sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0); ds@1952: testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0); ds@1952: assert( testAddr>0 || pParse->db->mallocFailed ); ds@1952: sqlite3VdbeAddOp(v, OP_MemInt, 1, mem); ds@1952: } ds@1952: ds@1952: switch( pExpr->op ){ ds@1952: case TK_IN: { ds@1952: char affinity; ds@1952: KeyInfo keyInfo; ds@1952: int addr; /* Address of OP_OpenEphemeral instruction */ ds@1952: ds@1952: affinity = sqlite3ExprAffinity(pExpr->pLeft); ds@1952: ds@1952: /* Whether this is an 'x IN(SELECT...)' or an 'x IN()' ds@1952: ** expression it is handled the same way. A virtual table is ds@1952: ** filled with single-field index keys representing the results ds@1952: ** from the SELECT or the . ds@1952: ** ds@1952: ** If the 'x' expression is a column value, or the SELECT... ds@1952: ** statement returns a column value, then the affinity of that ds@1952: ** column is used to build the index keys. If both 'x' and the ds@1952: ** SELECT... statement are columns, then numeric affinity is used ds@1952: ** if either column has NUMERIC or INTEGER affinity. If neither ds@1952: ** 'x' nor the SELECT... statement are columns, then numeric affinity ds@1952: ** is used. ds@1952: */ ds@1952: pExpr->iTable = pParse->nTab++; ds@1952: addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0); ds@1952: memset(&keyInfo, 0, sizeof(keyInfo)); ds@1952: keyInfo.nField = 1; ds@1952: sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); ds@1952: ds@1952: if( pExpr->pSelect ){ ds@1952: /* Case 1: expr IN (SELECT ...) ds@1952: ** ds@1952: ** Generate code to write the results of the select into the temporary ds@1952: ** table allocated and opened above. ds@1952: */ ds@1952: int iParm = pExpr->iTable + (((int)affinity)<<16); ds@1952: ExprList *pEList; ds@1952: assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); ds@1952: if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){ ds@1952: return; ds@1952: } ds@1952: pEList = pExpr->pSelect->pEList; ds@1952: if( pEList && pEList->nExpr>0 ){ ds@1952: keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, ds@1952: pEList->a[0].pExpr); ds@1952: } ds@1952: }else if( pExpr->pList ){ ds@1952: /* Case 2: expr IN (exprlist) ds@1952: ** ds@1952: ** For each expression, build an index key from the evaluation and ds@1952: ** store it in the temporary table. If is a column, then use ds@1952: ** that columns affinity when building index keys. If is not ds@1952: ** a column, use numeric affinity. ds@1952: */ ds@1952: int i; ds@1952: ExprList *pList = pExpr->pList; ds@1952: struct ExprList_item *pItem; ds@1952: ds@1952: if( !affinity ){ ds@1952: affinity = SQLITE_AFF_NONE; ds@1952: } ds@1952: keyInfo.aColl[0] = pExpr->pLeft->pColl; ds@1952: ds@1952: /* Loop through each expression in . */ ds@1952: for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ ds@1952: Expr *pE2 = pItem->pExpr; ds@1952: ds@1952: /* If the expression is not constant then we will need to ds@1952: ** disable the test that was generated above that makes sure ds@1952: ** this code only executes once. Because for a non-constant ds@1952: ** expression we need to rerun this code each time. ds@1952: */ ds@1952: if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){ ds@1952: sqlite3VdbeChangeToNoop(v, testAddr-1, 3); ds@1952: testAddr = 0; ds@1952: } ds@1952: ds@1952: /* Evaluate the expression and insert it into the temp table */ ds@1952: sqlite3ExprCode(pParse, pE2); ds@1952: sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); ds@1952: sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0); ds@1952: } ds@1952: } ds@1952: sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); ds@1952: break; ds@1952: } ds@1952: ds@1952: case TK_EXISTS: ds@1952: case TK_SELECT: { ds@1952: /* This has to be a scalar SELECT. Generate code to put the ds@1952: ** value of this select in a memory cell and record the number ds@1952: ** of the memory cell in iColumn. ds@1952: */ ds@1952: static const Token one = { (u8*)"1", 0, 1 }; ds@1952: Select *pSel; ds@1952: int iMem; ds@1952: int sop; ds@1952: ds@1952: pExpr->iColumn = iMem = pParse->nMem++; ds@1952: pSel = pExpr->pSelect; ds@1952: if( pExpr->op==TK_SELECT ){ ds@1952: sop = SRT_Mem; ds@1952: sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0); ds@1952: VdbeComment((v, "# Init subquery result")); ds@1952: }else{ ds@1952: sop = SRT_Exists; ds@1952: sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem); ds@1952: VdbeComment((v, "# Init EXISTS result")); ds@1952: } ds@1952: sqlite3ExprDelete(pSel->pLimit); ds@1952: pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one); ds@1952: if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){ ds@1952: return; ds@1952: } ds@1952: break; ds@1952: } ds@1952: } ds@1952: ds@1952: if( testAddr ){ ds@1952: sqlite3VdbeJumpHere(v, testAddr); ds@1952: } ds@1952: ds@1952: return; ds@1952: } ds@1952: #endif /* SQLITE_OMIT_SUBQUERY */ ds@1952: ds@1952: /* ds@1952: ** Generate an instruction that will put the integer describe by ds@1952: ** text z[0..n-1] on the stack. ds@1952: */ ds@1952: static void codeInteger(Vdbe *v, const char *z, int n){ ds@1952: assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); ds@1952: if( z ){ ds@1952: int i; ds@1952: if( sqlite3GetInt32(z, &i) ){ ds@1952: sqlite3VdbeAddOp(v, OP_Integer, i, 0); ds@1952: }else if( sqlite3FitsIn64Bits(z) ){ ds@1952: sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n); ds@1952: }else{ ds@1952: sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Generate code that will extract the iColumn-th column from ds@1952: ** table pTab and push that column value on the stack. There ds@1952: ** is an open cursor to pTab in iTable. If iColumn<0 then ds@1952: ** code is generated that extracts the rowid. ds@1952: */ ds@1952: void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){ ds@1952: if( iColumn<0 ){ ds@1952: int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid; ds@1952: sqlite3VdbeAddOp(v, op, iTable, 0); ds@1952: }else if( pTab==0 ){ ds@1952: sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn); ds@1952: }else{ ds@1952: int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; ds@1952: sqlite3VdbeAddOp(v, op, iTable, iColumn); ds@1952: sqlite3ColumnDefault(v, pTab, iColumn); ds@1952: #ifndef SQLITE_OMIT_FLOATING_POINT ds@1952: if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){ ds@1952: sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0); ds@1952: } ds@1952: #endif ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Generate code into the current Vdbe to evaluate the given ds@1952: ** expression and leave the result on the top of stack. ds@1952: ** ds@1952: ** This code depends on the fact that certain token values (ex: TK_EQ) ds@1952: ** are the same as opcode values (ex: OP_Eq) that implement the corresponding ds@1952: ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in ds@1952: ** the make process cause these values to align. Assert()s in the code ds@1952: ** below verify that the numbers are aligned correctly. ds@1952: */ ds@1952: void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ ds@1952: Vdbe *v = pParse->pVdbe; ds@1952: int op; ds@1952: int stackChng = 1; /* Amount of change to stack depth */ ds@1952: ds@1952: if( v==0 ) return; ds@1952: if( pExpr==0 ){ ds@1952: sqlite3VdbeAddOp(v, OP_Null, 0, 0); ds@1952: return; ds@1952: } ds@1952: op = pExpr->op; ds@1952: switch( op ){ ds@1952: case TK_AGG_COLUMN: { ds@1952: AggInfo *pAggInfo = pExpr->pAggInfo; ds@1952: struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; ds@1952: if( !pAggInfo->directMode ){ ds@1952: sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0); ds@1952: break; ds@1952: }else if( pAggInfo->useSortingIdx ){ ds@1952: sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx, ds@1952: pCol->iSorterColumn); ds@1952: break; ds@1952: } ds@1952: /* Otherwise, fall thru into the TK_COLUMN case */ ds@1952: } ds@1952: case TK_COLUMN: { ds@1952: if( pExpr->iTable<0 ){ ds@1952: /* This only happens when coding check constraints */ ds@1952: assert( pParse->ckOffset>0 ); ds@1952: sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1); ds@1952: }else{ ds@1952: sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable); ds@1952: } ds@1952: break; ds@1952: } ds@1952: case TK_INTEGER: { ds@1952: codeInteger(v, (char*)pExpr->token.z, pExpr->token.n); ds@1952: break; ds@1952: } ds@1952: case TK_FLOAT: ds@1952: case TK_STRING: { ds@1952: assert( TK_FLOAT==OP_Real ); ds@1952: assert( TK_STRING==OP_String8 ); ds@1952: sqlite3DequoteExpr(pParse->db, pExpr); ds@1952: sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n); ds@1952: break; ds@1952: } ds@1952: case TK_NULL: { ds@1952: sqlite3VdbeAddOp(v, OP_Null, 0, 0); ds@1952: break; ds@1952: } ds@1952: #ifndef SQLITE_OMIT_BLOB_LITERAL ds@1952: case TK_BLOB: { ds@1952: int n; ds@1952: const char *z; ds@1952: assert( TK_BLOB==OP_HexBlob ); ds@1952: n = pExpr->token.n - 3; ds@1952: z = (char*)pExpr->token.z + 2; ds@1952: assert( n>=0 ); ds@1952: if( n==0 ){ ds@1952: z = ""; ds@1952: } ds@1952: sqlite3VdbeOp3(v, op, 0, 0, z, n); ds@1952: break; ds@1952: } ds@1952: #endif ds@1952: case TK_VARIABLE: { ds@1952: sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); ds@1952: if( pExpr->token.n>1 ){ ds@1952: sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n); ds@1952: } ds@1952: break; ds@1952: } ds@1952: case TK_REGISTER: { ds@1952: sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0); ds@1952: break; ds@1952: } ds@1952: #ifndef SQLITE_OMIT_CAST ds@1952: case TK_CAST: { ds@1952: /* Expressions of the form: CAST(pLeft AS token) */ ds@1952: int aff, to_op; ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: aff = sqlite3AffinityType(&pExpr->token); ds@1952: to_op = aff - SQLITE_AFF_TEXT + OP_ToText; ds@1952: assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); ds@1952: assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); ds@1952: assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); ds@1952: assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); ds@1952: assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); ds@1952: sqlite3VdbeAddOp(v, to_op, 0, 0); ds@1952: stackChng = 0; ds@1952: break; ds@1952: } ds@1952: #endif /* SQLITE_OMIT_CAST */ ds@1952: case TK_LT: ds@1952: case TK_LE: ds@1952: case TK_GT: ds@1952: case TK_GE: ds@1952: case TK_NE: ds@1952: case TK_EQ: { ds@1952: assert( TK_LT==OP_Lt ); ds@1952: assert( TK_LE==OP_Le ); ds@1952: assert( TK_GT==OP_Gt ); ds@1952: assert( TK_GE==OP_Ge ); ds@1952: assert( TK_EQ==OP_Eq ); ds@1952: assert( TK_NE==OP_Ne ); ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: sqlite3ExprCode(pParse, pExpr->pRight); ds@1952: codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); ds@1952: stackChng = -1; ds@1952: break; ds@1952: } ds@1952: case TK_AND: ds@1952: case TK_OR: ds@1952: case TK_PLUS: ds@1952: case TK_STAR: ds@1952: case TK_MINUS: ds@1952: case TK_REM: ds@1952: case TK_BITAND: ds@1952: case TK_BITOR: ds@1952: case TK_SLASH: ds@1952: case TK_LSHIFT: ds@1952: case TK_RSHIFT: ds@1952: case TK_CONCAT: { ds@1952: assert( TK_AND==OP_And ); ds@1952: assert( TK_OR==OP_Or ); ds@1952: assert( TK_PLUS==OP_Add ); ds@1952: assert( TK_MINUS==OP_Subtract ); ds@1952: assert( TK_REM==OP_Remainder ); ds@1952: assert( TK_BITAND==OP_BitAnd ); ds@1952: assert( TK_BITOR==OP_BitOr ); ds@1952: assert( TK_SLASH==OP_Divide ); ds@1952: assert( TK_LSHIFT==OP_ShiftLeft ); ds@1952: assert( TK_RSHIFT==OP_ShiftRight ); ds@1952: assert( TK_CONCAT==OP_Concat ); ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: sqlite3ExprCode(pParse, pExpr->pRight); ds@1952: sqlite3VdbeAddOp(v, op, 0, 0); ds@1952: stackChng = -1; ds@1952: break; ds@1952: } ds@1952: case TK_UMINUS: { ds@1952: Expr *pLeft = pExpr->pLeft; ds@1952: assert( pLeft ); ds@1952: if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ ds@1952: Token *p = &pLeft->token; ds@1952: char *z = sqlite3MPrintf(pParse->db, "-%.*s", p->n, p->z); ds@1952: if( pLeft->op==TK_FLOAT ){ ds@1952: sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); ds@1952: }else{ ds@1952: codeInteger(v, z, p->n+1); ds@1952: } ds@1952: sqlite3_free(z); ds@1952: break; ds@1952: } ds@1952: /* Fall through into TK_NOT */ ds@1952: } ds@1952: case TK_BITNOT: ds@1952: case TK_NOT: { ds@1952: assert( TK_BITNOT==OP_BitNot ); ds@1952: assert( TK_NOT==OP_Not ); ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: sqlite3VdbeAddOp(v, op, 0, 0); ds@1952: stackChng = 0; ds@1952: break; ds@1952: } ds@1952: case TK_ISNULL: ds@1952: case TK_NOTNULL: { ds@1952: int dest; ds@1952: assert( TK_ISNULL==OP_IsNull ); ds@1952: assert( TK_NOTNULL==OP_NotNull ); ds@1952: sqlite3VdbeAddOp(v, OP_Integer, 1, 0); ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: dest = sqlite3VdbeCurrentAddr(v) + 2; ds@1952: sqlite3VdbeAddOp(v, op, 1, dest); ds@1952: sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); ds@1952: stackChng = 0; ds@1952: break; ds@1952: } ds@1952: case TK_AGG_FUNCTION: { ds@1952: AggInfo *pInfo = pExpr->pAggInfo; ds@1952: if( pInfo==0 ){ ds@1952: sqlite3ErrorMsg(pParse, "misuse of aggregate: %T", ds@1952: &pExpr->span); ds@1952: }else{ ds@1952: sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0); ds@1952: } ds@1952: break; ds@1952: } ds@1952: case TK_CONST_FUNC: ds@1952: case TK_FUNCTION: { ds@1952: ExprList *pList = pExpr->pList; ds@1952: int nExpr = pList ? pList->nExpr : 0; ds@1952: FuncDef *pDef; ds@1952: int nId; ds@1952: const char *zId; ds@1952: int constMask = 0; ds@1952: int i; ds@1952: sqlite3 *db = pParse->db; ds@1952: u8 enc = ENC(db); ds@1952: CollSeq *pColl = 0; ds@1952: ds@1952: zId = (char*)pExpr->token.z; ds@1952: nId = pExpr->token.n; ds@1952: pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); ds@1952: assert( pDef!=0 ); ds@1952: nExpr = sqlite3ExprCodeExprList(pParse, pList); ds@1952: #ifndef SQLITE_OMIT_VIRTUALTABLE ds@1952: /* Possibly overload the function if the first argument is ds@1952: ** a virtual table column. ds@1952: ** ds@1952: ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the ds@1952: ** second argument, not the first, as the argument to test to ds@1952: ** see if it is a column in a virtual table. This is done because ds@1952: ** the left operand of infix functions (the operand we want to ds@1952: ** control overloading) ends up as the second argument to the ds@1952: ** function. The expression "A glob B" is equivalent to ds@1952: ** "glob(B,A). We want to use the A in "A glob B" to test ds@1952: ** for function overloading. But we use the B term in "glob(B,A)". ds@1952: */ ds@1952: if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){ ds@1952: pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr); ds@1952: }else if( nExpr>0 ){ ds@1952: pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr); ds@1952: } ds@1952: #endif ds@1952: for(i=0; ia[i].pExpr) ){ ds@1952: constMask |= (1<needCollSeq && !pColl ){ ds@1952: pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); ds@1952: } ds@1952: } ds@1952: if( pDef->needCollSeq ){ ds@1952: if( !pColl ) pColl = pParse->db->pDfltColl; ds@1952: sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); ds@1952: } ds@1952: sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF); ds@1952: stackChng = 1-nExpr; ds@1952: break; ds@1952: } ds@1952: #ifndef SQLITE_OMIT_SUBQUERY ds@1952: case TK_EXISTS: ds@1952: case TK_SELECT: { ds@1952: if( pExpr->iColumn==0 ){ ds@1952: sqlite3CodeSubselect(pParse, pExpr); ds@1952: } ds@1952: sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); ds@1952: VdbeComment((v, "# load subquery result")); ds@1952: break; ds@1952: } ds@1952: case TK_IN: { ds@1952: int addr; ds@1952: char affinity; ds@1952: int ckOffset = pParse->ckOffset; ds@1952: sqlite3CodeSubselect(pParse, pExpr); ds@1952: ds@1952: /* Figure out the affinity to use to create a key from the results ds@1952: ** of the expression. affinityStr stores a static string suitable for ds@1952: ** P3 of OP_MakeRecord. ds@1952: */ ds@1952: affinity = comparisonAffinity(pExpr); ds@1952: ds@1952: sqlite3VdbeAddOp(v, OP_Integer, 1, 0); ds@1952: pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0); ds@1952: ds@1952: /* Code the from " IN (...)". The temporary table ds@1952: ** pExpr->iTable contains the values that make up the (...) set. ds@1952: */ ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: addr = sqlite3VdbeCurrentAddr(v); ds@1952: sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ ds@1952: sqlite3VdbeAddOp(v, OP_Pop, 2, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Null, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); ds@1952: sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */ ds@1952: sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); ds@1952: sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ ds@1952: ds@1952: break; ds@1952: } ds@1952: #endif ds@1952: case TK_BETWEEN: { ds@1952: Expr *pLeft = pExpr->pLeft; ds@1952: struct ExprList_item *pLItem = pExpr->pList->a; ds@1952: Expr *pRight = pLItem->pExpr; ds@1952: sqlite3ExprCode(pParse, pLeft); ds@1952: sqlite3VdbeAddOp(v, OP_Dup, 0, 0); ds@1952: sqlite3ExprCode(pParse, pRight); ds@1952: codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Pull, 1, 0); ds@1952: pLItem++; ds@1952: pRight = pLItem->pExpr; ds@1952: sqlite3ExprCode(pParse, pRight); ds@1952: codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_And, 0, 0); ds@1952: break; ds@1952: } ds@1952: case TK_UPLUS: { ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: stackChng = 0; ds@1952: break; ds@1952: } ds@1952: case TK_CASE: { ds@1952: int expr_end_label; ds@1952: int jumpInst; ds@1952: int nExpr; ds@1952: int i; ds@1952: ExprList *pEList; ds@1952: struct ExprList_item *aListelem; ds@1952: ds@1952: assert(pExpr->pList); ds@1952: assert((pExpr->pList->nExpr % 2) == 0); ds@1952: assert(pExpr->pList->nExpr > 0); ds@1952: pEList = pExpr->pList; ds@1952: aListelem = pEList->a; ds@1952: nExpr = pEList->nExpr; ds@1952: expr_end_label = sqlite3VdbeMakeLabel(v); ds@1952: if( pExpr->pLeft ){ ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: } ds@1952: for(i=0; ipLeft ){ ds@1952: sqlite3VdbeAddOp(v, OP_Dup, 1, 1); ds@1952: jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, ds@1952: OP_Ne, 0, 1); ds@1952: sqlite3VdbeAddOp(v, OP_Pop, 1, 0); ds@1952: }else{ ds@1952: jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); ds@1952: } ds@1952: sqlite3ExprCode(pParse, aListelem[i+1].pExpr); ds@1952: sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); ds@1952: sqlite3VdbeJumpHere(v, jumpInst); ds@1952: } ds@1952: if( pExpr->pLeft ){ ds@1952: sqlite3VdbeAddOp(v, OP_Pop, 1, 0); ds@1952: } ds@1952: if( pExpr->pRight ){ ds@1952: sqlite3ExprCode(pParse, pExpr->pRight); ds@1952: }else{ ds@1952: sqlite3VdbeAddOp(v, OP_Null, 0, 0); ds@1952: } ds@1952: sqlite3VdbeResolveLabel(v, expr_end_label); ds@1952: break; ds@1952: } ds@1952: #ifndef SQLITE_OMIT_TRIGGER ds@1952: case TK_RAISE: { ds@1952: if( !pParse->trigStack ){ ds@1952: sqlite3ErrorMsg(pParse, ds@1952: "RAISE() may only be used within a trigger-program"); ds@1952: return; ds@1952: } ds@1952: if( pExpr->iColumn!=OE_Ignore ){ ds@1952: assert( pExpr->iColumn==OE_Rollback || ds@1952: pExpr->iColumn == OE_Abort || ds@1952: pExpr->iColumn == OE_Fail ); ds@1952: sqlite3DequoteExpr(pParse->db, pExpr); ds@1952: sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, ds@1952: (char*)pExpr->token.z, pExpr->token.n); ds@1952: } else { ds@1952: assert( pExpr->iColumn == OE_Ignore ); ds@1952: sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); ds@1952: VdbeComment((v, "# raise(IGNORE)")); ds@1952: } ds@1952: stackChng = 0; ds@1952: break; ds@1952: } ds@1952: #endif ds@1952: } ds@1952: ds@1952: if( pParse->ckOffset ){ ds@1952: pParse->ckOffset += stackChng; ds@1952: assert( pParse->ckOffset ); ds@1952: } ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_TRIGGER ds@1952: /* ds@1952: ** Generate code that evalutes the given expression and leaves the result ds@1952: ** on the stack. See also sqlite3ExprCode(). ds@1952: ** ds@1952: ** This routine might also cache the result and modify the pExpr tree ds@1952: ** so that it will make use of the cached result on subsequent evaluations ds@1952: ** rather than evaluate the whole expression again. Trivial expressions are ds@1952: ** not cached. If the expression is cached, its result is stored in a ds@1952: ** memory location. ds@1952: */ ds@1952: void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){ ds@1952: Vdbe *v = pParse->pVdbe; ds@1952: int iMem; ds@1952: int addr1, addr2; ds@1952: if( v==0 ) return; ds@1952: addr1 = sqlite3VdbeCurrentAddr(v); ds@1952: sqlite3ExprCode(pParse, pExpr); ds@1952: addr2 = sqlite3VdbeCurrentAddr(v); ds@1952: if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){ ds@1952: iMem = pExpr->iTable = pParse->nMem++; ds@1952: sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); ds@1952: pExpr->op = TK_REGISTER; ds@1952: } ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Generate code that pushes the value of every element of the given ds@1952: ** expression list onto the stack. ds@1952: ** ds@1952: ** Return the number of elements pushed onto the stack. ds@1952: */ ds@1952: int sqlite3ExprCodeExprList( ds@1952: Parse *pParse, /* Parsing context */ ds@1952: ExprList *pList /* The expression list to be coded */ ds@1952: ){ ds@1952: struct ExprList_item *pItem; ds@1952: int i, n; ds@1952: if( pList==0 ) return 0; ds@1952: n = pList->nExpr; ds@1952: for(pItem=pList->a, i=n; i>0; i--, pItem++){ ds@1952: sqlite3ExprCode(pParse, pItem->pExpr); ds@1952: } ds@1952: return n; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Generate code for a boolean expression such that a jump is made ds@1952: ** to the label "dest" if the expression is true but execution ds@1952: ** continues straight thru if the expression is false. ds@1952: ** ds@1952: ** If the expression evaluates to NULL (neither true nor false), then ds@1952: ** take the jump if the jumpIfNull flag is true. ds@1952: ** ds@1952: ** This code depends on the fact that certain token values (ex: TK_EQ) ds@1952: ** are the same as opcode values (ex: OP_Eq) that implement the corresponding ds@1952: ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in ds@1952: ** the make process cause these values to align. Assert()s in the code ds@1952: ** below verify that the numbers are aligned correctly. ds@1952: */ ds@1952: void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ ds@1952: Vdbe *v = pParse->pVdbe; ds@1952: int op = 0; ds@1952: int ckOffset = pParse->ckOffset; ds@1952: if( v==0 || pExpr==0 ) return; ds@1952: op = pExpr->op; ds@1952: switch( op ){ ds@1952: case TK_AND: { ds@1952: int d2 = sqlite3VdbeMakeLabel(v); ds@1952: sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); ds@1952: sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); ds@1952: sqlite3VdbeResolveLabel(v, d2); ds@1952: break; ds@1952: } ds@1952: case TK_OR: { ds@1952: sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); ds@1952: sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); ds@1952: break; ds@1952: } ds@1952: case TK_NOT: { ds@1952: sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); ds@1952: break; ds@1952: } ds@1952: case TK_LT: ds@1952: case TK_LE: ds@1952: case TK_GT: ds@1952: case TK_GE: ds@1952: case TK_NE: ds@1952: case TK_EQ: { ds@1952: assert( TK_LT==OP_Lt ); ds@1952: assert( TK_LE==OP_Le ); ds@1952: assert( TK_GT==OP_Gt ); ds@1952: assert( TK_GE==OP_Ge ); ds@1952: assert( TK_EQ==OP_Eq ); ds@1952: assert( TK_NE==OP_Ne ); ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: sqlite3ExprCode(pParse, pExpr->pRight); ds@1952: codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); ds@1952: break; ds@1952: } ds@1952: case TK_ISNULL: ds@1952: case TK_NOTNULL: { ds@1952: assert( TK_ISNULL==OP_IsNull ); ds@1952: assert( TK_NOTNULL==OP_NotNull ); ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: sqlite3VdbeAddOp(v, op, 1, dest); ds@1952: break; ds@1952: } ds@1952: case TK_BETWEEN: { ds@1952: /* The expression "x BETWEEN y AND z" is implemented as: ds@1952: ** ds@1952: ** 1 IF (x < y) GOTO 3 ds@1952: ** 2 IF (x <= z) GOTO ds@1952: ** 3 ... ds@1952: */ ds@1952: int addr; ds@1952: Expr *pLeft = pExpr->pLeft; ds@1952: Expr *pRight = pExpr->pList->a[0].pExpr; ds@1952: sqlite3ExprCode(pParse, pLeft); ds@1952: sqlite3VdbeAddOp(v, OP_Dup, 0, 0); ds@1952: sqlite3ExprCode(pParse, pRight); ds@1952: addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); ds@1952: ds@1952: pRight = pExpr->pList->a[1].pExpr; ds@1952: sqlite3ExprCode(pParse, pRight); ds@1952: codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); ds@1952: ds@1952: sqlite3VdbeAddOp(v, OP_Integer, 0, 0); ds@1952: sqlite3VdbeJumpHere(v, addr); ds@1952: sqlite3VdbeAddOp(v, OP_Pop, 1, 0); ds@1952: break; ds@1952: } ds@1952: default: { ds@1952: sqlite3ExprCode(pParse, pExpr); ds@1952: sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); ds@1952: break; ds@1952: } ds@1952: } ds@1952: pParse->ckOffset = ckOffset; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Generate code for a boolean expression such that a jump is made ds@1952: ** to the label "dest" if the expression is false but execution ds@1952: ** continues straight thru if the expression is true. ds@1952: ** ds@1952: ** If the expression evaluates to NULL (neither true nor false) then ds@1952: ** jump if jumpIfNull is true or fall through if jumpIfNull is false. ds@1952: */ ds@1952: void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ ds@1952: Vdbe *v = pParse->pVdbe; ds@1952: int op = 0; ds@1952: int ckOffset = pParse->ckOffset; ds@1952: if( v==0 || pExpr==0 ) return; ds@1952: ds@1952: /* The value of pExpr->op and op are related as follows: ds@1952: ** ds@1952: ** pExpr->op op ds@1952: ** --------- ---------- ds@1952: ** TK_ISNULL OP_NotNull ds@1952: ** TK_NOTNULL OP_IsNull ds@1952: ** TK_NE OP_Eq ds@1952: ** TK_EQ OP_Ne ds@1952: ** TK_GT OP_Le ds@1952: ** TK_LE OP_Gt ds@1952: ** TK_GE OP_Lt ds@1952: ** TK_LT OP_Ge ds@1952: ** ds@1952: ** For other values of pExpr->op, op is undefined and unused. ds@1952: ** The value of TK_ and OP_ constants are arranged such that we ds@1952: ** can compute the mapping above using the following expression. ds@1952: ** Assert()s verify that the computation is correct. ds@1952: */ ds@1952: op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); ds@1952: ds@1952: /* Verify correct alignment of TK_ and OP_ constants ds@1952: */ ds@1952: assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); ds@1952: assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); ds@1952: assert( pExpr->op!=TK_NE || op==OP_Eq ); ds@1952: assert( pExpr->op!=TK_EQ || op==OP_Ne ); ds@1952: assert( pExpr->op!=TK_LT || op==OP_Ge ); ds@1952: assert( pExpr->op!=TK_LE || op==OP_Gt ); ds@1952: assert( pExpr->op!=TK_GT || op==OP_Le ); ds@1952: assert( pExpr->op!=TK_GE || op==OP_Lt ); ds@1952: ds@1952: switch( pExpr->op ){ ds@1952: case TK_AND: { ds@1952: sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); ds@1952: sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); ds@1952: break; ds@1952: } ds@1952: case TK_OR: { ds@1952: int d2 = sqlite3VdbeMakeLabel(v); ds@1952: sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); ds@1952: sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); ds@1952: sqlite3VdbeResolveLabel(v, d2); ds@1952: break; ds@1952: } ds@1952: case TK_NOT: { ds@1952: sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); ds@1952: break; ds@1952: } ds@1952: case TK_LT: ds@1952: case TK_LE: ds@1952: case TK_GT: ds@1952: case TK_GE: ds@1952: case TK_NE: ds@1952: case TK_EQ: { ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: sqlite3ExprCode(pParse, pExpr->pRight); ds@1952: codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); ds@1952: break; ds@1952: } ds@1952: case TK_ISNULL: ds@1952: case TK_NOTNULL: { ds@1952: sqlite3ExprCode(pParse, pExpr->pLeft); ds@1952: sqlite3VdbeAddOp(v, op, 1, dest); ds@1952: break; ds@1952: } ds@1952: case TK_BETWEEN: { ds@1952: /* The expression is "x BETWEEN y AND z". It is implemented as: ds@1952: ** ds@1952: ** 1 IF (x >= y) GOTO 3 ds@1952: ** 2 GOTO ds@1952: ** 3 IF (x > z) GOTO ds@1952: */ ds@1952: int addr; ds@1952: Expr *pLeft = pExpr->pLeft; ds@1952: Expr *pRight = pExpr->pList->a[0].pExpr; ds@1952: sqlite3ExprCode(pParse, pLeft); ds@1952: sqlite3VdbeAddOp(v, OP_Dup, 0, 0); ds@1952: sqlite3ExprCode(pParse, pRight); ds@1952: addr = sqlite3VdbeCurrentAddr(v); ds@1952: codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); ds@1952: ds@1952: sqlite3VdbeAddOp(v, OP_Pop, 1, 0); ds@1952: sqlite3VdbeAddOp(v, OP_Goto, 0, dest); ds@1952: pRight = pExpr->pList->a[1].pExpr; ds@1952: sqlite3ExprCode(pParse, pRight); ds@1952: codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); ds@1952: break; ds@1952: } ds@1952: default: { ds@1952: sqlite3ExprCode(pParse, pExpr); ds@1952: sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); ds@1952: break; ds@1952: } ds@1952: } ds@1952: pParse->ckOffset = ckOffset; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Do a deep comparison of two expression trees. Return TRUE (non-zero) ds@1952: ** if they are identical and return FALSE if they differ in any way. ds@1952: ** ds@1952: ** Sometimes this routine will return FALSE even if the two expressions ds@1952: ** really are equivalent. If we cannot prove that the expressions are ds@1952: ** identical, we return FALSE just to be safe. So if this routine ds@1952: ** returns false, then you do not really know for certain if the two ds@1952: ** expressions are the same. But if you get a TRUE return, then you ds@1952: ** can be sure the expressions are the same. In the places where ds@1952: ** this routine is used, it does not hurt to get an extra FALSE - that ds@1952: ** just might result in some slightly slower code. But returning ds@1952: ** an incorrect TRUE could lead to a malfunction. ds@1952: */ ds@1952: int sqlite3ExprCompare(Expr *pA, Expr *pB){ ds@1952: int i; ds@1952: if( pA==0||pB==0 ){ ds@1952: return pB==pA; ds@1952: } ds@1952: if( pA->op!=pB->op ) return 0; ds@1952: if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; ds@1952: if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; ds@1952: if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; ds@1952: if( pA->pList ){ ds@1952: if( pB->pList==0 ) return 0; ds@1952: if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; ds@1952: for(i=0; ipList->nExpr; i++){ ds@1952: if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ ds@1952: return 0; ds@1952: } ds@1952: } ds@1952: }else if( pB->pList ){ ds@1952: return 0; ds@1952: } ds@1952: if( pA->pSelect || pB->pSelect ) return 0; ds@1952: if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; ds@1952: if( pA->op!=TK_COLUMN && pA->token.z ){ ds@1952: if( pB->token.z==0 ) return 0; ds@1952: if( pB->token.n!=pA->token.n ) return 0; ds@1952: if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){ ds@1952: return 0; ds@1952: } ds@1952: } ds@1952: return 1; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Add a new element to the pAggInfo->aCol[] array. Return the index of ds@1952: ** the new element. Return a negative number if malloc fails. ds@1952: */ ds@1952: static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ ds@1952: int i; ds@1952: pInfo->aCol = sqlite3ArrayAllocate( ds@1952: db, ds@1952: pInfo->aCol, ds@1952: sizeof(pInfo->aCol[0]), ds@1952: 3, ds@1952: &pInfo->nColumn, ds@1952: &pInfo->nColumnAlloc, ds@1952: &i ds@1952: ); ds@1952: return i; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Add a new element to the pAggInfo->aFunc[] array. Return the index of ds@1952: ** the new element. Return a negative number if malloc fails. ds@1952: */ ds@1952: static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ ds@1952: int i; ds@1952: pInfo->aFunc = sqlite3ArrayAllocate( ds@1952: db, ds@1952: pInfo->aFunc, ds@1952: sizeof(pInfo->aFunc[0]), ds@1952: 3, ds@1952: &pInfo->nFunc, ds@1952: &pInfo->nFuncAlloc, ds@1952: &i ds@1952: ); ds@1952: return i; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This is an xFunc for walkExprTree() used to implement ds@1952: ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates ds@1952: ** for additional information. ds@1952: ** ds@1952: ** This routine analyzes the aggregate function at pExpr. ds@1952: */ ds@1952: static int analyzeAggregate(void *pArg, Expr *pExpr){ ds@1952: int i; ds@1952: NameContext *pNC = (NameContext *)pArg; ds@1952: Parse *pParse = pNC->pParse; ds@1952: SrcList *pSrcList = pNC->pSrcList; ds@1952: AggInfo *pAggInfo = pNC->pAggInfo; ds@1952: ds@1952: switch( pExpr->op ){ ds@1952: case TK_AGG_COLUMN: ds@1952: case TK_COLUMN: { ds@1952: /* Check to see if the column is in one of the tables in the FROM ds@1952: ** clause of the aggregate query */ ds@1952: if( pSrcList ){ ds@1952: struct SrcList_item *pItem = pSrcList->a; ds@1952: for(i=0; inSrc; i++, pItem++){ ds@1952: struct AggInfo_col *pCol; ds@1952: if( pExpr->iTable==pItem->iCursor ){ ds@1952: /* If we reach this point, it means that pExpr refers to a table ds@1952: ** that is in the FROM clause of the aggregate query. ds@1952: ** ds@1952: ** Make an entry for the column in pAggInfo->aCol[] if there ds@1952: ** is not an entry there already. ds@1952: */ ds@1952: int k; ds@1952: pCol = pAggInfo->aCol; ds@1952: for(k=0; knColumn; k++, pCol++){ ds@1952: if( pCol->iTable==pExpr->iTable && ds@1952: pCol->iColumn==pExpr->iColumn ){ ds@1952: break; ds@1952: } ds@1952: } ds@1952: if( (k>=pAggInfo->nColumn) ds@1952: && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 ds@1952: ){ ds@1952: pCol = &pAggInfo->aCol[k]; ds@1952: pCol->pTab = pExpr->pTab; ds@1952: pCol->iTable = pExpr->iTable; ds@1952: pCol->iColumn = pExpr->iColumn; ds@1952: pCol->iMem = pParse->nMem++; ds@1952: pCol->iSorterColumn = -1; ds@1952: pCol->pExpr = pExpr; ds@1952: if( pAggInfo->pGroupBy ){ ds@1952: int j, n; ds@1952: ExprList *pGB = pAggInfo->pGroupBy; ds@1952: struct ExprList_item *pTerm = pGB->a; ds@1952: n = pGB->nExpr; ds@1952: for(j=0; jpExpr; ds@1952: if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && ds@1952: pE->iColumn==pExpr->iColumn ){ ds@1952: pCol->iSorterColumn = j; ds@1952: break; ds@1952: } ds@1952: } ds@1952: } ds@1952: if( pCol->iSorterColumn<0 ){ ds@1952: pCol->iSorterColumn = pAggInfo->nSortingColumn++; ds@1952: } ds@1952: } ds@1952: /* There is now an entry for pExpr in pAggInfo->aCol[] (either ds@1952: ** because it was there before or because we just created it). ds@1952: ** Convert the pExpr to be a TK_AGG_COLUMN referring to that ds@1952: ** pAggInfo->aCol[] entry. ds@1952: */ ds@1952: pExpr->pAggInfo = pAggInfo; ds@1952: pExpr->op = TK_AGG_COLUMN; ds@1952: pExpr->iAgg = k; ds@1952: break; ds@1952: } /* endif pExpr->iTable==pItem->iCursor */ ds@1952: } /* end loop over pSrcList */ ds@1952: } ds@1952: return 1; ds@1952: } ds@1952: case TK_AGG_FUNCTION: { ds@1952: /* The pNC->nDepth==0 test causes aggregate functions in subqueries ds@1952: ** to be ignored */ ds@1952: if( pNC->nDepth==0 ){ ds@1952: /* Check to see if pExpr is a duplicate of another aggregate ds@1952: ** function that is already in the pAggInfo structure ds@1952: */ ds@1952: struct AggInfo_func *pItem = pAggInfo->aFunc; ds@1952: for(i=0; inFunc; i++, pItem++){ ds@1952: if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ ds@1952: break; ds@1952: } ds@1952: } ds@1952: if( i>=pAggInfo->nFunc ){ ds@1952: /* pExpr is original. Make a new entry in pAggInfo->aFunc[] ds@1952: */ ds@1952: u8 enc = ENC(pParse->db); ds@1952: i = addAggInfoFunc(pParse->db, pAggInfo); ds@1952: if( i>=0 ){ ds@1952: pItem = &pAggInfo->aFunc[i]; ds@1952: pItem->pExpr = pExpr; ds@1952: pItem->iMem = pParse->nMem++; ds@1952: pItem->pFunc = sqlite3FindFunction(pParse->db, ds@1952: (char*)pExpr->token.z, pExpr->token.n, ds@1952: pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); ds@1952: if( pExpr->flags & EP_Distinct ){ ds@1952: pItem->iDistinct = pParse->nTab++; ds@1952: }else{ ds@1952: pItem->iDistinct = -1; ds@1952: } ds@1952: } ds@1952: } ds@1952: /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry ds@1952: */ ds@1952: pExpr->iAgg = i; ds@1952: pExpr->pAggInfo = pAggInfo; ds@1952: return 1; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* Recursively walk subqueries looking for TK_COLUMN nodes that need ds@1952: ** to be changed to TK_AGG_COLUMN. But increment nDepth so that ds@1952: ** TK_AGG_FUNCTION nodes in subqueries will be unchanged. ds@1952: */ ds@1952: if( pExpr->pSelect ){ ds@1952: pNC->nDepth++; ds@1952: walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC); ds@1952: pNC->nDepth--; ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Analyze the given expression looking for aggregate functions and ds@1952: ** for variables that need to be added to the pParse->aAgg[] array. ds@1952: ** Make additional entries to the pParse->aAgg[] array as necessary. ds@1952: ** ds@1952: ** This routine should only be called after the expression has been ds@1952: ** analyzed by sqlite3ExprResolveNames(). ds@1952: ** ds@1952: ** If errors are seen, leave an error message in zErrMsg and return ds@1952: ** the number of errors. ds@1952: */ ds@1952: int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ ds@1952: int nErr = pNC->pParse->nErr; ds@1952: walkExprTree(pExpr, analyzeAggregate, pNC); ds@1952: return pNC->pParse->nErr - nErr; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Call sqlite3ExprAnalyzeAggregates() for every expression in an ds@1952: ** expression list. Return the number of errors. ds@1952: ** ds@1952: ** If an error is found, the analysis is cut short. ds@1952: */ ds@1952: int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ ds@1952: struct ExprList_item *pItem; ds@1952: int i; ds@1952: int nErr = 0; ds@1952: if( pList ){ ds@1952: for(pItem=pList->a, i=0; nErr==0 && inExpr; i++, pItem++){ ds@1952: nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); ds@1952: } ds@1952: } ds@1952: return nErr; ds@1952: }